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 SetLastError(0xdeadbeef);
2634 optlen = sizeof(LINGER);
2635 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2636 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT),
2637 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
2638 ret, WSAGetLastError());
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 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
2969 todo_wine ok(arg, "SIOCATMARK expected a non-zero value\n");
2971 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
2973 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
2974 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
2976 ret = ioctlsocket(sock, SIOCATMARK, &arg);
2977 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
2978 ok(arg, "SIOCATMARK expected a non-zero value\n");
2980 /* disable SO_OOBINLINE and get the same old bahavior */
2982 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
2983 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
2985 ret = ioctlsocket(sock, SIOCATMARK, &arg);
2986 todo_wine ok(arg, "SIOCATMARK expected a non-zero value\n");
2988 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
2989 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
2990 ret = WSAGetLastError();
2991 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
2993 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
2994 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
2995 ret = WSAGetLastError();
2996 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
2998 /* broken used to catch W95, W98, NT4 */
2999 make_keepalive(kalive, 0, 0, 0);
3000 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3001 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3003 make_keepalive(kalive, 1, 0, 0);
3004 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3005 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3007 make_keepalive(kalive, 1, 1000, 1000);
3008 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3009 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3011 make_keepalive(kalive, 1, 10000, 10000);
3012 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3013 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3015 make_keepalive(kalive, 1, 100, 100);
3016 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3017 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3019 make_keepalive(kalive, 0, 100, 100);
3020 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3021 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3026 static int drain_pause=0;
3027 static DWORD WINAPI drain_socket_thread(LPVOID arg)
3030 SOCKET sock = *(SOCKET*)arg;
3033 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
3037 if (WSAGetLastError() == WSAEWOULDBLOCK)
3041 FD_SET(sock, &readset);
3042 select(sock+1, &readset, NULL, NULL, NULL);
3053 static void test_send(void)
3055 SOCKET src = INVALID_SOCKET;
3056 SOCKET dst = INVALID_SOCKET;
3057 HANDLE hThread = NULL;
3058 const int buflen = 1024*1024;
3059 char *buffer = NULL;
3060 int ret, i, zero = 0;
3064 DWORD id, bytes_sent, dwRet;
3066 memset(&ov, 0, sizeof(ov));
3068 if (tcp_socketpair(&src, &dst) != 0)
3070 ok(0, "creating socket pair failed, skipping test\n");
3074 set_blocking(dst, FALSE);
3075 /* force disable buffering so we can get a pending overlapped request */
3076 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
3077 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
3079 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3080 if (hThread == NULL)
3082 ok(0, "CreateThread failed, error %d\n", GetLastError());
3086 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
3089 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
3093 /* fill the buffer with some nonsense */
3094 for (i = 0; i < buflen; ++i)
3096 buffer[i] = (char) i;
3099 ret = send(src, buffer, buflen, 0);
3101 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
3103 ok(0, "send failed, error %d\n", WSAGetLastError());
3108 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3109 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
3114 WSASetLastError(12345);
3115 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
3116 ok((ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING) || broken(bytes_sent == buflen),
3117 "Failed to start overlapped send %d - %d - %d/%d\n", ret, WSAGetLastError(), bytes_sent, buflen);
3119 /* don't check for completion yet, we may need to drain the buffer while still sending */
3120 set_blocking(src, FALSE);
3121 for (i = 0; i < buflen; ++i)
3125 ret = recv(src, buffer, 1, 0);
3126 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
3130 ret = recv(src, buffer, 1, 0);
3133 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
3137 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
3140 dwRet = WaitForSingleObject(ov.hEvent, 1000);
3141 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3142 if (dwRet == WAIT_OBJECT_0)
3144 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
3145 ok((bret && bytes_sent == buflen) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3146 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
3149 WSASetLastError(12345);
3150 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
3151 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
3152 "WSASend failed %d - %d\n", ret, WSAGetLastError());
3154 WSASetLastError(12345);
3155 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
3156 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
3157 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
3160 if (src != INVALID_SOCKET)
3162 if (dst != INVALID_SOCKET)
3164 if (hThread != NULL)
3165 CloseHandle(hThread);
3167 CloseHandle(ov.hEvent);
3168 HeapFree(GetProcessHeap(), 0, buffer);
3171 typedef struct async_message
3175 struct async_message *next;
3178 static struct async_message *messages_received;
3180 #define WM_SOCKET (WM_USER+100)
3181 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3183 struct async_message *message;
3188 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message));
3189 message->socket = (SOCKET) wparam;
3190 message->lparam = lparam;
3191 message->next = NULL;
3193 if (messages_received)
3195 struct async_message *last = messages_received;
3196 while (last->next) last = last->next;
3197 last->next = message;
3200 messages_received = message;
3204 return DefWindowProc(hwnd, msg, wparam, lparam);
3207 static void get_event_details(int event, int *bit, char *name)
3212 if (bit) *bit = FD_ACCEPT_BIT;
3213 if (name) strcpy(name, "FD_ACCEPT");
3216 if (bit) *bit = FD_CONNECT_BIT;
3217 if (name) strcpy(name, "FD_CONNECT");
3220 if (bit) *bit = FD_READ_BIT;
3221 if (name) strcpy(name, "FD_READ");
3224 if (bit) *bit = FD_OOB_BIT;
3225 if (name) strcpy(name, "FD_OOB");
3228 if (bit) *bit = FD_WRITE_BIT;
3229 if (name) strcpy(name, "FD_WRITE");
3232 if (bit) *bit = FD_CLOSE_BIT;
3233 if (name) strcpy(name, "FD_CLOSE");
3237 if (name) sprintf(name, "bad%x", event);
3241 static const char *dbgstr_event_seq(const LPARAM *seq)
3243 static char message[1024];
3251 get_event_details(WSAGETSELECTEVENT(*seq), NULL, name);
3252 len += sprintf(message + len, "%s(%d) ", name, WSAGETSELECTERROR(*seq));
3256 strcpy( message + len, "]" );
3260 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents)
3262 static char message[1024];
3263 struct async_message *curr = messages_received;
3264 int index, error, bit = 0;
3274 if (bit >= FD_MAX_EVENTS) break;
3275 if ( !(netEvents->lNetworkEvents & (1 << bit)) )
3280 get_event_details(1 << bit, &index, name);
3281 error = netEvents->iErrorCode[index];
3287 if (curr->socket != s)
3292 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name);
3293 error = WSAGETSELECTERROR(curr->lparam);
3297 len += sprintf(message + len, "%s(%d) ", name, error);
3300 strcpy( message + len, "]" );
3304 static void flush_events(SOCKET s, HANDLE hEvent)
3306 WSANETWORKEVENTS netEvents;
3307 struct async_message *prev = NULL, *curr = messages_received;
3311 if (hEvent != INVALID_HANDLE_VALUE)
3313 dwRet = WaitForSingleObject(hEvent, 100);
3314 if (dwRet == WAIT_OBJECT_0)
3316 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents);
3318 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3325 if (curr->socket == s)
3327 if (prev) prev->next = curr->next;
3328 else messages_received = curr->next;
3330 HeapFree(GetProcessHeap(), 0, curr);
3332 if (prev) curr = prev->next;
3333 else curr = messages_received;
3344 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq)
3346 int event, index, error, events;
3347 struct async_message *curr;
3351 events = netEvents->lNetworkEvents;
3354 event = WSAGETSELECTEVENT(*seq);
3355 error = WSAGETSELECTERROR(*seq);
3356 get_event_details(event, &index, NULL);
3358 if (!(events & event) && index != -1)
3360 if (events & event && index != -1)
3362 if (netEvents->iErrorCode[index] != error)
3373 curr = messages_received;
3376 if (curr->socket == s)
3378 if (!*seq) return 0;
3379 if (*seq != curr->lparam) return 0;
3390 /* checks for a sequence of events, (order only checked if window is used) */
3391 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq, const LPARAM **broken_seqs, int completelyBroken)
3394 WSANETWORKEVENTS events, *netEvents = NULL;
3398 if (hEvent != INVALID_HANDLE_VALUE)
3400 netEvents = &events;
3402 dwRet = WaitForSingleObject(hEvent, 200);
3403 if (dwRet == WAIT_OBJECT_0)
3405 ret = WSAEnumNetworkEvents(s, hEvent, netEvents);
3408 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3413 memset(netEvents, 0, sizeof(*netEvents));
3418 /* Run the message loop a little */
3419 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ))
3421 DispatchMessageA(&msg);
3425 if (match_event_sequence(s, netEvents, seq))
3427 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq));
3428 flush_events(s, hEvent);
3434 for (; *broken_seqs; broken_seqs++)
3436 if (match_event_sequence(s, netEvents, *broken_seqs))
3438 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s, netEvents), dbgstr_event_seq(seq));
3439 flush_events(s, hEvent);
3445 winetest_ok(broken(completelyBroken), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq),
3446 dbgstr_event_seq_result(s, netEvents));
3447 flush_events(s, hEvent);
3450 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
3452 static void test_events(int useMessages)
3454 SOCKET server = INVALID_SOCKET;
3455 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET;
3456 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET;
3457 struct sockaddr_in addr;
3458 HANDLE hThread = NULL;
3459 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE;
3460 WNDCLASSEX wndclass;
3462 char *buffer = NULL;
3463 int bufferSize = 1024*1024;
3467 DWORD bytesReturned;
3473 static char szClassName[] = "wstestclass";
3474 const LPARAM *broken_seq[3];
3475 static const LPARAM empty_seq[] = { 0 };
3476 static const LPARAM close_seq[] = { WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3477 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3478 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3479 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 };
3480 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0),
3481 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3482 static const LPARAM read_read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3483 WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3484 static const LPARAM read_write_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3485 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3486 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3487 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3489 memset(&ov, 0, sizeof(ov));
3490 memset(&ov2, 0, sizeof(ov2));
3492 /* don't use socketpair, we want connection event */
3493 src = socket(AF_INET, SOCK_STREAM, 0);
3494 if (src == INVALID_SOCKET)
3496 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3500 src2 = socket(AF_INET, SOCK_STREAM, 0);
3501 if (src2 == INVALID_SOCKET)
3503 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3508 if (getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len) == SOCKET_ERROR)
3510 ok(0, "failed to get oobinline status, %d\n", GetLastError());
3513 ok(bret == FALSE, "OOB not inline\n");
3517 trace("Event test using messages\n");
3519 wndclass.cbSize = sizeof(wndclass);
3520 wndclass.style = CS_HREDRAW | CS_VREDRAW;
3521 wndclass.lpfnWndProc = ws2_test_WndProc;
3522 wndclass.cbClsExtra = 0;
3523 wndclass.cbWndExtra = 0;
3524 wndclass.hInstance = GetModuleHandle(NULL);
3525 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
3526 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
3527 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3528 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3529 wndclass.lpszClassName = szClassName;
3530 wndclass.lpszMenuName = NULL;
3531 RegisterClassEx(&wndclass);
3533 hWnd = CreateWindow(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, GetModuleHandle(NULL), NULL);
3536 ok(0, "failed to create window: %d\n", GetLastError());
3540 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3543 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3547 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3550 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3556 trace("Event test using events\n");
3558 hEvent = WSACreateEvent();
3559 if (hEvent == INVALID_HANDLE_VALUE)
3561 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3565 hEvent2 = WSACreateEvent();
3566 if (hEvent2 == INVALID_HANDLE_VALUE)
3568 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3572 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3575 ok(0, "WSAEventSelect failed, error %d\n", ret);
3579 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3582 ok(0, "WSAEventSelect failed, error %d\n", ret);
3587 server = socket(AF_INET, SOCK_STREAM, 0);
3588 if (server == INVALID_SOCKET)
3590 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3594 memset(&addr, 0, sizeof(addr));
3595 addr.sin_family = AF_INET;
3596 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
3597 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
3600 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3605 ret = getsockname(server, (struct sockaddr*)&addr, &len);
3608 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3612 ret = listen(server, 2);
3615 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3619 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
3620 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3622 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3626 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr));
3627 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3629 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3634 dst = accept(server, (struct sockaddr*)&addr, &len);
3635 if (dst == INVALID_SOCKET)
3637 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3642 dst2 = accept(server, (struct sockaddr*)&addr, &len);
3643 if (dst2 == INVALID_SOCKET)
3645 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3649 closesocket(server);
3650 server = INVALID_SOCKET;
3652 /* On Windows it seems when a non-blocking socket sends to a
3653 blocking socket on the same host, the send() is BLOCKING,
3654 so make both sockets non-blocking. src is already non-blocking
3655 from the async select */
3657 if (set_blocking(dst, FALSE))
3659 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3663 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
3666 ok(0, "could not allocate memory for test\n");
3670 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3671 if (ov.hEvent == NULL)
3673 ok(0, "could not create event object, errno = %d\n", GetLastError());
3677 ov2.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3678 if (ov2.hEvent == NULL)
3680 ok(0, "could not create event object, errno = %d\n", GetLastError());
3684 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3685 ok_event_seq(src, hEvent, connect_seq, NULL, 1);
3686 ok_event_seq(src2, hEvent2, connect_seq, NULL, 1);
3687 /* broken on all windows - FD_CONNECT error is garbage */
3689 /* Test simple send/recv */
3690 ret = send(dst, buffer, 100, 0);
3691 ok(ret == 100, "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, MSG_PEEK);
3695 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError());
3696 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3698 ret = recv(src, buffer, 50, 0);
3699 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3700 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3702 ret = recv(src, buffer, 50, 0);
3703 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3704 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3706 /* fun fact - events are reenabled even on failure, but only for messages */
3707 ret = send(dst, "1", 1, 0);
3708 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3709 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3711 ret = recv(src, buffer, -1, 0);
3712 ok(ret == SOCKET_ERROR && (GetLastError() == WSAEFAULT || GetLastError() == WSAENOBUFS),
3713 "Failed to recv buffer %d err %d\n", ret, GetLastError());
3716 broken_seq[0] = empty_seq; /* win9x */
3717 broken_seq[1] = NULL;
3718 todo_wine ok_event_seq(src, hEvent, read_seq, broken_seq, 0);
3721 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3723 ret = recv(src, buffer, 1, 0);
3724 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3725 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3727 /* Interaction with overlapped */
3728 bufs.len = sizeof(char);
3730 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3731 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3732 "WSARecv failed - %d error %d\n", ret, GetLastError());
3734 bufs.len = sizeof(char);
3735 bufs.buf = buffer+1;
3736 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL);
3737 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3738 "WSARecv failed - %d error %d\n", ret, GetLastError());
3740 ret = send(dst, "12", 2, 0);
3741 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3742 broken_seq[0] = read_read_seq; /* win9x */
3743 broken_seq[1] = NULL;
3744 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3746 dwRet = WaitForSingleObject(ov.hEvent, 100);
3747 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3748 if (dwRet == WAIT_OBJECT_0)
3750 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3751 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3752 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3753 ok(buffer[0] == '1', "Got %c instead of 1\n", buffer[0]);
3756 dwRet = WaitForSingleObject(ov2.hEvent, 100);
3757 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3758 if (dwRet == WAIT_OBJECT_0)
3760 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE);
3761 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3762 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3763 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[1]);
3766 ret = send(dst, "1", 1, 0);
3767 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3768 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3770 ret = recv(src, buffer, 1, 0);
3771 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3772 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3774 /* Notifications are delivered as soon as possible, blocked only on
3775 * async requests on the same type */
3776 bufs.len = sizeof(char);
3778 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3779 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3780 "WSARecv failed - %d error %d\n", ret, GetLastError());
3783 ret = send(dst, "1", 1, MSG_OOB);
3784 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3785 ok_event_seq(src, hEvent, oob_seq, NULL, 0);
3788 dwRet = WaitForSingleObject(ov.hEvent, 100);
3789 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError());
3791 ret = send(dst, "2", 1, 0);
3792 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3793 broken_seq[0] = read_seq; /* win98 */
3794 broken_seq[1] = NULL;
3795 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3797 dwRet = WaitForSingleObject(ov.hEvent, 100);
3798 ok(dwRet == WAIT_OBJECT_0 || broken(dwRet == WAIT_TIMEOUT),
3799 "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3800 if (dwRet == WAIT_OBJECT_0)
3802 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3803 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3804 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3805 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]);
3807 else if (dwRet == WAIT_TIMEOUT)
3809 /* this happens on win98. We get an FD_READ later on the next test */
3810 CancelIo((HANDLE) src);
3814 ret = recv(src, buffer, 1, MSG_OOB);
3815 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3816 /* We get OOB notification, but no data on wine */
3817 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3820 /* Flood the send queue */
3821 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3822 if (hThread == NULL)
3824 ok(0, "CreateThread failed, error %d\n", GetLastError());
3828 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3829 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3831 /* Now if we send a ton of data and the 'server' does not drain it fast
3832 * enough (set drain_pause to be sure), the socket send buffer will only
3833 * take some of it, and we will get a short write. This will trigger
3834 * another FD_WRITE event as soon as data is sent and more space becomes
3835 * available, but not any earlier. */
3839 ret = send(src, buffer, bufferSize, 0);
3840 } while (ret == bufferSize);
3842 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
3844 Sleep(400); /* win9x */
3845 broken_seq[0] = read_write_seq;
3846 broken_seq[1] = NULL;
3847 ok_event_seq(src, hEvent, write_seq, broken_seq, 0);
3851 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3854 /* Test how FD_CLOSE is handled */
3855 ret = send(dst, "12", 2, 0);
3856 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3858 /* Wait a little and let the send complete */
3861 dst = INVALID_SOCKET;
3864 /* We can never implement this in wine, best we can hope for is
3865 sending FD_CLOSE after the reads complete */
3866 broken_seq[0] = read_seq; /* win9x */
3867 broken_seq[1] = NULL;
3868 todo_wine ok_event_seq(src, hEvent, read_close_seq, broken_seq, 0);
3870 ret = recv(src, buffer, 1, 0);
3871 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3872 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3874 ret = recv(src, buffer, 1, 0);
3875 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3876 /* want it? it's here, but you can't have it */
3877 broken_seq[0] = close_seq; /* win9x */
3878 broken_seq[1] = NULL;
3879 todo_wine ok_event_seq(src, hEvent, empty_seq, /* wine sends FD_CLOSE here */
3882 /* Test how FD_CLOSE is handled */
3883 ret = send(dst2, "12", 2, 0);
3884 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3887 shutdown(dst2, SD_SEND);
3890 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3891 regressions, don't mark them as todo_wine, and mark windows as broken */
3892 broken_seq[0] = read_close_seq;
3893 broken_seq[1] = close_seq;
3894 broken_seq[2] = NULL;
3895 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3897 ret = recv(src2, buffer, 1, 0);
3898 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3899 broken_seq[0] = close_seq; /* win98 */
3900 broken_seq[1] = NULL;
3901 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3903 ret = recv(src2, buffer, 1, 0);
3904 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3905 broken_seq[0] = empty_seq;
3906 broken_seq[1] = NULL;
3907 ok_event_seq(src2, hEvent2, close_seq, broken_seq, 0);
3909 ret = send(src2, "1", 1, 0);
3910 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3911 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3913 ret = send(src2, "1", 1, 0);
3914 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3915 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3918 if (src != INVALID_SOCKET)
3920 flush_events(src, hEvent);
3923 if (src2 != INVALID_SOCKET)
3925 flush_events(src2, hEvent2);
3928 HeapFree(GetProcessHeap(), 0, buffer);
3929 if (server != INVALID_SOCKET)
3930 closesocket(server);
3931 if (dst != INVALID_SOCKET)
3933 if (dst2 != INVALID_SOCKET)
3935 if (hThread != NULL)
3936 CloseHandle(hThread);
3938 DestroyWindow(hWnd);
3940 CloseHandle(hEvent);
3941 if (hEvent2 != NULL)
3942 CloseHandle(hEvent2);
3943 if (ov.hEvent != NULL)
3944 CloseHandle(ov.hEvent);
3945 if (ov2.hEvent != NULL)
3946 CloseHandle(ov2.hEvent);
3949 static void test_ipv6only(void)
3951 SOCKET v4 = INVALID_SOCKET,
3952 v6 = INVALID_SOCKET;
3953 struct sockaddr_in sin4;
3954 struct sockaddr_in6 sin6;
3957 memset(&sin4, 0, sizeof(sin4));
3958 sin4.sin_family = AF_INET;
3959 sin4.sin_port = htons(SERVERPORT);
3961 memset(&sin6, 0, sizeof(sin6));
3962 sin6.sin6_family = AF_INET6;
3963 sin6.sin6_port = htons(SERVERPORT);
3965 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
3966 if (v6 == INVALID_SOCKET) {
3967 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3968 WSAGetLastError(), WSAEAFNOSUPPORT);
3971 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
3973 skip("Could not bind IPv6 address (LastError: %d).\n",
3978 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3979 if (v4 == INVALID_SOCKET) {
3980 skip("Could not create IPv4 socket (LastError: %d).\n",
3984 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
3985 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
3986 WSAGetLastError(), WSAEADDRINUSE);
3989 if (v4 != INVALID_SOCKET)
3991 if (v6 != INVALID_SOCKET)
3995 static void test_WSASendTo(void)
3998 struct sockaddr_in addr;
3999 char buf[12] = "hello world";
4004 addr.sin_family = AF_INET;
4005 addr.sin_port = htons(139);
4006 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
4007 data_buf.len = sizeof(buf);
4010 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
4011 ok(0, "socket() failed error: %d\n", WSAGetLastError());
4015 WSASetLastError(12345);
4016 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4017 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
4018 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4020 WSASetLastError(12345);
4021 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4022 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4023 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4025 WSASetLastError(12345);
4026 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
4027 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
4030 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
4031 "a successful call to WSASendTo()\n");
4034 static DWORD WINAPI recv_thread(LPVOID arg)
4036 SOCKET sock = *(SOCKET *)arg;
4043 wsa.len = sizeof(buffer);
4044 ov.hEvent = WSACreateEvent();
4045 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL);
4047 WaitForSingleObject(ov.hEvent, 1000);
4048 WSACloseEvent(ov.hEvent);
4052 static void test_WSARecv(void)
4054 SOCKET src, dest, server = INVALID_SOCKET;
4058 DWORD bytesReturned, flags, id;
4060 struct sockaddr_in addr;
4066 memset(&ov, 0, sizeof(ov));
4068 tcp_socketpair(&src, &dest);
4069 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
4071 skip("failed to create sockets\n");
4075 bufs.len = sizeof(buf);
4079 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4080 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
4086 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
4087 ok(!iret, "Failed to set linger %d\n", GetLastError());
4089 iret = WSARecv(dest, &bufs, 1, NULL, &flags, &ov, NULL);
4090 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
4092 iret = WSARecv(dest, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
4093 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
4096 src = INVALID_SOCKET;
4098 dwret = WaitForSingleObject(ov.hEvent, 1000);
4099 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
4101 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
4102 todo_wine ok(!bret && (GetLastError() == ERROR_NETNAME_DELETED || broken(GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */),
4103 "Did not get disconnect event: %d, error %d\n", bret, GetLastError());
4104 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
4106 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
4107 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4108 if (src == INVALID_SOCKET) goto end;
4110 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
4111 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4112 if (server == INVALID_SOCKET) goto end;
4114 memset(&addr, 0, sizeof(addr));
4115 addr.sin_family = AF_INET;
4116 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
4117 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
4121 iret = getsockname(server, (struct sockaddr *)&addr, &len);
4124 iret = listen(server, 1);
4127 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr));
4131 dest = accept(server, (struct sockaddr *)&addr, &len);
4132 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4133 if (dest == INVALID_SOCKET) goto end;
4135 send(src, "test message", sizeof("test message"), 0);
4136 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id);
4137 CloseHandle(thread);
4140 if (server != INVALID_SOCKET)
4141 closesocket(server);
4142 if (dest != INVALID_SOCKET)
4144 if (src != INVALID_SOCKET)
4147 WSACloseEvent(ov.hEvent);
4150 static void test_GetAddrInfoW(void)
4152 static const WCHAR port[] = {'8','0',0};
4153 static const WCHAR empty[] = {0};
4154 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
4155 static const WCHAR zero[] = {'0',0};
4158 ADDRINFOW *result, hint;
4160 if (!pGetAddrInfoW || !pFreeAddrInfoW)
4162 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
4166 memset(&hint, 0, sizeof(ADDRINFOW));
4168 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
4169 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
4172 ret = pGetAddrInfoW(empty, NULL, NULL, &result);
4175 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4176 ok(result != NULL, "GetAddrInfoW failed\n");
4178 pFreeAddrInfoW(result);
4181 ret = pGetAddrInfoW(NULL, zero, NULL, &result);
4184 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4185 ok(result != NULL, "GetAddrInfoW failed\n");
4187 pFreeAddrInfoW(result);
4190 ret = pGetAddrInfoW(empty, zero, NULL, &result);
4193 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4194 ok(result != NULL, "GetAddrInfoW failed\n");
4196 pFreeAddrInfoW(result);
4198 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
4199 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4200 pFreeAddrInfoW(result);
4202 ret = pGetAddrInfoW(localhost, port, NULL, &result);
4203 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4204 pFreeAddrInfoW(result);
4206 ret = pGetAddrInfoW(localhost, port, &hint, &result);
4207 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4208 pFreeAddrInfoW(result);
4211 static void test_ConnectEx(void)
4213 SOCKET listener = INVALID_SOCKET;
4214 SOCKET acceptor = INVALID_SOCKET;
4215 SOCKET connector = INVALID_SOCKET;
4216 struct sockaddr_in address, conaddress;
4218 OVERLAPPED overlapped;
4219 LPFN_CONNECTEX pConnectEx;
4220 GUID connectExGuid = WSAID_CONNECTEX;
4221 DWORD bytesReturned;
4227 memset(&overlapped, 0, sizeof(overlapped));
4229 listener = socket(AF_INET, SOCK_STREAM, 0);
4230 if (listener == INVALID_SOCKET) {
4231 skip("could not create listener socket, error %d\n", WSAGetLastError());
4235 connector = socket(AF_INET, SOCK_STREAM, 0);
4236 if (connector == INVALID_SOCKET) {
4237 skip("could not create connector socket, error %d\n", WSAGetLastError());
4241 memset(&address, 0, sizeof(address));
4242 address.sin_family = AF_INET;
4243 address.sin_addr.s_addr = inet_addr("127.0.0.1");
4244 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
4246 skip("failed to bind, error %d\n", WSAGetLastError());
4250 addrlen = sizeof(address);
4251 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
4253 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4257 if (set_blocking(listener, TRUE)) {
4258 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4262 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
4263 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
4265 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret, WSAGetLastError());
4269 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4270 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
4271 "returned %d + errno %d\n", bret, WSAGetLastError());
4273 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4274 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
4275 "returned %d + errno %d\n", bret, WSAGetLastError());
4276 if (bret == TRUE || WSAGetLastError() != WSAEINVAL)
4278 acceptor = accept(listener, NULL, NULL);
4279 if (acceptor != INVALID_SOCKET) {
4280 closesocket(acceptor);
4281 acceptor = INVALID_SOCKET;
4284 closesocket(connector);
4285 connector = socket(AF_INET, SOCK_STREAM, 0);
4286 if (connector == INVALID_SOCKET) {
4287 skip("could not create connector socket, error %d\n", WSAGetLastError());
4292 /* ConnectEx needs a bound socket */
4293 memset(&conaddress, 0, sizeof(conaddress));
4294 conaddress.sin_family = AF_INET;
4295 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4296 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4298 skip("failed to bind, error %d\n", WSAGetLastError());
4302 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
4303 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
4304 "returned %d + errno %d\n", bret, WSAGetLastError());
4306 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4307 if (overlapped.hEvent == NULL) {
4308 skip("could not create event object, errno = %d\n", GetLastError());
4312 iret = listen(listener, 1);
4314 skip("listening failed, errno = %d\n", WSAGetLastError());
4318 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4319 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4320 "returned %d + errno %d\n", bret, WSAGetLastError());
4321 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4322 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4324 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4325 ok(bret, "Connecting failed, error %d\n", GetLastError());
4326 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
4328 closesocket(connector);
4329 connector = socket(AF_INET, SOCK_STREAM, 0);
4330 if (connector == INVALID_SOCKET) {
4331 skip("could not create connector socket, error %d\n", WSAGetLastError());
4334 /* ConnectEx needs a bound socket */
4335 memset(&conaddress, 0, sizeof(conaddress));
4336 conaddress.sin_family = AF_INET;
4337 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4338 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4340 skip("failed to bind, error %d\n", WSAGetLastError());
4344 acceptor = accept(listener, NULL, NULL);
4345 if (acceptor != INVALID_SOCKET) {
4346 closesocket(acceptor);
4352 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
4353 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4354 "returned %d + errno %d\n", bret, WSAGetLastError());
4355 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4356 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4358 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4359 ok(bret, "Connecting failed, error %d\n", GetLastError());
4360 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
4362 acceptor = accept(listener, NULL, NULL);
4363 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
4365 bytesReturned = recv(acceptor, buffer, 3, 0);
4367 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
4368 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
4369 "Failed to get the right data, expected '123', got '%s'\n", buffer);
4371 closesocket(connector);
4372 connector = socket(AF_INET, SOCK_STREAM, 0);
4373 if (connector == INVALID_SOCKET) {
4374 skip("could not create connector socket, error %d\n", WSAGetLastError());
4377 /* ConnectEx needs a bound socket */
4378 memset(&conaddress, 0, sizeof(conaddress));
4379 conaddress.sin_family = AF_INET;
4380 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4381 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4383 skip("failed to bind, error %d\n", WSAGetLastError());
4387 if (acceptor != INVALID_SOCKET) {
4388 closesocket(acceptor);
4389 acceptor = INVALID_SOCKET;
4392 /* Connect with error */
4393 closesocket(listener);
4394 listener = INVALID_SOCKET;
4396 address.sin_port = htons(1);
4398 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4399 ok(bret == FALSE && GetLastError(), "ConnectEx to bad destination failed: "
4400 "returned %d + errno %d\n", bret, GetLastError());
4402 if (GetLastError() == ERROR_IO_PENDING)
4404 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4405 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4407 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4408 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
4409 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4412 ok(GetLastError() == WSAECONNREFUSED,
4413 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4417 if (overlapped.hEvent)
4418 WSACloseEvent(overlapped.hEvent);
4419 if (listener != INVALID_SOCKET)
4420 closesocket(listener);
4421 if (acceptor != INVALID_SOCKET)
4422 closesocket(acceptor);
4423 if (connector != INVALID_SOCKET)
4424 closesocket(connector);
4427 static void test_AcceptEx(void)
4429 SOCKET listener = INVALID_SOCKET;
4430 SOCKET acceptor = INVALID_SOCKET;
4431 SOCKET connector = INVALID_SOCKET;
4432 SOCKET connector2 = INVALID_SOCKET;
4433 struct sockaddr_in bindAddress;
4435 GUID acceptExGuid = WSAID_ACCEPTEX;
4436 LPFN_ACCEPTEX pAcceptEx = NULL;
4437 fd_set fds_accept, fds_send;
4438 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
4440 DWORD bytesReturned;
4442 OVERLAPPED overlapped;
4447 memset(&overlapped, 0, sizeof(overlapped));
4449 listener = socket(AF_INET, SOCK_STREAM, 0);
4450 if (listener == INVALID_SOCKET) {
4451 skip("could not create listener socket, error %d\n", WSAGetLastError());
4455 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4456 if (acceptor == INVALID_SOCKET) {
4457 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4461 connector = socket(AF_INET, SOCK_STREAM, 0);
4462 if (connector == INVALID_SOCKET) {
4463 skip("could not create connector socket, error %d\n", WSAGetLastError());
4467 memset(&bindAddress, 0, sizeof(bindAddress));
4468 bindAddress.sin_family = AF_INET;
4469 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4470 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4472 skip("failed to bind, error %d\n", WSAGetLastError());
4476 socklen = sizeof(bindAddress);
4477 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
4479 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4483 if (set_blocking(listener, FALSE)) {
4484 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4488 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
4489 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
4491 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
4495 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4496 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4497 &bytesReturned, &overlapped);
4498 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
4499 "returned %d + errno %d\n", bret, WSAGetLastError());
4501 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4502 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4503 &bytesReturned, &overlapped);
4504 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
4505 "returned %d + errno %d\n", bret, WSAGetLastError());
4507 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4508 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4509 &bytesReturned, &overlapped);
4511 (WSAGetLastError() == WSAEINVAL ||
4512 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
4513 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
4515 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
4516 &bytesReturned, &overlapped);
4517 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
4518 "returned %d + errno %d\n", bret, WSAGetLastError());
4520 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
4521 &bytesReturned, &overlapped);
4522 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
4523 "returned %d + errno %d\n", bret, WSAGetLastError());
4525 bret = pAcceptEx(listener, acceptor, buffer, 0,
4526 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4527 &bytesReturned, NULL);
4528 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
4529 "returned %d + errno %d\n", bret, WSAGetLastError());
4531 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4532 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4533 &bytesReturned, &overlapped);
4534 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
4535 "returned %d + errno %d\n", bret, WSAGetLastError());
4537 iret = listen(listener, 5);
4539 skip("listening failed, errno = %d\n", WSAGetLastError());
4543 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4544 if (overlapped.hEvent == NULL) {
4545 skip("could not create event object, errno = %d\n", GetLastError());
4549 bret = pAcceptEx(listener, acceptor, buffer, 0,
4550 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4551 &bytesReturned, &overlapped);
4552 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4554 bret = pAcceptEx(listener, acceptor, buffer, 0,
4555 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4556 &bytesReturned, &overlapped);
4557 todo_wine ok((bret == FALSE && WSAGetLastError() == WSAEINVAL) || broken(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) /* NT4 */,
4558 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
4559 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
4560 /* We need to cancel this call, otherwise things fail */
4561 bret = CancelIo((HANDLE) listener);
4562 ok(bret, "Failed to cancel failed test. Bailing...\n");
4564 WaitForSingleObject(overlapped.hEvent, 0);
4566 bret = pAcceptEx(listener, acceptor, buffer, 0,
4567 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4568 &bytesReturned, &overlapped);
4569 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4572 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4573 todo_wine ok((iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL) || broken(!iret) /* NT4 */,
4574 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
4575 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
4576 /* We need to cancel this call, otherwise things fail */
4577 closesocket(acceptor);
4578 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4579 if (acceptor == INVALID_SOCKET) {
4580 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4584 bret = CancelIo((HANDLE) listener);
4585 ok(bret, "Failed to cancel failed test. Bailing...\n");
4588 bret = pAcceptEx(listener, acceptor, buffer, 0,
4589 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4590 &bytesReturned, &overlapped);
4591 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4594 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4595 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4597 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
4598 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4600 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4601 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4602 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4604 closesocket(connector);
4605 connector = INVALID_SOCKET;
4606 closesocket(acceptor);
4608 /* Test short reads */
4610 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4611 if (acceptor == INVALID_SOCKET) {
4612 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4615 connector = socket(AF_INET, SOCK_STREAM, 0);
4616 if (connector == INVALID_SOCKET) {
4617 skip("could not create connector socket, error %d\n", WSAGetLastError());
4620 bret = pAcceptEx(listener, acceptor, buffer, 2,
4621 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4622 &bytesReturned, &overlapped);
4623 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4625 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4626 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4628 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4629 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
4631 iret = send(connector, buffer, 1, 0);
4632 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
4634 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4635 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4637 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4638 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4639 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4641 closesocket(connector);
4642 connector = INVALID_SOCKET;
4643 closesocket(acceptor);
4645 /* Test CF_DEFER & AcceptEx interaction */
4647 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4648 if (acceptor == INVALID_SOCKET) {
4649 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4652 connector = socket(AF_INET, SOCK_STREAM, 0);
4653 if (connector == INVALID_SOCKET) {
4654 skip("could not create connector socket, error %d\n", WSAGetLastError());
4657 connector2 = socket(AF_INET, SOCK_STREAM, 0);
4658 if (connector == INVALID_SOCKET) {
4659 skip("could not create connector socket, error %d\n", WSAGetLastError());
4663 if (set_blocking(connector, FALSE)) {
4664 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4668 if (set_blocking(connector2, FALSE)) {
4669 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4673 /* Connect socket #1 */
4674 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4675 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4677 FD_ZERO ( &fds_accept );
4678 FD_ZERO ( &fds_send );
4680 FD_SET ( listener, &fds_accept );
4681 FD_SET ( connector, &fds_send );
4687 for (i = 0; i < 4000; ++i)
4689 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
4691 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
4692 "acceptex test(%d): could not select on socket, errno %d\n" );
4694 /* check for incoming requests */
4695 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
4698 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
4699 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
4700 bret = pAcceptEx(listener, acceptor, buffer, 0,
4701 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4702 &bytesReturned, &overlapped);
4703 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4705 else if (got == 2) {
4706 /* this should be socket #2 */
4707 SOCKET tmp = accept(listener, NULL, NULL);
4708 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
4712 ok(FALSE, "Got more than 2 connections?\n");
4715 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
4716 /* Send data on second socket, and stop */
4717 send(connector2, "2", 1, 0);
4718 FD_CLR ( connector2, &fds_send );
4722 if ( FD_ISSET ( connector, &fds_opensend ) ) {
4723 /* Once #1 is connected, allow #2 to connect */
4726 send(connector, "1", 1, 0);
4727 FD_CLR ( connector, &fds_send );
4729 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4730 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4731 FD_SET ( connector2, &fds_send );
4735 ok (got == 2 || broken(got == 1) /* NT4 */,
4736 "Did not get both connections, got %d\n", got);
4738 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4739 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4741 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4742 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4743 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4745 set_blocking(acceptor, TRUE);
4746 iret = recv( acceptor, buffer, 2, 0);
4747 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
4749 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
4751 closesocket(connector);
4752 connector = INVALID_SOCKET;
4753 closesocket(acceptor);
4755 /* clean up in case of failures */
4756 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
4757 closesocket(acceptor);
4759 /* Disconnect during receive? */
4761 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4762 if (acceptor == INVALID_SOCKET) {
4763 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4766 connector = socket(AF_INET, SOCK_STREAM, 0);
4767 if (connector == INVALID_SOCKET) {
4768 skip("could not create connector socket, error %d\n", WSAGetLastError());
4771 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4772 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4773 &bytesReturned, &overlapped);
4774 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4776 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4777 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4779 closesocket(connector);
4780 connector = INVALID_SOCKET;
4782 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4783 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4785 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4786 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4787 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4789 closesocket(acceptor);
4791 /* Test closing with pending requests */
4793 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4794 if (acceptor == INVALID_SOCKET) {
4795 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4798 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4799 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4800 &bytesReturned, &overlapped);
4801 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4803 closesocket(acceptor);
4805 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4806 todo_wine ok(dwret == WAIT_OBJECT_0 || broken(dwret == WAIT_TIMEOUT) /* NT4/2000 */,
4807 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4809 if (dwret != WAIT_TIMEOUT) {
4810 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4811 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
4814 bret = CancelIo((HANDLE) listener);
4815 ok(bret, "Failed to cancel failed test. Bailing...\n");
4817 WaitForSingleObject(overlapped.hEvent, 0);
4820 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4821 if (acceptor == INVALID_SOCKET) {
4822 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4825 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4826 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4827 &bytesReturned, &overlapped);
4828 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4830 CancelIo((HANDLE) acceptor);
4832 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4833 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
4835 closesocket(acceptor);
4837 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4838 if (acceptor == INVALID_SOCKET) {
4839 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4842 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4843 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4844 &bytesReturned, &overlapped);
4845 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4847 closesocket(listener);
4848 listener = INVALID_SOCKET;
4850 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4851 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4853 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4854 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
4857 if (overlapped.hEvent)
4858 WSACloseEvent(overlapped.hEvent);
4859 if (listener != INVALID_SOCKET)
4860 closesocket(listener);
4861 if (acceptor != INVALID_SOCKET)
4862 closesocket(acceptor);
4863 if (connector != INVALID_SOCKET)
4864 closesocket(connector);
4865 if (connector2 != INVALID_SOCKET)
4866 closesocket(connector2);
4869 static void test_getpeername(void)
4872 struct sockaddr_in sa, sa_out;
4874 const char buf[] = "hello world";
4877 /* Test the parameter validation order. */
4878 ret = getpeername(INVALID_SOCKET, NULL, NULL);
4879 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4880 ok(WSAGetLastError() == WSAENOTSOCK,
4881 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
4883 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
4884 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
4885 if (sock == INVALID_SOCKET)
4887 skip("Socket creation failed with %d\n", WSAGetLastError());
4891 ret = getpeername(sock, NULL, NULL);
4892 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4893 ok(WSAGetLastError() == WSAENOTCONN ||
4894 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
4895 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4897 memset(&sa, 0, sizeof(sa));
4898 sa.sin_family = AF_INET;
4899 sa.sin_port = htons(139);
4900 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
4902 /* sendto does not change a socket's connection state. */
4903 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
4904 ok(ret != SOCKET_ERROR,
4905 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4907 ret = getpeername(sock, NULL, NULL);
4908 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4909 ok(WSAGetLastError() == WSAENOTCONN ||
4910 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
4911 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4913 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
4915 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4917 ret = getpeername(sock, NULL, NULL);
4918 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4919 ok(WSAGetLastError() == WSAEFAULT,
4920 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4922 /* Test crashes on Wine. */
4925 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
4926 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
4927 ok(WSAGetLastError() == WSAEFAULT,
4928 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4932 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
4933 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
4934 ok(WSAGetLastError() == WSAEFAULT,
4935 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4937 sa_len = sizeof(sa_out);
4938 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
4939 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
4940 ok(!memcmp(&sa, &sa_out, sizeof(sa)),
4941 "Expected the returned structure to be identical to the connect structure\n");
4946 static void test_sioRoutingInterfaceQuery(void)
4950 SOCKADDR_IN sin = { 0 }, sout = { 0 };
4951 DWORD bytesReturned;
4953 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
4954 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
4955 if (sock == INVALID_SOCKET)
4957 skip("Socket creation failed with %d\n", WSAGetLastError());
4960 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
4962 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4963 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4964 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4965 NULL, 0, NULL, NULL, NULL);
4966 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4967 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4968 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4969 NULL, 0, &bytesReturned, NULL, NULL);
4970 ok(ret == SOCKET_ERROR &&
4971 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
4972 WSAGetLastError() == WSAEINVAL /* NT4 */||
4973 WSAGetLastError() == WSAEAFNOSUPPORT),
4974 "expected WSAEFAULT or WSAEINVAL or WSAEAFNOSUPPORT, got %d\n",
4976 sin.sin_family = AF_INET;
4977 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4978 NULL, 0, &bytesReturned, NULL, NULL);
4979 ok(ret == SOCKET_ERROR &&
4980 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
4981 WSAGetLastError() == WSAEINVAL),
4982 "expected WSAEFAULT or WSAEINVAL, got %d\n", WSAGetLastError());
4983 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4984 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4985 NULL, 0, &bytesReturned, NULL, NULL);
4986 ok(ret == SOCKET_ERROR &&
4987 (WSAGetLastError() == WSAEINVAL /* NT4 */ ||
4988 WSAGetLastError() == WSAEFAULT),
4989 "expected WSAEINVAL or WSAEFAULT, got %d\n", WSAGetLastError());
4990 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
4991 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
4992 ok(!ret || broken(WSAGetLastError() == WSAEINVAL /* NT4 */),
4993 "WSAIoctl failed: %d\n", WSAGetLastError());
4996 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n",
4998 /* We expect the source address to be INADDR_LOOPBACK as well, but
4999 * there's no guarantee that a route to the loopback address exists,
5000 * so rather than introduce spurious test failures we do not test the
5007 static void test_synchronous_WSAIoctl(void)
5009 HANDLE previous_port, io_port;
5010 WSAOVERLAPPED overlapped, *olp;
5018 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
5019 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
5021 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
5022 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
5024 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
5025 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
5028 memset( &overlapped, 0, sizeof(overlapped) );
5029 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
5030 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
5032 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
5033 ok( ret, "failed to get completion status %u\n", GetLastError() );
5035 CloseHandle( io_port );
5036 closesocket( socket );
5037 CloseHandle( previous_port );
5040 #define WM_ASYNCCOMPLETE (WM_USER + 100)
5041 static HWND create_async_message_window(void)
5043 static const char class_name[] = "ws2_32 async message window class";
5045 WNDCLASSEXA wndclass;
5048 wndclass.cbSize = sizeof(wndclass);
5049 wndclass.style = CS_HREDRAW | CS_VREDRAW;
5050 wndclass.lpfnWndProc = DefWindowProcA;
5051 wndclass.cbClsExtra = 0;
5052 wndclass.cbWndExtra = 0;
5053 wndclass.hInstance = GetModuleHandleA(NULL);
5054 wndclass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
5055 wndclass.hIconSm = LoadIconA(NULL, IDI_APPLICATION);
5056 wndclass.hCursor = LoadCursorA(NULL, IDC_ARROW);
5057 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
5058 wndclass.lpszClassName = class_name;
5059 wndclass.lpszMenuName = NULL;
5061 RegisterClassExA(&wndclass);
5063 hWnd = CreateWindow(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW,
5064 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
5067 ok(0, "failed to create window: %u\n", GetLastError());
5074 static void test_WSAAsyncGetServByPort(void)
5076 HWND hwnd = create_async_message_window();
5078 char buffer[MAXGETHOSTSTRUCT];
5083 /* FIXME: The asynchronous window messages should be tested. */
5085 /* Parameters are not checked when initiating the asynchronous operation. */
5086 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0);
5087 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5089 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0);
5090 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5092 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0);
5093 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5095 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT);
5096 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5098 DestroyWindow(hwnd);
5101 /**************** Main program ***************/
5107 /* Leave these tests at the beginning. They depend on WSAStartup not having been
5108 * called, which is done by Init() below. */
5109 test_WithoutWSAStartup();
5110 test_WithWSAStartup();
5114 test_set_getsockopt();
5115 test_so_reuseaddr();
5117 test_extendedSocketOptions();
5119 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
5121 trace ( " **** STARTING TEST %d ****\n", i );
5122 do_test ( &tests[i] );
5123 trace ( " **** TEST %d COMPLETE ****\n", i );
5128 test_getservbyname();
5131 test_WSAAddressToStringA();
5132 test_WSAAddressToStringW();
5134 test_WSAStringToAddressA();
5135 test_WSAStringToAddressW();
5143 test_addr_to_print();
5146 test_gethostbyname_hack();
5155 test_GetAddrInfoW();
5160 test_sioRoutingInterfaceQuery();
5162 test_WSAAsyncGetServByPort();
5164 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */
5166 test_synchronous_WSAIoctl();