1 /*
2 * Unit test suite for winsock functions
3 *
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
7 * Copyright 2017 Dmitry Timoshkov
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #ifdef __REACTOS__
25 #undef _WIN32_WINNT
26 #define _WIN32_WINNT 0x0600
27 #endif
28
29 #include <ntstatus.h>
30 #define WIN32_NO_STATUS
31 #include <winsock2.h>
32 #include <windows.h>
33 #include <winternl.h>
34 #include <ws2tcpip.h>
35 #include <wsipx.h>
36 #include <wsnwlink.h>
37 #include <mswsock.h>
38 #include <mstcpip.h>
39 #include <iphlpapi.h>
40 #include <stdio.h>
41 #include "wine/test.h"
42
43 // ReactOS: Wine has this in mstcpip.h, but it doesn't belong there
44 #define WSA_CMSG_ALIGN(len) (((len) + sizeof(SIZE_T) - 1) & ~(sizeof(SIZE_T) - 1))
45
46 #define MAX_CLIENTS 4 /* Max number of clients */
47 #define FIRST_CHAR 'A' /* First character in transferred pattern */
48 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
49 #define BIND_TRIES 6 /* Number of bind() attempts */
50 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
51 after server initialization, if something hangs */
52
53 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
54
55 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
56 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
57
58 #define SERVERIP "127.0.0.1" /* IP to bind to */
59 #define SERVERPORT 9374 /* Port number to bind to */
60
61 #define wsa_ok(op, cond, msg) \
62 do { \
63 int tmp, err = 0; \
64 tmp = op; \
65 if ( !(cond tmp) ) err = WSAGetLastError(); \
66 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
67 } while (0);
68
69 #define make_keepalive(k, enable, time, interval) \
70 k.onoff = enable; \
71 k.keepalivetime = time; \
72 k.keepaliveinterval = interval;
73
74 /* Function pointers */
75 static void (WINAPI *pfreeaddrinfo)(struct addrinfo *);
76 static int (WINAPI *pgetaddrinfo)(LPCSTR,LPCSTR,const struct addrinfo *,struct addrinfo **);
77 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW);
78 static void (WINAPI *pFreeAddrInfoExW)(ADDRINFOEXW *ai);
79 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *);
80 static int (WINAPI *pGetAddrInfoExW)(const WCHAR *name, const WCHAR *servname, DWORD namespace,
81 GUID *namespace_id, const ADDRINFOEXW *hints, ADDRINFOEXW **result,
82 struct timeval *timeout, OVERLAPPED *overlapped,
83 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine, HANDLE *handle);
84 static int (WINAPI *pGetAddrInfoExOverlappedResult)(OVERLAPPED *overlapped);
85 static PCSTR (WINAPI *p_inet_ntop)(INT,LPVOID,LPSTR,ULONG);
86 static PCWSTR(WINAPI *pInetNtopW)(INT,LPVOID,LPWSTR,ULONG);
87 static int (WINAPI *p_inet_pton)(INT,LPCSTR,LPVOID);
88 static int (WINAPI *pInetPtonW)(INT,LPWSTR,LPVOID);
89 static int (WINAPI *pWSALookupServiceBeginW)(LPWSAQUERYSETW,DWORD,LPHANDLE);
90 static int (WINAPI *pWSALookupServiceEnd)(HANDLE);
91 static int (WINAPI *pWSALookupServiceNextW)(HANDLE,DWORD,LPDWORD,LPWSAQUERYSETW);
92 static int (WINAPI *pWSAEnumNameSpaceProvidersA)(LPDWORD,LPWSANAMESPACE_INFOA);
93 static int (WINAPI *pWSAEnumNameSpaceProvidersW)(LPDWORD,LPWSANAMESPACE_INFOW);
94 static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT);
95
96 /* Function pointers from ntdll */
97 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
98 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
99
100 /* Function pointers from iphlpapi */
101 static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG);
102 static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL);
103
104 /* Function pointers from ntdll */
105 static DWORD (WINAPI *pNtClose)(HANDLE);
106
107 /**************** Structs and typedefs ***************/
108
109 typedef struct thread_info
110 {
111 HANDLE thread;
112 DWORD id;
113 } thread_info;
114
115 /* Information in the server about open client connections */
116 typedef struct sock_info
117 {
118 SOCKET s;
119 struct sockaddr_in addr;
120 struct sockaddr_in peer;
121 char *buf;
122 int n_recvd;
123 int n_sent;
124 } sock_info;
125
126 /* Test parameters for both server & client */
127 typedef struct test_params
128 {
129 int sock_type;
130 int sock_prot;
131 const char *inet_addr;
132 short inet_port;
133 int chunk_size;
134 int n_chunks;
135 int n_clients;
136 } test_params;
137
138 /* server-specific test parameters */
139 typedef struct server_params
140 {
141 test_params *general;
142 DWORD sock_flags;
143 int buflen;
144 } server_params;
145
146 /* client-specific test parameters */
147 typedef struct client_params
148 {
149 test_params *general;
150 DWORD sock_flags;
151 int buflen;
152 } client_params;
153
154 /* This type combines all information for setting up a test scenario */
155 typedef struct test_setup
156 {
157 test_params general;
158 LPVOID srv;
159 server_params srv_params;
160 LPVOID clt;
161 client_params clt_params;
162 } test_setup;
163
164 /* Thread local storage for server */
165 typedef struct server_memory
166 {
167 SOCKET s;
168 struct sockaddr_in addr;
169 sock_info sock[MAX_CLIENTS];
170 } server_memory;
171
172 /* Thread local storage for client */
173 typedef struct client_memory
174 {
175 SOCKET s;
176 struct sockaddr_in addr;
177 char *send_buf;
178 char *recv_buf;
179 } client_memory;
180
181 /* SelectReadThread thread parameters */
182 typedef struct select_thread_params
183 {
184 SOCKET s;
185 BOOL ReadKilled;
186 } select_thread_params;
187
188 /* Tests used in both getaddrinfo and GetAddrInfoW */
189 static const struct addr_hint_tests
190 {
191 int family, socktype, protocol;
192 DWORD error;
193 } hinttests[] = {
194 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0 },
195 {AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP, 0 },
196 {AF_UNSPEC, SOCK_STREAM, IPPROTO_IPV6,0 },
197 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_TCP, 0 },
198 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0 },
199 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_IPV6,0 },
200 {AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 },
201 {AF_INET, SOCK_STREAM, IPPROTO_UDP, 0 },
202 {AF_INET, SOCK_STREAM, IPPROTO_IPV6,0 },
203 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, 0 },
204 {AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0 },
205 {AF_INET, SOCK_DGRAM, IPPROTO_IPV6,0 },
206 {AF_UNSPEC, 0, IPPROTO_TCP, 0 },
207 {AF_UNSPEC, 0, IPPROTO_UDP, 0 },
208 {AF_UNSPEC, 0, IPPROTO_IPV6,0 },
209 {AF_UNSPEC, SOCK_STREAM, 0, 0 },
210 {AF_UNSPEC, SOCK_DGRAM, 0, 0 },
211 {AF_INET, 0, IPPROTO_TCP, 0 },
212 {AF_INET, 0, IPPROTO_UDP, 0 },
213 {AF_INET, 0, IPPROTO_IPV6,0 },
214 {AF_INET, SOCK_STREAM, 0, 0 },
215 {AF_INET, SOCK_DGRAM, 0, 0 },
216 {AF_UNSPEC, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
217 {AF_UNSPEC, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
218 {AF_UNSPEC, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
219 {AF_INET, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
220 {AF_INET, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
221 {AF_INET, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
222 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
223 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
224 {AF_INET, SOCK_DGRAM, 999, 0 },
225 {AF_INET, SOCK_DGRAM, 999, 0 },
226 };
227
228 /**************** Static variables ***************/
229
230 static DWORD tls; /* Thread local storage index */
231 static HANDLE thread[1+MAX_CLIENTS];
232 static DWORD thread_id[1+MAX_CLIENTS];
233 static HANDLE server_ready;
234 static HANDLE client_ready[MAX_CLIENTS];
235 static int client_id;
236
237 /**************** General utility functions ***************/
238
239 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len);
240 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock);
241
tcp_socketpair(SOCKET * src,SOCKET * dst)242 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
243 {
244 SOCKET server = INVALID_SOCKET;
245 struct sockaddr_in addr;
246 int len;
247 int ret;
248
249 *src = INVALID_SOCKET;
250 *dst = INVALID_SOCKET;
251
252 *src = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
253 if (*src == INVALID_SOCKET)
254 goto end;
255
256 server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
257 if (server == INVALID_SOCKET)
258 goto end;
259
260 memset(&addr, 0, sizeof(addr));
261 addr.sin_family = AF_INET;
262 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
263 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
264 if (ret != 0)
265 goto end;
266
267 len = sizeof(addr);
268 ret = getsockname(server, (struct sockaddr*)&addr, &len);
269 if (ret != 0)
270 goto end;
271
272 ret = listen(server, 1);
273 if (ret != 0)
274 goto end;
275
276 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
277 if (ret != 0)
278 goto end;
279
280 len = sizeof(addr);
281 *dst = accept(server, (struct sockaddr*)&addr, &len);
282
283 end:
284 if (server != INVALID_SOCKET)
285 closesocket(server);
286 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
287 return 0;
288 closesocket(*src);
289 closesocket(*dst);
290 return -1;
291 }
292
tcp_socketpair_ovl(SOCKET * src,SOCKET * dst)293 static int tcp_socketpair_ovl(SOCKET *src, SOCKET *dst)
294 {
295 SOCKET server = INVALID_SOCKET;
296 struct sockaddr_in addr;
297 int len, ret;
298
299 *src = INVALID_SOCKET;
300 *dst = INVALID_SOCKET;
301
302 *src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
303 if (*src == INVALID_SOCKET)
304 goto end;
305
306 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
307 if (server == INVALID_SOCKET)
308 goto end;
309
310 memset(&addr, 0, sizeof(addr));
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
313 ret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
314 if (ret != 0)
315 goto end;
316
317 len = sizeof(addr);
318 ret = getsockname(server, (struct sockaddr *)&addr, &len);
319 if (ret != 0)
320 goto end;
321
322 ret = listen(server, 1);
323 if (ret != 0)
324 goto end;
325
326 ret = connect(*src, (struct sockaddr *)&addr, sizeof(addr));
327 if (ret != 0)
328 goto end;
329
330 len = sizeof(addr);
331 *dst = accept(server, (struct sockaddr *)&addr, &len);
332
333 end:
334 if (server != INVALID_SOCKET)
335 closesocket(server);
336 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
337 return 0;
338 closesocket(*src);
339 closesocket(*dst);
340 return -1;
341 }
342
set_so_opentype(BOOL overlapped)343 static void set_so_opentype ( BOOL overlapped )
344 {
345 int optval = !overlapped, newval, len = sizeof (int);
346
347 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
348 (LPVOID) &optval, sizeof (optval) ) == 0,
349 "setting SO_OPENTYPE failed\n" );
350 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
351 (LPVOID) &newval, &len ) == 0,
352 "getting SO_OPENTYPE failed\n" );
353 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
354 }
355
set_blocking(SOCKET s,BOOL blocking)356 static int set_blocking ( SOCKET s, BOOL blocking )
357 {
358 u_long val = !blocking;
359 return ioctlsocket ( s, FIONBIO, &val );
360 }
361
fill_buffer(char * buf,int chunk_size,int n_chunks)362 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
363 {
364 char c, *p;
365 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
366 memset ( p, c, chunk_size );
367 }
368
test_buffer(char * buf,int chunk_size,int n_chunks)369 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
370 {
371 char c, *p;
372 int i;
373 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
374 {
375 for ( i = 0; i < chunk_size; i++ )
376 if ( p[i] != c ) return i;
377 }
378 return -1;
379 }
380
381 /*
382 * This routine is called when a client / server does not expect any more data,
383 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
384 */
read_zero_bytes(SOCKET s)385 static void read_zero_bytes ( SOCKET s )
386 {
387 char buf[256];
388 int tmp, n = 0;
389 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
390 n += tmp;
391 ok ( n <= 0, "garbage data received: %d bytes\n", n );
392 }
393
do_synchronous_send(SOCKET s,char * buf,int buflen,int flags,int sendlen)394 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen )
395 {
396 char* last = buf + buflen, *p;
397 int n = 1;
398 for ( p = buf; n > 0 && p < last; )
399 {
400 n = send ( s, p, min ( sendlen, last - p ), flags );
401 if (n > 0) p += n;
402 }
403 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
404 return p - buf;
405 }
406
do_synchronous_recv(SOCKET s,char * buf,int buflen,int flags,int recvlen)407 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen )
408 {
409 char* last = buf + buflen, *p;
410 int n = 1;
411 for ( p = buf; n > 0 && p < last; )
412 {
413 n = recv ( s, p, min ( recvlen, last - p ), flags );
414 if (n > 0) p += n;
415 }
416 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
417 return p - buf;
418 }
419
do_synchronous_recvfrom(SOCKET s,char * buf,int buflen,int flags,struct sockaddr * from,int * fromlen,int recvlen)420 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen )
421 {
422 char* last = buf + buflen, *p;
423 int n = 1;
424 for ( p = buf; n > 0 && p < last; )
425 {
426 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen );
427 if (n > 0) p += n;
428 }
429 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
430 return p - buf;
431 }
432
433 /*
434 * Call this routine right after thread startup.
435 * SO_OPENTYPE must by 0, regardless what the server did.
436 */
check_so_opentype(void)437 static void check_so_opentype (void)
438 {
439 int tmp = 1, len;
440 len = sizeof (tmp);
441 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
442 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
443 }
444
compare_addrinfo(ADDRINFO * a,ADDRINFO * b)445 static void compare_addrinfo (ADDRINFO *a, ADDRINFO *b)
446 {
447 for (; a && b ; a = a->ai_next, b = b->ai_next)
448 {
449 ok(a->ai_flags == b->ai_flags,
450 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
451 ok(a->ai_family == b->ai_family,
452 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
453 ok(a->ai_socktype == b->ai_socktype,
454 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
455 ok(a->ai_protocol == b->ai_protocol,
456 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
457 ok(a->ai_addrlen == b->ai_addrlen,
458 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
459 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
460 "Wrong address data\n");
461 if (a->ai_canonname && b->ai_canonname)
462 {
463 ok(!strcmp(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
464 a->ai_canonname, b->ai_canonname);
465 }
466 else
467 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
468 a->ai_canonname, b->ai_canonname);
469 }
470 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
471 }
472
compare_addrinfow(ADDRINFOW * a,ADDRINFOW * b)473 static void compare_addrinfow (ADDRINFOW *a, ADDRINFOW *b)
474 {
475 for (; a && b ; a = a->ai_next, b = b->ai_next)
476 {
477 ok(a->ai_flags == b->ai_flags,
478 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
479 ok(a->ai_family == b->ai_family,
480 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
481 ok(a->ai_socktype == b->ai_socktype,
482 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
483 ok(a->ai_protocol == b->ai_protocol,
484 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
485 ok(a->ai_addrlen == b->ai_addrlen,
486 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
487 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
488 "Wrong address data\n");
489 if (a->ai_canonname && b->ai_canonname)
490 {
491 ok(!lstrcmpW(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
492 wine_dbgstr_w(a->ai_canonname), wine_dbgstr_w(b->ai_canonname));
493 }
494 else
495 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
496 a->ai_canonname, b->ai_canonname);
497 }
498 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
499 }
500
501 /**************** Server utility functions ***************/
502
503 /*
504 * Even if we have closed our server socket cleanly,
505 * the OS may mark the address "in use" for some time -
506 * this happens with native Linux apps, too.
507 */
do_bind(SOCKET s,struct sockaddr * addr,int addrlen)508 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
509 {
510 int err, wsaerr = 0, n_try = BIND_TRIES;
511
512 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
513 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
514 n_try-- >= 0)
515 {
516 trace ( "address in use, waiting ...\n" );
517 Sleep ( 1000 * BIND_SLEEP );
518 }
519 ok ( err == 0, "failed to bind: %d\n", wsaerr );
520 }
521
server_start(server_params * par)522 static void server_start ( server_params *par )
523 {
524 int i;
525 test_params *gen = par->general;
526 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
527
528 TlsSetValue ( tls, mem );
529 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
530 NULL, 0, par->sock_flags );
531 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
532
533 mem->addr.sin_family = AF_INET;
534 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
535 mem->addr.sin_port = htons ( gen->inet_port );
536
537 for (i = 0; i < MAX_CLIENTS; i++)
538 {
539 mem->sock[i].s = INVALID_SOCKET;
540 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
541 mem->sock[i].n_recvd = 0;
542 mem->sock[i].n_sent = 0;
543 }
544
545 if ( gen->sock_type == SOCK_STREAM )
546 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
547 }
548
server_stop(void)549 static void server_stop (void)
550 {
551 int i;
552 server_memory *mem = TlsGetValue ( tls );
553
554 for (i = 0; i < MAX_CLIENTS; i++ )
555 {
556 LocalFree ( mem->sock[i].buf );
557 if ( mem->sock[i].s != INVALID_SOCKET )
558 closesocket ( mem->sock[i].s );
559 }
560 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
561 LocalFree ( mem );
562 ExitThread ( GetCurrentThreadId () );
563 }
564
565 /**************** Client utilitiy functions ***************/
566
client_start(client_params * par)567 static void client_start ( client_params *par )
568 {
569 test_params *gen = par->general;
570 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
571
572 TlsSetValue ( tls, mem );
573
574 WaitForSingleObject ( server_ready, INFINITE );
575
576 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
577 NULL, 0, par->sock_flags );
578
579 mem->addr.sin_family = AF_INET;
580 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
581 mem->addr.sin_port = htons ( gen->inet_port );
582
583 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
584
585 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
586 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
587 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
588
589 SetEvent ( client_ready[client_id] );
590 /* Wait for the other clients to come up */
591 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
592 }
593
client_stop(void)594 static void client_stop (void)
595 {
596 client_memory *mem = TlsGetValue ( tls );
597 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
598 LocalFree ( mem->send_buf );
599 LocalFree ( mem );
600 ExitThread(0);
601 }
602
603 /**************** Servers ***************/
604
605 /*
606 * simple_server: A very basic server doing synchronous IO.
607 */
simple_server(server_params * par)608 static VOID WINAPI simple_server ( server_params *par )
609 {
610 test_params *gen = par->general;
611 server_memory *mem;
612 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
613 id = GetCurrentThreadId();
614
615 trace ( "simple_server (%x) starting\n", id );
616
617 set_so_opentype ( FALSE ); /* non-overlapped */
618 server_start ( par );
619 mem = TlsGetValue ( tls );
620
621 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
622 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
623
624 trace ( "simple_server (%x) ready\n", id );
625 SetEvent ( server_ready ); /* notify clients */
626
627 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
628 {
629 trace ( "simple_server (%x): waiting for client\n", id );
630
631 /* accept a single connection */
632 tmp = sizeof ( mem->sock[0].peer );
633 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
634 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
635
636 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
637 "simple_server (%x): strange peer address\n", id );
638
639 /* Receive data & check it */
640 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
641 ok ( n_recvd == n_expected,
642 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
643 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
644 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
645
646 /* Echo data back */
647 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
648 ok ( n_sent == n_expected,
649 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
650
651 /* cleanup */
652 read_zero_bytes ( mem->sock[0].s );
653 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
654 mem->sock[0].s = INVALID_SOCKET;
655 }
656
657 trace ( "simple_server (%x) exiting\n", id );
658 server_stop ();
659 }
660
661 /*
662 * oob_server: A very basic server receiving out-of-band data.
663 */
oob_server(server_params * par)664 static VOID WINAPI oob_server ( server_params *par )
665 {
666 test_params *gen = par->general;
667 server_memory *mem;
668 u_long atmark = 0;
669 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
670 id = GetCurrentThreadId();
671
672 trace ( "oob_server (%x) starting\n", id );
673
674 set_so_opentype ( FALSE ); /* non-overlapped */
675 server_start ( par );
676 mem = TlsGetValue ( tls );
677
678 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
679 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
680
681 trace ( "oob_server (%x) ready\n", id );
682 SetEvent ( server_ready ); /* notify clients */
683
684 trace ( "oob_server (%x): waiting for client\n", id );
685
686 /* accept a single connection */
687 tmp = sizeof ( mem->sock[0].peer );
688 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
689 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
690
691 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
692 "oob_server (%x): strange peer address\n", id );
693
694 /* check initial atmark state */
695 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
696 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
697
698 /* Receive normal data */
699 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
700 ok ( n_recvd == n_expected,
701 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
702 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
703 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos);
704
705 /* check atmark state */
706 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
707 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
708
709 /* Echo data back */
710 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
711 ok ( n_sent == n_expected,
712 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
713
714 /* Receive a part of the out-of-band data and print atmark state */
715 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen );
716 ok ( n_recvd == 8,
717 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
718 n_expected -= 8;
719
720 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
721 trace( "oob_server (%x): %s the OOB mark: %i\n", id, atmark == 1 ? "not at" : "at", atmark );
722
723 /* Receive the rest of the out-of-band data and check atmark state */
724 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
725
726 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
727 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
728
729 /* cleanup */
730 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
731 mem->sock[0].s = INVALID_SOCKET;
732
733 trace ( "oob_server (%x) exiting\n", id );
734 server_stop ();
735 }
736
737 /*
738 * select_server: A non-blocking server.
739 */
select_server(server_params * par)740 static VOID WINAPI select_server ( server_params *par )
741 {
742 test_params *gen = par->general;
743 server_memory *mem;
744 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
745 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
746 n_set, delta, n_ready;
747 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
748 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
749
750 trace ( "select_server (%x) starting\n", id );
751
752 set_so_opentype ( FALSE ); /* non-overlapped */
753 server_start ( par );
754 mem = TlsGetValue ( tls );
755
756 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
757 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
758
759 trace ( "select_server (%x) ready\n", id );
760 SetEvent ( server_ready ); /* notify clients */
761
762 FD_ZERO ( &fds_openrecv );
763 FD_ZERO ( &fds_recv );
764 FD_ZERO ( &fds_send );
765 FD_ZERO ( &fds_opensend );
766
767 FD_SET ( mem->s, &fds_openrecv );
768
769 while(1)
770 {
771 fds_recv = fds_openrecv;
772 fds_send = fds_opensend;
773
774 n_set = 0;
775
776 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
777 "select_server (%x): select() failed: %d\n" );
778
779 /* check for incoming requests */
780 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
781 n_set += 1;
782
783 trace ( "select_server (%x): accepting client connection\n", id );
784
785 /* accept a single connection */
786 tmp = sizeof ( mem->sock[n_connections].peer );
787 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
788 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
789
790 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
791 "select_server (%x): strange peer address\n", id );
792
793 /* add to list of open connections */
794 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
795 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
796
797 n_connections++;
798 }
799
800 /* handle open requests */
801
802 for ( i = 0; i < n_connections; i++ )
803 {
804 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
805 n_set += 1;
806
807 if ( mem->sock[i].n_recvd < n_expected ) {
808 /* Receive data & check it */
809 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 );
810 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
811 mem->sock[i].n_recvd += n_recvd;
812
813 if ( mem->sock[i].n_recvd == n_expected ) {
814 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
815 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
816 FD_CLR ( mem->sock[i].s, &fds_openrecv );
817 }
818
819 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
820 }
821 }
822
823 /* only echo back what we've received */
824 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
825
826 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
827 n_set += 1;
828
829 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
830 /* Echo data back */
831 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
832 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
833 mem->sock[i].n_sent += n_sent;
834
835 if ( mem->sock[i].n_sent == n_expected ) {
836 FD_CLR ( mem->sock[i].s, &fds_opensend );
837 }
838
839 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
840 }
841 }
842 }
843
844 /* check that select returned the correct number of ready sockets */
845 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
846
847 /* check if all clients are done */
848 if ( ( fds_opensend.fd_count == 0 )
849 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
850 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
851 break;
852 }
853 }
854
855 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
856 {
857 /* cleanup */
858 read_zero_bytes ( mem->sock[i].s );
859 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
860 mem->sock[i].s = INVALID_SOCKET;
861 }
862
863 trace ( "select_server (%x) exiting\n", id );
864 server_stop ();
865 }
866
867 /**************** Clients ***************/
868
869 /*
870 * simple_client: A very basic client doing synchronous IO.
871 */
simple_client(client_params * par)872 static VOID WINAPI simple_client ( client_params *par )
873 {
874 test_params *gen = par->general;
875 client_memory *mem;
876 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
877
878 id = GetCurrentThreadId();
879 trace ( "simple_client (%x): starting\n", id );
880 /* wait here because we want to call set_so_opentype before creating a socket */
881 WaitForSingleObject ( server_ready, INFINITE );
882 trace ( "simple_client (%x): server ready\n", id );
883
884 check_so_opentype ();
885 set_so_opentype ( FALSE ); /* non-overlapped */
886 client_start ( par );
887 mem = TlsGetValue ( tls );
888
889 /* Connect */
890 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
891 0 ==, "simple_client (%x): connect error: %d\n" );
892 ok ( set_blocking ( mem->s, TRUE ) == 0,
893 "simple_client (%x): failed to set blocking mode\n", id );
894 trace ( "simple_client (%x) connected\n", id );
895
896 /* send data to server */
897 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
898 ok ( n_sent == n_expected,
899 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
900
901 /* shutdown send direction */
902 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
903
904 /* Receive data echoed back & check it */
905 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
906 ok ( n_recvd == n_expected,
907 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
908
909 /* check data */
910 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
911 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
912
913 /* cleanup */
914 read_zero_bytes ( mem->s );
915 trace ( "simple_client (%x) exiting\n", id );
916 client_stop ();
917 }
918
919 /*
920 * oob_client: A very basic client sending out-of-band data.
921 */
oob_client(client_params * par)922 static VOID WINAPI oob_client ( client_params *par )
923 {
924 test_params *gen = par->general;
925 client_memory *mem;
926 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
927
928 id = GetCurrentThreadId();
929 trace ( "oob_client (%x): starting\n", id );
930 /* wait here because we want to call set_so_opentype before creating a socket */
931 WaitForSingleObject ( server_ready, INFINITE );
932 trace ( "oob_client (%x): server ready\n", id );
933
934 check_so_opentype ();
935 set_so_opentype ( FALSE ); /* non-overlapped */
936 client_start ( par );
937 mem = TlsGetValue ( tls );
938
939 /* Connect */
940 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
941 0 ==, "oob_client (%x): connect error: %d\n" );
942 ok ( set_blocking ( mem->s, TRUE ) == 0,
943 "oob_client (%x): failed to set blocking mode\n", id );
944 trace ( "oob_client (%x) connected\n", id );
945
946 /* send data to server */
947 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
948 ok ( n_sent == n_expected,
949 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
950
951 /* Receive data echoed back & check it */
952 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
953 ok ( n_recvd == n_expected,
954 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
955 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
956 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
957
958 /* send out-of-band data to server */
959 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen );
960 ok ( n_sent == n_expected,
961 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
962
963 /* shutdown send direction */
964 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
965
966 /* cleanup */
967 read_zero_bytes ( mem->s );
968 trace ( "oob_client (%x) exiting\n", id );
969 client_stop ();
970 }
971
972 /*
973 * simple_mixed_client: mixing send and recvfrom
974 */
simple_mixed_client(client_params * par)975 static VOID WINAPI simple_mixed_client ( client_params *par )
976 {
977 test_params *gen = par->general;
978 client_memory *mem;
979 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
980 int fromLen = sizeof(mem->addr);
981 struct sockaddr test;
982
983 id = GetCurrentThreadId();
984 trace ( "simple_client (%x): starting\n", id );
985 /* wait here because we want to call set_so_opentype before creating a socket */
986 WaitForSingleObject ( server_ready, INFINITE );
987 trace ( "simple_client (%x): server ready\n", id );
988
989 check_so_opentype ();
990 set_so_opentype ( FALSE ); /* non-overlapped */
991 client_start ( par );
992 mem = TlsGetValue ( tls );
993
994 /* Connect */
995 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
996 0 ==, "simple_client (%x): connect error: %d\n" );
997 ok ( set_blocking ( mem->s, TRUE ) == 0,
998 "simple_client (%x): failed to set blocking mode\n", id );
999 trace ( "simple_client (%x) connected\n", id );
1000
1001 /* send data to server */
1002 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
1003 ok ( n_sent == n_expected,
1004 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
1005
1006 /* shutdown send direction */
1007 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
1008
1009 /* this shouldn't change, since lpFrom, is not updated on
1010 connection oriented sockets - exposed by bug 11640
1011 */
1012 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
1013
1014 /* Receive data echoed back & check it */
1015 n_recvd = do_synchronous_recvfrom ( mem->s,
1016 mem->recv_buf,
1017 n_expected,
1018 0,
1019 (struct sockaddr *)&test,
1020 &fromLen,
1021 par->buflen );
1022 ok ( n_recvd == n_expected,
1023 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
1024
1025 /* check that lpFrom was not updated */
1026 ok(0 ==
1027 strcmp(
1028 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
1029 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
1030
1031 /* check data */
1032 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1033 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
1034
1035 /* cleanup */
1036 read_zero_bytes ( mem->s );
1037 trace ( "simple_client (%x) exiting\n", id );
1038 client_stop ();
1039 }
1040
1041 /*
1042 * event_client: An event-driven client
1043 */
event_client(client_params * par)1044 static void WINAPI event_client ( client_params *par )
1045 {
1046 test_params *gen = par->general;
1047 client_memory *mem;
1048 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
1049 tmp, err, n;
1050 HANDLE event;
1051 WSANETWORKEVENTS wsa_events;
1052 char *send_last, *recv_last, *send_p, *recv_p;
1053 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
1054
1055 trace ( "event_client (%x): starting\n", id );
1056 client_start ( par );
1057 trace ( "event_client (%x): server ready\n", id );
1058
1059 mem = TlsGetValue ( tls );
1060
1061 /* Prepare event notification for connect, makes socket nonblocking */
1062 event = WSACreateEvent ();
1063 WSAEventSelect ( mem->s, event, FD_CONNECT );
1064 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
1065 if ( tmp != 0 ) {
1066 err = WSAGetLastError ();
1067 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
1068 tmp = WaitForSingleObject ( event, INFINITE );
1069 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
1070 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1071 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1072 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
1073 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
1074 if ( err ) goto out;
1075 }
1076
1077 trace ( "event_client (%x) connected\n", id );
1078
1079 WSAEventSelect ( mem->s, event, mask );
1080
1081 recv_p = mem->recv_buf;
1082 recv_last = mem->recv_buf + n_expected;
1083 send_p = mem->send_buf;
1084 send_last = mem->send_buf + n_expected;
1085
1086 while ( TRUE )
1087 {
1088 err = WaitForSingleObject ( event, INFINITE );
1089 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
1090
1091 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1092 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1093
1094 if ( wsa_events.lNetworkEvents & FD_WRITE )
1095 {
1096 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
1097 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
1098
1099 if ( err== 0 )
1100 do
1101 {
1102 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
1103 if ( n < 0 )
1104 {
1105 err = WSAGetLastError ();
1106 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
1107 }
1108 else
1109 send_p += n;
1110 }
1111 while ( n >= 0 && send_p < send_last );
1112
1113 if ( send_p == send_last )
1114 {
1115 trace ( "event_client (%x): all data sent - shutdown\n", id );
1116 shutdown ( mem->s, SD_SEND );
1117 mask &= ~FD_WRITE;
1118 WSAEventSelect ( mem->s, event, mask );
1119 }
1120 }
1121 if ( wsa_events.lNetworkEvents & FD_READ )
1122 {
1123 err = wsa_events.iErrorCode[ FD_READ_BIT ];
1124 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
1125 if ( err != 0 ) break;
1126
1127 /* First read must succeed */
1128 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1129 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
1130
1131 while ( n >= 0 ) {
1132 recv_p += n;
1133 if ( recv_p == recv_last )
1134 {
1135 mask &= ~FD_READ;
1136 trace ( "event_client (%x): all data received\n", id );
1137 WSAEventSelect ( mem->s, event, mask );
1138 break;
1139 }
1140 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1141 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
1142 ok ( 0, "event_client (%x): read error: %d\n", id, err );
1143
1144 }
1145 }
1146 if ( wsa_events.lNetworkEvents & FD_CLOSE )
1147 {
1148 trace ( "event_client (%x): close event\n", id );
1149 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
1150 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
1151 break;
1152 }
1153 }
1154
1155 n = send_p - mem->send_buf;
1156 ok ( send_p == send_last,
1157 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
1158 n = recv_p - mem->recv_buf;
1159 ok ( recv_p == recv_last,
1160 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
1161 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1162 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
1163
1164 out:
1165 WSACloseEvent ( event );
1166 trace ( "event_client (%x) exiting\n", id );
1167 client_stop ();
1168 }
1169
1170 /* Tests for WSAStartup */
test_WithoutWSAStartup(void)1171 static void test_WithoutWSAStartup(void)
1172 {
1173 DWORD err;
1174
1175 WSASetLastError(0xdeadbeef);
1176 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n");
1177 err = WSAGetLastError();
1178 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1179
1180 WSASetLastError(0xdeadbeef);
1181 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n");
1182 err = WSAGetLastError();
1183 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1184 }
1185
test_WithWSAStartup(void)1186 static void test_WithWSAStartup(void)
1187 {
1188 WSADATA data;
1189 WORD version = MAKEWORD( 2, 2 );
1190 INT res, socks, i, j;
1191 SOCKET sock;
1192 LPVOID ptr;
1193 struct
1194 {
1195 SOCKET src, dst, dup_src, dup_dst;
1196 } pairs[32];
1197 DWORD error;
1198
1199 res = WSAStartup( version, &data );
1200 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1201
1202 ptr = gethostbyname("localhost");
1203 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
1204
1205 /* Alloc some sockets to check if they are destroyed on WSACleanup */
1206 for (socks = 0; socks < sizeof(pairs) / sizeof(pairs[0]); socks++)
1207 {
1208 WSAPROTOCOL_INFOA info;
1209 if (tcp_socketpair(&pairs[socks].src, &pairs[socks].dst)) break;
1210
1211 memset(&info, 0, sizeof(info));
1212 ok(!WSADuplicateSocketA(pairs[socks].src, GetCurrentProcessId(), &info),
1213 "WSADuplicateSocketA should have worked\n");
1214 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0);
1215 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n");
1216
1217 memset(&info, 0, sizeof(info));
1218 ok(!WSADuplicateSocketA(pairs[socks].dst, GetCurrentProcessId(), &info),
1219 "WSADuplicateSocketA should have worked\n");
1220 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0);
1221 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n");
1222 }
1223
1224 res = send(pairs[0].src, "TEST", 4, 0);
1225 ok(res == 4, "send failed with error %d\n", WSAGetLastError());
1226
1227 WSACleanup();
1228
1229 res = WSAStartup( version, &data );
1230 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1231
1232 /* show that sockets are destroyed automatically after WSACleanup */
1233 SetLastError(0xdeadbeef);
1234 res = send(pairs[0].src, "TEST", 4, 0);
1235 error = WSAGetLastError();
1236 ok(res == SOCKET_ERROR, "send should have failed\n");
1237 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1238
1239 SetLastError(0xdeadbeef);
1240 res = send(pairs[0].dst, "TEST", 4, 0);
1241 error = WSAGetLastError();
1242 ok(res == SOCKET_ERROR, "send should have failed\n");
1243 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1244
1245 /* Check that all sockets were destroyed */
1246 for (i = 0; i < socks; i++)
1247 {
1248 for (j = 0; j < 4; j++)
1249 {
1250 struct sockaddr_in saddr;
1251 int size = sizeof(saddr);
1252 switch(j)
1253 {
1254 case 0: sock = pairs[i].src; break;
1255 case 1: sock = pairs[i].dup_src; break;
1256 case 2: sock = pairs[i].dst; break;
1257 case 3: sock = pairs[i].dup_dst; break;
1258 }
1259
1260 SetLastError(0xdeadbeef);
1261 res = getsockname(sock, (struct sockaddr *)&saddr, &size);
1262 error = WSAGetLastError();
1263 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i);
1264 todo_wine ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error);
1265 }
1266 }
1267
1268 /* While wine is not fixed, close all sockets manually */
1269 for (i = 0; i < socks; i++)
1270 {
1271 closesocket(pairs[i].src);
1272 closesocket(pairs[i].dst);
1273 closesocket(pairs[i].dup_src);
1274 closesocket(pairs[i].dup_dst);
1275 }
1276
1277 res = WSACleanup();
1278 ok(res == 0, "expected 0, got %d\n", res);
1279 WSASetLastError(0xdeadbeef);
1280 res = WSACleanup();
1281 error = WSAGetLastError();
1282 ok ( res == SOCKET_ERROR && error == WSANOTINITIALISED,
1283 "WSACleanup returned %d WSAGetLastError is %d\n", res, error);
1284 }
1285
1286 /**************** Main program utility functions ***************/
1287
Init(void)1288 static void Init (void)
1289 {
1290 WORD ver = MAKEWORD (2, 2);
1291 WSADATA data;
1292 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), hiphlpapi, ntdll;
1293
1294 pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo");
1295 pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo");
1296 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
1297 pFreeAddrInfoExW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoExW");
1298 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
1299 pGetAddrInfoExW = (void *)GetProcAddress(hws2_32, "GetAddrInfoExW");
1300 pGetAddrInfoExOverlappedResult = (void *)GetProcAddress(hws2_32, "GetAddrInfoExOverlappedResult");
1301 p_inet_ntop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1302 pInetNtopW = (void *)GetProcAddress(hws2_32, "InetNtopW");
1303 p_inet_pton = (void *)GetProcAddress(hws2_32, "inet_pton");
1304 pInetPtonW = (void *)GetProcAddress(hws2_32, "InetPtonW");
1305 pWSALookupServiceBeginW = (void *)GetProcAddress(hws2_32, "WSALookupServiceBeginW");
1306 pWSALookupServiceEnd = (void *)GetProcAddress(hws2_32, "WSALookupServiceEnd");
1307 pWSALookupServiceNextW = (void *)GetProcAddress(hws2_32, "WSALookupServiceNextW");
1308 pWSAEnumNameSpaceProvidersA = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersA");
1309 pWSAEnumNameSpaceProvidersW = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersW");
1310 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll");
1311
1312 hiphlpapi = LoadLibraryA("iphlpapi.dll");
1313 if (hiphlpapi)
1314 {
1315 pGetIpForwardTable = (void *)GetProcAddress(hiphlpapi, "GetIpForwardTable");
1316 pGetAdaptersInfo = (void *)GetProcAddress(hiphlpapi, "GetAdaptersInfo");
1317 }
1318
1319 ntdll = LoadLibraryA("ntdll.dll");
1320 if (ntdll)
1321 {
1322 pNtClose = (void *)GetProcAddress(ntdll, "NtClose");
1323 pNtSetInformationFile = (void *)GetProcAddress(ntdll, "NtSetInformationFile");
1324 pNtQueryInformationFile = (void *)GetProcAddress(ntdll, "NtQueryInformationFile");
1325 }
1326
1327 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1328 tls = TlsAlloc();
1329 }
1330
Exit(void)1331 static void Exit (void)
1332 {
1333 INT ret, err;
1334 TlsFree ( tls );
1335 ret = WSACleanup();
1336 err = WSAGetLastError();
1337 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1338 }
1339
StartServer(LPTHREAD_START_ROUTINE routine,test_params * general,server_params * par)1340 static void StartServer (LPTHREAD_START_ROUTINE routine,
1341 test_params *general, server_params *par)
1342 {
1343 par->general = general;
1344 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1345 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1346 }
1347
StartClients(LPTHREAD_START_ROUTINE routine,test_params * general,client_params * par)1348 static void StartClients (LPTHREAD_START_ROUTINE routine,
1349 test_params *general, client_params *par)
1350 {
1351 int i;
1352 par->general = general;
1353 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1354 {
1355 client_id = i - 1;
1356 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1357 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1358 /* Make sure the client is up and running */
1359 WaitForSingleObject ( client_ready[client_id], INFINITE );
1360 };
1361 }
1362
do_test(test_setup * test)1363 static void do_test( test_setup *test )
1364 {
1365 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1366 DWORD wait;
1367
1368 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1369 for (i = 0; i <= n; i++)
1370 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1371
1372 StartServer ( test->srv, &test->general, &test->srv_params );
1373 StartClients ( test->clt, &test->general, &test->clt_params );
1374 WaitForSingleObject ( server_ready, INFINITE );
1375
1376 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1377 ok ( wait <= WAIT_OBJECT_0 + n ,
1378 "some threads have not completed: %x\n", wait );
1379
1380 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
1381 {
1382 for (i = 0; i <= n; i++)
1383 {
1384 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
1385 {
1386 trace ("terminating thread %08x\n", thread_id[i]);
1387 TerminateThread ( thread [i], 0 );
1388 }
1389 }
1390 }
1391 CloseHandle ( server_ready );
1392 for (i = 0; i <= n; i++)
1393 CloseHandle ( client_ready[i] );
1394 }
1395
1396 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1397 /* optname = SO_LINGER */
1398 static const LINGER linger_testvals[] = {
1399 {0,0},
1400 {0,73},
1401 {1,0},
1402 {5,189}
1403 };
1404
1405 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1406 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1407 bug in the linux kernel (fixed in 2.6.8) */
1408 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1409
test_set_getsockopt(void)1410 static void test_set_getsockopt(void)
1411 {
1412 SOCKET s, s2;
1413 int i, err, lasterr;
1414 int timeout;
1415 LINGER lingval;
1416 int size;
1417 WSAPROTOCOL_INFOA infoA;
1418 WSAPROTOCOL_INFOW infoW;
1419 char providername[WSAPROTOCOL_LEN + 1];
1420 DWORD value;
1421 struct _prottest
1422 {
1423 int family, type, proto;
1424 } prottest[] = {
1425 {AF_INET, SOCK_STREAM, IPPROTO_TCP},
1426 {AF_INET, SOCK_DGRAM, IPPROTO_UDP},
1427 {AF_INET6, SOCK_STREAM, IPPROTO_TCP},
1428 {AF_INET6, SOCK_DGRAM, IPPROTO_UDP}
1429 };
1430 union _csspace
1431 {
1432 CSADDR_INFO cs;
1433 char space[128];
1434 } csinfoA, csinfoB;
1435
1436 s = socket(AF_INET, SOCK_STREAM, 0);
1437 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1438 if( s == INVALID_SOCKET) return;
1439 /* SO_RCVTIMEO */
1440 timeout = SOCKTIMEOUT1;
1441 size = sizeof(timeout);
1442 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1443 if( !err)
1444 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1445 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1446 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1447
1448 timeout = 0;
1449 size = sizeof(timeout);
1450 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1451 if( !err)
1452 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1453 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1454 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1455
1456 /* SO_SNDTIMEO */
1457 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1458 size = sizeof(timeout);
1459 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1460 if( !err)
1461 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1462 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1463 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1464
1465 /* SO_SNDBUF */
1466 value = 4096;
1467 size = sizeof(value);
1468 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size);
1469 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1470 value = 0xdeadbeef;
1471 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size);
1472 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1473 ok( value == 4096, "expected 4096, got %u\n", value );
1474
1475 /* SO_RCVBUF */
1476 value = 4096;
1477 size = sizeof(value);
1478 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size);
1479 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1480 value = 0xdeadbeef;
1481 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size);
1482 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1483 ok( value == 4096, "expected 4096, got %u\n", value );
1484
1485 /* SO_LINGER */
1486 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1487 size = sizeof(lingval);
1488 lingval = linger_testvals[i];
1489 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1490 if( !err)
1491 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1492 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1493 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1494 (lingval.l_linger == linger_testvals[i].l_linger ||
1495 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1496 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1497 lingval.l_onoff, lingval.l_linger,
1498 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1499 }
1500
1501 size = sizeof(lingval);
1502 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1503 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1504 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1505 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1506 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1507 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1508
1509 size = sizeof(BOOL);
1510 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1511 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1512 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1513 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1514 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1515 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1516
1517 /* Test for erroneously passing a value instead of a pointer as optval */
1518 size = sizeof(char);
1519 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1520 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1521 "instead of failing.\n");
1522 lasterr = WSAGetLastError();
1523 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1524 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1525 lasterr, WSAEFAULT);
1526
1527 /* SO_RCVTIMEO with invalid values for level */
1528 size = sizeof(timeout);
1529 timeout = SOCKTIMEOUT1;
1530 SetLastError(0xdeadbeef);
1531 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1532 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1533 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1534 err, WSAGetLastError());
1535
1536 timeout = SOCKTIMEOUT1;
1537 SetLastError(0xdeadbeef);
1538 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1539 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1540 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1541 err, WSAGetLastError());
1542
1543 /* Test SO_ERROR set/get */
1544 SetLastError(0xdeadbeef);
1545 i = 1234;
1546 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size);
1547 todo_wine
1548 ok( !err && !WSAGetLastError(),
1549 "got %d with %d (expected 0 with 0)\n",
1550 err, WSAGetLastError());
1551
1552 SetLastError(0xdeadbeef);
1553 i = 4321;
1554 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1555 todo_wine
1556 ok( !err && !WSAGetLastError(),
1557 "got %d with %d (expected 0 with 0)\n",
1558 err, WSAGetLastError());
1559 todo_wine
1560 ok (i == 1234, "got %d (expected 1234)\n", i);
1561
1562 /* Test invalid optlen */
1563 SetLastError(0xdeadbeef);
1564 size = 1;
1565 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1566 todo_wine
1567 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEFAULT),
1568 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1569 err, WSAGetLastError());
1570
1571 closesocket(s);
1572 /* Test with the closed socket */
1573 SetLastError(0xdeadbeef);
1574 size = sizeof(i);
1575 i = 1234;
1576 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1577 todo_wine
1578 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAENOTSOCK),
1579 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1580 err, WSAGetLastError());
1581 ok (i == 1234, "expected 1234, got %d\n", i);
1582
1583 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1584 s = socket(AF_INET, SOCK_DGRAM, 0);
1585 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1586 size = sizeof(i);
1587 i = 0x0000000a;
1588 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size);
1589 if (!err)
1590 {
1591 for (i = 0; i < 4; i++)
1592 {
1593 int k, j;
1594 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1595 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1);
1596 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1597 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size);
1598 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1599 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i];
1600 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k);
1601 }
1602 }
1603 else
1604 win_skip("IP_MULTICAST_TTL is unsupported\n");
1605 closesocket(s);
1606
1607 /* test SO_PROTOCOL_INFOA invalid parameters */
1608 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1609 "getsockopt should have failed\n");
1610 err = WSAGetLastError();
1611 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1612 size = sizeof(WSAPROTOCOL_INFOA);
1613 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1614 "getsockopt should have failed\n");
1615 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1616 err = WSAGetLastError();
1617 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1618 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1619 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1620 "getsockopt should have failed\n");
1621 err = WSAGetLastError();
1622 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1623 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, NULL),
1624 "getsockopt should have failed\n");
1625 err = WSAGetLastError();
1626 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1627 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, &size),
1628 "getsockopt should have failed\n");
1629 err = WSAGetLastError();
1630 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1631 size = sizeof(WSAPROTOCOL_INFOA) / 2;
1632 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1633 "getsockopt should have failed\n");
1634 err = WSAGetLastError();
1635 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1636 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1637 size = sizeof(WSAPROTOCOL_INFOA) * 2;
1638 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1639 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1640 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size);
1641
1642 closesocket(s);
1643
1644 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1645 for (i = 0; i < sizeof(prottest) / sizeof(prottest[0]); i++)
1646 {
1647 int k;
1648
1649 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto);
1650 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue;
1651
1652 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n",
1653 WSAGetLastError());
1654
1655 /* compare both A and W version */
1656 infoA.szProtocol[0] = 0;
1657 size = sizeof(WSAPROTOCOL_INFOA);
1658 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1659 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1660 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1661
1662 infoW.szProtocol[0] = 0;
1663 size = sizeof(WSAPROTOCOL_INFOW);
1664 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &infoW, &size);
1665 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1666 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size);
1667
1668 trace("provider name '%s', family %d, type %d, proto %d\n",
1669 infoA.szProtocol, prottest[i].family, prottest[i].type, prottest[i].proto);
1670
1671 ok(infoA.szProtocol[0], "WSAPROTOCOL_INFOA was not filled\n");
1672 ok(infoW.szProtocol[0], "WSAPROTOCOL_INFOW was not filled\n");
1673
1674 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1,
1675 providername, sizeof(providername), NULL, NULL);
1676 ok(!strcmp(infoA.szProtocol,providername),
1677 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername);
1678
1679 ok(!memcmp(&infoA, &infoW, FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol)),
1680 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1681
1682 /* Remove IF when WSAEnumProtocols support IPV6 data */
1683 todo_wine_if (prottest[i].family == AF_INET6)
1684 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n",
1685 prottest[i].family, infoA.iAddressFamily);
1686 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n",
1687 prottest[i].type, infoA.iSocketType);
1688 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n",
1689 prottest[i].proto, infoA.iProtocol);
1690
1691 /* IP_HDRINCL is supported only on SOCK_RAW but passed to SOCK_DGRAM by Impossible Creatures */
1692 size = sizeof(i);
1693 k = 1;
1694 SetLastError(0xdeadbeef);
1695 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1696 if (err == -1) /* >= Vista */
1697 {
1698 todo_wine {
1699 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1700 k = 99;
1701 SetLastError(0xdeadbeef);
1702 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1703 ok(err == -1, "Expected -1, got %d\n", err);
1704 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1705 ok(k == 99, "Expected 99, got %d\n", k);
1706
1707 size = sizeof(k);
1708 k = 0;
1709 SetLastError(0xdeadbeef);
1710 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1711 }
1712 ok(err == -1, "Expected -1, got %d\n", err);
1713 todo_wine {
1714 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1715 k = 99;
1716 SetLastError(0xdeadbeef);
1717 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1718 ok(err == -1, "Expected -1, got %d\n", err);
1719 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1720 ok(k == 99, "Expected 99, got %d\n", k);
1721 }
1722 }
1723 else /* <= 2003 the tests differ between TCP and UDP, UDP silently accepts */
1724 {
1725 SetLastError(0xdeadbeef);
1726 k = 99;
1727 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1728 if (prottest[i].type == SOCK_DGRAM)
1729 {
1730 ok(err == 0, "Expected 0, got %d\n", err);
1731 ok(k == 1, "Expected 1, got %d\n", k);
1732 }
1733 else
1734 {
1735 /* contratry to what we could expect the function returns error but k is changed */
1736 ok(err == -1, "Expected -1, got %d\n", err);
1737 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1738 ok(k == 0, "Expected 0, got %d\n", k);
1739 }
1740
1741 k = 0;
1742 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1743 ok(err == 0, "Expected 0, got %d\n", err);
1744
1745 k = 99;
1746 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1747 if (prottest[i].type == SOCK_DGRAM)
1748 {
1749 ok(err == 0, "Expected 0, got %d\n", err);
1750 ok(k == 0, "Expected 0, got %d\n", k);
1751 }
1752 else
1753 {
1754 /* contratry to what we could expect the function returns error but k is changed */
1755 ok(err == -1, "Expected -1, got %d\n", err);
1756 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1757 ok(k == 0, "Expected 0, got %d\n", k);
1758 }
1759 }
1760
1761 closesocket(s);
1762 }
1763
1764 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1765 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1766 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1767 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1768 ok(s2 != INVALID_SOCKET, "Failed to create socket\n");
1769
1770 SetLastError(0xdeadbeef);
1771 size = sizeof(csinfoA);
1772 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1773 if (!err)
1774 {
1775 struct sockaddr_in saddr;
1776 memset(&saddr, 0, sizeof(saddr));
1777 saddr.sin_family = AF_INET;
1778 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1779
1780 /* Socket is not bound, no information provided */
1781 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1782 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1783 /* Socket is not connected, no information provided */
1784 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1785 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1786
1787 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr));
1788 ok(!err, "Expected 0, got %d\n", err);
1789 size = sizeof(csinfoA);
1790 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1791 ok(!err, "Expected 0, got %d\n", err);
1792
1793 /* Socket is bound */
1794 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n");
1795 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n");
1796 /* Socket is not connected, no information provided */
1797 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1798 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1799
1800 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1801 ok(!err, "Expected 0, got %d\n", err);
1802 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1803 ok(!err, "Expected 0, got %d\n", err);
1804 err = listen(s2, 1);
1805 ok(!err, "Expected 0, got %d\n", err);
1806 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr));
1807 ok(!err, "Expected 0, got %d\n", err);
1808 size = sizeof(saddr);
1809 err = accept(s2, (struct sockaddr*)&saddr, &size);
1810 ok(err != INVALID_SOCKET, "Failed to accept socket\n");
1811 closesocket(s2);
1812 s2 = err;
1813
1814 size = sizeof(csinfoA);
1815 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1816 ok(!err, "Expected 0, got %d\n", err);
1817 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size);
1818 ok(!err, "Expected 0, got %d\n", err);
1819 ok(size == sizeof(csinfoA), "Got %d\n", size);
1820 size = sizeof(saddr);
1821 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1822 csinfoA.cs.LocalAddr.iSockaddrLength);
1823 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1824 csinfoA.cs.RemoteAddr.iSockaddrLength);
1825 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size),
1826 "Expected matching addresses\n");
1827 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size),
1828 "Expected matching addresses\n");
1829 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1830 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1831 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1832 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1833
1834 err = getpeername(s, (struct sockaddr *)&saddr, &size);
1835 ok(!err, "Expected 0, got %d\n", err);
1836 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1837 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1838 err = getpeername(s2, (struct sockaddr *)&saddr, &size);
1839 ok(!err, "Expected 0, got %d\n", err);
1840 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1841 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1842 err = getsockname(s, (struct sockaddr *)&saddr, &size);
1843 ok(!err, "Expected 0, got %d\n", err);
1844 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1845 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1846 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1847 ok(!err, "Expected 0, got %d\n", err);
1848 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1849 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1850
1851 SetLastError(0xdeadbeef);
1852 size = sizeof(CSADDR_INFO);
1853 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1854 ok(err, "Expected non-zero\n");
1855 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size);
1856 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1857
1858 /* At least for IPv4 the size is exactly 56 bytes */
1859 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs);
1860 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1861 ok(!err, "Expected 0, got %d\n", err);
1862 size--;
1863 SetLastError(0xdeadbeef);
1864 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1865 ok(err, "Expected non-zero\n");
1866 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1867 }
1868 else
1869 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1870
1871 closesocket(s);
1872 closesocket(s2);
1873
1874 for (i = 0; i < 2; i++)
1875 {
1876 int family, level;
1877
1878 if (i)
1879 {
1880 family = AF_INET6;
1881 level = IPPROTO_IPV6;
1882 }
1883 else
1884 {
1885 family = AF_INET;
1886 level = IPPROTO_IP;
1887 }
1888
1889 s = socket(family, SOCK_DGRAM, 0);
1890 if (s == INVALID_SOCKET && i)
1891 {
1892 skip("IPv6 is not supported\n");
1893 break;
1894 }
1895 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1896
1897 size = sizeof(value);
1898 value = 0xdead;
1899 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1900 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1901 ok(value == 0, "Expected 0, got %d\n", value);
1902
1903 size = sizeof(value);
1904 value = 1;
1905 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1906 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1907
1908 value = 0xdead;
1909 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1910 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1911 ok(value == 1, "Expected 1, got %d\n", value);
1912
1913 size = sizeof(value);
1914 value = 0xdead;
1915 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1916 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1917
1918 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1919 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1920 ok(value == 1, "Expected 1, got %d\n", value);
1921
1922 closesocket(s);
1923 }
1924 }
1925
test_so_reuseaddr(void)1926 static void test_so_reuseaddr(void)
1927 {
1928 struct sockaddr_in saddr;
1929 SOCKET s1,s2;
1930 unsigned int rc,reuse;
1931 int size;
1932 DWORD err;
1933
1934 saddr.sin_family = AF_INET;
1935 saddr.sin_port = htons(SERVERPORT+1);
1936 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1937
1938 s1=socket(AF_INET, SOCK_STREAM, 0);
1939 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1940 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1941 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1942
1943 s2=socket(AF_INET, SOCK_STREAM, 0);
1944 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1945
1946 reuse=0x1234;
1947 size=sizeof(reuse);
1948 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1949 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1950
1951 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1952 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1953
1954 reuse = 1;
1955 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1956 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1957
1958 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1959 * a port immediately after closing another socket on that port, so
1960 * basically following the BSD socket semantics here. */
1961 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1962 if(rc==0)
1963 {
1964 int s3=socket(AF_INET, SOCK_STREAM, 0), s4;
1965 trace("<= Win XP behavior of SO_REUSEADDR\n");
1966
1967 /* If we could bind again in the same port this is Windows version <= XP.
1968 * Lets test if we can really connect to one of them. */
1969 set_blocking(s1, FALSE);
1970 set_blocking(s2, FALSE);
1971 rc = listen(s1, 1);
1972 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1973 rc = listen(s2, 1);
1974 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1975 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr));
1976 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError());
1977
1978 /* the delivery of the connection is random so we need to try on both sockets */
1979 size = sizeof(saddr);
1980 s4 = accept(s1, (struct sockaddr*)&saddr, &size);
1981 if(s4 == INVALID_SOCKET)
1982 s4 = accept(s2, (struct sockaddr*)&saddr, &size);
1983 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n");
1984
1985 closesocket(s1);
1986 closesocket(s3);
1987 closesocket(s4);
1988 }
1989 else
1990 {
1991 trace(">= Win 2003 behavior of SO_REUSEADDR\n");
1992 err = WSAGetLastError();
1993 ok(err==WSAEACCES, "expected 10013, got %d\n", err);
1994
1995 closesocket(s1);
1996 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1997 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1998 }
1999
2000 closesocket(s2);
2001 }
2002
2003 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
2004
test_ip_pktinfo(void)2005 static void test_ip_pktinfo(void)
2006 {
2007 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
2008 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
2009 struct sockaddr_in s1addr, s2addr, s3addr;
2010 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
2011 LPFN_WSARECVMSG pWSARecvMsg = NULL;
2012 unsigned int rc, yes = 1;
2013 BOOL foundhdr;
2014 DWORD dwBytes, dwSize, dwFlags;
2015 socklen_t addrlen;
2016 WSACMSGHDR *cmsg;
2017 WSAOVERLAPPED ov;
2018 WSABUF iovec[1];
2019 SOCKET s1, s2;
2020 WSAMSG hdr;
2021 int i, err;
2022
2023 memset(&ov, 0, sizeof(ov));
2024 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2025 if (ov.hEvent == INVALID_HANDLE_VALUE)
2026 {
2027 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
2028 return;
2029 }
2030
2031 memset(&hdr, 0x00, sizeof(hdr));
2032 s1addr.sin_family = AF_INET;
2033 s1addr.sin_port = htons(0);
2034 /* Note: s1addr.sin_addr is set below */
2035 iovec[0].buf = recvbuf;
2036 iovec[0].len = sizeof(recvbuf);
2037 hdr.name = (struct sockaddr*)&s3addr;
2038 hdr.namelen = sizeof(s3addr);
2039 hdr.lpBuffers = &iovec[0];
2040 hdr.dwBufferCount = 1;
2041 hdr.Control.buf = pktbuf;
2042 /* Note: hdr.Control.len is set below */
2043 hdr.dwFlags = 0;
2044
2045 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++)
2046 {
2047 s1addr.sin_addr.s_addr = addresses[i];
2048
2049 /* Build "server" side socket */
2050 s1=socket(AF_INET, SOCK_DGRAM, 0);
2051 if (s1 == INVALID_SOCKET)
2052 {
2053 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
2054 goto cleanup;
2055 }
2056
2057 /* Obtain the WSARecvMsg function */
2058 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
2059 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
2060 if (!pWSARecvMsg)
2061 {
2062 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
2063 closesocket(s1);
2064 goto cleanup;
2065 }
2066
2067 /* Setup the server side socket */
2068 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
2069 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
2070 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
2071 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
2072
2073 /* Build "client" side socket */
2074 addrlen = sizeof(s2addr);
2075 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0)
2076 {
2077 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
2078 closesocket(s1);
2079 goto cleanup;
2080 }
2081 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
2082 s2=socket(AF_INET, SOCK_DGRAM, 0);
2083 if (s2 == INVALID_SOCKET)
2084 {
2085 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
2086 closesocket(s1);
2087 goto cleanup;
2088 }
2089
2090 /* Test an empty message header */
2091 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
2092 err=WSAGetLastError();
2093 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
2094
2095 /*
2096 * Send a packet from the client to the server and test for specifying
2097 * a short control header.
2098 */
2099 SetLastError(0xdeadbeef);
2100 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2101 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2102 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2103 hdr.Control.len = 1;
2104 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
2105 err=WSAGetLastError();
2106 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
2107 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
2108 hdr.dwFlags = 0; /* Reset flags */
2109
2110 /* Perform another short control header test, this time with an overlapped receive */
2111 hdr.Control.len = 1;
2112 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2113 err=WSAGetLastError();
2114 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2115 SetLastError(0xdeadbeef);
2116 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2117 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2118 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2119 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
2120 {
2121 skip("Server side did not receive packet, some tests skipped.\n");
2122 closesocket(s2);
2123 closesocket(s1);
2124 continue;
2125 }
2126 dwFlags = 0;
2127 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
2128 ok(dwFlags == 0,
2129 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
2130 ok(hdr.dwFlags == MSG_CTRUNC,
2131 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
2132 hdr.dwFlags = 0; /* Reset flags */
2133
2134 /*
2135 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
2136 * on the server end and check that the returned packet matches what was sent.
2137 */
2138 hdr.Control.len = sizeof(pktbuf);
2139 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2140 err=WSAGetLastError();
2141 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2142 ok(hdr.Control.len == sizeof(pktbuf),
2143 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
2144 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2145 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2146 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
2147 {
2148 skip("Server side did not receive packet, some tests skipped.\n");
2149 closesocket(s2);
2150 closesocket(s1);
2151 continue;
2152 }
2153 dwSize = 0;
2154 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
2155 ok(dwSize == sizeof(msg),
2156 "WSARecvMsg() buffer length does not match transmitted data!\n");
2157 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
2158 "WSARecvMsg() buffer does not match transmitted data!\n");
2159 ok(hdr.Control.len == IP_PKTINFO_LEN,
2160 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
2161
2162 /* Test for the expected IP_PKTINFO return information. */
2163 foundhdr = FALSE;
2164 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
2165 {
2166 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
2167 {
2168 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
2169
2170 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
2171 foundhdr = TRUE;
2172 }
2173 }
2174 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
2175
2176 closesocket(s2);
2177 closesocket(s1);
2178 }
2179
2180 cleanup:
2181 CloseHandle(ov.hEvent);
2182 }
2183
2184 /************* Array containing the tests to run **********/
2185
2186 #define STD_STREAM_SOCKET \
2187 SOCK_STREAM, \
2188 0, \
2189 SERVERIP, \
2190 SERVERPORT
2191
2192 static test_setup tests [] =
2193 {
2194 /* Test 0: synchronous client and server */
2195 {
2196 {
2197 STD_STREAM_SOCKET,
2198 2048,
2199 16,
2200 2
2201 },
2202 simple_server,
2203 {
2204 NULL,
2205 0,
2206 64
2207 },
2208 simple_client,
2209 {
2210 NULL,
2211 0,
2212 128
2213 }
2214 },
2215 /* Test 1: event-driven client, synchronous server */
2216 {
2217 {
2218 STD_STREAM_SOCKET,
2219 2048,
2220 16,
2221 2
2222 },
2223 simple_server,
2224 {
2225 NULL,
2226 0,
2227 64
2228 },
2229 event_client,
2230 {
2231 NULL,
2232 WSA_FLAG_OVERLAPPED,
2233 128
2234 }
2235 },
2236 /* Test 2: synchronous client, non-blocking server via select() */
2237 {
2238 {
2239 STD_STREAM_SOCKET,
2240 2048,
2241 16,
2242 2
2243 },
2244 select_server,
2245 {
2246 NULL,
2247 0,
2248 64
2249 },
2250 simple_client,
2251 {
2252 NULL,
2253 0,
2254 128
2255 }
2256 },
2257 /* Test 3: OOB client, OOB server */
2258 {
2259 {
2260 STD_STREAM_SOCKET,
2261 128,
2262 16,
2263 1
2264 },
2265 oob_server,
2266 {
2267 NULL,
2268 0,
2269 128
2270 },
2271 oob_client,
2272 {
2273 NULL,
2274 0,
2275 128
2276 }
2277 },
2278 /* Test 4: synchronous mixed client and server */
2279 {
2280 {
2281 STD_STREAM_SOCKET,
2282 2048,
2283 16,
2284 2
2285 },
2286 simple_server,
2287 {
2288 NULL,
2289 0,
2290 64
2291 },
2292 simple_mixed_client,
2293 {
2294 NULL,
2295 0,
2296 128
2297 }
2298 }
2299 };
2300
test_UDP(void)2301 static void test_UDP(void)
2302 {
2303 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2304 possible that this test fails due to dropped packets. */
2305
2306 /* peer 0 receives data from all other peers */
2307 struct sock_info peer[NUM_UDP_PEERS];
2308 char buf[16];
2309 int ss, i, n_recv, n_sent;
2310
2311 memset (buf,0,sizeof(buf));
2312 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
2313 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
2314
2315 peer[i].addr.sin_family = AF_INET;
2316 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
2317
2318 if ( i == 0 ) {
2319 peer[i].addr.sin_port = htons ( SERVERPORT );
2320 } else {
2321 peer[i].addr.sin_port = htons ( 0 );
2322 }
2323
2324 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
2325
2326 /* test getsockname() to get peer's port */
2327 ss = sizeof ( peer[i].addr );
2328 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
2329 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
2330 }
2331
2332 /* test getsockname() */
2333 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
2334
2335 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2336 /* send client's ip */
2337 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
2338 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
2339 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
2340 }
2341
2342 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2343 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
2344 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
2345 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
2346 }
2347 }
2348
do_getservbyname(void * param)2349 static DWORD WINAPI do_getservbyname( void *param )
2350 {
2351 struct {
2352 const char *name;
2353 const char *proto;
2354 int port;
2355 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
2356
2357 HANDLE *starttest = param;
2358 int i, j;
2359 struct servent *pserv[2];
2360
2361 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT,
2362 "test_getservbyname: timeout waiting for start signal\n" );
2363
2364 /* ensure that necessary buffer resizes are completed */
2365 for ( j = 0; j < 2; j++) {
2366 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2367 }
2368
2369 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
2370 for ( j = 0; j < 2; j++ ) {
2371 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2372 ok ( pserv[j] != NULL || broken(pserv[j] == NULL) /* win8, fixed in win81 */,
2373 "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
2374 if ( !pserv[j] ) continue;
2375 ok ( pserv[j]->s_port == htons(serv[j].port),
2376 "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
2377 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ),
2378 "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
2379 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ),
2380 "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
2381 }
2382
2383 ok ( pserv[0] == pserv[1] || broken(pserv[0] != pserv[1]) /* win8, fixed in win81 */,
2384 "getservbyname: winsock resized servent buffer when not necessary\n" );
2385 }
2386
2387 return 0;
2388 }
2389
test_getservbyname(void)2390 static void test_getservbyname(void)
2391 {
2392 int i;
2393 HANDLE starttest, thread[NUM_THREADS];
2394 DWORD thread_id[NUM_THREADS];
2395
2396 starttest = CreateEventA ( NULL, 1, 0, "test_getservbyname_starttest" );
2397
2398 /* create threads */
2399 for ( i = 0; i < NUM_THREADS; i++ ) {
2400 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
2401 }
2402
2403 /* signal threads to start */
2404 SetEvent ( starttest );
2405
2406 for ( i = 0; i < NUM_THREADS; i++) {
2407 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
2408 }
2409 }
2410
test_WSASocket(void)2411 static void test_WSASocket(void)
2412 {
2413 SOCKET sock = INVALID_SOCKET;
2414 WSAPROTOCOL_INFOA *pi;
2415 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP};
2416 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP};
2417 int items, err, size, socktype, i, j;
2418 UINT pi_size;
2419
2420 SetLastError(0xdeadbeef);
2421 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET,
2422 "WSASocketA should have failed\n");
2423 err = WSAGetLastError();
2424 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2425
2426 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2427 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2428 closesocket(sock);
2429
2430 sock = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
2431 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2432 closesocket(sock);
2433
2434 SetLastError(0xdeadbeef);
2435 ok(WSASocketA(0, SOCK_STREAM, -1, NULL, 0, 0) == INVALID_SOCKET,
2436 "WSASocketA should have failed\n");
2437 err = WSAGetLastError();
2438 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2439
2440 SetLastError(0xdeadbeef);
2441 ok(WSASocketA(0, -1, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2442 "WSASocketA should have failed\n");
2443 err = WSAGetLastError();
2444 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2445
2446 SetLastError(0xdeadbeef);
2447 ok(WSASocketA(0, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2448 "WSASocketA should have failed\n");
2449 err = WSAGetLastError();
2450 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2451
2452 SetLastError(0xdeadbeef);
2453 ok(WSASocketA(AF_INET, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2454 "WSASocketA should have failed\n");
2455 err = WSAGetLastError();
2456 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2457
2458 SetLastError(0xdeadbeef);
2459 ok(WSASocketA(AF_INET, 0, -1, NULL, 0, 0) == INVALID_SOCKET,
2460 "WSASocketA should have failed\n");
2461 err = WSAGetLastError();
2462 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2463
2464 SetLastError(0xdeadbeef);
2465 ok(WSASocketA(0, -1, -1, NULL, 0, 0) == INVALID_SOCKET,
2466 "WSASocketA should have failed\n");
2467 err = WSAGetLastError();
2468 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2469
2470 SetLastError(0xdeadbeef);
2471 ok(WSASocketA(-1, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2472 "WSASocketA should have failed\n");
2473 err = WSAGetLastError();
2474 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2475
2476 sock = WSASocketA(AF_INET, 0, IPPROTO_TCP, NULL, 0, 0);
2477 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2478 closesocket(sock);
2479
2480 SetLastError(0xdeadbeef);
2481 ok(WSASocketA(0, SOCK_STREAM, 0, NULL, 0, 0) == INVALID_SOCKET,
2482 "WSASocketA should have failed\n");
2483 err = WSAGetLastError();
2484 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2485
2486 SetLastError(0xdeadbeef);
2487 ok(WSASocketA(0, 0, 0xdead, NULL, 0, 0) == INVALID_SOCKET,
2488 "WSASocketA should have failed\n");
2489 err = WSAGetLastError();
2490 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2491
2492 SetLastError(0xdeadbeef);
2493 ok(WSASocketA(AF_INET, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2494 "WSASocketA should have failed\n");
2495 err = WSAGetLastError();
2496 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2497
2498 SetLastError(0xdeadbeef);
2499 ok(WSASocketA(0, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2500 "WSASocketA should have failed\n");
2501 err = WSAGetLastError();
2502 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2503
2504 sock = WSASocketA(0, 0, IPPROTO_TCP, NULL, 0, 0);
2505 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2506 closesocket(sock);
2507
2508 /* SOCK_STREAM does not support IPPROTO_UDP */
2509 SetLastError(0xdeadbeef);
2510 ok(WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2511 "WSASocketA should have failed\n");
2512 err = WSAGetLastError();
2513 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2514
2515 /* SOCK_DGRAM does not support IPPROTO_TCP */
2516 SetLastError(0xdeadbeef);
2517 ok(WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_TCP, NULL, 0, 0) == INVALID_SOCKET,
2518 "WSASocketA should have failed\n");
2519 err = WSAGetLastError();
2520 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2521
2522 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2523 * to avoid a crash on win98.
2524 */
2525 pi_size = 0;
2526 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size);
2527 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2528 items);
2529 err = WSAGetLastError();
2530 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2531 err, WSAENOBUFS);
2532
2533 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2534 ok(pi != NULL, "Failed to allocate memory\n");
2535 if (pi == NULL) {
2536 skip("Can't continue without memory.\n");
2537 return;
2538 }
2539
2540 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size);
2541 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2542 WSAGetLastError());
2543
2544 if (items == 0) {
2545 skip("No protocols enumerated.\n");
2546 HeapFree(GetProcessHeap(), 0, pi);
2547 return;
2548 }
2549
2550 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
2551 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
2552 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2553 WSAGetLastError());
2554 closesocket(sock);
2555
2556 /* find what parameters are used first: plain parameters or protocol info struct */
2557 pi[0].iProtocol = -1;
2558 pi[0].iSocketType = -1;
2559 pi[0].iAddressFamily = -1;
2560 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET,
2561 "WSASocketA should have failed\n");
2562 err = WSAGetLastError();
2563 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2564
2565 pi[0].iProtocol = 0;
2566 pi[0].iSocketType = 0;
2567 pi[0].iAddressFamily = 0;
2568 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0);
2569 if(sock != INVALID_SOCKET)
2570 {
2571 win_skip("must work only in OS <= 2003\n");
2572 closesocket(sock);
2573 }
2574 else
2575 {
2576 err = WSAGetLastError();
2577 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2578 }
2579
2580 pi[0].iProtocol = IPPROTO_UDP;
2581 pi[0].iSocketType = SOCK_DGRAM;
2582 pi[0].iAddressFamily = AF_INET;
2583 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0);
2584 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2585 WSAGetLastError());
2586 size = sizeof(socktype);
2587 socktype = 0xdead;
2588 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2589 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2590 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2591 SOCK_DGRAM, socktype);
2592 closesocket(sock);
2593
2594 sock = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi[0], 0, 0);
2595 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2596 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_STREAM, "Wrong socket type, expected %d received %d\n",
2602 SOCK_STREAM, socktype);
2603 closesocket(sock);
2604
2605 HeapFree(GetProcessHeap(), 0, pi);
2606
2607 pi_size = 0;
2608 items = WSAEnumProtocolsA(NULL, NULL, &pi_size);
2609 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2610 items);
2611 err = WSAGetLastError();
2612 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2613 err, WSAENOBUFS);
2614
2615 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2616 ok(pi != NULL, "Failed to allocate memory\n");
2617 if (pi == NULL) {
2618 skip("Can't continue without memory.\n");
2619 return;
2620 }
2621
2622 items = WSAEnumProtocolsA(NULL, pi, &pi_size);
2623 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2624 WSAGetLastError());
2625
2626 /* when no protocol and socket type are specified the first entry
2627 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2628 * is returned */
2629 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2630 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2631 WSAGetLastError());
2632
2633 size = sizeof(socktype);
2634 socktype = 0xdead;
2635 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2636 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2637 for(i = 0; i < items; i++)
2638 {
2639 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
2640 {
2641 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n",
2642 pi[i].iSocketType, socktype);
2643 break;
2644 }
2645 }
2646 ok(i != items, "Creating a socket without protocol and socket type didn't work\n");
2647 closesocket(sock);
2648
2649 /* when no socket type is specified the first entry from WSAEnumProtocols
2650 * that matches the protocol is returned */
2651 for (i = 0; i < sizeof(autoprotocols) / sizeof(autoprotocols[0]); i++)
2652 {
2653 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0);
2654 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n",
2655 autoprotocols[i], WSAGetLastError());
2656
2657 size = sizeof(socktype);
2658 socktype = 0xdead;
2659 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2660 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2661
2662 for (err = 1, j = 0; j < items; j++)
2663 {
2664 if (pi[j].iProtocol == autoprotocols[i])
2665 {
2666 if (socktype == pi[j].iSocketType)
2667 err = 0;
2668 else
2669 ok(0, "Wrong socket type, expected %d received %d\n",
2670 pi[j].iSocketType, socktype);
2671 break;
2672 }
2673 }
2674 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]);
2675
2676 closesocket(sock);
2677 }
2678
2679 HeapFree(GetProcessHeap(), 0, pi);
2680
2681 SetLastError(0xdeadbeef);
2682 /* starting on vista the socket function returns error during the socket
2683 creation and no longer in the socket operations (sendto, readfrom) */
2684 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
2685 if (sock == INVALID_SOCKET)
2686 {
2687 err = WSAGetLastError();
2688 ok(err == WSAEACCES, "Expected 10013, received %d\n", err);
2689 skip("SOCK_RAW is not supported\n");
2690 }
2691 else
2692 {
2693 trace("SOCK_RAW is supported\n");
2694
2695 size = sizeof(socktype);
2696 socktype = 0xdead;
2697 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2698 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2699 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2700 SOCK_RAW, socktype);
2701 closesocket(sock);
2702
2703 todo_wine {
2704 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0);
2705 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2706 WSAGetLastError());
2707 size = sizeof(socktype);
2708 socktype = 0xdead;
2709 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2710 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2711 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2712 SOCK_RAW, socktype);
2713 closesocket(sock);
2714 }
2715
2716 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_TCP, NULL, 0, 0);
2717 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2718 WSAGetLastError());
2719 size = sizeof(socktype);
2720 socktype = 0xdead;
2721 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2722 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2723 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2724 SOCK_RAW, socktype);
2725 closesocket(sock);
2726 }
2727
2728 /* IPX socket tests */
2729
2730 SetLastError(0xdeadbeef);
2731 sock = WSASocketA(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, NULL, 0, 0);
2732 if (sock == INVALID_SOCKET)
2733 {
2734 err = WSAGetLastError();
2735 todo_wine_if (err == WSAEPROTONOSUPPORT)
2736 ok(err == WSAEAFNOSUPPORT || broken(err == WSAEPROTONOSUPPORT), "Expected 10047, received %d\n", err);
2737 skip("IPX is not supported\n");
2738 }
2739 else
2740 {
2741 WSAPROTOCOL_INFOA info;
2742 closesocket(sock);
2743
2744 trace("IPX is supported\n");
2745
2746 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0);
2747 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2748 WSAGetLastError());
2749
2750 size = sizeof(socktype);
2751 socktype = 0xdead;
2752 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2753 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2754 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2755 SOCK_DGRAM, socktype);
2756
2757 /* check socket family, type and protocol */
2758 size = sizeof(WSAPROTOCOL_INFOA);
2759 err = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &info, &size);
2760 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2761 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n",
2762 NSPROTO_IPX, info.iProtocol);
2763 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n",
2764 AF_IPX, info.iProtocol);
2765 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2766 SOCK_DGRAM, info.iSocketType);
2767 closesocket(sock);
2768
2769 /* SOCK_STREAM does not support NSPROTO_IPX */
2770 SetLastError(0xdeadbeef);
2771 ok(WSASocketA(AF_IPX, SOCK_STREAM, NSPROTO_IPX, NULL, 0, 0) == INVALID_SOCKET,
2772 "WSASocketA should have failed\n");
2773 err = WSAGetLastError();
2774 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2775
2776 /* test extended IPX support - that is adding any number between 0 and 255
2777 * to the IPX protocol value will make it be used as IPX packet type */
2778 for(i = 0;i <= 255;i += 17)
2779 {
2780 SetLastError(0xdeadbeef);
2781 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0);
2782 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2783 WSAGetLastError());
2784
2785 size = sizeof(int);
2786 socktype = -1;
2787 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size);
2788 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2789 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n",
2790 i, socktype);
2791
2792 closesocket(sock);
2793 }
2794 }
2795 }
2796
test_WSADuplicateSocket(void)2797 static void test_WSADuplicateSocket(void)
2798 {
2799 SOCKET source, dupsock;
2800 WSAPROTOCOL_INFOA info;
2801 DWORD err;
2802 struct sockaddr_in addr;
2803 int socktype, size, addrsize, ret;
2804 char teststr[] = "TEST", buffer[16];
2805
2806 source = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
2807 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2808
2809 /* test invalid parameters */
2810 SetLastError(0xdeadbeef);
2811 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n");
2812 err = WSAGetLastError();
2813 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2814
2815 SetLastError(0xdeadbeef);
2816 ok(WSADuplicateSocketA(source, 0, NULL),
2817 "WSADuplicateSocketA should have failed\n");
2818 err = WSAGetLastError();
2819 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2820
2821 SetLastError(0xdeadbeef);
2822 ok(WSADuplicateSocketA(source, ~0, &info),
2823 "WSADuplicateSocketA should have failed\n");
2824 err = WSAGetLastError();
2825 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2826
2827 SetLastError(0xdeadbeef);
2828 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info),
2829 "WSADuplicateSocketA should have failed\n");
2830 err = WSAGetLastError();
2831 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2832
2833 SetLastError(0xdeadbeef);
2834 ok(WSADuplicateSocketA(source, GetCurrentProcessId(), NULL),
2835 "WSADuplicateSocketA should have failed\n");
2836 err = WSAGetLastError();
2837 ok(err == WSAEFAULT, "expected 10014, received %d\n", err);
2838
2839 /* test returned structure */
2840 memset(&info, 0, sizeof(info));
2841 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2842 "WSADuplicateSocketA should have worked\n");
2843
2844 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n",
2845 IPPROTO_TCP, info.iProtocol);
2846 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2847 AF_INET, info.iProtocol);
2848 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n",
2849 SOCK_STREAM, info.iSocketType);
2850
2851 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2852 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2853
2854 closesocket(dupsock);
2855 closesocket(source);
2856
2857 /* create a socket, bind it, duplicate it then send data on source and
2858 * receive in the duplicated socket */
2859 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2860 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2861
2862 memset(&info, 0, sizeof(info));
2863 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2864 "WSADuplicateSocketA should have worked\n");
2865
2866 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n",
2867 IPPROTO_UDP, info.iProtocol);
2868 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2869 AF_INET, info.iProtocol);
2870 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2871 SOCK_DGRAM, info.iSocketType);
2872
2873 memset(&addr, 0, sizeof(addr));
2874 addr.sin_family = AF_INET;
2875 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2876 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2877 "bind should have worked\n");
2878
2879 /* read address to find out the port number to be used in sendto */
2880 memset(&addr, 0, sizeof(addr));
2881 addrsize = sizeof(addr);
2882 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2883 "getsockname should have worked\n");
2884 ok(addr.sin_port, "socket port should be != 0\n");
2885
2886 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2887 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2888
2889 size = sizeof(int);
2890 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2891 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
2892 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2893 SOCK_DGRAM, socktype);
2894
2895 set_blocking(source, TRUE);
2896
2897 /* send data on source socket */
2898 addrsize = sizeof(addr);
2899 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2900 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2901
2902 /* receive on duplicated socket */
2903 addrsize = sizeof(addr);
2904 memset(buffer, 0, sizeof(buffer));
2905 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize);
2906 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2907 buffer[sizeof(teststr) - 1] = 0;
2908 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer);
2909
2910 closesocket(dupsock);
2911 closesocket(source);
2912
2913 /* show that the source socket need to be bound before the duplicated
2914 * socket is created */
2915 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2916 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2917
2918 memset(&info, 0, sizeof(info));
2919 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2920 "WSADuplicateSocketA should have worked\n");
2921
2922 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2923 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2924
2925 memset(&addr, 0, sizeof(addr));
2926 addr.sin_family = AF_INET;
2927 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2928 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2929 "bind should have worked\n");
2930
2931 /* read address to find out the port number to be used in sendto */
2932 memset(&addr, 0, sizeof(addr));
2933 addrsize = sizeof(addr);
2934 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2935 "getsockname should have worked\n");
2936 ok(addr.sin_port, "socket port should be != 0\n");
2937
2938 set_blocking(source, TRUE);
2939
2940 addrsize = sizeof(addr);
2941 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2942 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2943
2944 SetLastError(0xdeadbeef);
2945 addrsize = sizeof(addr);
2946 memset(buffer, 0, sizeof(buffer));
2947 todo_wine {
2948 ok(recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize) == -1,
2949 "recvfrom should have failed\n");
2950 err = WSAGetLastError();
2951 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2952 }
2953
2954 closesocket(dupsock);
2955 closesocket(source);
2956 }
2957
test_WSAEnumNetworkEvents(void)2958 static void test_WSAEnumNetworkEvents(void)
2959 {
2960 SOCKET s, s2;
2961 int sock_type[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM}, i, j, k, l;
2962 struct sockaddr_in address;
2963 HANDLE event;
2964 WSANETWORKEVENTS net_events;
2965
2966 memset(&address, 0, sizeof(address));
2967 address.sin_addr.s_addr = htonl(INADDR_ANY);
2968 address.sin_family = AF_INET;
2969
2970 /* This test follows the steps from bugs 10204 and 24946 */
2971 for (l = 0; l < 2; l++)
2972 {
2973 for (i = 0; i < sizeof(sock_type) / sizeof(sock_type[0]); i++)
2974 {
2975 if (i == 2)
2976 ok(!tcp_socketpair(&s, &s2), "Test[%d]: creating socket pair failed\n", i);
2977 else
2978 {
2979 s = socket(AF_INET, sock_type[i], 0);
2980 ok (s != SOCKET_ERROR, "Test[%d]: failed to create socket\n", i);
2981 ok (!bind(s, (struct sockaddr*) &address, sizeof(address)), "Test[%d]: bind failed\n", i);
2982 }
2983 event = WSACreateEvent();
2984 ok (event != NULL, "Test[%d]: failed to create event\n", i);
2985 for (j = 0; j < 5; j++) /* Repeat sometimes and the result must be the same */
2986 {
2987 /* When the TCP socket is not connected NO events will be returned.
2988 * When connected and no data pending it will get the write event.
2989 * UDP sockets don't have connections so as soon as they are bound
2990 * they can read/write data. Since nobody is sendind us data only
2991 * the write event will be returned and ONLY once.
2992 */
2993 ok (!WSAEventSelect(s, event, FD_READ | FD_WRITE), "Test[%d]: WSAEventSelect failed\n", i);
2994 memset(&net_events, 0xAB, sizeof(net_events));
2995 ok (!WSAEnumNetworkEvents(s, l == 0 ? event : NULL, &net_events),
2996 "Test[%d]: WSAEnumNetworkEvents failed\n", i);
2997 if (i >= 1 && j == 0) /* FD_WRITE is SET on first try for UDP and connected TCP */
2998 {
2999 todo_wine_if (i == 0 || net_events.lNetworkEvents == 0)
3000 ok (net_events.lNetworkEvents == FD_WRITE, "Test[%d]: expected 2, got %d\n",
3001 i, net_events.lNetworkEvents);
3002 }
3003 else
3004 {
3005 todo_wine_if (i != 0 && net_events.lNetworkEvents != 0)
3006 ok (net_events.lNetworkEvents == 0, "Test[%d]: expected 0, got %d\n",
3007 i, net_events.lNetworkEvents);
3008 }
3009 for (k = 0; k < FD_MAX_EVENTS; k++)
3010 {
3011 if (net_events.lNetworkEvents & (1 << k))
3012 {
3013 ok (net_events.iErrorCode[k] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n",
3014 i, k, net_events.iErrorCode[k]);
3015 }
3016 else
3017 {
3018 /* Bits that are not set in lNetworkEvents MUST not be changed */
3019 ok (net_events.iErrorCode[k] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n",
3020 i, k, net_events.iErrorCode[k]);
3021 }
3022 }
3023 }
3024 closesocket(s);
3025 WSACloseEvent(event);
3026 if (i == 2) closesocket(s2);
3027 }
3028 }
3029 }
3030
test_WSAAddressToStringA(void)3031 static void test_WSAAddressToStringA(void)
3032 {
3033 SOCKET v6 = INVALID_SOCKET;
3034 INT ret;
3035 DWORD len;
3036 int GLE;
3037 SOCKADDR_IN sockaddr;
3038 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3039
3040 CHAR expect1[] = "0.0.0.0";
3041 CHAR expect2[] = "255.255.255.255";
3042 CHAR expect3[] = "0.0.0.0:65535";
3043 CHAR expect4[] = "255.255.255.255:65535";
3044
3045 SOCKADDR_IN6 sockaddr6;
3046 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
3047
3048 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3049 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3050 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
3051
3052 CHAR expect6_1[] = "::1";
3053 CHAR expect6_2[] = "20ab::1";
3054 CHAR expect6_3[] = "[20ab::2001]:33274";
3055 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
3056 CHAR expect6_3_3[] = "20ab::2001%4660";
3057
3058 len = 0;
3059
3060 sockaddr.sin_family = AF_INET;
3061 sockaddr.sin_port = 0;
3062 sockaddr.sin_addr.s_addr = 0;
3063
3064 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3065 GLE = WSAGetLastError();
3066 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
3067 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
3068 GLE, ret );
3069
3070 len = sizeof(address);
3071
3072 sockaddr.sin_family = AF_INET;
3073 sockaddr.sin_port = 0;
3074 sockaddr.sin_addr.s_addr = 0;
3075
3076 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3077 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3078
3079 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
3080 ok( len == sizeof( expect1 ), "Got size %d\n", len);
3081
3082 len = sizeof(address);
3083
3084 sockaddr.sin_family = AF_INET;
3085 sockaddr.sin_port = 0;
3086 sockaddr.sin_addr.s_addr = 0xffffffff;
3087
3088 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3089 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3090
3091 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
3092
3093 len = sizeof(address);
3094
3095 sockaddr.sin_family = AF_INET;
3096 sockaddr.sin_port = 0xffff;
3097 sockaddr.sin_addr.s_addr = 0;
3098
3099 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3100 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3101
3102 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
3103
3104 len = sizeof(address);
3105
3106 sockaddr.sin_family = AF_INET;
3107 sockaddr.sin_port = 0xffff;
3108 sockaddr.sin_addr.s_addr = 0xffffffff;
3109
3110 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3111 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3112
3113 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
3114 ok( len == sizeof( expect4 ), "Got size %d\n", len);
3115
3116 /*check to see it IPv6 is available */
3117 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
3118 if (v6 == INVALID_SOCKET) {
3119 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3120 WSAGetLastError(), WSAEAFNOSUPPORT);
3121 goto end;
3122 }
3123 /* Test a short IPv6 address */
3124 len = sizeof(address6);
3125
3126 sockaddr6.sin6_family = AF_INET6;
3127 sockaddr6.sin6_port = 0x0000;
3128 sockaddr6.sin6_scope_id = 0;
3129 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
3130
3131 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3132 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3133 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
3134 ok( len == sizeof(expect6_1), "Got size %d\n", len);
3135
3136 /* Test a longer IPv6 address */
3137 len = sizeof(address6);
3138
3139 sockaddr6.sin6_family = AF_INET6;
3140 sockaddr6.sin6_port = 0x0000;
3141 sockaddr6.sin6_scope_id = 0;
3142 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
3143
3144 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3145 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3146 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
3147 ok( len == sizeof(expect6_2), "Got size %d\n", len);
3148
3149 /* Test IPv6 address and port number */
3150 len = sizeof(address6);
3151
3152 sockaddr6.sin6_family = AF_INET6;
3153 sockaddr6.sin6_port = 0xfa81;
3154 sockaddr6.sin6_scope_id = 0;
3155 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3156
3157 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3158 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3159 ok( !strcmp( address6, expect6_3 ), "Expected: %s, got: %s\n", expect6_3, address6 );
3160 ok( len == sizeof(expect6_3), "Got size %d\n", len );
3161
3162 /* Test IPv6 address, port number and scope_id */
3163 len = sizeof(address6);
3164
3165 sockaddr6.sin6_family = AF_INET6;
3166 sockaddr6.sin6_port = 0xfa81;
3167 sockaddr6.sin6_scope_id = 0x1234;
3168 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3169
3170 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3171 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3172 ok( !strcmp( address6, expect6_3_2 ), "Expected: %s, got: %s\n", expect6_3_2, address6 );
3173 ok( len == sizeof(expect6_3_2), "Got size %d\n", len );
3174
3175 /* Test IPv6 address and scope_id */
3176 len = sizeof(address6);
3177
3178 sockaddr6.sin6_family = AF_INET6;
3179 sockaddr6.sin6_port = 0x0000;
3180 sockaddr6.sin6_scope_id = 0x1234;
3181 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3182
3183 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3184 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3185 ok( !strcmp( address6, expect6_3_3 ), "Expected: %s, got: %s\n", expect6_3_3, address6 );
3186 ok( len == sizeof(expect6_3_3), "Got size %d\n", len );
3187
3188 end:
3189 if (v6 != INVALID_SOCKET)
3190 closesocket(v6);
3191 }
3192
test_WSAAddressToStringW(void)3193 static void test_WSAAddressToStringW(void)
3194 {
3195 SOCKET v6 = INVALID_SOCKET;
3196 INT ret;
3197 DWORD len;
3198 int GLE;
3199 SOCKADDR_IN sockaddr;
3200 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3201
3202 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
3203 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
3204 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
3205 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
3206 '6', '5', '5', '3', '5', 0 };
3207
3208 SOCKADDR_IN6 sockaddr6;
3209 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
3210
3211 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3212 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3213 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
3214
3215 WCHAR expect6_1[] = {':',':','1',0};
3216 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
3217 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
3218 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
3219 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
3220
3221 len = 0;
3222
3223 sockaddr.sin_family = AF_INET;
3224 sockaddr.sin_port = 0;
3225 sockaddr.sin_addr.s_addr = 0;
3226
3227 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3228 GLE = WSAGetLastError();
3229 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
3230 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
3231 GLE, ret );
3232
3233 len = sizeof(address);
3234
3235 sockaddr.sin_family = AF_INET;
3236 sockaddr.sin_port = 0;
3237 sockaddr.sin_addr.s_addr = 0;
3238
3239 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3240 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3241
3242 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
3243 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
3244
3245 len = sizeof(address);
3246
3247 sockaddr.sin_family = AF_INET;
3248 sockaddr.sin_port = 0;
3249 sockaddr.sin_addr.s_addr = 0xffffffff;
3250
3251 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3252 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3253
3254 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
3255
3256 len = sizeof(address);
3257
3258 sockaddr.sin_family = AF_INET;
3259 sockaddr.sin_port = 0xffff;
3260 sockaddr.sin_addr.s_addr = 0;
3261
3262 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3263 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3264
3265 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
3266
3267 len = sizeof(address);
3268
3269 sockaddr.sin_family = AF_INET;
3270 sockaddr.sin_port = 0xffff;
3271 sockaddr.sin_addr.s_addr = 0xffffffff;
3272
3273 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
3274 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3275
3276 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
3277 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len);
3278
3279 /*check to see it IPv6 is available */
3280 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
3281 if (v6 == INVALID_SOCKET) {
3282 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3283 WSAGetLastError(), WSAEAFNOSUPPORT);
3284 goto end;
3285 }
3286
3287 /* Test a short IPv6 address */
3288 len = sizeof(address6)/sizeof(WCHAR);
3289
3290 sockaddr6.sin6_family = AF_INET6;
3291 sockaddr6.sin6_port = 0x0000;
3292 sockaddr6.sin6_scope_id = 0;
3293 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
3294
3295 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3296 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3297 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
3298 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
3299
3300 /* Test a longer IPv6 address */
3301 len = sizeof(address6)/sizeof(WCHAR);
3302
3303 sockaddr6.sin6_family = AF_INET6;
3304 sockaddr6.sin6_port = 0x0000;
3305 sockaddr6.sin6_scope_id = 0;
3306 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
3307
3308 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3309 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3310
3311 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
3312 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
3313
3314 /* Test IPv6 address and port number */
3315 len = sizeof(address6)/sizeof(WCHAR);
3316
3317 sockaddr6.sin6_family = AF_INET6;
3318 sockaddr6.sin6_port = 0xfa81;
3319 sockaddr6.sin6_scope_id = 0;
3320 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3321
3322 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3323 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3324 ok( !lstrcmpW( address6, expect6_3 ),
3325 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3), wine_dbgstr_w(address6) );
3326 ok( len == sizeof(expect6_3)/sizeof(WCHAR), "Got %d\n", len );
3327
3328 /* Test IPv6 address, port number and scope_id */
3329 len = sizeof(address6)/sizeof(WCHAR);
3330
3331 sockaddr6.sin6_family = AF_INET6;
3332 sockaddr6.sin6_port = 0xfa81;
3333 sockaddr6.sin6_scope_id = 0x1234;
3334 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3335
3336 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3337 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3338 ok( !lstrcmpW( address6, expect6_3_2 ),
3339 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2), wine_dbgstr_w(address6) );
3340 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR), "Got %d\n", len );
3341
3342 /* Test IPv6 address and scope_id */
3343 len = sizeof(address6)/sizeof(WCHAR);
3344
3345 sockaddr6.sin6_family = AF_INET6;
3346 sockaddr6.sin6_port = 0x0000;
3347 sockaddr6.sin6_scope_id = 0xfffe;
3348 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3349
3350 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3351 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3352 ok( !lstrcmpW( address6, expect6_3_3 ),
3353 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3), wine_dbgstr_w(address6) );
3354 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR), "Got %d\n", len );
3355
3356 end:
3357 if (v6 != INVALID_SOCKET)
3358 closesocket(v6);
3359 }
3360
test_WSAStringToAddressA(void)3361 static void test_WSAStringToAddressA(void)
3362 {
3363 INT ret, len;
3364 SOCKADDR_IN sockaddr;
3365 SOCKADDR_IN6 sockaddr6;
3366 int GLE;
3367
3368 CHAR address1[] = "0.0.0.0";
3369 CHAR address2[] = "127.127.127.127";
3370 CHAR address3[] = "255.255.255.255";
3371 CHAR address4[] = "127.127.127.127:65535";
3372 CHAR address5[] = "255.255.255.255:65535";
3373 CHAR address6[] = "::1";
3374 CHAR address7[] = "[::1]";
3375 CHAR address8[] = "[::1]:65535";
3376 CHAR address9[] = "2001::1";
3377
3378 len = 0;
3379 sockaddr.sin_family = AF_INET;
3380
3381 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3382 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
3383 WSAGetLastError() );
3384
3385 len = sizeof(sockaddr);
3386 sockaddr.sin_port = 0;
3387 sockaddr.sin_addr.s_addr = 0;
3388
3389 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3390 ok( !ret && sockaddr.sin_addr.s_addr == 0,
3391 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3392
3393 len = sizeof(sockaddr);
3394 sockaddr.sin_port = 0;
3395 sockaddr.sin_addr.s_addr = 0;
3396
3397 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3398 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
3399 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3400
3401 len = sizeof(sockaddr);
3402 sockaddr.sin_port = 0;
3403 sockaddr.sin_addr.s_addr = 0;
3404
3405 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3406 GLE = WSAGetLastError();
3407 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
3408 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
3409 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
3410
3411 len = sizeof(sockaddr);
3412 sockaddr.sin_port = 0;
3413 sockaddr.sin_addr.s_addr = 0;
3414
3415 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3416 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
3417 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3418
3419 len = sizeof(sockaddr);
3420 sockaddr.sin_port = 0;
3421 sockaddr.sin_addr.s_addr = 0;
3422
3423 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3424 GLE = WSAGetLastError();
3425 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
3426 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
3427 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
3428
3429 len = sizeof(sockaddr);
3430
3431 ret = WSAStringToAddressA( address9, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3432 GLE = WSAGetLastError();
3433 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3434 "WSAStringToAddressA() should have failed with %d\n", GLE );
3435
3436 len = sizeof(sockaddr6);
3437 memset(&sockaddr6, 0, len);
3438 sockaddr6.sin6_family = AF_INET6;
3439
3440 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3441 &len );
3442 if (ret == SOCKET_ERROR)
3443 {
3444 win_skip("IPv6 not supported\n");
3445 return;
3446 }
3447
3448 GLE = WSAGetLastError();
3449 ok( ret == 0, "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
3450
3451 len = sizeof(sockaddr6);
3452 memset(&sockaddr6, 0, len);
3453 sockaddr6.sin6_family = AF_INET6;
3454
3455 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3456 &len );
3457 GLE = WSAGetLastError();
3458 ok( ret == 0, "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
3459
3460 len = sizeof(sockaddr6);
3461 memset(&sockaddr6, 0, len);
3462 sockaddr6.sin6_family = AF_INET6;
3463
3464 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3465 &len );
3466 GLE = WSAGetLastError();
3467 ok( ret == 0 && sockaddr6.sin6_port == 0xffff,
3468 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
3469
3470 len = sizeof(sockaddr6);
3471
3472 ret = WSAStringToAddressA( address7 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len );
3473 GLE = WSAGetLastError();
3474 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3475 "WSAStringToAddressW() should have failed with %d\n", GLE );
3476
3477 len = sizeof(sockaddr6);
3478
3479 ret = WSAStringToAddressA( address8 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len );
3480 GLE = WSAGetLastError();
3481 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3482 "WSAStringToAddressW() should have failed with %d\n", GLE );
3483 }
3484
test_WSAStringToAddressW(void)3485 static void test_WSAStringToAddressW(void)
3486 {
3487 INT ret, len;
3488 SOCKADDR_IN sockaddr, *sin;
3489 SOCKADDR_IN6 sockaddr6;
3490 SOCKADDR_STORAGE sockaddr_storage;
3491 int GLE;
3492
3493 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
3494 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
3495 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
3496 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
3497 ':', '6', '5', '5', '3', '5', 0 };
3498 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
3499 '6', '5', '5', '3', '5', 0 };
3500 WCHAR address6[] = {':',':','1','\0'};
3501 WCHAR address7[] = {'[',':',':','1',']','\0'};
3502 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
3503 WCHAR address9[] = {'2','0','0','1',':',':','1','\0'};
3504
3505 len = 0;
3506 sockaddr.sin_family = AF_INET;
3507
3508 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3509 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
3510 WSAGetLastError() );
3511
3512 len = sizeof(sockaddr);
3513 sockaddr.sin_port = 0;
3514 sockaddr.sin_addr.s_addr = 0;
3515
3516 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3517 ok( !ret && sockaddr.sin_addr.s_addr == 0,
3518 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3519
3520 len = sizeof(sockaddr);
3521 sockaddr.sin_port = 0;
3522 sockaddr.sin_addr.s_addr = 0;
3523
3524 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3525 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
3526 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3527
3528 len = sizeof(sockaddr);
3529 sockaddr.sin_port = 0;
3530 sockaddr.sin_addr.s_addr = 0;
3531
3532 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3533 GLE = WSAGetLastError();
3534 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
3535 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
3536 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
3537
3538 len = sizeof(sockaddr);
3539 sockaddr.sin_port = 0;
3540 sockaddr.sin_addr.s_addr = 0;
3541
3542 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3543 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
3544 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3545
3546 len = sizeof(sockaddr);
3547 sockaddr.sin_port = 0;
3548 sockaddr.sin_addr.s_addr = 0;
3549
3550 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3551 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
3552 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
3553 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
3554
3555 /* Test with a larger buffer than necessary */
3556 len = sizeof(sockaddr_storage);
3557 sin = (SOCKADDR_IN *)&sockaddr_storage;
3558 sin->sin_port = 0;
3559 sin->sin_addr.s_addr = 0;
3560
3561 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)sin, &len );
3562 ok( (ret == 0 && sin->sin_addr.s_addr == 0xffffffff && sin->sin_port == 0xffff) ||
3563 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
3564 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
3565 ok( len == sizeof(SOCKADDR_IN), "unexpected length %d\n", len );
3566
3567 len = sizeof(sockaddr);
3568
3569 ret = WSAStringToAddressW( address9, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
3570 GLE = WSAGetLastError();
3571 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3572 "WSAStringToAddressW() should have failed with %d\n", GLE );
3573
3574 len = sizeof(sockaddr6);
3575 memset(&sockaddr6, 0, len);
3576 sockaddr6.sin6_family = AF_INET6;
3577
3578 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3579 &len );
3580 if (ret == SOCKET_ERROR)
3581 {
3582 win_skip("IPv6 not supported\n");
3583 return;
3584 }
3585
3586 GLE = WSAGetLastError();
3587 ok( ret == 0, "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
3588
3589 len = sizeof(sockaddr6);
3590 memset(&sockaddr6, 0, len);
3591 sockaddr6.sin6_family = AF_INET6;
3592
3593 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3594 &len );
3595 GLE = WSAGetLastError();
3596 ok( ret == 0, "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
3597
3598 len = sizeof(sockaddr6);
3599 memset(&sockaddr6, 0, len);
3600 sockaddr6.sin6_family = AF_INET6;
3601
3602 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
3603 &len );
3604 GLE = WSAGetLastError();
3605 ok( ret == 0 && sockaddr6.sin6_port == 0xffff,
3606 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
3607
3608 len = sizeof(sockaddr6);
3609
3610 ret = WSAStringToAddressW( address7 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len );
3611 GLE = WSAGetLastError();
3612 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3613 "WSAStringToAddressW() should have failed with %d\n", GLE );
3614
3615 len = sizeof(sockaddr6);
3616
3617 ret = WSAStringToAddressW( address8 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len );
3618 GLE = WSAGetLastError();
3619 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL),
3620 "WSAStringToAddressW() should have failed with %d\n", GLE );
3621 }
3622
SelectReadThread(void * param)3623 static DWORD WINAPI SelectReadThread(void *param)
3624 {
3625 select_thread_params *par = param;
3626 fd_set readfds;
3627 int ret;
3628 struct sockaddr_in addr;
3629 struct timeval select_timeout;
3630
3631 memset(&readfds, 0, sizeof(readfds));
3632 FD_ZERO(&readfds);
3633 FD_SET(par->s, &readfds);
3634 select_timeout.tv_sec=5;
3635 select_timeout.tv_usec=0;
3636 addr.sin_family = AF_INET;
3637 addr.sin_addr.s_addr = inet_addr(SERVERIP);
3638 addr.sin_port = htons(SERVERPORT);
3639
3640 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
3641 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
3642
3643 SetEvent(server_ready);
3644 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
3645 par->ReadKilled = (ret == 1);
3646
3647 return 0;
3648 }
3649
SelectCloseThread(void * param)3650 static DWORD WINAPI SelectCloseThread(void *param)
3651 {
3652 SOCKET s = *(SOCKET*)param;
3653 Sleep(500);
3654 closesocket(s);
3655 return 0;
3656 }
3657
test_errors(void)3658 static void test_errors(void)
3659 {
3660 SOCKET sock;
3661 SOCKADDR_IN SockAddr;
3662 int ret, err;
3663
3664 WSASetLastError(NO_ERROR);
3665 sock = socket(PF_INET, SOCK_STREAM, 0);
3666 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3667 memset(&SockAddr, 0, sizeof(SockAddr));
3668 SockAddr.sin_family = AF_INET;
3669 SockAddr.sin_port = htons(6924);
3670 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
3671
3672 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
3673 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret );
3674 if (ret == SOCKET_ERROR)
3675 {
3676 err = WSAGetLastError();
3677 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err );
3678 }
3679
3680 {
3681 TIMEVAL timeval;
3682 fd_set set = {1, {sock}};
3683
3684 timeval.tv_sec = 0;
3685 timeval.tv_usec = 50000;
3686
3687 ret = select(1, NULL, &set, NULL, &timeval);
3688 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret );
3689 }
3690
3691 ret = closesocket(sock);
3692 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
3693 }
3694
test_listen(void)3695 static void test_listen(void)
3696 {
3697 SOCKET fdA, fdB;
3698 int ret, acceptc, olen = sizeof(acceptc);
3699 struct sockaddr_in address;
3700
3701 memset(&address, 0, sizeof(address));
3702 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3703 address.sin_family = AF_INET;
3704 address.sin_port = htons(SERVERPORT);
3705
3706 /* invalid socket tests */
3707 SetLastError(0xdeadbeef);
3708 ok ((listen(0, 0) == SOCKET_ERROR), "listen did not fail\n");
3709 ret = WSAGetLastError();
3710 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
3711
3712 SetLastError(0xdeadbeef);
3713 ok ((listen(0xdeadbeef, 0) == SOCKET_ERROR), "listen did not fail\n");
3714 ret = WSAGetLastError();
3715 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
3716
3717 /* tcp tests */
3718 fdA = socket(AF_INET, SOCK_STREAM, 0);
3719 ok ((fdA != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3720
3721 fdB = socket(AF_INET, SOCK_STREAM, 0);
3722 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3723
3724 SetLastError(0xdeadbeef);
3725 ok ((listen(fdA, -2) == SOCKET_ERROR), "listen did not fail\n");
3726 ret = WSAGetLastError();
3727 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3728
3729 SetLastError(0xdeadbeef);
3730 ok ((listen(fdA, 1) == SOCKET_ERROR), "listen did not fail\n");
3731 ret = WSAGetLastError();
3732 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3733
3734 SetLastError(0xdeadbeef);
3735 ok ((listen(fdA, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3736 ret = WSAGetLastError();
3737 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3738
3739 ok (!bind(fdA, (struct sockaddr*) &address, sizeof(address)), "bind failed\n");
3740
3741 SetLastError(0xdeadbeef);
3742 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
3743 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3744
3745 acceptc = 0xdead;
3746 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
3747 ok (!ret, "getsockopt failed\n");
3748 ok (acceptc == 0, "SO_ACCEPTCONN should be 0, received %d\n", acceptc);
3749
3750 ok (!listen(fdA, 0), "listen failed\n");
3751 ok (!listen(fdA, SOMAXCONN), "double listen failed\n");
3752
3753 acceptc = 0xdead;
3754 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
3755 ok (!ret, "getsockopt failed\n");
3756 ok (acceptc == 1, "SO_ACCEPTCONN should be 1, received %d\n", acceptc);
3757
3758 SetLastError(0xdeadbeef);
3759 ok ((listen(fdB, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3760 ret = WSAGetLastError();
3761 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
3762
3763 ret = closesocket(fdB);
3764 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3765
3766 fdB = socket(AF_INET, SOCK_STREAM, 0);
3767 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3768
3769 SetLastError(0xdeadbeef);
3770 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
3771 ret = WSAGetLastError();
3772 ok (ret == WSAEADDRINUSE, "expected 10048, received %d\n", ret);
3773
3774 ret = closesocket(fdA);
3775 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3776 ret = closesocket(fdB);
3777 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
3778 }
3779
3780 #define FD_ZERO_ALL() { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); }
3781 #define FD_SET_ALL(s) { FD_SET(s, &readfds); FD_SET(s, &writefds); FD_SET(s, &exceptfds); }
test_select(void)3782 static void test_select(void)
3783 {
3784 static char tmp_buf[1024];
3785
3786 SOCKET fdListen, fdRead, fdWrite;
3787 fd_set readfds, writefds, exceptfds;
3788 unsigned int maxfd;
3789 int ret, len;
3790 char buffer;
3791 struct timeval select_timeout;
3792 struct sockaddr_in address;
3793 select_thread_params thread_params;
3794 HANDLE thread_handle;
3795 DWORD ticks, id;
3796
3797 fdRead = socket(AF_INET, SOCK_STREAM, 0);
3798 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3799 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
3800 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3801
3802 maxfd = fdRead;
3803 if (fdWrite > maxfd)
3804 maxfd = fdWrite;
3805
3806 FD_ZERO_ALL();
3807 FD_SET_ALL(fdRead);
3808 FD_SET_ALL(fdWrite);
3809 select_timeout.tv_sec=0;
3810 select_timeout.tv_usec=0;
3811
3812 ticks = GetTickCount();
3813 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3814 ticks = GetTickCount() - ticks;
3815 ok(ret == 0, "select should not return any socket handles\n");
3816 ok(ticks < 10, "select was blocking for %u ms, expected < 10 ms\n", ticks);
3817 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3818 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
3819 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3820 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
3821
3822 FD_ZERO_ALL();
3823 FD_SET_ALL(fdRead);
3824 FD_SET_ALL(fdWrite);
3825 select_timeout.tv_sec=0;
3826 select_timeout.tv_usec=500;
3827
3828 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3829 ok(ret == 0, "select should not return any socket handles\n");
3830 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3831 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
3832 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3833 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
3834
3835 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
3836 ret = closesocket(fdWrite);
3837 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
3838
3839 thread_params.s = fdRead;
3840 thread_params.ReadKilled = FALSE;
3841 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
3842 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id );
3843 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
3844
3845 WaitForSingleObject (server_ready, INFINITE);
3846 Sleep(200);
3847 ret = closesocket(fdRead);
3848 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
3849
3850 WaitForSingleObject (thread_handle, 1000);
3851 ok ( thread_params.ReadKilled, "closesocket did not wake up select\n");
3852 ret = recv(fdRead, &buffer, 1, MSG_PEEK);
3853 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret);
3854
3855 /* Test selecting invalid handles */
3856 FD_ZERO_ALL();
3857
3858 SetLastError(0);
3859 ret = select(maxfd+1, 0, 0, 0, &select_timeout);
3860 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3861 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3862
3863 SetLastError(0);
3864 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3865 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3866 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3867
3868 FD_SET(INVALID_SOCKET, &readfds);
3869 SetLastError(0);
3870 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3871 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3872 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3873 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
3874
3875 FD_ZERO(&readfds);
3876 FD_SET(INVALID_SOCKET, &writefds);
3877 SetLastError(0);
3878 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3879 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3880 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3881 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n");
3882
3883 FD_ZERO(&writefds);
3884 FD_SET(INVALID_SOCKET, &exceptfds);
3885 SetLastError(0);
3886 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
3887 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
3888 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3889 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
3890
3891 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
3892 maxfd = fdRead;
3893 if(fdWrite > maxfd) maxfd = fdWrite;
3894
3895 FD_ZERO(&readfds);
3896 FD_SET(fdRead, &readfds);
3897 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
3898 ok(!ret, "select returned %d\n", ret);
3899
3900 FD_ZERO(&writefds);
3901 FD_SET(fdWrite, &writefds);
3902 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
3903 ok(ret == 1, "select returned %d\n", ret);
3904 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3905
3906 /* tests for overlapping fd_set pointers */
3907 FD_ZERO(&readfds);
3908 FD_SET(fdWrite, &readfds);
3909 ret = select(fdWrite+1, &readfds, &readfds, NULL, &select_timeout);
3910 ok(ret == 1, "select returned %d\n", ret);
3911 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3912
3913 FD_ZERO(&readfds);
3914 FD_SET(fdWrite, &readfds);
3915 FD_SET(fdRead, &readfds);
3916 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3917 ok(ret == 2, "select returned %d\n", ret);
3918 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3919 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3920
3921 ok(send(fdWrite, "test", 4, 0) == 4, "failed to send data\n");
3922 FD_ZERO(&readfds);
3923 FD_SET(fdRead, &readfds);
3924 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
3925 ok(ret == 1, "select returned %d\n", ret);
3926 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3927
3928 FD_ZERO(&readfds);
3929 FD_SET(fdWrite, &readfds);
3930 FD_SET(fdRead, &readfds);
3931 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3932 ok(ret == 2, "select returned %d\n", ret);
3933 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3934 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3935
3936 #if ROSTESTS_233_IS_FIXED
3937 while(1) {
3938 FD_ZERO(&writefds);
3939 FD_SET(fdWrite, &writefds);
3940 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
3941 if(!ret) break;
3942 ok(send(fdWrite, tmp_buf, sizeof(tmp_buf), 0) > 0, "failed to send data\n");
3943 }
3944 #endif /* ROSTESTS_233_IS_FIXED */
3945
3946 FD_ZERO(&readfds);
3947 FD_SET(fdWrite, &readfds);
3948 FD_SET(fdRead, &readfds);
3949 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3950 ok(ret == 1, "select returned %d\n", ret);
3951 ok(!FD_ISSET(fdWrite, &readfds), "fdWrite socket is in the set\n");
3952 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3953
3954 ok(send(fdRead, "test", 4, 0) == 4, "failed to send data\n");
3955 Sleep(100);
3956 FD_ZERO(&readfds);
3957 FD_SET(fdWrite, &readfds);
3958 FD_SET(fdRead, &readfds);
3959 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3960 ok(ret == 2, "select returned %d\n", ret);
3961 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3962 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3963
3964 closesocket(fdRead);
3965 closesocket(fdWrite);
3966
3967 /* select() works in 3 distinct states:
3968 * - to check if a connection attempt ended with success or error;
3969 * - to check if a pending connection is waiting for acceptance;
3970 * - to check for data to read, availability for write and OOB data
3971 *
3972 * The tests below ensure that all conditions are tested.
3973 */
3974 memset(&address, 0, sizeof(address));
3975 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3976 address.sin_family = AF_INET;
3977 len = sizeof(address);
3978 fdListen = setup_server_socket(&address, &len);
3979 select_timeout.tv_sec = 1;
3980 select_timeout.tv_usec = 250000;
3981
3982 /* When no events are pending select returns 0 with no error */
3983 FD_ZERO_ALL();
3984 FD_SET_ALL(fdListen);
3985 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3986 ok(ret == 0, "expected 0, got %d\n", ret);
3987
3988 /* When a socket is attempting to connect the listening socket receives the read descriptor */
3989 fdWrite = setup_connector_socket(&address, len, TRUE);
3990 FD_ZERO_ALL();
3991 FD_SET_ALL(fdListen);
3992 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3993 ok(ret == 1, "expected 1, got %d\n", ret);
3994 ok(FD_ISSET(fdListen, &readfds), "fdListen socket is not in the set\n");
3995 len = sizeof(address);
3996 fdRead = accept(fdListen, (struct sockaddr*) &address, &len);
3997 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n");
3998
3999 /* The connector is signaled through the write descriptor */
4000 FD_ZERO_ALL();
4001 FD_SET_ALL(fdListen);
4002 FD_SET_ALL(fdRead);
4003 FD_SET_ALL(fdWrite);
4004 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4005 ok(ret == 2, "expected 2, got %d\n", ret);
4006 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
4007 ok(FD_ISSET(fdRead, &writefds), "fdRead socket is not in the set\n");
4008 len = sizeof(id);
4009 id = 0xdeadbeef;
4010 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
4011 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
4012 ok(id == 0, "expected 0, got %d\n", id);
4013
4014 /* When data is received the receiver gets the read descriptor */
4015 ret = send(fdWrite, "1234", 4, 0);
4016 ok(ret == 4, "expected 4, got %d\n", ret);
4017 FD_ZERO_ALL();
4018 FD_SET_ALL(fdListen);
4019 FD_SET(fdRead, &readfds);
4020 FD_SET(fdRead, &exceptfds);
4021 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4022 ok(ret == 1, "expected 1, got %d\n", ret);
4023 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
4024 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
4025 ok(ret == 4, "expected 4, got %d\n", ret);
4026 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n");
4027
4028 /* When OOB data is received the socket is set in the except descriptor */
4029 ret = send(fdWrite, "A", 1, MSG_OOB);
4030 ok(ret == 1, "expected 1, got %d\n", ret);
4031 FD_ZERO_ALL();
4032 FD_SET_ALL(fdListen);
4033 FD_SET(fdRead, &readfds);
4034 FD_SET(fdRead, &exceptfds);
4035 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4036 ok(ret == 1, "expected 1, got %d\n", ret);
4037 ok(FD_ISSET(fdRead, &exceptfds), "fdRead socket is not in the set\n");
4038 tmp_buf[0] = 0xAF;
4039 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
4040 ok(ret == 1, "expected 1, got %d\n", ret);
4041 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
4042
4043 /* If the socket is OOBINLINED it will not receive the OOB in except fds */
4044 ret = 1;
4045 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret));
4046 ok(ret == 0, "expected 0, got %d\n", ret);
4047 ret = send(fdWrite, "A", 1, MSG_OOB);
4048 ok(ret == 1, "expected 1, got %d\n", ret);
4049 FD_ZERO_ALL();
4050 FD_SET_ALL(fdListen);
4051 FD_SET(fdRead, &readfds);
4052 FD_SET(fdRead, &exceptfds);
4053 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4054 ok(ret == 1, "expected 1, got %d\n", ret);
4055 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
4056 tmp_buf[0] = 0xAF;
4057 SetLastError(0xdeadbeef);
4058 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
4059 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
4060 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError());
4061 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
4062 ok(ret == 1, "expected 1, got %d\n", ret);
4063 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
4064
4065 /* When the connection is closed the socket is set in the read descriptor */
4066 ret = closesocket(fdRead);
4067 ok(ret == 0, "expected 0, got %d\n", ret);
4068 FD_ZERO_ALL();
4069 FD_SET_ALL(fdListen);
4070 FD_SET(fdWrite, &readfds);
4071 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4072 ok(ret == 1, "expected 1, got %d\n", ret);
4073 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
4074 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0);
4075 ok(ret == 0, "expected 0, got %d\n", ret);
4076
4077 /* When a connection is attempted to a non-listening socket it will get to the except descriptor */
4078 ret = closesocket(fdWrite);
4079 ok(ret == 0, "expected 0, got %d\n", ret);
4080 ret = closesocket(fdListen);
4081 ok(ret == 0, "expected 0, got %d\n", ret);
4082 len = sizeof(address);
4083 fdWrite = setup_connector_socket(&address, len, TRUE);
4084 FD_ZERO_ALL();
4085 FD_SET(fdWrite, &writefds);
4086 FD_SET(fdWrite, &exceptfds);
4087 select_timeout.tv_sec = 2; /* requires more time to realize it will not connect */
4088 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4089 ok(ret == 1, "expected 1, got %d\n", ret);
4090 len = sizeof(id);
4091 id = 0xdeadbeef;
4092 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
4093 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
4094 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id);
4095 ok(FD_ISSET(fdWrite, &exceptfds), "fdWrite socket is not in the set\n");
4096 ok(select_timeout.tv_usec == 250000, "select timeout should not have changed\n");
4097 closesocket(fdWrite);
4098
4099 /* Try select() on a closed socket after connection */
4100 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
4101 closesocket(fdRead);
4102 FD_ZERO_ALL();
4103 FD_SET_ALL(fdWrite);
4104 FD_SET_ALL(fdRead);
4105 SetLastError(0xdeadbeef);
4106 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
4107 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
4108 todo_wine
4109 ok(GetLastError() == WSAENOTSOCK, "expected 10038, got %d\n", GetLastError());
4110 /* descriptor sets are unchanged */
4111 ok(readfds.fd_count == 2, "expected 2, got %d\n", readfds.fd_count);
4112 ok(exceptfds.fd_count == 2, "expected 2, got %d\n", exceptfds.fd_count);
4113 closesocket(fdWrite);
4114
4115 #if ROSTESTS_233_IS_FIXED
4116
4117 /* Close the socket currently being selected in a thread - bug 38399 */
4118 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
4119 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
4120 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
4121 FD_ZERO_ALL();
4122 FD_SET_ALL(fdWrite);
4123 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
4124 ok(ret == 1, "expected 1, got %d\n", ret);
4125 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
4126 WaitForSingleObject (thread_handle, 1000);
4127 closesocket(fdRead);
4128 /* test again with only the except descriptor */
4129 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
4130 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
4131 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
4132 FD_ZERO_ALL();
4133 FD_SET(fdWrite, &exceptfds);
4134 SetLastError(0xdeadbeef);
4135 ret = select(0, NULL, NULL, &exceptfds, &select_timeout);
4136 todo_wine
4137 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
4138 todo_wine
4139 ok(GetLastError() == WSAENOTSOCK, "expected 10038, got %d\n", GetLastError());
4140 WaitForSingleObject (thread_handle, 1000);
4141 closesocket(fdRead);
4142
4143 #endif /* ROSTESTS_233_IS_FIXED */
4144
4145 /* test UDP behavior of unbound sockets */
4146 select_timeout.tv_sec = 0;
4147 select_timeout.tv_usec = 250000;
4148 fdWrite = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
4149 ok(fdWrite != INVALID_SOCKET, "socket call failed\n");
4150 FD_ZERO_ALL();
4151 FD_SET_ALL(fdWrite);
4152 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
4153 ok(ret == 1, "expected 1, got %d\n", ret);
4154 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
4155 closesocket(fdWrite);
4156 }
4157 #undef FD_SET_ALL
4158 #undef FD_ZERO_ALL
4159
AcceptKillThread(void * param)4160 static DWORD WINAPI AcceptKillThread(void *param)
4161 {
4162 select_thread_params *par = param;
4163 struct sockaddr_in address;
4164 int len = sizeof(address);
4165 SOCKET client_socket;
4166
4167 SetEvent(server_ready);
4168 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
4169 if (client_socket != INVALID_SOCKET)
4170 closesocket(client_socket);
4171 par->ReadKilled = (client_socket == INVALID_SOCKET);
4172 return 0;
4173 }
4174
4175
AlwaysDeferConditionFunc(LPWSABUF lpCallerId,LPWSABUF lpCallerData,LPQOS pQos,LPQOS lpGQOS,LPWSABUF lpCalleeId,LPWSABUF lpCalleeData,GROUP * g,DWORD_PTR dwCallbackData)4176 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
4177 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
4178 GROUP *g, DWORD_PTR dwCallbackData)
4179 {
4180 return CF_DEFER;
4181 }
4182
setup_server_socket(struct sockaddr_in * addr,int * len)4183 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len)
4184 {
4185 int ret, val;
4186 SOCKET server_socket;
4187
4188 server_socket = socket(AF_INET, SOCK_STREAM, 0);
4189 if (server_socket == INVALID_SOCKET)
4190 {
4191 trace("error creating server socket: %d\n", WSAGetLastError());
4192 return INVALID_SOCKET;
4193 }
4194
4195 val = 1;
4196 ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
4197 if (ret)
4198 {
4199 trace("error setting SO_REUSEADDR: %d\n", WSAGetLastError());
4200 closesocket(server_socket);
4201 return INVALID_SOCKET;
4202 }
4203
4204 ret = bind(server_socket, (struct sockaddr *)addr, *len);
4205 if (ret)
4206 {
4207 trace("error binding server socket: %d\n", WSAGetLastError());
4208 }
4209
4210 ret = getsockname(server_socket, (struct sockaddr *)addr, len);
4211 if (ret)
4212 {
4213 skip("failed to lookup bind address: %d\n", WSAGetLastError());
4214 closesocket(server_socket);
4215 return INVALID_SOCKET;
4216 }
4217
4218 ret = listen(server_socket, 5);
4219 if (ret)
4220 {
4221 trace("error making server socket listen: %d\n", WSAGetLastError());
4222 closesocket(server_socket);
4223 return INVALID_SOCKET;
4224 }
4225
4226 return server_socket;
4227 }
4228
setup_connector_socket(struct sockaddr_in * addr,int len,BOOL nonblock)4229 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock)
4230 {
4231 int ret;
4232 SOCKET connector;
4233
4234 connector = socket(AF_INET, SOCK_STREAM, 0);
4235 ok(connector != INVALID_SOCKET, "failed to create connector socket %d\n", WSAGetLastError());
4236
4237 if (nonblock)
4238 set_blocking(connector, !nonblock);
4239
4240 ret = connect(connector, (struct sockaddr *)addr, len);
4241 if (!nonblock)
4242 ok(!ret, "connecting to accepting socket failed %d\n", WSAGetLastError());
4243 else if (ret == SOCKET_ERROR)
4244 {
4245 DWORD error = WSAGetLastError();
4246 ok(error == WSAEWOULDBLOCK || error == WSAEINPROGRESS,
4247 "expected 10035 or 10036, got %d\n", error);
4248 }
4249
4250 return connector;
4251 }
4252
test_accept(void)4253 static void test_accept(void)
4254 {
4255 int ret;
4256 SOCKET server_socket, accepted = INVALID_SOCKET, connector;
4257 struct sockaddr_in address;
4258 SOCKADDR_STORAGE ss, ss_empty;
4259 int socklen;
4260 select_thread_params thread_params;
4261 HANDLE thread_handle = NULL;
4262 DWORD id;
4263
4264 memset(&address, 0, sizeof(address));
4265 address.sin_addr.s_addr = inet_addr("127.0.0.1");
4266 address.sin_family = AF_INET;
4267
4268 socklen = sizeof(address);
4269 server_socket = setup_server_socket(&address, &socklen);
4270 if (server_socket == INVALID_SOCKET)
4271 {
4272 trace("error creating server socket: %d\n", WSAGetLastError());
4273 return;
4274 }
4275
4276 connector = setup_connector_socket(&address, socklen, FALSE);
4277 if (connector == INVALID_SOCKET) goto done;
4278
4279 trace("Blocking accept next\n");
4280
4281 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0);
4282 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
4283
4284 accepted = accept(server_socket, NULL, 0);
4285 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
4286
4287 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
4288 if (server_ready == INVALID_HANDLE_VALUE)
4289 {
4290 trace("error creating event: %d\n", GetLastError());
4291 goto done;
4292 }
4293
4294 thread_params.s = server_socket;
4295 thread_params.ReadKilled = FALSE;
4296 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id);
4297 if (thread_handle == NULL)
4298 {
4299 trace("error creating thread: %d\n", GetLastError());
4300 goto done;
4301 }
4302
4303 WaitForSingleObject(server_ready, INFINITE);
4304 Sleep(200);
4305 ret = closesocket(server_socket);
4306 if (ret != 0)
4307 {
4308 trace("closesocket failed: %d\n", WSAGetLastError());
4309 goto done;
4310 }
4311
4312 WaitForSingleObject(thread_handle, 1000);
4313 ok(thread_params.ReadKilled, "closesocket did not wake up accept\n");
4314
4315 closesocket(accepted);
4316 closesocket(connector);
4317 accepted = connector = INVALID_SOCKET;
4318
4319 socklen = sizeof(address);
4320 server_socket = setup_server_socket(&address, &socklen);
4321 if (server_socket == INVALID_SOCKET) goto done;
4322
4323 connector = setup_connector_socket(&address, socklen, FALSE);
4324 if (connector == INVALID_SOCKET) goto done;
4325
4326 socklen = 0;
4327 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
4328 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
4329 ok(!socklen, "got %d\n", socklen);
4330 closesocket(connector);
4331 connector = INVALID_SOCKET;
4332
4333 socklen = sizeof(address);
4334 connector = setup_connector_socket(&address, socklen, FALSE);
4335 if (connector == INVALID_SOCKET) goto done;
4336
4337 accepted = WSAAccept(server_socket, NULL, NULL, NULL, 0);
4338 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
4339 closesocket(accepted);
4340 closesocket(connector);
4341 accepted = connector = INVALID_SOCKET;
4342
4343 socklen = sizeof(address);
4344 connector = setup_connector_socket(&address, socklen, FALSE);
4345 if (connector == INVALID_SOCKET) goto done;
4346
4347 socklen = sizeof(ss);
4348 memset(&ss, 0, sizeof(ss));
4349 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
4350 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
4351 ok(socklen != sizeof(ss), "unexpected length\n");
4352 ok(ss.ss_family, "family not set\n");
4353 closesocket(accepted);
4354 closesocket(connector);
4355 accepted = connector = INVALID_SOCKET;
4356
4357 socklen = sizeof(address);
4358 connector = setup_connector_socket(&address, socklen, FALSE);
4359 if (connector == INVALID_SOCKET) goto done;
4360
4361 socklen = 0;
4362 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
4363 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
4364 ok(!socklen, "got %d\n", socklen);
4365 closesocket(connector);
4366 accepted = connector = INVALID_SOCKET;
4367
4368 socklen = sizeof(address);
4369 connector = setup_connector_socket(&address, socklen, FALSE);
4370 if (connector == INVALID_SOCKET) goto done;
4371
4372 accepted = accept(server_socket, NULL, NULL);
4373 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
4374 closesocket(accepted);
4375 closesocket(connector);
4376 accepted = connector = INVALID_SOCKET;
4377
4378 socklen = sizeof(address);
4379 connector = setup_connector_socket(&address, socklen, FALSE);
4380 if (connector == INVALID_SOCKET) goto done;
4381
4382 socklen = sizeof(ss);
4383 memset(&ss, 0, sizeof(ss));
4384 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
4385 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
4386 ok(socklen != sizeof(ss), "unexpected length\n");
4387 ok(ss.ss_family, "family not set\n");
4388 closesocket(accepted);
4389 closesocket(connector);
4390 accepted = connector = INVALID_SOCKET;
4391
4392 socklen = sizeof(address);
4393 connector = setup_connector_socket(&address, socklen, FALSE);
4394 if (connector == INVALID_SOCKET) goto done;
4395
4396 memset(&ss, 0, sizeof(ss));
4397 memset(&ss_empty, 0, sizeof(ss_empty));
4398 accepted = accept(server_socket, (struct sockaddr *)&ss, NULL);
4399 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
4400 ok(!memcmp(&ss, &ss_empty, sizeof(ss)), "structure is different\n");
4401
4402 done:
4403 if (accepted != INVALID_SOCKET)
4404 closesocket(accepted);
4405 if (connector != INVALID_SOCKET)
4406 closesocket(connector);
4407 if (thread_handle != NULL)
4408 CloseHandle(thread_handle);
4409 if (server_ready != INVALID_HANDLE_VALUE)
4410 CloseHandle(server_ready);
4411 if (server_socket != INVALID_SOCKET)
4412 closesocket(server_socket);
4413 }
4414
test_extendedSocketOptions(void)4415 static void test_extendedSocketOptions(void)
4416 {
4417 WSADATA wsa;
4418 SOCKET sock;
4419 struct sockaddr_in sa;
4420 int sa_len = sizeof(struct sockaddr_in);
4421 int optval, optlen = sizeof(int), ret;
4422 BOOL bool_opt_val;
4423 LINGER linger_val;
4424
4425 if(WSAStartup(MAKEWORD(2,0), &wsa)){
4426 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
4427 return;
4428 }
4429
4430 memset(&sa, 0, sa_len);
4431
4432 sa.sin_family = AF_INET;
4433 sa.sin_port = htons(0);
4434 sa.sin_addr.s_addr = htonl(INADDR_ANY);
4435
4436 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
4437 trace("Creating the socket failed: %d\n", WSAGetLastError());
4438 WSACleanup();
4439 return;
4440 }
4441
4442 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
4443 trace("Failed to bind socket: %d\n", WSAGetLastError());
4444 closesocket(sock);
4445 WSACleanup();
4446 return;
4447 }
4448
4449 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4450
4451 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
4452 ok((optval == 65507) || (optval == 65527),
4453 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
4454
4455 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
4456 SetLastError(0xdeadbeef);
4457 optval = 0xdeadbeef;
4458 optlen = sizeof(int);
4459 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4460 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
4461 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4462 ret, WSAGetLastError(), optval, optval);
4463
4464 /* more invalid values for level */
4465 SetLastError(0xdeadbeef);
4466 optval = 0xdeadbeef;
4467 optlen = sizeof(int);
4468 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4469 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
4470 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4471 ret, WSAGetLastError(), optval, optval);
4472
4473 SetLastError(0xdeadbeef);
4474 optval = 0xdeadbeef;
4475 optlen = sizeof(int);
4476 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4477 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
4478 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4479 ret, WSAGetLastError(), optval, optval);
4480
4481 SetLastError(0xdeadbeef);
4482 optval = 0xdeadbeef;
4483 optlen = sizeof(int);
4484 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4485 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
4486 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4487 ret, WSAGetLastError(), optval, optval);
4488
4489 SetLastError(0xdeadbeef);
4490 optval = 0xdeadbeef;
4491 optlen = sizeof(int);
4492 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
4493 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
4494 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4495 ret, WSAGetLastError(), optval, optval);
4496
4497 SetLastError(0xdeadbeef);
4498 optlen = sizeof(LINGER);
4499 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
4500 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT),
4501 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
4502 ret, WSAGetLastError());
4503 closesocket(sock);
4504
4505 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
4506 trace("Creating the socket failed: %d\n", WSAGetLastError());
4507 WSACleanup();
4508 return;
4509 }
4510
4511 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
4512 trace("Failed to bind socket: %d\n", WSAGetLastError());
4513 closesocket(sock);
4514 WSACleanup();
4515 return;
4516 }
4517
4518 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
4519 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
4520
4521 optlen = sizeof(BOOL);
4522 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
4523 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
4524 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
4525 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
4526 bool_opt_val, linger_val.l_onoff);
4527
4528 closesocket(sock);
4529 WSACleanup();
4530 }
4531
test_getsockname(void)4532 static void test_getsockname(void)
4533 {
4534 WSADATA wsa;
4535 SOCKET sock;
4536 struct sockaddr_in sa_set, sa_get;
4537 int sa_set_len = sizeof(struct sockaddr_in);
4538 int sa_get_len = sa_set_len;
4539 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
4540 int ret;
4541 struct hostent *h;
4542
4543 if(WSAStartup(MAKEWORD(2,0), &wsa)){
4544 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
4545 return;
4546 }
4547
4548 memset(&sa_set, 0, sa_set_len);
4549
4550 sa_set.sin_family = AF_INET;
4551 sa_set.sin_port = htons(0);
4552 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
4553
4554 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
4555 trace("Creating the socket failed: %d\n", WSAGetLastError());
4556 WSACleanup();
4557 return;
4558 }
4559
4560 sa_get = sa_set;
4561 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
4562 ok(0, "getsockname on unbound socket should fail\n");
4563 else {
4564 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
4565 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
4566 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
4567 "failed getsockname modified sockaddr when it shouldn't\n");
4568 }
4569
4570 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
4571 trace("Failed to bind socket: %d\n", WSAGetLastError());
4572 closesocket(sock);
4573 WSACleanup();
4574 return;
4575 }
4576
4577 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
4578 trace("Failed to call getsockname: %d\n", WSAGetLastError());
4579 closesocket(sock);
4580 WSACleanup();
4581 return;
4582 }
4583
4584 ret = memcmp(sa_get.sin_zero, null_padding, 8);
4585 ok(ret == 0, "getsockname did not zero the sockaddr_in structure\n");
4586
4587 closesocket(sock);
4588
4589 h = gethostbyname("");
4590 if (h && h->h_length == 4) /* this test is only meaningful in IPv4 */
4591 {
4592 int i;
4593 for (i = 0; h->h_addr_list[i]; i++)
4594 {
4595 char ipstr[32];
4596 struct in_addr ip;
4597 ip.s_addr = *(ULONG *) h->h_addr_list[i];
4598
4599 sock = socket(AF_INET, SOCK_DGRAM, 0);
4600 ok(sock != INVALID_SOCKET, "socket failed with %d\n", GetLastError());
4601
4602 memset(&sa_set, 0, sizeof(sa_set));
4603 sa_set.sin_family = AF_INET;
4604 sa_set.sin_addr.s_addr = ip.s_addr;
4605 /* The same address we bind must be the same address we get */
4606 ret = bind(sock, (struct sockaddr*)&sa_set, sizeof(sa_set));
4607 ok(ret == 0, "bind failed with %d\n", GetLastError());
4608 sa_get_len = sizeof(sa_get);
4609 ret = getsockname(sock, (struct sockaddr*)&sa_get, &sa_get_len);
4610 ok(ret == 0, "getsockname failed with %d\n", GetLastError());
4611 strcpy(ipstr, inet_ntoa(sa_get.sin_addr));
4612 trace("testing bind on interface %s\n", ipstr);
4613 ok(sa_get.sin_addr.s_addr == sa_set.sin_addr.s_addr,
4614 "address does not match: %s != %s\n", ipstr, inet_ntoa(sa_set.sin_addr));
4615
4616 closesocket(sock);
4617 }
4618 }
4619
4620 WSACleanup();
4621 }
4622
test_dns(void)4623 static void test_dns(void)
4624 {
4625 struct hostent *h;
4626 union memaddress
4627 {
4628 char *chr;
4629 void *mem;
4630 } addr;
4631 char **ptr;
4632 int acount;
4633
4634 h = gethostbyname("");
4635 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
4636
4637 /* Use an address with valid alias names if possible */
4638 h = gethostbyname("source.winehq.org");
4639 if(!h)
4640 {
4641 skip("Can't test the hostent structure because gethostbyname failed\n");
4642 return;
4643 }
4644
4645 /* The returned struct must be allocated in a very strict way. First we need to
4646 * count how many aliases there are because they must be located right after
4647 * the struct hostent size. Knowing the amount of aliases we know the exact
4648 * location of the first IP returned. Rule valid for >= XP, for older OS's
4649 * it's somewhat the opposite. */
4650 addr.mem = h + 1;
4651 if(h->h_addr_list == addr.mem) /* <= W2K */
4652 {
4653 win_skip("Skipping hostent tests since this OS is unsupported\n");
4654 return;
4655 }
4656
4657 ok(h->h_aliases == addr.mem,
4658 "hostent->h_aliases should be in %p, it is in %p\n", addr.mem, h->h_aliases);
4659
4660 for(ptr = h->h_aliases, acount = 1; *ptr; ptr++) acount++;
4661 addr.chr += sizeof(*ptr) * acount;
4662 ok(h->h_addr_list == addr.mem,
4663 "hostent->h_addr_list should be in %p, it is in %p\n", addr.mem, h->h_addr_list);
4664
4665 for(ptr = h->h_addr_list, acount = 1; *ptr; ptr++) acount++;
4666
4667 addr.chr += sizeof(*ptr) * acount;
4668 ok(h->h_addr_list[0] == addr.mem,
4669 "hostent->h_addr_list[0] should be in %p, it is in %p\n", addr.mem, h->h_addr_list[0]);
4670 }
4671
4672 #ifndef __REACTOS__
4673 /* Our winsock headers don't define gethostname because it conflicts with the
4674 * definition in unistd.h. Define it here to get rid of the warning. */
4675
4676 int WINAPI gethostname(char *name, int namelen);
4677 #endif
4678
test_gethostbyname(void)4679 static void test_gethostbyname(void)
4680 {
4681 struct hostent *he;
4682 struct in_addr **addr_list;
4683 char name[256], first_ip[16];
4684 int ret, i, count;
4685 PMIB_IPFORWARDTABLE routes = NULL;
4686 PIP_ADAPTER_INFO adapters = NULL, k;
4687 DWORD adap_size = 0, route_size = 0;
4688 BOOL found_default = FALSE;
4689 BOOL local_ip = FALSE;
4690
4691 ret = gethostname(name, sizeof(name));
4692 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4693
4694 he = gethostbyname(name);
4695 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4696 #ifdef __REACTOS__ /* ROSTESTS-233 */
4697 count = 0;
4698 if (he != NULL)
4699 {
4700 #endif
4701 addr_list = (struct in_addr **)he->h_addr_list;
4702 strcpy(first_ip, inet_ntoa(*addr_list[0]));
4703
4704 trace("List of local IPs:\n");
4705 for(count = 0; addr_list[count] != NULL; count++)
4706 {
4707 char *ip = inet_ntoa(*addr_list[count]);
4708 if (!strcmp(ip, "127.0.0.1"))
4709 local_ip = TRUE;
4710 trace("%s\n", ip);
4711 }
4712 #ifdef __REACTOS__ /* ROSTESTS-233 */
4713 }
4714 #endif
4715
4716 if (local_ip)
4717 {
4718 ok (count == 1, "expected 127.0.0.1 to be the only IP returned\n");
4719 skip("Only the loopback address is present, skipping tests\n");
4720 return;
4721 }
4722
4723 if (!pGetAdaptersInfo || !pGetIpForwardTable)
4724 {
4725 win_skip("GetAdaptersInfo and/or GetIpForwardTable not found, skipping tests\n");
4726 return;
4727 }
4728
4729 ret = pGetAdaptersInfo(NULL, &adap_size);
4730 ok (ret == ERROR_BUFFER_OVERFLOW, "GetAdaptersInfo failed with a different error: %d\n", ret);
4731 ret = pGetIpForwardTable(NULL, &route_size, FALSE);
4732 ok (ret == ERROR_INSUFFICIENT_BUFFER, "GetIpForwardTable failed with a different error: %d\n", ret);
4733
4734 adapters = HeapAlloc(GetProcessHeap(), 0, adap_size);
4735 routes = HeapAlloc(GetProcessHeap(), 0, route_size);
4736
4737 ret = pGetAdaptersInfo(adapters, &adap_size);
4738 ok (ret == NO_ERROR, "GetAdaptersInfo failed, error: %d\n", ret);
4739 ret = pGetIpForwardTable(routes, &route_size, FALSE);
4740 ok (ret == NO_ERROR, "GetIpForwardTable failed, error: %d\n", ret);
4741
4742 /* This test only has meaning if there is more than one IP configured */
4743 if (adapters->Next == NULL && count == 1)
4744 {
4745 skip("Only one IP is present, skipping tests\n");
4746 goto cleanup;
4747 }
4748
4749 for (i = 0; !found_default && i < routes->dwNumEntries; i++)
4750 {
4751 /* default route (ip 0.0.0.0) ? */
4752 if (routes->table[i].dwForwardDest) continue;
4753
4754 for (k = adapters; k != NULL; k = k->Next)
4755 {
4756 char *ip;
4757
4758 if (k->Index != routes->table[i].dwForwardIfIndex) continue;
4759
4760 /* the first IP returned from gethostbyname must be a default route */
4761 ip = k->IpAddressList.IpAddress.String;
4762 if (!strcmp(first_ip, ip))
4763 {
4764 found_default = TRUE;
4765 break;
4766 }
4767 }
4768 }
4769 ok (found_default, "failed to find the first IP from gethostbyname!\n");
4770
4771 cleanup:
4772 HeapFree(GetProcessHeap(), 0, adapters);
4773 HeapFree(GetProcessHeap(), 0, routes);
4774 }
4775
test_gethostbyname_hack(void)4776 static void test_gethostbyname_hack(void)
4777 {
4778 struct hostent *he;
4779 char name[256];
4780 static BYTE loopback[] = {127, 0, 0, 1};
4781 static BYTE magic_loopback[] = {127, 12, 34, 56};
4782 int ret;
4783
4784 ret = gethostname(name, 256);
4785 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4786
4787 he = gethostbyname("localhost");
4788 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
4789 if(he)
4790 {
4791 if(he->h_length != 4)
4792 {
4793 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
4794 return;
4795 }
4796
4797 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
4798 "gethostbyname(\"localhost\") returned %u.%u.%u.%u\n",
4799 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
4800 he->h_addr_list[0][3]);
4801 }
4802
4803 if(strcmp(name, "localhost") == 0)
4804 {
4805 skip("hostname seems to be \"localhost\", skipping test.\n");
4806 return;
4807 }
4808
4809 he = gethostbyname(name);
4810 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
4811 if(he)
4812 {
4813 if(he->h_length != 4)
4814 {
4815 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
4816 return;
4817 }
4818
4819 if (he->h_addr_list[0][0] == 127)
4820 {
4821 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
4822 "gethostbyname(\"%s\") returned %u.%u.%u.%u not 127.12.34.56\n",
4823 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
4824 he->h_addr_list[0][2], he->h_addr_list[0][3]);
4825 }
4826 }
4827
4828 gethostbyname("nonexistent.winehq.org");
4829 /* Don't check for the return value, as some braindead ISPs will kindly
4830 * resolve nonexistent host names to addresses of the ISP's spam pages. */
4831 }
4832
test_gethostname(void)4833 static void test_gethostname(void)
4834 {
4835 struct hostent *he;
4836 char name[256];
4837 int ret, len;
4838
4839 WSASetLastError(0xdeadbeef);
4840 ret = gethostname(NULL, 256);
4841 ok(ret == -1, "gethostname() returned %d\n", ret);
4842 ok(WSAGetLastError() == WSAEFAULT, "gethostname with null buffer "
4843 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
4844
4845 ret = gethostname(name, sizeof(name));
4846 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4847 he = gethostbyname(name);
4848 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4849
4850 len = strlen(name);
4851 WSASetLastError(0xdeadbeef);
4852 strcpy(name, "deadbeef");
4853 ret = gethostname(name, len);
4854 ok(ret == -1, "gethostname() returned %d\n", ret);
4855 ok(!strcmp(name, "deadbeef"), "name changed unexpected!\n");
4856 ok(WSAGetLastError() == WSAEFAULT, "gethostname with insufficient length "
4857 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
4858
4859 len++;
4860 ret = gethostname(name, len);
4861 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
4862 he = gethostbyname(name);
4863 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
4864 }
4865
test_inet_addr(void)4866 static void test_inet_addr(void)
4867 {
4868 u_long addr;
4869
4870 addr = inet_addr(NULL);
4871 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
4872 }
4873
test_addr_to_print(void)4874 static void test_addr_to_print(void)
4875 {
4876 char dst[16];
4877 char dst6[64];
4878 const char * pdst;
4879 struct in_addr in;
4880 struct in6_addr in6;
4881
4882 u_long addr0_Num = 0x00000000;
4883 PCSTR addr0_Str = "0.0.0.0";
4884 u_long addr1_Num = 0x20201015;
4885 PCSTR addr1_Str = "21.16.32.32";
4886 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
4887 PCSTR addr2_Str = "::fffe:cc98:bd74";
4888 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
4889 PCSTR addr3_Str = "2030:a4b1::";
4890 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
4891 PCSTR addr4_Str = "::204.152.189.116";
4892
4893 /* Test IPv4 addresses */
4894 in.s_addr = addr0_Num;
4895
4896 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
4897 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
4898 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
4899
4900 /* Test that inet_ntoa and inet_ntop return the same value */
4901 in.S_un.S_addr = addr1_Num;
4902 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
4903 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
4904 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4905
4906 /* inet_ntop became available in Vista and Win2008 */
4907 if (!p_inet_ntop)
4908 {
4909 win_skip("InetNtop not present, not executing tests\n");
4910 return;
4911 }
4912
4913 /* Second part of test */
4914 pdst = p_inet_ntop(AF_INET, &in.s_addr, dst, sizeof(dst));
4915 ok(pdst != NULL, "InetNtop failed %s\n", dst);
4916 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4917
4918 /* Test invalid parm conditions */
4919 pdst = p_inet_ntop(1, (void *)&in.s_addr, dst, sizeof(dst));
4920 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4921 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
4922
4923 /* Test Null destination */
4924 pdst = NULL;
4925 pdst = p_inet_ntop(AF_INET, &in.s_addr, NULL, sizeof(dst));
4926 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4927 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4928 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4929
4930 /* Test zero length passed */
4931 WSASetLastError(0);
4932 pdst = NULL;
4933 pdst = p_inet_ntop(AF_INET, &in.s_addr, dst, 0);
4934 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4935 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4936 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4937
4938 /* Test length one shorter than the address length */
4939 WSASetLastError(0);
4940 pdst = NULL;
4941 pdst = p_inet_ntop(AF_INET, &in.s_addr, dst, 6);
4942 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4943 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4944 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4945
4946 /* Test longer length is ok */
4947 WSASetLastError(0);
4948 pdst = NULL;
4949 pdst = p_inet_ntop(AF_INET, &in.s_addr, dst, sizeof(dst)+1);
4950 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
4951 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
4952
4953 /* Test the IPv6 addresses */
4954
4955 /* Test an zero prefixed IPV6 address */
4956 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
4957 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, sizeof(dst6));
4958 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4959 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
4960
4961 /* Test an zero suffixed IPV6 address */
4962 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
4963 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, sizeof(dst6));
4964 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4965 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
4966
4967 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
4968 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num));
4969 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, sizeof(dst6));
4970 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
4971 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str);
4972
4973 /* Test invalid parm conditions */
4974 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
4975
4976 /* Test Null destination */
4977 pdst = NULL;
4978 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, NULL, sizeof(dst6));
4979 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4980 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4981 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4982
4983 /* Test zero length passed */
4984 WSASetLastError(0);
4985 pdst = NULL;
4986 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, 0);
4987 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4988 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4989 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4990
4991 /* Test length one shorter than the address length */
4992 WSASetLastError(0);
4993 pdst = NULL;
4994 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, 16);
4995 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
4996 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
4997 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4998
4999 /* Test longer length is ok */
5000 WSASetLastError(0);
5001 pdst = NULL;
5002 pdst = p_inet_ntop(AF_INET6, &in6.s6_addr, dst6, 18);
5003 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
5004 }
test_inet_pton(void)5005 static void test_inet_pton(void)
5006 {
5007 static const struct
5008 {
5009 char input[32];
5010 int ret;
5011 DWORD addr;
5012 }
5013 ipv4_tests[] =
5014 {
5015 {"", 0, 0xdeadbeef},
5016 {" ", 0, 0xdeadbeef},
5017 {"1.1.1.1", 1, 0x01010101},
5018 {"0.0.0.0", 1, 0x00000000},
5019 {"127.127.127.255", 1, 0xff7f7f7f},
5020 {"127.127.127.255:123", 0, 0xff7f7f7f},
5021 {"127.127.127.256", 0, 0xdeadbeef},
5022 {"a", 0, 0xdeadbeef},
5023 {"1.1.1.0xaA", 0, 0xdeadbeef},
5024 {"1.1.1.0x", 0, 0xdeadbeef},
5025 {"1.1.1.010", 0, 0xdeadbeef},
5026 {"1.1.1.00", 0, 0xdeadbeef},
5027 {"1.1.1.0a", 0, 0x00010101},
5028 {"1.1.1.0o10", 0, 0x00010101},
5029 {"1.1.1.0b10", 0, 0x00010101},
5030 {"1.1.1.-2", 0, 0xdeadbeef},
5031 {"1", 0, 0xdeadbeef},
5032 {"1.2", 0, 0xdeadbeef},
5033 {"1.2.3", 0, 0xdeadbeef},
5034 {"203569230", 0, 0xdeadbeef},
5035 {"3.4.5.6.7", 0, 0xdeadbeef},
5036 {" 3.4.5.6", 0, 0xdeadbeef},
5037 {"\t3.4.5.6", 0, 0xdeadbeef},
5038 {"3.4.5.6 ", 0, 0x06050403},
5039 {"3. 4.5.6", 0, 0xdeadbeef},
5040 {"[0.1.2.3]", 0, 0xdeadbeef},
5041 {"0x00010203", 0, 0xdeadbeef},
5042 {"0x2134", 0, 0xdeadbeef},
5043 {"1234BEEF", 0, 0xdeadbeef},
5044 {"017700000001", 0, 0xdeadbeef},
5045 {"0777", 0, 0xdeadbeef},
5046 {"2607:f0d0:1002:51::4", 0, 0xdeadbeef},
5047 {"::177.32.45.20", 0, 0xdeadbeef},
5048 {"::1/128", 0, 0xdeadbeef},
5049 {"::1", 0, 0xdeadbeef},
5050 {":1", 0, 0xdeadbeef},
5051 };
5052
5053 static const struct
5054 {
5055 char input[64];
5056 int ret;
5057 unsigned short addr[8];
5058 int broken;
5059 int broken_ret;
5060 }
5061 ipv6_tests[] =
5062 {
5063 {"0000:0000:0000:0000:0000:0000:0000:0000", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5064 {"0000:0000:0000:0000:0000:0000:0000:0001", 1, {0, 0, 0, 0, 0, 0, 0, 0x100}},
5065 {"0:0:0:0:0:0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5066 {"0:0:0:0:0:0:0:1", 1, {0, 0, 0, 0, 0, 0, 0, 0x100}},
5067 {"0:0:0:0:0:0:0::", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5068 {"0:0:0:0:0:0:13.1.68.3", 1, {0, 0, 0, 0, 0, 0, 0x10d, 0x344}},
5069 {"0:0:0:0:0:0::", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5070 {"0:0:0:0:0::", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5071 {"0:0:0:0:0:FFFF:129.144.52.38", 1, {0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634}},
5072 {"0::", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5073 {"0:1:2:3:4:5:6:7", 1, {0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700}},
5074 {"1080:0:0:0:8:800:200c:417a", 1, {0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41}},
5075 {"0:a:b:c:d:e:f::", 1, {0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00, 0}},
5076 {"1111:2222:3333:4444:5555:6666:123.123.123.123", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b}},
5077 {"1111:2222:3333:4444:5555:6666:7777:8888", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5078 {"1111:2222:3333:4444:0x5555:6666:7777:8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab}},
5079 {"1111:2222:3333:4444:x555:6666:7777:8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab}},
5080 {"1111:2222:3333:4444:0r5555:6666:7777:8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab}},
5081 {"1111:2222:3333:4444:r5555:6666:7777:8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab}},
5082 {"1111:2222:3333:4444:5555:6666:7777::", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0}},
5083 {"1111:2222:3333:4444:5555:6666::", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0}},
5084 {"1111:2222:3333:4444:5555:6666::8888", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888}},
5085 {"1111:2222:3333:4444:5555:6666::7777:8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x7777}},
5086 {"1111:2222:3333:4444:5555:6666:7777::8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0}},
5087 {"1111:2222:3333:4444:5555::", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0}},
5088 {"1111:2222:3333:4444:5555::123.123.123.123", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b}},
5089 {"1111:2222:3333:4444:5555::0x1.123.123.123", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x100}},
5090 {"1111:2222:3333:4444:5555::0x88", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800}},
5091 {"1111:2222:3333:4444:5555::0X88", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800}},
5092 {"1111:2222:3333:4444:5555::0X", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0}},
5093 {"1111:2222:3333:4444:5555::0X88:7777", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800}},
5094 {"1111:2222:3333:4444:5555::0x8888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888}},
5095 {"1111:2222:3333:4444:5555::0x80000000", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0xffff}},
5096 {"1111:2222:3333:4444::5555:0x012345678", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0, 0, 0x5555, 0x7856}},
5097 {"1111:2222:3333:4444::5555:0x123456789", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0, 0, 0x5555, 0xffff}},
5098 {"1111:2222:3333:4444:5555:6666:0x12345678", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0xabab, 0xabab}},
5099 {"1111:2222:3333:4444:5555:6666:7777:0x80000000", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xffff}},
5100 {"1111:2222:3333:4444:5555:6666:7777:0x012345678", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x7856}},
5101 {"1111:2222:3333:4444:5555:6666:7777:0x123456789", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xffff}},
5102 {"111:222:333:444:555:666:777:0x123456789abcdef0", 0, {0x1101, 0x2202, 0x3303, 0x4404, 0x5505, 0x6606, 0x7707, 0xffff}},
5103 {"1111:2222:3333:4444:5555::08888", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab}},
5104 {"1111:2222:3333:4444:5555::08888::", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab}},
5105 {"1111:2222:3333:4444:5555:6666:7777:fffff:", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xabab}},
5106 {"1111:2222:3333:4444:5555:6666::fffff:", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0xabab, 0xabab}},
5107 {"1111:2222:3333:4444:5555::fffff", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab}},
5108 {"1111:2222:3333:4444::fffff", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab}},
5109 {"1111:2222:3333::fffff", 0, {0x1111, 0x2222, 0x3333, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5110 {"1111:2222:3333:4444:5555::7777:8888", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888}},
5111 {"1111:2222:3333:4444:5555::8888", 1, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888}},
5112 {"1111::", 1, {0x1111, 0, 0, 0, 0, 0, 0, 0}},
5113 {"1111::123.123.123.123", 1, {0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b}},
5114 {"1111::3333:4444:5555:6666:123.123.123.123", 1, {0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b}},
5115 {"1111::3333:4444:5555:6666:7777:8888", 1, {0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5116 {"1111::4444:5555:6666:123.123.123.123", 1, {0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b}},
5117 {"1111::4444:5555:6666:7777:8888", 1, {0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5118 {"1111::5555:6666:123.123.123.123", 1, {0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b}},
5119 {"1111::5555:6666:7777:8888", 1, {0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888}},
5120 {"1111::6666:123.123.123.123", 1, {0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b}},
5121 {"1111::6666:7777:8888", 1, {0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888}},
5122 {"1111::7777:8888", 1, {0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888}},
5123 {"1111::8888", 1, {0x1111, 0, 0, 0, 0, 0, 0, 0x8888}},
5124 {"1:2:3:4:5:6:1.2.3.4", 1, {0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403}},
5125 {"1:2:3:4:5:6:7:8", 1, {0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800}},
5126 {"1:2:3:4:5:6::", 1, {0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0}},
5127 {"1:2:3:4:5:6::8", 1, {0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800}},
5128 {"2001:0000:1234:0000:0000:C1C0:ABCD:0876", 1, {0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608}},
5129 {"2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1, {0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd}},
5130 {"2001:0db8:0:0:0:0:1428:57ab", 1, {0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57}},
5131 {"2001:0db8:1234:ffff:ffff:ffff:ffff:ffff", 1, {0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}},
5132 {"2001::CE49:7601:2CAD:DFFF:7C94:FFFE", 1, {0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff}},
5133 {"2001:db8:85a3::8a2e:370:7334", 1, {0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473}},
5134 {"3ffe:0b00:0000:0000:0001:0000:0000:000a", 1, {0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00}},
5135 {"::", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5136 {"::%16", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5137 {"::/16", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5138 {"::01234", 0, {0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5139 {"::0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5140 {"::0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5141 {"::0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5142 {"::0:0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5143 {"::0:0:0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5144 {"::0:0:0:0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}},
5145 {"::0:0:0:0:0:0:0", 1, {0, 0, 0, 0, 0, 0, 0, 0}, 1},
5146 {"::0:a:b:c:d:e:f", 1, {0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00}, 1},
5147 {"::123.123.123.123", 1, {0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b}},
5148 {"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 1, {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}},
5149 {"':10.0.0.1", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5150 {"-1", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5151 {"02001:0000:1234:0000:0000:C1C0:ABCD:0876", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5152 {"2001:00000:1234:0000:0000:C1C0:ABCD:0876", 0, {0x120, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5153 {"2001:0000:01234:0000:0000:C1C0:ABCD:0876", 0, {0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5154 {"2001:0000::01234.0", 0, {0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5155 {"2001:0::b.0", 0, {0x120, 0, 0, 0, 0, 0, 0, 0xb00}},
5156 {"2001::0:b.0", 0, {0x120, 0, 0, 0, 0, 0, 0, 0xb00}},
5157 {"1.2.3.4", 0, {0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5158 {"1.2.3.4:1111::5555", 0, {0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5159 {"1.2.3.4::5555", 0, {0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5160 {"11112222:3333:4444:5555:6666:1.2.3.4", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5161 {"11112222:3333:4444:5555:6666:7777:8888", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5162 {"1111", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5163 {"0x1111", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5164 {"1111:22223333:4444:5555:6666:1.2.3.4", 0, {0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5165 {"1111:22223333:4444:5555:6666:7777:8888", 0, {0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5166 {"1111:123456789:4444:5555:6666:7777:8888", 0, {0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5167 {"1111:1234567890abcdef0:4444:5555:6666:7777:888", 0, {0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5168 {"1111:2222:", 0, {0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5169 {"1111:2222:1.2.3.4", 0, {0x1111, 0x2222, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab}},
5170 {"1111:2222:3333", 0, {0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5171 {"1111:2222:3333:4444:5555:6666::1.2.3.4", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x100}},
5172 {"1111:2222:3333:4444:5555:6666:7777:1.2.3.4", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100}},
5173 {"1111:2222:3333:4444:5555:6666:7777:8888:", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5174 {"1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4",0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5175 {"1111:2222:3333:4444:5555:6666:7777:8888:9999", 0, {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888}},
5176 {"1111:2222:::", 0, {0x1111, 0x2222, 0, 0, 0, 0, 0, 0}},
5177 {"1111::5555:", 0, {0x1111, 0x5555, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5178 {"1111::3333:4444:5555:6666:7777::", 0, {0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777}},
5179 {"1111:2222:::4444:5555:6666:1.2.3.4", 0, {0x1111, 0x2222, 0, 0, 0, 0, 0, 0}},
5180 {"1111::3333::5555:6666:1.2.3.4", 0, {0x1111, 0, 0, 0, 0, 0, 0, 0x3333}},
5181 {"12345::6:7:8", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5182 {"1::001.2.3.4", 1, {0x100, 0, 0, 0, 0, 0, 0x201, 0x403}},
5183 {"1::1.002.3.4", 1, {0x100, 0, 0, 0, 0, 0, 0x201, 0x403}},
5184 {"1::0001.2.3.4", 0, {0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5185 {"1::1.0002.3.4", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5186 {"1::1.2.256.4", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5187 {"1::1.2.4294967296.4", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5188 {"1::1.2.18446744073709551616.4", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5189 {"1::1.2.3.256", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5190 {"1::1.2.3.4294967296", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5191 {"1::1.2.3.18446744073709551616", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5192 {"1::1.2.3.300", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5193 {"1::1.2.3.300.", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5194 {"1::1.2::1", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5195 {"1::1.2.3.4::1", 0, {0x100, 0, 0, 0, 0, 0, 0x201, 0x403}},
5196 {"1::1.", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5197 {"1::1.2", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5198 {"1::1.2.", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5199 {"1::1.2.3", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5200 {"1::1.2.3.", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5201 {"1::1.2.3.4", 1, {0x100, 0, 0, 0, 0, 0, 0x201, 0x403}},
5202 {"1::1.2.3.900", 0, {0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5203 {"1::1.2.300.4", 0, {0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5204 {"1::1.256.3.4", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5205 {"1::1.256:3.4", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5206 {"1::1.2a.3.4", 0, {0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5207 {"1::256.2.3.4", 0, {0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5208 {"1::1a.2.3.4", 0, {0x100, 0, 0, 0, 0, 0, 0, 0x1a00}},
5209 {"1::2::3", 0, {0x100, 0, 0, 0, 0, 0, 0, 0x200}},
5210 {"2001:0000:1234: 0000:0000:C1C0:ABCD:0876", 0, {0x120, 0, 0x3412, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5211 {"2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", 0, {0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608}},
5212 {"2001:1:1:1:1:1:255Z255X255Y255", 0, {0x120, 0x100, 0x100, 0x100, 0x100, 0x100, 0xabab, 0xabab}},
5213 {"2001::FFD3::57ab", 0, {0x120, 0, 0, 0, 0, 0, 0, 0xd3ff}},
5214 {":", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5215 {":1111:2222:3333:4444:5555:6666:1.2.3.4", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5216 {":1111:2222:3333:4444:5555:6666:7777:8888", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5217 {":1111::", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5218 {"::-1", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5219 {"::12345678", 0, {0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5220 {"::123456789", 0, {0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5221 {"::1234567890abcdef0", 0, {0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5222 {"::0x80000000", 0, {0, 0, 0, 0, 0, 0, 0, 0xffff}},
5223 {"::0x012345678", 0, {0, 0, 0, 0, 0, 0, 0, 0x7856}},
5224 {"::0x123456789", 0, {0, 0, 0, 0, 0, 0, 0, 0xffff}},
5225 {"::0x1234567890abcdef0", 0, {0, 0, 0, 0, 0, 0, 0, 0xffff}},
5226 {"::.", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5227 {"::..", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5228 {"::...", 0, {0, 0, 0, 0, 0, 0, 0, 0}},
5229 {"XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5230 {"[::]", 0, {0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab}},
5231 };
5232
5233 BYTE buffer[32];
5234 int i, ret;
5235
5236 /* inet_ntop and inet_pton became available in Vista and Win2008 */
5237 if (!p_inet_ntop)
5238 {
5239 win_skip("inet_ntop is not available\n");
5240 return;
5241 }
5242
5243 WSASetLastError(0xdeadbeef);
5244 ret = p_inet_pton(AF_UNSPEC, NULL, buffer);
5245 ok(ret == -1, "got %d\n", ret);
5246 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
5247
5248 WSASetLastError(0xdeadbeef);
5249 ret = p_inet_pton(AF_INET, NULL, buffer);
5250 ok(ret == -1, "got %d\n", ret);
5251 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
5252
5253 WSASetLastError(0xdeadbeef);
5254 ret = pInetPtonW(AF_UNSPEC, NULL, buffer);
5255 ok(ret == -1, "got %d\n", ret);
5256 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
5257
5258 WSASetLastError(0xdeadbeef);
5259 ret = pInetPtonW(AF_INET, NULL, buffer);
5260 ok(ret == -1, "got %d\n", ret);
5261 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
5262
5263 WSASetLastError(0xdeadbeef);
5264 ret = p_inet_pton(AF_UNSPEC, "127.0.0.1", buffer);
5265 ok(ret == -1, "got %d\n", ret);
5266 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "got error %u\n", WSAGetLastError());
5267
5268 WSASetLastError(0xdeadbeef);
5269 ret = p_inet_pton(AF_UNSPEC, "2607:f0d0:1002:51::4", buffer);
5270 ok(ret == -1, "got %d\n", ret);
5271 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "got error %u\n", WSAGetLastError());
5272
5273 for (i = 0; i < ARRAY_SIZE(ipv4_tests); ++i)
5274 {
5275 WCHAR inputW[32];
5276 DWORD addr;
5277
5278 winetest_push_context( "Address %s", debugstr_a(ipv4_tests[i].input) );
5279
5280 WSASetLastError(0xdeadbeef);
5281 addr = 0xdeadbeef;
5282 ret = p_inet_pton(AF_INET, ipv4_tests[i].input, &addr);
5283 ok(ret == ipv4_tests[i].ret, "got %d\n", ret);
5284 ok(WSAGetLastError() == 0xdeadbeef, "got error %u\n", WSAGetLastError());
5285 ok(addr == ipv4_tests[i].addr, "got addr %#08lx\n", addr);
5286
5287 MultiByteToWideChar(CP_ACP, 0, ipv4_tests[i].input, -1, inputW, ARRAY_SIZE(inputW));
5288 WSASetLastError(0xdeadbeef);
5289 addr = 0xdeadbeef;
5290 ret = pInetPtonW(AF_INET, inputW, &addr);
5291 ok(ret == ipv4_tests[i].ret, "got %d\n", ret);
5292 ok(WSAGetLastError() == (ret ? 0xdeadbeef : WSAEINVAL), "got error %u\n", WSAGetLastError());
5293 ok(addr == ipv4_tests[i].addr, "got addr %#08lx\n", addr);
5294
5295 WSASetLastError(0xdeadbeef);
5296 addr = inet_addr(ipv4_tests[i].input);
5297 ok(addr == ipv4_tests[i].ret ? ipv4_tests[i].addr : INADDR_NONE, "got addr %#08lx\n", addr);
5298 ok(WSAGetLastError() == 0xdeadbeef, "got error %u\n", WSAGetLastError());
5299
5300 winetest_pop_context();
5301 }
5302
5303 for (i = 0; i < ARRAY_SIZE(ipv6_tests); ++i)
5304 {
5305 unsigned short addr[8];
5306 WCHAR inputW[64];
5307
5308 winetest_push_context( "Address %s", debugstr_a(ipv6_tests[i].input) );
5309
5310 WSASetLastError(0xdeadbeef);
5311 memset(addr, 0xab, sizeof(addr));
5312 ret = p_inet_pton(AF_INET6, ipv6_tests[i].input, addr);
5313 if (ipv6_tests[i].broken)
5314 ok(ret == ipv6_tests[i].ret || broken(ret == ipv6_tests[i].broken_ret), "got %d\n", ret);
5315 else
5316 ok(ret == ipv6_tests[i].ret, "got %d\n", ret);
5317 ok(WSAGetLastError() == 0xdeadbeef, "got error %u\n", WSAGetLastError());
5318 if (ipv6_tests[i].broken)
5319 ok(!memcmp(addr, ipv6_tests[i].addr, sizeof(addr)) || broken(memcmp(addr, ipv6_tests[i].addr, sizeof(addr))),
5320 "address didn't match\n");
5321 else
5322 ok(!memcmp(addr, ipv6_tests[i].addr, sizeof(addr)), "address didn't match\n");
5323
5324 MultiByteToWideChar(CP_ACP, 0, ipv6_tests[i].input, -1, inputW, ARRAY_SIZE(inputW));
5325 WSASetLastError(0xdeadbeef);
5326 memset(addr, 0xab, sizeof(addr));
5327 ret = pInetPtonW(AF_INET6, inputW, addr);
5328 if (ipv6_tests[i].broken)
5329 ok(ret == ipv6_tests[i].ret || broken(ret == ipv6_tests[i].broken_ret), "got %d\n", ret);
5330 else
5331 ok(ret == ipv6_tests[i].ret, "got %d\n", ret);
5332 ok(WSAGetLastError() == (ret ? 0xdeadbeef : WSAEINVAL), "got error %u\n", WSAGetLastError());
5333 if (ipv6_tests[i].broken)
5334 ok(!memcmp(addr, ipv6_tests[i].addr, sizeof(addr)) || broken(memcmp(addr, ipv6_tests[i].addr, sizeof(addr))),
5335 "address didn't match\n");
5336 else
5337 ok(!memcmp(addr, ipv6_tests[i].addr, sizeof(addr)), "address didn't match\n");
5338
5339 winetest_pop_context();
5340 }
5341 }
5342
test_ioctlsocket(void)5343 static void test_ioctlsocket(void)
5344 {
5345 SOCKET sock, src, dst;
5346 struct tcp_keepalive kalive;
5347 struct sockaddr_in address;
5348 int ret, optval;
5349 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
5350 UINT i, bytes_rec;
5351 char data;
5352 WSABUF bufs;
5353 u_long arg = 0;
5354
5355 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5356 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
5357 if(sock == INVALID_SOCKET)
5358 {
5359 skip("Can't continue without a socket.\n");
5360 return;
5361 }
5362
5363 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
5364 {
5365 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
5366 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
5367 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
5368 ret = WSAGetLastError();
5369 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
5370 }
5371
5372 /* A fresh and not connected socket has no urgent data, this test shows
5373 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
5374
5375 ret = ioctlsocket(sock, SIOCATMARK, &arg);
5376 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
5377 ok(arg, "SIOCATMARK expected a non-zero value\n");
5378
5379 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
5380 optval = 1;
5381 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
5382 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
5383 arg = 0;
5384 ret = ioctlsocket(sock, SIOCATMARK, &arg);
5385 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
5386 ok(arg, "SIOCATMARK expected a non-zero value\n");
5387
5388 /* disable SO_OOBINLINE and get the same old behavior */
5389 optval = 0;
5390 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
5391 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
5392 arg = 0;
5393 ret = ioctlsocket(sock, SIOCATMARK, &arg);
5394 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
5395 ok(arg, "SIOCATMARK expected a non-zero value\n");
5396
5397 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
5398 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
5399 ret = WSAGetLastError();
5400 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
5401
5402 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5403 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
5404 ret = WSAGetLastError();
5405 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
5406
5407 make_keepalive(kalive, 0, 0, 0);
5408 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5409 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5410
5411 make_keepalive(kalive, 1, 0, 0);
5412 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5413 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5414
5415 make_keepalive(kalive, 1, 1000, 1000);
5416 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5417 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5418
5419 make_keepalive(kalive, 1, 10000, 10000);
5420 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5421 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5422
5423 make_keepalive(kalive, 1, 100, 100);
5424 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5425 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5426
5427 make_keepalive(kalive, 0, 100, 100);
5428 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
5429 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
5430
5431 closesocket(sock);
5432
5433 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5434 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
5435 if(sock == INVALID_SOCKET)
5436 {
5437 skip("Can't continue without a socket.\n");
5438 return;
5439 }
5440
5441 /* test FIONREAD with a fresh and non-connected socket */
5442 arg = 0xdeadbeef;
5443 ret = ioctlsocket(sock, FIONREAD, &arg);
5444 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
5445 ok(arg == 0, "expected 0, got %u\n", arg);
5446
5447 memset(&address, 0, sizeof(address));
5448 address.sin_family = AF_INET;
5449 address.sin_addr.s_addr = inet_addr( SERVERIP );
5450 address.sin_port = htons( SERVERPORT );
5451 ret = bind(sock, (struct sockaddr *)&address, sizeof(address));
5452 ok(ret == 0, "bind failed unexpectedly with error %d\n", WSAGetLastError());
5453
5454 ret = listen(sock, SOMAXCONN);
5455 ok(ret == 0, "listen failed unexpectedly with error %d\n", WSAGetLastError());
5456
5457 /* test FIONREAD with listening socket */
5458 arg = 0xdeadbeef;
5459 ret = ioctlsocket(sock, FIONREAD, &arg);
5460 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
5461 ok(arg == 0, "expected 0, got %u\n", arg);
5462
5463 closesocket(sock);
5464
5465 if (tcp_socketpair(&src, &dst) != 0)
5466 {
5467 ok(0, "creating socket pair failed, skipping test\n");
5468 return;
5469 }
5470
5471 /* test FIONREAD on TCP sockets */
5472 optval = 0xdeadbeef;
5473 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
5474 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
5475 ok(optval == 0, "FIONREAD should have returned 0 bytes, got %d instead\n", optval);
5476
5477 optval = 0xdeadbeef;
5478 ok(send(src, "TEST", 4, 0) == 4, "failed to send test data\n");
5479 Sleep(100);
5480 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
5481 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
5482 ok(optval == 4, "FIONREAD should have returned 4 bytes, got %d instead\n", optval);
5483
5484 /* trying to read from an OOB inlined socket with MSG_OOB results in WSAEINVAL */
5485 set_blocking(dst, FALSE);
5486 i = MSG_OOB;
5487 SetLastError(0xdeadbeef);
5488 ret = recv(dst, &data, 1, i);
5489 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5490 ret = GetLastError();
5491 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
5492 bufs.len = sizeof(char);
5493 bufs.buf = &data;
5494 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
5495 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5496 ret = GetLastError();
5497 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
5498 optval = 1;
5499 ret = setsockopt(dst, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
5500 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
5501 i = MSG_OOB;
5502 SetLastError(0xdeadbeef);
5503 ret = recv(dst, &data, 1, i);
5504 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
5505 ret = GetLastError();
5506 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
5507 bufs.len = sizeof(char);
5508 bufs.buf = &data;
5509 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
5510 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5511 ret = GetLastError();
5512 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
5513
5514 closesocket(dst);
5515 optval = 0xdeadbeef;
5516 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
5517 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
5518 ok(optval == 0xdeadbeef, "FIONREAD should not have changed last error, got %d instead\n", optval);
5519 closesocket(src);
5520 }
5521
5522 static BOOL drain_pause = FALSE;
drain_socket_thread(LPVOID arg)5523 static DWORD WINAPI drain_socket_thread(LPVOID arg)
5524 {
5525 char buffer[1024];
5526 SOCKET sock = *(SOCKET*)arg;
5527 int ret;
5528
5529 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
5530 {
5531 if (ret < 0)
5532 {
5533 if (WSAGetLastError() == WSAEWOULDBLOCK)
5534 {
5535 fd_set readset;
5536 FD_ZERO(&readset);
5537 FD_SET(sock, &readset);
5538 select(sock+1, &readset, NULL, NULL, NULL);
5539 while (drain_pause)
5540 Sleep(100);
5541 }
5542 else
5543 break;
5544 }
5545 }
5546 return 0;
5547 }
5548
test_send(void)5549 static void test_send(void)
5550 {
5551 SOCKET src = INVALID_SOCKET;
5552 SOCKET dst = INVALID_SOCKET;
5553 HANDLE hThread = NULL;
5554 const int buflen = 1024*1024;
5555 char *buffer = NULL;
5556 int ret, i, zero = 0;
5557 WSABUF buf;
5558 OVERLAPPED ov;
5559 BOOL bret;
5560 DWORD id, bytes_sent, dwRet;
5561 DWORD expected_time, connect_time;
5562 socklen_t optlen;
5563
5564 memset(&ov, 0, sizeof(ov));
5565
5566 if (tcp_socketpair(&src, &dst) != 0)
5567 {
5568 ok(0, "creating socket pair failed, skipping test\n");
5569 return;
5570 }
5571 expected_time = GetTickCount();
5572
5573 set_blocking(dst, FALSE);
5574 /* force disable buffering so we can get a pending overlapped request */
5575 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
5576 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
5577
5578 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
5579 if (hThread == NULL)
5580 {
5581 ok(0, "CreateThread failed, error %d\n", GetLastError());
5582 goto end;
5583 }
5584
5585 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
5586 if (buffer == NULL)
5587 {
5588 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
5589 goto end;
5590 }
5591
5592 /* fill the buffer with some nonsense */
5593 for (i = 0; i < buflen; ++i)
5594 {
5595 buffer[i] = (char) i;
5596 }
5597
5598 ret = send(src, buffer, buflen, 0);
5599 if (ret >= 0)
5600 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
5601 else
5602 ok(0, "send failed, error %d\n", WSAGetLastError());
5603
5604 buf.buf = buffer;
5605 buf.len = buflen;
5606
5607 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
5608 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
5609 if (!ov.hEvent)
5610 goto end;
5611
5612 bytes_sent = 0;
5613 WSASetLastError(12345);
5614 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
5615 ok((ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING) || broken(bytes_sent == buflen),
5616 "Failed to start overlapped send %d - %d - %d/%d\n", ret, WSAGetLastError(), bytes_sent, buflen);
5617
5618 /* don't check for completion yet, we may need to drain the buffer while still sending */
5619 set_blocking(src, FALSE);
5620 for (i = 0; i < buflen; ++i)
5621 {
5622 int j = 0;
5623
5624 ret = recv(src, buffer, 1, 0);
5625 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
5626 {
5627 j++;
5628 Sleep(50);
5629 ret = recv(src, buffer, 1, 0);
5630 }
5631
5632 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
5633 if (ret != 1)
5634 break;
5635
5636 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
5637 }
5638
5639 dwRet = WaitForSingleObject(ov.hEvent, 1000);
5640 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
5641 if (dwRet == WAIT_OBJECT_0)
5642 {
5643 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
5644 ok(bret && bytes_sent == buflen,
5645 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
5646 }
5647
5648 WSASetLastError(12345);
5649 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
5650 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
5651 "WSASend failed %d - %d\n", ret, WSAGetLastError());
5652
5653 WSASetLastError(12345);
5654 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
5655 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
5656 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
5657
5658 expected_time = (GetTickCount() - expected_time) / 1000;
5659
5660 connect_time = 0xdeadbeef;
5661 optlen = sizeof(connect_time);
5662 ret = getsockopt(dst, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
5663 ok(!ret, "getsockopt failed %d\n", WSAGetLastError());
5664 ok(connect_time >= expected_time && connect_time <= expected_time + 1,
5665 "unexpected connect time %u, expected %u\n", connect_time, expected_time);
5666
5667 connect_time = 0xdeadbeef;
5668 optlen = sizeof(connect_time);
5669 ret = getsockopt(src, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
5670 ok(!ret, "getsockopt failed %d\n", WSAGetLastError());
5671 ok(connect_time >= expected_time && connect_time <= expected_time + 1,
5672 "unexpected connect time %u, expected %u\n", connect_time, expected_time);
5673
5674 end:
5675 if (src != INVALID_SOCKET)
5676 closesocket(src);
5677 if (dst != INVALID_SOCKET)
5678 closesocket(dst);
5679 if (hThread != NULL)
5680 {
5681 dwRet = WaitForSingleObject(hThread, 500);
5682 ok(dwRet == WAIT_OBJECT_0, "failed to wait for thread termination: %d\n", GetLastError());
5683 CloseHandle(hThread);
5684 }
5685 if (ov.hEvent)
5686 CloseHandle(ov.hEvent);
5687 HeapFree(GetProcessHeap(), 0, buffer);
5688 }
5689
5690 typedef struct async_message
5691 {
5692 SOCKET socket;
5693 LPARAM lparam;
5694 struct async_message *next;
5695 } async_message;
5696
5697 static struct async_message *messages_received;
5698
5699 #define WM_SOCKET (WM_USER+100)
ws2_test_WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)5700 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5701 {
5702 struct async_message *message;
5703
5704 switch (msg)
5705 {
5706 case WM_SOCKET:
5707 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message));
5708 message->socket = (SOCKET) wparam;
5709 message->lparam = lparam;
5710 message->next = NULL;
5711
5712 if (messages_received)
5713 {
5714 struct async_message *last = messages_received;
5715 while (last->next) last = last->next;
5716 last->next = message;
5717 }
5718 else
5719 messages_received = message;
5720 return 0;
5721 }
5722
5723 return DefWindowProcA(hwnd, msg, wparam, lparam);
5724 }
5725
get_event_details(int event,int * bit,char * name)5726 static void get_event_details(int event, int *bit, char *name)
5727 {
5728 switch (event)
5729 {
5730 case FD_ACCEPT:
5731 if (bit) *bit = FD_ACCEPT_BIT;
5732 if (name) strcpy(name, "FD_ACCEPT");
5733 break;
5734 case FD_CONNECT:
5735 if (bit) *bit = FD_CONNECT_BIT;
5736 if (name) strcpy(name, "FD_CONNECT");
5737 break;
5738 case FD_READ:
5739 if (bit) *bit = FD_READ_BIT;
5740 if (name) strcpy(name, "FD_READ");
5741 break;
5742 case FD_OOB:
5743 if (bit) *bit = FD_OOB_BIT;
5744 if (name) strcpy(name, "FD_OOB");
5745 break;
5746 case FD_WRITE:
5747 if (bit) *bit = FD_WRITE_BIT;
5748 if (name) strcpy(name, "FD_WRITE");
5749 break;
5750 case FD_CLOSE:
5751 if (bit) *bit = FD_CLOSE_BIT;
5752 if (name) strcpy(name, "FD_CLOSE");
5753 break;
5754 default:
5755 if (bit) *bit = -1;
5756 if (name) sprintf(name, "bad%x", event);
5757 }
5758 }
5759
dbgstr_event_seq(const LPARAM * seq)5760 static const char *dbgstr_event_seq(const LPARAM *seq)
5761 {
5762 static char message[1024];
5763 char name[12];
5764 int len = 1;
5765
5766 message[0] = '[';
5767 message[1] = 0;
5768 while (*seq)
5769 {
5770 get_event_details(WSAGETSELECTEVENT(*seq), NULL, name);
5771 len += sprintf(message + len, "%s(%d) ", name, WSAGETSELECTERROR(*seq));
5772 seq++;
5773 }
5774 if (len > 1) len--;
5775 strcpy( message + len, "]" );
5776 return message;
5777 }
5778
dbgstr_event_seq_result(SOCKET s,WSANETWORKEVENTS * netEvents)5779 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents)
5780 {
5781 static char message[1024];
5782 struct async_message *curr = messages_received;
5783 int index, error, bit = 0;
5784 char name[12];
5785 int len = 1;
5786
5787 message[0] = '[';
5788 message[1] = 0;
5789 while (1)
5790 {
5791 if (netEvents)
5792 {
5793 if (bit >= FD_MAX_EVENTS) break;
5794 if ( !(netEvents->lNetworkEvents & (1 << bit)) )
5795 {
5796 bit++;
5797 continue;
5798 }
5799 get_event_details(1 << bit, &index, name);
5800 error = netEvents->iErrorCode[index];
5801 bit++;
5802 }
5803 else
5804 {
5805 if (!curr) break;
5806 if (curr->socket != s)
5807 {
5808 curr = curr->next;
5809 continue;
5810 }
5811 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name);
5812 error = WSAGETSELECTERROR(curr->lparam);
5813 curr = curr->next;
5814 }
5815
5816 len += sprintf(message + len, "%s(%d) ", name, error);
5817 }
5818 if (len > 1) len--;
5819 strcpy( message + len, "]" );
5820 return message;
5821 }
5822
flush_events(SOCKET s,HANDLE hEvent)5823 static void flush_events(SOCKET s, HANDLE hEvent)
5824 {
5825 WSANETWORKEVENTS netEvents;
5826 struct async_message *prev = NULL, *curr = messages_received;
5827 int ret;
5828 DWORD dwRet;
5829
5830 if (hEvent != INVALID_HANDLE_VALUE)
5831 {
5832 dwRet = WaitForSingleObject(hEvent, 100);
5833 if (dwRet == WAIT_OBJECT_0)
5834 {
5835 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents);
5836 if (ret)
5837 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
5838 }
5839 }
5840 else
5841 {
5842 while (curr)
5843 {
5844 if (curr->socket == s)
5845 {
5846 if (prev) prev->next = curr->next;
5847 else messages_received = curr->next;
5848
5849 HeapFree(GetProcessHeap(), 0, curr);
5850
5851 if (prev) curr = prev->next;
5852 else curr = messages_received;
5853 }
5854 else
5855 {
5856 prev = curr;
5857 curr = curr->next;
5858 }
5859 }
5860 }
5861 }
5862
match_event_sequence(SOCKET s,WSANETWORKEVENTS * netEvents,const LPARAM * seq)5863 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq)
5864 {
5865 int event, index, error, events;
5866 struct async_message *curr;
5867
5868 if (netEvents)
5869 {
5870 events = netEvents->lNetworkEvents;
5871 while (*seq)
5872 {
5873 event = WSAGETSELECTEVENT(*seq);
5874 error = WSAGETSELECTERROR(*seq);
5875 get_event_details(event, &index, NULL);
5876
5877 if (!(events & event) && index != -1)
5878 return 0;
5879 if (events & event && index != -1)
5880 {
5881 if (netEvents->iErrorCode[index] != error)
5882 return 0;
5883 }
5884 events &= ~event;
5885 seq++;
5886 }
5887 if (events)
5888 return 0;
5889 }
5890 else
5891 {
5892 curr = messages_received;
5893 while (curr)
5894 {
5895 if (curr->socket == s)
5896 {
5897 if (!*seq) return 0;
5898 if (*seq != curr->lparam) return 0;
5899 seq++;
5900 }
5901 curr = curr->next;
5902 }
5903 if (*seq)
5904 return 0;
5905 }
5906 return 1;
5907 }
5908
5909 /* checks for a sequence of events, (order only checked if window is used) */
ok_event_sequence(SOCKET s,HANDLE hEvent,const LPARAM * seq,const LPARAM ** broken_seqs,int completelyBroken)5910 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq, const LPARAM **broken_seqs, int completelyBroken)
5911 {
5912 MSG msg;
5913 WSANETWORKEVENTS events, *netEvents = NULL;
5914 int ret;
5915 DWORD dwRet;
5916
5917 if (hEvent != INVALID_HANDLE_VALUE)
5918 {
5919 netEvents = &events;
5920
5921 dwRet = WaitForSingleObject(hEvent, 200);
5922 if (dwRet == WAIT_OBJECT_0)
5923 {
5924 ret = WSAEnumNetworkEvents(s, hEvent, netEvents);
5925 if (ret)
5926 {
5927 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
5928 return;
5929 }
5930 }
5931 else
5932 memset(netEvents, 0, sizeof(*netEvents));
5933 }
5934 else
5935 {
5936 Sleep(200);
5937 /* Run the message loop a little */
5938 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ))
5939 {
5940 DispatchMessageA(&msg);
5941 }
5942 }
5943
5944 if (match_event_sequence(s, netEvents, seq))
5945 {
5946 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq));
5947 flush_events(s, hEvent);
5948 return;
5949 }
5950
5951 if (broken_seqs)
5952 {
5953 for (; *broken_seqs; broken_seqs++)
5954 {
5955 if (match_event_sequence(s, netEvents, *broken_seqs))
5956 {
5957 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s, netEvents), dbgstr_event_seq(seq));
5958 flush_events(s, hEvent);
5959 return;
5960 }
5961 }
5962 }
5963
5964 winetest_ok(broken(completelyBroken), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq),
5965 dbgstr_event_seq_result(s, netEvents));
5966 flush_events(s, hEvent);
5967 }
5968
5969 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
5970
test_events(int useMessages)5971 static void test_events(int useMessages)
5972 {
5973 SOCKET server = INVALID_SOCKET;
5974 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET;
5975 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET;
5976 struct sockaddr_in addr;
5977 HANDLE hThread = NULL;
5978 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE;
5979 WNDCLASSEXA wndclass;
5980 HWND hWnd = NULL;
5981 char *buffer = NULL;
5982 int bufferSize = 1024*1024;
5983 WSABUF bufs;
5984 OVERLAPPED ov, ov2;
5985 DWORD flags = 0;
5986 DWORD bytesReturned;
5987 DWORD id;
5988 int len;
5989 int ret;
5990 DWORD dwRet;
5991 BOOL bret;
5992 static char szClassName[] = "wstestclass";
5993 const LPARAM *broken_seq[3];
5994 static const LPARAM empty_seq[] = { 0 };
5995 static const LPARAM close_seq[] = { WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
5996 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
5997 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 };
5998 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 };
5999 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0),
6000 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
6001 static const LPARAM read_read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
6002 WSAMAKESELECTREPLY(FD_READ, 0), 0 };
6003 static const LPARAM read_write_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
6004 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
6005 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
6006 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
6007
6008 memset(&ov, 0, sizeof(ov));
6009 memset(&ov2, 0, sizeof(ov2));
6010
6011 /* don't use socketpair, we want connection event */
6012 src = socket(AF_INET, SOCK_STREAM, 0);
6013 if (src == INVALID_SOCKET)
6014 {
6015 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6016 goto end;
6017 }
6018
6019 ret = set_blocking(src, TRUE);
6020 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6021
6022 src2 = socket(AF_INET, SOCK_STREAM, 0);
6023 if (src2 == INVALID_SOCKET)
6024 {
6025 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6026 goto end;
6027 }
6028
6029 ret = set_blocking(src2, TRUE);
6030 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6031
6032 len = sizeof(BOOL);
6033 if (getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len) == SOCKET_ERROR)
6034 {
6035 ok(0, "failed to get oobinline status, %d\n", GetLastError());
6036 goto end;
6037 }
6038 ok(bret == FALSE, "OOB not inline\n");
6039
6040 if (useMessages)
6041 {
6042 trace("Event test using messages\n");
6043
6044 wndclass.cbSize = sizeof(wndclass);
6045 wndclass.style = CS_HREDRAW | CS_VREDRAW;
6046 wndclass.lpfnWndProc = ws2_test_WndProc;
6047 wndclass.cbClsExtra = 0;
6048 wndclass.cbWndExtra = 0;
6049 wndclass.hInstance = GetModuleHandleA(NULL);
6050 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
6051 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
6052 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
6053 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
6054 wndclass.lpszClassName = szClassName;
6055 wndclass.lpszMenuName = NULL;
6056 RegisterClassExA(&wndclass);
6057
6058 hWnd = CreateWindowA(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW,
6059 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
6060 if (!hWnd)
6061 {
6062 ok(0, "failed to create window: %d\n", GetLastError());
6063 return;
6064 }
6065
6066 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
6067 if (ret)
6068 {
6069 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6070 goto end;
6071 }
6072
6073 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
6074 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
6075
6076 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
6077 if (ret)
6078 {
6079 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6080 goto end;
6081 }
6082
6083 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
6084 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
6085 }
6086 else
6087 {
6088 trace("Event test using events\n");
6089
6090 hEvent = WSACreateEvent();
6091 if (hEvent == INVALID_HANDLE_VALUE)
6092 {
6093 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
6094 goto end;
6095 }
6096
6097 hEvent2 = WSACreateEvent();
6098 if (hEvent2 == INVALID_HANDLE_VALUE)
6099 {
6100 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
6101 goto end;
6102 }
6103
6104 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
6105 if (ret)
6106 {
6107 ok(0, "WSAEventSelect failed, error %d\n", ret);
6108 goto end;
6109 }
6110
6111 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
6112 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
6113
6114 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
6115 if (ret)
6116 {
6117 ok(0, "WSAEventSelect failed, error %d\n", ret);
6118 goto end;
6119 }
6120
6121 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n");
6122 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
6123 }
6124
6125 server = socket(AF_INET, SOCK_STREAM, 0);
6126 if (server == INVALID_SOCKET)
6127 {
6128 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6129 goto end;
6130 }
6131
6132 memset(&addr, 0, sizeof(addr));
6133 addr.sin_family = AF_INET;
6134 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
6135 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
6136 if (ret != 0)
6137 {
6138 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6139 goto end;
6140 }
6141
6142 len = sizeof(addr);
6143 ret = getsockname(server, (struct sockaddr*)&addr, &len);
6144 if (ret != 0)
6145 {
6146 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6147 goto end;
6148 }
6149
6150 ret = listen(server, 2);
6151 if (ret != 0)
6152 {
6153 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6154 goto end;
6155 }
6156
6157 SetLastError(0xdeadbeef);
6158 ret = connect(src, NULL, 0);
6159 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
6160 ok(GetLastError() == WSAEFAULT, "expected 10014, got %d\n", GetLastError());
6161
6162 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
6163 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
6164 {
6165 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6166 goto end;
6167 }
6168
6169 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr));
6170 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
6171 {
6172 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6173 goto end;
6174 }
6175
6176 len = sizeof(addr);
6177 dst = accept(server, (struct sockaddr*)&addr, &len);
6178 if (dst == INVALID_SOCKET)
6179 {
6180 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6181 goto end;
6182 }
6183
6184 len = sizeof(addr);
6185 dst2 = accept(server, (struct sockaddr*)&addr, &len);
6186 if (dst2 == INVALID_SOCKET)
6187 {
6188 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
6189 goto end;
6190 }
6191
6192 closesocket(server);
6193 server = INVALID_SOCKET;
6194
6195 /* On Windows it seems when a non-blocking socket sends to a
6196 blocking socket on the same host, the send() is BLOCKING,
6197 so make both sockets non-blocking. src is already non-blocking
6198 from the async select */
6199
6200 if (set_blocking(dst, FALSE))
6201 {
6202 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
6203 goto end;
6204 }
6205
6206 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
6207 if (buffer == NULL)
6208 {
6209 ok(0, "could not allocate memory for test\n");
6210 goto end;
6211 }
6212
6213 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6214 if (ov.hEvent == NULL)
6215 {
6216 ok(0, "could not create event object, errno = %d\n", GetLastError());
6217 goto end;
6218 }
6219
6220 ov2.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6221 if (ov2.hEvent == NULL)
6222 {
6223 ok(0, "could not create event object, errno = %d\n", GetLastError());
6224 goto end;
6225 }
6226
6227 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
6228 ok_event_seq(src, hEvent, connect_seq, NULL, 1);
6229 ok_event_seq(src2, hEvent2, connect_seq, NULL, 1);
6230 /* broken on all windows - FD_CONNECT error is garbage */
6231
6232 /* Test simple send/recv */
6233 SetLastError(0xdeadbeef);
6234 ret = send(dst, buffer, 100, 0);
6235 ok(ret == 100, "Failed to send buffer %d err %d\n", ret, GetLastError());
6236 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6237 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6238
6239 SetLastError(0xdeadbeef);
6240 ret = recv(src, buffer, 1, MSG_PEEK);
6241 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError());
6242 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6243 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6244
6245 SetLastError(0xdeadbeef);
6246 ret = recv(src, buffer, 50, 0);
6247 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
6248 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6249 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6250
6251 ret = recv(src, buffer, 50, 0);
6252 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
6253 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6254
6255 /* fun fact - events are re-enabled even on failure, but only for messages */
6256 ret = send(dst, "1", 1, 0);
6257 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
6258 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6259
6260 ret = recv(src, buffer, -1, 0);
6261 ok(ret == SOCKET_ERROR && (GetLastError() == WSAEFAULT || GetLastError() == WSAENOBUFS),
6262 "Failed to recv buffer %d err %d\n", ret, GetLastError());
6263 if (useMessages)
6264 {
6265 broken_seq[0] = empty_seq; /* win9x */
6266 broken_seq[1] = NULL;
6267 todo_wine ok_event_seq(src, hEvent, read_seq, broken_seq, 0);
6268 }
6269 else
6270 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6271
6272 SetLastError(0xdeadbeef);
6273 ret = recv(src, buffer, 1, 0);
6274 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError());
6275 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6276 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6277
6278 /* Interaction with overlapped */
6279 bufs.len = sizeof(char);
6280 bufs.buf = buffer;
6281 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
6282 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
6283 "WSARecv failed - %d error %d\n", ret, GetLastError());
6284
6285 bufs.len = sizeof(char);
6286 bufs.buf = buffer+1;
6287 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL);
6288 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
6289 "WSARecv failed - %d error %d\n", ret, GetLastError());
6290
6291 ret = send(dst, "12", 2, 0);
6292 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
6293 broken_seq[0] = read_read_seq; /* win9x */
6294 broken_seq[1] = NULL;
6295 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
6296
6297 dwRet = WaitForSingleObject(ov.hEvent, 100);
6298 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
6299 if (dwRet == WAIT_OBJECT_0)
6300 {
6301 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
6302 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
6303 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
6304 ok(buffer[0] == '1', "Got %c instead of 1\n", buffer[0]);
6305 }
6306
6307 dwRet = WaitForSingleObject(ov2.hEvent, 100);
6308 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
6309 if (dwRet == WAIT_OBJECT_0)
6310 {
6311 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE);
6312 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
6313 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
6314 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[1]);
6315 }
6316
6317 SetLastError(0xdeadbeef);
6318 ret = send(dst, "1", 1, 0);
6319 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
6320 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6321 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6322
6323 ret = recv(src, buffer, 1, 0);
6324 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6325 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6326
6327 /* Notifications are delivered as soon as possible, blocked only on
6328 * async requests on the same type */
6329 bufs.len = sizeof(char);
6330 bufs.buf = buffer;
6331 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
6332 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
6333 "WSARecv failed - %d error %d\n", ret, GetLastError());
6334
6335 if (0) {
6336 ret = send(dst, "1", 1, MSG_OOB);
6337 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
6338 ok_event_seq(src, hEvent, oob_seq, NULL, 0);
6339 }
6340
6341 dwRet = WaitForSingleObject(ov.hEvent, 100);
6342 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError());
6343
6344 ret = send(dst, "2", 1, 0);
6345 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
6346 broken_seq[0] = read_seq; /* win98 */
6347 broken_seq[1] = NULL;
6348 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
6349
6350 dwRet = WaitForSingleObject(ov.hEvent, 100);
6351 ok(dwRet == WAIT_OBJECT_0 || broken(dwRet == WAIT_TIMEOUT),
6352 "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
6353 if (dwRet == WAIT_OBJECT_0)
6354 {
6355 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
6356 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
6357 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
6358 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]);
6359 }
6360
6361 if (0) {
6362 ret = recv(src, buffer, 1, MSG_OOB);
6363 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6364 /* We get OOB notification, but no data on wine */
6365 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6366 }
6367
6368 /* Flood the send queue */
6369 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
6370 if (hThread == NULL)
6371 {
6372 ok(0, "CreateThread failed, error %d\n", GetLastError());
6373 goto end;
6374 }
6375
6376 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
6377 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
6378
6379 /* Now if we send a ton of data and the 'server' does not drain it fast
6380 * enough (set drain_pause to be sure), the socket send buffer will only
6381 * take some of it, and we will get a short write. This will trigger
6382 * another FD_WRITE event as soon as data is sent and more space becomes
6383 * available, but not any earlier. */
6384 drain_pause = TRUE;
6385 do
6386 {
6387 ret = send(src, buffer, bufferSize, 0);
6388 } while (ret == bufferSize);
6389 drain_pause = FALSE;
6390 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
6391 {
6392 Sleep(400); /* win9x */
6393 broken_seq[0] = read_write_seq;
6394 broken_seq[1] = NULL;
6395 ok_event_seq(src, hEvent, write_seq, broken_seq, 0);
6396 }
6397 else
6398 {
6399 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
6400 }
6401
6402 /* Test how FD_CLOSE is handled */
6403 ret = send(dst, "12", 2, 0);
6404 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
6405
6406 /* Wait a little and let the send complete */
6407 Sleep(100);
6408 closesocket(dst);
6409 dst = INVALID_SOCKET;
6410 Sleep(100);
6411
6412 /* We can never implement this in wine, best we can hope for is
6413 sending FD_CLOSE after the reads complete */
6414 broken_seq[0] = read_seq; /* win9x */
6415 broken_seq[1] = NULL;
6416 todo_wine ok_event_seq(src, hEvent, read_close_seq, broken_seq, 0);
6417
6418 ret = recv(src, buffer, 1, 0);
6419 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6420 ok_event_seq(src, hEvent, read_seq, NULL, 0);
6421
6422 ret = recv(src, buffer, 1, 0);
6423 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6424 /* want it? it's here, but you can't have it */
6425 broken_seq[0] = close_seq; /* win9x */
6426 broken_seq[1] = NULL;
6427 todo_wine ok_event_seq(src, hEvent, empty_seq, /* wine sends FD_CLOSE here */
6428 broken_seq, 0);
6429
6430 /* Test how FD_CLOSE is handled */
6431 ret = send(dst2, "12", 2, 0);
6432 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
6433
6434 Sleep(200);
6435 shutdown(dst2, SD_SEND);
6436 Sleep(200);
6437
6438 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
6439 regressions, don't mark them as todo_wine, and mark windows as broken */
6440 broken_seq[0] = read_close_seq;
6441 broken_seq[1] = close_seq;
6442 broken_seq[2] = NULL;
6443 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
6444
6445 ret = recv(src2, buffer, 1, 0);
6446 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6447 broken_seq[0] = close_seq; /* win98 */
6448 broken_seq[1] = NULL;
6449 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
6450
6451 ret = recv(src2, buffer, 1, 0);
6452 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
6453 broken_seq[0] = empty_seq;
6454 broken_seq[1] = NULL;
6455 ok_event_seq(src2, hEvent2, close_seq, broken_seq, 0);
6456
6457 ret = send(src2, "1", 1, 0);
6458 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
6459 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
6460
6461 ret = send(src2, "1", 1, 0);
6462 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
6463 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
6464
6465 if (useMessages)
6466 {
6467 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, 0);
6468 if (ret)
6469 {
6470 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6471 goto end;
6472 }
6473
6474 ret = set_blocking(src, TRUE);
6475 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6476
6477 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, 0);
6478 if (ret)
6479 {
6480 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6481 goto end;
6482 }
6483
6484 ret = set_blocking(src2, TRUE);
6485 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6486 }
6487 else
6488 {
6489 ret = WSAEventSelect(src, hEvent2, 0);
6490 if (ret)
6491 {
6492 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6493 goto end;
6494 }
6495
6496 ret = set_blocking(src, TRUE);
6497 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6498
6499 ret = WSAEventSelect(src2, hEvent2, 0);
6500 if (ret)
6501 {
6502 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
6503 goto end;
6504 }
6505
6506 ret = set_blocking(src2, TRUE);
6507 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError());
6508 }
6509
6510 end:
6511 if (src != INVALID_SOCKET)
6512 {
6513 flush_events(src, hEvent);
6514 closesocket(src);
6515 }
6516 if (src2 != INVALID_SOCKET)
6517 {
6518 flush_events(src2, hEvent2);
6519 closesocket(src2);
6520 }
6521 HeapFree(GetProcessHeap(), 0, buffer);
6522 if (server != INVALID_SOCKET)
6523 closesocket(server);
6524 if (dst != INVALID_SOCKET)
6525 closesocket(dst);
6526 if (dst2 != INVALID_SOCKET)
6527 closesocket(dst2);
6528 if (hThread != NULL)
6529 CloseHandle(hThread);
6530 if (hWnd != NULL)
6531 DestroyWindow(hWnd);
6532 if (hEvent != NULL)
6533 CloseHandle(hEvent);
6534 if (hEvent2 != NULL)
6535 CloseHandle(hEvent2);
6536 if (ov.hEvent != NULL)
6537 CloseHandle(ov.hEvent);
6538 if (ov2.hEvent != NULL)
6539 CloseHandle(ov2.hEvent);
6540 }
6541
test_ipv6only(void)6542 static void test_ipv6only(void)
6543 {
6544 SOCKET v4 = INVALID_SOCKET, v6;
6545 struct sockaddr_in sin4;
6546 struct sockaddr_in6 sin6;
6547 int ret, enabled, len = sizeof(enabled);
6548
6549 memset(&sin4, 0, sizeof(sin4));
6550 sin4.sin_family = AF_INET;
6551 sin4.sin_port = htons(SERVERPORT);
6552
6553 memset(&sin6, 0, sizeof(sin6));
6554 sin6.sin6_family = AF_INET6;
6555 sin6.sin6_port = htons(SERVERPORT);
6556
6557 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
6558 if (v6 == INVALID_SOCKET)
6559 {
6560 skip("Could not create IPv6 socket (LastError: %d)\n", WSAGetLastError());
6561 goto end;
6562 }
6563
6564 enabled = 2;
6565 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6566 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6567 ok(enabled == 1, "expected 1, got %d\n", enabled);
6568
6569 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
6570 if (ret)
6571 {
6572 skip("Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError());
6573 goto end;
6574 }
6575
6576 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6577 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
6578
6579 todo_wine {
6580 enabled = 2;
6581 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6582 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6583 ok(enabled == 1, "expected 1, got %d\n", enabled);
6584 }
6585
6586 enabled = 0;
6587 len = sizeof(enabled);
6588 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
6589 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6590
6591 todo_wine {
6592 enabled = 2;
6593 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6594 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6595 ok(!enabled, "expected 0, got %d\n", enabled);
6596 }
6597
6598 enabled = 1;
6599 len = sizeof(enabled);
6600 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
6601 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6602
6603 /* bind on IPv4 socket should succeed - IPV6_V6ONLY is enabled by default */
6604 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
6605 ok(!ret, "Could not bind IPv4 address (LastError: %d)\n", WSAGetLastError());
6606
6607 todo_wine {
6608 enabled = 2;
6609 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6610 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6611 ok(enabled == 1, "expected 1, got %d\n", enabled);
6612 }
6613
6614 enabled = 0;
6615 len = sizeof(enabled);
6616 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
6617 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
6618
6619 todo_wine {
6620 enabled = 0;
6621 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6622 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6623 ok(enabled == 1, "expected 1, got %d\n", enabled);
6624 }
6625
6626 enabled = 1;
6627 len = sizeof(enabled);
6628 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
6629 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
6630
6631 closesocket(v4);
6632 closesocket(v6);
6633
6634 /* Test again, this time disabling IPV6_V6ONLY. */
6635 sin4.sin_port = htons(SERVERPORT+2);
6636 sin6.sin6_port = htons(SERVERPORT+2);
6637
6638 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
6639 ok(v6 != INVALID_SOCKET, "Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
6640 WSAGetLastError(), WSAEAFNOSUPPORT);
6641
6642 enabled = 0;
6643 ret = setsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
6644 ok(!ret, "Could not disable IPV6_V6ONLY (LastError: %d).\n", WSAGetLastError());
6645
6646 enabled = 2;
6647 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6648 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6649 ok(!enabled, "expected 0, got %d\n", enabled);
6650
6651 /*
6652 Observaition:
6653 On Windows, bind on both IPv4 and IPv6 with IPV6_V6ONLY disabled succeeds by default.
6654 Application must set SO_EXCLUSIVEADDRUSE on first socket to disallow another successful bind.
6655 In general, a standard application should not use SO_REUSEADDR.
6656 Setting both SO_EXCLUSIVEADDRUSE and SO_REUSEADDR on the same socket is not possible in
6657 either order, the later setsockopt call always fails.
6658 */
6659 enabled = 1;
6660 ret = setsockopt(v6, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&enabled, len);
6661 ok(!ret, "Could not set SO_EXCLUSIVEADDRUSE on IPv6 socket (LastError: %d)\n", WSAGetLastError());
6662
6663 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
6664 ok(!ret, "Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError());
6665
6666 enabled = 2;
6667 len = sizeof(enabled);
6668 getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
6669 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
6670 ok(!enabled, "IPV6_V6ONLY is enabled after bind\n");
6671
6672 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6673 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
6674
6675 enabled = 1;
6676 ret = setsockopt(v4, SOL_SOCKET, SO_REUSEADDR, (char*)&enabled, len);
6677 ok(!ret, "Could not set SO_REUSEADDR on IPv4 socket (LastError: %d)\n", WSAGetLastError());
6678
6679 WSASetLastError(0xdeadbeef);
6680 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
6681 ok(ret, "bind succeeded unexpectedly for the IPv4 socket\n");
6682 ok(WSAGetLastError() == WSAEACCES, "Expected 10013, got %d\n", WSAGetLastError());
6683
6684 end:
6685 if (v4 != INVALID_SOCKET)
6686 closesocket(v4);
6687 if (v6 != INVALID_SOCKET)
6688 closesocket(v6);
6689 }
6690
test_WSASendMsg(void)6691 static void test_WSASendMsg(void)
6692 {
6693 SOCKET sock, dst;
6694 struct sockaddr_in sendaddr, sockaddr;
6695 GUID WSASendMsg_GUID = WSAID_WSASENDMSG;
6696 LPFN_WSASENDMSG pWSASendMsg = NULL;
6697 char teststr[12] = "hello world", buffer[32];
6698 WSABUF iovec[2];
6699 WSAMSG msg;
6700 DWORD bytesSent, err;
6701 int ret, addrlen;
6702
6703 /* FIXME: Missing OVERLAPPED and OVERLAPPED COMPLETION ROUTINE tests */
6704
6705 sock = socket(AF_INET, SOCK_DGRAM, 0);
6706 ok(sock != INVALID_SOCKET, "socket() failed\n");
6707
6708 /* Obtain the WSASendMsg function */
6709 WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSASendMsg_GUID, sizeof(WSASendMsg_GUID),
6710 &pWSASendMsg, sizeof(pWSASendMsg), &err, NULL, NULL);
6711 if (!pWSASendMsg)
6712 {
6713 closesocket(sock);
6714 win_skip("WSASendMsg is unsupported, some tests will be skipped.\n");
6715 return;
6716 }
6717
6718 /* fake address for now */
6719 sendaddr.sin_family = AF_INET;
6720 sendaddr.sin_port = htons(139);
6721 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
6722
6723 memset(&msg, 0, sizeof(msg));
6724 iovec[0].buf = teststr;
6725 iovec[0].len = sizeof(teststr);
6726 iovec[1].buf = teststr;
6727 iovec[1].len = sizeof(teststr) / 2;
6728 msg.name = (struct sockaddr *) &sendaddr;
6729 msg.namelen = sizeof(sendaddr);
6730 msg.lpBuffers = iovec;
6731 msg.dwBufferCount = 1; /* send only one buffer for now */
6732
6733 WSASetLastError(0xdeadbeef);
6734 ret = pWSASendMsg(INVALID_SOCKET, &msg, 0, NULL, NULL, NULL);
6735 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
6736 err = WSAGetLastError();
6737 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
6738
6739 WSASetLastError(0xdeadbeef);
6740 ret = pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL);
6741 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
6742 err = WSAGetLastError();
6743 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
6744
6745 WSASetLastError(0xdeadbeef);
6746 ret = pWSASendMsg(sock, NULL, 0, &bytesSent, NULL, NULL);
6747 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
6748 err = WSAGetLastError();
6749 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
6750
6751 WSASetLastError(0xdeadbeef);
6752 ret = pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL);
6753 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
6754 err = WSAGetLastError();
6755 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
6756
6757 closesocket(sock);
6758
6759 sock = socket(AF_INET, SOCK_DGRAM, 0);
6760 ok(sock != INVALID_SOCKET, "socket() failed\n");
6761
6762 dst = socket(AF_INET, SOCK_DGRAM, 0);
6763 ok(dst != INVALID_SOCKET, "socket() failed\n");
6764
6765 memset(&sockaddr, 0, sizeof(sockaddr));
6766 sockaddr.sin_family = AF_INET;
6767 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
6768 ok(!bind(dst, (struct sockaddr*)&sockaddr, sizeof(sockaddr)),
6769 "bind should have worked\n");
6770
6771 /* read address to find out the port number to be used in send */
6772 memset(&sendaddr, 0, sizeof(sendaddr));
6773 addrlen = sizeof(sendaddr);
6774 ok(!getsockname(dst, (struct sockaddr *) &sendaddr, &addrlen),
6775 "getsockname should have worked\n");
6776 ok(sendaddr.sin_port, "socket port should be != 0\n");
6777
6778 /* ensure the sending socket is not bound */
6779 WSASetLastError(0xdeadbeef);
6780 addrlen = sizeof(sockaddr);
6781 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
6782 ok(ret == SOCKET_ERROR, "getsockname should have failed\n");
6783 err = WSAGetLastError();
6784 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
6785
6786 set_blocking(sock, TRUE);
6787
6788 bytesSent = 0;
6789 SetLastError(0xdeadbeef);
6790 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
6791 ok(!ret, "WSASendMsg should have worked\n");
6792 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
6793 "Expected 0, got %d\n", GetLastError());
6794 ok(bytesSent == iovec[0].len, "incorret bytes sent, expected %d, sent %d\n",
6795 iovec[0].len, bytesSent);
6796
6797 /* receive data */
6798 addrlen = sizeof(sockaddr);
6799 memset(buffer, 0, sizeof(buffer));
6800 SetLastError(0xdeadbeef);
6801 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
6802 ok(ret == bytesSent, "got %d, expected %d\n",
6803 ret, bytesSent);
6804 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6805
6806 /* A successful call to WSASendMsg must have bound the socket */
6807 addrlen = sizeof(sockaddr);
6808 sockaddr.sin_port = 0;
6809 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
6810 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
6811 ok(!ret, "getsockname should have worked\n");
6812 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
6813 inet_ntoa(sockaddr.sin_addr));
6814 ok(sockaddr.sin_port, "sin_port should be != 0\n");
6815
6816 msg.dwBufferCount = 2; /* send both buffers */
6817
6818 bytesSent = 0;
6819 SetLastError(0xdeadbeef);
6820 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
6821 ok(!ret, "WSASendMsg should have worked\n");
6822 ok(bytesSent == iovec[0].len + iovec[1].len, "incorret bytes sent, expected %d, sent %d\n",
6823 iovec[0].len + iovec[1].len, bytesSent);
6824 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
6825 "Expected 0, got %d\n", GetLastError());
6826
6827 /* receive data */
6828 addrlen = sizeof(sockaddr);
6829 memset(buffer, 0, sizeof(buffer));
6830 SetLastError(0xdeadbeef);
6831 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
6832 ok(ret == bytesSent, "got %d, expected %d\n",
6833 ret, bytesSent);
6834 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6835
6836 closesocket(sock);
6837 closesocket(dst);
6838
6839 /* a bad call to WSASendMsg will also bind the socket */
6840 addrlen = sizeof(sockaddr);
6841 sockaddr.sin_port = 0;
6842 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
6843 sock = socket(AF_INET, SOCK_DGRAM, 0);
6844 ok(sock != INVALID_SOCKET, "socket() failed\n");
6845 ok(pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
6846 todo_wine {
6847 ok(!getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have worked\n");
6848 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
6849 inet_ntoa(sockaddr.sin_addr));
6850 ok(sockaddr.sin_port, "sin_port should be > 0\n");
6851 }
6852 closesocket(sock);
6853
6854 /* a bad call without msg parameter will not trigger the auto-bind */
6855 sock = socket(AF_INET, SOCK_DGRAM, 0);
6856 ok(sock != INVALID_SOCKET, "socket() failed\n");
6857 ok(pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
6858 ok(getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have failed\n");
6859 err = WSAGetLastError();
6860 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
6861 closesocket(sock);
6862
6863 /* SOCK_STREAM sockets are not supported */
6864 bytesSent = 0;
6865 sock = socket(AF_INET, SOCK_STREAM, 0);
6866 ok(sock != INVALID_SOCKET, "socket() failed\n");
6867 SetLastError(0xdeadbeef);
6868 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
6869 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
6870 err = WSAGetLastError();
6871 todo_wine
6872 ok(err == WSAEINVAL, "expected 10014, got %d instead\n", err);
6873 closesocket(sock);
6874 }
6875
test_WSASendTo(void)6876 static void test_WSASendTo(void)
6877 {
6878 SOCKET s;
6879 struct sockaddr_in addr;
6880 char buf[12] = "hello world";
6881 WSABUF data_buf;
6882 DWORD bytesSent;
6883 int ret;
6884
6885 addr.sin_family = AF_INET;
6886 addr.sin_port = htons(139);
6887 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
6888 data_buf.len = sizeof(buf);
6889 data_buf.buf = buf;
6890
6891 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
6892 ok(0, "socket() failed error: %d\n", WSAGetLastError());
6893 return;
6894 }
6895
6896 WSASetLastError(12345);
6897 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
6898 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
6899 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
6900
6901 WSASetLastError(12345);
6902 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
6903 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
6904 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
6905
6906 WSASetLastError(12345);
6907 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
6908 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
6909 return;
6910 }
6911 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
6912 "a successful call to WSASendTo()\n");
6913 }
6914
recv_thread(LPVOID arg)6915 static DWORD WINAPI recv_thread(LPVOID arg)
6916 {
6917 SOCKET sock = *(SOCKET *)arg;
6918 char buffer[32];
6919 WSABUF wsa;
6920 WSAOVERLAPPED ov;
6921 DWORD flags = 0;
6922
6923 wsa.buf = buffer;
6924 wsa.len = sizeof(buffer);
6925 ov.hEvent = WSACreateEvent();
6926 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL);
6927
6928 WaitForSingleObject(ov.hEvent, 1000);
6929 WSACloseEvent(ov.hEvent);
6930 return 0;
6931 }
6932
6933 static int completion_called;
6934
io_completion(DWORD error,DWORD transferred,WSAOVERLAPPED * overlapped,DWORD flags)6935 static void WINAPI io_completion(DWORD error, DWORD transferred, WSAOVERLAPPED *overlapped, DWORD flags)
6936 {
6937 completion_called++;
6938 }
6939
test_WSARecv(void)6940 static void test_WSARecv(void)
6941 {
6942 SOCKET src, dest, server = INVALID_SOCKET;
6943 char buf[20];
6944 WSABUF bufs[2];
6945 WSAOVERLAPPED ov;
6946 DWORD bytesReturned, flags, id;
6947 struct linger ling;
6948 struct sockaddr_in addr;
6949 int iret, len;
6950 DWORD dwret;
6951 BOOL bret;
6952 HANDLE thread, event = NULL, io_port;
6953
6954 tcp_socketpair(&src, &dest);
6955 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
6956 {
6957 skip("failed to create sockets\n");
6958 goto end;
6959 }
6960
6961 memset(&ov, 0, sizeof(ov));
6962 flags = 0;
6963 bufs[0].len = 2;
6964 bufs[0].buf = buf;
6965
6966 /* Send 4 bytes and receive in two calls of 2 */
6967 SetLastError(0xdeadbeef);
6968 iret = send(src, "test", 4, 0);
6969 ok(iret == 4, "Expected 4, got %d\n", iret);
6970 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6971 SetLastError(0xdeadbeef);
6972 bytesReturned = 0xdeadbeef;
6973 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
6974 ok(!iret, "Expected 0, got %d\n", iret);
6975 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
6976 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6977 SetLastError(0xdeadbeef);
6978 bytesReturned = 0xdeadbeef;
6979 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
6980 ok(!iret, "Expected 0, got %d\n", iret);
6981 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
6982 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6983
6984 bufs[0].len = 4;
6985 SetLastError(0xdeadbeef);
6986 iret = send(src, "test", 4, 0);
6987 ok(iret == 4, "Expected 4, got %d\n", iret);
6988 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6989 SetLastError(0xdeadbeef);
6990 bytesReturned = 0xdeadbeef;
6991 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
6992 ok(!iret, "Expected 0, got %d\n", iret);
6993 ok(bytesReturned == 4, "Expected 4, got %d\n", bytesReturned);
6994 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
6995
6996 /* Test 2 buffers */
6997 bufs[0].len = 4;
6998 bufs[1].len = 5;
6999 bufs[1].buf = buf + 10;
7000 SetLastError(0xdeadbeef);
7001 iret = send(src, "deadbeefs", 9, 0);
7002 ok(iret == 9, "Expected 9, got %d\n", iret);
7003 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
7004 SetLastError(0xdeadbeef);
7005 bytesReturned = 0xdeadbeef;
7006 iret = WSARecv(dest, bufs, 2, &bytesReturned, &flags, NULL, NULL);
7007 ok(!iret, "Expected 0, got %d\n", iret);
7008 ok(bytesReturned == 9, "Expected 9, got %d\n", bytesReturned);
7009 bufs[0].buf[4] = '\0';
7010 bufs[1].buf[5] = '\0';
7011 ok(!strcmp(bufs[0].buf, "dead"), "buf[0] doesn't match: %s != dead\n", bufs[0].buf);
7012 ok(!strcmp(bufs[1].buf, "beefs"), "buf[1] doesn't match: %s != beefs\n", bufs[1].buf);
7013 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
7014
7015 bufs[0].len = sizeof(buf);
7016 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
7017 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
7018 if (!event)
7019 goto end;
7020
7021 ling.l_onoff = 1;
7022 ling.l_linger = 0;
7023 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
7024 ok(!iret, "Failed to set linger %d\n", GetLastError());
7025
7026 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, NULL);
7027 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
7028
7029 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, &ov, NULL);
7030 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
7031
7032 closesocket(src);
7033 src = INVALID_SOCKET;
7034
7035 dwret = WaitForSingleObject(ov.hEvent, 1000);
7036 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
7037
7038 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
7039 todo_wine ok(!bret && (GetLastError() == ERROR_NETNAME_DELETED || broken(GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */),
7040 "Did not get disconnect event: %d, error %d\n", bret, GetLastError());
7041 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
7042 closesocket(dest);
7043 dest = INVALID_SOCKET;
7044
7045 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
7046 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
7047 if (src == INVALID_SOCKET) goto end;
7048
7049 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
7050 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
7051 if (server == INVALID_SOCKET) goto end;
7052
7053 memset(&addr, 0, sizeof(addr));
7054 addr.sin_family = AF_INET;
7055 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
7056 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
7057 if (iret) goto end;
7058
7059 len = sizeof(addr);
7060 iret = getsockname(server, (struct sockaddr *)&addr, &len);
7061 if (iret) goto end;
7062
7063 iret = listen(server, 1);
7064 if (iret) goto end;
7065
7066 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr));
7067 if (iret) goto end;
7068
7069 len = sizeof(addr);
7070 dest = accept(server, (struct sockaddr *)&addr, &len);
7071 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
7072 if (dest == INVALID_SOCKET) goto end;
7073
7074 send(src, "test message", sizeof("test message"), 0);
7075 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id);
7076 WaitForSingleObject(thread, 3000);
7077 CloseHandle(thread);
7078
7079 memset(&ov, 0, sizeof(ov));
7080 ov.hEvent = event;
7081 ResetEvent(event);
7082 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
7083 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
7084 send(src, "test message", sizeof("test message"), 0);
7085
7086 completion_called = 0;
7087 dwret = SleepEx(1000, TRUE);
7088 ok(dwret == WAIT_IO_COMPLETION, "got %u\n", dwret);
7089 ok(completion_called == 1, "completion not called\n");
7090
7091 dwret = WaitForSingleObject(event, 1);
7092 ok(dwret == WAIT_TIMEOUT, "got %u\n", dwret);
7093
7094 io_port = CreateIoCompletionPort( (HANDLE)dest, NULL, 0, 0 );
7095 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7096
7097 /* Using completion function on socket associated with completion port is not allowed. */
7098 memset(&ov, 0, sizeof(ov));
7099 completion_called = 0;
7100 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
7101 ok(iret == SOCKET_ERROR && GetLastError() == WSAEINVAL, "WSARecv failed - %d error %d\n", iret, GetLastError());
7102 ok(!completion_called, "completion called\n");
7103
7104 CloseHandle(io_port);
7105
7106 end:
7107 if (server != INVALID_SOCKET)
7108 closesocket(server);
7109 if (dest != INVALID_SOCKET)
7110 closesocket(dest);
7111 if (src != INVALID_SOCKET)
7112 closesocket(src);
7113 if (event)
7114 WSACloseEvent(event);
7115 }
7116
7117 struct write_watch_thread_args
7118 {
7119 int func;
7120 SOCKET dest;
7121 void *base;
7122 DWORD size;
7123 const char *expect;
7124 };
7125
write_watch_thread(void * arg)7126 static DWORD CALLBACK write_watch_thread( void *arg )
7127 {
7128 struct write_watch_thread_args *args = arg;
7129 struct sockaddr addr;
7130 int addr_len = sizeof(addr), ret;
7131 DWORD bytes, flags = 0;
7132 WSABUF buf[1];
7133
7134 switch (args->func)
7135 {
7136 case 0:
7137 ret = recv( args->dest, args->base, args->size, 0 );
7138 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
7139 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
7140 break;
7141 case 1:
7142 ret = recvfrom( args->dest, args->base, args->size, 0, &addr, &addr_len );
7143 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
7144 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
7145 break;
7146 case 2:
7147 buf[0].len = args->size;
7148 buf[0].buf = args->base;
7149 ret = WSARecv( args->dest, buf, 1, &bytes, &flags, NULL, NULL );
7150 ok( !ret, "WSARecv failed %u\n", GetLastError() );
7151 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
7152 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
7153 break;
7154 case 3:
7155 buf[0].len = args->size;
7156 buf[0].buf = args->base;
7157 ret = WSARecvFrom( args->dest, buf, 1, &bytes, &flags, &addr, &addr_len, NULL, NULL );
7158 ok( !ret, "WSARecvFrom failed %u\n", GetLastError() );
7159 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
7160 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
7161 break;
7162 }
7163 return 0;
7164 }
7165
test_write_watch(void)7166 static void test_write_watch(void)
7167 {
7168 SOCKET src, dest;
7169 WSABUF bufs[2];
7170 WSAOVERLAPPED ov;
7171 struct write_watch_thread_args args;
7172 DWORD bytesReturned, flags, size;
7173 struct sockaddr addr;
7174 int addr_len, ret;
7175 HANDLE thread, event;
7176 char *base;
7177 void *results[64];
7178 ULONG_PTR count;
7179 ULONG pagesize;
7180 UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
7181
7182 pGetWriteWatch = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetWriteWatch" );
7183 if (!pGetWriteWatch)
7184 {
7185 win_skip( "write watched not supported\n" );
7186 return;
7187 }
7188
7189 tcp_socketpair(&src, &dest);
7190 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
7191 {
7192 skip("failed to create sockets\n");
7193 return;
7194 }
7195
7196 memset(&ov, 0, sizeof(ov));
7197 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
7198 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
7199
7200 flags = 0;
7201
7202 size = 0x10000;
7203 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
7204 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
7205
7206 #ifdef __REACTOS__
7207 if (!base)
7208 {
7209 skip("VirtualAlloc(MEM_WRITE_WATCH) is not supported yet on ReactOS\n");
7210 skip("Skipping tests due to hang. See ROSTESTS-385\n");
7211 return;
7212 }
7213 #endif
7214
7215 memset( base, 0, size );
7216 count = 64;
7217 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7218 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7219 ok( count == 16, "wrong count %lu\n", count );
7220
7221 bufs[0].len = 5;
7222 bufs[0].buf = base;
7223 bufs[1].len = 0x8000;
7224 bufs[1].buf = base + 0x4000;
7225
7226 ret = WSARecv( dest, bufs, 2, NULL, &flags, &ov, NULL);
7227 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
7228 "WSARecv failed - %d error %d\n", ret, GetLastError());
7229
7230 count = 64;
7231 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7232 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7233 ok( count == 9, "wrong count %lu\n", count );
7234 ok( !base[0], "data set\n" );
7235
7236 send(src, "test message", sizeof("test message"), 0);
7237
7238 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
7239 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
7240 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
7241 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
7242 ok( !memcmp( base + 0x4000, "message", 8 ), "wrong data %s\n", base + 0x4000 );
7243
7244 count = 64;
7245 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7246 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7247 ok( count == 0, "wrong count %lu\n", count );
7248
7249 memset( base, 0, size );
7250 count = 64;
7251 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7252 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7253 ok( count == 16, "wrong count %lu\n", count );
7254
7255 bufs[1].len = 0x4000;
7256 bufs[1].buf = base + 0x2000;
7257 ret = WSARecvFrom( dest, bufs, 2, NULL, &flags, &addr, &addr_len, &ov, NULL);
7258 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
7259 "WSARecv failed - %d error %d\n", ret, GetLastError());
7260
7261 count = 64;
7262 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7263 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7264 ok( count == 5, "wrong count %lu\n", count );
7265 ok( !base[0], "data set\n" );
7266
7267 send(src, "test message", sizeof("test message"), 0);
7268
7269 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
7270 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
7271 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
7272 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
7273 ok( !memcmp( base + 0x2000, "message", 8 ), "wrong data %s\n", base + 0x2000 );
7274
7275 count = 64;
7276 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7277 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7278 ok( count == 0, "wrong count %lu\n", count );
7279
7280 memset( base, 0, size );
7281 count = 64;
7282 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7283 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7284 ok( count == 16, "wrong count %lu\n", count );
7285
7286 args.dest = dest;
7287 args.base = base;
7288 args.size = 0x7002;
7289 args.expect = "test message";
7290 for (args.func = 0; args.func < 4; args.func++)
7291 {
7292 thread = CreateThread( NULL, 0, write_watch_thread, &args, 0, NULL );
7293 Sleep( 200 );
7294
7295 count = 64;
7296 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7297 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7298 ok( count == 8, "wrong count %lu\n", count );
7299
7300 send(src, "test message", sizeof("test message"), 0);
7301 WaitForSingleObject( thread, 10000 );
7302 CloseHandle( thread );
7303
7304 count = 64;
7305 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
7306 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
7307 ok( count == 0, "wrong count %lu\n", count );
7308 }
7309 WSACloseEvent( event );
7310 closesocket( dest );
7311 closesocket( src );
7312 VirtualFree( base, 0, MEM_FREE );
7313 }
7314
7315 #define POLL_CLEAR() ix = 0
7316 #define POLL_SET(s, ev) {fds[ix].fd = s; fds[ix++].events = ev;}
7317 #define POLL_ISSET(s, rev) poll_isset(fds, ix, s, rev)
poll_isset(WSAPOLLFD * fds,int max,SOCKET s,int rev)7318 static BOOL poll_isset(WSAPOLLFD *fds, int max, SOCKET s, int rev)
7319 {
7320 int k;
7321 for (k = 0; k < max; k++)
7322 if (fds[k].fd == s && (fds[k].revents == rev)) return TRUE;
7323 return FALSE;
7324 }
7325
test_WSAPoll(void)7326 static void test_WSAPoll(void)
7327 {
7328 int ix, ret, err, poll_timeout;
7329 SOCKET fdListen, fdRead, fdWrite;
7330 struct sockaddr_in address;
7331 socklen_t len;
7332 static char tmp_buf[1024];
7333 WSAPOLLFD fds[16];
7334 HANDLE thread_handle;
7335 DWORD id;
7336
7337 if (!pWSAPoll) /* >= Vista */
7338 {
7339 skip("WSAPoll is unsupported, some tests will be skipped.\n");
7340 return;
7341 }
7342
7343 /* Invalid parameters test */
7344 SetLastError(0xdeadbeef);
7345 ret = pWSAPoll(NULL, 0, 0);
7346 err = GetLastError();
7347 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
7348 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
7349 SetLastError(0xdeadbeef);
7350 ret = pWSAPoll(NULL, 1, 0);
7351 err = GetLastError();
7352 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
7353 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
7354 SetLastError(0xdeadbeef);
7355 ret = pWSAPoll(NULL, 0, 1);
7356 err = GetLastError();
7357 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
7358 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
7359 SetLastError(0xdeadbeef);
7360 ret = pWSAPoll(NULL, 1, 1);
7361 err = GetLastError();
7362 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
7363 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
7364
7365 /* WSAPoll() tries to mime the unix poll() call. The following tests do:
7366 * - check if a connection attempt ended with success or error;
7367 * - check if a pending connection is waiting for acceptance;
7368 * - check for data to read, availability for write and OOB data
7369 */
7370 memset(&address, 0, sizeof(address));
7371 address.sin_addr.s_addr = inet_addr("127.0.0.1");
7372 address.sin_family = AF_INET;
7373 len = sizeof(address);
7374 fdListen = setup_server_socket(&address, &len);
7375 poll_timeout = 100;
7376
7377 /* When no events are pending poll returns 0 with no error */
7378 POLL_CLEAR();
7379 POLL_SET(fdListen, POLLIN);
7380 ret = pWSAPoll(fds, ix, poll_timeout);
7381 ok(ret == 0, "expected 0, got %d\n", ret);
7382
7383 /* Test listening socket connection attempt notifications */
7384 fdWrite = setup_connector_socket(&address, len, TRUE);
7385 POLL_CLEAR();
7386 POLL_SET(fdListen, POLLIN | POLLOUT);
7387 ret = pWSAPoll(fds, ix, poll_timeout);
7388 ok(ret == 1, "expected 1, got %d\n", ret);
7389 ok(POLL_ISSET(fdListen, POLLRDNORM), "fdListen socket events incorrect\n");
7390 len = sizeof(address);
7391 fdRead = accept(fdListen, (struct sockaddr*) &address, &len);
7392 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n");
7393
7394 /* Test client side connection attempt notifications */
7395 POLL_CLEAR();
7396 POLL_SET(fdListen, POLLIN | POLLOUT);
7397 POLL_SET(fdRead, POLLIN | POLLOUT);
7398 POLL_SET(fdWrite, POLLIN | POLLOUT);
7399 ret = pWSAPoll(fds, ix, poll_timeout);
7400 ok(ret == 2, "expected 2, got %d\n", ret);
7401 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n");
7402 ok(POLL_ISSET(fdRead, POLLWRNORM), "fdRead socket events incorrect\n");
7403 len = sizeof(id);
7404 id = 0xdeadbeef;
7405 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
7406 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
7407 ok(id == 0, "expected 0, got %d\n", id);
7408
7409 /* Test data receiving notifications */
7410 ret = send(fdWrite, "1234", 4, 0);
7411 ok(ret == 4, "expected 4, got %d\n", ret);
7412 POLL_CLEAR();
7413 POLL_SET(fdListen, POLLIN | POLLOUT);
7414 POLL_SET(fdRead, POLLIN);
7415 ret = pWSAPoll(fds, ix, poll_timeout);
7416 ok(ret == 1, "expected 1, got %d\n", ret);
7417 ok(POLL_ISSET(fdRead, POLLRDNORM), "fdRead socket events incorrect\n");
7418 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
7419 ok(ret == 4, "expected 4, got %d\n", ret);
7420 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n");
7421
7422 /* Test OOB data notifications */
7423 ret = send(fdWrite, "A", 1, MSG_OOB);
7424 ok(ret == 1, "expected 1, got %d\n", ret);
7425 POLL_CLEAR();
7426 POLL_SET(fdListen, POLLIN | POLLOUT);
7427 POLL_SET(fdRead, POLLIN);
7428 ret = pWSAPoll(fds, ix, poll_timeout);
7429 ok(ret == 1, "expected 1, got %d\n", ret);
7430 ok(POLL_ISSET(fdRead, POLLRDBAND), "fdRead socket events incorrect\n");
7431 tmp_buf[0] = 0xAF;
7432 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
7433 ok(ret == 1, "expected 1, got %d\n", ret);
7434 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
7435
7436 /* If the socket is OOBINLINED the notification is like normal data */
7437 ret = 1;
7438 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret));
7439 ok(ret == 0, "expected 0, got %d\n", ret);
7440 ret = send(fdWrite, "A", 1, MSG_OOB);
7441 ok(ret == 1, "expected 1, got %d\n", ret);
7442 POLL_CLEAR();
7443 POLL_SET(fdListen, POLLIN | POLLOUT);
7444 POLL_SET(fdRead, POLLIN | POLLOUT);
7445 ret = pWSAPoll(fds, ix, poll_timeout);
7446 ok(ret == 1, "expected 1, got %d\n", ret);
7447 tmp_buf[0] = 0xAF;
7448 SetLastError(0xdeadbeef);
7449 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
7450 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
7451 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError());
7452 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
7453 ok(ret == 1, "expected 1, got %d\n", ret);
7454 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
7455
7456 /* Test connection closed notifications */
7457 ret = closesocket(fdRead);
7458 ok(ret == 0, "expected 0, got %d\n", ret);
7459 POLL_CLEAR();
7460 POLL_SET(fdListen, POLLIN | POLLOUT);
7461 POLL_SET(fdWrite, POLLIN);
7462 ret = pWSAPoll(fds, ix, poll_timeout);
7463 ok(ret == 1, "expected 1, got %d\n", ret);
7464 ok(POLL_ISSET(fdWrite, POLLHUP), "fdWrite socket events incorrect\n");
7465 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0);
7466 ok(ret == 0, "expected 0, got %d\n", ret);
7467
7468 /* When a connection is attempted to a non-listening socket due to a bug
7469 * in the MS code it will never be notified. This is a long standing issue
7470 * that will never be fixed for compatibility reasons so we have to deal
7471 * with it manually. */
7472 ret = closesocket(fdWrite);
7473 ok(ret == 0, "expected 0, got %d\n", ret);
7474 ret = closesocket(fdListen);
7475 ok(ret == 0, "expected 0, got %d\n", ret);
7476 len = sizeof(address);
7477 fdWrite = setup_connector_socket(&address, len, TRUE);
7478 POLL_CLEAR();
7479 POLL_SET(fdWrite, POLLIN | POLLOUT);
7480 poll_timeout = 2000;
7481 ret = pWSAPoll(fds, ix, poll_timeout);
7482 todo_wine
7483 ok(ret == 0, "expected 0, got %d\n", ret);
7484 len = sizeof(id);
7485 id = 0xdeadbeef;
7486 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
7487 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
7488 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id);
7489 closesocket(fdWrite);
7490
7491 /* Try poll() on a closed socket after connection */
7492 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
7493 closesocket(fdRead);
7494 POLL_CLEAR();
7495 POLL_SET(fdWrite, POLLIN | POLLOUT);
7496 POLL_SET(fdRead, POLLIN | POLLOUT);
7497 ret = pWSAPoll(fds, ix, poll_timeout);
7498 ok(ret == 1, "expected 1, got %d\n", ret);
7499 ok(POLL_ISSET(fdRead, POLLNVAL), "fdRead socket events incorrect\n");
7500 POLL_CLEAR();
7501 POLL_SET(fdWrite, POLLIN | POLLOUT);
7502 ret = pWSAPoll(fds, ix, poll_timeout);
7503 ok(ret == 1, "expected 1, got %d\n", ret);
7504 todo_wine
7505 ok(POLL_ISSET(fdWrite, POLLWRNORM | POLLHUP) || broken(POLL_ISSET(fdWrite, POLLWRNORM)) /* <= 2008 */,
7506 "fdWrite socket events incorrect\n");
7507 closesocket(fdWrite);
7508
7509 /* Close the socket currently being polled in a thread */
7510 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
7511 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
7512 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
7513 POLL_CLEAR();
7514 POLL_SET(fdWrite, POLLIN | POLLOUT);
7515 ret = pWSAPoll(fds, ix, poll_timeout);
7516 ok(ret == 1, "expected 1, got %d\n", ret);
7517 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n");
7518 WaitForSingleObject (thread_handle, 1000);
7519 closesocket(fdRead);
7520 /* test again with less flags - behavior changes */
7521 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n");
7522 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
7523 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
7524 POLL_CLEAR();
7525 POLL_SET(fdWrite, POLLIN);
7526 ret = pWSAPoll(fds, ix, poll_timeout);
7527 ok(ret == 1, "expected 1, got %d\n", ret);
7528 ok(POLL_ISSET(fdWrite, POLLNVAL), "fdWrite socket events incorrect\n");
7529 WaitForSingleObject (thread_handle, 1000);
7530 closesocket(fdRead);
7531 }
7532 #undef POLL_SET
7533 #undef POLL_ISSET
7534 #undef POLL_CLEAR
7535
test_GetAddrInfoW(void)7536 static void test_GetAddrInfoW(void)
7537 {
7538 static const WCHAR port[] = {'8','0',0};
7539 static const WCHAR empty[] = {0};
7540 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
7541 static const WCHAR nxdomain[] =
7542 {'n','x','d','o','m','a','i','n','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
7543 static const WCHAR zero[] = {'0',0};
7544 int i, ret;
7545 ADDRINFOW *result, *result2, *p, hint;
7546 WCHAR name[256];
7547 DWORD size = sizeof(name)/sizeof(WCHAR);
7548 /* te su to.winehq.org written in katakana */
7549 static const WCHAR idn_domain[] =
7550 {0x30C6,0x30B9,0x30C8,'.','w','i','n','e','h','q','.','o','r','g',0};
7551 static const WCHAR idn_punycode[] =
7552 {'x','n','-','-','z','c','k','z','a','h','.','w','i','n','e','h','q','.','o','r','g',0};
7553
7554 if (!pGetAddrInfoW || !pFreeAddrInfoW)
7555 {
7556 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
7557 return;
7558 }
7559 memset(&hint, 0, sizeof(ADDRINFOW));
7560 name[0] = 0;
7561 GetComputerNameExW( ComputerNamePhysicalDnsHostname, name, &size );
7562
7563 result = (ADDRINFOW *)0xdeadbeef;
7564 WSASetLastError(0xdeadbeef);
7565 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
7566 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7567 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7568 ok(result == NULL, "got %p\n", result);
7569
7570 result = NULL;
7571 WSASetLastError(0xdeadbeef);
7572 ret = pGetAddrInfoW(empty, NULL, NULL, &result);
7573 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7574 ok(result != NULL, "GetAddrInfoW failed\n");
7575 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7576 pFreeAddrInfoW(result);
7577
7578 result = NULL;
7579 ret = pGetAddrInfoW(NULL, zero, NULL, &result);
7580 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7581 ok(result != NULL, "GetAddrInfoW failed\n");
7582
7583 result2 = NULL;
7584 ret = pGetAddrInfoW(NULL, empty, NULL, &result2);
7585 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7586 ok(result2 != NULL, "GetAddrInfoW failed\n");
7587 compare_addrinfow(result, result2);
7588 pFreeAddrInfoW(result);
7589 pFreeAddrInfoW(result2);
7590
7591 result = NULL;
7592 ret = pGetAddrInfoW(empty, zero, NULL, &result);
7593 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7594 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7595 ok(result != NULL, "GetAddrInfoW failed\n");
7596
7597 result2 = NULL;
7598 ret = pGetAddrInfoW(empty, empty, NULL, &result2);
7599 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7600 ok(result2 != NULL, "GetAddrInfoW failed\n");
7601 compare_addrinfow(result, result2);
7602 pFreeAddrInfoW(result);
7603 pFreeAddrInfoW(result2);
7604
7605 result = NULL;
7606 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
7607 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7608 pFreeAddrInfoW(result);
7609
7610 result = NULL;
7611 ret = pGetAddrInfoW(localhost, empty, NULL, &result);
7612 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7613 pFreeAddrInfoW(result);
7614
7615 result = NULL;
7616 ret = pGetAddrInfoW(localhost, zero, NULL, &result);
7617 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7618 pFreeAddrInfoW(result);
7619
7620 result = NULL;
7621 ret = pGetAddrInfoW(localhost, port, NULL, &result);
7622 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7623 pFreeAddrInfoW(result);
7624
7625 result = NULL;
7626 ret = pGetAddrInfoW(localhost, NULL, &hint, &result);
7627 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7628 pFreeAddrInfoW(result);
7629
7630 result = NULL;
7631 SetLastError(0xdeadbeef);
7632 ret = pGetAddrInfoW(localhost, port, &hint, &result);
7633 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7634 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7635 pFreeAddrInfoW(result);
7636
7637 /* try to get information from the computer name, result is the same
7638 * as if requesting with an empty host name. */
7639 ret = pGetAddrInfoW(name, NULL, NULL, &result);
7640 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7641 ok(result != NULL, "GetAddrInfoW failed\n");
7642
7643 ret = pGetAddrInfoW(empty, NULL, NULL, &result2);
7644 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7645 #ifdef __REACTOS__
7646 ok(result2 != NULL, "GetAddrInfoW failed\n");
7647 #else
7648 ok(result != NULL, "GetAddrInfoW failed\n");
7649 #endif
7650 compare_addrinfow(result, result2);
7651 pFreeAddrInfoW(result);
7652 pFreeAddrInfoW(result2);
7653
7654 ret = pGetAddrInfoW(name, empty, NULL, &result);
7655 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7656 ok(result != NULL, "GetAddrInfoW failed\n");
7657
7658 ret = pGetAddrInfoW(empty, empty, NULL, &result2);
7659 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
7660 #ifdef __REACTOS__
7661 ok(result2 != NULL, "GetAddrInfoW failed\n");
7662 #else
7663 ok(result != NULL, "GetAddrInfoW failed\n");
7664 #endif
7665 compare_addrinfow(result, result2);
7666 pFreeAddrInfoW(result);
7667 pFreeAddrInfoW(result2);
7668
7669 result = (ADDRINFOW *)0xdeadbeef;
7670 WSASetLastError(0xdeadbeef);
7671 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
7672 if(ret == 0)
7673 {
7674 skip("nxdomain returned success. Broken ISP redirects?\n");
7675 return;
7676 }
7677 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7678 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7679 ok(result == NULL, "got %p\n", result);
7680
7681 result = (ADDRINFOW *)0xdeadbeef;
7682 WSASetLastError(0xdeadbeef);
7683 ret = pGetAddrInfoW(nxdomain, NULL, NULL, &result);
7684 if(ret == 0)
7685 {
7686 skip("nxdomain returned success. Broken ISP redirects?\n");
7687 return;
7688 }
7689 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7690 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7691 ok(result == NULL, "got %p\n", result);
7692
7693 for (i = 0;i < (sizeof(hinttests) / sizeof(hinttests[0]));i++)
7694 {
7695 hint.ai_family = hinttests[i].family;
7696 hint.ai_socktype = hinttests[i].socktype;
7697 hint.ai_protocol = hinttests[i].protocol;
7698
7699 result = NULL;
7700 SetLastError(0xdeadbeef);
7701 ret = pGetAddrInfoW(localhost, NULL, &hint, &result);
7702 if (!ret)
7703 {
7704 if (hinttests[i].error)
7705 ok(0, "test %d: GetAddrInfoW succeeded unexpectedly\n", i);
7706 else
7707 {
7708 p = result;
7709 do
7710 {
7711 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
7712 if (hinttests[i].family == AF_UNSPEC)
7713 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6,
7714 "test %d: expected AF_INET or AF_INET6, got %d\n",
7715 i, p->ai_family);
7716 else
7717 ok(p->ai_family == hinttests[i].family,
7718 "test %d: expected family %d, got %d\n",
7719 i, hinttests[i].family, p->ai_family);
7720
7721 ok(p->ai_socktype == hinttests[i].socktype,
7722 "test %d: expected type %d, got %d\n",
7723 i, hinttests[i].socktype, p->ai_socktype);
7724 ok(p->ai_protocol == hinttests[i].protocol,
7725 "test %d: expected protocol %d, got %d\n",
7726 i, hinttests[i].protocol, p->ai_protocol);
7727 p = p->ai_next;
7728 }
7729 while (p);
7730 }
7731 pFreeAddrInfoW(result);
7732 }
7733 else
7734 {
7735 DWORD err = WSAGetLastError();
7736 if (hinttests[i].error)
7737 ok(hinttests[i].error == err, "test %d: GetAddrInfoW failed with error %d, expected %d\n",
7738 i, err, hinttests[i].error);
7739 else
7740 ok(0, "test %d: GetAddrInfoW failed with %d (err %d)\n", i, ret, err);
7741 }
7742 }
7743
7744 /* Test IDN resolution (Internationalized Domain Names) present since Windows 8 */
7745 trace("Testing punycode IDN %s\n", wine_dbgstr_w(idn_punycode));
7746 result = NULL;
7747 ret = pGetAddrInfoW(idn_punycode, NULL, NULL, &result);
7748 ok(!ret, "got %d expected success\n", ret);
7749 ok(result != NULL, "got %p\n", result);
7750 pFreeAddrInfoW(result);
7751
7752 hint.ai_family = AF_INET;
7753 hint.ai_socktype = 0;
7754 hint.ai_protocol = 0;
7755 hint.ai_flags = 0;
7756
7757 result = NULL;
7758 ret = pGetAddrInfoW(idn_punycode, NULL, &hint, &result);
7759 ok(!ret, "got %d expected success\n", ret);
7760 ok(result != NULL, "got %p\n", result);
7761
7762 trace("Testing unicode IDN %s\n", wine_dbgstr_w(idn_domain));
7763 result2 = NULL;
7764 ret = pGetAddrInfoW(idn_domain, NULL, NULL, &result2);
7765 if (ret == WSAHOST_NOT_FOUND && broken(1))
7766 {
7767 pFreeAddrInfoW(result);
7768 win_skip("IDN resolution not supported in Win <= 7\n");
7769 return;
7770 }
7771
7772 ok(!ret, "got %d expected success\n", ret);
7773 ok(result2 != NULL, "got %p\n", result2);
7774 pFreeAddrInfoW(result2);
7775
7776 hint.ai_family = AF_INET;
7777 hint.ai_socktype = 0;
7778 hint.ai_protocol = 0;
7779 hint.ai_flags = 0;
7780
7781 result2 = NULL;
7782 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2);
7783 ok(!ret, "got %d expected success\n", ret);
7784 ok(result2 != NULL, "got %p\n", result2);
7785
7786 /* ensure manually resolved punycode and unicode hosts result in same data */
7787 compare_addrinfow(result, result2);
7788
7789 pFreeAddrInfoW(result);
7790 pFreeAddrInfoW(result2);
7791
7792 hint.ai_family = AF_INET;
7793 hint.ai_socktype = 0;
7794 hint.ai_protocol = 0;
7795 hint.ai_flags = 0;
7796
7797 result2 = NULL;
7798 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2);
7799 ok(!ret, "got %d expected success\n", ret);
7800 ok(result2 != NULL, "got %p\n", result2);
7801 pFreeAddrInfoW(result2);
7802
7803 /* Disable IDN resolution and test again*/
7804 hint.ai_family = AF_INET;
7805 hint.ai_socktype = 0;
7806 hint.ai_protocol = 0;
7807 hint.ai_flags = AI_DISABLE_IDN_ENCODING;
7808
7809 SetLastError(0xdeadbeef);
7810 result2 = NULL;
7811 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2);
7812 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7813 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7814 ok(result2 == NULL, "got %p\n", result2);
7815 }
7816
test_GetAddrInfoExW(void)7817 static void test_GetAddrInfoExW(void)
7818 {
7819 static const WCHAR empty[] = {0};
7820 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
7821 static const WCHAR winehq[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
7822 ADDRINFOEXW *result;
7823 OVERLAPPED overlapped;
7824 HANDLE event;
7825 int ret;
7826
7827 if (!pGetAddrInfoExW || !pGetAddrInfoExOverlappedResult)
7828 {
7829 win_skip("GetAddrInfoExW and/or GetAddrInfoExOverlappedResult not present\n");
7830 return;
7831 }
7832
7833 event = WSACreateEvent();
7834
7835 result = (ADDRINFOEXW *)0xdeadbeef;
7836 WSASetLastError(0xdeadbeef);
7837 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
7838 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7839 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7840 ok(result == NULL, "got %p\n", result);
7841
7842 result = NULL;
7843 WSASetLastError(0xdeadbeef);
7844 ret = pGetAddrInfoExW(empty, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
7845 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
7846 ok(result != NULL, "GetAddrInfoW failed\n");
7847 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7848 pFreeAddrInfoExW(result);
7849
7850 result = NULL;
7851 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL);
7852 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
7853 pFreeAddrInfoExW(result);
7854
7855 result = (void*)0xdeadbeef;
7856 memset(&overlapped, 0xcc, sizeof(overlapped));
7857 overlapped.hEvent = event;
7858 ResetEvent(event);
7859 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
7860 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
7861 ok(!result, "result != NULL\n");
7862 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
7863 ret = pGetAddrInfoExOverlappedResult(&overlapped);
7864 ok(!ret, "overlapped result is %d\n", ret);
7865 pFreeAddrInfoExW(result);
7866
7867 result = (void*)0xdeadbeef;
7868 memset(&overlapped, 0xcc, sizeof(overlapped));
7869 ResetEvent(event);
7870 overlapped.hEvent = event;
7871 WSASetLastError(0xdeadbeef);
7872 ret = pGetAddrInfoExW(winehq, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
7873 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError());
7874 ok(WSAGetLastError() == ERROR_IO_PENDING, "expected 11001, got %d\n", WSAGetLastError());
7875 ret = overlapped.Internal;
7876 ok(ret == WSAEINPROGRESS || ret == ERROR_SUCCESS, "overlapped.Internal = %u\n", ret);
7877 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n");
7878 ret = pGetAddrInfoExOverlappedResult(&overlapped);
7879 ok(!ret, "overlapped result is %d\n", ret);
7880 ok(overlapped.hEvent == event, "hEvent changed %p\n", overlapped.hEvent);
7881 ok(overlapped.Internal == ERROR_SUCCESS, "overlapped.Internal = %lx\n", overlapped.Internal);
7882 ok(overlapped.Pointer == &result, "overlapped.Pointer != &result\n");
7883 ok(result != NULL, "result == NULL\n");
7884 if (result != NULL)
7885 {
7886 ok(!result->ai_blob, "ai_blob != NULL\n");
7887 ok(!result->ai_bloblen, "ai_bloblen != 0\n");
7888 ok(!result->ai_provider, "ai_provider = %s\n", wine_dbgstr_guid(result->ai_provider));
7889 pFreeAddrInfoExW(result);
7890 }
7891
7892 result = (void*)0xdeadbeef;
7893 memset(&overlapped, 0xcc, sizeof(overlapped));
7894 ResetEvent(event);
7895 overlapped.hEvent = event;
7896 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL);
7897 todo_wine
7898 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7899 todo_wine
7900 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7901 ok(result == NULL, "got %p\n", result);
7902 ret = WaitForSingleObject(event, 0);
7903 todo_wine_if(ret != WAIT_TIMEOUT) /* Remove when abowe todo_wines are fixed */
7904 ok(ret == WAIT_TIMEOUT, "wait failed\n");
7905
7906 WSACloseEvent(event);
7907 }
7908
verify_ipv6_addrinfo(ADDRINFOA * result,const char * expectedIp)7909 static void verify_ipv6_addrinfo(ADDRINFOA *result, const char *expectedIp)
7910 {
7911 SOCKADDR_IN6 *sockaddr6;
7912 char ipBuffer[256];
7913 const char *ret;
7914
7915 ok(result->ai_family == AF_INET6, "ai_family == %d\n", result->ai_family);
7916 ok(result->ai_addrlen >= sizeof(struct sockaddr_in6), "ai_addrlen == %d\n", (int)result->ai_addrlen);
7917 ok(result->ai_addr != NULL, "ai_addr == NULL\n");
7918
7919 if (result->ai_addr != NULL)
7920 {
7921 sockaddr6 = (SOCKADDR_IN6 *)result->ai_addr;
7922 ok(sockaddr6->sin6_family == AF_INET6, "ai_addr->sin6_family == %d\n", sockaddr6->sin6_family);
7923 ok(sockaddr6->sin6_port == 0, "ai_addr->sin6_port == %d\n", sockaddr6->sin6_port);
7924
7925 ZeroMemory(ipBuffer, sizeof(ipBuffer));
7926 ret = p_inet_ntop(AF_INET6, &sockaddr6->sin6_addr, ipBuffer, sizeof(ipBuffer));
7927 ok(ret != NULL, "inet_ntop failed (%d)\n", WSAGetLastError());
7928 ok(strcmp(ipBuffer, expectedIp) == 0, "ai_addr->sin6_addr == '%s' (expected '%s')\n", ipBuffer, expectedIp);
7929 }
7930 }
7931
test_getaddrinfo(void)7932 static void test_getaddrinfo(void)
7933 {
7934 int i, ret;
7935 ADDRINFOA *result, *result2, *p, hint;
7936 SOCKADDR_IN *sockaddr;
7937 CHAR name[256], *ip;
7938 DWORD size = sizeof(name);
7939
7940 if (!pgetaddrinfo || !pfreeaddrinfo)
7941 {
7942 win_skip("getaddrinfo and/or freeaddrinfo not present\n");
7943 return;
7944 }
7945 memset(&hint, 0, sizeof(ADDRINFOA));
7946 GetComputerNameExA( ComputerNamePhysicalDnsHostname, name, &size );
7947
7948 result = (ADDRINFOA *)0xdeadbeef;
7949 WSASetLastError(0xdeadbeef);
7950 ret = pgetaddrinfo(NULL, NULL, NULL, &result);
7951 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
7952 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
7953 ok(result == NULL, "got %p\n", result);
7954
7955 result = NULL;
7956 WSASetLastError(0xdeadbeef);
7957 ret = pgetaddrinfo("", NULL, NULL, &result);
7958 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7959 ok(result != NULL, "getaddrinfo failed\n");
7960 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7961 pfreeaddrinfo(result);
7962
7963 result = NULL;
7964 ret = pgetaddrinfo(NULL, "0", NULL, &result);
7965 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7966 ok(result != NULL, "getaddrinfo failed\n");
7967
7968 result2 = NULL;
7969 ret = pgetaddrinfo(NULL, "", NULL, &result2);
7970 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7971 ok(result2 != NULL, "getaddrinfo failed\n");
7972 compare_addrinfo(result, result2);
7973 pfreeaddrinfo(result);
7974 pfreeaddrinfo(result2);
7975
7976 result = NULL;
7977 WSASetLastError(0xdeadbeef);
7978 ret = pgetaddrinfo("", "0", NULL, &result);
7979 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7980 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
7981 ok(result != NULL, "getaddrinfo failed\n");
7982
7983 result2 = NULL;
7984 ret = pgetaddrinfo("", "", NULL, &result2);
7985 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7986 ok(result2 != NULL, "getaddrinfo failed\n");
7987 compare_addrinfo(result, result2);
7988 pfreeaddrinfo(result);
7989 pfreeaddrinfo(result2);
7990
7991 result = NULL;
7992 ret = pgetaddrinfo("localhost", NULL, NULL, &result);
7993 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7994 pfreeaddrinfo(result);
7995
7996 result = NULL;
7997 ret = pgetaddrinfo("localhost", "", NULL, &result);
7998 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
7999 pfreeaddrinfo(result);
8000
8001 result = NULL;
8002 ret = pgetaddrinfo("localhost", "0", NULL, &result);
8003 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8004 pfreeaddrinfo(result);
8005
8006 result = NULL;
8007 ret = pgetaddrinfo("localhost", "80", NULL, &result);
8008 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8009 pfreeaddrinfo(result);
8010
8011 result = NULL;
8012 ret = pgetaddrinfo("localhost", NULL, &hint, &result);
8013 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8014 pfreeaddrinfo(result);
8015
8016 result = NULL;
8017 WSASetLastError(0xdeadbeef);
8018 ret = pgetaddrinfo("localhost", "80", &hint, &result);
8019 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8020 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
8021 pfreeaddrinfo(result);
8022
8023 hint.ai_flags = AI_NUMERICHOST;
8024 result = (void*)0xdeadbeef;
8025 ret = pgetaddrinfo("localhost", "80", &hint, &result);
8026 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", WSAGetLastError());
8027 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected WSAHOST_NOT_FOUND, got %d\n", WSAGetLastError());
8028 ok(!result, "result = %p\n", result);
8029 hint.ai_flags = 0;
8030
8031 /* try to get information from the computer name, result is the same
8032 * as if requesting with an empty host name. */
8033 ret = pgetaddrinfo(name, NULL, NULL, &result);
8034 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8035 #ifdef __REACTOS__
8036 ok(result != NULL, "getaddrinfo failed\n");
8037 #else
8038 ok(result != NULL, "GetAddrInfoW failed\n");
8039 #endif
8040
8041 ret = pgetaddrinfo("", NULL, NULL, &result2);
8042 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8043 #ifdef __REACTOS__
8044 ok(result2 != NULL, "getaddrinfo failed\n");
8045 #else
8046 ok(result != NULL, "GetAddrInfoW failed\n");
8047 #endif
8048 compare_addrinfo(result, result2);
8049 pfreeaddrinfo(result);
8050 pfreeaddrinfo(result2);
8051
8052 ret = pgetaddrinfo(name, "", NULL, &result);
8053 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8054 #ifdef __REACTOS__
8055 ok(result != NULL, "getaddrinfo failed\n");
8056 #else
8057 ok(result != NULL, "GetAddrInfoW failed\n");
8058 #endif
8059
8060 ret = pgetaddrinfo("", "", NULL, &result2);
8061 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
8062 #ifdef __REACTOS__
8063 ok(result2 != NULL, "getaddrinfo failed\n");
8064 #else
8065 ok(result != NULL, "GetAddrInfoW failed\n");
8066 #endif
8067 compare_addrinfo(result, result2);
8068 pfreeaddrinfo(result);
8069 pfreeaddrinfo(result2);
8070
8071 result = (ADDRINFOA *)0xdeadbeef;
8072 WSASetLastError(0xdeadbeef);
8073 ret = pgetaddrinfo("nxdomain.codeweavers.com", NULL, NULL, &result);
8074 if(ret == 0)
8075 {
8076 skip("nxdomain returned success. Broken ISP redirects?\n");
8077 return;
8078 }
8079 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
8080 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError());
8081 ok(result == NULL, "got %p\n", result);
8082
8083 /* Test IPv4 address conversion */
8084 result = NULL;
8085 ret = pgetaddrinfo("192.168.1.253", NULL, NULL, &result);
8086 ok(!ret, "getaddrinfo failed with %d\n", ret);
8087 ok(result->ai_family == AF_INET, "ai_family == %d\n", result->ai_family);
8088 ok(result->ai_addrlen >= sizeof(struct sockaddr_in), "ai_addrlen == %d\n", (int)result->ai_addrlen);
8089 ok(result->ai_addr != NULL, "ai_addr == NULL\n");
8090 sockaddr = (SOCKADDR_IN *)result->ai_addr;
8091 ok(sockaddr->sin_family == AF_INET, "ai_addr->sin_family == %d\n", sockaddr->sin_family);
8092 ok(sockaddr->sin_port == 0, "ai_addr->sin_port == %d\n", sockaddr->sin_port);
8093
8094 ip = inet_ntoa(sockaddr->sin_addr);
8095 ok(strcmp(ip, "192.168.1.253") == 0, "sockaddr->ai_addr == '%s'\n", ip);
8096 pfreeaddrinfo(result);
8097
8098 /* Test IPv4 address conversion with port */
8099 result = NULL;
8100 hint.ai_flags = AI_NUMERICHOST;
8101 ret = pgetaddrinfo("192.168.1.253:1024", NULL, &hint, &result);
8102 hint.ai_flags = 0;
8103 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo returned unexpected result: %d\n", ret);
8104 ok(result == NULL, "expected NULL, got %p\n", result);
8105
8106 /* Test IPv6 address conversion */
8107 result = NULL;
8108 SetLastError(0xdeadbeef);
8109 ret = pgetaddrinfo("2a00:2039:dead:beef:cafe::6666", NULL, NULL, &result);
8110
8111 if (result != NULL)
8112 {
8113 ok(!ret, "getaddrinfo failed with %d\n", ret);
8114 verify_ipv6_addrinfo(result, "2a00:2039:dead:beef:cafe::6666");
8115 pfreeaddrinfo(result);
8116
8117 /* Test IPv6 address conversion with brackets */
8118 result = NULL;
8119 ret = pgetaddrinfo("[beef::cafe]", NULL, NULL, &result);
8120 ok(!ret, "getaddrinfo failed with %d\n", ret);
8121 verify_ipv6_addrinfo(result, "beef::cafe");
8122 pfreeaddrinfo(result);
8123
8124 /* Test IPv6 address conversion with brackets and hints */
8125 memset(&hint, 0, sizeof(ADDRINFOA));
8126 hint.ai_flags = AI_NUMERICHOST;
8127 hint.ai_family = AF_INET6;
8128 result = NULL;
8129 ret = pgetaddrinfo("[beef::cafe]", NULL, &hint, &result);
8130 ok(!ret, "getaddrinfo failed with %d\n", ret);
8131 verify_ipv6_addrinfo(result, "beef::cafe");
8132 pfreeaddrinfo(result);
8133
8134 memset(&hint, 0, sizeof(ADDRINFOA));
8135 hint.ai_flags = AI_NUMERICHOST;
8136 hint.ai_family = AF_INET;
8137 result = NULL;
8138 ret = pgetaddrinfo("[beef::cafe]", NULL, &hint, &result);
8139 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
8140
8141 /* Test IPv6 address conversion with brackets and port */
8142 result = NULL;
8143 ret = pgetaddrinfo("[beef::cafe]:10239", NULL, NULL, &result);
8144 ok(!ret, "getaddrinfo failed with %d\n", ret);
8145 verify_ipv6_addrinfo(result, "beef::cafe");
8146 pfreeaddrinfo(result);
8147
8148 /* Test IPv6 address conversion with unmatched brackets */
8149 result = NULL;
8150 hint.ai_flags = AI_NUMERICHOST;
8151 ret = pgetaddrinfo("[beef::cafe", NULL, &hint, &result);
8152 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
8153
8154 ret = pgetaddrinfo("beef::cafe]", NULL, &hint, &result);
8155 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
8156 }
8157 else
8158 {
8159 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret);
8160 win_skip("getaddrinfo does not support IPV6\n");
8161 }
8162
8163 hint.ai_flags = 0;
8164
8165 for (i = 0;i < (sizeof(hinttests) / sizeof(hinttests[0]));i++)
8166 {
8167 hint.ai_family = hinttests[i].family;
8168 hint.ai_socktype = hinttests[i].socktype;
8169 hint.ai_protocol = hinttests[i].protocol;
8170
8171 result = NULL;
8172 SetLastError(0xdeadbeef);
8173 ret = pgetaddrinfo("localhost", NULL, &hint, &result);
8174 if(!ret)
8175 {
8176 if (hinttests[i].error)
8177 ok(0, "test %d: getaddrinfo succeeded unexpectedly\n", i);
8178 else
8179 {
8180 p = result;
8181 do
8182 {
8183 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
8184 if (hinttests[i].family == AF_UNSPEC)
8185 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6,
8186 "test %d: expected AF_INET or AF_INET6, got %d\n",
8187 i, p->ai_family);
8188 else
8189 ok(p->ai_family == hinttests[i].family,
8190 "test %d: expected family %d, got %d\n",
8191 i, hinttests[i].family, p->ai_family);
8192
8193 ok(p->ai_socktype == hinttests[i].socktype,
8194 "test %d: expected type %d, got %d\n",
8195 i, hinttests[i].socktype, p->ai_socktype);
8196 ok(p->ai_protocol == hinttests[i].protocol,
8197 "test %d: expected protocol %d, got %d\n",
8198 i, hinttests[i].protocol, p->ai_protocol);
8199 p = p->ai_next;
8200 }
8201 while (p);
8202 }
8203 pfreeaddrinfo(result);
8204 }
8205 else
8206 {
8207 DWORD err = WSAGetLastError();
8208 if (hinttests[i].error)
8209 ok(hinttests[i].error == err, "test %d: getaddrinfo failed with error %d, expected %d\n",
8210 i, err, hinttests[i].error);
8211 else
8212 ok(0, "test %d: getaddrinfo failed with %d (err %d)\n", i, ret, err);
8213 }
8214 }
8215 }
8216
test_ConnectEx(void)8217 static void test_ConnectEx(void)
8218 {
8219 SOCKET listener = INVALID_SOCKET;
8220 SOCKET acceptor = INVALID_SOCKET;
8221 SOCKET connector = INVALID_SOCKET;
8222 struct sockaddr_in address, conaddress;
8223 int addrlen;
8224 OVERLAPPED overlapped;
8225 LPFN_CONNECTEX pConnectEx;
8226 GUID connectExGuid = WSAID_CONNECTEX;
8227 DWORD bytesReturned;
8228 char buffer[1024];
8229 BOOL bret;
8230 DWORD dwret;
8231 int iret;
8232
8233 memset(&overlapped, 0, sizeof(overlapped));
8234
8235 listener = socket(AF_INET, SOCK_STREAM, 0);
8236 if (listener == INVALID_SOCKET) {
8237 skip("could not create listener socket, error %d\n", WSAGetLastError());
8238 goto end;
8239 }
8240
8241 connector = socket(AF_INET, SOCK_STREAM, 0);
8242 if (connector == INVALID_SOCKET) {
8243 skip("could not create connector socket, error %d\n", WSAGetLastError());
8244 goto end;
8245 }
8246
8247 memset(&address, 0, sizeof(address));
8248 address.sin_family = AF_INET;
8249 address.sin_addr.s_addr = inet_addr("127.0.0.1");
8250 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
8251 if (iret != 0) {
8252 skip("failed to bind, error %d\n", WSAGetLastError());
8253 goto end;
8254 }
8255
8256 addrlen = sizeof(address);
8257 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
8258 if (iret != 0) {
8259 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
8260 goto end;
8261 }
8262
8263 if (set_blocking(listener, TRUE)) {
8264 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
8265 goto end;
8266 }
8267
8268 bytesReturned = 0xdeadbeef;
8269 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
8270 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
8271 if (iret) {
8272 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret, WSAGetLastError());
8273 goto end;
8274 }
8275
8276 ok(bytesReturned == sizeof(pConnectEx), "expected sizeof(pConnectEx), got %u\n", bytesReturned);
8277
8278 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
8279 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
8280 "returned %d + errno %d\n", bret, WSAGetLastError());
8281
8282 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
8283 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
8284 "returned %d + errno %d\n", bret, WSAGetLastError());
8285 if (bret == TRUE || WSAGetLastError() != WSAEINVAL)
8286 {
8287 acceptor = accept(listener, NULL, NULL);
8288 if (acceptor != INVALID_SOCKET) {
8289 closesocket(acceptor);
8290 acceptor = INVALID_SOCKET;
8291 }
8292
8293 closesocket(connector);
8294 connector = socket(AF_INET, SOCK_STREAM, 0);
8295 if (connector == INVALID_SOCKET) {
8296 skip("could not create connector socket, error %d\n", WSAGetLastError());
8297 goto end;
8298 }
8299 }
8300
8301 /* ConnectEx needs a bound socket */
8302 memset(&conaddress, 0, sizeof(conaddress));
8303 conaddress.sin_family = AF_INET;
8304 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
8305 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
8306 if (iret != 0) {
8307 skip("failed to bind, error %d\n", WSAGetLastError());
8308 goto end;
8309 }
8310
8311 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
8312 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
8313 "returned %d + errno %d\n", bret, WSAGetLastError());
8314
8315 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8316 if (overlapped.hEvent == NULL) {
8317 skip("could not create event object, errno = %d\n", GetLastError());
8318 goto end;
8319 }
8320
8321 iret = listen(listener, 1);
8322 if (iret != 0) {
8323 skip("listening failed, errno = %d\n", WSAGetLastError());
8324 goto end;
8325 }
8326
8327 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
8328 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
8329 "returned %d + errno %d\n", bret, WSAGetLastError());
8330 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
8331 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
8332
8333 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
8334 ok(bret, "Connecting failed, error %d\n", GetLastError());
8335 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
8336
8337 closesocket(connector);
8338 connector = socket(AF_INET, SOCK_STREAM, 0);
8339 if (connector == INVALID_SOCKET) {
8340 skip("could not create connector socket, error %d\n", WSAGetLastError());
8341 goto end;
8342 }
8343 /* ConnectEx needs a bound socket */
8344 memset(&conaddress, 0, sizeof(conaddress));
8345 conaddress.sin_family = AF_INET;
8346 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
8347 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
8348 if (iret != 0) {
8349 skip("failed to bind, error %d\n", WSAGetLastError());
8350 goto end;
8351 }
8352
8353 acceptor = accept(listener, NULL, NULL);
8354 if (acceptor != INVALID_SOCKET) {
8355 closesocket(acceptor);
8356 }
8357
8358 buffer[0] = '1';
8359 buffer[1] = '2';
8360 buffer[2] = '3';
8361 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
8362 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
8363 "returned %d + errno %d\n", bret, WSAGetLastError());
8364 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
8365 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
8366
8367 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
8368 ok(bret, "Connecting failed, error %d\n", GetLastError());
8369 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
8370
8371 acceptor = accept(listener, NULL, NULL);
8372 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
8373
8374 bytesReturned = recv(acceptor, buffer, 3, 0);
8375 buffer[4] = 0;
8376 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
8377 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
8378 "Failed to get the right data, expected '123', got '%s'\n", buffer);
8379
8380 closesocket(connector);
8381 connector = socket(AF_INET, SOCK_STREAM, 0);
8382 if (connector == INVALID_SOCKET) {
8383 skip("could not create connector socket, error %d\n", WSAGetLastError());
8384 goto end;
8385 }
8386 /* ConnectEx needs a bound socket */
8387 memset(&conaddress, 0, sizeof(conaddress));
8388 conaddress.sin_family = AF_INET;
8389 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
8390 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
8391 if (iret != 0) {
8392 skip("failed to bind, error %d\n", WSAGetLastError());
8393 goto end;
8394 }
8395
8396 if (acceptor != INVALID_SOCKET) {
8397 closesocket(acceptor);
8398 acceptor = INVALID_SOCKET;
8399 }
8400
8401 /* Connect with error */
8402 closesocket(listener);
8403 listener = INVALID_SOCKET;
8404
8405 address.sin_port = htons(1);
8406
8407 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
8408 ok(bret == FALSE && GetLastError() == ERROR_IO_PENDING, "ConnectEx to bad destination failed: "
8409 "returned %d + errno %d\n", bret, GetLastError());
8410 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
8411 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
8412
8413 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
8414 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
8415 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
8416
8417 end:
8418 if (overlapped.hEvent)
8419 WSACloseEvent(overlapped.hEvent);
8420 if (listener != INVALID_SOCKET)
8421 closesocket(listener);
8422 if (acceptor != INVALID_SOCKET)
8423 closesocket(acceptor);
8424 if (connector != INVALID_SOCKET)
8425 closesocket(connector);
8426 }
8427
test_AcceptEx(void)8428 static void test_AcceptEx(void)
8429 {
8430 SOCKET listener = INVALID_SOCKET;
8431 SOCKET acceptor = INVALID_SOCKET;
8432 SOCKET connector = INVALID_SOCKET;
8433 SOCKET connector2 = INVALID_SOCKET;
8434 struct sockaddr_in bindAddress, peerAddress, *readBindAddress, *readRemoteAddress;
8435 int socklen, optlen;
8436 GUID acceptExGuid = WSAID_ACCEPTEX, getAcceptExGuid = WSAID_GETACCEPTEXSOCKADDRS;
8437 LPFN_ACCEPTEX pAcceptEx = NULL;
8438 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs = NULL;
8439 fd_set fds_accept, fds_send;
8440 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
8441 int got, conn1, i;
8442 DWORD bytesReturned, connect_time;
8443 char buffer[1024], ipbuffer[32];
8444 OVERLAPPED overlapped;
8445 int iret, localSize = sizeof(struct sockaddr_in), remoteSize = localSize;
8446 BOOL bret;
8447 DWORD dwret;
8448
8449 memset(&overlapped, 0, sizeof(overlapped));
8450
8451 listener = socket(AF_INET, SOCK_STREAM, 0);
8452 if (listener == INVALID_SOCKET) {
8453 skip("could not create listener socket, error %d\n", WSAGetLastError());
8454 goto end;
8455 }
8456
8457 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8458 if (acceptor == INVALID_SOCKET) {
8459 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8460 goto end;
8461 }
8462
8463 connector = socket(AF_INET, SOCK_STREAM, 0);
8464 if (connector == INVALID_SOCKET) {
8465 skip("could not create connector socket, error %d\n", WSAGetLastError());
8466 goto end;
8467 }
8468
8469 memset(&bindAddress, 0, sizeof(bindAddress));
8470 bindAddress.sin_family = AF_INET;
8471 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
8472 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8473 if (iret != 0) {
8474 skip("failed to bind, error %d\n", WSAGetLastError());
8475 goto end;
8476 }
8477
8478 socklen = sizeof(bindAddress);
8479 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
8480 if (iret != 0) {
8481 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
8482 goto end;
8483 }
8484
8485 if (set_blocking(listener, FALSE)) {
8486 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
8487 goto end;
8488 }
8489
8490 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
8491 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
8492 if (iret) {
8493 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
8494 goto end;
8495 }
8496
8497 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &getAcceptExGuid, sizeof(getAcceptExGuid),
8498 &pGetAcceptExSockaddrs, sizeof(pGetAcceptExSockaddrs), &bytesReturned, NULL, NULL);
8499 if (iret) {
8500 skip("WSAIoctl failed to get GetAcceptExSockaddrs with ret %d + errno %d\n", iret, WSAGetLastError());
8501 goto end;
8502 }
8503
8504 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8505 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8506 &bytesReturned, &overlapped);
8507 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
8508 "returned %d + errno %d\n", bret, WSAGetLastError());
8509
8510 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8511 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8512 &bytesReturned, &overlapped);
8513 todo_wine
8514 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
8515 "returned %d + errno %d\n", bret, WSAGetLastError());
8516
8517 iret = listen(listener, 5);
8518 if (iret != 0) {
8519 skip("listening failed, errno = %d\n", WSAGetLastError());
8520 goto end;
8521 }
8522
8523 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8524 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8525 &bytesReturned, &overlapped);
8526 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid accepting socket "
8527 "returned %d + errno %d\n", bret, WSAGetLastError());
8528
8529 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8530 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8531 &bytesReturned, &overlapped);
8532 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
8533 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
8534
8535 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
8536 &bytesReturned, &overlapped);
8537 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
8538 "AcceptEx on too small local address size returned %d + errno %d\n",
8539 bret, WSAGetLastError());
8540 bret = CancelIo((HANDLE) listener);
8541 ok(bret, "Failed to cancel pending accept socket\n");
8542
8543 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 15,
8544 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
8545 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx on too small local address "
8546 "size returned %d + errno %d\n",
8547 bret, WSAGetLastError());
8548 bret = CancelIo((HANDLE) listener);
8549 ok(bret, "Failed to cancel pending accept socket\n");
8550
8551 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
8552 &bytesReturned, &overlapped);
8553 ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
8554 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
8555
8556 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16,
8557 sizeof(struct sockaddr_in) + 15, &bytesReturned, &overlapped);
8558 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
8559 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
8560 bret = CancelIo((HANDLE) listener);
8561 ok(bret, "Failed to cancel pending accept socket\n");
8562
8563 bret = pAcceptEx(listener, acceptor, buffer, 0,
8564 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8565 &bytesReturned, NULL);
8566 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
8567 "returned %d + errno %d\n", bret, WSAGetLastError());
8568
8569 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, 0, &bytesReturned, NULL);
8570 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
8571 "returned %d + errno %d\n", bret, WSAGetLastError());
8572
8573 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
8574 if (overlapped.hEvent == NULL) {
8575 skip("could not create event object, errno = %d\n", GetLastError());
8576 goto end;
8577 }
8578
8579 bret = pAcceptEx(listener, acceptor, buffer, 0,
8580 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8581 &bytesReturned, &overlapped);
8582 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8583
8584 bret = pAcceptEx(listener, acceptor, buffer, 0,
8585 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8586 &bytesReturned, &overlapped);
8587 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL,
8588 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
8589 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
8590 /* We need to cancel this call, otherwise things fail */
8591 bret = CancelIo((HANDLE) listener);
8592 ok(bret, "Failed to cancel failed test. Bailing...\n");
8593 if (!bret) return;
8594 WaitForSingleObject(overlapped.hEvent, 0);
8595
8596 bret = pAcceptEx(listener, acceptor, buffer, 0,
8597 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8598 &bytesReturned, &overlapped);
8599 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8600 }
8601
8602 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8603 todo_wine ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
8604 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
8605 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
8606 /* We need to cancel this call, otherwise things fail */
8607 closesocket(acceptor);
8608 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8609 if (acceptor == INVALID_SOCKET) {
8610 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8611 goto end;
8612 }
8613
8614 bret = CancelIo((HANDLE) listener);
8615 ok(bret, "Failed to cancel failed test. Bailing...\n");
8616 if (!bret) return;
8617
8618 bret = pAcceptEx(listener, acceptor, buffer, 0,
8619 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8620 &bytesReturned, &overlapped);
8621 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8622 }
8623
8624 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8625 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
8626
8627 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
8628 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8629
8630 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8631 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
8632 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
8633
8634 closesocket(connector);
8635 connector = INVALID_SOCKET;
8636 closesocket(acceptor);
8637
8638 /* Test short reads */
8639
8640 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8641 if (acceptor == INVALID_SOCKET) {
8642 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8643 goto end;
8644 }
8645 connector = socket(AF_INET, SOCK_STREAM, 0);
8646 if (connector == INVALID_SOCKET) {
8647 skip("could not create connector socket, error %d\n", WSAGetLastError());
8648 goto end;
8649 }
8650 bret = pAcceptEx(listener, acceptor, buffer, 2,
8651 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8652 &bytesReturned, &overlapped);
8653 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8654
8655 connect_time = 0xdeadbeef;
8656 optlen = sizeof(connect_time);
8657 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
8658 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
8659 ok(connect_time == ~0u, "unexpected connect time %u\n", connect_time);
8660
8661 /* AcceptEx() still won't complete until we send data */
8662 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8663 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
8664
8665 connect_time = 0xdeadbeef;
8666 optlen = sizeof(connect_time);
8667 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
8668 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
8669 ok(connect_time < 0xdeadbeef, "unexpected connect time %u\n", connect_time);
8670
8671 dwret = WaitForSingleObject(overlapped.hEvent, 0);
8672 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
8673
8674 iret = getsockname( connector, (struct sockaddr *)&peerAddress, &remoteSize);
8675 ok( !iret, "getsockname failed.\n");
8676
8677 /* AcceptEx() could complete any time now */
8678 iret = send(connector, buffer, 1, 0);
8679 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
8680
8681 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
8682 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8683
8684 /* Check if the buffer from AcceptEx is decoded correctly */
8685 pGetAcceptExSockaddrs(buffer, 2, sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8686 (struct sockaddr **)&readBindAddress, &localSize,
8687 (struct sockaddr **)&readRemoteAddress, &remoteSize);
8688 strcpy( ipbuffer, inet_ntoa(readBindAddress->sin_addr));
8689 ok( readBindAddress->sin_addr.s_addr == bindAddress.sin_addr.s_addr,
8690 "Local socket address is different %s != %s\n",
8691 ipbuffer, inet_ntoa(bindAddress.sin_addr));
8692 ok( readBindAddress->sin_port == bindAddress.sin_port,
8693 "Local socket port is different: %d != %d\n",
8694 readBindAddress->sin_port, bindAddress.sin_port);
8695 strcpy( ipbuffer, inet_ntoa(readRemoteAddress->sin_addr));
8696 ok( readRemoteAddress->sin_addr.s_addr == peerAddress.sin_addr.s_addr,
8697 "Remote socket address is different %s != %s\n",
8698 ipbuffer, inet_ntoa(peerAddress.sin_addr));
8699 ok( readRemoteAddress->sin_port == peerAddress.sin_port,
8700 "Remote socket port is different: %d != %d\n",
8701 readRemoteAddress->sin_port, peerAddress.sin_port);
8702
8703 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8704 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
8705 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
8706
8707 closesocket(connector);
8708 connector = INVALID_SOCKET;
8709 closesocket(acceptor);
8710
8711 /* Test CF_DEFER & AcceptEx interaction */
8712
8713 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8714 if (acceptor == INVALID_SOCKET) {
8715 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8716 goto end;
8717 }
8718 connector = socket(AF_INET, SOCK_STREAM, 0);
8719 if (connector == INVALID_SOCKET) {
8720 skip("could not create connector socket, error %d\n", WSAGetLastError());
8721 goto end;
8722 }
8723 connector2 = socket(AF_INET, SOCK_STREAM, 0);
8724 if (connector == INVALID_SOCKET) {
8725 skip("could not create connector socket, error %d\n", WSAGetLastError());
8726 goto end;
8727 }
8728
8729 if (set_blocking(connector, FALSE)) {
8730 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
8731 goto end;
8732 }
8733
8734 if (set_blocking(connector2, FALSE)) {
8735 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
8736 goto end;
8737 }
8738
8739 /* Connect socket #1 */
8740 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8741 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
8742
8743 FD_ZERO ( &fds_accept );
8744 FD_ZERO ( &fds_send );
8745
8746 FD_SET ( listener, &fds_accept );
8747 FD_SET ( connector, &fds_send );
8748
8749 buffer[0] = '0';
8750 got = 0;
8751 conn1 = 0;
8752
8753 for (i = 0; i < 4000; ++i)
8754 {
8755 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
8756
8757 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
8758 "acceptex test(%d): could not select on socket, errno %d\n" );
8759
8760 /* check for incoming requests */
8761 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
8762 got++;
8763 if (got == 1) {
8764 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
8765 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
8766 bret = pAcceptEx(listener, acceptor, buffer, 0,
8767 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8768 &bytesReturned, &overlapped);
8769 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8770 }
8771 else if (got == 2) {
8772 /* this should be socket #2 */
8773 SOCKET tmp = accept(listener, NULL, NULL);
8774 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
8775 closesocket(tmp);
8776 }
8777 else {
8778 ok(FALSE, "Got more than 2 connections?\n");
8779 }
8780 }
8781 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
8782 /* Send data on second socket, and stop */
8783 send(connector2, "2", 1, 0);
8784 FD_CLR ( connector2, &fds_send );
8785
8786 break;
8787 }
8788 if ( FD_ISSET ( connector, &fds_opensend ) ) {
8789 /* Once #1 is connected, allow #2 to connect */
8790 conn1 = 1;
8791
8792 send(connector, "1", 1, 0);
8793 FD_CLR ( connector, &fds_send );
8794
8795 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8796 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
8797 FD_SET ( connector2, &fds_send );
8798 }
8799 }
8800
8801 ok (got == 2 || broken(got == 1) /* NT4 */,
8802 "Did not get both connections, got %d\n", got);
8803
8804 dwret = WaitForSingleObject(overlapped.hEvent, 0);
8805 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8806
8807 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8808 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
8809 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
8810
8811 set_blocking(acceptor, TRUE);
8812 iret = recv( acceptor, buffer, 2, 0);
8813 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
8814
8815 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
8816
8817 closesocket(connector);
8818 connector = INVALID_SOCKET;
8819 closesocket(acceptor);
8820
8821 /* clean up in case of failures */
8822 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
8823 closesocket(acceptor);
8824
8825 /* Disconnect during receive? */
8826
8827 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8828 if (acceptor == INVALID_SOCKET) {
8829 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8830 goto end;
8831 }
8832 connector = socket(AF_INET, SOCK_STREAM, 0);
8833 if (connector == INVALID_SOCKET) {
8834 skip("could not create connector socket, error %d\n", WSAGetLastError());
8835 goto end;
8836 }
8837 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8838 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8839 &bytesReturned, &overlapped);
8840 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8841
8842 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
8843 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
8844
8845 closesocket(connector);
8846 connector = INVALID_SOCKET;
8847
8848 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
8849 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8850
8851 bytesReturned = 123456;
8852 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8853 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
8854 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
8855
8856 closesocket(acceptor);
8857
8858 /* Test closing with pending requests */
8859
8860 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8861 if (acceptor == INVALID_SOCKET) {
8862 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8863 goto end;
8864 }
8865 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8866 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8867 &bytesReturned, &overlapped);
8868 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8869
8870 closesocket(acceptor);
8871
8872 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
8873 todo_wine ok(dwret == WAIT_OBJECT_0,
8874 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8875
8876 if (dwret != WAIT_TIMEOUT) {
8877 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8878 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
8879 }
8880 else {
8881 bret = CancelIo((HANDLE) listener);
8882 ok(bret, "Failed to cancel failed test. Bailing...\n");
8883 if (!bret) return;
8884 WaitForSingleObject(overlapped.hEvent, 0);
8885 }
8886
8887 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8888 if (acceptor == INVALID_SOCKET) {
8889 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8890 goto end;
8891 }
8892 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8893 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8894 &bytesReturned, &overlapped);
8895 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8896
8897 CancelIo((HANDLE) acceptor);
8898
8899 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
8900 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
8901
8902 closesocket(acceptor);
8903
8904 acceptor = socket(AF_INET, SOCK_STREAM, 0);
8905 if (acceptor == INVALID_SOCKET) {
8906 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8907 goto end;
8908 }
8909 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
8910 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
8911 &bytesReturned, &overlapped);
8912 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
8913
8914 closesocket(listener);
8915 listener = INVALID_SOCKET;
8916
8917 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
8918 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
8919
8920 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
8921 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
8922
8923 end:
8924 if (overlapped.hEvent)
8925 WSACloseEvent(overlapped.hEvent);
8926 if (listener != INVALID_SOCKET)
8927 closesocket(listener);
8928 if (acceptor != INVALID_SOCKET)
8929 closesocket(acceptor);
8930 if (connector != INVALID_SOCKET)
8931 closesocket(connector);
8932 if (connector2 != INVALID_SOCKET)
8933 closesocket(connector2);
8934 }
8935
test_DisconnectEx(void)8936 static void test_DisconnectEx(void)
8937 {
8938 SOCKET listener, acceptor, connector;
8939 LPFN_DISCONNECTEX pDisconnectEx;
8940 GUID disconnectExGuid = WSAID_DISCONNECTEX;
8941 struct sockaddr_in address;
8942 DWORD num_bytes, flags;
8943 OVERLAPPED overlapped;
8944 int addrlen, iret;
8945 BOOL bret;
8946
8947 connector = socket(AF_INET, SOCK_STREAM, 0);
8948 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
8949
8950 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectExGuid, sizeof(disconnectExGuid),
8951 &pDisconnectEx, sizeof(pDisconnectEx), &num_bytes, NULL, NULL);
8952 if (iret)
8953 {
8954 win_skip("WSAIoctl failed to get DisconnectEx, error %d\n", WSAGetLastError());
8955 closesocket(connector);
8956 return;
8957 }
8958
8959 listener = socket(AF_INET, SOCK_STREAM, 0);
8960 ok(listener != INVALID_SOCKET, "failed to create listener socket, error %d\n", WSAGetLastError());
8961
8962 memset(&address, 0, sizeof(address));
8963 address.sin_family = AF_INET;
8964 address.sin_addr.s_addr = inet_addr("127.0.0.1");
8965 iret = bind(listener, (struct sockaddr *)&address, sizeof(address));
8966 ok(iret == 0, "failed to bind, error %d\n", WSAGetLastError());
8967
8968 addrlen = sizeof(address);
8969 iret = getsockname(listener, (struct sockaddr *)&address, &addrlen);
8970 ok(iret == 0, "failed to lookup bind address, error %d\n", WSAGetLastError());
8971
8972 iret = listen(listener, 1);
8973 ok(iret == 0, "failed to listen, error %d\n", WSAGetLastError());
8974
8975 set_blocking(listener, TRUE);
8976
8977 memset(&overlapped, 0, sizeof(overlapped));
8978 bret = pDisconnectEx(INVALID_SOCKET, &overlapped, 0, 0);
8979 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
8980 ok(WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %d\n", WSAGetLastError());
8981
8982 memset(&overlapped, 0, sizeof(overlapped));
8983 bret = pDisconnectEx(connector, &overlapped, 0, 0);
8984 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
8985 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "expected WSAENOTCONN, got %d\n", WSAGetLastError());
8986
8987 iret = connect(connector, (struct sockaddr *)&address, addrlen);
8988 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
8989
8990 acceptor = accept(listener, NULL, NULL);
8991 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
8992
8993 memset(&overlapped, 0, sizeof(overlapped));
8994 overlapped.hEvent = WSACreateEvent();
8995 ok(overlapped.hEvent != WSA_INVALID_EVENT, "WSACreateEvent failed, error %d\n", WSAGetLastError());
8996 bret = pDisconnectEx(connector, &overlapped, 0, 0);
8997 if (bret)
8998 ok(overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", overlapped.Internal);
8999 else if (WSAGetLastError() == ERROR_IO_PENDING)
9000 bret = WSAGetOverlappedResult(connector, &overlapped, &num_bytes, TRUE, &flags);
9001 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
9002 WSACloseEvent(overlapped.hEvent);
9003
9004 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
9005 ok(iret != 0, "connect unexpectedly succeeded\n");
9006 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
9007
9008 closesocket(acceptor);
9009 closesocket(connector);
9010
9011 connector = socket(AF_INET, SOCK_STREAM, 0);
9012 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
9013
9014 iret = connect(connector, (struct sockaddr *)&address, addrlen);
9015 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
9016
9017 acceptor = accept(listener, NULL, NULL);
9018 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
9019
9020 bret = pDisconnectEx(connector, NULL, 0, 0);
9021 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
9022
9023 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
9024 ok(iret != 0, "connect unexpectedly succeeded\n");
9025 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
9026
9027 closesocket(acceptor);
9028 closesocket(connector);
9029 closesocket(listener);
9030 }
9031
9032 #define compare_file(h,s,o) compare_file2(h,s,o,__FILE__,__LINE__)
9033
compare_file2(HANDLE handle,SOCKET sock,int offset,const char * file,int line)9034 static void compare_file2(HANDLE handle, SOCKET sock, int offset, const char *file, int line)
9035 {
9036 char buf1[256], buf2[256];
9037 BOOL success;
9038 int i = 0;
9039
9040 SetFilePointer(handle, offset, NULL, FILE_BEGIN);
9041 while (1)
9042 {
9043 DWORD n1 = 0, n2 = 0;
9044
9045 success = ReadFile(handle, buf1, sizeof(buf1), &n1, NULL);
9046 ok_(file,line)(success, "Failed to read from file.\n");
9047 if (success && n1 == 0)
9048 break;
9049 else if(!success)
9050 return;
9051 n2 = recv(sock, buf2, n1, 0);
9052 ok_(file,line)(n1 == n2, "Block %d size mismatch (%d != %d)\n", i, n1, n2);
9053 ok_(file,line)(memcmp(buf1, buf2, n2) == 0, "Block %d failed\n", i);
9054 i++;
9055 }
9056 }
9057
test_TransmitFile(void)9058 static void test_TransmitFile(void)
9059 {
9060 DWORD num_bytes, err, file_size, total_sent;
9061 GUID transmitFileGuid = WSAID_TRANSMITFILE;
9062 LPFN_TRANSMITFILE pTransmitFile = NULL;
9063 HANDLE file = INVALID_HANDLE_VALUE;
9064 char header_msg[] = "hello world";
9065 char footer_msg[] = "goodbye!!!";
9066 char system_ini_path[MAX_PATH];
9067 struct sockaddr_in bindAddress;
9068 TRANSMIT_FILE_BUFFERS buffers;
9069 SOCKET client, server, dest;
9070 WSAOVERLAPPED ov;
9071 char buf[256];
9072 int iret, len;
9073 BOOL bret;
9074
9075 memset( &ov, 0, sizeof(ov) );
9076
9077 /* Setup sockets for testing TransmitFile */
9078 client = socket(AF_INET, SOCK_STREAM, 0);
9079 server = socket(AF_INET, SOCK_STREAM, 0);
9080 if (client == INVALID_SOCKET || server == INVALID_SOCKET)
9081 {
9082 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
9083 goto cleanup;
9084 }
9085 iret = WSAIoctl(client, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmitFileGuid, sizeof(transmitFileGuid),
9086 &pTransmitFile, sizeof(pTransmitFile), &num_bytes, NULL, NULL);
9087 if (iret)
9088 {
9089 skip("WSAIoctl failed to get TransmitFile with ret %d + errno %d\n", iret, WSAGetLastError());
9090 goto cleanup;
9091 }
9092 GetSystemWindowsDirectoryA(system_ini_path, MAX_PATH );
9093 strcat(system_ini_path, "\\system.ini");
9094 file = CreateFileA(system_ini_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0x0, NULL);
9095 if (file == INVALID_HANDLE_VALUE)
9096 {
9097 skip("Unable to open a file to transmit.\n");
9098 goto cleanup;
9099 }
9100 file_size = GetFileSize(file, NULL);
9101
9102 /* Test TransmitFile with an invalid socket */
9103 bret = pTransmitFile(INVALID_SOCKET, file, 0, 0, NULL, NULL, 0);
9104 err = WSAGetLastError();
9105 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9106 ok(err == WSAENOTSOCK, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTSOCK);
9107
9108 /* Test a bogus TransmitFile without a connected socket */
9109 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, TF_REUSE_SOCKET);
9110 err = WSAGetLastError();
9111 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9112 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
9113
9114 /* Setup a properly connected socket for transfers */
9115 memset(&bindAddress, 0, sizeof(bindAddress));
9116 bindAddress.sin_family = AF_INET;
9117 bindAddress.sin_port = htons(SERVERPORT+1);
9118 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
9119 iret = bind(server, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9120 if (iret != 0)
9121 {
9122 skip("failed to bind(), error %d\n", WSAGetLastError());
9123 goto cleanup;
9124 }
9125 iret = listen(server, 1);
9126 if (iret != 0)
9127 {
9128 skip("failed to listen(), error %d\n", WSAGetLastError());
9129 goto cleanup;
9130 }
9131 iret = connect(client, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9132 if (iret != 0)
9133 {
9134 skip("failed to connect(), error %d\n", WSAGetLastError());
9135 goto cleanup;
9136 }
9137 len = sizeof(bindAddress);
9138 dest = accept(server, (struct sockaddr*)&bindAddress, &len);
9139 if (dest == INVALID_SOCKET)
9140 {
9141 skip("failed to accept(), error %d\n", WSAGetLastError());
9142 goto cleanup;
9143 }
9144 if (set_blocking(dest, FALSE))
9145 {
9146 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
9147 goto cleanup;
9148 }
9149
9150 /* Test TransmitFile with no possible buffer */
9151 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
9152 ok(bret, "TransmitFile failed unexpectedly.\n");
9153 iret = recv(dest, buf, sizeof(buf), 0);
9154 ok(iret == -1, "Returned an unexpected buffer from TransmitFile (%d != -1).\n", iret);
9155
9156 /* Test TransmitFile with only buffer data */
9157 buffers.Head = &header_msg[0];
9158 buffers.HeadLength = sizeof(header_msg);
9159 buffers.Tail = &footer_msg[0];
9160 buffers.TailLength = sizeof(footer_msg);
9161 bret = pTransmitFile(client, NULL, 0, 0, NULL, &buffers, 0);
9162 ok(bret, "TransmitFile failed unexpectedly.\n");
9163 iret = recv(dest, buf, sizeof(buf), 0);
9164 ok(iret == sizeof(header_msg)+sizeof(footer_msg),
9165 "Returned an unexpected buffer from TransmitFile: %d\n", iret );
9166 ok(memcmp(&buf[0], &header_msg[0], sizeof(header_msg)) == 0,
9167 "TransmitFile header buffer did not match!\n");
9168 ok(memcmp(&buf[sizeof(header_msg)], &footer_msg[0], sizeof(footer_msg)) == 0,
9169 "TransmitFile footer buffer did not match!\n");
9170
9171 /* Test TransmitFile with only file data */
9172 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, 0);
9173 ok(bret, "TransmitFile failed unexpectedly.\n");
9174 compare_file(file, dest, 0);
9175
9176 /* Test TransmitFile with both file and buffer data */
9177 buffers.Head = &header_msg[0];
9178 buffers.HeadLength = sizeof(header_msg);
9179 buffers.Tail = &footer_msg[0];
9180 buffers.TailLength = sizeof(footer_msg);
9181 SetFilePointer(file, 0, NULL, FILE_BEGIN);
9182 bret = pTransmitFile(client, file, 0, 0, NULL, &buffers, 0);
9183 ok(bret, "TransmitFile failed unexpectedly.\n");
9184 iret = recv(dest, buf, sizeof(header_msg), 0);
9185 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
9186 "TransmitFile header buffer did not match!\n");
9187 compare_file(file, dest, 0);
9188 iret = recv(dest, buf, sizeof(footer_msg), 0);
9189 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
9190 "TransmitFile footer buffer did not match!\n");
9191
9192 /* Test overlapped TransmitFile */
9193 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
9194 if (ov.hEvent == INVALID_HANDLE_VALUE)
9195 {
9196 skip("Could not create event object, some tests will be skipped. errno = %d\n",
9197 GetLastError());
9198 goto cleanup;
9199 }
9200 SetFilePointer(file, 0, NULL, FILE_BEGIN);
9201 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
9202 err = WSAGetLastError();
9203 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9204 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n",
9205 err, ERROR_IO_PENDING);
9206 iret = WaitForSingleObject(ov.hEvent, 2000);
9207 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
9208 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
9209 ok(total_sent == file_size,
9210 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
9211 total_sent, file_size);
9212 compare_file(file, dest, 0);
9213
9214 /* Test overlapped TransmitFile w/ start offset */
9215 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
9216 if (ov.hEvent == INVALID_HANDLE_VALUE)
9217 {
9218 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
9219 goto cleanup;
9220 }
9221 SetFilePointer(file, 0, NULL, FILE_BEGIN);
9222 ov.Offset = 10;
9223 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
9224 err = WSAGetLastError();
9225 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9226 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
9227 iret = WaitForSingleObject(ov.hEvent, 2000);
9228 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
9229 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
9230 ok(total_sent == (file_size - ov.Offset),
9231 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
9232 total_sent, file_size - ov.Offset);
9233 compare_file(file, dest, ov.Offset);
9234
9235 /* Test overlapped TransmitFile w/ file and buffer data */
9236 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
9237 if (ov.hEvent == INVALID_HANDLE_VALUE)
9238 {
9239 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
9240 goto cleanup;
9241 }
9242 buffers.Head = &header_msg[0];
9243 buffers.HeadLength = sizeof(header_msg);
9244 buffers.Tail = &footer_msg[0];
9245 buffers.TailLength = sizeof(footer_msg);
9246 SetFilePointer(file, 0, NULL, FILE_BEGIN);
9247 ov.Offset = 0;
9248 bret = pTransmitFile(client, file, 0, 0, &ov, &buffers, 0);
9249 err = WSAGetLastError();
9250 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9251 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
9252 iret = WaitForSingleObject(ov.hEvent, 2000);
9253 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
9254 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
9255 ok(total_sent == (file_size + buffers.HeadLength + buffers.TailLength),
9256 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
9257 total_sent, file_size + buffers.HeadLength + buffers.TailLength);
9258 iret = recv(dest, buf, sizeof(header_msg), 0);
9259 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
9260 "TransmitFile header buffer did not match!\n");
9261 compare_file(file, dest, 0);
9262 iret = recv(dest, buf, sizeof(footer_msg), 0);
9263 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
9264 "TransmitFile footer buffer did not match!\n");
9265
9266 /* Test TransmitFile w/ TF_DISCONNECT */
9267 SetFilePointer(file, 0, NULL, FILE_BEGIN);
9268 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, TF_DISCONNECT);
9269 ok(bret, "TransmitFile failed unexpectedly.\n");
9270 compare_file(file, dest, 0);
9271 closesocket(client);
9272 ok(send(client, "test", 4, 0) == -1, "send() after TF_DISCONNECT succeeded unexpectedly.\n");
9273 err = WSAGetLastError();
9274 todo_wine ok(err == WSAENOTSOCK, "send() after TF_DISCONNECT triggered unexpected errno (%d != %d)\n",
9275 err, WSAENOTSOCK);
9276
9277 /* Test TransmitFile with a UDP datagram socket */
9278 client = socket(AF_INET, SOCK_DGRAM, 0);
9279 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
9280 err = WSAGetLastError();
9281 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
9282 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
9283
9284 cleanup:
9285 CloseHandle(file);
9286 CloseHandle(ov.hEvent);
9287 closesocket(client);
9288 closesocket(server);
9289 }
9290
test_getpeername(void)9291 static void test_getpeername(void)
9292 {
9293 SOCKET sock;
9294 struct sockaddr_in sa, sa_out;
9295 SOCKADDR_STORAGE ss;
9296 int sa_len;
9297 const char buf[] = "hello world";
9298 int ret;
9299
9300 /* Test the parameter validation order. */
9301 ret = getpeername(INVALID_SOCKET, NULL, NULL);
9302 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
9303 ok(WSAGetLastError() == WSAENOTSOCK,
9304 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
9305
9306 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
9307 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
9308 if (sock == INVALID_SOCKET)
9309 {
9310 skip("Socket creation failed with %d\n", WSAGetLastError());
9311 return;
9312 }
9313
9314 ret = getpeername(sock, NULL, NULL);
9315 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
9316 ok(WSAGetLastError() == WSAENOTCONN,
9317 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
9318
9319 memset(&sa, 0, sizeof(sa));
9320 sa.sin_family = AF_INET;
9321 sa.sin_port = htons(139);
9322 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
9323
9324 /* sendto does not change a socket's connection state. */
9325 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
9326 ok(ret != SOCKET_ERROR,
9327 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
9328
9329 ret = getpeername(sock, NULL, NULL);
9330 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
9331 ok(WSAGetLastError() == WSAENOTCONN,
9332 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
9333
9334 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
9335 ok(ret == 0,
9336 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
9337
9338 ret = getpeername(sock, NULL, NULL);
9339 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
9340 ok(WSAGetLastError() == WSAEFAULT,
9341 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
9342
9343 /* Test crashes on Wine. */
9344 if (0)
9345 {
9346 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
9347 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
9348 ok(WSAGetLastError() == WSAEFAULT,
9349 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
9350 }
9351
9352 ret = getpeername(sock, (struct sockaddr*)&sa_out, NULL);
9353 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
9354 ok(WSAGetLastError() == WSAEFAULT,
9355 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
9356
9357 sa_len = 0;
9358 ret = getpeername(sock, NULL, &sa_len);
9359 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
9360 ok(WSAGetLastError() == WSAEFAULT,
9361 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
9362 ok(!sa_len, "got %d\n", sa_len);
9363
9364 sa_len = 0;
9365 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
9366 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
9367 ok(WSAGetLastError() == WSAEFAULT,
9368 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
9369 ok(!sa_len, "got %d\n", sa_len);
9370
9371 sa_len = sizeof(ss);
9372 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
9373 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
9374 ok(!memcmp(&sa, &ss, sizeof(sa)),
9375 "Expected the returned structure to be identical to the connect structure\n");
9376 ok(sa_len == sizeof(sa), "got %d\n", sa_len);
9377
9378 closesocket(sock);
9379 }
9380
test_sioRoutingInterfaceQuery(void)9381 static void test_sioRoutingInterfaceQuery(void)
9382 {
9383 int ret;
9384 SOCKET sock;
9385 SOCKADDR_IN sin = { 0 }, sout = { 0 };
9386 DWORD bytesReturned;
9387
9388 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
9389 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
9390 if (sock == INVALID_SOCKET)
9391 {
9392 skip("Socket creation failed with %d\n", WSAGetLastError());
9393 return;
9394 }
9395 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
9396 NULL, NULL);
9397 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
9398 "expected WSAEFAULT, got %d\n", WSAGetLastError());
9399 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
9400 NULL, 0, NULL, NULL, NULL);
9401 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
9402 "expected WSAEFAULT, got %d\n", WSAGetLastError());
9403 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
9404 NULL, 0, &bytesReturned, NULL, NULL);
9405 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEAFNOSUPPORT,
9406 "expected WSAEAFNOSUPPORT, got %d\n", WSAGetLastError());
9407 sin.sin_family = AF_INET;
9408 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
9409 NULL, 0, &bytesReturned, NULL, NULL);
9410 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
9411 "expected WSAEINVAL, got %d\n", WSAGetLastError());
9412 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
9413 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
9414 NULL, 0, &bytesReturned, NULL, NULL);
9415 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
9416 "expected WSAEFAULT, got %d\n", WSAGetLastError());
9417 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
9418 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
9419 ok(!ret, "WSAIoctl failed: %d\n", WSAGetLastError());
9420 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n", sout.sin_family);
9421 /* We expect the source address to be INADDR_LOOPBACK as well, but
9422 * there's no guarantee that a route to the loopback address exists,
9423 * so rather than introduce spurious test failures we do not test the
9424 * source address.
9425 */
9426 closesocket(sock);
9427 }
9428
test_sioAddressListChange(void)9429 static void test_sioAddressListChange(void)
9430 {
9431 struct sockaddr_in bindAddress;
9432 struct in_addr net_address;
9433 WSAOVERLAPPED overlapped, *olp;
9434 struct hostent *h;
9435 DWORD num_bytes, error, tick;
9436 SOCKET sock, sock2, sock3;
9437 WSAEVENT event2, event3;
9438 HANDLE io_port;
9439 ULONG_PTR key;
9440 int acount;
9441 BOOL bret;
9442 int ret;
9443
9444 /* Use gethostbyname to find the list of local network interfaces */
9445 h = gethostbyname("");
9446 if (!h)
9447 {
9448 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, gethostbyname failed with %u\n",
9449 WSAGetLastError());
9450 return;
9451 }
9452 for (acount = 0; h->h_addr_list[acount]; acount++);
9453 if (acount == 0)
9454 {
9455 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, test requires a network card.\n");
9456 return;
9457 }
9458
9459 net_address.s_addr = *(ULONG *) h->h_addr_list[0];
9460
9461 sock = socket(AF_INET, 0, IPPROTO_TCP);
9462 ok(sock != INVALID_SOCKET, "socket() failed\n");
9463
9464 memset(&bindAddress, 0, sizeof(bindAddress));
9465 bindAddress.sin_family = AF_INET;
9466 bindAddress.sin_addr.s_addr = net_address.s_addr;
9467 SetLastError(0xdeadbeef);
9468 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9469 ok (!ret, "bind() failed with error %d\n", GetLastError());
9470 set_blocking(sock, FALSE);
9471
9472 memset(&overlapped, 0, sizeof(overlapped));
9473 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
9474 SetLastError(0xdeadbeef);
9475 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9476 error = GetLastError();
9477 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
9478 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
9479
9480 CloseHandle(overlapped.hEvent);
9481 closesocket(sock);
9482
9483 sock = socket(AF_INET, 0, IPPROTO_TCP);
9484 ok(sock != INVALID_SOCKET, "socket() failed\n");
9485
9486 SetLastError(0xdeadbeef);
9487 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9488 ok (!ret, "bind() failed with error %d\n", GetLastError());
9489 set_blocking(sock, TRUE);
9490
9491 memset(&overlapped, 0, sizeof(overlapped));
9492 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
9493 SetLastError(0xdeadbeef);
9494 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9495 error = GetLastError();
9496 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
9497 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
9498
9499 CloseHandle(overlapped.hEvent);
9500 closesocket(sock);
9501
9502 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9503 ok(sock != INVALID_SOCKET, "socket() failed\n");
9504
9505 SetLastError(0xdeadbeef);
9506 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9507 ok (!ret, "bind() failed with error %d\n", GetLastError());
9508 set_blocking(sock, FALSE);
9509
9510 memset(&overlapped, 0, sizeof(overlapped));
9511 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
9512 SetLastError(0xdeadbeef);
9513 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9514 error = GetLastError();
9515 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
9516 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
9517
9518 CloseHandle(overlapped.hEvent);
9519 closesocket(sock);
9520
9521 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9522 ok(sock != INVALID_SOCKET, "socket() failed\n");
9523
9524 SetLastError(0xdeadbeef);
9525 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9526 ok (!ret, "bind() failed with error %d\n", GetLastError());
9527 set_blocking(sock, TRUE);
9528
9529 memset(&overlapped, 0, sizeof(overlapped));
9530 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
9531 SetLastError(0xdeadbeef);
9532 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9533 error = GetLastError();
9534 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
9535 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
9536
9537 CloseHandle(overlapped.hEvent);
9538 closesocket(sock);
9539
9540 /* When the socket is overlapped non-blocking and the list change is requested without
9541 * an overlapped structure the error will be different. */
9542 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9543 ok(sock != INVALID_SOCKET, "socket() failed\n");
9544
9545 SetLastError(0xdeadbeef);
9546 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9547 ok (!ret, "bind() failed with error %d\n", GetLastError());
9548 set_blocking(sock, FALSE);
9549
9550 SetLastError(0xdeadbeef);
9551 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
9552 error = GetLastError();
9553 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
9554 ok (error == WSAEWOULDBLOCK, "expected 10035, got %d\n", error);
9555
9556 io_port = CreateIoCompletionPort( (HANDLE)sock, NULL, 0, 0 );
9557 ok (io_port != NULL, "failed to create completion port %u\n", GetLastError());
9558
9559 set_blocking(sock, FALSE);
9560 memset(&overlapped, 0, sizeof(overlapped));
9561 SetLastError(0xdeadbeef);
9562 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9563 error = GetLastError();
9564 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %u\n", error);
9565 ok (error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error);
9566
9567 olp = (WSAOVERLAPPED *)0xdeadbeef;
9568 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
9569 ok(!bret, "failed to get completion status %u\n", bret);
9570 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9571 ok(!olp, "Overlapped structure is at %p\n", olp);
9572
9573 closesocket(sock);
9574
9575 olp = (WSAOVERLAPPED *)0xdeadbeef;
9576 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
9577 ok(!bret, "failed to get completion status %u\n", bret);
9578 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %u\n", GetLastError());
9579 ok(olp == &overlapped, "Overlapped structure is at %p\n", olp);
9580
9581 CloseHandle(io_port);
9582
9583 /* Misuse of the API by using a blocking socket and not using an overlapped structure,
9584 * this leads to a hang forever. */
9585 if (0)
9586 {
9587 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9588
9589 SetLastError(0xdeadbeef);
9590 bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9591
9592 set_blocking(sock, TRUE);
9593 WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
9594 /* hang */
9595
9596 closesocket(sock);
9597 }
9598
9599 if (!winetest_interactive)
9600 {
9601 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, interactive tests must be enabled\n");
9602 return;
9603 }
9604
9605 /* Bind an overlapped socket to the first found network interface */
9606 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9607 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
9608 sock2 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9609 ok(sock2 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
9610 sock3 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
9611 ok(sock3 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
9612
9613 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9614 ok(!ret, "bind failed unexpectedly\n");
9615 ret = bind(sock2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9616 ok(!ret, "bind failed unexpectedly\n");
9617 ret = bind(sock3, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
9618 ok(!ret, "bind failed unexpectedly\n");
9619
9620 set_blocking(sock2, FALSE);
9621 set_blocking(sock3, FALSE);
9622
9623 /* Wait for address changes, request that the user connects/disconnects an interface */
9624 memset(&overlapped, 0, sizeof(overlapped));
9625 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
9626 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
9627 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
9628 ok(WSAGetLastError() == WSA_IO_PENDING, "Expected pending last error, got %d\n", WSAGetLastError());
9629
9630 ret = WSAIoctl(sock2, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
9631 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
9632 ok(WSAGetLastError() == WSAEWOULDBLOCK, "Expected would block last error, got %d\n", WSAGetLastError());
9633
9634 event2 = WSACreateEvent();
9635 event3 = WSACreateEvent();
9636 ret = WSAEventSelect (sock2, event2, FD_ADDRESS_LIST_CHANGE);
9637 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
9638 /* sock3 did not request SIO_ADDRESS_LIST_CHANGE but it is trying to wait anyway */
9639 ret = WSAEventSelect (sock3, event3, FD_ADDRESS_LIST_CHANGE);
9640 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
9641
9642 trace("Testing socket-based ipv4 address list change notification. Please connect/disconnect or"
9643 " change the ipv4 address of any of the local network interfaces (15 second timeout).\n");
9644 tick = GetTickCount();
9645 ret = WaitForSingleObject(overlapped.hEvent, 15000);
9646 ok(ret == WAIT_OBJECT_0, "failed to get overlapped event %u\n", ret);
9647
9648 ret = WaitForSingleObject(event2, 500);
9649 todo_wine
9650 ok(ret == WAIT_OBJECT_0, "failed to get change event %u\n", ret);
9651
9652 ret = WaitForSingleObject(event3, 500);
9653 ok(ret == WAIT_TIMEOUT, "unexpected change event\n");
9654
9655 trace("Spent %d ms waiting.\n", GetTickCount() - tick);
9656
9657 WSACloseEvent(event2);
9658 WSACloseEvent(event3);
9659
9660 closesocket(sock);
9661 closesocket(sock2);
9662 closesocket(sock3);
9663 }
9664
test_synchronous_WSAIoctl(void)9665 static void test_synchronous_WSAIoctl(void)
9666 {
9667 HANDLE previous_port, io_port;
9668 WSAOVERLAPPED overlapped, *olp;
9669 SOCKET socket;
9670 ULONG on;
9671 ULONG_PTR key;
9672 DWORD num_bytes;
9673 BOOL ret;
9674 int res;
9675
9676 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
9677 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
9678
9679 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
9680 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
9681
9682 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
9683 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
9684
9685 on = 1;
9686 memset( &overlapped, 0, sizeof(overlapped) );
9687 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
9688 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
9689
9690 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
9691 ok( ret, "failed to get completion status %u\n", GetLastError() );
9692
9693 CloseHandle( io_port );
9694 closesocket( socket );
9695 CloseHandle( previous_port );
9696 }
9697
9698 #define WM_ASYNCCOMPLETE (WM_USER + 100)
create_async_message_window(void)9699 static HWND create_async_message_window(void)
9700 {
9701 static const char class_name[] = "ws2_32 async message window class";
9702
9703 WNDCLASSEXA wndclass;
9704 HWND hWnd;
9705
9706 wndclass.cbSize = sizeof(wndclass);
9707 wndclass.style = CS_HREDRAW | CS_VREDRAW;
9708 wndclass.lpfnWndProc = DefWindowProcA;
9709 wndclass.cbClsExtra = 0;
9710 wndclass.cbWndExtra = 0;
9711 wndclass.hInstance = GetModuleHandleA(NULL);
9712 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
9713 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
9714 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
9715 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
9716 wndclass.lpszClassName = class_name;
9717 wndclass.lpszMenuName = NULL;
9718
9719 RegisterClassExA(&wndclass);
9720
9721 hWnd = CreateWindowA(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW,
9722 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
9723 if (!hWnd)
9724 {
9725 ok(0, "failed to create window: %u\n", GetLastError());
9726 return NULL;
9727 }
9728
9729 return hWnd;
9730 }
9731
wait_for_async_message(HWND hwnd,HANDLE handle)9732 static void wait_for_async_message(HWND hwnd, HANDLE handle)
9733 {
9734 BOOL ret;
9735 MSG msg;
9736
9737 while ((ret = GetMessageA(&msg, 0, 0, 0)) &&
9738 !(msg.hwnd == hwnd && msg.message == WM_ASYNCCOMPLETE))
9739 {
9740 TranslateMessage(&msg);
9741 DispatchMessageA(&msg);
9742 }
9743
9744 ok(ret, "did not expect WM_QUIT message\n");
9745 ok(msg.wParam == (WPARAM)handle, "expected wParam = %p, got %lx\n", handle, msg.wParam);
9746 }
9747
test_WSAAsyncGetServByPort(void)9748 static void test_WSAAsyncGetServByPort(void)
9749 {
9750 HWND hwnd = create_async_message_window();
9751 HANDLE ret;
9752 char buffer[MAXGETHOSTSTRUCT];
9753
9754 if (!hwnd)
9755 return;
9756
9757 /* FIXME: The asynchronous window messages should be tested. */
9758
9759 /* Parameters are not checked when initiating the asynchronous operation. */
9760 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0);
9761 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
9762
9763 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0);
9764 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
9765 wait_for_async_message(hwnd, ret);
9766
9767 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0);
9768 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
9769 wait_for_async_message(hwnd, ret);
9770
9771 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT);
9772 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
9773 wait_for_async_message(hwnd, ret);
9774
9775 DestroyWindow(hwnd);
9776 }
9777
test_WSAAsyncGetServByName(void)9778 static void test_WSAAsyncGetServByName(void)
9779 {
9780 HWND hwnd = create_async_message_window();
9781 HANDLE ret;
9782 char buffer[MAXGETHOSTSTRUCT];
9783
9784 if (!hwnd)
9785 return;
9786
9787 /* FIXME: The asynchronous window messages should be tested. */
9788
9789 /* Parameters are not checked when initiating the asynchronous operation. */
9790 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", NULL, NULL, 0);
9791 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
9792 wait_for_async_message(hwnd, ret);
9793
9794 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", "", buffer, MAXGETHOSTSTRUCT);
9795 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
9796 wait_for_async_message(hwnd, ret);
9797
9798 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", NULL, NULL, 0);
9799 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
9800 wait_for_async_message(hwnd, ret);
9801
9802 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", "tcp", buffer, MAXGETHOSTSTRUCT);
9803 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
9804 wait_for_async_message(hwnd, ret);
9805
9806 DestroyWindow(hwnd);
9807 }
9808
9809 /*
9810 * Provide consistent initialization for the AcceptEx IOCP tests.
9811 */
setup_iocp_src(struct sockaddr_in * bindAddress)9812 static SOCKET setup_iocp_src(struct sockaddr_in *bindAddress)
9813 {
9814 SOCKET src, ret = INVALID_SOCKET;
9815 int iret, socklen;
9816
9817 src = socket(AF_INET, SOCK_STREAM, 0);
9818 if (src == INVALID_SOCKET)
9819 {
9820 skip("could not create listener socket, error %d\n", WSAGetLastError());
9821 goto end;
9822 }
9823
9824 memset(bindAddress, 0, sizeof(*bindAddress));
9825 bindAddress->sin_family = AF_INET;
9826 bindAddress->sin_addr.s_addr = inet_addr("127.0.0.1");
9827 iret = bind(src, (struct sockaddr*)bindAddress, sizeof(*bindAddress));
9828 if (iret != 0)
9829 {
9830 skip("failed to bind, error %d\n", WSAGetLastError());
9831 goto end;
9832 }
9833
9834 socklen = sizeof(*bindAddress);
9835 iret = getsockname(src, (struct sockaddr*)bindAddress, &socklen);
9836 if (iret != 0) {
9837 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
9838 goto end;
9839 }
9840
9841 if (set_blocking(src, FALSE))
9842 {
9843 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
9844 goto end;
9845 }
9846
9847 iret = listen(src, 5);
9848 if (iret != 0)
9849 {
9850 skip("listening failed, errno = %d\n", WSAGetLastError());
9851 goto end;
9852 }
9853
9854 ret = src;
9855 end:
9856 if (src != ret && ret == INVALID_SOCKET)
9857 closesocket(src);
9858 return ret;
9859 }
9860
test_completion_port(void)9861 static void test_completion_port(void)
9862 {
9863 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION io_info;
9864 HANDLE previous_port, io_port;
9865 WSAOVERLAPPED ov, *olp;
9866 SOCKET src, dest, dup, connector = INVALID_SOCKET;
9867 WSAPROTOCOL_INFOA info;
9868 IO_STATUS_BLOCK io;
9869 NTSTATUS status;
9870 char buf[1024];
9871 WSABUF bufs;
9872 DWORD num_bytes, flags;
9873 struct linger ling;
9874 int iret;
9875 BOOL bret;
9876 ULONG_PTR key;
9877 struct sockaddr_in bindAddress;
9878 GUID acceptExGuid = WSAID_ACCEPTEX;
9879 LPFN_ACCEPTEX pAcceptEx = NULL;
9880 fd_set fds_recv;
9881
9882 memset(buf, 0, sizeof(buf));
9883 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
9884 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError());
9885
9886 memset(&ov, 0, sizeof(ov));
9887
9888 tcp_socketpair(&src, &dest);
9889 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
9890 {
9891 skip("failed to create sockets\n");
9892 goto end;
9893 }
9894
9895 bufs.len = sizeof(buf);
9896 bufs.buf = buf;
9897 flags = 0;
9898
9899 ling.l_onoff = 1;
9900 ling.l_linger = 0;
9901 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
9902 ok(!iret, "Failed to set linger %d\n", GetLastError());
9903
9904 io_port = CreateIoCompletionPort( (HANDLE)dest, previous_port, 125, 0 );
9905 ok(io_port != NULL, "Failed to create completion port %u\n", GetLastError());
9906
9907 SetLastError(0xdeadbeef);
9908
9909 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
9910 ok(iret == SOCKET_ERROR, "WSARecv returned %d\n", iret);
9911 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
9912
9913 Sleep(100);
9914
9915 closesocket(src);
9916 src = INVALID_SOCKET;
9917
9918 SetLastError(0xdeadbeef);
9919 key = 0xdeadbeef;
9920 num_bytes = 0xdeadbeef;
9921 olp = (WSAOVERLAPPED *)0xdeadbeef;
9922
9923 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9924 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret);
9925 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "Last error was %d\n", GetLastError());
9926 ok(key == 125, "Key is %lu\n", key);
9927 ok(num_bytes == 0, "Number of bytes received is %u\n", num_bytes);
9928 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
9929
9930 SetLastError(0xdeadbeef);
9931 key = 0xdeadbeef;
9932 num_bytes = 0xdeadbeef;
9933 olp = (WSAOVERLAPPED *)0xdeadbeef;
9934
9935 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
9936 ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret );
9937 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9938 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9939 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9940 ok(!olp, "Overlapped structure is at %p\n", olp);
9941
9942 if (dest != INVALID_SOCKET)
9943 closesocket(dest);
9944
9945 memset(&ov, 0, sizeof(ov));
9946
9947 tcp_socketpair(&src, &dest);
9948 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
9949 {
9950 skip("failed to create sockets\n");
9951 goto end;
9952 }
9953
9954 bufs.len = sizeof(buf);
9955 bufs.buf = buf;
9956 flags = 0;
9957
9958 ling.l_onoff = 1;
9959 ling.l_linger = 0;
9960 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
9961 ok(!iret, "Failed to set linger %d\n", GetLastError());
9962
9963 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
9964 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
9965
9966 set_blocking(dest, FALSE);
9967
9968 closesocket(src);
9969 src = INVALID_SOCKET;
9970
9971 Sleep(100);
9972
9973 num_bytes = 0xdeadbeef;
9974 SetLastError(0xdeadbeef);
9975
9976 iret = WSASend(dest, &bufs, 1, &num_bytes, 0, &ov, NULL);
9977 ok(iret == SOCKET_ERROR, "WSASend failed - %d\n", iret);
9978 ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
9979 ok(num_bytes == 0xdeadbeef, "Managed to send %d\n", num_bytes);
9980
9981 SetLastError(0xdeadbeef);
9982 key = 0xdeadbeef;
9983 num_bytes = 0xdeadbeef;
9984 olp = (WSAOVERLAPPED *)0xdeadbeef;
9985
9986 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
9987 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
9988 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
9989 ok(key == 0xdeadbeef, "Key is %lu\n", key);
9990 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
9991 ok(!olp, "Overlapped structure is at %p\n", olp);
9992
9993 if (dest != INVALID_SOCKET)
9994 closesocket(dest);
9995
9996 /* Test IOCP response on successful immediate read. */
9997 tcp_socketpair(&src, &dest);
9998 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
9999 {
10000 skip("failed to create sockets\n");
10001 goto end;
10002 }
10003
10004 bufs.len = sizeof(buf);
10005 bufs.buf = buf;
10006 flags = 0;
10007 SetLastError(0xdeadbeef);
10008
10009 iret = WSASend(src, &bufs, 1, &num_bytes, 0, &ov, NULL);
10010 ok(!iret, "WSASend failed - %d, last error %u\n", iret, GetLastError());
10011 ok(num_bytes == sizeof(buf), "Managed to send %d\n", num_bytes);
10012
10013 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
10014 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10015 set_blocking(dest, FALSE);
10016
10017 FD_ZERO(&fds_recv);
10018 FD_SET(dest, &fds_recv);
10019 select(dest + 1, &fds_recv, NULL, NULL, NULL);
10020
10021 num_bytes = 0xdeadbeef;
10022 flags = 0;
10023
10024 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
10025 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
10026 ok(num_bytes == sizeof(buf), "Managed to read %d\n", num_bytes);
10027
10028 SetLastError(0xdeadbeef);
10029 key = 0xdeadbeef;
10030 num_bytes = 0xdeadbeef;
10031 olp = (WSAOVERLAPPED *)0xdeadbeef;
10032
10033 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10034 ok(bret == TRUE, "failed to get completion status %u\n", bret);
10035 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
10036 ok(key == 125, "Key is %lu\n", key);
10037 ok(num_bytes == sizeof(buf), "Number of bytes transferred is %u\n", num_bytes);
10038 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10039
10040 /* Test IOCP response on graceful shutdown. */
10041 closesocket(src);
10042
10043 FD_ZERO(&fds_recv);
10044 FD_SET(dest, &fds_recv);
10045 select(dest + 1, &fds_recv, NULL, NULL, NULL);
10046
10047 num_bytes = 0xdeadbeef;
10048 flags = 0;
10049 memset(&ov, 0, sizeof(ov));
10050
10051 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
10052 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
10053 ok(!num_bytes, "Managed to read %d\n", num_bytes);
10054
10055 SetLastError(0xdeadbeef);
10056 key = 0xdeadbeef;
10057 num_bytes = 0xdeadbeef;
10058 olp = (WSAOVERLAPPED *)0xdeadbeef;
10059
10060 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10061 ok(bret == TRUE, "failed to get completion status %u\n", bret);
10062 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
10063 ok(key == 125, "Key is %lu\n", key);
10064 ok(!num_bytes, "Number of bytes transferred is %u\n", num_bytes);
10065 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10066
10067 closesocket(src);
10068 src = INVALID_SOCKET;
10069 closesocket(dest);
10070 dest = INVALID_SOCKET;
10071
10072 /* Test IOCP response on hard shutdown. This was the condition that triggered
10073 * a crash in an actual app (bug 38980). */
10074 tcp_socketpair(&src, &dest);
10075 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
10076 {
10077 skip("failed to create sockets\n");
10078 goto end;
10079 }
10080
10081 bufs.len = sizeof(buf);
10082 bufs.buf = buf;
10083 flags = 0;
10084 memset(&ov, 0, sizeof(ov));
10085
10086 ling.l_onoff = 1;
10087 ling.l_linger = 0;
10088 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
10089 ok(!iret, "Failed to set linger %d\n", GetLastError());
10090
10091 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
10092 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10093 set_blocking(dest, FALSE);
10094
10095 closesocket(src);
10096 src = INVALID_SOCKET;
10097
10098 FD_ZERO(&fds_recv);
10099 FD_SET(dest, &fds_recv);
10100 select(dest + 1, &fds_recv, NULL, NULL, NULL);
10101
10102 num_bytes = 0xdeadbeef;
10103 SetLastError(0xdeadbeef);
10104
10105 /* Somehow a hard shutdown doesn't work on my Linux box. It seems SO_LINGER is ignored. */
10106 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
10107 todo_wine ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
10108 todo_wine ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
10109 todo_wine ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
10110
10111 SetLastError(0xdeadbeef);
10112 key = 0xdeadbeef;
10113 num_bytes = 0xdeadbeef;
10114 olp = (WSAOVERLAPPED *)0xdeadbeef;
10115
10116 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10117 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
10118 todo_wine ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10119 todo_wine ok(key == 0xdeadbeef, "Key is %lu\n", key);
10120 todo_wine ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10121 todo_wine ok(!olp, "Overlapped structure is at %p\n", olp);
10122
10123 closesocket(dest);
10124
10125 /* Test reading from a non-connected socket, mostly because the above test is marked todo. */
10126 dest = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
10127 ok(dest != INVALID_SOCKET, "socket() failed\n");
10128
10129 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
10130 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10131 set_blocking(dest, FALSE);
10132
10133 num_bytes = 0xdeadbeef;
10134 SetLastError(0xdeadbeef);
10135 memset(&ov, 0, sizeof(ov));
10136
10137 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
10138 ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
10139 ok(GetLastError() == WSAENOTCONN, "Last error was %d\n", GetLastError());
10140 ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
10141
10142 SetLastError(0xdeadbeef);
10143 key = 0xdeadbeef;
10144 num_bytes = 0xdeadbeef;
10145 olp = (WSAOVERLAPPED *)0xdeadbeef;
10146
10147 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10148 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
10149 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10150 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10151 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10152 ok(!olp, "Overlapped structure is at %p\n", olp);
10153
10154 num_bytes = 0xdeadbeef;
10155 closesocket(dest);
10156
10157 dest = socket(AF_INET, SOCK_STREAM, 0);
10158 if (dest == INVALID_SOCKET)
10159 {
10160 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
10161 goto end;
10162 }
10163
10164 iret = WSAIoctl(dest, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
10165 &pAcceptEx, sizeof(pAcceptEx), &num_bytes, NULL, NULL);
10166 if (iret)
10167 {
10168 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
10169 goto end;
10170 }
10171
10172 /* Test IOCP response on socket close (IOCP created after AcceptEx) */
10173
10174 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10175 goto end;
10176
10177 SetLastError(0xdeadbeef);
10178
10179 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10180 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10181 &num_bytes, &ov);
10182 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10183 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10184
10185 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10186 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10187
10188 closesocket(src);
10189 src = INVALID_SOCKET;
10190
10191 SetLastError(0xdeadbeef);
10192 key = 0xdeadbeef;
10193 num_bytes = 0xdeadbeef;
10194 olp = (WSAOVERLAPPED *)0xdeadbeef;
10195
10196 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10197 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10198 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
10199 ok(key == 125, "Key is %lu\n", key);
10200 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10201 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10202 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
10203
10204 SetLastError(0xdeadbeef);
10205 key = 0xdeadbeef;
10206 num_bytes = 0xdeadbeef;
10207 olp = (WSAOVERLAPPED *)0xdeadbeef;
10208 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10209 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10210 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10211 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10212 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10213 ok(!olp, "Overlapped structure is at %p\n", olp);
10214
10215 /* Test IOCP response on socket close (IOCP created before AcceptEx) */
10216
10217 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10218 goto end;
10219
10220 SetLastError(0xdeadbeef);
10221
10222 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10223 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10224
10225 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10226 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10227 &num_bytes, &ov);
10228 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10229 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10230
10231 closesocket(src);
10232 src = INVALID_SOCKET;
10233
10234 SetLastError(0xdeadbeef);
10235 key = 0xdeadbeef;
10236 num_bytes = 0xdeadbeef;
10237 olp = (WSAOVERLAPPED *)0xdeadbeef;
10238
10239 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10240 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10241 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
10242 ok(key == 125, "Key is %lu\n", key);
10243 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10244 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10245 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
10246
10247 SetLastError(0xdeadbeef);
10248 key = 0xdeadbeef;
10249 num_bytes = 0xdeadbeef;
10250 olp = (WSAOVERLAPPED *)0xdeadbeef;
10251 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10252 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10253 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10254 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10255 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10256 ok(!olp, "Overlapped structure is at %p\n", olp);
10257
10258 /* Test IOCP with duplicated handle */
10259
10260 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10261 goto end;
10262
10263 SetLastError(0xdeadbeef);
10264
10265 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10266 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10267
10268 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
10269 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
10270 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
10271
10272 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10273 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10274 &num_bytes, &ov);
10275 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10276 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10277
10278 SetLastError(0xdeadbeef);
10279 key = 0xdeadbeef;
10280 num_bytes = 0xdeadbeef;
10281 olp = (WSAOVERLAPPED *)0xdeadbeef;
10282 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10283 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10284 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10285 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10286 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10287 ok(!olp, "Overlapped structure is at %p\n", olp);
10288
10289 closesocket(src);
10290 src = INVALID_SOCKET;
10291 closesocket(dup);
10292 dup = INVALID_SOCKET;
10293
10294 SetLastError(0xdeadbeef);
10295 key = 0xdeadbeef;
10296 num_bytes = 0xdeadbeef;
10297 olp = (WSAOVERLAPPED *)0xdeadbeef;
10298 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10299 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10300 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
10301 ok(key == 125, "Key is %lu\n", key);
10302 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10303 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10304 ok(olp && olp->Internal == (ULONG)STATUS_CANCELLED, "Internal status is %lx\n", olp ? olp->Internal : 0);
10305
10306 SetLastError(0xdeadbeef);
10307 key = 0xdeadbeef;
10308 num_bytes = 0xdeadbeef;
10309 olp = (WSAOVERLAPPED *)0xdeadbeef;
10310 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10311 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10312 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10313 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10314 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10315 ok(!olp, "Overlapped structure is at %p\n", olp);
10316
10317 /* Test IOCP with duplicated handle (closing duplicated handle) */
10318
10319 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10320 goto end;
10321
10322 SetLastError(0xdeadbeef);
10323
10324 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10325 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10326
10327 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
10328 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
10329 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
10330
10331 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10332 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10333 &num_bytes, &ov);
10334 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10335 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10336
10337 closesocket(dup);
10338 dup = INVALID_SOCKET;
10339
10340 SetLastError(0xdeadbeef);
10341 key = 0xdeadbeef;
10342 num_bytes = 0xdeadbeef;
10343 olp = (WSAOVERLAPPED *)0xdeadbeef;
10344 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10345 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10346 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10347 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10348 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10349 ok(!olp, "Overlapped structure is at %p\n", olp);
10350
10351 SetLastError(0xdeadbeef);
10352 key = 0xdeadbeef;
10353 num_bytes = 0xdeadbeef;
10354 olp = (WSAOVERLAPPED *)0xdeadbeef;
10355 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10356 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10357 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10358 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10359 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10360 ok(!olp, "Overlapped structure is at %p\n", olp);
10361
10362 closesocket(src);
10363 src = INVALID_SOCKET;
10364
10365 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10366 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10367 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
10368 ok(key == 125, "Key is %lu\n", key);
10369 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10370 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10371 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
10372
10373 SetLastError(0xdeadbeef);
10374 key = 0xdeadbeef;
10375 num_bytes = 0xdeadbeef;
10376 olp = (WSAOVERLAPPED *)0xdeadbeef;
10377 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10378 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10379 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10380 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10381 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10382 ok(!olp, "Overlapped structure is at %p\n", olp);
10383
10384 /* Test IOCP with duplicated handle (closing original handle) */
10385
10386 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10387 goto end;
10388
10389 SetLastError(0xdeadbeef);
10390
10391 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10392 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10393
10394 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
10395 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
10396 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
10397
10398 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10399 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10400 &num_bytes, &ov);
10401 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10402 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10403
10404 closesocket(src);
10405 src = INVALID_SOCKET;
10406
10407 SetLastError(0xdeadbeef);
10408 key = 0xdeadbeef;
10409 num_bytes = 0xdeadbeef;
10410 olp = (WSAOVERLAPPED *)0xdeadbeef;
10411 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10412 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10413 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10414 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10415 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10416 ok(!olp, "Overlapped structure is at %p\n", olp);
10417
10418 closesocket(dup);
10419 dup = INVALID_SOCKET;
10420
10421 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10422 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10423 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
10424 ok(key == 125, "Key is %lu\n", key);
10425 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10426 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10427 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
10428
10429 SetLastError(0xdeadbeef);
10430 key = 0xdeadbeef;
10431 num_bytes = 0xdeadbeef;
10432 olp = (WSAOVERLAPPED *)0xdeadbeef;
10433 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10434 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10435 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10436 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10437 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10438 ok(!olp, "Overlapped structure is at %p\n", olp);
10439
10440 /* Test IOCP without AcceptEx */
10441
10442 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10443 goto end;
10444
10445 SetLastError(0xdeadbeef);
10446
10447 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10448 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10449
10450 closesocket(src);
10451 src = INVALID_SOCKET;
10452
10453 SetLastError(0xdeadbeef);
10454 key = 0xdeadbeef;
10455 num_bytes = 0xdeadbeef;
10456 olp = (WSAOVERLAPPED *)0xdeadbeef;
10457 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10458 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10459 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10460 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10461 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10462 ok(!olp, "Overlapped structure is at %p\n", olp);
10463
10464 /* */
10465
10466 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10467 goto end;
10468
10469 connector = socket(AF_INET, SOCK_STREAM, 0);
10470 if (connector == INVALID_SOCKET) {
10471 skip("could not create connector socket, error %d\n", WSAGetLastError());
10472 goto end;
10473 }
10474
10475 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10476 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10477
10478 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
10479 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10480
10481 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10482 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10483 &num_bytes, &ov);
10484 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10485 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10486
10487 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
10488 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
10489
10490 closesocket(connector);
10491 connector = INVALID_SOCKET;
10492
10493 SetLastError(0xdeadbeef);
10494 key = 0xdeadbeef;
10495 num_bytes = 0xdeadbeef;
10496 olp = (WSAOVERLAPPED *)0xdeadbeef;
10497
10498 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10499 ok(bret == TRUE, "failed to get completion status %u\n", bret);
10500 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
10501 ok(key == 125, "Key is %lu\n", key);
10502 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10503 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10504 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
10505
10506 SetLastError(0xdeadbeef);
10507 key = 0xdeadbeef;
10508 num_bytes = 0xdeadbeef;
10509 olp = (WSAOVERLAPPED *)0xdeadbeef;
10510 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10511 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10512 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10513 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10514 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10515 ok(!olp, "Overlapped structure is at %p\n", olp);
10516
10517 if (dest != INVALID_SOCKET)
10518 closesocket(dest);
10519 if (src != INVALID_SOCKET)
10520 closesocket(dest);
10521
10522 /* */
10523
10524 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10525 goto end;
10526
10527 dest = socket(AF_INET, SOCK_STREAM, 0);
10528 if (dest == INVALID_SOCKET)
10529 {
10530 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
10531 goto end;
10532 }
10533
10534 connector = socket(AF_INET, SOCK_STREAM, 0);
10535 if (connector == INVALID_SOCKET) {
10536 skip("could not create connector socket, error %d\n", WSAGetLastError());
10537 goto end;
10538 }
10539
10540 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10541 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10542
10543 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
10544 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10545
10546 io_info.Flags = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS;
10547 status = pNtSetInformationFile((HANDLE)src, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation);
10548 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */,
10549 "expected STATUS_SUCCESS, got %08x\n", status);
10550
10551 io_info.Flags = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS;
10552 status = pNtSetInformationFile((HANDLE)dest, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation);
10553 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */,
10554 "expected STATUS_SUCCESS, got %08x\n", status);
10555
10556 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10557 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10558 &num_bytes, &ov);
10559 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10560 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10561
10562 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
10563 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
10564
10565 iret = send(connector, buf, 1, 0);
10566 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
10567
10568 Sleep(100);
10569
10570 closesocket(dest);
10571 dest = INVALID_SOCKET;
10572
10573 SetLastError(0xdeadbeef);
10574 key = 0xdeadbeef;
10575 num_bytes = 0xdeadbeef;
10576 olp = (WSAOVERLAPPED *)0xdeadbeef;
10577
10578 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10579 ok(bret == TRUE, "failed to get completion status %u\n", bret);
10580 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
10581 ok(key == 125, "Key is %lu\n", key);
10582 ok(num_bytes == 1, "Number of bytes transferred is %u\n", num_bytes);
10583 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10584 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
10585
10586 io_info.Flags = 0;
10587 status = pNtQueryInformationFile((HANDLE)dest, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation);
10588 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */,
10589 "expected STATUS_SUCCESS, got %08x\n", status);
10590 if (status == STATUS_SUCCESS)
10591 ok((io_info.Flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS) != 0, "got %08x\n", io_info.Flags);
10592
10593 SetLastError(0xdeadbeef);
10594 key = 0xdeadbeef;
10595 num_bytes = 0xdeadbeef;
10596 olp = (WSAOVERLAPPED *)0xdeadbeef;
10597 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10598 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10599 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10600 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10601 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10602 ok(!olp, "Overlapped structure is at %p\n", olp);
10603
10604 if (src != INVALID_SOCKET)
10605 closesocket(src);
10606 if (connector != INVALID_SOCKET)
10607 closesocket(connector);
10608
10609 /* */
10610
10611 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
10612 goto end;
10613
10614 dest = socket(AF_INET, SOCK_STREAM, 0);
10615 if (dest == INVALID_SOCKET)
10616 {
10617 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
10618 goto end;
10619 }
10620
10621 connector = socket(AF_INET, SOCK_STREAM, 0);
10622 if (connector == INVALID_SOCKET) {
10623 skip("could not create connector socket, error %d\n", WSAGetLastError());
10624 goto end;
10625 }
10626
10627 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
10628 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10629
10630 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
10631 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
10632
10633 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
10634 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
10635 &num_bytes, &ov);
10636 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
10637 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
10638
10639 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
10640 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
10641
10642 closesocket(dest);
10643 dest = INVALID_SOCKET;
10644
10645 SetLastError(0xdeadbeef);
10646 key = 0xdeadbeef;
10647 num_bytes = 0xdeadbeef;
10648 olp = (WSAOVERLAPPED *)0xdeadbeef;
10649
10650 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
10651 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10652 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED ||
10653 GetLastError() == ERROR_OPERATION_ABORTED ||
10654 GetLastError() == ERROR_CONNECTION_ABORTED ||
10655 GetLastError() == ERROR_PIPE_NOT_CONNECTED /* win 2000 */,
10656 "Last error was %d\n", GetLastError());
10657 ok(key == 125, "Key is %lu\n", key);
10658 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
10659 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
10660 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT ||
10661 olp->Internal == (ULONG)STATUS_CANCELLED ||
10662 olp->Internal == (ULONG)STATUS_CONNECTION_ABORTED ||
10663 olp->Internal == (ULONG)STATUS_PIPE_DISCONNECTED /* win 2000 */),
10664 "Internal status is %lx\n", olp ? olp->Internal : 0);
10665
10666 SetLastError(0xdeadbeef);
10667 key = 0xdeadbeef;
10668 num_bytes = 0xdeadbeef;
10669 olp = (WSAOVERLAPPED *)0xdeadbeef;
10670 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
10671 ok(bret == FALSE, "failed to get completion status %u\n", bret);
10672 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
10673 ok(key == 0xdeadbeef, "Key is %lu\n", key);
10674 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
10675 ok(!olp, "Overlapped structure is at %p\n", olp);
10676
10677
10678 end:
10679 if (dest != INVALID_SOCKET)
10680 closesocket(dest);
10681 if (src != INVALID_SOCKET)
10682 closesocket(src);
10683 if (connector != INVALID_SOCKET)
10684 closesocket(connector);
10685 CloseHandle(previous_port);
10686 }
10687
test_address_list_query(void)10688 static void test_address_list_query(void)
10689 {
10690 SOCKET_ADDRESS_LIST *address_list;
10691 DWORD bytes_returned, size;
10692 unsigned int i;
10693 SOCKET s;
10694 int ret;
10695
10696 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
10697 ok(s != INVALID_SOCKET, "Failed to create socket, error %d.\n", WSAGetLastError());
10698
10699 bytes_returned = 0;
10700 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0, &bytes_returned, NULL, NULL);
10701 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10702 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
10703 ok(bytes_returned >= FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]),
10704 "Got unexpected bytes_returned %u.\n", bytes_returned);
10705
10706 size = bytes_returned;
10707 bytes_returned = 0;
10708 address_list = HeapAlloc(GetProcessHeap(), 0, size * 2);
10709 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size * 2, &bytes_returned, NULL, NULL);
10710 ok(!ret, "Got unexpected ret %d, error %d.\n", ret, WSAGetLastError());
10711 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
10712
10713 bytes_returned = FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[address_list->iAddressCount]);
10714 for (i = 0; i < address_list->iAddressCount; ++i)
10715 {
10716 bytes_returned += address_list->Address[i].iSockaddrLength;
10717 }
10718 ok(size == bytes_returned, "Got unexpected size %u, expected %u.\n", size, bytes_returned);
10719
10720 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size, NULL, NULL, NULL);
10721 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10722 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
10723
10724 bytes_returned = 0xdeadbeef;
10725 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, size, &bytes_returned, NULL, NULL);
10726 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10727 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
10728 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
10729
10730 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, 1, &bytes_returned, NULL, NULL);
10731 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10732 ok(WSAGetLastError() == WSAEINVAL, "Got unexpected error %d.\n", WSAGetLastError());
10733 ok(bytes_returned == 0, "Got unexpected bytes_returned %u.\n", bytes_returned);
10734
10735 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list,
10736 FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]), &bytes_returned, NULL, NULL);
10737 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
10738 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
10739 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
10740
10741 HeapFree(GetProcessHeap(), 0, address_list);
10742 closesocket(s);
10743 }
10744
inet_ntoa_thread_proc(void * param)10745 static DWORD WINAPI inet_ntoa_thread_proc(void *param)
10746 {
10747 ULONG addr;
10748 const char *str;
10749 HANDLE *event = param;
10750
10751 addr = inet_addr("4.3.2.1");
10752 ok(addr == htonl(0x04030201), "expected 0x04030201, got %08x\n", addr);
10753 str = inet_ntoa(*(struct in_addr *)&addr);
10754 ok(!strcmp(str, "4.3.2.1"), "expected 4.3.2.1, got %s\n", str);
10755
10756 SetEvent(event[0]);
10757 WaitForSingleObject(event[1], 3000);
10758
10759 return 0;
10760 }
10761
test_inet_ntoa(void)10762 static void test_inet_ntoa(void)
10763 {
10764 ULONG addr;
10765 const char *str;
10766 HANDLE thread, event[2];
10767 DWORD tid;
10768
10769 addr = inet_addr("1.2.3.4");
10770 ok(addr == htonl(0x01020304), "expected 0x01020304, got %08x\n", addr);
10771 str = inet_ntoa(*(struct in_addr *)&addr);
10772 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str);
10773
10774 event[0] = CreateEventW(NULL, TRUE, FALSE, NULL);
10775 event[1] = CreateEventW(NULL, TRUE, FALSE, NULL);
10776
10777 thread = CreateThread(NULL, 0, inet_ntoa_thread_proc, event, 0, &tid);
10778 WaitForSingleObject(event[0], 3000);
10779
10780 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str);
10781
10782 SetEvent(event[1]);
10783 WaitForSingleObject(thread, 3000);
10784
10785 CloseHandle(event[0]);
10786 CloseHandle(event[1]);
10787 CloseHandle(thread);
10788 }
10789
test_WSALookupService(void)10790 static void test_WSALookupService(void)
10791 {
10792 char buffer[4096], strbuff[128];
10793 WSAQUERYSETW *qs = NULL;
10794 HANDLE hnd;
10795 PNLA_BLOB netdata;
10796 int ret;
10797 DWORD error, offset, bsize;
10798
10799 if (!pWSALookupServiceBeginW || !pWSALookupServiceEnd || !pWSALookupServiceNextW)
10800 {
10801 win_skip("WSALookupServiceBeginW or WSALookupServiceEnd or WSALookupServiceNextW not found\n");
10802 return;
10803 }
10804
10805 qs = (WSAQUERYSETW *)buffer;
10806 memset(qs, 0, sizeof(*qs));
10807
10808 /* invalid parameter tests */
10809 ret = pWSALookupServiceBeginW(NULL, 0, &hnd);
10810 error = WSAGetLastError();
10811 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
10812 todo_wine
10813 ok(error == WSAEFAULT, "expected 10014, got %d\n", error);
10814
10815 ret = pWSALookupServiceBeginW(qs, 0, NULL);
10816 error = WSAGetLastError();
10817 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
10818 todo_wine
10819 ok(error == WSAEFAULT, "expected 10014, got %d\n", error);
10820
10821 ret = pWSALookupServiceBeginW(qs, 0, &hnd);
10822 error = WSAGetLastError();
10823 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n");
10824 todo_wine
10825 ok(error == WSAEINVAL
10826 || broken(error == ERROR_INVALID_PARAMETER) /* == XP */
10827 || broken(error == WSAEFAULT) /* == NT */
10828 || broken(error == WSASERVICE_NOT_FOUND) /* == 2000 */,
10829 "expected 10022, got %d\n", error);
10830
10831 ret = pWSALookupServiceEnd(NULL);
10832 error = WSAGetLastError();
10833 todo_wine
10834 ok(ret == SOCKET_ERROR, "WSALookupServiceEnd should have failed\n");
10835 todo_wine
10836 ok(error == ERROR_INVALID_HANDLE, "expected 6, got %d\n", error);
10837
10838 /* standard network list query */
10839 qs->dwSize = sizeof(*qs);
10840 hnd = (HANDLE)0xdeadbeef;
10841 ret = pWSALookupServiceBeginW(qs, LUP_RETURN_ALL | LUP_DEEP, &hnd);
10842 error = WSAGetLastError();
10843 if(ret && error == ERROR_INVALID_PARAMETER)
10844 {
10845 win_skip("the current WSALookupServiceBeginW test is not supported in win <= 2000\n");
10846 return;
10847 }
10848
10849 todo_wine
10850 ok(!ret, "WSALookupServiceBeginW failed unexpectedly with error %d\n", error);
10851 todo_wine
10852 ok(hnd != (HANDLE)0xdeadbeef, "Handle was not filled\n");
10853
10854 offset = 0;
10855 do
10856 {
10857 memset(qs, 0, sizeof(*qs));
10858 bsize = sizeof(buffer);
10859
10860 if (pWSALookupServiceNextW(hnd, 0, &bsize, qs) == SOCKET_ERROR)
10861 {
10862 error = WSAGetLastError();
10863 if (error == WSA_E_NO_MORE) break;
10864 ok(0, "Error %d happened while listing services\n", error);
10865 break;
10866 }
10867
10868 WideCharToMultiByte(CP_ACP, 0, qs->lpszServiceInstanceName, -1,
10869 strbuff, sizeof(strbuff), NULL, NULL);
10870 trace("Network Name: %s\n", strbuff);
10871
10872 /* network data is written in the blob field */
10873 if (qs->lpBlob)
10874 {
10875 /* each network may have multiple NLA_BLOB information structures */
10876 do
10877 {
10878 netdata = (PNLA_BLOB) &qs->lpBlob->pBlobData[offset];
10879 switch (netdata->header.type)
10880 {
10881 case NLA_RAW_DATA:
10882 trace("\tNLA Data Type: NLA_RAW_DATA\n");
10883 break;
10884 case NLA_INTERFACE:
10885 trace("\tNLA Data Type: NLA_INTERFACE\n");
10886 trace("\t\tType: %d\n", netdata->data.interfaceData.dwType);
10887 trace("\t\tSpeed: %d\n", netdata->data.interfaceData.dwSpeed);
10888 trace("\t\tAdapter Name: %s\n", netdata->data.interfaceData.adapterName);
10889 break;
10890 case NLA_802_1X_LOCATION:
10891 trace("\tNLA Data Type: NLA_802_1X_LOCATION\n");
10892 trace("\t\tInformation: %s\n", netdata->data.locationData.information);
10893 break;
10894 case NLA_CONNECTIVITY:
10895 switch (netdata->data.connectivity.type)
10896 {
10897 case NLA_NETWORK_AD_HOC:
10898 trace("\t\tNetwork Type: AD HOC\n");
10899 break;
10900 case NLA_NETWORK_MANAGED:
10901 trace("\t\tNetwork Type: Managed\n");
10902 break;
10903 case NLA_NETWORK_UNMANAGED:
10904 trace("\t\tNetwork Type: Unmanaged\n");
10905 break;
10906 case NLA_NETWORK_UNKNOWN:
10907 trace("\t\tNetwork Type: Unknown\n");
10908 }
10909 switch (netdata->data.connectivity.internet)
10910 {
10911 case NLA_INTERNET_NO:
10912 trace("\t\tInternet connectivity: No\n");
10913 break;
10914 case NLA_INTERNET_YES:
10915 trace("\t\tInternet connectivity: Yes\n");
10916 break;
10917 case NLA_INTERNET_UNKNOWN:
10918 trace("\t\tInternet connectivity: Unknown\n");
10919 break;
10920 }
10921 break;
10922 case NLA_ICS:
10923 trace("\tNLA Data Type: NLA_ICS\n");
10924 trace("\t\tSpeed: %d\n",
10925 netdata->data.ICS.remote.speed);
10926 trace("\t\tType: %d\n",
10927 netdata->data.ICS.remote.type);
10928 trace("\t\tState: %d\n",
10929 netdata->data.ICS.remote.state);
10930 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.machineName, -1,
10931 strbuff, sizeof(strbuff), NULL, NULL);
10932 trace("\t\tMachine Name: %s\n", strbuff);
10933 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.sharedAdapterName, -1,
10934 strbuff, sizeof(strbuff), NULL, NULL);
10935 trace("\t\tShared Adapter Name: %s\n", strbuff);
10936 break;
10937 default:
10938 trace("\tNLA Data Type: Unknown\n");
10939 break;
10940 }
10941 }
10942 while (offset);
10943 }
10944 }
10945 while (1);
10946
10947 ret = pWSALookupServiceEnd(hnd);
10948 ok(!ret, "WSALookupServiceEnd failed unexpectedly\n");
10949 }
10950
test_WSAEnumNameSpaceProvidersA(void)10951 static void test_WSAEnumNameSpaceProvidersA(void)
10952 {
10953 LPWSANAMESPACE_INFOA name = NULL;
10954 DWORD ret, error, blen = 0, i;
10955 if (!pWSAEnumNameSpaceProvidersA)
10956 {
10957 win_skip("WSAEnumNameSpaceProvidersA not found\n");
10958 return;
10959 }
10960
10961 SetLastError(0xdeadbeef);
10962 ret = pWSAEnumNameSpaceProvidersA(&blen, name);
10963 error = WSAGetLastError();
10964 todo_wine
10965 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
10966 todo_wine
10967 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
10968
10969 /* Invalid parameter tests */
10970 SetLastError(0xdeadbeef);
10971 ret = pWSAEnumNameSpaceProvidersA(NULL, name);
10972 error = WSAGetLastError();
10973 todo_wine
10974 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
10975 todo_wine
10976 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
10977
10978 SetLastError(0xdeadbeef);
10979 ret = pWSAEnumNameSpaceProvidersA(NULL, NULL);
10980 error = WSAGetLastError();
10981 todo_wine
10982 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
10983 todo_wine
10984 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
10985
10986 SetLastError(0xdeadbeef);
10987 ret = pWSAEnumNameSpaceProvidersA(&blen, NULL);
10988 error = WSAGetLastError();
10989 todo_wine
10990 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
10991 todo_wine
10992 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
10993
10994 #ifdef __REACTOS__ /* ROSTESTS-233 */
10995 if (!blen)
10996 {
10997 skip("Failed to get length needed for name space providers.\n");
10998 return;
10999 }
11000 #endif
11001
11002 name = HeapAlloc(GetProcessHeap(), 0, blen);
11003 if (!name)
11004 {
11005 skip("Failed to alloc memory\n");
11006 return;
11007 }
11008
11009 ret = pWSAEnumNameSpaceProvidersA(&blen, name);
11010 todo_wine
11011 ok(ret > 0, "Expected more than zero name space providers\n");
11012
11013 for (i = 0;i < ret; i++)
11014 {
11015 trace("Name space Identifier (%p): %s\n", name[i].lpszIdentifier,
11016 name[i].lpszIdentifier);
11017 switch (name[i].dwNameSpace)
11018 {
11019 case NS_DNS:
11020 trace("\tName space ID: NS_DNS (%u)\n", name[i].dwNameSpace);
11021 break;
11022 case NS_NLA:
11023 trace("\tName space ID: NS_NLA (%u)\n", name[i].dwNameSpace);
11024 break;
11025 default:
11026 trace("\tName space ID: Unknown (%u)\n", name[i].dwNameSpace);
11027 break;
11028 }
11029 trace("\tActive: %d\n", name[i].fActive);
11030 trace("\tVersion: %d\n", name[i].dwVersion);
11031 }
11032
11033 HeapFree(GetProcessHeap(), 0, name);
11034 }
11035
test_WSAEnumNameSpaceProvidersW(void)11036 static void test_WSAEnumNameSpaceProvidersW(void)
11037 {
11038 LPWSANAMESPACE_INFOW name = NULL;
11039 DWORD ret, error, blen = 0, i;
11040 if (!pWSAEnumNameSpaceProvidersW)
11041 {
11042 win_skip("WSAEnumNameSpaceProvidersW not found\n");
11043 return;
11044 }
11045
11046 SetLastError(0xdeadbeef);
11047 ret = pWSAEnumNameSpaceProvidersW(&blen, name);
11048 error = WSAGetLastError();
11049 todo_wine
11050 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
11051 todo_wine
11052 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
11053
11054 /* Invalid parameter tests */
11055 SetLastError(0xdeadbeef);
11056 ret = pWSAEnumNameSpaceProvidersW(NULL, name);
11057 error = WSAGetLastError();
11058 todo_wine
11059 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
11060 todo_wine
11061 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
11062
11063 SetLastError(0xdeadbeef);
11064 ret = pWSAEnumNameSpaceProvidersW(NULL, NULL);
11065 error = WSAGetLastError();
11066 todo_wine
11067 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
11068 todo_wine
11069 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
11070
11071 SetLastError(0xdeadbeef);
11072 ret = pWSAEnumNameSpaceProvidersW(&blen, NULL);
11073 error = WSAGetLastError();
11074 todo_wine
11075 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret);
11076 todo_wine
11077 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error);
11078
11079 #ifdef __REACTOS__ /* ROSTESTS-233 */
11080 if (!blen)
11081 {
11082 skip("Failed to get length needed for name space providers.\n");
11083 return;
11084 }
11085 #endif
11086
11087 name = HeapAlloc(GetProcessHeap(), 0, blen);
11088 if (!name)
11089 {
11090 skip("Failed to alloc memory\n");
11091 return;
11092 }
11093
11094 ret = pWSAEnumNameSpaceProvidersW(&blen, name);
11095 todo_wine
11096 ok(ret > 0, "Expected more than zero name space providers\n");
11097
11098 for (i = 0;i < ret; i++)
11099 {
11100 trace("Name space Identifier (%p): %s\n", name[i].lpszIdentifier,
11101 wine_dbgstr_w(name[i].lpszIdentifier));
11102 switch (name[i].dwNameSpace)
11103 {
11104 case NS_DNS:
11105 trace("\tName space ID: NS_DNS (%u)\n", name[i].dwNameSpace);
11106 break;
11107 case NS_NLA:
11108 trace("\tName space ID: NS_NLA (%u)\n", name[i].dwNameSpace);
11109 break;
11110 default:
11111 trace("\tName space ID: Unknown (%u)\n", name[i].dwNameSpace);
11112 break;
11113 }
11114 trace("\tActive: %d\n", name[i].fActive);
11115 trace("\tVersion: %d\n", name[i].dwVersion);
11116 }
11117
11118 HeapFree(GetProcessHeap(), 0, name);
11119 }
11120
sync_read(SOCKET src,SOCKET dst)11121 static void sync_read(SOCKET src, SOCKET dst)
11122 {
11123 int ret;
11124 char data[512];
11125
11126 ret = send(dst, "Hello World!", 12, 0);
11127 ok(ret == 12, "send returned %d\n", ret);
11128
11129 memset(data, 0, sizeof(data));
11130 ret = recv(src, data, sizeof(data), 0);
11131 ok(ret == 12, "expected 12, got %d\n", ret);
11132 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", ret, ret, data);
11133 }
11134
iocp_async_read(SOCKET src,SOCKET dst)11135 static void iocp_async_read(SOCKET src, SOCKET dst)
11136 {
11137 HANDLE port;
11138 WSAOVERLAPPED ovl, *ovl_iocp;
11139 WSABUF buf;
11140 int ret;
11141 char data[512];
11142 DWORD flags, bytes;
11143 ULONG_PTR key;
11144
11145 memset(data, 0, sizeof(data));
11146 memset(&ovl, 0, sizeof(ovl));
11147
11148 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
11149 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
11150
11151 buf.len = sizeof(data);
11152 buf.buf = data;
11153 bytes = 0xdeadbeef;
11154 flags = 0;
11155 SetLastError(0xdeadbeef);
11156 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
11157 ok(ret == SOCKET_ERROR, "got %d\n", ret);
11158 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
11159 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11160
11161 bytes = 0xdeadbeef;
11162 key = 0xdeadbeef;
11163 ovl_iocp = (void *)0xdeadbeef;
11164 SetLastError(0xdeadbeef);
11165 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11166 ok(!ret, "got %d\n", ret);
11167 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11168 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11169 ok(key == 0xdeadbeef, "got key %#lx\n", key);
11170 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11171
11172 ret = send(dst, "Hello World!", 12, 0);
11173 ok(ret == 12, "send returned %d\n", ret);
11174
11175 bytes = 0xdeadbeef;
11176 key = 0xdeadbeef;
11177 ovl_iocp = NULL;
11178 SetLastError(0xdeadbeef);
11179 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11180 ok(ret, "got %d\n", ret);
11181 ok(bytes == 12, "got bytes %u\n", bytes);
11182 ok(key == 0x12345678, "got key %#lx\n", key);
11183 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
11184 if (ovl_iocp)
11185 {
11186 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
11187 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
11188 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
11189 }
11190
11191 bytes = 0xdeadbeef;
11192 key = 0xdeadbeef;
11193 ovl_iocp = (void *)0xdeadbeef;
11194 SetLastError(0xdeadbeef);
11195 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11196 ok(!ret, "got %d\n", ret);
11197 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11198 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11199 ok(key == 0xdeadbeef, "got key %#lx\n", key);
11200 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11201
11202 CloseHandle(port);
11203 }
11204
iocp_async_read_closesocket(SOCKET src,int how_to_close)11205 static void iocp_async_read_closesocket(SOCKET src, int how_to_close)
11206 {
11207 HANDLE port;
11208 WSAOVERLAPPED ovl, *ovl_iocp;
11209 WSABUF buf;
11210 int ret;
11211 char data[512];
11212 DWORD flags, bytes;
11213 ULONG_PTR key;
11214 HWND hwnd;
11215 MSG msg;
11216
11217 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
11218 0, 0, 0, 0, NULL, NULL, 0, NULL);
11219 ok(hwnd != 0, "CreateWindowEx failed\n");
11220
11221 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
11222 ok(!ret, "got %d\n", ret);
11223
11224 Sleep(100);
11225 memset(&msg, 0, sizeof(msg));
11226 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11227 ok(ret, "got %d\n", ret);
11228 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11229 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11230 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11231 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
11232
11233 memset(data, 0, sizeof(data));
11234 memset(&ovl, 0, sizeof(ovl));
11235
11236 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
11237 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
11238
11239 Sleep(100);
11240 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11241 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11242
11243 buf.len = sizeof(data);
11244 buf.buf = data;
11245 bytes = 0xdeadbeef;
11246 flags = 0;
11247 SetLastError(0xdeadbeef);
11248 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
11249 ok(ret == SOCKET_ERROR, "got %d\n", ret);
11250 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
11251 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11252
11253 Sleep(100);
11254 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11255 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11256
11257 bytes = 0xdeadbeef;
11258 key = 0xdeadbeef;
11259 ovl_iocp = (void *)0xdeadbeef;
11260 SetLastError(0xdeadbeef);
11261 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11262 ok(!ret, "got %d\n", ret);
11263 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11264 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11265 ok(key == 0xdeadbeef, "got key %#lx\n", key);
11266 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11267
11268 Sleep(100);
11269 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11270 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11271
11272 switch (how_to_close)
11273 {
11274 case 0:
11275 closesocket(src);
11276 break;
11277 case 1:
11278 CloseHandle((HANDLE)src);
11279 break;
11280 case 2:
11281 pNtClose((HANDLE)src);
11282 break;
11283 default:
11284 ok(0, "wrong value %d\n", how_to_close);
11285 break;
11286 }
11287
11288 Sleep(200);
11289 memset(&msg, 0, sizeof(msg));
11290 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11291 switch (how_to_close)
11292 {
11293 case 0:
11294 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11295 break;
11296 case 1:
11297 case 2:
11298 todo_wine
11299 {
11300 ok(ret, "got %d\n", ret);
11301 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11302 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11303 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11304 ok(msg.lParam == 0x20, "got %08lx\n", msg.lParam);
11305 }
11306 break;
11307 default:
11308 ok(0, "wrong value %d\n", how_to_close);
11309 break;
11310 }
11311
11312 bytes = 0xdeadbeef;
11313 key = 0xdeadbeef;
11314 ovl_iocp = NULL;
11315 SetLastError(0xdeadbeef);
11316 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11317 ok(!ret, "got %d\n", ret);
11318 todo_wine
11319 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
11320 ok(!bytes, "got bytes %u\n", bytes);
11321 ok(key == 0x12345678, "got key %#lx\n", key);
11322 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
11323 if (ovl_iocp)
11324 {
11325 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
11326 todo_wine
11327 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
11328 }
11329
11330 bytes = 0xdeadbeef;
11331 key = 0xdeadbeef;
11332 ovl_iocp = (void *)0xdeadbeef;
11333 SetLastError(0xdeadbeef);
11334 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11335 ok(!ret, "got %d\n", ret);
11336 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11337 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11338 ok(key == 0xdeadbeef, "got key %#lx\n", key);
11339 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11340
11341 CloseHandle(port);
11342
11343 DestroyWindow(hwnd);
11344 }
11345
iocp_async_closesocket(SOCKET src)11346 static void iocp_async_closesocket(SOCKET src)
11347 {
11348 HANDLE port;
11349 WSAOVERLAPPED *ovl_iocp;
11350 int ret;
11351 DWORD bytes;
11352 ULONG_PTR key;
11353 HWND hwnd;
11354 MSG msg;
11355
11356 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
11357 0, 0, 0, 0, NULL, NULL, 0, NULL);
11358 ok(hwnd != 0, "CreateWindowEx failed\n");
11359
11360 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
11361 ok(!ret, "got %d\n", ret);
11362
11363 Sleep(100);
11364 memset(&msg, 0, sizeof(msg));
11365 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11366 ok(ret, "got %d\n", ret);
11367 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11368 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11369 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11370 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
11371
11372 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
11373 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
11374
11375 Sleep(100);
11376 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11377 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11378
11379 bytes = 0xdeadbeef;
11380 key = 0xdeadbeef;
11381 ovl_iocp = (void *)0xdeadbeef;
11382 SetLastError(0xdeadbeef);
11383 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11384 ok(!ret, "got %d\n", ret);
11385 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11386 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11387 ok(key == 0xdeadbeef, "got key %lu\n", key);
11388 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11389
11390 Sleep(100);
11391 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11392 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11393
11394 closesocket(src);
11395
11396 Sleep(100);
11397 memset(&msg, 0, sizeof(msg));
11398 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11399 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11400
11401 bytes = 0xdeadbeef;
11402 key = 0xdeadbeef;
11403 ovl_iocp = (void *)0xdeadbeef;
11404 SetLastError(0xdeadbeef);
11405 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11406 ok(!ret, "got %d\n", ret);
11407 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11408 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11409 ok(key == 0xdeadbeef, "got key %lu\n", key);
11410 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11411
11412 CloseHandle(port);
11413
11414 DestroyWindow(hwnd);
11415 }
11416
11417 struct wsa_async_select_info
11418 {
11419 SOCKET sock;
11420 HWND hwnd;
11421 };
11422
wsa_async_select_thread(void * param)11423 static DWORD WINAPI wsa_async_select_thread(void *param)
11424 {
11425 struct wsa_async_select_info *info = param;
11426 int ret;
11427
11428 ret = WSAAsyncSelect(info->sock, info->hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
11429 ok(!ret, "got %d\n", ret);
11430
11431 return 0;
11432 }
11433
11434 struct wsa_recv_info
11435 {
11436 SOCKET sock;
11437 WSABUF wsa_buf;
11438 WSAOVERLAPPED ovl;
11439 };
11440
wsa_recv_thread(void * param)11441 static DWORD WINAPI wsa_recv_thread(void *param)
11442 {
11443 struct wsa_recv_info *info = param;
11444 int ret;
11445 DWORD flags, bytes;
11446
11447 bytes = 0xdeadbeef;
11448 flags = 0;
11449 SetLastError(0xdeadbeef);
11450 ret = WSARecv(info->sock, &info->wsa_buf, 1, &bytes, &flags, &info->ovl, NULL);
11451 ok(ret == SOCKET_ERROR, "got %d\n", ret);
11452 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
11453 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11454
11455 return 0;
11456 }
11457
iocp_async_read_thread_closesocket(SOCKET src)11458 static void iocp_async_read_thread_closesocket(SOCKET src)
11459 {
11460 struct wsa_async_select_info select_info;
11461 struct wsa_recv_info recv_info;
11462 HANDLE port, thread;
11463 WSAOVERLAPPED *ovl_iocp;
11464 int ret;
11465 char data[512];
11466 DWORD bytes, tid;
11467 ULONG_PTR key;
11468 HWND hwnd;
11469 MSG msg;
11470
11471 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
11472 0, 0, 0, 0, NULL, NULL, 0, NULL);
11473 ok(hwnd != 0, "CreateWindowEx failed\n");
11474
11475 select_info.sock = src;
11476 select_info.hwnd = hwnd;
11477 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
11478 ok(thread != 0, "CreateThread error %u\n", GetLastError());
11479 ret = WaitForSingleObject(thread, 10000);
11480 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
11481
11482 Sleep(100);
11483 memset(&msg, 0, sizeof(msg));
11484 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11485 ok(ret, "got %d\n", ret);
11486 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11487 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11488 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11489 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
11490
11491 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
11492 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
11493
11494 Sleep(100);
11495 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11496 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11497
11498 memset(data, 0, sizeof(data));
11499 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
11500 recv_info.sock = src;
11501 recv_info.wsa_buf.len = sizeof(data);
11502 recv_info.wsa_buf.buf = data;
11503 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
11504 ok(thread != 0, "CreateThread error %u\n", GetLastError());
11505 ret = WaitForSingleObject(thread, 10000);
11506 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
11507
11508 Sleep(100);
11509 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11510 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11511
11512 bytes = 0xdeadbeef;
11513 key = 0xdeadbeef;
11514 ovl_iocp = (void *)0xdeadbeef;
11515 SetLastError(0xdeadbeef);
11516 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11517 ok(!ret, "got %d\n", ret);
11518 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */,
11519 "got %u\n", GetLastError());
11520 if (GetLastError() == WAIT_TIMEOUT)
11521 {
11522 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11523 ok(key == 0xdeadbeef, "got key %lx\n", key);
11524 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11525 }
11526 else /* document XP behaviour */
11527 {
11528 ok(!bytes, "got bytes %u\n", bytes);
11529 ok(key == 0x12345678, "got key %#lx\n", key);
11530 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
11531 if (ovl_iocp)
11532 {
11533 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
11534 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
11535 }
11536
11537 closesocket(src);
11538 goto xp_is_broken;
11539 }
11540
11541 Sleep(100);
11542 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11543 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11544
11545 closesocket(src);
11546
11547 Sleep(100);
11548 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11549 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11550
11551 bytes = 0xdeadbeef;
11552 key = 0xdeadbeef;
11553 ovl_iocp = NULL;
11554 SetLastError(0xdeadbeef);
11555 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11556 ok(!ret, "got %d\n", ret);
11557 todo_wine
11558 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
11559 ok(!bytes, "got bytes %u\n", bytes);
11560 ok(key == 0x12345678, "got key %#lx\n", key);
11561 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
11562 if (ovl_iocp)
11563 {
11564 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
11565 todo_wine
11566 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
11567 }
11568
11569 xp_is_broken:
11570 bytes = 0xdeadbeef;
11571 key = 0xdeadbeef;
11572 ovl_iocp = (void *)0xdeadbeef;
11573 SetLastError(0xdeadbeef);
11574 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11575 ok(!ret, "got %d\n", ret);
11576 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
11577 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11578 ok(key == 0xdeadbeef, "got key %lu\n", key);
11579 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11580
11581 CloseHandle(port);
11582
11583 DestroyWindow(hwnd);
11584 }
11585
iocp_async_read_thread(SOCKET src,SOCKET dst)11586 static void iocp_async_read_thread(SOCKET src, SOCKET dst)
11587 {
11588 struct wsa_async_select_info select_info;
11589 struct wsa_recv_info recv_info;
11590 HANDLE port, thread;
11591 WSAOVERLAPPED *ovl_iocp;
11592 int ret;
11593 char data[512];
11594 DWORD bytes, tid;
11595 ULONG_PTR key;
11596 HWND hwnd;
11597 MSG msg;
11598
11599 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
11600 0, 0, 0, 0, NULL, NULL, 0, NULL);
11601 ok(hwnd != 0, "CreateWindowEx failed\n");
11602
11603 select_info.sock = src;
11604 select_info.hwnd = hwnd;
11605 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
11606 ok(thread != 0, "CreateThread error %u\n", GetLastError());
11607 ret = WaitForSingleObject(thread, 10000);
11608 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
11609
11610 Sleep(100);
11611 memset(&msg, 0, sizeof(msg));
11612 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11613 ok(ret, "got %d\n", ret);
11614 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11615 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11616 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11617 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
11618
11619 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
11620 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
11621
11622 Sleep(100);
11623 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11624 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11625
11626 memset(data, 0, sizeof(data));
11627 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
11628 recv_info.sock = src;
11629 recv_info.wsa_buf.len = sizeof(data);
11630 recv_info.wsa_buf.buf = data;
11631 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
11632 ok(thread != 0, "CreateThread error %u\n", GetLastError());
11633 ret = WaitForSingleObject(thread, 10000);
11634 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
11635
11636 Sleep(100);
11637 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11638 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11639
11640 bytes = 0xdeadbeef;
11641 key = 0xdeadbeef;
11642 ovl_iocp = (void *)0xdeadbeef;
11643 SetLastError(0xdeadbeef);
11644 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11645 ok(!ret, "got %d\n", ret);
11646 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */, "got %u\n", GetLastError());
11647 if (GetLastError() == WAIT_TIMEOUT)
11648 {
11649 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11650 ok(key == 0xdeadbeef, "got key %lu\n", key);
11651 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11652 }
11653 else /* document XP behaviour */
11654 {
11655 ok(bytes == 0, "got bytes %u\n", bytes);
11656 ok(key == 0x12345678, "got key %#lx\n", key);
11657 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
11658 if (ovl_iocp)
11659 {
11660 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
11661 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
11662 }
11663 }
11664
11665 Sleep(100);
11666 memset(&msg, 0, sizeof(msg));
11667 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11668 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11669 if (ret) /* document XP behaviour */
11670 {
11671 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11672 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11673 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
11674 }
11675
11676 ret = send(dst, "Hello World!", 12, 0);
11677 ok(ret == 12, "send returned %d\n", ret);
11678
11679 Sleep(100);
11680 memset(&msg, 0, sizeof(msg));
11681 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
11682 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
11683 if (ret) /* document XP behaviour */
11684 {
11685 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
11686 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
11687 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
11688 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
11689 }
11690
11691 bytes = 0xdeadbeef;
11692 key = 0xdeadbeef;
11693 ovl_iocp = (void *)0xdeadbeef;
11694 SetLastError(0xdeadbeef);
11695 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
11696 ok(ret || broken(GetLastError() == WAIT_TIMEOUT) /* XP */, "got %u\n", GetLastError());
11697 if (ret)
11698 {
11699 ok(bytes == 12, "got bytes %u\n", bytes);
11700 ok(key == 0x12345678, "got key %#lx\n", key);
11701 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
11702 if (ovl_iocp)
11703 {
11704 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
11705 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
11706 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
11707 }
11708 }
11709 else /* document XP behaviour */
11710 {
11711 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
11712 ok(key == 0xdeadbeef, "got key %lu\n", key);
11713 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
11714 }
11715
11716 CloseHandle(port);
11717
11718 DestroyWindow(hwnd);
11719 }
11720
test_iocp(void)11721 static void test_iocp(void)
11722 {
11723 SOCKET src, dst;
11724 int i, ret;
11725
11726 ret = tcp_socketpair_ovl(&src, &dst);
11727 ok(!ret, "creating socket pair failed\n");
11728 sync_read(src, dst);
11729 iocp_async_read(src, dst);
11730 closesocket(src);
11731 closesocket(dst);
11732
11733 ret = tcp_socketpair_ovl(&src, &dst);
11734 ok(!ret, "creating socket pair failed\n");
11735 iocp_async_read_thread(src, dst);
11736 closesocket(src);
11737 closesocket(dst);
11738
11739 for (i = 0; i <= 2; i++)
11740 {
11741 ret = tcp_socketpair_ovl(&src, &dst);
11742 ok(!ret, "creating socket pair failed\n");
11743 iocp_async_read_closesocket(src, i);
11744 closesocket(dst);
11745 }
11746
11747 ret = tcp_socketpair_ovl(&src, &dst);
11748 ok(!ret, "creating socket pair failed\n");
11749 iocp_async_closesocket(src);
11750 closesocket(dst);
11751
11752 ret = tcp_socketpair_ovl(&src, &dst);
11753 ok(!ret, "creating socket pair failed\n");
11754 iocp_async_read_thread_closesocket(src);
11755 closesocket(dst);
11756 }
11757
START_TEST(sock)11758 START_TEST( sock )
11759 {
11760 int i;
11761
11762 /* Leave these tests at the beginning. They depend on WSAStartup not having been
11763 * called, which is done by Init() below. */
11764 test_WithoutWSAStartup();
11765 test_WithWSAStartup();
11766
11767 Init();
11768
11769 test_inet_ntoa();
11770 test_inet_pton();
11771 test_set_getsockopt();
11772 test_so_reuseaddr();
11773 test_ip_pktinfo();
11774 test_extendedSocketOptions();
11775
11776 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
11777 {
11778 trace ( " **** STARTING TEST %d ****\n", i );
11779 do_test ( &tests[i] );
11780 trace ( " **** TEST %d COMPLETE ****\n", i );
11781 }
11782
11783 test_UDP();
11784
11785 test_getservbyname();
11786 test_WSASocket();
11787 test_WSADuplicateSocket();
11788 test_WSAEnumNetworkEvents();
11789
11790 test_WSAAddressToStringA();
11791 test_WSAAddressToStringW();
11792
11793 test_WSAStringToAddressA();
11794 test_WSAStringToAddressW();
11795
11796 test_errors();
11797 test_listen();
11798 test_select();
11799 test_accept();
11800 test_getpeername();
11801 test_getsockname();
11802 test_inet_addr();
11803 test_addr_to_print();
11804 test_ioctlsocket();
11805 test_dns();
11806 test_gethostbyname();
11807 test_gethostbyname_hack();
11808 test_gethostname();
11809
11810 test_WSASendMsg();
11811 test_WSASendTo();
11812 test_WSARecv();
11813 test_WSAPoll();
11814 test_write_watch();
11815 test_iocp();
11816
11817 test_events(0);
11818 test_events(1);
11819
11820 test_ipv6only();
11821 test_TransmitFile();
11822 test_GetAddrInfoW();
11823 test_GetAddrInfoExW();
11824 test_getaddrinfo();
11825
11826 #ifdef __REACTOS__
11827 if (!winetest_interactive)
11828 {
11829 skip("WSPAcceptEx(), WSPConnectEx() and WSPDisconnectEx() are UNIMPLEMENTED on ReactOS\n");
11830 skip("Skipping tests due to hang. See ROSTESTS-385\n");
11831 }
11832 else
11833 {
11834 #endif
11835 test_AcceptEx();
11836 test_ConnectEx();
11837 test_DisconnectEx();
11838 #ifdef __REACTOS__
11839 }
11840 #endif
11841
11842 test_sioRoutingInterfaceQuery();
11843 test_sioAddressListChange();
11844
11845 test_WSALookupService();
11846 test_WSAEnumNameSpaceProvidersA();
11847 test_WSAEnumNameSpaceProvidersW();
11848
11849 test_WSAAsyncGetServByPort();
11850 test_WSAAsyncGetServByName();
11851
11852 test_completion_port();
11853 test_address_list_query();
11854
11855 /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */
11856 test_send();
11857 test_synchronous_WSAIoctl();
11858
11859 Exit();
11860 }
11861