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 );
1580 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1582 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
1583 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
1586 /* Test a longer IPv6 address */
1587 len = sizeof(address6)/sizeof(WCHAR);
1589 sockaddr6.sin6_family = AF_INET6;
1590 sockaddr6.sin6_port = 0x0000;
1591 sockaddr6.sin6_scope_id = 0;
1592 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1594 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1597 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1599 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
1600 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
1603 /* Test IPv6 address and port number */
1604 len = sizeof(address6)/sizeof(WCHAR);
1606 sockaddr6.sin6_family = AF_INET6;
1607 sockaddr6.sin6_port = 0xfa81;
1608 sockaddr6.sin6_scope_id = 0;
1609 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1611 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1614 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1616 ok( !lstrcmpW( address6, expect6_3 ), "Wrong string returned\n" );
1617 ok( len == sizeof(expect6_3)/sizeof(WCHAR), "Got %d\n", len);
1620 /* Test IPv6 address, port number and scope_id */
1621 len = sizeof(address6)/sizeof(WCHAR);
1623 sockaddr6.sin6_family = AF_INET6;
1624 sockaddr6.sin6_port = 0xfa81;
1625 sockaddr6.sin6_scope_id = 0x1234;
1626 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1628 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1631 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1632 ok( !lstrcmpW( address6, expect6_3_2 ), "Wrong string returned\n" );
1633 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR), "Got %d\n", len);
1636 /* Test IPv6 address and scope_id */
1637 len = sizeof(address6)/sizeof(WCHAR);
1639 sockaddr6.sin6_family = AF_INET6;
1640 sockaddr6.sin6_port = 0x0000;
1641 sockaddr6.sin6_scope_id = 0xfffe;
1642 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1644 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1647 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1649 ok( !lstrcmpW( address6, expect6_3_3 ), "Wrong string returned\n" );
1650 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR), "Got %d\n", len);
1654 if (v6 != INVALID_SOCKET)
1658 static void test_WSAStringToAddressA(void)
1661 SOCKADDR_IN sockaddr;
1662 SOCKADDR_IN6 sockaddr6;
1665 CHAR address1[] = "0.0.0.0";
1666 CHAR address2[] = "127.127.127.127";
1667 CHAR address3[] = "255.255.255.255";
1668 CHAR address4[] = "127.127.127.127:65535";
1669 CHAR address5[] = "255.255.255.255:65535";
1670 CHAR address6[] = "::1";
1671 CHAR address7[] = "[::1]";
1672 CHAR address8[] = "[::1]:65535";
1675 sockaddr.sin_family = AF_INET;
1677 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1678 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1679 WSAGetLastError() );
1681 len = sizeof(sockaddr);
1682 sockaddr.sin_port = 0;
1683 sockaddr.sin_addr.s_addr = 0;
1685 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1686 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1687 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1689 len = sizeof(sockaddr);
1690 sockaddr.sin_port = 0;
1691 sockaddr.sin_addr.s_addr = 0;
1693 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1694 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1695 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1697 len = sizeof(sockaddr);
1698 sockaddr.sin_port = 0;
1699 sockaddr.sin_addr.s_addr = 0;
1701 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1702 GLE = WSAGetLastError();
1703 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1704 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1705 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1707 len = sizeof(sockaddr);
1708 sockaddr.sin_port = 0;
1709 sockaddr.sin_addr.s_addr = 0;
1711 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1712 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1713 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1715 len = sizeof(sockaddr);
1716 sockaddr.sin_port = 0;
1717 sockaddr.sin_addr.s_addr = 0;
1719 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1720 GLE = WSAGetLastError();
1721 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1722 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1723 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1725 len = sizeof(sockaddr6);
1726 memset(&sockaddr6, 0, len);
1727 sockaddr6.sin6_family = AF_INET6;
1729 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1731 GLE = WSAGetLastError();
1732 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1733 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1735 len = sizeof(sockaddr6);
1736 memset(&sockaddr6, 0, len);
1737 sockaddr6.sin6_family = AF_INET6;
1739 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1741 GLE = WSAGetLastError();
1742 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1743 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1745 len = sizeof(sockaddr6);
1746 memset(&sockaddr6, 0, len);
1747 sockaddr6.sin6_family = AF_INET6;
1749 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1751 GLE = WSAGetLastError();
1752 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1753 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1754 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1758 static void test_WSAStringToAddressW(void)
1761 SOCKADDR_IN sockaddr;
1762 SOCKADDR_IN6 sockaddr6;
1765 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1766 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1767 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1768 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1769 ':', '6', '5', '5', '3', '5', 0 };
1770 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1771 '6', '5', '5', '3', '5', 0 };
1772 WCHAR address6[] = {':',':','1','\0'};
1773 WCHAR address7[] = {'[',':',':','1',']','\0'};
1774 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1777 sockaddr.sin_family = AF_INET;
1779 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1780 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1781 WSAGetLastError() );
1783 len = sizeof(sockaddr);
1784 sockaddr.sin_port = 0;
1785 sockaddr.sin_addr.s_addr = 0;
1787 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1788 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1789 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1791 len = sizeof(sockaddr);
1792 sockaddr.sin_port = 0;
1793 sockaddr.sin_addr.s_addr = 0;
1795 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1796 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1797 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1799 len = sizeof(sockaddr);
1800 sockaddr.sin_port = 0;
1801 sockaddr.sin_addr.s_addr = 0;
1803 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1804 GLE = WSAGetLastError();
1805 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1806 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1807 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1809 len = sizeof(sockaddr);
1810 sockaddr.sin_port = 0;
1811 sockaddr.sin_addr.s_addr = 0;
1813 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1814 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1815 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1817 len = sizeof(sockaddr);
1818 sockaddr.sin_port = 0;
1819 sockaddr.sin_addr.s_addr = 0;
1821 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1822 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1823 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1824 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1826 len = sizeof(sockaddr6);
1827 memset(&sockaddr6, 0, len);
1828 sockaddr6.sin6_family = AF_INET6;
1830 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1832 GLE = WSAGetLastError();
1833 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1834 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1836 len = sizeof(sockaddr6);
1837 memset(&sockaddr6, 0, len);
1838 sockaddr6.sin6_family = AF_INET6;
1840 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1842 GLE = WSAGetLastError();
1843 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1844 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1846 len = sizeof(sockaddr6);
1847 memset(&sockaddr6, 0, len);
1848 sockaddr6.sin6_family = AF_INET6;
1850 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1852 GLE = WSAGetLastError();
1853 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1854 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1855 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1859 static VOID WINAPI SelectReadThread(select_thread_params *par)
1863 struct sockaddr_in addr;
1864 struct timeval select_timeout;
1867 FD_SET(par->s, &readfds);
1868 select_timeout.tv_sec=5;
1869 select_timeout.tv_usec=0;
1870 addr.sin_family = AF_INET;
1871 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1872 addr.sin_port = htons(SERVERPORT);
1874 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1875 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1877 SetEvent(server_ready);
1878 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1879 par->ReadKilled = (ret == 1);
1882 static void test_select(void)
1884 SOCKET fdRead, fdWrite;
1885 fd_set readfds, writefds, exceptfds;
1888 struct timeval select_timeout;
1889 select_thread_params thread_params;
1890 HANDLE thread_handle;
1893 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1894 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1895 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1896 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1900 FD_ZERO(&exceptfds);
1901 FD_SET(fdRead, &readfds);
1902 FD_SET(fdWrite, &writefds);
1903 FD_SET(fdRead, &exceptfds);
1904 FD_SET(fdWrite, &exceptfds);
1905 select_timeout.tv_sec=0;
1906 select_timeout.tv_usec=500;
1909 if (fdWrite > maxfd)
1913 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1914 ok ( (ret == 0), "select should not return any socket handles\n");
1915 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1916 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1919 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1920 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1923 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1925 ret = closesocket(fdWrite);
1926 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1928 thread_params.s = fdRead;
1929 thread_params.ReadKilled = FALSE;
1930 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1931 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1932 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1934 WaitForSingleObject (server_ready, INFINITE);
1936 ret = closesocket(fdRead);
1937 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1939 WaitForSingleObject (thread_handle, 1000);
1940 ok ( (thread_params.ReadKilled) ||
1941 broken(thread_params.ReadKilled == 0), /*Win98*/
1942 "closesocket did not wakeup select\n");
1946 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1948 struct sockaddr_in address;
1949 int len = sizeof(address);
1950 SOCKET client_socket;
1952 SetEvent(server_ready);
1953 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1954 if (client_socket != INVALID_SOCKET)
1955 closesocket(client_socket);
1956 par->ReadKilled = (client_socket == INVALID_SOCKET);
1960 static void test_accept(void)
1963 SOCKET server_socket = INVALID_SOCKET;
1964 struct sockaddr_in address;
1965 select_thread_params thread_params;
1966 HANDLE thread_handle = NULL;
1969 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1970 if (server_socket == INVALID_SOCKET)
1972 trace("error creating server socket: %d\n", WSAGetLastError());
1976 memset(&address, 0, sizeof(address));
1977 address.sin_family = AF_INET;
1978 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1981 trace("error binding server socket: %d\n", WSAGetLastError());
1985 ret = listen(server_socket, 1);
1988 trace("error making server socket listen: %d\n", WSAGetLastError());
1992 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1993 if (server_ready == INVALID_HANDLE_VALUE)
1995 trace("error creating event: %d\n", GetLastError());
1999 thread_params.s = server_socket;
2000 thread_params.ReadKilled = FALSE;
2001 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
2002 &thread_params, 0, &id);
2003 if (thread_handle == NULL)
2005 trace("error creating thread: %d\n", GetLastError());
2009 WaitForSingleObject(server_ready, INFINITE);
2011 ret = closesocket(server_socket);
2014 trace("closesocket failed: %d\n", WSAGetLastError());
2018 WaitForSingleObject(thread_handle, 1000);
2019 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
2022 if (thread_handle != NULL)
2023 CloseHandle(thread_handle);
2024 if (server_ready != INVALID_HANDLE_VALUE)
2025 CloseHandle(server_ready);
2026 if (server_socket != INVALID_SOCKET)
2027 closesocket(server_socket);
2030 static void test_extendedSocketOptions(void)
2034 struct sockaddr_in sa;
2035 int sa_len = sizeof(struct sockaddr_in);
2036 int optval, optlen = sizeof(int), ret;
2040 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2041 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2045 memset(&sa, 0, sa_len);
2047 sa.sin_family = AF_INET;
2048 sa.sin_port = htons(0);
2049 sa.sin_addr.s_addr = htonl(INADDR_ANY);
2051 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
2052 trace("Creating the socket failed: %d\n", WSAGetLastError());
2057 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2058 trace("Failed to bind socket: %d\n", WSAGetLastError());
2064 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2066 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
2067 ok((optval == 65507) || (optval == 65527),
2068 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
2070 optlen = sizeof(LINGER);
2071 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2073 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
2078 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2079 trace("Creating the socket failed: %d\n", WSAGetLastError());
2084 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2085 trace("Failed to bind socket: %d\n", WSAGetLastError());
2091 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2092 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
2094 optlen = sizeof(BOOL);
2095 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
2096 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
2097 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
2098 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2099 bool_opt_val, linger_val.l_onoff);
2105 static void test_getsockname(void)
2109 struct sockaddr_in sa_set, sa_get;
2110 int sa_set_len = sizeof(struct sockaddr_in);
2111 int sa_get_len = sa_set_len;
2112 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
2115 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2116 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2120 memset(&sa_set, 0, sa_set_len);
2122 sa_set.sin_family = AF_INET;
2123 sa_set.sin_port = htons(0);
2124 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
2126 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2127 trace("Creating the socket failed: %d\n", WSAGetLastError());
2132 memcpy(&sa_get, &sa_set, sizeof(sa_set));
2133 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
2134 ok(0, "getsockname on unbound socket should fail\n");
2136 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
2137 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
2138 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
2139 "failed getsockname modified sockaddr when it shouldn't\n");
2142 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
2143 trace("Failed to bind socket: %d\n", WSAGetLastError());
2149 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
2150 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2156 ret = memcmp(sa_get.sin_zero, null_padding, 8);
2157 ok(ret == 0 || broken(ret != 0), /* NT4 */
2158 "getsockname did not zero the sockaddr_in structure\n");
2164 static void test_dns(void)
2168 h = gethostbyname("");
2169 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
2172 /* Our winsock headers don't define gethostname because it conflicts with the
2173 * definition in unistd.h. Define it here to get rid of the warning. */
2175 int WINAPI gethostname(char *name, int namelen);
2177 static void test_gethostbyname_hack(void)
2181 static BYTE loopback[] = {127, 0, 0, 1};
2182 static BYTE magic_loopback[] = {127, 12, 34, 56};
2185 ret = gethostname(name, 256);
2186 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
2188 he = gethostbyname("localhost");
2189 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
2192 if(he->h_length != 4)
2194 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2198 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
2199 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2200 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
2201 he->h_addr_list[0][3]);
2204 /* No reason to test further with NULL hostname */
2208 if(strcmp(name, "localhost") == 0)
2210 skip("hostname seems to be \"localhost\", skipping test.\n");
2215 he = gethostbyname(name);
2216 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
2219 if(he->h_length != 4)
2221 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2225 if (he->h_addr_list[0][0] == 127)
2227 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
2228 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2229 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
2230 he->h_addr_list[0][2], he->h_addr_list[0][3]);
2235 he = gethostbyname("nonexistent.winehq.org");
2236 /* Don't check for the return value, as some braindead ISPs will kindly
2237 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2240 static void test_inet_addr(void)
2244 addr = inet_addr(NULL);
2245 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
2248 static void test_addr_to_print(void)
2254 struct in6_addr in6;
2256 u_long addr0_Num = 0x00000000;
2257 PCSTR addr0_Str = "0.0.0.0";
2258 u_long addr1_Num = 0x20201015;
2259 PCSTR addr1_Str = "21.16.32.32";
2260 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2261 PCSTR addr2_Str = "::fffe:cc98:bd74";
2262 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
2263 PCSTR addr3_Str = "2030:a4b1::";
2265 in.s_addr = addr0_Num;
2267 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2268 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2269 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
2271 /* Test that inet_ntoa and inet_ntop return the same value */
2272 in.S_un.S_addr = addr1_Num;
2273 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2274 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2275 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2277 /* InetNtop became available in Vista and Win2008 */
2280 win_skip("InetNtop not present, not executing tests\n");
2284 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
2285 ok(pdst != NULL, "InetNtop failed %s\n", dst);
2286 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2288 /* Test invalid parm conditions */
2289 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
2290 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2291 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
2293 /* Test an zero prefixed IPV6 address */
2294 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2295 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2296 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2297 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
2299 /* Test an zero suffixed IPV6 address */
2300 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
2301 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2302 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2303 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
2306 static void test_ioctlsocket(void)
2310 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2313 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2314 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2315 if(sock == INVALID_SOCKET)
2317 skip("Can't continue without a socket.\n");
2321 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
2323 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2324 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
2325 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
2326 ret = WSAGetLastError();
2327 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
2331 static int drain_pause=0;
2332 static DWORD WINAPI drain_socket_thread(LPVOID arg)
2335 SOCKET sock = *(SOCKET*)arg;
2338 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
2342 if (WSAGetLastError() == WSAEWOULDBLOCK)
2346 FD_SET(sock, &readset);
2347 select(0, &readset, NULL, NULL, NULL);
2358 static void test_send(void)
2360 SOCKET src = INVALID_SOCKET;
2361 SOCKET dst = INVALID_SOCKET;
2362 HANDLE hThread = NULL;
2363 const int buflen = 1024*1024;
2364 char *buffer = NULL;
2368 if (tcp_socketpair(&src, &dst) != 0)
2370 ok(0, "creating socket pair failed, skipping test\n");
2374 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2375 if (hThread == NULL)
2377 ok(0, "CreateThread failed, error %d\n", GetLastError());
2381 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
2384 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2388 ret = send(src, buffer, buflen, 0);
2390 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
2392 ok(0, "send failed, error %d\n", WSAGetLastError());
2395 if (src != INVALID_SOCKET)
2397 if (dst != INVALID_SOCKET)
2399 if (hThread != NULL)
2400 CloseHandle(hThread);
2401 HeapFree(GetProcessHeap(), 0, buffer);
2404 static void test_write_events(void)
2406 SOCKET src = INVALID_SOCKET;
2407 SOCKET dst = INVALID_SOCKET;
2408 HANDLE hThread = NULL;
2409 HANDLE hEvent = INVALID_HANDLE_VALUE;
2410 char *buffer = NULL;
2411 int bufferSize = 1024*1024;
2415 WSANETWORKEVENTS netEvents;
2418 if (tcp_socketpair(&src, &dst) != 0)
2420 ok(0, "creating socket pair failed, skipping test\n");
2424 /* On Windows it seems when a non-blocking socket sends to a
2425 blocking socket on the same host, the send() is BLOCKING,
2426 so make both sockets non-blocking */
2427 ret = ioctlsocket(src, FIONBIO, &one);
2430 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2433 ret = ioctlsocket(dst, FIONBIO, &one);
2436 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2440 buffer = HeapAlloc(GetProcessHeap(), 0, bufferSize);
2443 ok(0, "could not allocate memory for test\n");
2447 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2448 if (hThread == NULL)
2450 ok(0, "CreateThread failed, error %d\n", GetLastError());
2454 hEvent = CreateEventA(NULL, FALSE, TRUE, NULL);
2455 if (hEvent == INVALID_HANDLE_VALUE)
2457 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2461 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
2464 ok(0, "WSAEventSelect failed, error %d\n", ret);
2468 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2469 dwRet = WaitForSingleObject(hEvent, 5000);
2470 if (dwRet != WAIT_OBJECT_0)
2472 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet);
2475 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2478 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2481 if (netEvents.lNetworkEvents & FD_WRITE)
2483 ret = send(src, "a", 1, 0);
2484 ok(ret == 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2490 ok(0, "FD_WRITE not among initial events\n");
2494 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2495 dwRet = WaitForSingleObject(hEvent, 2000);
2496 if (dwRet == WAIT_OBJECT_0)
2498 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2502 /* Now if we send a ton of data and the 'server' does not drain it fast
2503 * enough (set drain_pause to be sure), the socket send buffer will only
2504 * take some of it, and we will get a short write. This will trigger
2505 * another FD_WRITE event as soon as data is sent and more space becomes
2506 * available, but not any earlier. */
2510 ret = send(src, buffer, bufferSize, 0);
2511 } while (ret == bufferSize);
2513 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
2515 dwRet = WaitForSingleObject(hEvent, 5000);
2516 ok(dwRet == WAIT_OBJECT_0, "Waiting failed with %d\n", dwRet);
2517 if (dwRet == WAIT_OBJECT_0)
2519 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2520 ok(ret == 0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2523 ok(netEvents.lNetworkEvents & FD_WRITE,
2524 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents.lNetworkEvents);
2531 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2536 HeapFree(GetProcessHeap(), 0, buffer);
2537 if (src != INVALID_SOCKET)
2539 if (dst != INVALID_SOCKET)
2541 if (hThread != NULL)
2542 CloseHandle(hThread);
2543 CloseHandle(hEvent);
2546 static void test_ipv6only(void)
2548 SOCKET v4 = INVALID_SOCKET,
2549 v6 = INVALID_SOCKET;
2550 struct sockaddr_in sin4;
2551 struct sockaddr_in6 sin6;
2554 memset(&sin4, 0, sizeof(sin4));
2555 sin4.sin_family = AF_INET;
2556 sin4.sin_port = htons(SERVERPORT);
2558 memset(&sin6, 0, sizeof(sin6));
2559 sin6.sin6_family = AF_INET6;
2560 sin6.sin6_port = htons(SERVERPORT);
2562 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2563 if (v6 == INVALID_SOCKET) {
2564 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2565 WSAGetLastError(), WSAEAFNOSUPPORT);
2568 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2570 skip("Could not bind IPv6 address (LastError: %d).\n",
2575 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2576 if (v4 == INVALID_SOCKET) {
2577 skip("Could not create IPv4 socket (LastError: %d).\n",
2581 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2582 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2583 WSAGetLastError(), WSAEADDRINUSE);
2586 if (v4 != INVALID_SOCKET)
2588 if (v6 != INVALID_SOCKET)
2592 static void test_WSASendTo(void)
2595 struct sockaddr_in addr;
2596 char buf[12] = "hello world";
2600 addr.sin_family = AF_INET;
2601 addr.sin_port = htons(139);
2602 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2603 data_buf.len = sizeof(buf);
2606 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2607 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2611 WSASetLastError(12345);
2612 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
2613 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2616 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2617 "a successful call to WSASendTo()\n");
2620 static void test_GetAddrInfoW(void)
2622 static const WCHAR port[] = {'8','0',0};
2623 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
2626 ADDRINFOW *result, hint;
2628 if (!pGetAddrInfoW || !pFreeAddrInfoW)
2630 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
2634 memset(&hint, 0, sizeof(ADDRINFOW));
2636 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
2637 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
2639 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
2640 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2641 pFreeAddrInfoW(result);
2643 ret = pGetAddrInfoW(localhost, port, NULL, &result);
2644 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2645 pFreeAddrInfoW(result);
2647 ret = pGetAddrInfoW(localhost, port, &hint, &result);
2648 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2649 pFreeAddrInfoW(result);
2652 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
2653 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
2654 GROUP FAR * g, DWORD_PTR dwCallbackData)
2659 static void test_AcceptEx(void)
2661 SOCKET listener = INVALID_SOCKET;
2662 SOCKET acceptor = INVALID_SOCKET;
2663 SOCKET connector = INVALID_SOCKET;
2664 SOCKET connector2 = INVALID_SOCKET;
2665 struct sockaddr_in bindAddress;
2667 GUID acceptExGuid = WSAID_ACCEPTEX;
2668 LPFN_ACCEPTEX pAcceptEx = NULL;
2669 fd_set fds_accept, fds_send;
2670 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
2672 DWORD bytesReturned;
2674 OVERLAPPED overlapped;
2679 memset(&overlapped, 0, sizeof(overlapped));
2681 listener = socket(AF_INET, SOCK_STREAM, 0);
2682 if (listener == INVALID_SOCKET) {
2683 skip("could not create listener socket, error %d\n", WSAGetLastError());
2687 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2688 if (acceptor == INVALID_SOCKET) {
2689 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2693 connector = socket(AF_INET, SOCK_STREAM, 0);
2694 if (connector == INVALID_SOCKET) {
2695 skip("could not create connector socket, error %d\n", WSAGetLastError());
2699 memset(&bindAddress, 0, sizeof(bindAddress));
2700 bindAddress.sin_family = AF_INET;
2701 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
2702 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2704 skip("failed to bind, error %d\n", WSAGetLastError());
2708 socklen = sizeof(bindAddress);
2709 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
2711 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2715 if (set_blocking(listener, FALSE)) {
2716 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2720 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
2721 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
2723 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
2727 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2728 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2729 &bytesReturned, &overlapped);
2730 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
2731 "returned %d + errno %d\n", bret, WSAGetLastError());
2733 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2734 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2735 &bytesReturned, &overlapped);
2736 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
2737 "returned %d + errno %d\n", bret, WSAGetLastError());
2739 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2740 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2741 &bytesReturned, &overlapped);
2743 (WSAGetLastError() == WSAEINVAL ||
2744 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
2745 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
2747 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
2748 &bytesReturned, &overlapped);
2749 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
2750 "returned %d + errno %d\n", bret, WSAGetLastError());
2752 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
2753 &bytesReturned, &overlapped);
2754 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
2755 "returned %d + errno %d\n", bret, WSAGetLastError());
2757 bret = pAcceptEx(listener, acceptor, buffer, 0,
2758 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2759 &bytesReturned, NULL);
2760 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
2761 "returned %d + errno %d\n", bret, WSAGetLastError());
2763 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2764 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2765 &bytesReturned, &overlapped);
2766 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
2767 "returned %d + errno %d\n", bret, WSAGetLastError());
2769 iret = listen(listener, 5);
2771 skip("listening failed, errno = %d\n", WSAGetLastError());
2775 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2776 if (overlapped.hEvent == NULL) {
2777 skip("could not create event object, errno = %d\n", GetLastError());
2781 bret = pAcceptEx(listener, acceptor, buffer, 0,
2782 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2783 &bytesReturned, &overlapped);
2784 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2786 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2787 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2789 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
2790 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2792 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2793 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2794 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2796 closesocket(connector);
2797 connector = INVALID_SOCKET;
2798 closesocket(acceptor);
2799 acceptor = INVALID_SOCKET;
2801 /* Test short reads */
2803 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2804 if (acceptor == INVALID_SOCKET) {
2805 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2808 connector = socket(AF_INET, SOCK_STREAM, 0);
2809 if (connector == INVALID_SOCKET) {
2810 skip("could not create connector socket, error %d\n", WSAGetLastError());
2813 bret = pAcceptEx(listener, acceptor, buffer, 2,
2814 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2815 &bytesReturned, &overlapped);
2816 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2818 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2819 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2821 dwret = WaitForSingleObject(overlapped.hEvent, 0);
2822 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
2824 iret = send(connector, buffer, 1, 0);
2825 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
2827 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
2828 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2830 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2831 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2832 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2834 closesocket(connector);
2835 connector = INVALID_SOCKET;
2836 closesocket(acceptor);
2837 acceptor = INVALID_SOCKET;
2839 /* Test CF_DEFER & AcceptEx interaction */
2841 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2842 if (acceptor == INVALID_SOCKET) {
2843 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2846 connector = socket(AF_INET, SOCK_STREAM, 0);
2847 if (connector == INVALID_SOCKET) {
2848 skip("could not create connector socket, error %d\n", WSAGetLastError());
2851 connector2 = socket(AF_INET, SOCK_STREAM, 0);
2852 if (connector == INVALID_SOCKET) {
2853 skip("could not create connector socket, error %d\n", WSAGetLastError());
2857 if (set_blocking(connector, FALSE)) {
2858 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2862 if (set_blocking(connector2, FALSE)) {
2863 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2867 /* Connect socket #1 */
2868 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2870 (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK), "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2872 FD_ZERO ( &fds_accept );
2873 FD_ZERO ( &fds_send );
2875 FD_SET ( listener, &fds_accept );
2876 FD_SET ( connector, &fds_send );
2882 for (i = 0; i < 4000; ++i)
2884 wsa_ok ( ( select ( 0, &fds_accept, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
2885 "select_server (%x): select() failed: %d\n" );
2887 /* check for incoming requests */
2888 if ( FD_ISSET ( listener, &fds_accept ) ) {
2891 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
2892 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection\n");
2893 bret = pAcceptEx(listener, acceptor, buffer, 0,
2894 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2895 &bytesReturned, &overlapped);
2896 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2898 else if (got == 2) {
2899 /* this should be socket #2 */
2900 SOCKET tmp = accept(listener, NULL, NULL);
2901 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
2905 ok(FALSE, "Got more than 2 connections?\n");
2908 if ( conn1 && FD_ISSET ( connector2, &fds_send ) ) {
2909 /* Send data on second socket, and stop */
2910 send(connector2, "2", 1, 0);
2911 FD_CLR ( connector2, &fds_send );
2915 if ( FD_ISSET ( connector, &fds_send ) ) {
2916 /* Once #1 is connected, allow #2 to connect */
2919 send(connector, "1", 1, 0);
2920 FD_CLR ( connector, &fds_send );
2922 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2924 (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK), "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2925 FD_SET ( connector2, &fds_send );
2929 ok (got == 2, "Did not get both connections\n");
2931 dwret = WaitForSingleObject(overlapped.hEvent, 0);
2932 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2934 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2935 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2936 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2938 set_blocking(acceptor, TRUE);
2939 iret = recv( acceptor, buffer, 2, 0);
2940 ok(iret == 1, "Failed to get data, %d\n", iret);
2942 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
2944 closesocket(connector);
2945 connector = INVALID_SOCKET;
2946 closesocket(acceptor);
2947 acceptor = INVALID_SOCKET;
2949 /* Disconnect during receive? */
2951 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2952 if (acceptor == INVALID_SOCKET) {
2953 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2956 connector = socket(AF_INET, SOCK_STREAM, 0);
2957 if (connector == INVALID_SOCKET) {
2958 skip("could not create connector socket, error %d\n", WSAGetLastError());
2961 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2962 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2963 &bytesReturned, &overlapped);
2964 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2966 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2967 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2969 closesocket(connector);
2970 connector = INVALID_SOCKET;
2972 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
2973 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2975 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2976 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2977 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2980 if (overlapped.hEvent)
2981 WSACloseEvent(overlapped.hEvent);
2982 if (listener != INVALID_SOCKET)
2983 closesocket(listener);
2984 if (acceptor != INVALID_SOCKET)
2985 closesocket(acceptor);
2986 if (connector != INVALID_SOCKET)
2987 closesocket(connector);
2988 if (connector2 != INVALID_SOCKET)
2989 closesocket(connector2);
2992 /**************** Main program ***************/
2998 /* Leave these tests at the beginning. They depend on WSAStartup not having been
2999 * called, which is done by Init() below. */
3000 test_WithoutWSAStartup();
3001 test_WithWSAStartup();
3005 test_set_getsockopt();
3006 test_so_reuseaddr();
3007 test_extendedSocketOptions();
3009 for (i = 0; i < NUM_TESTS; i++)
3011 trace ( " **** STARTING TEST %d ****\n", i );
3012 do_test ( &tests[i] );
3013 trace ( " **** TEST %d COMPLETE ****\n", i );
3018 test_getservbyname();
3021 test_WSAAddressToStringA();
3022 test_WSAAddressToStringW();
3024 test_WSAStringToAddressA();
3025 test_WSAStringToAddressW();
3031 test_addr_to_print();
3034 test_gethostbyname_hack();
3037 test_write_events();
3042 test_GetAddrInfoW();