ReactOS 0.4.16-dev-122-g325d74c
sock.c
Go to the documentation of this file.
1/*
2 * Unit test suite for winsock functions
3 *
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
7 * Copyright 2017 Dmitry Timoshkov
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifdef __REACTOS__
25#undef _WIN32_WINNT
26#define _WIN32_WINNT 0x0600
27#endif
28
29#include <ntstatus.h>
30#define WIN32_NO_STATUS
31#include <winsock2.h>
32#include <windows.h>
33#include <winternl.h>
34#include <ws2tcpip.h>
35#include <wsipx.h>
36#include <wsnwlink.h>
37#include <mswsock.h>
38#include <mstcpip.h>
39#include <iphlpapi.h>
40#include <stdio.h>
41#include "wine/test.h"
42
43// ReactOS: Wine has this in mstcpip.h, but it doesn't belong there
44#define WSA_CMSG_ALIGN(len) (((len) + sizeof(SIZE_T) - 1) & ~(sizeof(SIZE_T) - 1))
45
46#define MAX_CLIENTS 4 /* Max number of clients */
47#define FIRST_CHAR 'A' /* First character in transferred pattern */
48#define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
49#define BIND_TRIES 6 /* Number of bind() attempts */
50#define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
51 after server initialization, if something hangs */
53#define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
55#define NUM_THREADS 3 /* Number of threads to run getservbyname */
56#define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
58#define SERVERIP "127.0.0.1" /* IP to bind to */
59#define SERVERPORT 9374 /* Port number to bind to */
61#define wsa_ok(op, cond, msg) \
62 do { \
63 int tmp, err = 0; \
64 tmp = op; \
65 if ( !(cond tmp) ) err = WSAGetLastError(); \
66 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
67 } while (0);
69#define make_keepalive(k, enable, time, interval) \
70 k.onoff = enable; \
71 k.keepalivetime = time; \
72 k.keepaliveinterval = interval;
73
74/* Function pointers */
75static void (WINAPI *pfreeaddrinfo)(struct addrinfo *);
76static int (WINAPI *pgetaddrinfo)(LPCSTR,LPCSTR,const struct addrinfo *,struct addrinfo **);
77static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW);
78static void (WINAPI *pFreeAddrInfoExW)(ADDRINFOEXW *ai);
79static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *);
80static int (WINAPI *pGetAddrInfoExW)(const WCHAR *name, const WCHAR *servname, DWORD namespace,
81 GUID *namespace_id, const ADDRINFOEXW *hints, ADDRINFOEXW **result,
83 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine, HANDLE *handle);
84static int (WINAPI *pGetAddrInfoExOverlappedResult)(OVERLAPPED *overlapped);
85static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG);
86static PCWSTR(WINAPI *pInetNtopW)(INT,LPVOID,LPWSTR,ULONG);
87static int (WINAPI *pInetPtonA)(INT,LPCSTR,LPVOID);
88static int (WINAPI *pInetPtonW)(INT,LPWSTR,LPVOID);
89static int (WINAPI *pWSALookupServiceBeginW)(LPWSAQUERYSETW,DWORD,LPHANDLE);
90static int (WINAPI *pWSALookupServiceEnd)(HANDLE);
91static int (WINAPI *pWSALookupServiceNextW)(HANDLE,DWORD,LPDWORD,LPWSAQUERYSETW);
92static int (WINAPI *pWSAEnumNameSpaceProvidersA)(LPDWORD,LPWSANAMESPACE_INFOA);
93static int (WINAPI *pWSAEnumNameSpaceProvidersW)(LPDWORD,LPWSANAMESPACE_INFOW);
94static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT);
95
96/* Function pointers from ntdll */
97static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
98static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
99
100/* Function pointers from iphlpapi */
101static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG);
102static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL);
103
104/* Function pointers from ntdll */
105static DWORD (WINAPI *pNtClose)(HANDLE);
106
107/**************** Structs and typedefs ***************/
108
109typedef struct thread_info
114
115/* Information in the server about open client connections */
116typedef struct sock_info
121 char *buf;
124} sock_info;
125
126/* Test parameters for both server & client */
127typedef struct test_params
131 const char *inet_addr;
137
138/* server-specific test parameters */
139typedef struct server_params
145
146/* client-specific test parameters */
147typedef struct client_params
153
154/* This type combines all information for setting up a test scenario */
155typedef struct test_setup
162} test_setup;
163
164/* Thread local storage for server */
165typedef struct server_memory
171
172/* Thread local storage for client */
173typedef struct client_memory
177 char *send_buf;
178 char *recv_buf;
180
181/* SelectReadThread thread parameters */
182typedef struct select_thread_params
187
188/* Tests used in both getaddrinfo and GetAddrInfoW */
189static const struct addr_hint_tests
192 DWORD error;
193} hinttests[] = {
196 {AF_UNSPEC, SOCK_STREAM, IPPROTO_IPV6,0 },
199 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_IPV6,0 },
202 {AF_INET, SOCK_STREAM, IPPROTO_IPV6,0 },
205 {AF_INET, SOCK_DGRAM, IPPROTO_IPV6,0 },
206 {AF_UNSPEC, 0, IPPROTO_TCP, 0 },
207 {AF_UNSPEC, 0, IPPROTO_UDP, 0 },
208 {AF_UNSPEC, 0, IPPROTO_IPV6,0 },
209 {AF_UNSPEC, SOCK_STREAM, 0, 0 },
210 {AF_UNSPEC, SOCK_DGRAM, 0, 0 },
211 {AF_INET, 0, IPPROTO_TCP, 0 },
212 {AF_INET, 0, IPPROTO_UDP, 0 },
213 {AF_INET, 0, IPPROTO_IPV6,0 },
214 {AF_INET, SOCK_STREAM, 0, 0 },
215 {AF_INET, SOCK_DGRAM, 0, 0 },
218 {AF_UNSPEC, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
221 {AF_INET, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT },
222 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
223 {AF_UNSPEC, SOCK_STREAM, 999, 0 },
224 {AF_INET, SOCK_DGRAM, 999, 0 },
225 {AF_INET, SOCK_DGRAM, 999, 0 },
226};
227
228/**************** Static variables ***************/
230static DWORD tls; /* Thread local storage index */
235static int client_id;
236
237/**************** General utility functions ***************/
238
239static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len);
242static int tcp_socketpair(SOCKET *src, SOCKET *dst)
243{
245 struct sockaddr_in addr;
246 int len;
247 int ret;
248
251
253 if (*src == INVALID_SOCKET)
254 goto end;
255
257 if (server == INVALID_SOCKET)
258 goto end;
259
260 memset(&addr, 0, sizeof(addr));
261 addr.sin_family = AF_INET;
262 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
263 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
264 if (ret != 0)
265 goto end;
266
267 len = sizeof(addr);
268 ret = getsockname(server, (struct sockaddr*)&addr, &len);
269 if (ret != 0)
270 goto end;
271
272 ret = listen(server, 1);
273 if (ret != 0)
274 goto end;
275
276 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
277 if (ret != 0)
278 goto end;
279
280 len = sizeof(addr);
281 *dst = accept(server, (struct sockaddr*)&addr, &len);
282
283end:
284 if (server != INVALID_SOCKET)
286 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
287 return 0;
290 return -1;
291}
294{
296 struct sockaddr_in addr;
297 int len, ret;
298
301
303 if (*src == INVALID_SOCKET)
304 goto end;
305
307 if (server == INVALID_SOCKET)
308 goto end;
309
310 memset(&addr, 0, sizeof(addr));
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
313 ret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
314 if (ret != 0)
315 goto end;
316
317 len = sizeof(addr);
318 ret = getsockname(server, (struct sockaddr *)&addr, &len);
319 if (ret != 0)
320 goto end;
321
322 ret = listen(server, 1);
323 if (ret != 0)
324 goto end;
325
326 ret = connect(*src, (struct sockaddr *)&addr, sizeof(addr));
327 if (ret != 0)
328 goto end;
329
330 len = sizeof(addr);
331 *dst = accept(server, (struct sockaddr *)&addr, &len);
332
333end:
334 if (server != INVALID_SOCKET)
336 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
337 return 0;
340 return -1;
341}
343static void set_so_opentype ( BOOL overlapped )
344{
345 int optval = !overlapped, newval, len = sizeof (int);
346
348 (LPVOID) &optval, sizeof (optval) ) == 0,
349 "setting SO_OPENTYPE failed\n" );
351 (LPVOID) &newval, &len ) == 0,
352 "getting SO_OPENTYPE failed\n" );
353 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
354}
356static int set_blocking ( SOCKET s, BOOL blocking )
357{
358 u_long val = !blocking;
359 return ioctlsocket ( s, FIONBIO, &val );
360}
362static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
363{
364 char c, *p;
365 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
366 memset ( p, c, chunk_size );
367}
369static int test_buffer ( char *buf, int chunk_size, int n_chunks )
370{
371 char c, *p;
372 int i;
373 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
374 {
375 for ( i = 0; i < chunk_size; i++ )
376 if ( p[i] != c ) return i;
377 }
378 return -1;
379}
380
381/*
382 * This routine is called when a client / server does not expect any more data,
383 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
384 */
385static void read_zero_bytes ( SOCKET s )
386{
387 char buf[256];
388 int tmp, n = 0;
389 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
390 n += tmp;
391 ok ( n <= 0, "garbage data received: %d bytes\n", n );
392}
394static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen )
395{
396 char* last = buf + buflen, *p;
397 int n = 1;
398 for ( p = buf; n > 0 && p < last; )
399 {
400 n = send ( s, p, min ( sendlen, last - p ), flags );
401 if (n > 0) p += n;
402 }
403 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
404 return p - buf;
405}
407static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen )
408{
409 char* last = buf + buflen, *p;
410 int n = 1;
411 for ( p = buf; n > 0 && p < last; )
412 {
413 n = recv ( s, p, min ( recvlen, last - p ), flags );
414 if (n > 0) p += n;
415 }
416 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
417 return p - buf;
418}
420static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen )
421{
422 char* last = buf + buflen, *p;
423 int n = 1;
424 for ( p = buf; n > 0 && p < last; )
425 {
426 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen );
427 if (n > 0) p += n;
428 }
429 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
430 return p - buf;
431}
432
433/*
434 * Call this routine right after thread startup.
435 * SO_OPENTYPE must by 0, regardless what the server did.
436 */
437static void check_so_opentype (void)
438{
439 int tmp = 1, len;
440 len = sizeof (tmp);
442 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
443}
445static void compare_addrinfo (ADDRINFO *a, ADDRINFO *b)
446{
447 for (; a && b ; a = a->ai_next, b = b->ai_next)
448 {
449 ok(a->ai_flags == b->ai_flags,
450 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
451 ok(a->ai_family == b->ai_family,
452 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
453 ok(a->ai_socktype == b->ai_socktype,
454 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
455 ok(a->ai_protocol == b->ai_protocol,
456 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
457 ok(a->ai_addrlen == b->ai_addrlen,
458 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
459 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
460 "Wrong address data\n");
461 if (a->ai_canonname && b->ai_canonname)
462 {
463 ok(!strcmp(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
464 a->ai_canonname, b->ai_canonname);
465 }
466 else
467 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
468 a->ai_canonname, b->ai_canonname);
469 }
470 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
471}
473static void compare_addrinfow (ADDRINFOW *a, ADDRINFOW *b)
474{
475 for (; a && b ; a = a->ai_next, b = b->ai_next)
476 {
477 ok(a->ai_flags == b->ai_flags,
478 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags);
479 ok(a->ai_family == b->ai_family,
480 "Wrong family %d != %d\n", a->ai_family, b->ai_family);
481 ok(a->ai_socktype == b->ai_socktype,
482 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype);
483 ok(a->ai_protocol == b->ai_protocol,
484 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol);
485 ok(a->ai_addrlen == b->ai_addrlen,
486 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen);
487 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)),
488 "Wrong address data\n");
489 if (a->ai_canonname && b->ai_canonname)
490 {
491 ok(!lstrcmpW(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n",
492 wine_dbgstr_w(a->ai_canonname), wine_dbgstr_w(b->ai_canonname));
493 }
494 else
495 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n",
496 a->ai_canonname, b->ai_canonname);
497 }
498 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b);
499}
500
501/**************** Server utility functions ***************/
502
503/*
504 * Even if we have closed our server socket cleanly,
505 * the OS may mark the address "in use" for some time -
506 * this happens with native Linux apps, too.
507 */
508static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
509{
510 int err, wsaerr = 0, n_try = BIND_TRIES;
511
512 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
513 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
514 n_try-- >= 0)
515 {
516 trace ( "address in use, waiting ...\n" );
517 Sleep ( 1000 * BIND_SLEEP );
518 }
519 ok ( err == 0, "failed to bind: %d\n", wsaerr );
520}
522static void server_start ( server_params *par )
523{
524 int i;
525 test_params *gen = par->general;
526 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
527
528 TlsSetValue ( tls, mem );
529 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
530 NULL, 0, par->sock_flags );
531 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
532
533 mem->addr.sin_family = AF_INET;
534 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
535 mem->addr.sin_port = htons ( gen->inet_port );
536
537 for (i = 0; i < MAX_CLIENTS; i++)
538 {
539 mem->sock[i].s = INVALID_SOCKET;
540 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
541 mem->sock[i].n_recvd = 0;
542 mem->sock[i].n_sent = 0;
543 }
544
545 if ( gen->sock_type == SOCK_STREAM )
546 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
547}
549static void server_stop (void)
550{
551 int i;
553
554 for (i = 0; i < MAX_CLIENTS; i++ )
555 {
556 LocalFree ( mem->sock[i].buf );
557 if ( mem->sock[i].s != INVALID_SOCKET )
558 closesocket ( mem->sock[i].s );
559 }
560 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
561 LocalFree ( mem );
563}
564
565/**************** Client utilitiy functions ***************/
567static void client_start ( client_params *par )
568{
569 test_params *gen = par->general;
571
572 TlsSetValue ( tls, mem );
573
575
576 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
577 NULL, 0, par->sock_flags );
578
579 mem->addr.sin_family = AF_INET;
580 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
581 mem->addr.sin_port = htons ( gen->inet_port );
582
583 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
584
585 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
586 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
587 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
588
590 /* Wait for the other clients to come up */
592}
594static void client_stop (void)
595{
597 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
598 LocalFree ( mem->send_buf );
599 LocalFree ( mem );
600 ExitThread(0);
601}
602
603/**************** Servers ***************/
604
605/*
606 * simple_server: A very basic server doing synchronous IO.
607 */
609{
610 test_params *gen = par->general;
612 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
613 id = GetCurrentThreadId();
614
615 trace ( "simple_server (%x) starting\n", id );
616
617 set_so_opentype ( FALSE ); /* non-overlapped */
618 server_start ( par );
619 mem = TlsGetValue ( tls );
620
621 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
622 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
623
624 trace ( "simple_server (%x) ready\n", id );
625 SetEvent ( server_ready ); /* notify clients */
626
627 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
628 {
629 trace ( "simple_server (%x): waiting for client\n", id );
630
631 /* accept a single connection */
632 tmp = sizeof ( mem->sock[0].peer );
633 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
634 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
635
636 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
637 "simple_server (%x): strange peer address\n", id );
638
639 /* Receive data & check it */
640 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
641 ok ( n_recvd == n_expected,
642 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
643 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
644 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
645
646 /* Echo data back */
647 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
648 ok ( n_sent == n_expected,
649 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
650
651 /* cleanup */
652 read_zero_bytes ( mem->sock[0].s );
653 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
654 mem->sock[0].s = INVALID_SOCKET;
655 }
656
657 trace ( "simple_server (%x) exiting\n", id );
658 server_stop ();
659}
660
661/*
662 * oob_server: A very basic server receiving out-of-band data.
663 */
664static VOID WINAPI oob_server ( server_params *par )
665{
666 test_params *gen = par->general;
668 u_long atmark = 0;
669 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
670 id = GetCurrentThreadId();
671
672 trace ( "oob_server (%x) starting\n", id );
673
674 set_so_opentype ( FALSE ); /* non-overlapped */
675 server_start ( par );
676 mem = TlsGetValue ( tls );
677
678 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
679 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
680
681 trace ( "oob_server (%x) ready\n", id );
682 SetEvent ( server_ready ); /* notify clients */
683
684 trace ( "oob_server (%x): waiting for client\n", id );
685
686 /* accept a single connection */
687 tmp = sizeof ( mem->sock[0].peer );
688 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
689 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
690
691 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
692 "oob_server (%x): strange peer address\n", id );
693
694 /* check initial atmark state */
695 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
696 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
697
698 /* Receive normal data */
699 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
700 ok ( n_recvd == n_expected,
701 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
702 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
703 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos);
704
705 /* check atmark state */
706 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
707 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
708
709 /* Echo data back */
710 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
711 ok ( n_sent == n_expected,
712 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
713
714 /* Receive a part of the out-of-band data and print atmark state */
715 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen );
716 ok ( n_recvd == 8,
717 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
718 n_expected -= 8;
719
720 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
721 trace( "oob_server (%x): %s the OOB mark: %i\n", id, atmark == 1 ? "not at" : "at", atmark );
722
723 /* Receive the rest of the out-of-band data and check atmark state */
724 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
725
726 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
727 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
728
729 /* cleanup */
730 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
731 mem->sock[0].s = INVALID_SOCKET;
732
733 trace ( "oob_server (%x) exiting\n", id );
734 server_stop ();
735}
736
737/*
738 * select_server: A non-blocking server.
739 */
741{
742 test_params *gen = par->general;
744 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
745 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
746 n_set, delta, n_ready;
747 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
748 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
749
750 trace ( "select_server (%x) starting\n", id );
751
752 set_so_opentype ( FALSE ); /* non-overlapped */
753 server_start ( par );
754 mem = TlsGetValue ( tls );
755
756 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
757 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
758
759 trace ( "select_server (%x) ready\n", id );
760 SetEvent ( server_ready ); /* notify clients */
761
762 FD_ZERO ( &fds_openrecv );
763 FD_ZERO ( &fds_recv );
764 FD_ZERO ( &fds_send );
765 FD_ZERO ( &fds_opensend );
766
767 FD_SET ( mem->s, &fds_openrecv );
768
769 while(1)
770 {
771 fds_recv = fds_openrecv;
772 fds_send = fds_opensend;
773
774 n_set = 0;
775
776 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
777 "select_server (%x): select() failed: %d\n" );
778
779 /* check for incoming requests */
780 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
781 n_set += 1;
782
783 trace ( "select_server (%x): accepting client connection\n", id );
784
785 /* accept a single connection */
786 tmp = sizeof ( mem->sock[n_connections].peer );
787 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
788 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
789
790 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
791 "select_server (%x): strange peer address\n", id );
792
793 /* add to list of open connections */
794 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
795 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
796
797 n_connections++;
798 }
799
800 /* handle open requests */
801
802 for ( i = 0; i < n_connections; i++ )
803 {
804 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
805 n_set += 1;
806
807 if ( mem->sock[i].n_recvd < n_expected ) {
808 /* Receive data & check it */
809 n_recvd = recv ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_recvd, min ( n_expected - mem->sock[i].n_recvd, par->buflen ), 0 );
810 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
811 mem->sock[i].n_recvd += n_recvd;
812
813 if ( mem->sock[i].n_recvd == n_expected ) {
814 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
815 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
816 FD_CLR ( mem->sock[i].s, &fds_openrecv );
817 }
818
819 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
820 }
821 }
822
823 /* only echo back what we've received */
824 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
825
826 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
827 n_set += 1;
828
829 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
830 /* Echo data back */
831 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
832 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
833 mem->sock[i].n_sent += n_sent;
834
835 if ( mem->sock[i].n_sent == n_expected ) {
836 FD_CLR ( mem->sock[i].s, &fds_opensend );
837 }
838
839 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
840 }
841 }
842 }
843
844 /* check that select returned the correct number of ready sockets */
845 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
846
847 /* check if all clients are done */
848 if ( ( fds_opensend.fd_count == 0 )
849 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
850 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
851 break;
852 }
853 }
854
855 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
856 {
857 /* cleanup */
858 read_zero_bytes ( mem->sock[i].s );
859 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
860 mem->sock[i].s = INVALID_SOCKET;
861 }
862
863 trace ( "select_server (%x) exiting\n", id );
864 server_stop ();
865}
866
867/**************** Clients ***************/
868
869/*
870 * simple_client: A very basic client doing synchronous IO.
871 */
873{
874 test_params *gen = par->general;
876 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
877
878 id = GetCurrentThreadId();
879 trace ( "simple_client (%x): starting\n", id );
880 /* wait here because we want to call set_so_opentype before creating a socket */
882 trace ( "simple_client (%x): server ready\n", id );
883
885 set_so_opentype ( FALSE ); /* non-overlapped */
886 client_start ( par );
887 mem = TlsGetValue ( tls );
888
889 /* Connect */
890 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
891 0 ==, "simple_client (%x): connect error: %d\n" );
892 ok ( set_blocking ( mem->s, TRUE ) == 0,
893 "simple_client (%x): failed to set blocking mode\n", id );
894 trace ( "simple_client (%x) connected\n", id );
895
896 /* send data to server */
897 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
898 ok ( n_sent == n_expected,
899 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
900
901 /* shutdown send direction */
902 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
903
904 /* Receive data echoed back & check it */
905 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
906 ok ( n_recvd == n_expected,
907 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
908
909 /* check data */
910 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
911 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
912
913 /* cleanup */
914 read_zero_bytes ( mem->s );
915 trace ( "simple_client (%x) exiting\n", id );
916 client_stop ();
917}
918
919/*
920 * oob_client: A very basic client sending out-of-band data.
921 */
922static VOID WINAPI oob_client ( client_params *par )
923{
924 test_params *gen = par->general;
926 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
927
928 id = GetCurrentThreadId();
929 trace ( "oob_client (%x): starting\n", id );
930 /* wait here because we want to call set_so_opentype before creating a socket */
932 trace ( "oob_client (%x): server ready\n", id );
933
935 set_so_opentype ( FALSE ); /* non-overlapped */
936 client_start ( par );
937 mem = TlsGetValue ( tls );
938
939 /* Connect */
940 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
941 0 ==, "oob_client (%x): connect error: %d\n" );
942 ok ( set_blocking ( mem->s, TRUE ) == 0,
943 "oob_client (%x): failed to set blocking mode\n", id );
944 trace ( "oob_client (%x) connected\n", id );
945
946 /* send data to server */
947 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
948 ok ( n_sent == n_expected,
949 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
950
951 /* Receive data echoed back & check it */
952 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
953 ok ( n_recvd == n_expected,
954 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
955 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
956 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
957
958 /* send out-of-band data to server */
959 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen );
960 ok ( n_sent == n_expected,
961 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
962
963 /* shutdown send direction */
964 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
965
966 /* cleanup */
967 read_zero_bytes ( mem->s );
968 trace ( "oob_client (%x) exiting\n", id );
969 client_stop ();
970}
971
972/*
973 * simple_mixed_client: mixing send and recvfrom
974 */
976{
977 test_params *gen = par->general;
979 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
980 int fromLen = sizeof(mem->addr);
981 struct sockaddr test;
982
983 id = GetCurrentThreadId();
984 trace ( "simple_client (%x): starting\n", id );
985 /* wait here because we want to call set_so_opentype before creating a socket */
987 trace ( "simple_client (%x): server ready\n", id );
988
990 set_so_opentype ( FALSE ); /* non-overlapped */
991 client_start ( par );
992 mem = TlsGetValue ( tls );
993
994 /* Connect */
995 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
996 0 ==, "simple_client (%x): connect error: %d\n" );
997 ok ( set_blocking ( mem->s, TRUE ) == 0,
998 "simple_client (%x): failed to set blocking mode\n", id );
999 trace ( "simple_client (%x) connected\n", id );
1000
1001 /* send data to server */
1002 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
1003 ok ( n_sent == n_expected,
1004 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
1005
1006 /* shutdown send direction */
1007 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
1008
1009 /* this shouldn't change, since lpFrom, is not updated on
1010 connection oriented sockets - exposed by bug 11640
1011 */
1012 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
1013
1014 /* Receive data echoed back & check it */
1015 n_recvd = do_synchronous_recvfrom ( mem->s,
1016 mem->recv_buf,
1017 n_expected,
1018 0,
1019 (struct sockaddr *)&test,
1020 &fromLen,
1021 par->buflen );
1022 ok ( n_recvd == n_expected,
1023 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
1024
1025 /* check that lpFrom was not updated */
1026 ok(0 ==
1027 strcmp(
1028 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
1029 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
1030
1031 /* check data */
1032 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1033 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
1034
1035 /* cleanup */
1036 read_zero_bytes ( mem->s );
1037 trace ( "simple_client (%x) exiting\n", id );
1038 client_stop ();
1039}
1040
1041/*
1042 * event_client: An event-driven client
1044static void WINAPI event_client ( client_params *par )
1045{
1046 test_params *gen = par->general;
1048 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
1049 tmp, err, n;
1050 HANDLE event;
1051 WSANETWORKEVENTS wsa_events;
1052 char *send_last, *recv_last, *send_p, *recv_p;
1054
1055 trace ( "event_client (%x): starting\n", id );
1056 client_start ( par );
1057 trace ( "event_client (%x): server ready\n", id );
1058
1059 mem = TlsGetValue ( tls );
1060
1061 /* Prepare event notification for connect, makes socket nonblocking */
1062 event = WSACreateEvent ();
1064 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
1065 if ( tmp != 0 ) {
1066 err = WSAGetLastError ();
1067 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
1069 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
1070 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1071 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1072 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
1073 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
1074 if ( err ) goto out;
1075 }
1076
1077 trace ( "event_client (%x) connected\n", id );
1078
1079 WSAEventSelect ( mem->s, event, mask );
1080
1081 recv_p = mem->recv_buf;
1082 recv_last = mem->recv_buf + n_expected;
1083 send_p = mem->send_buf;
1084 send_last = mem->send_buf + n_expected;
1085
1086 while ( TRUE )
1087 {
1089 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
1090
1091 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
1092 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
1093
1094 if ( wsa_events.lNetworkEvents & FD_WRITE )
1095 {
1096 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
1097 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
1098
1099 if ( err== 0 )
1100 do
1101 {
1102 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
1103 if ( n < 0 )
1104 {
1105 err = WSAGetLastError ();
1106 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
1107 }
1108 else
1109 send_p += n;
1110 }
1111 while ( n >= 0 && send_p < send_last );
1112
1113 if ( send_p == send_last )
1114 {
1115 trace ( "event_client (%x): all data sent - shutdown\n", id );
1116 shutdown ( mem->s, SD_SEND );
1117 mask &= ~FD_WRITE;
1118 WSAEventSelect ( mem->s, event, mask );
1119 }
1120 }
1121 if ( wsa_events.lNetworkEvents & FD_READ )
1122 {
1123 err = wsa_events.iErrorCode[ FD_READ_BIT ];
1124 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
1125 if ( err != 0 ) break;
1126
1127 /* First read must succeed */
1128 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1129 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
1130
1131 while ( n >= 0 ) {
1132 recv_p += n;
1133 if ( recv_p == recv_last )
1134 {
1135 mask &= ~FD_READ;
1136 trace ( "event_client (%x): all data received\n", id );
1137 WSAEventSelect ( mem->s, event, mask );
1138 break;
1139 }
1140 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
1141 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
1142 ok ( 0, "event_client (%x): read error: %d\n", id, err );
1143
1144 }
1145 }
1146 if ( wsa_events.lNetworkEvents & FD_CLOSE )
1147 {
1148 trace ( "event_client (%x): close event\n", id );
1149 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
1150 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
1151 break;
1152 }
1153 }
1154
1155 n = send_p - mem->send_buf;
1156 ok ( send_p == send_last,
1157 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
1158 n = recv_p - mem->recv_buf;
1159 ok ( recv_p == recv_last,
1160 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
1161 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
1162 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
1163
1164out:
1165 WSACloseEvent ( event );
1166 trace ( "event_client (%x) exiting\n", id );
1167 client_stop ();
1168}
1169
1170/* Tests for WSAStartup */
1171static void test_WithoutWSAStartup(void)
1172{
1173 DWORD err;
1174
1175 WSASetLastError(0xdeadbeef);
1176 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n");
1177 err = WSAGetLastError();
1178 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1179
1180 WSASetLastError(0xdeadbeef);
1181 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n");
1182 err = WSAGetLastError();
1183 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
1184}
1186static void test_WithWSAStartup(void)
1187{
1188 WSADATA data;
1189 WORD version = MAKEWORD( 2, 2 );
1190 INT res, socks, i, j;
1191 SOCKET sock;
1192 LPVOID ptr;
1193 struct
1194 {
1195 SOCKET src, dst, dup_src, dup_dst;
1196 } pairs[32];
1197 DWORD error;
1198
1199 res = WSAStartup( version, &data );
1200 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1201
1202 ptr = gethostbyname("localhost");
1203 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
1204
1205 /* Alloc some sockets to check if they are destroyed on WSACleanup */
1206 for (socks = 0; socks < sizeof(pairs) / sizeof(pairs[0]); socks++)
1207 {
1209 if (tcp_socketpair(&pairs[socks].src, &pairs[socks].dst)) break;
1210
1211 memset(&info, 0, sizeof(info));
1213 "WSADuplicateSocketA should have worked\n");
1214 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0);
1215 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n");
1216
1217 memset(&info, 0, sizeof(info));
1219 "WSADuplicateSocketA should have worked\n");
1220 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0);
1221 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n");
1222 }
1223
1224 res = send(pairs[0].src, "TEST", 4, 0);
1225 ok(res == 4, "send failed with error %d\n", WSAGetLastError());
1226
1227 WSACleanup();
1228
1229 res = WSAStartup( version, &data );
1230 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
1231
1232 /* show that sockets are destroyed automatically after WSACleanup */
1233 SetLastError(0xdeadbeef);
1234 res = send(pairs[0].src, "TEST", 4, 0);
1236 ok(res == SOCKET_ERROR, "send should have failed\n");
1237 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1238
1239 SetLastError(0xdeadbeef);
1240 res = send(pairs[0].dst, "TEST", 4, 0);
1242 ok(res == SOCKET_ERROR, "send should have failed\n");
1243 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1244
1245 /* Check that all sockets were destroyed */
1246 for (i = 0; i < socks; i++)
1247 {
1248 for (j = 0; j < 4; j++)
1249 {
1250 struct sockaddr_in saddr;
1251 int size = sizeof(saddr);
1252 switch(j)
1253 {
1254 case 0: sock = pairs[i].src; break;
1255 case 1: sock = pairs[i].dup_src; break;
1256 case 2: sock = pairs[i].dst; break;
1257 case 3: sock = pairs[i].dup_dst; break;
1258 }
1259
1260 SetLastError(0xdeadbeef);
1261 res = getsockname(sock, (struct sockaddr *)&saddr, &size);
1263 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i);
1264 todo_wine ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error);
1265 }
1266 }
1267
1268 /* While wine is not fixed, close all sockets manually */
1269 for (i = 0; i < socks; i++)
1270 {
1271 closesocket(pairs[i].src);
1272 closesocket(pairs[i].dst);
1273 closesocket(pairs[i].dup_src);
1274 closesocket(pairs[i].dup_dst);
1275 }
1276
1277 res = WSACleanup();
1278 ok(res == 0, "expected 0, got %d\n", res);
1279 WSASetLastError(0xdeadbeef);
1280 res = WSACleanup();
1283 "WSACleanup returned %d WSAGetLastError is %d\n", res, error);
1284}
1285
1286/**************** Main program utility functions ***************/
1288static void Init (void)
1289{
1290 WORD ver = MAKEWORD (2, 2);
1291 WSADATA data;
1292 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), hiphlpapi, ntdll;
1293
1294 pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo");
1295 pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo");
1296 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
1297 pFreeAddrInfoExW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoExW");
1298 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
1299 pGetAddrInfoExW = (void *)GetProcAddress(hws2_32, "GetAddrInfoExW");
1300 pGetAddrInfoExOverlappedResult = (void *)GetProcAddress(hws2_32, "GetAddrInfoExOverlappedResult");
1301 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1302 pInetNtopW = (void *)GetProcAddress(hws2_32, "InetNtopW");
1303 pInetPtonA = (void *)GetProcAddress(hws2_32, "inet_pton");
1304 pInetPtonW = (void *)GetProcAddress(hws2_32, "InetPtonW");
1305 pWSALookupServiceBeginW = (void *)GetProcAddress(hws2_32, "WSALookupServiceBeginW");
1306 pWSALookupServiceEnd = (void *)GetProcAddress(hws2_32, "WSALookupServiceEnd");
1307 pWSALookupServiceNextW = (void *)GetProcAddress(hws2_32, "WSALookupServiceNextW");
1308 pWSAEnumNameSpaceProvidersA = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersA");
1309 pWSAEnumNameSpaceProvidersW = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersW");
1310 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll");
1311
1312 hiphlpapi = LoadLibraryA("iphlpapi.dll");
1313 if (hiphlpapi)
1314 {
1315 pGetIpForwardTable = (void *)GetProcAddress(hiphlpapi, "GetIpForwardTable");
1316 pGetAdaptersInfo = (void *)GetProcAddress(hiphlpapi, "GetAdaptersInfo");
1317 }
1318
1319 ntdll = LoadLibraryA("ntdll.dll");
1320 if (ntdll)
1321 {
1322 pNtClose = (void *)GetProcAddress(ntdll, "NtClose");
1323 pNtSetInformationFile = (void *)GetProcAddress(ntdll, "NtSetInformationFile");
1324 pNtQueryInformationFile = (void *)GetProcAddress(ntdll, "NtQueryInformationFile");
1325 }
1326
1327 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1328 tls = TlsAlloc();
1329}
1331static void Exit (void)
1332{
1333 INT ret, err;
1334 TlsFree ( tls );
1335 ret = WSACleanup();
1336 err = WSAGetLastError();
1337 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1338}
1341 test_params *general, server_params *par)
1342{
1343 par->general = general;
1344 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1345 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1346}
1349 test_params *general, client_params *par)
1350{
1351 int i;
1352 par->general = general;
1353 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1354 {
1355 client_id = i - 1;
1356 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1357 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1358 /* Make sure the client is up and running */
1360 };
1361}
1363static void do_test( test_setup *test )
1364{
1365 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1366 DWORD wait;
1367
1369 for (i = 0; i <= n; i++)
1371
1372 StartServer ( test->srv, &test->general, &test->srv_params );
1373 StartClients ( test->clt, &test->general, &test->clt_params );
1375
1376 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1377 ok ( wait <= WAIT_OBJECT_0 + n ,
1378 "some threads have not completed: %x\n", wait );
1379
1380 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
1381 {
1382 for (i = 0; i <= n; i++)
1383 {
1384 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
1385 {
1386 trace ("terminating thread %08x\n", thread_id[i]);
1387 TerminateThread ( thread [i], 0 );
1388 }
1389 }
1390 }
1392 for (i = 0; i <= n; i++)
1394}
1395
1396/********* some tests for getsockopt(setsockopt(X)) == X ***********/
1397/* optname = SO_LINGER */
1398static const LINGER linger_testvals[] = {
1399 {0,0},
1400 {0,73},
1401 {1,0},
1402 {5,189}
1403};
1404
1405/* optname = SO_RCVTIMEO, SOSNDTIMEO */
1406#define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1407 bug in the linux kernel (fixed in 2.6.8) */
1408#define SOCKTIMEOUT2 997000 /* 997 seconds */
1409
1410static void test_set_getsockopt(void)
1411{
1412 SOCKET s, s2;
1413 int i, err, lasterr;
1414 int timeout;
1415 LINGER lingval;
1416 int size;
1419 char providername[WSAPROTOCOL_LEN + 1];
1420 DWORD value;
1421 struct _prottest
1422 {
1423 int family, type, proto;
1424 } prottest[] = {
1429 };
1430 union _csspace
1431 {
1433 char space[128];
1434 } csinfoA, csinfoB;
1435
1436 s = socket(AF_INET, SOCK_STREAM, 0);
1437 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1438 if( s == INVALID_SOCKET) return;
1439 /* SO_RCVTIMEO */
1441 size = sizeof(timeout);
1442 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1443 if( !err)
1444 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1445 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1446 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1447
1448 timeout = 0;
1449 size = sizeof(timeout);
1451 if( !err)
1452 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1453 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1454 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1455
1456 /* SO_SNDTIMEO */
1457 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1458 size = sizeof(timeout);
1459 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1460 if( !err)
1461 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1462 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1463 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1464
1465 /* SO_SNDBUF */
1466 value = 4096;
1467 size = sizeof(value);
1468 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size);
1469 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1470 value = 0xdeadbeef;
1471 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size);
1472 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1473 ok( value == 4096, "expected 4096, got %u\n", value );
1474
1475 /* SO_RCVBUF */
1476 value = 4096;
1477 size = sizeof(value);
1478 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size);
1479 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1480 value = 0xdeadbeef;
1481 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size);
1482 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1483 ok( value == 4096, "expected 4096, got %u\n", value );
1484
1485 /* SO_LINGER */
1486 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1487 size = sizeof(lingval);
1488 lingval = linger_testvals[i];
1489 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1490 if( !err)
1491 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1492 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1493 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1494 (lingval.l_linger == linger_testvals[i].l_linger ||
1495 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1496 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1497 lingval.l_onoff, lingval.l_linger,
1499 }
1500
1501 size = sizeof(lingval);
1504 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1507 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1508
1509 size = sizeof(BOOL);
1512 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1515 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1516
1517 /* Test for erroneously passing a value instead of a pointer as optval */
1518 size = sizeof(char);
1519 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1520 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1521 "instead of failing.\n");
1522 lasterr = WSAGetLastError();
1523 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1524 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1525 lasterr, WSAEFAULT);
1526
1527 /* SO_RCVTIMEO with invalid values for level */
1528 size = sizeof(timeout);
1530 SetLastError(0xdeadbeef);
1531 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1533 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1534 err, WSAGetLastError());
1535
1537 SetLastError(0xdeadbeef);
1538 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1540 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1541 err, WSAGetLastError());
1542
1543 /* Test SO_ERROR set/get */
1544 SetLastError(0xdeadbeef);
1545 i = 1234;
1546 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size);
1548 ok( !err && !WSAGetLastError(),
1549 "got %d with %d (expected 0 with 0)\n",
1550 err, WSAGetLastError());
1551
1552 SetLastError(0xdeadbeef);
1553 i = 4321;
1554 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1556 ok( !err && !WSAGetLastError(),
1557 "got %d with %d (expected 0 with 0)\n",
1558 err, WSAGetLastError());
1560 ok (i == 1234, "got %d (expected 1234)\n", i);
1561
1562 /* Test invalid optlen */
1563 SetLastError(0xdeadbeef);
1564 size = 1;
1565 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1568 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1569 err, WSAGetLastError());
1570
1571 closesocket(s);
1572 /* Test with the closed socket */
1573 SetLastError(0xdeadbeef);
1574 size = sizeof(i);
1575 i = 1234;
1576 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1579 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1580 err, WSAGetLastError());
1581 ok (i == 1234, "expected 1234, got %d\n", i);
1582
1583 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1584 s = socket(AF_INET, SOCK_DGRAM, 0);
1585 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1586 size = sizeof(i);
1587 i = 0x0000000a;
1588 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size);
1589 if (!err)
1590 {
1591 for (i = 0; i < 4; i++)
1592 {
1593 int k, j;
1594 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1595 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1);
1596 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1597 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size);
1598 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1599 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i];
1600 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k);
1601 }
1602 }
1603 else
1604 win_skip("IP_MULTICAST_TTL is unsupported\n");
1605 closesocket(s);
1606
1607 /* test SO_PROTOCOL_INFOA invalid parameters */
1609 "getsockopt should have failed\n");
1610 err = WSAGetLastError();
1611 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1612 size = sizeof(WSAPROTOCOL_INFOA);
1614 "getsockopt should have failed\n");
1615 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1616 err = WSAGetLastError();
1617 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1620 "getsockopt should have failed\n");
1621 err = WSAGetLastError();
1622 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1624 "getsockopt should have failed\n");
1625 err = WSAGetLastError();
1626 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1628 "getsockopt should have failed\n");
1629 err = WSAGetLastError();
1630 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1631 size = sizeof(WSAPROTOCOL_INFOA) / 2;
1633 "getsockopt should have failed\n");
1634 err = WSAGetLastError();
1635 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1636 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1637 size = sizeof(WSAPROTOCOL_INFOA) * 2;
1639 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1640 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size);
1641
1642 closesocket(s);
1643
1644 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1645 for (i = 0; i < sizeof(prottest) / sizeof(prottest[0]); i++)
1646 {
1647 int k;
1648
1649 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto);
1650 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue;
1651
1652 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n",
1653 WSAGetLastError());
1654
1655 /* compare both A and W version */
1656 infoA.szProtocol[0] = 0;
1657 size = sizeof(WSAPROTOCOL_INFOA);
1659 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1660 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1661
1662 infoW.szProtocol[0] = 0;
1663 size = sizeof(WSAPROTOCOL_INFOW);
1665 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1666 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size);
1667
1668 trace("provider name '%s', family %d, type %d, proto %d\n",
1669 infoA.szProtocol, prottest[i].family, prottest[i].type, prottest[i].proto);
1670
1671 ok(infoA.szProtocol[0], "WSAPROTOCOL_INFOA was not filled\n");
1672 ok(infoW.szProtocol[0], "WSAPROTOCOL_INFOW was not filled\n");
1673
1674 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1,
1675 providername, sizeof(providername), NULL, NULL);
1676 ok(!strcmp(infoA.szProtocol,providername),
1677 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername);
1678
1680 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1681
1682 /* Remove IF when WSAEnumProtocols support IPV6 data */
1683 todo_wine_if (prottest[i].family == AF_INET6)
1684 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n",
1685 prottest[i].family, infoA.iAddressFamily);
1686 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n",
1687 prottest[i].type, infoA.iSocketType);
1688 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n",
1689 prottest[i].proto, infoA.iProtocol);
1690
1691 /* IP_HDRINCL is supported only on SOCK_RAW but passed to SOCK_DGRAM by Impossible Creatures */
1692 size = sizeof(i);
1693 k = 1;
1694 SetLastError(0xdeadbeef);
1695 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1696 if (err == -1) /* >= Vista */
1697 {
1698 todo_wine {
1699 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1700 k = 99;
1701 SetLastError(0xdeadbeef);
1702 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1703 ok(err == -1, "Expected -1, got %d\n", err);
1704 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1705 ok(k == 99, "Expected 99, got %d\n", k);
1706
1707 size = sizeof(k);
1708 k = 0;
1709 SetLastError(0xdeadbeef);
1710 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1711 }
1712 ok(err == -1, "Expected -1, got %d\n", err);
1713 todo_wine {
1714 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1715 k = 99;
1716 SetLastError(0xdeadbeef);
1717 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1718 ok(err == -1, "Expected -1, got %d\n", err);
1719 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1720 ok(k == 99, "Expected 99, got %d\n", k);
1721 }
1722 }
1723 else /* <= 2003 the tests differ between TCP and UDP, UDP silently accepts */
1724 {
1725 SetLastError(0xdeadbeef);
1726 k = 99;
1727 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1728 if (prottest[i].type == SOCK_DGRAM)
1729 {
1730 ok(err == 0, "Expected 0, got %d\n", err);
1731 ok(k == 1, "Expected 1, got %d\n", k);
1732 }
1733 else
1734 {
1735 /* contratry to what we could expect the function returns error but k is changed */
1736 ok(err == -1, "Expected -1, got %d\n", err);
1737 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1738 ok(k == 0, "Expected 0, got %d\n", k);
1739 }
1740
1741 k = 0;
1742 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1743 ok(err == 0, "Expected 0, got %d\n", err);
1744
1745 k = 99;
1746 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1747 if (prottest[i].type == SOCK_DGRAM)
1748 {
1749 ok(err == 0, "Expected 0, got %d\n", err);
1750 ok(k == 0, "Expected 0, got %d\n", k);
1751 }
1752 else
1753 {
1754 /* contratry to what we could expect the function returns error but k is changed */
1755 ok(err == -1, "Expected -1, got %d\n", err);
1756 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1757 ok(k == 0, "Expected 0, got %d\n", k);
1758 }
1759 }
1760
1761 closesocket(s);
1762 }
1763
1764 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1766 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1768 ok(s2 != INVALID_SOCKET, "Failed to create socket\n");
1769
1770 SetLastError(0xdeadbeef);
1771 size = sizeof(csinfoA);
1772 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1773 if (!err)
1774 {
1775 struct sockaddr_in saddr;
1776 memset(&saddr, 0, sizeof(saddr));
1777 saddr.sin_family = AF_INET;
1778 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1779
1780 /* Socket is not bound, no information provided */
1781 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1782 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1783 /* Socket is not connected, no information provided */
1784 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1785 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1786
1787 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr));
1788 ok(!err, "Expected 0, got %d\n", err);
1789 size = sizeof(csinfoA);
1790 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1791 ok(!err, "Expected 0, got %d\n", err);
1792
1793 /* Socket is bound */
1794 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n");
1795 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n");
1796 /* Socket is not connected, no information provided */
1797 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1798 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1799
1800 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1801 ok(!err, "Expected 0, got %d\n", err);
1802 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1803 ok(!err, "Expected 0, got %d\n", err);
1804 err = listen(s2, 1);
1805 ok(!err, "Expected 0, got %d\n", err);
1806 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr));
1807 ok(!err, "Expected 0, got %d\n", err);
1808 size = sizeof(saddr);
1809 err = accept(s2, (struct sockaddr*)&saddr, &size);
1810 ok(err != INVALID_SOCKET, "Failed to accept socket\n");
1811 closesocket(s2);
1812 s2 = err;
1813
1814 size = sizeof(csinfoA);
1815 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1816 ok(!err, "Expected 0, got %d\n", err);
1817 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size);
1818 ok(!err, "Expected 0, got %d\n", err);
1819 ok(size == sizeof(csinfoA), "Got %d\n", size);
1820 size = sizeof(saddr);
1821 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1822 csinfoA.cs.LocalAddr.iSockaddrLength);
1823 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1824 csinfoA.cs.RemoteAddr.iSockaddrLength);
1825 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size),
1826 "Expected matching addresses\n");
1827 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size),
1828 "Expected matching addresses\n");
1829 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1830 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1831 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1832 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1833
1834 err = getpeername(s, (struct sockaddr *)&saddr, &size);
1835 ok(!err, "Expected 0, got %d\n", err);
1836 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1837 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1838 err = getpeername(s2, (struct sockaddr *)&saddr, &size);
1839 ok(!err, "Expected 0, got %d\n", err);
1840 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1841 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1842 err = getsockname(s, (struct sockaddr *)&saddr, &size);
1843 ok(!err, "Expected 0, got %d\n", err);
1844 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1845 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1846 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1847 ok(!err, "Expected 0, got %d\n", err);
1848 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1849 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1850
1851 SetLastError(0xdeadbeef);
1852 size = sizeof(CSADDR_INFO);
1853 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1854 ok(err, "Expected non-zero\n");
1855 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size);
1856 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1857
1858 /* At least for IPv4 the size is exactly 56 bytes */
1859 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs);
1860 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1861 ok(!err, "Expected 0, got %d\n", err);
1862 size--;
1863 SetLastError(0xdeadbeef);
1864 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1865 ok(err, "Expected non-zero\n");
1866 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1867 }
1868 else
1869 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1870
1871 closesocket(s);
1872 closesocket(s2);
1873
1874 for (i = 0; i < 2; i++)
1875 {
1876 int family, level;
1877
1878 if (i)
1879 {
1880 family = AF_INET6;
1881 level = IPPROTO_IPV6;
1882 }
1883 else
1884 {
1885 family = AF_INET;
1886 level = IPPROTO_IP;
1887 }
1888
1889 s = socket(family, SOCK_DGRAM, 0);
1890 if (s == INVALID_SOCKET && i)
1891 {
1892 skip("IPv6 is not supported\n");
1893 break;
1894 }
1895 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1896
1897 size = sizeof(value);
1898 value = 0xdead;
1899 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1900 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1901 ok(value == 0, "Expected 0, got %d\n", value);
1902
1903 size = sizeof(value);
1904 value = 1;
1905 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1906 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1907
1908 value = 0xdead;
1909 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1910 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1911 ok(value == 1, "Expected 1, got %d\n", value);
1912
1913 size = sizeof(value);
1914 value = 0xdead;
1915 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1916 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1917
1918 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1919 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1920 ok(value == 1, "Expected 1, got %d\n", value);
1921
1922 closesocket(s);
1923 }
1925
1926static void test_so_reuseaddr(void)
1927{
1928 struct sockaddr_in saddr;
1929 SOCKET s1,s2;
1930 unsigned int rc,reuse;
1931 int size;
1932 DWORD err;
1933
1934 saddr.sin_family = AF_INET;
1935 saddr.sin_port = htons(SERVERPORT+1);
1936 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1937
1939 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1940 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1941 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1942
1944 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1945
1946 reuse=0x1234;
1947 size=sizeof(reuse);
1948 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1949 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1950
1951 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1952 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1953
1954 reuse = 1;
1955 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1956 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1957
1958 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1959 * a port immediately after closing another socket on that port, so
1960 * basically following the BSD socket semantics here. */
1961 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1962 if(rc==0)
1963 {
1964 int s3=socket(AF_INET, SOCK_STREAM, 0), s4;
1965 trace("<= Win XP behavior of SO_REUSEADDR\n");
1966
1967 /* If we could bind again in the same port this is Windows version <= XP.
1968 * Lets test if we can really connect to one of them. */
1971 rc = listen(s1, 1);
1972 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1973 rc = listen(s2, 1);
1974 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1975 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr));
1976 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError());
1977
1978 /* the delivery of the connection is random so we need to try on both sockets */
1979 size = sizeof(saddr);
1980 s4 = accept(s1, (struct sockaddr*)&saddr, &size);
1981 if(s4 == INVALID_SOCKET)
1982 s4 = accept(s2, (struct sockaddr*)&saddr, &size);
1983 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n");
1984
1985 closesocket(s1);
1986 closesocket(s3);
1987 closesocket(s4);
1988 }
1989 else
1990 {
1991 trace(">= Win 2003 behavior of SO_REUSEADDR\n");
1992 err = WSAGetLastError();
1993 ok(err==WSAEACCES, "expected 10013, got %d\n", err);
1994
1995 closesocket(s1);
1996 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1997 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1998 }
1999
2000 closesocket(s2);
2002
2003#define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
2004
2005static void test_ip_pktinfo(void)
2006{
2007 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
2008 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
2009 struct sockaddr_in s1addr, s2addr, s3addr;
2010 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
2011 LPFN_WSARECVMSG pWSARecvMsg = NULL;
2012 unsigned int rc, yes = 1;
2013 BOOL foundhdr;
2014 DWORD dwBytes, dwSize, dwFlags;
2015 socklen_t addrlen;
2016 WSACMSGHDR *cmsg;
2017 WSAOVERLAPPED ov;
2018 WSABUF iovec[1];
2019 SOCKET s1, s2;
2020 WSAMSG hdr;
2021 int i, err;
2022
2023 memset(&ov, 0, sizeof(ov));
2025 if (ov.hEvent == INVALID_HANDLE_VALUE)
2026 {
2027 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
2028 return;
2029 }
2030
2031 memset(&hdr, 0x00, sizeof(hdr));
2032 s1addr.sin_family = AF_INET;
2033 s1addr.sin_port = htons(0);
2034 /* Note: s1addr.sin_addr is set below */
2035 iovec[0].buf = recvbuf;
2036 iovec[0].len = sizeof(recvbuf);
2037 hdr.name = (struct sockaddr*)&s3addr;
2038 hdr.namelen = sizeof(s3addr);
2039 hdr.lpBuffers = &iovec[0];
2040 hdr.dwBufferCount = 1;
2041 hdr.Control.buf = pktbuf;
2042 /* Note: hdr.Control.len is set below */
2043 hdr.dwFlags = 0;
2044
2045 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++)
2046 {
2047 s1addr.sin_addr.s_addr = addresses[i];
2048
2049 /* Build "server" side socket */
2051 if (s1 == INVALID_SOCKET)
2052 {
2053 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
2054 goto cleanup;
2055 }
2056
2057 /* Obtain the WSARecvMsg function */
2058 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
2059 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
2060 if (!pWSARecvMsg)
2061 {
2062 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
2063 closesocket(s1);
2064 goto cleanup;
2065 }
2066
2067 /* Setup the server side socket */
2068 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
2069 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
2070 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
2071 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
2072
2073 /* Build "client" side socket */
2074 addrlen = sizeof(s2addr);
2075 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0)
2076 {
2077 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
2078 closesocket(s1);
2079 goto cleanup;
2080 }
2081 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
2083 if (s2 == INVALID_SOCKET)
2084 {
2085 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
2086 closesocket(s1);
2087 goto cleanup;
2088 }
2089
2090 /* Test an empty message header */
2091 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
2093 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
2094
2095 /*
2096 * Send a packet from the client to the server and test for specifying
2097 * a short control header.
2098 */
2099 SetLastError(0xdeadbeef);
2100 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2101 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2102 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2103 hdr.Control.len = 1;
2104 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
2106 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
2107 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
2108 hdr.dwFlags = 0; /* Reset flags */
2109
2110 /* Perform another short control header test, this time with an overlapped receive */
2111 hdr.Control.len = 1;
2112 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2114 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2115 SetLastError(0xdeadbeef);
2116 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2117 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2118 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
2120 {
2121 skip("Server side did not receive packet, some tests skipped.\n");
2122 closesocket(s2);
2123 closesocket(s1);
2124 continue;
2125 }
2126 dwFlags = 0;
2128 ok(dwFlags == 0,
2129 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
2130 ok(hdr.dwFlags == MSG_CTRUNC,
2131 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
2132 hdr.dwFlags = 0; /* Reset flags */
2133
2134 /*
2135 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
2136 * on the server end and check that the returned packet matches what was sent.
2137 */
2138 hdr.Control.len = sizeof(pktbuf);
2139 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
2141 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
2142 ok(hdr.Control.len == sizeof(pktbuf),
2143 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
2144 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
2145 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
2147 {
2148 skip("Server side did not receive packet, some tests skipped.\n");
2149 closesocket(s2);
2150 closesocket(s1);
2151 continue;
2152 }
2153 dwSize = 0;
2155 ok(dwSize == sizeof(msg),
2156 "WSARecvMsg() buffer length does not match transmitted data!\n");
2157 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
2158 "WSARecvMsg() buffer does not match transmitted data!\n");
2159 ok(hdr.Control.len == IP_PKTINFO_LEN,
2160 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
2161
2162 /* Test for the expected IP_PKTINFO return information. */
2163 foundhdr = FALSE;
2164 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
2165 {
2166 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
2167 {
2168 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
2169
2170 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
2171 foundhdr = TRUE;
2172 }
2173 }
2174 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
2175
2176 closesocket(s2);
2177 closesocket(s1);
2178 }
2179
2180cleanup:
2181 CloseHandle(ov.hEvent);
2182}
2183
2184/************* Array containing the tests to run **********/
2185
2186#define STD_STREAM_SOCKET \
2187 SOCK_STREAM, \
2188 0, \
2189 SERVERIP, \
2190 SERVERPORT
2191
2192static test_setup tests [] =
2193{
2194 /* Test 0: synchronous client and server */
2195 {
2196 {
2198 2048,
2199 16,
2200 2
2201 },
2203 {
2204 NULL,
2205 0,
2206 64
2207 },
2209 {
2210 NULL,
2211 0,
2212 128
2213 }
2214 },
2215 /* Test 1: event-driven client, synchronous server */
2216 {
2217 {
2219 2048,
2220 16,
2221 2
2222 },
2224 {
2225 NULL,
2226 0,
2227 64
2228 },
2230 {
2231 NULL,
2233 128
2234 }
2235 },
2236 /* Test 2: synchronous client, non-blocking server via select() */
2237 {
2238 {
2240 2048,
2241 16,
2242 2
2243 },
2245 {
2246 NULL,
2247 0,
2248 64
2249 },
2251 {
2252 NULL,
2253 0,
2254 128
2255 }
2256 },
2257 /* Test 3: OOB client, OOB server */
2258 {
2259 {
2261 128,
2262 16,
2263 1
2264 },
2265 oob_server,
2266 {
2267 NULL,
2268 0,
2269 128
2270 },
2271 oob_client,
2272 {
2273 NULL,
2274 0,
2275 128
2276 }
2277 },
2278 /* Test 4: synchronous mixed client and server */
2279 {
2280 {
2282 2048,
2283 16,
2284 2
2285 },
2287 {
2288 NULL,
2289 0,
2290 64
2291 },
2293 {
2294 NULL,
2295 0,
2296 128
2297 }
2298 }
2300
2301static void test_UDP(void)
2302{
2303 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2304 possible that this test fails due to dropped packets. */
2305
2306 /* peer 0 receives data from all other peers */
2308 char buf[16];
2309 int ss, i, n_recv, n_sent;
2310
2311 memset (buf,0,sizeof(buf));
2312 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
2313 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
2314
2315 peer[i].addr.sin_family = AF_INET;
2316 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
2317
2318 if ( i == 0 ) {
2319 peer[i].addr.sin_port = htons ( SERVERPORT );
2320 } else {
2321 peer[i].addr.sin_port = htons ( 0 );
2322 }
2323
2324 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
2325
2326 /* test getsockname() to get peer's port */
2327 ss = sizeof ( peer[i].addr );
2328 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
2329 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
2330 }
2331
2332 /* test getsockname() */
2333 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
2334
2335 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2336 /* send client's ip */
2337 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
2338 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
2339 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
2340 }
2341
2342 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2343 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
2344 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
2345 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
2346 }
2348
2349static DWORD WINAPI do_getservbyname( void *param )
2350{
2351 struct {
2352 const char *name;
2353 const char *proto;
2354 int port;
2355 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
2356
2357 HANDLE *starttest = param;
2358 int i, j;
2359 struct servent *pserv[2];
2360
2361 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT,
2362 "test_getservbyname: timeout waiting for start signal\n" );
2363
2364 /* ensure that necessary buffer resizes are completed */
2365 for ( j = 0; j < 2; j++) {
2366 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2367 }
2368
2369 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
2370 for ( j = 0; j < 2; j++ ) {
2371 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
2372 ok ( pserv[j] != NULL || broken(pserv[j] == NULL) /* win8, fixed in win81 */,
2373 "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
2374 if ( !pserv[j] ) continue;
2375 ok ( pserv[j]->s_port == htons(serv[j].port),
2376 "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
2377 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ),
2378 "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
2379 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ),
2380 "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
2381 }
2382
2383 ok ( pserv[0] == pserv[1] || broken(pserv[0] != pserv[1]) /* win8, fixed in win81 */,
2384 "getservbyname: winsock resized servent buffer when not necessary\n" );
2385 }
2386
2387 return 0;
2389
2390static void test_getservbyname(void)
2391{
2392 int i;
2393 HANDLE starttest, thread[NUM_THREADS];
2395
2396 starttest = CreateEventA ( NULL, 1, 0, "test_getservbyname_starttest" );
2397
2398 /* create threads */
2399 for ( i = 0; i < NUM_THREADS; i++ ) {
2400 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
2401 }
2402
2403 /* signal threads to start */
2404 SetEvent ( starttest );
2405
2406 for ( i = 0; i < NUM_THREADS; i++) {
2408 }
2410
2411static void test_WSASocket(void)
2412{
2415 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP};
2416 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP};
2417 int items, err, size, socktype, i, j;
2418 UINT pi_size;
2419
2420 SetLastError(0xdeadbeef);
2421 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET,
2422 "WSASocketA should have failed\n");
2423 err = WSAGetLastError();
2424 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2425
2426 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2427 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2429
2430 sock = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
2431 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2433
2434 SetLastError(0xdeadbeef);
2435 ok(WSASocketA(0, SOCK_STREAM, -1, NULL, 0, 0) == INVALID_SOCKET,
2436 "WSASocketA should have failed\n");
2437 err = WSAGetLastError();
2438 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2439
2440 SetLastError(0xdeadbeef);
2441 ok(WSASocketA(0, -1, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET,
2442 "WSASocketA should have failed\n");
2443 err = WSAGetLastError();
2444 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2445
2446 SetLastError(0xdeadbeef);
2447 ok(WSASocketA(0, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2448 "WSASocketA should have failed\n");
2449 err = WSAGetLastError();
2450 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2451
2452 SetLastError(0xdeadbeef);
2453 ok(WSASocketA(AF_INET, -1, 0, NULL, 0, 0) == INVALID_SOCKET,
2454 "WSASocketA should have failed\n");
2455 err = WSAGetLastError();
2456 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2457
2458 SetLastError(0xdeadbeef);
2459 ok(WSASocketA(AF_INET, 0, -1, NULL, 0, 0) == INVALID_SOCKET,
2460 "WSASocketA should have failed\n");
2461 err = WSAGetLastError();
2462 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2463
2464 SetLastError(0xdeadbeef);
2465 ok(WSASocketA(0, -1, -1, NULL, 0, 0) == INVALID_SOCKET,
2466 "WSASocketA should have failed\n");
2467 err = WSAGetLastError();
2468 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2469
2470 SetLastError(0xdeadbeef);
2472 "WSASocketA should have failed\n");
2473 err = WSAGetLastError();
2474 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2475
2476 sock = WSASocketA(AF_INET, 0, IPPROTO_TCP, NULL, 0, 0);
2477 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2479
2480 SetLastError(0xdeadbeef);
2481 ok(WSASocketA(0, SOCK_STREAM, 0, NULL, 0, 0) == INVALID_SOCKET,
2482 "WSASocketA should have failed\n");
2483 err = WSAGetLastError();
2484 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2485
2486 SetLastError(0xdeadbeef);
2487 ok(WSASocketA(0, 0, 0xdead, NULL, 0, 0) == INVALID_SOCKET,
2488 "WSASocketA should have failed\n");
2489 err = WSAGetLastError();
2490 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2491
2492 SetLastError(0xdeadbeef);
2493 ok(WSASocketA(AF_INET, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2494 "WSASocketA should have failed\n");
2495 err = WSAGetLastError();
2496 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err);
2497
2498 SetLastError(0xdeadbeef);
2499 ok(WSASocketA(0, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET,
2500 "WSASocketA should have failed\n");
2501 err = WSAGetLastError();
2502 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err);
2503
2504 sock = WSASocketA(0, 0, IPPROTO_TCP, NULL, 0, 0);
2505 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2507
2508 /* SOCK_STREAM does not support IPPROTO_UDP */
2509 SetLastError(0xdeadbeef);
2511 "WSASocketA should have failed\n");
2512 err = WSAGetLastError();
2513 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2514
2515 /* SOCK_DGRAM does not support IPPROTO_TCP */
2516 SetLastError(0xdeadbeef);
2518 "WSASocketA should have failed\n");
2519 err = WSAGetLastError();
2520 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2521
2522 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2523 * to avoid a crash on win98.
2524 */
2525 pi_size = 0;
2526 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size);
2527 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2528 items);
2529 err = WSAGetLastError();
2530 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2531 err, WSAENOBUFS);
2532
2533 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2534 ok(pi != NULL, "Failed to allocate memory\n");
2535 if (pi == NULL) {
2536 skip("Can't continue without memory.\n");
2537 return;
2538 }
2539
2540 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size);
2541 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2542 WSAGetLastError());
2543
2544 if (items == 0) {
2545 skip("No protocols enumerated.\n");
2546 HeapFree(GetProcessHeap(), 0, pi);
2547 return;
2548 }
2549
2551 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
2552 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2553 WSAGetLastError());
2555
2556 /* find what parameters are used first: plain parameters or protocol info struct */
2557 pi[0].iProtocol = -1;
2558 pi[0].iSocketType = -1;
2559 pi[0].iAddressFamily = -1;
2560 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET,
2561 "WSASocketA should have failed\n");
2562 err = WSAGetLastError();
2563 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2564
2565 pi[0].iProtocol = 0;
2566 pi[0].iSocketType = 0;
2567 pi[0].iAddressFamily = 0;
2568 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0);
2569 if(sock != INVALID_SOCKET)
2570 {
2571 win_skip("must work only in OS <= 2003\n");
2573 }
2574 else
2575 {
2576 err = WSAGetLastError();
2577 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2578 }
2579
2580 pi[0].iProtocol = IPPROTO_UDP;
2581 pi[0].iSocketType = SOCK_DGRAM;
2582 pi[0].iAddressFamily = AF_INET;
2583 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0);
2584 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2585 WSAGetLastError());
2586 size = sizeof(socktype);
2587 socktype = 0xdead;
2588 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2589 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2590 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2591 SOCK_DGRAM, socktype);
2593
2595 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2596 WSAGetLastError());
2597 size = sizeof(socktype);
2598 socktype = 0xdead;
2599 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2600 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2601 ok(socktype == SOCK_STREAM, "Wrong socket type, expected %d received %d\n",
2602 SOCK_STREAM, socktype);
2604
2605 HeapFree(GetProcessHeap(), 0, pi);
2606
2607 pi_size = 0;
2608 items = WSAEnumProtocolsA(NULL, NULL, &pi_size);
2609 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2610 items);
2611 err = WSAGetLastError();
2612 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2613 err, WSAENOBUFS);
2614
2615 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2616 ok(pi != NULL, "Failed to allocate memory\n");
2617 if (pi == NULL) {
2618 skip("Can't continue without memory.\n");
2619 return;
2620 }
2621
2622 items = WSAEnumProtocolsA(NULL, pi, &pi_size);
2623 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2624 WSAGetLastError());
2625
2626 /* when no protocol and socket type are specified the first entry
2627 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2628 * is returned */
2629 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2630 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2631 WSAGetLastError());
2632
2633 size = sizeof(socktype);
2634 socktype = 0xdead;
2635 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2636 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2637 for(i = 0; i < items; i++)
2638 {
2639 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
2640 {
2641 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n",
2642 pi[i].iSocketType, socktype);
2643 break;
2644 }
2645 }
2646 ok(i != items, "Creating a socket without protocol and socket type didn't work\n");
2648
2649 /* when no socket type is specified the first entry from WSAEnumProtocols
2650 * that matches the protocol is returned */
2651 for (i = 0; i < sizeof(autoprotocols) / sizeof(autoprotocols[0]); i++)
2652 {
2653 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0);
2654 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n",
2655 autoprotocols[i], WSAGetLastError());
2656
2657 size = sizeof(socktype);
2658 socktype = 0xdead;
2659 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2660 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2661
2662 for (err = 1, j = 0; j < items; j++)
2663 {
2664 if (pi[j].iProtocol == autoprotocols[i])
2665 {
2666 if (socktype == pi[j].iSocketType)
2667 err = 0;
2668 else
2669 ok(0, "Wrong socket type, expected %d received %d\n",
2670 pi[j].iSocketType, socktype);
2671 break;
2672 }
2673 }
2674 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]);
2675
2677 }
2678
2679 HeapFree(GetProcessHeap(), 0, pi);
2680
2681 SetLastError(0xdeadbeef);
2682 /* starting on vista the socket function returns error during the socket
2683 creation and no longer in the socket operations (sendto, readfrom) */
2685 if (sock == INVALID_SOCKET)
2686 {
2687 err = WSAGetLastError();
2688 ok(err == WSAEACCES, "Expected 10013, received %d\n", err);
2689 skip("SOCK_RAW is not supported\n");
2690 }
2691 else
2692 {
2693 trace("SOCK_RAW is supported\n");
2694
2695 size = sizeof(socktype);
2696 socktype = 0xdead;
2697 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2698 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2699 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2700 SOCK_RAW, socktype);
2702
2703 todo_wine {
2704 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0);
2705 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2706 WSAGetLastError());
2707 size = sizeof(socktype);
2708 socktype = 0xdead;
2709 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2710 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2711 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2712 SOCK_RAW, socktype);
2714 }
2715
2717 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2718 WSAGetLastError());
2719 size = sizeof(socktype);
2720 socktype = 0xdead;
2721 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2722 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2723 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2724 SOCK_RAW, socktype);
2726 }
2727
2728 /* IPX socket tests */
2729
2730 SetLastError(0xdeadbeef);
2732 if (sock == INVALID_SOCKET)
2733 {
2734 err = WSAGetLastError();
2736 ok(err == WSAEAFNOSUPPORT || broken(err == WSAEPROTONOSUPPORT), "Expected 10047, received %d\n", err);
2737 skip("IPX is not supported\n");
2738 }
2739 else
2740 {
2743
2744 trace("IPX is supported\n");
2745
2746 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0);
2747 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2748 WSAGetLastError());
2749
2750 size = sizeof(socktype);
2751 socktype = 0xdead;
2752 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2753 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2754 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2755 SOCK_DGRAM, socktype);
2756
2757 /* check socket family, type and protocol */
2758 size = sizeof(WSAPROTOCOL_INFOA);
2760 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2761 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n",
2762 NSPROTO_IPX, info.iProtocol);
2763 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n",
2764 AF_IPX, info.iProtocol);
2765 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2766 SOCK_DGRAM, info.iSocketType);
2768
2769 /* SOCK_STREAM does not support NSPROTO_IPX */
2770 SetLastError(0xdeadbeef);
2772 "WSASocketA should have failed\n");
2773 err = WSAGetLastError();
2774 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2775
2776 /* test extended IPX support - that is adding any number between 0 and 255
2777 * to the IPX protocol value will make it be used as IPX packet type */
2778 for(i = 0;i <= 255;i += 17)
2779 {
2780 SetLastError(0xdeadbeef);
2781 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0);
2782 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2783 WSAGetLastError());
2784
2785 size = sizeof(int);
2786 socktype = -1;
2787 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size);
2788 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2789 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n",
2790 i, socktype);
2791
2793 }
2794 }
2796
2797static void test_WSADuplicateSocket(void)
2798{
2799 SOCKET source, dupsock;
2801 DWORD err;
2802 struct sockaddr_in addr;
2803 int socktype, size, addrsize, ret;
2804 char teststr[] = "TEST", buffer[16];
2805
2807 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2808
2809 /* test invalid parameters */
2810 SetLastError(0xdeadbeef);
2811 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n");
2812 err = WSAGetLastError();
2813 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2814
2815 SetLastError(0xdeadbeef);
2817 "WSADuplicateSocketA should have failed\n");
2818 err = WSAGetLastError();
2819 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2820
2821 SetLastError(0xdeadbeef);
2823 "WSADuplicateSocketA should have failed\n");
2824 err = WSAGetLastError();
2825 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2826
2827 SetLastError(0xdeadbeef);
2829 "WSADuplicateSocketA should have failed\n");
2830 err = WSAGetLastError();
2831 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2832
2833 SetLastError(0xdeadbeef);
2835 "WSADuplicateSocketA should have failed\n");
2836 err = WSAGetLastError();
2837 ok(err == WSAEFAULT, "expected 10014, received %d\n", err);
2838
2839 /* test returned structure */
2840 memset(&info, 0, sizeof(info));
2842 "WSADuplicateSocketA should have worked\n");
2843
2844 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n",
2845 IPPROTO_TCP, info.iProtocol);
2846 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2847 AF_INET, info.iProtocol);
2848 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n",
2849 SOCK_STREAM, info.iSocketType);
2850
2851 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2852 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2853
2854 closesocket(dupsock);
2856
2857 /* create a socket, bind it, duplicate it then send data on source and
2858 * receive in the duplicated socket */
2860 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2861
2862 memset(&info, 0, sizeof(info));
2864 "WSADuplicateSocketA should have worked\n");
2865
2866 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n",
2867 IPPROTO_UDP, info.iProtocol);
2868 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2869 AF_INET, info.iProtocol);
2870 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2871 SOCK_DGRAM, info.iSocketType);
2872
2873 memset(&addr, 0, sizeof(addr));
2874 addr.sin_family = AF_INET;
2875 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2876 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2877 "bind should have worked\n");
2878
2879 /* read address to find out the port number to be used in sendto */
2880 memset(&addr, 0, sizeof(addr));
2881 addrsize = sizeof(addr);
2882 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2883 "getsockname should have worked\n");
2884 ok(addr.sin_port, "socket port should be != 0\n");
2885
2886 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2887 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2888
2889 size = sizeof(int);
2890 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2891 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
2892 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2893 SOCK_DGRAM, socktype);
2894
2896
2897 /* send data on source socket */
2898 addrsize = sizeof(addr);
2899 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2900 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2901
2902 /* receive on duplicated socket */
2903 addrsize = sizeof(addr);
2904 memset(buffer, 0, sizeof(buffer));
2905 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize);
2906 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2907 buffer[sizeof(teststr) - 1] = 0;
2908 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer);
2909
2910 closesocket(dupsock);
2912
2913 /* show that the source socket need to be bound before the duplicated
2914 * socket is created */
2916 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2917
2918 memset(&info, 0, sizeof(info));
2920 "WSADuplicateSocketA should have worked\n");
2921
2922 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2923 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2924
2925 memset(&addr, 0, sizeof(addr));
2926 addr.sin_family = AF_INET;
2927 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2928 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2929 "bind should have worked\n");
2930
2931 /* read address to find out the port number to be used in sendto */
2932 memset(&addr, 0, sizeof(addr));
2933 addrsize = sizeof(addr);
2934 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2935 "getsockname should have worked\n");
2936 ok(addr.sin_port, "socket port should be != 0\n");
2937
2939
2940 addrsize = sizeof(addr);
2941 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2942 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2943
2944 SetLastError(0xdeadbeef);
2945 addrsize = sizeof(addr);
2946 memset(buffer, 0, sizeof(buffer));
2947 todo_wine {
2948 ok(recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize) == -1,
2949 "recvfrom should have failed\n");
2950 err = WSAGetLastError();
2951 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2952 }
2953
2954 closesocket(dupsock);
2957
2958static void test_WSAEnumNetworkEvents(void)
2959{
2960 SOCKET s, s2;
2961 int sock_type[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM}, i, j, k, l;
2962 struct sockaddr_in address;
2963 HANDLE event;
2964 WSANETWORKEVENTS net_events;
2965
2966 memset(&address, 0, sizeof(address));
2967 address.sin_addr.s_addr = htonl(INADDR_ANY);
2968 address.sin_family = AF_INET;
2969
2970 /* This test follows the steps from bugs 10204 and 24946 */
2971 for (l = 0; l < 2; l++)
2972 {
2973 for (i = 0; i < sizeof(sock_type) / sizeof(sock_type[0]); i++)
2974 {
2975 if (i == 2)
2976 ok(!tcp_socketpair(&s, &s2), "Test[%d]: creating socket pair failed\n", i);
2977 else
2978 {
2979 s = socket(AF_INET, sock_type[i], 0);
2980 ok (s != SOCKET_ERROR, "Test[%d]: failed to create socket\n", i);
2981 ok (!bind(s, (struct sockaddr*) &address, sizeof(address)), "Test[%d]: bind failed\n", i);
2982 }
2983 event = WSACreateEvent();
2984 ok (event != NULL, "Test[%d]: failed to create event\n", i);
2985 for (j = 0; j < 5; j++) /* Repeat sometimes and the result must be the same */
2986 {
2987 /* When the TCP socket is not connected NO events will be returned.
2988 * When connected and no data pending it will get the write event.
2989 * UDP sockets don't have connections so as soon as they are bound
2990 * they can read/write data. Since nobody is sendind us data only
2991 * the write event will be returned and ONLY once.
2992 */
2993 ok (!WSAEventSelect(s, event, FD_READ | FD_WRITE), "Test[%d]: WSAEventSelect failed\n", i);
2994 memset(&net_events, 0xAB, sizeof(net_events));
2995 ok (!WSAEnumNetworkEvents(s, l == 0 ? event : NULL, &net_events),
2996 "Test[%d]: WSAEnumNetworkEvents failed\n", i);
2997 if (i >= 1 && j == 0) /* FD_WRITE is SET on first try for UDP and connected TCP */
2998 {
2999 todo_wine_if (i == 0 || net_events.lNetworkEvents == 0)
3000 ok (net_events.lNetworkEvents == FD_WRITE, "Test[%d]: expected 2, got %d\n",
3001 i, net_events.lNetworkEvents);
3002 }
3003 else
3004 {
3005 todo_wine_if (i != 0 && net_events.lNetworkEvents != 0)
3006 ok (net_events.lNetworkEvents == 0, "Test[%d]: expected 0, got %d\n",
3007 i, net_events.lNetworkEvents);
3008 }
3009 for (k = 0; k < FD_MAX_EVENTS; k++)
3010 {
3011 if (net_events.lNetworkEvents & (1 << k))
3012 {
3013 ok (net_events.iErrorCode[k] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n",
3014 i, k, net_events.iErrorCode[k]);
3015 }
3016 else
3017 {
3018 /* Bits that are not set in lNetworkEvents MUST not be changed */
3019 ok (net_events.iErrorCode[k] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n",
3020 i, k, net_events.iErrorCode[k]);
3021 }
3022 }
3023 }
3024 closesocket(s);
3026 if (i == 2) closesocket(s2);
3027 }
3028 }
3030
3031static void test_WSAAddressToStringA(void)
3032{
3034 INT ret;
3035 DWORD len;
3036 int GLE;
3038 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3039
3040 CHAR expect1[] = "0.0.0.0";
3041 CHAR expect2[] = "255.255.255.255";
3042 CHAR expect3[] = "0.0.0.0:65535";
3043 CHAR expect4[] = "255.255.255.255:65535";
3044
3045 SOCKADDR_IN6 sockaddr6;
3046 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
3047
3048 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3049 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3050 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
3051
3052 CHAR expect6_1[] = "::1";
3053 CHAR expect6_2[] = "20ab::1";
3054 CHAR expect6_3[] = "[20ab::2001]:33274";
3055 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
3056 CHAR expect6_3_3[] = "20ab::2001%4660";
3057
3058 len = 0;
3059
3060 sockaddr.sin_family = AF_INET;
3061 sockaddr.sin_port = 0;
3062 sockaddr.sin_addr.s_addr = 0;
3063
3065 GLE = WSAGetLastError();
3066 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
3067 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
3068 GLE, ret );
3069
3070 len = sizeof(address);
3071
3072 sockaddr.sin_family = AF_INET;
3073 sockaddr.sin_port = 0;
3074 sockaddr.sin_addr.s_addr = 0;
3075
3077 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3078
3079 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
3080 ok( len == sizeof( expect1 ), "Got size %d\n", len);
3081
3082 len = sizeof(address);
3083
3084 sockaddr.sin_family = AF_INET;
3085 sockaddr.sin_port = 0;
3086 sockaddr.sin_addr.s_addr = 0xffffffff;
3087
3089 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3090
3091 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
3092
3093 len = sizeof(address);
3094
3095 sockaddr.sin_family = AF_INET;
3096 sockaddr.sin_port = 0xffff;
3097 sockaddr.sin_addr.s_addr = 0;
3098
3100 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3101
3102 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
3103
3104 len = sizeof(address);
3105
3106 sockaddr.sin_family = AF_INET;
3107 sockaddr.sin_port = 0xffff;
3108 sockaddr.sin_addr.s_addr = 0xffffffff;
3109
3111 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3112
3113 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
3114 ok( len == sizeof( expect4 ), "Got size %d\n", len);
3115
3116 /*check to see it IPv6 is available */
3118 if (v6 == INVALID_SOCKET) {
3119 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3121 goto end;
3122 }
3123 /* Test a short IPv6 address */
3124 len = sizeof(address6);
3125
3126 sockaddr6.sin6_family = AF_INET6;
3127 sockaddr6.sin6_port = 0x0000;
3128 sockaddr6.sin6_scope_id = 0;
3129 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
3130
3131 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3132 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3133 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
3134 ok( len == sizeof(expect6_1), "Got size %d\n", len);
3135
3136 /* Test a longer IPv6 address */
3137 len = sizeof(address6);
3138
3139 sockaddr6.sin6_family = AF_INET6;
3140 sockaddr6.sin6_port = 0x0000;
3141 sockaddr6.sin6_scope_id = 0;
3142 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
3143
3144 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3145 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3146 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
3147 ok( len == sizeof(expect6_2), "Got size %d\n", len);
3148
3149 /* Test IPv6 address and port number */
3150 len = sizeof(address6);
3151
3152 sockaddr6.sin6_family = AF_INET6;
3153 sockaddr6.sin6_port = 0xfa81;
3154 sockaddr6.sin6_scope_id = 0;
3155 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3156
3157 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3158 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3159 ok( !strcmp( address6, expect6_3 ), "Expected: %s, got: %s\n", expect6_3, address6 );
3160 ok( len == sizeof(expect6_3), "Got size %d\n", len );
3161
3162 /* Test IPv6 address, port number and scope_id */
3163 len = sizeof(address6);
3164
3165 sockaddr6.sin6_family = AF_INET6;
3166 sockaddr6.sin6_port = 0xfa81;
3167 sockaddr6.sin6_scope_id = 0x1234;
3168 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3169
3170 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3171 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3172 ok( !strcmp( address6, expect6_3_2 ), "Expected: %s, got: %s\n", expect6_3_2, address6 );
3173 ok( len == sizeof(expect6_3_2), "Got size %d\n", len );
3174
3175 /* Test IPv6 address and scope_id */
3176 len = sizeof(address6);
3177
3178 sockaddr6.sin6_family = AF_INET6;
3179 sockaddr6.sin6_port = 0x0000;
3180 sockaddr6.sin6_scope_id = 0x1234;
3181 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
3182
3183 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
3184 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
3185 ok( !strcmp( address6, expect6_3_3 ), "Expected: %s, got: %s\n", expect6_3_3, address6 );
3186 ok( len == sizeof(expect6_3_3), "Got size %d\n", len );
3187
3188end:
3189 if (v6 != INVALID_SOCKET)
3190 closesocket(v6);
3192
3193static void test_WSAAddressToStringW(void)
3194{
3196 INT ret;
3197 DWORD len;
3198 int GLE;
3200 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3201
3202 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
3203 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
3204 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
3205 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
3206 '6', '5', '5', '3', '5', 0 };
3207
3208 SOCKADDR_IN6 sockaddr6;
3209 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
3210
3211 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3212 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
3213 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
3214
3215 WCHAR expect6_1[] = {':',':','1',0};
3216 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
3217 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
3218 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
3219 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
3220
3221 len = 0;
3222
3223 sockaddr.sin_family = AF_INET;
3224 sockaddr.sin_port = 0;
3225 sockaddr.sin_addr.s_addr = 0;
3226
3228 GLE = WSAGetLastError();
3229 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
3230 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
3231 GLE, ret );
3232
3233 len = sizeof(address);
3234
3235 sockaddr.sin_family = AF_INET;
3236 sockaddr.sin_port = 0;
3237 sockaddr.sin_addr.s_addr = 0;
3238
3240 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3241
3242 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
3243 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
3244
3245 len = sizeof(address);
3246
3247 sockaddr.sin_family = AF_INET;
3248 sockaddr.sin_port = 0;
3249 sockaddr.sin_addr.s_addr = 0xffffffff;
3250
3252 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3253
3254 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
3255
3256 len = sizeof(address);
3257
3258 sockaddr.sin_family = AF_INET;
3259 sockaddr.sin_port = 0xffff;
3260 sockaddr.sin_addr.s_addr = 0;
3261
3263 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3264
3265 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
3266
3267 len = sizeof(address);
3268
3269 sockaddr.sin_family = AF_INET;
3270 sockaddr.sin_port = 0xffff;
3271 sockaddr.sin_addr.s_addr = 0xffffffff;
3272
3273 ret =