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 *pfreeaddrinfo)(struct addrinfo *);
63 static int (WINAPI *pgetaddrinfo)(LPCSTR,LPCSTR,const struct addrinfo *,struct addrinfo **);
64 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW);
65 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *);
66 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG);
68 /**************** Structs and typedefs ***************/
70 typedef struct thread_info
76 /* Information in the server about open client connections */
77 typedef struct sock_info
80 struct sockaddr_in addr;
81 struct sockaddr_in peer;
87 /* Test parameters for both server & client */
88 typedef struct test_params
92 const char *inet_addr;
99 /* server-specific test parameters */
100 typedef struct server_params
102 test_params *general;
107 /* client-specific test parameters */
108 typedef struct client_params
110 test_params *general;
115 /* This type combines all information for setting up a test scenario */
116 typedef struct test_setup
120 server_params srv_params;
122 client_params clt_params;
125 /* Thread local storage for server */
126 typedef struct server_memory
129 struct sockaddr_in addr;
130 sock_info sock[MAX_CLIENTS];
133 /* Thread local storage for client */
134 typedef struct client_memory
137 struct sockaddr_in addr;
142 /* SelectReadThread thread parameters */
143 typedef struct select_thread_params
147 } select_thread_params;
149 /**************** Static variables ***************/
151 static DWORD tls; /* Thread local storage index */
152 static HANDLE thread[1+MAX_CLIENTS];
153 static DWORD thread_id[1+MAX_CLIENTS];
154 static HANDLE server_ready;
155 static HANDLE client_ready[MAX_CLIENTS];
156 static int client_id;
158 /**************** General utility functions ***************/
160 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
162 SOCKET server = INVALID_SOCKET;
163 struct sockaddr_in addr;
167 *src = INVALID_SOCKET;
168 *dst = INVALID_SOCKET;
170 *src = socket(AF_INET, SOCK_STREAM, 0);
171 if (*src == INVALID_SOCKET)
174 server = socket(AF_INET, SOCK_STREAM, 0);
175 if (server == INVALID_SOCKET)
178 memset(&addr, 0, sizeof(addr));
179 addr.sin_family = AF_INET;
180 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
181 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
186 ret = getsockname(server, (struct sockaddr*)&addr, &len);
190 ret = listen(server, 1);
194 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
199 *dst = accept(server, (struct sockaddr*)&addr, &len);
202 if (server != INVALID_SOCKET)
204 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
211 static void set_so_opentype ( BOOL overlapped )
213 int optval = !overlapped, newval, len = sizeof (int);
215 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
216 (LPVOID) &optval, sizeof (optval) ) == 0,
217 "setting SO_OPENTYPE failed\n" );
218 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
219 (LPVOID) &newval, &len ) == 0,
220 "getting SO_OPENTYPE failed\n" );
221 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
224 static int set_blocking ( SOCKET s, BOOL blocking )
226 u_long val = !blocking;
227 return ioctlsocket ( s, FIONBIO, &val );
230 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
233 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
234 memset ( p, c, chunk_size );
237 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
241 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
243 for ( i = 0; i < chunk_size; i++ )
244 if ( p[i] != c ) return i;
250 * This routine is called when a client / server does not expect any more data,
251 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
253 static void read_zero_bytes ( SOCKET s )
257 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
259 ok ( n <= 0, "garbage data received: %d bytes\n", n );
262 static int do_oob_send ( SOCKET s, char *buf, int buflen, int sendlen )
264 char* last = buf + buflen, *p;
266 for ( p = buf; n > 0 && p < last; p += n )
267 n = send ( s, p, min ( sendlen, last - p ), MSG_OOB );
268 wsa_ok ( n, 0 <=, "do_oob_send (%x): error %d\n" );
272 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
274 char* last = buf + buflen, *p;
276 for ( p = buf; n > 0 && p < last; p += n )
277 n = send ( s, p, min ( sendlen, last - p ), 0 );
278 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
282 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
284 char* last = buf + buflen, *p;
286 for ( p = buf; n > 0 && p < last; p += n )
287 n = recv ( s, p, min ( recvlen, last - p ), 0 );
288 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
292 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, int *fromlen, int recvlen )
294 char* last = buf + buflen, *p;
296 for ( p = buf; n > 0 && p < last; p += n )
297 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
298 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
303 * Call this routine right after thread startup.
304 * SO_OPENTYPE must by 0, regardless what the server did.
306 static void check_so_opentype (void)
310 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
311 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
314 /**************** Server utility functions ***************/
317 * Even if we have closed our server socket cleanly,
318 * the OS may mark the address "in use" for some time -
319 * this happens with native Linux apps, too.
321 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
323 int err, wsaerr = 0, n_try = BIND_TRIES;
325 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
326 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
329 trace ( "address in use, waiting ...\n" );
330 Sleep ( 1000 * BIND_SLEEP );
332 ok ( err == 0, "failed to bind: %d\n", wsaerr );
335 static void server_start ( server_params *par )
338 test_params *gen = par->general;
339 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
341 TlsSetValue ( tls, mem );
342 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
343 NULL, 0, par->sock_flags );
344 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
346 mem->addr.sin_family = AF_INET;
347 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
348 mem->addr.sin_port = htons ( gen->inet_port );
350 for (i = 0; i < MAX_CLIENTS; i++)
352 mem->sock[i].s = INVALID_SOCKET;
353 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
354 mem->sock[i].n_recvd = 0;
355 mem->sock[i].n_sent = 0;
358 if ( gen->sock_type == SOCK_STREAM )
359 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
362 static void server_stop (void)
365 server_memory *mem = TlsGetValue ( tls );
367 for (i = 0; i < MAX_CLIENTS; i++ )
369 LocalFree ( mem->sock[i].buf );
370 if ( mem->sock[i].s != INVALID_SOCKET )
371 closesocket ( mem->sock[i].s );
373 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
375 ExitThread ( GetCurrentThreadId () );
378 /**************** Client utilitiy functions ***************/
380 static void client_start ( client_params *par )
382 test_params *gen = par->general;
383 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
385 TlsSetValue ( tls, mem );
387 WaitForSingleObject ( server_ready, INFINITE );
389 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
390 NULL, 0, par->sock_flags );
392 mem->addr.sin_family = AF_INET;
393 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
394 mem->addr.sin_port = htons ( gen->inet_port );
396 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
398 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
399 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
400 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
402 SetEvent ( client_ready[client_id] );
403 /* Wait for the other clients to come up */
404 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
407 static void client_stop (void)
409 client_memory *mem = TlsGetValue ( tls );
410 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
411 LocalFree ( mem->send_buf );
416 /**************** Servers ***************/
419 * simple_server: A very basic server doing synchronous IO.
421 static VOID WINAPI simple_server ( server_params *par )
423 test_params *gen = par->general;
425 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
426 id = GetCurrentThreadId();
428 trace ( "simple_server (%x) starting\n", id );
430 set_so_opentype ( FALSE ); /* non-overlapped */
431 server_start ( par );
432 mem = TlsGetValue ( tls );
434 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
435 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
437 trace ( "simple_server (%x) ready\n", id );
438 SetEvent ( server_ready ); /* notify clients */
440 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
442 trace ( "simple_server (%x): waiting for client\n", id );
444 /* accept a single connection */
445 tmp = sizeof ( mem->sock[0].peer );
446 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
447 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
449 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
450 "simple_server (%x): strange peer address\n", id );
452 /* Receive data & check it */
453 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
454 ok ( n_recvd == n_expected,
455 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
456 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
457 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
460 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
461 ok ( n_sent == n_expected,
462 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
465 read_zero_bytes ( mem->sock[0].s );
466 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
467 mem->sock[0].s = INVALID_SOCKET;
470 trace ( "simple_server (%x) exiting\n", id );
475 * oob_server: A very basic server receiving out-of-band data.
477 static VOID WINAPI oob_server ( server_params *par )
479 test_params *gen = par->general;
482 int pos, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
483 id = GetCurrentThreadId();
485 trace ( "oob_server (%x) starting\n", id );
487 set_so_opentype ( FALSE ); /* non-overlapped */
488 server_start ( par );
489 mem = TlsGetValue ( tls );
491 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
492 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
494 trace ( "oob_server (%x) ready\n", id );
495 SetEvent ( server_ready ); /* notify clients */
497 trace ( "oob_server (%x): waiting for client\n", id );
499 /* accept a single connection */
500 tmp = sizeof ( mem->sock[0].peer );
501 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
502 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
504 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
505 "oob_server (%x): strange peer address\n", id );
507 /* check atmark state */
508 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
509 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
511 /* Receive normal data and check atmark state */
512 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
513 ok ( n_recvd == n_expected,
514 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
515 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
516 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
518 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
519 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
521 /* Receive a part of the out-of-band data and check atmark state */
522 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, par->buflen );
524 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
527 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
528 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
530 /* Receive the rest of the out-of-band data and check atmark state */
531 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
533 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
534 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
537 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
538 mem->sock[0].s = INVALID_SOCKET;
540 trace ( "oob_server (%x) exiting\n", id );
545 * select_server: A non-blocking server.
547 static VOID WINAPI select_server ( server_params *par )
549 test_params *gen = par->general;
551 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
552 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
553 n_set, delta, n_ready;
554 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
555 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
557 trace ( "select_server (%x) starting\n", id );
559 set_so_opentype ( FALSE ); /* non-overlapped */
560 server_start ( par );
561 mem = TlsGetValue ( tls );
563 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
564 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
566 trace ( "select_server (%x) ready\n", id );
567 SetEvent ( server_ready ); /* notify clients */
569 FD_ZERO ( &fds_openrecv );
570 FD_ZERO ( &fds_recv );
571 FD_ZERO ( &fds_send );
572 FD_ZERO ( &fds_opensend );
574 FD_SET ( mem->s, &fds_openrecv );
578 fds_recv = fds_openrecv;
579 fds_send = fds_opensend;
583 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
584 "select_server (%x): select() failed: %d\n" );
586 /* check for incoming requests */
587 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
590 trace ( "select_server (%x): accepting client connection\n", id );
592 /* accept a single connection */
593 tmp = sizeof ( mem->sock[n_connections].peer );
594 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
595 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
597 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
598 "select_server (%x): strange peer address\n", id );
600 /* add to list of open connections */
601 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
602 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
607 /* handle open requests */
609 for ( i = 0; i < n_connections; i++ )
611 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
614 if ( mem->sock[i].n_recvd < n_expected ) {
615 /* Receive data & check it */
616 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 );
617 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
618 mem->sock[i].n_recvd += n_recvd;
620 if ( mem->sock[i].n_recvd == n_expected ) {
621 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
622 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
623 FD_CLR ( mem->sock[i].s, &fds_openrecv );
626 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
630 /* only echo back what we've received */
631 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
633 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
636 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
638 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
639 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
640 mem->sock[i].n_sent += n_sent;
642 if ( mem->sock[i].n_sent == n_expected ) {
643 FD_CLR ( mem->sock[i].s, &fds_opensend );
646 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
651 /* check that select returned the correct number of ready sockets */
652 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
654 /* check if all clients are done */
655 if ( ( fds_opensend.fd_count == 0 )
656 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
657 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
662 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
665 read_zero_bytes ( mem->sock[i].s );
666 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
667 mem->sock[i].s = INVALID_SOCKET;
670 trace ( "select_server (%x) exiting\n", id );
674 /**************** Clients ***************/
677 * simple_client: A very basic client doing synchronous IO.
679 static VOID WINAPI simple_client ( client_params *par )
681 test_params *gen = par->general;
683 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
685 id = GetCurrentThreadId();
686 trace ( "simple_client (%x): starting\n", id );
687 /* wait here because we want to call set_so_opentype before creating a socket */
688 WaitForSingleObject ( server_ready, INFINITE );
689 trace ( "simple_client (%x): server ready\n", id );
691 check_so_opentype ();
692 set_so_opentype ( FALSE ); /* non-overlapped */
693 client_start ( par );
694 mem = TlsGetValue ( tls );
697 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
698 0 ==, "simple_client (%x): connect error: %d\n" );
699 ok ( set_blocking ( mem->s, TRUE ) == 0,
700 "simple_client (%x): failed to set blocking mode\n", id );
701 trace ( "simple_client (%x) connected\n", id );
703 /* send data to server */
704 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
705 ok ( n_sent == n_expected,
706 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
708 /* shutdown send direction */
709 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
711 /* Receive data echoed back & check it */
712 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
713 ok ( n_recvd == n_expected,
714 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
717 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
718 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
721 read_zero_bytes ( mem->s );
722 trace ( "simple_client (%x) exiting\n", id );
727 * oob_client: A very basic client sending out-of-band data.
729 static VOID WINAPI oob_client ( client_params *par )
731 test_params *gen = par->general;
733 int n_sent, n_expected = gen->n_chunks * gen->chunk_size, id;
735 id = GetCurrentThreadId();
736 trace ( "oob_client (%x): starting\n", id );
737 /* wait here because we want to call set_so_opentype before creating a socket */
738 WaitForSingleObject ( server_ready, INFINITE );
739 trace ( "oob_client (%x): server ready\n", id );
741 check_so_opentype ();
742 set_so_opentype ( FALSE ); /* non-overlapped */
743 client_start ( par );
744 mem = TlsGetValue ( tls );
747 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
748 0 ==, "oob_client (%x): connect error: %d\n" );
749 ok ( set_blocking ( mem->s, TRUE ) == 0,
750 "oob_client (%x): failed to set blocking mode\n", id );
751 trace ( "oob_client (%x) connected\n", id );
753 /* send data to server */
754 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
755 ok ( n_sent == n_expected,
756 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
758 /* send out-of-band data to server */
759 n_sent = do_oob_send ( mem->s, mem->send_buf, n_expected, par->buflen );
760 ok ( n_sent == n_expected,
761 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
763 /* shutdown send direction */
764 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
767 read_zero_bytes ( mem->s );
768 trace ( "oob_client (%x) exiting\n", id );
773 * simple_mixed_client: mixing send and recvfrom
775 static VOID WINAPI simple_mixed_client ( client_params *par )
777 test_params *gen = par->general;
779 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
780 int fromLen = sizeof(mem->addr);
781 struct sockaddr test;
783 id = GetCurrentThreadId();
784 trace ( "simple_client (%x): starting\n", id );
785 /* wait here because we want to call set_so_opentype before creating a socket */
786 WaitForSingleObject ( server_ready, INFINITE );
787 trace ( "simple_client (%x): server ready\n", id );
789 check_so_opentype ();
790 set_so_opentype ( FALSE ); /* non-overlapped */
791 client_start ( par );
792 mem = TlsGetValue ( tls );
795 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
796 0 ==, "simple_client (%x): connect error: %d\n" );
797 ok ( set_blocking ( mem->s, TRUE ) == 0,
798 "simple_client (%x): failed to set blocking mode\n", id );
799 trace ( "simple_client (%x) connected\n", id );
801 /* send data to server */
802 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
803 ok ( n_sent == n_expected,
804 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
806 /* shutdown send direction */
807 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
809 /* this shouldn't change, since lpFrom, is not updated on
810 connection oriented sockets - exposed by bug 11640
812 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
814 /* Receive data echoed back & check it */
815 n_recvd = do_synchronous_recvfrom ( mem->s,
819 (struct sockaddr *)&test,
822 ok ( n_recvd == n_expected,
823 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
825 /* check that lpFrom was not updated */
828 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
829 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
832 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
833 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
836 read_zero_bytes ( mem->s );
837 trace ( "simple_client (%x) exiting\n", id );
842 * event_client: An event-driven client
844 static void WINAPI event_client ( client_params *par )
846 test_params *gen = par->general;
848 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
851 WSANETWORKEVENTS wsa_events;
852 char *send_last, *recv_last, *send_p, *recv_p;
853 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
855 trace ( "event_client (%x): starting\n", id );
856 client_start ( par );
857 trace ( "event_client (%x): server ready\n", id );
859 mem = TlsGetValue ( tls );
861 /* Prepare event notification for connect, makes socket nonblocking */
862 event = WSACreateEvent ();
863 WSAEventSelect ( mem->s, event, FD_CONNECT );
864 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
866 err = WSAGetLastError ();
867 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
868 tmp = WaitForSingleObject ( event, INFINITE );
869 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
870 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
871 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
872 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
873 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
877 trace ( "event_client (%x) connected\n", id );
879 WSAEventSelect ( mem->s, event, mask );
881 recv_p = mem->recv_buf;
882 recv_last = mem->recv_buf + n_expected;
883 send_p = mem->send_buf;
884 send_last = mem->send_buf + n_expected;
888 err = WaitForSingleObject ( event, INFINITE );
889 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
891 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
892 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
894 if ( wsa_events.lNetworkEvents & FD_WRITE )
896 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
897 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
902 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
905 err = WSAGetLastError ();
906 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
911 while ( n >= 0 && send_p < send_last );
913 if ( send_p == send_last )
915 trace ( "event_client (%x): all data sent - shutdown\n", id );
916 shutdown ( mem->s, SD_SEND );
918 WSAEventSelect ( mem->s, event, mask );
921 if ( wsa_events.lNetworkEvents & FD_READ )
923 err = wsa_events.iErrorCode[ FD_READ_BIT ];
924 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
925 if ( err != 0 ) break;
927 /* First read must succeed */
928 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
929 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
933 if ( recv_p == recv_last )
936 trace ( "event_client (%x): all data received\n", id );
937 WSAEventSelect ( mem->s, event, mask );
940 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
941 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
942 ok ( 0, "event_client (%x): read error: %d\n", id, err );
946 if ( wsa_events.lNetworkEvents & FD_CLOSE )
948 trace ( "event_client (%x): close event\n", id );
949 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
950 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
955 n = send_p - mem->send_buf;
956 ok ( send_p == send_last,
957 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
958 n = recv_p - mem->recv_buf;
959 ok ( recv_p == recv_last,
960 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
961 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
962 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
965 WSACloseEvent ( event );
966 trace ( "event_client (%x) exiting\n", id );
970 /* Tests for WSAStartup */
971 static void test_WithoutWSAStartup(void)
975 WSASetLastError(0xdeadbeef);
976 ptr = gethostbyname("localhost");
978 ok(ptr == NULL, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
979 ok(WSAGetLastError() == WSANOTINITIALISED, "gethostbyname() failed with unexpected error: %d\n",
983 static void test_WithWSAStartup(void)
986 WORD version = MAKEWORD( 2, 2 );
990 res = WSAStartup( version, &data );
991 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
993 ptr = gethostbyname("localhost");
994 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
999 /**************** Main program utility functions ***************/
1001 static void Init (void)
1003 WORD ver = MAKEWORD (2, 2);
1005 HMODULE hws2_32 = GetModuleHandle("ws2_32.dll");
1007 pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo");
1008 pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo");
1009 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
1010 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
1011 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
1013 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1017 static void Exit (void)
1022 err = WSAGetLastError();
1023 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1025 err = WSAGetLastError();
1026 ok ( (ret == SOCKET_ERROR && err == WSANOTINITIALISED) ||
1027 broken(ret == 0), /* WinME */
1028 "WSACleanup returned %d GetLastError is %d\n", ret, err);
1031 static void StartServer (LPTHREAD_START_ROUTINE routine,
1032 test_params *general, server_params *par)
1034 par->general = general;
1035 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1036 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1039 static void StartClients (LPTHREAD_START_ROUTINE routine,
1040 test_params *general, client_params *par)
1043 par->general = general;
1044 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1047 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1048 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1049 /* Make sure the client is up and running */
1050 WaitForSingleObject ( client_ready[client_id], INFINITE );
1054 static void do_test( test_setup *test )
1056 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1059 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1060 for (i = 0; i <= n; i++)
1061 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1063 StartServer ( test->srv, &test->general, &test->srv_params );
1064 StartClients ( test->clt, &test->general, &test->clt_params );
1065 WaitForSingleObject ( server_ready, INFINITE );
1067 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1068 ok ( wait <= WAIT_OBJECT_0 + n ,
1069 "some threads have not completed: %x\n", wait );
1071 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
1073 for (i = 0; i <= n; i++)
1075 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
1077 trace ("terminating thread %08x\n", thread_id[i]);
1078 TerminateThread ( thread [i], 0 );
1082 CloseHandle ( server_ready );
1083 for (i = 0; i <= n; i++)
1084 CloseHandle ( client_ready[i] );
1087 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1088 /* optname = SO_LINGER */
1089 static const LINGER linger_testvals[] = {
1096 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1097 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1098 bug in the linux kernel (fixed in 2.6.8) */
1099 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1101 static void test_set_getsockopt(void)
1104 int i, err, lasterr;
1109 s = socket(AF_INET, SOCK_STREAM, 0);
1110 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1111 if( s == INVALID_SOCKET) return;
1113 timeout = SOCKTIMEOUT1;
1114 size = sizeof(timeout);
1115 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1117 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1118 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1119 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1122 size = sizeof(timeout);
1123 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1125 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1126 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1127 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1130 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1131 size = sizeof(timeout);
1132 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1134 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1135 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1136 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1138 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1139 size = sizeof(lingval);
1140 lingval = linger_testvals[i];
1141 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1143 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1144 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1145 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1146 (lingval.l_linger == linger_testvals[i].l_linger ||
1147 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1148 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1149 lingval.l_onoff, lingval.l_linger,
1150 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1153 size = sizeof(lingval);
1154 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1155 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1156 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1157 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1158 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1159 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1161 size = sizeof(BOOL);
1162 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1163 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1164 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1165 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1166 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1167 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1169 /* Test for erroneously passing a value instead of a pointer as optval */
1170 size = sizeof(char);
1171 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1172 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1173 "instead of failing.\n");
1174 lasterr = WSAGetLastError();
1175 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1176 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1177 lasterr, WSAEFAULT);
1179 /* SO_RCVTIMEO with invalid values for level */
1180 size = sizeof(timeout);
1181 timeout = SOCKTIMEOUT1;
1182 SetLastError(0xdeadbeef);
1183 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1184 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1185 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1186 err, WSAGetLastError());
1188 timeout = SOCKTIMEOUT1;
1189 SetLastError(0xdeadbeef);
1190 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1191 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1192 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1193 err, WSAGetLastError());
1198 static void test_so_reuseaddr(void)
1200 struct sockaddr_in saddr;
1202 unsigned int rc,reuse;
1205 saddr.sin_family = AF_INET;
1206 saddr.sin_port = htons(9375);
1207 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1209 s1=socket(AF_INET, SOCK_STREAM, 0);
1210 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1211 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1212 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1214 s2=socket(AF_INET, SOCK_STREAM, 0);
1215 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1219 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1220 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1222 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1223 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1226 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1227 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1229 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1230 * a port immediately after closing another socket on that port, so
1231 * basically following the BSD socket semantics here. */
1233 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1234 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1239 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1241 static void test_ip_pktinfo(void)
1243 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
1244 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
1245 struct sockaddr_in s1addr, s2addr, s3addr;
1246 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
1247 LPFN_WSARECVMSG pWSARecvMsg = NULL;
1248 unsigned int rc, foundhdr, yes = 1;
1249 DWORD dwBytes, dwSize, dwFlags;
1258 memset(&ov, 0, sizeof(ov));
1259 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1260 if (ov.hEvent == INVALID_HANDLE_VALUE)
1262 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
1266 memset(&hdr, 0x00, sizeof(hdr));
1267 s1addr.sin_family = AF_INET;
1268 s1addr.sin_port = htons(0);
1269 /* Note: s1addr.sin_addr is set below */
1270 iovec[0].buf = recvbuf;
1271 iovec[0].len = sizeof(recvbuf);
1272 hdr.name = (struct sockaddr*)&s3addr;
1273 hdr.namelen = sizeof(s3addr);
1274 hdr.lpBuffers = &iovec[0];
1275 hdr.dwBufferCount = 1;
1276 hdr.Control.buf = pktbuf;
1277 /* Note: hdr.Control.len is set below */
1280 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++)
1282 s1addr.sin_addr.s_addr = addresses[i];
1284 /* Build "server" side socket */
1285 s1=socket(AF_INET, SOCK_DGRAM, 0);
1286 if (s1 == INVALID_SOCKET)
1288 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1292 /* Obtain the WSARecvMsg function */
1293 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
1294 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
1297 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
1302 /* Setup the server side socket */
1303 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
1304 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
1305 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
1306 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1308 /* Build "client" side socket */
1309 addrlen = sizeof(s2addr);
1310 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0)
1312 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
1316 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
1317 s2=socket(AF_INET, SOCK_DGRAM, 0);
1318 if (s2 == INVALID_SOCKET)
1320 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1325 /* Test an empty message header */
1326 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
1327 err=WSAGetLastError();
1328 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
1331 * Send a packet from the client to the server and test for specifying
1332 * a short control header.
1334 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1335 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1336 hdr.Control.len = 1;
1337 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1338 err=WSAGetLastError();
1339 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
1340 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
1341 hdr.dwFlags = 0; /* Reset flags */
1343 /* Perform another short control header test, this time with an overlapped receive */
1344 hdr.Control.len = 1;
1345 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1346 err=WSAGetLastError();
1347 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1348 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1349 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1350 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1352 skip("Server side did not receive packet, some tests skipped.\n");
1358 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
1360 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
1361 ok(hdr.dwFlags == MSG_CTRUNC,
1362 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
1363 hdr.dwFlags = 0; /* Reset flags */
1366 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1367 * on the server end and check that the returned packet matches what was sent.
1369 hdr.Control.len = sizeof(pktbuf);
1370 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1371 err=WSAGetLastError();
1372 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1373 ok(hdr.Control.len == sizeof(pktbuf),
1374 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
1375 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1376 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1377 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0)
1379 skip("Server side did not receive packet, some tests skipped.\n");
1385 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
1386 ok(dwSize == sizeof(msg),
1387 "WSARecvMsg() buffer length does not match transmitted data!\n");
1388 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
1389 "WSARecvMsg() buffer does not match transmitted data!\n");
1390 ok(hdr.Control.len == IP_PKTINFO_LEN,
1391 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
1393 /* Test for the expected IP_PKTINFO return information. */
1395 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
1397 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
1399 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
1401 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
1405 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
1412 CloseHandle(ov.hEvent);
1415 /************* Array containing the tests to run **********/
1417 #define STD_STREAM_SOCKET \
1423 static test_setup tests [] =
1425 /* Test 0: synchronous client and server */
1446 /* Test 1: event-driven client, synchronous server */
1463 WSA_FLAG_OVERLAPPED,
1467 /* Test 2: synchronous client, non-blocking server via select() */
1488 /* Test 3: OOB client, OOB server */
1509 /* Test 4: synchronous mixed client and server */
1523 simple_mixed_client,
1532 static void test_UDP(void)
1534 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1535 possible that this test fails due to dropped packets. */
1537 /* peer 0 receives data from all other peers */
1538 struct sock_info peer[NUM_UDP_PEERS];
1540 int ss, i, n_recv, n_sent;
1542 memset (buf,0,sizeof(buf));
1543 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1544 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1546 peer[i].addr.sin_family = AF_INET;
1547 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1550 peer[i].addr.sin_port = htons ( SERVERPORT );
1552 peer[i].addr.sin_port = htons ( 0 );
1555 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1557 /* test getsockname() to get peer's port */
1558 ss = sizeof ( peer[i].addr );
1559 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1560 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1563 /* test getsockname() */
1564 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1566 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1567 /* send client's ip */
1568 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1569 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1570 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1573 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1574 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1575 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1576 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1580 static DWORD WINAPI do_getservbyname( void *param )
1586 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1588 HANDLE *starttest = param;
1590 struct servent *pserv[2];
1592 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1594 /* ensure that necessary buffer resizes are completed */
1595 for ( j = 0; j < 2; j++) {
1596 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1599 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1600 for ( j = 0; j < 2; j++ ) {
1601 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1602 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1603 if ( !pserv[j] ) continue;
1604 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) );
1605 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 );
1606 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 );
1609 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1615 static void test_getservbyname(void)
1618 HANDLE starttest, thread[NUM_THREADS];
1619 DWORD thread_id[NUM_THREADS];
1621 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1623 /* create threads */
1624 for ( i = 0; i < NUM_THREADS; i++ ) {
1625 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] );
1628 /* signal threads to start */
1629 SetEvent ( starttest );
1631 for ( i = 0; i < NUM_THREADS; i++) {
1632 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1636 static void test_WSASocket(void)
1638 SOCKET sock = INVALID_SOCKET;
1639 WSAPROTOCOL_INFOA *pi;
1640 int providers[] = {6, 0};
1644 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1645 * to avoid a crash on win98.
1648 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1649 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1651 err = WSAGetLastError();
1652 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1655 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1656 ok(pi != NULL, "Failed to allocate memory\n");
1658 skip("Can't continue without memory.\n");
1662 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1663 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1667 skip("No protocols enumerated.\n");
1668 HeapFree(GetProcessHeap(), 0, pi);
1672 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1673 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1674 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1678 HeapFree(GetProcessHeap(), 0, pi);
1681 static void test_WSAAddressToStringA(void)
1683 SOCKET v6 = INVALID_SOCKET;
1687 SOCKADDR_IN sockaddr;
1688 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1690 CHAR expect1[] = "0.0.0.0";
1691 CHAR expect2[] = "255.255.255.255";
1692 CHAR expect3[] = "0.0.0.0:65535";
1693 CHAR expect4[] = "255.255.255.255:65535";
1695 SOCKADDR_IN6 sockaddr6;
1696 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1698 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1699 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1700 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1702 CHAR expect6_1[] = "::1";
1703 CHAR expect6_2[] = "20ab::1";
1704 CHAR expect6_3[] = "[20ab::2001]:33274";
1705 CHAR expect6_3_nt[] = "20ab::2001@33274";
1706 CHAR expect6_3_w2k[] = "20ab::2001";
1707 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
1708 CHAR expect6_3_2_nt[] = "4660/20ab::2001@33274";
1709 CHAR expect6_3_2_w2k[] = "20ab::2001%4660";
1710 CHAR expect6_3_3[] = "20ab::2001%4660";
1711 CHAR expect6_3_3_nt[] = "4660/20ab::2001";
1715 sockaddr.sin_family = AF_INET;
1716 sockaddr.sin_port = 0;
1717 sockaddr.sin_addr.s_addr = 0;
1719 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1720 GLE = WSAGetLastError();
1721 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1722 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1725 len = sizeof(address);
1727 sockaddr.sin_family = AF_INET;
1728 sockaddr.sin_port = 0;
1729 sockaddr.sin_addr.s_addr = 0;
1731 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1732 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1734 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1735 ok( len == sizeof( expect1 ), "Got size %d\n", len);
1737 len = sizeof(address);
1739 sockaddr.sin_family = AF_INET;
1740 sockaddr.sin_port = 0;
1741 sockaddr.sin_addr.s_addr = 0xffffffff;
1743 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1744 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1746 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1748 len = sizeof(address);
1750 sockaddr.sin_family = AF_INET;
1751 sockaddr.sin_port = 0xffff;
1752 sockaddr.sin_addr.s_addr = 0;
1754 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1755 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1757 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1759 len = sizeof(address);
1761 sockaddr.sin_family = AF_INET;
1762 sockaddr.sin_port = 0xffff;
1763 sockaddr.sin_addr.s_addr = 0xffffffff;
1765 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1766 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1768 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1769 ok( len == sizeof( expect4 ), "Got size %d\n", len);
1771 /*check to see it IPv6 is available */
1772 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1773 if (v6 == INVALID_SOCKET) {
1774 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1775 WSAGetLastError(), WSAEAFNOSUPPORT);
1778 /* Test a short IPv6 address */
1779 len = sizeof(address6);
1781 sockaddr6.sin6_family = AF_INET6;
1782 sockaddr6.sin6_port = 0x0000;
1783 sockaddr6.sin6_scope_id = 0;
1784 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1786 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1787 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1788 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
1789 ok( len == sizeof(expect6_1), "Got size %d\n", len);
1791 /* Test a longer IPv6 address */
1792 len = sizeof(address6);
1794 sockaddr6.sin6_family = AF_INET6;
1795 sockaddr6.sin6_port = 0x0000;
1796 sockaddr6.sin6_scope_id = 0;
1797 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1799 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1800 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1801 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
1802 ok( len == sizeof(expect6_2), "Got size %d\n", len);
1804 /* Test IPv6 address and port number */
1805 len = sizeof(address6);
1807 sockaddr6.sin6_family = AF_INET6;
1808 sockaddr6.sin6_port = 0xfa81;
1809 sockaddr6.sin6_scope_id = 0;
1810 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1812 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1813 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1814 ok( !strcmp( address6, expect6_3 ) ||
1815 broken( !strcmp( address6, expect6_3_nt ) ) || /* NT4 */
1816 broken( !strcmp( address6, expect6_3_w2k ) ), /* Win2000 */
1817 "Expected: %s, got: %s\n", expect6_3, address6 );
1818 ok( len == sizeof(expect6_3) ||
1819 broken( len == sizeof(expect6_3_nt) ) || /* NT4 */
1820 broken( len == sizeof(expect6_3_w2k) ), /* Win2000 */
1821 "Got size %d\n", len);
1823 /* Test IPv6 address, port number and scope_id */
1824 len = sizeof(address6);
1826 sockaddr6.sin6_family = AF_INET6;
1827 sockaddr6.sin6_port = 0xfa81;
1828 sockaddr6.sin6_scope_id = 0x1234;
1829 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1831 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1832 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1833 ok( !strcmp( address6, expect6_3_2 ) ||
1834 broken( !strcmp( address6, expect6_3_2_nt ) ) || /* NT4 */
1835 broken( !strcmp( address6, expect6_3_2_w2k ) ), /* Win2000 */
1836 "Expected: %s, got: %s\n", expect6_3_2, address6 );
1837 ok( len == sizeof(expect6_3_2) ||
1838 broken( len == sizeof(expect6_3_2_nt) ) || /* NT4 */
1839 broken( len == sizeof(expect6_3_2_w2k) ), /* Win2000 */
1840 "Got size %d\n", len);
1842 /* Test IPv6 address and scope_id */
1843 len = sizeof(address6);
1845 sockaddr6.sin6_family = AF_INET6;
1846 sockaddr6.sin6_port = 0x0000;
1847 sockaddr6.sin6_scope_id = 0x1234;
1848 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1850 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1851 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1852 ok( !strcmp( address6, expect6_3_3 ) ||
1853 broken( !strcmp( address6, expect6_3_3_nt ) ), /* NT4 */
1854 "Expected: %s, got: %s\n", expect6_3_3, address6 );
1855 ok( len == sizeof(expect6_3_3) ||
1856 broken( len == sizeof(expect6_3_3_nt) ), /* NT4 */
1857 "Got size %d\n", len);
1860 if (v6 != INVALID_SOCKET)
1864 static void test_WSAAddressToStringW(void)
1866 SOCKET v6 = INVALID_SOCKET;
1870 SOCKADDR_IN sockaddr;
1871 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1873 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1874 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1875 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1876 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1877 '6', '5', '5', '3', '5', 0 };
1879 SOCKADDR_IN6 sockaddr6;
1880 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1882 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1883 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1884 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1886 WCHAR expect6_1[] = {':',':','1',0};
1887 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
1888 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1889 WCHAR expect6_3_nt[] = {'2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1890 WCHAR expect6_3_w2k[] = {'2','0','a','b',':',':','2','0','0','1',0};
1891 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1892 WCHAR expect6_3_2_nt[] = {'4','6','6','0','/','2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1893 WCHAR expect6_3_2_w2k[] = {'2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',0};
1894 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1895 WCHAR expect6_3_3_nt[] = {'6','5','5','3','4','/','2','0','a','b',':',':','2','0','0','1',0};
1899 sockaddr.sin_family = AF_INET;
1900 sockaddr.sin_port = 0;
1901 sockaddr.sin_addr.s_addr = 0;
1903 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1904 GLE = WSAGetLastError();
1905 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1906 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1909 len = sizeof(address);
1911 sockaddr.sin_family = AF_INET;
1912 sockaddr.sin_port = 0;
1913 sockaddr.sin_addr.s_addr = 0;
1915 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1916 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1918 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1919 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
1921 len = sizeof(address);
1923 sockaddr.sin_family = AF_INET;
1924 sockaddr.sin_port = 0;
1925 sockaddr.sin_addr.s_addr = 0xffffffff;
1927 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1928 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1930 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1932 len = sizeof(address);
1934 sockaddr.sin_family = AF_INET;
1935 sockaddr.sin_port = 0xffff;
1936 sockaddr.sin_addr.s_addr = 0;
1938 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1939 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1941 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1943 len = sizeof(address);
1945 sockaddr.sin_family = AF_INET;
1946 sockaddr.sin_port = 0xffff;
1947 sockaddr.sin_addr.s_addr = 0xffffffff;
1949 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1950 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1952 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1953 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len);
1955 /*check to see it IPv6 is available */
1956 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1957 if (v6 == INVALID_SOCKET) {
1958 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1959 WSAGetLastError(), WSAEAFNOSUPPORT);
1963 /* Test a short IPv6 address */
1964 len = sizeof(address6)/sizeof(WCHAR);
1966 sockaddr6.sin6_family = AF_INET6;
1967 sockaddr6.sin6_port = 0x0000;
1968 sockaddr6.sin6_scope_id = 0;
1969 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1971 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1972 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1973 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
1974 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
1976 /* Test a longer IPv6 address */
1977 len = sizeof(address6)/sizeof(WCHAR);
1979 sockaddr6.sin6_family = AF_INET6;
1980 sockaddr6.sin6_port = 0x0000;
1981 sockaddr6.sin6_scope_id = 0;
1982 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1984 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1985 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1987 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
1988 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
1990 /* Test IPv6 address and port number */
1991 len = sizeof(address6)/sizeof(WCHAR);
1993 sockaddr6.sin6_family = AF_INET6;
1994 sockaddr6.sin6_port = 0xfa81;
1995 sockaddr6.sin6_scope_id = 0;
1996 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1998 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1999 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2000 ok( !lstrcmpW( address6, expect6_3 ) ||
2001 broken( !lstrcmpW( address6, expect6_3_nt ) ) || /* NT4 */
2002 broken( !lstrcmpW( address6, expect6_3_w2k ) ), /* Win2000 */
2003 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3),
2004 wine_dbgstr_w(address6) );
2005 ok( len == sizeof(expect6_3)/sizeof(WCHAR) ||
2006 broken(len == sizeof(expect6_3_nt)/sizeof(WCHAR) ) || /* NT4 */
2007 broken(len == sizeof(expect6_3_w2k)/sizeof(WCHAR) ), /* Win2000 */
2010 /* Test IPv6 address, port number and scope_id */
2011 len = sizeof(address6)/sizeof(WCHAR);
2013 sockaddr6.sin6_family = AF_INET6;
2014 sockaddr6.sin6_port = 0xfa81;
2015 sockaddr6.sin6_scope_id = 0x1234;
2016 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
2018 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
2019 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2020 ok( !lstrcmpW( address6, expect6_3_2 ) ||
2021 broken( !lstrcmpW( address6, expect6_3_2_nt ) ) || /* NT4 */
2022 broken( !lstrcmpW( address6, expect6_3_2_w2k ) ), /* Win2000 */
2023 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2),
2024 wine_dbgstr_w(address6) );
2025 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR) ||
2026 broken( len == sizeof(expect6_3_2_nt)/sizeof(WCHAR) ) || /* NT4 */
2027 broken( len == sizeof(expect6_3_2_w2k)/sizeof(WCHAR) ), /* Win2000 */
2030 /* Test IPv6 address and scope_id */
2031 len = sizeof(address6)/sizeof(WCHAR);
2033 sockaddr6.sin6_family = AF_INET6;
2034 sockaddr6.sin6_port = 0x0000;
2035 sockaddr6.sin6_scope_id = 0xfffe;
2036 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
2038 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
2039 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2040 ok( !lstrcmpW( address6, expect6_3_3 ) ||
2041 broken( !lstrcmpW( address6, expect6_3_3_nt ) ), /* NT4 */
2042 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3),
2043 wine_dbgstr_w(address6) );
2044 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR) ||
2045 broken( len == sizeof(expect6_3_3_nt)/sizeof(WCHAR) ), /* NT4 */
2049 if (v6 != INVALID_SOCKET)
2053 static void test_WSAStringToAddressA(void)
2056 SOCKADDR_IN sockaddr;
2057 SOCKADDR_IN6 sockaddr6;
2060 CHAR address1[] = "0.0.0.0";
2061 CHAR address2[] = "127.127.127.127";
2062 CHAR address3[] = "255.255.255.255";
2063 CHAR address4[] = "127.127.127.127:65535";
2064 CHAR address5[] = "255.255.255.255:65535";
2065 CHAR address6[] = "::1";
2066 CHAR address7[] = "[::1]";
2067 CHAR address8[] = "[::1]:65535";
2070 sockaddr.sin_family = AF_INET;
2072 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2073 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
2074 WSAGetLastError() );
2076 len = sizeof(sockaddr);
2077 sockaddr.sin_port = 0;
2078 sockaddr.sin_addr.s_addr = 0;
2080 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2081 ok( !ret && sockaddr.sin_addr.s_addr == 0,
2082 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2084 len = sizeof(sockaddr);
2085 sockaddr.sin_port = 0;
2086 sockaddr.sin_addr.s_addr = 0;
2088 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2089 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
2090 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2092 len = sizeof(sockaddr);
2093 sockaddr.sin_port = 0;
2094 sockaddr.sin_addr.s_addr = 0;
2096 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2097 GLE = WSAGetLastError();
2098 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
2099 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2100 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
2102 len = sizeof(sockaddr);
2103 sockaddr.sin_port = 0;
2104 sockaddr.sin_addr.s_addr = 0;
2106 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2107 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
2108 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
2110 len = sizeof(sockaddr);
2111 sockaddr.sin_port = 0;
2112 sockaddr.sin_addr.s_addr = 0;
2114 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2115 GLE = WSAGetLastError();
2116 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
2117 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2118 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
2120 len = sizeof(sockaddr6);
2121 memset(&sockaddr6, 0, len);
2122 sockaddr6.sin6_family = AF_INET6;
2124 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2126 GLE = WSAGetLastError();
2127 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2128 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2130 len = sizeof(sockaddr6);
2131 memset(&sockaddr6, 0, len);
2132 sockaddr6.sin6_family = AF_INET6;
2134 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2136 GLE = WSAGetLastError();
2137 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2138 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2140 len = sizeof(sockaddr6);
2141 memset(&sockaddr6, 0, len);
2142 sockaddr6.sin6_family = AF_INET6;
2144 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2146 GLE = WSAGetLastError();
2147 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
2148 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2149 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
2153 static void test_WSAStringToAddressW(void)
2156 SOCKADDR_IN sockaddr, *sin;
2157 SOCKADDR_IN6 sockaddr6;
2158 SOCKADDR_STORAGE sockaddr_storage;
2161 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
2162 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
2163 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
2164 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
2165 ':', '6', '5', '5', '3', '5', 0 };
2166 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
2167 '6', '5', '5', '3', '5', 0 };
2168 WCHAR address6[] = {':',':','1','\0'};
2169 WCHAR address7[] = {'[',':',':','1',']','\0'};
2170 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
2173 sockaddr.sin_family = AF_INET;
2175 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2176 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
2177 WSAGetLastError() );
2179 len = sizeof(sockaddr);
2180 sockaddr.sin_port = 0;
2181 sockaddr.sin_addr.s_addr = 0;
2183 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2184 ok( !ret && sockaddr.sin_addr.s_addr == 0,
2185 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2187 len = sizeof(sockaddr);
2188 sockaddr.sin_port = 0;
2189 sockaddr.sin_addr.s_addr = 0;
2191 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2192 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
2193 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2195 len = sizeof(sockaddr);
2196 sockaddr.sin_port = 0;
2197 sockaddr.sin_addr.s_addr = 0;
2199 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2200 GLE = WSAGetLastError();
2201 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
2202 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2203 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2205 len = sizeof(sockaddr);
2206 sockaddr.sin_port = 0;
2207 sockaddr.sin_addr.s_addr = 0;
2209 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2210 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
2211 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2213 len = sizeof(sockaddr);
2214 sockaddr.sin_port = 0;
2215 sockaddr.sin_addr.s_addr = 0;
2217 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
2218 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
2219 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2220 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2222 /* Test with a larger buffer than necessary */
2223 len = sizeof(sockaddr_storage);
2224 sin = (SOCKADDR_IN *)&sockaddr_storage;
2226 sin->sin_addr.s_addr = 0;
2228 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)sin, &len );
2229 ok( (ret == 0 && sin->sin_addr.s_addr == 0xffffffff && sin->sin_port == 0xffff) ||
2230 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
2231 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
2232 ok( len == sizeof(SOCKADDR_IN) ||
2233 broken(len == sizeof(SOCKADDR_STORAGE)) /* NT4/2k */,
2234 "unexpected length %d\n", len );
2236 len = sizeof(sockaddr6);
2237 memset(&sockaddr6, 0, len);
2238 sockaddr6.sin6_family = AF_INET6;
2240 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2242 GLE = WSAGetLastError();
2243 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2244 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2246 len = sizeof(sockaddr6);
2247 memset(&sockaddr6, 0, len);
2248 sockaddr6.sin6_family = AF_INET6;
2250 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2252 GLE = WSAGetLastError();
2253 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2254 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2256 len = sizeof(sockaddr6);
2257 memset(&sockaddr6, 0, len);
2258 sockaddr6.sin6_family = AF_INET6;
2260 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
2262 GLE = WSAGetLastError();
2263 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
2264 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
2265 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
2269 static DWORD WINAPI SelectReadThread(void *param)
2271 select_thread_params *par = param;
2274 struct sockaddr_in addr;
2275 struct timeval select_timeout;
2278 FD_SET(par->s, &readfds);
2279 select_timeout.tv_sec=5;
2280 select_timeout.tv_usec=0;
2281 addr.sin_family = AF_INET;
2282 addr.sin_addr.s_addr = inet_addr(SERVERIP);
2283 addr.sin_port = htons(SERVERPORT);
2285 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
2286 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
2288 SetEvent(server_ready);
2289 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
2290 par->ReadKilled = (ret == 1);
2295 static void test_errors(void)
2298 SOCKADDR_IN SockAddr;
2301 WSASetLastError(NO_ERROR);
2302 sock = socket(PF_INET, SOCK_STREAM, 0);
2303 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2304 memset(&SockAddr, 0, sizeof(SockAddr));
2305 SockAddr.sin_family = AF_INET;
2306 SockAddr.sin_port = htons(6924);
2307 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
2309 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
2310 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret );
2311 if (ret == SOCKET_ERROR)
2313 err = WSAGetLastError();
2314 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err );
2319 fd_set set = {1, {sock}};
2322 timeval.tv_usec = 50000;
2324 ret = select(1, NULL, &set, NULL, &timeval);
2325 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret );
2328 ret = closesocket(sock);
2329 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
2332 static void test_select(void)
2334 SOCKET fdRead, fdWrite;
2335 fd_set readfds, writefds, exceptfds;
2339 struct timeval select_timeout;
2340 select_thread_params thread_params;
2341 HANDLE thread_handle;
2344 fdRead = socket(AF_INET, SOCK_STREAM, 0);
2345 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2346 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
2347 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2351 FD_ZERO(&exceptfds);
2352 FD_SET(fdRead, &readfds);
2353 FD_SET(fdWrite, &writefds);
2354 FD_SET(fdRead, &exceptfds);
2355 FD_SET(fdWrite, &exceptfds);
2356 select_timeout.tv_sec=0;
2357 select_timeout.tv_usec=500;
2360 if (fdWrite > maxfd)
2364 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2365 ok ( (ret == 0), "select should not return any socket handles\n");
2366 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2368 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
2370 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2371 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
2374 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
2376 ret = closesocket(fdWrite);
2377 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2379 thread_params.s = fdRead;
2380 thread_params.ReadKilled = FALSE;
2381 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2382 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id );
2383 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
2385 WaitForSingleObject (server_ready, INFINITE);
2387 ret = closesocket(fdRead);
2388 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2390 WaitForSingleObject (thread_handle, 1000);
2391 ok ( (thread_params.ReadKilled) ||
2392 broken(thread_params.ReadKilled == 0), /*Win98*/
2393 "closesocket did not wakeup select\n");
2394 ret = recv(fdRead, &buffer, 1, MSG_PEEK);
2395 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret);
2397 /* Test selecting invalid handles */
2400 FD_ZERO(&exceptfds);
2403 ret = select(maxfd+1, 0, 0, 0, &select_timeout);
2404 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2405 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2408 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2409 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2410 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2412 FD_SET(INVALID_SOCKET, &readfds);
2414 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2415 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2416 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2417 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2420 FD_SET(INVALID_SOCKET, &writefds);
2422 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2423 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2424 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2425 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n");
2428 FD_SET(INVALID_SOCKET, &exceptfds);
2430 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2431 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2432 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2433 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2436 static DWORD WINAPI AcceptKillThread(void *param)
2438 select_thread_params *par = param;
2439 struct sockaddr_in address;
2440 int len = sizeof(address);
2441 SOCKET client_socket;
2443 SetEvent(server_ready);
2444 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
2445 if (client_socket != INVALID_SOCKET)
2446 closesocket(client_socket);
2447 par->ReadKilled = (client_socket == INVALID_SOCKET);
2452 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
2453 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
2454 GROUP FAR * g, DWORD_PTR dwCallbackData)
2459 static void test_accept(void)
2462 SOCKET server_socket = INVALID_SOCKET, accepted = INVALID_SOCKET, connector = INVALID_SOCKET;
2463 struct sockaddr_in address;
2465 select_thread_params thread_params;
2466 HANDLE thread_handle = NULL;
2469 server_socket = socket(AF_INET, SOCK_STREAM, 0);
2470 if (server_socket == INVALID_SOCKET)
2472 trace("error creating server socket: %d\n", WSAGetLastError());
2476 memset(&address, 0, sizeof(address));
2477 address.sin_addr.s_addr = inet_addr("127.0.0.1");
2478 address.sin_family = AF_INET;
2479 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
2482 trace("error binding server socket: %d\n", WSAGetLastError());
2486 socklen = sizeof(address);
2487 ret = getsockname(server_socket, (struct sockaddr*)&address, &socklen);
2489 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2493 ret = listen(server_socket, 5);
2496 trace("error making server socket listen: %d\n", WSAGetLastError());
2500 trace("Blocking accept next\n");
2502 connector = socket(AF_INET, SOCK_STREAM, 0);
2503 ok(connector != INVALID_SOCKET, "Failed to create connector socket, error %d\n", WSAGetLastError());
2505 ret = connect(connector, (struct sockaddr*)&address, sizeof(address));
2506 ok(ret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2508 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0);
2509 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
2511 accepted = accept(server_socket, NULL, 0);
2512 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2514 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2515 if (server_ready == INVALID_HANDLE_VALUE)
2517 trace("error creating event: %d\n", GetLastError());
2521 thread_params.s = server_socket;
2522 thread_params.ReadKilled = FALSE;
2523 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id);
2524 if (thread_handle == NULL)
2526 trace("error creating thread: %d\n", GetLastError());
2530 WaitForSingleObject(server_ready, INFINITE);
2532 ret = closesocket(server_socket);
2535 trace("closesocket failed: %d\n", WSAGetLastError());
2539 WaitForSingleObject(thread_handle, 1000);
2540 ok(thread_params.ReadKilled || broken(!thread_params.ReadKilled) /* Win98/ME, after accept */,
2541 "closesocket did not wakeup accept\n");
2544 if (accepted != INVALID_SOCKET)
2545 closesocket(accepted);
2546 if (connector != INVALID_SOCKET)
2547 closesocket(connector);
2548 if (thread_handle != NULL)
2549 CloseHandle(thread_handle);
2550 if (server_ready != INVALID_HANDLE_VALUE)
2551 CloseHandle(server_ready);
2552 if (server_socket != INVALID_SOCKET)
2553 closesocket(server_socket);
2556 static void test_extendedSocketOptions(void)
2560 struct sockaddr_in sa;
2561 int sa_len = sizeof(struct sockaddr_in);
2562 int optval, optlen = sizeof(int), ret;
2566 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2567 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2571 memset(&sa, 0, sa_len);
2573 sa.sin_family = AF_INET;
2574 sa.sin_port = htons(0);
2575 sa.sin_addr.s_addr = htonl(INADDR_ANY);
2577 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
2578 trace("Creating the socket failed: %d\n", WSAGetLastError());
2583 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2584 trace("Failed to bind socket: %d\n", WSAGetLastError());
2590 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2592 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
2593 ok((optval == 65507) || (optval == 65527),
2594 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
2596 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2597 SetLastError(0xdeadbeef);
2598 optval = 0xdeadbeef;
2599 optlen = sizeof(int);
2600 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2601 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2602 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2603 ret, WSAGetLastError(), optval, optval);
2605 /* more invalid values for level */
2606 SetLastError(0xdeadbeef);
2607 optval = 0xdeadbeef;
2608 optlen = sizeof(int);
2609 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2610 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2611 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2612 ret, WSAGetLastError(), optval, optval);
2614 SetLastError(0xdeadbeef);
2615 optval = 0xdeadbeef;
2616 optlen = sizeof(int);
2617 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2618 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2619 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2620 ret, WSAGetLastError(), optval, optval);
2622 SetLastError(0xdeadbeef);
2623 optval = 0xdeadbeef;
2624 optlen = sizeof(int);
2625 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2626 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2627 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2628 ret, WSAGetLastError(), optval, optval);
2630 SetLastError(0xdeadbeef);
2631 optval = 0xdeadbeef;
2632 optlen = sizeof(int);
2633 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2634 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2635 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2636 ret, WSAGetLastError(), optval, optval);
2638 SetLastError(0xdeadbeef);
2639 optlen = sizeof(LINGER);
2640 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2641 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT),
2642 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
2643 ret, WSAGetLastError());
2646 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2647 trace("Creating the socket failed: %d\n", WSAGetLastError());
2652 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2653 trace("Failed to bind socket: %d\n", WSAGetLastError());
2659 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2660 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
2662 optlen = sizeof(BOOL);
2663 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
2664 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
2665 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
2666 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2667 bool_opt_val, linger_val.l_onoff);
2673 static void test_getsockname(void)
2677 struct sockaddr_in sa_set, sa_get;
2678 int sa_set_len = sizeof(struct sockaddr_in);
2679 int sa_get_len = sa_set_len;
2680 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
2683 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2684 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2688 memset(&sa_set, 0, sa_set_len);
2690 sa_set.sin_family = AF_INET;
2691 sa_set.sin_port = htons(0);
2692 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
2694 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2695 trace("Creating the socket failed: %d\n", WSAGetLastError());
2700 memcpy(&sa_get, &sa_set, sizeof(sa_set));
2701 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
2702 ok(0, "getsockname on unbound socket should fail\n");
2704 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
2705 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
2706 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
2707 "failed getsockname modified sockaddr when it shouldn't\n");
2710 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
2711 trace("Failed to bind socket: %d\n", WSAGetLastError());
2717 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
2718 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2724 ret = memcmp(sa_get.sin_zero, null_padding, 8);
2725 ok(ret == 0 || broken(ret != 0), /* NT4 */
2726 "getsockname did not zero the sockaddr_in structure\n");
2732 static void test_dns(void)
2743 h = gethostbyname("");
2744 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
2746 /* Use an address with valid alias names if possible */
2747 h = gethostbyname("source.winehq.org");
2750 skip("Can't test the hostent structure because gethostbyname failed\n");
2754 /* The returned struct must be allocated in a very strict way. First we need to
2755 * count how many aliases there are because they must be located right after
2756 * the struct hostent size. Knowing the amount of aliases we know the exact
2757 * location of the first IP returned. Rule valid for >= XP, for older OS's
2758 * it's somewhat the opposite. */
2760 if(h->h_addr_list == addr.mem) /* <= W2K */
2762 skip("Skipping hostent tests since this OS is unsupported\n");
2766 todo_wine ok(h->h_aliases == addr.mem,
2767 "hostent->h_aliases should be in %p, it is in %p\n", addr.mem, h->h_aliases);
2769 for(ptr = h->h_aliases, acount = 1; *ptr; ptr++) acount++;
2770 addr.chr += sizeof(*ptr) * acount;
2771 todo_wine ok(h->h_addr_list == addr.mem,
2772 "hostent->h_addr_list should be in %p, it is in %p\n", addr.mem, h->h_addr_list);
2774 for(ptr = h->h_addr_list, acount = 1; *ptr; ptr++) acount++;
2776 addr.chr += sizeof(*ptr) * acount;
2777 todo_wine ok(h->h_addr_list[0] == addr.mem,
2778 "hostent->h_addr_list[0] should be in %p, it is in %p\n", addr.mem, h->h_addr_list[0]);
2781 /* Our winsock headers don't define gethostname because it conflicts with the
2782 * definition in unistd.h. Define it here to get rid of the warning. */
2784 int WINAPI gethostname(char *name, int namelen);
2786 static void test_gethostbyname_hack(void)
2790 static BYTE loopback[] = {127, 0, 0, 1};
2791 static BYTE magic_loopback[] = {127, 12, 34, 56};
2794 ret = gethostname(name, 256);
2795 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
2797 he = gethostbyname("localhost");
2798 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
2801 if(he->h_length != 4)
2803 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2807 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
2808 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2809 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
2810 he->h_addr_list[0][3]);
2813 if(strcmp(name, "localhost") == 0)
2815 skip("hostname seems to be \"localhost\", skipping test.\n");
2820 he = gethostbyname(name);
2821 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
2824 if(he->h_length != 4)
2826 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2830 if (he->h_addr_list[0][0] == 127)
2832 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
2833 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2834 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
2835 he->h_addr_list[0][2], he->h_addr_list[0][3]);
2839 gethostbyname("nonexistent.winehq.org");
2840 /* Don't check for the return value, as some braindead ISPs will kindly
2841 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2844 static void test_inet_addr(void)
2848 addr = inet_addr(NULL);
2849 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
2852 static void test_addr_to_print(void)
2858 struct in6_addr in6;
2860 u_long addr0_Num = 0x00000000;
2861 PCSTR addr0_Str = "0.0.0.0";
2862 u_long addr1_Num = 0x20201015;
2863 PCSTR addr1_Str = "21.16.32.32";
2864 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2865 PCSTR addr2_Str = "::fffe:cc98:bd74";
2866 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
2867 PCSTR addr3_Str = "2030:a4b1::";
2868 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2869 PCSTR addr4_Str = "::204.152.189.116";
2871 /* Test IPv4 addresses */
2872 in.s_addr = addr0_Num;
2874 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2875 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2876 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
2878 /* Test that inet_ntoa and inet_ntop return the same value */
2879 in.S_un.S_addr = addr1_Num;
2880 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2881 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2882 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2884 /* InetNtop became available in Vista and Win2008 */
2887 win_skip("InetNtop not present, not executing tests\n");
2891 /* Second part of test */
2892 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
2893 ok(pdst != NULL, "InetNtop failed %s\n", dst);
2894 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2896 /* Test invalid parm conditions */
2897 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
2898 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2899 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
2901 /* Test Null destination */
2903 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, NULL, sizeof(dst));
2904 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2905 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2906 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2908 /* Test zero length passed */
2911 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 0);
2912 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2913 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2914 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2916 /* Test length one shorter than the address length */
2919 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 6);
2920 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2921 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2922 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2924 /* Test longer length is ok */
2927 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, sizeof(dst)+1);
2928 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2929 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2931 /* Test the IPv6 addresses */
2933 /* Test an zero prefixed IPV6 address */
2934 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2935 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2936 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2937 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
2939 /* Test an zero suffixed IPV6 address */
2940 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
2941 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2942 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2943 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
2945 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2946 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num));
2947 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, sizeof(dst6));
2948 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2949 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str);
2951 /* Test invalid parm conditions */
2952 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2954 /* Test Null destination */
2956 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, NULL, sizeof(dst6));
2957 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2958 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2959 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2961 /* Test zero length passed */
2964 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 0);
2965 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2966 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2967 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2969 /* Test length one shorter than the address length */
2972 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 16);
2973 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2974 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2975 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2977 /* Test longer length is ok */
2980 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 18);
2981 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2984 static void test_ioctlsocket(void)
2987 struct tcp_keepalive kalive;
2989 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2993 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2994 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2995 if(sock == INVALID_SOCKET)
2997 skip("Can't continue without a socket.\n");
3001 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
3003 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
3004 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
3005 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
3006 ret = WSAGetLastError();
3007 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
3010 /* A fresh and not connected socket has no urgent data, this test shows
3011 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
3013 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3014 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
3015 ok(arg, "SIOCATMARK expected a non-zero value\n");
3017 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
3019 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
3020 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
3022 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3023 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
3024 ok(arg, "SIOCATMARK expected a non-zero value\n");
3026 /* disable SO_OOBINLINE and get the same old bahavior */
3028 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
3029 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
3031 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3032 ok(arg, "SIOCATMARK expected a non-zero value\n");
3034 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
3035 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
3036 ret = WSAGetLastError();
3037 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
3039 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3040 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
3041 ret = WSAGetLastError();
3042 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret);
3044 /* broken used to catch W95, W98, NT4 */
3045 make_keepalive(kalive, 0, 0, 0);
3046 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3047 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3049 make_keepalive(kalive, 1, 0, 0);
3050 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3051 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3053 make_keepalive(kalive, 1, 1000, 1000);
3054 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3055 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3057 make_keepalive(kalive, 1, 10000, 10000);
3058 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3059 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3061 make_keepalive(kalive, 1, 100, 100);
3062 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3063 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3065 make_keepalive(kalive, 0, 100, 100);
3066 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3067 ok(ret == 0 || broken(ret == SOCKET_ERROR), "WSAIoctl failed unexpectedly\n");
3072 static int drain_pause=0;
3073 static DWORD WINAPI drain_socket_thread(LPVOID arg)
3076 SOCKET sock = *(SOCKET*)arg;
3079 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
3083 if (WSAGetLastError() == WSAEWOULDBLOCK)
3087 FD_SET(sock, &readset);
3088 select(sock+1, &readset, NULL, NULL, NULL);
3099 static void test_send(void)
3101 SOCKET src = INVALID_SOCKET;
3102 SOCKET dst = INVALID_SOCKET;
3103 HANDLE hThread = NULL;
3104 const int buflen = 1024*1024;
3105 char *buffer = NULL;
3106 int ret, i, zero = 0;
3110 DWORD id, bytes_sent, dwRet;
3112 memset(&ov, 0, sizeof(ov));
3114 if (tcp_socketpair(&src, &dst) != 0)
3116 ok(0, "creating socket pair failed, skipping test\n");
3120 set_blocking(dst, FALSE);
3121 /* force disable buffering so we can get a pending overlapped request */
3122 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
3123 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
3125 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3126 if (hThread == NULL)
3128 ok(0, "CreateThread failed, error %d\n", GetLastError());
3132 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
3135 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
3139 /* fill the buffer with some nonsense */
3140 for (i = 0; i < buflen; ++i)
3142 buffer[i] = (char) i;
3145 ret = send(src, buffer, buflen, 0);
3147 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
3149 ok(0, "send failed, error %d\n", WSAGetLastError());
3154 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3155 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
3160 WSASetLastError(12345);
3161 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
3162 ok((ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING) || broken(bytes_sent == buflen),
3163 "Failed to start overlapped send %d - %d - %d/%d\n", ret, WSAGetLastError(), bytes_sent, buflen);
3165 /* don't check for completion yet, we may need to drain the buffer while still sending */
3166 set_blocking(src, FALSE);
3167 for (i = 0; i < buflen; ++i)
3171 ret = recv(src, buffer, 1, 0);
3172 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
3176 ret = recv(src, buffer, 1, 0);
3179 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
3183 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
3186 dwRet = WaitForSingleObject(ov.hEvent, 1000);
3187 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3188 if (dwRet == WAIT_OBJECT_0)
3190 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
3191 ok((bret && bytes_sent == buflen) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3192 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
3195 WSASetLastError(12345);
3196 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
3197 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
3198 "WSASend failed %d - %d\n", ret, WSAGetLastError());
3200 WSASetLastError(12345);
3201 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
3202 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
3203 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
3206 if (src != INVALID_SOCKET)
3208 if (dst != INVALID_SOCKET)
3210 if (hThread != NULL)
3211 CloseHandle(hThread);
3213 CloseHandle(ov.hEvent);
3214 HeapFree(GetProcessHeap(), 0, buffer);
3217 typedef struct async_message
3221 struct async_message *next;
3224 static struct async_message *messages_received;
3226 #define WM_SOCKET (WM_USER+100)
3227 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3229 struct async_message *message;
3234 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message));
3235 message->socket = (SOCKET) wparam;
3236 message->lparam = lparam;
3237 message->next = NULL;
3239 if (messages_received)
3241 struct async_message *last = messages_received;
3242 while (last->next) last = last->next;
3243 last->next = message;
3246 messages_received = message;
3250 return DefWindowProc(hwnd, msg, wparam, lparam);
3253 static void get_event_details(int event, int *bit, char *name)
3258 if (bit) *bit = FD_ACCEPT_BIT;
3259 if (name) strcpy(name, "FD_ACCEPT");
3262 if (bit) *bit = FD_CONNECT_BIT;
3263 if (name) strcpy(name, "FD_CONNECT");
3266 if (bit) *bit = FD_READ_BIT;
3267 if (name) strcpy(name, "FD_READ");
3270 if (bit) *bit = FD_OOB_BIT;
3271 if (name) strcpy(name, "FD_OOB");
3274 if (bit) *bit = FD_WRITE_BIT;
3275 if (name) strcpy(name, "FD_WRITE");
3278 if (bit) *bit = FD_CLOSE_BIT;
3279 if (name) strcpy(name, "FD_CLOSE");
3283 if (name) sprintf(name, "bad%x", event);
3287 static const char *dbgstr_event_seq(const LPARAM *seq)
3289 static char message[1024];
3297 get_event_details(WSAGETSELECTEVENT(*seq), NULL, name);
3298 len += sprintf(message + len, "%s(%d) ", name, WSAGETSELECTERROR(*seq));
3302 strcpy( message + len, "]" );
3306 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents)
3308 static char message[1024];
3309 struct async_message *curr = messages_received;
3310 int index, error, bit = 0;
3320 if (bit >= FD_MAX_EVENTS) break;
3321 if ( !(netEvents->lNetworkEvents & (1 << bit)) )
3326 get_event_details(1 << bit, &index, name);
3327 error = netEvents->iErrorCode[index];
3333 if (curr->socket != s)
3338 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name);
3339 error = WSAGETSELECTERROR(curr->lparam);
3343 len += sprintf(message + len, "%s(%d) ", name, error);
3346 strcpy( message + len, "]" );
3350 static void flush_events(SOCKET s, HANDLE hEvent)
3352 WSANETWORKEVENTS netEvents;
3353 struct async_message *prev = NULL, *curr = messages_received;
3357 if (hEvent != INVALID_HANDLE_VALUE)
3359 dwRet = WaitForSingleObject(hEvent, 100);
3360 if (dwRet == WAIT_OBJECT_0)
3362 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents);
3364 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3371 if (curr->socket == s)
3373 if (prev) prev->next = curr->next;
3374 else messages_received = curr->next;
3376 HeapFree(GetProcessHeap(), 0, curr);
3378 if (prev) curr = prev->next;
3379 else curr = messages_received;
3390 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq)
3392 int event, index, error, events;
3393 struct async_message *curr;
3397 events = netEvents->lNetworkEvents;
3400 event = WSAGETSELECTEVENT(*seq);
3401 error = WSAGETSELECTERROR(*seq);
3402 get_event_details(event, &index, NULL);
3404 if (!(events & event) && index != -1)
3406 if (events & event && index != -1)
3408 if (netEvents->iErrorCode[index] != error)
3419 curr = messages_received;
3422 if (curr->socket == s)
3424 if (!*seq) return 0;
3425 if (*seq != curr->lparam) return 0;
3436 /* checks for a sequence of events, (order only checked if window is used) */
3437 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq, const LPARAM **broken_seqs, int completelyBroken)
3440 WSANETWORKEVENTS events, *netEvents = NULL;
3444 if (hEvent != INVALID_HANDLE_VALUE)
3446 netEvents = &events;
3448 dwRet = WaitForSingleObject(hEvent, 200);
3449 if (dwRet == WAIT_OBJECT_0)
3451 ret = WSAEnumNetworkEvents(s, hEvent, netEvents);
3454 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
3459 memset(netEvents, 0, sizeof(*netEvents));
3464 /* Run the message loop a little */
3465 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ))
3467 DispatchMessageA(&msg);
3471 if (match_event_sequence(s, netEvents, seq))
3473 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq));
3474 flush_events(s, hEvent);
3480 for (; *broken_seqs; broken_seqs++)
3482 if (match_event_sequence(s, netEvents, *broken_seqs))
3484 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s, netEvents), dbgstr_event_seq(seq));
3485 flush_events(s, hEvent);
3491 winetest_ok(broken(completelyBroken), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq),
3492 dbgstr_event_seq_result(s, netEvents));
3493 flush_events(s, hEvent);
3496 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
3498 static void test_events(int useMessages)
3500 SOCKET server = INVALID_SOCKET;
3501 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET;
3502 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET;
3503 struct sockaddr_in addr;
3504 HANDLE hThread = NULL;
3505 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE;
3506 WNDCLASSEX wndclass;
3508 char *buffer = NULL;
3509 int bufferSize = 1024*1024;
3513 DWORD bytesReturned;
3519 static char szClassName[] = "wstestclass";
3520 const LPARAM *broken_seq[3];
3521 static const LPARAM empty_seq[] = { 0 };
3522 static const LPARAM close_seq[] = { WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3523 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3524 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3525 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 };
3526 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0),
3527 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3528 static const LPARAM read_read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3529 WSAMAKESELECTREPLY(FD_READ, 0), 0 };
3530 static const LPARAM read_write_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3531 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 };
3532 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0),
3533 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 };
3535 memset(&ov, 0, sizeof(ov));
3536 memset(&ov2, 0, sizeof(ov2));
3538 /* don't use socketpair, we want connection event */
3539 src = socket(AF_INET, SOCK_STREAM, 0);
3540 if (src == INVALID_SOCKET)
3542 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3546 src2 = socket(AF_INET, SOCK_STREAM, 0);
3547 if (src2 == INVALID_SOCKET)
3549 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3554 if (getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len) == SOCKET_ERROR)
3556 ok(0, "failed to get oobinline status, %d\n", GetLastError());
3559 ok(bret == FALSE, "OOB not inline\n");
3563 trace("Event test using messages\n");
3565 wndclass.cbSize = sizeof(wndclass);
3566 wndclass.style = CS_HREDRAW | CS_VREDRAW;
3567 wndclass.lpfnWndProc = ws2_test_WndProc;
3568 wndclass.cbClsExtra = 0;
3569 wndclass.cbWndExtra = 0;
3570 wndclass.hInstance = GetModuleHandle(NULL);
3571 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
3572 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
3573 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3574 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3575 wndclass.lpszClassName = szClassName;
3576 wndclass.lpszMenuName = NULL;
3577 RegisterClassEx(&wndclass);
3579 hWnd = CreateWindow(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, GetModuleHandle(NULL), NULL);
3582 ok(0, "failed to create window: %d\n", GetLastError());
3586 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3589 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3593 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3596 ok(0, "WSAAsyncSelect failed, error %d\n", ret);
3602 trace("Event test using events\n");
3604 hEvent = WSACreateEvent();
3605 if (hEvent == INVALID_HANDLE_VALUE)
3607 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3611 hEvent2 = WSACreateEvent();
3612 if (hEvent2 == INVALID_HANDLE_VALUE)
3614 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3618 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3621 ok(0, "WSAEventSelect failed, error %d\n", ret);
3625 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE);
3628 ok(0, "WSAEventSelect failed, error %d\n", ret);
3633 server = socket(AF_INET, SOCK_STREAM, 0);
3634 if (server == INVALID_SOCKET)
3636 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3640 memset(&addr, 0, sizeof(addr));
3641 addr.sin_family = AF_INET;
3642 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
3643 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
3646 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3651 ret = getsockname(server, (struct sockaddr*)&addr, &len);
3654 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3658 ret = listen(server, 2);
3661 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3665 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
3666 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3668 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3672 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr));
3673 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
3675 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3680 dst = accept(server, (struct sockaddr*)&addr, &len);
3681 if (dst == INVALID_SOCKET)
3683 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3688 dst2 = accept(server, (struct sockaddr*)&addr, &len);
3689 if (dst2 == INVALID_SOCKET)
3691 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3695 closesocket(server);
3696 server = INVALID_SOCKET;
3698 /* On Windows it seems when a non-blocking socket sends to a
3699 blocking socket on the same host, the send() is BLOCKING,
3700 so make both sockets non-blocking. src is already non-blocking
3701 from the async select */
3703 if (set_blocking(dst, FALSE))
3705 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3709 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
3712 ok(0, "could not allocate memory for test\n");
3716 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3717 if (ov.hEvent == NULL)
3719 ok(0, "could not create event object, errno = %d\n", GetLastError());
3723 ov2.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3724 if (ov2.hEvent == NULL)
3726 ok(0, "could not create event object, errno = %d\n", GetLastError());
3730 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3731 ok_event_seq(src, hEvent, connect_seq, NULL, 1);
3732 ok_event_seq(src2, hEvent2, connect_seq, NULL, 1);
3733 /* broken on all windows - FD_CONNECT error is garbage */
3735 /* Test simple send/recv */
3736 ret = send(dst, buffer, 100, 0);
3737 ok(ret == 100, "Failed to send buffer %d err %d\n", ret, GetLastError());
3738 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3740 ret = recv(src, buffer, 1, MSG_PEEK);
3741 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError());
3742 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3744 ret = recv(src, buffer, 50, 0);
3745 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3746 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3748 ret = recv(src, buffer, 50, 0);
3749 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3750 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3752 /* fun fact - events are reenabled even on failure, but only for messages */
3753 ret = send(dst, "1", 1, 0);
3754 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3755 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3757 ret = recv(src, buffer, -1, 0);
3758 ok(ret == SOCKET_ERROR && (GetLastError() == WSAEFAULT || GetLastError() == WSAENOBUFS),
3759 "Failed to recv buffer %d err %d\n", ret, GetLastError());
3762 broken_seq[0] = empty_seq; /* win9x */
3763 broken_seq[1] = NULL;
3764 todo_wine ok_event_seq(src, hEvent, read_seq, broken_seq, 0);
3767 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3769 ret = recv(src, buffer, 1, 0);
3770 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError());
3771 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3773 /* Interaction with overlapped */
3774 bufs.len = sizeof(char);
3776 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3777 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3778 "WSARecv failed - %d error %d\n", ret, GetLastError());
3780 bufs.len = sizeof(char);
3781 bufs.buf = buffer+1;
3782 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL);
3783 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3784 "WSARecv failed - %d error %d\n", ret, GetLastError());
3786 ret = send(dst, "12", 2, 0);
3787 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3788 broken_seq[0] = read_read_seq; /* win9x */
3789 broken_seq[1] = NULL;
3790 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3792 dwRet = WaitForSingleObject(ov.hEvent, 100);
3793 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3794 if (dwRet == WAIT_OBJECT_0)
3796 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3797 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3798 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3799 ok(buffer[0] == '1', "Got %c instead of 1\n", buffer[0]);
3802 dwRet = WaitForSingleObject(ov2.hEvent, 100);
3803 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3804 if (dwRet == WAIT_OBJECT_0)
3806 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE);
3807 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3808 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3809 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[1]);
3812 ret = send(dst, "1", 1, 0);
3813 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3814 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3816 ret = recv(src, buffer, 1, 0);
3817 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3818 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3820 /* Notifications are delivered as soon as possible, blocked only on
3821 * async requests on the same type */
3822 bufs.len = sizeof(char);
3824 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
3825 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
3826 "WSARecv failed - %d error %d\n", ret, GetLastError());
3829 ret = send(dst, "1", 1, MSG_OOB);
3830 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3831 ok_event_seq(src, hEvent, oob_seq, NULL, 0);
3834 dwRet = WaitForSingleObject(ov.hEvent, 100);
3835 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError());
3837 ret = send(dst, "2", 1, 0);
3838 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError());
3839 broken_seq[0] = read_seq; /* win98 */
3840 broken_seq[1] = NULL;
3841 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0);
3843 dwRet = WaitForSingleObject(ov.hEvent, 100);
3844 ok(dwRet == WAIT_OBJECT_0 || broken(dwRet == WAIT_TIMEOUT),
3845 "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3846 if (dwRet == WAIT_OBJECT_0)
3848 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE);
3849 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */,
3850 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError());
3851 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]);
3853 else if (dwRet == WAIT_TIMEOUT)
3855 /* this happens on win98. We get an FD_READ later on the next test */
3856 CancelIo((HANDLE) src);
3860 ret = recv(src, buffer, 1, MSG_OOB);
3861 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3862 /* We get OOB notification, but no data on wine */
3863 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3866 /* Flood the send queue */
3867 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3868 if (hThread == NULL)
3870 ok(0, "CreateThread failed, error %d\n", GetLastError());
3874 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3875 ok_event_seq(src, hEvent, empty_seq, NULL, 0);
3877 /* Now if we send a ton of data and the 'server' does not drain it fast
3878 * enough (set drain_pause to be sure), the socket send buffer will only
3879 * take some of it, and we will get a short write. This will trigger
3880 * another FD_WRITE event as soon as data is sent and more space becomes
3881 * available, but not any earlier. */
3885 ret = send(src, buffer, bufferSize, 0);
3886 } while (ret == bufferSize);
3888 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
3890 Sleep(400); /* win9x */
3891 broken_seq[0] = read_write_seq;
3892 broken_seq[1] = NULL;
3893 ok_event_seq(src, hEvent, write_seq, broken_seq, 0);
3897 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3900 /* Test how FD_CLOSE is handled */
3901 ret = send(dst, "12", 2, 0);
3902 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3904 /* Wait a little and let the send complete */
3907 dst = INVALID_SOCKET;
3910 /* We can never implement this in wine, best we can hope for is
3911 sending FD_CLOSE after the reads complete */
3912 broken_seq[0] = read_seq; /* win9x */
3913 broken_seq[1] = NULL;
3914 todo_wine ok_event_seq(src, hEvent, read_close_seq, broken_seq, 0);
3916 ret = recv(src, buffer, 1, 0);
3917 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3918 ok_event_seq(src, hEvent, read_seq, NULL, 0);
3920 ret = recv(src, buffer, 1, 0);
3921 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3922 /* want it? it's here, but you can't have it */
3923 broken_seq[0] = close_seq; /* win9x */
3924 broken_seq[1] = NULL;
3925 todo_wine ok_event_seq(src, hEvent, empty_seq, /* wine sends FD_CLOSE here */
3928 /* Test how FD_CLOSE is handled */
3929 ret = send(dst2, "12", 2, 0);
3930 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError());
3933 shutdown(dst2, SD_SEND);
3936 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3937 regressions, don't mark them as todo_wine, and mark windows as broken */
3938 broken_seq[0] = read_close_seq;
3939 broken_seq[1] = close_seq;
3940 broken_seq[2] = NULL;
3941 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3943 ret = recv(src2, buffer, 1, 0);
3944 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3945 broken_seq[0] = close_seq; /* win98 */
3946 broken_seq[1] = NULL;
3947 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0);
3949 ret = recv(src2, buffer, 1, 0);
3950 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError());
3951 broken_seq[0] = empty_seq;
3952 broken_seq[1] = NULL;
3953 ok_event_seq(src2, hEvent2, close_seq, broken_seq, 0);
3955 ret = send(src2, "1", 1, 0);
3956 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3957 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3959 ret = send(src2, "1", 1, 0);
3960 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError());
3961 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0);
3964 if (src != INVALID_SOCKET)
3966 flush_events(src, hEvent);
3969 if (src2 != INVALID_SOCKET)
3971 flush_events(src2, hEvent2);
3974 HeapFree(GetProcessHeap(), 0, buffer);
3975 if (server != INVALID_SOCKET)
3976 closesocket(server);
3977 if (dst != INVALID_SOCKET)
3979 if (dst2 != INVALID_SOCKET)
3981 if (hThread != NULL)
3982 CloseHandle(hThread);
3984 DestroyWindow(hWnd);
3986 CloseHandle(hEvent);
3987 if (hEvent2 != NULL)
3988 CloseHandle(hEvent2);
3989 if (ov.hEvent != NULL)
3990 CloseHandle(ov.hEvent);
3991 if (ov2.hEvent != NULL)
3992 CloseHandle(ov2.hEvent);
3995 static void test_ipv6only(void)
3997 SOCKET v4 = INVALID_SOCKET,
3998 v6 = INVALID_SOCKET;
3999 struct sockaddr_in sin4;
4000 struct sockaddr_in6 sin6;
4003 memset(&sin4, 0, sizeof(sin4));
4004 sin4.sin_family = AF_INET;
4005 sin4.sin_port = htons(SERVERPORT);
4007 memset(&sin6, 0, sizeof(sin6));
4008 sin6.sin6_family = AF_INET6;
4009 sin6.sin6_port = htons(SERVERPORT);
4011 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
4012 if (v6 == INVALID_SOCKET) {
4013 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
4014 WSAGetLastError(), WSAEAFNOSUPPORT);
4017 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
4019 skip("Could not bind IPv6 address (LastError: %d).\n",
4024 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4025 if (v4 == INVALID_SOCKET) {
4026 skip("Could not create IPv4 socket (LastError: %d).\n",
4030 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
4031 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
4032 WSAGetLastError(), WSAEADDRINUSE);
4035 if (v4 != INVALID_SOCKET)
4037 if (v6 != INVALID_SOCKET)
4041 static void test_WSASendTo(void)
4044 struct sockaddr_in addr;
4045 char buf[12] = "hello world";
4050 addr.sin_family = AF_INET;
4051 addr.sin_port = htons(139);
4052 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
4053 data_buf.len = sizeof(buf);
4056 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
4057 ok(0, "socket() failed error: %d\n", WSAGetLastError());
4061 WSASetLastError(12345);
4062 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4063 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
4064 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4066 WSASetLastError(12345);
4067 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4068 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4069 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4071 WSASetLastError(12345);
4072 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
4073 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
4076 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
4077 "a successful call to WSASendTo()\n");
4080 static DWORD WINAPI recv_thread(LPVOID arg)
4082 SOCKET sock = *(SOCKET *)arg;
4089 wsa.len = sizeof(buffer);
4090 ov.hEvent = WSACreateEvent();
4091 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL);
4093 WaitForSingleObject(ov.hEvent, 1000);
4094 WSACloseEvent(ov.hEvent);
4098 static void test_WSARecv(void)
4100 SOCKET src, dest, server = INVALID_SOCKET;
4104 DWORD bytesReturned, flags, id;
4106 struct sockaddr_in addr;
4112 memset(&ov, 0, sizeof(ov));
4114 tcp_socketpair(&src, &dest);
4115 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
4117 skip("failed to create sockets\n");
4121 bufs.len = sizeof(buf);
4125 ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4126 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
4132 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
4133 ok(!iret, "Failed to set linger %d\n", GetLastError());
4135 iret = WSARecv(dest, &bufs, 1, NULL, &flags, &ov, NULL);
4136 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
4138 iret = WSARecv(dest, &bufs, 1, &bytesReturned, &flags, &ov, NULL);
4139 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
4142 src = INVALID_SOCKET;
4144 dwret = WaitForSingleObject(ov.hEvent, 1000);
4145 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
4147 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
4148 todo_wine ok(!bret && (GetLastError() == ERROR_NETNAME_DELETED || broken(GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */),
4149 "Did not get disconnect event: %d, error %d\n", bret, GetLastError());
4150 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
4152 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
4153 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4154 if (src == INVALID_SOCKET) goto end;
4156 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
4157 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4158 if (server == INVALID_SOCKET) goto end;
4160 memset(&addr, 0, sizeof(addr));
4161 addr.sin_family = AF_INET;
4162 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
4163 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
4167 iret = getsockname(server, (struct sockaddr *)&addr, &len);
4170 iret = listen(server, 1);
4173 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr));
4177 dest = accept(server, (struct sockaddr *)&addr, &len);
4178 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
4179 if (dest == INVALID_SOCKET) goto end;
4181 send(src, "test message", sizeof("test message"), 0);
4182 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id);
4183 CloseHandle(thread);
4186 if (server != INVALID_SOCKET)
4187 closesocket(server);
4188 if (dest != INVALID_SOCKET)
4190 if (src != INVALID_SOCKET)
4193 WSACloseEvent(ov.hEvent);
4196 static void test_GetAddrInfoW(void)
4198 static const WCHAR port[] = {'8','0',0};
4199 static const WCHAR empty[] = {0};
4200 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
4201 static const WCHAR zero[] = {'0',0};
4203 ADDRINFOW *result, hint;
4205 if (!pGetAddrInfoW || !pFreeAddrInfoW)
4207 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
4210 memset(&hint, 0, sizeof(ADDRINFOW));
4212 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
4213 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
4216 ret = pGetAddrInfoW(empty, NULL, NULL, &result);
4217 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4218 ok(result != NULL, "GetAddrInfoW failed\n");
4219 pFreeAddrInfoW(result);
4222 ret = pGetAddrInfoW(NULL, zero, NULL, &result);
4223 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4224 ok(result != NULL, "GetAddrInfoW failed\n");
4225 pFreeAddrInfoW(result);
4228 ret = pGetAddrInfoW(empty, zero, NULL, &result);
4229 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4230 ok(result != NULL, "GetAddrInfoW failed\n");
4231 pFreeAddrInfoW(result);
4234 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
4235 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4236 pFreeAddrInfoW(result);
4239 ret = pGetAddrInfoW(localhost, empty, NULL, &result);
4240 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4241 pFreeAddrInfoW(result);
4244 ret = pGetAddrInfoW(localhost, zero, NULL, &result);
4245 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4246 pFreeAddrInfoW(result);
4249 ret = pGetAddrInfoW(localhost, port, NULL, &result);
4250 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4251 pFreeAddrInfoW(result);
4254 ret = pGetAddrInfoW(localhost, port, &hint, &result);
4255 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
4256 pFreeAddrInfoW(result);
4259 static void test_getaddrinfo(void)
4262 ADDRINFOA *result, hint;
4264 if (!pgetaddrinfo || !pfreeaddrinfo)
4266 win_skip("getaddrinfo and/or freeaddrinfo not present\n");
4269 memset(&hint, 0, sizeof(ADDRINFOA));
4271 ret = pgetaddrinfo(NULL, NULL, NULL, &result);
4272 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
4275 ret = pgetaddrinfo("", NULL, NULL, &result);
4276 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4277 ok(result != NULL, "getaddrinfo failed\n");
4278 pfreeaddrinfo(result);
4281 ret = pgetaddrinfo(NULL, "0", NULL, &result);
4282 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4283 ok(result != NULL, "getaddrinfo failed\n");
4284 pfreeaddrinfo(result);
4287 ret = pgetaddrinfo("", "0", NULL, &result);
4288 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4289 ok(result != NULL, "getaddrinfo failed\n");
4290 pfreeaddrinfo(result);
4293 ret = pgetaddrinfo("localhost", NULL, NULL, &result);
4294 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4295 pfreeaddrinfo(result);
4298 ret = pgetaddrinfo("localhost", "", NULL, &result);
4299 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4300 pfreeaddrinfo(result);
4303 ret = pgetaddrinfo("localhost", "0", NULL, &result);
4304 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4305 pfreeaddrinfo(result);
4308 ret = pgetaddrinfo("localhost", "80", NULL, &result);
4309 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4310 pfreeaddrinfo(result);
4313 ret = pgetaddrinfo("localhost", "80", &hint, &result);
4314 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError());
4315 pfreeaddrinfo(result);
4318 static void test_ConnectEx(void)
4320 SOCKET listener = INVALID_SOCKET;
4321 SOCKET acceptor = INVALID_SOCKET;
4322 SOCKET connector = INVALID_SOCKET;
4323 struct sockaddr_in address, conaddress;
4325 OVERLAPPED overlapped;
4326 LPFN_CONNECTEX pConnectEx;
4327 GUID connectExGuid = WSAID_CONNECTEX;
4328 DWORD bytesReturned;
4334 memset(&overlapped, 0, sizeof(overlapped));
4336 listener = socket(AF_INET, SOCK_STREAM, 0);
4337 if (listener == INVALID_SOCKET) {
4338 skip("could not create listener socket, error %d\n", WSAGetLastError());
4342 connector = socket(AF_INET, SOCK_STREAM, 0);
4343 if (connector == INVALID_SOCKET) {
4344 skip("could not create connector socket, error %d\n", WSAGetLastError());
4348 memset(&address, 0, sizeof(address));
4349 address.sin_family = AF_INET;
4350 address.sin_addr.s_addr = inet_addr("127.0.0.1");
4351 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
4353 skip("failed to bind, error %d\n", WSAGetLastError());
4357 addrlen = sizeof(address);
4358 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
4360 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4364 if (set_blocking(listener, TRUE)) {
4365 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4369 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
4370 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
4372 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret, WSAGetLastError());
4376 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4377 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
4378 "returned %d + errno %d\n", bret, WSAGetLastError());
4380 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4381 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
4382 "returned %d + errno %d\n", bret, WSAGetLastError());
4383 if (bret == TRUE || WSAGetLastError() != WSAEINVAL)
4385 acceptor = accept(listener, NULL, NULL);
4386 if (acceptor != INVALID_SOCKET) {
4387 closesocket(acceptor);
4388 acceptor = INVALID_SOCKET;
4391 closesocket(connector);
4392 connector = socket(AF_INET, SOCK_STREAM, 0);
4393 if (connector == INVALID_SOCKET) {
4394 skip("could not create connector socket, error %d\n", WSAGetLastError());
4399 /* ConnectEx needs a bound socket */
4400 memset(&conaddress, 0, sizeof(conaddress));
4401 conaddress.sin_family = AF_INET;
4402 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4403 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4405 skip("failed to bind, error %d\n", WSAGetLastError());
4409 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
4410 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
4411 "returned %d + errno %d\n", bret, WSAGetLastError());
4413 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4414 if (overlapped.hEvent == NULL) {
4415 skip("could not create event object, errno = %d\n", GetLastError());
4419 iret = listen(listener, 1);
4421 skip("listening failed, errno = %d\n", WSAGetLastError());
4425 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4426 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4427 "returned %d + errno %d\n", bret, WSAGetLastError());
4428 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4429 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4431 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4432 ok(bret, "Connecting failed, error %d\n", GetLastError());
4433 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
4435 closesocket(connector);
4436 connector = socket(AF_INET, SOCK_STREAM, 0);
4437 if (connector == INVALID_SOCKET) {
4438 skip("could not create connector socket, error %d\n", WSAGetLastError());
4441 /* ConnectEx needs a bound socket */
4442 memset(&conaddress, 0, sizeof(conaddress));
4443 conaddress.sin_family = AF_INET;
4444 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4445 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4447 skip("failed to bind, error %d\n", WSAGetLastError());
4451 acceptor = accept(listener, NULL, NULL);
4452 if (acceptor != INVALID_SOCKET) {
4453 closesocket(acceptor);
4459 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
4460 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
4461 "returned %d + errno %d\n", bret, WSAGetLastError());
4462 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4463 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4465 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4466 ok(bret, "Connecting failed, error %d\n", GetLastError());
4467 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
4469 acceptor = accept(listener, NULL, NULL);
4470 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
4472 bytesReturned = recv(acceptor, buffer, 3, 0);
4474 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
4475 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
4476 "Failed to get the right data, expected '123', got '%s'\n", buffer);
4478 closesocket(connector);
4479 connector = socket(AF_INET, SOCK_STREAM, 0);
4480 if (connector == INVALID_SOCKET) {
4481 skip("could not create connector socket, error %d\n", WSAGetLastError());
4484 /* ConnectEx needs a bound socket */
4485 memset(&conaddress, 0, sizeof(conaddress));
4486 conaddress.sin_family = AF_INET;
4487 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4488 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
4490 skip("failed to bind, error %d\n", WSAGetLastError());
4494 if (acceptor != INVALID_SOCKET) {
4495 closesocket(acceptor);
4496 acceptor = INVALID_SOCKET;
4499 /* Connect with error */
4500 closesocket(listener);
4501 listener = INVALID_SOCKET;
4503 address.sin_port = htons(1);
4505 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
4506 ok(bret == FALSE && GetLastError(), "ConnectEx to bad destination failed: "
4507 "returned %d + errno %d\n", bret, GetLastError());
4509 if (GetLastError() == ERROR_IO_PENDING)
4511 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
4512 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
4514 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
4515 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
4516 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4519 ok(GetLastError() == WSAECONNREFUSED,
4520 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
4524 if (overlapped.hEvent)
4525 WSACloseEvent(overlapped.hEvent);
4526 if (listener != INVALID_SOCKET)
4527 closesocket(listener);
4528 if (acceptor != INVALID_SOCKET)
4529 closesocket(acceptor);
4530 if (connector != INVALID_SOCKET)
4531 closesocket(connector);
4534 static void test_AcceptEx(void)
4536 SOCKET listener = INVALID_SOCKET;
4537 SOCKET acceptor = INVALID_SOCKET;
4538 SOCKET connector = INVALID_SOCKET;
4539 SOCKET connector2 = INVALID_SOCKET;
4540 struct sockaddr_in bindAddress, peerAddress, *readBindAddress, *readRemoteAddress;
4541 int socklen, optlen;
4542 GUID acceptExGuid = WSAID_ACCEPTEX, getAcceptExGuid = WSAID_GETACCEPTEXSOCKADDRS;
4543 LPFN_ACCEPTEX pAcceptEx = NULL;
4544 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs = NULL;
4545 fd_set fds_accept, fds_send;
4546 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
4548 DWORD bytesReturned, connect_time;
4549 char buffer[1024], ipbuffer[32];
4550 OVERLAPPED overlapped;
4551 int iret, localSize = sizeof(struct sockaddr_in), remoteSize = localSize;
4555 memset(&overlapped, 0, sizeof(overlapped));
4557 listener = socket(AF_INET, SOCK_STREAM, 0);
4558 if (listener == INVALID_SOCKET) {
4559 skip("could not create listener socket, error %d\n", WSAGetLastError());
4563 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4564 if (acceptor == INVALID_SOCKET) {
4565 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4569 connector = socket(AF_INET, SOCK_STREAM, 0);
4570 if (connector == INVALID_SOCKET) {
4571 skip("could not create connector socket, error %d\n", WSAGetLastError());
4575 memset(&bindAddress, 0, sizeof(bindAddress));
4576 bindAddress.sin_family = AF_INET;
4577 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
4578 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4580 skip("failed to bind, error %d\n", WSAGetLastError());
4584 socklen = sizeof(bindAddress);
4585 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
4587 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4591 if (set_blocking(listener, FALSE)) {
4592 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4596 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
4597 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
4599 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
4603 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &getAcceptExGuid, sizeof(getAcceptExGuid),
4604 &pGetAcceptExSockaddrs, sizeof(pGetAcceptExSockaddrs), &bytesReturned, NULL, NULL);
4606 skip("WSAIoctl failed to get GetAcceptExSockaddrs with ret %d + errno %d\n", iret, WSAGetLastError());
4610 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4611 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4612 &bytesReturned, &overlapped);
4613 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
4614 "returned %d + errno %d\n", bret, WSAGetLastError());
4616 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4617 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4618 &bytesReturned, &overlapped);
4619 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
4620 "returned %d + errno %d\n", bret, WSAGetLastError());
4622 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4623 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4624 &bytesReturned, &overlapped);
4626 (WSAGetLastError() == WSAEINVAL ||
4627 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
4628 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
4630 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
4631 &bytesReturned, &overlapped);
4632 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
4633 "returned %d + errno %d\n", bret, WSAGetLastError());
4635 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
4636 &bytesReturned, &overlapped);
4637 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
4638 "returned %d + errno %d\n", bret, WSAGetLastError());
4640 bret = pAcceptEx(listener, acceptor, buffer, 0,
4641 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4642 &bytesReturned, NULL);
4643 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
4644 "returned %d + errno %d\n", bret, WSAGetLastError());
4646 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4647 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4648 &bytesReturned, &overlapped);
4649 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
4650 "returned %d + errno %d\n", bret, WSAGetLastError());
4652 iret = listen(listener, 5);
4654 skip("listening failed, errno = %d\n", WSAGetLastError());
4658 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4659 if (overlapped.hEvent == NULL) {
4660 skip("could not create event object, errno = %d\n", GetLastError());
4664 bret = pAcceptEx(listener, acceptor, buffer, 0,
4665 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4666 &bytesReturned, &overlapped);
4667 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4669 bret = pAcceptEx(listener, acceptor, buffer, 0,
4670 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4671 &bytesReturned, &overlapped);
4672 todo_wine ok((bret == FALSE && WSAGetLastError() == WSAEINVAL) || broken(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) /* NT4 */,
4673 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
4674 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
4675 /* We need to cancel this call, otherwise things fail */
4676 bret = CancelIo((HANDLE) listener);
4677 ok(bret, "Failed to cancel failed test. Bailing...\n");
4679 WaitForSingleObject(overlapped.hEvent, 0);
4681 bret = pAcceptEx(listener, acceptor, buffer, 0,
4682 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4683 &bytesReturned, &overlapped);
4684 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4687 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4688 todo_wine ok((iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL) || broken(!iret) /* NT4 */,
4689 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
4690 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
4691 /* We need to cancel this call, otherwise things fail */
4692 closesocket(acceptor);
4693 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4694 if (acceptor == INVALID_SOCKET) {
4695 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4699 bret = CancelIo((HANDLE) listener);
4700 ok(bret, "Failed to cancel failed test. Bailing...\n");
4703 bret = pAcceptEx(listener, acceptor, buffer, 0,
4704 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4705 &bytesReturned, &overlapped);
4706 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4709 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4710 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4712 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
4713 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4715 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4716 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4717 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4719 closesocket(connector);
4720 connector = INVALID_SOCKET;
4721 closesocket(acceptor);
4723 /* Test short reads */
4725 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4726 if (acceptor == INVALID_SOCKET) {
4727 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4730 connector = socket(AF_INET, SOCK_STREAM, 0);
4731 if (connector == INVALID_SOCKET) {
4732 skip("could not create connector socket, error %d\n", WSAGetLastError());
4735 bret = pAcceptEx(listener, acceptor, buffer, 2,
4736 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4737 &bytesReturned, &overlapped);
4738 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4740 connect_time = 0xdeadbeef;
4741 optlen = sizeof(connect_time);
4742 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
4743 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
4744 ok(connect_time == ~0u, "unexpected connect time %u\n", connect_time);
4746 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4747 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4749 connect_time = 0xdeadbeef;
4750 optlen = sizeof(connect_time);
4751 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
4752 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
4753 ok(connect_time < 0xdeadbeef, "unexpected connect time %u\n", connect_time);
4755 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4756 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
4758 iret = getsockname( connector, (struct sockaddr *)&peerAddress, &remoteSize);
4759 ok( !iret, "getsockname failed.\n");
4761 /* Check if the buffer from AcceptEx is decoded correclty */
4762 pGetAcceptExSockaddrs(buffer, 2, sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4763 (struct sockaddr **)&readBindAddress, &localSize,
4764 (struct sockaddr **)&readRemoteAddress, &remoteSize);
4765 strcpy( ipbuffer, inet_ntoa(readBindAddress->sin_addr));
4766 ok( readBindAddress->sin_addr.s_addr == bindAddress.sin_addr.s_addr,
4767 "Local socket address is different %s != %s\n",
4768 ipbuffer, inet_ntoa(bindAddress.sin_addr));
4769 ok( readBindAddress->sin_port == bindAddress.sin_port,
4770 "Local socket port is different: %d != %d\n",
4771 readBindAddress->sin_port, bindAddress.sin_port);
4772 strcpy( ipbuffer, inet_ntoa(readRemoteAddress->sin_addr));
4773 ok( readRemoteAddress->sin_addr.s_addr == peerAddress.sin_addr.s_addr,
4774 "Remote socket address is different %s != %s\n",
4775 ipbuffer, inet_ntoa(peerAddress.sin_addr));
4776 ok( readRemoteAddress->sin_port == peerAddress.sin_port,
4777 "Remote socket port is different: %d != %d\n",
4778 readRemoteAddress->sin_port, peerAddress.sin_port);
4780 iret = send(connector, buffer, 1, 0);
4781 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
4783 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4784 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4786 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4787 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4788 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4790 closesocket(connector);
4791 connector = INVALID_SOCKET;
4792 closesocket(acceptor);
4794 /* Test CF_DEFER & AcceptEx interaction */
4796 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4797 if (acceptor == INVALID_SOCKET) {
4798 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4801 connector = socket(AF_INET, SOCK_STREAM, 0);
4802 if (connector == INVALID_SOCKET) {
4803 skip("could not create connector socket, error %d\n", WSAGetLastError());
4806 connector2 = socket(AF_INET, SOCK_STREAM, 0);
4807 if (connector == INVALID_SOCKET) {
4808 skip("could not create connector socket, error %d\n", WSAGetLastError());
4812 if (set_blocking(connector, FALSE)) {
4813 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4817 if (set_blocking(connector2, FALSE)) {
4818 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4822 /* Connect socket #1 */
4823 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4824 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4826 FD_ZERO ( &fds_accept );
4827 FD_ZERO ( &fds_send );
4829 FD_SET ( listener, &fds_accept );
4830 FD_SET ( connector, &fds_send );
4836 for (i = 0; i < 4000; ++i)
4838 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
4840 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
4841 "acceptex test(%d): could not select on socket, errno %d\n" );
4843 /* check for incoming requests */
4844 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
4847 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
4848 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
4849 bret = pAcceptEx(listener, acceptor, buffer, 0,
4850 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4851 &bytesReturned, &overlapped);
4852 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4854 else if (got == 2) {
4855 /* this should be socket #2 */
4856 SOCKET tmp = accept(listener, NULL, NULL);
4857 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
4861 ok(FALSE, "Got more than 2 connections?\n");
4864 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
4865 /* Send data on second socket, and stop */
4866 send(connector2, "2", 1, 0);
4867 FD_CLR ( connector2, &fds_send );
4871 if ( FD_ISSET ( connector, &fds_opensend ) ) {
4872 /* Once #1 is connected, allow #2 to connect */
4875 send(connector, "1", 1, 0);
4876 FD_CLR ( connector, &fds_send );
4878 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4879 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4880 FD_SET ( connector2, &fds_send );
4884 ok (got == 2 || broken(got == 1) /* NT4 */,
4885 "Did not get both connections, got %d\n", got);
4887 dwret = WaitForSingleObject(overlapped.hEvent, 0);
4888 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4890 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4891 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4892 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4894 set_blocking(acceptor, TRUE);
4895 iret = recv( acceptor, buffer, 2, 0);
4896 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
4898 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
4900 closesocket(connector);
4901 connector = INVALID_SOCKET;
4902 closesocket(acceptor);
4904 /* clean up in case of failures */
4905 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
4906 closesocket(acceptor);
4908 /* Disconnect during receive? */
4910 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4911 if (acceptor == INVALID_SOCKET) {
4912 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4915 connector = socket(AF_INET, SOCK_STREAM, 0);
4916 if (connector == INVALID_SOCKET) {
4917 skip("could not create connector socket, error %d\n", WSAGetLastError());
4920 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4921 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4922 &bytesReturned, &overlapped);
4923 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4925 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
4926 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4928 closesocket(connector);
4929 connector = INVALID_SOCKET;
4931 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4932 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4934 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4935 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
4936 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
4938 closesocket(acceptor);
4940 /* Test closing with pending requests */
4942 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4943 if (acceptor == INVALID_SOCKET) {
4944 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4947 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4948 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4949 &bytesReturned, &overlapped);
4950 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4952 closesocket(acceptor);
4954 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4955 todo_wine ok(dwret == WAIT_OBJECT_0 || broken(dwret == WAIT_TIMEOUT) /* NT4/2000 */,
4956 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
4958 if (dwret != WAIT_TIMEOUT) {
4959 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
4960 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
4963 bret = CancelIo((HANDLE) listener);
4964 ok(bret, "Failed to cancel failed test. Bailing...\n");
4966 WaitForSingleObject(overlapped.hEvent, 0);
4969 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4970 if (acceptor == INVALID_SOCKET) {
4971 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4974 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4975 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4976 &bytesReturned, &overlapped);
4977 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4979 CancelIo((HANDLE) acceptor);
4981 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
4982 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
4984 closesocket(acceptor);
4986 acceptor = socket(AF_INET, SOCK_STREAM, 0);
4987 if (acceptor == INVALID_SOCKET) {
4988 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4991 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
4992 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
4993 &bytesReturned, &overlapped);
4994 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
4996 closesocket(listener);
4997 listener = INVALID_SOCKET;
4999 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
5000 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
5002 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
5003 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
5006 if (overlapped.hEvent)
5007 WSACloseEvent(overlapped.hEvent);
5008 if (listener != INVALID_SOCKET)
5009 closesocket(listener);
5010 if (acceptor != INVALID_SOCKET)
5011 closesocket(acceptor);
5012 if (connector != INVALID_SOCKET)
5013 closesocket(connector);
5014 if (connector2 != INVALID_SOCKET)
5015 closesocket(connector2);
5018 static void test_getpeername(void)
5021 struct sockaddr_in sa, sa_out;
5023 const char buf[] = "hello world";
5026 /* Test the parameter validation order. */
5027 ret = getpeername(INVALID_SOCKET, NULL, NULL);
5028 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
5029 ok(WSAGetLastError() == WSAENOTSOCK,
5030 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
5032 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
5033 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
5034 if (sock == INVALID_SOCKET)
5036 skip("Socket creation failed with %d\n", WSAGetLastError());
5040 ret = getpeername(sock, NULL, NULL);
5041 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
5042 ok(WSAGetLastError() == WSAENOTCONN ||
5043 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
5044 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
5046 memset(&sa, 0, sizeof(sa));
5047 sa.sin_family = AF_INET;
5048 sa.sin_port = htons(139);
5049 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
5051 /* sendto does not change a socket's connection state. */
5052 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
5053 ok(ret != SOCKET_ERROR,
5054 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
5056 ret = getpeername(sock, NULL, NULL);
5057 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
5058 ok(WSAGetLastError() == WSAENOTCONN ||
5059 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
5060 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
5062 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
5064 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
5066 ret = getpeername(sock, NULL, NULL);
5067 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
5068 ok(WSAGetLastError() == WSAEFAULT,
5069 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
5071 /* Test crashes on Wine. */
5074 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
5075 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
5076 ok(WSAGetLastError() == WSAEFAULT,
5077 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
5081 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
5082 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
5083 ok(WSAGetLastError() == WSAEFAULT,
5084 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
5086 sa_len = sizeof(sa_out);
5087 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
5088 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
5089 ok(!memcmp(&sa, &sa_out, sizeof(sa)),
5090 "Expected the returned structure to be identical to the connect structure\n");
5095 static void test_sioRoutingInterfaceQuery(void)
5099 SOCKADDR_IN sin = { 0 }, sout = { 0 };
5100 DWORD bytesReturned;
5102 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
5103 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
5104 if (sock == INVALID_SOCKET)
5106 skip("Socket creation failed with %d\n", WSAGetLastError());
5109 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
5111 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
5112 "expected WSAEFAULT, got %d\n", WSAGetLastError());
5113 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
5114 NULL, 0, NULL, NULL, NULL);
5115 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
5116 "expected WSAEFAULT, got %d\n", WSAGetLastError());
5117 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
5118 NULL, 0, &bytesReturned, NULL, NULL);
5119 ok(ret == SOCKET_ERROR &&
5120 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
5121 WSAGetLastError() == WSAEINVAL /* NT4 */||
5122 WSAGetLastError() == WSAEAFNOSUPPORT),
5123 "expected WSAEFAULT or WSAEINVAL or WSAEAFNOSUPPORT, got %d\n",
5125 sin.sin_family = AF_INET;
5126 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
5127 NULL, 0, &bytesReturned, NULL, NULL);
5128 ok(ret == SOCKET_ERROR &&
5129 (WSAGetLastError() == WSAEFAULT /* Win98 */ ||
5130 WSAGetLastError() == WSAEINVAL),
5131 "expected WSAEFAULT or WSAEINVAL, got %d\n", WSAGetLastError());
5132 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
5133 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
5134 NULL, 0, &bytesReturned, NULL, NULL);
5135 ok(ret == SOCKET_ERROR &&
5136 (WSAGetLastError() == WSAEINVAL /* NT4 */ ||
5137 WSAGetLastError() == WSAEFAULT),
5138 "expected WSAEINVAL or WSAEFAULT, got %d\n", WSAGetLastError());
5139 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
5140 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
5141 ok(!ret || broken(WSAGetLastError() == WSAEINVAL /* NT4 */),
5142 "WSAIoctl failed: %d\n", WSAGetLastError());
5145 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n",
5147 /* We expect the source address to be INADDR_LOOPBACK as well, but
5148 * there's no guarantee that a route to the loopback address exists,
5149 * so rather than introduce spurious test failures we do not test the
5156 static void test_synchronous_WSAIoctl(void)
5158 HANDLE previous_port, io_port;
5159 WSAOVERLAPPED overlapped, *olp;
5167 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
5168 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
5170 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
5171 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
5173 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
5174 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
5177 memset( &overlapped, 0, sizeof(overlapped) );
5178 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
5179 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
5181 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
5182 ok( ret, "failed to get completion status %u\n", GetLastError() );
5184 CloseHandle( io_port );
5185 closesocket( socket );
5186 CloseHandle( previous_port );
5189 #define WM_ASYNCCOMPLETE (WM_USER + 100)
5190 static HWND create_async_message_window(void)
5192 static const char class_name[] = "ws2_32 async message window class";
5194 WNDCLASSEXA wndclass;
5197 wndclass.cbSize = sizeof(wndclass);
5198 wndclass.style = CS_HREDRAW | CS_VREDRAW;
5199 wndclass.lpfnWndProc = DefWindowProcA;
5200 wndclass.cbClsExtra = 0;
5201 wndclass.cbWndExtra = 0;
5202 wndclass.hInstance = GetModuleHandleA(NULL);
5203 wndclass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
5204 wndclass.hIconSm = LoadIconA(NULL, IDI_APPLICATION);
5205 wndclass.hCursor = LoadCursorA(NULL, IDC_ARROW);
5206 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
5207 wndclass.lpszClassName = class_name;
5208 wndclass.lpszMenuName = NULL;
5210 RegisterClassExA(&wndclass);
5212 hWnd = CreateWindow(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW,
5213 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL);
5216 ok(0, "failed to create window: %u\n", GetLastError());
5223 static void test_WSAAsyncGetServByPort(void)
5225 HWND hwnd = create_async_message_window();
5227 char buffer[MAXGETHOSTSTRUCT];
5232 /* FIXME: The asynchronous window messages should be tested. */
5234 /* Parameters are not checked when initiating the asynchronous operation. */
5235 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0);
5236 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5238 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0);
5239 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5241 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0);
5242 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5244 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT);
5245 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n");
5247 DestroyWindow(hwnd);
5250 static void test_WSAAsyncGetServByName(void)
5252 HWND hwnd = create_async_message_window();
5254 char buffer[MAXGETHOSTSTRUCT];
5259 /* FIXME: The asynchronous window messages should be tested. */
5261 /* Parameters are not checked when initiating the asynchronous operation. */
5262 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", NULL, NULL, 0);
5263 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
5265 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", "", buffer, MAXGETHOSTSTRUCT);
5266 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
5268 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", NULL, NULL, 0);
5269 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
5271 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", "tcp", buffer, MAXGETHOSTSTRUCT);
5272 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n");
5274 DestroyWindow(hwnd);
5278 * Provide consistent initialization for the AcceptEx IOCP tests.
5280 static SOCKET setup_iocp_src(struct sockaddr_in *bindAddress)
5282 SOCKET src, ret = INVALID_SOCKET;
5285 src = socket(AF_INET, SOCK_STREAM, 0);
5286 if (src == INVALID_SOCKET)
5288 skip("could not create listener socket, error %d\n", WSAGetLastError());
5292 memset(bindAddress, 0, sizeof(*bindAddress));
5293 bindAddress->sin_family = AF_INET;
5294 bindAddress->sin_addr.s_addr = inet_addr("127.0.0.1");
5295 iret = bind(src, (struct sockaddr*)bindAddress, sizeof(*bindAddress));
5298 skip("failed to bind, error %d\n", WSAGetLastError());
5302 socklen = sizeof(*bindAddress);
5303 iret = getsockname(src, (struct sockaddr*)bindAddress, &socklen);
5305 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
5309 if (set_blocking(src, FALSE))
5311 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
5315 iret = listen(src, 5);
5318 skip("listening failed, errno = %d\n", WSAGetLastError());
5324 if (src != ret && ret == INVALID_SOCKET)
5329 static void test_completion_port(void)
5331 HANDLE previous_port, io_port;
5332 WSAOVERLAPPED ov, *olp;
5333 SOCKET src, dest, dup, connector = INVALID_SOCKET;
5334 WSAPROTOCOL_INFOA info;
5337 DWORD num_bytes, flags;
5342 struct sockaddr_in bindAddress;
5343 GUID acceptExGuid = WSAID_ACCEPTEX;
5344 LPFN_ACCEPTEX pAcceptEx = NULL;
5346 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
5347 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError());
5349 memset(&ov, 0, sizeof(ov));
5351 tcp_socketpair(&src, &dest);
5352 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
5354 skip("failed to create sockets\n");
5358 bufs.len = sizeof(buf);
5364 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
5365 ok(!iret, "Failed to set linger %d\n", GetLastError());
5367 io_port = CreateIoCompletionPort( (HANDLE)dest, previous_port, 125, 0 );
5368 ok(io_port != NULL, "Failed to create completion port %u\n", GetLastError());
5370 SetLastError(0xdeadbeef);
5372 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
5373 ok(iret == SOCKET_ERROR, "WSARecv returned %d\n", iret);
5374 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5377 src = INVALID_SOCKET;
5379 SetLastError(0xdeadbeef);
5381 num_bytes = 0xdeadbeef;
5382 olp = (WSAOVERLAPPED *)0xdeadbeef;
5384 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5385 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret);
5386 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "Last error was %d\n", GetLastError());
5387 ok(key == 125, "Key is %lu\n", key);
5388 ok(num_bytes == 0, "Number of bytes received is %u\n", num_bytes);
5389 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5391 SetLastError(0xdeadbeef);
5393 num_bytes = 0xdeadbeef;
5394 olp = (WSAOVERLAPPED *)0xdeadbeef;
5396 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5397 ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret );
5398 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5399 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5400 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5401 ok(!olp, "Overlapped structure is at %p\n", olp);
5403 if (dest != INVALID_SOCKET)
5405 if (src != INVALID_SOCKET)
5408 memset(&ov, 0, sizeof(ov));
5410 tcp_socketpair(&src, &dest);
5411 if (src == INVALID_SOCKET || dest == INVALID_SOCKET)
5413 skip("failed to create sockets\n");
5417 bufs.len = sizeof(buf);
5423 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
5424 ok(!iret, "Failed to set linger %d\n", GetLastError());
5426 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
5427 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5429 set_blocking(dest, FALSE);
5432 src = INVALID_SOCKET;
5433 num_bytes = 0xdeadbeef;
5434 SetLastError(0xdeadbeef);
5436 iret = WSASend(dest, &bufs, 1, &num_bytes, 0, &ov, NULL);
5437 ok(iret == SOCKET_ERROR, "WSASend failed - %d\n", iret);
5438 ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
5439 ok(num_bytes == 0xdeadbeef, "Managed to send %d\n", num_bytes);
5441 SetLastError(0xdeadbeef);
5443 num_bytes = 0xdeadbeef;
5444 olp = (WSAOVERLAPPED *)0xdeadbeef;
5446 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5447 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
5448 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5449 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5450 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5451 ok(!olp, "Overlapped structure is at %p\n", olp);
5453 if (dest != INVALID_SOCKET)
5456 dest = socket(AF_INET, SOCK_STREAM, 0);
5457 if (dest == INVALID_SOCKET)
5459 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
5463 iret = WSAIoctl(dest, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
5464 &pAcceptEx, sizeof(pAcceptEx), &num_bytes, NULL, NULL);
5467 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
5471 /* Test IOCP response on socket close (IOCP created after AcceptEx) */
5473 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5476 SetLastError(0xdeadbeef);
5478 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5479 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5481 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5482 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5484 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5485 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5488 src = INVALID_SOCKET;
5490 SetLastError(0xdeadbeef);
5492 num_bytes = 0xdeadbeef;
5493 olp = (WSAOVERLAPPED *)0xdeadbeef;
5495 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5496 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5497 todo_wine ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
5498 todo_wine ok(key == 125, "Key is %lu\n", key);
5499 todo_wine ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5500 todo_wine ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5501 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
5503 SetLastError(0xdeadbeef);
5505 num_bytes = 0xdeadbeef;
5506 olp = (WSAOVERLAPPED *)0xdeadbeef;
5507 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5508 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5509 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5510 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5511 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5512 ok(!olp, "Overlapped structure is at %p\n", olp);
5514 /* Test IOCP response on socket close (IOCP created before AcceptEx) */
5516 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5519 SetLastError(0xdeadbeef);
5521 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5522 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5524 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5525 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5527 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5528 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5531 src = INVALID_SOCKET;
5533 SetLastError(0xdeadbeef);
5535 num_bytes = 0xdeadbeef;
5536 olp = (WSAOVERLAPPED *)0xdeadbeef;
5538 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5539 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5540 todo_wine ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
5541 todo_wine ok(key == 125, "Key is %lu\n", key);
5542 todo_wine ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5543 todo_wine ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5544 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
5546 SetLastError(0xdeadbeef);
5548 num_bytes = 0xdeadbeef;
5549 olp = (WSAOVERLAPPED *)0xdeadbeef;
5550 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5551 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5552 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5553 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5554 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5555 ok(!olp, "Overlapped structure is at %p\n", olp);
5557 /* Test IOCP with duplicated handle */
5559 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5562 SetLastError(0xdeadbeef);
5564 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5565 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5567 WSADuplicateSocket( src, GetCurrentProcessId(), &info );
5568 dup = WSASocket(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
5569 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
5571 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5572 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5574 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5575 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5577 SetLastError(0xdeadbeef);
5579 num_bytes = 0xdeadbeef;
5580 olp = (WSAOVERLAPPED *)0xdeadbeef;
5581 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5582 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5583 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5584 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5585 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5586 ok(!olp, "Overlapped structure is at %p\n", olp);
5589 src = INVALID_SOCKET;
5591 dup = INVALID_SOCKET;
5593 SetLastError(0xdeadbeef);
5595 num_bytes = 0xdeadbeef;
5596 olp = (WSAOVERLAPPED *)0xdeadbeef;
5597 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5598 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5599 todo_wine ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
5600 todo_wine ok(key == 125, "Key is %lu\n", key);
5601 todo_wine ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5602 todo_wine ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5603 todo_wine ok(olp && olp->Internal == (ULONG)STATUS_CANCELLED, "Internal status is %lx\n", olp ? olp->Internal : 0);
5605 SetLastError(0xdeadbeef);
5607 num_bytes = 0xdeadbeef;
5608 olp = (WSAOVERLAPPED *)0xdeadbeef;
5609 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5610 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5611 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5612 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5613 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5614 ok(!olp, "Overlapped structure is at %p\n", olp);
5616 /* Test IOCP with duplicated handle (closing duplicated handle) */
5618 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5621 SetLastError(0xdeadbeef);
5623 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5624 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5626 WSADuplicateSocket( src, GetCurrentProcessId(), &info );
5627 dup = WSASocket(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
5628 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
5630 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5631 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5633 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5634 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5637 dup = INVALID_SOCKET;
5639 SetLastError(0xdeadbeef);
5641 num_bytes = 0xdeadbeef;
5642 olp = (WSAOVERLAPPED *)0xdeadbeef;
5643 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5644 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5645 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5646 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5647 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5648 ok(!olp, "Overlapped structure is at %p\n", olp);
5650 SetLastError(0xdeadbeef);
5652 num_bytes = 0xdeadbeef;
5653 olp = (WSAOVERLAPPED *)0xdeadbeef;
5654 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5655 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5656 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5657 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5658 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5659 ok(!olp, "Overlapped structure is at %p\n", olp);
5662 src = INVALID_SOCKET;
5664 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5665 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5666 todo_wine ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
5667 todo_wine ok(key == 125, "Key is %lu\n", key);
5668 todo_wine ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5669 todo_wine ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5670 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
5672 SetLastError(0xdeadbeef);
5674 num_bytes = 0xdeadbeef;
5675 olp = (WSAOVERLAPPED *)0xdeadbeef;
5676 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5677 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5678 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5679 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5680 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5681 ok(!olp, "Overlapped structure is at %p\n", olp);
5683 /* Test IOCP with duplicated handle (closing original handle) */
5685 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5688 SetLastError(0xdeadbeef);
5690 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5691 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5693 WSADuplicateSocket( src, GetCurrentProcessId(), &info );
5694 dup = WSASocket(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
5695 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
5697 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5698 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5700 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5701 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5704 src = INVALID_SOCKET;
5706 SetLastError(0xdeadbeef);
5708 num_bytes = 0xdeadbeef;
5709 olp = (WSAOVERLAPPED *)0xdeadbeef;
5710 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5711 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5712 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5713 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5714 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5715 ok(!olp, "Overlapped structure is at %p\n", olp);
5718 dup = INVALID_SOCKET;
5720 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5721 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5722 todo_wine ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
5723 todo_wine ok(key == 125, "Key is %lu\n", key);
5724 todo_wine ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5725 todo_wine ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5726 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
5728 SetLastError(0xdeadbeef);
5730 num_bytes = 0xdeadbeef;
5731 olp = (WSAOVERLAPPED *)0xdeadbeef;
5732 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5733 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5734 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5735 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5736 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5737 ok(!olp, "Overlapped structure is at %p\n", olp);
5739 /* Test IOCP without AcceptEx */
5741 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5744 SetLastError(0xdeadbeef);
5746 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5747 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5750 src = INVALID_SOCKET;
5752 SetLastError(0xdeadbeef);
5754 num_bytes = 0xdeadbeef;
5755 olp = (WSAOVERLAPPED *)0xdeadbeef;
5756 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5757 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5758 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5759 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5760 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5761 ok(!olp, "Overlapped structure is at %p\n", olp);
5765 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5768 connector = socket(AF_INET, SOCK_STREAM, 0);
5769 if (connector == INVALID_SOCKET) {
5770 skip("could not create connector socket, error %d\n", WSAGetLastError());
5774 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5775 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5777 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
5778 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5780 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5781 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5783 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5784 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5786 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
5787 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
5789 closesocket(connector);
5790 connector = INVALID_SOCKET;
5792 SetLastError(0xdeadbeef);
5794 num_bytes = 0xdeadbeef;
5795 olp = (WSAOVERLAPPED *)0xdeadbeef;
5797 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5798 ok(bret == TRUE, "failed to get completion status %u\n", bret);
5799 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
5800 ok(key == 125, "Key is %lu\n", key);
5801 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5802 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5803 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
5805 SetLastError(0xdeadbeef);
5807 num_bytes = 0xdeadbeef;
5808 olp = (WSAOVERLAPPED *)0xdeadbeef;
5809 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5810 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5811 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5812 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5813 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5814 ok(!olp, "Overlapped structure is at %p\n", olp);
5816 if (dest != INVALID_SOCKET)
5818 if (src != INVALID_SOCKET)
5823 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5826 dest = socket(AF_INET, SOCK_STREAM, 0);
5827 if (dest == INVALID_SOCKET)
5829 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
5833 connector = socket(AF_INET, SOCK_STREAM, 0);
5834 if (connector == INVALID_SOCKET) {
5835 skip("could not create connector socket, error %d\n", WSAGetLastError());
5839 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5840 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5842 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
5843 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5845 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5846 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5848 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5849 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5851 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
5852 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
5854 iret = send(connector, buf, 1, 0);
5855 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
5858 dest = INVALID_SOCKET;
5860 SetLastError(0xdeadbeef);
5862 num_bytes = 0xdeadbeef;
5863 olp = (WSAOVERLAPPED *)0xdeadbeef;
5865 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5866 ok(bret == TRUE, "failed to get completion status %u\n", bret);
5867 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
5868 ok(key == 125, "Key is %lu\n", key);
5869 ok(num_bytes == 1, "Number of bytes transferred is %u\n", num_bytes);
5870 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5871 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
5873 SetLastError(0xdeadbeef);
5875 num_bytes = 0xdeadbeef;
5876 olp = (WSAOVERLAPPED *)0xdeadbeef;
5877 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5878 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5879 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5880 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5881 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5882 ok(!olp, "Overlapped structure is at %p\n", olp);
5884 if (src != INVALID_SOCKET)
5886 if (connector != INVALID_SOCKET)
5887 closesocket(connector);
5891 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET)
5894 dest = socket(AF_INET, SOCK_STREAM, 0);
5895 if (dest == INVALID_SOCKET)
5897 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
5901 connector = socket(AF_INET, SOCK_STREAM, 0);
5902 if (connector == INVALID_SOCKET) {
5903 skip("could not create connector socket, error %d\n", WSAGetLastError());
5907 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
5908 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5910 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
5911 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5913 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
5914 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5916 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
5917 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
5919 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
5920 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
5923 dest = INVALID_SOCKET;
5925 SetLastError(0xdeadbeef);
5927 num_bytes = 0xdeadbeef;
5928 olp = (WSAOVERLAPPED *)0xdeadbeef;
5930 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
5931 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5932 todo_wine ok((GetLastError() == ERROR_NETNAME_DELETED) || (GetLastError() == ERROR_CONNECTION_ABORTED), "Last error was %d\n", GetLastError());
5933 ok(key == 125, "Key is %lu\n", key);
5934 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
5935 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
5936 todo_wine ok(olp && ((olp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT)
5937 || (olp->Internal == (ULONG)STATUS_CONNECTION_ABORTED)), "Internal status is %lx\n", olp ? olp->Internal : 0);
5939 SetLastError(0xdeadbeef);
5941 num_bytes = 0xdeadbeef;
5942 olp = (WSAOVERLAPPED *)0xdeadbeef;
5943 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
5944 ok(bret == FALSE, "failed to get completion status %u\n", bret);
5945 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
5946 ok(key == 0xdeadbeef, "Key is %lu\n", key);
5947 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
5948 ok(!olp, "Overlapped structure is at %p\n", olp);
5952 if (dest != INVALID_SOCKET)
5954 if (src != INVALID_SOCKET)
5956 if (connector != INVALID_SOCKET)
5957 closesocket(connector);
5958 CloseHandle(previous_port);
5961 /**************** Main program ***************/
5967 /* Leave these tests at the beginning. They depend on WSAStartup not having been
5968 * called, which is done by Init() below. */
5969 test_WithoutWSAStartup();
5970 test_WithWSAStartup();
5974 test_set_getsockopt();
5975 test_so_reuseaddr();
5977 test_extendedSocketOptions();
5979 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
5981 trace ( " **** STARTING TEST %d ****\n", i );
5982 do_test ( &tests[i] );
5983 trace ( " **** TEST %d COMPLETE ****\n", i );
5988 test_getservbyname();
5991 test_WSAAddressToStringA();
5992 test_WSAAddressToStringW();
5994 test_WSAStringToAddressA();
5995 test_WSAStringToAddressW();
6003 test_addr_to_print();
6006 test_gethostbyname_hack();
6015 test_GetAddrInfoW();
6020 test_sioRoutingInterfaceQuery();
6022 test_WSAAsyncGetServByPort();
6023 test_WSAAsyncGetServByName();
6025 test_completion_port();
6027 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */
6029 test_synchronous_WSAIoctl();