2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
34 #define MAX_CLIENTS 4 /* Max number of clients */
35 #define NUM_TESTS 4 /* Number of tests performed */
36 #define FIRST_CHAR 'A' /* First character in transferred pattern */
37 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
38 #define BIND_TRIES 6 /* Number of bind() attempts */
39 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
40 after server initialization, if something hangs */
42 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
44 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
45 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
47 #define SERVERIP "127.0.0.1" /* IP to bind to */
48 #define SERVERPORT 9374 /* Port number to bind to */
50 #define wsa_ok(op, cond, msg) \
54 if ( !(cond tmp) ) err = WSAGetLastError(); \
55 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
58 /* Function pointers */
59 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW) = 0;
60 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *) = 0;
61 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG) = 0;
63 /**************** Structs and typedefs ***************/
65 typedef struct thread_info
71 /* Information in the server about open client connections */
72 typedef struct sock_info
75 struct sockaddr_in addr;
76 struct sockaddr_in peer;
82 /* Test parameters for both server & client */
83 typedef struct test_params
87 const char *inet_addr;
94 /* server-specific test parameters */
95 typedef struct server_params
102 /* client-specific test parameters */
103 typedef struct client_params
105 test_params *general;
110 /* This type combines all information for setting up a test scenario */
111 typedef struct test_setup
115 server_params srv_params;
117 client_params clt_params;
120 /* Thread local storage for server */
121 typedef struct server_memory
124 struct sockaddr_in addr;
125 sock_info sock[MAX_CLIENTS];
128 /* Thread local storage for client */
129 typedef struct client_memory
132 struct sockaddr_in addr;
137 /* SelectReadThread thread parameters */
138 typedef struct select_thread_params
142 } select_thread_params;
144 /**************** Static variables ***************/
146 static DWORD tls; /* Thread local storage index */
147 static HANDLE thread[1+MAX_CLIENTS];
148 static DWORD thread_id[1+MAX_CLIENTS];
149 static HANDLE server_ready;
150 static HANDLE client_ready[MAX_CLIENTS];
151 static int client_id;
153 /**************** General utility functions ***************/
155 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
157 SOCKET server = INVALID_SOCKET;
158 struct sockaddr_in addr;
162 *src = INVALID_SOCKET;
163 *dst = INVALID_SOCKET;
165 *src = socket(AF_INET, SOCK_STREAM, 0);
166 if (*src == INVALID_SOCKET)
169 server = socket(AF_INET, SOCK_STREAM, 0);
170 if (server == INVALID_SOCKET)
173 memset(&addr, 0, sizeof(addr));
174 addr.sin_family = AF_INET;
175 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
176 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
181 ret = getsockname(server, (struct sockaddr*)&addr, &len);
185 ret = listen(server, 1);
189 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
194 *dst = accept(server, (struct sockaddr*)&addr, &len);
197 if (server != INVALID_SOCKET)
199 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
206 static void set_so_opentype ( BOOL overlapped )
208 int optval = !overlapped, newval, len = sizeof (int);
210 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
211 (LPVOID) &optval, sizeof (optval) ) == 0,
212 "setting SO_OPENTYPE failed\n" );
213 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
214 (LPVOID) &newval, &len ) == 0,
215 "getting SO_OPENTYPE failed\n" );
216 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
219 static int set_blocking ( SOCKET s, BOOL blocking )
221 u_long val = !blocking;
222 return ioctlsocket ( s, FIONBIO, &val );
225 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
228 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
229 memset ( p, c, chunk_size );
232 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
236 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
238 for ( i = 0; i < chunk_size; i++ )
239 if ( p[i] != c ) return i;
245 * This routine is called when a client / server does not expect any more data,
246 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
248 static void read_zero_bytes ( SOCKET s )
252 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
254 ok ( n <= 0, "garbage data received: %d bytes\n", n );
257 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
259 char* last = buf + buflen, *p;
261 for ( p = buf; n > 0 && p < last; p += n )
262 n = send ( s, p, min ( sendlen, last - p ), 0 );
263 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
267 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
269 char* last = buf + buflen, *p;
271 for ( p = buf; n > 0 && p < last; p += n )
272 n = recv ( s, p, min ( recvlen, last - p ), 0 );
273 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
277 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, int *fromlen, int recvlen )
279 char* last = buf + buflen, *p;
281 for ( p = buf; n > 0 && p < last; p += n )
282 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
283 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
288 * Call this routine right after thread startup.
289 * SO_OPENTYPE must by 0, regardless what the server did.
291 static void check_so_opentype (void)
295 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
296 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
299 /**************** Server utility functions ***************/
302 * Even if we have closed our server socket cleanly,
303 * the OS may mark the address "in use" for some time -
304 * this happens with native Linux apps, too.
306 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
308 int err, wsaerr = 0, n_try = BIND_TRIES;
310 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
311 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
314 trace ( "address in use, waiting ...\n" );
315 Sleep ( 1000 * BIND_SLEEP );
317 ok ( err == 0, "failed to bind: %d\n", wsaerr );
320 static void server_start ( server_params *par )
323 test_params *gen = par->general;
324 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
326 TlsSetValue ( tls, mem );
327 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
328 NULL, 0, par->sock_flags );
329 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
331 mem->addr.sin_family = AF_INET;
332 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
333 mem->addr.sin_port = htons ( gen->inet_port );
335 for (i = 0; i < MAX_CLIENTS; i++)
337 mem->sock[i].s = INVALID_SOCKET;
338 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
339 mem->sock[i].n_recvd = 0;
340 mem->sock[i].n_sent = 0;
343 if ( gen->sock_type == SOCK_STREAM )
344 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
347 static void server_stop (void)
350 server_memory *mem = TlsGetValue ( tls );
352 for (i = 0; i < MAX_CLIENTS; i++ )
354 LocalFree ( mem->sock[i].buf );
355 if ( mem->sock[i].s != INVALID_SOCKET )
356 closesocket ( mem->sock[i].s );
358 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
360 ExitThread ( GetCurrentThreadId () );
363 /**************** Client utilitiy functions ***************/
365 static void client_start ( client_params *par )
367 test_params *gen = par->general;
368 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
370 TlsSetValue ( tls, mem );
372 WaitForSingleObject ( server_ready, INFINITE );
374 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
375 NULL, 0, par->sock_flags );
377 mem->addr.sin_family = AF_INET;
378 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
379 mem->addr.sin_port = htons ( gen->inet_port );
381 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
383 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
384 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
385 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
387 SetEvent ( client_ready[client_id] );
388 /* Wait for the other clients to come up */
389 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
392 static void client_stop (void)
394 client_memory *mem = TlsGetValue ( tls );
395 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
396 LocalFree ( mem->send_buf );
401 /**************** Servers ***************/
404 * simple_server: A very basic server doing synchronous IO.
406 static VOID WINAPI simple_server ( server_params *par )
408 test_params *gen = par->general;
410 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
411 id = GetCurrentThreadId();
413 trace ( "simple_server (%x) starting\n", id );
415 set_so_opentype ( FALSE ); /* non-overlapped */
416 server_start ( par );
417 mem = TlsGetValue ( tls );
419 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
420 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
422 trace ( "simple_server (%x) ready\n", id );
423 SetEvent ( server_ready ); /* notify clients */
425 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
427 trace ( "simple_server (%x): waiting for client\n", id );
429 /* accept a single connection */
430 tmp = sizeof ( mem->sock[0].peer );
431 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
432 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
434 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
435 "simple_server (%x): strange peer address\n", id );
437 /* Receive data & check it */
438 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
439 ok ( n_recvd == n_expected,
440 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
441 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
442 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
445 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
446 ok ( n_sent == n_expected,
447 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
450 read_zero_bytes ( mem->sock[0].s );
451 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
452 mem->sock[0].s = INVALID_SOCKET;
455 trace ( "simple_server (%x) exiting\n", id );
460 * select_server: A non-blocking server.
462 static VOID WINAPI select_server ( server_params *par )
464 test_params *gen = par->general;
466 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
467 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
468 n_set, delta, n_ready;
469 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
470 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
472 trace ( "select_server (%x) starting\n", id );
474 set_so_opentype ( FALSE ); /* non-overlapped */
475 server_start ( par );
476 mem = TlsGetValue ( tls );
478 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
479 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
481 trace ( "select_server (%x) ready\n", id );
482 SetEvent ( server_ready ); /* notify clients */
484 FD_ZERO ( &fds_openrecv );
485 FD_ZERO ( &fds_recv );
486 FD_ZERO ( &fds_send );
487 FD_ZERO ( &fds_opensend );
489 FD_SET ( mem->s, &fds_openrecv );
493 fds_recv = fds_openrecv;
494 fds_send = fds_opensend;
498 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
499 "select_server (%x): select() failed: %d\n" );
501 /* check for incoming requests */
502 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
505 trace ( "select_server (%x): accepting client connection\n", id );
507 /* accept a single connection */
508 tmp = sizeof ( mem->sock[n_connections].peer );
509 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
510 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
512 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
513 "select_server (%x): strange peer address\n", id );
515 /* add to list of open connections */
516 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
517 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
522 /* handle open requests */
524 for ( i = 0; i < n_connections; i++ )
526 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
529 if ( mem->sock[i].n_recvd < n_expected ) {
530 /* Receive data & check it */
531 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 );
532 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
533 mem->sock[i].n_recvd += n_recvd;
535 if ( mem->sock[i].n_recvd == n_expected ) {
536 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
537 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
538 FD_CLR ( mem->sock[i].s, &fds_openrecv );
541 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
545 /* only echo back what we've received */
546 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
548 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
551 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
553 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
554 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
555 mem->sock[i].n_sent += n_sent;
557 if ( mem->sock[i].n_sent == n_expected ) {
558 FD_CLR ( mem->sock[i].s, &fds_opensend );
561 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
566 /* check that select returned the correct number of ready sockets */
567 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
569 /* check if all clients are done */
570 if ( ( fds_opensend.fd_count == 0 )
571 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
572 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
577 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
580 read_zero_bytes ( mem->sock[i].s );
581 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
582 mem->sock[i].s = INVALID_SOCKET;
585 trace ( "select_server (%x) exiting\n", id );
589 /**************** Clients ***************/
592 * simple_client: A very basic client doing synchronous IO.
594 static VOID WINAPI simple_client ( client_params *par )
596 test_params *gen = par->general;
598 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
600 id = GetCurrentThreadId();
601 trace ( "simple_client (%x): starting\n", id );
602 /* wait here because we want to call set_so_opentype before creating a socket */
603 WaitForSingleObject ( server_ready, INFINITE );
604 trace ( "simple_client (%x): server ready\n", id );
606 check_so_opentype ();
607 set_so_opentype ( FALSE ); /* non-overlapped */
608 client_start ( par );
609 mem = TlsGetValue ( tls );
612 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
613 0 ==, "simple_client (%x): connect error: %d\n" );
614 ok ( set_blocking ( mem->s, TRUE ) == 0,
615 "simple_client (%x): failed to set blocking mode\n", id );
616 trace ( "simple_client (%x) connected\n", id );
618 /* send data to server */
619 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
620 ok ( n_sent == n_expected,
621 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
623 /* shutdown send direction */
624 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
626 /* Receive data echoed back & check it */
627 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
628 ok ( n_recvd == n_expected,
629 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
632 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
633 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
636 read_zero_bytes ( mem->s );
637 trace ( "simple_client (%x) exiting\n", id );
642 * simple_mixed_client: mixing send and recvfrom
644 static VOID WINAPI simple_mixed_client ( client_params *par )
646 test_params *gen = par->general;
648 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
649 int fromLen = sizeof(mem->addr);
650 struct sockaddr test;
652 id = GetCurrentThreadId();
653 trace ( "simple_client (%x): starting\n", id );
654 /* wait here because we want to call set_so_opentype before creating a socket */
655 WaitForSingleObject ( server_ready, INFINITE );
656 trace ( "simple_client (%x): server ready\n", id );
658 check_so_opentype ();
659 set_so_opentype ( FALSE ); /* non-overlapped */
660 client_start ( par );
661 mem = TlsGetValue ( tls );
664 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
665 0 ==, "simple_client (%x): connect error: %d\n" );
666 ok ( set_blocking ( mem->s, TRUE ) == 0,
667 "simple_client (%x): failed to set blocking mode\n", id );
668 trace ( "simple_client (%x) connected\n", id );
670 /* send data to server */
671 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
672 ok ( n_sent == n_expected,
673 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
675 /* shutdown send direction */
676 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
678 /* this shouldn't change, since lpFrom, is not updated on
679 connection oriented sockets - exposed by bug 11640
681 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
683 /* Receive data echoed back & check it */
684 n_recvd = do_synchronous_recvfrom ( mem->s,
688 (struct sockaddr *)&test,
691 ok ( n_recvd == n_expected,
692 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
694 /* check that lpFrom was not updated */
697 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
698 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
701 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
702 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
705 read_zero_bytes ( mem->s );
706 trace ( "simple_client (%x) exiting\n", id );
711 * event_client: An event-driven client
713 static void WINAPI event_client ( client_params *par )
715 test_params *gen = par->general;
717 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
720 WSANETWORKEVENTS wsa_events;
721 char *send_last, *recv_last, *send_p, *recv_p;
722 long mask = FD_READ | FD_WRITE | FD_CLOSE;
724 trace ( "event_client (%x): starting\n", id );
725 client_start ( par );
726 trace ( "event_client (%x): server ready\n", id );
728 mem = TlsGetValue ( tls );
730 /* Prepare event notification for connect, makes socket nonblocking */
731 event = WSACreateEvent ();
732 WSAEventSelect ( mem->s, event, FD_CONNECT );
733 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
735 err = WSAGetLastError ();
736 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
737 tmp = WaitForSingleObject ( event, INFINITE );
738 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
739 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
740 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
741 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
742 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
746 trace ( "event_client (%x) connected\n", id );
748 WSAEventSelect ( mem->s, event, mask );
750 recv_p = mem->recv_buf;
751 recv_last = mem->recv_buf + n_expected;
752 send_p = mem->send_buf;
753 send_last = mem->send_buf + n_expected;
757 err = WaitForSingleObject ( event, INFINITE );
758 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
760 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
761 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
763 if ( wsa_events.lNetworkEvents & FD_WRITE )
765 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
766 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
771 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
774 err = WSAGetLastError ();
775 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
780 while ( n >= 0 && send_p < send_last );
782 if ( send_p == send_last )
784 trace ( "event_client (%x): all data sent - shutdown\n", id );
785 shutdown ( mem->s, SD_SEND );
787 WSAEventSelect ( mem->s, event, mask );
790 if ( wsa_events.lNetworkEvents & FD_READ )
792 err = wsa_events.iErrorCode[ FD_READ_BIT ];
793 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
794 if ( err != 0 ) break;
796 /* First read must succeed */
797 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
798 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
802 if ( recv_p == recv_last )
805 trace ( "event_client (%x): all data received\n", id );
806 WSAEventSelect ( mem->s, event, mask );
809 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
810 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
811 ok ( 0, "event_client (%x): read error: %d\n", id, err );
815 if ( wsa_events.lNetworkEvents & FD_CLOSE )
817 trace ( "event_client (%x): close event\n", id );
818 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
819 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
824 n = send_p - mem->send_buf;
825 ok ( send_p == send_last,
826 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
827 n = recv_p - mem->recv_buf;
828 ok ( recv_p == recv_last,
829 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
830 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
831 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
834 WSACloseEvent ( event );
835 trace ( "event_client (%x) exiting\n", id );
839 /* Tests for WSAStartup */
841 /* This should fail. WSAStartup should be called before any network function is used. */
842 static void test_WithoutWSAStartup(void)
846 WSASetLastError(0xdeadbeef);
847 ptr = gethostbyname("localhost");
849 todo_wine ok(ptr == NULL, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
850 todo_wine ok(WSAGetLastError() == WSANOTINITIALISED, "gethostbyname() failed with unexpected error: %d\n",
854 static void test_WithWSAStartup(void)
857 WORD version = MAKEWORD( 2, 2 );
861 res = WSAStartup( version, &data );
862 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
864 ptr = gethostbyname("localhost");
865 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
870 /**************** Main program utility functions ***************/
872 static void Init (void)
874 WORD ver = MAKEWORD (2, 2);
876 HMODULE hws2_32 = GetModuleHandle("ws2_32.dll");
878 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
879 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
880 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
882 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
886 static void Exit (void)
891 err = WSAGetLastError();
892 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
894 err = WSAGetLastError();
895 ok ( (ret == SOCKET_ERROR && err == WSANOTINITIALISED) ||
896 broken(ret == 0), /* WinME */
897 "WSACleanup returned %d GetLastError is %d\n", ret, err);
900 static void StartServer (LPTHREAD_START_ROUTINE routine,
901 test_params *general, server_params *par)
903 par->general = general;
904 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
905 ok ( thread[0] != NULL, "Failed to create server thread\n" );
908 static void StartClients (LPTHREAD_START_ROUTINE routine,
909 test_params *general, client_params *par)
912 par->general = general;
913 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
916 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
917 ok ( thread[i] != NULL, "Failed to create client thread\n" );
918 /* Make sure the client is up and running */
919 WaitForSingleObject ( client_ready[client_id], INFINITE );
923 static void do_test( test_setup *test )
925 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
928 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
929 for (i = 0; i <= n; i++)
930 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
932 StartServer ( test->srv, &test->general, &test->srv_params );
933 StartClients ( test->clt, &test->general, &test->clt_params );
934 WaitForSingleObject ( server_ready, INFINITE );
936 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
937 ok ( wait <= WAIT_OBJECT_0 + n ,
938 "some threads have not completed: %x\n", wait );
940 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
942 for (i = 0; i <= n; i++)
944 trace ("terminating thread %08x\n", thread_id[i]);
945 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
946 TerminateThread ( thread [i], 0 );
949 CloseHandle ( server_ready );
950 for (i = 0; i <= n; i++)
951 CloseHandle ( client_ready[i] );
954 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
955 /* optname = SO_LINGER */
956 LINGER linger_testvals[] = {
963 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
964 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
965 bug in the linux kernel (fixed in 2.6.8) */
966 #define SOCKTIMEOUT2 997000 /* 997 seconds */
968 static void test_set_getsockopt(void)
976 s = socket(AF_INET, SOCK_STREAM, 0);
977 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
978 if( s == INVALID_SOCKET) return;
980 timeout = SOCKTIMEOUT1;
981 size = sizeof(timeout);
982 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
984 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
985 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
986 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
988 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
989 size = sizeof(timeout);
990 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
992 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
993 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
994 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
996 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
997 size = sizeof(lingval);
998 lingval = linger_testvals[i];
999 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1001 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1002 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1003 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1004 (lingval.l_linger == linger_testvals[i].l_linger ||
1005 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1006 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1007 lingval.l_onoff, lingval.l_linger,
1008 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1010 /* Test for erroneously passing a value instead of a pointer as optval */
1011 size = sizeof(char);
1012 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1013 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1014 "instead of failing.\n");
1015 lasterr = WSAGetLastError();
1016 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1017 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1018 lasterr, WSAEFAULT);
1022 static void test_so_reuseaddr(void)
1024 struct sockaddr_in saddr;
1026 unsigned int rc,reuse;
1029 saddr.sin_family = AF_INET;
1030 saddr.sin_port = htons(9375);
1031 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1033 s1=socket(AF_INET, SOCK_STREAM, 0);
1034 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1035 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1036 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1038 s2=socket(AF_INET, SOCK_STREAM, 0);
1039 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1043 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1044 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1046 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1047 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1050 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1051 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1053 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1054 * a port immediately after closing another socket on that port, so
1055 * basically following the BSD socket semantics here. */
1057 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1058 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1063 /************* Array containing the tests to run **********/
1065 #define STD_STREAM_SOCKET \
1071 static test_setup tests [NUM_TESTS] =
1073 /* Test 0: synchronous client and server */
1094 /* Test 1: event-driven client, synchronous server */
1111 WSA_FLAG_OVERLAPPED,
1115 /* Test 2: synchronous client, non-blocking server via select() */
1136 /* Test 3: synchronous mixed client and server */
1150 simple_mixed_client,
1159 static void test_UDP(void)
1161 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1162 possible that this test fails due to dropped packets. */
1164 /* peer 0 receives data from all other peers */
1165 struct sock_info peer[NUM_UDP_PEERS];
1167 int ss, i, n_recv, n_sent;
1169 memset (buf,0,sizeof(buf));
1170 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1171 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1173 peer[i].addr.sin_family = AF_INET;
1174 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1177 peer[i].addr.sin_port = htons ( SERVERPORT );
1179 peer[i].addr.sin_port = htons ( 0 );
1182 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1184 /* test getsockname() to get peer's port */
1185 ss = sizeof ( peer[i].addr );
1186 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1187 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1190 /* test getsockname() */
1191 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1193 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1194 /* send client's ip */
1195 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1196 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1197 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1200 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1201 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1202 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1203 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1207 static void WINAPI do_getservbyname( HANDLE *starttest )
1213 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1216 struct servent *pserv[2];
1218 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1220 /* ensure that necessary buffer resizes are completed */
1221 for ( j = 0; j < 2; j++) {
1222 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1225 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1226 for ( j = 0; j < 2; j++ ) {
1227 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1228 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1229 ok ( pserv[j]->s_port == htons(serv[j].port), "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
1230 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ), "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
1231 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ), "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
1234 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1238 static void test_getservbyname(void)
1241 HANDLE starttest, thread[NUM_THREADS];
1242 DWORD thread_id[NUM_THREADS];
1244 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1246 /* create threads */
1247 for ( i = 0; i < NUM_THREADS; i++ ) {
1248 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1251 /* signal threads to start */
1252 SetEvent ( starttest );
1254 for ( i = 0; i < NUM_THREADS; i++) {
1255 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1259 static void test_WSASocket(void)
1261 SOCKET sock = INVALID_SOCKET;
1262 WSAPROTOCOL_INFOA *pi;
1263 int providers[] = {6, 0};
1267 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1268 * to avoid a crash on win98.
1271 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1272 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1274 err = WSAGetLastError();
1275 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1278 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1279 ok(pi != NULL, "Failed to allocate memory\n");
1281 skip("Can't continue without memory.\n");
1285 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1286 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1290 skip("No protocols enumerated.\n");
1291 HeapFree(GetProcessHeap(), 0, pi);
1295 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1296 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1297 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1301 HeapFree(GetProcessHeap(), 0, pi);
1304 static void test_WSAAddressToStringA(void)
1306 SOCKET v6 = INVALID_SOCKET;
1310 SOCKADDR_IN sockaddr;
1311 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1313 CHAR expect1[] = "0.0.0.0";
1314 CHAR expect2[] = "255.255.255.255";
1315 CHAR expect3[] = "0.0.0.0:65535";
1316 CHAR expect4[] = "255.255.255.255:65535";
1318 SOCKADDR_IN6 sockaddr6;
1319 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1321 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1322 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1323 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1325 CHAR expect6_1[] = "::1";
1326 CHAR expect6_2[] = "20ab::1";
1327 CHAR expect6_3[] = "[20ab::2001]:33274";
1328 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
1329 CHAR expect6_3_3[] = "20ab::2001%4660";
1333 sockaddr.sin_family = AF_INET;
1334 sockaddr.sin_port = 0;
1335 sockaddr.sin_addr.s_addr = 0;
1337 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1338 GLE = WSAGetLastError();
1339 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1340 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1343 len = sizeof(address);
1345 sockaddr.sin_family = AF_INET;
1346 sockaddr.sin_port = 0;
1347 sockaddr.sin_addr.s_addr = 0;
1349 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1350 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1352 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1353 ok( len == sizeof( expect1 ), "Got size %d\n", len);
1355 len = sizeof(address);
1357 sockaddr.sin_family = AF_INET;
1358 sockaddr.sin_port = 0;
1359 sockaddr.sin_addr.s_addr = 0xffffffff;
1361 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1362 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1364 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1366 len = sizeof(address);
1368 sockaddr.sin_family = AF_INET;
1369 sockaddr.sin_port = 0xffff;
1370 sockaddr.sin_addr.s_addr = 0;
1372 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1373 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1375 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1377 len = sizeof(address);
1379 sockaddr.sin_family = AF_INET;
1380 sockaddr.sin_port = 0xffff;
1381 sockaddr.sin_addr.s_addr = 0xffffffff;
1383 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1384 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1386 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1387 ok( len == sizeof( expect4 ), "Got size %d\n", len);
1389 /*check to see it IPv6 is available */
1390 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1391 if (v6 == INVALID_SOCKET) {
1392 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1393 WSAGetLastError(), WSAEAFNOSUPPORT);
1396 /* Test a short IPv6 address */
1397 len = sizeof(address6);
1399 sockaddr6.sin6_family = AF_INET6;
1400 sockaddr6.sin6_port = 0x0000;
1401 sockaddr6.sin6_scope_id = 0;
1402 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1404 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1405 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1406 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
1407 ok( len == sizeof(expect6_1), "Got size %d\n", len);
1409 /* Test a longer IPv6 address */
1410 len = sizeof(address6);
1412 sockaddr6.sin6_family = AF_INET6;
1413 sockaddr6.sin6_port = 0x0000;
1414 sockaddr6.sin6_scope_id = 0;
1415 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1417 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1418 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1419 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
1420 ok( len == sizeof(expect6_2), "Got size %d\n", len);
1422 /* Test IPv6 address and port number */
1423 len = sizeof(address6);
1425 sockaddr6.sin6_family = AF_INET6;
1426 sockaddr6.sin6_port = 0xfa81;
1427 sockaddr6.sin6_scope_id = 0;
1428 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1430 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1431 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1434 ok( !strcmp( address6, expect6_3 ), "Expected: %s, got: %s\n", expect6_3, address6 );
1435 ok( len == sizeof(expect6_3), "Got size %d\n", len);
1438 /* Test IPv6 address, port number and scope_id */
1439 len = sizeof(address6);
1441 sockaddr6.sin6_family = AF_INET6;
1442 sockaddr6.sin6_port = 0xfa81;
1443 sockaddr6.sin6_scope_id = 0x1234;
1444 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1446 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1447 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1450 ok( !strcmp( address6, expect6_3_2 ), "Expected: %s, got: %s\n", expect6_3_2, address6 );
1451 ok( len == sizeof(expect6_3_2), "Got size %d\n", len);
1454 /* Test IPv6 address and scope_id */
1455 len = sizeof(address6);
1457 sockaddr6.sin6_family = AF_INET6;
1458 sockaddr6.sin6_port = 0x0000;
1459 sockaddr6.sin6_scope_id = 0x1234;
1460 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1462 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1463 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1466 ok( !strcmp( address6, expect6_3_3 ), "Expected: %s, got: %s\n", expect6_3_3, address6 );
1467 ok( len == sizeof(expect6_3_3), "Got size %d\n", len);
1471 if (v6 != INVALID_SOCKET)
1475 static void test_WSAAddressToStringW(void)
1477 SOCKET v6 = INVALID_SOCKET;
1481 SOCKADDR_IN sockaddr;
1482 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1484 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1485 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1486 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1487 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1488 '6', '5', '5', '3', '5', 0 };
1490 SOCKADDR_IN6 sockaddr6;
1491 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1493 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1494 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1495 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1497 WCHAR expect6_1[] = {':',':','1',0};
1498 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
1499 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1500 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1501 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1505 sockaddr.sin_family = AF_INET;
1506 sockaddr.sin_port = 0;
1507 sockaddr.sin_addr.s_addr = 0;
1509 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1510 GLE = WSAGetLastError();
1511 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1512 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1515 len = sizeof(address);
1517 sockaddr.sin_family = AF_INET;
1518 sockaddr.sin_port = 0;
1519 sockaddr.sin_addr.s_addr = 0;
1521 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1522 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1524 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1525 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
1527 len = sizeof(address);
1529 sockaddr.sin_family = AF_INET;
1530 sockaddr.sin_port = 0;
1531 sockaddr.sin_addr.s_addr = 0xffffffff;
1533 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1534 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1536 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1538 len = sizeof(address);
1540 sockaddr.sin_family = AF_INET;
1541 sockaddr.sin_port = 0xffff;
1542 sockaddr.sin_addr.s_addr = 0;
1544 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1545 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1547 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1549 len = sizeof(address);
1551 sockaddr.sin_family = AF_INET;
1552 sockaddr.sin_port = 0xffff;
1553 sockaddr.sin_addr.s_addr = 0xffffffff;
1555 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1556 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1558 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1559 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len);
1561 /*check to see it IPv6 is available */
1562 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1563 if (v6 == INVALID_SOCKET) {
1564 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1565 WSAGetLastError(), WSAEAFNOSUPPORT);
1569 /* Test a short IPv6 address */
1570 len = sizeof(address6)/sizeof(WCHAR);
1572 sockaddr6.sin6_family = AF_INET6;
1573 sockaddr6.sin6_port = 0x0000;
1574 sockaddr6.sin6_scope_id = 0;
1575 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1577 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1578 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1579 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
1580 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
1582 /* Test a longer IPv6 address */
1583 len = sizeof(address6)/sizeof(WCHAR);
1585 sockaddr6.sin6_family = AF_INET6;
1586 sockaddr6.sin6_port = 0x0000;
1587 sockaddr6.sin6_scope_id = 0;
1588 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1590 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1591 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1593 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
1594 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
1596 /* Test IPv6 address and port number */
1597 len = sizeof(address6)/sizeof(WCHAR);
1599 sockaddr6.sin6_family = AF_INET6;
1600 sockaddr6.sin6_port = 0xfa81;
1601 sockaddr6.sin6_scope_id = 0;
1602 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1604 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1605 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1608 ok( !lstrcmpW( address6, expect6_3 ), "Wrong string returned\n" );
1609 ok( len == sizeof(expect6_3)/sizeof(WCHAR), "Got %d\n", len);
1612 /* Test IPv6 address, port number and scope_id */
1613 len = sizeof(address6)/sizeof(WCHAR);
1615 sockaddr6.sin6_family = AF_INET6;
1616 sockaddr6.sin6_port = 0xfa81;
1617 sockaddr6.sin6_scope_id = 0x1234;
1618 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1620 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1621 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1624 ok( !lstrcmpW( address6, expect6_3_2 ), "Wrong string returned\n" );
1625 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR), "Got %d\n", len);
1628 /* Test IPv6 address and scope_id */
1629 len = sizeof(address6)/sizeof(WCHAR);
1631 sockaddr6.sin6_family = AF_INET6;
1632 sockaddr6.sin6_port = 0x0000;
1633 sockaddr6.sin6_scope_id = 0xfffe;
1634 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1636 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1637 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1640 ok( !lstrcmpW( address6, expect6_3_3 ), "Wrong string returned\n" );
1641 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR), "Got %d\n", len);
1645 if (v6 != INVALID_SOCKET)
1649 static void test_WSAStringToAddressA(void)
1652 SOCKADDR_IN sockaddr;
1653 SOCKADDR_IN6 sockaddr6;
1656 CHAR address1[] = "0.0.0.0";
1657 CHAR address2[] = "127.127.127.127";
1658 CHAR address3[] = "255.255.255.255";
1659 CHAR address4[] = "127.127.127.127:65535";
1660 CHAR address5[] = "255.255.255.255:65535";
1661 CHAR address6[] = "::1";
1662 CHAR address7[] = "[::1]";
1663 CHAR address8[] = "[::1]:65535";
1666 sockaddr.sin_family = AF_INET;
1668 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1669 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1670 WSAGetLastError() );
1672 len = sizeof(sockaddr);
1673 sockaddr.sin_port = 0;
1674 sockaddr.sin_addr.s_addr = 0;
1676 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1677 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1678 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1680 len = sizeof(sockaddr);
1681 sockaddr.sin_port = 0;
1682 sockaddr.sin_addr.s_addr = 0;
1684 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1685 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1686 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1688 len = sizeof(sockaddr);
1689 sockaddr.sin_port = 0;
1690 sockaddr.sin_addr.s_addr = 0;
1692 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1693 GLE = WSAGetLastError();
1694 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1695 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1696 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1698 len = sizeof(sockaddr);
1699 sockaddr.sin_port = 0;
1700 sockaddr.sin_addr.s_addr = 0;
1702 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1703 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1704 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1706 len = sizeof(sockaddr);
1707 sockaddr.sin_port = 0;
1708 sockaddr.sin_addr.s_addr = 0;
1710 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1711 GLE = WSAGetLastError();
1712 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1713 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1714 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1716 len = sizeof(sockaddr6);
1717 memset(&sockaddr6, 0, len);
1718 sockaddr6.sin6_family = AF_INET6;
1720 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1722 GLE = WSAGetLastError();
1723 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1724 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1726 len = sizeof(sockaddr6);
1727 memset(&sockaddr6, 0, len);
1728 sockaddr6.sin6_family = AF_INET6;
1730 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1732 GLE = WSAGetLastError();
1733 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1734 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1736 len = sizeof(sockaddr6);
1737 memset(&sockaddr6, 0, len);
1738 sockaddr6.sin6_family = AF_INET6;
1740 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1742 GLE = WSAGetLastError();
1743 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1744 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1745 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1749 static void test_WSAStringToAddressW(void)
1752 SOCKADDR_IN sockaddr;
1753 SOCKADDR_IN6 sockaddr6;
1756 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1757 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1758 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1759 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1760 ':', '6', '5', '5', '3', '5', 0 };
1761 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1762 '6', '5', '5', '3', '5', 0 };
1763 WCHAR address6[] = {':',':','1','\0'};
1764 WCHAR address7[] = {'[',':',':','1',']','\0'};
1765 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1768 sockaddr.sin_family = AF_INET;
1770 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1771 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1772 WSAGetLastError() );
1774 len = sizeof(sockaddr);
1775 sockaddr.sin_port = 0;
1776 sockaddr.sin_addr.s_addr = 0;
1778 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1779 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1780 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1782 len = sizeof(sockaddr);
1783 sockaddr.sin_port = 0;
1784 sockaddr.sin_addr.s_addr = 0;
1786 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1787 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1788 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1790 len = sizeof(sockaddr);
1791 sockaddr.sin_port = 0;
1792 sockaddr.sin_addr.s_addr = 0;
1794 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1795 GLE = WSAGetLastError();
1796 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1797 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1798 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1800 len = sizeof(sockaddr);
1801 sockaddr.sin_port = 0;
1802 sockaddr.sin_addr.s_addr = 0;
1804 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1805 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1806 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1808 len = sizeof(sockaddr);
1809 sockaddr.sin_port = 0;
1810 sockaddr.sin_addr.s_addr = 0;
1812 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1813 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1814 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1815 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1817 len = sizeof(sockaddr6);
1818 memset(&sockaddr6, 0, len);
1819 sockaddr6.sin6_family = AF_INET6;
1821 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1823 GLE = WSAGetLastError();
1824 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1825 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1827 len = sizeof(sockaddr6);
1828 memset(&sockaddr6, 0, len);
1829 sockaddr6.sin6_family = AF_INET6;
1831 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1833 GLE = WSAGetLastError();
1834 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1835 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1837 len = sizeof(sockaddr6);
1838 memset(&sockaddr6, 0, len);
1839 sockaddr6.sin6_family = AF_INET6;
1841 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1843 GLE = WSAGetLastError();
1844 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1845 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1846 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1850 static VOID WINAPI SelectReadThread(select_thread_params *par)
1854 struct sockaddr_in addr;
1855 struct timeval select_timeout;
1858 FD_SET(par->s, &readfds);
1859 select_timeout.tv_sec=5;
1860 select_timeout.tv_usec=0;
1861 addr.sin_family = AF_INET;
1862 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1863 addr.sin_port = htons(SERVERPORT);
1865 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1866 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1868 SetEvent(server_ready);
1869 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1870 par->ReadKilled = (ret == 1);
1873 static void test_select(void)
1875 SOCKET fdRead, fdWrite;
1876 fd_set readfds, writefds, exceptfds;
1879 struct timeval select_timeout;
1880 select_thread_params thread_params;
1881 HANDLE thread_handle;
1884 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1885 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1886 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1887 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1891 FD_ZERO(&exceptfds);
1892 FD_SET(fdRead, &readfds);
1893 FD_SET(fdWrite, &writefds);
1894 FD_SET(fdRead, &exceptfds);
1895 FD_SET(fdWrite, &exceptfds);
1896 select_timeout.tv_sec=0;
1897 select_timeout.tv_usec=500;
1900 if (fdWrite > maxfd)
1904 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1905 ok ( (ret == 0), "select should not return any socket handles\n");
1906 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1907 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1910 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1911 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1914 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1916 ret = closesocket(fdWrite);
1917 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1919 thread_params.s = fdRead;
1920 thread_params.ReadKilled = FALSE;
1921 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1922 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1923 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1925 WaitForSingleObject (server_ready, INFINITE);
1927 ret = closesocket(fdRead);
1928 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1930 WaitForSingleObject (thread_handle, 1000);
1931 ok ( (thread_params.ReadKilled) ||
1932 broken(thread_params.ReadKilled == 0), /*Win98*/
1933 "closesocket did not wakeup select\n");
1937 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1939 struct sockaddr_in address;
1940 int len = sizeof(address);
1941 SOCKET client_socket;
1943 SetEvent(server_ready);
1944 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1945 if (client_socket != INVALID_SOCKET)
1946 closesocket(client_socket);
1947 par->ReadKilled = (client_socket == INVALID_SOCKET);
1951 static void test_accept(void)
1954 SOCKET server_socket = INVALID_SOCKET;
1955 struct sockaddr_in address;
1956 select_thread_params thread_params;
1957 HANDLE thread_handle = NULL;
1960 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1961 if (server_socket == INVALID_SOCKET)
1963 trace("error creating server socket: %d\n", WSAGetLastError());
1967 memset(&address, 0, sizeof(address));
1968 address.sin_family = AF_INET;
1969 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1972 trace("error binding server socket: %d\n", WSAGetLastError());
1976 ret = listen(server_socket, 1);
1979 trace("error making server socket listen: %d\n", WSAGetLastError());
1983 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1984 if (server_ready == INVALID_HANDLE_VALUE)
1986 trace("error creating event: %d\n", GetLastError());
1990 thread_params.s = server_socket;
1991 thread_params.ReadKilled = FALSE;
1992 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1993 &thread_params, 0, &id);
1994 if (thread_handle == NULL)
1996 trace("error creating thread: %d\n", GetLastError());
2000 WaitForSingleObject(server_ready, INFINITE);
2002 ret = closesocket(server_socket);
2005 trace("closesocket failed: %d\n", WSAGetLastError());
2009 WaitForSingleObject(thread_handle, 1000);
2010 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
2013 if (thread_handle != NULL)
2014 CloseHandle(thread_handle);
2015 if (server_ready != INVALID_HANDLE_VALUE)
2016 CloseHandle(server_ready);
2017 if (server_socket != INVALID_SOCKET)
2018 closesocket(server_socket);
2021 static void test_extendedSocketOptions(void)
2025 struct sockaddr_in sa;
2026 int sa_len = sizeof(struct sockaddr_in);
2027 int optval, optlen = sizeof(int), ret;
2031 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2032 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2036 memset(&sa, 0, sa_len);
2038 sa.sin_family = AF_INET;
2039 sa.sin_port = htons(0);
2040 sa.sin_addr.s_addr = htonl(INADDR_ANY);
2042 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
2043 trace("Creating the socket failed: %d\n", WSAGetLastError());
2048 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2049 trace("Failed to bind socket: %d\n", WSAGetLastError());
2055 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2057 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
2058 ok((optval == 65507) || (optval == 65527),
2059 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
2061 optlen = sizeof(LINGER);
2062 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2064 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
2069 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2070 trace("Creating the socket failed: %d\n", WSAGetLastError());
2075 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2076 trace("Failed to bind socket: %d\n", WSAGetLastError());
2082 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2083 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
2085 optlen = sizeof(BOOL);
2086 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
2087 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
2088 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
2089 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2090 bool_opt_val, linger_val.l_onoff);
2096 static void test_getsockname(void)
2100 struct sockaddr_in sa_set, sa_get;
2101 int sa_set_len = sizeof(struct sockaddr_in);
2102 int sa_get_len = sa_set_len;
2103 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
2106 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2107 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2111 memset(&sa_set, 0, sa_set_len);
2113 sa_set.sin_family = AF_INET;
2114 sa_set.sin_port = htons(0);
2115 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
2117 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2118 trace("Creating the socket failed: %d\n", WSAGetLastError());
2123 memcpy(&sa_get, &sa_set, sizeof(sa_set));
2124 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
2125 ok(0, "getsockname on unbound socket should fail\n");
2127 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
2128 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
2129 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
2130 "failed getsockname modified sockaddr when it shouldn't\n");
2133 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
2134 trace("Failed to bind socket: %d\n", WSAGetLastError());
2140 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
2141 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2147 ret = memcmp(sa_get.sin_zero, null_padding, 8);
2148 ok(ret == 0 || broken(ret != 0), /* NT4 */
2149 "getsockname did not zero the sockaddr_in structure\n");
2155 static void test_dns(void)
2159 h = gethostbyname("");
2160 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
2163 /* Our winsock headers don't define gethostname because it conflicts with the
2164 * definition in unistd.h. Define it here to get rid of the warning. */
2166 int WINAPI gethostname(char *name, int namelen);
2168 static void test_gethostbyname_hack(void)
2172 static BYTE loopback[] = {127, 0, 0, 1};
2173 static BYTE magic_loopback[] = {127, 12, 34, 56};
2176 ret = gethostname(name, 256);
2177 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
2179 he = gethostbyname("localhost");
2180 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
2183 if(he->h_length != 4)
2185 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2189 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
2190 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2191 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
2192 he->h_addr_list[0][3]);
2195 /* No reason to test further with NULL hostname */
2199 if(strcmp(name, "localhost") == 0)
2201 skip("hostname seems to be \"localhost\", skipping test.\n");
2206 he = gethostbyname(name);
2207 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
2210 if(he->h_length != 4)
2212 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2216 if (he->h_addr_list[0][0] == 127)
2218 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
2219 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2220 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
2221 he->h_addr_list[0][2], he->h_addr_list[0][3]);
2226 he = gethostbyname("nonexistent.winehq.org");
2227 /* Don't check for the return value, as some braindead ISPs will kindly
2228 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2231 static void test_inet_addr(void)
2235 addr = inet_addr(NULL);
2236 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
2239 static void test_addr_to_print(void)
2245 struct in6_addr in6;
2247 u_long addr0_Num = 0x00000000;
2248 PCSTR addr0_Str = "0.0.0.0";
2249 u_long addr1_Num = 0x20201015;
2250 PCSTR addr1_Str = "21.16.32.32";
2251 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2252 PCSTR addr2_Str = "::fffe:cc98:bd74";
2253 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
2254 PCSTR addr3_Str = "2030:a4b1::";
2256 in.s_addr = addr0_Num;
2258 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2259 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2260 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
2262 /* Test that inet_ntoa and inet_ntop return the same value */
2263 in.S_un.S_addr = addr1_Num;
2264 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2265 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2266 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2268 /* InetNtop became available in Vista and Win2008 */
2271 win_skip("InetNtop not present, not executing tests\n");
2275 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
2276 ok(pdst != NULL, "InetNtop failed %s\n", dst);
2277 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2279 /* Test invalid parm conditions */
2280 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
2281 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2282 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
2284 /* Test an zero prefixed IPV6 address */
2285 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2286 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2287 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2288 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
2290 /* Test an zero suffixed IPV6 address */
2291 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
2292 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2293 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2294 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
2297 static void test_ioctlsocket(void)
2301 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2304 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2305 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2306 if(sock == INVALID_SOCKET)
2308 skip("Can't continue without a socket.\n");
2312 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
2314 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2315 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
2316 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
2317 ret = WSAGetLastError();
2318 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
2322 static int drain_pause=0;
2323 static DWORD WINAPI drain_socket_thread(LPVOID arg)
2326 SOCKET sock = *(SOCKET*)arg;
2329 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
2333 if (WSAGetLastError() == WSAEWOULDBLOCK)
2337 FD_SET(sock, &readset);
2338 select(0, &readset, NULL, NULL, NULL);
2349 static void test_send(void)
2351 SOCKET src = INVALID_SOCKET;
2352 SOCKET dst = INVALID_SOCKET;
2353 HANDLE hThread = NULL;
2354 const int buflen = 1024*1024;
2355 char *buffer = NULL;
2359 if (tcp_socketpair(&src, &dst) != 0)
2361 ok(0, "creating socket pair failed, skipping test\n");
2365 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2366 if (hThread == NULL)
2368 ok(0, "CreateThread failed, error %d\n", GetLastError());
2372 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
2375 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2379 ret = send(src, buffer, buflen, 0);
2381 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
2383 ok(0, "send failed, error %d\n", WSAGetLastError());
2386 if (src != INVALID_SOCKET)
2388 if (dst != INVALID_SOCKET)
2390 if (hThread != NULL)
2391 CloseHandle(hThread);
2392 HeapFree(GetProcessHeap(), 0, buffer);
2395 static void test_write_events(void)
2397 SOCKET src = INVALID_SOCKET;
2398 SOCKET dst = INVALID_SOCKET;
2399 HANDLE hThread = NULL;
2400 HANDLE hEvent = INVALID_HANDLE_VALUE;
2401 char *buffer = NULL;
2402 int bufferSize = 1024*1024;
2406 WSANETWORKEVENTS netEvents;
2409 if (tcp_socketpair(&src, &dst) != 0)
2411 ok(0, "creating socket pair failed, skipping test\n");
2415 /* On Windows it seems when a non-blocking socket sends to a
2416 blocking socket on the same host, the send() is BLOCKING,
2417 so make both sockets non-blocking */
2418 ret = ioctlsocket(src, FIONBIO, &one);
2421 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2424 ret = ioctlsocket(dst, FIONBIO, &one);
2427 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2431 buffer = HeapAlloc(GetProcessHeap(), 0, bufferSize);
2434 ok(0, "could not allocate memory for test\n");
2438 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2439 if (hThread == NULL)
2441 ok(0, "CreateThread failed, error %d\n", GetLastError());
2445 hEvent = CreateEventA(NULL, FALSE, TRUE, NULL);
2446 if (hEvent == INVALID_HANDLE_VALUE)
2448 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2452 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
2455 ok(0, "WSAEventSelect failed, error %d\n", ret);
2459 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2460 dwRet = WaitForSingleObject(hEvent, 5000);
2461 if (dwRet != WAIT_OBJECT_0)
2463 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet);
2466 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2469 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2472 if (netEvents.lNetworkEvents & FD_WRITE)
2474 ret = send(src, "a", 1, 0);
2475 ok(ret == 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2481 ok(0, "FD_WRITE not among initial events\n");
2485 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2486 dwRet = WaitForSingleObject(hEvent, 2000);
2487 if (dwRet == WAIT_OBJECT_0)
2489 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2493 /* Now if we send a ton of data and the 'server' does not drain it fast
2494 * enough (set drain_pause to be sure), the socket send buffer will only
2495 * take some of it, and we will get a short write. This will trigger
2496 * another FD_WRITE event as soon as data is sent and more space becomes
2497 * available, but not any earlier. */
2501 ret = send(src, buffer, bufferSize, 0);
2502 } while (ret == bufferSize);
2504 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
2506 dwRet = WaitForSingleObject(hEvent, 5000);
2507 ok(dwRet == WAIT_OBJECT_0, "Waiting failed with %d\n", dwRet);
2508 if (dwRet == WAIT_OBJECT_0)
2510 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2511 ok(ret == 0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2514 ok(netEvents.lNetworkEvents & FD_WRITE,
2515 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents.lNetworkEvents);
2522 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2527 HeapFree(GetProcessHeap(), 0, buffer);
2528 if (src != INVALID_SOCKET)
2530 if (dst != INVALID_SOCKET)
2532 if (hThread != NULL)
2533 CloseHandle(hThread);
2534 CloseHandle(hEvent);
2537 static void test_ipv6only(void)
2539 SOCKET v4 = INVALID_SOCKET,
2540 v6 = INVALID_SOCKET;
2541 struct sockaddr_in sin4;
2542 struct sockaddr_in6 sin6;
2545 memset(&sin4, 0, sizeof(sin4));
2546 sin4.sin_family = AF_INET;
2547 sin4.sin_port = htons(SERVERPORT);
2549 memset(&sin6, 0, sizeof(sin6));
2550 sin6.sin6_family = AF_INET6;
2551 sin6.sin6_port = htons(SERVERPORT);
2553 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2554 if (v6 == INVALID_SOCKET) {
2555 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2556 WSAGetLastError(), WSAEAFNOSUPPORT);
2559 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2561 skip("Could not bind IPv6 address (LastError: %d).\n",
2566 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2567 if (v4 == INVALID_SOCKET) {
2568 skip("Could not create IPv4 socket (LastError: %d).\n",
2572 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2573 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2574 WSAGetLastError(), WSAEADDRINUSE);
2577 if (v4 != INVALID_SOCKET)
2579 if (v6 != INVALID_SOCKET)
2583 static void test_WSASendTo(void)
2586 struct sockaddr_in addr;
2587 char buf[12] = "hello world";
2591 addr.sin_family = AF_INET;
2592 addr.sin_port = htons(139);
2593 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2594 data_buf.len = sizeof(buf);
2597 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2598 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2602 WSASetLastError(12345);
2603 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
2604 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2607 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2608 "a successful call to WSASendTo()\n");
2611 static void test_GetAddrInfoW(void)
2613 static const WCHAR port[] = {'8','0',0};
2614 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
2617 ADDRINFOW *result, hint;
2619 if (!pGetAddrInfoW || !pFreeAddrInfoW)
2621 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
2625 memset(&hint, 0, sizeof(ADDRINFOW));
2627 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
2628 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
2630 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
2631 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2632 pFreeAddrInfoW(result);
2634 ret = pGetAddrInfoW(localhost, port, NULL, &result);
2635 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2636 pFreeAddrInfoW(result);
2638 ret = pGetAddrInfoW(localhost, port, &hint, &result);
2639 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2640 pFreeAddrInfoW(result);
2643 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
2644 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
2645 GROUP FAR * g, DWORD_PTR dwCallbackData)
2650 static void test_AcceptEx(void)
2652 SOCKET listener = INVALID_SOCKET;
2653 SOCKET acceptor = INVALID_SOCKET;
2654 SOCKET connector = INVALID_SOCKET;
2655 SOCKET connector2 = INVALID_SOCKET;
2656 struct sockaddr_in bindAddress;
2658 GUID acceptExGuid = WSAID_ACCEPTEX;
2659 LPFN_ACCEPTEX pAcceptEx = NULL;
2660 fd_set fds_accept, fds_send;
2661 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
2663 DWORD bytesReturned;
2665 OVERLAPPED overlapped;
2670 memset(&overlapped, 0, sizeof(overlapped));
2672 listener = socket(AF_INET, SOCK_STREAM, 0);
2673 if (listener == INVALID_SOCKET) {
2674 skip("could not create listener socket, error %d\n", WSAGetLastError());
2678 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2679 if (acceptor == INVALID_SOCKET) {
2680 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2684 connector = socket(AF_INET, SOCK_STREAM, 0);
2685 if (connector == INVALID_SOCKET) {
2686 skip("could not create connector socket, error %d\n", WSAGetLastError());
2690 memset(&bindAddress, 0, sizeof(bindAddress));
2691 bindAddress.sin_family = AF_INET;
2692 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
2693 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2695 skip("failed to bind, error %d\n", WSAGetLastError());
2699 socklen = sizeof(bindAddress);
2700 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
2702 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2706 if (set_blocking(listener, FALSE)) {
2707 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2711 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
2712 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
2714 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
2718 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2719 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2720 &bytesReturned, &overlapped);
2721 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
2722 "returned %d + errno %d\n", bret, WSAGetLastError());
2724 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2725 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2726 &bytesReturned, &overlapped);
2727 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
2728 "returned %d + errno %d\n", bret, WSAGetLastError());
2730 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2731 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2732 &bytesReturned, &overlapped);
2734 (WSAGetLastError() == WSAEINVAL ||
2735 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
2736 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
2738 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
2739 &bytesReturned, &overlapped);
2740 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
2741 "returned %d + errno %d\n", bret, WSAGetLastError());
2743 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
2744 &bytesReturned, &overlapped);
2745 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
2746 "returned %d + errno %d\n", bret, WSAGetLastError());
2748 bret = pAcceptEx(listener, acceptor, buffer, 0,
2749 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2750 &bytesReturned, NULL);
2751 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
2752 "returned %d + errno %d\n", bret, WSAGetLastError());
2754 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2755 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2756 &bytesReturned, &overlapped);
2757 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
2758 "returned %d + errno %d\n", bret, WSAGetLastError());
2760 iret = listen(listener, 5);
2762 skip("listening failed, errno = %d\n", WSAGetLastError());
2766 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2767 if (overlapped.hEvent == NULL) {
2768 skip("could not create event object, errno = %d\n", GetLastError());
2772 bret = pAcceptEx(listener, acceptor, buffer, 0,
2773 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2774 &bytesReturned, &overlapped);
2775 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2777 bret = pAcceptEx(listener, acceptor, buffer, 0,
2778 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2779 &bytesReturned, &overlapped);
2780 ok((bret == FALSE && WSAGetLastError() == WSAEINVAL) || broken(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) /* NT4 */,
2781 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
2782 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
2783 /* We need to cancel this call, otherwise things fail */
2784 bret = CancelIo((HANDLE) listener);
2785 ok(bret, "Failed to cancel failed test. Bailing...\n");
2788 bret = pAcceptEx(listener, acceptor, buffer, 0,
2789 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2790 &bytesReturned, &overlapped);
2791 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2794 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2795 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2797 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
2798 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2800 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2801 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2802 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2804 closesocket(connector);
2805 connector = INVALID_SOCKET;
2806 closesocket(acceptor);
2807 acceptor = INVALID_SOCKET;
2809 /* Test short reads */
2811 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2812 if (acceptor == INVALID_SOCKET) {
2813 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2816 connector = socket(AF_INET, SOCK_STREAM, 0);
2817 if (connector == INVALID_SOCKET) {
2818 skip("could not create connector socket, error %d\n", WSAGetLastError());
2821 bret = pAcceptEx(listener, acceptor, buffer, 2,
2822 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2823 &bytesReturned, &overlapped);
2824 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2826 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2827 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2829 dwret = WaitForSingleObject(overlapped.hEvent, 0);
2830 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
2832 iret = send(connector, buffer, 1, 0);
2833 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
2835 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
2836 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2838 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2839 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2840 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2842 closesocket(connector);
2843 connector = INVALID_SOCKET;
2844 closesocket(acceptor);
2845 acceptor = INVALID_SOCKET;
2847 /* Test CF_DEFER & AcceptEx interaction */
2849 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2850 if (acceptor == INVALID_SOCKET) {
2851 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2854 connector = socket(AF_INET, SOCK_STREAM, 0);
2855 if (connector == INVALID_SOCKET) {
2856 skip("could not create connector socket, error %d\n", WSAGetLastError());
2859 connector2 = socket(AF_INET, SOCK_STREAM, 0);
2860 if (connector == INVALID_SOCKET) {
2861 skip("could not create connector socket, error %d\n", WSAGetLastError());
2865 if (set_blocking(connector, FALSE)) {
2866 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2870 if (set_blocking(connector2, FALSE)) {
2871 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2875 /* Connect socket #1 */
2876 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2877 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2879 FD_ZERO ( &fds_accept );
2880 FD_ZERO ( &fds_send );
2882 FD_SET ( listener, &fds_accept );
2883 FD_SET ( connector, &fds_send );
2889 for (i = 0; i < 4000; ++i)
2891 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
2893 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
2894 "acceptex test(%d): could not select on socket, errno %d\n" );
2896 /* check for incoming requests */
2897 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
2900 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
2901 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
2902 bret = pAcceptEx(listener, acceptor, buffer, 0,
2903 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2904 &bytesReturned, &overlapped);
2905 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2907 else if (got == 2) {
2908 /* this should be socket #2 */
2909 SOCKET tmp = accept(listener, NULL, NULL);
2910 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
2914 ok(FALSE, "Got more than 2 connections?\n");
2917 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
2918 /* Send data on second socket, and stop */
2919 send(connector2, "2", 1, 0);
2920 FD_CLR ( connector2, &fds_send );
2924 if ( FD_ISSET ( connector, &fds_opensend ) ) {
2925 /* Once #1 is connected, allow #2 to connect */
2928 send(connector, "1", 1, 0);
2929 FD_CLR ( connector, &fds_send );
2931 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2932 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2933 FD_SET ( connector2, &fds_send );
2937 ok (got == 2 || broken(got == 1) /* NT4 */,
2938 "Did not get both connections, got %d\n", got);
2940 dwret = WaitForSingleObject(overlapped.hEvent, 0);
2941 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2943 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2944 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2945 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2947 set_blocking(acceptor, TRUE);
2948 iret = recv( acceptor, buffer, 2, 0);
2949 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
2951 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
2953 closesocket(connector);
2954 connector = INVALID_SOCKET;
2955 closesocket(acceptor);
2956 acceptor = INVALID_SOCKET;
2958 /* clean up in case of failures */
2959 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
2960 closesocket(acceptor);
2962 /* Disconnect during receive? */
2964 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2965 if (acceptor == INVALID_SOCKET) {
2966 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2969 connector = socket(AF_INET, SOCK_STREAM, 0);
2970 if (connector == INVALID_SOCKET) {
2971 skip("could not create connector socket, error %d\n", WSAGetLastError());
2974 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2975 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2976 &bytesReturned, &overlapped);
2977 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2979 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2980 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2982 closesocket(connector);
2983 connector = INVALID_SOCKET;
2985 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
2986 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2988 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2989 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2990 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2992 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2993 if (acceptor == INVALID_SOCKET) {
2994 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2997 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2998 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2999 &bytesReturned, &overlapped);
3000 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3002 closesocket(listener);
3003 listener = INVALID_SOCKET;
3005 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3006 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3008 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3009 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
3012 if (overlapped.hEvent)
3013 WSACloseEvent(overlapped.hEvent);
3014 if (listener != INVALID_SOCKET)
3015 closesocket(listener);
3016 if (acceptor != INVALID_SOCKET)
3017 closesocket(acceptor);
3018 if (connector != INVALID_SOCKET)
3019 closesocket(connector);
3020 if (connector2 != INVALID_SOCKET)
3021 closesocket(connector2);
3024 /**************** Main program ***************/
3030 /* Leave these tests at the beginning. They depend on WSAStartup not having been
3031 * called, which is done by Init() below. */
3032 test_WithoutWSAStartup();
3033 test_WithWSAStartup();
3037 test_set_getsockopt();
3038 test_so_reuseaddr();
3039 test_extendedSocketOptions();
3041 for (i = 0; i < NUM_TESTS; i++)
3043 trace ( " **** STARTING TEST %d ****\n", i );
3044 do_test ( &tests[i] );
3045 trace ( " **** TEST %d COMPLETE ****\n", i );
3050 test_getservbyname();
3053 test_WSAAddressToStringA();
3054 test_WSAAddressToStringW();
3056 test_WSAStringToAddressA();
3057 test_WSAStringToAddressW();
3063 test_addr_to_print();
3066 test_gethostbyname_hack();
3069 test_write_events();
3074 test_GetAddrInfoW();