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
24 #define WIN32_NO_STATUS
31 #include "wine/test.h"
33 #define MAX_CLIENTS 4 /* Max number of clients */
34 #define FIRST_CHAR 'A' /* First character in transferred pattern */
35 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
36 #define BIND_TRIES 6 /* Number of bind() attempts */
37 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
38 after server initialization, if something hangs */
40 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
42 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
43 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
45 #define SERVERIP "127.0.0.1" /* IP to bind to */
46 #define SERVERPORT 9374 /* Port number to bind to */
48 #define wsa_ok(op, cond, msg) \
52 if ( !(cond tmp) ) err = WSAGetLastError(); \
53 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
56 #define make_keepalive(k, enable, time, interval) \
58 k.keepalivetime = time; \
59 k.keepaliveinterval = interval;
61 /* Function pointers */
62 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW) = 0;
63 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *) = 0;
64 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG) = 0;
66 /**************** Structs and typedefs ***************/
68 typedef struct thread_info
74 /* Information in the server about open client connections */
75 typedef struct sock_info
78 struct sockaddr_in addr;
79 struct sockaddr_in peer;
85 /* Test parameters for both server & client */
86 typedef struct test_params
90 const char *inet_addr;
97 /* server-specific test parameters */
98 typedef struct server_params
100 test_params *general;
105 /* client-specific test parameters */
106 typedef struct client_params
108 test_params *general;
113 /* This type combines all information for setting up a test scenario */
114 typedef struct test_setup
118 server_params srv_params;
120 client_params clt_params;
123 /* Thread local storage for server */
124 typedef struct server_memory
127 struct sockaddr_in addr;
128 sock_info sock[MAX_CLIENTS];
131 /* Thread local storage for client */
132 typedef struct client_memory
135 struct sockaddr_in addr;
140 /* SelectReadThread thread parameters */
141 typedef struct select_thread_params
145 } select_thread_params;
147 /**************** Static variables ***************/
149 static DWORD tls; /* Thread local storage index */
150 static HANDLE thread[1+MAX_CLIENTS];
151 static DWORD thread_id[1+MAX_CLIENTS];
152 static HANDLE server_ready;
153 static HANDLE client_ready[MAX_CLIENTS];
154 static int client_id;
156 /**************** General utility functions ***************/
158 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
160 SOCKET server = INVALID_SOCKET;
161 struct sockaddr_in addr;
165 *src = INVALID_SOCKET;
166 *dst = INVALID_SOCKET;
168 *src = socket(AF_INET, SOCK_STREAM, 0);
169 if (*src == INVALID_SOCKET)
172 server = socket(AF_INET, SOCK_STREAM, 0);
173 if (server == INVALID_SOCKET)
176 memset(&addr, 0, sizeof(addr));
177 addr.sin_family = AF_INET;
178 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
179 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
184 ret = getsockname(server, (struct sockaddr*)&addr, &len);
188 ret = listen(server, 1);
192 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
197 *dst = accept(server, (struct sockaddr*)&addr, &len);
200 if (server != INVALID_SOCKET)
202 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
209 static void set_so_opentype ( BOOL overlapped )
211 int optval = !overlapped, newval, len = sizeof (int);
213 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
214 (LPVOID) &optval, sizeof (optval) ) == 0,
215 "setting SO_OPENTYPE failed\n" );
216 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
217 (LPVOID) &newval, &len ) == 0,
218 "getting SO_OPENTYPE failed\n" );
219 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
222 static int set_blocking ( SOCKET s, BOOL blocking )
224 u_long val = !blocking;
225 return ioctlsocket ( s, FIONBIO, &val );
228 static void fill_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 )
232 memset ( p, c, chunk_size );
235 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
239 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
241 for ( i = 0; i < chunk_size; i++ )
242 if ( p[i] != c ) return i;
248 * This routine is called when a client / server does not expect any more data,
249 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
251 static void read_zero_bytes ( SOCKET s )
255 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
257 ok ( n <= 0, "garbage data received: %d bytes\n", n );
260 static int do_oob_send ( SOCKET s, char *buf, int buflen, int sendlen )
262 char* last = buf + buflen, *p;
264 for ( p = buf; n > 0 && p < last; p += n )
265 n = send ( s, p, min ( sendlen, last - p ), MSG_OOB );
266 wsa_ok ( n, 0 <=, "do_oob_send (%x): error %d\n" );
270 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
272 char* last = buf + buflen, *p;
274 for ( p = buf; n > 0 && p < last; p += n )
275 n = send ( s, p, min ( sendlen, last - p ), 0 );
276 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
280 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
282 char* last = buf + buflen, *p;
284 for ( p = buf; n > 0 && p < last; p += n )
285 n = recv ( s, p, min ( recvlen, last - p ), 0 );
286 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
290 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, int *fromlen, int recvlen )
292 char* last = buf + buflen, *p;
294 for ( p = buf; n > 0 && p < last; p += n )
295 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
296 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
301 * Call this routine right after thread startup.
302 * SO_OPENTYPE must by 0, regardless what the server did.
304 static void check_so_opentype (void)
308 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
309 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
312 /**************** Server utility functions ***************/
315 * Even if we have closed our server socket cleanly,
316 * the OS may mark the address "in use" for some time -
317 * this happens with native Linux apps, too.
319 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
321 int err, wsaerr = 0, n_try = BIND_TRIES;
323 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
324 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
327 trace ( "address in use, waiting ...\n" );
328 Sleep ( 1000 * BIND_SLEEP );
330 ok ( err == 0, "failed to bind: %d\n", wsaerr );
333 static void server_start ( server_params *par )
336 test_params *gen = par->general;
337 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
339 TlsSetValue ( tls, mem );
340 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
341 NULL, 0, par->sock_flags );
342 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
344 mem->addr.sin_family = AF_INET;
345 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
346 mem->addr.sin_port = htons ( gen->inet_port );
348 for (i = 0; i < MAX_CLIENTS; i++)
350 mem->sock[i].s = INVALID_SOCKET;
351 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
352 mem->sock[i].n_recvd = 0;
353 mem->sock[i].n_sent = 0;
356 if ( gen->sock_type == SOCK_STREAM )
357 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
360 static void server_stop (void)
363 server_memory *mem = TlsGetValue ( tls );
365 for (i = 0; i < MAX_CLIENTS; i++ )
367 LocalFree ( mem->sock[i].buf );
368 if ( mem->sock[i].s != INVALID_SOCKET )
369 closesocket ( mem->sock[i].s );
371 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
373 ExitThread ( GetCurrentThreadId () );
376 /**************** Client utilitiy functions ***************/
378 static void client_start ( client_params *par )
380 test_params *gen = par->general;
381 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
383 TlsSetValue ( tls, mem );
385 WaitForSingleObject ( server_ready, INFINITE );
387 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
388 NULL, 0, par->sock_flags );
390 mem->addr.sin_family = AF_INET;
391 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
392 mem->addr.sin_port = htons ( gen->inet_port );
394 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
396 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
397 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
398 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
400 SetEvent ( client_ready[client_id] );
401 /* Wait for the other clients to come up */
402 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
405 static void client_stop (void)
407 client_memory *mem = TlsGetValue ( tls );
408 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
409 LocalFree ( mem->send_buf );
414 /**************** Servers ***************/
417 * simple_server: A very basic server doing synchronous IO.
419 static VOID WINAPI simple_server ( server_params *par )
421 test_params *gen = par->general;
423 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
424 id = GetCurrentThreadId();
426 trace ( "simple_server (%x) starting\n", id );
428 set_so_opentype ( FALSE ); /* non-overlapped */
429 server_start ( par );
430 mem = TlsGetValue ( tls );
432 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
433 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
435 trace ( "simple_server (%x) ready\n", id );
436 SetEvent ( server_ready ); /* notify clients */
438 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
440 trace ( "simple_server (%x): waiting for client\n", id );
442 /* accept a single connection */
443 tmp = sizeof ( mem->sock[0].peer );
444 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
445 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
447 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
448 "simple_server (%x): strange peer address\n", id );
450 /* Receive data & check it */
451 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
452 ok ( n_recvd == n_expected,
453 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
454 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
455 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
458 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
459 ok ( n_sent == n_expected,
460 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
463 read_zero_bytes ( mem->sock[0].s );
464 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
465 mem->sock[0].s = INVALID_SOCKET;
468 trace ( "simple_server (%x) exiting\n", id );
473 * oob_server: A very basic server receiving out-of-band data.
475 static VOID WINAPI oob_server ( server_params *par )
477 test_params *gen = par->general;
480 int pos, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
481 id = GetCurrentThreadId();
483 trace ( "oob_server (%x) starting\n", id );
485 set_so_opentype ( FALSE ); /* non-overlapped */
486 server_start ( par );
487 mem = TlsGetValue ( tls );
489 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
490 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
492 trace ( "oob_server (%x) ready\n", id );
493 SetEvent ( server_ready ); /* notify clients */
495 trace ( "oob_server (%x): waiting for client\n", id );
497 /* accept a single connection */
498 tmp = sizeof ( mem->sock[0].peer );
499 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
500 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
502 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
503 "oob_server (%x): strange peer address\n", id );
505 /* check atmark state */
506 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
507 todo_wine ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
509 /* Receive normal data and check atmark state */
510 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
511 ok ( n_recvd == n_expected,
512 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
513 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
514 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
516 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
517 todo_wine ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
519 /* Receive a part of the out-of-band data and check atmark state */
520 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, par->buflen );
522 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
525 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
526 ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
528 /* Receive the rest of the out-of-band data and check atmark state */
529 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
531 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
532 ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
535 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
536 mem->sock[0].s = INVALID_SOCKET;
538 trace ( "oob_server (%x) exiting\n", id );
543 * select_server: A non-blocking server.
545 static VOID WINAPI select_server ( server_params *par )
547 test_params *gen = par->general;
549 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
550 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
551 n_set, delta, n_ready;
552 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
553 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
555 trace ( "select_server (%x) starting\n", id );
557 set_so_opentype ( FALSE ); /* non-overlapped */
558 server_start ( par );
559 mem = TlsGetValue ( tls );
561 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
562 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
564 trace ( "select_server (%x) ready\n", id );
565 SetEvent ( server_ready ); /* notify clients */
567 FD_ZERO ( &fds_openrecv );
568 FD_ZERO ( &fds_recv );
569 FD_ZERO ( &fds_send );
570 FD_ZERO ( &fds_opensend );
572 FD_SET ( mem->s, &fds_openrecv );
576 fds_recv = fds_openrecv;
577 fds_send = fds_opensend;
581 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
582 "select_server (%x): select() failed: %d\n" );
584 /* check for incoming requests */
585 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
588 trace ( "select_server (%x): accepting client connection\n", id );
590 /* accept a single connection */
591 tmp = sizeof ( mem->sock[n_connections].peer );
592 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
593 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
595 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
596 "select_server (%x): strange peer address\n", id );
598 /* add to list of open connections */
599 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
600 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
605 /* handle open requests */
607 for ( i = 0; i < n_connections; i++ )
609 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
612 if ( mem->sock[i].n_recvd < n_expected ) {
613 /* Receive data & check it */
614 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 );
615 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
616 mem->sock[i].n_recvd += n_recvd;
618 if ( mem->sock[i].n_recvd == n_expected ) {
619 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
620 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
621 FD_CLR ( mem->sock[i].s, &fds_openrecv );
624 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
628 /* only echo back what we've received */
629 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
631 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
634 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
636 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
637 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
638 mem->sock[i].n_sent += n_sent;
640 if ( mem->sock[i].n_sent == n_expected ) {
641 FD_CLR ( mem->sock[i].s, &fds_opensend );
644 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
649 /* check that select returned the correct number of ready sockets */
650 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
652 /* check if all clients are done */
653 if ( ( fds_opensend.fd_count == 0 )
654 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
655 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
660 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
663 read_zero_bytes ( mem->sock[i].s );
664 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
665 mem->sock[i].s = INVALID_SOCKET;
668 trace ( "select_server (%x) exiting\n", id );
672 /**************** Clients ***************/
675 * simple_client: A very basic client doing synchronous IO.
677 static VOID WINAPI simple_client ( client_params *par )
679 test_params *gen = par->general;
681 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
683 id = GetCurrentThreadId();
684 trace ( "simple_client (%x): starting\n", id );
685 /* wait here because we want to call set_so_opentype before creating a socket */
686 WaitForSingleObject ( server_ready, INFINITE );
687 trace ( "simple_client (%x): server ready\n", id );
689 check_so_opentype ();
690 set_so_opentype ( FALSE ); /* non-overlapped */
691 client_start ( par );
692 mem = TlsGetValue ( tls );
695 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
696 0 ==, "simple_client (%x): connect error: %d\n" );
697 ok ( set_blocking ( mem->s, TRUE ) == 0,
698 "simple_client (%x): failed to set blocking mode\n", id );
699 trace ( "simple_client (%x) connected\n", id );
701 /* send data to server */
702 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
703 ok ( n_sent == n_expected,
704 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
706 /* shutdown send direction */
707 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
709 /* Receive data echoed back & check it */
710 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
711 ok ( n_recvd == n_expected,
712 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
715 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
716 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
719 read_zero_bytes ( mem->s );
720 trace ( "simple_client (%x) exiting\n", id );
725 * oob_client: A very basic client sending out-of-band data.
727 static VOID WINAPI oob_client ( client_params *par )
729 test_params *gen = par->general;
731 int n_sent, n_expected = gen->n_chunks * gen->chunk_size, id;
733 id = GetCurrentThreadId();
734 trace ( "oob_client (%x): starting\n", id );
735 /* wait here because we want to call set_so_opentype before creating a socket */
736 WaitForSingleObject ( server_ready, INFINITE );
737 trace ( "oob_client (%x): server ready\n", id );
739 check_so_opentype ();
740 set_so_opentype ( FALSE ); /* non-overlapped */
741 client_start ( par );
742 mem = TlsGetValue ( tls );
745 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
746 0 ==, "oob_client (%x): connect error: %d\n" );
747 ok ( set_blocking ( mem->s, TRUE ) == 0,
748 "oob_client (%x): failed to set blocking mode\n", id );
749 trace ( "oob_client (%x) connected\n", id );
751 /* send data to server */
752 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
753 ok ( n_sent == n_expected,
754 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
756 /* send out-of-band data to server */
757 n_sent = do_oob_send ( mem->s, mem->send_buf, n_expected, par->buflen );
758 ok ( n_sent == n_expected,
759 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
761 /* shutdown send direction */
762 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
765 read_zero_bytes ( mem->s );
766 trace ( "oob_client (%x) exiting\n", id );
771 * simple_mixed_client: mixing send and recvfrom
773 static VOID WINAPI simple_mixed_client ( client_params *par )
775 test_params *gen = par->general;
777 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
778 int fromLen = sizeof(mem->addr);
779 struct sockaddr test;
781 id = GetCurrentThreadId();
782 trace ( "simple_client (%x): starting\n", id );
783 /* wait here because we want to call set_so_opentype before creating a socket */
784 WaitForSingleObject ( server_ready, INFINITE );
785 trace ( "simple_client (%x): server ready\n", id );
787 check_so_opentype ();
788 set_so_opentype ( FALSE ); /* non-overlapped */
789 client_start ( par );
790 mem = TlsGetValue ( tls );
793 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
794 0 ==, "simple_client (%x): connect error: %d\n" );
795 ok ( set_blocking ( mem->s, TRUE ) == 0,
796 "simple_client (%x): failed to set blocking mode\n", id );
797 trace ( "simple_client (%x) connected\n", id );
799 /* send data to server */
800 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
801 ok ( n_sent == n_expected,
802 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
804 /* shutdown send direction */
805 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
807 /* this shouldn't change, since lpFrom, is not updated on
808 connection oriented sockets - exposed by bug 11640
810 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
812 /* Receive data echoed back & check it */
813 n_recvd = do_synchronous_recvfrom ( mem->s,
817 (struct sockaddr *)&test,
820 ok ( n_recvd == n_expected,
821 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
823 /* check that lpFrom was not updated */
826 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
827 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
830 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
831 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
834 read_zero_bytes ( mem->s );
835 trace ( "simple_client (%x) exiting\n", id );
840 * event_client: An event-driven client
842 static void WINAPI event_client ( client_params *par )
844 test_params *gen = par->general;
846 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
849 WSANETWORKEVENTS wsa_events;
850 char *send_last, *recv_last, *send_p, *recv_p;
851 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
853 trace ( "event_client (%x): starting\n", id );
854 client_start ( par );
855 trace ( "event_client (%x): server ready\n", id );
857 mem = TlsGetValue ( tls );
859 /* Prepare event notification for connect, makes socket nonblocking */
860 event = WSACreateEvent ();
861 WSAEventSelect ( mem->s, event, FD_CONNECT );
862 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
864 err = WSAGetLastError ();
865 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
866 tmp = WaitForSingleObject ( event, INFINITE );
867 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
868 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
869 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
870 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
871 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
875 trace ( "event_client (%x) connected\n", id );
877 WSAEventSelect ( mem->s, event, mask );
879 recv_p = mem->recv_buf;
880 recv_last = mem->recv_buf + n_expected;
881 send_p = mem->send_buf;
882 send_last = mem->send_buf + n_expected;
886 err = WaitForSingleObject ( event, INFINITE );
887 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
889 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
890 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
892 if ( wsa_events.lNetworkEvents & FD_WRITE )
894 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
895 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
900 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
903 err = WSAGetLastError ();
904 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
909 while ( n >= 0 && send_p < send_last );
911 if ( send_p == send_last )
913 trace ( "event_client (%x): all data sent - shutdown\n", id );
914 shutdown ( mem->s, SD_SEND );
916 WSAEventSelect ( mem->s, event, mask );
919 if ( wsa_events.lNetworkEvents & FD_READ )
921 err = wsa_events.iErrorCode[ FD_READ_BIT ];
922 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
923 if ( err != 0 ) break;
925 /* First read must succeed */
926 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
927 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
931 if ( recv_p == recv_last )
934 trace ( "event_client (%x): all data received\n", id );
935 WSAEventSelect ( mem->s, event, mask );
938 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
939 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
940 ok ( 0, "event_client (%x): read error: %d\n", id, err );
944 if ( wsa_events.lNetworkEvents & FD_CLOSE )
946 trace ( "event_client (%x): close event\n", id );
947 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
948 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
953 n = send_p - mem->send_buf;
954 ok ( send_p == send_last,
955 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
956 n = recv_p - mem->recv_buf;
957 ok ( recv_p == recv_last,
958 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
959 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
960 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
963 WSACloseEvent ( event );
964 trace ( "event_client (%x) exiting\n", id );
968 /* Tests for WSAStartup */
969 static void test_WithoutWSAStartup(void)
973 WSASetLastError(0xdeadbeef);
974 ptr = gethostbyname("localhost");
976 ok(ptr == NULL, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
977 ok(WSAGetLastError() == WSANOTINITIALISED, "gethostbyname() failed with unexpected error: %d\n",
981 static void test_WithWSAStartup(void)
984 WORD version = MAKEWORD( 2, 2 );
988 res = WSAStartup( version, &data );
989 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
991 ptr = gethostbyname("localhost");
992 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
997 /**************** Main program utility functions ***************/
999 static void Init (void)
1001 WORD ver = MAKEWORD (2, 2);
1003 HMODULE hws2_32 = GetModuleHandle("ws2_32.dll");
1005 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
1006 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
1007 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1009 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1013 static void Exit (void)
1018 err = WSAGetLastError();
1019 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1021 err = WSAGetLastError();
1022 ok ( (ret == SOCKET_ERROR && err == WSANOTINITIALISED) ||
1023 broken(ret == 0), /* WinME */
1024 "WSACleanup returned %d GetLastError is %d\n", ret, err);
1027 static void StartServer (LPTHREAD_START_ROUTINE routine,
1028 test_params *general, server_params *par)
1030 par->general = general;
1031 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1032 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1035 static void StartClients (LPTHREAD_START_ROUTINE routine,
1036 test_params *general, client_params *par)
1039 par->general = general;
1040 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1043 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1044 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1045 /* Make sure the client is up and running */
1046 WaitForSingleObject ( client_ready[client_id], INFINITE );
1050 static void do_test( test_setup *test )
1052 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1055 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1056 for (i = 0; i <= n; i++)
1057 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1059 StartServer ( test->srv, &test->general, &test->srv_params );
1060 StartClients ( test->clt, &test->general, &test->clt_params );
1061 WaitForSingleObject ( server_ready, INFINITE );
1063 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1064 ok ( wait <= WAIT_OBJECT_0 + n ,
1065 "some threads have not completed: %x\n", wait );
1067 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
1069 for (i = 0; i <= n; i++)
1071 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
1073 trace ("terminating thread %08x\n", thread_id[i]);
1074 TerminateThread ( thread [i], 0 );
1078 CloseHandle ( server_ready );
1079 for (i = 0; i <= n; i++)
1080 CloseHandle ( client_ready[i] );
1083 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1084 /* optname = SO_LINGER */
1085 static const LINGER linger_testvals[] = {
1092 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1093 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1094 bug in the linux kernel (fixed in 2.6.8) */
1095 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1097 static void test_set_getsockopt(void)
1100 int i, err, lasterr;
1105 s = socket(AF_INET, SOCK_STREAM, 0);
1106 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1107 if( s == INVALID_SOCKET) return;
1109 timeout = SOCKTIMEOUT1;
1110 size = sizeof(timeout);
1111 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1113 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1114 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1115 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1118 size = sizeof(timeout);
1119 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1121 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1122 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1123 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1126 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1127 size = sizeof(timeout);
1128 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1130 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1131 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1132 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1134 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1135 size = sizeof(lingval);
1136 lingval = linger_testvals[i];
1137 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1139 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1140 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1141 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1142 (lingval.l_linger == linger_testvals[i].l_linger ||
1143 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1144 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1145 lingval.l_onoff, lingval.l_linger,
1146 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1149 size = sizeof(lingval);
1150 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1151 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1152 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1153 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1154 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1155 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1157 size = sizeof(BOOL);
1158 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1159 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1160 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1161 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1162 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1163 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1165 /* Test for erroneously passing a value instead of a pointer as optval */
1166 size = sizeof(char);
1167 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1168 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1169 "instead of failing.\n");
1170 lasterr = WSAGetLastError();
1171 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1172 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1173 lasterr, WSAEFAULT);
1175 /* SO_RCVTIMEO with invalid values for level */
1176 size = sizeof(timeout);
1177 timeout = SOCKTIMEOUT1;
1178 SetLastError(0xdeadbeef);
1179 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1180 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1181 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1182 err, WSAGetLastError());
1184 timeout = SOCKTIMEOUT1;
1185 SetLastError(0xdeadbeef);
1186 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1187 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1188 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1189 err, WSAGetLastError());
1194 static void test_so_reuseaddr(void)
1196 struct sockaddr_in saddr;
1198 unsigned int rc,reuse;
1201 saddr.sin_family = AF_INET;
1202 saddr.sin_port = htons(9375);
1203 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1205 s1=socket(AF_INET, SOCK_STREAM, 0);
1206 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1207 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1208 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1210 s2=socket(AF_INET, SOCK_STREAM, 0);
1211 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1215 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1216 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1218 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1219 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1222 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1223 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1225 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1226 * a port immediately after closing another socket on that port, so
1227 * basically following the BSD socket semantics here. */
1229 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1230 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1235 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1237 static void test_ip_pktinfo(void)
1239 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
1240 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
1241 struct sockaddr_in s1addr, s2addr, s3addr;
1242 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
1243 LPFN_WSARECVMSG pWSARecvMsg = NULL;
1244 unsigned int rc, foundhdr, yes = 1;
1245 DWORD dwBytes, dwSize, dwFlags;
1254 memset(&ov, 0, sizeof(ov));
1255 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1256 if (ov.hEvent == INVALID_HANDLE_VALUE)
1258 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
1262 memset(&hdr, 0x00, sizeof(hdr));
1263 s1addr.sin_family = AF_INET;
1264 s1addr.sin_port = htons(0);
1265 /* Note: s1addr.sin_addr is set below */
1266 iovec[0].buf = recvbuf;
1267 iovec[0].len = sizeof(recvbuf);
1268 hdr.name = (struct sockaddr*)&s3addr;
1269 hdr.namelen = sizeof(s3addr);
1270 hdr.lpBuffers = &iovec[0];
1271 hdr.dwBufferCount = 1;
1272 hdr.Control.buf = pktbuf;
1273 /* Note: hdr.Control.len is set below */
1276 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++)
1278 s1addr.sin_addr.s_addr = addresses[i];
1280 /* Build "server" side socket */
1281 s1=socket(AF_INET, SOCK_DGRAM, 0);
1282 if (s1 == INVALID_SOCKET)
1284 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1288 /* Obtain the WSARecvMsg function */
1289 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
1290 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
1293 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
1298 /* Setup the server side socket */
1299 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
1300 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
1301 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
1302 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1304 /* Build "client" side socket */
1305 addrlen = sizeof(s2addr);
1306 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0)
1308 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
1312 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
1313 s2=socket(AF_INET, SOCK_DGRAM, 0);
1314 if (s2 == INVALID_SOCKET)
1316 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1321 /* Test an empty message header */
1322 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
1323 err=WSAGetLastError();
1324 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
1327 * Send a packet from the client to the server and test for specifying
1328 * a short control header.
1330 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1331 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1332 hdr.Control.len = 1;
1333 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1334 err=WSAGetLastError();
1335 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
1336 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
1337 hdr.dwFlags = 0; /* Reset flags */
1339 /* Perform another short control header test, this time with an overlapped receive */
1340 hdr.Control.len = 1;
1341 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1342 err=WSAGetLastError();
1343 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1344 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1345 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1346 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1348 skip("Server side did not receive packet, some tests skipped.\n");
1354 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
1356 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
1357 ok(hdr.dwFlags == MSG_CTRUNC,
1358 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
1359 hdr.dwFlags = 0; /* Reset flags */
1362 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1363 * on the server end and check that the returned packet matches what was sent.
1365 hdr.Control.len = sizeof(pktbuf);
1366 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1367 err=WSAGetLastError();
1368 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1369 ok(hdr.Control.len == sizeof(pktbuf),
1370 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
1371 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1372 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1373 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1375 skip("Server side did not receive packet, some tests skipped.\n");
1381 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
1382 ok(dwSize == sizeof(msg),
1383 "WSARecvMsg() buffer length does not match transmitted data!\n");
1384 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
1385 "WSARecvMsg() buffer does not match transmitted data!\n");
1386 ok(hdr.Control.len == IP_PKTINFO_LEN,
1387 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
1389 /* Test for the expected IP_PKTINFO return information. */
1391 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
1393 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
1395 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
1397 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
1401 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
1408 CloseHandle(ov.hEvent);
1411 /************* Array containing the tests to run **********/
1413 #define STD_STREAM_SOCKET \
1419 static test_setup tests [] =
1421 /* Test 0: synchronous client and server */
1442 /* Test 1: event-driven client, synchronous server */
1459 WSA_FLAG_OVERLAPPED,
1463 /* Test 2: synchronous client, non-blocking server via select() */
1484 /* Test 3: OOB client, OOB server */
1505 /* Test 4: synchronous mixed client and server */
1519 simple_mixed_client,
1528 static void test_UDP(void)
1530 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1531 possible that this test fails due to dropped packets. */
1533 /* peer 0 receives data from all other peers */
1534 struct sock_info peer[NUM_UDP_PEERS];
1536 int ss, i, n_recv, n_sent;
1538 memset (buf,0,sizeof(buf));
1539 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1540 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1542 peer[i].addr.sin_family = AF_INET;
1543 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1546 peer[i].addr.sin_port = htons ( SERVERPORT );
1548 peer[i].addr.sin_port = htons ( 0 );
1551 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1553 /* test getsockname() to get peer's port */
1554 ss = sizeof ( peer[i].addr );
1555 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1556 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1559 /* test getsockname() */
1560 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1562 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1563 /* send client's ip */
1564 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1565 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1566 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1569 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1570 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1571 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1572 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1576 static DWORD WINAPI do_getservbyname( void *param )
1582 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1584 HANDLE *starttest = param;
1586 struct servent *pserv[2];
1588 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1590 /* ensure that necessary buffer resizes are completed */
1591 for ( j = 0; j < 2; j++) {
1592 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1595 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1596 for ( j = 0; j < 2; j++ ) {
1597 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1598 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1599 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) );
1600 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 );
1601 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 );
1604 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1610 static void test_getservbyname(void)
1613 HANDLE starttest, thread[NUM_THREADS];
1614 DWORD thread_id[NUM_THREADS];
1616 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1618 /* create threads */
1619 for ( i = 0; i < NUM_THREADS; i++ ) {
1620 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
1623 /* signal threads to start */
1624 SetEvent ( starttest );
1626 for ( i = 0; i < NUM_THREADS; i++) {
1627 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1631 static void test_WSASocket(void)
1633 SOCKET sock = INVALID_SOCKET;
1634 WSAPROTOCOL_INFOA *pi;
1635 int providers[] = {6, 0};
1639 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1640 * to avoid a crash on win98.
1643 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1644 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1646 err = WSAGetLastError();
1647 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1650 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1651 ok(pi != NULL, "Failed to allocate memory\n");
1653 skip("Can't continue without memory.\n");
1657 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1658 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1662 skip("No protocols enumerated.\n");
1663 HeapFree(GetProcessHeap(), 0, pi);
1667 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1668 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1669 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1673 HeapFree(GetProcessHeap(), 0, pi);
1676 static void test_WSAAddressToStringA(void)
1678 SOCKET v6 = INVALID_SOCKET;
1682 SOCKADDR_IN sockaddr;
1683 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1685 CHAR expect1[] = "0.0.0.0";
1686 CHAR expect2[] = "255.255.255.255";
1687 CHAR expect3[] = "0.0.0.0:65535";
1688 CHAR expect4[] = "255.255.255.255:65535";
1690 SOCKADDR_IN6 sockaddr6;
1691 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1693 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1694 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1695 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1697 CHAR expect6_1[] = "::1";
1698 CHAR expect6_2[] = "20ab::1";
1699 CHAR expect6_3[] = "[20ab::2001]:33274";
1700 CHAR expect6_3_nt[] = "20ab::2001@33274";
1701 CHAR expect6_3_w2k[] = "20ab::2001";
1702 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
1703 CHAR expect6_3_2_nt[] = "4660/20ab::2001@33274";
1704 CHAR expect6_3_2_w2k[] = "20ab::2001%4660";
1705 CHAR expect6_3_3[] = "20ab::2001%4660";
1706 CHAR expect6_3_3_nt[] = "4660/20ab::2001";
1710 sockaddr.sin_family = AF_INET;
1711 sockaddr.sin_port = 0;
1712 sockaddr.sin_addr.s_addr = 0;
1714 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1715 GLE = WSAGetLastError();
1716 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1717 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1720 len = sizeof(address);
1722 sockaddr.sin_family = AF_INET;
1723 sockaddr.sin_port = 0;
1724 sockaddr.sin_addr.s_addr = 0;
1726 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1727 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1729 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1730 ok( len == sizeof( expect1 ), "Got size %d\n", len);
1732 len = sizeof(address);
1734 sockaddr.sin_family = AF_INET;
1735 sockaddr.sin_port = 0;
1736 sockaddr.sin_addr.s_addr = 0xffffffff;
1738 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1739 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1741 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1743 len = sizeof(address);
1745 sockaddr.sin_family = AF_INET;
1746 sockaddr.sin_port = 0xffff;
1747 sockaddr.sin_addr.s_addr = 0;
1749 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1750 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1752 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1754 len = sizeof(address);
1756 sockaddr.sin_family = AF_INET;
1757 sockaddr.sin_port = 0xffff;
1758 sockaddr.sin_addr.s_addr = 0xffffffff;
1760 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1761 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1763 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1764 ok( len == sizeof( expect4 ), "Got size %d\n", len);
1766 /*check to see it IPv6 is available */
1767 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1768 if (v6 == INVALID_SOCKET) {
1769 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1770 WSAGetLastError(), WSAEAFNOSUPPORT);
1773 /* Test a short IPv6 address */
1774 len = sizeof(address6);
1776 sockaddr6.sin6_family = AF_INET6;
1777 sockaddr6.sin6_port = 0x0000;
1778 sockaddr6.sin6_scope_id = 0;
1779 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1781 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1782 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1783 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
1784 ok( len == sizeof(expect6_1), "Got size %d\n", len);
1786 /* Test a longer IPv6 address */
1787 len = sizeof(address6);
1789 sockaddr6.sin6_family = AF_INET6;
1790 sockaddr6.sin6_port = 0x0000;
1791 sockaddr6.sin6_scope_id = 0;
1792 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1794 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1795 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1796 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
1797 ok( len == sizeof(expect6_2), "Got size %d\n", len);
1799 /* Test IPv6 address and port number */
1800 len = sizeof(address6);
1802 sockaddr6.sin6_family = AF_INET6;
1803 sockaddr6.sin6_port = 0xfa81;
1804 sockaddr6.sin6_scope_id = 0;
1805 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1807 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1808 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1809 ok( !strcmp( address6, expect6_3 ) ||
1810 broken( !strcmp( address6, expect6_3_nt ) ) || /* NT4 */
1811 broken( !strcmp( address6, expect6_3_w2k ) ), /* Win2000 */
1812 "Expected: %s, got: %s\n", expect6_3, address6 );
1813 ok( len == sizeof(expect6_3) ||
1814 broken( len == sizeof(expect6_3_nt) ) || /* NT4 */
1815 broken( len == sizeof(expect6_3_w2k) ), /* Win2000 */
1816 "Got size %d\n", len);
1818 /* Test IPv6 address, port number and scope_id */
1819 len = sizeof(address6);
1821 sockaddr6.sin6_family = AF_INET6;
1822 sockaddr6.sin6_port = 0xfa81;
1823 sockaddr6.sin6_scope_id = 0x1234;
1824 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1826 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1827 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1828 ok( !strcmp( address6, expect6_3_2 ) ||
1829 broken( !strcmp( address6, expect6_3_2_nt ) ) || /* NT4 */
1830 broken( !strcmp( address6, expect6_3_2_w2k ) ), /* Win2000 */
1831 "Expected: %s, got: %s\n", expect6_3_2, address6 );
1832 ok( len == sizeof(expect6_3_2) ||
1833 broken( len == sizeof(expect6_3_2_nt) ) || /* NT4 */
1834 broken( len == sizeof(expect6_3_2_w2k) ), /* Win2000 */
1835 "Got size %d\n", len);
1837 /* Test IPv6 address and scope_id */
1838 len = sizeof(address6);
1840 sockaddr6.sin6_family = AF_INET6;
1841 sockaddr6.sin6_port = 0x0000;
1842 sockaddr6.sin6_scope_id = 0x1234;
1843 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1845 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1846 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1847 ok( !strcmp( address6, expect6_3_3 ) ||
1848 broken( !strcmp( address6, expect6_3_3_nt ) ), /* NT4 */
1849 "Expected: %s, got: %s\n", expect6_3_3, address6 );
1850 ok( len == sizeof(expect6_3_3) ||
1851 broken( len == sizeof(expect6_3_3_nt) ), /* NT4 */
1852 "Got size %d\n", len);
1855 if (v6 != INVALID_SOCKET)
1859 static void test_WSAAddressToStringW(void)
1861 SOCKET v6 = INVALID_SOCKET;
1865 SOCKADDR_IN sockaddr;
1866 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1868 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1869 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1870 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1871 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1872 '6', '5', '5', '3', '5', 0 };
1874 SOCKADDR_IN6 sockaddr6;
1875 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1877 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1878 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1879 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1881 WCHAR expect6_1[] = {':',':','1',0};
1882 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
1883 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1884 WCHAR expect6_3_nt[] = {'2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1885 WCHAR expect6_3_w2k[] = {'2','0','a','b',':',':','2','0','0','1',0};
1886 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1887 WCHAR expect6_3_2_nt[] = {'4','6','6','0','/','2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1888 WCHAR expect6_3_2_w2k[] = {'2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',0};
1889 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1890 WCHAR expect6_3_3_nt[] = {'6','5','5','3','4','/','2','0','a','b',':',':','2','0','0','1',0};
1894 sockaddr.sin_family = AF_INET;
1895 sockaddr.sin_port = 0;
1896 sockaddr.sin_addr.s_addr = 0;
1898 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1899 GLE = WSAGetLastError();
1900 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1901 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1904 len = sizeof(address);
1906 sockaddr.sin_family = AF_INET;
1907 sockaddr.sin_port = 0;
1908 sockaddr.sin_addr.s_addr = 0;
1910 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1911 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1913 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1914 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
1916 len = sizeof(address);
1918 sockaddr.sin_family = AF_INET;
1919 sockaddr.sin_port = 0;
1920 sockaddr.sin_addr.s_addr = 0xffffffff;
1922 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1923 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1925 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1927 len = sizeof(address);
1929 sockaddr.sin_family = AF_INET;
1930 sockaddr.sin_port = 0xffff;
1931 sockaddr.sin_addr.s_addr = 0;
1933 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1934 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1936 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1938 len = sizeof(address);
1940 sockaddr.sin_family = AF_INET;
1941 sockaddr.sin_port = 0xffff;
1942 sockaddr.sin_addr.s_addr = 0xffffffff;
1944 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1945 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1947 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1948 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len);
1950 /*check to see it IPv6 is available */
1951 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1952 if (v6 == INVALID_SOCKET) {
1953 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1954 WSAGetLastError(), WSAEAFNOSUPPORT);
1958 /* Test a short IPv6 address */
1959 len = sizeof(address6)/sizeof(WCHAR);
1961 sockaddr6.sin6_family = AF_INET6;
1962 sockaddr6.sin6_port = 0x0000;
1963 sockaddr6.sin6_scope_id = 0;
1964 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1966 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1967 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1968 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
1969 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
1971 /* Test a longer IPv6 address */
1972 len = sizeof(address6)/sizeof(WCHAR);
1974 sockaddr6.sin6_family = AF_INET6;
1975 sockaddr6.sin6_port = 0x0000;
1976 sockaddr6.sin6_scope_id = 0;
1977 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1979 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1980 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1982 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
1983 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
1985 /* Test IPv6 address and port number */
1986 len = sizeof(address6)/sizeof(WCHAR);
1988 sockaddr6.sin6_family = AF_INET6;
1989 sockaddr6.sin6_port = 0xfa81;
1990 sockaddr6.sin6_scope_id = 0;
1991 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1993 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1994 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1995 ok( !lstrcmpW( address6, expect6_3 ) ||
1996 broken( !lstrcmpW( address6, expect6_3_nt ) ) || /* NT4 */
1997 broken( !lstrcmpW( address6, expect6_3_w2k ) ), /* Win2000 */
1998 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3),
1999 wine_dbgstr_w(address6) );
2000 ok( len == sizeof(expect6_3)/sizeof(WCHAR) ||
2001 broken(len == sizeof(expect6_3_nt)/sizeof(WCHAR) ) || /* NT4 */
2002 broken(len == sizeof(expect6_3_w2k)/sizeof(WCHAR) ), /* Win2000 */
2005 /* Test IPv6 address, port number and scope_id */
2006 len = sizeof(address6)/sizeof(WCHAR);
2008 sockaddr6.sin6_family = AF_INET6;
2009 sockaddr6.sin6_port = 0xfa81;
2010 sockaddr6.sin6_scope_id = 0x1234;
2011 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
2013 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
2014 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2015 ok( !lstrcmpW( address6, expect6_3_2 ) ||
2016 broken( !lstrcmpW( address6, expect6_3_2_nt ) ) || /* NT4 */
2017 broken( !lstrcmpW( address6, expect6_3_2_w2k ) ), /* Win2000 */
2018 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2),
2019 wine_dbgstr_w(address6) );
2020 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR) ||
2021 broken( len == sizeof(expect6_3_2_nt)/sizeof(WCHAR) ) || /* NT4 */
2022 broken( len == sizeof(expect6_3_2_w2k)/sizeof(WCHAR) ), /* Win2000 */
2025 /* Test IPv6 address and scope_id */
2026 len = sizeof(address6)/sizeof(WCHAR);
2028 sockaddr6.sin6_family = AF_INET6;
2029 sockaddr6.sin6_port = 0x0000;
2030 sockaddr6.sin6_scope_id = 0xfffe;
2031 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
2033 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
2034 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2035 ok( !lstrcmpW( address6, expect6_3_3 ) ||
2036 broken( !lstrcmpW( address6, expect6_3_3_nt ) ), /* NT4 */
2037 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3),
2038 wine_dbgstr_w(address6) );
2039 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR) ||
2040 broken( len == sizeof(expect6_3_3_nt)/sizeof(WCHAR) ), /* NT4 */
2044 if (v6 != INVALID_SOCKET)
2048 static void test_WSAStringToAddressA(void)
2051 SOCKADDR_IN sockaddr;
2052 SOCKADDR_IN6 sockaddr6;
2055 CHAR address1[] = "0.0.0.0";
2056 CHAR address2[] = "127.127.127.127";
2057 CHAR address3[] = "255.255.255.255";
2058 CHAR address4[] = "127.127.127.127:65535";
2059 CHAR address5[] = "255.255.255.255:65535";
2060 CHAR address6[] = "::1";
2061 CHAR address7[] = "[::1]";
2062 CHAR address8[] = "[::1]:65535";
2065 sockaddr.sin_family = AF_INET;
2067 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2068 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
2069 WSAGetLastError() );
2071 len = sizeof(sockaddr);
2072 sockaddr.sin_port = 0;
2073 sockaddr.sin_addr.s_addr = 0;
2075 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2076 ok( !ret && sockaddr.sin_addr.s_addr == 0,
2077 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2079 len = sizeof(sockaddr);
2080 sockaddr.sin_port = 0;
2081 sockaddr.sin_addr.s_addr = 0;
2083 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2084 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
2085 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2087 len = sizeof(sockaddr);
2088 sockaddr.sin_port = 0;
2089 sockaddr.sin_addr.s_addr = 0;
2091 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2092 GLE = WSAGetLastError();
2093 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
2094 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2095 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
2097 len = sizeof(sockaddr);
2098 sockaddr.sin_port = 0;
2099 sockaddr.sin_addr.s_addr = 0;
2101 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2102 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
2103 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2105 len = sizeof(sockaddr);
2106 sockaddr.sin_port = 0;
2107 sockaddr.sin_addr.s_addr = 0;
2109 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2110 GLE = WSAGetLastError();
2111 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
2112 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2113 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
2115 len = sizeof(sockaddr6);
2116 memset(&sockaddr6, 0, len);
2117 sockaddr6.sin6_family = AF_INET6;
2119 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2121 GLE = WSAGetLastError();
2122 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2123 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2125 len = sizeof(sockaddr6);
2126 memset(&sockaddr6, 0, len);
2127 sockaddr6.sin6_family = AF_INET6;
2129 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2131 GLE = WSAGetLastError();
2132 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2133 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2135 len = sizeof(sockaddr6);
2136 memset(&sockaddr6, 0, len);
2137 sockaddr6.sin6_family = AF_INET6;
2139 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2141 GLE = WSAGetLastError();
2142 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
2143 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2144 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2148 static void test_WSAStringToAddressW(void)
2151 SOCKADDR_IN sockaddr, *sin;
2152 SOCKADDR_IN6 sockaddr6;
2153 SOCKADDR_STORAGE sockaddr_storage;
2156 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
2157 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
2158 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
2159 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
2160 ':', '6', '5', '5', '3', '5', 0 };
2161 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
2162 '6', '5', '5', '3', '5', 0 };
2163 WCHAR address6[] = {':',':','1','\0'};
2164 WCHAR address7[] = {'[',':',':','1',']','\0'};
2165 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
2168 sockaddr.sin_family = AF_INET;
2170 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2171 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
2172 WSAGetLastError() );
2174 len = sizeof(sockaddr);
2175 sockaddr.sin_port = 0;
2176 sockaddr.sin_addr.s_addr = 0;
2178 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2179 ok( !ret && sockaddr.sin_addr.s_addr == 0,
2180 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2182 len = sizeof(sockaddr);
2183 sockaddr.sin_port = 0;
2184 sockaddr.sin_addr.s_addr = 0;
2186 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2187 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
2188 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2190 len = sizeof(sockaddr);
2191 sockaddr.sin_port = 0;
2192 sockaddr.sin_addr.s_addr = 0;
2194 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2195 GLE = WSAGetLastError();
2196 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
2197 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2198 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2200 len = sizeof(sockaddr);
2201 sockaddr.sin_port = 0;
2202 sockaddr.sin_addr.s_addr = 0;
2204 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2205 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
2206 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2208 len = sizeof(sockaddr);
2209 sockaddr.sin_port = 0;
2210 sockaddr.sin_addr.s_addr = 0;
2212 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2213 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
2214 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2215 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2217 /* Test with a larger buffer than necessary */
2218 len = sizeof(sockaddr_storage);
2219 sin = (SOCKADDR_IN *)&sockaddr_storage;
2221 sin->sin_addr.s_addr = 0;
2223 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)sin, &len );
2224 ok( (ret == 0 && sin->sin_addr.s_addr == 0xffffffff && sin->sin_port == 0xffff) ||
2225 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2226 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2227 ok( len == sizeof(SOCKADDR_IN) ||
2228 broken(len == sizeof(SOCKADDR_STORAGE)) /* NT4/2k */,
2229 "unexpected length %d\n", len );
2231 len = sizeof(sockaddr6);
2232 memset(&sockaddr6, 0, len);
2233 sockaddr6.sin6_family = AF_INET6;
2235 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2237 GLE = WSAGetLastError();
2238 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2239 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2241 len = sizeof(sockaddr6);
2242 memset(&sockaddr6, 0, len);
2243 sockaddr6.sin6_family = AF_INET6;
2245 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2247 GLE = WSAGetLastError();
2248 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2249 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2251 len = sizeof(sockaddr6);
2252 memset(&sockaddr6, 0, len);
2253 sockaddr6.sin6_family = AF_INET6;
2255 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2257 GLE = WSAGetLastError();
2258 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
2259 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2260 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2264 static DWORD WINAPI SelectReadThread(void *param)
2266 select_thread_params *par = param;
2269 struct sockaddr_in addr;
2270 struct timeval select_timeout;
2273 FD_SET(par->s, &readfds);
2274 select_timeout.tv_sec=5;
2275 select_timeout.tv_usec=0;
2276 addr.sin_family = AF_INET;
2277 addr.sin_addr.s_addr = inet_addr(SERVERIP);
2278 addr.sin_port = htons(SERVERPORT);
2280 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
2281 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
2283 SetEvent(server_ready);
2284 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
2285 par->ReadKilled = (ret == 1);
2290 static void test_errors(void)
2293 SOCKADDR_IN SockAddr;
2296 WSASetLastError(NO_ERROR);
2297 sock = socket(PF_INET, SOCK_STREAM, 0);
2298 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2299 memset(&SockAddr, 0, sizeof(SockAddr));
2300 SockAddr.sin_family = AF_INET;
2301 SockAddr.sin_port = htons(6924);
2302 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
2304 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
2305 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret );
2306 if (ret == SOCKET_ERROR)
2308 err = WSAGetLastError();
2309 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err );
2314 fd_set set = {1, {sock}};
2317 timeval.tv_usec = 50000;
2319 ret = select(1, NULL, &set, NULL, &timeval);
2320 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret );
2323 ret = closesocket(sock);
2324 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
2327 static void test_select(void)
2329 SOCKET fdRead, fdWrite;
2330 fd_set readfds, writefds, exceptfds;
2334 struct timeval select_timeout;
2335 select_thread_params thread_params;
2336 HANDLE thread_handle;
2339 fdRead = socket(AF_INET, SOCK_STREAM, 0);
2340 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2341 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
2342 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2346 FD_ZERO(&exceptfds);
2347 FD_SET(fdRead, &readfds);
2348 FD_SET(fdWrite, &writefds);
2349 FD_SET(fdRead, &exceptfds);
2350 FD_SET(fdWrite, &exceptfds);
2351 select_timeout.tv_sec=0;
2352 select_timeout.tv_usec=500;
2355 if (fdWrite > maxfd)
2359 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2360 ok ( (ret == 0), "select should not return any socket handles\n");
2361 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2363 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
2365 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2366 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
2369 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
2371 ret = closesocket(fdWrite);
2372 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2374 thread_params.s = fdRead;
2375 thread_params.ReadKilled = FALSE;
2376 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2377 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id );
2378 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
2380 WaitForSingleObject (server_ready, INFINITE);
2382 ret = closesocket(fdRead);
2383 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2385 WaitForSingleObject (thread_handle, 1000);
2386 ok ( (thread_params.ReadKilled) ||
2387 broken(thread_params.ReadKilled == 0), /*Win98*/
2388 "closesocket did not wakeup select\n");
2389 ret = recv(fdRead, &buffer, 1, MSG_PEEK);
2390 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret);
2392 /* Test selecting invalid handles */
2395 FD_ZERO(&exceptfds);
2398 ret = select(maxfd+1, 0, 0, 0, &select_timeout);
2399 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2400 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2403 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2404 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2405 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2407 FD_SET(INVALID_SOCKET, &readfds);
2409 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2410 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2411 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2412 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2415 FD_SET(INVALID_SOCKET, &writefds);
2417 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2418 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2419 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2420 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n");
2423 FD_SET(INVALID_SOCKET, &exceptfds);
2425 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2426 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2427 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2428 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2431 static DWORD WINAPI AcceptKillThread(void *param)
2433 select_thread_params *par = param;
2434 struct sockaddr_in address;
2435 int len = sizeof(address);
2436 SOCKET client_socket;
2438 SetEvent(server_ready);
2439 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
2440 if (client_socket != INVALID_SOCKET)
2441 closesocket(client_socket);
2442 par->ReadKilled = (client_socket == INVALID_SOCKET);
2447 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
2448 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
2449 GROUP FAR * g, DWORD_PTR dwCallbackData)
2454 static void test_accept(void)
2457 SOCKET server_socket = INVALID_SOCKET, accepted = INVALID_SOCKET, connector = INVALID_SOCKET;
2458 struct sockaddr_in address;
2460 select_thread_params thread_params;
2461 HANDLE thread_handle = NULL;
2464 server_socket = socket(AF_INET, SOCK_STREAM, 0);
2465 if (server_socket == INVALID_SOCKET)
2467 trace("error creating server socket: %d\n", WSAGetLastError());
2471 memset(&address, 0, sizeof(address));
2472 address.sin_addr.s_addr = inet_addr("127.0.0.1");
2473 address.sin_family = AF_INET;
2474 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
2477 trace("error binding server socket: %d\n", WSAGetLastError());
2481 socklen = sizeof(address);
2482 ret = getsockname(server_socket, (struct sockaddr*)&address, &socklen);
2484 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2488 ret = listen(server_socket, 5);
2491 trace("error making server socket listen: %d\n", WSAGetLastError());
2495 trace("Blocking accept next\n");
2497 connector = socket(AF_INET, SOCK_STREAM, 0);
2498 ok(connector != INVALID_SOCKET, "Failed to create connector socket, error %d\n", WSAGetLastError());
2500 ret = connect(connector, (struct sockaddr*)&address, sizeof(address));
2501 ok(ret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2503 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0);
2504 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
2506 accepted = accept(server_socket, NULL, 0);
2507 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2509 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2510 if (server_ready == INVALID_HANDLE_VALUE)
2512 trace("error creating event: %d\n", GetLastError());
2516 thread_params.s = server_socket;
2517 thread_params.ReadKilled = FALSE;
2518 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id);
2519 if (thread_handle == NULL)
2521 trace("error creating thread: %d\n", GetLastError());
2525 WaitForSingleObject(server_ready, INFINITE);
2527 ret = closesocket(server_socket);
2530 trace("closesocket failed: %d\n", WSAGetLastError());
2534 WaitForSingleObject(thread_handle, 1000);
2535 ok(thread_params.ReadKilled || broken(!thread_params.ReadKilled) /* Win98/ME, after accept */,
2536 "closesocket did not wakeup accept\n");
2539 if (accepted != INVALID_SOCKET)
2540 closesocket(accepted);
2541 if (connector != INVALID_SOCKET)
2542 closesocket(connector);
2543 if (thread_handle != NULL)
2544 CloseHandle(thread_handle);
2545 if (server_ready != INVALID_HANDLE_VALUE)
2546 CloseHandle(server_ready);
2547 if (server_socket != INVALID_SOCKET)
2548 closesocket(server_socket);
2551 static void test_extendedSocketOptions(void)
2555 struct sockaddr_in sa;
2556 int sa_len = sizeof(struct sockaddr_in);
2557 int optval, optlen = sizeof(int), ret;
2561 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2562 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2566 memset(&sa, 0, sa_len);
2568 sa.sin_family = AF_INET;
2569 sa.sin_port = htons(0);
2570 sa.sin_addr.s_addr = htonl(INADDR_ANY);
2572 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
2573 trace("Creating the socket failed: %d\n", WSAGetLastError());
2578 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2579 trace("Failed to bind socket: %d\n", WSAGetLastError());
2585 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2587 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
2588 ok((optval == 65507) || (optval == 65527),
2589 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
2591 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2592 SetLastError(0xdeadbeef);
2593 optval = 0xdeadbeef;
2594 optlen = sizeof(int);
2595 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2596 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2597 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2598 ret, WSAGetLastError(), optval, optval);
2600 /* more invalid values for level */
2601 SetLastError(0xdeadbeef);
2602 optval = 0xdeadbeef;
2603 optlen = sizeof(int);
2604 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2605 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2606 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2607 ret, WSAGetLastError(), optval, optval);
2609 SetLastError(0xdeadbeef);
2610 optval = 0xdeadbeef;
2611 optlen = sizeof(int);
2612 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2613 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2614 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2615 ret, WSAGetLastError(), optval, optval);
2617 SetLastError(0xdeadbeef);
2618 optval = 0xdeadbeef;
2619 optlen = sizeof(int);
2620 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2621 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2622 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2623 ret, WSAGetLastError(), optval, optval);
2625 SetLastError(0xdeadbeef);
2626 optval = 0xdeadbeef;
2627 optlen = sizeof(int);
2628 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2629 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2630 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2631 ret, WSAGetLastError(), optval, optval);
2633 optlen = sizeof(LINGER);
2634 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2636 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
2641 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2642 trace("Creating the socket failed: %d\n", WSAGetLastError());
2647 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2648 trace("Failed to bind socket: %d\n", WSAGetLastError());
2654 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2655 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
2657 optlen = sizeof(BOOL);
2658 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
2659 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
2660 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
2661 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2662 bool_opt_val, linger_val.l_onoff);
2668 static void test_getsockname(void)
2672 struct sockaddr_in sa_set, sa_get;
2673 int sa_set_len = sizeof(struct sockaddr_in);
2674 int sa_get_len = sa_set_len;
2675 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
2678 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2679 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2683 memset(&sa_set, 0, sa_set_len);
2685 sa_set.sin_family = AF_INET;
2686 sa_set.sin_port = htons(0);
2687 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
2689 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2690 trace("Creating the socket failed: %d\n", WSAGetLastError());
2695 memcpy(&sa_get, &sa_set, sizeof(sa_set));
2696 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
2697 ok(0, "getsockname on unbound socket should fail\n");
2699 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
2700 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
2701 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
2702 "failed getsockname modified sockaddr when it shouldn't\n");
2705 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
2706 trace("Failed to bind socket: %d\n", WSAGetLastError());
2712 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
2713 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2719 ret = memcmp(sa_get.sin_zero, null_padding, 8);
2720 ok(ret == 0 || broken(ret != 0), /* NT4 */
2721 "getsockname did not zero the sockaddr_in structure\n");
2727 static void test_dns(void)
2731 h = gethostbyname("");
2732 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
2735 /* Our winsock headers don't define gethostname because it conflicts with the
2736 * definition in unistd.h. Define it here to get rid of the warning. */
2738 int WINAPI gethostname(char *name, int namelen);
2740 static void test_gethostbyname_hack(void)
2744 static BYTE loopback[] = {127, 0, 0, 1};
2745 static BYTE magic_loopback[] = {127, 12, 34, 56};
2748 ret = gethostname(name, 256);
2749 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
2751 he = gethostbyname("localhost");
2752 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
2755 if(he->h_length != 4)
2757 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2761 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
2762 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2763 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
2764 he->h_addr_list[0][3]);
2767 if(strcmp(name, "localhost") == 0)
2769 skip("hostname seems to be \"localhost\", skipping test.\n");
2774 he = gethostbyname(name);
2775 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
2778 if(he->h_length != 4)
2780 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2784 if (he->h_addr_list[0][0] == 127)
2786 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
2787 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2788 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
2789 he->h_addr_list[0][2], he->h_addr_list[0][3]);
2793 gethostbyname("nonexistent.winehq.org");
2794 /* Don't check for the return value, as some braindead ISPs will kindly
2795 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2798 static void test_inet_addr(void)
2802 addr = inet_addr(NULL);
2803 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
2806 static void test_addr_to_print(void)
2812 struct in6_addr in6;
2814 u_long addr0_Num = 0x00000000;
2815 PCSTR addr0_Str = "0.0.0.0";
2816 u_long addr1_Num = 0x20201015;
2817 PCSTR addr1_Str = "21.16.32.32";
2818 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2819 PCSTR addr2_Str = "::fffe:cc98:bd74";
2820 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
2821 PCSTR addr3_Str = "2030:a4b1::";
2822 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2823 PCSTR addr4_Str = "::204.152.189.116";
2825 /* Test IPv4 addresses */
2826 in.s_addr = addr0_Num;
2828 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2829 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2830 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
2832 /* Test that inet_ntoa and inet_ntop return the same value */
2833 in.S_un.S_addr = addr1_Num;
2834 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2835 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2836 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2838 /* InetNtop became available in Vista and Win2008 */
2841 win_skip("InetNtop not present, not executing tests\n");
2845 /* Second part of test */
2846 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
2847 ok(pdst != NULL, "InetNtop failed %s\n", dst);
2848 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2850 /* Test invalid parm conditions */
2851 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
2852 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2853 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
2855 /* Test Null destination */
2857 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, NULL, sizeof(dst));
2858 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2859 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2860 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2862 /* Test zero length passed */
2865 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 0);
2866 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2867 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2868 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2870 /* Test length one shorter than the address length */
2873 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 6);
2874 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2875 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2876 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2878 /* Test longer length is ok */
2881 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, sizeof(dst)+1);
2882 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2883 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2885 /* Test the IPv6 addresses */
2887 /* Test an zero prefixed IPV6 address */
2888 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2889 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2890 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2891 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
2893 /* Test an zero suffixed IPV6 address */
2894 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
2895 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2896 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2897 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
2899 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2900 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num));
2901 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, sizeof(dst6));
2902 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2903 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str);
2905 /* Test invalid parm conditions */
2906 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2908 /* Test Null destination */
2910 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, NULL, sizeof(dst6));
2911 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2912 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2913 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2915 /* Test zero length passed */
2918 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 0);
2919 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2920 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2921 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2923 /* Test length one shorter than the address length */
2926 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 16);
2927 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2928 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2929 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2931 /* Test longer length is ok */
2934 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 18);
2935 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2938 static void test_ioctlsocket(void)
2941 struct tcp_keepalive kalive;
2943 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2947 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2948 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2949 if(sock == INVALID_SOCKET)
2951 skip("Can't continue without a socket.\n");
2955 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
2957 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2958 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
2959 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
2960 ret = WSAGetLastError();
2961 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
2964 /* A fresh and not connected socket has no urgent data, this test shows
2965 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2967 ret = ioctlsocket(sock, SIOCATMARK, &arg);
2968 if(ret != SOCKET_ERROR)
2969 todo_wine ok(arg, "expected a non-zero value\n");
2971 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
2972 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
2973 ret = WSAGetLastError();
2974 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
2976 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2977 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
2978 ret = WSAGetLastError();
2979 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
2981 /* broken used to catch W95, W98, NT4 */
2982 make_keepalive(kalive, 0, 0, 0);
2983 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2984 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
2986 make_keepalive(kalive, 1, 0, 0);
2987 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2988 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
2990 make_keepalive(kalive, 1, 1000, 1000);
2991 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2992 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
2994 make_keepalive(kalive, 1, 10000, 10000);
2995 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2996 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
2998 make_keepalive(kalive, 1, 100, 100);
2999 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3000 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3002 make_keepalive(kalive, 0, 100, 100);
3003 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3004 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3009 static int drain_pause=0;
3010 static DWORD WINAPI drain_socket_thread(LPVOID arg)
3013 SOCKET sock = *(SOCKET*)arg;
3016 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
3020 if (WSAGetLastError() == WSAEWOULDBLOCK)
3024 FD_SET(sock, &readset);
3025 select(sock+1, &readset, NULL, NULL, NULL);
3036 static void test_send(void)
3038 SOCKET src = INVALID_SOCKET;
3039 SOCKET dst = INVALID_SOCKET;
3040 HANDLE hThread = NULL;
3041 const int buflen = 1024*1024;
3042 char *buffer = NULL;
3043 int ret, i, zero = 0;
3047 DWORD id, bytes_sent, dwRet;
3049 memset(&ov, 0, sizeof(ov));
3051 if (tcp_socketpair(&src, &dst) != 0)
3053 ok(0, "creating socket pair failed, skipping test\n");
3057 set_blocking(dst, FALSE);
3058 /* force disable buffering so we can get a pending overlapped request */
3059 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
3060 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
3062 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3063 if (hThread == NULL)
3065 ok(0, "CreateThread failed, error %d\n", GetLastError());
3069 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
3072 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
3076 /* fill the buffer with some nonsense */
3077 for (i = 0; i < buflen; ++i)
3079 buffer[i] = (char) i;
3082 ret = send(src, buffer, buflen, 0);
3084 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
3086 ok(0, "send failed, error %d\n", WSAGetLastError());
3091 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3092 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
3097 WSASetLastError(12345);
3098 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
3099 ok((ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING) || broken(bytes_sent == buflen),
3100 "Failed to start overlapped send %d - %d - %d/%d\n", ret, WSAGetLastError(), bytes_sent, buflen);
3102 /* don't check for completion yet, we may need to drain the buffer while still sending */
3103 set_blocking(src, FALSE);
3104 for (i = 0; i < buflen; ++i)
3108 ret = recv(src, buffer, 1, 0);
3109 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
3113 ret = recv(src, buffer, 1, 0);
3116 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
3120 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
3123 dwRet = WaitForSingleObject(ov.hEvent, 1000);
3124 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3125 if (dwRet == WAIT_OBJECT_0)
3127 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
3128 ok((bret && bytes_sent == buflen) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3129 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
3132 WSASetLastError(12345);
3133 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
3134 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
3135 "WSASend failed %d - %d\n", ret, WSAGetLastError());
3137 WSASetLastError(12345);
3138 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
3139 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
3140 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
3143 if (src != INVALID_SOCKET)
3145 if (dst != INVALID_SOCKET)
3147 if (hThread != NULL)
3148 CloseHandle(hThread);
3150 CloseHandle(ov.hEvent);
3151 HeapFree(GetProcessHeap(), 0, buffer);
3154 typedef struct async_message
3158 struct async_message *next;
3161 static struct async_message *messages_received;
3163 #define WM_SOCKET (WM_USER+100)
3164 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3166 struct async_message *message;
3171 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message));
3172 message->socket = (SOCKET) wparam;
3173 message->lparam = lparam;
3174 message->next = NULL;
3176 if (messages_received)
3178 struct async_message *last = messages_received;
3179 while (last->next) last = last->next;
3180 last->next = message;
3183 messages_received = message;
3187 return DefWindowProc(hwnd, msg, wparam, lparam);
3190 static void get_event_details(int event, int *bit, char *name)
3195 if (bit) *bit = FD_ACCEPT_BIT;
3196 if (name) strcpy(name, "FD_ACCEPT");
3199 if (bit) *bit = FD_CONNECT_BIT;
3200 if (name) strcpy(name, "FD_CONNECT");
3203 if (bit) *bit = FD_READ_BIT;
3204 if (name) strcpy(name, "FD_READ");
3207 if (bit) *bit = FD_OOB_BIT;
3208 if (name) strcpy(name, "FD_OOB");
3211 if (bit) *bit = FD_WRITE_BIT;
3212 if (name) strcpy(name, "FD_WRITE");
3215 if (bit) *bit = FD_CLOSE_BIT;
3216 if (name) strcpy(name, "FD_CLOSE");
3220 if (name) sprintf(name, "bad%x", event);
3224 static const char *dbgstr_event_seq(const LPARAM *seq)
3226 static char message[1024];
3234 get_event_details(WSAGETSELECTEVENT(*seq), NULL, name);
3235 len += sprintf(message + len, "%s(%d) ", name, WSAGETSELECTERROR(*seq));
3239 strcpy( message + len, "]" );
3243 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents)
3245 static char message[1024];
3246 struct async_message *curr = messages_received;
3247 int index, error, bit = 0;
3257 if (bit >= FD_MAX_EVENTS) break;
3258 if ( !(netEvents->lNetworkEvents & (1 << bit)) )
3263 get_event_details(1 << bit, &index, name);
3264 error = netEvents->iErrorCode[index];
3270 if (curr->socket != s)
3275 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name);
3276 error = WSAGETSELECTERROR(curr->lparam);
3280 len += sprintf(message + len, "%s(%d) ", name, error);
3283 strcpy( message + len, "]" );
3287 static void flush_events(SOCKET s, HANDLE hEvent)
3289 WSANETWORKEVENTS netEvents;
3290 struct async_message *prev = NULL, *curr = messages_received;
3294 if (hEvent != INVALID_HANDLE_VALUE)
3296 dwRet = WaitForSingleObject(hEvent, 100);
3297 if (dwRet == WAIT_OBJECT_0)
3299 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents);
3301 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3308 if (curr->socket == s)
3310 if (prev) prev->next = curr->next;
3311 else messages_received = curr->next;
3313 HeapFree(GetProcessHeap(), 0, curr);
3315 if (prev) curr = prev->next;
3316 else curr = messages_received;
3327 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq)
3329 int event, index, error, events;
3330 struct async_message *curr;
3334 events = netEvents->lNetworkEvents;
3337 event = WSAGETSELECTEVENT(*seq);
3338 error = WSAGETSELECTERROR(*seq);
3339 get_event_details(event, &index, NULL);
3341 if (!(events & event) && index != -1)
3343 if (events & event && index != -1)
3345 if (netEvents->iErrorCode[index] != error)
3356 curr = messages_received;
3359 if (curr->socket == s)
3361 if (!*seq) return 0;
3362 if (*seq != curr->lparam) return 0;
3373 /* checks for a sequence of events, (order only checked if window is used) */
3374 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq, const LPARAM **broken_seqs, int completelyBroken)
3377 WSANETWORKEVENTS events, *netEvents = NULL;
3381 if (hEvent != INVALID_HANDLE_VALUE)
3383 netEvents = &events;
3385 dwRet = WaitForSingleObject(hEvent, 200);
3386 if (dwRet == WAIT_OBJECT_0)
3388 ret = WSAEnumNetworkEvents(s, hEvent, netEvents);
3391 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3396 memset(netEvents, 0, sizeof(*netEvents));
3401 /* Run the message loop a little */
3402 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ))
3404 DispatchMessageA(&msg);
3408 if (match_event_sequence(s, netEvents, seq))
3410 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq));
3411 flush_events(s, hEvent);
3417 for (; *broken_seqs; broken_seqs++)
3419 if (match_event_sequence(s, netEvents, *broken_seqs))
3421 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s, netEvents), dbgstr_event_seq(seq));
3422 flush_events(s, hEvent);
3428 winetest_ok(broken(completelyBroken), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq),
3429 dbgstr_event_seq_result(s, netEvents));
3430 flush_events(s, hEvent);
3433 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
3435 static void test_events(int useMessages)
3437 SOCKET server = INVALID_SOCKET;
3438 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET;
3439 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET;
3440 struct sockaddr_in addr;
3441 HANDLE hThread = NULL;
3442 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE;
3443 WNDCLASSEX wndclass;
3445 char *buffer = NULL;
3446 int bufferSize = 1024*1024;
3450 DWORD bytesReturned;
3456 static char szClassName[] = "wstestclass";
3457 const LPARAM *broken_seq[3];
3458 static const LPARAM empty_seq[] = { 0 };
3459 static const LPARAM close_seq[] = { WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3460 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3461 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3462 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 };
3463 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0),
3464 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3465 static const LPARAM read_read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3466 WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3467 static const LPARAM read_write_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3468 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3469 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3470 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3472 memset(&ov, 0, sizeof(ov));
3473 memset(&ov2, 0, sizeof(ov2));
3475 /* don't use socketpair, we want connection event */
3476 src = socket(AF_INET, SOCK_STREAM, 0);
3477 if (src == INVALID_SOCKET)
3479 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3483 src2 = socket(AF_INET, SOCK_STREAM, 0);
3484 if (src2 == INVALID_SOCKET)
3486 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3491 if (getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len) == SOCKET_ERROR)
3493 ok(0, "failed to get oobinline status, %d\n", GetLastError());
3496 ok(bret == FALSE, "OOB not inline\n");
3500 trace("Event test using messages\n");
3502 wndclass.cbSize = sizeof(wndclass);
3503 wndclass.style = CS_HREDRAW | CS_VREDRAW;
3504 wndclass.lpfnWndProc = ws2_test_WndProc;
3505 wndclass.cbClsExtra = 0;
3506 wndclass.cbWndExtra = 0;
3507 wndclass.hInstance = GetModuleHandle(NULL);
3508 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
3509 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
3510 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3511 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3512 wndclass.lpszClassName = szClassName;
3513 wndclass.lpszMenuName = NULL;
3514 RegisterClassEx(&wndclass);
3516 hWnd = CreateWindow(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, GetModuleHandle(NULL), NULL);
3519 ok(0, "failed to create window: %d\n", GetLastError());
3523 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3526 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3530 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3533 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3539 trace("Event test using events\n");
3541 hEvent = WSACreateEvent();
3542 if (hEvent == INVALID_HANDLE_VALUE)
3544 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3548 hEvent2 = WSACreateEvent();
3549 if (hEvent2 == INVALID_HANDLE_VALUE)
3551 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3555 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3558 ok(0, "WSAEventSelect failed, error %d\n", ret);
3562 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3565 ok(0, "WSAEventSelect failed, error %d\n", ret);
3570 server = socket(AF_INET, SOCK_STREAM, 0);
3571 if (server == INVALID_SOCKET)
3573 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3577 memset(&addr, 0, sizeof(addr));
3578 addr.sin_family = AF_INET;
3579 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
3580 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
3583 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3588 ret = getsockname(server, (struct sockaddr*)&addr, &len);
3591 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3595 ret = listen(server, 2);
3598 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3602 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
3603 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3605 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3609 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr));
3610 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3612 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3617 dst = accept(server, (struct sockaddr*)&addr, &len);
3618 if (dst == INVALID_SOCKET)
3620 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3625 dst2 = accept(server, (struct sockaddr*)&addr, &len);
3626 if (dst2 == INVALID_SOCKET)
3628 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3632 closesocket(server);
3633 server = INVALID_SOCKET;
3635 /* On Windows it seems when a non-blocking socket sends to a
3636 blocking socket on the same host, the send() is BLOCKING,
3637 so make both sockets non-blocking. src is already non-blocking
3638 from the async select */
3640 if (set_blocking(dst, FALSE))
3642 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3646 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
3649 ok(0, "could not allocate memory for test\n");
3653 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3654 if (ov.hEvent == NULL)
3656 ok(0, "could not create event object, errno = %d\n", GetLastError());
3660 ov2.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3661 if (ov2.hEvent == NULL)
3663 ok(0, "could not create event object, errno = %d\n", GetLastError());
3667 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3668 ok_event_seq(src, hEvent, connect_seq, NULL, 1);
3669 ok_event_seq(src2, hEvent2, connect_seq, NULL, 1);
3670 /* broken on all windows - FD_CONNECT error is garbage */
3672 /* Test simple send/recv */
3673 ret = send(dst, buffer, 100, 0);
3674 ok(ret == 100, "Failed to send buffer %d err %d\n", ret, GetLastError());
3675 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3677 ret = recv(src, buffer, 1, MSG_PEEK);
3678 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError());
3679 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3681 ret = recv(src, buffer, 50, 0);
3682 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3683 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3685 ret = recv(src, buffer, 50, 0);
3686 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3687 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3689 /* fun fact - events are reenabled even on failure, but only for messages */
3690 ret = send(dst, "1", 1, 0);
3691 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3692 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3694 ret = recv(src, buffer, -1, 0);
3695 ok(ret == SOCKET_ERROR && (GetLastError() == WSAEFAULT || GetLastError() == WSAENOBUFS),
3696 "Failed to recv buffer %d err %d\n", ret, GetLastError());
3699 broken_seq[0] = empty_seq; /* win9x */
3700 broken_seq[1] = NULL;
3701 todo_wine ok_event_seq(src, hEvent, read_seq, broken_seq, 0);
3704 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3706 ret = recv(src, buffer, 1, 0);
3707 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3708 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3710 /* Interaction with overlapped */
3711 bufs.len = sizeof(char);
3713 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3714 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3715 "WSARecv failed - %d error %d\n", ret, GetLastError());
3717 bufs.len = sizeof(char);
3718 bufs.buf = buffer+1;
3719 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL);
3720 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3721 "WSARecv failed - %d error %d\n", ret, GetLastError());
3723 ret = send(dst, "12", 2, 0);
3724 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3725 broken_seq[0] = read_read_seq; /* win9x */
3726 broken_seq[1] = NULL;
3727 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3729 dwRet = WaitForSingleObject(ov.hEvent, 100);
3730 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3731 if (dwRet == WAIT_OBJECT_0)
3733 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3734 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3735 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3736 ok(buffer[0] == '1', "Got %c instead of 1\n", buffer[0]);
3739 dwRet = WaitForSingleObject(ov2.hEvent, 100);
3740 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3741 if (dwRet == WAIT_OBJECT_0)
3743 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE);
3744 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3745 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3746 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[1]);
3749 ret = send(dst, "1", 1, 0);
3750 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3751 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3753 ret = recv(src, buffer, 1, 0);
3754 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3755 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3757 /* Notifications are delivered as soon as possible, blocked only on
3758 * async requests on the same type */
3759 bufs.len = sizeof(char);
3761 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3762 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3763 "WSARecv failed - %d error %d\n", ret, GetLastError());
3766 ret = send(dst, "1", 1, MSG_OOB);
3767 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3768 ok_event_seq(src, hEvent, oob_seq, NULL, 0);
3771 dwRet = WaitForSingleObject(ov.hEvent, 100);
3772 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError());
3774 ret = send(dst, "2", 1, 0);
3775 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3776 broken_seq[0] = read_seq; /* win98 */
3777 broken_seq[1] = NULL;
3778 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3780 dwRet = WaitForSingleObject(ov.hEvent, 100);
3781 ok(dwRet == WAIT_OBJECT_0 || broken(dwRet == WAIT_TIMEOUT),
3782 "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3783 if (dwRet == WAIT_OBJECT_0)
3785 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3786 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3787 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3788 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]);
3790 else if (dwRet == WAIT_TIMEOUT)
3792 /* this happens on win98. We get an FD_READ later on the next test */
3793 CancelIo((HANDLE) src);
3797 ret = recv(src, buffer, 1, MSG_OOB);
3798 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3799 /* We get OOB notification, but no data on wine */
3800 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3803 /* Flood the send queue */
3804 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3805 if (hThread == NULL)
3807 ok(0, "CreateThread failed, error %d\n", GetLastError());
3811 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3812 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3814 /* Now if we send a ton of data and the 'server' does not drain it fast
3815 * enough (set drain_pause to be sure), the socket send buffer will only
3816 * take some of it, and we will get a short write. This will trigger
3817 * another FD_WRITE event as soon as data is sent and more space becomes
3818 * available, but not any earlier. */
3822 ret = send(src, buffer, bufferSize, 0);
3823 } while (ret == bufferSize);
3825 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
3827 Sleep(400); /* win9x */
3828 broken_seq[0] = read_write_seq;
3829 broken_seq[1] = NULL;
3830 ok_event_seq(src, hEvent, write_seq, broken_seq, 0);
3834 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3837 /* Test how FD_CLOSE is handled */
3838 ret = send(dst, "12", 2, 0);
3839 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3841 /* Wait a little and let the send complete */
3844 dst = INVALID_SOCKET;
3847 /* We can never implement this in wine, best we can hope for is
3848 sending FD_CLOSE after the reads complete */
3849 broken_seq[0] = read_seq; /* win9x */
3850 broken_seq[1] = NULL;
3851 todo_wine ok_event_seq(src, hEvent, read_close_seq, broken_seq, 0);
3853 ret = recv(src, buffer, 1, 0);
3854 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3855 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3857 ret = recv(src, buffer, 1, 0);
3858 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3859 /* want it? it's here, but you can't have it */
3860 broken_seq[0] = close_seq; /* win9x */
3861 broken_seq[1] = NULL;
3862 todo_wine ok_event_seq(src, hEvent, empty_seq, /* wine sends FD_CLOSE here */
3865 /* Test how FD_CLOSE is handled */
3866 ret = send(dst2, "12", 2, 0);
3867 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3870 shutdown(dst2, SD_SEND);
3873 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3874 regressions, don't mark them as todo_wine, and mark windows as broken */
3875 broken_seq[0] = read_close_seq;
3876 broken_seq[1] = close_seq;
3877 broken_seq[2] = NULL;
3878 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3880 ret = recv(src2, buffer, 1, 0);
3881 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3882 broken_seq[0] = close_seq; /* win98 */
3883 broken_seq[1] = NULL;
3884 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3886 ret = recv(src2, buffer, 1, 0);
3887 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3888 broken_seq[0] = empty_seq;
3889 broken_seq[1] = NULL;
3890 ok_event_seq(src2, hEvent2, close_seq, broken_seq, 0);
3892 ret = send(src2, "1", 1, 0);
3893 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3894 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3896 ret = send(src2, "1", 1, 0);
3897 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3898 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3901 if (src != INVALID_SOCKET)
3903 flush_events(src, hEvent);
3906 if (src2 != INVALID_SOCKET)
3908 flush_events(src2, hEvent2);
3911 HeapFree(GetProcessHeap(), 0, buffer);
3912 if (server != INVALID_SOCKET)
3913 closesocket(server);
3914 if (dst != INVALID_SOCKET)
3916 if (dst2 != INVALID_SOCKET)
3918 if (hThread != NULL)
3919 CloseHandle(hThread);
3921 DestroyWindow(hWnd);
3923 CloseHandle(hEvent);
3924 if (hEvent2 != NULL)
3925 CloseHandle(hEvent2);
3926 if (ov.hEvent != NULL)
3927 CloseHandle(ov.hEvent);
3928 if (ov2.hEvent != NULL)
3929 CloseHandle(ov2.hEvent);
3932 static void test_ipv6only(void)
3934 SOCKET v4 = INVALID_SOCKET,
3935 v6 = INVALID_SOCKET;
3936 struct sockaddr_in sin4;
3937 struct sockaddr_in6 sin6;
3940 memset(&sin4, 0, sizeof(sin4));
3941 sin4.sin_family = AF_INET;
3942 sin4.sin_port = htons(SERVERPORT);
3944 memset(&sin6, 0, sizeof(sin6));
3945 sin6.sin6_family = AF_INET6;
3946 sin6.sin6_port = htons(SERVERPORT);
3948 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
3949 if (v6 == INVALID_SOCKET) {
3950 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3951 WSAGetLastError(), WSAEAFNOSUPPORT);
3954 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
3956 skip("Could not bind IPv6 address (LastError: %d).\n",
3961 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3962 if (v4 == INVALID_SOCKET) {
3963 skip("Could not create IPv4 socket (LastError: %d).\n",
3967 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
3968 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
3969 WSAGetLastError(), WSAEADDRINUSE);
3972 if (v4 != INVALID_SOCKET)
3974 if (v6 != INVALID_SOCKET)
3978 static void test_WSASendTo(void)
3981 struct sockaddr_in addr;
3982 char buf[12] = "hello world";
3987 addr.sin_family = AF_INET;
3988 addr.sin_port = htons(139);
3989 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
3990 data_buf.len = sizeof(buf);
3993 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
3994 ok(0, "socket() failed error: %d\n", WSAGetLastError());
3998 WSASetLastError(12345);
3999 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4000 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
4001 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4003 WSASetLastError(12345);
4004 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4005 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4006 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4008 WSASetLastError(12345);
4009 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
4010 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
4013 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
4014 "a successful call to WSASendTo()\n");
4017 static void test_WSARecv(void)
4023 DWORD bytesReturned;
4030 memset(&ov, 0, sizeof(ov));
4032 tcp_socketpair(&src, &dest);
4033 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
4035 skip("failed to create sockets\n");
4039 bufs.len = sizeof(buf);
4043 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4044 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
4050 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
4051 ok(!iret, "Failed to set linger %d\n", GetLastError());
4053 iret = WSARecv(dest, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
4054 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
4057 src = INVALID_SOCKET;
4059 dwret = WaitForSingleObject(ov.hEvent, 1000);
4060 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
4062 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
4063 todo_wine ok(!bret && (GetLastError() == ERROR_NETNAME_DELETED || broken(GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */),
4064 "Did not get disconnect event: %d, error %d\n", bret, GetLastError());
4065 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
4068 if (dest != INVALID_SOCKET)
4070 if (src != INVALID_SOCKET)
4073 WSACloseEvent(ov.hEvent);
4076 static void test_GetAddrInfoW(void)
4078 static const WCHAR port[] = {'8','0',0};
4079 static const WCHAR empty[] = {0};
4080 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
4083 ADDRINFOW *result, hint;
4085 if (!pGetAddrInfoW || !pFreeAddrInfoW)
4087 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
4091 memset(&hint, 0, sizeof(ADDRINFOW));
4093 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
4094 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
4097 ret = pGetAddrInfoW(empty, NULL, NULL, &result);
4100 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4101 ok(result != NULL, "GetAddrInfoW failed\n");
4103 pFreeAddrInfoW(result);
4105 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
4106 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4107 pFreeAddrInfoW(result);
4109 ret = pGetAddrInfoW(localhost, port, NULL, &result);
4110 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4111 pFreeAddrInfoW(result);
4113 ret = pGetAddrInfoW(localhost, port, &hint, &result);
4114 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4115 pFreeAddrInfoW(result);
4118 static void test_ConnectEx(void)
4120 SOCKET listener = INVALID_SOCKET;
4121 SOCKET acceptor = INVALID_SOCKET;
4122 SOCKET connector = INVALID_SOCKET;
4123 struct sockaddr_in address, conaddress;
4125 OVERLAPPED overlapped;
4126 LPFN_CONNECTEX pConnectEx;
4127 GUID connectExGuid = WSAID_CONNECTEX;
4128 DWORD bytesReturned;
4134 memset(&overlapped, 0, sizeof(overlapped));
4136 listener = socket(AF_INET, SOCK_STREAM, 0);
4137 if (listener == INVALID_SOCKET) {
4138 skip("could not create listener socket, error %d\n", WSAGetLastError());
4142 connector = socket(AF_INET, SOCK_STREAM, 0);
4143 if (connector == INVALID_SOCKET) {
4144 skip("could not create connector socket, error %d\n", WSAGetLastError());
4148 memset(&address, 0, sizeof(address));
4149 address.sin_family = AF_INET;
4150 address.sin_addr.s_addr = inet_addr("127.0.0.1");
4151 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
4153 skip("failed to bind, error %d\n", WSAGetLastError());
4157 addrlen = sizeof(address);
4158 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
4160 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4164 if (set_blocking(listener, TRUE)) {
4165 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4169 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
4170 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
4172 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret, WSAGetLastError());
4176 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4177 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
4178 "returned %d + errno %d\n", bret, WSAGetLastError());
4180 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4181 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
4182 "returned %d + errno %d\n", bret, WSAGetLastError());
4183 if (bret == TRUE || WSAGetLastError() != WSAEINVAL)
4185 acceptor = accept(listener, NULL, NULL);
4186 if (acceptor != INVALID_SOCKET) {
4187 closesocket(acceptor);
4188 acceptor = INVALID_SOCKET;
4191 closesocket(connector);
4192 connector = socket(AF_INET, SOCK_STREAM, 0);
4193 if (connector == INVALID_SOCKET) {
4194 skip("could not create connector socket, error %d\n", WSAGetLastError());
4199 /* ConnectEx needs a bound socket */
4200 memset(&conaddress, 0, sizeof(conaddress));
4201 conaddress.sin_family = AF_INET;
4202 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4203 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4205 skip("failed to bind, error %d\n", WSAGetLastError());
4209 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
4210 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
4211 "returned %d + errno %d\n", bret, WSAGetLastError());
4213 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4214 if (overlapped.hEvent == NULL) {
4215 skip("could not create event object, errno = %d\n", GetLastError());
4219 iret = listen(listener, 1);
4221 skip("listening failed, errno = %d\n", WSAGetLastError());
4225 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4226 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4227 "returned %d + errno %d\n", bret, WSAGetLastError());
4228 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4229 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4231 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4232 ok(bret, "Connecting failed, error %d\n", GetLastError());
4233 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
4235 closesocket(connector);
4236 connector = socket(AF_INET, SOCK_STREAM, 0);
4237 if (connector == INVALID_SOCKET) {
4238 skip("could not create connector socket, error %d\n", WSAGetLastError());
4241 /* ConnectEx needs a bound socket */
4242 memset(&conaddress, 0, sizeof(conaddress));
4243 conaddress.sin_family = AF_INET;
4244 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4245 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4247 skip("failed to bind, error %d\n", WSAGetLastError());
4251 acceptor = accept(listener, NULL, NULL);
4252 if (acceptor != INVALID_SOCKET) {
4253 closesocket(acceptor);
4259 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
4260 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4261 "returned %d + errno %d\n", bret, WSAGetLastError());
4262 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4263 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4265 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4266 ok(bret, "Connecting failed, error %d\n", GetLastError());
4267 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
4269 acceptor = accept(listener, NULL, NULL);
4270 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
4272 bytesReturned = recv(acceptor, buffer, 3, 0);
4274 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
4275 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
4276 "Failed to get the right data, expected '123', got '%s'\n", buffer);
4278 closesocket(connector);
4279 connector = socket(AF_INET, SOCK_STREAM, 0);
4280 if (connector == INVALID_SOCKET) {
4281 skip("could not create connector socket, error %d\n", WSAGetLastError());
4284 /* ConnectEx needs a bound socket */
4285 memset(&conaddress, 0, sizeof(conaddress));
4286 conaddress.sin_family = AF_INET;
4287 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4288 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4290 skip("failed to bind, error %d\n", WSAGetLastError());
4294 if (acceptor != INVALID_SOCKET) {
4295 closesocket(acceptor);
4296 acceptor = INVALID_SOCKET;
4299 /* Connect with error */
4300 closesocket(listener);
4301 listener = INVALID_SOCKET;
4303 address.sin_port = htons(1);
4305 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4306 ok(bret == FALSE && GetLastError(), "ConnectEx to bad destination failed: "
4307 "returned %d + errno %d\n", bret, GetLastError());
4309 if (GetLastError() == ERROR_IO_PENDING)
4311 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4312 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4314 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4315 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
4316 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4319 ok(GetLastError() == WSAECONNREFUSED,
4320 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4324 if (overlapped.hEvent)
4325 WSACloseEvent(overlapped.hEvent);
4326 if (listener != INVALID_SOCKET)
4327 closesocket(listener);
4328 if (acceptor != INVALID_SOCKET)
4329 closesocket(acceptor);
4330 if (connector != INVALID_SOCKET)
4331 closesocket(connector);
4334 static void test_AcceptEx(void)
4336 SOCKET listener = INVALID_SOCKET;
4337 SOCKET acceptor = INVALID_SOCKET;
4338 SOCKET connector = INVALID_SOCKET;
4339 SOCKET connector2 = INVALID_SOCKET;
4340 struct sockaddr_in bindAddress;
4342 GUID acceptExGuid = WSAID_ACCEPTEX;
4343 LPFN_ACCEPTEX pAcceptEx = NULL;
4344 fd_set fds_accept, fds_send;
4345 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
4347 DWORD bytesReturned;
4349 OVERLAPPED overlapped;
4354 memset(&overlapped, 0, sizeof(overlapped));
4356 listener = socket(AF_INET, SOCK_STREAM, 0);
4357 if (listener == INVALID_SOCKET) {
4358 skip("could not create listener socket, error %d\n", WSAGetLastError());
4362 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4363 if (acceptor == INVALID_SOCKET) {
4364 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4368 connector = socket(AF_INET, SOCK_STREAM, 0);
4369 if (connector == INVALID_SOCKET) {
4370 skip("could not create connector socket, error %d\n", WSAGetLastError());
4374 memset(&bindAddress, 0, sizeof(bindAddress));
4375 bindAddress.sin_family = AF_INET;
4376 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4377 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4379 skip("failed to bind, error %d\n", WSAGetLastError());
4383 socklen = sizeof(bindAddress);
4384 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
4386 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4390 if (set_blocking(listener, FALSE)) {
4391 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4395 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
4396 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
4398 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
4402 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4403 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4404 &bytesReturned, &overlapped);
4405 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
4406 "returned %d + errno %d\n", bret, WSAGetLastError());
4408 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4409 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4410 &bytesReturned, &overlapped);
4411 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
4412 "returned %d + errno %d\n", bret, WSAGetLastError());
4414 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4415 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4416 &bytesReturned, &overlapped);
4418 (WSAGetLastError() == WSAEINVAL ||
4419 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
4420 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
4422 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
4423 &bytesReturned, &overlapped);
4424 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
4425 "returned %d + errno %d\n", bret, WSAGetLastError());
4427 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
4428 &bytesReturned, &overlapped);
4429 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
4430 "returned %d + errno %d\n", bret, WSAGetLastError());
4432 bret = pAcceptEx(listener, acceptor, buffer, 0,
4433 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4434 &bytesReturned, NULL);
4435 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
4436 "returned %d + errno %d\n", bret, WSAGetLastError());
4438 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4439 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4440 &bytesReturned, &overlapped);
4441 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
4442 "returned %d + errno %d\n", bret, WSAGetLastError());
4444 iret = listen(listener, 5);
4446 skip("listening failed, errno = %d\n", WSAGetLastError());
4450 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4451 if (overlapped.hEvent == NULL) {
4452 skip("could not create event object, errno = %d\n", GetLastError());
4456 bret = pAcceptEx(listener, acceptor, buffer, 0,
4457 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4458 &bytesReturned, &overlapped);
4459 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4461 bret = pAcceptEx(listener, acceptor, buffer, 0,
4462 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4463 &bytesReturned, &overlapped);
4464 todo_wine ok((bret == FALSE && WSAGetLastError() == WSAEINVAL) || broken(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) /* NT4 */,
4465 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
4466 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
4467 /* We need to cancel this call, otherwise things fail */
4468 bret = CancelIo((HANDLE) listener);
4469 ok(bret, "Failed to cancel failed test. Bailing...\n");
4471 WaitForSingleObject(overlapped.hEvent, 0);
4473 bret = pAcceptEx(listener, acceptor, buffer, 0,
4474 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4475 &bytesReturned, &overlapped);
4476 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4479 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4480 todo_wine ok((iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL) || broken(!iret) /* NT4 */,
4481 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
4482 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
4483 /* We need to cancel this call, otherwise things fail */
4484 closesocket(acceptor);
4485 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4486 if (acceptor == INVALID_SOCKET) {
4487 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4491 bret = CancelIo((HANDLE) listener);
4492 ok(bret, "Failed to cancel failed test. Bailing...\n");
4495 bret = pAcceptEx(listener, acceptor, buffer, 0,
4496 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4497 &bytesReturned, &overlapped);
4498 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4501 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4502 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4504 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
4505 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4507 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4508 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4509 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4511 closesocket(connector);
4512 connector = INVALID_SOCKET;
4513 closesocket(acceptor);
4515 /* Test short reads */
4517 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4518 if (acceptor == INVALID_SOCKET) {
4519 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4522 connector = socket(AF_INET, SOCK_STREAM, 0);
4523 if (connector == INVALID_SOCKET) {
4524 skip("could not create connector socket, error %d\n", WSAGetLastError());
4527 bret = pAcceptEx(listener, acceptor, buffer, 2,
4528 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4529 &bytesReturned, &overlapped);
4530 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4532 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4533 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4535 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4536 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
4538 iret = send(connector, buffer, 1, 0);
4539 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
4541 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4542 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4544 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4545 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4546 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4548 closesocket(connector);
4549 connector = INVALID_SOCKET;
4550 closesocket(acceptor);
4552 /* Test CF_DEFER & AcceptEx interaction */
4554 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4555 if (acceptor == INVALID_SOCKET) {
4556 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4559 connector = socket(AF_INET, SOCK_STREAM, 0);
4560 if (connector == INVALID_SOCKET) {
4561 skip("could not create connector socket, error %d\n", WSAGetLastError());
4564 connector2 = socket(AF_INET, SOCK_STREAM, 0);
4565 if (connector == INVALID_SOCKET) {
4566 skip("could not create connector socket, error %d\n", WSAGetLastError());
4570 if (set_blocking(connector, FALSE)) {
4571 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4575 if (set_blocking(connector2, FALSE)) {
4576 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4580 /* Connect socket #1 */
4581 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4582 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4584 FD_ZERO ( &fds_accept );
4585 FD_ZERO ( &fds_send );
4587 FD_SET ( listener, &fds_accept );
4588 FD_SET ( connector, &fds_send );
4594 for (i = 0; i < 4000; ++i)
4596 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
4598 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
4599 "acceptex test(%d): could not select on socket, errno %d\n" );
4601 /* check for incoming requests */
4602 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
4605 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
4606 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
4607 bret = pAcceptEx(listener, acceptor, buffer, 0,
4608 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4609 &bytesReturned, &overlapped);
4610 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4612 else if (got == 2) {
4613 /* this should be socket #2 */
4614 SOCKET tmp = accept(listener, NULL, NULL);
4615 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
4619 ok(FALSE, "Got more than 2 connections?\n");
4622 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
4623 /* Send data on second socket, and stop */
4624 send(connector2, "2", 1, 0);
4625 FD_CLR ( connector2, &fds_send );
4629 if ( FD_ISSET ( connector, &fds_opensend ) ) {
4630 /* Once #1 is connected, allow #2 to connect */
4633 send(connector, "1", 1, 0);
4634 FD_CLR ( connector, &fds_send );
4636 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4637 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4638 FD_SET ( connector2, &fds_send );
4642 ok (got == 2 || broken(got == 1) /* NT4 */,
4643 "Did not get both connections, got %d\n", got);
4645 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4646 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4648 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4649 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4650 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4652 set_blocking(acceptor, TRUE);
4653 iret = recv( acceptor, buffer, 2, 0);
4654 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
4656 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
4658 closesocket(connector);
4659 connector = INVALID_SOCKET;
4660 closesocket(acceptor);
4662 /* clean up in case of failures */
4663 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
4664 closesocket(acceptor);
4666 /* Disconnect during receive? */
4668 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4669 if (acceptor == INVALID_SOCKET) {
4670 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4673 connector = socket(AF_INET, SOCK_STREAM, 0);
4674 if (connector == INVALID_SOCKET) {
4675 skip("could not create connector socket, error %d\n", WSAGetLastError());
4678 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4679 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4680 &bytesReturned, &overlapped);
4681 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4683 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4684 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4686 closesocket(connector);
4687 connector = INVALID_SOCKET;
4689 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4690 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4692 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4693 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4694 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4696 closesocket(acceptor);
4698 /* Test closing with pending requests */
4700 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4701 if (acceptor == INVALID_SOCKET) {
4702 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4705 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4706 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4707 &bytesReturned, &overlapped);
4708 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4710 closesocket(acceptor);
4712 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4713 todo_wine ok(dwret == WAIT_OBJECT_0 || broken(dwret == WAIT_TIMEOUT) /* NT4/2000 */,
4714 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4716 if (dwret != WAIT_TIMEOUT) {
4717 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4718 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
4721 bret = CancelIo((HANDLE) listener);
4722 ok(bret, "Failed to cancel failed test. Bailing...\n");
4724 WaitForSingleObject(overlapped.hEvent, 0);
4727 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4728 if (acceptor == INVALID_SOCKET) {
4729 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4732 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4733 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4734 &bytesReturned, &overlapped);
4735 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4737 CancelIo((HANDLE) acceptor);
4739 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4740 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
4742 closesocket(acceptor);
4744 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4745 if (acceptor == INVALID_SOCKET) {
4746 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4749 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4750 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4751 &bytesReturned, &overlapped);
4752 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4754 closesocket(listener);
4755 listener = INVALID_SOCKET;
4757 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4758 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4760 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4761 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
4764 if (overlapped.hEvent)
4765 WSACloseEvent(overlapped.hEvent);
4766 if (listener != INVALID_SOCKET)
4767 closesocket(listener);
4768 if (acceptor != INVALID_SOCKET)
4769 closesocket(acceptor);
4770 if (connector != INVALID_SOCKET)
4771 closesocket(connector);
4772 if (connector2 != INVALID_SOCKET)
4773 closesocket(connector2);
4776 static void test_getpeername(void)
4779 struct sockaddr_in sa, sa_out;
4781 const char buf[] = "hello world";
4784 /* Test the parameter validation order. */
4785 ret = getpeername(INVALID_SOCKET, NULL, NULL);
4786 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4787 ok(WSAGetLastError() == WSAENOTSOCK,
4788 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
4790 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
4791 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
4792 if (sock == INVALID_SOCKET)
4794 skip("Socket creation failed with %d\n", WSAGetLastError());
4798 ret = getpeername(sock, NULL, NULL);
4799 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4800 ok(WSAGetLastError() == WSAENOTCONN ||
4801 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
4802 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4804 memset(&sa, 0, sizeof(sa));
4805 sa.sin_family = AF_INET;
4806 sa.sin_port = htons(139);
4807 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
4809 /* sendto does not change a socket's connection state. */
4810 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
4811 ok(ret != SOCKET_ERROR,
4812 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4814 ret = getpeername(sock, NULL, NULL);
4815 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4816 ok(WSAGetLastError() == WSAENOTCONN ||
4817 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
4818 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4820 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
4822 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4824 ret = getpeername(sock, NULL, NULL);
4825 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4826 ok(WSAGetLastError() == WSAEFAULT,
4827 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4829 /* Test crashes on Wine. */
4832 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
4833 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4834 ok(WSAGetLastError() == WSAEFAULT,
4835 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4839 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
4840 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
4841 ok(WSAGetLastError() == WSAEFAULT,
4842 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4844 sa_len = sizeof(sa_out);
4845 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
4846 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
4847 ok(!memcmp(&sa, &sa_out, sizeof(sa)),
4848 "Expected the returned structure to be identical to the connect structure\n");
4853 static void test_sioRoutingInterfaceQuery(void)
4857 SOCKADDR_IN sin = { 0 }, sout = { 0 };
4858 DWORD bytesReturned;
4860 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
4861 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
4862 if (sock == INVALID_SOCKET)
4864 skip("Socket creation failed with %d\n", WSAGetLastError());
4867 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
4869 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4870 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4871 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4872 NULL, 0, NULL, NULL, NULL);
4873 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4874 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4875 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4876 NULL, 0, &bytesReturned, NULL, NULL);
4877 ok(ret == SOCKET_ERROR &&
4878 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
4879 WSAGetLastError() == WSAEINVAL /* NT4 */||
4880 WSAGetLastError() == WSAEAFNOSUPPORT),
4881 "expected WSAEFAULT or WSAEINVAL or WSAEAFNOSUPPORT, got %d\n",
4883 sin.sin_family = AF_INET;
4884 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4885 NULL, 0, &bytesReturned, NULL, NULL);
4886 ok(ret == SOCKET_ERROR &&
4887 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
4888 WSAGetLastError() == WSAEINVAL),
4889 "expected WSAEFAULT or WSAEINVAL, got %d\n", WSAGetLastError());
4890 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4891 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4892 NULL, 0, &bytesReturned, NULL, NULL);
4893 ok(ret == SOCKET_ERROR &&
4894 (WSAGetLastError() == WSAEINVAL /* NT4 */ ||
4895 WSAGetLastError() == WSAEFAULT),
4896 "expected WSAEINVAL or WSAEFAULT, got %d\n", WSAGetLastError());
4897 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4898 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
4899 ok(!ret || broken(WSAGetLastError() == WSAEINVAL /* NT4 */),
4900 "WSAIoctl failed: %d\n", WSAGetLastError());
4903 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n",
4905 /* We expect the source address to be INADDR_LOOPBACK as well, but
4906 * there's no guarantee that a route to the loopback address exists,
4907 * so rather than introduce spurious test failures we do not test the
4914 static void test_synchronous_WSAIoctl(void)
4916 HANDLE previous_port, io_port;
4917 WSAOVERLAPPED overlapped, *olp;
4925 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
4926 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
4928 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
4929 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
4931 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
4932 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
4935 memset( &overlapped, 0, sizeof(overlapped) );
4936 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
4937 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
4939 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
4940 ok( ret, "failed to get completion status %u\n", GetLastError() );
4942 CloseHandle( io_port );
4943 closesocket( socket );
4944 CloseHandle( previous_port );
4947 #define WM_ASYNCCOMPLETE (WM_USER + 100)
4948 static HWND create_async_message_window(void)
4950 static const char class_name[] = "ws2_32 async message window class";
4952 WNDCLASSEXA wndclass;
4955 wndclass.cbSize = sizeof(wndclass);
4956 wndclass.style = CS_HREDRAW | CS_VREDRAW;
4957 wndclass.lpfnWndProc = DefWindowProcA;
4958 wndclass.cbClsExtra = 0;
4959 wndclass.cbWndExtra = 0;
4960 wndclass.hInstance = GetModuleHandleA(NULL);
4961 wndclass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
4962 wndclass.hIconSm = LoadIconA(NULL, IDI_APPLICATION);
4963 wndclass.hCursor = LoadCursorA(NULL, IDC_ARROW);
4964 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
4965 wndclass.lpszClassName = class_name;
4966 wndclass.lpszMenuName = NULL;
4968 RegisterClassExA(&wndclass);
4970 hWnd = CreateWindow(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW,
4971 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
4974 ok(0, "failed to create window: %u\n", GetLastError());
4981 static void test_WSAAsyncGetServByPort(void)
4983 HWND hwnd = create_async_message_window();
4985 char buffer[MAXGETHOSTSTRUCT];
4990 /* FIXME: The asynchronous window messages should be tested. */
4992 /* Parameters are not checked when initiating the asynchronous operation. */
4993 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0);
4994 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
4996 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0);
4997 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
4999 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0);
5000 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5002 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT);
5003 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5005 DestroyWindow(hwnd);
5008 /**************** Main program ***************/
5014 /* Leave these tests at the beginning. They depend on WSAStartup not having been
5015 * called, which is done by Init() below. */
5016 test_WithoutWSAStartup();
5017 test_WithWSAStartup();
5021 test_set_getsockopt();
5022 test_so_reuseaddr();
5024 test_extendedSocketOptions();
5026 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
5028 trace ( " **** STARTING TEST %d ****\n", i );
5029 do_test ( &tests[i] );
5030 trace ( " **** TEST %d COMPLETE ****\n", i );
5035 test_getservbyname();
5038 test_WSAAddressToStringA();
5039 test_WSAAddressToStringW();
5041 test_WSAStringToAddressA();
5042 test_WSAStringToAddressW();
5050 test_addr_to_print();
5053 test_gethostbyname_hack();
5062 test_GetAddrInfoW();
5067 test_sioRoutingInterfaceQuery();
5069 test_WSAAsyncGetServByPort();
5071 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */
5073 test_synchronous_WSAIoctl();