[WS2_32_WINETEST] Sync with Wine Staging 1.9.20.
[reactos.git] / rostests / winetests / ws2_32 / sock.c
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 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #undef _WIN32_WINNT
24 #define _WIN32_WINNT 0x0600
25
26 #include <ntstatus.h>
27 #define WIN32_NO_STATUS
28 #include <wine/test.h>
29 #include <winsock2.h>
30 #include <wine/winternl.h>
31 #include <ws2tcpip.h>
32 #include <wsipx.h>
33 #include <wsnwlink.h>
34 #include <mswsock.h>
35 #include <mstcpip.h>
36 #include <iphlpapi.h>
37 #include <stdio.h>
38
39
40 // ReactOS: Wine has this in mstcpip.h, but it doesn't belong there
41 #define WSA_CMSG_ALIGN(len) (((len) + sizeof(SIZE_T) - 1) & ~(sizeof(SIZE_T) - 1))
42
43 #define MAX_CLIENTS 4 /* Max number of clients */
44 #define FIRST_CHAR 'A' /* First character in transferred pattern */
45 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
46 #define BIND_TRIES 6 /* Number of bind() attempts */
47 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
48 after server initialization, if something hangs */
49
50 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
51
52 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
53 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
54
55 #define SERVERIP "127.0.0.1" /* IP to bind to */
56 #define SERVERPORT 9374 /* Port number to bind to */
57
58 #define wsa_ok(op, cond, msg) \
59 do { \
60 int tmp, err = 0; \
61 tmp = op; \
62 if ( !(cond tmp) ) err = WSAGetLastError(); \
63 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
64 } while (0);
65
66 #define make_keepalive(k, enable, time, interval) \
67 k.onoff = enable; \
68 k.keepalivetime = time; \
69 k.keepaliveinterval = interval;
70
71 /* Function pointers */
72 static void (WINAPI *pfreeaddrinfo)(struct addrinfo *);
73 static int (WINAPI *pgetaddrinfo)(LPCSTR,LPCSTR,const struct addrinfo *,struct addrinfo **);
74 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW);
75 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *);
76 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG);
77 static PCWSTR(WINAPI *pInetNtopW)(INT,LPVOID,LPWSTR,ULONG);
78 static int (WINAPI *pInetPtonA)(INT,LPCSTR,LPVOID);
79 static int (WINAPI *pInetPtonW)(INT,LPWSTR,LPVOID);
80 static int (WINAPI *pWSALookupServiceBeginW)(LPWSAQUERYSETW,DWORD,LPHANDLE);
81 static int (WINAPI *pWSALookupServiceEnd)(HANDLE);
82 static int (WINAPI *pWSALookupServiceNextW)(HANDLE,DWORD,LPDWORD,LPWSAQUERYSETW);
83 static int (WINAPI *pWSAEnumNameSpaceProvidersA)(LPDWORD,LPWSANAMESPACE_INFOA);
84 static int (WINAPI *pWSAEnumNameSpaceProvidersW)(LPDWORD,LPWSANAMESPACE_INFOW);
85 static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT);
86
87 /* Function pointers from iphlpapi */
88 static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG);
89 static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL);
90
91 /**************** Structs and typedefs ***************/
92
93 typedef struct thread_info
94 {
95 HANDLE thread;
96 DWORD id;
97 } thread_info;
98
99 /* Information in the server about open client connections */
100 typedef struct sock_info
101 {
102 SOCKET s;
103 struct sockaddr_in addr;
104 struct sockaddr_in peer;
105 char *buf;
106 int n_recvd;
107 int n_sent;
108 } sock_info;
109
110 /* Test parameters for both server & client */
111 typedef struct test_params
112 {
113 int sock_type;
114 int sock_prot;
115 const char *inet_addr;
116 short inet_port;
117 int chunk_size;
118 int n_chunks;
119 int n_clients;
120 } test_params;
121
122 /* server-specific test parameters */
123 typedef struct server_params
124 {
125 test_params *general;
126 DWORD sock_flags;
127 int buflen;
128 } server_params;
129
130 /* client-specific test parameters */
131 typedef struct client_params
132 {
133 test_params *general;
134 DWORD sock_flags;
135 int buflen;
136 } client_params;
137
138 /* This type combines all information for setting up a test scenario */
139 typedef struct test_setup
140 {
141 test_params general;
142 LPVOID srv;
143 server_params srv_params;
144 LPVOID clt;
145 client_params clt_params;
146 } test_setup;
147
148 /* Thread local storage for server */
149 typedef struct server_memory
150 {
151 SOCKET s;
152 struct sockaddr_in addr;
153 sock_info sock[MAX_CLIENTS];
154 } server_memory;
155
156 /* Thread local storage for client */
157 typedef struct client_memory
158 {
159 SOCKET s;
160 struct sockaddr_in addr;
161 char *send_buf;
162 char *recv_buf;
163 } client_memory;
164
165 /* SelectReadThread thread parameters */
166 typedef struct select_thread_params
167 {
168 SOCKET s;
169 BOOL ReadKilled;
170 } select_thread_params;
171
172 /* Tests used in both getaddrinfo and GetAddrInfoW */
173 static const struct addr_hint_tests
174 {
175 int family, socktype, protocol;
176 DWORD error;
177 } hinttests[] = {
178 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0 },
179 {AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP, 0 },
180 {AF_UNSPEC, SOCK_STREAM, IPPROTO_IPV6,0 },
181 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_TCP, 0 },
182 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0 },
183 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_IPV6,0 },
184 {AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 },
185 {AF_INET, SOCK_STREAM, IPPROTO_UDP, 0 },
186 {AF_INET, SOCK_STREAM, IPPROTO_IPV6,0 },
187 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, 0 },
188 {AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0 },
189 {AF_INET, SOCK_DGRAM, IPPROTO_IPV6,0 },
190 {AF_UNSPEC, 0, IPPROTO_TCP, 0 },
191 {AF_UNSPEC, 0, IPPROTO_UDP, 0 },
192 {AF_UNSPEC, 0, IPPROTO_IPV6,0 },
193 {AF_UNSPEC, SOCK_STREAM, 0, 0 },
194 {AF_UNSPEC, SOCK_DGRAM, 0, 0 },
195 {AF_INET, 0, IPPROTO_TCP, 0 },
196 {AF_INET, 0, IPPROTO_UDP, 0 },
197 {AF_INET, 0, IPPROTO_IPV6,0 },
198 {AF_INET, SOCK_STREAM, 0, 0 },
199 {AF_INET, SOCK_DGRAM, 0, 0 },
200 {AF_UNSPEC, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
201 {AF_UNSPEC, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
202 {AF_UNSPEC, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
203 {AF_INET, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT },
204 {AF_INET, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT },
205 {AF_INET, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
206 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
207 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
208 {AF_INET, SOCK_DGRAM, 999, 0 },
209 {AF_INET, SOCK_DGRAM, 999, 0 },
210 };
211
212 /**************** Static variables ***************/
213
214 static DWORD tls; /* Thread local storage index */
215 static HANDLE thread[1+MAX_CLIENTS];
216 static DWORD thread_id[1+MAX_CLIENTS];
217 static HANDLE server_ready;
218 static HANDLE client_ready[MAX_CLIENTS];
219 static int client_id;
220
221 /**************** General utility functions ***************/
222
223 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len);
224 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock);
225
226 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
227 {
228 SOCKET server = INVALID_SOCKET;
229 struct sockaddr_in addr;
230 int len;
231 int ret;
232
233 *src = INVALID_SOCKET;
234 *dst = INVALID_SOCKET;
235
236 *src = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
237 if (*src == INVALID_SOCKET)
238 goto end;
239
240 server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
241 if (server == INVALID_SOCKET)
242 goto end;
243
244 memset(&addr, 0, sizeof(addr));
245 addr.sin_family = AF_INET;
246 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
247 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
248 if (ret != 0)
249 goto end;
250
251 len = sizeof(addr);
252 ret = getsockname(server, (struct sockaddr*)&addr, &len);
253 if (ret != 0)
254 goto end;
255
256 ret = listen(server, 1);
257 if (ret != 0)
258 goto end;
259
260 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
261 if (ret != 0)
262 goto end;
263
264 len = sizeof(addr);
265 *dst = accept(server, (struct sockaddr*)&addr, &len);
266
267 end:
268 if (server != INVALID_SOCKET)
269 closesocket(server);
270 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
271 return 0;
272 closesocket(*src);
273 closesocket(*dst);
274 return -1;
275 }
276
277 static void set_so_opentype ( BOOL overlapped )
278 {
279 int optval = !overlapped, newval, len = sizeof (int);
280
281 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
282 (LPVOID) &optval, sizeof (optval) ) == 0,
283 "setting SO_OPENTYPE failed\n" );
284 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
285 (LPVOID) &newval, &len ) == 0,
286 "getting SO_OPENTYPE failed\n" );
287 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
288 }
289
290 static int set_blocking ( SOCKET s, BOOL blocking )
291 {
292 u_long val = !blocking;
293 return ioctlsocket ( s, FIONBIO, &val );
294 }
295
296 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
297 {
298 char c, *p;
299 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
300 memset ( p, c, chunk_size );
301 }
302
303 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
304 {
305 char c, *p;
306 int i;
307 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
308 {
309 for ( i = 0; i < chunk_size; i++ )
310 if ( p[i] != c ) return i;
311 }
312 return -1;
313 }
314
315 /*
316 * This routine is called when a client / server does not expect any more data,
317 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
318 */
319 static void read_zero_bytes ( SOCKET s )
320 {
321 char buf[256];
322 int tmp, n = 0;
323 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
324 n += tmp;
325 ok ( n <= 0, "garbage data received: %d bytes\n", n );
326 }
327
328 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen )
329 {
330 char* last = buf + buflen, *p;
331 int n = 1;
332 for ( p = buf; n > 0 && p < last; )
333 {
334 n = send ( s, p, min ( sendlen, last - p ), flags );
335 if (n > 0) p += n;
336 }
337 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
338 return p - buf;
339 }
340
341 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen )
342 {
343 char* last = buf + buflen, *p;
344 int n = 1;
345 for ( p = buf; n > 0 && p < last; )
346 {
347 n = recv ( s, p, min ( recvlen, last - p ), flags );
348 if (n > 0) p += n;
349 }
350 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
351 return p - buf;
352 }
353
354 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen )
355 {
356 char* last = buf + buflen, *p;
357 int n = 1;
358 for ( p = buf; n > 0 && p < last; )
359 {
360 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen );
361 if (n > 0) p += n;
362 }
363 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
364 return p - buf;
365 }
366
367 /*
368 * Call this routine right after thread startup.
369 * SO_OPENTYPE must by 0, regardless what the server did.
370 */
371 static void check_so_opentype (void)
372 {
373 int tmp = 1, len;
374 len = sizeof (tmp);
375 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
376 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
377 }
378
379 static void compare_addrinfo (ADDRINFO *a, ADDRINFO *b)
380 {
381 for (; a && b ; a = a->ai_next, b = b->ai_next)
382 {
383 ok(a->ai_flags == b->ai_flags,
384 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
385 ok(a->ai_family == b->ai_family,
386 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
387 ok(a->ai_socktype == b->ai_socktype,
388 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
389 ok(a->ai_protocol == b->ai_protocol,
390 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
391 ok(a->ai_addrlen == b->ai_addrlen,
392 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
393 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
394 "Wrong address data\n");
395 if (a->ai_canonname && b->ai_canonname)
396 {
397 ok(!strcmp(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
398 a->ai_canonname, b->ai_canonname);
399 }
400 else
401 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
402 a->ai_canonname, b->ai_canonname);
403 }
404 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
405 }
406
407 static void compare_addrinfow (ADDRINFOW *a, ADDRINFOW *b)
408 {
409 for (; a && b ; a = a->ai_next, b = b->ai_next)
410 {
411 ok(a->ai_flags == b->ai_flags,
412 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
413 ok(a->ai_family == b->ai_family,
414 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
415 ok(a->ai_socktype == b->ai_socktype,
416 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
417 ok(a->ai_protocol == b->ai_protocol,
418 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
419 ok(a->ai_addrlen == b->ai_addrlen,
420 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
421 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
422 "Wrong address data\n");
423 if (a->ai_canonname && b->ai_canonname)
424 {
425 ok(!lstrcmpW(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
426 wine_dbgstr_w(a->ai_canonname), wine_dbgstr_w(b->ai_canonname));
427 }
428 else
429 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
430 a->ai_canonname, b->ai_canonname);
431 }
432 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
433 }
434
435 /**************** Server utility functions ***************/
436
437 /*
438 * Even if we have closed our server socket cleanly,
439 * the OS may mark the address "in use" for some time -
440 * this happens with native Linux apps, too.
441 */
442 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
443 {
444 int err, wsaerr = 0, n_try = BIND_TRIES;
445
446 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
447 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
448 n_try-- >= 0)
449 {
450 trace ( "address in use, waiting ...\n" );
451 Sleep ( 1000 * BIND_SLEEP );
452 }
453 ok ( err == 0, "failed to bind: %d\n", wsaerr );
454 }
455
456 static void server_start ( server_params *par )
457 {
458 int i;
459 test_params *gen = par->general;
460 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
461
462 TlsSetValue ( tls, mem );
463 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
464 NULL, 0, par->sock_flags );
465 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
466
467 mem->addr.sin_family = AF_INET;
468 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
469 mem->addr.sin_port = htons ( gen->inet_port );
470
471 for (i = 0; i < MAX_CLIENTS; i++)
472 {
473 mem->sock[i].s = INVALID_SOCKET;
474 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
475 mem->sock[i].n_recvd = 0;
476 mem->sock[i].n_sent = 0;
477 }
478
479 if ( gen->sock_type == SOCK_STREAM )
480 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
481 }
482
483 static void server_stop (void)
484 {
485 int i;
486 server_memory *mem = TlsGetValue ( tls );
487
488 for (i = 0; i < MAX_CLIENTS; i++ )
489 {
490 LocalFree ( mem->sock[i].buf );
491 if ( mem->sock[i].s != INVALID_SOCKET )
492 closesocket ( mem->sock[i].s );
493 }
494 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
495 LocalFree ( mem );
496 ExitThread ( GetCurrentThreadId () );
497 }
498
499 /**************** Client utilitiy functions ***************/
500
501 static void client_start ( client_params *par )
502 {
503 test_params *gen = par->general;
504 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
505
506 TlsSetValue ( tls, mem );
507
508 WaitForSingleObject ( server_ready, INFINITE );
509
510 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
511 NULL, 0, par->sock_flags );
512
513 mem->addr.sin_family = AF_INET;
514 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
515 mem->addr.sin_port = htons ( gen->inet_port );
516
517 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
518
519 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
520 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
521 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
522
523 SetEvent ( client_ready[client_id] );
524 /* Wait for the other clients to come up */
525 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
526 }
527
528 static void client_stop (void)
529 {
530 client_memory *mem = TlsGetValue ( tls );
531 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
532 LocalFree ( mem->send_buf );
533 LocalFree ( mem );
534 ExitThread(0);
535 }
536
537 /**************** Servers ***************/
538
539 /*
540 * simple_server: A very basic server doing synchronous IO.
541 */
542 static VOID WINAPI simple_server ( server_params *par )
543 {
544 test_params *gen = par->general;
545 server_memory *mem;
546 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
547 id = GetCurrentThreadId();
548
549 trace ( "simple_server (%x) starting\n", id );
550
551 set_so_opentype ( FALSE ); /* non-overlapped */
552 server_start ( par );
553 mem = TlsGetValue ( tls );
554
555 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
556 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
557
558 trace ( "simple_server (%x) ready\n", id );
559 SetEvent ( server_ready ); /* notify clients */
560
561 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
562 {
563 trace ( "simple_server (%x): waiting for client\n", id );
564
565 /* accept a single connection */
566 tmp = sizeof ( mem->sock[0].peer );
567 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
568 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
569
570 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
571 "simple_server (%x): strange peer address\n", id );
572
573 /* Receive data & check it */
574 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
575 ok ( n_recvd == n_expected,
576 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
577 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
578 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
579
580 /* Echo data back */
581 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
582 ok ( n_sent == n_expected,
583 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
584
585 /* cleanup */
586 read_zero_bytes ( mem->sock[0].s );
587 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
588 mem->sock[0].s = INVALID_SOCKET;
589 }
590
591 trace ( "simple_server (%x) exiting\n", id );
592 server_stop ();
593 }
594
595 /*
596 * oob_server: A very basic server receiving out-of-band data.
597 */
598 static VOID WINAPI oob_server ( server_params *par )
599 {
600 test_params *gen = par->general;
601 server_memory *mem;
602 u_long atmark = 0;
603 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
604 id = GetCurrentThreadId();
605
606 trace ( "oob_server (%x) starting\n", id );
607
608 set_so_opentype ( FALSE ); /* non-overlapped */
609 server_start ( par );
610 mem = TlsGetValue ( tls );
611
612 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
613 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
614
615 trace ( "oob_server (%x) ready\n", id );
616 SetEvent ( server_ready ); /* notify clients */
617
618 trace ( "oob_server (%x): waiting for client\n", id );
619
620 /* accept a single connection */
621 tmp = sizeof ( mem->sock[0].peer );
622 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
623 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
624
625 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
626 "oob_server (%x): strange peer address\n", id );
627
628 /* check initial atmark state */
629 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
630 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
631
632 /* Receive normal data */
633 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
634 ok ( n_recvd == n_expected,
635 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
636 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
637 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos);
638
639 /* check atmark state */
640 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
641 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
642
643 /* Echo data back */
644 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
645 ok ( n_sent == n_expected,
646 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
647
648 /* Receive a part of the out-of-band data and print atmark state */
649 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen );
650 ok ( n_recvd == 8,
651 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
652 n_expected -= 8;
653
654 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
655 trace( "oob_server (%x): %s the OOB mark: %i\n", id, atmark == 1 ? "not at" : "at", atmark );
656
657 /* Receive the rest of the out-of-band data and check atmark state */
658 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
659
660 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
661 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
662
663 /* cleanup */
664 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
665 mem->sock[0].s = INVALID_SOCKET;
666
667 trace ( "oob_server (%x) exiting\n", id );
668 server_stop ();
669 }
670
671 /*
672 * select_server: A non-blocking server.
673 */
674 static VOID WINAPI select_server ( server_params *par )
675 {
676 test_params *gen = par->general;
677 server_memory *mem;
678 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
679 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
680 n_set, delta, n_ready;
681 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
682 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
683
684 trace ( "select_server (%x) starting\n", id );
685
686 set_so_opentype ( FALSE ); /* non-overlapped */
687 server_start ( par );
688 mem = TlsGetValue ( tls );
689
690 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
691 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
692
693 trace ( "select_server (%x) ready\n", id );
694 SetEvent ( server_ready ); /* notify clients */
695
696 FD_ZERO ( &fds_openrecv );
697 FD_ZERO ( &fds_recv );
698 FD_ZERO ( &fds_send );
699 FD_ZERO ( &fds_opensend );
700
701 FD_SET ( mem->s, &fds_openrecv );
702
703 while(1)
704 {
705 fds_recv = fds_openrecv;
706 fds_send = fds_opensend;
707
708 n_set = 0;
709
710 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
711 "select_server (%x): select() failed: %d\n" );
712
713 /* check for incoming requests */
714 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
715 n_set += 1;
716
717 trace ( "select_server (%x): accepting client connection\n", id );
718
719 /* accept a single connection */
720 tmp = sizeof ( mem->sock[n_connections].peer );
721 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
722 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
723
724 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
725 "select_server (%x): strange peer address\n", id );
726
727 /* add to list of open connections */
728 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
729 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
730
731 n_connections++;
732 }
733
734 /* handle open requests */
735
736 for ( i = 0; i < n_connections; i++ )
737 {
738 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
739 n_set += 1;
740
741 if ( mem->sock[i].n_recvd < n_expected ) {
742 /* Receive data & check it */
743 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 );
744 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
745 mem->sock[i].n_recvd += n_recvd;
746
747 if ( mem->sock[i].n_recvd == n_expected ) {
748 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
749 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
750 FD_CLR ( mem->sock[i].s, &fds_openrecv );
751 }
752
753 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
754 }
755 }
756
757 /* only echo back what we've received */
758 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
759
760 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
761 n_set += 1;
762
763 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
764 /* Echo data back */
765 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
766 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
767 mem->sock[i].n_sent += n_sent;
768
769 if ( mem->sock[i].n_sent == n_expected ) {
770 FD_CLR ( mem->sock[i].s, &fds_opensend );
771 }
772
773 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
774 }
775 }
776 }
777
778 /* check that select returned the correct number of ready sockets */
779 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
780
781 /* check if all clients are done */
782 if ( ( fds_opensend.fd_count == 0 )
783 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
784 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
785 break;
786 }
787 }
788
789 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
790 {
791 /* cleanup */
792 read_zero_bytes ( mem->sock[i].s );
793 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
794 mem->sock[i].s = INVALID_SOCKET;
795 }
796
797 trace ( "select_server (%x) exiting\n", id );
798 server_stop ();
799 }
800
801 /**************** Clients ***************/
802
803 /*
804 * simple_client: A very basic client doing synchronous IO.
805 */
806 static VOID WINAPI simple_client ( client_params *par )
807 {
808 test_params *gen = par->general;
809 client_memory *mem;
810 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
811
812 id = GetCurrentThreadId();
813 trace ( "simple_client (%x): starting\n", id );
814 /* wait here because we want to call set_so_opentype before creating a socket */
815 WaitForSingleObject ( server_ready, INFINITE );
816 trace ( "simple_client (%x): server ready\n", id );
817
818 check_so_opentype ();
819 set_so_opentype ( FALSE ); /* non-overlapped */
820 client_start ( par );
821 mem = TlsGetValue ( tls );
822
823 /* Connect */
824 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
825 0 ==, "simple_client (%x): connect error: %d\n" );
826 ok ( set_blocking ( mem->s, TRUE ) == 0,
827 "simple_client (%x): failed to set blocking mode\n", id );
828 trace ( "simple_client (%x) connected\n", id );
829
830 /* send data to server */
831 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
832 ok ( n_sent == n_expected,
833 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
834
835 /* shutdown send direction */
836 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
837
838 /* Receive data echoed back & check it */
839 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
840 ok ( n_recvd == n_expected,
841 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
842
843 /* check data */
844 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
845 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
846
847 /* cleanup */
848 read_zero_bytes ( mem->s );
849 trace ( "simple_client (%x) exiting\n", id );
850 client_stop ();
851 }
852
853 /*
854 * oob_client: A very basic client sending out-of-band data.
855 */
856 static VOID WINAPI oob_client ( client_params *par )
857 {
858 test_params *gen = par->general;
859 client_memory *mem;
860 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
861
862 id = GetCurrentThreadId();
863 trace ( "oob_client (%x): starting\n", id );
864 /* wait here because we want to call set_so_opentype before creating a socket */
865 WaitForSingleObject ( server_ready, INFINITE );
866 trace ( "oob_client (%x): server ready\n", id );
867
868 check_so_opentype ();
869 set_so_opentype ( FALSE ); /* non-overlapped */
870 client_start ( par );
871 mem = TlsGetValue ( tls );
872
873 /* Connect */
874 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
875 0 ==, "oob_client (%x): connect error: %d\n" );
876 ok ( set_blocking ( mem->s, TRUE ) == 0,
877 "oob_client (%x): failed to set blocking mode\n", id );
878 trace ( "oob_client (%x) connected\n", id );
879
880 /* send data to server */
881 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
882 ok ( n_sent == n_expected,
883 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
884
885 /* Receive data echoed back & check it */
886 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
887 ok ( n_recvd == n_expected,
888 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
889 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
890 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
891
892 /* send out-of-band data to server */
893 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen );
894 ok ( n_sent == n_expected,
895 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
896
897 /* shutdown send direction */
898 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
899
900 /* cleanup */
901 read_zero_bytes ( mem->s );
902 trace ( "oob_client (%x) exiting\n", id );
903 client_stop ();
904 }
905
906 /*
907 * simple_mixed_client: mixing send and recvfrom
908 */
909 static VOID WINAPI simple_mixed_client ( client_params *par )
910 {
911 test_params *gen = par->general;
912 client_memory *mem;
913 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
914 int fromLen = sizeof(mem->addr);
915 struct sockaddr test;
916
917 id = GetCurrentThreadId();
918 trace ( "simple_client (%x): starting\n", id );
919 /* wait here because we want to call set_so_opentype before creating a socket */
920 WaitForSingleObject ( server_ready, INFINITE );
921 trace ( "simple_client (%x): server ready\n", id );
922
923 check_so_opentype ();
924 set_so_opentype ( FALSE ); /* non-overlapped */
925 client_start ( par );
926 mem = TlsGetValue ( tls );
927
928 /* Connect */
929 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
930 0 ==, "simple_client (%x): connect error: %d\n" );
931 ok ( set_blocking ( mem->s, TRUE ) == 0,
932 "simple_client (%x): failed to set blocking mode\n", id );
933 trace ( "simple_client (%x) connected\n", id );
934
935 /* send data to server */
936 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
937 ok ( n_sent == n_expected,
938 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
939
940 /* shutdown send direction */
941 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
942
943 /* this shouldn't change, since lpFrom, is not updated on
944 connection oriented sockets - exposed by bug 11640
945 */
946 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
947
948 /* Receive data echoed back & check it */
949 n_recvd = do_synchronous_recvfrom ( mem->s,
950 mem->recv_buf,
951 n_expected,
952 0,
953 (struct sockaddr *)&test,
954 &fromLen,
955 par->buflen );
956 ok ( n_recvd == n_expected,
957 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
958
959 /* check that lpFrom was not updated */
960 ok(0 ==
961 strcmp(
962 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
963 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
964
965 /* check data */
966 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
967 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
968
969 /* cleanup */
970 read_zero_bytes ( mem->s );
971 trace ( "simple_client (%x) exiting\n", id );
972 client_stop ();
973 }
974
975 /*
976 * event_client: An event-driven client
977 */
978 static void WINAPI event_client ( client_params *par )
979 {
980 test_params *gen = par->general;
981 client_memory *mem;
982 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
983 tmp, err, n;
984 HANDLE event;
985 WSANETWORKEVENTS wsa_events;
986 char *send_last, *recv_last, *send_p, *recv_p;
987 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
988
989 trace ( "event_client (%x): starting\n", id );
990 client_start ( par );
991 trace ( "event_client (%x): server ready\n", id );
992
993 mem = TlsGetValue ( tls );
994
995 /* Prepare event notification for connect, makes socket nonblocking */
996 event = WSACreateEvent ();
997 WSAEventSelect ( mem->s, event, FD_CONNECT );
998 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
999 if ( tmp != 0 ) {
1000 err = WSAGetLastError ();
1001 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
1002 tmp = WaitForSingleObject ( event, INFINITE );
1003 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
1004 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1005 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1006 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
1007 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
1008 if ( err ) goto out;
1009 }
1010
1011 trace ( "event_client (%x) connected\n", id );
1012
1013 WSAEventSelect ( mem->s, event, mask );
1014
1015 recv_p = mem->recv_buf;
1016 recv_last = mem->recv_buf + n_expected;
1017 send_p = mem->send_buf;
1018 send_last = mem->send_buf + n_expected;
1019
1020 while ( TRUE )
1021 {
1022 err = WaitForSingleObject ( event, INFINITE );
1023 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
1024
1025 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1026 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1027
1028 if ( wsa_events.lNetworkEvents & FD_WRITE )
1029 {
1030 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
1031 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
1032
1033 if ( err== 0 )
1034 do
1035 {
1036 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
1037 if ( n < 0 )
1038 {
1039 err = WSAGetLastError ();
1040 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
1041 }
1042 else
1043 send_p += n;
1044 }
1045 while ( n >= 0 && send_p < send_last );
1046
1047 if ( send_p == send_last )
1048 {
1049 trace ( "event_client (%x): all data sent - shutdown\n", id );
1050 shutdown ( mem->s, SD_SEND );
1051 mask &= ~FD_WRITE;
1052 WSAEventSelect ( mem->s, event, mask );
1053 }
1054 }
1055 if ( wsa_events.lNetworkEvents & FD_READ )
1056 {
1057 err = wsa_events.iErrorCode[ FD_READ_BIT ];
1058 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
1059 if ( err != 0 ) break;
1060
1061 /* First read must succeed */
1062 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1063 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
1064
1065 while ( n >= 0 ) {
1066 recv_p += n;
1067 if ( recv_p == recv_last )
1068 {
1069 mask &= ~FD_READ;
1070 trace ( "event_client (%x): all data received\n", id );
1071 WSAEventSelect ( mem->s, event, mask );
1072 break;
1073 }
1074 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1075 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
1076 ok ( 0, "event_client (%x): read error: %d\n", id, err );
1077
1078 }
1079 }
1080 if ( wsa_events.lNetworkEvents & FD_CLOSE )
1081 {
1082 trace ( "event_client (%x): close event\n", id );
1083 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
1084 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
1085 break;
1086 }
1087 }
1088
1089 n = send_p - mem->send_buf;
1090 ok ( send_p == send_last,
1091 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
1092 n = recv_p - mem->recv_buf;
1093 ok ( recv_p == recv_last,
1094 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
1095 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1096 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
1097
1098 out:
1099 WSACloseEvent ( event );
1100 trace ( "event_client (%x) exiting\n", id );
1101 client_stop ();
1102 }
1103
1104 /* Tests for WSAStartup */
1105 static void test_WithoutWSAStartup(void)
1106 {
1107 DWORD err;
1108
1109 WSASetLastError(0xdeadbeef);
1110 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n");
1111 err = WSAGetLastError();
1112 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1113
1114 WSASetLastError(0xdeadbeef);
1115 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n");
1116 err = WSAGetLastError();
1117 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1118 }
1119
1120 static void test_WithWSAStartup(void)
1121 {
1122 WSADATA data;
1123 WORD version = MAKEWORD( 2, 2 );
1124 INT res, socks, i, j;
1125 SOCKET sock;
1126 LPVOID ptr;
1127 struct
1128 {
1129 SOCKET src, dst, dup_src, dup_dst;
1130 } pairs[128];
1131 DWORD error;
1132
1133 res = WSAStartup( version, &data );
1134 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1135
1136 ptr = gethostbyname("localhost");
1137 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
1138
1139 /* Alloc some sockets to check if they are destroyed on WSACleanup */
1140 for (socks = 0; socks < sizeof(pairs) / sizeof(pairs[0]); socks++)
1141 {
1142 WSAPROTOCOL_INFOA info;
1143 if (tcp_socketpair(&pairs[socks].src, &pairs[socks].dst)) break;
1144
1145 memset(&info, 0, sizeof(info));
1146 ok(!WSADuplicateSocketA(pairs[socks].src, GetCurrentProcessId(), &info),
1147 "WSADuplicateSocketA should have worked\n");
1148 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0);
1149 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n");
1150
1151 memset(&info, 0, sizeof(info));
1152 ok(!WSADuplicateSocketA(pairs[socks].dst, GetCurrentProcessId(), &info),
1153 "WSADuplicateSocketA should have worked\n");
1154 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0);
1155 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n");
1156 }
1157
1158 res = send(pairs[0].src, "TEST", 4, 0);
1159 ok(res == 4, "send failed with error %d\n", WSAGetLastError());
1160
1161 WSACleanup();
1162
1163 res = WSAStartup( version, &data );
1164 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1165
1166 /* show that sockets are destroyed automatically after WSACleanup */
1167 SetLastError(0xdeadbeef);
1168 res = send(pairs[0].src, "TEST", 4, 0);
1169 error = WSAGetLastError();
1170 ok(res == SOCKET_ERROR, "send should have failed\n");
1171 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1172
1173 SetLastError(0xdeadbeef);
1174 res = send(pairs[0].dst, "TEST", 4, 0);
1175 error = WSAGetLastError();
1176 ok(res == SOCKET_ERROR, "send should have failed\n");
1177 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1178
1179 /* Check that all sockets were destroyed */
1180 for (i = 0; i < socks; i++)
1181 {
1182 for (j = 0; j < 4; j++)
1183 {
1184 struct sockaddr_in saddr;
1185 int size = sizeof(saddr);
1186 switch(j)
1187 {
1188 case 0: sock = pairs[i].src; break;
1189 case 1: sock = pairs[i].dup_src; break;
1190 case 2: sock = pairs[i].dst; break;
1191 case 3: sock = pairs[i].dup_dst; break;
1192 }
1193
1194 SetLastError(0xdeadbeef);
1195 res = getsockname(sock, (struct sockaddr *)&saddr, &size);
1196 error = WSAGetLastError();
1197 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i);
1198 todo_wine ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error);
1199 }
1200 }
1201
1202 /* While wine is not fixed, close all sockets manually */
1203 for (i = 0; i < socks; i++)
1204 {
1205 closesocket(pairs[i].src);
1206 closesocket(pairs[i].dst);
1207 closesocket(pairs[i].dup_src);
1208 closesocket(pairs[i].dup_dst);
1209 }
1210
1211 res = WSACleanup();
1212 ok(res == 0, "expected 0, got %d\n", res);
1213 WSASetLastError(0xdeadbeef);
1214 res = WSACleanup();
1215 error = WSAGetLastError();
1216 ok ( (res == SOCKET_ERROR && error == WSANOTINITIALISED) ||
1217 broken(res == 0), /* WinME */
1218 "WSACleanup returned %d WSAGetLastError is %d\n", res, error);
1219 }
1220
1221 /**************** Main program utility functions ***************/
1222
1223 static void Init (void)
1224 {
1225 WORD ver = MAKEWORD (2, 2);
1226 WSADATA data;
1227 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), hiphlpapi;
1228
1229 pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo");
1230 pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo");
1231 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
1232 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
1233 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1234 pInetNtopW = (void *)GetProcAddress(hws2_32, "InetNtopW");
1235 pInetPtonA = (void *)GetProcAddress(hws2_32, "inet_pton");
1236 pInetPtonW = (void *)GetProcAddress(hws2_32, "InetPtonW");
1237 pWSALookupServiceBeginW = (void *)GetProcAddress(hws2_32, "WSALookupServiceBeginW");
1238 pWSALookupServiceEnd = (void *)GetProcAddress(hws2_32, "WSALookupServiceEnd");
1239 pWSALookupServiceNextW = (void *)GetProcAddress(hws2_32, "WSALookupServiceNextW");
1240 pWSAEnumNameSpaceProvidersA = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersA");
1241 pWSAEnumNameSpaceProvidersW = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersW");
1242 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll");
1243
1244 hiphlpapi = LoadLibraryA("iphlpapi.dll");
1245 if (hiphlpapi)
1246 {
1247 pGetIpForwardTable = (void *)GetProcAddress(hiphlpapi, "GetIpForwardTable");
1248 pGetAdaptersInfo = (void *)GetProcAddress(hiphlpapi, "GetAdaptersInfo");
1249 }
1250
1251 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1252 tls = TlsAlloc();
1253 }
1254
1255 static void Exit (void)
1256 {
1257 INT ret, err;
1258 TlsFree ( tls );
1259 ret = WSACleanup();
1260 err = WSAGetLastError();
1261 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1262 }
1263
1264 static void StartServer (LPTHREAD_START_ROUTINE routine,
1265 test_params *general, server_params *par)
1266 {
1267 par->general = general;
1268 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1269 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1270 }
1271
1272 static void StartClients (LPTHREAD_START_ROUTINE routine,
1273 test_params *general, client_params *par)
1274 {
1275 int i;
1276 par->general = general;
1277 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1278 {
1279 client_id = i - 1;
1280 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1281 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1282 /* Make sure the client is up and running */
1283 WaitForSingleObject ( client_ready[client_id], INFINITE );
1284 };
1285 }
1286
1287 static void do_test( test_setup *test )
1288 {
1289 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1290 DWORD wait;
1291
1292 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1293 for (i = 0; i <= n; i++)
1294 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1295
1296 StartServer ( test->srv, &test->general, &test->srv_params );
1297 StartClients ( test->clt, &test->general, &test->clt_params );
1298 WaitForSingleObject ( server_ready, INFINITE );
1299
1300 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1301 ok ( wait <= WAIT_OBJECT_0 + n ,
1302 "some threads have not completed: %x\n", wait );
1303
1304 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
1305 {
1306 for (i = 0; i <= n; i++)
1307 {
1308 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
1309 {
1310 trace ("terminating thread %08x\n", thread_id[i]);
1311 TerminateThread ( thread [i], 0 );
1312 }
1313 }
1314 }
1315 CloseHandle ( server_ready );
1316 for (i = 0; i <= n; i++)
1317 CloseHandle ( client_ready[i] );
1318 }
1319
1320 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1321 /* optname = SO_LINGER */
1322 static const LINGER linger_testvals[] = {
1323 {0,0},
1324 {0,73},
1325 {1,0},
1326 {5,189}
1327 };
1328
1329 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1330 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1331 bug in the linux kernel (fixed in 2.6.8) */
1332 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1333
1334 static void test_set_getsockopt(void)
1335 {
1336 SOCKET s, s2;
1337 int i, err, lasterr;
1338 int timeout;
1339 LINGER lingval;
1340 int size;
1341 WSAPROTOCOL_INFOA infoA;
1342 WSAPROTOCOL_INFOW infoW;
1343 char providername[WSAPROTOCOL_LEN + 1];
1344 DWORD value;
1345 struct _prottest
1346 {
1347 int family, type, proto;
1348 } prottest[] = {
1349 {AF_INET, SOCK_STREAM, IPPROTO_TCP},
1350 {AF_INET, SOCK_DGRAM, IPPROTO_UDP},
1351 {AF_INET6, SOCK_STREAM, IPPROTO_TCP},
1352 {AF_INET6, SOCK_DGRAM, IPPROTO_UDP}
1353 };
1354 union _csspace
1355 {
1356 CSADDR_INFO cs;
1357 char space[128];
1358 } csinfoA, csinfoB;
1359
1360 s = socket(AF_INET, SOCK_STREAM, 0);
1361 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1362 if( s == INVALID_SOCKET) return;
1363 /* SO_RCVTIMEO */
1364 timeout = SOCKTIMEOUT1;
1365 size = sizeof(timeout);
1366 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1367 if( !err)
1368 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1369 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1370 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1371
1372 timeout = 0;
1373 size = sizeof(timeout);
1374 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1375 if( !err)
1376 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1377 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1378 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1379
1380 /* SO_SNDTIMEO */
1381 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1382 size = sizeof(timeout);
1383 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1384 if( !err)
1385 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1386 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1387 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1388
1389 /* SO_SNDBUF */
1390 value = 4096;
1391 size = sizeof(value);
1392 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size);
1393 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1394 value = 0xdeadbeef;
1395 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size);
1396 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1397 ok( value == 4096, "expected 4096, got %u\n", value );
1398
1399 /* SO_RCVBUF */
1400 value = 4096;
1401 size = sizeof(value);
1402 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size);
1403 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1404 value = 0xdeadbeef;
1405 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size);
1406 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1407 ok( value == 4096, "expected 4096, got %u\n", value );
1408
1409 /* SO_LINGER */
1410 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1411 size = sizeof(lingval);
1412 lingval = linger_testvals[i];
1413 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1414 if( !err)
1415 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1416 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1417 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1418 (lingval.l_linger == linger_testvals[i].l_linger ||
1419 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1420 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1421 lingval.l_onoff, lingval.l_linger,
1422 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1423 }
1424
1425 size = sizeof(lingval);
1426 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1427 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1428 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1429 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1430 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1431 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1432
1433 size = sizeof(BOOL);
1434 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1435 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1436 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1437 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1438 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1439 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1440
1441 /* Test for erroneously passing a value instead of a pointer as optval */
1442 size = sizeof(char);
1443 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1444 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1445 "instead of failing.\n");
1446 lasterr = WSAGetLastError();
1447 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1448 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1449 lasterr, WSAEFAULT);
1450
1451 /* SO_RCVTIMEO with invalid values for level */
1452 size = sizeof(timeout);
1453 timeout = SOCKTIMEOUT1;
1454 SetLastError(0xdeadbeef);
1455 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1456 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1457 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1458 err, WSAGetLastError());
1459
1460 timeout = SOCKTIMEOUT1;
1461 SetLastError(0xdeadbeef);
1462 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1463 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1464 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1465 err, WSAGetLastError());
1466
1467 /* Test SO_ERROR set/get */
1468 SetLastError(0xdeadbeef);
1469 i = 1234;
1470 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size);
1471 todo_wine
1472 ok( !err && !WSAGetLastError(),
1473 "got %d with %d (expected 0 with 0)\n",
1474 err, WSAGetLastError());
1475
1476 SetLastError(0xdeadbeef);
1477 i = 4321;
1478 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1479 todo_wine
1480 ok( !err && !WSAGetLastError(),
1481 "got %d with %d (expected 0 with 0)\n",
1482 err, WSAGetLastError());
1483 todo_wine
1484 ok (i == 1234, "got %d (expected 1234)\n", i);
1485
1486 /* Test invalid optlen */
1487 SetLastError(0xdeadbeef);
1488 size = 1;
1489 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1490 todo_wine
1491 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEFAULT),
1492 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1493 err, WSAGetLastError());
1494
1495 closesocket(s);
1496 /* Test with the closed socket */
1497 SetLastError(0xdeadbeef);
1498 size = sizeof(i);
1499 i = 1234;
1500 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1501 todo_wine
1502 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAENOTSOCK),
1503 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1504 err, WSAGetLastError());
1505 ok (i == 1234, "expected 1234, got %d\n", i);
1506
1507 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1508 s = socket(AF_INET, SOCK_DGRAM, 0);
1509 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1510 size = sizeof(i);
1511 i = 0x0000000a;
1512 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size);
1513 if (!err)
1514 {
1515 for (i = 0; i < 4; i++)
1516 {
1517 int k, j;
1518 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1519 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1);
1520 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1521 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size);
1522 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1523 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i];
1524 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k);
1525 }
1526 }
1527 else
1528 win_skip("IP_MULTICAST_TTL is unsupported\n");
1529 closesocket(s);
1530
1531 /* test SO_PROTOCOL_INFOA invalid parameters */
1532 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1533 "getsockopt should have failed\n");
1534 err = WSAGetLastError();
1535 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1536 size = sizeof(WSAPROTOCOL_INFOA);
1537 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1538 "getsockopt should have failed\n");
1539 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1540 err = WSAGetLastError();
1541 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1542 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1543 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1544 "getsockopt should have failed\n");
1545 err = WSAGetLastError();
1546 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1547 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, NULL),
1548 "getsockopt should have failed\n");
1549 err = WSAGetLastError();
1550 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1551 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, &size),
1552 "getsockopt should have failed\n");
1553 err = WSAGetLastError();
1554 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1555 size = sizeof(WSAPROTOCOL_INFOA) / 2;
1556 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1557 "getsockopt should have failed\n");
1558 err = WSAGetLastError();
1559 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1560 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1561 size = sizeof(WSAPROTOCOL_INFOA) * 2;
1562 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1563 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1564 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size);
1565
1566 closesocket(s);
1567
1568 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1569 for (i = 0; i < sizeof(prottest) / sizeof(prottest[0]); i++)
1570 {
1571 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto);
1572 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue;
1573
1574 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n",
1575 WSAGetLastError());
1576
1577 /* compare both A and W version */
1578 infoA.szProtocol[0] = 0;
1579 size = sizeof(WSAPROTOCOL_INFOA);
1580 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1581 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1582 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1583
1584 infoW.szProtocol[0] = 0;
1585 size = sizeof(WSAPROTOCOL_INFOW);
1586 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &infoW, &size);
1587 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1588 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size);
1589
1590 trace("provider name '%s', family %d, type %d, proto %d\n",
1591 infoA.szProtocol, prottest[i].family, prottest[i].type, prottest[i].proto);
1592
1593 ok(infoA.szProtocol[0] || broken(!infoA.szProtocol[0]) /* NT4 */,
1594 "WSAPROTOCOL_INFOA was not filled\n");
1595 ok(infoW.szProtocol[0] || broken(!infoA.szProtocol[0]) /* NT4 */,
1596 "WSAPROTOCOL_INFOW was not filled\n");
1597
1598 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1,
1599 providername, sizeof(providername), NULL, NULL);
1600 ok(!strcmp(infoA.szProtocol,providername),
1601 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername);
1602
1603 ok(!memcmp(&infoA, &infoW, FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol)),
1604 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1605
1606 /* Remove IF when WSAEnumProtocols support IPV6 data */
1607 todo_wine_if (prottest[i].family == AF_INET6)
1608 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n",
1609 prottest[i].family, infoA.iAddressFamily);
1610 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n",
1611 prottest[i].type, infoA.iSocketType);
1612 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n",
1613 prottest[i].proto, infoA.iProtocol);
1614
1615 closesocket(s);
1616 }
1617
1618 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1619 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1620 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1621 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1622 ok(s2 != INVALID_SOCKET, "Failed to create socket\n");
1623
1624 SetLastError(0xdeadbeef);
1625 size = sizeof(csinfoA);
1626 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1627 if (!err)
1628 {
1629 struct sockaddr_in saddr;
1630 memset(&saddr, 0, sizeof(saddr));
1631 saddr.sin_family = AF_INET;
1632 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1633
1634 /* Socket is not bound, no information provided */
1635 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1636 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1637 /* Socket is not connected, no information provided */
1638 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1639 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1640
1641 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr));
1642 ok(!err, "Expected 0, got %d\n", err);
1643 size = sizeof(csinfoA);
1644 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1645 ok(!err, "Expected 0, got %d\n", err);
1646
1647 /* Socket is bound */
1648 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n");
1649 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n");
1650 /* Socket is not connected, no information provided */
1651 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1652 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1653
1654 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1655 ok(!err, "Expected 0, got %d\n", err);
1656 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1657 ok(!err, "Expected 0, got %d\n", err);
1658 err = listen(s2, 1);
1659 ok(!err, "Expected 0, got %d\n", err);
1660 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr));
1661 ok(!err, "Expected 0, got %d\n", err);
1662 size = sizeof(saddr);
1663 err = accept(s2, (struct sockaddr*)&saddr, &size);
1664 ok(err != INVALID_SOCKET, "Failed to accept socket\n");
1665 closesocket(s2);
1666 s2 = err;
1667
1668 size = sizeof(csinfoA);
1669 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1670 ok(!err, "Expected 0, got %d\n", err);
1671 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size);
1672 ok(!err, "Expected 0, got %d\n", err);
1673 ok(size == sizeof(csinfoA), "Got %d\n", size);
1674 size = sizeof(saddr);
1675 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1676 csinfoA.cs.LocalAddr.iSockaddrLength);
1677 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1678 csinfoA.cs.RemoteAddr.iSockaddrLength);
1679 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size),
1680 "Expected matching addresses\n");
1681 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size),
1682 "Expected matching addresses\n");
1683 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1684 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1685 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1686 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1687
1688 err = getpeername(s, (struct sockaddr *)&saddr, &size);
1689 ok(!err, "Expected 0, got %d\n", err);
1690 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1691 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1692 err = getpeername(s2, (struct sockaddr *)&saddr, &size);
1693 ok(!err, "Expected 0, got %d\n", err);
1694 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1695 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1696 err = getsockname(s, (struct sockaddr *)&saddr, &size);
1697 ok(!err, "Expected 0, got %d\n", err);
1698 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1699 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1700 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1701 ok(!err, "Expected 0, got %d\n", err);
1702 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1703 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1704
1705 SetLastError(0xdeadbeef);
1706 size = sizeof(CSADDR_INFO);
1707 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1708 ok(err, "Expected non-zero\n");
1709 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size);
1710 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1711
1712 /* At least for IPv4 the size is exactly 56 bytes */
1713 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs);
1714 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1715 ok(!err, "Expected 0, got %d\n", err);
1716 size--;
1717 SetLastError(0xdeadbeef);
1718 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1719 ok(err, "Expected non-zero\n");
1720 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1721 }
1722 else
1723 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1724
1725 closesocket(s);
1726 closesocket(s2);
1727 }
1728
1729 static void test_so_reuseaddr(void)
1730 {
1731 struct sockaddr_in saddr;
1732 SOCKET s1,s2;
1733 unsigned int rc,reuse;
1734 int size;
1735 DWORD err;
1736
1737 saddr.sin_family = AF_INET;
1738 saddr.sin_port = htons(9375);
1739 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1740
1741 s1=socket(AF_INET, SOCK_STREAM, 0);
1742 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1743 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1744 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1745
1746 s2=socket(AF_INET, SOCK_STREAM, 0);
1747 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1748
1749 reuse=0x1234;
1750 size=sizeof(reuse);
1751 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1752 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1753
1754 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1755 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1756
1757 reuse = 1;
1758 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1759 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1760
1761 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1762 * a port immediately after closing another socket on that port, so
1763 * basically following the BSD socket semantics here. */
1764 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1765 if(rc==0)
1766 {
1767 int s3=socket(AF_INET, SOCK_STREAM, 0), s4;
1768 trace("<= Win XP behavior of SO_REUSEADDR\n");
1769
1770 /* If we could bind again in the same port this is Windows version <= XP.
1771 * Lets test if we can really connect to one of them. */
1772 set_blocking(s1, FALSE);
1773 set_blocking(s2, FALSE);
1774 rc = listen(s1, 1);
1775 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1776 rc = listen(s2, 1);
1777 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1778 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr));
1779 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError());
1780
1781 /* the delivery of the connection is random so we need to try on both sockets */
1782 size = sizeof(saddr);
1783 s4 = accept(s1, (struct sockaddr*)&saddr, &size);
1784 if(s4 == INVALID_SOCKET)
1785 s4 = accept(s2, (struct sockaddr*)&saddr, &size);
1786 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n");
1787
1788 closesocket(s1);
1789 closesocket(s3);
1790 closesocket(s4);
1791 }
1792 else
1793 {
1794 trace(">= Win 2003 behavior of SO_REUSEADDR\n");
1795 err = WSAGetLastError();
1796 ok(err==WSAEACCES, "expected 10013, got %d\n", err);
1797
1798 closesocket(s1);
1799 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1800 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1801 }
1802
1803 closesocket(s2);
1804 }
1805
1806 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1807
1808 static void test_ip_pktinfo(void)
1809 {
1810 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
1811 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
1812 struct sockaddr_in s1addr, s2addr, s3addr;
1813 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
1814 LPFN_WSARECVMSG pWSARecvMsg = NULL;
1815 unsigned int rc, yes = 1;
1816 BOOL foundhdr;
1817 DWORD dwBytes, dwSize, dwFlags;
1818 socklen_t addrlen;
1819 WSACMSGHDR *cmsg;
1820 WSAOVERLAPPED ov;
1821 WSABUF iovec[1];
1822 SOCKET s1, s2;
1823 WSAMSG hdr;
1824 int i, err;
1825
1826 memset(&ov, 0, sizeof(ov));
1827 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1828 if (ov.hEvent == INVALID_HANDLE_VALUE)
1829 {
1830 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
1831 return;
1832 }
1833
1834 memset(&hdr, 0x00, sizeof(hdr));
1835 s1addr.sin_family = AF_INET;
1836 s1addr.sin_port = htons(0);
1837 /* Note: s1addr.sin_addr is set below */
1838 iovec[0].buf = recvbuf;
1839 iovec[0].len = sizeof(recvbuf);
1840 hdr.name = (struct sockaddr*)&s3addr;
1841 hdr.namelen = sizeof(s3addr);
1842 hdr.lpBuffers = &iovec[0];
1843 hdr.dwBufferCount = 1;
1844 hdr.Control.buf = pktbuf;
1845 /* Note: hdr.Control.len is set below */
1846 hdr.dwFlags = 0;
1847
1848 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++)
1849 {
1850 s1addr.sin_addr.s_addr = addresses[i];
1851
1852 /* Build "server" side socket */
1853 s1=socket(AF_INET, SOCK_DGRAM, 0);
1854 if (s1 == INVALID_SOCKET)
1855 {
1856 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1857 goto cleanup;
1858 }
1859
1860 /* Obtain the WSARecvMsg function */
1861 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
1862 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
1863 if (!pWSARecvMsg)
1864 {
1865 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
1866 closesocket(s1);
1867 goto cleanup;
1868 }
1869
1870 /* Setup the server side socket */
1871 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
1872 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
1873 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
1874 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1875
1876 /* Build "client" side socket */
1877 addrlen = sizeof(s2addr);
1878 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0)
1879 {
1880 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
1881 closesocket(s1);
1882 goto cleanup;
1883 }
1884 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
1885 s2=socket(AF_INET, SOCK_DGRAM, 0);
1886 if (s2 == INVALID_SOCKET)
1887 {
1888 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1889 closesocket(s1);
1890 goto cleanup;
1891 }
1892
1893 /* Test an empty message header */
1894 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
1895 err=WSAGetLastError();
1896 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
1897
1898 /*
1899 * Send a packet from the client to the server and test for specifying
1900 * a short control header.
1901 */
1902 SetLastError(0xdeadbeef);
1903 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1904 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1905 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1906 hdr.Control.len = 1;
1907 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1908 err=WSAGetLastError();
1909 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
1910 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
1911 hdr.dwFlags = 0; /* Reset flags */
1912
1913 /* Perform another short control header test, this time with an overlapped receive */
1914 hdr.Control.len = 1;
1915 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1916 err=WSAGetLastError();
1917 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1918 SetLastError(0xdeadbeef);
1919 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1920 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1921 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1922 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1923 {
1924 skip("Server side did not receive packet, some tests skipped.\n");
1925 closesocket(s2);
1926 closesocket(s1);
1927 continue;
1928 }
1929 dwFlags = 0;
1930 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
1931 ok(dwFlags == 0,
1932 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
1933 ok(hdr.dwFlags == MSG_CTRUNC,
1934 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
1935 hdr.dwFlags = 0; /* Reset flags */
1936
1937 /*
1938 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1939 * on the server end and check that the returned packet matches what was sent.
1940 */
1941 hdr.Control.len = sizeof(pktbuf);
1942 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1943 err=WSAGetLastError();
1944 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1945 ok(hdr.Control.len == sizeof(pktbuf),
1946 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
1947 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1948 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1949 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1950 {
1951 skip("Server side did not receive packet, some tests skipped.\n");
1952 closesocket(s2);
1953 closesocket(s1);
1954 continue;
1955 }
1956 dwSize = 0;
1957 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
1958 ok(dwSize == sizeof(msg),
1959 "WSARecvMsg() buffer length does not match transmitted data!\n");
1960 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
1961 "WSARecvMsg() buffer does not match transmitted data!\n");
1962 ok(hdr.Control.len == IP_PKTINFO_LEN,
1963 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
1964
1965 /* Test for the expected IP_PKTINFO return information. */
1966 foundhdr = FALSE;
1967 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
1968 {
1969 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
1970 {
1971 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
1972
1973 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
1974 foundhdr = TRUE;
1975 }
1976 }
1977 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
1978
1979 closesocket(s2);
1980 closesocket(s1);
1981 }
1982
1983 cleanup:
1984 CloseHandle(ov.hEvent);
1985 }
1986
1987 /************* Array containing the tests to run **********/
1988
1989 #define STD_STREAM_SOCKET \
1990 SOCK_STREAM, \
1991 0, \
1992 SERVERIP, \
1993 SERVERPORT
1994
1995 static test_setup tests [] =
1996 {
1997 /* Test 0: synchronous client and server */
1998 {
1999 {
2000 STD_STREAM_SOCKET,
2001 2048,
2002 16,
2003 2
2004 },
2005 simple_server,
2006 {
2007 NULL,
2008 0,
2009 64
2010 },
2011 simple_client,
2012 {
2013 NULL,
2014 0,
2015 128
2016 }
2017 },
2018 /* Test 1: event-driven client, synchronous server */
2019 {
2020 {
2021 STD_STREAM_SOCKET,
2022 2048,
2023 16,
2024 2
2025 },
2026 simple_server,
2027 {
2028 NULL,
2029 0,
2030 64
2031 },
2032 event_client,
2033 {
2034 NULL,
2035 WSA_FLAG_OVERLAPPED,
2036 128
2037 }
2038 },
2039 /* Test 2: synchronous client, non-blocking server via select() */
2040 {
2041 {
2042 STD_STREAM_SOCKET,
2043 2048,
2044 16,
2045 2
2046 },
2047 select_server,
2048 {
2049 NULL,
2050 0,
2051 64
2052 },
2053 simple_client,
2054 {
2055 NULL,
2056 0,
2057 128
2058 }
2059 },
2060 /* Test 3: OOB client, OOB server */
2061 {
2062 {
2063 STD_STREAM_SOCKET,
2064 128,
2065 16,
2066 1
2067 },
2068 oob_server,
2069 {
2070 NULL,
2071 0,
2072 128
2073 },
2074 oob_client,
2075 {
2076 NULL,
2077 0,
2078 128
2079 }
2080 },
2081 /* Test 4: synchronous mixed client and server */
2082 {
2083 {
2084 STD_STREAM_SOCKET,
2085 2048,
2086 16,
2087 2
2088 },
2089 simple_server,
2090 {
2091 NULL,
2092 0,
2093 64
2094 },
2095 simple_mixed_client,
2096 {
2097 NULL,
2098 0,
2099 128
2100 }
2101 }
2102 };
2103
2104 static void test_UDP(void)
2105 {
2106 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2107 possible that this test fails due to dropped packets. */
2108
2109 /* peer 0 receives data from all other peers */
2110 struct sock_info peer[NUM_UDP_PEERS];
2111 char buf[16];
2112 int ss, i, n_recv, n_sent;
2113
2114 memset (buf,0,sizeof(buf));
2115 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
2116 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
2117
2118 peer[i].addr.sin_family = AF_INET;
2119 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
2120
2121 if ( i == 0 ) {
2122 peer[i].addr.sin_port = htons ( SERVERPORT );
2123 } else {
2124 peer[i].addr.sin_port = htons ( 0 );
2125 }
2126
2127 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
2128
2129 /* test getsockname() to get peer's port */
2130 ss = sizeof ( peer[i].addr );
2131 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
2132 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
2133 }
2134
2135 /* test getsockname() */
2136 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
2137
2138 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2139 /* send client's ip */
2140 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
2141 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
2142 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
2143 }
2144
2145 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2146 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
2147 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
2148 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
2149 }
2150 }
2151
2152 static DWORD WINAPI do_getservbyname( void *param )
2153 {
2154 struct {
2155 const char *name;
2156 const char *proto;
2157 int port;
2158 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
2159
2160 HANDLE *starttest = param;
2161 int i, j;
2162 struct servent *pserv[2];
2163
2164 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT,
2165 "test_getservbyname: timeout waiting for start signal\n" );
2166
2167 /* ensure that necessary buffer resizes are completed */
2168 for ( j = 0; j < 2; j++) {
2169 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2170 }
2171
2172 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
2173 for ( j = 0; j < 2; j++ ) {
2174 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2175 ok ( pserv[j] != NULL || broken(pserv[j] == NULL) /* win8, fixed in win81 */,
2176 "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
2177 if ( !pserv[j] ) continue;
2178 ok ( pserv[j]->s_port == htons(serv[j].port),
2179 "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
2180 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ),
2181 "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
2182 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ),
2183 "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
2184 }
2185
2186 ok ( pserv[0] == pserv[1] || broken(pserv[0] != pserv[1]) /* win8, fixed in win81 */,
2187 "getservbyname: winsock resized servent buffer when not necessary\n" );
2188 }
2189
2190 return 0;
2191 }
2192
2193 static void test_getservbyname(void)
2194 {
2195 int i;
2196 HANDLE starttest, thread[NUM_THREADS];
2197 DWORD thread_id[NUM_THREADS];
2198
2199 starttest = CreateEventA ( NULL, 1, 0, "test_getservbyname_starttest" );
2200
2201 /* create threads */
2202 for ( i = 0; i < NUM_THREADS; i++ ) {
2203 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
2204 }
2205
2206 /* signal threads to start */
2207 SetEvent ( starttest );
2208
2209 for ( i = 0; i < NUM_THREADS; i++) {
2210 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
2211 }
2212 }
2213
2214 static void test_WSASocket(void)
2215 {
2216 SOCKET sock = INVALID_SOCKET;
2217 WSAPROTOCOL_INFOA *pi;
2218 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP};
2219 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP};
2220 int items, err, size, socktype, i, j;
2221 UINT pi_size;
2222
2223 SetLastError(0xdeadbeef);
2224 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET,
2225 "WSASocketA should have failed\n");
2226 err = WSAGetLastError();
2227 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2228
2229 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2230 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2231 closesocket(sock);
2232
2233 sock = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
2234 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2235 closesocket(sock);
2236
2237 SetLastError(0xdeadbeef);
2238 ok(WSASocketA(0, SOCK_STREAM, -1, NULL, 0, 0) == INVALID_SOCKET,
2239 "WSASocketA should have failed\n");
2240 err = WSAGetLastError();
2241 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2242
2243 SetLastError(0xdeadbeef);
2244 ok(WSASocketA(0, -1, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2245 "WSASocketA should have failed\n");
2246 err = WSAGetLastError();
2247 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2248
2249 SetLastError(0xdeadbeef);
2250 ok(WSASocketA(0, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2251 "WSASocketA should have failed\n");
2252 err = WSAGetLastError();
2253 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2254
2255 SetLastError(0xdeadbeef);
2256 ok(WSASocketA(AF_INET, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2257 "WSASocketA should have failed\n");
2258 err = WSAGetLastError();
2259 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2260
2261 SetLastError(0xdeadbeef);
2262 ok(WSASocketA(AF_INET, 0, -1, NULL, 0, 0) == INVALID_SOCKET,
2263 "WSASocketA should have failed\n");
2264 err = WSAGetLastError();
2265 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2266
2267 SetLastError(0xdeadbeef);
2268 ok(WSASocketA(0, -1, -1, NULL, 0, 0) == INVALID_SOCKET,
2269 "WSASocketA should have failed\n");
2270 err = WSAGetLastError();
2271 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2272
2273 SetLastError(0xdeadbeef);
2274 ok(WSASocketA(-1, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2275 "WSASocketA should have failed\n");
2276 err = WSAGetLastError();
2277 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2278
2279 sock = WSASocketA(AF_INET, 0, IPPROTO_TCP, NULL, 0, 0);
2280 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2281 closesocket(sock);
2282
2283 SetLastError(0xdeadbeef);
2284 ok(WSASocketA(0, SOCK_STREAM, 0, NULL, 0, 0) == INVALID_SOCKET,
2285 "WSASocketA should have failed\n");
2286 err = WSAGetLastError();
2287 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2288
2289 SetLastError(0xdeadbeef);
2290 ok(WSASocketA(0, 0, 0xdead, NULL, 0, 0) == INVALID_SOCKET,
2291 "WSASocketA should have failed\n");
2292 err = WSAGetLastError();
2293 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2294
2295 SetLastError(0xdeadbeef);
2296 ok(WSASocketA(AF_INET, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2297 "WSASocketA should have failed\n");
2298 err = WSAGetLastError();
2299 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2300
2301 SetLastError(0xdeadbeef);
2302 ok(WSASocketA(0, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2303 "WSASocketA should have failed\n");
2304 err = WSAGetLastError();
2305 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2306
2307 sock = WSASocketA(0, 0, IPPROTO_TCP, NULL, 0, 0);
2308 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2309 closesocket(sock);
2310
2311 /* SOCK_STREAM does not support IPPROTO_UDP */
2312 SetLastError(0xdeadbeef);
2313 ok(WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2314 "WSASocketA should have failed\n");
2315 err = WSAGetLastError();
2316 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2317
2318 /* SOCK_DGRAM does not support IPPROTO_TCP */
2319 SetLastError(0xdeadbeef);
2320 ok(WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_TCP, NULL, 0, 0) == INVALID_SOCKET,
2321 "WSASocketA should have failed\n");
2322 err = WSAGetLastError();
2323 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2324
2325 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2326 * to avoid a crash on win98.
2327 */
2328 pi_size = 0;
2329 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size);
2330 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2331 items);
2332 err = WSAGetLastError();
2333 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2334 err, WSAENOBUFS);
2335
2336 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2337 ok(pi != NULL, "Failed to allocate memory\n");
2338 if (pi == NULL) {
2339 skip("Can't continue without memory.\n");
2340 return;
2341 }
2342
2343 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size);
2344 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2345 WSAGetLastError());
2346
2347 if (items == 0) {
2348 skip("No protocols enumerated.\n");
2349 HeapFree(GetProcessHeap(), 0, pi);
2350 return;
2351 }
2352
2353 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
2354 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
2355 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2356 WSAGetLastError());
2357 closesocket(sock);
2358
2359 /* find what parameters are used first: plain parameters or protocol info struct */
2360 pi[0].iProtocol = -1;
2361 pi[0].iSocketType = -1;
2362 pi[0].iAddressFamily = -1;
2363 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET,
2364 "WSASocketA should have failed\n");
2365 err = WSAGetLastError();
2366 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2367
2368 pi[0].iProtocol = 0;
2369 pi[0].iSocketType = 0;
2370 pi[0].iAddressFamily = 0;
2371 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0);
2372 if(sock != INVALID_SOCKET)
2373 {
2374 win_skip("must work only in OS <= 2003\n");
2375 closesocket(sock);
2376 }
2377 else
2378 {
2379 err = WSAGetLastError();
2380 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2381 }
2382
2383 pi[0].iProtocol = IPPROTO_UDP;
2384 pi[0].iSocketType = SOCK_DGRAM;
2385 pi[0].iAddressFamily = AF_INET;
2386 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0);
2387 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2388 WSAGetLastError());
2389 size = sizeof(socktype);
2390 socktype = 0xdead;
2391 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2392 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2393 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2394 SOCK_DGRAM, socktype);
2395 closesocket(sock);
2396
2397 sock = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi[0], 0, 0);
2398 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2399 WSAGetLastError());
2400 size = sizeof(socktype);
2401 socktype = 0xdead;
2402 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2403 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2404 ok(socktype == SOCK_STREAM, "Wrong socket type, expected %d received %d\n",
2405 SOCK_STREAM, socktype);
2406 closesocket(sock);
2407
2408 HeapFree(GetProcessHeap(), 0, pi);
2409
2410 pi_size = 0;
2411 items = WSAEnumProtocolsA(NULL, NULL, &pi_size);
2412 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2413 items);
2414 err = WSAGetLastError();
2415 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2416 err, WSAENOBUFS);
2417
2418 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2419 ok(pi != NULL, "Failed to allocate memory\n");
2420 if (pi == NULL) {
2421 skip("Can't continue without memory.\n");
2422 return;
2423 }
2424
2425 items = WSAEnumProtocolsA(NULL, pi, &pi_size);
2426 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2427 WSAGetLastError());
2428
2429 /* when no protocol and socket type are specified the first entry
2430 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2431 * is returned */
2432 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2433 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2434 WSAGetLastError());
2435
2436 size = sizeof(socktype);
2437 socktype = 0xdead;
2438 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2439 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2440 for(i = 0; i < items; i++)
2441 {
2442 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
2443 {
2444 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n",
2445 pi[i].iSocketType, socktype);
2446 break;
2447 }
2448 }
2449 ok(i != items, "Creating a socket without protocol and socket type didn't work\n");
2450 closesocket(sock);
2451
2452 /* when no socket type is specified the first entry from WSAEnumProtocols
2453 * that matches the protocol is returned */
2454 for (i = 0; i < sizeof(autoprotocols) / sizeof(autoprotocols[0]); i++)
2455 {
2456 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0);
2457 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n",
2458 autoprotocols[i], WSAGetLastError());
2459
2460 size = sizeof(socktype);
2461 socktype = 0xdead;
2462 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2463 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2464
2465 for (err = 1, j = 0; j < items; j++)
2466 {
2467 if (pi[j].iProtocol == autoprotocols[i])
2468 {
2469 if (socktype == pi[j].iSocketType)
2470 err = 0;
2471 else
2472 ok(0, "Wrong socket type, expected %d received %d\n",
2473 pi[j].iSocketType, socktype);
2474 break;
2475 }
2476 }
2477 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]);
2478
2479 closesocket(sock);
2480 }
2481
2482 HeapFree(GetProcessHeap(), 0, pi);
2483
2484 SetLastError(0xdeadbeef);
2485 /* starting on vista the socket function returns error during the socket
2486 creation and no longer in the socket operations (sendto, readfrom) */
2487 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
2488 if (sock == INVALID_SOCKET)
2489 {
2490 err = WSAGetLastError();
2491 ok(err == WSAEACCES, "Expected 10013, received %d\n", err);
2492 skip("SOCK_RAW is not supported\n");
2493 }
2494 else
2495 {
2496 trace("SOCK_RAW is supported\n");
2497
2498 size = sizeof(socktype);
2499 socktype = 0xdead;
2500 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2501 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2502 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2503 SOCK_RAW, socktype);
2504 closesocket(sock);
2505
2506 todo_wine {
2507 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0);
2508 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2509 WSAGetLastError());
2510 size = sizeof(socktype);
2511 socktype = 0xdead;
2512 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2513 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2514 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2515 SOCK_RAW, socktype);
2516 closesocket(sock);
2517 }
2518
2519 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_TCP, NULL, 0, 0);
2520 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2521 WSAGetLastError());
2522 size = sizeof(socktype);
2523 socktype = 0xdead;
2524 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2525 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2526 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2527 SOCK_RAW, socktype);
2528 closesocket(sock);
2529 }
2530
2531 /* IPX socket tests */
2532
2533 SetLastError(0xdeadbeef);
2534 sock = WSASocketA(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, NULL, 0, 0);
2535 if (sock == INVALID_SOCKET)
2536 {
2537 err = WSAGetLastError();
2538 todo_wine_if (err == WSAEPROTONOSUPPORT)
2539 ok(err == WSAEAFNOSUPPORT || broken(err == WSAEPROTONOSUPPORT), "Expected 10047, received %d\n", err);
2540 skip("IPX is not supported\n");
2541 }
2542 else
2543 {
2544 WSAPROTOCOL_INFOA info;
2545 closesocket(sock);
2546
2547 trace("IPX is supported\n");
2548
2549 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0);
2550 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2551 WSAGetLastError());
2552
2553 size = sizeof(socktype);
2554 socktype = 0xdead;
2555 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2556 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2557 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2558 SOCK_DGRAM, socktype);
2559
2560 /* check socket family, type and protocol */
2561 size = sizeof(WSAPROTOCOL_INFOA);
2562 err = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &info, &size);
2563 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2564 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n",
2565 NSPROTO_IPX, info.iProtocol);
2566 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n",
2567 AF_IPX, info.iProtocol);
2568 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2569 SOCK_DGRAM, info.iSocketType);
2570 closesocket(sock);
2571
2572 /* SOCK_STREAM does not support NSPROTO_IPX */
2573 SetLastError(0xdeadbeef);
2574 ok(WSASocketA(AF_IPX, SOCK_STREAM, NSPROTO_IPX, NULL, 0, 0) == INVALID_SOCKET,
2575 "WSASocketA should have failed\n");
2576 err = WSAGetLastError();
2577 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2578
2579 /* test extended IPX support - that is adding any number between 0 and 255
2580 * to the IPX protocol value will make it be used as IPX packet type */
2581 for(i = 0;i <= 255;i += 17)
2582 {
2583 SetLastError(0xdeadbeef);
2584 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0);
2585 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2586 WSAGetLastError());
2587
2588 size = sizeof(int);
2589 socktype = -1;
2590 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size);
2591 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2592 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n",
2593 i, socktype);
2594
2595 closesocket(sock);
2596 }
2597 }
2598 }
2599
2600 static void test_WSADuplicateSocket(void)
2601 {
2602 SOCKET source, dupsock;
2603 WSAPROTOCOL_INFOA info;
2604 DWORD err;
2605 struct sockaddr_in addr;
2606 int socktype, size, addrsize, ret;
2607 char teststr[] = "TEST", buffer[16];
2608
2609 source = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
2610 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2611
2612 /* test invalid parameters */
2613 SetLastError(0xdeadbeef);
2614 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n");
2615 err = WSAGetLastError();
2616 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2617
2618 SetLastError(0xdeadbeef);
2619 ok(WSADuplicateSocketA(source, 0, NULL),
2620 "WSADuplicateSocketA should have failed\n");
2621 err = WSAGetLastError();
2622 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2623
2624 SetLastError(0xdeadbeef);
2625 ok(WSADuplicateSocketA(source, ~0, &info),
2626 "WSADuplicateSocketA should have failed\n");
2627 err = WSAGetLastError();
2628 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2629
2630 SetLastError(0xdeadbeef);
2631 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info),
2632 "WSADuplicateSocketA should have failed\n");
2633 err = WSAGetLastError();
2634 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2635
2636 SetLastError(0xdeadbeef);
2637 ok(WSADuplicateSocketA(source, GetCurrentProcessId(), NULL),
2638 "WSADuplicateSocketA should have failed\n");
2639 err = WSAGetLastError();
2640 ok(err == WSAEFAULT, "expected 10014, received %d\n", err);
2641
2642 /* test returned structure */
2643 memset(&info, 0, sizeof(info));
2644 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2645 "WSADuplicateSocketA should have worked\n");
2646
2647 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n",
2648 IPPROTO_TCP, info.iProtocol);
2649 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2650 AF_INET, info.iProtocol);
2651 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n",
2652 SOCK_STREAM, info.iSocketType);
2653
2654 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2655 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2656
2657 closesocket(dupsock);
2658 closesocket(source);
2659
2660 /* create a socket, bind it, duplicate it then send data on source and
2661 * receive in the duplicated socket */
2662 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2663 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2664
2665 memset(&info, 0, sizeof(info));
2666 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2667 "WSADuplicateSocketA should have worked\n");
2668
2669 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n",
2670 IPPROTO_UDP, info.iProtocol);
2671 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2672 AF_INET, info.iProtocol);
2673 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2674 SOCK_DGRAM, info.iSocketType);
2675
2676 memset(&addr, 0, sizeof(addr));
2677 addr.sin_family = AF_INET;
2678 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2679 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2680 "bind should have worked\n");
2681
2682 /* read address to find out the port number to be used in sendto */
2683 memset(&addr, 0, sizeof(addr));
2684 addrsize = sizeof(addr);
2685 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2686 "getsockname should have worked\n");
2687 ok(addr.sin_port, "socket port should be != 0\n");
2688
2689 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2690 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2691
2692 size = sizeof(int);
2693 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2694 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
2695 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2696 SOCK_DGRAM, socktype);
2697
2698 set_blocking(source, TRUE);
2699
2700 /* send data on source socket */
2701 addrsize = sizeof(addr);
2702 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2703 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2704
2705 /* receive on duplicated socket */
2706 addrsize = sizeof(addr);
2707 memset(buffer, 0, sizeof(buffer));
2708 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize);
2709 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2710 buffer[sizeof(teststr) - 1] = 0;
2711 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer);
2712
2713 closesocket(dupsock);
2714 closesocket(source);
2715
2716 /* show that the source socket need to be bound before the duplicated
2717 * socket is created */
2718 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2719 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2720
2721 memset(&info, 0, sizeof(info));
2722 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2723 "WSADuplicateSocketA should have worked\n");
2724
2725 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2726 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2727
2728 memset(&addr, 0, sizeof(addr));
2729 addr.sin_family = AF_INET;
2730 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2731 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2732 "bind should have worked\n");
2733
2734 /* read address to find out the port number to be used in sendto */
2735 memset(&addr, 0, sizeof(addr));
2736 addrsize = sizeof(addr);