ws2_32: Use server-side async I/O in AcceptEx().
[wine/zf.git] / dlls / ws2_32 / tests / sock.c
blob7c36456c91ff80bf8bc68d26c4444bc09324f821
1 /*
2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
7 * Copyright 2017 Dmitry Timoshkov
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <ntstatus.h>
25 #define WIN32_NO_STATUS
26 #include <winsock2.h>
27 #include <windows.h>
28 #include <winternl.h>
29 #include <ws2tcpip.h>
30 #include <ws2spi.h>
31 #include <wsipx.h>
32 #include <wsnwlink.h>
33 #include <mswsock.h>
34 #include <mstcpip.h>
35 #include <iphlpapi.h>
36 #include <stdio.h>
37 #include "wine/test.h"
39 #define MAX_CLIENTS 4 /* Max number of clients */
40 #define FIRST_CHAR 'A' /* First character in transferred pattern */
41 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
42 #define BIND_TRIES 6 /* Number of bind() attempts */
43 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
44 after server initialization, if something hangs */
46 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
48 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
49 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
51 #define SERVERIP "127.0.0.1" /* IP to bind to */
52 #define SERVERPORT 9374 /* Port number to bind to */
54 #define wsa_ok(op, cond, msg) \
55 do { \
56 int tmp, err = 0; \
57 tmp = op; \
58 if ( !(cond tmp) ) err = WSAGetLastError(); \
59 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
60 } while (0);
62 #define make_keepalive(k, enable, time, interval) \
63 k.onoff = enable; \
64 k.keepalivetime = time; \
65 k.keepaliveinterval = interval;
67 /* Function pointers */
68 static void (WINAPI *pFreeAddrInfoExW)(ADDRINFOEXW *ai);
69 static int (WINAPI *pGetAddrInfoExW)(const WCHAR *name, const WCHAR *servname, DWORD namespace,
70 GUID *namespace_id, const ADDRINFOEXW *hints, ADDRINFOEXW **result,
71 struct timeval *timeout, OVERLAPPED *overlapped,
72 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine, HANDLE *handle);
73 static int (WINAPI *pGetAddrInfoExOverlappedResult)(OVERLAPPED *overlapped);
74 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG);
75 static PCWSTR(WINAPI *pInetNtopW)(INT,LPVOID,LPWSTR,ULONG);
76 static int (WINAPI *pInetPtonA)(INT,LPCSTR,LPVOID);
77 static int (WINAPI *pInetPtonW)(INT,LPWSTR,LPVOID);
78 static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT);
79 static int (WINAPI *pWSCGetProviderInfo)(LPGUID,WSC_PROVIDER_INFO_TYPE,PBYTE,size_t*,DWORD,LPINT);
81 /* Function pointers from ntdll */
82 static DWORD (WINAPI *pNtClose)(HANDLE);
84 /**************** Structs and typedefs ***************/
86 typedef struct thread_info
88 HANDLE thread;
89 DWORD id;
90 } thread_info;
92 /* Information in the server about open client connections */
93 typedef struct sock_info
95 SOCKET s;
96 struct sockaddr_in addr;
97 struct sockaddr_in peer;
98 char *buf;
99 int n_recvd;
100 int n_sent;
101 } sock_info;
103 /* Test parameters for both server & client */
104 typedef struct test_params
106 int sock_type;
107 int sock_prot;
108 const char *inet_addr;
109 short inet_port;
110 int chunk_size;
111 int n_chunks;
112 int n_clients;
113 } test_params;
115 /* server-specific test parameters */
116 typedef struct server_params
118 test_params *general;
119 DWORD sock_flags;
120 int buflen;
121 } server_params;
123 /* client-specific test parameters */
124 typedef struct client_params
126 test_params *general;
127 DWORD sock_flags;
128 int buflen;
129 } client_params;
131 /* This type combines all information for setting up a test scenario */
132 typedef struct test_setup
134 test_params general;
135 LPVOID srv;
136 server_params srv_params;
137 LPVOID clt;
138 client_params clt_params;
139 } test_setup;
141 /* Thread local storage for server */
142 typedef struct server_memory
144 SOCKET s;
145 struct sockaddr_in addr;
146 sock_info sock[MAX_CLIENTS];
147 } server_memory;
149 /* Thread local storage for client */
150 typedef struct client_memory
152 SOCKET s;
153 struct sockaddr_in addr;
154 char *send_buf;
155 char *recv_buf;
156 } client_memory;
158 /* SelectReadThread thread parameters */
159 typedef struct select_thread_params
161 SOCKET s;
162 BOOL ReadKilled;
163 } select_thread_params;
165 /* Tests used in both getaddrinfo and GetAddrInfoW */
166 static const struct addr_hint_tests
168 int family, socktype, protocol;
169 DWORD error;
170 } hinttests[] = {
171 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0 },
172 {AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP, 0 },
173 {AF_UNSPEC, SOCK_STREAM, IPPROTO_IPV6,0 },
174 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_TCP, 0 },
175 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0 },
176 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_IPV6,0 },
177 {AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 },
178 {AF_INET, SOCK_STREAM, IPPROTO_UDP, 0 },
179 {AF_INET, SOCK_STREAM, IPPROTO_IPV6,0 },
180 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, 0 },
181 {AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0 },
182 {AF_INET, SOCK_DGRAM, IPPROTO_IPV6,0 },
183 {AF_UNSPEC, 0, IPPROTO_TCP, 0 },
184 {AF_UNSPEC, 0, IPPROTO_UDP, 0 },
185 {AF_UNSPEC, 0, IPPROTO_IPV6,0 },
186 {AF_UNSPEC, SOCK_STREAM, 0, 0 },
187 {AF_UNSPEC, SOCK_DGRAM, 0, 0 },
188 {AF_INET, 0, IPPROTO_TCP, 0 },
189 {AF_INET, 0, IPPROTO_UDP, 0 },
190 {AF_INET, 0, IPPROTO_IPV6,0 },
191 {AF_INET, SOCK_STREAM, 0, 0 },
192 {AF_INET, SOCK_DGRAM, 0, 0 },
193 {AF_UNSPEC, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
194 {AF_UNSPEC, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
195 {AF_UNSPEC, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
196 {AF_INET, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
197 {AF_INET, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
198 {AF_INET, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
199 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
200 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
201 {AF_INET, SOCK_DGRAM, 999, 0 },
202 {AF_INET, SOCK_DGRAM, 999, 0 },
205 /**************** Static variables ***************/
207 static DWORD tls; /* Thread local storage index */
208 static HANDLE thread[1+MAX_CLIENTS];
209 static DWORD thread_id[1+MAX_CLIENTS];
210 static HANDLE server_ready;
211 static HANDLE client_ready[MAX_CLIENTS];
212 static int client_id;
214 /**************** General utility functions ***************/
216 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len);
217 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock);
219 static void tcp_socketpair(SOCKET *src, SOCKET *dst)
221 SOCKET server = INVALID_SOCKET;
222 struct sockaddr_in addr;
223 int len;
224 int ret;
226 *src = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
227 ok(*src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
229 server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
230 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
232 memset(&addr, 0, sizeof(addr));
233 addr.sin_family = AF_INET;
234 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
235 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
236 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
238 len = sizeof(addr);
239 ret = getsockname(server, (struct sockaddr*)&addr, &len);
240 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
242 ret = listen(server, 1);
243 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
245 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
246 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
248 len = sizeof(addr);
249 *dst = accept(server, (struct sockaddr*)&addr, &len);
250 ok(*dst != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
252 closesocket(server);
255 static void tcp_socketpair_ovl(SOCKET *src, SOCKET *dst)
257 SOCKET server = INVALID_SOCKET;
258 struct sockaddr_in addr;
259 int len, ret;
261 *src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
262 ok(*src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
264 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
265 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
267 memset(&addr, 0, sizeof(addr));
268 addr.sin_family = AF_INET;
269 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
270 ret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
271 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
273 len = sizeof(addr);
274 ret = getsockname(server, (struct sockaddr *)&addr, &len);
275 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
277 ret = listen(server, 1);
278 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
280 ret = connect(*src, (struct sockaddr *)&addr, sizeof(addr));
281 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
283 len = sizeof(addr);
284 *dst = accept(server, (struct sockaddr *)&addr, &len);
285 ok(*dst != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
287 closesocket(server);
290 static void set_so_opentype ( BOOL overlapped )
292 int optval = !overlapped, newval, len = sizeof (int);
294 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
295 (LPVOID) &optval, sizeof (optval) ) == 0,
296 "setting SO_OPENTYPE failed\n" );
297 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
298 (LPVOID) &newval, &len ) == 0,
299 "getting SO_OPENTYPE failed\n" );
300 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
303 static int set_blocking ( SOCKET s, BOOL blocking )
305 u_long val = !blocking;
306 return ioctlsocket ( s, FIONBIO, &val );
309 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
311 char c, *p;
312 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
313 memset ( p, c, chunk_size );
316 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
318 char c, *p;
319 int i;
320 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
322 for ( i = 0; i < chunk_size; i++ )
323 if ( p[i] != c ) return i;
325 return -1;
329 * This routine is called when a client / server does not expect any more data,
330 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
332 static void read_zero_bytes ( SOCKET s )
334 char buf[256];
335 int tmp, n = 0;
336 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
337 n += tmp;
338 ok ( n <= 0, "garbage data received: %d bytes\n", n );
341 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen )
343 char* last = buf + buflen, *p;
344 int n = 1;
345 for ( p = buf; n > 0 && p < last; )
347 n = send ( s, p, min ( sendlen, last - p ), flags );
348 if (n > 0) p += n;
350 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
351 return p - buf;
354 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen )
356 char* last = buf + buflen, *p;
357 int n = 1;
358 for ( p = buf; n > 0 && p < last; )
360 n = recv ( s, p, min ( recvlen, last - p ), flags );
361 if (n > 0) p += n;
363 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
364 return p - buf;
367 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen )
369 char* last = buf + buflen, *p;
370 int n = 1;
371 for ( p = buf; n > 0 && p < last; )
373 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen );
374 if (n > 0) p += n;
376 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
377 return p - buf;
381 * Call this routine right after thread startup.
382 * SO_OPENTYPE must by 0, regardless what the server did.
384 static void check_so_opentype (void)
386 int tmp = 1, len;
387 len = sizeof (tmp);
388 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
389 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
392 static void compare_addrinfo (ADDRINFO *a, ADDRINFO *b)
394 for (; a && b ; a = a->ai_next, b = b->ai_next)
396 ok(a->ai_flags == b->ai_flags,
397 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
398 ok(a->ai_family == b->ai_family,
399 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
400 ok(a->ai_socktype == b->ai_socktype,
401 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
402 ok(a->ai_protocol == b->ai_protocol,
403 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
404 ok(a->ai_addrlen == b->ai_addrlen,
405 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
406 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
407 "Wrong address data\n");
408 if (a->ai_canonname && b->ai_canonname)
410 ok(!strcmp(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
411 a->ai_canonname, b->ai_canonname);
413 else
414 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
415 a->ai_canonname, b->ai_canonname);
417 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
420 static void compare_addrinfow (ADDRINFOW *a, ADDRINFOW *b)
422 for (; a && b ; a = a->ai_next, b = b->ai_next)
424 ok(a->ai_flags == b->ai_flags,
425 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
426 ok(a->ai_family == b->ai_family,
427 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
428 ok(a->ai_socktype == b->ai_socktype,
429 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
430 ok(a->ai_protocol == b->ai_protocol,
431 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
432 ok(a->ai_addrlen == b->ai_addrlen,
433 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
434 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
435 "Wrong address data\n");
436 if (a->ai_canonname && b->ai_canonname)
438 ok(!lstrcmpW(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
439 wine_dbgstr_w(a->ai_canonname), wine_dbgstr_w(b->ai_canonname));
441 else
442 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
443 a->ai_canonname, b->ai_canonname);
445 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
448 /**************** Server utility functions ***************/
451 * Even if we have closed our server socket cleanly,
452 * the OS may mark the address "in use" for some time -
453 * this happens with native Linux apps, too.
455 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
457 int err, wsaerr = 0, n_try = BIND_TRIES;
459 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
460 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
461 n_try-- >= 0)
463 trace ( "address in use, waiting ...\n" );
464 Sleep ( 1000 * BIND_SLEEP );
466 ok ( err == 0, "failed to bind: %d\n", wsaerr );
469 static void server_start ( server_params *par )
471 int i;
472 test_params *gen = par->general;
473 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
475 TlsSetValue ( tls, mem );
476 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
477 NULL, 0, par->sock_flags );
478 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
480 mem->addr.sin_family = AF_INET;
481 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
482 mem->addr.sin_port = htons ( gen->inet_port );
484 for (i = 0; i < MAX_CLIENTS; i++)
486 mem->sock[i].s = INVALID_SOCKET;
487 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
488 mem->sock[i].n_recvd = 0;
489 mem->sock[i].n_sent = 0;
492 if ( gen->sock_type == SOCK_STREAM )
493 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
496 static void server_stop (void)
498 int i;
499 server_memory *mem = TlsGetValue ( tls );
501 for (i = 0; i < MAX_CLIENTS; i++ )
503 LocalFree ( mem->sock[i].buf );
504 if ( mem->sock[i].s != INVALID_SOCKET )
505 closesocket ( mem->sock[i].s );
507 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
508 LocalFree ( mem );
509 ExitThread ( GetCurrentThreadId () );
512 /**************** Client utilitiy functions ***************/
514 static void client_start ( client_params *par )
516 test_params *gen = par->general;
517 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
519 TlsSetValue ( tls, mem );
521 WaitForSingleObject ( server_ready, INFINITE );
523 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
524 NULL, 0, par->sock_flags );
526 mem->addr.sin_family = AF_INET;
527 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
528 mem->addr.sin_port = htons ( gen->inet_port );
530 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
532 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
533 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
534 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
536 SetEvent ( client_ready[client_id] );
537 /* Wait for the other clients to come up */
538 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
541 static void client_stop (void)
543 client_memory *mem = TlsGetValue ( tls );
544 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
545 LocalFree ( mem->send_buf );
546 LocalFree ( mem );
547 ExitThread(0);
550 /**************** Servers ***************/
553 * simple_server: A very basic server doing synchronous IO.
555 static VOID WINAPI simple_server ( server_params *par )
557 test_params *gen = par->general;
558 server_memory *mem;
559 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
560 id = GetCurrentThreadId();
562 set_so_opentype ( FALSE ); /* non-overlapped */
563 server_start ( par );
564 mem = TlsGetValue ( tls );
566 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
567 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
569 SetEvent ( server_ready ); /* notify clients */
571 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
573 /* accept a single connection */
574 tmp = sizeof ( mem->sock[0].peer );
575 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
576 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
578 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
579 "simple_server (%x): strange peer address\n", id );
581 /* Receive data & check it */
582 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
583 ok ( n_recvd == n_expected,
584 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
585 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
586 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
588 /* Echo data back */
589 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
590 ok ( n_sent == n_expected,
591 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
593 /* cleanup */
594 read_zero_bytes ( mem->sock[0].s );
595 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
596 mem->sock[0].s = INVALID_SOCKET;
599 server_stop ();
603 * oob_server: A very basic server receiving out-of-band data.
605 static VOID WINAPI oob_server ( server_params *par )
607 test_params *gen = par->general;
608 server_memory *mem;
609 u_long atmark = 0;
610 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
611 id = GetCurrentThreadId();
613 set_so_opentype ( FALSE ); /* non-overlapped */
614 server_start ( par );
615 mem = TlsGetValue ( tls );
617 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
618 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
620 SetEvent ( server_ready ); /* notify clients */
622 /* accept a single connection */
623 tmp = sizeof ( mem->sock[0].peer );
624 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
625 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
627 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
628 "oob_server (%x): strange peer address\n", id );
630 /* check initial atmark state */
631 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
632 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
634 /* Receive normal data */
635 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
636 ok ( n_recvd == n_expected,
637 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
638 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
639 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos);
641 /* check atmark state */
642 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
643 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
645 /* Echo data back */
646 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
647 ok ( n_sent == n_expected,
648 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
650 /* Receive a part of the out-of-band data and print atmark state */
651 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen );
652 ok ( n_recvd == 8,
653 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
654 n_expected -= 8;
656 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
658 /* Receive the rest of the out-of-band data and check atmark state */
659 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
661 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
662 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
664 /* cleanup */
665 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
666 mem->sock[0].s = INVALID_SOCKET;
668 server_stop ();
672 * select_server: A non-blocking server.
674 static VOID WINAPI select_server ( server_params *par )
676 test_params *gen = par->general;
677 server_memory *mem;
678 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
679 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
680 n_set, delta, n_ready;
681 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
682 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
684 set_so_opentype ( FALSE ); /* non-overlapped */
685 server_start ( par );
686 mem = TlsGetValue ( tls );
688 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
689 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
691 SetEvent ( server_ready ); /* notify clients */
693 FD_ZERO ( &fds_openrecv );
694 FD_ZERO ( &fds_recv );
695 FD_ZERO ( &fds_send );
696 FD_ZERO ( &fds_opensend );
698 FD_SET ( mem->s, &fds_openrecv );
700 while(1)
702 fds_recv = fds_openrecv;
703 fds_send = fds_opensend;
705 n_set = 0;
707 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
708 "select_server (%x): select() failed: %d\n" );
710 /* check for incoming requests */
711 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
712 n_set += 1;
714 /* accept a single connection */
715 tmp = sizeof ( mem->sock[n_connections].peer );
716 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
717 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
719 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
720 "select_server (%x): strange peer address\n", id );
722 /* add to list of open connections */
723 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
724 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
726 n_connections++;
729 /* handle open requests */
731 for ( i = 0; i < n_connections; i++ )
733 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
734 n_set += 1;
736 if ( mem->sock[i].n_recvd < n_expected ) {
737 /* Receive data & check it */
738 n_recvd = recv ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_recvd, min ( n_expected - mem->sock[i].n_recvd, par->buflen ), 0 );
739 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
740 mem->sock[i].n_recvd += n_recvd;
742 if ( mem->sock[i].n_recvd == n_expected ) {
743 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
744 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
745 FD_CLR ( mem->sock[i].s, &fds_openrecv );
748 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
752 /* only echo back what we've received */
753 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
755 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
756 n_set += 1;
758 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
759 /* Echo data back */
760 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
761 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
762 mem->sock[i].n_sent += n_sent;
764 if ( mem->sock[i].n_sent == n_expected ) {
765 FD_CLR ( mem->sock[i].s, &fds_opensend );
768 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
773 /* check that select returned the correct number of ready sockets */
774 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
776 /* check if all clients are done */
777 if ( ( fds_opensend.fd_count == 0 )
778 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
779 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
780 break;
784 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
786 /* cleanup */
787 read_zero_bytes ( mem->sock[i].s );
788 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
789 mem->sock[i].s = INVALID_SOCKET;
792 server_stop ();
795 /**************** Clients ***************/
798 * simple_client: A very basic client doing synchronous IO.
800 static VOID WINAPI simple_client ( client_params *par )
802 test_params *gen = par->general;
803 client_memory *mem;
804 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
806 id = GetCurrentThreadId();
807 /* wait here because we want to call set_so_opentype before creating a socket */
808 WaitForSingleObject ( server_ready, INFINITE );
810 check_so_opentype ();
811 set_so_opentype ( FALSE ); /* non-overlapped */
812 client_start ( par );
813 mem = TlsGetValue ( tls );
815 /* Connect */
816 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
817 0 ==, "simple_client (%x): connect error: %d\n" );
818 ok ( set_blocking ( mem->s, TRUE ) == 0,
819 "simple_client (%x): failed to set blocking mode\n", id );
821 /* send data to server */
822 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
823 ok ( n_sent == n_expected,
824 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
826 /* shutdown send direction */
827 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
829 /* Receive data echoed back & check it */
830 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
831 ok ( n_recvd == n_expected,
832 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
834 /* check data */
835 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
836 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
838 /* cleanup */
839 read_zero_bytes ( mem->s );
840 client_stop ();
844 * oob_client: A very basic client sending out-of-band data.
846 static VOID WINAPI oob_client ( client_params *par )
848 test_params *gen = par->general;
849 client_memory *mem;
850 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
852 id = GetCurrentThreadId();
853 /* wait here because we want to call set_so_opentype before creating a socket */
854 WaitForSingleObject ( server_ready, INFINITE );
856 check_so_opentype ();
857 set_so_opentype ( FALSE ); /* non-overlapped */
858 client_start ( par );
859 mem = TlsGetValue ( tls );
861 /* Connect */
862 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
863 0 ==, "oob_client (%x): connect error: %d\n" );
864 ok ( set_blocking ( mem->s, TRUE ) == 0,
865 "oob_client (%x): failed to set blocking mode\n", id );
867 /* send data to server */
868 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
869 ok ( n_sent == n_expected,
870 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
872 /* Receive data echoed back & check it */
873 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
874 ok ( n_recvd == n_expected,
875 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
876 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
877 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
879 /* send out-of-band data to server */
880 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen );
881 ok ( n_sent == n_expected,
882 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
884 /* shutdown send direction */
885 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
887 /* cleanup */
888 read_zero_bytes ( mem->s );
889 client_stop ();
893 * simple_mixed_client: mixing send and recvfrom
895 static VOID WINAPI simple_mixed_client ( client_params *par )
897 test_params *gen = par->general;
898 client_memory *mem;
899 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
900 int fromLen = sizeof(mem->addr);
901 struct sockaddr test;
903 id = GetCurrentThreadId();
904 /* wait here because we want to call set_so_opentype before creating a socket */
905 WaitForSingleObject ( server_ready, INFINITE );
907 check_so_opentype ();
908 set_so_opentype ( FALSE ); /* non-overlapped */
909 client_start ( par );
910 mem = TlsGetValue ( tls );
912 /* Connect */
913 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
914 0 ==, "simple_client (%x): connect error: %d\n" );
915 ok ( set_blocking ( mem->s, TRUE ) == 0,
916 "simple_client (%x): failed to set blocking mode\n", id );
918 /* send data to server */
919 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
920 ok ( n_sent == n_expected,
921 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
923 /* shutdown send direction */
924 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
926 /* this shouldn't change, since lpFrom, is not updated on
927 connection oriented sockets - exposed by bug 11640
929 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
931 /* Receive data echoed back & check it */
932 n_recvd = do_synchronous_recvfrom ( mem->s,
933 mem->recv_buf,
934 n_expected,
936 (struct sockaddr *)&test,
937 &fromLen,
938 par->buflen );
939 ok ( n_recvd == n_expected,
940 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
942 /* check that lpFrom was not updated */
943 ok(0 ==
944 strcmp(
945 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
946 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
948 /* check data */
949 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
950 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
952 /* cleanup */
953 read_zero_bytes ( mem->s );
954 client_stop ();
958 * event_client: An event-driven client
960 static void WINAPI event_client ( client_params *par )
962 test_params *gen = par->general;
963 client_memory *mem;
964 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
965 tmp, err, n;
966 HANDLE event;
967 WSANETWORKEVENTS wsa_events;
968 char *send_last, *recv_last, *send_p, *recv_p;
969 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
971 client_start ( par );
973 mem = TlsGetValue ( tls );
975 /* Prepare event notification for connect, makes socket nonblocking */
976 event = WSACreateEvent ();
977 WSAEventSelect ( mem->s, event, FD_CONNECT );
978 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
979 if ( tmp != 0 ) {
980 err = WSAGetLastError ();
981 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
982 tmp = WaitForSingleObject ( event, INFINITE );
983 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
984 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
985 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
986 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
987 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
988 if ( err ) goto out;
991 WSAEventSelect ( mem->s, event, mask );
993 recv_p = mem->recv_buf;
994 recv_last = mem->recv_buf + n_expected;
995 send_p = mem->send_buf;
996 send_last = mem->send_buf + n_expected;
998 while ( TRUE )
1000 err = WaitForSingleObject ( event, INFINITE );
1001 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
1003 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1004 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1006 if ( wsa_events.lNetworkEvents & FD_WRITE )
1008 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
1009 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
1011 if ( err== 0 )
1014 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
1015 if ( n < 0 )
1017 err = WSAGetLastError ();
1018 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
1020 else
1021 send_p += n;
1023 while ( n >= 0 && send_p < send_last );
1025 if ( send_p == send_last )
1027 shutdown ( mem->s, SD_SEND );
1028 mask &= ~FD_WRITE;
1029 WSAEventSelect ( mem->s, event, mask );
1032 if ( wsa_events.lNetworkEvents & FD_READ )
1034 err = wsa_events.iErrorCode[ FD_READ_BIT ];
1035 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
1036 if ( err != 0 ) break;
1038 /* First read must succeed */
1039 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1040 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
1042 while ( n >= 0 ) {
1043 recv_p += n;
1044 if ( recv_p == recv_last )
1046 mask &= ~FD_READ;
1047 WSAEventSelect ( mem->s, event, mask );
1048 break;
1050 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1051 ok(n >= 0 || WSAGetLastError() == WSAEWOULDBLOCK,
1052 "event_client (%x): got error %u\n", id, WSAGetLastError());
1056 if ( wsa_events.lNetworkEvents & FD_CLOSE )
1058 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
1059 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
1060 break;
1064 n = send_p - mem->send_buf;
1065 ok ( send_p == send_last,
1066 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
1067 n = recv_p - mem->recv_buf;
1068 ok ( recv_p == recv_last,
1069 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
1070 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1071 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
1073 out:
1074 WSACloseEvent ( event );
1075 client_stop ();
1078 /* Tests for WSAStartup */
1079 static void test_WithoutWSAStartup(void)
1081 DWORD err;
1083 WSASetLastError(0xdeadbeef);
1084 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n");
1085 err = WSAGetLastError();
1086 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1088 WSASetLastError(0xdeadbeef);
1089 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n");
1090 err = WSAGetLastError();
1091 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1094 static void test_WithWSAStartup(void)
1096 WSADATA data;
1097 WORD version = MAKEWORD( 2, 2 );
1098 INT res, socks, i, j;
1099 SOCKET sock;
1100 LPVOID ptr;
1101 struct
1103 SOCKET src, dst, dup_src, dup_dst;
1104 } pairs[32];
1105 DWORD error;
1107 res = WSAStartup( version, &data );
1108 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1110 ptr = gethostbyname("localhost");
1111 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
1113 /* Alloc some sockets to check if they are destroyed on WSACleanup */
1114 for (socks = 0; socks < ARRAY_SIZE(pairs); socks++)
1116 WSAPROTOCOL_INFOA info;
1117 tcp_socketpair(&pairs[socks].src, &pairs[socks].dst);
1119 memset(&info, 0, sizeof(info));
1120 ok(!WSADuplicateSocketA(pairs[socks].src, GetCurrentProcessId(), &info),
1121 "WSADuplicateSocketA should have worked\n");
1122 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0);
1123 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n");
1125 memset(&info, 0, sizeof(info));
1126 ok(!WSADuplicateSocketA(pairs[socks].dst, GetCurrentProcessId(), &info),
1127 "WSADuplicateSocketA should have worked\n");
1128 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0);
1129 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n");
1132 res = send(pairs[0].src, "TEST", 4, 0);
1133 ok(res == 4, "send failed with error %d\n", WSAGetLastError());
1135 WSACleanup();
1137 res = WSAStartup( version, &data );
1138 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1140 /* show that sockets are destroyed automatically after WSACleanup */
1141 SetLastError(0xdeadbeef);
1142 res = send(pairs[0].src, "TEST", 4, 0);
1143 error = WSAGetLastError();
1144 ok(res == SOCKET_ERROR, "send should have failed\n");
1145 ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1147 SetLastError(0xdeadbeef);
1148 res = send(pairs[0].dst, "TEST", 4, 0);
1149 error = WSAGetLastError();
1150 ok(res == SOCKET_ERROR, "send should have failed\n");
1151 ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1153 /* Check that all sockets were destroyed */
1154 for (i = 0; i < socks; i++)
1156 for (j = 0; j < 4; j++)
1158 struct sockaddr_in saddr;
1159 int size = sizeof(saddr);
1160 switch(j)
1162 case 0: sock = pairs[i].src; break;
1163 case 1: sock = pairs[i].dup_src; break;
1164 case 2: sock = pairs[i].dst; break;
1165 case 3: sock = pairs[i].dup_dst; break;
1168 SetLastError(0xdeadbeef);
1169 res = getsockname(sock, (struct sockaddr *)&saddr, &size);
1170 error = WSAGetLastError();
1171 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i);
1172 if (res == SOCKET_ERROR)
1173 ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error);
1177 /* While wine is not fixed, close all sockets manually */
1178 for (i = 0; i < socks; i++)
1180 closesocket(pairs[i].src);
1181 closesocket(pairs[i].dst);
1182 closesocket(pairs[i].dup_src);
1183 closesocket(pairs[i].dup_dst);
1186 res = WSACleanup();
1187 ok(res == 0, "expected 0, got %d\n", res);
1188 WSASetLastError(0xdeadbeef);
1189 res = WSACleanup();
1190 error = WSAGetLastError();
1191 ok ( res == SOCKET_ERROR && error == WSANOTINITIALISED,
1192 "WSACleanup returned %d WSAGetLastError is %d\n", res, error);
1195 /**************** Main program utility functions ***************/
1197 static void Init (void)
1199 WORD ver = MAKEWORD (2, 2);
1200 WSADATA data;
1201 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), ntdll;
1203 pFreeAddrInfoExW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoExW");
1204 pGetAddrInfoExW = (void *)GetProcAddress(hws2_32, "GetAddrInfoExW");
1205 pGetAddrInfoExOverlappedResult = (void *)GetProcAddress(hws2_32, "GetAddrInfoExOverlappedResult");
1206 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1207 pInetNtopW = (void *)GetProcAddress(hws2_32, "InetNtopW");
1208 pInetPtonA = (void *)GetProcAddress(hws2_32, "inet_pton");
1209 pInetPtonW = (void *)GetProcAddress(hws2_32, "InetPtonW");
1210 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll");
1211 pWSCGetProviderInfo = (void *)GetProcAddress(hws2_32, "WSCGetProviderInfo");
1213 ntdll = LoadLibraryA("ntdll.dll");
1214 if (ntdll)
1215 pNtClose = (void *)GetProcAddress(ntdll, "NtClose");
1217 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1218 tls = TlsAlloc();
1221 static void Exit (void)
1223 INT ret, err;
1224 TlsFree ( tls );
1225 ret = WSACleanup();
1226 err = WSAGetLastError();
1227 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1230 static void StartServer (LPTHREAD_START_ROUTINE routine,
1231 test_params *general, server_params *par)
1233 par->general = general;
1234 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1235 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1238 static void StartClients (LPTHREAD_START_ROUTINE routine,
1239 test_params *general, client_params *par)
1241 int i;
1242 par->general = general;
1243 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1245 client_id = i - 1;
1246 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1247 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1248 /* Make sure the client is up and running */
1249 WaitForSingleObject ( client_ready[client_id], INFINITE );
1253 static void do_test( test_setup *test )
1255 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1256 DWORD wait;
1258 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1259 for (i = 0; i <= n; i++)
1260 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1262 StartServer ( test->srv, &test->general, &test->srv_params );
1263 StartClients ( test->clt, &test->general, &test->clt_params );
1264 WaitForSingleObject ( server_ready, INFINITE );
1266 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1267 ok(!wait, "wait failed, error %u\n", wait);
1269 CloseHandle ( server_ready );
1270 for (i = 0; i <= n; i++)
1271 CloseHandle ( client_ready[i] );
1274 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1275 /* optname = SO_LINGER */
1276 static const LINGER linger_testvals[] = {
1277 {0,0},
1278 {0,73},
1279 {1,0},
1280 {5,189}
1283 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1284 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1285 bug in the linux kernel (fixed in 2.6.8) */
1286 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1288 static void test_set_getsockopt(void)
1290 SOCKET s, s2;
1291 int i, err, lasterr;
1292 int timeout;
1293 LINGER lingval;
1294 int size;
1295 WSAPROTOCOL_INFOA infoA;
1296 WSAPROTOCOL_INFOW infoW;
1297 char providername[WSAPROTOCOL_LEN + 1];
1298 DWORD value;
1299 struct _prottest
1301 int family, type, proto;
1302 } prottest[] = {
1303 {AF_INET, SOCK_STREAM, IPPROTO_TCP},
1304 {AF_INET, SOCK_DGRAM, IPPROTO_UDP},
1305 {AF_INET6, SOCK_STREAM, IPPROTO_TCP},
1306 {AF_INET6, SOCK_DGRAM, IPPROTO_UDP}
1308 union _csspace
1310 CSADDR_INFO cs;
1311 char space[128];
1312 } csinfoA, csinfoB;
1314 s = socket(AF_INET, SOCK_STREAM, 0);
1315 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1316 if( s == INVALID_SOCKET) return;
1317 /* SO_RCVTIMEO */
1318 timeout = SOCKTIMEOUT1;
1319 size = sizeof(timeout);
1320 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1321 if( !err)
1322 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1323 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1324 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1326 timeout = 0;
1327 size = sizeof(timeout);
1328 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1329 if( !err)
1330 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1331 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1332 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1334 /* SO_SNDTIMEO */
1335 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1336 size = sizeof(timeout);
1337 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1338 if( !err)
1339 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1340 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1341 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1343 /* SO_SNDBUF */
1344 value = 4096;
1345 size = sizeof(value);
1346 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size);
1347 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1348 value = 0xdeadbeef;
1349 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size);
1350 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1351 todo_wine ok( value == 4096, "expected 4096, got %u\n", value );
1353 /* SO_RCVBUF */
1354 value = 4096;
1355 size = sizeof(value);
1356 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size);
1357 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1358 value = 0xdeadbeef;
1359 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size);
1360 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1361 todo_wine ok( value == 4096, "expected 4096, got %u\n", value );
1363 /* SO_LINGER */
1364 for( i = 0; i < ARRAY_SIZE(linger_testvals);i++) {
1365 size = sizeof(lingval);
1366 lingval = linger_testvals[i];
1367 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingval, size);
1368 ok(!err, "Test %u: failed to set SO_LINGER, error %u\n", i, WSAGetLastError());
1369 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingval, &size);
1370 ok(!err, "Test %u: failed to get SO_LINGER, error %u\n", i, WSAGetLastError());
1371 ok(!lingval.l_onoff == !linger_testvals[i].l_onoff, "Test %u: expected %d, got %d\n",
1372 i, linger_testvals[i].l_onoff, lingval.l_onoff);
1373 if (lingval.l_onoff)
1374 ok(lingval.l_linger == linger_testvals[i].l_linger, "Test %u: expected %d, got %d\n",
1375 i, linger_testvals[i].l_linger, lingval.l_linger);
1378 size = sizeof(lingval);
1379 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1380 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1381 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1382 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1383 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1384 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1386 size = sizeof(BOOL);
1387 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1388 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1389 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1390 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1391 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1392 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1394 /* Test for erroneously passing a value instead of a pointer as optval */
1395 size = sizeof(char);
1396 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1397 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1398 "instead of failing.\n");
1399 lasterr = WSAGetLastError();
1400 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1401 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1402 lasterr, WSAEFAULT);
1404 /* SO_RCVTIMEO with invalid values for level */
1405 size = sizeof(timeout);
1406 timeout = SOCKTIMEOUT1;
1407 SetLastError(0xdeadbeef);
1408 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1409 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1410 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1411 err, WSAGetLastError());
1413 timeout = SOCKTIMEOUT1;
1414 SetLastError(0xdeadbeef);
1415 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1416 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1417 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1418 err, WSAGetLastError());
1420 /* Test SO_ERROR set/get */
1421 SetLastError(0xdeadbeef);
1422 i = 1234;
1423 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size);
1424 todo_wine
1425 ok( !err && !WSAGetLastError(),
1426 "got %d with %d (expected 0 with 0)\n",
1427 err, WSAGetLastError());
1429 SetLastError(0xdeadbeef);
1430 i = 4321;
1431 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1432 todo_wine
1433 ok( !err && !WSAGetLastError(),
1434 "got %d with %d (expected 0 with 0)\n",
1435 err, WSAGetLastError());
1436 todo_wine
1437 ok (i == 1234, "got %d (expected 1234)\n", i);
1439 /* Test invalid optlen */
1440 SetLastError(0xdeadbeef);
1441 size = 1;
1442 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1443 todo_wine
1444 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEFAULT),
1445 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1446 err, WSAGetLastError());
1448 closesocket(s);
1449 /* Test with the closed socket */
1450 SetLastError(0xdeadbeef);
1451 size = sizeof(i);
1452 i = 1234;
1453 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1454 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAENOTSOCK),
1455 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1456 err, WSAGetLastError());
1457 ok (i == 1234, "expected 1234, got %d\n", i);
1459 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1460 s = socket(AF_INET, SOCK_DGRAM, 0);
1461 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1462 size = sizeof(i);
1463 i = 0x0000000a;
1464 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size);
1465 if (!err)
1467 for (i = 0; i < 4; i++)
1469 int k, j;
1470 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1471 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1);
1472 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1473 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size);
1474 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1475 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i];
1476 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k);
1479 else
1480 win_skip("IP_MULTICAST_TTL is unsupported\n");
1481 closesocket(s);
1483 /* test SO_PROTOCOL_INFOA invalid parameters */
1484 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1485 "getsockopt should have failed\n");
1486 err = WSAGetLastError();
1487 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1488 size = sizeof(WSAPROTOCOL_INFOA);
1489 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1490 "getsockopt should have failed\n");
1491 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1492 err = WSAGetLastError();
1493 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1494 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1495 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1496 "getsockopt should have failed\n");
1497 err = WSAGetLastError();
1498 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1499 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, NULL),
1500 "getsockopt should have failed\n");
1501 err = WSAGetLastError();
1502 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1503 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, &size),
1504 "getsockopt should have failed\n");
1505 err = WSAGetLastError();
1506 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1507 size = sizeof(WSAPROTOCOL_INFOA) / 2;
1508 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1509 "getsockopt should have failed\n");
1510 err = WSAGetLastError();
1511 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1512 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1513 size = sizeof(WSAPROTOCOL_INFOA) * 2;
1514 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1515 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1516 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size);
1518 closesocket(s);
1520 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1521 for (i = 0; i < ARRAY_SIZE(prottest); i++)
1523 int k;
1525 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto);
1526 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue;
1528 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n",
1529 WSAGetLastError());
1531 /* compare both A and W version */
1532 infoA.szProtocol[0] = 0;
1533 size = sizeof(WSAPROTOCOL_INFOA);
1534 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1535 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1536 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1538 infoW.szProtocol[0] = 0;
1539 size = sizeof(WSAPROTOCOL_INFOW);
1540 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &infoW, &size);
1541 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1542 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size);
1544 ok(infoA.szProtocol[0], "WSAPROTOCOL_INFOA was not filled\n");
1545 ok(infoW.szProtocol[0], "WSAPROTOCOL_INFOW was not filled\n");
1547 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1,
1548 providername, sizeof(providername), NULL, NULL);
1549 ok(!strcmp(infoA.szProtocol,providername),
1550 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername);
1552 ok(!memcmp(&infoA, &infoW, FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol)),
1553 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1555 /* Remove IF when WSAEnumProtocols support IPV6 data */
1556 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n",
1557 prottest[i].family, infoA.iAddressFamily);
1558 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n",
1559 prottest[i].type, infoA.iSocketType);
1560 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n",
1561 prottest[i].proto, infoA.iProtocol);
1563 /* IP_HDRINCL is supported only on SOCK_RAW but passed to SOCK_DGRAM by Impossible Creatures */
1564 size = sizeof(i);
1565 k = 1;
1566 SetLastError(0xdeadbeef);
1567 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1568 if (err == -1) /* >= Vista */
1570 todo_wine {
1571 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1572 k = 99;
1573 SetLastError(0xdeadbeef);
1574 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1575 ok(err == -1, "Expected -1, got %d\n", err);
1576 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1577 ok(k == 99, "Expected 99, got %d\n", k);
1579 size = sizeof(k);
1580 k = 0;
1581 SetLastError(0xdeadbeef);
1582 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1584 ok(err == -1, "Expected -1, got %d\n", err);
1585 todo_wine {
1586 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1587 k = 99;
1588 SetLastError(0xdeadbeef);
1589 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1590 ok(err == -1, "Expected -1, got %d\n", err);
1591 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1592 ok(k == 99, "Expected 99, got %d\n", k);
1595 else /* <= 2003 the tests differ between TCP and UDP, UDP silently accepts */
1597 SetLastError(0xdeadbeef);
1598 k = 99;
1599 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1600 if (prottest[i].type == SOCK_DGRAM)
1602 ok(err == 0, "Expected 0, got %d\n", err);
1603 ok(k == 1, "Expected 1, got %d\n", k);
1605 else
1607 /* contratry to what we could expect the function returns error but k is changed */
1608 ok(err == -1, "Expected -1, got %d\n", err);
1609 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1610 ok(k == 0, "Expected 0, got %d\n", k);
1613 k = 0;
1614 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1615 ok(err == 0, "Expected 0, got %d\n", err);
1617 k = 99;
1618 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1619 if (prottest[i].type == SOCK_DGRAM)
1621 ok(err == 0, "Expected 0, got %d\n", err);
1622 ok(k == 0, "Expected 0, got %d\n", k);
1624 else
1626 /* contratry to what we could expect the function returns error but k is changed */
1627 ok(err == -1, "Expected -1, got %d\n", err);
1628 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1629 ok(k == 0, "Expected 0, got %d\n", k);
1633 closesocket(s);
1636 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1637 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1638 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1639 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1640 ok(s2 != INVALID_SOCKET, "Failed to create socket\n");
1642 SetLastError(0xdeadbeef);
1643 size = sizeof(csinfoA);
1644 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1645 if (!err)
1647 struct sockaddr_in saddr;
1648 memset(&saddr, 0, sizeof(saddr));
1649 saddr.sin_family = AF_INET;
1650 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1652 /* Socket is not bound, no information provided */
1653 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1654 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1655 /* Socket is not connected, no information provided */
1656 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1657 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1659 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr));
1660 ok(!err, "Expected 0, got %d\n", err);
1661 size = sizeof(csinfoA);
1662 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1663 ok(!err, "Expected 0, got %d\n", err);
1665 /* Socket is bound */
1666 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n");
1667 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n");
1668 /* Socket is not connected, no information provided */
1669 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1670 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1672 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1673 ok(!err, "Expected 0, got %d\n", err);
1674 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1675 ok(!err, "Expected 0, got %d\n", err);
1676 err = listen(s2, 1);
1677 ok(!err, "Expected 0, got %d\n", err);
1678 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr));
1679 ok(!err, "Expected 0, got %d\n", err);
1680 size = sizeof(saddr);
1681 err = accept(s2, (struct sockaddr*)&saddr, &size);
1682 ok(err != INVALID_SOCKET, "Failed to accept socket\n");
1683 closesocket(s2);
1684 s2 = err;
1686 size = sizeof(csinfoA);
1687 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1688 ok(!err, "Expected 0, got %d\n", err);
1689 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size);
1690 ok(!err, "Expected 0, got %d\n", err);
1691 ok(size == sizeof(csinfoA), "Got %d\n", size);
1692 size = sizeof(saddr);
1693 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1694 csinfoA.cs.LocalAddr.iSockaddrLength);
1695 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1696 csinfoA.cs.RemoteAddr.iSockaddrLength);
1697 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size),
1698 "Expected matching addresses\n");
1699 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size),
1700 "Expected matching addresses\n");
1701 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1702 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1703 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1704 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1706 err = getpeername(s, (struct sockaddr *)&saddr, &size);
1707 ok(!err, "Expected 0, got %d\n", err);
1708 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1709 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1710 err = getpeername(s2, (struct sockaddr *)&saddr, &size);
1711 ok(!err, "Expected 0, got %d\n", err);
1712 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1713 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1714 err = getsockname(s, (struct sockaddr *)&saddr, &size);
1715 ok(!err, "Expected 0, got %d\n", err);
1716 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1717 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1718 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1719 ok(!err, "Expected 0, got %d\n", err);
1720 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1721 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1723 SetLastError(0xdeadbeef);
1724 size = sizeof(CSADDR_INFO);
1725 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1726 ok(err, "Expected non-zero\n");
1727 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size);
1728 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1730 /* At least for IPv4 the size is exactly 56 bytes */
1731 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs);
1732 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1733 ok(!err, "Expected 0, got %d\n", err);
1734 size--;
1735 SetLastError(0xdeadbeef);
1736 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1737 ok(err, "Expected non-zero\n");
1738 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1740 else
1741 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1743 closesocket(s);
1744 closesocket(s2);
1746 for (i = 0; i < 2; i++)
1748 int family, level;
1750 if (i)
1752 family = AF_INET6;
1753 level = IPPROTO_IPV6;
1755 else
1757 family = AF_INET;
1758 level = IPPROTO_IP;
1761 s = socket(family, SOCK_DGRAM, 0);
1762 if (s == INVALID_SOCKET && i)
1764 skip("IPv6 is not supported\n");
1765 break;
1767 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1769 size = sizeof(value);
1770 value = 0xdead;
1771 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1772 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1773 ok(value == 0, "Expected 0, got %d\n", value);
1775 size = sizeof(value);
1776 value = 1;
1777 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1778 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1780 value = 0xdead;
1781 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1782 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1783 ok(value == 1, "Expected 1, got %d\n", value);
1785 size = sizeof(value);
1786 value = 0xdead;
1787 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1788 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1790 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1791 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1792 ok(value == 1, "Expected 1, got %d\n", value);
1794 closesocket(s);
1796 s = socket(family, SOCK_STREAM, 0);
1797 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1799 size = sizeof(value);
1800 value = 0xdead;
1801 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1802 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1803 ok(value == 1 || broken(value == 0) /* < vista */, "Expected 1, got %d\n", value);
1805 size = sizeof(value);
1806 value = 0;
1807 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1808 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1810 value = 0xdead;
1811 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1812 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1813 ok(value == 0, "Expected 0, got %d\n", value);
1815 closesocket(s);
1817 s = socket(family, SOCK_RAW, 0);
1818 if (s == INVALID_SOCKET)
1820 if (WSAGetLastError() == WSAEACCES) skip("SOCK_RAW is not available\n");
1821 else if (i) skip("IPv6 is not supported\n");
1822 break;
1824 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1826 size = sizeof(value);
1827 value = 0xdead;
1828 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1829 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1830 ok(value == 0, "Expected 0, got %d\n", value);
1832 size = sizeof(value);
1833 value = 1;
1834 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1835 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1837 value = 0xdead;
1838 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1839 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1840 ok(value == 1, "Expected 1, got %d\n", value);
1842 closesocket(s);
1846 static void test_so_reuseaddr(void)
1848 struct sockaddr_in saddr;
1849 SOCKET s1,s2;
1850 unsigned int rc,reuse;
1851 int size;
1852 DWORD err;
1854 saddr.sin_family = AF_INET;
1855 saddr.sin_port = htons(SERVERPORT+1);
1856 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1858 s1=socket(AF_INET, SOCK_STREAM, 0);
1859 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1860 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1861 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1863 s2=socket(AF_INET, SOCK_STREAM, 0);
1864 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1866 reuse=0x1234;
1867 size=sizeof(reuse);
1868 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1869 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1871 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1872 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1874 reuse = 1;
1875 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1876 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1878 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1879 * a port immediately after closing another socket on that port, so
1880 * basically following the BSD socket semantics here. */
1881 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1882 if(rc==0)
1884 int s3=socket(AF_INET, SOCK_STREAM, 0), s4;
1886 /* If we could bind again in the same port this is Windows version <= XP.
1887 * Lets test if we can really connect to one of them. */
1888 set_blocking(s1, FALSE);
1889 set_blocking(s2, FALSE);
1890 rc = listen(s1, 1);
1891 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1892 rc = listen(s2, 1);
1893 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1894 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr));
1895 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError());
1897 /* the delivery of the connection is random so we need to try on both sockets */
1898 size = sizeof(saddr);
1899 s4 = accept(s1, (struct sockaddr*)&saddr, &size);
1900 if(s4 == INVALID_SOCKET)
1901 s4 = accept(s2, (struct sockaddr*)&saddr, &size);
1902 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n");
1904 closesocket(s1);
1905 closesocket(s3);
1906 closesocket(s4);
1908 else
1910 err = WSAGetLastError();
1911 ok(err==WSAEACCES, "expected 10013, got %d\n", err);
1913 closesocket(s1);
1914 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1915 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1918 closesocket(s2);
1921 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1923 static void test_ip_pktinfo(void)
1925 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
1926 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
1927 struct sockaddr_in s1addr, s2addr, s3addr;
1928 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
1929 LPFN_WSARECVMSG pWSARecvMsg = NULL;
1930 unsigned int rc, yes = 1;
1931 BOOL foundhdr;
1932 DWORD dwBytes, dwSize, dwFlags;
1933 socklen_t addrlen;
1934 WSACMSGHDR *cmsg;
1935 WSAOVERLAPPED ov;
1936 WSABUF iovec[1];
1937 SOCKET s1, s2;
1938 WSAMSG hdr;
1939 int i, err;
1941 memset(&ov, 0, sizeof(ov));
1942 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1944 memset(&hdr, 0x00, sizeof(hdr));
1945 s1addr.sin_family = AF_INET;
1946 s1addr.sin_port = htons(0);
1947 /* Note: s1addr.sin_addr is set below */
1948 iovec[0].buf = recvbuf;
1949 iovec[0].len = sizeof(recvbuf);
1950 hdr.name = (struct sockaddr*)&s3addr;
1951 hdr.namelen = sizeof(s3addr);
1952 hdr.lpBuffers = &iovec[0];
1953 hdr.dwBufferCount = 1;
1954 hdr.Control.buf = pktbuf;
1955 /* Note: hdr.Control.len is set below */
1956 hdr.dwFlags = 0;
1958 for (i=0;i<ARRAY_SIZE(addresses);i++)
1960 s1addr.sin_addr.s_addr = addresses[i];
1962 /* Build "server" side socket */
1963 s1=socket(AF_INET, SOCK_DGRAM, 0);
1964 ok(s1 != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
1966 /* Obtain the WSARecvMsg function */
1967 rc = WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
1968 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
1969 ok(!rc, "failed to get WSARecvMsg, error %u\n", WSAGetLastError());
1971 /* Setup the server side socket */
1972 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
1973 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
1975 /* Build "client" side socket */
1976 addrlen = sizeof(s2addr);
1977 rc = getsockname(s1, (struct sockaddr *) &s2addr, &addrlen);
1978 ok(!rc, "failed to get address, error %u\n", WSAGetLastError());
1979 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
1980 s2=socket(AF_INET, SOCK_DGRAM, 0);
1981 ok(s2 != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
1983 /* Test an empty message header */
1984 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
1985 err=WSAGetLastError();
1986 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
1988 /* Test that when no control data arrives, a 0-length NULL-valued control buffer should succeed */
1989 SetLastError(0xdeadbeef);
1990 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1991 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1992 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1993 hdr.Control.buf = NULL;
1994 hdr.Control.len = 0;
1995 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1996 ok(rc == 0, "WSARecvMsg() failed error: %d\n", WSAGetLastError());
1997 hdr.Control.buf = pktbuf;
1999 /* Now start IP_PKTINFO for future tests */
2000 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
2001 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
2004 * Send a packet from the client to the server and test for specifying
2005 * a short control header.
2007 SetLastError(0xdeadbeef);
2008 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2009 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2010 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2011 hdr.Control.len = 1;
2012 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
2013 err=WSAGetLastError();
2014 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
2015 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
2016 hdr.dwFlags = 0; /* Reset flags */
2018 /* Perform another short control header test, this time with an overlapped receive */
2019 hdr.Control.len = 1;
2020 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2021 err=WSAGetLastError();
2022 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2023 SetLastError(0xdeadbeef);
2024 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2025 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2026 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2027 ok(!WaitForSingleObject(ov.hEvent, 100), "wait failed\n");
2028 dwFlags = 0;
2029 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
2030 ok(dwFlags == 0,
2031 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
2032 ok(hdr.dwFlags == MSG_CTRUNC,
2033 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
2034 hdr.dwFlags = 0; /* Reset flags */
2037 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
2038 * on the server end and check that the returned packet matches what was sent.
2040 hdr.Control.len = sizeof(pktbuf);
2041 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2042 err=WSAGetLastError();
2043 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2044 ok(hdr.Control.len == sizeof(pktbuf),
2045 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
2046 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2047 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2048 ok(!WaitForSingleObject(ov.hEvent, 100), "wait failed\n");
2049 dwSize = 0;
2050 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
2051 ok(dwSize == sizeof(msg),
2052 "WSARecvMsg() buffer length does not match transmitted data!\n");
2053 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
2054 "WSARecvMsg() buffer does not match transmitted data!\n");
2055 ok(hdr.Control.len == IP_PKTINFO_LEN,
2056 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
2058 /* Test for the expected IP_PKTINFO return information. */
2059 foundhdr = FALSE;
2060 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
2062 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
2064 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
2066 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
2067 foundhdr = TRUE;
2070 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
2072 closesocket(s2);
2073 closesocket(s1);
2076 CloseHandle(ov.hEvent);
2079 /************* Array containing the tests to run **********/
2081 #define STD_STREAM_SOCKET \
2082 SOCK_STREAM, \
2083 0, \
2084 SERVERIP, \
2085 SERVERPORT
2087 static test_setup tests [] =
2089 /* Test 0: synchronous client and server */
2092 STD_STREAM_SOCKET,
2093 2048,
2097 simple_server,
2099 NULL,
2103 simple_client,
2105 NULL,
2110 /* Test 1: event-driven client, synchronous server */
2113 STD_STREAM_SOCKET,
2114 2048,
2118 simple_server,
2120 NULL,
2124 event_client,
2126 NULL,
2127 WSA_FLAG_OVERLAPPED,
2131 /* Test 2: synchronous client, non-blocking server via select() */
2134 STD_STREAM_SOCKET,
2135 2048,
2139 select_server,
2141 NULL,
2145 simple_client,
2147 NULL,
2152 /* Test 3: OOB client, OOB server */
2155 STD_STREAM_SOCKET,
2156 128,
2160 oob_server,
2162 NULL,
2166 oob_client,
2168 NULL,
2173 /* Test 4: synchronous mixed client and server */
2176 STD_STREAM_SOCKET,
2177 2048,
2181 simple_server,
2183 NULL,
2187 simple_mixed_client,
2189 NULL,
2196 static void test_UDP(void)
2198 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2199 possible that this test fails due to dropped packets. */
2201 /* peer 0 receives data from all other peers */
2202 struct sock_info peer[NUM_UDP_PEERS];
2203 char buf[16];
2204 int ss, i, n_recv, n_sent;
2206 memset (buf,0,sizeof(buf));
2207 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
2208 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
2210 peer[i].addr.sin_family = AF_INET;
2211 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
2213 if ( i == 0 ) {
2214 peer[i].addr.sin_port = htons ( SERVERPORT );
2215 } else {
2216 peer[i].addr.sin_port = htons ( 0 );
2219 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
2221 /* test getsockname() to get peer's port */
2222 ss = sizeof ( peer[i].addr );
2223 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
2224 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
2227 /* test getsockname() */
2228 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
2230 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2231 /* send client's ip */
2232 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
2233 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
2234 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
2237 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2238 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
2239 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
2240 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
2244 static DWORD WINAPI do_getservbyname( void *param )
2246 struct {
2247 const char *name;
2248 const char *proto;
2249 int port;
2250 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
2252 HANDLE *starttest = param;
2253 int i, j;
2254 struct servent *pserv[2];
2256 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT,
2257 "test_getservbyname: timeout waiting for start signal\n" );
2259 /* ensure that necessary buffer resizes are completed */
2260 for ( j = 0; j < 2; j++) {
2261 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2264 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
2265 for ( j = 0; j < 2; j++ ) {
2266 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2267 ok ( pserv[j] != NULL || broken(pserv[j] == NULL) /* win8, fixed in win81 */,
2268 "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
2269 if ( !pserv[j] ) continue;
2270 ok ( pserv[j]->s_port == htons(serv[j].port),
2271 "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
2272 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ),
2273 "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
2274 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ),
2275 "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
2278 ok ( pserv[0] == pserv[1] || broken(pserv[0] != pserv[1]) /* win8, fixed in win81 */,
2279 "getservbyname: winsock resized servent buffer when not necessary\n" );
2282 return 0;
2285 static void test_getservbyname(void)
2287 int i;
2288 HANDLE starttest, thread[NUM_THREADS];
2289 DWORD thread_id[NUM_THREADS];
2291 starttest = CreateEventA ( NULL, 1, 0, "test_getservbyname_starttest" );
2293 /* create threads */
2294 for ( i = 0; i < NUM_THREADS; i++ ) {
2295 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
2298 /* signal threads to start */
2299 SetEvent ( starttest );
2301 for ( i = 0; i < NUM_THREADS; i++) {
2302 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
2306 static void test_WSASocket(void)
2308 SOCKET sock = INVALID_SOCKET;
2309 WSAPROTOCOL_INFOA *pi;
2310 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP};
2311 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP};
2312 int items, err, size, socktype, i, j;
2313 UINT pi_size;
2315 static const struct
2317 int family, type, protocol;
2318 DWORD error;
2319 int ret_family, ret_type, ret_protocol;
2321 tests[] =
2323 /* 0 */
2324 {0xdead, SOCK_STREAM, IPPROTO_TCP, WSAEAFNOSUPPORT},
2325 {-1, SOCK_STREAM, IPPROTO_TCP, WSAEAFNOSUPPORT},
2326 {AF_INET, 0xdead, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2327 {AF_INET, -1, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2328 {AF_INET, SOCK_STREAM, 0xdead, WSAEPROTONOSUPPORT},
2329 {AF_INET, SOCK_STREAM, -1, WSAEPROTONOSUPPORT},
2330 {0xdead, 0xdead, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2331 {0xdead, SOCK_STREAM, 0xdead, WSAEAFNOSUPPORT},
2332 {AF_INET, 0xdead, 0xdead, WSAESOCKTNOSUPPORT},
2333 {0xdead, SOCK_STREAM, IPPROTO_UDP, WSAEAFNOSUPPORT},
2335 /* 10 */
2336 {AF_INET, SOCK_STREAM, 0, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2337 {AF_INET, SOCK_DGRAM, 0, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2338 {AF_INET, 0xdead, 0, WSAESOCKTNOSUPPORT},
2339 {AF_INET, 0, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2340 {AF_INET, 0, IPPROTO_UDP, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2341 {AF_INET, 0, 0xdead, WSAEPROTONOSUPPORT},
2342 {AF_INET, 0, 0, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2343 {AF_INET, SOCK_STREAM, IPPROTO_UDP, WSAEPROTONOSUPPORT},
2344 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, WSAEPROTONOSUPPORT},
2346 /* 19 */
2347 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2348 {AF_UNSPEC, SOCK_STREAM, 0xdead, WSAEPROTONOSUPPORT},
2349 {AF_UNSPEC, 0xdead, IPPROTO_UDP, WSAESOCKTNOSUPPORT},
2350 {AF_UNSPEC, SOCK_STREAM, 0, WSAEINVAL},
2351 {AF_UNSPEC, SOCK_DGRAM, 0, WSAEINVAL},
2352 {AF_UNSPEC, 0xdead, 0, WSAEINVAL},
2353 {AF_UNSPEC, 0, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2354 {AF_UNSPEC, 0, IPPROTO_UDP, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2355 {AF_UNSPEC, 0, 0xdead, WSAEPROTONOSUPPORT},
2356 {AF_UNSPEC, 0, 0, WSAEINVAL},
2359 for (i = 0; i < ARRAY_SIZE(tests); ++i)
2361 SetLastError( 0xdeadbeef );
2362 sock = WSASocketA( tests[i].family, tests[i].type, tests[i].protocol, NULL, 0, 0 );
2363 todo_wine_if (!tests[i].error || i == 7)
2364 ok(WSAGetLastError() == tests[i].error, "Test %u: got wrong error %u\n", i, WSAGetLastError());
2365 if (tests[i].error)
2367 ok(sock == INVALID_SOCKET, "Test %u: expected failure\n", i);
2369 else
2371 WSAPROTOCOL_INFOA info;
2373 ok(sock != INVALID_SOCKET, "Text %u: expected success\n", i);
2375 size = sizeof(info);
2376 err = getsockopt( sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *)&info, &size );
2377 ok(!err, "Test %u: getsockopt failed, error %u\n", i, WSAGetLastError());
2378 ok(info.iAddressFamily == tests[i].ret_family, "Test %u: got wrong family %d\n", i, info.iAddressFamily);
2379 ok(info.iSocketType == tests[i].ret_type, "Test %u: got wrong type %d\n", i, info.iSocketType);
2380 ok(info.iProtocol == tests[i].ret_protocol, "Test %u: got wrong protocol %d\n", i, info.iProtocol);
2382 closesocket( sock );
2386 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2387 * to avoid a crash on win98.
2389 pi_size = 0;
2390 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size);
2391 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2392 items);
2393 err = WSAGetLastError();
2394 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2395 err, WSAENOBUFS);
2397 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2398 ok(pi != NULL, "Failed to allocate memory\n");
2400 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size);
2401 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2402 WSAGetLastError());
2404 if (items == 0) {
2405 skip("No protocols enumerated.\n");
2406 HeapFree(GetProcessHeap(), 0, pi);
2407 return;
2410 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
2411 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
2412 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2413 WSAGetLastError());
2414 closesocket(sock);
2416 /* find what parameters are used first: plain parameters or protocol info struct */
2417 pi[0].iProtocol = -1;
2418 pi[0].iSocketType = -1;
2419 pi[0].iAddressFamily = -1;
2420 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET,
2421 "WSASocketA should have failed\n");
2422 err = WSAGetLastError();
2423 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2425 pi[0].iProtocol = 0;
2426 pi[0].iSocketType = 0;
2427 pi[0].iAddressFamily = 0;
2428 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0);
2429 if(sock != INVALID_SOCKET)
2431 win_skip("must work only in OS <= 2003\n");
2432 closesocket(sock);
2434 else
2436 err = WSAGetLastError();
2437 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2440 pi[0].iProtocol = IPPROTO_UDP;
2441 pi[0].iSocketType = SOCK_DGRAM;
2442 pi[0].iAddressFamily = AF_INET;
2443 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0);
2444 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2445 WSAGetLastError());
2446 size = sizeof(socktype);
2447 socktype = 0xdead;
2448 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2449 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2450 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2451 SOCK_DGRAM, socktype);
2452 closesocket(sock);
2454 sock = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi[0], 0, 0);
2455 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2456 WSAGetLastError());
2457 size = sizeof(socktype);
2458 socktype = 0xdead;
2459 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2460 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2461 ok(socktype == SOCK_STREAM, "Wrong socket type, expected %d received %d\n",
2462 SOCK_STREAM, socktype);
2463 closesocket(sock);
2465 HeapFree(GetProcessHeap(), 0, pi);
2467 pi_size = 0;
2468 items = WSAEnumProtocolsA(NULL, NULL, &pi_size);
2469 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2470 items);
2471 err = WSAGetLastError();
2472 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2473 err, WSAENOBUFS);
2475 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2476 ok(pi != NULL, "Failed to allocate memory\n");
2478 items = WSAEnumProtocolsA(NULL, pi, &pi_size);
2479 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2480 WSAGetLastError());
2482 /* when no protocol and socket type are specified the first entry
2483 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2484 * is returned */
2485 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2486 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2487 WSAGetLastError());
2489 size = sizeof(socktype);
2490 socktype = 0xdead;
2491 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2492 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2493 for(i = 0; i < items; i++)
2495 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
2497 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n",
2498 pi[i].iSocketType, socktype);
2499 break;
2502 ok(i != items, "Creating a socket without protocol and socket type didn't work\n");
2503 closesocket(sock);
2505 /* when no socket type is specified the first entry from WSAEnumProtocols
2506 * that matches the protocol is returned */
2507 for (i = 0; i < ARRAY_SIZE(autoprotocols); i++)
2509 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0);
2510 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n",
2511 autoprotocols[i], WSAGetLastError());
2513 size = sizeof(socktype);
2514 socktype = 0xdead;
2515 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2516 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2518 for (err = 1, j = 0; j < items; j++)
2520 if (pi[j].iProtocol == autoprotocols[i])
2522 ok(pi[j].iSocketType == socktype, "expected %d, got %d\n", socktype, pi[j].iSocketType);
2523 err = 0;
2524 break;
2527 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]);
2529 closesocket(sock);
2532 HeapFree(GetProcessHeap(), 0, pi);
2534 SetLastError(0xdeadbeef);
2535 /* starting on vista the socket function returns error during the socket
2536 creation and no longer in the socket operations (sendto, readfrom) */
2537 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
2538 if (sock == INVALID_SOCKET)
2540 err = WSAGetLastError();
2541 ok(err == WSAEACCES, "Expected 10013, received %d\n", err);
2542 skip("SOCK_RAW is not supported\n");
2544 else
2546 size = sizeof(socktype);
2547 socktype = 0xdead;
2548 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2549 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2550 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2551 SOCK_RAW, socktype);
2552 closesocket(sock);
2554 todo_wine {
2555 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0);
2556 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2557 WSAGetLastError());
2558 size = sizeof(socktype);
2559 socktype = 0xdead;
2560 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2561 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2562 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2563 SOCK_RAW, socktype);
2564 closesocket(sock);
2567 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_TCP, NULL, 0, 0);
2568 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2569 WSAGetLastError());
2570 size = sizeof(socktype);
2571 socktype = 0xdead;
2572 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2573 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2574 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2575 SOCK_RAW, socktype);
2576 closesocket(sock);
2579 /* IPX socket tests */
2581 SetLastError(0xdeadbeef);
2582 sock = WSASocketA(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, NULL, 0, 0);
2583 if (sock == INVALID_SOCKET)
2585 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "got error %u\n", WSAGetLastError());
2586 skip("IPX is not supported\n");
2588 else
2590 WSAPROTOCOL_INFOA info;
2591 closesocket(sock);
2593 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0);
2594 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2595 WSAGetLastError());
2597 size = sizeof(socktype);
2598 socktype = 0xdead;
2599 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2600 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2601 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2602 SOCK_DGRAM, socktype);
2604 /* check socket family, type and protocol */
2605 size = sizeof(WSAPROTOCOL_INFOA);
2606 err = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &info, &size);
2607 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2608 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n",
2609 NSPROTO_IPX, info.iProtocol);
2610 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n",
2611 AF_IPX, info.iProtocol);
2612 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2613 SOCK_DGRAM, info.iSocketType);
2614 closesocket(sock);
2616 /* SOCK_STREAM does not support NSPROTO_IPX */
2617 SetLastError(0xdeadbeef);
2618 ok(WSASocketA(AF_IPX, SOCK_STREAM, NSPROTO_IPX, NULL, 0, 0) == INVALID_SOCKET,
2619 "WSASocketA should have failed\n");
2620 err = WSAGetLastError();
2621 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2623 /* test extended IPX support - that is adding any number between 0 and 255
2624 * to the IPX protocol value will make it be used as IPX packet type */
2625 for(i = 0;i <= 255;i += 17)
2627 SetLastError(0xdeadbeef);
2628 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0);
2629 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2630 WSAGetLastError());
2632 size = sizeof(int);
2633 socktype = -1;
2634 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size);
2635 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2636 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n",
2637 i, socktype);
2639 closesocket(sock);
2644 static void test_WSADuplicateSocket(void)
2646 SOCKET source, dupsock;
2647 WSAPROTOCOL_INFOA info;
2648 DWORD err;
2649 struct sockaddr_in addr;
2650 int socktype, size, addrsize, ret;
2651 char teststr[] = "TEST", buffer[16];
2653 source = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
2654 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2656 /* test invalid parameters */
2657 SetLastError(0xdeadbeef);
2658 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n");
2659 err = WSAGetLastError();
2660 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2662 SetLastError(0xdeadbeef);
2663 ok(WSADuplicateSocketA(source, 0, NULL),
2664 "WSADuplicateSocketA should have failed\n");
2665 err = WSAGetLastError();
2666 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2668 SetLastError(0xdeadbeef);
2669 ok(WSADuplicateSocketA(source, ~0, &info),
2670 "WSADuplicateSocketA should have failed\n");
2671 err = WSAGetLastError();
2672 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2674 SetLastError(0xdeadbeef);
2675 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info),
2676 "WSADuplicateSocketA should have failed\n");
2677 err = WSAGetLastError();
2678 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2680 SetLastError(0xdeadbeef);
2681 ok(WSADuplicateSocketA(source, GetCurrentProcessId(), NULL),
2682 "WSADuplicateSocketA should have failed\n");
2683 err = WSAGetLastError();
2684 ok(err == WSAEFAULT, "expected 10014, received %d\n", err);
2686 /* test returned structure */
2687 memset(&info, 0, sizeof(info));
2688 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2689 "WSADuplicateSocketA should have worked\n");
2691 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n",
2692 IPPROTO_TCP, info.iProtocol);
2693 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2694 AF_INET, info.iProtocol);
2695 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n",
2696 SOCK_STREAM, info.iSocketType);
2698 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2699 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2701 closesocket(dupsock);
2702 closesocket(source);
2704 /* create a socket, bind it, duplicate it then send data on source and
2705 * receive in the duplicated socket */
2706 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2707 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2709 memset(&info, 0, sizeof(info));
2710 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2711 "WSADuplicateSocketA should have worked\n");
2713 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n",
2714 IPPROTO_UDP, info.iProtocol);
2715 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2716 AF_INET, info.iProtocol);
2717 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2718 SOCK_DGRAM, info.iSocketType);
2720 memset(&addr, 0, sizeof(addr));
2721 addr.sin_family = AF_INET;
2722 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2723 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2724 "bind should have worked\n");
2726 /* read address to find out the port number to be used in sendto */
2727 memset(&addr, 0, sizeof(addr));
2728 addrsize = sizeof(addr);
2729 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2730 "getsockname should have worked\n");
2731 ok(addr.sin_port, "socket port should be != 0\n");
2733 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2734 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2736 size = sizeof(int);
2737 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2738 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
2739 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2740 SOCK_DGRAM, socktype);
2742 set_blocking(source, TRUE);
2744 /* send data on source socket */
2745 addrsize = sizeof(addr);
2746 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2747 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2749 /* receive on duplicated socket */
2750 addrsize = sizeof(addr);
2751 memset(buffer, 0, sizeof(buffer));
2752 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize);
2753 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2754 buffer[sizeof(teststr) - 1] = 0;
2755 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer);
2757 closesocket(dupsock);
2758 closesocket(source);
2760 /* show that the source socket need to be bound before the duplicated
2761 * socket is created */
2762 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2763 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2765 memset(&info, 0, sizeof(info));
2766 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2767 "WSADuplicateSocketA should have worked\n");
2769 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2770 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2772 memset(&addr, 0, sizeof(addr));
2773 addr.sin_family = AF_INET;
2774 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2775 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2776 "bind should have worked\n");
2778 /* read address to find out the port number to be used in sendto */
2779 memset(&addr, 0, sizeof(addr));
2780 addrsize = sizeof(addr);
2781 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2782 "getsockname should have worked\n");
2783 ok(addr.sin_port, "socket port should be != 0\n");
2785 set_blocking(source, TRUE);
2787 addrsize = sizeof(addr);
2788 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2789 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2791 SetLastError(0xdeadbeef);
2792 addrsize = sizeof(addr);
2793 memset(buffer, 0, sizeof(buffer));
2794 todo_wine {
2795 ok(recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize) == -1,
2796 "recvfrom should have failed\n");
2797 err = WSAGetLastError();
2798 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2801 closesocket(dupsock);
2802 closesocket(source);
2805 static void test_WSAEnumNetworkEvents(void)
2807 SOCKET s, s2;
2808 int sock_type[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM}, i, j, k, l;
2809 struct sockaddr_in address;
2810 HANDLE event;
2811 WSANETWORKEVENTS net_events;
2813 memset(&address, 0, sizeof(address));
2814 address.sin_addr.s_addr = htonl(INADDR_ANY);
2815 address.sin_family = AF_INET;
2817 /* This test follows the steps from bugs 10204 and 24946 */
2818 for (l = 0; l < 2; l++)
2820 for (i = 0; i < ARRAY_SIZE(sock_type); i++)
2822 if (i == 2)
2823 tcp_socketpair(&s, &s2);
2824 else
2826 s = socket(AF_INET, sock_type[i], 0);
2827 ok (s != SOCKET_ERROR, "Test[%d]: failed to create socket\n", i);
2828 ok (!bind(s, (struct sockaddr*) &address, sizeof(address)), "Test[%d]: bind failed\n", i);
2830 event = WSACreateEvent();
2831 ok (event != NULL, "Test[%d]: failed to create event\n", i);
2832 for (j = 0; j < 5; j++) /* Repeat sometimes and the result must be the same */
2834 /* When the TCP socket is not connected NO events will be returned.
2835 * When connected and no data pending it will get the write event.
2836 * UDP sockets don't have connections so as soon as they are bound
2837 * they can read/write data. Since nobody is sendind us data only
2838 * the write event will be returned and ONLY once.
2840 ok (!WSAEventSelect(s, event, FD_READ | FD_WRITE), "Test[%d]: WSAEventSelect failed\n", i);
2841 memset(&net_events, 0xAB, sizeof(net_events));
2842 ok (!WSAEnumNetworkEvents(s, l == 0 ? event : NULL, &net_events),
2843 "Test[%d]: WSAEnumNetworkEvents failed\n", i);
2844 if (i >= 1 && j == 0) /* FD_WRITE is SET on first try for UDP and connected TCP */
2846 ok (net_events.lNetworkEvents == FD_WRITE, "Test[%d]: expected 2, got %d\n",
2847 i, net_events.lNetworkEvents);
2849 else
2851 todo_wine_if (i != 0) /* Remove when fixed */
2852 ok (net_events.lNetworkEvents == 0, "Test[%d]: expected 0, got %d\n",
2853 i, net_events.lNetworkEvents);
2855 for (k = 0; k < FD_MAX_EVENTS; k++)
2857 if (net_events.lNetworkEvents & (1 << k))
2859 ok (net_events.iErrorCode[k] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n",
2860 i, k, net_events.iErrorCode[k]);
2862 else
2864 /* Bits that are not set in lNetworkEvents MUST not be changed */
2865 ok (net_events.iErrorCode[k] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n",
2866 i, k, net_events.iErrorCode[k]);
2870 closesocket(s);
2871 WSACloseEvent(event);
2872 if (i == 2) closesocket(s2);
2877 static void test_WSAAddressToString(void)
2879 static struct
2881 ULONG address;
2882 USHORT port;
2883 char output[32];
2885 ipv4_tests[] =
2887 { 0, 0, "0.0.0.0" },
2888 { 0xffffffff, 0, "255.255.255.255" },
2889 { 0, 0xffff, "0.0.0.0:65535" },
2890 { 0xffffffff, 0xffff, "255.255.255.255:65535" },
2892 static struct
2894 USHORT address[8];
2895 ULONG scope;
2896 USHORT port;
2897 char output[64];
2899 ipv6_tests[] =
2901 { { 0, 0, 0, 0, 0, 0, 0, 0x100 }, 0, 0, "::1" },
2902 { { 0xab20, 0, 0, 0, 0, 0, 0, 0x100 }, 0, 0, "20ab::1" },
2903 { { 0xab20, 0, 0, 0, 0, 0, 0, 0x120 }, 0, 0xfa81, "[20ab::2001]:33274" },
2904 { { 0xab20, 0, 0, 0, 0, 0, 0, 0x120 }, 0x1234, 0xfa81, "[20ab::2001%4660]:33274" },
2905 { { 0xab20, 0, 0, 0, 0, 0, 0, 0x120 }, 0x1234, 0, "20ab::2001%4660" },
2907 SOCKADDR_IN sockaddr;
2908 SOCKADDR_IN6 sockaddr6;
2909 char output[64];
2910 WCHAR outputW[64], expected_outputW[64];
2911 SOCKET v6;
2912 INT ret;
2913 DWORD len;
2914 int i, j;
2916 len = 0;
2917 sockaddr.sin_family = AF_INET;
2918 sockaddr.sin_addr.s_addr = 0;
2919 sockaddr.sin_port = 0;
2920 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, output, &len );
2921 ok( ret == SOCKET_ERROR, "WSAAddressToStringA() returned %d, expected SOCKET_ERROR\n", ret );
2922 ok( WSAGetLastError() == WSAEFAULT, "WSAAddressToStringA() gave error %d, expected WSAEFAULT\n", WSAGetLastError() );
2923 ok( len == 8, "WSAAddressToStringA() gave length %d, expected 8\n", len );
2925 for (i = 0; i < 2; i++)
2927 for (j = 0; j < ARRAY_SIZE(ipv4_tests); j++)
2929 sockaddr.sin_family = AF_INET;
2930 sockaddr.sin_addr.s_addr = ipv4_tests[j].address;
2931 sockaddr.sin_port = ipv4_tests[j].port;
2933 if (i == 0)
2935 len = sizeof(output);
2936 memset(output, 0, len);
2937 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, output, &len );
2938 ok( !ret, "ipv4_tests[%d] failed unexpectedly: %d\n", j, WSAGetLastError() );
2939 ok( lstrcmpA( output, ipv4_tests[j].output ) == 0,
2940 "ipv4_tests[%d]: got address %s, expected %s\n",
2941 j, wine_dbgstr_a(output), wine_dbgstr_a(ipv4_tests[j].output) );
2942 ok( len == lstrlenA(ipv4_tests[j].output) + 1,
2943 "ipv4_tests[%d]: got length %d, expected %d\n",
2944 j, len, lstrlenA(ipv4_tests[j].output) + 1 );
2946 else
2948 len = sizeof(outputW);
2949 memset(outputW, 0, len);
2950 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, outputW, &len );
2951 MultiByteToWideChar( CP_ACP, 0, ipv4_tests[j].output, -1,
2952 expected_outputW, ARRAY_SIZE(expected_outputW) );
2953 ok( !ret, "ipv4_tests[%d] failed unexpectedly: %d\n", j, WSAGetLastError() );
2954 ok( lstrcmpW( outputW, expected_outputW ) == 0,
2955 "ipv4_tests[%d]: got address %s, expected %s\n",
2956 j, wine_dbgstr_w(outputW), wine_dbgstr_w(expected_outputW) );
2957 ok( len == lstrlenW(expected_outputW) + 1,
2958 "ipv4_tests[%d]: got length %d, expected %d\n",
2959 j, len, lstrlenW(expected_outputW) + 1 );
2963 /* check to see if IPv6 is available */
2964 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2965 if (v6 == INVALID_SOCKET) {
2966 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2967 WSAGetLastError(), WSAEAFNOSUPPORT);
2968 continue;
2970 closesocket(v6);
2972 for (j = 0; j < ARRAY_SIZE(ipv6_tests); j++)
2974 sockaddr6.sin6_family = AF_INET6;
2975 sockaddr6.sin6_scope_id = ipv6_tests[j].scope;
2976 sockaddr6.sin6_port = ipv6_tests[j].port;
2977 memcpy( sockaddr6.sin6_addr.s6_addr, ipv6_tests[j].address, sizeof(ipv6_tests[j].address) );
2979 if (i == 0)
2981 len = sizeof(output);
2982 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, output, &len );
2983 ok( !ret, "ipv6_tests[%d] failed unexpectedly: %d\n", j, WSAGetLastError() );
2984 ok( lstrcmpA( output, ipv6_tests[j].output ) == 0,
2985 "ipv6_tests[%d]: gave address %s, expected %s\n",
2986 j, wine_dbgstr_a(output), wine_dbgstr_a(ipv6_tests[j].output) );
2987 ok( len == lstrlenA(ipv6_tests[j].output) + 1,
2988 "ipv6_tests[%d]: got length %d, expected %d\n",
2989 j, len, lstrlenA(ipv6_tests[j].output) + 1 );
2991 else
2993 len = sizeof(outputW);
2994 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, outputW, &len );
2995 MultiByteToWideChar( CP_ACP, 0, ipv6_tests[j].output, -1,
2996 expected_outputW, ARRAY_SIZE(expected_outputW) );
2997 ok( !ret, "ipv6_tests[%d] failed unexpectedly: %d\n", j, WSAGetLastError() );
2998 ok( lstrcmpW( outputW, expected_outputW ) == 0,
2999 "ipv6_tests[%d]: got address %s, expected %s\n",
3000 j, wine_dbgstr_w(outputW), wine_dbgstr_w(expected_outputW) );
3001 ok( len == lstrlenW(expected_outputW) + 1,
3002 "ipv6_tests[%d]: got length %d, expected %d\n",
3003 j, len, lstrlenW(expected_outputW) + 1 );
3009 static void test_WSAStringToAddress(void)
3011 static struct
3013 char input[32];
3014 ULONG address;
3015 USHORT port;
3016 int error;
3018 ipv4_tests[] =
3020 { "0.0.0.0", 0 },
3021 { "127.127.127.127", 0x7f7f7f7f },
3022 { "255.255.255.255", 0xffffffff },
3023 { "127.127.127.127:65535", 0x7f7f7f7f, 65535 },
3024 { "255.255.255.255:65535", 0xffffffff, 65535 },
3025 { "2001::1", 0xd1070000, 0, WSAEINVAL },
3026 { "1.2.3.", 0, 0, WSAEINVAL },
3027 { "", 0, 0, WSAEINVAL },
3029 static struct
3031 char input[64];
3032 USHORT address[8];
3033 USHORT port;
3034 int error;
3036 ipv6_tests[] =
3038 { "::1", { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
3039 { "[::1]", { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
3040 { "[::1]:65535", { 0, 0, 0, 0, 0, 0, 0, 0x100 }, 0xffff },
3041 { "2001::1", { 0x120, 0, 0, 0, 0, 0, 0, 0x100 } },
3042 { "::1]:65535", { 0, 0, 0, 0, 0, 0, 0, 0x100 }, 0, WSAEINVAL },
3043 { "001::1", { 0x100, 0, 0, 0, 0, 0, 0, 0x100 } },
3044 { "::1:2:3:4:5:6:7", { 0, 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600 }, 0, WSAEINVAL }, /* Windows bug */
3045 { "1.2.3.4", { 0x201, 0x3, 0, 0, 0, 0, 0, 0 }, 0, WSAEINVAL },
3046 { "1:2:3:", { 0x100, 0x200, 0x300, 0, 0, 0, 0 }, 0, WSAEINVAL },
3047 { "", { 0, 0, 0, 0, 0, 0, 0, 0 }, 0, WSAEINVAL },
3050 WCHAR inputW[64];
3051 INT len, ret, expected_len, expected_ret;
3052 short expected_family;
3053 SOCKADDR_IN sockaddr;
3054 SOCKADDR_IN6 sockaddr6;
3055 int i, j;
3057 len = 0;
3058 WSASetLastError( 0 );
3059 ret = WSAStringToAddressA( ipv4_tests[0].input, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3060 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() returned %d, expected SOCKET_ERROR\n", ret );
3061 ok( WSAGetLastError() == WSAEFAULT, "WSAStringToAddress() gave error %d, expected WSAEFAULT\n", WSAGetLastError() );
3062 ok( len >= sizeof(sockaddr) || broken(len == 0) /* xp */,
3063 "WSAStringToAddress() gave length %d, expected at least %d\n", len, sizeof(sockaddr) );
3065 for (i = 0; i < 2; i++)
3067 for (j = 0; j < ARRAY_SIZE(ipv4_tests); j++)
3069 len = sizeof(sockaddr) + 10;
3070 expected_len = ipv4_tests[j].error ? len : sizeof(sockaddr);
3071 memset( &sockaddr, 0xab, sizeof(sockaddr) );
3073 WSASetLastError( 0 );
3074 if (i == 0)
3076 ret = WSAStringToAddressA( ipv4_tests[j].input, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3078 else
3080 MultiByteToWideChar( CP_ACP, 0, ipv4_tests[j].input, -1, inputW, ARRAY_SIZE(inputW) );
3081 ret = WSAStringToAddressW( inputW, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3083 expected_ret = ipv4_tests[j].error ? SOCKET_ERROR : 0;
3084 expected_family = ipv4_tests[j].error ? 0 : AF_INET;
3085 ok( ret == expected_ret,
3086 "WSAStringToAddress(%s) returned %d, expected %d\n",
3087 wine_dbgstr_a( ipv4_tests[j].input ), ret, expected_ret );
3088 ok( WSAGetLastError() == ipv4_tests[j].error,
3089 "WSAStringToAddress(%s) gave error %d, expected %d\n",
3090 wine_dbgstr_a( ipv4_tests[j].input ), WSAGetLastError(), ipv4_tests[j].error );
3091 ok( sockaddr.sin_family == expected_family,
3092 "WSAStringToAddress(%s) gave family %d, expected %d\n",
3093 wine_dbgstr_a( ipv4_tests[j].input ), sockaddr.sin_family, expected_family );
3094 ok( sockaddr.sin_addr.s_addr == ipv4_tests[j].address,
3095 "WSAStringToAddress(%s) gave address %08x, expected %08x\n",
3096 wine_dbgstr_a( ipv4_tests[j].input ), sockaddr.sin_addr.s_addr, ipv4_tests[j].address );
3097 ok( sockaddr.sin_port == ipv4_tests[j].port,
3098 "WSAStringToAddress(%s) gave port %04x, expected %04x\n",
3099 wine_dbgstr_a( ipv4_tests[j].input ), sockaddr.sin_port, ipv4_tests[j].port );
3100 ok( len == expected_len,
3101 "WSAStringToAddress(%s) gave length %d, expected %d\n",
3102 wine_dbgstr_a( ipv4_tests[j].input ), len, expected_len );
3105 for (j = 0; j < ARRAY_SIZE(ipv6_tests); j++)
3107 len = sizeof(sockaddr6) + 10;
3108 expected_len = ipv6_tests[j].error ? len : sizeof(sockaddr6);
3109 memset( &sockaddr6, 0xab, sizeof(sockaddr6) );
3111 WSASetLastError( 0 );
3112 if (i == 0)
3114 ret = WSAStringToAddressA( ipv6_tests[j].input, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, &len );
3116 else
3118 MultiByteToWideChar( CP_ACP, 0, ipv6_tests[j].input, -1, inputW, ARRAY_SIZE(inputW) );
3119 ret = WSAStringToAddressW( inputW, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, &len );
3121 if (j == 0 && ret == SOCKET_ERROR)
3123 win_skip("IPv6 not supported\n");
3124 break;
3126 expected_ret = ipv6_tests[j].error ? SOCKET_ERROR : 0;
3127 expected_family = ipv6_tests[j].error ? 0 : AF_INET6;
3128 ok( ret == expected_ret,
3129 "WSAStringToAddress(%s) returned %d, expected %d\n",
3130 wine_dbgstr_a( ipv6_tests[j].input ), ret, expected_ret );
3131 ok( WSAGetLastError() == ipv6_tests[j].error,
3132 "WSAStringToAddress(%s) gave error %d, expected %d\n",
3133 wine_dbgstr_a( ipv6_tests[j].input ), WSAGetLastError(), ipv6_tests[j].error );
3134 ok( sockaddr6.sin6_family == expected_family,
3135 "WSAStringToAddress(%s) gave family %d, expected %d\n",
3136 wine_dbgstr_a( ipv4_tests[j].input ), sockaddr6.sin6_family, expected_family );
3137 ok( memcmp(&sockaddr6.sin6_addr, ipv6_tests[j].address, sizeof(sockaddr6.sin6_addr)) == 0,
3138 "WSAStringToAddress(%s) gave address %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
3139 wine_dbgstr_a( ipv6_tests[j].input ),
3140 sockaddr6.sin6_addr.s6_words[0], sockaddr6.sin6_addr.s6_words[1],
3141 sockaddr6.sin6_addr.s6_words[2], sockaddr6.sin6_addr.s6_words[3],
3142 sockaddr6.sin6_addr.s6_words[4], sockaddr6.sin6_addr.s6_words[5],
3143 sockaddr6.sin6_addr.s6_words[6], sockaddr6.sin6_addr.s6_words[7],
3144 ipv6_tests[j].address[0], ipv6_tests[j].address[1],
3145 ipv6_tests[j].address[2], ipv6_tests[j].address[3],
3146 ipv6_tests[j].address[4], ipv6_tests[j].address[5],
3147 ipv6_tests[j].address[6], ipv6_tests[j].address[7] );
3148 ok( sockaddr6.sin6_scope_id == 0,
3149 "WSAStringToAddress(%s) gave scope %d, expected 0\n",
3150 wine_dbgstr_a( ipv6_tests[j].input ), sockaddr6.sin6_scope_id );
3151 ok( sockaddr6.sin6_port == ipv6_tests[j].port,
3152 "WSAStringToAddress(%s) gave port %04x, expected %04x\n",
3153 wine_dbgstr_a( ipv6_tests[j].input ), sockaddr6.sin6_port, ipv6_tests[j].port );
3154 ok( sockaddr6.sin6_flowinfo == 0,
3155 "WSAStringToAddress(%s) gave flowinfo %d, expected 0\n",
3156 wine_dbgstr_a( ipv6_tests[j].input ), sockaddr6.sin6_flowinfo );
3157 ok( len == expected_len,
3158 "WSAStringToAddress(%s) gave length %d, expected %d\n",
3159 wine_dbgstr_a( ipv6_tests[j].input ), len, expected_len );
3164 static DWORD WINAPI SelectReadThread(void *param)
3166 select_thread_params *par = param;
3167 fd_set readfds;
3168 int ret;
3169 struct sockaddr_in addr;
3170 struct timeval select_timeout;
3172 FD_ZERO(&readfds);
3173 FD_SET(par->s, &readfds);
3174 select_timeout.tv_sec=5;
3175 select_timeout.tv_usec=0;
3176 addr.sin_family = AF_INET;
3177 addr.sin_addr.s_addr = inet_addr(SERVERIP);
3178 addr.sin_port = htons(SERVERPORT);
3180 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
3181 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
3183 SetEvent(server_ready);
3184 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
3185 par->ReadKilled = (ret == 1);
3187 return 0;
3190 static DWORD WINAPI SelectCloseThread(void *param)
3192 SOCKET s = *(SOCKET*)param;
3193 Sleep(500);
3194 closesocket(s);
3195 return 0;
3198 static void test_errors(void)
3200 SOCKET sock;
3201 SOCKADDR_IN SockAddr;
3202 int ret, err;
3204 WSASetLastError(NO_ERROR);
3205 sock = socket(PF_INET, SOCK_STREAM, 0);
3206 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3207 memset(&SockAddr, 0, sizeof(SockAddr));
3208 SockAddr.sin_family = AF_INET;
3209 SockAddr.sin_port = htons(6924);
3210 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
3212 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
3213 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret );
3214 if (ret == SOCKET_ERROR)
3216 err = WSAGetLastError();
3217 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err );
3221 TIMEVAL timeval;
3222 fd_set set = {1, {sock}};
3224 timeval.tv_sec = 0;
3225 timeval.tv_usec = 50000;
3227 ret = select(1, NULL, &set, NULL, &timeval);
3228 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret );
3231 ret = closesocket(sock);
3232 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
3235 static void test_listen(void)
3237 SOCKET fdA, fdB;
3238 int ret, acceptc, olen = sizeof(acceptc);
3239 struct sockaddr_in address;
3241 memset(&address, 0, sizeof(address));
3242 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3243 address.sin_family = AF_INET;
3244 address.sin_port = htons(SERVERPORT);
3246 /* invalid socket tests */
3247 SetLastError(0xdeadbeef);
3248 ok ((listen(0, 0) == SOCKET_ERROR), "listen did not fail\n");
3249 ret = WSAGetLastError();
3250 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
3252 SetLastError(0xdeadbeef);
3253 ok ((listen(0xdeadbeef, 0) == SOCKET_ERROR), "listen did not fail\n");
3254 ret = WSAGetLastError();
3255 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
3257 /* tcp tests */
3258 fdA = socket(AF_INET, SOCK_STREAM, 0);
3259 ok ((fdA != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3261 fdB = socket(AF_INET, SOCK_STREAM, 0);
3262 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3264 SetLastError(0xdeadbeef);
3265 ok ((listen(fdA, -2) == SOCKET_ERROR), "listen did not fail\n");
3266 ret = WSAGetLastError();
3267 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3269 SetLastError(0xdeadbeef);
3270 ok ((listen(fdA, 1) == SOCKET_ERROR), "listen did not fail\n");
3271 ret = WSAGetLastError();
3272 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3274 SetLastError(0xdeadbeef);
3275 ok ((listen(fdA, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3276 ret = WSAGetLastError();
3277 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3279 ok (!bind(fdA, (struct sockaddr*) &address, sizeof(address)), "bind failed\n");
3281 SetLastError(0xdeadbeef);
3282 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
3283 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3285 acceptc = 0xdead;
3286 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
3287 ok (!ret, "getsockopt failed\n");
3288 ok (acceptc == 0, "SO_ACCEPTCONN should be 0, received %d\n", acceptc);
3290 ok (!listen(fdA, 0), "listen failed\n");
3291 ok (!listen(fdA, SOMAXCONN), "double listen failed\n");
3293 acceptc = 0xdead;
3294 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
3295 ok (!ret, "getsockopt failed\n");
3296 ok (acceptc == 1, "SO_ACCEPTCONN should be 1, received %d\n", acceptc);
3298 SetLastError(0xdeadbeef);
3299 ok ((listen(fdB, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3300 ret = WSAGetLastError();
3301 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3303 ret = closesocket(fdB);
3304 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3306 fdB = socket(AF_INET, SOCK_STREAM, 0);
3307 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3309 SetLastError(0xdeadbeef);
3310 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
3311 ret = WSAGetLastError();
3312 ok (ret == WSAEADDRINUSE, "expected 10048, received %d\n", ret);
3314 ret = closesocket(fdA);
3315 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3316 ret = closesocket(fdB);
3317 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3320 #define FD_ZERO_ALL() { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); }
3321 #define FD_SET_ALL(s) { FD_SET(s, &readfds); FD_SET(s, &writefds); FD_SET(s, &exceptfds); }
3322 static void test_select(void)
3324 static char tmp_buf[1024];
3326 SOCKET fdListen, fdRead, fdWrite;
3327 fd_set readfds, writefds, exceptfds;
3328 unsigned int maxfd;
3329 int ret, len;
3330 char buffer;
3331 struct timeval select_timeout;
3332 struct sockaddr_in address;
3333 select_thread_params thread_params;
3334 HANDLE thread_handle;
3335 DWORD ticks, id;
3337 fdRead = socket(AF_INET, SOCK_STREAM, 0);
3338 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3339 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
3340 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3342 maxfd = fdRead;
3343 if (fdWrite > maxfd)
3344 maxfd = fdWrite;
3346 FD_ZERO_ALL();
3347 FD_SET_ALL(fdRead);
3348 FD_SET_ALL(fdWrite);
3349 select_timeout.tv_sec=0;
3350 select_timeout.tv_usec=0;
3352 ticks = GetTickCount();
3353 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3354 ticks = GetTickCount() - ticks;
3355 ok(ret == 0, "select should not return any socket handles\n");
3356 ok(ticks < 10, "select was blocking for %u ms, expected < 10 ms\n", ticks);
3357 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3358 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
3359 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3360 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
3362 FD_ZERO_ALL();
3363 FD_SET_ALL(fdRead);
3364 FD_SET_ALL(fdWrite);
3365 select_timeout.tv_sec=0;
3366 select_timeout.tv_usec=500;
3368 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3369 ok(ret == 0, "select should not return any socket handles\n");
3370 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3371 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
3372 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3373 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
3375 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3376 ret = closesocket(fdWrite);
3377 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
3379 thread_params.s = fdRead;
3380 thread_params.ReadKilled = FALSE;
3381 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
3382 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id );
3383 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
3385 WaitForSingleObject (server_ready, INFINITE);
3386 Sleep(200);
3387 ret = closesocket(fdRead);
3388 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
3390 WaitForSingleObject (thread_handle, 1000);
3391 ok ( thread_params.ReadKilled, "closesocket did not wake up select\n");
3392 ret = recv(fdRead, &buffer, 1, MSG_PEEK);
3393 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret);
3395 /* Test selecting invalid handles */
3396 FD_ZERO_ALL();
3398 SetLastError(0);
3399 ret = select(maxfd+1, 0, 0, 0, &select_timeout);
3400 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3401 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3403 SetLastError(0);
3404 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3405 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3406 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3408 FD_SET(INVALID_SOCKET, &readfds);
3409 SetLastError(0);
3410 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3411 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3412 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3413 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3415 FD_ZERO(&readfds);
3416 FD_SET(INVALID_SOCKET, &writefds);
3417 SetLastError(0);
3418 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3419 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3420 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3421 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n");
3423 FD_ZERO(&writefds);
3424 FD_SET(INVALID_SOCKET, &exceptfds);
3425 SetLastError(0);
3426 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3427 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3428 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3429 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3431 tcp_socketpair(&fdRead, &fdWrite);
3432 maxfd = fdRead;
3433 if(fdWrite > maxfd) maxfd = fdWrite;
3435 FD_ZERO(&readfds);
3436 FD_SET(fdRead, &readfds);
3437 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
3438 ok(!ret, "select returned %d\n", ret);
3440 FD_ZERO(&writefds);
3441 FD_SET(fdWrite, &writefds);
3442 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
3443 ok(ret == 1, "select returned %d\n", ret);
3444 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3446 /* tests for overlapping fd_set pointers */
3447 FD_ZERO(&readfds);
3448 FD_SET(fdWrite, &readfds);
3449 ret = select(fdWrite+1, &readfds, &readfds, NULL, &select_timeout);
3450 ok(ret == 1, "select returned %d\n", ret);
3451 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3453 FD_ZERO(&readfds);
3454 FD_SET(fdWrite, &readfds);
3455 FD_SET(fdRead, &readfds);
3456 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3457 ok(ret == 2, "select returned %d\n", ret);
3458 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3459 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3461 ok(send(fdWrite, "test", 4, 0) == 4, "failed to send data\n");
3462 FD_ZERO(&readfds);
3463 FD_SET(fdRead, &readfds);
3464 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
3465 ok(ret == 1, "select returned %d\n", ret);
3466 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3468 FD_ZERO(&readfds);
3469 FD_SET(fdWrite, &readfds);
3470 FD_SET(fdRead, &readfds);
3471 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3472 ok(ret == 2, "select returned %d\n", ret);
3473 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3474 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3476 while(1) {
3477 FD_ZERO(&writefds);
3478 FD_SET(fdWrite, &writefds);
3479 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
3480 if(!ret) break;
3481 ok(send(fdWrite, tmp_buf, sizeof(tmp_buf), 0) > 0, "failed to send data\n");
3483 FD_ZERO(&readfds);
3484 FD_SET(fdWrite, &readfds);
3485 FD_SET(fdRead, &readfds);
3486 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3487 ok(ret == 1, "select returned %d\n", ret);
3488 ok(!FD_ISSET(fdWrite, &readfds), "fdWrite socket is in the set\n");
3489 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3491 ok(send(fdRead, "test", 4, 0) == 4, "failed to send data\n");
3492 Sleep(100);
3493 FD_ZERO(&readfds);
3494 FD_SET(fdWrite, &readfds);
3495 FD_SET(fdRead, &readfds);
3496 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3497 ok(ret == 2, "select returned %d\n", ret);
3498 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3499 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3501 closesocket(fdRead);
3502 closesocket(fdWrite);
3504 /* select() works in 3 distinct states:
3505 * - to check if a connection attempt ended with success or error;
3506 * - to check if a pending connection is waiting for acceptance;
3507 * - to check for data to read, availability for write and OOB data
3509 * The tests below ensure that all conditions are tested.
3511 memset(&address, 0, sizeof(address));
3512 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3513 address.sin_family = AF_INET;
3514 len = sizeof(address);
3515 fdListen = setup_server_socket(&address, &len);
3516 select_timeout.tv_sec = 1;
3517 select_timeout.tv_usec = 250000;
3519 /* When no events are pending select returns 0 with no error */
3520 FD_ZERO_ALL();
3521 FD_SET_ALL(fdListen);
3522 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3523 ok(ret == 0, "expected 0, got %d\n", ret);
3525 /* When a socket is attempting to connect the listening socket receives the read descriptor */
3526 fdWrite = setup_connector_socket(&address, len, TRUE);
3527 FD_ZERO_ALL();
3528 FD_SET_ALL(fdListen);
3529 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3530 ok(ret == 1, "expected 1, got %d\n", ret);
3531 ok(FD_ISSET(fdListen, &readfds), "fdListen socket is not in the set\n");
3532 len = sizeof(address);
3533 fdRead = accept(fdListen, (struct sockaddr*) &address, &len);
3534 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n");
3536 /* The connector is signaled through the write descriptor */
3537 FD_ZERO_ALL();
3538 FD_SET_ALL(fdListen);
3539 FD_SET_ALL(fdRead);
3540 FD_SET_ALL(fdWrite);
3541 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3542 ok(ret == 2, "expected 2, got %d\n", ret);
3543 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3544 ok(FD_ISSET(fdRead, &writefds), "fdRead socket is not in the set\n");
3545 len = sizeof(id);
3546 id = 0xdeadbeef;
3547 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
3548 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
3549 ok(id == 0, "expected 0, got %d\n", id);
3551 /* When data is received the receiver gets the read descriptor */
3552 ret = send(fdWrite, "1234", 4, 0);
3553 ok(ret == 4, "expected 4, got %d\n", ret);
3554 FD_ZERO_ALL();
3555 FD_SET_ALL(fdListen);
3556 FD_SET(fdRead, &readfds);
3557 FD_SET(fdRead, &exceptfds);
3558 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3559 ok(ret == 1, "expected 1, got %d\n", ret);
3560 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3561 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
3562 ok(ret == 4, "expected 4, got %d\n", ret);
3563 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n");
3565 /* When OOB data is received the socket is set in the except descriptor */
3566 ret = send(fdWrite, "A", 1, MSG_OOB);
3567 ok(ret == 1, "expected 1, got %d\n", ret);
3568 FD_ZERO_ALL();
3569 FD_SET_ALL(fdListen);
3570 FD_SET(fdRead, &readfds);
3571 FD_SET(fdRead, &exceptfds);
3572 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3573 ok(ret == 1, "expected 1, got %d\n", ret);
3574 ok(FD_ISSET(fdRead, &exceptfds), "fdRead socket is not in the set\n");
3575 tmp_buf[0] = 0xAF;
3576 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
3577 ok(ret == 1, "expected 1, got %d\n", ret);
3578 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
3580 /* If the socket is OOBINLINED it will not receive the OOB in except fds */
3581 ret = 1;
3582 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret));
3583 ok(ret == 0, "expected 0, got %d\n", ret);
3584 ret = send(fdWrite, "A", 1, MSG_OOB);
3585 ok(ret == 1, "expected 1, got %d\n", ret);
3586 FD_ZERO_ALL();
3587 FD_SET_ALL(fdListen);
3588 FD_SET(fdRead, &readfds);
3589 FD_SET(fdRead, &exceptfds);
3590 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3591 ok(ret == 1, "expected 1, got %d\n", ret);
3592 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3593 tmp_buf[0] = 0xAF;
3594 SetLastError(0xdeadbeef);
3595 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
3596 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
3597 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError());
3598 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
3599 ok(ret == 1, "expected 1, got %d\n", ret);
3600 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
3602 /* When the connection is closed the socket is set in the read descriptor */
3603 ret = closesocket(fdRead);
3604 ok(ret == 0, "expected 0, got %d\n", ret);
3605 FD_ZERO_ALL();
3606 FD_SET_ALL(fdListen);
3607 FD_SET(fdWrite, &readfds);
3608 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3609 ok(ret == 1, "expected 1, got %d\n", ret);
3610 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3611 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0);
3612 ok(ret == 0, "expected 0, got %d\n", ret);
3613 ret = closesocket(fdWrite);
3614 ok(ret == 0, "expected 0, got %d\n", ret);
3615 ret = closesocket(fdListen);
3616 ok(ret == 0, "expected 0, got %d\n", ret);
3618 /* w10pro64 sometimes takes over 2 seconds for an error to be reported. */
3619 if (winetest_interactive)
3621 len = sizeof(address);
3622 fdWrite = setup_connector_socket(&address, len, TRUE);
3623 FD_ZERO_ALL();
3624 FD_SET(fdWrite, &writefds);
3625 FD_SET(fdWrite, &exceptfds);
3626 select_timeout.tv_sec = 10;
3627 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3628 ok(ret == 1, "expected 1, got %d\n", ret);
3629 len = sizeof(id);
3630 id = 0xdeadbeef;
3631 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
3632 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
3633 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id);
3634 ok(FD_ISSET(fdWrite, &exceptfds), "fdWrite socket is not in the set\n");
3635 ok(select_timeout.tv_usec == 250000, "select timeout should not have changed\n");
3636 closesocket(fdWrite);
3639 /* Try select() on a closed socket after connection */
3640 tcp_socketpair(&fdRead, &fdWrite);
3641 closesocket(fdRead);
3642 FD_ZERO_ALL();
3643 FD_SET_ALL(fdWrite);
3644 FD_SET_ALL(fdRead);
3645 SetLastError(0xdeadbeef);
3646 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
3647 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3648 ok(GetLastError() == WSAENOTSOCK, "got %d\n", GetLastError());
3649 /* descriptor sets are unchanged */
3650 ok(readfds.fd_count == 2, "expected 2, got %d\n", readfds.fd_count);
3651 ok(exceptfds.fd_count == 2, "expected 2, got %d\n", exceptfds.fd_count);
3652 closesocket(fdWrite);
3654 /* Close the socket currently being selected in a thread - bug 38399 */
3655 tcp_socketpair(&fdRead, &fdWrite);
3656 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
3657 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
3658 FD_ZERO_ALL();
3659 FD_SET_ALL(fdWrite);
3660 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
3661 ok(ret == 1, "expected 1, got %d\n", ret);
3662 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3663 WaitForSingleObject (thread_handle, 1000);
3664 closesocket(fdRead);
3665 /* test again with only the except descriptor */
3666 tcp_socketpair(&fdRead, &fdWrite);
3667 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
3668 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
3669 FD_ZERO_ALL();
3670 FD_SET(fdWrite, &exceptfds);
3671 SetLastError(0xdeadbeef);
3672 ret = select(0, NULL, NULL, &exceptfds, &select_timeout);
3673 todo_wine
3674 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3675 ok(GetLastError() == WSAENOTSOCK, "got %d\n", GetLastError());
3676 WaitForSingleObject (thread_handle, 1000);
3677 closesocket(fdRead);
3679 /* test UDP behavior of unbound sockets */
3680 select_timeout.tv_sec = 0;
3681 select_timeout.tv_usec = 250000;
3682 fdWrite = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3683 ok(fdWrite != INVALID_SOCKET, "socket call failed\n");
3684 FD_ZERO_ALL();
3685 FD_SET_ALL(fdWrite);
3686 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3687 ok(ret == 1, "expected 1, got %d\n", ret);
3688 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3689 closesocket(fdWrite);
3691 #undef FD_SET_ALL
3692 #undef FD_ZERO_ALL
3694 static DWORD WINAPI AcceptKillThread(void *param)
3696 select_thread_params *par = param;
3697 struct sockaddr_in address;
3698 int len = sizeof(address);
3699 SOCKET client_socket;
3701 SetEvent(server_ready);
3702 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
3703 if (client_socket != INVALID_SOCKET)
3704 closesocket(client_socket);
3705 par->ReadKilled = (client_socket == INVALID_SOCKET);
3706 return 0;
3710 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
3711 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
3712 GROUP *g, DWORD_PTR dwCallbackData)
3714 return CF_DEFER;
3717 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len)
3719 int ret, val;
3720 SOCKET server_socket;
3722 server_socket = socket(AF_INET, SOCK_STREAM, 0);
3723 ok(server_socket != INVALID_SOCKET, "failed to bind socket, error %u\n", WSAGetLastError());
3725 val = 1;
3726 ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
3727 ok(!ret, "failed to set SO_REUSEADDR, error %u\n", WSAGetLastError());
3729 ret = bind(server_socket, (struct sockaddr *)addr, *len);
3730 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3732 ret = getsockname(server_socket, (struct sockaddr *)addr, len);
3733 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
3735 ret = listen(server_socket, 5);
3736 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
3738 return server_socket;
3741 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock)
3743 int ret;
3744 SOCKET connector;
3746 connector = socket(AF_INET, SOCK_STREAM, 0);
3747 ok(connector != INVALID_SOCKET, "failed to create connector socket %d\n", WSAGetLastError());
3749 if (nonblock)
3750 set_blocking(connector, !nonblock);
3752 ret = connect(connector, (struct sockaddr *)addr, len);
3753 if (!nonblock)
3754 ok(!ret, "connecting to accepting socket failed %d\n", WSAGetLastError());
3755 else if (ret == SOCKET_ERROR)
3756 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
3758 return connector;
3761 static void test_accept(void)
3763 int ret;
3764 SOCKET server_socket, accepted = INVALID_SOCKET, connector;
3765 struct sockaddr_in address;
3766 SOCKADDR_STORAGE ss, ss_empty;
3767 int socklen;
3768 select_thread_params thread_params;
3769 HANDLE thread_handle = NULL;
3770 DWORD id;
3772 memset(&address, 0, sizeof(address));
3773 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3774 address.sin_family = AF_INET;
3776 socklen = sizeof(address);
3777 server_socket = setup_server_socket(&address, &socklen);
3779 connector = setup_connector_socket(&address, socklen, FALSE);
3780 if (connector == INVALID_SOCKET) goto done;
3782 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0);
3783 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
3785 accepted = accept(server_socket, NULL, 0);
3786 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
3788 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
3790 thread_params.s = server_socket;
3791 thread_params.ReadKilled = FALSE;
3792 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id);
3794 WaitForSingleObject(server_ready, INFINITE);
3795 Sleep(200);
3796 ret = closesocket(server_socket);
3797 ok(!ret, "failed to close socket, error %u\n", WSAGetLastError());
3799 WaitForSingleObject(thread_handle, 1000);
3800 ok(thread_params.ReadKilled, "closesocket did not wake up accept\n");
3802 closesocket(accepted);
3803 closesocket(connector);
3804 accepted = connector = INVALID_SOCKET;
3806 socklen = sizeof(address);
3807 server_socket = setup_server_socket(&address, &socklen);
3809 connector = setup_connector_socket(&address, socklen, FALSE);
3810 if (connector == INVALID_SOCKET) goto done;
3812 socklen = 0;
3813 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
3814 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
3815 ok(!socklen, "got %d\n", socklen);
3816 closesocket(connector);
3817 connector = INVALID_SOCKET;
3819 socklen = sizeof(address);
3820 connector = setup_connector_socket(&address, socklen, FALSE);
3821 if (connector == INVALID_SOCKET) goto done;
3823 accepted = WSAAccept(server_socket, NULL, NULL, NULL, 0);
3824 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3825 closesocket(accepted);
3826 closesocket(connector);
3827 accepted = connector = INVALID_SOCKET;
3829 socklen = sizeof(address);
3830 connector = setup_connector_socket(&address, socklen, FALSE);
3831 if (connector == INVALID_SOCKET) goto done;
3833 socklen = sizeof(ss);
3834 memset(&ss, 0, sizeof(ss));
3835 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
3836 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3837 ok(socklen != sizeof(ss), "unexpected length\n");
3838 ok(ss.ss_family, "family not set\n");
3839 closesocket(accepted);
3840 closesocket(connector);
3841 accepted = connector = INVALID_SOCKET;
3843 socklen = sizeof(address);
3844 connector = setup_connector_socket(&address, socklen, FALSE);
3845 if (connector == INVALID_SOCKET) goto done;
3847 socklen = 0;
3848 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
3849 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
3850 ok(!socklen, "got %d\n", socklen);
3851 closesocket(connector);
3852 accepted = connector = INVALID_SOCKET;
3854 socklen = sizeof(address);
3855 connector = setup_connector_socket(&address, socklen, FALSE);
3856 if (connector == INVALID_SOCKET) goto done;
3858 accepted = accept(server_socket, NULL, NULL);
3859 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3860 closesocket(accepted);
3861 closesocket(connector);
3862 accepted = connector = INVALID_SOCKET;
3864 socklen = sizeof(address);
3865 connector = setup_connector_socket(&address, socklen, FALSE);
3866 if (connector == INVALID_SOCKET) goto done;
3868 socklen = sizeof(ss);
3869 memset(&ss, 0, sizeof(ss));
3870 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
3871 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3872 ok(socklen != sizeof(ss), "unexpected length\n");
3873 ok(ss.ss_family, "family not set\n");
3874 closesocket(accepted);
3875 closesocket(connector);
3876 accepted = connector = INVALID_SOCKET;
3878 socklen = sizeof(address);
3879 connector = setup_connector_socket(&address, socklen, FALSE);
3880 if (connector == INVALID_SOCKET) goto done;
3882 memset(&ss, 0, sizeof(ss));
3883 memset(&ss_empty, 0, sizeof(ss_empty));
3884 accepted = accept(server_socket, (struct sockaddr *)&ss, NULL);
3885 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3886 ok(!memcmp(&ss, &ss_empty, sizeof(ss)), "structure is different\n");
3888 done:
3889 if (accepted != INVALID_SOCKET)
3890 closesocket(accepted);
3891 if (connector != INVALID_SOCKET)
3892 closesocket(connector);
3893 if (thread_handle != NULL)
3894 CloseHandle(thread_handle);
3895 if (server_ready != INVALID_HANDLE_VALUE)
3896 CloseHandle(server_ready);
3897 if (server_socket != INVALID_SOCKET)
3898 closesocket(server_socket);
3901 static void test_extendedSocketOptions(void)
3903 WSADATA wsa;
3904 SOCKET sock;
3905 struct sockaddr_in sa;
3906 int sa_len = sizeof(struct sockaddr_in);
3907 int optval, optlen = sizeof(int), ret;
3908 BOOL bool_opt_val;
3909 LINGER linger_val;
3911 ret = WSAStartup(MAKEWORD(2,0), &wsa);
3912 ok(!ret, "failed to startup, error %u\n", WSAGetLastError());
3914 memset(&sa, 0, sa_len);
3916 sa.sin_family = AF_INET;
3917 sa.sin_port = htons(0);
3918 sa.sin_addr.s_addr = htonl(INADDR_ANY);
3920 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
3921 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
3923 ret = bind(sock, (struct sockaddr *) &sa, sa_len);
3924 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3926 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3928 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
3929 ok((optval == 65507) || (optval == 65527),
3930 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
3932 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
3933 SetLastError(0xdeadbeef);
3934 optval = 0xdeadbeef;
3935 optlen = sizeof(int);
3936 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3937 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3938 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3939 ret, WSAGetLastError(), optval, optval);
3941 /* more invalid values for level */
3942 SetLastError(0xdeadbeef);
3943 optval = 0xdeadbeef;
3944 optlen = sizeof(int);
3945 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3946 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3947 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3948 ret, WSAGetLastError(), optval, optval);
3950 SetLastError(0xdeadbeef);
3951 optval = 0xdeadbeef;
3952 optlen = sizeof(int);
3953 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3954 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3955 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3956 ret, WSAGetLastError(), optval, optval);
3958 SetLastError(0xdeadbeef);
3959 optval = 0xdeadbeef;
3960 optlen = sizeof(int);
3961 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3962 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3963 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3964 ret, WSAGetLastError(), optval, optval);
3966 SetLastError(0xdeadbeef);
3967 optval = 0xdeadbeef;
3968 optlen = sizeof(int);
3969 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3970 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3971 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3972 ret, WSAGetLastError(), optval, optval);
3974 SetLastError(0xdeadbeef);
3975 optlen = sizeof(LINGER);
3976 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
3977 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT),
3978 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
3979 ret, WSAGetLastError());
3980 closesocket(sock);
3982 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
3983 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
3985 ret = bind(sock, (struct sockaddr *) &sa, sa_len);
3986 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3988 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
3989 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
3991 optlen = sizeof(BOOL);
3992 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
3993 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
3994 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
3995 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
3996 bool_opt_val, linger_val.l_onoff);
3998 closesocket(sock);
3999 WSACleanup();
4002 static void test_getsockname(void)
4004 WSADATA wsa;
4005 SOCKET sock;
4006 struct sockaddr_in sa_set, sa_get;
4007 int sa_set_len = sizeof(struct sockaddr_in);
4008 int sa_get_len = sa_set_len;
4009 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
4010 int ret;
4011 struct hostent *h;
4013 ret = WSAStartup(MAKEWORD(2,0), &wsa);
4014 ok(!ret, "failed to startup, error %u\n", WSAGetLastError());
4016 memset(&sa_set, 0, sa_set_len);
4018 sa_set.sin_family = AF_INET;
4019 sa_set.sin_port = htons(0);
4020 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
4022 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
4023 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
4025 sa_get = sa_set;
4026 ret = getsockname(sock, (struct sockaddr *)&sa_get, &sa_get_len);
4027 ok(ret == SOCKET_ERROR, "expected failure\n");
4028 ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
4029 ok(!memcmp(&sa_get, &sa_set, sizeof(sa_get)), "address should not be changed\n");
4031 ret = bind(sock, (struct sockaddr *) &sa_set, sa_set_len);
4032 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
4034 ret = getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len);
4035 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
4037 ret = memcmp(sa_get.sin_zero, null_padding, 8);
4038 ok(ret == 0, "getsockname did not zero the sockaddr_in structure\n");
4040 closesocket(sock);
4042 h = gethostbyname("");
4043 if (h && h->h_length == 4) /* this test is only meaningful in IPv4 */
4045 int i;
4046 for (i = 0; h->h_addr_list[i]; i++)
4048 char ipstr[32];
4049 struct in_addr ip;
4050 ip.s_addr = *(ULONG *) h->h_addr_list[i];
4052 sock = socket(AF_INET, SOCK_DGRAM, 0);
4053 ok(sock != INVALID_SOCKET, "socket failed with %d\n", GetLastError());
4055 memset(&sa_set, 0, sizeof(sa_set));
4056 sa_set.sin_family = AF_INET;
4057 sa_set.sin_addr.s_addr = ip.s_addr;
4058 /* The same address we bind must be the same address we get */
4059 ret = bind(sock, (struct sockaddr*)&sa_set, sizeof(sa_set));
4060 ok(ret == 0, "bind failed with %d\n", GetLastError());
4061 sa_get_len = sizeof(sa_get);
4062 ret = getsockname(sock, (struct sockaddr*)&sa_get, &sa_get_len);
4063 ok(ret == 0, "getsockname failed with %d\n", GetLastError());
4064 strcpy(ipstr, inet_ntoa(sa_get.sin_addr));
4065 ok(sa_get.sin_addr.s_addr == sa_set.sin_addr.s_addr,
4066 "address does not match: %s != %s\n", ipstr, inet_ntoa(sa_set.sin_addr));
4068 closesocket(sock);
4072 WSACleanup();
4075 static void test_dns(void)
4077 struct hostent *h;
4078 union memaddress
4080 char *chr;
4081 void *mem;
4082 } addr;
4083 char **ptr;
4084 int acount;
4086 h = gethostbyname("");
4087 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
4089 /* Use an address with valid alias names if possible */
4090 h = gethostbyname("source.winehq.org");
4091 if(!h)
4093 skip("Can't test the hostent structure because gethostbyname failed\n");
4094 return;
4097 /* The returned struct must be allocated in a very strict way. First we need to
4098 * count how many aliases there are because they must be located right after
4099 * the struct hostent size. Knowing the amount of aliases we know the exact
4100 * location of the first IP returned. Rule valid for >= XP, for older OS's
4101 * it's somewhat the opposite. */
4102 addr.mem = h + 1;
4103 if(h->h_addr_list == addr.mem) /* <= W2K */
4105 win_skip("Skipping hostent tests since this OS is unsupported\n");
4106 return;
4109 ok(h->h_aliases == addr.mem,
4110 "hostent->h_aliases should be in %p, it is in %p\n", addr.mem, h->h_aliases);
4112 for(ptr = h->h_aliases, acount = 1; *ptr; ptr++) acount++;
4113 addr.chr += sizeof(*ptr) * acount;
4114 ok(h->h_addr_list == addr.mem,
4115 "hostent->h_addr_list should be in %p, it is in %p\n", addr.mem, h->h_addr_list);
4117 for(ptr = h->h_addr_list, acount = 1; *ptr; ptr++) acount++;
4119 addr.chr += sizeof(*ptr) * acount;
4120 ok(h->h_addr_list[0] == addr.mem,
4121 "hostent->h_addr_list[0] should be in %p, it is in %p\n", addr.mem, h->h_addr_list[0]);
4124 /* Our winsock headers don't define gethostname because it conflicts with the
4125 * definition in unistd.h. Define it here to get rid of the warning. */
4127 int WINAPI gethostname(char *name, int namelen);
4129 static void test_gethostbyname(void)
4131 struct hostent *he;
4132 struct in_addr **addr_list;
4133 char name[256], first_ip[16];
4134 int ret, i, count;
4135 PMIB_IPFORWARDTABLE routes = NULL;
4136 PIP_ADAPTER_INFO adapters = NULL, k;
4137 DWORD adap_size = 0, route_size = 0;
4138 BOOL found_default = FALSE;
4139 BOOL local_ip = FALSE;
4141 ret = gethostname(name, sizeof(name));
4142 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4144 he = gethostbyname(name);
4145 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4146 addr_list = (struct in_addr **)he->h_addr_list;
4147 strcpy(first_ip, inet_ntoa(*addr_list[0]));
4149 if (winetest_debug > 1) trace("List of local IPs:\n");
4150 for(count = 0; addr_list[count] != NULL; count++)
4152 char *ip = inet_ntoa(*addr_list[count]);
4153 if (!strcmp(ip, "127.0.0.1"))
4154 local_ip = TRUE;
4155 if (winetest_debug > 1) trace("%s\n", ip);
4158 if (local_ip)
4160 ok (count == 1, "expected 127.0.0.1 to be the only IP returned\n");
4161 skip("Only the loopback address is present, skipping tests\n");
4162 return;
4165 ret = GetAdaptersInfo(NULL, &adap_size);
4166 ok (ret == ERROR_BUFFER_OVERFLOW, "GetAdaptersInfo failed with a different error: %d\n", ret);
4167 ret = GetIpForwardTable(NULL, &route_size, FALSE);
4168 ok (ret == ERROR_INSUFFICIENT_BUFFER, "GetIpForwardTable failed with a different error: %d\n", ret);
4170 adapters = HeapAlloc(GetProcessHeap(), 0, adap_size);
4171 routes = HeapAlloc(GetProcessHeap(), 0, route_size);
4173 ret = GetAdaptersInfo(adapters, &adap_size);
4174 ok (ret == NO_ERROR, "GetAdaptersInfo failed, error: %d\n", ret);
4175 ret = GetIpForwardTable(routes, &route_size, FALSE);
4176 ok (ret == NO_ERROR, "GetIpForwardTable failed, error: %d\n", ret);
4178 /* This test only has meaning if there is more than one IP configured */
4179 if (adapters->Next == NULL && count == 1)
4181 skip("Only one IP is present, skipping tests\n");
4182 goto cleanup;
4185 for (i = 0; !found_default && i < routes->dwNumEntries; i++)
4187 /* default route (ip 0.0.0.0) ? */
4188 if (routes->table[i].dwForwardDest) continue;
4190 for (k = adapters; k != NULL; k = k->Next)
4192 char *ip;
4194 if (k->Index != routes->table[i].dwForwardIfIndex) continue;
4196 /* the first IP returned from gethostbyname must be a default route */
4197 ip = k->IpAddressList.IpAddress.String;
4198 if (!strcmp(first_ip, ip))
4200 found_default = TRUE;
4201 break;
4205 ok (found_default, "failed to find the first IP from gethostbyname!\n");
4207 cleanup:
4208 HeapFree(GetProcessHeap(), 0, adapters);
4209 HeapFree(GetProcessHeap(), 0, routes);
4212 static void test_gethostbyname_hack(void)
4214 struct hostent *he;
4215 char name[256];
4216 static BYTE loopback[] = {127, 0, 0, 1};
4217 static BYTE magic_loopback[] = {127, 12, 34, 56};
4218 int ret;
4220 ret = gethostname(name, 256);
4221 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4223 he = gethostbyname("localhost");
4224 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
4225 if(he)
4227 if(he->h_length != 4)
4229 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
4230 return;
4233 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
4234 "gethostbyname(\"localhost\") returned %u.%u.%u.%u\n",
4235 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
4236 he->h_addr_list[0][3]);
4239 if(strcmp(name, "localhost") == 0)
4241 skip("hostname seems to be \"localhost\", skipping test.\n");
4242 return;
4245 he = gethostbyname(name);
4246 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
4247 if(he)
4249 if(he->h_length != 4)
4251 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
4252 return;
4255 if (he->h_addr_list[0][0] == 127)
4257 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
4258 "gethostbyname(\"%s\") returned %u.%u.%u.%u not 127.12.34.56\n",
4259 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
4260 he->h_addr_list[0][2], he->h_addr_list[0][3]);
4264 gethostbyname("nonexistent.winehq.org");
4265 /* Don't check for the return value, as some braindead ISPs will kindly
4266 * resolve nonexistent host names to addresses of the ISP's spam pages. */
4269 static void test_gethostname(void)
4271 struct hostent *he;
4272 char name[256];
4273 int ret, len;
4275 WSASetLastError(0xdeadbeef);
4276 ret = gethostname(NULL, 256);
4277 ok(ret == -1, "gethostname() returned %d\n", ret);
4278 ok(WSAGetLastError() == WSAEFAULT, "gethostname with null buffer "
4279 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
4281 ret = gethostname(name, sizeof(name));
4282 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4283 he = gethostbyname(name);
4284 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4286 len = strlen(name);
4287 WSASetLastError(0xdeadbeef);
4288 strcpy(name, "deadbeef");
4289 ret = gethostname(name, len);
4290 ok(ret == -1, "gethostname() returned %d\n", ret);
4291 ok(!strcmp(name, "deadbeef"), "name changed unexpected!\n");
4292 ok(WSAGetLastError() == WSAEFAULT, "gethostname with insufficient length "
4293 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
4295 len++;
4296 ret = gethostname(name, len);
4297 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4298 he = gethostbyname(name);
4299 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4302 static void test_inet_addr(void)
4304 u_long addr;
4306 addr = inet_addr(NULL);
4307 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
4310 static void test_addr_to_print(void)
4312 char dst[16];
4313 char dst6[64];
4314 const char * pdst;
4315 struct in_addr in;
4316 struct in6_addr in6;
4318 u_long addr0_Num = 0x00000000;
4319 PCSTR addr0_Str = "0.0.0.0";
4320 u_long addr1_Num = 0x20201015;
4321 PCSTR addr1_Str = "21.16.32.32";
4322 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
4323 PCSTR addr2_Str = "::fffe:cc98:bd74";
4324 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
4325 PCSTR addr3_Str = "2030:a4b1::";
4326 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
4327 PCSTR addr4_Str = "::204.152.189.116";
4329 /* Test IPv4 addresses */
4330 in.s_addr = addr0_Num;
4332 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
4333 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
4334 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
4336 /* Test that inet_ntoa and inet_ntop return the same value */
4337 in.S_un.S_addr = addr1_Num;
4338 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
4339 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
4340 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4342 /* InetNtop became available in Vista and Win2008 */
4343 if (!pInetNtop)
4345 win_skip("InetNtop not present, not executing tests\n");
4346 return;
4349 /* Second part of test */
4350 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
4351 ok(pdst != NULL, "InetNtop failed %s\n", dst);
4352 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4354 /* Test invalid parm conditions */
4355 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
4356 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4357 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
4359 /* Test Null destination */
4360 pdst = NULL;
4361 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, NULL, sizeof(dst));
4362 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4363 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4364 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4366 /* Test zero length passed */
4367 WSASetLastError(0);
4368 pdst = NULL;
4369 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 0);
4370 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4371 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4372 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4374 /* Test length one shorter than the address length */
4375 WSASetLastError(0);
4376 pdst = NULL;
4377 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 6);
4378 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4379 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4380 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4382 /* Test longer length is ok */
4383 WSASetLastError(0);
4384 pdst = NULL;
4385 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, sizeof(dst)+1);
4386 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
4387 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4389 /* Test the IPv6 addresses */
4391 /* Test an zero prefixed IPV6 address */
4392 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
4393 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
4394 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4395 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
4397 /* Test an zero suffixed IPV6 address */
4398 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
4399 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
4400 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4401 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
4403 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
4404 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num));
4405 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, sizeof(dst6));
4406 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4407 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str);
4409 /* Test invalid parm conditions */
4410 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
4412 /* Test Null destination */
4413 pdst = NULL;
4414 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, NULL, sizeof(dst6));
4415 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4416 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4417 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4419 /* Test zero length passed */
4420 WSASetLastError(0);
4421 pdst = NULL;
4422 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 0);
4423 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4424 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4425 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4427 /* Test length one shorter than the address length */
4428 WSASetLastError(0);
4429 pdst = NULL;
4430 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 16);
4431 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4432 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4433 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4435 /* Test longer length is ok */
4436 WSASetLastError(0);
4437 pdst = NULL;
4438 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 18);
4439 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
4441 static void test_inet_pton(void)
4443 struct TEST_DATA
4445 int family, ret;
4446 DWORD err;
4447 const char *printable, *collapsed, *raw_data;
4448 } tests[] = {
4449 {AF_UNSPEC, -1, WSAEFAULT, /* Test 0 */
4450 NULL, NULL, NULL},
4451 {AF_INET, -1, WSAEFAULT,
4452 NULL, NULL, NULL},
4453 {AF_INET6, -1, WSAEFAULT,
4454 NULL, NULL, NULL},
4455 {AF_UNSPEC, -1, WSAEAFNOSUPPORT,
4456 "127.0.0.1", NULL, NULL},
4457 {AF_INET, 1, 0,
4458 "127.0.0.1", "127.0.0.1",
4459 "\x7f\x00\x00\x01"},
4460 {AF_INET6, 0, 0,
4461 "127.0.0.1", "127.0.0.1", NULL},
4462 {AF_INET, 0, 0,
4463 "::1/128", NULL, NULL},
4464 {AF_INET6, 0, 0,
4465 "::1/128", NULL, NULL},
4466 {AF_UNSPEC, -1, WSAEAFNOSUPPORT,
4467 "broken", NULL, NULL},
4468 {AF_INET, 0, 0,
4469 "broken", NULL, NULL},
4470 {AF_INET6, 0, 0, /* Test 10 */
4471 "broken", NULL, NULL},
4472 {AF_UNSPEC, -1, WSAEAFNOSUPPORT,
4473 "177.32.45.20", NULL, NULL},
4474 {AF_INET, 1, 0,
4475 "177.32.45.20", "177.32.45.20",
4476 "\xb1\x20\x2d\x14"},
4477 {AF_INET6, 0, 0,
4478 "177.32.45.20", NULL, NULL},
4479 {AF_INET, 0, 0,
4480 "2607:f0d0:1002:51::4", NULL, NULL},
4481 {AF_INET6, 1, 0,
4482 "2607:f0d0:1002:51::4", "2607:f0d0:1002:51::4",
4483 "\x26\x07\xf0\xd0\x10\x02\x00\x51\x00\x00\x00\x00\x00\x00\x00\x04"},
4484 {AF_INET, 0, 0,
4485 "::177.32.45.20", NULL, NULL},
4486 {AF_INET6, 1, 0,
4487 "::177.32.45.20", "::177.32.45.20",
4488 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb1\x20\x2d\x14"},
4489 {AF_INET, 0, 0,
4490 "fe80::0202:b3ff:fe1e:8329", NULL, NULL},
4491 {AF_INET6, 1, 0,
4492 "fe80::0202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329",
4493 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"},
4494 {AF_INET6, 1, 0, /* Test 20 */
4495 "fe80::202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329",
4496 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"},
4497 {AF_INET, 0, 0,
4498 "a", NULL, NULL},
4499 {AF_INET, 0, 0,
4500 "a.b", NULL, NULL},
4501 {AF_INET, 0, 0,
4502 "a.b.c", NULL, NULL},
4503 {AF_INET, 0, 0,
4504 "a.b.c.d", NULL, NULL},
4505 {AF_INET6, 1, 0,
4506 "2001:cdba:0000:0000:0000:0000:3257:9652", "2001:cdba::3257:9652",
4507 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"},
4508 {AF_INET6, 1, 0,
4509 "2001:cdba::3257:9652", "2001:cdba::3257:9652",
4510 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"},
4511 {AF_INET6, 1, 0,
4512 "2001:cdba:0:0:0:0:3257:9652", "2001:cdba::3257:9652",
4513 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"},
4514 {AF_INET, 0, 0,
4515 "0x12345678", NULL, NULL},
4516 {AF_INET6, 0, 0, /* windows bug */
4517 "::1:2:3:4:5:6:7", NULL, NULL},
4518 {AF_INET6, 1, 0, /* Test 30 */
4519 "::5efe:1.2.3.4", "::5efe:1.2.3.4",
4520 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5e\xfe\x01\x02\x03\x04"},
4521 {AF_INET6, 1, 0,
4522 "::ffff:0:1.2.3.4", "::ffff:0:1.2.3.4",
4523 "\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x01\x02\x03\x04"},
4525 int i, ret;
4526 DWORD err;
4527 char buffer[64],str[64];
4528 WCHAR printableW[64], collapsedW[64];
4529 const char *ptr;
4530 const WCHAR *ptrW;
4532 /* InetNtop and InetPton became available in Vista and Win2008 */
4533 if (!pInetNtop || !pInetNtopW || !pInetPtonA || !pInetPtonW)
4535 win_skip("InetNtop and/or InetPton not present, not executing tests\n");
4536 return;
4539 for (i = 0; i < ARRAY_SIZE(tests); i++)
4541 WSASetLastError(0xdeadbeef);
4542 ret = pInetPtonA(tests[i].family, tests[i].printable, buffer);
4543 ok (ret == tests[i].ret, "Test [%d]: Expected %d, got %d\n", i, tests[i].ret, ret);
4544 err = WSAGetLastError();
4545 if (tests[i].ret == -1)
4546 ok (tests[i].err == err, "Test [%d]: Expected 0x%x, got 0x%x\n", i, tests[i].err, err);
4547 else
4548 ok (err == 0xdeadbeef, "Test [%d]: Expected 0xdeadbeef, got 0x%x\n", i, err);
4549 if (tests[i].ret != 1) continue;
4550 ok (memcmp(buffer, tests[i].raw_data,
4551 tests[i].family == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr)) == 0,
4552 "Test [%d]: Expected binary data differs\n", i);
4554 /* Test the result from Pton with Ntop */
4555 strcpy (str, "deadbeef");
4556 ptr = pInetNtop(tests[i].family, buffer, str, sizeof(str));
4557 ok (ptr != NULL, "Test [%d]: Failed with NULL\n", i);
4558 ok (ptr == str, "Test [%d]: Pointers differ (%p != %p)\n", i, ptr, str);
4559 if (!ptr) continue;
4560 ok (strcmp(ptr, tests[i].collapsed) == 0, "Test [%d]: Expected '%s', got '%s'\n",
4561 i, tests[i].collapsed, ptr);
4564 for (i = 0; i < ARRAY_SIZE(tests); i++)
4566 if (tests[i].printable)
4567 MultiByteToWideChar(CP_ACP, 0, tests[i].printable, -1, printableW, ARRAY_SIZE(printableW));
4568 WSASetLastError(0xdeadbeef);
4569 ret = pInetPtonW(tests[i].family, tests[i].printable ? printableW : NULL, buffer);
4570 ok(ret == tests[i].ret, "Test [%d]: Expected %d, got %d\n", i, tests[i].ret, ret);
4571 err = WSAGetLastError();
4572 if (tests[i].ret == -1)
4573 ok(tests[i].err == err, "Test [%d]: Expected 0x%x, got 0x%x\n", i, tests[i].err, err);
4574 else if (tests[i].ret == 0)
4575 ok(err == WSAEINVAL || broken(err == 0xdeadbeef) /* win2008 */,
4576 "Test [%d]: Expected WSAEINVAL, got 0x%x\n", i, err);
4577 else
4578 ok(err == 0xdeadbeef, "Test [%d]: Expected 0xdeadbeef, got 0x%x\n", i, err);
4579 if (tests[i].ret != 1) continue;
4580 ok(memcmp(buffer, tests[i].raw_data,
4581 tests[i].family == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr)) == 0,
4582 "Test [%d]: Expected binary data differs\n", i);
4584 /* Test the result from Pton with Ntop */
4585 printableW[0] = 0xdead;
4586 ptrW = pInetNtopW(tests[i].family, buffer, printableW, ARRAY_SIZE(printableW));
4587 ok (ptrW != NULL, "Test [%d]: Failed with NULL\n", i);
4588 ok (ptrW == printableW, "Test [%d]: Pointers differ (%p != %p)\n", i, ptrW, printableW);
4589 if (!ptrW) continue;
4591 MultiByteToWideChar(CP_ACP, 0, tests[i].collapsed, -1, collapsedW, ARRAY_SIZE(collapsedW));
4592 ok (lstrcmpW(ptrW, collapsedW) == 0, "Test [%d]: Expected '%s', got '%s'\n",
4593 i, tests[i].collapsed, wine_dbgstr_w(ptrW));
4597 static void test_ioctlsocket(void)
4599 SOCKET sock, src, dst;
4600 struct tcp_keepalive kalive;
4601 struct sockaddr_in address;
4602 int ret, optval;
4603 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
4604 UINT i, bytes_rec;
4605 char data;
4606 WSABUF bufs;
4607 u_long arg = 0;
4609 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4610 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
4612 for(i = 0; i < ARRAY_SIZE(cmds); i++)
4614 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
4615 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
4616 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
4617 ret = WSAGetLastError();
4618 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
4621 /* A fresh and not connected socket has no urgent data, this test shows
4622 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
4624 ret = ioctlsocket(sock, SIOCATMARK, &arg);
4625 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
4626 ok(arg, "SIOCATMARK expected a non-zero value\n");
4628 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
4629 optval = 1;
4630 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
4631 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
4632 arg = 0;
4633 ret = ioctlsocket(sock, SIOCATMARK, &arg);
4634 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
4635 ok(arg, "SIOCATMARK expected a non-zero value\n");
4637 /* disable SO_OOBINLINE and get the same old behavior */
4638 optval = 0;
4639 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
4640 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
4641 arg = 0;
4642 ret = ioctlsocket(sock, SIOCATMARK, &arg);
4643 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
4644 ok(arg, "SIOCATMARK expected a non-zero value\n");
4646 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
4647 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
4648 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
4650 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4651 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
4652 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
4654 make_keepalive(kalive, 0, 0, 0);
4655 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4656 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4658 make_keepalive(kalive, 1, 0, 0);
4659 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4660 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4662 make_keepalive(kalive, 1, 1000, 1000);
4663 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4664 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4666 make_keepalive(kalive, 1, 10000, 10000);
4667 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4668 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4670 make_keepalive(kalive, 1, 100, 100);
4671 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4672 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4674 make_keepalive(kalive, 0, 100, 100);
4675 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
4676 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
4678 closesocket(sock);
4680 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4681 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
4683 /* test FIONREAD with a fresh and non-connected socket */
4684 arg = 0xdeadbeef;
4685 ret = ioctlsocket(sock, FIONREAD, &arg);
4686 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
4687 ok(arg == 0, "expected 0, got %u\n", arg);
4689 memset(&address, 0, sizeof(address));
4690 address.sin_family = AF_INET;
4691 address.sin_addr.s_addr = inet_addr( SERVERIP );
4692 address.sin_port = htons( SERVERPORT );
4693 ret = bind(sock, (struct sockaddr *)&address, sizeof(address));
4694 ok(ret == 0, "bind failed unexpectedly with error %d\n", WSAGetLastError());
4696 ret = listen(sock, SOMAXCONN);
4697 ok(ret == 0, "listen failed unexpectedly with error %d\n", WSAGetLastError());
4699 /* test FIONREAD with listening socket */
4700 arg = 0xdeadbeef;
4701 ret = ioctlsocket(sock, FIONREAD, &arg);
4702 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
4703 ok(arg == 0, "expected 0, got %u\n", arg);
4705 closesocket(sock);
4707 tcp_socketpair(&src, &dst);
4709 /* test FIONREAD on TCP sockets */
4710 optval = 0xdeadbeef;
4711 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
4712 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
4713 ok(optval == 0, "FIONREAD should have returned 0 bytes, got %d instead\n", optval);
4715 optval = 0xdeadbeef;
4716 ok(send(src, "TEST", 4, 0) == 4, "failed to send test data\n");
4717 Sleep(100);
4718 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
4719 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
4720 ok(optval == 4, "FIONREAD should have returned 4 bytes, got %d instead\n", optval);
4722 /* trying to read from an OOB inlined socket with MSG_OOB results in WSAEINVAL */
4723 set_blocking(dst, FALSE);
4724 i = MSG_OOB;
4725 SetLastError(0xdeadbeef);
4726 ret = recv(dst, &data, 1, i);
4727 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
4728 ret = GetLastError();
4729 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
4730 bufs.len = sizeof(char);
4731 bufs.buf = &data;
4732 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
4733 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
4734 ret = GetLastError();
4735 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
4736 optval = 1;
4737 ret = setsockopt(dst, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
4738 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
4739 i = MSG_OOB;
4740 SetLastError(0xdeadbeef);
4741 ret = recv(dst, &data, 1, i);
4742 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
4743 ret = GetLastError();
4744 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
4745 bufs.len = sizeof(char);
4746 bufs.buf = &data;
4747 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
4748 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
4749 ret = GetLastError();
4750 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
4752 closesocket(dst);
4753 optval = 0xdeadbeef;
4754 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
4755 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
4756 ok(optval == 0xdeadbeef, "FIONREAD should not have changed last error, got %d instead\n", optval);
4757 closesocket(src);
4760 static BOOL drain_pause = FALSE;
4761 static DWORD WINAPI drain_socket_thread(LPVOID arg)
4763 char buffer[1024];
4764 SOCKET sock = *(SOCKET*)arg;
4765 int ret;
4767 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
4769 if (ret < 0)
4771 if (WSAGetLastError() == WSAEWOULDBLOCK)
4773 fd_set readset;
4774 FD_ZERO(&readset);
4775 FD_SET(sock, &readset);
4776 select(sock+1, &readset, NULL, NULL, NULL);
4777 while (drain_pause)
4778 Sleep(100);
4780 else
4781 break;
4784 return 0;
4787 static void test_send(void)
4789 SOCKET src = INVALID_SOCKET;
4790 SOCKET dst = INVALID_SOCKET;
4791 HANDLE hThread = NULL;
4792 const int buflen = 1024*1024;
4793 char *buffer = NULL;
4794 int ret, i, zero = 0;
4795 WSABUF buf;
4796 OVERLAPPED ov;
4797 BOOL bret;
4798 DWORD id, bytes_sent, dwRet;
4800 memset(&ov, 0, sizeof(ov));
4802 tcp_socketpair(&src, &dst);
4804 set_blocking(dst, FALSE);
4805 /* force disable buffering so we can get a pending overlapped request */
4806 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
4807 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
4809 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
4811 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
4813 /* fill the buffer with some nonsense */
4814 for (i = 0; i < buflen; ++i)
4816 buffer[i] = (char) i;
4819 ret = send(src, buffer, buflen, 0);
4820 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
4822 buf.buf = buffer;
4823 buf.len = buflen;
4825 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
4826 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
4827 if (!ov.hEvent)
4828 goto end;
4830 bytes_sent = 0;
4831 WSASetLastError(12345);
4832 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
4833 ok(ret == SOCKET_ERROR, "expected failure\n");
4834 ok(WSAGetLastError() == ERROR_IO_PENDING, "wrong error %u\n", WSAGetLastError());
4836 /* don't check for completion yet, we may need to drain the buffer while still sending */
4837 set_blocking(src, FALSE);
4838 for (i = 0; i < buflen; ++i)
4840 int j = 0;
4842 ret = recv(src, buffer, 1, 0);
4843 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
4845 j++;
4846 Sleep(50);
4847 ret = recv(src, buffer, 1, 0);
4850 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
4851 if (ret != 1)
4852 break;
4854 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
4857 dwRet = WaitForSingleObject(ov.hEvent, 1000);
4858 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
4859 if (dwRet == WAIT_OBJECT_0)
4861 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
4862 ok(bret && bytes_sent == buflen,
4863 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
4866 WSASetLastError(12345);
4867 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
4868 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
4869 "WSASend failed %d - %d\n", ret, WSAGetLastError());
4871 WSASetLastError(12345);
4872 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
4873 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
4874 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
4876 end:
4877 if (src != INVALID_SOCKET)
4878 closesocket(src);
4879 if (dst != INVALID_SOCKET)
4880 closesocket(dst);
4881 if (hThread != NULL)
4883 dwRet = WaitForSingleObject(hThread, 500);
4884 ok(dwRet == WAIT_OBJECT_0, "failed to wait for thread termination: %d\n", GetLastError());
4885 CloseHandle(hThread);
4887 if (ov.hEvent)
4888 CloseHandle(ov.hEvent);
4889 HeapFree(GetProcessHeap(), 0, buffer);
4892 typedef struct async_message
4894 SOCKET socket;
4895 LPARAM lparam;
4896 struct async_message *next;
4897 } async_message;
4899 static struct async_message *messages_received;
4901 #define WM_SOCKET (WM_USER+100)
4902 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4904 struct async_message *message;
4906 switch (msg)
4908 case WM_SOCKET:
4909 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message));
4910 message->socket = (SOCKET) wparam;
4911 message->lparam = lparam;
4912 message->next = NULL;
4914 if (messages_received)
4916 struct async_message *last = messages_received;
4917 while (last->next) last = last->next;
4918 last->next = message;
4920 else
4921 messages_received = message;
4922 return 0;
4925 return DefWindowProcA(hwnd, msg, wparam, lparam);
4928 static void get_event_details(int event, int *bit, char *name)
4930 switch (event)
4932 case FD_ACCEPT:
4933 if (bit) *bit = FD_ACCEPT_BIT;
4934 if (name) strcpy(name, "FD_ACCEPT");
4935 break;
4936 case FD_CONNECT:
4937 if (bit) *bit = FD_CONNECT_BIT;
4938 if (name) strcpy(name, "FD_CONNECT");
4939 break;
4940 case FD_READ:
4941 if (bit) *bit = FD_READ_BIT;
4942 if (name) strcpy(name, "FD_READ");
4943 break;
4944 case FD_OOB:
4945 if (bit) *bit = FD_OOB_BIT;
4946 if (name) strcpy(name, "FD_OOB");
4947 break;
4948 case FD_WRITE:
4949 if (bit) *bit = FD_WRITE_BIT;
4950 if (name) strcpy(name, "FD_WRITE");
4951 break;
4952 case FD_CLOSE:
4953 if (bit) *bit = FD_CLOSE_BIT;
4954 if (name) strcpy(name, "FD_CLOSE");
4955 break;
4956 default:
4957 if (bit) *bit = -1;
4958 if (name) sprintf(name, "bad%x", event);
4962 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents)
4964 static char message[1024];
4965 struct async_message *curr = messages_received;
4966 int index, error, bit = 0;
4967 char name[12];
4968 int len = 1;
4970 message[0] = '[';
4971 message[1] = 0;
4972 while (1)
4974 if (netEvents)
4976 if (bit >= FD_MAX_EVENTS) break;
4977 if ( !(netEvents->lNetworkEvents & (1 << bit)) )
4979 bit++;
4980 continue;
4982 get_event_details(1 << bit, &index, name);
4983 error = netEvents->iErrorCode[index];
4984 bit++;
4986 else
4988 if (!curr) break;
4989 if (curr->socket != s)
4991 curr = curr->next;
4992 continue;
4994 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name);
4995 error = WSAGETSELECTERROR(curr->lparam);
4996 curr = curr->next;
4999 len += sprintf(message + len, "%s(%d) ", name, error);
5001 if (len > 1) len--;
5002 strcpy( message + len, "]" );
5003 return message;
5006 static void flush_events(SOCKET s, HANDLE hEvent)
5008 WSANETWORKEVENTS netEvents;
5009 struct async_message *prev = NULL, *curr = messages_received;
5010 int ret;
5011 DWORD dwRet;
5013 if (hEvent != INVALID_HANDLE_VALUE)
5015 dwRet = WaitForSingleObject(hEvent, 100);
5016 if (dwRet == WAIT_OBJECT_0)
5018 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents);
5019 ok(!ret, "failed to get network events, error %u\n", WSAGetLastError());
5022 else
5024 while (curr)
5026 if (curr->socket == s)
5028 if (prev) prev->next = curr->next;
5029 else messages_received = curr->next;
5031 HeapFree(GetProcessHeap(), 0, curr);
5033 if (prev) curr = prev->next;
5034 else curr = messages_received;
5036 else
5038 prev = curr;
5039 curr = curr->next;
5045 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq)
5047 int event, index, error, events;
5048 struct async_message *curr;
5050 if (netEvents)
5052 events = netEvents->lNetworkEvents;
5053 while (*seq)
5055 event = WSAGETSELECTEVENT(*seq);
5056 error = WSAGETSELECTERROR(*seq);
5057 get_event_details(event, &index, NULL);
5059 if (!(events & event) && index != -1)
5060 return 0;
5061 if (events & event && index != -1)
5063 if (netEvents->iErrorCode[index] != error)
5064 return 0;
5066 events &= ~event;
5067 seq++;
5069 if (events)
5070 return 0;
5072 else
5074 curr = messages_received;
5075 while (curr)
5077 if (curr->socket == s)
5079 if (!*seq) return 0;
5080 if (*seq != curr->lparam) return 0;
5081 seq++;
5083 curr = curr->next;
5085 if (*seq)
5086 return 0;
5088 return 1;
5091 /* checks for a sequence of events, (order only checked if window is used) */
5092 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq)
5094 MSG msg;
5095 WSANETWORKEVENTS events, *netEvents = NULL;
5096 int ret;
5097 DWORD dwRet;
5099 if (hEvent != INVALID_HANDLE_VALUE)
5101 netEvents = &events;
5103 dwRet = WaitForSingleObject(hEvent, 200);
5104 if (dwRet == WAIT_OBJECT_0)
5106 ret = WSAEnumNetworkEvents(s, hEvent, netEvents);
5107 if (ret)
5109 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
5110 return;
5113 else
5114 memset(netEvents, 0, sizeof(*netEvents));
5116 else
5118 Sleep(200);
5119 /* Run the message loop a little */
5120 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ))
5122 DispatchMessageA(&msg);
5126 winetest_ok(match_event_sequence(s, netEvents, seq), "Got sequence %s\n", dbgstr_event_seq_result(s, netEvents));
5127 flush_events(s, hEvent);
5130 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
5132 static void test_events(int useMessages)
5134 SOCKET server = INVALID_SOCKET;
5135 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET;
5136 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET;
5137 struct sockaddr_in addr;
5138 HANDLE hThread = NULL;
5139 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE;
5140 WNDCLASSEXA wndclass;
5141 HWND hWnd = NULL;
5142 char *buffer = NULL;
5143 int bufferSize = 1024*1024;
5144 WSABUF bufs;
5145 OVERLAPPED ov, ov2;
5146 DWORD flags = 0;
5147 DWORD bytesReturned;
5148 DWORD id;
5149 int len;
5150 int ret;
5151 DWORD dwRet;
5152 BOOL bret;
5153 static char szClassName[] = "wstestclass";
5154 static const LPARAM empty_seq[] = { 0 };
5155 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
5156 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 };
5157 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 };
5158 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0),
5159 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
5160 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
5161 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
5163 memset(&ov, 0, sizeof(ov));
5164 memset(&ov2, 0, sizeof(ov2));
5166 /* don't use socketpair, we want connection event */
5167 src = socket(AF_INET, SOCK_STREAM, 0);
5168 ok(src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5170 ret = set_blocking(src, TRUE);
5171 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5173 src2 = socket(AF_INET, SOCK_STREAM, 0);
5174 ok(src2 != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5176 ret = set_blocking(src2, TRUE);
5177 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5179 len = sizeof(BOOL);
5180 ret = getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len);
5181 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
5182 ok(bret == FALSE, "OOB not inline\n");
5184 if (useMessages)
5186 wndclass.cbSize = sizeof(wndclass);
5187 wndclass.style = CS_HREDRAW | CS_VREDRAW;
5188 wndclass.lpfnWndProc = ws2_test_WndProc;
5189 wndclass.cbClsExtra = 0;
5190 wndclass.cbWndExtra = 0;
5191 wndclass.hInstance = GetModuleHandleA(NULL);
5192 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
5193 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
5194 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
5195 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
5196 wndclass.lpszClassName = szClassName;
5197 wndclass.lpszMenuName = NULL;
5198 RegisterClassExA(&wndclass);
5200 hWnd = CreateWindowA(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW,
5201 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
5202 ok(!!hWnd, "failed to create window\n");
5204 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
5205 ok(!ret, "got error %u\n", WSAGetLastError());
5207 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
5208 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5210 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
5211 ok(!ret, "got error %u\n", WSAGetLastError());
5213 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
5214 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5216 else
5218 hEvent = WSACreateEvent();
5219 ok(hEvent != WSA_INVALID_EVENT, "failed to create event, error %u\n", WSAGetLastError());
5221 hEvent2 = WSACreateEvent();
5222 ok(hEvent2 != WSA_INVALID_EVENT, "failed to create event, error %u\n", WSAGetLastError());
5224 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
5225 ok(!ret, "got error %u\n", WSAGetLastError());
5227 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
5228 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5230 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
5231 ok(!ret, "got error %u\n", WSAGetLastError());
5233 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
5234 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5237 server = socket(AF_INET, SOCK_STREAM, 0);
5238 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5240 memset(&addr, 0, sizeof(addr));
5241 addr.sin_family = AF_INET;
5242 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
5243 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
5244 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
5246 len = sizeof(addr);
5247 ret = getsockname(server, (struct sockaddr*)&addr, &len);
5248 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
5250 ret = listen(server, 2);
5251 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
5253 SetLastError(0xdeadbeef);
5254 ret = connect(src, NULL, 0);
5255 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5256 ok(GetLastError() == WSAEFAULT, "expected 10014, got %d\n", GetLastError());
5258 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
5259 ok(!ret || WSAGetLastError() == WSAEWOULDBLOCK, "failed to connect, error %u\n", WSAGetLastError());
5261 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr));
5262 ok(!ret || WSAGetLastError() == WSAEWOULDBLOCK, "failed to connect, error %u\n", WSAGetLastError());
5264 len = sizeof(addr);
5265 dst = accept(server, (struct sockaddr*)&addr, &len);
5266 ok(dst != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
5268 len = sizeof(addr);
5269 dst2 = accept(server, (struct sockaddr*)&addr, &len);
5270 ok(dst2 != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
5272 closesocket(server);
5273 server = INVALID_SOCKET;
5275 /* On Windows it seems when a non-blocking socket sends to a
5276 blocking socket on the same host, the send() is BLOCKING,
5277 so make both sockets non-blocking. src is already non-blocking
5278 from the async select */
5280 ret = set_blocking(dst, FALSE);
5281 ok(!ret, "failed to set nonblocking, error %u\n", WSAGetLastError());
5283 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
5284 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
5285 ov2.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
5287 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
5288 ok_event_seq(src, hEvent, connect_seq);
5289 ok_event_seq(src2, hEvent2, connect_seq);
5291 /* Test simple send/recv */
5292 SetLastError(0xdeadbeef);
5293 ret = send(dst, buffer, 100, 0);
5294 ok(ret == 100, "Failed to send buffer %d err %d\n", ret, GetLastError());
5295 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5296 ok_event_seq(src, hEvent, read_seq);
5298 SetLastError(0xdeadbeef);
5299 ret = recv(src, buffer, 1, MSG_PEEK);
5300 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError());
5301 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5302 ok_event_seq(src, hEvent, read_seq);
5304 SetLastError(0xdeadbeef);
5305 ret = recv(src, buffer, 50, 0);
5306 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
5307 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5308 ok_event_seq(src, hEvent, read_seq);
5310 ret = recv(src, buffer, 50, 0);
5311 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
5312 ok_event_seq(src, hEvent, empty_seq);
5314 /* fun fact - events are re-enabled even on failure, but only for messages */
5315 ret = send(dst, "1", 1, 0);
5316 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
5317 ok_event_seq(src, hEvent, read_seq);
5319 ret = recv(src, buffer, -1, 0);
5320 ok(ret == SOCKET_ERROR, "expected failure\n");
5321 ok(WSAGetLastError() == WSAEFAULT || WSAGetLastError() == WSAENOBUFS /* < 7 */,
5322 "got error %u\n", WSAGetLastError());
5323 if (useMessages)
5324 todo_wine ok_event_seq(src, hEvent, read_seq);
5325 else
5326 ok_event_seq(src, hEvent, empty_seq);
5328 SetLastError(0xdeadbeef);
5329 ret = recv(src, buffer, 1, 0);
5330 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError());
5331 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5332 ok_event_seq(src, hEvent, empty_seq);
5334 /* Interaction with overlapped */
5335 bufs.len = sizeof(char);
5336 bufs.buf = buffer;
5337 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
5338 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
5339 "WSARecv failed - %d error %d\n", ret, GetLastError());
5341 bufs.len = sizeof(char);
5342 bufs.buf = buffer+1;
5343 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL);
5344 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
5345 "WSARecv failed - %d error %d\n", ret, GetLastError());
5347 ret = send(dst, "12", 2, 0);
5348 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
5349 ok_event_seq(src, hEvent, empty_seq);
5351 dwRet = WaitForSingleObject(ov.hEvent, 100);
5352 ok(!dwRet, "got %u\n", dwRet);
5353 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
5354 ok(bret, "got error %u\n", GetLastError());
5355 ok(bytesReturned == 1, "got %u bytes\n", bytesReturned);
5356 ok(buffer[0] == '1', "Got %c instead of 2\n", buffer[0]);
5358 dwRet = WaitForSingleObject(ov2.hEvent, 100);
5359 ok(!dwRet, "got %u\n", dwRet);
5360 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE);
5361 ok(bret, "got error %u\n", GetLastError());
5362 ok(bytesReturned == 1, "got %u bytes\n", bytesReturned);
5363 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[0]);
5365 SetLastError(0xdeadbeef);
5366 ret = send(dst, "1", 1, 0);
5367 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
5368 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5369 ok_event_seq(src, hEvent, read_seq);
5371 ret = recv(src, buffer, 1, 0);
5372 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
5373 ok_event_seq(src, hEvent, empty_seq);
5375 /* Notifications are delivered as soon as possible, blocked only on
5376 * async requests on the same type */
5377 bufs.len = sizeof(char);
5378 bufs.buf = buffer;
5379 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
5380 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
5381 "WSARecv failed - %d error %d\n", ret, GetLastError());
5383 ret = send(dst, "1", 1, MSG_OOB);
5384 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
5385 todo_wine ok_event_seq(src, hEvent, oob_seq);
5387 dwRet = WaitForSingleObject(ov.hEvent, 100);
5388 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError());
5390 ret = send(dst, "2", 1, 0);
5391 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
5392 ok_event_seq(src, hEvent, empty_seq);
5394 dwRet = WaitForSingleObject(ov.hEvent, 100);
5395 ok(!dwRet, "got %u\n", dwRet);
5396 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
5397 ok(bret, "got error %u\n", GetLastError());
5398 ok(bytesReturned == 1, "got %u bytes\n", bytesReturned);
5399 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]);
5401 ret = recv(src, buffer, 1, MSG_OOB);
5402 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
5403 ok_event_seq(src, hEvent, empty_seq);
5405 /* Flood the send queue */
5406 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
5408 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
5409 ok_event_seq(src, hEvent, empty_seq);
5411 /* Now if we send a ton of data and the 'server' does not drain it fast
5412 * enough (set drain_pause to be sure), the socket send buffer will only
5413 * take some of it, and we will get a short write. This will trigger
5414 * another FD_WRITE event as soon as data is sent and more space becomes
5415 * available, but not any earlier. */
5416 drain_pause = TRUE;
5419 ret = send(src, buffer, bufferSize, 0);
5420 } while (ret == bufferSize);
5421 drain_pause = FALSE;
5422 ok(ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
5423 ok_event_seq(src, hEvent, write_seq);
5425 /* Test how FD_CLOSE is handled */
5426 ret = send(dst, "12", 2, 0);
5427 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
5429 /* Wait a little and let the send complete */
5430 Sleep(100);
5431 closesocket(dst);
5432 Sleep(100);
5434 /* We can never implement this in wine, best we can hope for is
5435 sending FD_CLOSE after the reads complete */
5436 todo_wine ok_event_seq(src, hEvent, read_close_seq);
5438 ret = recv(src, buffer, 1, 0);
5439 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
5440 ok_event_seq(src, hEvent, read_seq);
5442 ret = recv(src, buffer, 1, 0);
5443 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
5444 /* want it? it's here, but you can't have it */
5445 todo_wine ok_event_seq(src, hEvent, empty_seq);
5447 /* Test how FD_CLOSE is handled */
5448 ret = send(dst2, "12", 2, 0);
5449 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
5451 Sleep(200);
5452 shutdown(dst2, SD_SEND);
5453 Sleep(200);
5455 todo_wine ok_event_seq(src2, hEvent2, read_close_seq);
5457 ret = recv(src2, buffer, 1, 0);
5458 ok(ret == 1, "got ret %d, error %u\n", ret, WSAGetLastError());
5459 ok_event_seq(src2, hEvent2, read_seq);
5461 ret = recv(src2, buffer, 1, 0);
5462 ok(ret == 1, "got ret %d, error %u\n", ret, WSAGetLastError());
5463 todo_wine ok_event_seq(src2, hEvent2, empty_seq);
5465 ret = send(src2, "1", 1, 0);
5466 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
5467 ok_event_seq(src2, hEvent2, empty_seq);
5469 ret = send(src2, "1", 1, 0);
5470 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
5471 ok_event_seq(src2, hEvent2, empty_seq);
5473 if (useMessages)
5475 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, 0);
5476 ok(!ret, "got error %u\n", WSAGetLastError());
5478 ret = set_blocking(src, TRUE);
5479 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5481 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, 0);
5482 ok(!ret, "got error %u\n", WSAGetLastError());
5484 ret = set_blocking(src2, TRUE);
5485 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5487 else
5489 ret = WSAEventSelect(src, hEvent2, 0);
5490 ok(!ret, "got error %u\n", WSAGetLastError());
5492 ret = set_blocking(src, TRUE);
5493 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5495 ret = WSAEventSelect(src2, hEvent2, 0);
5496 ok(!ret, "got error %u\n", WSAGetLastError());
5498 ret = set_blocking(src2, TRUE);
5499 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
5502 flush_events(src, hEvent);
5503 closesocket(src);
5504 flush_events(src2, hEvent2);
5505 closesocket(src2);
5506 HeapFree(GetProcessHeap(), 0, buffer);
5507 closesocket(dst2);
5508 CloseHandle(hThread);
5509 DestroyWindow(hWnd);
5510 CloseHandle(hEvent);
5511 CloseHandle(hEvent2);
5512 CloseHandle(ov.hEvent);
5513 CloseHandle(ov2.hEvent);
5516 static void test_ipv6only(void)
5518 SOCKET v4 = INVALID_SOCKET, v6;
5519 struct sockaddr_in sin4;
5520 struct sockaddr_in6 sin6;
5521 int ret, enabled, len = sizeof(enabled);
5523 memset(&sin4, 0, sizeof(sin4));
5524 sin4.sin_family = AF_INET;
5525 sin4.sin_port = htons(SERVERPORT);
5527 memset(&sin6, 0, sizeof(sin6));
5528 sin6.sin6_family = AF_INET6;
5529 sin6.sin6_port = htons(SERVERPORT);
5531 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
5532 if (v6 == INVALID_SOCKET)
5534 skip("Could not create IPv6 socket (LastError: %d)\n", WSAGetLastError());
5535 goto end;
5538 enabled = 2;
5539 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5540 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5541 ok(enabled == 1, "expected 1, got %d\n", enabled);
5543 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
5544 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
5546 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5547 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
5549 todo_wine {
5550 enabled = 2;
5551 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5552 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5553 ok(enabled == 1, "expected 1, got %d\n", enabled);
5556 enabled = 0;
5557 len = sizeof(enabled);
5558 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
5559 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5561 todo_wine {
5562 enabled = 2;
5563 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5564 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5565 ok(!enabled, "expected 0, got %d\n", enabled);
5568 enabled = 1;
5569 len = sizeof(enabled);
5570 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
5571 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5573 /* bind on IPv4 socket should succeed - IPV6_V6ONLY is enabled by default */
5574 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
5575 ok(!ret, "Could not bind IPv4 address (LastError: %d)\n", WSAGetLastError());
5577 todo_wine {
5578 enabled = 2;
5579 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5580 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5581 ok(enabled == 1, "expected 1, got %d\n", enabled);
5584 enabled = 0;
5585 len = sizeof(enabled);
5586 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
5587 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
5589 todo_wine {
5590 enabled = 0;
5591 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5592 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5593 ok(enabled == 1, "expected 1, got %d\n", enabled);
5596 enabled = 1;
5597 len = sizeof(enabled);
5598 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
5599 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
5601 closesocket(v4);
5602 closesocket(v6);
5604 /* Test again, this time disabling IPV6_V6ONLY. */
5605 sin4.sin_port = htons(SERVERPORT+2);
5606 sin6.sin6_port = htons(SERVERPORT+2);
5608 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
5609 ok(v6 != INVALID_SOCKET, "Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
5610 WSAGetLastError(), WSAEAFNOSUPPORT);
5612 enabled = 0;
5613 ret = setsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
5614 ok(!ret, "Could not disable IPV6_V6ONLY (LastError: %d).\n", WSAGetLastError());
5616 enabled = 2;
5617 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5618 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5619 ok(!enabled, "expected 0, got %d\n", enabled);
5622 Observaition:
5623 On Windows, bind on both IPv4 and IPv6 with IPV6_V6ONLY disabled succeeds by default.
5624 Application must set SO_EXCLUSIVEADDRUSE on first socket to disallow another successful bind.
5625 In general, a standard application should not use SO_REUSEADDR.
5626 Setting both SO_EXCLUSIVEADDRUSE and SO_REUSEADDR on the same socket is not possible in
5627 either order, the later setsockopt call always fails.
5629 enabled = 1;
5630 ret = setsockopt(v6, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&enabled, len);
5631 ok(!ret, "Could not set SO_EXCLUSIVEADDRUSE on IPv6 socket (LastError: %d)\n", WSAGetLastError());
5633 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
5634 ok(!ret, "Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError());
5636 enabled = 2;
5637 len = sizeof(enabled);
5638 getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
5639 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
5640 ok(!enabled, "IPV6_V6ONLY is enabled after bind\n");
5642 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5643 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
5645 enabled = 1;
5646 ret = setsockopt(v4, SOL_SOCKET, SO_REUSEADDR, (char*)&enabled, len);
5647 ok(!ret, "Could not set SO_REUSEADDR on IPv4 socket (LastError: %d)\n", WSAGetLastError());
5649 WSASetLastError(0xdeadbeef);
5650 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
5651 ok(ret, "bind succeeded unexpectedly for the IPv4 socket\n");
5652 ok(WSAGetLastError() == WSAEACCES, "Expected 10013, got %d\n", WSAGetLastError());
5654 end:
5655 if (v4 != INVALID_SOCKET)
5656 closesocket(v4);
5657 if (v6 != INVALID_SOCKET)
5658 closesocket(v6);
5661 static void test_WSASendMsg(void)
5663 SOCKET sock, dst;
5664 struct sockaddr_in sendaddr, sockaddr;
5665 GUID WSASendMsg_GUID = WSAID_WSASENDMSG;
5666 LPFN_WSASENDMSG pWSASendMsg = NULL;
5667 char teststr[12] = "hello world", buffer[32];
5668 WSABUF iovec[2];
5669 WSAMSG msg;
5670 DWORD bytesSent, err;
5671 int ret, addrlen;
5673 /* FIXME: Missing OVERLAPPED and OVERLAPPED COMPLETION ROUTINE tests */
5675 sock = socket(AF_INET, SOCK_DGRAM, 0);
5676 ok(sock != INVALID_SOCKET, "socket() failed\n");
5678 /* Obtain the WSASendMsg function */
5679 WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSASendMsg_GUID, sizeof(WSASendMsg_GUID),
5680 &pWSASendMsg, sizeof(pWSASendMsg), &err, NULL, NULL);
5681 if (!pWSASendMsg)
5683 closesocket(sock);
5684 win_skip("WSASendMsg is unsupported, some tests will be skipped.\n");
5685 return;
5688 /* fake address for now */
5689 sendaddr.sin_family = AF_INET;
5690 sendaddr.sin_port = htons(139);
5691 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
5693 memset(&msg, 0, sizeof(msg));
5694 iovec[0].buf = teststr;
5695 iovec[0].len = sizeof(teststr);
5696 iovec[1].buf = teststr;
5697 iovec[1].len = sizeof(teststr) / 2;
5698 msg.name = (struct sockaddr *) &sendaddr;
5699 msg.namelen = sizeof(sendaddr);
5700 msg.lpBuffers = iovec;
5701 msg.dwBufferCount = 1; /* send only one buffer for now */
5703 WSASetLastError(0xdeadbeef);
5704 ret = pWSASendMsg(INVALID_SOCKET, &msg, 0, NULL, NULL, NULL);
5705 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
5706 err = WSAGetLastError();
5707 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
5709 WSASetLastError(0xdeadbeef);
5710 ret = pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL);
5711 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
5712 err = WSAGetLastError();
5713 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
5715 WSASetLastError(0xdeadbeef);
5716 ret = pWSASendMsg(sock, NULL, 0, &bytesSent, NULL, NULL);
5717 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
5718 err = WSAGetLastError();
5719 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
5721 WSASetLastError(0xdeadbeef);
5722 ret = pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL);
5723 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
5724 err = WSAGetLastError();
5725 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
5727 closesocket(sock);
5729 sock = socket(AF_INET, SOCK_DGRAM, 0);
5730 ok(sock != INVALID_SOCKET, "socket() failed\n");
5732 dst = socket(AF_INET, SOCK_DGRAM, 0);
5733 ok(dst != INVALID_SOCKET, "socket() failed\n");
5735 memset(&sockaddr, 0, sizeof(sockaddr));
5736 sockaddr.sin_family = AF_INET;
5737 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
5738 ok(!bind(dst, (struct sockaddr*)&sockaddr, sizeof(sockaddr)),
5739 "bind should have worked\n");
5741 /* read address to find out the port number to be used in send */
5742 memset(&sendaddr, 0, sizeof(sendaddr));
5743 addrlen = sizeof(sendaddr);
5744 ok(!getsockname(dst, (struct sockaddr *) &sendaddr, &addrlen),
5745 "getsockname should have worked\n");
5746 ok(sendaddr.sin_port, "socket port should be != 0\n");
5748 /* ensure the sending socket is not bound */
5749 WSASetLastError(0xdeadbeef);
5750 addrlen = sizeof(sockaddr);
5751 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
5752 ok(ret == SOCKET_ERROR, "getsockname should have failed\n");
5753 err = WSAGetLastError();
5754 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
5756 set_blocking(sock, TRUE);
5758 bytesSent = 0;
5759 SetLastError(0xdeadbeef);
5760 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
5761 ok(!ret, "WSASendMsg should have worked\n");
5762 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
5763 "Expected 0, got %d\n", GetLastError());
5764 ok(bytesSent == iovec[0].len, "incorrect bytes sent, expected %d, sent %d\n",
5765 iovec[0].len, bytesSent);
5767 /* receive data */
5768 addrlen = sizeof(sockaddr);
5769 memset(buffer, 0, sizeof(buffer));
5770 SetLastError(0xdeadbeef);
5771 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
5772 ok(ret == bytesSent, "got %d, expected %d\n",
5773 ret, bytesSent);
5774 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5776 /* A successful call to WSASendMsg must have bound the socket */
5777 addrlen = sizeof(sockaddr);
5778 sockaddr.sin_port = 0;
5779 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
5780 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
5781 ok(!ret, "getsockname should have worked\n");
5782 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
5783 inet_ntoa(sockaddr.sin_addr));
5784 ok(sockaddr.sin_port, "sin_port should be != 0\n");
5786 msg.dwBufferCount = 2; /* send both buffers */
5788 bytesSent = 0;
5789 SetLastError(0xdeadbeef);
5790 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
5791 ok(!ret, "WSASendMsg should have worked\n");
5792 ok(bytesSent == iovec[0].len + iovec[1].len, "incorrect bytes sent, expected %d, sent %d\n",
5793 iovec[0].len + iovec[1].len, bytesSent);
5794 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
5795 "Expected 0, got %d\n", GetLastError());
5797 /* receive data */
5798 addrlen = sizeof(sockaddr);
5799 memset(buffer, 0, sizeof(buffer));
5800 SetLastError(0xdeadbeef);
5801 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
5802 ok(ret == bytesSent, "got %d, expected %d\n",
5803 ret, bytesSent);
5804 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5806 closesocket(sock);
5807 closesocket(dst);
5809 /* a bad call to WSASendMsg will also bind the socket */
5810 addrlen = sizeof(sockaddr);
5811 sockaddr.sin_port = 0;
5812 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
5813 sock = socket(AF_INET, SOCK_DGRAM, 0);
5814 ok(sock != INVALID_SOCKET, "socket() failed\n");
5815 ok(pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
5816 todo_wine {
5817 ok(!getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have worked\n");
5818 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
5819 inet_ntoa(sockaddr.sin_addr));
5820 ok(sockaddr.sin_port, "sin_port should be > 0\n");
5822 closesocket(sock);
5824 /* a bad call without msg parameter will not trigger the auto-bind */
5825 sock = socket(AF_INET, SOCK_DGRAM, 0);
5826 ok(sock != INVALID_SOCKET, "socket() failed\n");
5827 ok(pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
5828 ok(getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have failed\n");
5829 err = WSAGetLastError();
5830 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
5831 closesocket(sock);
5833 /* SOCK_STREAM sockets are not supported */
5834 bytesSent = 0;
5835 sock = socket(AF_INET, SOCK_STREAM, 0);
5836 ok(sock != INVALID_SOCKET, "socket() failed\n");
5837 SetLastError(0xdeadbeef);
5838 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
5839 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
5840 err = WSAGetLastError();
5841 todo_wine
5842 ok(err == WSAEINVAL, "expected 10014, got %d instead\n", err);
5843 closesocket(sock);
5846 static void test_WSASendTo(void)
5848 SOCKET s;
5849 struct sockaddr_in addr;
5850 char buf[12] = "hello world";
5851 WSABUF data_buf;
5852 DWORD bytesSent;
5853 int ret;
5855 addr.sin_family = AF_INET;
5856 addr.sin_port = htons(139);
5857 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
5858 data_buf.len = sizeof(buf);
5859 data_buf.buf = buf;
5861 s = socket(AF_INET, SOCK_DGRAM, 0);
5862 ok(s != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5864 WSASetLastError(12345);
5865 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
5866 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
5867 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
5869 WSASetLastError(12345);
5870 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
5871 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
5872 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
5874 WSASetLastError(12345);
5875 ret = WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr *)&addr, sizeof(addr), NULL, NULL);
5876 ok(!ret, "expected success\n");
5877 ok(!WSAGetLastError(), "got error %u\n", WSAGetLastError());
5880 static DWORD WINAPI recv_thread(LPVOID arg)
5882 SOCKET sock = *(SOCKET *)arg;
5883 char buffer[32];
5884 WSABUF wsa;
5885 WSAOVERLAPPED ov;
5886 DWORD flags = 0;
5888 wsa.buf = buffer;
5889 wsa.len = sizeof(buffer);
5890 ov.hEvent = WSACreateEvent();
5891 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL);
5893 WaitForSingleObject(ov.hEvent, 1000);
5894 WSACloseEvent(ov.hEvent);
5895 return 0;
5898 static int completion_called;
5900 static void WINAPI io_completion(DWORD error, DWORD transferred, WSAOVERLAPPED *overlapped, DWORD flags)
5902 completion_called++;
5905 static void test_WSARecv(void)
5907 SOCKET src, dest, server = INVALID_SOCKET;
5908 char buf[20];
5909 WSABUF bufs[2];
5910 WSAOVERLAPPED ov;
5911 DWORD bytesReturned, flags, id;
5912 struct linger ling;
5913 struct sockaddr_in addr;
5914 int iret, len;
5915 DWORD dwret;
5916 BOOL bret;
5917 HANDLE thread, event = NULL, io_port;
5919 tcp_socketpair(&src, &dest);
5921 memset(&ov, 0, sizeof(ov));
5922 flags = 0;
5923 bufs[0].len = 2;
5924 bufs[0].buf = buf;
5926 /* Send 4 bytes and receive in two calls of 2 */
5927 SetLastError(0xdeadbeef);
5928 iret = send(src, "test", 4, 0);
5929 ok(iret == 4, "Expected 4, got %d\n", iret);
5930 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5931 SetLastError(0xdeadbeef);
5932 bytesReturned = 0xdeadbeef;
5933 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5934 ok(!iret, "Expected 0, got %d\n", iret);
5935 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
5936 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5937 SetLastError(0xdeadbeef);
5938 bytesReturned = 0xdeadbeef;
5939 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5940 ok(!iret, "Expected 0, got %d\n", iret);
5941 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
5942 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5944 bufs[0].len = 4;
5945 SetLastError(0xdeadbeef);
5946 iret = send(src, "test", 4, 0);
5947 ok(iret == 4, "Expected 4, got %d\n", iret);
5948 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5949 SetLastError(0xdeadbeef);
5950 bytesReturned = 0xdeadbeef;
5951 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5952 ok(!iret, "Expected 0, got %d\n", iret);
5953 ok(bytesReturned == 4, "Expected 4, got %d\n", bytesReturned);
5954 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5956 /* Test 2 buffers */
5957 bufs[0].len = 4;
5958 bufs[1].len = 5;
5959 bufs[1].buf = buf + 10;
5960 SetLastError(0xdeadbeef);
5961 iret = send(src, "deadbeefs", 9, 0);
5962 ok(iret == 9, "Expected 9, got %d\n", iret);
5963 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5964 SetLastError(0xdeadbeef);
5965 bytesReturned = 0xdeadbeef;
5966 iret = WSARecv(dest, bufs, 2, &bytesReturned, &flags, NULL, NULL);
5967 ok(!iret, "Expected 0, got %d\n", iret);
5968 ok(bytesReturned == 9, "Expected 9, got %d\n", bytesReturned);
5969 bufs[0].buf[4] = '\0';
5970 bufs[1].buf[5] = '\0';
5971 ok(!strcmp(bufs[0].buf, "dead"), "buf[0] doesn't match: %s != dead\n", bufs[0].buf);
5972 ok(!strcmp(bufs[1].buf, "beefs"), "buf[1] doesn't match: %s != beefs\n", bufs[1].buf);
5973 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5975 bufs[0].len = sizeof(buf);
5976 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
5977 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
5978 if (!event)
5979 goto end;
5981 ling.l_onoff = 1;
5982 ling.l_linger = 0;
5983 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
5984 ok(!iret, "Failed to set linger %d\n", GetLastError());
5986 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, NULL);
5987 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
5989 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, &ov, NULL);
5990 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
5992 closesocket(src);
5993 src = INVALID_SOCKET;
5995 dwret = WaitForSingleObject(ov.hEvent, 1000);
5996 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
5998 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
5999 todo_wine ok(!bret, "expected failure\n");
6000 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "got error %u\n", GetLastError());
6001 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
6002 closesocket(dest);
6003 dest = INVALID_SOCKET;
6005 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
6006 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
6007 if (src == INVALID_SOCKET) goto end;
6009 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6010 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
6011 if (server == INVALID_SOCKET) goto end;
6013 memset(&addr, 0, sizeof(addr));
6014 addr.sin_family = AF_INET;
6015 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
6016 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
6017 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
6019 len = sizeof(addr);
6020 iret = getsockname(server, (struct sockaddr *)&addr, &len);
6021 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
6023 iret = listen(server, 1);
6024 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
6026 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr));
6027 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
6029 len = sizeof(addr);
6030 dest = accept(server, (struct sockaddr *)&addr, &len);
6031 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
6032 if (dest == INVALID_SOCKET) goto end;
6034 send(src, "test message", sizeof("test message"), 0);
6035 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id);
6036 WaitForSingleObject(thread, 3000);
6037 CloseHandle(thread);
6039 memset(&ov, 0, sizeof(ov));
6040 ov.hEvent = event;
6041 ResetEvent(event);
6042 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
6043 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
6044 send(src, "test message", sizeof("test message"), 0);
6046 completion_called = 0;
6047 dwret = SleepEx(1000, TRUE);
6048 ok(dwret == WAIT_IO_COMPLETION, "got %u\n", dwret);
6049 ok(completion_called == 1, "completion not called\n");
6051 dwret = WaitForSingleObject(event, 1);
6052 ok(dwret == WAIT_TIMEOUT, "got %u\n", dwret);
6054 io_port = CreateIoCompletionPort( (HANDLE)dest, NULL, 0, 0 );
6055 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
6057 /* Using completion function on socket associated with completion port is not allowed. */
6058 memset(&ov, 0, sizeof(ov));
6059 completion_called = 0;
6060 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
6061 ok(iret == SOCKET_ERROR && GetLastError() == WSAEINVAL, "WSARecv failed - %d error %d\n", iret, GetLastError());
6062 ok(!completion_called, "completion called\n");
6064 CloseHandle(io_port);
6066 end:
6067 if (server != INVALID_SOCKET)
6068 closesocket(server);
6069 if (dest != INVALID_SOCKET)
6070 closesocket(dest);
6071 if (src != INVALID_SOCKET)
6072 closesocket(src);
6073 if (event)
6074 WSACloseEvent(event);
6077 struct write_watch_thread_args
6079 int func;
6080 SOCKET dest;
6081 void *base;
6082 DWORD size;
6083 const char *expect;
6086 static DWORD CALLBACK write_watch_thread( void *arg )
6088 struct write_watch_thread_args *args = arg;
6089 struct sockaddr addr;
6090 int addr_len = sizeof(addr), ret;
6091 DWORD bytes, flags = 0;
6092 WSABUF buf[1];
6094 switch (args->func)
6096 case 0:
6097 ret = recv( args->dest, args->base, args->size, 0 );
6098 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
6099 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
6100 break;
6101 case 1:
6102 ret = recvfrom( args->dest, args->base, args->size, 0, &addr, &addr_len );
6103 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
6104 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
6105 break;
6106 case 2:
6107 buf[0].len = args->size;
6108 buf[0].buf = args->base;
6109 ret = WSARecv( args->dest, buf, 1, &bytes, &flags, NULL, NULL );
6110 ok( !ret, "WSARecv failed %u\n", GetLastError() );
6111 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
6112 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
6113 break;
6114 case 3:
6115 buf[0].len = args->size;
6116 buf[0].buf = args->base;
6117 ret = WSARecvFrom( args->dest, buf, 1, &bytes, &flags, &addr, &addr_len, NULL, NULL );
6118 ok( !ret, "WSARecvFrom failed %u\n", GetLastError() );
6119 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
6120 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
6121 break;
6123 return 0;
6126 static void test_write_watch(void)
6128 SOCKET src, dest;
6129 WSABUF bufs[2];
6130 WSAOVERLAPPED ov;
6131 struct write_watch_thread_args args;
6132 DWORD bytesReturned, flags, size;
6133 struct sockaddr addr;
6134 int addr_len, ret;
6135 HANDLE thread, event;
6136 char *base;
6137 void *results[64];
6138 ULONG_PTR count;
6139 ULONG pagesize;
6140 UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
6142 pGetWriteWatch = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetWriteWatch" );
6143 if (!pGetWriteWatch)
6145 win_skip( "write watched not supported\n" );
6146 return;
6149 tcp_socketpair(&src, &dest);
6151 memset(&ov, 0, sizeof(ov));
6152 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
6153 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
6155 flags = 0;
6157 size = 0x10000;
6158 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
6159 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
6161 memset( base, 0, size );
6162 count = 64;
6163 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6164 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6165 ok( count == 16, "wrong count %lu\n", count );
6167 bufs[0].len = 5;
6168 bufs[0].buf = base;
6169 bufs[1].len = 0x8000;
6170 bufs[1].buf = base + 0x4000;
6172 ret = WSARecv( dest, bufs, 2, NULL, &flags, &ov, NULL);
6173 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
6174 "WSARecv failed - %d error %d\n", ret, GetLastError());
6176 count = 64;
6177 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6178 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6179 ok( count == 9, "wrong count %lu\n", count );
6180 ok( !base[0], "data set\n" );
6182 send(src, "test message", sizeof("test message"), 0);
6184 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
6185 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
6186 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
6187 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
6188 ok( !memcmp( base + 0x4000, "message", 8 ), "wrong data %s\n", base + 0x4000 );
6190 count = 64;
6191 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6192 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6193 ok( count == 0, "wrong count %lu\n", count );
6195 memset( base, 0, size );
6196 count = 64;
6197 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6198 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6199 ok( count == 16, "wrong count %lu\n", count );
6201 bufs[1].len = 0x4000;
6202 bufs[1].buf = base + 0x2000;
6203 ret = WSARecvFrom( dest, bufs, 2, NULL, &flags, &addr, &addr_len, &ov, NULL);
6204 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
6205 "WSARecv failed - %d error %d\n", ret, GetLastError());
6207 count = 64;
6208 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6209 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6210 ok( count == 5, "wrong count %lu\n", count );
6211 ok( !base[0], "data set\n" );
6213 send(src, "test message", sizeof("test message"), 0);
6215 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
6216 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
6217 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
6218 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
6219 ok( !memcmp( base + 0x2000, "message", 8 ), "wrong data %s\n", base + 0x2000 );
6221 count = 64;
6222 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6223 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6224 ok( count == 0, "wrong count %lu\n", count );
6226 memset( base, 0, size );
6227 count = 64;
6228 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6229 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6230 ok( count == 16, "wrong count %lu\n", count );
6232 args.dest = dest;
6233 args.base = base;
6234 args.size = 0x7002;
6235 args.expect = "test message";
6236 for (args.func = 0; args.func < 4; args.func++)
6238 thread = CreateThread( NULL, 0, write_watch_thread, &args, 0, NULL );
6239 Sleep( 200 );
6241 count = 64;
6242 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6243 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6244 ok( count == 8, "wrong count %lu\n", count );
6246 send(src, "test message", sizeof("test message"), 0);
6247 WaitForSingleObject( thread, 10000 );
6248 CloseHandle( thread );
6250 count = 64;
6251 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
6252 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
6253 ok( count == 0, "wrong count %lu\n", count );
6255 WSACloseEvent( event );
6256 closesocket( dest );
6257 closesocket( src );
6258 VirtualFree( base, 0, MEM_FREE );
6261 #define POLL_CLEAR() ix = 0
6262 #define POLL_SET(s, ev) {fds[ix].fd = s; fds[ix++].events = ev;}
6263 #define POLL_ISSET(s, rev) poll_isset(fds, ix, s, rev)
6264 static BOOL poll_isset(WSAPOLLFD *fds, int max, SOCKET s, int rev)
6266 int k;
6267 for (k = 0; k < max; k++)
6268 if (fds[k].fd == s && (fds[k].revents == rev)) return TRUE;
6269 return FALSE;
6272 static void test_WSAPoll(void)
6274 int ix, ret, err;
6275 SOCKET fdListen, fdRead, fdWrite;
6276 struct sockaddr_in address;
6277 socklen_t len;
6278 static char tmp_buf[1024];
6279 WSAPOLLFD fds[16];
6280 HANDLE thread_handle;
6281 DWORD id;
6283 if (!pWSAPoll) /* >= Vista */
6285 win_skip("WSAPoll is unsupported, some tests will be skipped.\n");
6286 return;
6289 /* Invalid parameters test */
6290 SetLastError(0xdeadbeef);
6291 ret = pWSAPoll(NULL, 0, 0);
6292 err = GetLastError();
6293 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6294 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
6295 SetLastError(0xdeadbeef);
6296 ret = pWSAPoll(NULL, 1, 0);
6297 err = GetLastError();
6298 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6299 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
6300 SetLastError(0xdeadbeef);
6301 ret = pWSAPoll(NULL, 0, 1);
6302 err = GetLastError();
6303 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6304 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
6305 SetLastError(0xdeadbeef);
6306 ret = pWSAPoll(NULL, 1, 1);
6307 err = GetLastError();
6308 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6309 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
6311 /* WSAPoll() tries to mime the unix poll() call. The following tests do:
6312 * - check if a connection attempt ended with success or error;
6313 * - check if a pending connection is waiting for acceptance;
6314 * - check for data to read, availability for write and OOB data
6316 memset(&address, 0, sizeof(address));
6317 address.sin_addr.s_addr = inet_addr("127.0.0.1");
6318 address.sin_family = AF_INET;
6319 len = sizeof(address);
6320 fdListen = setup_server_socket(&address, &len);
6322 /* When no events are pending poll returns 0 with no error */
6323 POLL_CLEAR();
6324 POLL_SET(fdListen, POLLIN);
6325 ret = pWSAPoll(fds, ix, 100);
6326 ok(ret == 0, "expected 0, got %d\n", ret);
6328 /* Test listening socket connection attempt notifications */
6329 fdWrite = setup_connector_socket(&address, len, TRUE);
6330 POLL_CLEAR();
6331 POLL_SET(fdListen, POLLIN | POLLOUT);
6332 ret = pWSAPoll(fds, ix, 100);
6333 ok(ret == 1, "expected 1, got %d\n", ret);
6334 ok(POLL_ISSET(fdListen, POLLRDNORM), "fdListen socket events incorrect\n");
6335 len = sizeof(address);
6336 fdRead = accept(fdListen, (struct sockaddr*) &address, &len);
6337 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n");
6339 /* Test client side connection attempt notifications */
6340 POLL_CLEAR();
6341 POLL_SET(fdListen, POLLIN | POLLOUT);
6342 POLL_SET(fdRead, POLLIN | POLLOUT);
6343 POLL_SET(fdWrite, POLLIN | POLLOUT);
6344 ret = pWSAPoll(fds, ix, 100);
6345 ok(ret == 2, "expected 2, got %d\n", ret);
6346 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n");
6347 ok(POLL_ISSET(fdRead, POLLWRNORM), "fdRead socket events incorrect\n");
6348 len = sizeof(id);
6349 id = 0xdeadbeef;
6350 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
6351 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
6352 ok(id == 0, "expected 0, got %d\n", id);
6354 /* Test data receiving notifications */
6355 ret = send(fdWrite, "1234", 4, 0);
6356 ok(ret == 4, "expected 4, got %d\n", ret);
6357 POLL_CLEAR();
6358 POLL_SET(fdListen, POLLIN | POLLOUT);
6359 POLL_SET(fdRead, POLLIN);
6360 ret = pWSAPoll(fds, ix, 100);
6361 ok(ret == 1, "expected 1, got %d\n", ret);
6362 ok(POLL_ISSET(fdRead, POLLRDNORM), "fdRead socket events incorrect\n");
6363 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
6364 ok(ret == 4, "expected 4, got %d\n", ret);
6365 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n");
6367 /* Test OOB data notifications */
6368 ret = send(fdWrite, "A", 1, MSG_OOB);
6369 ok(ret == 1, "expected 1, got %d\n", ret);
6370 POLL_CLEAR();
6371 POLL_SET(fdListen, POLLIN | POLLOUT);
6372 POLL_SET(fdRead, POLLIN);
6373 ret = pWSAPoll(fds, ix, 100);
6374 ok(ret == 1, "expected 1, got %d\n", ret);
6375 ok(POLL_ISSET(fdRead, POLLRDBAND), "fdRead socket events incorrect\n");
6376 tmp_buf[0] = 0xAF;
6377 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
6378 ok(ret == 1, "expected 1, got %d\n", ret);
6379 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
6381 /* If the socket is OOBINLINED the notification is like normal data */
6382 ret = 1;
6383 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret));
6384 ok(ret == 0, "expected 0, got %d\n", ret);
6385 ret = send(fdWrite, "A", 1, MSG_OOB);
6386 ok(ret == 1, "expected 1, got %d\n", ret);
6387 POLL_CLEAR();
6388 POLL_SET(fdListen, POLLIN | POLLOUT);
6389 POLL_SET(fdRead, POLLIN | POLLOUT);
6390 ret = pWSAPoll(fds, ix, 100);
6391 ok(ret == 1, "expected 1, got %d\n", ret);
6392 tmp_buf[0] = 0xAF;
6393 SetLastError(0xdeadbeef);
6394 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
6395 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6396 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError());
6397 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
6398 ok(ret == 1, "expected 1, got %d\n", ret);
6399 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
6401 /* Test connection closed notifications */
6402 ret = closesocket(fdRead);
6403 ok(ret == 0, "expected 0, got %d\n", ret);
6404 POLL_CLEAR();
6405 POLL_SET(fdListen, POLLIN | POLLOUT);
6406 POLL_SET(fdWrite, POLLIN);
6407 ret = pWSAPoll(fds, ix, 100);
6408 ok(ret == 1, "expected 1, got %d\n", ret);
6409 ok(POLL_ISSET(fdWrite, POLLHUP), "fdWrite socket events incorrect\n");
6410 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0);
6411 ok(ret == 0, "expected 0, got %d\n", ret);
6412 ret = closesocket(fdWrite);
6413 ok(ret == 0, "expected 0, got %d\n", ret);
6414 ret = closesocket(fdListen);
6415 ok(ret == 0, "expected 0, got %d\n", ret);
6417 /* The following WSAPoll() call times out on versions older than w10pro64,
6418 * but even on w10pro64 it takes over 2 seconds for an error to be reported,
6419 * so make the test interactive-only. */
6420 if (winetest_interactive)
6422 len = sizeof(address);
6423 fdWrite = setup_connector_socket(&address, len, TRUE);
6424 POLL_CLEAR();
6425 POLL_SET(fdWrite, POLLIN | POLLOUT);
6426 ret = pWSAPoll(fds, ix, 10000);
6427 ok(ret == 1, "expected 0, got %d\n", ret);
6428 len = sizeof(id);
6429 id = 0xdeadbeef;
6430 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
6431 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
6432 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id);
6433 closesocket(fdWrite);
6436 /* Try poll() on a closed socket after connection */
6437 tcp_socketpair(&fdRead, &fdWrite);
6438 closesocket(fdRead);
6439 POLL_CLEAR();
6440 POLL_SET(fdWrite, POLLIN | POLLOUT);
6441 POLL_SET(fdRead, POLLIN | POLLOUT);
6442 ret = pWSAPoll(fds, ix, 2000);
6443 ok(ret == 1, "expected 1, got %d\n", ret);
6444 ok(POLL_ISSET(fdRead, POLLNVAL), "fdRead socket events incorrect\n");
6445 POLL_CLEAR();
6446 POLL_SET(fdWrite, POLLIN | POLLOUT);
6447 ret = pWSAPoll(fds, ix, 2000);
6448 ok(ret == 1, "expected 1, got %d\n", ret);
6449 todo_wine
6450 ok(POLL_ISSET(fdWrite, POLLWRNORM | POLLHUP) || broken(POLL_ISSET(fdWrite, POLLWRNORM)) /* <= 2008 */,
6451 "fdWrite socket events incorrect\n");
6452 closesocket(fdWrite);
6454 /* Close the socket currently being polled in a thread */
6455 tcp_socketpair(&fdRead, &fdWrite);
6456 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
6457 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
6458 POLL_CLEAR();
6459 POLL_SET(fdWrite, POLLIN | POLLOUT);
6460 ret = pWSAPoll(fds, ix, 2000);
6461 ok(ret == 1, "expected 1, got %d\n", ret);
6462 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n");
6463 WaitForSingleObject (thread_handle, 1000);
6464 closesocket(fdRead);
6465 /* test again with less flags - behavior changes */
6466 tcp_socketpair(&fdRead, &fdWrite);
6467 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
6468 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
6469 POLL_CLEAR();
6470 POLL_SET(fdWrite, POLLIN);
6471 ret = pWSAPoll(fds, ix, 2000);
6472 ok(ret == 1, "expected 1, got %d\n", ret);
6473 ok(POLL_ISSET(fdWrite, POLLNVAL), "fdWrite socket events incorrect\n");
6474 WaitForSingleObject (thread_handle, 1000);
6475 closesocket(fdRead);
6477 #undef POLL_SET
6478 #undef POLL_ISSET
6479 #undef POLL_CLEAR
6481 static void test_GetAddrInfoW(void)
6483 static const WCHAR port[] = {'8','0',0};
6484 static const WCHAR empty[] = {0};
6485 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
6486 static const WCHAR nxdomain[] =
6487 {'n','x','d','o','m','a','i','n','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
6488 static const WCHAR zero[] = {'0',0};
6489 int i, ret;
6490 ADDRINFOW *result, *result2, *p, hint;
6491 WCHAR name[256];
6492 DWORD size = ARRAY_SIZE(name);
6493 /* te su to.winehq.org written in katakana */
6494 static const WCHAR idn_domain[] =
6495 {0x30C6,0x30B9,0x30C8,'.','w','i','n','e','h','q','.','o','r','g',0};
6496 static const WCHAR idn_punycode[] =
6497 {'x','n','-','-','z','c','k','z','a','h','.','w','i','n','e','h','q','.','o','r','g',0};
6499 memset(&hint, 0, sizeof(ADDRINFOW));
6500 name[0] = 0;
6501 GetComputerNameExW( ComputerNamePhysicalDnsHostname, name, &size );
6503 result = (ADDRINFOW *)0xdeadbeef;
6504 WSASetLastError(0xdeadbeef);
6505 ret = GetAddrInfoW(NULL, NULL, NULL, &result);
6506 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6507 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6508 ok(result == NULL, "got %p\n", result);
6510 result = NULL;
6511 WSASetLastError(0xdeadbeef);
6512 ret = GetAddrInfoW(empty, NULL, NULL, &result);
6513 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6514 ok(result != NULL, "GetAddrInfoW failed\n");
6515 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6516 FreeAddrInfoW(result);
6518 result = NULL;
6519 ret = GetAddrInfoW(NULL, zero, NULL, &result);
6520 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6521 ok(result != NULL, "GetAddrInfoW failed\n");
6523 result2 = NULL;
6524 ret = GetAddrInfoW(NULL, empty, NULL, &result2);
6525 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6526 ok(result2 != NULL, "GetAddrInfoW failed\n");
6527 compare_addrinfow(result, result2);
6528 FreeAddrInfoW(result);
6529 FreeAddrInfoW(result2);
6531 result = NULL;
6532 ret = GetAddrInfoW(empty, zero, NULL, &result);
6533 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6534 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6535 ok(result != NULL, "GetAddrInfoW failed\n");
6537 result2 = NULL;
6538 ret = GetAddrInfoW(empty, empty, NULL, &result2);
6539 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6540 ok(result2 != NULL, "GetAddrInfoW failed\n");
6541 compare_addrinfow(result, result2);
6542 FreeAddrInfoW(result);
6543 FreeAddrInfoW(result2);
6545 result = NULL;
6546 ret = GetAddrInfoW(localhost, NULL, NULL, &result);
6547 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6548 FreeAddrInfoW(result);
6550 result = NULL;
6551 ret = GetAddrInfoW(localhost, empty, NULL, &result);
6552 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6553 FreeAddrInfoW(result);
6555 result = NULL;
6556 ret = GetAddrInfoW(localhost, zero, NULL, &result);
6557 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6558 FreeAddrInfoW(result);
6560 result = NULL;
6561 ret = GetAddrInfoW(localhost, port, NULL, &result);
6562 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6563 FreeAddrInfoW(result);
6565 result = NULL;
6566 ret = GetAddrInfoW(localhost, NULL, &hint, &result);
6567 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6568 FreeAddrInfoW(result);
6570 result = NULL;
6571 SetLastError(0xdeadbeef);
6572 ret = GetAddrInfoW(localhost, port, &hint, &result);
6573 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6574 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6575 FreeAddrInfoW(result);
6577 /* try to get information from the computer name, result is the same
6578 * as if requesting with an empty host name. */
6579 ret = GetAddrInfoW(name, NULL, NULL, &result);
6580 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6581 ok(result != NULL, "GetAddrInfoW failed\n");
6583 ret = GetAddrInfoW(empty, NULL, NULL, &result2);
6584 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6585 ok(result != NULL, "GetAddrInfoW failed\n");
6586 compare_addrinfow(result, result2);
6587 FreeAddrInfoW(result);
6588 FreeAddrInfoW(result2);
6590 ret = GetAddrInfoW(name, empty, NULL, &result);
6591 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6592 ok(result != NULL, "GetAddrInfoW failed\n");
6594 ret = GetAddrInfoW(empty, empty, NULL, &result2);
6595 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6596 ok(result != NULL, "GetAddrInfoW failed\n");
6597 compare_addrinfow(result, result2);
6598 FreeAddrInfoW(result);
6599 FreeAddrInfoW(result2);
6601 result = (ADDRINFOW *)0xdeadbeef;
6602 WSASetLastError(0xdeadbeef);
6603 ret = GetAddrInfoW(NULL, NULL, NULL, &result);
6604 if(ret == 0)
6606 skip("nxdomain returned success. Broken ISP redirects?\n");
6607 return;
6609 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6610 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6611 ok(result == NULL, "got %p\n", result);
6613 result = (ADDRINFOW *)0xdeadbeef;
6614 WSASetLastError(0xdeadbeef);
6615 ret = GetAddrInfoW(nxdomain, NULL, NULL, &result);
6616 if(ret == 0)
6618 skip("nxdomain returned success. Broken ISP redirects?\n");
6619 return;
6621 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6622 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6623 ok(result == NULL, "got %p\n", result);
6625 for (i = 0;i < (ARRAY_SIZE(hinttests));i++)
6627 hint.ai_family = hinttests[i].family;
6628 hint.ai_socktype = hinttests[i].socktype;
6629 hint.ai_protocol = hinttests[i].protocol;
6631 result = NULL;
6632 SetLastError(0xdeadbeef);
6633 ret = GetAddrInfoW(localhost, NULL, &hint, &result);
6634 todo_wine_if(hinttests[i].error) ok(ret == hinttests[i].error, "test %d: wrong ret %d\n", i, ret);
6635 if(!ret)
6637 for (p = result; p; p = p->ai_next)
6639 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
6640 if (hinttests[i].family == AF_UNSPEC)
6641 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6,
6642 "test %d: expected AF_INET or AF_INET6, got %d\n",
6643 i, p->ai_family);
6644 else
6645 ok(p->ai_family == hinttests[i].family,
6646 "test %d: expected family %d, got %d\n",
6647 i, hinttests[i].family, p->ai_family);
6649 ok(p->ai_socktype == hinttests[i].socktype,
6650 "test %d: expected type %d, got %d\n",
6651 i, hinttests[i].socktype, p->ai_socktype);
6652 ok(p->ai_protocol == hinttests[i].protocol,
6653 "test %d: expected protocol %d, got %d\n",
6654 i, hinttests[i].protocol, p->ai_protocol);
6656 FreeAddrInfoW(result);
6658 else
6660 ok(WSAGetLastError() == hinttests[i].error, "test %d: wrong error %d\n", i, WSAGetLastError());
6664 /* Test IDN resolution (Internationalized Domain Names) present since Windows 8 */
6665 result = NULL;
6666 ret = GetAddrInfoW(idn_punycode, NULL, NULL, &result);
6667 ok(!ret, "got %d expected success\n", ret);
6668 ok(result != NULL, "got %p\n", result);
6669 FreeAddrInfoW(result);
6671 hint.ai_family = AF_INET;
6672 hint.ai_socktype = 0;
6673 hint.ai_protocol = 0;
6674 hint.ai_flags = 0;
6676 result = NULL;
6677 ret = GetAddrInfoW(idn_punycode, NULL, &hint, &result);
6678 ok(!ret, "got %d expected success\n", ret);
6679 ok(result != NULL, "got %p\n", result);
6681 result2 = NULL;
6682 ret = GetAddrInfoW(idn_domain, NULL, NULL, &result2);
6683 if (broken(ret == WSAHOST_NOT_FOUND))
6685 FreeAddrInfoW(result);
6686 win_skip("IDN resolution not supported in Win <= 7\n");
6687 return;
6690 ok(!ret, "got %d expected success\n", ret);
6691 ok(result2 != NULL, "got %p\n", result2);
6692 FreeAddrInfoW(result2);
6694 hint.ai_family = AF_INET;
6695 hint.ai_socktype = 0;
6696 hint.ai_protocol = 0;
6697 hint.ai_flags = 0;
6699 result2 = NULL;
6700 ret = GetAddrInfoW(idn_domain, NULL, &hint, &result2);
6701 ok(!ret, "got %d expected success\n", ret);
6702 ok(result2 != NULL, "got %p\n", result2);
6704 /* ensure manually resolved punycode and unicode hosts result in same data */
6705 compare_addrinfow(result, result2);
6707 FreeAddrInfoW(result);
6708 FreeAddrInfoW(result2);
6710 hint.ai_family = AF_INET;
6711 hint.ai_socktype = 0;
6712 hint.ai_protocol = 0;
6713 hint.ai_flags = 0;
6715 result2 = NULL;
6716 ret = GetAddrInfoW(idn_domain, NULL, &hint, &result2);
6717 ok(!ret, "got %d expected success\n", ret);
6718 ok(result2 != NULL, "got %p\n", result2);
6719 FreeAddrInfoW(result2);
6721 /* Disable IDN resolution and test again*/
6722 hint.ai_family = AF_INET;
6723 hint.ai_socktype = 0;
6724 hint.ai_protocol = 0;
6725 hint.ai_flags = AI_DISABLE_IDN_ENCODING;
6727 SetLastError(0xdeadbeef);
6728 result2 = NULL;
6729 ret = GetAddrInfoW(idn_domain, NULL, &hint, &result2);
6730 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6731 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6732 ok(result2 == NULL, "got %p\n", result2);
6735 static struct completion_routine_test
6737 WSAOVERLAPPED *overlapped;
6738 DWORD error;
6739 ADDRINFOEXW **result;
6740 HANDLE event;
6741 DWORD called;
6742 } completion_routine_test;
6744 static void CALLBACK completion_routine(DWORD error, DWORD byte_count, WSAOVERLAPPED *overlapped)
6746 struct completion_routine_test *test = &completion_routine_test;
6748 ok(error == test->error, "got %u\n", error);
6749 ok(!byte_count, "got %u\n", byte_count);
6750 ok(overlapped == test->overlapped, "got %p\n", overlapped);
6751 ok(overlapped->Internal == test->error, "got %lu\n", overlapped->Internal);
6752 ok(overlapped->Pointer == test->result, "got %p\n", overlapped->Pointer);
6753 ok(overlapped->hEvent == NULL, "got %p\n", overlapped->hEvent);
6755 test->called++;
6756 SetEvent(test->event);
6759 static void test_GetAddrInfoExW(void)
6761 static const WCHAR empty[] = {0};
6762 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
6763 static const WCHAR winehq[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
6764 static const WCHAR nxdomain[] = {'n','x','d','o','m','a','i','n','.','w','i','n','e','h','q','.','o','r','g',0};
6765 ADDRINFOEXW *result;
6766 OVERLAPPED overlapped;
6767 HANDLE event;
6768 int ret;
6770 if (!pGetAddrInfoExW || !pGetAddrInfoExOverlappedResult)
6772 win_skip("GetAddrInfoExW and/or GetAddrInfoExOverlappedResult not present\n");
6773 return;
6776 event = WSACreateEvent();
6778 result = (ADDRINFOEXW *)0xdeadbeef;
6779 WSASetLastError(0xdeadbeef);
6780 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
6781 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6782 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6783 ok(result == NULL, "got %p\n", result);
6785 result = NULL;
6786 WSASetLastError(0xdeadbeef);
6787 ret = pGetAddrInfoExW(empty, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
6788 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6789 ok(result != NULL, "GetAddrInfoW failed\n");
6790 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6791 pFreeAddrInfoExW(result);
6793 result = NULL;
6794 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
6795 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6796 pFreeAddrInfoExW(result);
6798 result = (void*)0xdeadbeef;
6799 memset(&overlapped, 0xcc, sizeof(overlapped));
6800 overlapped.hEvent = event;
6801 ResetEvent(event);
6802 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
6803 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6804 ok(!result, "result != NULL\n");
6805 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
6806 ret = pGetAddrInfoExOverlappedResult(&overlapped);
6807 ok(!ret, "overlapped result is %d\n", ret);
6808 pFreeAddrInfoExW(result);
6810 result = (void*)0xdeadbeef;
6811 memset(&overlapped, 0xcc, sizeof(overlapped));
6812 ResetEvent(event);
6813 overlapped.hEvent = event;
6814 WSASetLastError(0xdeadbeef);
6815 ret = pGetAddrInfoExW(winehq, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
6816 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6817 ok(WSAGetLastError() == ERROR_IO_PENDING, "expected 11001, got %d\n", WSAGetLastError());
6818 ret = overlapped.Internal;
6819 ok(ret == WSAEINPROGRESS || ret == ERROR_SUCCESS, "overlapped.Internal = %u\n", ret);
6820 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
6821 ret = pGetAddrInfoExOverlappedResult(&overlapped);
6822 ok(!ret, "overlapped result is %d\n", ret);
6823 ok(overlapped.hEvent == event, "hEvent changed %p\n", overlapped.hEvent);
6824 ok(overlapped.Internal == ERROR_SUCCESS, "overlapped.Internal = %lx\n", overlapped.Internal);
6825 ok(overlapped.Pointer == &result, "overlapped.Pointer != &result\n");
6826 ok(result != NULL, "result == NULL\n");
6827 if (result != NULL)
6829 ok(!result->ai_blob, "ai_blob != NULL\n");
6830 ok(!result->ai_bloblen, "ai_bloblen != 0\n");
6831 ok(!result->ai_provider, "ai_provider = %s\n", wine_dbgstr_guid(result->ai_provider));
6832 pFreeAddrInfoExW(result);
6835 result = (void*)0xdeadbeef;
6836 memset(&overlapped, 0xcc, sizeof(overlapped));
6837 ResetEvent(event);
6838 overlapped.hEvent = event;
6839 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
6840 todo_wine
6841 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6842 todo_wine
6843 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6844 ok(result == NULL, "got %p\n", result);
6845 ret = WaitForSingleObject(event, 0);
6846 todo_wine_if(ret != WAIT_TIMEOUT) /* Remove when abowe todo_wines are fixed */
6847 ok(ret == WAIT_TIMEOUT, "wait failed\n");
6849 /* event + completion routine */
6850 result = (void*)0xdeadbeef;
6851 memset(&overlapped, 0xcc, sizeof(overlapped));
6852 overlapped.hEvent = event;
6853 ResetEvent(event);
6854 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, completion_routine, NULL);
6855 ok(ret == WSAEINVAL, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6857 /* completion routine, existing domain */
6858 result = (void *)0xdeadbeef;
6859 overlapped.hEvent = NULL;
6860 completion_routine_test.overlapped = &overlapped;
6861 completion_routine_test.error = ERROR_SUCCESS;
6862 completion_routine_test.result = &result;
6863 completion_routine_test.event = event;
6864 completion_routine_test.called = 0;
6865 ResetEvent(event);
6866 ret = pGetAddrInfoExW(winehq, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, completion_routine, NULL);
6867 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6868 ok(!result, "result != NULL\n");
6869 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
6870 ret = pGetAddrInfoExOverlappedResult(&overlapped);
6871 ok(!ret, "overlapped result is %d\n", ret);
6872 ok(overlapped.hEvent == NULL, "hEvent changed %p\n", overlapped.hEvent);
6873 ok(overlapped.Internal == ERROR_SUCCESS, "overlapped.Internal = %lx\n", overlapped.Internal);
6874 ok(overlapped.Pointer == &result, "overlapped.Pointer != &result\n");
6875 ok(completion_routine_test.called == 1, "got %u\n", completion_routine_test.called);
6876 pFreeAddrInfoExW(result);
6878 /* completion routine, non-existing domain */
6879 result = (void *)0xdeadbeef;
6880 completion_routine_test.overlapped = &overlapped;
6881 completion_routine_test.error = WSAHOST_NOT_FOUND;
6882 completion_routine_test.called = 0;
6883 ResetEvent(event);
6884 ret = pGetAddrInfoExW(nxdomain, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, completion_routine, NULL);
6885 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
6886 ok(!result, "result != NULL\n");
6887 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
6888 ret = pGetAddrInfoExOverlappedResult(&overlapped);
6889 ok(ret == WSAHOST_NOT_FOUND, "overlapped result is %d\n", ret);
6890 ok(overlapped.hEvent == NULL, "hEvent changed %p\n", overlapped.hEvent);
6891 ok(overlapped.Internal == WSAHOST_NOT_FOUND, "overlapped.Internal = %lx\n", overlapped.Internal);
6892 ok(overlapped.Pointer == &result, "overlapped.Pointer != &result\n");
6893 ok(completion_routine_test.called == 1, "got %u\n", completion_routine_test.called);
6894 ok(result == NULL, "got %p\n", result);
6896 WSACloseEvent(event);
6899 static void verify_ipv6_addrinfo(ADDRINFOA *result, const char *expectedIp)
6901 SOCKADDR_IN6 *sockaddr6;
6902 char ipBuffer[256];
6903 const char *ret;
6905 ok(result->ai_family == AF_INET6, "ai_family == %d\n", result->ai_family);
6906 ok(result->ai_addrlen >= sizeof(struct sockaddr_in6), "ai_addrlen == %d\n", (int)result->ai_addrlen);
6907 ok(result->ai_addr != NULL, "ai_addr == NULL\n");
6909 if (result->ai_addr != NULL)
6911 sockaddr6 = (SOCKADDR_IN6 *)result->ai_addr;
6912 ok(sockaddr6->sin6_family == AF_INET6, "ai_addr->sin6_family == %d\n", sockaddr6->sin6_family);
6913 ok(sockaddr6->sin6_port == 0, "ai_addr->sin6_port == %d\n", sockaddr6->sin6_port);
6915 ZeroMemory(ipBuffer, sizeof(ipBuffer));
6916 ret = pInetNtop(AF_INET6, &sockaddr6->sin6_addr, ipBuffer, sizeof(ipBuffer));
6917 ok(ret != NULL, "inet_ntop failed (%d)\n", WSAGetLastError());
6918 ok(strcmp(ipBuffer, expectedIp) == 0, "ai_addr->sin6_addr == '%s' (expected '%s')\n", ipBuffer, expectedIp);
6922 static void test_getaddrinfo(void)
6924 int i, ret;
6925 ADDRINFOA *result, *result2, *p, hint;
6926 SOCKADDR_IN *sockaddr;
6927 CHAR name[256], *ip;
6928 DWORD size = sizeof(name);
6930 memset(&hint, 0, sizeof(ADDRINFOA));
6931 GetComputerNameExA( ComputerNamePhysicalDnsHostname, name, &size );
6933 result = (ADDRINFOA *)0xdeadbeef;
6934 WSASetLastError(0xdeadbeef);
6935 ret = getaddrinfo(NULL, NULL, NULL, &result);
6936 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
6937 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
6938 ok(result == NULL, "got %p\n", result);
6940 result = NULL;
6941 WSASetLastError(0xdeadbeef);
6942 ret = getaddrinfo("", NULL, NULL, &result);
6943 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6944 ok(result != NULL, "getaddrinfo failed\n");
6945 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6946 freeaddrinfo(result);
6948 result = NULL;
6949 ret = getaddrinfo(NULL, "0", NULL, &result);
6950 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6951 ok(result != NULL, "getaddrinfo failed\n");
6953 result2 = NULL;
6954 ret = getaddrinfo(NULL, "", NULL, &result2);
6955 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6956 ok(result2 != NULL, "getaddrinfo failed\n");
6957 compare_addrinfo(result, result2);
6958 freeaddrinfo(result);
6959 freeaddrinfo(result2);
6961 result = NULL;
6962 WSASetLastError(0xdeadbeef);
6963 ret = getaddrinfo("", "0", NULL, &result);
6964 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6965 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6966 ok(result != NULL, "getaddrinfo failed\n");
6968 result2 = NULL;
6969 ret = getaddrinfo("", "", NULL, &result2);
6970 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6971 ok(result2 != NULL, "getaddrinfo failed\n");
6972 compare_addrinfo(result, result2);
6973 freeaddrinfo(result);
6974 freeaddrinfo(result2);
6976 result = NULL;
6977 ret = getaddrinfo("localhost", NULL, NULL, &result);
6978 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6979 freeaddrinfo(result);
6981 result = NULL;
6982 ret = getaddrinfo("localhost", "", NULL, &result);
6983 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6984 freeaddrinfo(result);
6986 result = NULL;
6987 ret = getaddrinfo("localhost", "0", NULL, &result);
6988 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6989 freeaddrinfo(result);
6991 result = NULL;
6992 ret = getaddrinfo("localhost", "80", NULL, &result);
6993 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6994 freeaddrinfo(result);
6996 result = NULL;
6997 ret = getaddrinfo("localhost", NULL, &hint, &result);
6998 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
6999 freeaddrinfo(result);
7001 result = NULL;
7002 WSASetLastError(0xdeadbeef);
7003 ret = getaddrinfo("localhost", "80", &hint, &result);
7004 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7005 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7006 freeaddrinfo(result);
7008 hint.ai_flags = AI_NUMERICHOST;
7009 result = (void*)0xdeadbeef;
7010 ret = getaddrinfo("localhost", "80", &hint, &result);
7011 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", WSAGetLastError());
7012 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected WSAHOST_NOT_FOUND, got %d\n", WSAGetLastError());
7013 ok(!result, "result = %p\n", result);
7014 hint.ai_flags = 0;
7016 /* try to get information from the computer name, result is the same
7017 * as if requesting with an empty host name. */
7018 ret = getaddrinfo(name, NULL, NULL, &result);
7019 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7020 ok(result != NULL, "GetAddrInfoW failed\n");
7022 ret = getaddrinfo("", NULL, NULL, &result2);
7023 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7024 ok(result != NULL, "GetAddrInfoW failed\n");
7025 compare_addrinfo(result, result2);
7026 freeaddrinfo(result);
7027 freeaddrinfo(result2);
7029 ret = getaddrinfo(name, "", NULL, &result);
7030 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7031 ok(result != NULL, "GetAddrInfoW failed\n");
7033 ret = getaddrinfo("", "", NULL, &result2);
7034 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7035 ok(result != NULL, "GetAddrInfoW failed\n");
7036 compare_addrinfo(result, result2);
7037 freeaddrinfo(result);
7038 freeaddrinfo(result2);
7040 result = (ADDRINFOA *)0xdeadbeef;
7041 WSASetLastError(0xdeadbeef);
7042 ret = getaddrinfo("nxdomain.codeweavers.com", NULL, NULL, &result);
7043 if(ret == 0)
7045 skip("nxdomain returned success. Broken ISP redirects?\n");
7046 return;
7048 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7049 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7050 ok(result == NULL, "got %p\n", result);
7052 /* Test IPv4 address conversion */
7053 result = NULL;
7054 ret = getaddrinfo("192.168.1.253", NULL, NULL, &result);
7055 ok(!ret, "getaddrinfo failed with %d\n", ret);
7056 ok(result->ai_family == AF_INET, "ai_family == %d\n", result->ai_family);
7057 ok(result->ai_addrlen >= sizeof(struct sockaddr_in), "ai_addrlen == %d\n", (int)result->ai_addrlen);
7058 ok(result->ai_addr != NULL, "ai_addr == NULL\n");
7059 sockaddr = (SOCKADDR_IN *)result->ai_addr;
7060 ok(sockaddr->sin_family == AF_INET, "ai_addr->sin_family == %d\n", sockaddr->sin_family);
7061 ok(sockaddr->sin_port == 0, "ai_addr->sin_port == %d\n", sockaddr->sin_port);
7063 ip = inet_ntoa(sockaddr->sin_addr);
7064 ok(strcmp(ip, "192.168.1.253") == 0, "sockaddr->ai_addr == '%s'\n", ip);
7065 freeaddrinfo(result);
7067 /* Test IPv4 address conversion with port */
7068 result = NULL;
7069 hint.ai_flags = AI_NUMERICHOST;
7070 ret = getaddrinfo("192.168.1.253:1024", NULL, &hint, &result);
7071 hint.ai_flags = 0;
7072 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo returned unexpected result: %d\n", ret);
7073 ok(result == NULL, "expected NULL, got %p\n", result);
7075 /* Test IPv6 address conversion */
7076 result = NULL;
7077 SetLastError(0xdeadbeef);
7078 ret = getaddrinfo("2a00:2039:dead:beef:cafe::6666", NULL, NULL, &result);
7080 if (result != NULL)
7082 ok(!ret, "getaddrinfo failed with %d\n", ret);
7083 verify_ipv6_addrinfo(result, "2a00:2039:dead:beef:cafe::6666");
7084 freeaddrinfo(result);
7086 /* Test IPv6 address conversion with brackets */
7087 result = NULL;
7088 ret = getaddrinfo("[beef::cafe]", NULL, NULL, &result);
7089 ok(!ret, "getaddrinfo failed with %d\n", ret);
7090 verify_ipv6_addrinfo(result, "beef::cafe");
7091 freeaddrinfo(result);
7093 /* Test IPv6 address conversion with brackets and hints */
7094 memset(&hint, 0, sizeof(ADDRINFOA));
7095 hint.ai_flags = AI_NUMERICHOST;
7096 hint.ai_family = AF_INET6;
7097 result = NULL;
7098 ret = getaddrinfo("[beef::cafe]", NULL, &hint, &result);
7099 ok(!ret, "getaddrinfo failed with %d\n", ret);
7100 verify_ipv6_addrinfo(result, "beef::cafe");
7101 freeaddrinfo(result);
7103 memset(&hint, 0, sizeof(ADDRINFOA));
7104 hint.ai_flags = AI_NUMERICHOST;
7105 hint.ai_family = AF_INET;
7106 result = NULL;
7107 ret = getaddrinfo("[beef::cafe]", NULL, &hint, &result);
7108 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
7110 /* Test IPv6 address conversion with brackets and port */
7111 result = NULL;
7112 ret = getaddrinfo("[beef::cafe]:10239", NULL, NULL, &result);
7113 ok(!ret, "getaddrinfo failed with %d\n", ret);
7114 verify_ipv6_addrinfo(result, "beef::cafe");
7115 freeaddrinfo(result);
7117 /* Test IPv6 address conversion with unmatched brackets */
7118 result = NULL;
7119 hint.ai_flags = AI_NUMERICHOST;
7120 ret = getaddrinfo("[beef::cafe", NULL, &hint, &result);
7121 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
7123 ret = getaddrinfo("beef::cafe]", NULL, &hint, &result);
7124 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
7126 else
7128 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
7129 win_skip("getaddrinfo does not support IPV6\n");
7132 hint.ai_flags = 0;
7134 for (i = 0;i < (ARRAY_SIZE(hinttests));i++)
7136 hint.ai_family = hinttests[i].family;
7137 hint.ai_socktype = hinttests[i].socktype;
7138 hint.ai_protocol = hinttests[i].protocol;
7140 result = NULL;
7141 SetLastError(0xdeadbeef);
7142 ret = getaddrinfo("localhost", NULL, &hint, &result);
7143 todo_wine_if(hinttests[i].error) ok(ret == hinttests[i].error, "test %d: wrong ret %d\n", i, ret);
7144 if(!ret)
7146 for (p = result; p; p = p->ai_next)
7148 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
7149 if (hinttests[i].family == AF_UNSPEC)
7150 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6,
7151 "test %d: expected AF_INET or AF_INET6, got %d\n",
7152 i, p->ai_family);
7153 else
7154 ok(p->ai_family == hinttests[i].family,
7155 "test %d: expected family %d, got %d\n",
7156 i, hinttests[i].family, p->ai_family);
7158 ok(p->ai_socktype == hinttests[i].socktype,
7159 "test %d: expected type %d, got %d\n",
7160 i, hinttests[i].socktype, p->ai_socktype);
7161 ok(p->ai_protocol == hinttests[i].protocol,
7162 "test %d: expected protocol %d, got %d\n",
7163 i, hinttests[i].protocol, p->ai_protocol);
7165 freeaddrinfo(result);
7167 else
7169 ok(WSAGetLastError() == hinttests[i].error, "test %d: wrong error %d\n", i, WSAGetLastError());
7173 memset(&hint, 0, sizeof(hint));
7174 ret = getaddrinfo(NULL, "nonexistentservice", &hint, &result);
7175 ok(ret == WSATYPE_NOT_FOUND, "got %d\n", ret);
7178 static void test_ConnectEx(void)
7180 SOCKET listener = INVALID_SOCKET;
7181 SOCKET acceptor = INVALID_SOCKET;
7182 SOCKET connector = INVALID_SOCKET;
7183 struct sockaddr_in address, conaddress;
7184 int addrlen;
7185 OVERLAPPED overlapped;
7186 LPFN_CONNECTEX pConnectEx;
7187 GUID connectExGuid = WSAID_CONNECTEX;
7188 DWORD bytesReturned;
7189 char buffer[1024];
7190 BOOL bret;
7191 DWORD dwret;
7192 int iret;
7194 memset(&overlapped, 0, sizeof(overlapped));
7196 listener = socket(AF_INET, SOCK_STREAM, 0);
7197 ok(listener != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7199 connector = socket(AF_INET, SOCK_STREAM, 0);
7200 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7202 memset(&address, 0, sizeof(address));
7203 address.sin_family = AF_INET;
7204 address.sin_addr.s_addr = inet_addr("127.0.0.1");
7205 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
7206 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7208 addrlen = sizeof(address);
7209 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
7210 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
7212 iret = set_blocking(listener, TRUE);
7213 ok(!iret, "failed to set nonblocking, error %u\n", WSAGetLastError());
7215 bytesReturned = 0xdeadbeef;
7216 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
7217 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
7218 ok(!iret, "failed to get ConnectEx, error %u\n", WSAGetLastError());
7220 ok(bytesReturned == sizeof(pConnectEx), "expected sizeof(pConnectEx), got %u\n", bytesReturned);
7222 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
7223 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
7224 "returned %d + errno %d\n", bret, WSAGetLastError());
7226 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
7227 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
7228 "returned %d + errno %d\n", bret, WSAGetLastError());
7230 /* ConnectEx needs a bound socket */
7231 memset(&conaddress, 0, sizeof(conaddress));
7232 conaddress.sin_family = AF_INET;
7233 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
7234 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
7235 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7237 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
7238 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
7239 "returned %d + errno %d\n", bret, WSAGetLastError());
7241 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
7243 iret = listen(listener, 1);
7244 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
7246 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
7247 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
7248 "returned %d + errno %d\n", bret, WSAGetLastError());
7249 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
7250 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
7252 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
7253 ok(bret, "Connecting failed, error %d\n", GetLastError());
7254 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
7256 closesocket(connector);
7257 connector = socket(AF_INET, SOCK_STREAM, 0);
7258 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7259 /* ConnectEx needs a bound socket */
7260 memset(&conaddress, 0, sizeof(conaddress));
7261 conaddress.sin_family = AF_INET;
7262 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
7263 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
7264 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7266 acceptor = accept(listener, NULL, NULL);
7267 ok(acceptor != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
7269 buffer[0] = '1';
7270 buffer[1] = '2';
7271 buffer[2] = '3';
7272 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
7273 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
7274 "returned %d + errno %d\n", bret, WSAGetLastError());
7275 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
7276 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
7278 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
7279 ok(bret, "Connecting failed, error %d\n", GetLastError());
7280 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
7282 acceptor = accept(listener, NULL, NULL);
7283 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
7285 bytesReturned = recv(acceptor, buffer, 3, 0);
7286 buffer[4] = 0;
7287 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
7288 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
7289 "Failed to get the right data, expected '123', got '%s'\n", buffer);
7291 closesocket(connector);
7292 connector = socket(AF_INET, SOCK_STREAM, 0);
7293 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7294 /* ConnectEx needs a bound socket */
7295 memset(&conaddress, 0, sizeof(conaddress));
7296 conaddress.sin_family = AF_INET;
7297 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
7298 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
7299 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7301 closesocket(acceptor);
7302 closesocket(listener);
7304 /* Connect with error */
7306 address.sin_port = htons(1);
7308 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
7309 ok(bret == FALSE && GetLastError() == ERROR_IO_PENDING, "ConnectEx to bad destination failed: "
7310 "returned %d + errno %d\n", bret, GetLastError());
7311 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
7312 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
7314 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
7315 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
7316 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
7318 WSACloseEvent(overlapped.hEvent);
7319 closesocket(connector);
7322 static void test_AcceptEx(void)
7324 SOCKET listener, acceptor, acceptor2, connector, connector2;
7325 struct sockaddr_in bindAddress, peerAddress, *readBindAddress, *readRemoteAddress;
7326 int socklen, optlen;
7327 GUID acceptExGuid = WSAID_ACCEPTEX, getAcceptExGuid = WSAID_GETACCEPTEXSOCKADDRS;
7328 LPFN_ACCEPTEX pAcceptEx = NULL;
7329 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs = NULL;
7330 fd_set fds_accept, fds_send;
7331 static const struct timeval timeout = {1, 0};
7332 DWORD bytesReturned, connect_time;
7333 char buffer[1024], ipbuffer[32];
7334 OVERLAPPED overlapped;
7335 int iret, localSize = sizeof(struct sockaddr_in), remoteSize = localSize;
7336 BOOL bret;
7337 DWORD dwret;
7339 memset(&overlapped, 0, sizeof(overlapped));
7340 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
7342 listener = socket(AF_INET, SOCK_STREAM, 0);
7343 ok(listener != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7345 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7346 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7348 memset(&bindAddress, 0, sizeof(bindAddress));
7349 bindAddress.sin_family = AF_INET;
7350 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
7351 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7352 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7354 socklen = sizeof(bindAddress);
7355 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
7356 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
7358 iret = set_blocking(listener, FALSE);
7359 ok(!iret, "Failed to set nonblocking, error %u\n", WSAGetLastError());
7361 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
7362 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
7363 ok(!iret, "Failed to get AcceptEx, error %u\n", WSAGetLastError());
7365 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &getAcceptExGuid, sizeof(getAcceptExGuid),
7366 &pGetAcceptExSockaddrs, sizeof(pGetAcceptExSockaddrs), &bytesReturned, NULL, NULL);
7367 ok(!iret, "Failed to get GetAcceptExSockaddrs, error %u\n", WSAGetLastError());
7369 overlapped.Internal = 0xdeadbeef;
7370 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7371 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7372 &bytesReturned, &overlapped);
7373 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
7374 "returned %d + errno %d\n", bret, WSAGetLastError());
7375 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7377 overlapped.Internal = 0xdeadbeef;
7378 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7379 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7380 &bytesReturned, &overlapped);
7381 todo_wine
7382 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
7383 "returned %d + errno %d\n", bret, WSAGetLastError());
7384 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7385 if (!bret && WSAGetLastError() == ERROR_IO_PENDING)
7386 CancelIo((HANDLE)listener);
7388 iret = listen(listener, 5);
7389 ok(!iret, "failed to listen, error %u\n", GetLastError());
7391 overlapped.Internal = 0xdeadbeef;
7392 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7393 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7394 &bytesReturned, &overlapped);
7395 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid accepting socket "
7396 "returned %d + errno %d\n", bret, WSAGetLastError());
7397 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7399 overlapped.Internal = 0xdeadbeef;
7400 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7401 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7402 &bytesReturned, &overlapped);
7403 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
7404 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
7405 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7407 overlapped.Internal = 0xdeadbeef;
7408 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
7409 &bytesReturned, &overlapped);
7410 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
7411 "AcceptEx on too small local address size returned %d + errno %d\n",
7412 bret, WSAGetLastError());
7413 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7415 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7416 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
7417 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
7418 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
7419 iret = getsockname(connector, (struct sockaddr *)&peerAddress, &remoteSize);
7420 ok(!iret, "getsockname failed, error %u\n", WSAGetLastError());
7422 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7423 ok(!dwret, "wait failed\n");
7424 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7425 ok(bret, "got error %u\n", GetLastError());
7426 ok(!(NTSTATUS)overlapped.Internal, "got %#lx\n", overlapped.Internal);
7427 ok(!bytesReturned, "got size %u\n", bytesReturned);
7429 readBindAddress = readRemoteAddress = (struct sockaddr_in *)0xdeadbeef;
7430 localSize = remoteSize = 0xdeadbeef;
7431 pGetAcceptExSockaddrs(buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
7432 (struct sockaddr **)&readBindAddress, &localSize, (struct sockaddr **)&readRemoteAddress, &remoteSize);
7433 todo_wine ok(readBindAddress == (struct sockaddr_in *)0xdeadbeef, "got local addr %p\n", readBindAddress);
7434 ok(!memcmp(readRemoteAddress, &peerAddress, sizeof(peerAddress)), "remote addr didn't match\n");
7435 todo_wine ok(localSize == 0xdeadbeef, "got local size %u\n", localSize);
7436 ok(remoteSize == sizeof(struct sockaddr_in), "got remote size %u\n", remoteSize);
7438 closesocket(connector);
7439 closesocket(acceptor);
7441 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7442 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
7444 overlapped.Internal = 0xdeadbeef;
7445 bret = pAcceptEx(listener, acceptor, buffer, 0, 3,
7446 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
7447 ok(!bret && WSAGetLastError() == ERROR_IO_PENDING, "got %d, error %u\n", bret, WSAGetLastError());
7448 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "got %#lx\n", overlapped.Internal);
7450 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7451 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
7452 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
7453 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
7455 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7456 ok(!dwret, "wait failed\n");
7457 bytesReturned = 0xdeadbeef;
7458 SetLastError(0xdeadbeef);
7459 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7460 ok(!bret, "expected failure\n");
7461 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got error %u\n", GetLastError());
7462 ok((NTSTATUS)overlapped.Internal == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", overlapped.Internal);
7463 ok(!bytesReturned, "got size %u\n", bytesReturned);
7465 closesocket(acceptor);
7467 /* The above connection request is not accepted. */
7468 acceptor = accept(listener, NULL, NULL);
7469 todo_wine ok(acceptor != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
7470 closesocket(acceptor);
7472 closesocket(connector);
7474 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7475 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
7477 overlapped.Internal = 0xdeadbeef;
7478 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 4,
7479 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
7480 ok(!bret && WSAGetLastError() == ERROR_IO_PENDING, "got %d, error %u\n", bret, WSAGetLastError());
7481 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "got %#lx\n", overlapped.Internal);
7483 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7484 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
7485 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
7486 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
7488 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7489 ok(!dwret, "wait failed\n");
7490 bytesReturned = 0xdeadbeef;
7491 SetLastError(0xdeadbeef);
7492 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7493 todo_wine ok(!bret, "expected failure\n");
7494 todo_wine ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got error %u\n", GetLastError());
7495 todo_wine ok((NTSTATUS)overlapped.Internal == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", overlapped.Internal);
7496 ok(!bytesReturned, "got size %u\n", bytesReturned);
7498 closesocket(acceptor);
7500 /* The above connection request is not accepted. */
7501 acceptor = accept(listener, NULL, NULL);
7502 todo_wine ok(acceptor != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
7503 closesocket(acceptor);
7505 closesocket(connector);
7507 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7508 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
7510 overlapped.Internal = 0xdeadbeef;
7511 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 15,
7512 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
7513 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx on too small local address "
7514 "size returned %d + errno %d\n",
7515 bret, WSAGetLastError());
7516 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7517 bret = CancelIo((HANDLE) listener);
7518 ok(bret, "Failed to cancel pending accept socket\n");
7520 overlapped.Internal = 0xdeadbeef;
7521 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
7522 &bytesReturned, &overlapped);
7523 ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
7524 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
7525 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7527 overlapped.Internal = 0xdeadbeef;
7528 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16,
7529 sizeof(struct sockaddr_in) + 15, &bytesReturned, &overlapped);
7530 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
7531 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
7532 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7533 bret = CancelIo((HANDLE) listener);
7534 ok(bret, "Failed to cancel pending accept socket\n");
7536 bret = pAcceptEx(listener, acceptor, buffer, 0,
7537 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7538 &bytesReturned, NULL);
7539 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
7540 "returned %d + errno %d\n", bret, WSAGetLastError());
7542 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, 0, &bytesReturned, NULL);
7543 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
7544 "returned %d + errno %d\n", bret, WSAGetLastError());
7546 overlapped.Internal = 0xdeadbeef;
7547 bret = pAcceptEx(listener, acceptor, buffer, 0,
7548 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7549 &bytesReturned, &overlapped);
7550 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7551 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7553 overlapped.Internal = 0xdeadbeef;
7554 bret = pAcceptEx(listener, acceptor, buffer, 0,
7555 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7556 &bytesReturned, &overlapped);
7557 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL,
7558 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
7559 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7560 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
7561 /* We need to cancel this call, otherwise things fail */
7562 bret = CancelIo((HANDLE) listener);
7563 ok(bret, "Failed to cancel failed test. Bailing...\n");
7564 if (!bret) return;
7565 WaitForSingleObject(overlapped.hEvent, 0);
7567 overlapped.Internal = 0xdeadbeef;
7568 bret = pAcceptEx(listener, acceptor, buffer, 0,
7569 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7570 &bytesReturned, &overlapped);
7571 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7572 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7575 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7576 todo_wine ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
7577 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
7578 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
7579 /* We need to cancel this call, otherwise things fail */
7580 closesocket(acceptor);
7581 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7582 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7584 bret = CancelIo((HANDLE) listener);
7585 ok(bret, "Failed to cancel failed test. Bailing...\n");
7586 if (!bret) return;
7588 overlapped.Internal = 0xdeadbeef;
7589 bret = pAcceptEx(listener, acceptor, buffer, 0,
7590 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7591 &bytesReturned, &overlapped);
7592 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7593 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7596 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7597 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
7598 overlapped.Internal = 0xdeadbeef;
7599 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7600 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7602 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
7603 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7604 ok(overlapped.Internal == STATUS_SUCCESS, "got %08x\n", (ULONG)overlapped.Internal);
7606 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7607 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
7608 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
7610 closesocket(connector);
7611 connector = INVALID_SOCKET;
7612 closesocket(acceptor);
7614 /* Test short reads */
7616 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7617 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7618 connector = socket(AF_INET, SOCK_STREAM, 0);
7619 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7620 overlapped.Internal = 0xdeadbeef;
7621 bret = pAcceptEx(listener, acceptor, buffer, 2,
7622 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7623 &bytesReturned, &overlapped);
7624 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7625 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7627 connect_time = 0xdeadbeef;
7628 optlen = sizeof(connect_time);
7629 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
7630 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
7631 ok(connect_time == ~0u, "unexpected connect time %u\n", connect_time);
7633 /* AcceptEx() still won't complete until we send data */
7634 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7635 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7637 connect_time = 0xdeadbeef;
7638 optlen = sizeof(connect_time);
7639 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
7640 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
7641 ok(connect_time < 0xdeadbeef, "unexpected connect time %u\n", connect_time);
7643 dwret = WaitForSingleObject(overlapped.hEvent, 0);
7644 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
7645 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
7647 iret = getsockname( connector, (struct sockaddr *)&peerAddress, &remoteSize);
7648 ok( !iret, "getsockname failed.\n");
7650 /* AcceptEx() could complete any time now */
7651 iret = send(connector, buffer, 1, 0);
7652 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
7654 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7655 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7656 ok(overlapped.Internal == STATUS_SUCCESS, "got %08x\n", (ULONG)overlapped.Internal);
7658 /* Check if the buffer from AcceptEx is decoded correctly */
7659 pGetAcceptExSockaddrs(buffer, 2, sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7660 (struct sockaddr **)&readBindAddress, &localSize,
7661 (struct sockaddr **)&readRemoteAddress, &remoteSize);
7662 strcpy( ipbuffer, inet_ntoa(readBindAddress->sin_addr));
7663 ok( readBindAddress->sin_addr.s_addr == bindAddress.sin_addr.s_addr,
7664 "Local socket address is different %s != %s\n",
7665 ipbuffer, inet_ntoa(bindAddress.sin_addr));
7666 ok( readBindAddress->sin_port == bindAddress.sin_port,
7667 "Local socket port is different: %d != %d\n",
7668 readBindAddress->sin_port, bindAddress.sin_port);
7669 strcpy( ipbuffer, inet_ntoa(readRemoteAddress->sin_addr));
7670 ok( readRemoteAddress->sin_addr.s_addr == peerAddress.sin_addr.s_addr,
7671 "Remote socket address is different %s != %s\n",
7672 ipbuffer, inet_ntoa(peerAddress.sin_addr));
7673 ok( readRemoteAddress->sin_port == peerAddress.sin_port,
7674 "Remote socket port is different: %d != %d\n",
7675 readRemoteAddress->sin_port, peerAddress.sin_port);
7677 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7678 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
7679 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
7681 closesocket(connector);
7682 connector = INVALID_SOCKET;
7683 closesocket(acceptor);
7685 /* Test CF_DEFER & AcceptEx interaction */
7687 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7688 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7689 connector = socket(AF_INET, SOCK_STREAM, 0);
7690 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7691 connector2 = socket(AF_INET, SOCK_STREAM, 0);
7692 ok(connector2 != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7694 iret = set_blocking(connector, FALSE);
7695 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
7696 iret = set_blocking(connector2, FALSE);
7697 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
7699 /* Connect socket #1 */
7700 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7701 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7703 buffer[0] = '0';
7705 FD_ZERO(&fds_accept);
7706 FD_SET(listener, &fds_accept);
7707 iret = select(0, &fds_accept, NULL, NULL, &timeout);
7708 ok(iret == 1, "wait timed out\n");
7710 acceptor2 = WSAAccept(listener, NULL, NULL, AlwaysDeferConditionFunc, 0);
7711 ok(acceptor2 == INVALID_SOCKET, "expected failure\n");
7712 ok(WSAGetLastError() == WSATRY_AGAIN, "got error %u\n", WSAGetLastError());
7713 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16,
7714 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
7715 ok(!bret, "expected failure\n");
7716 ok(WSAGetLastError() == ERROR_IO_PENDING, "got error %u\n", WSAGetLastError());
7718 FD_ZERO(&fds_send);
7719 FD_SET(connector, &fds_send);
7720 iret = select(0, NULL, &fds_send, NULL, &timeout);
7721 ok(iret == 1, "wait timed out\n");
7723 iret = send(connector, "1", 1, 0);
7724 ok(iret == 1, "got ret %d, error %u\n", iret, WSAGetLastError());
7726 iret = connect(connector2, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
7727 ok(iret == SOCKET_ERROR, "expected failure\n");
7728 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
7730 iret = select(0, &fds_accept, NULL, NULL, &timeout);
7731 ok(iret == 1, "wait timed out\n");
7733 acceptor2 = accept(listener, NULL, NULL);
7734 ok(acceptor2 != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
7735 closesocket(acceptor2);
7737 FD_ZERO(&fds_send);
7738 FD_SET(connector2, &fds_send);
7739 iret = select(0, NULL, &fds_send, NULL, &timeout);
7740 ok(iret == 1, "wait timed out\n");
7742 iret = send(connector2, "2", 1, 0);
7743 ok(iret == 1, "got ret %d, error %u\n", iret, WSAGetLastError());
7745 dwret = WaitForSingleObject(overlapped.hEvent, 0);
7746 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7748 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7749 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
7750 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
7752 set_blocking(acceptor, TRUE);
7753 iret = recv( acceptor, buffer, 2, 0);
7754 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
7755 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
7757 closesocket(connector);
7758 closesocket(connector2);
7759 closesocket(acceptor);
7761 /* clean up in case of failures */
7762 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
7763 closesocket(acceptor);
7765 /* Disconnect during receive? */
7767 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7768 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7769 connector = socket(AF_INET, SOCK_STREAM, 0);
7770 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7771 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7772 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7773 &bytesReturned, &overlapped);
7774 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7776 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7777 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7779 closesocket(connector);
7780 connector = INVALID_SOCKET;
7782 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7783 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7785 bytesReturned = 123456;
7786 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7787 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
7788 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
7790 closesocket(acceptor);
7792 /* Test closing with pending requests */
7794 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7795 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7796 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7797 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7798 &bytesReturned, &overlapped);
7799 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7801 closesocket(acceptor);
7803 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7804 ok(dwret == WAIT_OBJECT_0,
7805 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7806 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7807 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
7809 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7810 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7811 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7812 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7813 &bytesReturned, &overlapped);
7814 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7816 CancelIo((HANDLE) acceptor);
7818 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7819 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
7821 closesocket(acceptor);
7823 acceptor = socket(AF_INET, SOCK_STREAM, 0);
7824 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7825 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
7826 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7827 &bytesReturned, &overlapped);
7828 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
7830 closesocket(listener);
7832 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
7833 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
7835 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
7836 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
7838 WSACloseEvent(overlapped.hEvent);
7839 closesocket(acceptor);
7840 closesocket(connector2);
7843 static void test_DisconnectEx(void)
7845 SOCKET listener, acceptor, connector;
7846 LPFN_DISCONNECTEX pDisconnectEx;
7847 GUID disconnectExGuid = WSAID_DISCONNECTEX;
7848 struct sockaddr_in address;
7849 DWORD num_bytes, flags;
7850 OVERLAPPED overlapped;
7851 int addrlen, iret;
7852 BOOL bret;
7854 connector = socket(AF_INET, SOCK_STREAM, 0);
7855 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
7857 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectExGuid, sizeof(disconnectExGuid),
7858 &pDisconnectEx, sizeof(pDisconnectEx), &num_bytes, NULL, NULL);
7859 if (iret)
7861 win_skip("WSAIoctl failed to get DisconnectEx, error %d\n", WSAGetLastError());
7862 closesocket(connector);
7863 return;
7866 listener = socket(AF_INET, SOCK_STREAM, 0);
7867 ok(listener != INVALID_SOCKET, "failed to create listener socket, error %d\n", WSAGetLastError());
7869 memset(&address, 0, sizeof(address));
7870 address.sin_family = AF_INET;
7871 address.sin_addr.s_addr = inet_addr("127.0.0.1");
7872 iret = bind(listener, (struct sockaddr *)&address, sizeof(address));
7873 ok(iret == 0, "failed to bind, error %d\n", WSAGetLastError());
7875 addrlen = sizeof(address);
7876 iret = getsockname(listener, (struct sockaddr *)&address, &addrlen);
7877 ok(iret == 0, "failed to lookup bind address, error %d\n", WSAGetLastError());
7879 iret = listen(listener, 1);
7880 ok(iret == 0, "failed to listen, error %d\n", WSAGetLastError());
7882 set_blocking(listener, TRUE);
7884 memset(&overlapped, 0, sizeof(overlapped));
7885 bret = pDisconnectEx(INVALID_SOCKET, &overlapped, 0, 0);
7886 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
7887 ok(WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %d\n", WSAGetLastError());
7889 memset(&overlapped, 0, sizeof(overlapped));
7890 bret = pDisconnectEx(connector, &overlapped, 0, 0);
7891 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
7892 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "expected WSAENOTCONN, got %d\n", WSAGetLastError());
7894 iret = connect(connector, (struct sockaddr *)&address, addrlen);
7895 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
7897 acceptor = accept(listener, NULL, NULL);
7898 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
7900 memset(&overlapped, 0, sizeof(overlapped));
7901 overlapped.hEvent = WSACreateEvent();
7902 ok(overlapped.hEvent != WSA_INVALID_EVENT, "WSACreateEvent failed, error %d\n", WSAGetLastError());
7903 bret = pDisconnectEx(connector, &overlapped, 0, 0);
7904 if (bret)
7905 ok(overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", overlapped.Internal);
7906 else if (WSAGetLastError() == ERROR_IO_PENDING)
7907 bret = WSAGetOverlappedResult(connector, &overlapped, &num_bytes, TRUE, &flags);
7908 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
7909 WSACloseEvent(overlapped.hEvent);
7911 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
7912 ok(iret != 0, "connect unexpectedly succeeded\n");
7913 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
7915 closesocket(acceptor);
7916 closesocket(connector);
7918 connector = socket(AF_INET, SOCK_STREAM, 0);
7919 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
7921 iret = connect(connector, (struct sockaddr *)&address, addrlen);
7922 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
7924 acceptor = accept(listener, NULL, NULL);
7925 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
7927 bret = pDisconnectEx(connector, NULL, 0, 0);
7928 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
7930 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
7931 ok(iret != 0, "connect unexpectedly succeeded\n");
7932 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
7934 closesocket(acceptor);
7935 closesocket(connector);
7936 closesocket(listener);
7939 #define compare_file(h,s,o) compare_file2(h,s,o,__FILE__,__LINE__)
7941 static void compare_file2(HANDLE handle, SOCKET sock, int offset, const char *file, int line)
7943 char buf1[256], buf2[256];
7944 BOOL success;
7945 int i = 0;
7947 SetFilePointer(handle, offset, NULL, FILE_BEGIN);
7948 while (1)
7950 DWORD n1 = 0, n2 = 0;
7952 success = ReadFile(handle, buf1, sizeof(buf1), &n1, NULL);
7953 ok_(file,line)(success, "Failed to read from file.\n");
7954 if (success && n1 == 0)
7955 break;
7956 else if(!success)
7957 return;
7958 n2 = recv(sock, buf2, n1, 0);
7959 ok_(file,line)(n1 == n2, "Block %d size mismatch (%d != %d)\n", i, n1, n2);
7960 ok_(file,line)(memcmp(buf1, buf2, n2) == 0, "Block %d failed\n", i);
7961 i++;
7965 static void test_TransmitFile(void)
7967 DWORD num_bytes, err, file_size, total_sent;
7968 GUID transmitFileGuid = WSAID_TRANSMITFILE;
7969 LPFN_TRANSMITFILE pTransmitFile = NULL;
7970 HANDLE file = INVALID_HANDLE_VALUE;
7971 char header_msg[] = "hello world";
7972 char footer_msg[] = "goodbye!!!";
7973 char system_ini_path[MAX_PATH];
7974 struct sockaddr_in bindAddress;
7975 TRANSMIT_FILE_BUFFERS buffers;
7976 SOCKET client, server, dest;
7977 WSAOVERLAPPED ov;
7978 char buf[256];
7979 int iret, len;
7980 BOOL bret;
7982 memset( &ov, 0, sizeof(ov) );
7984 /* Setup sockets for testing TransmitFile */
7985 client = socket(AF_INET, SOCK_STREAM, 0);
7986 ok(client != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7987 server = socket(AF_INET, SOCK_STREAM, 0);
7988 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
7989 iret = WSAIoctl(client, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmitFileGuid, sizeof(transmitFileGuid),
7990 &pTransmitFile, sizeof(pTransmitFile), &num_bytes, NULL, NULL);
7991 ok(!iret, "failed to get TransmitFile, error %u\n", GetLastError());
7992 GetSystemWindowsDirectoryA(system_ini_path, MAX_PATH );
7993 strcat(system_ini_path, "\\system.ini");
7994 file = CreateFileA(system_ini_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0x0, NULL);
7995 ok(file != INVALID_HANDLE_VALUE, "failed to open file, error %u\n", GetLastError());
7996 file_size = GetFileSize(file, NULL);
7998 /* Test TransmitFile with an invalid socket */
7999 bret = pTransmitFile(INVALID_SOCKET, file, 0, 0, NULL, NULL, 0);
8000 err = WSAGetLastError();
8001 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8002 ok(err == WSAENOTSOCK, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTSOCK);
8004 /* Test a bogus TransmitFile without a connected socket */
8005 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, TF_REUSE_SOCKET);
8006 err = WSAGetLastError();
8007 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8008 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
8010 /* Setup a properly connected socket for transfers */
8011 memset(&bindAddress, 0, sizeof(bindAddress));
8012 bindAddress.sin_family = AF_INET;
8013 bindAddress.sin_port = htons(SERVERPORT+1);
8014 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
8015 iret = bind(server, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8016 ok(!iret, "failed to bind socket, error %u\n", GetLastError());
8017 iret = listen(server, 1);
8018 ok(!iret, "failed to listen, error %u\n", GetLastError());
8019 iret = connect(client, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8020 ok(!iret, "failed to connect, error %u\n", GetLastError());
8021 len = sizeof(bindAddress);
8022 dest = accept(server, (struct sockaddr*)&bindAddress, &len);
8023 ok(dest != INVALID_SOCKET, "failed to accept, error %u\n", GetLastError());
8024 iret = set_blocking(dest, FALSE);
8025 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
8027 /* Test TransmitFile with no possible buffer */
8028 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
8029 ok(bret, "TransmitFile failed unexpectedly.\n");
8030 iret = recv(dest, buf, sizeof(buf), 0);
8031 ok(iret == -1, "Returned an unexpected buffer from TransmitFile (%d != -1).\n", iret);
8033 /* Test TransmitFile with only buffer data */
8034 buffers.Head = &header_msg[0];
8035 buffers.HeadLength = sizeof(header_msg);
8036 buffers.Tail = &footer_msg[0];
8037 buffers.TailLength = sizeof(footer_msg);
8038 bret = pTransmitFile(client, NULL, 0, 0, NULL, &buffers, 0);
8039 ok(bret, "TransmitFile failed unexpectedly.\n");
8040 iret = recv(dest, buf, sizeof(buf), 0);
8041 ok(iret == sizeof(header_msg)+sizeof(footer_msg),
8042 "Returned an unexpected buffer from TransmitFile: %d\n", iret );
8043 ok(memcmp(&buf[0], &header_msg[0], sizeof(header_msg)) == 0,
8044 "TransmitFile header buffer did not match!\n");
8045 ok(memcmp(&buf[sizeof(header_msg)], &footer_msg[0], sizeof(footer_msg)) == 0,
8046 "TransmitFile footer buffer did not match!\n");
8048 /* Test TransmitFile with only file data */
8049 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, 0);
8050 ok(bret, "TransmitFile failed unexpectedly.\n");
8051 compare_file(file, dest, 0);
8053 /* Test TransmitFile with both file and buffer data */
8054 buffers.Head = &header_msg[0];
8055 buffers.HeadLength = sizeof(header_msg);
8056 buffers.Tail = &footer_msg[0];
8057 buffers.TailLength = sizeof(footer_msg);
8058 SetFilePointer(file, 0, NULL, FILE_BEGIN);
8059 bret = pTransmitFile(client, file, 0, 0, NULL, &buffers, 0);
8060 ok(bret, "TransmitFile failed unexpectedly.\n");
8061 iret = recv(dest, buf, sizeof(header_msg), 0);
8062 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
8063 "TransmitFile header buffer did not match!\n");
8064 compare_file(file, dest, 0);
8065 iret = recv(dest, buf, sizeof(footer_msg), 0);
8066 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
8067 "TransmitFile footer buffer did not match!\n");
8069 /* Test overlapped TransmitFile */
8070 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
8071 SetFilePointer(file, 0, NULL, FILE_BEGIN);
8072 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
8073 err = WSAGetLastError();
8074 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8075 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n",
8076 err, ERROR_IO_PENDING);
8077 iret = WaitForSingleObject(ov.hEvent, 2000);
8078 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
8079 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
8080 ok(total_sent == file_size,
8081 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
8082 total_sent, file_size);
8083 compare_file(file, dest, 0);
8085 /* Test overlapped TransmitFile w/ start offset */
8086 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
8087 SetFilePointer(file, 0, NULL, FILE_BEGIN);
8088 ov.Offset = 10;
8089 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
8090 err = WSAGetLastError();
8091 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8092 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
8093 iret = WaitForSingleObject(ov.hEvent, 2000);
8094 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
8095 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
8096 ok(total_sent == (file_size - ov.Offset),
8097 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
8098 total_sent, file_size - ov.Offset);
8099 compare_file(file, dest, ov.Offset);
8101 /* Test overlapped TransmitFile w/ file and buffer data */
8102 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
8103 buffers.Head = &header_msg[0];
8104 buffers.HeadLength = sizeof(header_msg);
8105 buffers.Tail = &footer_msg[0];
8106 buffers.TailLength = sizeof(footer_msg);
8107 SetFilePointer(file, 0, NULL, FILE_BEGIN);
8108 ov.Offset = 0;
8109 bret = pTransmitFile(client, file, 0, 0, &ov, &buffers, 0);
8110 err = WSAGetLastError();
8111 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8112 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
8113 iret = WaitForSingleObject(ov.hEvent, 2000);
8114 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
8115 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
8116 ok(total_sent == (file_size + buffers.HeadLength + buffers.TailLength),
8117 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
8118 total_sent, file_size + buffers.HeadLength + buffers.TailLength);
8119 iret = recv(dest, buf, sizeof(header_msg), 0);
8120 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
8121 "TransmitFile header buffer did not match!\n");
8122 compare_file(file, dest, 0);
8123 iret = recv(dest, buf, sizeof(footer_msg), 0);
8124 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
8125 "TransmitFile footer buffer did not match!\n");
8127 /* Test TransmitFile with a UDP datagram socket */
8128 closesocket(client);
8129 client = socket(AF_INET, SOCK_DGRAM, 0);
8130 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
8131 err = WSAGetLastError();
8132 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
8133 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
8135 CloseHandle(file);
8136 CloseHandle(ov.hEvent);
8137 closesocket(client);
8138 closesocket(server);
8141 static void test_getpeername(void)
8143 SOCKET sock;
8144 struct sockaddr_in sa, sa_out;
8145 SOCKADDR_STORAGE ss;
8146 int sa_len;
8147 const char buf[] = "hello world";
8148 int ret;
8150 /* Test the parameter validation order. */
8151 ret = getpeername(INVALID_SOCKET, NULL, NULL);
8152 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
8153 ok(WSAGetLastError() == WSAENOTSOCK,
8154 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
8156 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
8157 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
8159 ret = getpeername(sock, NULL, NULL);
8160 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
8161 ok(WSAGetLastError() == WSAENOTCONN,
8162 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
8164 memset(&sa, 0, sizeof(sa));
8165 sa.sin_family = AF_INET;
8166 sa.sin_port = htons(139);
8167 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
8169 /* sendto does not change a socket's connection state. */
8170 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
8171 ok(ret != SOCKET_ERROR,
8172 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
8174 ret = getpeername(sock, NULL, NULL);
8175 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
8176 ok(WSAGetLastError() == WSAENOTCONN,
8177 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
8179 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
8180 ok(ret == 0,
8181 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
8183 ret = getpeername(sock, NULL, NULL);
8184 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
8185 ok(WSAGetLastError() == WSAEFAULT,
8186 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
8188 /* Test crashes on Wine. */
8189 if (0)
8191 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
8192 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
8193 ok(WSAGetLastError() == WSAEFAULT,
8194 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
8197 ret = getpeername(sock, (struct sockaddr*)&sa_out, NULL);
8198 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
8199 ok(WSAGetLastError() == WSAEFAULT,
8200 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
8202 sa_len = 0;
8203 ret = getpeername(sock, NULL, &sa_len);
8204 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
8205 ok(WSAGetLastError() == WSAEFAULT,
8206 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
8207 ok(!sa_len, "got %d\n", sa_len);
8209 sa_len = 0;
8210 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
8211 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
8212 ok(WSAGetLastError() == WSAEFAULT,
8213 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
8214 ok(!sa_len, "got %d\n", sa_len);
8216 sa_len = sizeof(ss);
8217 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
8218 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
8219 ok(!memcmp(&sa, &ss, sizeof(sa)),
8220 "Expected the returned structure to be identical to the connect structure\n");
8221 ok(sa_len == sizeof(sa), "got %d\n", sa_len);
8223 closesocket(sock);
8226 static void test_sioRoutingInterfaceQuery(void)
8228 int ret;
8229 SOCKET sock;
8230 SOCKADDR_IN sin = { 0 }, sout = { 0 };
8231 DWORD bytesReturned;
8233 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
8234 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
8235 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
8236 NULL, NULL);
8237 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
8238 "expected WSAEFAULT, got %d\n", WSAGetLastError());
8239 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
8240 NULL, 0, NULL, NULL, NULL);
8241 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
8242 "expected WSAEFAULT, got %d\n", WSAGetLastError());
8243 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
8244 NULL, 0, &bytesReturned, NULL, NULL);
8245 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEAFNOSUPPORT,
8246 "expected WSAEAFNOSUPPORT, got %d\n", WSAGetLastError());
8247 sin.sin_family = AF_INET;
8248 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
8249 NULL, 0, &bytesReturned, NULL, NULL);
8250 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
8251 "expected WSAEINVAL, got %d\n", WSAGetLastError());
8252 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
8253 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
8254 NULL, 0, &bytesReturned, NULL, NULL);
8255 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
8256 "expected WSAEFAULT, got %d\n", WSAGetLastError());
8257 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
8258 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
8259 ok(!ret, "WSAIoctl failed: %d\n", WSAGetLastError());
8260 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n", sout.sin_family);
8261 /* We expect the source address to be INADDR_LOOPBACK as well, but
8262 * there's no guarantee that a route to the loopback address exists,
8263 * so rather than introduce spurious test failures we do not test the
8264 * source address.
8266 closesocket(sock);
8269 static void test_sioAddressListChange(void)
8271 struct sockaddr_in bindAddress;
8272 struct in_addr net_address;
8273 WSAOVERLAPPED overlapped, *olp;
8274 struct hostent *h;
8275 DWORD num_bytes, error, tick;
8276 SOCKET sock, sock2, sock3;
8277 WSAEVENT event2, event3;
8278 HANDLE io_port;
8279 ULONG_PTR key;
8280 int acount;
8281 BOOL bret;
8282 int ret;
8284 /* Use gethostbyname to find the list of local network interfaces */
8285 h = gethostbyname("");
8286 ok(!!h, "failed to get interface list, error %u\n", WSAGetLastError());
8287 for (acount = 0; h->h_addr_list[acount]; acount++);
8288 if (acount == 0)
8290 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, test requires a network card.\n");
8291 return;
8294 net_address.s_addr = *(ULONG *) h->h_addr_list[0];
8296 sock = socket(AF_INET, 0, IPPROTO_TCP);
8297 ok(sock != INVALID_SOCKET, "socket() failed\n");
8299 memset(&bindAddress, 0, sizeof(bindAddress));
8300 bindAddress.sin_family = AF_INET;
8301 bindAddress.sin_addr.s_addr = net_address.s_addr;
8302 SetLastError(0xdeadbeef);
8303 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8304 ok (!ret, "bind() failed with error %d\n", GetLastError());
8305 set_blocking(sock, FALSE);
8307 memset(&overlapped, 0, sizeof(overlapped));
8308 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8309 SetLastError(0xdeadbeef);
8310 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8311 error = GetLastError();
8312 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
8313 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
8315 CloseHandle(overlapped.hEvent);
8316 closesocket(sock);
8318 sock = socket(AF_INET, 0, IPPROTO_TCP);
8319 ok(sock != INVALID_SOCKET, "socket() failed\n");
8321 SetLastError(0xdeadbeef);
8322 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8323 ok (!ret, "bind() failed with error %d\n", GetLastError());
8324 set_blocking(sock, TRUE);
8326 memset(&overlapped, 0, sizeof(overlapped));
8327 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8328 SetLastError(0xdeadbeef);
8329 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8330 error = GetLastError();
8331 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
8332 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
8334 CloseHandle(overlapped.hEvent);
8335 closesocket(sock);
8337 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8338 ok(sock != INVALID_SOCKET, "socket() failed\n");
8340 SetLastError(0xdeadbeef);
8341 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8342 ok (!ret, "bind() failed with error %d\n", GetLastError());
8343 set_blocking(sock, FALSE);
8345 memset(&overlapped, 0, sizeof(overlapped));
8346 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8347 SetLastError(0xdeadbeef);
8348 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8349 error = GetLastError();
8350 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
8351 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
8353 CloseHandle(overlapped.hEvent);
8354 closesocket(sock);
8356 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8357 ok(sock != INVALID_SOCKET, "socket() failed\n");
8359 SetLastError(0xdeadbeef);
8360 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8361 ok (!ret, "bind() failed with error %d\n", GetLastError());
8362 set_blocking(sock, TRUE);
8364 memset(&overlapped, 0, sizeof(overlapped));
8365 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8366 SetLastError(0xdeadbeef);
8367 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8368 error = GetLastError();
8369 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
8370 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
8372 CloseHandle(overlapped.hEvent);
8373 closesocket(sock);
8375 /* When the socket is overlapped non-blocking and the list change is requested without
8376 * an overlapped structure the error will be different. */
8377 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8378 ok(sock != INVALID_SOCKET, "socket() failed\n");
8380 SetLastError(0xdeadbeef);
8381 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8382 ok (!ret, "bind() failed with error %d\n", GetLastError());
8383 set_blocking(sock, FALSE);
8385 SetLastError(0xdeadbeef);
8386 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
8387 error = GetLastError();
8388 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
8389 ok (error == WSAEWOULDBLOCK, "expected 10035, got %d\n", error);
8391 io_port = CreateIoCompletionPort( (HANDLE)sock, NULL, 0, 0 );
8392 ok (io_port != NULL, "failed to create completion port %u\n", GetLastError());
8394 set_blocking(sock, FALSE);
8395 memset(&overlapped, 0, sizeof(overlapped));
8396 SetLastError(0xdeadbeef);
8397 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8398 error = GetLastError();
8399 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %u\n", error);
8400 ok (error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error);
8402 olp = (WSAOVERLAPPED *)0xdeadbeef;
8403 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
8404 ok(!bret, "failed to get completion status %u\n", bret);
8405 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8406 ok(!olp, "Overlapped structure is at %p\n", olp);
8408 closesocket(sock);
8410 olp = (WSAOVERLAPPED *)0xdeadbeef;
8411 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
8412 ok(!bret, "failed to get completion status %u\n", bret);
8413 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %u\n", GetLastError());
8414 ok(olp == &overlapped, "Overlapped structure is at %p\n", olp);
8416 CloseHandle(io_port);
8418 /* Misuse of the API by using a blocking socket and not using an overlapped structure,
8419 * this leads to a hang forever. */
8420 if (0)
8422 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8424 SetLastError(0xdeadbeef);
8425 bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8427 set_blocking(sock, TRUE);
8428 WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
8429 /* hang */
8431 closesocket(sock);
8434 if (!winetest_interactive)
8436 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, interactive tests must be enabled\n");
8437 return;
8440 /* Bind an overlapped socket to the first found network interface */
8441 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8442 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
8443 sock2 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8444 ok(sock2 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
8445 sock3 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8446 ok(sock3 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
8448 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8449 ok(!ret, "bind failed unexpectedly\n");
8450 ret = bind(sock2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8451 ok(!ret, "bind failed unexpectedly\n");
8452 ret = bind(sock3, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8453 ok(!ret, "bind failed unexpectedly\n");
8455 set_blocking(sock2, FALSE);
8456 set_blocking(sock3, FALSE);
8458 /* Wait for address changes, request that the user connects/disconnects an interface */
8459 memset(&overlapped, 0, sizeof(overlapped));
8460 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8461 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
8462 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
8463 ok(WSAGetLastError() == WSA_IO_PENDING, "Expected pending last error, got %d\n", WSAGetLastError());
8465 ret = WSAIoctl(sock2, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
8466 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
8467 ok(WSAGetLastError() == WSAEWOULDBLOCK, "Expected would block last error, got %d\n", WSAGetLastError());
8469 event2 = WSACreateEvent();
8470 event3 = WSACreateEvent();
8471 ret = WSAEventSelect (sock2, event2, FD_ADDRESS_LIST_CHANGE);
8472 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
8473 /* sock3 did not request SIO_ADDRESS_LIST_CHANGE but it is trying to wait anyway */
8474 ret = WSAEventSelect (sock3, event3, FD_ADDRESS_LIST_CHANGE);
8475 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
8477 trace("Testing socket-based ipv4 address list change notification. Please connect/disconnect or"
8478 " change the ipv4 address of any of the local network interfaces (15 second timeout).\n");
8479 tick = GetTickCount();
8480 ret = WaitForSingleObject(overlapped.hEvent, 15000);
8481 ok(ret == WAIT_OBJECT_0, "failed to get overlapped event %u\n", ret);
8483 ret = WaitForSingleObject(event2, 500);
8484 todo_wine
8485 ok(ret == WAIT_OBJECT_0, "failed to get change event %u\n", ret);
8487 ret = WaitForSingleObject(event3, 500);
8488 ok(ret == WAIT_TIMEOUT, "unexpected change event\n");
8490 trace("Spent %d ms waiting.\n", GetTickCount() - tick);
8492 WSACloseEvent(event2);
8493 WSACloseEvent(event3);
8495 closesocket(sock);
8496 closesocket(sock2);
8497 closesocket(sock3);
8500 static void test_synchronous_WSAIoctl(void)
8502 HANDLE previous_port, io_port;
8503 WSAOVERLAPPED overlapped, *olp;
8504 SOCKET socket;
8505 ULONG on;
8506 ULONG_PTR key;
8507 DWORD num_bytes;
8508 BOOL ret;
8509 int res;
8511 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
8512 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
8514 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
8515 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
8517 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
8518 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
8520 on = 1;
8521 memset( &overlapped, 0, sizeof(overlapped) );
8522 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
8523 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
8525 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
8526 ok( ret, "failed to get completion status %u\n", GetLastError() );
8528 CloseHandle( io_port );
8529 closesocket( socket );
8530 CloseHandle( previous_port );
8533 #define WM_ASYNCCOMPLETE (WM_USER + 100)
8534 static HWND create_async_message_window(void)
8536 static const char class_name[] = "ws2_32 async message window class";
8538 WNDCLASSEXA wndclass;
8539 HWND hWnd;
8541 wndclass.cbSize = sizeof(wndclass);
8542 wndclass.style = CS_HREDRAW | CS_VREDRAW;
8543 wndclass.lpfnWndProc = DefWindowProcA;
8544 wndclass.cbClsExtra = 0;
8545 wndclass.cbWndExtra = 0;
8546 wndclass.hInstance = GetModuleHandleA(NULL);
8547 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
8548 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
8549 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
8550 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8551 wndclass.lpszClassName = class_name;
8552 wndclass.lpszMenuName = NULL;
8554 RegisterClassExA(&wndclass);
8556 hWnd = CreateWindowA(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW,
8557 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
8558 ok(!!hWnd, "failed to create window\n");
8560 return hWnd;
8563 static void wait_for_async_message(HWND hwnd, HANDLE handle)
8565 BOOL ret;
8566 MSG msg;
8568 while ((ret = GetMessageA(&msg, 0, 0, 0)) &&
8569 !(msg.hwnd == hwnd && msg.message == WM_ASYNCCOMPLETE))
8571 TranslateMessage(&msg);
8572 DispatchMessageA(&msg);
8575 ok(ret, "did not expect WM_QUIT message\n");
8576 ok(msg.wParam == (WPARAM)handle, "expected wParam = %p, got %lx\n", handle, msg.wParam);
8579 static void test_WSAAsyncGetServByPort(void)
8581 HWND hwnd = create_async_message_window();
8582 HANDLE ret;
8583 char buffer[MAXGETHOSTSTRUCT];
8585 /* FIXME: The asynchronous window messages should be tested. */
8587 /* Parameters are not checked when initiating the asynchronous operation. */
8588 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0);
8589 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
8591 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0);
8592 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
8593 wait_for_async_message(hwnd, ret);
8595 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0);
8596 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
8597 wait_for_async_message(hwnd, ret);
8599 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT);
8600 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
8601 wait_for_async_message(hwnd, ret);
8603 DestroyWindow(hwnd);
8606 static void test_WSAAsyncGetServByName(void)
8608 HWND hwnd = create_async_message_window();
8609 HANDLE ret;
8610 char buffer[MAXGETHOSTSTRUCT];
8612 /* FIXME: The asynchronous window messages should be tested. */
8614 /* Parameters are not checked when initiating the asynchronous operation. */
8615 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", NULL, NULL, 0);
8616 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
8617 wait_for_async_message(hwnd, ret);
8619 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", "", buffer, MAXGETHOSTSTRUCT);
8620 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
8621 wait_for_async_message(hwnd, ret);
8623 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", NULL, NULL, 0);
8624 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
8625 wait_for_async_message(hwnd, ret);
8627 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", "tcp", buffer, MAXGETHOSTSTRUCT);
8628 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
8629 wait_for_async_message(hwnd, ret);
8631 DestroyWindow(hwnd);
8635 * Provide consistent initialization for the AcceptEx IOCP tests.
8637 static SOCKET setup_iocp_src(struct sockaddr_in *bindAddress)
8639 SOCKET src;
8640 int iret, socklen;
8642 src = socket(AF_INET, SOCK_STREAM, 0);
8643 ok(src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
8645 memset(bindAddress, 0, sizeof(*bindAddress));
8646 bindAddress->sin_family = AF_INET;
8647 bindAddress->sin_addr.s_addr = inet_addr("127.0.0.1");
8648 iret = bind(src, (struct sockaddr*)bindAddress, sizeof(*bindAddress));
8649 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
8651 socklen = sizeof(*bindAddress);
8652 iret = getsockname(src, (struct sockaddr*)bindAddress, &socklen);
8653 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
8655 iret = set_blocking(src, FALSE);
8656 ok(!iret, "failed to make socket non-blocking, error %u\n", WSAGetLastError());
8658 iret = listen(src, 5);
8659 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
8661 return src;
8664 static void test_completion_port(void)
8666 HANDLE previous_port, io_port;
8667 WSAOVERLAPPED ov, *olp;
8668 SOCKET src, dest, dup, connector = INVALID_SOCKET;
8669 WSAPROTOCOL_INFOA info;
8670 char buf[1024];
8671 WSABUF bufs;
8672 DWORD num_bytes, flags;
8673 struct linger ling;
8674 int iret;
8675 BOOL bret;
8676 ULONG_PTR key;
8677 struct sockaddr_in bindAddress;
8678 GUID acceptExGuid = WSAID_ACCEPTEX;
8679 LPFN_ACCEPTEX pAcceptEx = NULL;
8680 fd_set fds_recv;
8682 memset(buf, 0, sizeof(buf));
8683 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
8684 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError());
8686 memset(&ov, 0, sizeof(ov));
8688 tcp_socketpair(&src, &dest);
8690 bufs.len = sizeof(buf);
8691 bufs.buf = buf;
8692 flags = 0;
8694 ling.l_onoff = 1;
8695 ling.l_linger = 0;
8696 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
8697 ok(!iret, "Failed to set linger %d\n", GetLastError());
8699 io_port = CreateIoCompletionPort( (HANDLE)dest, previous_port, 125, 0 );
8700 ok(io_port != NULL, "Failed to create completion port %u\n", GetLastError());
8702 SetLastError(0xdeadbeef);
8704 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
8705 ok(iret == SOCKET_ERROR, "WSARecv returned %d\n", iret);
8706 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
8708 Sleep(100);
8710 closesocket(src);
8711 src = INVALID_SOCKET;
8713 SetLastError(0xdeadbeef);
8714 key = 0xdeadbeef;
8715 num_bytes = 0xdeadbeef;
8716 olp = (WSAOVERLAPPED *)0xdeadbeef;
8718 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
8719 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret);
8720 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "Last error was %d\n", GetLastError());
8721 ok(key == 125, "Key is %lu\n", key);
8722 ok(num_bytes == 0, "Number of bytes received is %u\n", num_bytes);
8723 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
8725 SetLastError(0xdeadbeef);
8726 key = 0xdeadbeef;
8727 num_bytes = 0xdeadbeef;
8728 olp = (WSAOVERLAPPED *)0xdeadbeef;
8730 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
8731 ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret );
8732 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8733 ok(key == 0xdeadbeef, "Key is %lu\n", key);
8734 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
8735 ok(!olp, "Overlapped structure is at %p\n", olp);
8737 if (dest != INVALID_SOCKET)
8738 closesocket(dest);
8740 memset(&ov, 0, sizeof(ov));
8742 tcp_socketpair(&src, &dest);
8744 bufs.len = sizeof(buf);
8745 bufs.buf = buf;
8746 flags = 0;
8748 ling.l_onoff = 1;
8749 ling.l_linger = 0;
8750 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
8751 ok(!iret, "Failed to set linger %d\n", GetLastError());
8753 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
8754 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8756 set_blocking(dest, FALSE);
8758 closesocket(src);
8759 src = INVALID_SOCKET;
8761 Sleep(100);
8763 num_bytes = 0xdeadbeef;
8764 SetLastError(0xdeadbeef);
8766 iret = WSASend(dest, &bufs, 1, &num_bytes, 0, &ov, NULL);
8767 ok(iret == SOCKET_ERROR, "WSASend failed - %d\n", iret);
8768 ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
8769 ok(num_bytes == 0xdeadbeef, "Managed to send %d\n", num_bytes);
8771 SetLastError(0xdeadbeef);
8772 key = 0xdeadbeef;
8773 num_bytes = 0xdeadbeef;
8774 olp = (WSAOVERLAPPED *)0xdeadbeef;
8776 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8777 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
8778 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8779 ok(key == 0xdeadbeef, "Key is %lu\n", key);
8780 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
8781 ok(!olp, "Overlapped structure is at %p\n", olp);
8783 if (dest != INVALID_SOCKET)
8784 closesocket(dest);
8786 /* Test IOCP response on successful immediate read. */
8787 tcp_socketpair(&src, &dest);
8789 bufs.len = sizeof(buf);
8790 bufs.buf = buf;
8791 flags = 0;
8792 SetLastError(0xdeadbeef);
8794 iret = WSASend(src, &bufs, 1, &num_bytes, 0, &ov, NULL);
8795 ok(!iret, "WSASend failed - %d, last error %u\n", iret, GetLastError());
8796 ok(num_bytes == sizeof(buf), "Managed to send %d\n", num_bytes);
8798 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
8799 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8800 set_blocking(dest, FALSE);
8802 FD_ZERO(&fds_recv);
8803 FD_SET(dest, &fds_recv);
8804 select(dest + 1, &fds_recv, NULL, NULL, NULL);
8806 num_bytes = 0xdeadbeef;
8807 flags = 0;
8809 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
8810 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
8811 ok(num_bytes == sizeof(buf), "Managed to read %d\n", num_bytes);
8813 SetLastError(0xdeadbeef);
8814 key = 0xdeadbeef;
8815 num_bytes = 0xdeadbeef;
8816 olp = (WSAOVERLAPPED *)0xdeadbeef;
8818 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8819 ok(bret == TRUE, "failed to get completion status %u\n", bret);
8820 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
8821 ok(key == 125, "Key is %lu\n", key);
8822 ok(num_bytes == sizeof(buf), "Number of bytes transferred is %u\n", num_bytes);
8823 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
8825 /* Test IOCP response on graceful shutdown. */
8826 closesocket(src);
8828 FD_ZERO(&fds_recv);
8829 FD_SET(dest, &fds_recv);
8830 select(dest + 1, &fds_recv, NULL, NULL, NULL);
8832 num_bytes = 0xdeadbeef;
8833 flags = 0;
8834 memset(&ov, 0, sizeof(ov));
8836 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
8837 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
8838 ok(!num_bytes, "Managed to read %d\n", num_bytes);
8840 SetLastError(0xdeadbeef);
8841 key = 0xdeadbeef;
8842 num_bytes = 0xdeadbeef;
8843 olp = (WSAOVERLAPPED *)0xdeadbeef;
8845 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8846 ok(bret == TRUE, "failed to get completion status %u\n", bret);
8847 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
8848 ok(key == 125, "Key is %lu\n", key);
8849 ok(!num_bytes, "Number of bytes transferred is %u\n", num_bytes);
8850 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
8852 closesocket(src);
8853 src = INVALID_SOCKET;
8854 closesocket(dest);
8855 dest = INVALID_SOCKET;
8857 /* Test IOCP response on hard shutdown. This was the condition that triggered
8858 * a crash in an actual app (bug 38980). */
8859 tcp_socketpair(&src, &dest);
8861 bufs.len = sizeof(buf);
8862 bufs.buf = buf;
8863 flags = 0;
8864 memset(&ov, 0, sizeof(ov));
8866 ling.l_onoff = 1;
8867 ling.l_linger = 0;
8868 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
8869 ok(!iret, "Failed to set linger %d\n", GetLastError());
8871 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
8872 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8873 set_blocking(dest, FALSE);
8875 closesocket(src);
8876 src = INVALID_SOCKET;
8878 FD_ZERO(&fds_recv);
8879 FD_SET(dest, &fds_recv);
8880 select(dest + 1, &fds_recv, NULL, NULL, NULL);
8882 num_bytes = 0xdeadbeef;
8883 SetLastError(0xdeadbeef);
8885 /* Somehow a hard shutdown doesn't work on my Linux box. It seems SO_LINGER is ignored. */
8886 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
8887 todo_wine ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
8888 todo_wine ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
8889 todo_wine ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
8891 SetLastError(0xdeadbeef);
8892 key = 0xdeadbeef;
8893 num_bytes = 0xdeadbeef;
8894 olp = (WSAOVERLAPPED *)0xdeadbeef;
8896 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8897 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
8898 todo_wine ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8899 todo_wine ok(key == 0xdeadbeef, "Key is %lu\n", key);
8900 todo_wine ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
8901 todo_wine ok(!olp, "Overlapped structure is at %p\n", olp);
8903 closesocket(dest);
8905 /* Test reading from a non-connected socket, mostly because the above test is marked todo. */
8906 dest = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
8907 ok(dest != INVALID_SOCKET, "socket() failed\n");
8909 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
8910 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8911 set_blocking(dest, FALSE);
8913 num_bytes = 0xdeadbeef;
8914 SetLastError(0xdeadbeef);
8915 memset(&ov, 0, sizeof(ov));
8917 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
8918 ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
8919 ok(GetLastError() == WSAENOTCONN, "Last error was %d\n", GetLastError());
8920 ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
8922 SetLastError(0xdeadbeef);
8923 key = 0xdeadbeef;
8924 num_bytes = 0xdeadbeef;
8925 olp = (WSAOVERLAPPED *)0xdeadbeef;
8927 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8928 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
8929 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8930 ok(key == 0xdeadbeef, "Key is %lu\n", key);
8931 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
8932 ok(!olp, "Overlapped structure is at %p\n", olp);
8934 num_bytes = 0xdeadbeef;
8935 closesocket(dest);
8937 dest = socket(AF_INET, SOCK_STREAM, 0);
8938 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
8940 iret = WSAIoctl(dest, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
8941 &pAcceptEx, sizeof(pAcceptEx), &num_bytes, NULL, NULL);
8942 ok(!iret, "failed to get AcceptEx, error %u\n", WSAGetLastError());
8944 /* Test IOCP response on socket close (IOCP created after AcceptEx) */
8946 src = setup_iocp_src(&bindAddress);
8948 SetLastError(0xdeadbeef);
8950 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
8951 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8952 &num_bytes, &ov);
8953 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
8954 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
8956 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
8957 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8959 closesocket(src);
8960 src = INVALID_SOCKET;
8962 SetLastError(0xdeadbeef);
8963 key = 0xdeadbeef;
8964 num_bytes = 0xdeadbeef;
8965 olp = (WSAOVERLAPPED *)0xdeadbeef;
8967 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
8968 ok(bret == FALSE, "failed to get completion status %u\n", bret);
8969 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
8970 ok(key == 125, "Key is %lu\n", key);
8971 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
8972 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
8973 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
8975 SetLastError(0xdeadbeef);
8976 key = 0xdeadbeef;
8977 num_bytes = 0xdeadbeef;
8978 olp = (WSAOVERLAPPED *)0xdeadbeef;
8979 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
8980 ok(bret == FALSE, "failed to get completion status %u\n", bret);
8981 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
8982 ok(key == 0xdeadbeef, "Key is %lu\n", key);
8983 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
8984 ok(!olp, "Overlapped structure is at %p\n", olp);
8986 /* Test IOCP response on socket close (IOCP created before AcceptEx) */
8988 src = setup_iocp_src(&bindAddress);
8990 SetLastError(0xdeadbeef);
8992 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
8993 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
8995 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
8996 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8997 &num_bytes, &ov);
8998 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
8999 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9001 closesocket(src);
9002 src = INVALID_SOCKET;
9004 SetLastError(0xdeadbeef);
9005 key = 0xdeadbeef;
9006 num_bytes = 0xdeadbeef;
9007 olp = (WSAOVERLAPPED *)0xdeadbeef;
9009 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9010 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9011 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
9012 ok(key == 125, "Key is %lu\n", key);
9013 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9014 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9015 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
9017 SetLastError(0xdeadbeef);
9018 key = 0xdeadbeef;
9019 num_bytes = 0xdeadbeef;
9020 olp = (WSAOVERLAPPED *)0xdeadbeef;
9021 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9022 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9023 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9024 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9025 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9026 ok(!olp, "Overlapped structure is at %p\n", olp);
9028 /* Test IOCP with duplicated handle */
9030 src = setup_iocp_src(&bindAddress);
9032 SetLastError(0xdeadbeef);
9034 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9035 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9037 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
9038 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
9039 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
9041 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9042 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9043 &num_bytes, &ov);
9044 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9045 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9047 SetLastError(0xdeadbeef);
9048 key = 0xdeadbeef;
9049 num_bytes = 0xdeadbeef;
9050 olp = (WSAOVERLAPPED *)0xdeadbeef;
9051 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9052 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9053 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9054 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9055 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9056 ok(!olp, "Overlapped structure is at %p\n", olp);
9058 closesocket(src);
9059 src = INVALID_SOCKET;
9060 closesocket(dup);
9061 dup = INVALID_SOCKET;
9063 SetLastError(0xdeadbeef);
9064 key = 0xdeadbeef;
9065 num_bytes = 0xdeadbeef;
9066 olp = (WSAOVERLAPPED *)0xdeadbeef;
9067 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9068 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9069 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
9070 ok(key == 125, "Key is %lu\n", key);
9071 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9072 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9073 ok(olp && olp->Internal == (ULONG)STATUS_CANCELLED, "Internal status is %lx\n", olp ? olp->Internal : 0);
9075 SetLastError(0xdeadbeef);
9076 key = 0xdeadbeef;
9077 num_bytes = 0xdeadbeef;
9078 olp = (WSAOVERLAPPED *)0xdeadbeef;
9079 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9080 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9081 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9082 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9083 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9084 ok(!olp, "Overlapped structure is at %p\n", olp);
9086 /* Test IOCP with duplicated handle (closing duplicated handle) */
9088 src = setup_iocp_src(&bindAddress);
9090 SetLastError(0xdeadbeef);
9092 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9093 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9095 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
9096 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
9097 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
9099 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9100 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9101 &num_bytes, &ov);
9102 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9103 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9105 closesocket(dup);
9106 dup = INVALID_SOCKET;
9108 SetLastError(0xdeadbeef);
9109 key = 0xdeadbeef;
9110 num_bytes = 0xdeadbeef;
9111 olp = (WSAOVERLAPPED *)0xdeadbeef;
9112 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9113 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9114 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9115 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9116 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9117 ok(!olp, "Overlapped structure is at %p\n", olp);
9119 SetLastError(0xdeadbeef);
9120 key = 0xdeadbeef;
9121 num_bytes = 0xdeadbeef;
9122 olp = (WSAOVERLAPPED *)0xdeadbeef;
9123 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9124 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9125 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9126 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9127 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9128 ok(!olp, "Overlapped structure is at %p\n", olp);
9130 closesocket(src);
9131 src = INVALID_SOCKET;
9133 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9134 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9135 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
9136 ok(key == 125, "Key is %lu\n", key);
9137 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9138 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9139 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
9141 SetLastError(0xdeadbeef);
9142 key = 0xdeadbeef;
9143 num_bytes = 0xdeadbeef;
9144 olp = (WSAOVERLAPPED *)0xdeadbeef;
9145 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9146 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9147 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9148 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9149 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9150 ok(!olp, "Overlapped structure is at %p\n", olp);
9152 /* Test IOCP with duplicated handle (closing original handle) */
9154 src = setup_iocp_src(&bindAddress);
9156 SetLastError(0xdeadbeef);
9158 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9159 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9161 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
9162 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
9163 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
9165 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9166 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9167 &num_bytes, &ov);
9168 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9169 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9171 closesocket(src);
9172 src = INVALID_SOCKET;
9174 SetLastError(0xdeadbeef);
9175 key = 0xdeadbeef;
9176 num_bytes = 0xdeadbeef;
9177 olp = (WSAOVERLAPPED *)0xdeadbeef;
9178 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9179 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9180 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9181 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9182 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9183 ok(!olp, "Overlapped structure is at %p\n", olp);
9185 closesocket(dup);
9186 dup = INVALID_SOCKET;
9188 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9189 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9190 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
9191 ok(key == 125, "Key is %lu\n", key);
9192 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9193 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9194 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
9196 SetLastError(0xdeadbeef);
9197 key = 0xdeadbeef;
9198 num_bytes = 0xdeadbeef;
9199 olp = (WSAOVERLAPPED *)0xdeadbeef;
9200 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9201 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9202 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9203 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9204 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9205 ok(!olp, "Overlapped structure is at %p\n", olp);
9207 /* Test IOCP without AcceptEx */
9209 src = setup_iocp_src(&bindAddress);
9211 SetLastError(0xdeadbeef);
9213 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9214 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9216 closesocket(src);
9217 src = INVALID_SOCKET;
9219 SetLastError(0xdeadbeef);
9220 key = 0xdeadbeef;
9221 num_bytes = 0xdeadbeef;
9222 olp = (WSAOVERLAPPED *)0xdeadbeef;
9223 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9224 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9225 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9226 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9227 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9228 ok(!olp, "Overlapped structure is at %p\n", olp);
9230 /* */
9232 src = setup_iocp_src(&bindAddress);
9234 connector = socket(AF_INET, SOCK_STREAM, 0);
9235 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
9237 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9238 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9240 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
9241 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9243 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9244 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9245 &num_bytes, &ov);
9246 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9247 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9249 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9250 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
9252 closesocket(connector);
9253 connector = INVALID_SOCKET;
9255 SetLastError(0xdeadbeef);
9256 key = 0xdeadbeef;
9257 num_bytes = 0xdeadbeef;
9258 olp = (WSAOVERLAPPED *)0xdeadbeef;
9260 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9261 ok(bret == TRUE, "failed to get completion status %u\n", bret);
9262 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
9263 ok(key == 125, "Key is %lu\n", key);
9264 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9265 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9266 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
9268 SetLastError(0xdeadbeef);
9269 key = 0xdeadbeef;
9270 num_bytes = 0xdeadbeef;
9271 olp = (WSAOVERLAPPED *)0xdeadbeef;
9272 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9273 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9274 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9275 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9276 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9277 ok(!olp, "Overlapped structure is at %p\n", olp);
9279 if (dest != INVALID_SOCKET)
9280 closesocket(dest);
9281 if (src != INVALID_SOCKET)
9282 closesocket(dest);
9284 /* */
9286 src = setup_iocp_src(&bindAddress);
9288 dest = socket(AF_INET, SOCK_STREAM, 0);
9289 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
9291 connector = socket(AF_INET, SOCK_STREAM, 0);
9292 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
9294 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9295 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9297 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
9298 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9300 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9301 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9302 &num_bytes, &ov);
9303 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9304 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9306 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9307 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
9309 iret = send(connector, buf, 1, 0);
9310 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
9312 Sleep(100);
9314 closesocket(dest);
9315 dest = INVALID_SOCKET;
9317 SetLastError(0xdeadbeef);
9318 key = 0xdeadbeef;
9319 num_bytes = 0xdeadbeef;
9320 olp = (WSAOVERLAPPED *)0xdeadbeef;
9322 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9323 ok(bret == TRUE, "failed to get completion status %u\n", bret);
9324 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
9325 ok(key == 125, "Key is %lu\n", key);
9326 ok(num_bytes == 1, "Number of bytes transferred is %u\n", num_bytes);
9327 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9328 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
9330 SetLastError(0xdeadbeef);
9331 key = 0xdeadbeef;
9332 num_bytes = 0xdeadbeef;
9333 olp = (WSAOVERLAPPED *)0xdeadbeef;
9334 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9335 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9336 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9337 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9338 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9339 ok(!olp, "Overlapped structure is at %p\n", olp);
9341 if (src != INVALID_SOCKET)
9342 closesocket(src);
9343 if (connector != INVALID_SOCKET)
9344 closesocket(connector);
9346 /* */
9348 src = setup_iocp_src(&bindAddress);
9350 dest = socket(AF_INET, SOCK_STREAM, 0);
9351 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
9353 connector = socket(AF_INET, SOCK_STREAM, 0);
9354 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
9356 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
9357 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9359 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
9360 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9362 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
9363 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
9364 &num_bytes, &ov);
9365 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
9366 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9368 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9369 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
9371 closesocket(dest);
9373 SetLastError(0xdeadbeef);
9374 key = 0xdeadbeef;
9375 num_bytes = 0xdeadbeef;
9376 olp = (WSAOVERLAPPED *)0xdeadbeef;
9378 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9379 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9380 ok(GetLastError() == ERROR_OPERATION_ABORTED
9381 || GetLastError() == ERROR_CONNECTION_ABORTED, "got error %u\n", GetLastError());
9382 ok(key == 125, "Key is %lu\n", key);
9383 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
9384 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9385 ok((NTSTATUS)olp->Internal == STATUS_CANCELLED
9386 || (NTSTATUS)olp->Internal == STATUS_CONNECTION_ABORTED, "got status %#lx\n", olp->Internal);
9388 SetLastError(0xdeadbeef);
9389 key = 0xdeadbeef;
9390 num_bytes = 0xdeadbeef;
9391 olp = (WSAOVERLAPPED *)0xdeadbeef;
9392 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9393 ok(bret == FALSE, "failed to get completion status %u\n", bret);
9394 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9395 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9396 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9397 ok(!olp, "Overlapped structure is at %p\n", olp);
9400 closesocket(src);
9401 closesocket(connector);
9402 CloseHandle(previous_port);
9405 static void test_address_list_query(void)
9407 SOCKET_ADDRESS_LIST *address_list;
9408 DWORD bytes_returned, size;
9409 unsigned int i;
9410 SOCKET s;
9411 int ret;
9413 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
9414 ok(s != INVALID_SOCKET, "Failed to create socket, error %d.\n", WSAGetLastError());
9416 bytes_returned = 0;
9417 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0, &bytes_returned, NULL, NULL);
9418 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
9419 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
9420 ok(bytes_returned >= FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]),
9421 "Got unexpected bytes_returned %u.\n", bytes_returned);
9423 size = bytes_returned;
9424 bytes_returned = 0;
9425 address_list = HeapAlloc(GetProcessHeap(), 0, size * 2);
9426 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size * 2, &bytes_returned, NULL, NULL);
9427 ok(!ret, "Got unexpected ret %d, error %d.\n", ret, WSAGetLastError());
9428 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
9430 bytes_returned = FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[address_list->iAddressCount]);
9431 for (i = 0; i < address_list->iAddressCount; ++i)
9433 bytes_returned += address_list->Address[i].iSockaddrLength;
9435 ok(size == bytes_returned, "Got unexpected size %u, expected %u.\n", size, bytes_returned);
9437 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size, NULL, NULL, NULL);
9438 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
9439 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
9441 bytes_returned = 0xdeadbeef;
9442 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, size, &bytes_returned, NULL, NULL);
9443 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
9444 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
9445 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
9447 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, 1, &bytes_returned, NULL, NULL);
9448 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
9449 ok(WSAGetLastError() == WSAEINVAL, "Got unexpected error %d.\n", WSAGetLastError());
9450 ok(bytes_returned == 0, "Got unexpected bytes_returned %u.\n", bytes_returned);
9452 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list,
9453 FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]), &bytes_returned, NULL, NULL);
9454 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
9455 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
9456 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
9458 HeapFree(GetProcessHeap(), 0, address_list);
9459 closesocket(s);
9462 static DWORD WINAPI inet_ntoa_thread_proc(void *param)
9464 ULONG addr;
9465 const char *str;
9466 HANDLE *event = param;
9468 addr = inet_addr("4.3.2.1");
9469 ok(addr == htonl(0x04030201), "expected 0x04030201, got %08x\n", addr);
9470 str = inet_ntoa(*(struct in_addr *)&addr);
9471 ok(!strcmp(str, "4.3.2.1"), "expected 4.3.2.1, got %s\n", str);
9473 SetEvent(event[0]);
9474 WaitForSingleObject(event[1], 3000);
9476 return 0;
9479 static void test_inet_ntoa(void)
9481 ULONG addr;
9482 const char *str;
9483 HANDLE thread, event[2];
9484 DWORD tid;
9486 addr = inet_addr("1.2.3.4");
9487 ok(addr == htonl(0x01020304), "expected 0x01020304, got %08x\n", addr);
9488 str = inet_ntoa(*(struct in_addr *)&addr);
9489 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str);
9491 event[0] = CreateEventW(NULL, TRUE, FALSE, NULL);
9492 event[1] = CreateEventW(NULL, TRUE, FALSE, NULL);
9494 thread = CreateThread(NULL, 0, inet_ntoa_thread_proc, event, 0, &tid);
9495 WaitForSingleObject(event[0], 3000);
9497 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str);
9499 SetEvent(event[1]);
9500 WaitForSingleObject(thread, 3000);
9502 CloseHandle(event[0]);
9503 CloseHandle(event[1]);
9504 CloseHandle(thread);
9507 static void test_WSALookupService(void)
9509 char buffer[4096], strbuff[128];
9510 WSAQUERYSETW *qs = NULL;
9511 HANDLE hnd;
9512 PNLA_BLOB netdata;
9513 int ret;
9514 DWORD error, offset, bsize;
9516 qs = (WSAQUERYSETW *)buffer;
9517 memset(qs, 0, sizeof(*qs));
9519 /* invalid parameter tests */
9520 ret = WSALookupServiceBeginW(NULL, 0, &hnd);
9521 error = WSAGetLastError();
9522 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
9523 todo_wine
9524 ok(error == WSAEFAULT, "expected 10014, got %d\n", error);
9526 ret = WSALookupServiceBeginW(qs, 0, NULL);
9527 error = WSAGetLastError();
9528 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
9529 todo_wine
9530 ok(error == WSAEFAULT, "expected 10014, got %d\n", error);
9532 ret = WSALookupServiceBeginW(qs, 0, &hnd);
9533 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
9534 todo_wine ok(WSAGetLastError() == ERROR_INVALID_PARAMETER
9535 || broken(WSAGetLastError() == WSASERVICE_NOT_FOUND) /* win10 1809 */,
9536 "got error %u\n", WSAGetLastError());
9538 ret = WSALookupServiceEnd(NULL);
9539 error = WSAGetLastError();
9540 todo_wine
9541 ok(ret == SOCKET_ERROR, "WSALookupServiceEnd should have failed\n");
9542 todo_wine
9543 ok(error == ERROR_INVALID_HANDLE, "expected 6, got %d\n", error);
9545 /* standard network list query */
9546 qs->dwSize = sizeof(*qs);
9547 hnd = (HANDLE)0xdeadbeef;
9548 ret = WSALookupServiceBeginW(qs, LUP_RETURN_ALL | LUP_DEEP, &hnd);
9549 error = WSAGetLastError();
9550 if(ret && error == ERROR_INVALID_PARAMETER)
9552 win_skip("the current WSALookupServiceBeginW test is not supported in win <= 2000\n");
9553 return;
9556 todo_wine
9557 ok(!ret, "WSALookupServiceBeginW failed unexpectedly with error %d\n", error);
9558 todo_wine
9559 ok(hnd != (HANDLE)0xdeadbeef, "Handle was not filled\n");
9561 offset = 0;
9564 memset(qs, 0, sizeof(*qs));
9565 bsize = sizeof(buffer);
9567 if (WSALookupServiceNextW(hnd, 0, &bsize, qs) == SOCKET_ERROR)
9569 ok(WSAGetLastError() == WSA_E_NO_MORE, "got error %u\n", WSAGetLastError());
9570 break;
9573 if (winetest_debug <= 1) continue;
9575 WideCharToMultiByte(CP_ACP, 0, qs->lpszServiceInstanceName, -1,
9576 strbuff, sizeof(strbuff), NULL, NULL);
9577 trace("Network Name: %s\n", strbuff);
9579 /* network data is written in the blob field */
9580 if (qs->lpBlob)
9582 /* each network may have multiple NLA_BLOB information structures */
9585 netdata = (PNLA_BLOB) &qs->lpBlob->pBlobData[offset];
9586 switch (netdata->header.type)
9588 case NLA_RAW_DATA:
9589 trace("\tNLA Data Type: NLA_RAW_DATA\n");
9590 break;
9591 case NLA_INTERFACE:
9592 trace("\tNLA Data Type: NLA_INTERFACE\n");
9593 trace("\t\tType: %d\n", netdata->data.interfaceData.dwType);
9594 trace("\t\tSpeed: %d\n", netdata->data.interfaceData.dwSpeed);
9595 trace("\t\tAdapter Name: %s\n", netdata->data.interfaceData.adapterName);
9596 break;
9597 case NLA_802_1X_LOCATION:
9598 trace("\tNLA Data Type: NLA_802_1X_LOCATION\n");
9599 trace("\t\tInformation: %s\n", netdata->data.locationData.information);
9600 break;
9601 case NLA_CONNECTIVITY:
9602 switch (netdata->data.connectivity.type)
9604 case NLA_NETWORK_AD_HOC:
9605 trace("\t\tNetwork Type: AD HOC\n");
9606 break;
9607 case NLA_NETWORK_MANAGED:
9608 trace("\t\tNetwork Type: Managed\n");
9609 break;
9610 case NLA_NETWORK_UNMANAGED:
9611 trace("\t\tNetwork Type: Unmanaged\n");
9612 break;
9613 case NLA_NETWORK_UNKNOWN:
9614 trace("\t\tNetwork Type: Unknown\n");
9616 switch (netdata->data.connectivity.internet)
9618 case NLA_INTERNET_NO:
9619 trace("\t\tInternet connectivity: No\n");
9620 break;
9621 case NLA_INTERNET_YES:
9622 trace("\t\tInternet connectivity: Yes\n");
9623 break;
9624 case NLA_INTERNET_UNKNOWN:
9625 trace("\t\tInternet connectivity: Unknown\n");
9626 break;
9628 break;
9629 case NLA_ICS:
9630 trace("\tNLA Data Type: NLA_ICS\n");
9631 trace("\t\tSpeed: %d\n",
9632 netdata->data.ICS.remote.speed);
9633 trace("\t\tType: %d\n",
9634 netdata->data.ICS.remote.type);
9635 trace("\t\tState: %d\n",
9636 netdata->data.ICS.remote.state);
9637 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.machineName, -1,
9638 strbuff, sizeof(strbuff), NULL, NULL);
9639 trace("\t\tMachine Name: %s\n", strbuff);
9640 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.sharedAdapterName, -1,
9641 strbuff, sizeof(strbuff), NULL, NULL);
9642 trace("\t\tShared Adapter Name: %s\n", strbuff);
9643 break;
9644 default:
9645 trace("\tNLA Data Type: Unknown\n");
9646 break;
9649 while (offset);
9652 while (1);
9654 ret = WSALookupServiceEnd(hnd);
9655 ok(!ret, "WSALookupServiceEnd failed unexpectedly\n");
9658 static void test_WSAEnumNameSpaceProvidersA(void)
9660 LPWSANAMESPACE_INFOA name = NULL;
9661 DWORD ret, error, blen = 0;
9663 SetLastError(0xdeadbeef);
9664 ret = WSAEnumNameSpaceProvidersA(&blen, name);
9665 error = WSAGetLastError();
9666 todo_wine
9667 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9668 todo_wine
9669 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9671 /* Invalid parameter tests */
9672 SetLastError(0xdeadbeef);
9673 ret = WSAEnumNameSpaceProvidersA(NULL, name);
9674 error = WSAGetLastError();
9675 todo_wine
9676 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9677 todo_wine
9678 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9680 SetLastError(0xdeadbeef);
9681 ret = WSAEnumNameSpaceProvidersA(NULL, NULL);
9682 error = WSAGetLastError();
9683 todo_wine
9684 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9685 todo_wine
9686 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9688 SetLastError(0xdeadbeef);
9689 ret = WSAEnumNameSpaceProvidersA(&blen, NULL);
9690 error = WSAGetLastError();
9691 todo_wine
9692 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9693 todo_wine
9694 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9696 name = HeapAlloc(GetProcessHeap(), 0, blen);
9698 ret = WSAEnumNameSpaceProvidersA(&blen, name);
9699 todo_wine
9700 ok(ret > 0, "Expected more than zero name space providers\n");
9702 HeapFree(GetProcessHeap(), 0, name);
9705 static void test_WSAEnumNameSpaceProvidersW(void)
9707 LPWSANAMESPACE_INFOW name = NULL;
9708 DWORD ret, error, blen = 0, i;
9710 SetLastError(0xdeadbeef);
9711 ret = WSAEnumNameSpaceProvidersW(&blen, name);
9712 error = WSAGetLastError();
9713 todo_wine
9714 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9715 todo_wine
9716 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9718 /* Invalid parameter tests */
9719 SetLastError(0xdeadbeef);
9720 ret = WSAEnumNameSpaceProvidersW(NULL, name);
9721 error = WSAGetLastError();
9722 todo_wine
9723 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9724 todo_wine
9725 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9727 SetLastError(0xdeadbeef);
9728 ret = WSAEnumNameSpaceProvidersW(NULL, NULL);
9729 error = WSAGetLastError();
9730 todo_wine
9731 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9732 todo_wine
9733 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9735 SetLastError(0xdeadbeef);
9736 ret = WSAEnumNameSpaceProvidersW(&blen, NULL);
9737 error = WSAGetLastError();
9738 todo_wine
9739 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
9740 todo_wine
9741 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
9743 name = HeapAlloc(GetProcessHeap(), 0, blen);
9745 ret = WSAEnumNameSpaceProvidersW(&blen, name);
9746 todo_wine
9747 ok(ret > 0, "Expected more than zero name space providers\n");
9749 if (winetest_debug > 1)
9751 for (i = 0;i < ret; i++)
9753 trace("Name space Identifier (%p): %s\n", name[i].lpszIdentifier,
9754 wine_dbgstr_w(name[i].lpszIdentifier));
9755 switch (name[i].dwNameSpace)
9757 case NS_DNS:
9758 trace("\tName space ID: NS_DNS (%u)\n", name[i].dwNameSpace);
9759 break;
9760 case NS_NLA:
9761 trace("\tName space ID: NS_NLA (%u)\n", name[i].dwNameSpace);
9762 break;
9763 default:
9764 trace("\tName space ID: Unknown (%u)\n", name[i].dwNameSpace);
9765 break;
9767 trace("\tActive: %d\n", name[i].fActive);
9768 trace("\tVersion: %d\n", name[i].dwVersion);
9772 HeapFree(GetProcessHeap(), 0, name);
9775 static void sync_read(SOCKET src, SOCKET dst)
9777 int ret;
9778 char data[512];
9780 ret = send(dst, "Hello World!", 12, 0);
9781 ok(ret == 12, "send returned %d\n", ret);
9783 memset(data, 0, sizeof(data));
9784 ret = recv(src, data, sizeof(data), 0);
9785 ok(ret == 12, "expected 12, got %d\n", ret);
9786 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", ret, ret, data);
9789 static void iocp_async_read(SOCKET src, SOCKET dst)
9791 HANDLE port;
9792 WSAOVERLAPPED ovl, *ovl_iocp;
9793 WSABUF buf;
9794 int ret;
9795 char data[512];
9796 DWORD flags, bytes;
9797 ULONG_PTR key;
9799 memset(data, 0, sizeof(data));
9800 memset(&ovl, 0, sizeof(ovl));
9802 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
9803 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
9805 buf.len = sizeof(data);
9806 buf.buf = data;
9807 bytes = 0xdeadbeef;
9808 flags = 0;
9809 SetLastError(0xdeadbeef);
9810 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
9811 ok(ret == SOCKET_ERROR, "got %d\n", ret);
9812 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
9813 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9815 bytes = 0xdeadbeef;
9816 key = 0xdeadbeef;
9817 ovl_iocp = (void *)0xdeadbeef;
9818 SetLastError(0xdeadbeef);
9819 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9820 ok(!ret, "got %d\n", ret);
9821 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
9822 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9823 ok(key == 0xdeadbeef, "got key %#lx\n", key);
9824 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
9826 ret = send(dst, "Hello World!", 12, 0);
9827 ok(ret == 12, "send returned %d\n", ret);
9829 bytes = 0xdeadbeef;
9830 key = 0xdeadbeef;
9831 ovl_iocp = NULL;
9832 SetLastError(0xdeadbeef);
9833 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9834 ok(ret, "got %d\n", ret);
9835 ok(bytes == 12, "got bytes %u\n", bytes);
9836 ok(key == 0x12345678, "got key %#lx\n", key);
9837 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
9838 if (ovl_iocp)
9840 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
9841 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
9842 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
9845 bytes = 0xdeadbeef;
9846 key = 0xdeadbeef;
9847 ovl_iocp = (void *)0xdeadbeef;
9848 SetLastError(0xdeadbeef);
9849 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9850 ok(!ret, "got %d\n", ret);
9851 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
9852 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9853 ok(key == 0xdeadbeef, "got key %#lx\n", key);
9854 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
9856 CloseHandle(port);
9859 static void iocp_async_read_closesocket(SOCKET src, int how_to_close)
9861 HANDLE port;
9862 WSAOVERLAPPED ovl, *ovl_iocp;
9863 WSABUF buf;
9864 int ret;
9865 char data[512];
9866 DWORD flags, bytes;
9867 ULONG_PTR key;
9868 HWND hwnd;
9869 MSG msg;
9871 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
9872 0, 0, 0, 0, NULL, NULL, 0, NULL);
9873 ok(hwnd != 0, "CreateWindowEx failed\n");
9875 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
9876 ok(!ret, "got %d\n", ret);
9878 Sleep(100);
9879 memset(&msg, 0, sizeof(msg));
9880 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
9881 ok(ret, "got %d\n", ret);
9882 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
9883 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
9884 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
9885 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
9887 memset(data, 0, sizeof(data));
9888 memset(&ovl, 0, sizeof(ovl));
9890 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
9891 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
9893 Sleep(100);
9894 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
9895 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
9897 buf.len = sizeof(data);
9898 buf.buf = data;
9899 bytes = 0xdeadbeef;
9900 flags = 0;
9901 SetLastError(0xdeadbeef);
9902 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
9903 ok(ret == SOCKET_ERROR, "got %d\n", ret);
9904 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
9905 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9907 Sleep(100);
9908 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
9909 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
9911 bytes = 0xdeadbeef;
9912 key = 0xdeadbeef;
9913 ovl_iocp = (void *)0xdeadbeef;
9914 SetLastError(0xdeadbeef);
9915 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9916 ok(!ret, "got %d\n", ret);
9917 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
9918 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9919 ok(key == 0xdeadbeef, "got key %#lx\n", key);
9920 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
9922 Sleep(100);
9923 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
9924 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
9926 switch (how_to_close)
9928 case 0:
9929 closesocket(src);
9930 break;
9931 case 1:
9932 CloseHandle((HANDLE)src);
9933 break;
9934 case 2:
9935 pNtClose((HANDLE)src);
9936 break;
9937 default:
9938 ok(0, "wrong value %d\n", how_to_close);
9939 break;
9942 Sleep(200);
9943 memset(&msg, 0, sizeof(msg));
9944 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
9945 switch (how_to_close)
9947 case 0:
9948 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
9949 break;
9950 case 1:
9951 case 2:
9952 todo_wine
9954 ok(ret, "got %d\n", ret);
9955 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
9956 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
9957 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
9958 ok(msg.lParam == 0x20, "got %08lx\n", msg.lParam);
9960 break;
9961 default:
9962 ok(0, "wrong value %d\n", how_to_close);
9963 break;
9966 bytes = 0xdeadbeef;
9967 key = 0xdeadbeef;
9968 ovl_iocp = NULL;
9969 SetLastError(0xdeadbeef);
9970 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9971 ok(!ret, "got %d\n", ret);
9972 todo_wine
9973 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
9974 ok(!bytes, "got bytes %u\n", bytes);
9975 ok(key == 0x12345678, "got key %#lx\n", key);
9976 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
9977 if (ovl_iocp)
9979 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
9980 todo_wine
9981 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
9984 bytes = 0xdeadbeef;
9985 key = 0xdeadbeef;
9986 ovl_iocp = (void *)0xdeadbeef;
9987 SetLastError(0xdeadbeef);
9988 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
9989 ok(!ret, "got %d\n", ret);
9990 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
9991 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
9992 ok(key == 0xdeadbeef, "got key %#lx\n", key);
9993 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
9995 CloseHandle(port);
9997 DestroyWindow(hwnd);
10000 static void iocp_async_closesocket(SOCKET src)
10002 HANDLE port;
10003 WSAOVERLAPPED *ovl_iocp;
10004 int ret;
10005 DWORD bytes;
10006 ULONG_PTR key;
10007 HWND hwnd;
10008 MSG msg;
10010 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
10011 0, 0, 0, 0, NULL, NULL, 0, NULL);
10012 ok(hwnd != 0, "CreateWindowEx failed\n");
10014 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
10015 ok(!ret, "got %d\n", ret);
10017 Sleep(100);
10018 memset(&msg, 0, sizeof(msg));
10019 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10020 ok(ret, "got %d\n", ret);
10021 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
10022 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
10023 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
10024 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
10026 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
10027 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
10029 Sleep(100);
10030 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10031 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10033 bytes = 0xdeadbeef;
10034 key = 0xdeadbeef;
10035 ovl_iocp = (void *)0xdeadbeef;
10036 SetLastError(0xdeadbeef);
10037 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10038 ok(!ret, "got %d\n", ret);
10039 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
10040 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10041 ok(key == 0xdeadbeef, "got key %lu\n", key);
10042 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10044 Sleep(100);
10045 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10046 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10048 closesocket(src);
10050 Sleep(100);
10051 memset(&msg, 0, sizeof(msg));
10052 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10053 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10055 bytes = 0xdeadbeef;
10056 key = 0xdeadbeef;
10057 ovl_iocp = (void *)0xdeadbeef;
10058 SetLastError(0xdeadbeef);
10059 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10060 ok(!ret, "got %d\n", ret);
10061 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
10062 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10063 ok(key == 0xdeadbeef, "got key %lu\n", key);
10064 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10066 CloseHandle(port);
10068 DestroyWindow(hwnd);
10071 struct wsa_async_select_info
10073 SOCKET sock;
10074 HWND hwnd;
10077 static DWORD WINAPI wsa_async_select_thread(void *param)
10079 struct wsa_async_select_info *info = param;
10080 int ret;
10082 ret = WSAAsyncSelect(info->sock, info->hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
10083 ok(!ret, "got %d\n", ret);
10085 return 0;
10088 struct wsa_recv_info
10090 SOCKET sock;
10091 WSABUF wsa_buf;
10092 WSAOVERLAPPED ovl;
10095 static DWORD WINAPI wsa_recv_thread(void *param)
10097 struct wsa_recv_info *info = param;
10098 int ret;
10099 DWORD flags, bytes;
10101 bytes = 0xdeadbeef;
10102 flags = 0;
10103 SetLastError(0xdeadbeef);
10104 ret = WSARecv(info->sock, &info->wsa_buf, 1, &bytes, &flags, &info->ovl, NULL);
10105 ok(ret == SOCKET_ERROR, "got %d\n", ret);
10106 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
10107 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10109 return 0;
10112 static void iocp_async_read_thread_closesocket(SOCKET src)
10114 struct wsa_async_select_info select_info;
10115 struct wsa_recv_info recv_info;
10116 HANDLE port, thread;
10117 WSAOVERLAPPED *ovl_iocp;
10118 int ret;
10119 char data[512];
10120 DWORD bytes, tid;
10121 ULONG_PTR key;
10122 HWND hwnd;
10123 MSG msg;
10125 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
10126 0, 0, 0, 0, NULL, NULL, 0, NULL);
10127 ok(hwnd != 0, "CreateWindowEx failed\n");
10129 select_info.sock = src;
10130 select_info.hwnd = hwnd;
10131 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
10132 ok(thread != 0, "CreateThread error %u\n", GetLastError());
10133 ret = WaitForSingleObject(thread, 10000);
10134 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
10136 Sleep(100);
10137 memset(&msg, 0, sizeof(msg));
10138 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10139 ok(ret, "got %d\n", ret);
10140 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
10141 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
10142 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
10143 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
10145 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
10146 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
10148 Sleep(100);
10149 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10150 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10152 memset(data, 0, sizeof(data));
10153 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
10154 recv_info.sock = src;
10155 recv_info.wsa_buf.len = sizeof(data);
10156 recv_info.wsa_buf.buf = data;
10157 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
10158 ok(thread != 0, "CreateThread error %u\n", GetLastError());
10159 ret = WaitForSingleObject(thread, 10000);
10160 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
10162 Sleep(100);
10163 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10164 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10166 bytes = 0xdeadbeef;
10167 key = 0xdeadbeef;
10168 ovl_iocp = (void *)0xdeadbeef;
10169 SetLastError(0xdeadbeef);
10170 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10171 ok(!ret, "got %d\n", ret);
10172 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */,
10173 "got %u\n", GetLastError());
10174 if (GetLastError() == WAIT_TIMEOUT)
10176 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10177 ok(key == 0xdeadbeef, "got key %lx\n", key);
10178 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10180 else /* document XP behaviour */
10182 ok(!bytes, "got bytes %u\n", bytes);
10183 ok(key == 0x12345678, "got key %#lx\n", key);
10184 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
10185 if (ovl_iocp)
10187 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
10188 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
10191 closesocket(src);
10192 goto xp_is_broken;
10195 Sleep(100);
10196 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10197 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10199 closesocket(src);
10201 Sleep(100);
10202 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10203 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10205 bytes = 0xdeadbeef;
10206 key = 0xdeadbeef;
10207 ovl_iocp = NULL;
10208 SetLastError(0xdeadbeef);
10209 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10210 ok(!ret, "got %d\n", ret);
10211 todo_wine
10212 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
10213 ok(!bytes, "got bytes %u\n", bytes);
10214 ok(key == 0x12345678, "got key %#lx\n", key);
10215 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
10216 if (ovl_iocp)
10218 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
10219 todo_wine
10220 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
10223 xp_is_broken:
10224 bytes = 0xdeadbeef;
10225 key = 0xdeadbeef;
10226 ovl_iocp = (void *)0xdeadbeef;
10227 SetLastError(0xdeadbeef);
10228 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10229 ok(!ret, "got %d\n", ret);
10230 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
10231 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10232 ok(key == 0xdeadbeef, "got key %lu\n", key);
10233 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10235 CloseHandle(port);
10237 DestroyWindow(hwnd);
10240 static void iocp_async_read_thread(SOCKET src, SOCKET dst)
10242 struct wsa_async_select_info select_info;
10243 struct wsa_recv_info recv_info;
10244 HANDLE port, thread;
10245 WSAOVERLAPPED *ovl_iocp;
10246 int ret;
10247 char data[512];
10248 DWORD bytes, tid;
10249 ULONG_PTR key;
10250 HWND hwnd;
10251 MSG msg;
10253 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
10254 0, 0, 0, 0, NULL, NULL, 0, NULL);
10255 ok(hwnd != 0, "CreateWindowEx failed\n");
10257 select_info.sock = src;
10258 select_info.hwnd = hwnd;
10259 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
10260 ok(thread != 0, "CreateThread error %u\n", GetLastError());
10261 ret = WaitForSingleObject(thread, 10000);
10262 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
10264 Sleep(100);
10265 memset(&msg, 0, sizeof(msg));
10266 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10267 ok(ret, "got %d\n", ret);
10268 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
10269 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
10270 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
10271 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
10273 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
10274 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
10276 Sleep(100);
10277 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10278 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10280 memset(data, 0, sizeof(data));
10281 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
10282 recv_info.sock = src;
10283 recv_info.wsa_buf.len = sizeof(data);
10284 recv_info.wsa_buf.buf = data;
10285 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
10286 ok(thread != 0, "CreateThread error %u\n", GetLastError());
10287 ret = WaitForSingleObject(thread, 10000);
10288 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
10290 Sleep(100);
10291 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10292 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10294 bytes = 0xdeadbeef;
10295 key = 0xdeadbeef;
10296 ovl_iocp = (void *)0xdeadbeef;
10297 SetLastError(0xdeadbeef);
10298 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10299 ok(!ret, "got %d\n", ret);
10300 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */, "got %u\n", GetLastError());
10301 if (GetLastError() == WAIT_TIMEOUT)
10303 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10304 ok(key == 0xdeadbeef, "got key %lu\n", key);
10305 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10307 else /* document XP behaviour */
10309 ok(bytes == 0, "got bytes %u\n", bytes);
10310 ok(key == 0x12345678, "got key %#lx\n", key);
10311 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
10312 if (ovl_iocp)
10314 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
10315 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
10319 Sleep(100);
10320 memset(&msg, 0, sizeof(msg));
10321 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10322 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10323 if (ret) /* document XP behaviour */
10325 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
10326 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
10327 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
10330 ret = send(dst, "Hello World!", 12, 0);
10331 ok(ret == 12, "send returned %d\n", ret);
10333 Sleep(100);
10334 memset(&msg, 0, sizeof(msg));
10335 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
10336 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
10337 if (ret) /* document XP behaviour */
10339 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
10340 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
10341 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
10342 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
10345 bytes = 0xdeadbeef;
10346 key = 0xdeadbeef;
10347 ovl_iocp = (void *)0xdeadbeef;
10348 SetLastError(0xdeadbeef);
10349 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
10350 ok(ret || broken(GetLastError() == WAIT_TIMEOUT) /* XP */, "got %u\n", GetLastError());
10351 if (ret)
10353 ok(bytes == 12, "got bytes %u\n", bytes);
10354 ok(key == 0x12345678, "got key %#lx\n", key);
10355 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
10356 if (ovl_iocp)
10358 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
10359 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
10360 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
10363 else /* document XP behaviour */
10365 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
10366 ok(key == 0xdeadbeef, "got key %lu\n", key);
10367 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
10370 CloseHandle(port);
10372 DestroyWindow(hwnd);
10375 static void test_iocp(void)
10377 SOCKET src, dst;
10378 int i;
10380 tcp_socketpair_ovl(&src, &dst);
10381 sync_read(src, dst);
10382 iocp_async_read(src, dst);
10383 closesocket(src);
10384 closesocket(dst);
10386 tcp_socketpair_ovl(&src, &dst);
10387 iocp_async_read_thread(src, dst);
10388 closesocket(src);
10389 closesocket(dst);
10391 for (i = 0; i <= 2; i++)
10393 tcp_socketpair_ovl(&src, &dst);
10394 iocp_async_read_closesocket(src, i);
10395 closesocket(dst);
10398 tcp_socketpair_ovl(&src, &dst);
10399 iocp_async_closesocket(src);
10400 closesocket(dst);
10402 tcp_socketpair_ovl(&src, &dst);
10403 iocp_async_read_thread_closesocket(src);
10404 closesocket(dst);
10407 static void test_WSCGetProviderInfo(void)
10409 int ret;
10410 int errcode;
10411 GUID provider = {};
10412 char info[1];
10413 size_t len = 0;
10415 if (!pWSCGetProviderInfo) {
10416 skip("WSCGetProviderInfo is not available.\n");
10417 return;
10420 ret = pWSCGetProviderInfo(NULL, -1, NULL, NULL, 0, NULL);
10421 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10423 errcode = 0xdeadbeef;
10424 ret = pWSCGetProviderInfo(NULL, ProviderInfoLspCategories, (PBYTE)&info, &len, 0, &errcode);
10425 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10426 ok(errcode == WSAEFAULT, "got %d, expected WSAEFAULT\n", errcode);
10428 errcode = 0xdeadbeef;
10429 ret = pWSCGetProviderInfo(&provider, -1, (PBYTE)&info, &len, 0, &errcode);
10430 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10431 ok(errcode == WSANO_RECOVERY, "got %d, expected WSANO_RECOVERY\n", errcode);
10433 errcode = 0xdeadbeef;
10434 ret = pWSCGetProviderInfo(&provider, ProviderInfoLspCategories, NULL, &len, 0, &errcode);
10435 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10436 ok(errcode == WSANO_RECOVERY, "got %d, expected WSANO_RECOVERY\n", errcode);
10438 errcode = 0xdeadbeef;
10439 ret = pWSCGetProviderInfo(&provider, ProviderInfoLspCategories, (PBYTE)&info, NULL, 0, &errcode);
10440 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10441 ok(errcode == WSANO_RECOVERY, "got %d, expected WSANO_RECOVERY\n", errcode);
10443 errcode = 0xdeadbeef;
10444 ret = pWSCGetProviderInfo(&provider, ProviderInfoLspCategories, (PBYTE)&info, &len, 0, &errcode);
10445 ok(ret == SOCKET_ERROR, "got %d, expected SOCKET_ERROR\n", ret);
10446 ok(errcode == WSANO_RECOVERY, "got %d, expected WSANO_RECOVERY\n", errcode);
10449 static void test_WSCGetProviderPath(void)
10451 GUID provider = {};
10452 WCHAR buffer[256];
10453 INT ret, err, len;
10455 ret = WSCGetProviderPath(NULL, NULL, NULL, NULL);
10456 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10458 ret = WSCGetProviderPath(&provider, NULL, NULL, NULL);
10459 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10461 ret = WSCGetProviderPath(NULL, buffer, NULL, NULL);
10462 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10464 len = -1;
10465 ret = WSCGetProviderPath(NULL, NULL, &len, NULL);
10466 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10467 ok(len == -1, "Got unexpected len %d.\n", len);
10469 err = 0;
10470 ret = WSCGetProviderPath(NULL, NULL, NULL, &err);
10471 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10472 ok(err == WSAEFAULT, "Got unexpected error %d.\n", err);
10474 err = 0;
10475 ret = WSCGetProviderPath(&provider, NULL, NULL, &err);
10476 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10477 ok(err == WSAEFAULT, "Got unexpected error %d.\n", err);
10479 err = 0;
10480 len = -1;
10481 ret = WSCGetProviderPath(&provider, NULL, &len, &err);
10482 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10483 ok(err == WSAEINVAL, "Got unexpected error %d.\n", err);
10484 ok(len == -1, "Got unexpected len %d.\n", len);
10486 err = 0;
10487 len = 256;
10488 ret = WSCGetProviderPath(&provider, NULL, &len, &err);
10489 todo_wine ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10490 todo_wine ok(err == WSAEINVAL, "Got unexpected error %d.\n", err);
10491 ok(len == 256, "Got unexpected len %d.\n", len);
10493 /* Valid pointers and length but invalid GUID */
10494 err = 0;
10495 len = 256;
10496 ret = WSCGetProviderPath(&provider, buffer, &len, &err);
10497 todo_wine ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10498 todo_wine ok(err == WSAEINVAL, "Got unexpected error %d.\n", err);
10499 ok(len == 256, "Got unexpected len %d.\n", len);
10502 START_TEST( sock )
10504 int i;
10506 /* Leave these tests at the beginning. They depend on WSAStartup not having been
10507 * called, which is done by Init() below. */
10508 test_WithoutWSAStartup();
10509 test_WithWSAStartup();
10511 Init();
10513 test_inet_ntoa();
10514 test_inet_pton();
10515 test_set_getsockopt();
10516 test_so_reuseaddr();
10517 test_ip_pktinfo();
10518 test_extendedSocketOptions();
10520 for (i = 0; i < ARRAY_SIZE(tests); i++)
10521 do_test(&tests[i]);
10523 test_UDP();
10525 test_getservbyname();
10526 test_WSASocket();
10527 test_WSADuplicateSocket();
10528 test_WSAEnumNetworkEvents();
10529 test_WSAAddressToString();
10530 test_WSAStringToAddress();
10532 test_errors();
10533 test_listen();
10534 test_select();
10535 test_accept();
10536 test_getpeername();
10537 test_getsockname();
10538 test_inet_addr();
10539 test_addr_to_print();
10540 test_ioctlsocket();
10541 test_dns();
10542 test_gethostbyname();
10543 test_gethostbyname_hack();
10544 test_gethostname();
10546 test_WSASendMsg();
10547 test_WSASendTo();
10548 test_WSARecv();
10549 test_WSAPoll();
10550 test_write_watch();
10551 test_iocp();
10553 test_events(0);
10554 test_events(1);
10556 test_ipv6only();
10557 test_TransmitFile();
10558 test_GetAddrInfoW();
10559 test_GetAddrInfoExW();
10560 test_getaddrinfo();
10561 test_AcceptEx();
10562 test_ConnectEx();
10563 test_DisconnectEx();
10565 test_sioRoutingInterfaceQuery();
10566 test_sioAddressListChange();
10568 test_WSALookupService();
10569 test_WSAEnumNameSpaceProvidersA();
10570 test_WSAEnumNameSpaceProvidersW();
10572 test_WSAAsyncGetServByPort();
10573 test_WSAAsyncGetServByName();
10575 test_completion_port();
10576 test_address_list_query();
10578 test_WSCGetProviderInfo();
10579 test_WSCGetProviderPath();
10581 /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */
10582 test_send();
10583 test_synchronous_WSAIoctl();
10585 Exit();