2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/test.h"
33 #define MAX_CLIENTS 4 /* Max number of clients */
34 #define NUM_TESTS 3 /* Number of tests performed */
35 #define FIRST_CHAR 'A' /* First character in transferred pattern */
36 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
37 #define BIND_TRIES 6 /* Number of bind() attempts */
38 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
39 after server initialization, if something hangs */
41 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
43 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
44 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
46 #define SERVERIP "127.0.0.1" /* IP to bind to */
47 #define SERVERPORT 9374 /* Port number to bind to */
49 #define wsa_ok(op, cond, msg) \
53 if ( !(cond tmp) ) err = WSAGetLastError(); \
54 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
58 /**************** Structs and typedefs ***************/
60 typedef struct thread_info
66 /* Information in the server about open client connections */
67 typedef struct sock_info
70 struct sockaddr_in addr;
71 struct sockaddr_in peer;
77 /* Test parameters for both server & client */
78 typedef struct test_params
82 const char *inet_addr;
89 /* server-specific test parameters */
90 typedef struct server_params
97 /* client-specific test parameters */
98 typedef struct client_params
100 test_params *general;
105 /* This type combines all information for setting up a test scenario */
106 typedef struct test_setup
110 server_params srv_params;
112 client_params clt_params;
115 /* Thread local storage for server */
116 typedef struct server_memory
119 struct sockaddr_in addr;
120 sock_info sock[MAX_CLIENTS];
123 /* Thread local storage for client */
124 typedef struct client_memory
127 struct sockaddr_in addr;
132 /* SelectReadThread thread parameters */
133 typedef struct select_thread_params
137 } select_thread_params;
139 /**************** Static variables ***************/
141 static DWORD tls; /* Thread local storage index */
142 static HANDLE thread[1+MAX_CLIENTS];
143 static DWORD thread_id[1+MAX_CLIENTS];
144 static HANDLE server_ready;
145 static HANDLE client_ready[MAX_CLIENTS];
146 static int client_id;
148 /**************** General utility functions ***************/
150 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
152 SOCKET server = INVALID_SOCKET;
153 struct sockaddr_in addr;
157 *src = INVALID_SOCKET;
158 *dst = INVALID_SOCKET;
160 *src = socket(AF_INET, SOCK_STREAM, 0);
161 if (*src == INVALID_SOCKET)
164 server = socket(AF_INET, SOCK_STREAM, 0);
165 if (server == INVALID_SOCKET)
168 memset(&addr, 0, sizeof(addr));
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
171 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
176 ret = getsockname(server, (struct sockaddr*)&addr, &len);
180 ret = listen(server, 1);
184 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
189 *dst = accept(server, (struct sockaddr*)&addr, &len);
192 if (server != INVALID_SOCKET)
194 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
201 static void set_so_opentype ( BOOL overlapped )
203 int optval = !overlapped, newval, len = sizeof (int);
205 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
206 (LPVOID) &optval, sizeof (optval) ) == 0,
207 "setting SO_OPENTYPE failed\n" );
208 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
209 (LPVOID) &newval, &len ) == 0,
210 "getting SO_OPENTYPE failed\n" );
211 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
214 static int set_blocking ( SOCKET s, BOOL blocking )
216 u_long val = !blocking;
217 return ioctlsocket ( s, FIONBIO, &val );
220 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
223 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
224 memset ( p, c, chunk_size );
227 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
231 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
233 for ( i = 0; i < chunk_size; i++ )
234 if ( p[i] != c ) return p + i;
240 * This routine is called when a client / server does not expect any more data,
241 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
243 static void read_zero_bytes ( SOCKET s )
247 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
249 ok ( n <= 0, "garbage data received: %d bytes\n", n );
252 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
254 char* last = buf + buflen, *p;
256 for ( p = buf; n > 0 && p < last; p += n )
257 n = send ( s, p, min ( sendlen, last - p ), 0 );
258 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
262 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
264 char* last = buf + buflen, *p;
266 for ( p = buf; n > 0 && p < last; p += n )
267 n = recv ( s, p, min ( recvlen, last - p ), 0 );
268 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
273 * Call this routine right after thread startup.
274 * SO_OPENTYPE must by 0, regardless what the server did.
276 static void check_so_opentype (void)
280 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
281 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
284 /**************** Server utility functions ***************/
287 * Even if we have closed our server socket cleanly,
288 * the OS may mark the address "in use" for some time -
289 * this happens with native Linux apps, too.
291 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
293 int err, wsaerr = 0, n_try = BIND_TRIES;
295 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
296 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
299 trace ( "address in use, waiting ...\n" );
300 Sleep ( 1000 * BIND_SLEEP );
302 ok ( err == 0, "failed to bind: %d\n", wsaerr );
305 static void server_start ( server_params *par )
308 test_params *gen = par->general;
309 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
311 TlsSetValue ( tls, mem );
312 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
313 NULL, 0, par->sock_flags );
314 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
316 mem->addr.sin_family = AF_INET;
317 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
318 mem->addr.sin_port = htons ( gen->inet_port );
320 for (i = 0; i < MAX_CLIENTS; i++)
322 mem->sock[i].s = INVALID_SOCKET;
323 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
324 mem->sock[i].n_recvd = 0;
325 mem->sock[i].n_sent = 0;
328 if ( gen->sock_type == SOCK_STREAM )
329 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
332 static void server_stop (void)
335 server_memory *mem = TlsGetValue ( tls );
337 for (i = 0; i < MAX_CLIENTS; i++ )
339 LocalFree ( (HANDLE) mem->sock[i].buf );
340 if ( mem->sock[i].s != INVALID_SOCKET )
341 closesocket ( mem->sock[i].s );
343 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
344 LocalFree ( (HANDLE) mem );
345 ExitThread ( GetCurrentThreadId () );
348 /**************** Client utilitiy functions ***************/
350 static void client_start ( client_params *par )
352 test_params *gen = par->general;
353 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
355 TlsSetValue ( tls, mem );
357 WaitForSingleObject ( server_ready, INFINITE );
359 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
360 NULL, 0, par->sock_flags );
362 mem->addr.sin_family = AF_INET;
363 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
364 mem->addr.sin_port = htons ( gen->inet_port );
366 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
368 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
369 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
370 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
372 SetEvent ( client_ready[client_id] );
373 /* Wait for the other clients to come up */
374 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
377 static void client_stop (void)
379 client_memory *mem = TlsGetValue ( tls );
380 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
381 LocalFree ( (HANDLE) mem->send_buf );
382 LocalFree ( (HANDLE) mem );
386 /**************** Servers ***************/
389 * simple_server: A very basic server doing synchronous IO.
391 static VOID WINAPI simple_server ( server_params *par )
393 test_params *gen = par->general;
395 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
396 id = GetCurrentThreadId();
399 trace ( "simple_server (%x) starting\n", id );
401 set_so_opentype ( FALSE ); /* non-overlapped */
402 server_start ( par );
403 mem = TlsGetValue ( tls );
405 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
406 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
408 trace ( "simple_server (%x) ready\n", id );
409 SetEvent ( server_ready ); /* notify clients */
411 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
413 trace ( "simple_server (%x): waiting for client\n", id );
415 /* accept a single connection */
416 tmp = sizeof ( mem->sock[0].peer );
417 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
418 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
420 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
421 "simple_server (%x): strange peer address\n", id );
423 /* Receive data & check it */
424 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
425 ok ( n_recvd == n_expected,
426 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
427 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
428 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
431 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
432 ok ( n_sent == n_expected,
433 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
436 read_zero_bytes ( mem->sock[0].s );
437 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
438 mem->sock[0].s = INVALID_SOCKET;
441 trace ( "simple_server (%x) exiting\n", id );
446 * select_server: A non-blocking server.
448 static VOID WINAPI select_server ( server_params *par )
450 test_params *gen = par->general;
452 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
453 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
454 n_set, delta, n_ready;
456 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
457 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
459 trace ( "select_server (%x) starting\n", id );
461 set_so_opentype ( FALSE ); /* non-overlapped */
462 server_start ( par );
463 mem = TlsGetValue ( tls );
465 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
466 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
468 trace ( "select_server (%x) ready\n", id );
469 SetEvent ( server_ready ); /* notify clients */
471 FD_ZERO ( &fds_openrecv );
472 FD_ZERO ( &fds_recv );
473 FD_ZERO ( &fds_send );
474 FD_ZERO ( &fds_opensend );
476 FD_SET ( mem->s, &fds_openrecv );
480 fds_recv = fds_openrecv;
481 fds_send = fds_opensend;
485 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
486 "select_server (%x): select() failed: %d\n" );
488 /* check for incoming requests */
489 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
492 trace ( "select_server (%x): accepting client connection\n", id );
494 /* accept a single connection */
495 tmp = sizeof ( mem->sock[n_connections].peer );
496 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
497 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
499 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
500 "select_server (%x): strange peer address\n", id );
502 /* add to list of open connections */
503 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
504 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
509 /* handle open requests */
511 for ( i = 0; i < n_connections; i++ )
513 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
516 if ( mem->sock[i].n_recvd < n_expected ) {
517 /* Receive data & check it */
518 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 );
519 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
520 mem->sock[i].n_recvd += n_recvd;
522 if ( mem->sock[i].n_recvd == n_expected ) {
523 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
524 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
525 FD_CLR ( mem->sock[i].s, &fds_openrecv );
528 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
532 /* only echo back what we've received */
533 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
535 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
538 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
540 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
541 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
542 mem->sock[i].n_sent += n_sent;
544 if ( mem->sock[i].n_sent == n_expected ) {
545 FD_CLR ( mem->sock[i].s, &fds_opensend );
548 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
553 /* check that select returned the correct number of ready sockets */
554 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
556 /* check if all clients are done */
557 if ( ( fds_opensend.fd_count == 0 )
558 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
559 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
564 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
567 read_zero_bytes ( mem->sock[i].s );
568 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
569 mem->sock[i].s = INVALID_SOCKET;
572 trace ( "select_server (%x) exiting\n", id );
576 /**************** Clients ***************/
579 * simple_client: A very basic client doing synchronous IO.
581 static VOID WINAPI simple_client ( client_params *par )
583 test_params *gen = par->general;
585 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
588 id = GetCurrentThreadId();
589 trace ( "simple_client (%x): starting\n", id );
590 /* wait here because we want to call set_so_opentype before creating a socket */
591 WaitForSingleObject ( server_ready, INFINITE );
592 trace ( "simple_client (%x): server ready\n", id );
594 check_so_opentype ();
595 set_so_opentype ( FALSE ); /* non-overlapped */
596 client_start ( par );
597 mem = TlsGetValue ( tls );
600 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
601 0 ==, "simple_client (%x): connect error: %d\n" );
602 ok ( set_blocking ( mem->s, TRUE ) == 0,
603 "simple_client (%x): failed to set blocking mode\n", id );
604 trace ( "simple_client (%x) connected\n", id );
606 /* send data to server */
607 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
608 ok ( n_sent == n_expected,
609 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
611 /* shutdown send direction */
612 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
614 /* Receive data echoed back & check it */
615 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
616 ok ( n_recvd == n_expected,
617 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
620 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
621 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
624 read_zero_bytes ( mem->s );
625 trace ( "simple_client (%x) exiting\n", id );
630 * event_client: An event-driven client
632 static void WINAPI event_client ( client_params *par )
634 test_params *gen = par->general;
636 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
639 WSANETWORKEVENTS wsa_events;
640 char *send_last, *recv_last, *send_p, *recv_p;
641 long mask = FD_READ | FD_WRITE | FD_CLOSE;
643 trace ( "event_client (%x): starting\n", id );
644 client_start ( par );
645 trace ( "event_client (%x): server ready\n", id );
647 mem = TlsGetValue ( tls );
649 /* Prepare event notification for connect, makes socket nonblocking */
650 event = WSACreateEvent ();
651 WSAEventSelect ( mem->s, event, FD_CONNECT );
652 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
654 err = WSAGetLastError ();
655 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
656 tmp = WaitForSingleObject ( event, INFINITE );
657 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
658 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
659 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
660 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
661 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
665 trace ( "event_client (%x) connected\n", id );
667 WSAEventSelect ( mem->s, event, mask );
669 recv_p = mem->recv_buf;
670 recv_last = mem->recv_buf + n_expected;
671 send_p = mem->send_buf;
672 send_last = mem->send_buf + n_expected;
676 err = WaitForSingleObject ( event, INFINITE );
677 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
679 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
680 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
682 if ( wsa_events.lNetworkEvents & FD_WRITE )
684 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
685 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
690 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
693 err = WSAGetLastError ();
694 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
699 while ( n >= 0 && send_p < send_last );
701 if ( send_p == send_last )
703 trace ( "event_client (%x): all data sent - shutdown\n", id );
704 shutdown ( mem->s, SD_SEND );
706 WSAEventSelect ( mem->s, event, mask );
709 if ( wsa_events.lNetworkEvents & FD_READ )
711 err = wsa_events.iErrorCode[ FD_READ_BIT ];
712 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
713 if ( err != 0 ) break;
715 /* First read must succeed */
716 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
717 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
721 if ( recv_p == recv_last )
724 trace ( "event_client (%x): all data received\n", id );
725 WSAEventSelect ( mem->s, event, mask );
728 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
729 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
730 ok ( 0, "event_client (%x): read error: %d\n", id, err );
734 if ( wsa_events.lNetworkEvents & FD_CLOSE )
736 trace ( "event_client (%x): close event\n", id );
737 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
738 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
743 ok ( send_p == send_last,
744 "simple_client (%x): sent less data than expected: %d of %d\n",
745 id, send_p - mem->send_buf, n_expected );
746 ok ( recv_p == recv_last,
747 "simple_client (%x): received less data than expected: %d of %d\n",
748 id, recv_p - mem->recv_buf, n_expected );
749 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
750 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
753 WSACloseEvent ( event );
754 trace ( "event_client (%x) exiting\n", id );
758 /**************** Main program utility functions ***************/
760 static void Init (void)
762 WORD ver = MAKEWORD (2, 2);
765 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
769 static void Exit (void)
774 err = WSAGetLastError();
775 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
777 err = WSAGetLastError();
778 ok ( ret == SOCKET_ERROR && err == WSANOTINITIALISED,
779 "WSACleanup returned %d GetLastError is %d\n", ret, err);
782 static void StartServer (LPTHREAD_START_ROUTINE routine,
783 test_params *general, server_params *par)
785 par->general = general;
786 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
787 ok ( thread[0] != NULL, "Failed to create server thread\n" );
790 static void StartClients (LPTHREAD_START_ROUTINE routine,
791 test_params *general, client_params *par)
794 par->general = general;
795 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
798 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
799 ok ( thread[i] != NULL, "Failed to create client thread\n" );
800 /* Make sure the client is up and running */
801 WaitForSingleObject ( client_ready[client_id], INFINITE );
805 static void do_test( test_setup *test )
807 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
810 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
811 for (i = 0; i <= n; i++)
812 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
814 StartServer ( test->srv, &test->general, &test->srv_params );
815 StartClients ( test->clt, &test->general, &test->clt_params );
816 WaitForSingleObject ( server_ready, INFINITE );
818 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
819 ok ( wait <= WAIT_OBJECT_0 + n ,
820 "some threads have not completed: %x\n", wait );
822 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
824 for (i = 0; i <= n; i++)
826 trace ("terminating thread %08x\n", thread_id[i]);
827 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
828 TerminateThread ( thread [i], 0 );
831 CloseHandle ( server_ready );
832 for (i = 0; i <= n; i++)
833 CloseHandle ( client_ready[i] );
836 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
837 /* optname = SO_LINGER */
838 LINGER linger_testvals[] = {
845 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
846 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
847 bug in the linux kernel (fixed in 2.6.8) */
848 #define SOCKTIMEOUT2 997000 /* 997 seconds */
850 static void test_set_getsockopt(void)
858 s = socket(AF_INET, SOCK_STREAM, 0);
859 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
860 if( s == INVALID_SOCKET) return;
862 timeout = SOCKTIMEOUT1;
863 size = sizeof(timeout);
864 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
866 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
867 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
868 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
870 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
871 size = sizeof(timeout);
872 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
874 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
875 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
876 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
878 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
879 size = sizeof(lingval);
880 lingval = linger_testvals[i];
881 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
883 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
884 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
885 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
886 (lingval.l_linger == linger_testvals[i].l_linger ||
887 (!lingval.l_linger && !linger_testvals[i].l_onoff))
888 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
889 lingval.l_onoff, lingval.l_linger,
890 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
892 /* Test for erroneously passing a value instead of a pointer as optval */
894 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
895 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
896 "instead of failing.\n");
897 lasterr = WSAGetLastError();
898 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
899 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
904 static void test_so_reuseaddr(void)
906 struct sockaddr_in saddr;
908 unsigned int rc,reuse;
911 saddr.sin_family = AF_INET;
912 saddr.sin_port = htons(9375);
913 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
915 s1=socket(AF_INET, SOCK_STREAM, 0);
916 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
917 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
918 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
920 s2=socket(AF_INET, SOCK_STREAM, 0);
921 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
925 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
926 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
928 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
929 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
932 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
933 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
936 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
937 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
944 /************* Array containing the tests to run **********/
946 #define STD_STREAM_SOCKET \
952 static test_setup tests [NUM_TESTS] =
954 /* Test 0: synchronous client and server */
975 /* Test 1: event-driven client, synchronous server */
996 /* Test 2: synchronous client, non-blocking server via select() */
1019 static void test_UDP(void)
1021 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1022 possible that this test fails due to dropped packets. */
1024 /* peer 0 receives data from all other peers */
1025 struct sock_info peer[NUM_UDP_PEERS];
1027 int ss, i, n_recv, n_sent;
1029 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1030 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1032 peer[i].addr.sin_family = AF_INET;
1033 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1036 peer[i].addr.sin_port = htons ( SERVERPORT );
1038 peer[i].addr.sin_port = htons ( 0 );
1041 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1043 /* test getsockname() to get peer's port */
1044 ss = sizeof ( peer[i].addr );
1045 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1046 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1049 /* test getsockname() */
1050 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1052 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1053 /* send client's ip */
1054 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1055 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1056 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1059 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1060 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1061 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1062 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1066 static void WINAPI do_getservbyname( HANDLE *starttest )
1072 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1075 struct servent *pserv[2];
1077 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1079 /* ensure that necessary buffer resizes are completed */
1080 for ( j = 0; j < 2; j++) {
1081 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1084 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1085 for ( j = 0; j < 2; j++ ) {
1086 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1087 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1088 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) );
1089 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 );
1090 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 );
1093 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1097 static void test_getservbyname(void)
1100 HANDLE starttest, thread[NUM_THREADS];
1101 DWORD thread_id[NUM_THREADS];
1103 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1105 /* create threads */
1106 for ( i = 0; i < NUM_THREADS; i++ ) {
1107 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1110 /* signal threads to start */
1111 SetEvent ( starttest );
1113 for ( i = 0; i < NUM_THREADS; i++) {
1114 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1118 static void test_WSASocket(void)
1120 SOCKET sock = INVALID_SOCKET;
1121 WSAPROTOCOL_INFOA *pi;
1122 int providers[] = {6, 0};
1126 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1127 * to avoid a crash on win98.
1130 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1131 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1133 err = WSAGetLastError();
1134 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1137 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1138 ok(pi != NULL, "Failed to allocate memory\n");
1140 skip("Can't continue without memory.\n");
1144 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1145 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1149 skip("No protocols enumerated.\n");
1150 HeapFree(GetProcessHeap(), 0, pi);
1154 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1155 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1156 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1160 HeapFree(GetProcessHeap(), 0, pi);
1163 static void test_WSAAddressToStringA(void)
1168 SOCKADDR_IN sockaddr;
1169 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1171 CHAR expect1[] = "0.0.0.0";
1172 CHAR expect2[] = "255.255.255.255";
1173 CHAR expect3[] = "0.0.0.0:65535";
1174 CHAR expect4[] = "255.255.255.255:65535";
1178 sockaddr.sin_family = AF_INET;
1179 sockaddr.sin_port = 0;
1180 sockaddr.sin_addr.s_addr = 0;
1182 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1183 GLE = WSAGetLastError();
1184 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1185 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1188 len = sizeof(address);
1190 sockaddr.sin_family = AF_INET;
1191 sockaddr.sin_port = 0;
1192 sockaddr.sin_addr.s_addr = 0;
1194 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1195 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1197 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1199 len = sizeof(address);
1201 sockaddr.sin_family = AF_INET;
1202 sockaddr.sin_port = 0;
1203 sockaddr.sin_addr.s_addr = 0xffffffff;
1205 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1206 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1208 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1210 len = sizeof(address);
1212 sockaddr.sin_family = AF_INET;
1213 sockaddr.sin_port = 0xffff;
1214 sockaddr.sin_addr.s_addr = 0;
1216 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1217 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1219 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1221 len = sizeof(address);
1223 sockaddr.sin_family = AF_INET;
1224 sockaddr.sin_port = 0xffff;
1225 sockaddr.sin_addr.s_addr = 0xffffffff;
1227 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1228 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1230 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1233 static void test_WSAAddressToStringW(void)
1238 SOCKADDR_IN sockaddr;
1239 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1241 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1242 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1243 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1244 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1245 '6', '5', '5', '3', '5', 0 };
1249 sockaddr.sin_family = AF_INET;
1250 sockaddr.sin_port = 0;
1251 sockaddr.sin_addr.s_addr = 0;
1253 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1254 GLE = WSAGetLastError();
1255 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1256 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1259 len = sizeof(address);
1261 sockaddr.sin_family = AF_INET;
1262 sockaddr.sin_port = 0;
1263 sockaddr.sin_addr.s_addr = 0;
1265 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1266 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1268 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1270 len = sizeof(address);
1272 sockaddr.sin_family = AF_INET;
1273 sockaddr.sin_port = 0;
1274 sockaddr.sin_addr.s_addr = 0xffffffff;
1276 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1277 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1279 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1281 len = sizeof(address);
1283 sockaddr.sin_family = AF_INET;
1284 sockaddr.sin_port = 0xffff;
1285 sockaddr.sin_addr.s_addr = 0;
1287 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1288 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1290 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1292 len = sizeof(address);
1294 sockaddr.sin_family = AF_INET;
1295 sockaddr.sin_port = 0xffff;
1296 sockaddr.sin_addr.s_addr = 0xffffffff;
1298 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1299 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1301 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1304 static void test_WSAStringToAddressA(void)
1307 SOCKADDR_IN sockaddr;
1308 SOCKADDR_IN6 sockaddr6;
1311 CHAR address1[] = "0.0.0.0";
1312 CHAR address2[] = "127.127.127.127";
1313 CHAR address3[] = "255.255.255.255";
1314 CHAR address4[] = "127.127.127.127:65535";
1315 CHAR address5[] = "255.255.255.255:65535";
1316 CHAR address6[] = "::1";
1317 CHAR address7[] = "[::1]";
1318 CHAR address8[] = "[::1]:65535";
1321 sockaddr.sin_family = AF_INET;
1323 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1324 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1325 WSAGetLastError() );
1327 len = sizeof(sockaddr);
1328 sockaddr.sin_port = 0;
1329 sockaddr.sin_addr.s_addr = 0;
1331 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1332 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1333 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1335 len = sizeof(sockaddr);
1336 sockaddr.sin_port = 0;
1337 sockaddr.sin_addr.s_addr = 0;
1339 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1340 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1341 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1343 len = sizeof(sockaddr);
1344 sockaddr.sin_port = 0;
1345 sockaddr.sin_addr.s_addr = 0;
1347 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1348 GLE = WSAGetLastError();
1349 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1350 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1351 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1353 len = sizeof(sockaddr);
1354 sockaddr.sin_port = 0;
1355 sockaddr.sin_addr.s_addr = 0;
1357 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1358 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1359 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1361 len = sizeof(sockaddr);
1362 sockaddr.sin_port = 0;
1363 sockaddr.sin_addr.s_addr = 0;
1365 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1366 GLE = WSAGetLastError();
1367 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1368 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1369 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1371 len = sizeof(sockaddr6);
1372 memset(&sockaddr6, 0, len);
1373 sockaddr6.sin6_family = AF_INET6;
1375 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1377 GLE = WSAGetLastError();
1378 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1379 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1381 len = sizeof(sockaddr6);
1382 memset(&sockaddr6, 0, len);
1383 sockaddr6.sin6_family = AF_INET6;
1385 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1387 GLE = WSAGetLastError();
1388 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1389 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1391 len = sizeof(sockaddr6);
1392 memset(&sockaddr6, 0, len);
1393 sockaddr6.sin6_family = AF_INET6;
1395 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1397 GLE = WSAGetLastError();
1398 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1399 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1400 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1404 static void test_WSAStringToAddressW(void)
1407 SOCKADDR_IN sockaddr;
1408 SOCKADDR_IN6 sockaddr6;
1411 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1412 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1413 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1414 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1415 ':', '6', '5', '5', '3', '5', 0 };
1416 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1417 '6', '5', '5', '3', '5', 0 };
1418 WCHAR address6[] = {':',':','1','\0'};
1419 WCHAR address7[] = {'[',':',':','1',']','\0'};
1420 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1423 sockaddr.sin_family = AF_INET;
1425 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1426 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1427 WSAGetLastError() );
1429 len = sizeof(sockaddr);
1430 sockaddr.sin_port = 0;
1431 sockaddr.sin_addr.s_addr = 0;
1433 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1434 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1435 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1437 len = sizeof(sockaddr);
1438 sockaddr.sin_port = 0;
1439 sockaddr.sin_addr.s_addr = 0;
1441 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1442 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1443 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1445 len = sizeof(sockaddr);
1446 sockaddr.sin_port = 0;
1447 sockaddr.sin_addr.s_addr = 0;
1449 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1450 GLE = WSAGetLastError();
1451 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1452 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1453 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1455 len = sizeof(sockaddr);
1456 sockaddr.sin_port = 0;
1457 sockaddr.sin_addr.s_addr = 0;
1459 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1460 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1461 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1463 len = sizeof(sockaddr);
1464 sockaddr.sin_port = 0;
1465 sockaddr.sin_addr.s_addr = 0;
1467 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1468 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1469 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1470 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1472 len = sizeof(sockaddr6);
1473 memset(&sockaddr6, 0, len);
1474 sockaddr6.sin6_family = AF_INET6;
1476 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1478 GLE = WSAGetLastError();
1479 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1480 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1482 len = sizeof(sockaddr6);
1483 memset(&sockaddr6, 0, len);
1484 sockaddr6.sin6_family = AF_INET6;
1486 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1488 GLE = WSAGetLastError();
1489 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1490 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1492 len = sizeof(sockaddr6);
1493 memset(&sockaddr6, 0, len);
1494 sockaddr6.sin6_family = AF_INET6;
1496 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1498 GLE = WSAGetLastError();
1499 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1500 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1501 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1505 static VOID WINAPI SelectReadThread(select_thread_params *par)
1509 struct sockaddr_in addr;
1510 struct timeval select_timeout;
1513 FD_SET(par->s, &readfds);
1514 select_timeout.tv_sec=5;
1515 select_timeout.tv_usec=0;
1516 addr.sin_family = AF_INET;
1517 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1518 addr.sin_port = htons(SERVERPORT);
1520 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1521 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1523 SetEvent(server_ready);
1524 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1525 par->ReadKilled = (ret == 1);
1528 static void test_select(void)
1530 SOCKET fdRead, fdWrite;
1531 fd_set readfds, writefds, exceptfds;
1534 struct timeval select_timeout;
1535 select_thread_params thread_params;
1536 HANDLE thread_handle;
1539 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1540 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1541 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1542 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1546 FD_ZERO(&exceptfds);
1547 FD_SET(fdRead, &readfds);
1548 FD_SET(fdWrite, &writefds);
1549 FD_SET(fdRead, &exceptfds);
1550 FD_SET(fdWrite, &exceptfds);
1551 select_timeout.tv_sec=0;
1552 select_timeout.tv_usec=500;
1555 if (fdWrite > maxfd)
1559 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1560 ok ( (ret == 0), "select should not return any socket handles\n");
1561 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1562 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1565 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1566 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1569 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1571 ret = closesocket(fdWrite);
1572 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1574 thread_params.s = fdRead;
1575 thread_params.ReadKilled = FALSE;
1576 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1577 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1578 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1580 WaitForSingleObject (server_ready, INFINITE);
1582 ret = closesocket(fdRead);
1583 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1585 WaitForSingleObject (thread_handle, 1000);
1586 ok ( (thread_params.ReadKilled), "closesocket did not wakeup select\n");
1590 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1592 struct sockaddr_in address;
1593 int len = sizeof(address);
1594 SOCKET client_socket;
1596 SetEvent(server_ready);
1597 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1598 if (client_socket != INVALID_SOCKET)
1599 closesocket(client_socket);
1600 par->ReadKilled = (client_socket == INVALID_SOCKET);
1604 static void test_accept(void)
1607 SOCKET server_socket = INVALID_SOCKET;
1608 struct sockaddr_in address;
1609 select_thread_params thread_params;
1610 HANDLE thread_handle = NULL;
1613 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1614 if (server_socket == INVALID_SOCKET)
1616 trace("error creating server socket: %d\n", WSAGetLastError());
1620 memset(&address, 0, sizeof(address));
1621 address.sin_family = AF_INET;
1622 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1625 trace("error binding server socket: %d\n", WSAGetLastError());
1629 ret = listen(server_socket, 1);
1632 trace("error making server socket listen: %d\n", WSAGetLastError());
1636 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1637 if (server_ready == INVALID_HANDLE_VALUE)
1639 trace("error creating event: %d\n", GetLastError());
1643 thread_params.s = server_socket;
1644 thread_params.ReadKilled = FALSE;
1645 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1646 &thread_params, 0, &id);
1647 if (thread_handle == NULL)
1649 trace("error creating thread: %d\n", GetLastError());
1653 WaitForSingleObject(server_ready, INFINITE);
1655 ret = closesocket(server_socket);
1658 trace("closesocket failed: %d\n", WSAGetLastError());
1662 WaitForSingleObject(thread_handle, 1000);
1663 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
1666 if (thread_handle != NULL)
1667 CloseHandle(thread_handle);
1668 if (server_ready != INVALID_HANDLE_VALUE)
1669 CloseHandle(server_ready);
1670 if (server_socket != INVALID_SOCKET)
1671 closesocket(server_socket);
1674 static void test_extendedSocketOptions(void)
1678 struct sockaddr_in sa;
1679 int sa_len = sizeof(struct sockaddr_in);
1680 int optval, optlen = sizeof(int), ret;
1684 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1685 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1689 memset(&sa, 0, sa_len);
1691 sa.sin_family = AF_INET;
1692 sa.sin_port = htons(0);
1693 sa.sin_addr.s_addr = htonl(INADDR_ANY);
1695 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
1696 trace("Creating the socket failed: %d\n", WSAGetLastError());
1701 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1702 trace("Failed to bind socket: %d\n", WSAGetLastError());
1708 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
1710 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
1711 ok((optval == 65507) || (optval == 65527),
1712 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
1714 optlen = sizeof(LINGER);
1715 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1717 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
1722 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1723 trace("Creating the socket failed: %d\n", WSAGetLastError());
1728 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1729 trace("Failed to bind socket: %d\n", WSAGetLastError());
1735 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1736 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
1738 optlen = sizeof(BOOL);
1739 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
1740 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
1741 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
1742 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1743 bool_opt_val, linger_val.l_onoff);
1749 static void test_getsockname(void)
1753 struct sockaddr_in sa_set, sa_get;
1754 int sa_set_len = sizeof(struct sockaddr_in);
1755 int sa_get_len = sa_set_len;
1756 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
1758 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1759 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1763 memset(&sa_set, 0, sa_set_len);
1765 sa_set.sin_family = AF_INET;
1766 sa_set.sin_port = htons(0);
1767 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
1769 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1770 trace("Creating the socket failed: %d\n", WSAGetLastError());
1775 memcpy(&sa_get, &sa_set, sizeof(sa_set));
1776 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
1777 ok(0, "getsockname on unbound socket should fail\n");
1779 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
1780 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
1781 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
1782 "failed getsockname modified sockaddr when it shouldn't\n");
1785 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
1786 trace("Failed to bind socket: %d\n", WSAGetLastError());
1792 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
1793 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1799 ok(memcmp(sa_get.sin_zero, null_padding, 8) == 0,
1800 "getsockname did not zero the sockaddr_in structure\n");
1806 static void test_dns(void)
1810 h = gethostbyname("");
1811 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
1814 /* Our winsock headers don't define gethostname because it conflicts with the
1815 * definition in unistd.h. Define it here to get rid of the warning. */
1817 int WINAPI gethostname(char *name, int namelen);
1819 static void test_gethostbyname_hack(void)
1823 static BYTE loopback[] = {127, 0, 0, 1};
1824 static BYTE magic_loopback[] = {127, 12, 34, 56};
1827 ret = gethostname(name, 256);
1828 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
1830 he = gethostbyname("localhost");
1831 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
1834 if(he->h_length != 4)
1836 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1840 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
1841 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
1842 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
1843 he->h_addr_list[0][3]);
1846 /* No reason to test further with NULL hostname */
1850 if(strcmp(name, "localhost") == 0)
1852 skip("hostname seems to be \"localhost\", skipping test.\n");
1857 he = gethostbyname(name);
1858 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
1861 if(he->h_length != 4)
1863 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1867 if (he->h_addr_list[0][0] == 127)
1869 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
1870 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
1871 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
1872 he->h_addr_list[0][2], he->h_addr_list[0][3]);
1877 he = gethostbyname("nonexistent.winehq.org");
1878 /* Don't check for the return value, as some braindead ISPs will kindly
1879 * resolve nonexistent host names to addresses of the ISP's spam pages. */
1882 static void test_inet_addr(void)
1886 addr = inet_addr(NULL);
1887 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
1890 static DWORD WINAPI drain_socket_thread(LPVOID arg)
1893 SOCKET sock = *(SOCKET*)arg;
1895 while (recv(sock, buffer, sizeof(buffer), 0) > 0)
1900 static void test_send(void)
1902 SOCKET src = INVALID_SOCKET;
1903 SOCKET dst = INVALID_SOCKET;
1904 HANDLE hThread = NULL;
1905 const int buflen = 1024*1024;
1906 char *buffer = NULL;
1910 if (tcp_socketpair(&src, &dst) != 0)
1912 ok(0, "creating socket pair failed, skipping test\n");
1916 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
1917 if (hThread == NULL)
1919 ok(0, "CreateThread failed, error %d\n", GetLastError());
1923 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
1926 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
1930 ret = send(src, buffer, buflen, 0);
1932 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
1934 ok(0, "send failed, error %d\n", WSAGetLastError());
1937 if (src != INVALID_SOCKET)
1939 if (dst != INVALID_SOCKET)
1941 if (hThread != NULL)
1942 CloseHandle(hThread);
1943 HeapFree(GetProcessHeap(), 0, buffer);
1946 static void test_write_events(void)
1948 SOCKET src = INVALID_SOCKET;
1949 SOCKET dst = INVALID_SOCKET;
1950 HANDLE hThread = NULL;
1951 HANDLE hEvent = INVALID_HANDLE_VALUE;
1957 if (tcp_socketpair(&src, &dst) != 0)
1959 ok(0, "creating socket pair failed, skipping test\n");
1963 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
1964 if (hThread == NULL)
1966 ok(0, "CreateThread failed, error %d\n", GetLastError());
1970 hEvent = CreateEventW(NULL, FALSE, TRUE, NULL);
1971 if (hEvent == INVALID_HANDLE_VALUE)
1973 ok(0, "CreateEventW failed, error %d\n", GetLastError());
1977 ret = ioctlsocket(src, FIONBIO, &one);
1980 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
1984 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
1987 ok(0, "WSAEventSelect failed, error %d\n", ret);
1991 for (len = 100; len > 0; --len)
1993 WSANETWORKEVENTS netEvents;
1994 DWORD dwRet = WaitForSingleObject(hEvent, 5000);
1995 if (dwRet != WAIT_OBJECT_0)
1997 ok(0, "WaitForSingleObject failed, error %d\n", dwRet);
2001 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2004 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2008 if (netEvents.lNetworkEvents & FD_WRITE)
2010 ret = send(src, "a", 1, 0);
2011 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
2013 ok(0, "send failed, error %d\n", WSAGetLastError());
2018 if (netEvents.lNetworkEvents & FD_CLOSE)
2020 ok(0, "unexpected close\n");
2026 if (src != INVALID_SOCKET)
2028 if (dst != INVALID_SOCKET)
2030 if (hThread != NULL)
2031 CloseHandle(hThread);
2032 CloseHandle(hEvent);
2035 static void test_ipv6only(void)
2037 SOCKET v4 = INVALID_SOCKET,
2038 v6 = INVALID_SOCKET;
2039 struct sockaddr_in sin4;
2040 struct sockaddr_in6 sin6;
2043 memset(&sin4, 0, sizeof(sin4));
2044 sin4.sin_family = AF_INET;
2045 sin4.sin_port = htons(SERVERPORT);
2047 memset(&sin6, 0, sizeof(sin6));
2048 sin6.sin6_family = AF_INET6;
2049 sin6.sin6_port = htons(SERVERPORT);
2051 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2052 if (v6 == INVALID_SOCKET) {
2053 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2054 WSAGetLastError(), WSAEAFNOSUPPORT);
2057 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2059 skip("Could not bind IPv6 address (LastError: %d).\n",
2064 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2065 if (v4 == INVALID_SOCKET) {
2066 skip("Could not create IPv4 socket (LastError: %d).\n",
2070 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2071 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2072 WSAGetLastError(), WSAEADDRINUSE);
2075 if (v4 != INVALID_SOCKET)
2077 if (v6 != INVALID_SOCKET)
2081 /**************** Main program ***************/
2088 test_set_getsockopt();
2089 test_so_reuseaddr();
2090 test_extendedSocketOptions();
2092 for (i = 0; i < NUM_TESTS; i++)
2094 trace ( " **** STARTING TEST %d ****\n", i );
2095 do_test ( &tests[i] );
2096 trace ( " **** TEST %d COMPLETE ****\n", i );
2101 test_getservbyname();
2104 test_WSAAddressToStringA();
2105 test_WSAAddressToStringW();
2107 test_WSAStringToAddressA();
2108 test_WSAStringToAddressW();
2115 test_gethostbyname_hack();
2118 test_write_events();