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
29 #include "wine/test.h"
31 #define MAX_CLIENTS 4 /* Max number of clients */
32 #define NUM_TESTS 4 /* Number of tests performed */
33 #define FIRST_CHAR 'A' /* First character in transferred pattern */
34 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
35 #define BIND_TRIES 6 /* Number of bind() attempts */
36 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
37 after server initialization, if something hangs */
39 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
41 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
42 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
44 #define SERVERIP "127.0.0.1" /* IP to bind to */
45 #define SERVERPORT 9374 /* Port number to bind to */
47 #define wsa_ok(op, cond, msg) \
51 if ( !(cond tmp) ) err = WSAGetLastError(); \
52 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
55 /* Function pointers */
56 static void (WINAPI
*pFreeAddrInfoW
)(PADDRINFOW
) = 0;
57 static int (WINAPI
*pGetAddrInfoW
)(LPCWSTR
,LPCWSTR
,const ADDRINFOW
*,PADDRINFOW
*) = 0;
58 static PCSTR (WINAPI
*pInetNtop
)(INT
,LPVOID
,LPSTR
,ULONG
) = 0;
60 /**************** Structs and typedefs ***************/
62 typedef struct thread_info
68 /* Information in the server about open client connections */
69 typedef struct sock_info
72 struct sockaddr_in addr
;
73 struct sockaddr_in peer
;
79 /* Test parameters for both server & client */
80 typedef struct test_params
84 const char *inet_addr
;
91 /* server-specific test parameters */
92 typedef struct server_params
99 /* client-specific test parameters */
100 typedef struct client_params
102 test_params
*general
;
107 /* This type combines all information for setting up a test scenario */
108 typedef struct test_setup
112 server_params srv_params
;
114 client_params clt_params
;
117 /* Thread local storage for server */
118 typedef struct server_memory
121 struct sockaddr_in addr
;
122 sock_info sock
[MAX_CLIENTS
];
125 /* Thread local storage for client */
126 typedef struct client_memory
129 struct sockaddr_in addr
;
134 /* SelectReadThread thread parameters */
135 typedef struct select_thread_params
139 } select_thread_params
;
141 /**************** Static variables ***************/
143 static DWORD tls
; /* Thread local storage index */
144 static HANDLE thread
[1+MAX_CLIENTS
];
145 static DWORD thread_id
[1+MAX_CLIENTS
];
146 static HANDLE server_ready
;
147 static HANDLE client_ready
[MAX_CLIENTS
];
148 static int client_id
;
150 /**************** General utility functions ***************/
152 static int tcp_socketpair(SOCKET
*src
, SOCKET
*dst
)
154 SOCKET server
= INVALID_SOCKET
;
155 struct sockaddr_in addr
;
159 *src
= INVALID_SOCKET
;
160 *dst
= INVALID_SOCKET
;
162 *src
= socket(AF_INET
, SOCK_STREAM
, 0);
163 if (*src
== INVALID_SOCKET
)
166 server
= socket(AF_INET
, SOCK_STREAM
, 0);
167 if (server
== INVALID_SOCKET
)
170 memset(&addr
, 0, sizeof(addr
));
171 addr
.sin_family
= AF_INET
;
172 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
173 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
178 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
182 ret
= listen(server
, 1);
186 ret
= connect(*src
, (struct sockaddr
*)&addr
, sizeof(addr
));
191 *dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
194 if (server
!= INVALID_SOCKET
)
196 if (*src
!= INVALID_SOCKET
&& *dst
!= INVALID_SOCKET
)
203 static void set_so_opentype ( BOOL overlapped
)
205 int optval
= !overlapped
, newval
, len
= sizeof (int);
207 ok ( setsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
208 (LPVOID
) &optval
, sizeof (optval
) ) == 0,
209 "setting SO_OPENTYPE failed\n" );
210 ok ( getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
211 (LPVOID
) &newval
, &len
) == 0,
212 "getting SO_OPENTYPE failed\n" );
213 ok ( optval
== newval
, "failed to set SO_OPENTYPE\n" );
216 static int set_blocking ( SOCKET s
, BOOL blocking
)
218 u_long val
= !blocking
;
219 return ioctlsocket ( s
, FIONBIO
, &val
);
222 static void fill_buffer ( char *buf
, int chunk_size
, int n_chunks
)
225 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
226 memset ( p
, c
, chunk_size
);
229 static int test_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
)
235 for ( i
= 0; i
< chunk_size
; i
++ )
236 if ( p
[i
] != c
) return i
;
242 * This routine is called when a client / server does not expect any more data,
243 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
245 static void read_zero_bytes ( SOCKET s
)
249 while ( ( tmp
= recv ( s
, buf
, 256, 0 ) ) > 0 )
251 ok ( n
<= 0, "garbage data received: %d bytes\n", n
);
254 static int do_synchronous_send ( SOCKET s
, char *buf
, int buflen
, int sendlen
)
256 char* last
= buf
+ buflen
, *p
;
258 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
259 n
= send ( s
, p
, min ( sendlen
, last
- p
), 0 );
260 wsa_ok ( n
, 0 <=, "do_synchronous_send (%x): error %d\n" );
264 static int do_synchronous_recv ( SOCKET s
, char *buf
, int buflen
, int recvlen
)
266 char* last
= buf
+ buflen
, *p
;
268 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
269 n
= recv ( s
, p
, min ( recvlen
, last
- p
), 0 );
270 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
274 static int do_synchronous_recvfrom ( SOCKET s
, char *buf
, int buflen
,int flags
,struct sockaddr
*from
, int *fromlen
, int recvlen
)
276 char* last
= buf
+ buflen
, *p
;
278 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
279 n
= recvfrom ( s
, p
, min ( recvlen
, last
- p
), 0, from
, fromlen
);
280 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
285 * Call this routine right after thread startup.
286 * SO_OPENTYPE must by 0, regardless what the server did.
288 static void check_so_opentype (void)
292 getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (LPVOID
) &tmp
, &len
);
293 ok ( tmp
== 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp
);
296 /**************** Server utility functions ***************/
299 * Even if we have closed our server socket cleanly,
300 * the OS may mark the address "in use" for some time -
301 * this happens with native Linux apps, too.
303 static void do_bind ( SOCKET s
, struct sockaddr
* addr
, int addrlen
)
305 int err
, wsaerr
= 0, n_try
= BIND_TRIES
;
307 while ( ( err
= bind ( s
, addr
, addrlen
) ) != 0 &&
308 ( wsaerr
= WSAGetLastError () ) == WSAEADDRINUSE
&&
311 trace ( "address in use, waiting ...\n" );
312 Sleep ( 1000 * BIND_SLEEP
);
314 ok ( err
== 0, "failed to bind: %d\n", wsaerr
);
317 static void server_start ( server_params
*par
)
320 test_params
*gen
= par
->general
;
321 server_memory
*mem
= LocalAlloc ( LPTR
, sizeof ( server_memory
) );
323 TlsSetValue ( tls
, mem
);
324 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
325 NULL
, 0, par
->sock_flags
);
326 ok ( mem
->s
!= INVALID_SOCKET
, "Server: WSASocket failed\n" );
328 mem
->addr
.sin_family
= AF_INET
;
329 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
330 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
332 for (i
= 0; i
< MAX_CLIENTS
; i
++)
334 mem
->sock
[i
].s
= INVALID_SOCKET
;
335 mem
->sock
[i
].buf
= LocalAlloc ( LPTR
, gen
->n_chunks
* gen
->chunk_size
);
336 mem
->sock
[i
].n_recvd
= 0;
337 mem
->sock
[i
].n_sent
= 0;
340 if ( gen
->sock_type
== SOCK_STREAM
)
341 do_bind ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof (mem
->addr
) );
344 static void server_stop (void)
347 server_memory
*mem
= TlsGetValue ( tls
);
349 for (i
= 0; i
< MAX_CLIENTS
; i
++ )
351 LocalFree ( mem
->sock
[i
].buf
);
352 if ( mem
->sock
[i
].s
!= INVALID_SOCKET
)
353 closesocket ( mem
->sock
[i
].s
);
355 ok ( closesocket ( mem
->s
) == 0, "closesocket failed\n" );
357 ExitThread ( GetCurrentThreadId () );
360 /**************** Client utilitiy functions ***************/
362 static void client_start ( client_params
*par
)
364 test_params
*gen
= par
->general
;
365 client_memory
*mem
= LocalAlloc (LPTR
, sizeof (client_memory
));
367 TlsSetValue ( tls
, mem
);
369 WaitForSingleObject ( server_ready
, INFINITE
);
371 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
372 NULL
, 0, par
->sock_flags
);
374 mem
->addr
.sin_family
= AF_INET
;
375 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
376 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
378 ok ( mem
->s
!= INVALID_SOCKET
, "Client: WSASocket failed\n" );
380 mem
->send_buf
= LocalAlloc ( LPTR
, 2 * gen
->n_chunks
* gen
->chunk_size
);
381 mem
->recv_buf
= mem
->send_buf
+ gen
->n_chunks
* gen
->chunk_size
;
382 fill_buffer ( mem
->send_buf
, gen
->chunk_size
, gen
->n_chunks
);
384 SetEvent ( client_ready
[client_id
] );
385 /* Wait for the other clients to come up */
386 WaitForMultipleObjects ( min ( gen
->n_clients
, MAX_CLIENTS
), client_ready
, TRUE
, INFINITE
);
389 static void client_stop (void)
391 client_memory
*mem
= TlsGetValue ( tls
);
392 wsa_ok ( closesocket ( mem
->s
), 0 ==, "closesocket error (%x): %d\n" );
393 LocalFree ( mem
->send_buf
);
398 /**************** Servers ***************/
401 * simple_server: A very basic server doing synchronous IO.
403 static VOID WINAPI
simple_server ( server_params
*par
)
405 test_params
*gen
= par
->general
;
407 int pos
, n_recvd
, n_sent
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
408 id
= GetCurrentThreadId();
410 trace ( "simple_server (%x) starting\n", id
);
412 set_so_opentype ( FALSE
); /* non-overlapped */
413 server_start ( par
);
414 mem
= TlsGetValue ( tls
);
416 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
417 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "simple_server (%x): listen failed: %d\n");
419 trace ( "simple_server (%x) ready\n", id
);
420 SetEvent ( server_ready
); /* notify clients */
422 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
424 trace ( "simple_server (%x): waiting for client\n", id
);
426 /* accept a single connection */
427 tmp
= sizeof ( mem
->sock
[0].peer
);
428 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
429 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "simple_server (%x): accept failed: %d\n" );
431 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
432 "simple_server (%x): strange peer address\n", id
);
434 /* Receive data & check it */
435 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
436 ok ( n_recvd
== n_expected
,
437 "simple_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
438 pos
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
439 ok ( pos
== -1, "simple_server (%x): test pattern error: %d\n", id
, pos
);
442 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
443 ok ( n_sent
== n_expected
,
444 "simple_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
447 read_zero_bytes ( mem
->sock
[0].s
);
448 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "simple_server (%x): closesocket error: %d\n" );
449 mem
->sock
[0].s
= INVALID_SOCKET
;
452 trace ( "simple_server (%x) exiting\n", id
);
457 * select_server: A non-blocking server.
459 static VOID WINAPI
select_server ( server_params
*par
)
461 test_params
*gen
= par
->general
;
463 int n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
464 id
= GetCurrentThreadId(), n_connections
= 0, n_sent
, n_recvd
,
465 n_set
, delta
, n_ready
;
466 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
467 fd_set fds_recv
, fds_send
, fds_openrecv
, fds_opensend
;
469 trace ( "select_server (%x) starting\n", id
);
471 set_so_opentype ( FALSE
); /* non-overlapped */
472 server_start ( par
);
473 mem
= TlsGetValue ( tls
);
475 wsa_ok ( set_blocking ( mem
->s
, FALSE
), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
476 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "select_server (%x): listen failed: %d\n");
478 trace ( "select_server (%x) ready\n", id
);
479 SetEvent ( server_ready
); /* notify clients */
481 FD_ZERO ( &fds_openrecv
);
482 FD_ZERO ( &fds_recv
);
483 FD_ZERO ( &fds_send
);
484 FD_ZERO ( &fds_opensend
);
486 FD_SET ( mem
->s
, &fds_openrecv
);
490 fds_recv
= fds_openrecv
;
491 fds_send
= fds_opensend
;
495 wsa_ok ( ( n_ready
= select ( 0, &fds_recv
, &fds_send
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
496 "select_server (%x): select() failed: %d\n" );
498 /* check for incoming requests */
499 if ( FD_ISSET ( mem
->s
, &fds_recv
) ) {
502 trace ( "select_server (%x): accepting client connection\n", id
);
504 /* accept a single connection */
505 tmp
= sizeof ( mem
->sock
[n_connections
].peer
);
506 mem
->sock
[n_connections
].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[n_connections
].peer
, &tmp
);
507 wsa_ok ( mem
->sock
[n_connections
].s
, INVALID_SOCKET
!=, "select_server (%x): accept() failed: %d\n" );
509 ok ( mem
->sock
[n_connections
].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
510 "select_server (%x): strange peer address\n", id
);
512 /* add to list of open connections */
513 FD_SET ( mem
->sock
[n_connections
].s
, &fds_openrecv
);
514 FD_SET ( mem
->sock
[n_connections
].s
, &fds_opensend
);
519 /* handle open requests */
521 for ( i
= 0; i
< n_connections
; i
++ )
523 if ( FD_ISSET( mem
->sock
[i
].s
, &fds_recv
) ) {
526 if ( mem
->sock
[i
].n_recvd
< n_expected
) {
527 /* Receive data & check it */
528 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 );
529 ok ( n_recvd
!= SOCKET_ERROR
, "select_server (%x): error in recv(): %d\n", id
, WSAGetLastError() );
530 mem
->sock
[i
].n_recvd
+= n_recvd
;
532 if ( mem
->sock
[i
].n_recvd
== n_expected
) {
533 int pos
= test_buffer ( mem
->sock
[i
].buf
, gen
->chunk_size
, gen
->n_chunks
);
534 ok ( pos
== -1, "select_server (%x): test pattern error: %d\n", id
, pos
);
535 FD_CLR ( mem
->sock
[i
].s
, &fds_openrecv
);
538 ok ( mem
->sock
[i
].n_recvd
<= n_expected
, "select_server (%x): received too many bytes: %d\n", id
, mem
->sock
[i
].n_recvd
);
542 /* only echo back what we've received */
543 delta
= mem
->sock
[i
].n_recvd
- mem
->sock
[i
].n_sent
;
545 if ( FD_ISSET ( mem
->sock
[i
].s
, &fds_send
) ) {
548 if ( ( delta
> 0 ) && ( mem
->sock
[i
].n_sent
< n_expected
) ) {
550 n_sent
= send ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_sent
, min ( delta
, par
->buflen
), 0 );
551 ok ( n_sent
!= SOCKET_ERROR
, "select_server (%x): error in send(): %d\n", id
, WSAGetLastError() );
552 mem
->sock
[i
].n_sent
+= n_sent
;
554 if ( mem
->sock
[i
].n_sent
== n_expected
) {
555 FD_CLR ( mem
->sock
[i
].s
, &fds_opensend
);
558 ok ( mem
->sock
[i
].n_sent
<= n_expected
, "select_server (%x): sent too many bytes: %d\n", id
, mem
->sock
[i
].n_sent
);
563 /* check that select returned the correct number of ready sockets */
564 ok ( ( n_set
== n_ready
), "select_server (%x): select() returns wrong number of ready sockets\n", id
);
566 /* check if all clients are done */
567 if ( ( fds_opensend
.fd_count
== 0 )
568 && ( fds_openrecv
.fd_count
== 1 ) /* initial socket that accepts clients */
569 && ( n_connections
== min ( gen
->n_clients
, MAX_CLIENTS
) ) ) {
574 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
577 read_zero_bytes ( mem
->sock
[i
].s
);
578 wsa_ok ( closesocket ( mem
->sock
[i
].s
), 0 ==, "select_server (%x): closesocket error: %d\n" );
579 mem
->sock
[i
].s
= INVALID_SOCKET
;
582 trace ( "select_server (%x) exiting\n", id
);
586 /**************** Clients ***************/
589 * simple_client: A very basic client doing synchronous IO.
591 static VOID WINAPI
simple_client ( client_params
*par
)
593 test_params
*gen
= par
->general
;
595 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
597 id
= GetCurrentThreadId();
598 trace ( "simple_client (%x): starting\n", id
);
599 /* wait here because we want to call set_so_opentype before creating a socket */
600 WaitForSingleObject ( server_ready
, INFINITE
);
601 trace ( "simple_client (%x): server ready\n", id
);
603 check_so_opentype ();
604 set_so_opentype ( FALSE
); /* non-overlapped */
605 client_start ( par
);
606 mem
= TlsGetValue ( tls
);
609 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
610 0 ==, "simple_client (%x): connect error: %d\n" );
611 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
612 "simple_client (%x): failed to set blocking mode\n", id
);
613 trace ( "simple_client (%x) connected\n", id
);
615 /* send data to server */
616 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
617 ok ( n_sent
== n_expected
,
618 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
620 /* shutdown send direction */
621 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
623 /* Receive data echoed back & check it */
624 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, par
->buflen
);
625 ok ( n_recvd
== n_expected
,
626 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
629 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
630 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
633 read_zero_bytes ( mem
->s
);
634 trace ( "simple_client (%x) exiting\n", id
);
639 * simple_mixed_client: mixing send and recvfrom
641 static VOID WINAPI
simple_mixed_client ( client_params
*par
)
643 test_params
*gen
= par
->general
;
645 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
646 int fromLen
= sizeof(mem
->addr
);
647 struct sockaddr test
;
649 id
= GetCurrentThreadId();
650 trace ( "simple_client (%x): starting\n", id
);
651 /* wait here because we want to call set_so_opentype before creating a socket */
652 WaitForSingleObject ( server_ready
, INFINITE
);
653 trace ( "simple_client (%x): server ready\n", id
);
655 check_so_opentype ();
656 set_so_opentype ( FALSE
); /* non-overlapped */
657 client_start ( par
);
658 mem
= TlsGetValue ( tls
);
661 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
662 0 ==, "simple_client (%x): connect error: %d\n" );
663 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
664 "simple_client (%x): failed to set blocking mode\n", id
);
665 trace ( "simple_client (%x) connected\n", id
);
667 /* send data to server */
668 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
669 ok ( n_sent
== n_expected
,
670 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
672 /* shutdown send direction */
673 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
675 /* this shouldn't change, since lpFrom, is not updated on
676 connection oriented sockets - exposed by bug 11640
678 ((struct sockaddr_in
*)&test
)->sin_addr
.s_addr
= inet_addr("0.0.0.0");
680 /* Receive data echoed back & check it */
681 n_recvd
= do_synchronous_recvfrom ( mem
->s
,
685 (struct sockaddr
*)&test
,
688 ok ( n_recvd
== n_expected
,
689 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
691 /* check that lpFrom was not updated */
694 inet_ntoa(((struct sockaddr_in
*)&test
)->sin_addr
),
695 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
698 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
699 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
702 read_zero_bytes ( mem
->s
);
703 trace ( "simple_client (%x) exiting\n", id
);
708 * event_client: An event-driven client
710 static void WINAPI
event_client ( client_params
*par
)
712 test_params
*gen
= par
->general
;
714 int id
= GetCurrentThreadId(), n_expected
= gen
->n_chunks
* gen
->chunk_size
,
717 WSANETWORKEVENTS wsa_events
;
718 char *send_last
, *recv_last
, *send_p
, *recv_p
;
719 LONG mask
= FD_READ
| FD_WRITE
| FD_CLOSE
;
721 trace ( "event_client (%x): starting\n", id
);
722 client_start ( par
);
723 trace ( "event_client (%x): server ready\n", id
);
725 mem
= TlsGetValue ( tls
);
727 /* Prepare event notification for connect, makes socket nonblocking */
728 event
= WSACreateEvent ();
729 WSAEventSelect ( mem
->s
, event
, FD_CONNECT
);
730 tmp
= connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) );
732 err
= WSAGetLastError ();
733 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): connect error: %d\n", id
, err
);
734 tmp
= WaitForSingleObject ( event
, INFINITE
);
735 ok ( tmp
== WAIT_OBJECT_0
, "event_client (%x): wait for connect event failed: %d\n", id
, tmp
);
736 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
737 ok ( err
== 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id
, err
);
738 err
= wsa_events
.iErrorCode
[ FD_CONNECT_BIT
];
739 ok ( err
== 0, "event_client (%x): connect error: %d\n", id
, err
);
743 trace ( "event_client (%x) connected\n", id
);
745 WSAEventSelect ( mem
->s
, event
, mask
);
747 recv_p
= mem
->recv_buf
;
748 recv_last
= mem
->recv_buf
+ n_expected
;
749 send_p
= mem
->send_buf
;
750 send_last
= mem
->send_buf
+ n_expected
;
754 err
= WaitForSingleObject ( event
, INFINITE
);
755 ok ( err
== WAIT_OBJECT_0
, "event_client (%x): wait failed\n", id
);
757 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
758 ok( err
== 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id
, err
);
760 if ( wsa_events
.lNetworkEvents
& FD_WRITE
)
762 err
= wsa_events
.iErrorCode
[ FD_WRITE_BIT
];
763 ok ( err
== 0, "event_client (%x): FD_WRITE error code: %d\n", id
, err
);
768 n
= send ( mem
->s
, send_p
, min ( send_last
- send_p
, par
->buflen
), 0 );
771 err
= WSAGetLastError ();
772 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): send error: %d\n", id
, err
);
777 while ( n
>= 0 && send_p
< send_last
);
779 if ( send_p
== send_last
)
781 trace ( "event_client (%x): all data sent - shutdown\n", id
);
782 shutdown ( mem
->s
, SD_SEND
);
784 WSAEventSelect ( mem
->s
, event
, mask
);
787 if ( wsa_events
.lNetworkEvents
& FD_READ
)
789 err
= wsa_events
.iErrorCode
[ FD_READ_BIT
];
790 ok ( err
== 0, "event_client (%x): FD_READ error code: %d\n", id
, err
);
791 if ( err
!= 0 ) break;
793 /* First read must succeed */
794 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
795 wsa_ok ( n
, 0 <=, "event_client (%x): recv error: %d\n" );
799 if ( recv_p
== recv_last
)
802 trace ( "event_client (%x): all data received\n", id
);
803 WSAEventSelect ( mem
->s
, event
, mask
);
806 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
807 if ( n
< 0 && ( err
= WSAGetLastError()) != WSAEWOULDBLOCK
)
808 ok ( 0, "event_client (%x): read error: %d\n", id
, err
);
812 if ( wsa_events
.lNetworkEvents
& FD_CLOSE
)
814 trace ( "event_client (%x): close event\n", id
);
815 err
= wsa_events
.iErrorCode
[ FD_CLOSE_BIT
];
816 ok ( err
== 0, "event_client (%x): FD_CLOSE error code: %d\n", id
, err
);
821 n
= send_p
- mem
->send_buf
;
822 ok ( send_p
== send_last
,
823 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n
, n_expected
);
824 n
= recv_p
- mem
->recv_buf
;
825 ok ( recv_p
== recv_last
,
826 "simple_client (%x): received less data than expected: %d of %d\n", id
, n
, n_expected
);
827 n
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
828 ok ( n
== -1, "event_client (%x): test pattern error: %d\n", id
, n
);
831 WSACloseEvent ( event
);
832 trace ( "event_client (%x) exiting\n", id
);
836 /* Tests for WSAStartup */
837 static void test_WithoutWSAStartup(void)
841 WSASetLastError(0xdeadbeef);
842 ptr
= gethostbyname("localhost");
844 ok(ptr
== NULL
, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
845 ok(WSAGetLastError() == WSANOTINITIALISED
, "gethostbyname() failed with unexpected error: %d\n",
849 static void test_WithWSAStartup(void)
852 WORD version
= MAKEWORD( 2, 2 );
856 res
= WSAStartup( version
, &data
);
857 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
859 ptr
= gethostbyname("localhost");
860 ok(ptr
!= NULL
, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
865 /**************** Main program utility functions ***************/
867 static void Init (void)
869 WORD ver
= MAKEWORD (2, 2);
871 HMODULE hws2_32
= GetModuleHandle("ws2_32.dll");
873 pFreeAddrInfoW
= (void *)GetProcAddress(hws2_32
, "FreeAddrInfoW");
874 pGetAddrInfoW
= (void *)GetProcAddress(hws2_32
, "GetAddrInfoW");
875 pInetNtop
= (void *)GetProcAddress(hws2_32
, "inet_ntop");
877 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
881 static void Exit (void)
886 err
= WSAGetLastError();
887 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
889 err
= WSAGetLastError();
890 ok ( (ret
== SOCKET_ERROR
&& err
== WSANOTINITIALISED
) ||
891 broken(ret
== 0), /* WinME */
892 "WSACleanup returned %d GetLastError is %d\n", ret
, err
);
895 static void StartServer (LPTHREAD_START_ROUTINE routine
,
896 test_params
*general
, server_params
*par
)
898 par
->general
= general
;
899 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
900 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
903 static void StartClients (LPTHREAD_START_ROUTINE routine
,
904 test_params
*general
, client_params
*par
)
907 par
->general
= general
;
908 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
911 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
912 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
913 /* Make sure the client is up and running */
914 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
918 static void do_test( test_setup
*test
)
920 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
923 server_ready
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
924 for (i
= 0; i
<= n
; i
++)
925 client_ready
[i
] = CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
927 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
928 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
929 WaitForSingleObject ( server_ready
, INFINITE
);
931 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
932 ok ( wait
<= WAIT_OBJECT_0
+ n
,
933 "some threads have not completed: %x\n", wait
);
935 if ( ! ( wait
<= WAIT_OBJECT_0
+ n
) )
937 for (i
= 0; i
<= n
; i
++)
939 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
941 trace ("terminating thread %08x\n", thread_id
[i
]);
942 TerminateThread ( thread
[i
], 0 );
946 CloseHandle ( server_ready
);
947 for (i
= 0; i
<= n
; i
++)
948 CloseHandle ( client_ready
[i
] );
951 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
952 /* optname = SO_LINGER */
953 static const LINGER linger_testvals
[] = {
960 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
961 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
962 bug in the linux kernel (fixed in 2.6.8) */
963 #define SOCKTIMEOUT2 997000 /* 997 seconds */
965 static void test_set_getsockopt(void)
973 s
= socket(AF_INET
, SOCK_STREAM
, 0);
974 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
975 if( s
== INVALID_SOCKET
) return;
977 timeout
= SOCKTIMEOUT1
;
978 size
= sizeof(timeout
);
979 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
981 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
982 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
983 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
986 size
= sizeof(timeout
);
987 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
989 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
990 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
991 ok( timeout
== 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
994 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
995 size
= sizeof(timeout
);
996 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
998 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
999 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1000 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
1002 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
1003 size
= sizeof(lingval
);
1004 lingval
= linger_testvals
[i
];
1005 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1007 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1008 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1009 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1010 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1011 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1012 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1013 lingval
.l_onoff
, lingval
.l_linger
,
1014 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1016 /* Test for erroneously passing a value instead of a pointer as optval */
1017 size
= sizeof(char);
1018 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1019 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1020 "instead of failing.\n");
1021 lasterr
= WSAGetLastError();
1022 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1023 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1024 lasterr
, WSAEFAULT
);
1026 /* SO_RCVTIMEO with invalid values for level */
1027 size
= sizeof(timeout
);
1028 timeout
= SOCKTIMEOUT1
;
1029 SetLastError(0xdeadbeef);
1030 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1031 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1032 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1033 err
, WSAGetLastError());
1035 timeout
= SOCKTIMEOUT1
;
1036 SetLastError(0xdeadbeef);
1037 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1038 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1039 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1040 err
, WSAGetLastError());
1045 static void test_so_reuseaddr(void)
1047 struct sockaddr_in saddr
;
1049 unsigned int rc
,reuse
;
1052 saddr
.sin_family
= AF_INET
;
1053 saddr
.sin_port
= htons(9375);
1054 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1056 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1057 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1058 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1059 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1061 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1062 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1066 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1067 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1069 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1070 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1073 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1074 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1076 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1077 * a port immediately after closing another socket on that port, so
1078 * basically following the BSD socket semantics here. */
1080 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1081 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1086 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1088 static void test_ip_pktinfo(void)
1090 ULONG addresses
[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY
)};
1091 char recvbuf
[10], pktbuf
[512], msg
[] = "HELLO";
1092 struct sockaddr_in s1addr
, s2addr
, s3addr
;
1093 GUID WSARecvMsg_GUID
= WSAID_WSARECVMSG
;
1094 LPFN_WSARECVMSG pWSARecvMsg
= NULL
;
1095 unsigned int rc
, foundhdr
, yes
= 1;
1096 DWORD dwBytes
, dwSize
, dwFlags
;
1105 memset(&ov
, 0, sizeof(ov
));
1106 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1107 if (ov
.hEvent
== INVALID_HANDLE_VALUE
)
1109 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
1113 memset(&hdr
, 0x00, sizeof(hdr
));
1114 s1addr
.sin_family
= AF_INET
;
1115 s1addr
.sin_port
= htons(0);
1116 /* Note: s1addr.sin_addr is set below */
1117 iovec
[0].buf
= recvbuf
;
1118 iovec
[0].len
= sizeof(recvbuf
);
1119 hdr
.name
= (struct sockaddr
*)&s3addr
;
1120 hdr
.namelen
= sizeof(s3addr
);
1121 hdr
.lpBuffers
= &iovec
[0];
1122 hdr
.dwBufferCount
= 1;
1123 hdr
.Control
.buf
= pktbuf
;
1124 /* Note: hdr.Control.len is set below */
1127 for (i
=0;i
<sizeof(addresses
)/sizeof(UINT32
);i
++)
1129 s1addr
.sin_addr
.s_addr
= addresses
[i
];
1131 /* Build "server" side socket */
1132 s1
=socket(AF_INET
, SOCK_DGRAM
, 0);
1133 if (s1
== INVALID_SOCKET
)
1135 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1139 /* Obtain the WSARecvMsg function */
1140 WSAIoctl(s1
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &WSARecvMsg_GUID
, sizeof(WSARecvMsg_GUID
),
1141 &pWSARecvMsg
, sizeof(pWSARecvMsg
), &dwBytes
, NULL
, NULL
);
1144 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
1149 /* Setup the server side socket */
1150 rc
=bind(s1
, (struct sockaddr
*)&s1addr
, sizeof(s1addr
));
1151 ok(rc
!= SOCKET_ERROR
, "bind() failed error: %d\n", WSAGetLastError());
1152 rc
=setsockopt(s1
, IPPROTO_IP
, IP_PKTINFO
, (const char*)&yes
, sizeof(yes
));
1153 ok(rc
== 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1155 /* Build "client" side socket */
1156 addrlen
= sizeof(s2addr
);
1157 if (getsockname(s1
, (struct sockaddr
*) &s2addr
, &addrlen
) != 0)
1159 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
1163 s2addr
.sin_addr
.s_addr
= addresses
[0]; /* Always target the local adapter address */
1164 s2
=socket(AF_INET
, SOCK_DGRAM
, 0);
1165 if (s2
== INVALID_SOCKET
)
1167 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1172 /* Test an empty message header */
1173 rc
=pWSARecvMsg(s1
, NULL
, NULL
, NULL
, NULL
);
1174 err
=WSAGetLastError();
1175 ok(rc
== SOCKET_ERROR
&& err
== WSAEFAULT
, "WSARecvMsg() failed error: %d (ret = %d)\n", err
, rc
);
1178 * Send a packet from the client to the server and test for specifying
1179 * a short control header.
1181 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1182 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1183 hdr
.Control
.len
= 1;
1184 rc
=pWSARecvMsg(s1
, &hdr
, &dwSize
, NULL
, NULL
);
1185 err
=WSAGetLastError();
1186 ok(rc
== SOCKET_ERROR
&& err
== WSAEMSGSIZE
&& (hdr
.dwFlags
& MSG_CTRUNC
),
1187 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err
, rc
, hdr
.dwFlags
);
1188 hdr
.dwFlags
= 0; /* Reset flags */
1190 /* Perform another short control header test, this time with an overlapped receive */
1191 hdr
.Control
.len
= 1;
1192 rc
=pWSARecvMsg(s1
, &hdr
, NULL
, &ov
, NULL
);
1193 err
=WSAGetLastError();
1194 ok(rc
!= 0 && err
== WSA_IO_PENDING
, "WSARecvMsg() failed error: %d\n", err
);
1195 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1196 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1197 if (WaitForSingleObject(ov
.hEvent
, 100) != WAIT_OBJECT_0
)
1199 skip("Server side did not receive packet, some tests skipped.\n");
1205 WSAGetOverlappedResult(s1
, &ov
, NULL
, FALSE
, &dwFlags
);
1207 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags
);
1208 ok(hdr
.dwFlags
== MSG_CTRUNC
,
1209 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr
.dwFlags
);
1210 hdr
.dwFlags
= 0; /* Reset flags */
1213 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1214 * on the server end and check that the returned packet matches what was sent.
1216 hdr
.Control
.len
= sizeof(pktbuf
);
1217 rc
=pWSARecvMsg(s1
, &hdr
, NULL
, &ov
, NULL
);
1218 err
=WSAGetLastError();
1219 ok(rc
!= 0 && err
== WSA_IO_PENDING
, "WSARecvMsg() failed error: %d\n", err
);
1220 ok(hdr
.Control
.len
== sizeof(pktbuf
),
1221 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr
.Control
.len
);
1222 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1223 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1224 if (WaitForSingleObject(ov
.hEvent
, 100) != WAIT_OBJECT_0
)
1226 skip("Server side did not receive packet, some tests skipped.\n");
1232 WSAGetOverlappedResult(s1
, &ov
, &dwSize
, FALSE
, NULL
);
1233 ok(dwSize
== sizeof(msg
),
1234 "WSARecvMsg() buffer length does not match transmitted data!\n");
1235 ok(strncmp(iovec
[0].buf
, msg
, sizeof(msg
)) == 0,
1236 "WSARecvMsg() buffer does not match transmitted data!\n");
1237 ok(hdr
.Control
.len
== IP_PKTINFO_LEN
,
1238 "WSARecvMsg() control length mismatch (%d).\n", hdr
.Control
.len
);
1240 /* Test for the expected IP_PKTINFO return information. */
1242 for (cmsg
= WSA_CMSG_FIRSTHDR(&hdr
); cmsg
!= NULL
; cmsg
= WSA_CMSG_NXTHDR(&hdr
, cmsg
))
1244 if (cmsg
->cmsg_level
== IPPROTO_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
1246 struct in_pktinfo
*pi
= (struct in_pktinfo
*)WSA_CMSG_DATA(cmsg
);
1248 ok(pi
->ipi_addr
.s_addr
== s2addr
.sin_addr
.s_addr
, "destination ip mismatch!\n");
1252 ok(foundhdr
, "IP_PKTINFO header information was not returned!\n");
1259 CloseHandle(ov
.hEvent
);
1262 /************* Array containing the tests to run **********/
1264 #define STD_STREAM_SOCKET \
1270 static test_setup tests
[NUM_TESTS
] =
1272 /* Test 0: synchronous client and server */
1293 /* Test 1: event-driven client, synchronous server */
1310 WSA_FLAG_OVERLAPPED
,
1314 /* Test 2: synchronous client, non-blocking server via select() */
1335 /* Test 3: synchronous mixed client and server */
1349 simple_mixed_client
,
1358 static void test_UDP(void)
1360 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1361 possible that this test fails due to dropped packets. */
1363 /* peer 0 receives data from all other peers */
1364 struct sock_info peer
[NUM_UDP_PEERS
];
1366 int ss
, i
, n_recv
, n_sent
;
1368 memset (buf
,0,sizeof(buf
));
1369 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1370 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1372 peer
[i
].addr
.sin_family
= AF_INET
;
1373 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1376 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1378 peer
[i
].addr
.sin_port
= htons ( 0 );
1381 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1383 /* test getsockname() to get peer's port */
1384 ss
= sizeof ( peer
[i
].addr
);
1385 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1386 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1389 /* test getsockname() */
1390 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1392 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1393 /* send client's ip */
1394 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1395 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1396 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1399 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1400 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1401 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1402 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1406 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1412 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1415 struct servent
*pserv
[2];
1417 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1419 /* ensure that necessary buffer resizes are completed */
1420 for ( j
= 0; j
< 2; j
++) {
1421 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1424 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1425 for ( j
= 0; j
< 2; j
++ ) {
1426 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1427 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1428 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
) );
1429 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
);
1430 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
);
1433 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1437 static void test_getservbyname(void)
1440 HANDLE starttest
, thread
[NUM_THREADS
];
1441 DWORD thread_id
[NUM_THREADS
];
1443 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1445 /* create threads */
1446 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1447 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1450 /* signal threads to start */
1451 SetEvent ( starttest
);
1453 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1454 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1458 static void test_WSASocket(void)
1460 SOCKET sock
= INVALID_SOCKET
;
1461 WSAPROTOCOL_INFOA
*pi
;
1462 int providers
[] = {6, 0};
1466 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1467 * to avoid a crash on win98.
1470 ret
= WSAEnumProtocolsA(providers
, NULL
, &pi_size
);
1471 ok(ret
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1473 err
= WSAGetLastError();
1474 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1477 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
1478 ok(pi
!= NULL
, "Failed to allocate memory\n");
1480 skip("Can't continue without memory.\n");
1484 ret
= WSAEnumProtocolsA(providers
, pi
, &pi_size
);
1485 ok(ret
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
1489 skip("No protocols enumerated.\n");
1490 HeapFree(GetProcessHeap(), 0, pi
);
1494 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
1495 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
1496 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1500 HeapFree(GetProcessHeap(), 0, pi
);
1503 static void test_WSAAddressToStringA(void)
1505 SOCKET v6
= INVALID_SOCKET
;
1509 SOCKADDR_IN sockaddr
;
1510 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1512 CHAR expect1
[] = "0.0.0.0";
1513 CHAR expect2
[] = "255.255.255.255";
1514 CHAR expect3
[] = "0.0.0.0:65535";
1515 CHAR expect4
[] = "255.255.255.255:65535";
1517 SOCKADDR_IN6 sockaddr6
;
1518 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1520 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1521 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1522 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1524 CHAR expect6_1
[] = "::1";
1525 CHAR expect6_2
[] = "20ab::1";
1526 CHAR expect6_3
[] = "[20ab::2001]:33274";
1527 CHAR expect6_3_nt
[] = "20ab::2001@33274";
1528 CHAR expect6_3_w2k
[] = "20ab::2001";
1529 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
1530 CHAR expect6_3_2_nt
[] = "4660/20ab::2001@33274";
1531 CHAR expect6_3_2_w2k
[] = "20ab::2001%4660";
1532 CHAR expect6_3_3
[] = "20ab::2001%4660";
1533 CHAR expect6_3_3_nt
[] = "4660/20ab::2001";
1537 sockaddr
.sin_family
= AF_INET
;
1538 sockaddr
.sin_port
= 0;
1539 sockaddr
.sin_addr
.s_addr
= 0;
1541 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1542 GLE
= WSAGetLastError();
1543 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1544 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1547 len
= sizeof(address
);
1549 sockaddr
.sin_family
= AF_INET
;
1550 sockaddr
.sin_port
= 0;
1551 sockaddr
.sin_addr
.s_addr
= 0;
1553 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1554 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1556 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1557 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
1559 len
= sizeof(address
);
1561 sockaddr
.sin_family
= AF_INET
;
1562 sockaddr
.sin_port
= 0;
1563 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1565 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1566 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1568 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1570 len
= sizeof(address
);
1572 sockaddr
.sin_family
= AF_INET
;
1573 sockaddr
.sin_port
= 0xffff;
1574 sockaddr
.sin_addr
.s_addr
= 0;
1576 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1577 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1579 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1581 len
= sizeof(address
);
1583 sockaddr
.sin_family
= AF_INET
;
1584 sockaddr
.sin_port
= 0xffff;
1585 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1587 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1588 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1590 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1591 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
1593 /*check to see it IPv6 is available */
1594 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1595 if (v6
== INVALID_SOCKET
) {
1596 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1597 WSAGetLastError(), WSAEAFNOSUPPORT
);
1600 /* Test a short IPv6 address */
1601 len
= sizeof(address6
);
1603 sockaddr6
.sin6_family
= AF_INET6
;
1604 sockaddr6
.sin6_port
= 0x0000;
1605 sockaddr6
.sin6_scope_id
= 0;
1606 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1608 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1609 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1610 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
1611 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
1613 /* Test a longer IPv6 address */
1614 len
= sizeof(address6
);
1616 sockaddr6
.sin6_family
= AF_INET6
;
1617 sockaddr6
.sin6_port
= 0x0000;
1618 sockaddr6
.sin6_scope_id
= 0;
1619 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1621 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1622 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1623 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
1624 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
1626 /* Test IPv6 address and port number */
1627 len
= sizeof(address6
);
1629 sockaddr6
.sin6_family
= AF_INET6
;
1630 sockaddr6
.sin6_port
= 0xfa81;
1631 sockaddr6
.sin6_scope_id
= 0;
1632 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1634 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1635 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1636 ok( !strcmp( address6
, expect6_3
) ||
1637 broken( !strcmp( address6
, expect6_3_nt
) ) || /* NT4 */
1638 broken( !strcmp( address6
, expect6_3_w2k
) ), /* Win2000 */
1639 "Expected: %s, got: %s\n", expect6_3
, address6
);
1640 ok( len
== sizeof(expect6_3
) ||
1641 broken( len
== sizeof(expect6_3_nt
) ) || /* NT4 */
1642 broken( len
== sizeof(expect6_3_w2k
) ), /* Win2000 */
1643 "Got size %d\n", len
);
1645 /* Test IPv6 address, port number and scope_id */
1646 len
= sizeof(address6
);
1648 sockaddr6
.sin6_family
= AF_INET6
;
1649 sockaddr6
.sin6_port
= 0xfa81;
1650 sockaddr6
.sin6_scope_id
= 0x1234;
1651 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1653 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1654 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1655 ok( !strcmp( address6
, expect6_3_2
) ||
1656 broken( !strcmp( address6
, expect6_3_2_nt
) ) || /* NT4 */
1657 broken( !strcmp( address6
, expect6_3_2_w2k
) ), /* Win2000 */
1658 "Expected: %s, got: %s\n", expect6_3_2
, address6
);
1659 ok( len
== sizeof(expect6_3_2
) ||
1660 broken( len
== sizeof(expect6_3_2_nt
) ) || /* NT4 */
1661 broken( len
== sizeof(expect6_3_2_w2k
) ), /* Win2000 */
1662 "Got size %d\n", len
);
1664 /* Test IPv6 address and scope_id */
1665 len
= sizeof(address6
);
1667 sockaddr6
.sin6_family
= AF_INET6
;
1668 sockaddr6
.sin6_port
= 0x0000;
1669 sockaddr6
.sin6_scope_id
= 0x1234;
1670 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1672 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1673 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1674 ok( !strcmp( address6
, expect6_3_3
) ||
1675 broken( !strcmp( address6
, expect6_3_3_nt
) ), /* NT4 */
1676 "Expected: %s, got: %s\n", expect6_3_3
, address6
);
1677 ok( len
== sizeof(expect6_3_3
) ||
1678 broken( len
== sizeof(expect6_3_3_nt
) ), /* NT4 */
1679 "Got size %d\n", len
);
1682 if (v6
!= INVALID_SOCKET
)
1686 static void test_WSAAddressToStringW(void)
1688 SOCKET v6
= INVALID_SOCKET
;
1692 SOCKADDR_IN sockaddr
;
1693 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1695 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1696 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1697 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1698 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1699 '6', '5', '5', '3', '5', 0 };
1701 SOCKADDR_IN6 sockaddr6
;
1702 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1704 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1705 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1706 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1708 WCHAR expect6_1
[] = {':',':','1',0};
1709 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
1710 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1711 WCHAR expect6_3_nt
[] = {'2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1712 WCHAR expect6_3_w2k
[] = {'2','0','a','b',':',':','2','0','0','1',0};
1713 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1714 WCHAR expect6_3_2_nt
[] = {'4','6','6','0','/','2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
1715 WCHAR expect6_3_2_w2k
[] = {'2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',0};
1716 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1717 WCHAR expect6_3_3_nt
[] = {'6','5','5','3','4','/','2','0','a','b',':',':','2','0','0','1',0};
1721 sockaddr
.sin_family
= AF_INET
;
1722 sockaddr
.sin_port
= 0;
1723 sockaddr
.sin_addr
.s_addr
= 0;
1725 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1726 GLE
= WSAGetLastError();
1727 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1728 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1731 len
= sizeof(address
);
1733 sockaddr
.sin_family
= AF_INET
;
1734 sockaddr
.sin_port
= 0;
1735 sockaddr
.sin_addr
.s_addr
= 0;
1737 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1738 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1740 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1741 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
1743 len
= sizeof(address
);
1745 sockaddr
.sin_family
= AF_INET
;
1746 sockaddr
.sin_port
= 0;
1747 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1749 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1750 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1752 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1754 len
= sizeof(address
);
1756 sockaddr
.sin_family
= AF_INET
;
1757 sockaddr
.sin_port
= 0xffff;
1758 sockaddr
.sin_addr
.s_addr
= 0;
1760 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1761 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1763 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1765 len
= sizeof(address
);
1767 sockaddr
.sin_family
= AF_INET
;
1768 sockaddr
.sin_port
= 0xffff;
1769 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1771 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1772 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1774 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1775 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
1777 /*check to see it IPv6 is available */
1778 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1779 if (v6
== INVALID_SOCKET
) {
1780 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1781 WSAGetLastError(), WSAEAFNOSUPPORT
);
1785 /* Test a short IPv6 address */
1786 len
= sizeof(address6
)/sizeof(WCHAR
);
1788 sockaddr6
.sin6_family
= AF_INET6
;
1789 sockaddr6
.sin6_port
= 0x0000;
1790 sockaddr6
.sin6_scope_id
= 0;
1791 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1793 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1794 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1795 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
1796 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
1798 /* Test a longer IPv6 address */
1799 len
= sizeof(address6
)/sizeof(WCHAR
);
1801 sockaddr6
.sin6_family
= AF_INET6
;
1802 sockaddr6
.sin6_port
= 0x0000;
1803 sockaddr6
.sin6_scope_id
= 0;
1804 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1806 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1807 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1809 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
1810 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1812 /* Test IPv6 address and port number */
1813 len
= sizeof(address6
)/sizeof(WCHAR
);
1815 sockaddr6
.sin6_family
= AF_INET6
;
1816 sockaddr6
.sin6_port
= 0xfa81;
1817 sockaddr6
.sin6_scope_id
= 0;
1818 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1820 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1821 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1822 ok( !lstrcmpW( address6
, expect6_3
) ||
1823 broken( !lstrcmpW( address6
, expect6_3_nt
) ) || /* NT4 */
1824 broken( !lstrcmpW( address6
, expect6_3_w2k
) ), /* Win2000 */
1825 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3
),
1826 wine_dbgstr_w(address6
) );
1827 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
) ||
1828 broken(len
== sizeof(expect6_3_nt
)/sizeof(WCHAR
) ) || /* NT4 */
1829 broken(len
== sizeof(expect6_3_w2k
)/sizeof(WCHAR
) ), /* Win2000 */
1832 /* Test IPv6 address, port number and scope_id */
1833 len
= sizeof(address6
)/sizeof(WCHAR
);
1835 sockaddr6
.sin6_family
= AF_INET6
;
1836 sockaddr6
.sin6_port
= 0xfa81;
1837 sockaddr6
.sin6_scope_id
= 0x1234;
1838 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1840 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1841 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1842 ok( !lstrcmpW( address6
, expect6_3_2
) ||
1843 broken( !lstrcmpW( address6
, expect6_3_2_nt
) ) || /* NT4 */
1844 broken( !lstrcmpW( address6
, expect6_3_2_w2k
) ), /* Win2000 */
1845 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2
),
1846 wine_dbgstr_w(address6
) );
1847 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
) ||
1848 broken( len
== sizeof(expect6_3_2_nt
)/sizeof(WCHAR
) ) || /* NT4 */
1849 broken( len
== sizeof(expect6_3_2_w2k
)/sizeof(WCHAR
) ), /* Win2000 */
1852 /* Test IPv6 address and scope_id */
1853 len
= sizeof(address6
)/sizeof(WCHAR
);
1855 sockaddr6
.sin6_family
= AF_INET6
;
1856 sockaddr6
.sin6_port
= 0x0000;
1857 sockaddr6
.sin6_scope_id
= 0xfffe;
1858 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1860 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1861 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1862 ok( !lstrcmpW( address6
, expect6_3_3
) ||
1863 broken( !lstrcmpW( address6
, expect6_3_3_nt
) ), /* NT4 */
1864 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3
),
1865 wine_dbgstr_w(address6
) );
1866 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
) ||
1867 broken( len
== sizeof(expect6_3_3_nt
)/sizeof(WCHAR
) ), /* NT4 */
1871 if (v6
!= INVALID_SOCKET
)
1875 static void test_WSAStringToAddressA(void)
1878 SOCKADDR_IN sockaddr
;
1879 SOCKADDR_IN6 sockaddr6
;
1882 CHAR address1
[] = "0.0.0.0";
1883 CHAR address2
[] = "127.127.127.127";
1884 CHAR address3
[] = "255.255.255.255";
1885 CHAR address4
[] = "127.127.127.127:65535";
1886 CHAR address5
[] = "255.255.255.255:65535";
1887 CHAR address6
[] = "::1";
1888 CHAR address7
[] = "[::1]";
1889 CHAR address8
[] = "[::1]:65535";
1892 sockaddr
.sin_family
= AF_INET
;
1894 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1895 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1896 WSAGetLastError() );
1898 len
= sizeof(sockaddr
);
1899 sockaddr
.sin_port
= 0;
1900 sockaddr
.sin_addr
.s_addr
= 0;
1902 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1903 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1904 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1906 len
= sizeof(sockaddr
);
1907 sockaddr
.sin_port
= 0;
1908 sockaddr
.sin_addr
.s_addr
= 0;
1910 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1911 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1912 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1914 len
= sizeof(sockaddr
);
1915 sockaddr
.sin_port
= 0;
1916 sockaddr
.sin_addr
.s_addr
= 0;
1918 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1919 GLE
= WSAGetLastError();
1920 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1921 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1922 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1924 len
= sizeof(sockaddr
);
1925 sockaddr
.sin_port
= 0;
1926 sockaddr
.sin_addr
.s_addr
= 0;
1928 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1929 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1930 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1932 len
= sizeof(sockaddr
);
1933 sockaddr
.sin_port
= 0;
1934 sockaddr
.sin_addr
.s_addr
= 0;
1936 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1937 GLE
= WSAGetLastError();
1938 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1939 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1940 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1942 len
= sizeof(sockaddr6
);
1943 memset(&sockaddr6
, 0, len
);
1944 sockaddr6
.sin6_family
= AF_INET6
;
1946 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1948 GLE
= WSAGetLastError();
1949 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1950 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1952 len
= sizeof(sockaddr6
);
1953 memset(&sockaddr6
, 0, len
);
1954 sockaddr6
.sin6_family
= AF_INET6
;
1956 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1958 GLE
= WSAGetLastError();
1959 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1960 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1962 len
= sizeof(sockaddr6
);
1963 memset(&sockaddr6
, 0, len
);
1964 sockaddr6
.sin6_family
= AF_INET6
;
1966 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1968 GLE
= WSAGetLastError();
1969 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1970 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1971 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1975 static void test_WSAStringToAddressW(void)
1978 SOCKADDR_IN sockaddr
, *sin
;
1979 SOCKADDR_IN6 sockaddr6
;
1980 SOCKADDR_STORAGE sockaddr_storage
;
1983 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1984 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1985 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1986 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1987 ':', '6', '5', '5', '3', '5', 0 };
1988 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1989 '6', '5', '5', '3', '5', 0 };
1990 WCHAR address6
[] = {':',':','1','\0'};
1991 WCHAR address7
[] = {'[',':',':','1',']','\0'};
1992 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1995 sockaddr
.sin_family
= AF_INET
;
1997 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1998 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1999 WSAGetLastError() );
2001 len
= sizeof(sockaddr
);
2002 sockaddr
.sin_port
= 0;
2003 sockaddr
.sin_addr
.s_addr
= 0;
2005 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
2006 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
2007 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2009 len
= sizeof(sockaddr
);
2010 sockaddr
.sin_port
= 0;
2011 sockaddr
.sin_addr
.s_addr
= 0;
2013 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
2014 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
2015 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2017 len
= sizeof(sockaddr
);
2018 sockaddr
.sin_port
= 0;
2019 sockaddr
.sin_addr
.s_addr
= 0;
2021 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
2022 GLE
= WSAGetLastError();
2023 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
2024 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
2025 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
2027 len
= sizeof(sockaddr
);
2028 sockaddr
.sin_port
= 0;
2029 sockaddr
.sin_addr
.s_addr
= 0;
2031 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
2032 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
2033 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
2035 len
= sizeof(sockaddr
);
2036 sockaddr
.sin_port
= 0;
2037 sockaddr
.sin_addr
.s_addr
= 0;
2039 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
2040 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
2041 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
2042 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
2044 /* Test with a larger buffer than necessary */
2045 len
= sizeof(sockaddr_storage
);
2046 sin
= (SOCKADDR_IN
*)&sockaddr_storage
;
2048 sin
->sin_addr
.s_addr
= 0;
2050 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)sin
, &len
);
2051 ok( (ret
== 0 && sin
->sin_addr
.s_addr
== 0xffffffff && sin
->sin_port
== 0xffff) ||
2052 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
2053 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
2054 ok( len
== sizeof(SOCKADDR_IN
) ||
2055 broken(len
== sizeof(SOCKADDR_STORAGE
)) /* NT4/2k */,
2056 "unexpected length %d\n", len
);
2058 len
= sizeof(sockaddr6
);
2059 memset(&sockaddr6
, 0, len
);
2060 sockaddr6
.sin6_family
= AF_INET6
;
2062 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
2064 GLE
= WSAGetLastError();
2065 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
2066 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
2068 len
= sizeof(sockaddr6
);
2069 memset(&sockaddr6
, 0, len
);
2070 sockaddr6
.sin6_family
= AF_INET6
;
2072 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
2074 GLE
= WSAGetLastError();
2075 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
2076 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
2078 len
= sizeof(sockaddr6
);
2079 memset(&sockaddr6
, 0, len
);
2080 sockaddr6
.sin6_family
= AF_INET6
;
2082 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
2084 GLE
= WSAGetLastError();
2085 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
2086 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
2087 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
2091 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
2095 struct sockaddr_in addr
;
2096 struct timeval select_timeout
;
2099 FD_SET(par
->s
, &readfds
);
2100 select_timeout
.tv_sec
=5;
2101 select_timeout
.tv_usec
=0;
2102 addr
.sin_family
= AF_INET
;
2103 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
2104 addr
.sin_port
= htons(SERVERPORT
);
2106 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
2107 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
2109 SetEvent(server_ready
);
2110 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
2111 par
->ReadKilled
= (ret
== 1);
2114 static void test_select(void)
2116 SOCKET fdRead
, fdWrite
;
2117 fd_set readfds
, writefds
, exceptfds
;
2120 struct timeval select_timeout
;
2121 select_thread_params thread_params
;
2122 HANDLE thread_handle
;
2125 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
2126 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2127 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
2128 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2132 FD_ZERO(&exceptfds
);
2133 FD_SET(fdRead
, &readfds
);
2134 FD_SET(fdWrite
, &writefds
);
2135 FD_SET(fdRead
, &exceptfds
);
2136 FD_SET(fdWrite
, &exceptfds
);
2137 select_timeout
.tv_sec
=0;
2138 select_timeout
.tv_usec
=500;
2141 if (fdWrite
> maxfd
)
2145 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
2146 ok ( (ret
== 0), "select should not return any socket handles\n");
2147 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
2148 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
2151 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
2152 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
2155 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
2157 ret
= closesocket(fdWrite
);
2158 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
2160 thread_params
.s
= fdRead
;
2161 thread_params
.ReadKilled
= FALSE
;
2162 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2163 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
2164 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
2166 WaitForSingleObject (server_ready
, INFINITE
);
2168 ret
= closesocket(fdRead
);
2169 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
2171 WaitForSingleObject (thread_handle
, 1000);
2172 ok ( (thread_params
.ReadKilled
) ||
2173 broken(thread_params
.ReadKilled
== 0), /*Win98*/
2174 "closesocket did not wakeup select\n");
2176 /* Test selecting invalid handles */
2179 FD_ZERO(&exceptfds
);
2182 ret
= select(maxfd
+1, 0, 0, 0, &select_timeout
);
2183 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
2184 ok ( GetLastError() == WSAEINVAL
, "expected WSAEINVAL, got %i\n", ret
);
2187 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
2188 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
2189 ok ( GetLastError() == WSAEINVAL
, "expected WSAEINVAL, got %i\n", ret
);
2191 FD_SET(INVALID_SOCKET
, &readfds
);
2193 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
2194 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
2195 ok ( GetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", ret
);
2196 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
2199 FD_SET(INVALID_SOCKET
, &writefds
);
2201 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
2202 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
2203 ok ( GetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", ret
);
2204 ok ( !FD_ISSET(fdRead
, &writefds
), "FD should not be set\n");
2207 FD_SET(INVALID_SOCKET
, &exceptfds
);
2209 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
2210 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
2211 ok ( GetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", ret
);
2212 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
2215 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
2217 struct sockaddr_in address
;
2218 int len
= sizeof(address
);
2219 SOCKET client_socket
;
2221 SetEvent(server_ready
);
2222 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
2223 if (client_socket
!= INVALID_SOCKET
)
2224 closesocket(client_socket
);
2225 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
2230 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
2231 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
2232 GROUP FAR
* g
, DWORD_PTR dwCallbackData
)
2237 static void test_accept(void)
2240 SOCKET server_socket
= INVALID_SOCKET
, accepted
= INVALID_SOCKET
, connector
= INVALID_SOCKET
;
2241 struct sockaddr_in address
;
2243 select_thread_params thread_params
;
2244 HANDLE thread_handle
= NULL
;
2247 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
2248 if (server_socket
== INVALID_SOCKET
)
2250 trace("error creating server socket: %d\n", WSAGetLastError());
2254 memset(&address
, 0, sizeof(address
));
2255 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2256 address
.sin_family
= AF_INET
;
2257 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
2260 trace("error binding server socket: %d\n", WSAGetLastError());
2264 socklen
= sizeof(address
);
2265 ret
= getsockname(server_socket
, (struct sockaddr
*)&address
, &socklen
);
2267 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2271 ret
= listen(server_socket
, 5);
2274 trace("error making server socket listen: %d\n", WSAGetLastError());
2278 trace("Blocking accept next\n");
2280 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2281 ok(connector
!= INVALID_SOCKET
, "Failed to create connector socket, error %d\n", WSAGetLastError());
2283 ret
= connect(connector
, (struct sockaddr
*)&address
, sizeof(address
));
2284 ok(ret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2286 accepted
= WSAAccept(server_socket
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
2287 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
2289 accepted
= accept(server_socket
, NULL
, 0);
2290 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2292 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2293 if (server_ready
== INVALID_HANDLE_VALUE
)
2295 trace("error creating event: %d\n", GetLastError());
2299 thread_params
.s
= server_socket
;
2300 thread_params
.ReadKilled
= FALSE
;
2301 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
2302 &thread_params
, 0, &id
);
2303 if (thread_handle
== NULL
)
2305 trace("error creating thread: %d\n", GetLastError());
2309 WaitForSingleObject(server_ready
, INFINITE
);
2311 ret
= closesocket(server_socket
);
2314 trace("closesocket failed: %d\n", WSAGetLastError());
2318 WaitForSingleObject(thread_handle
, 1000);
2319 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
2320 "closesocket did not wakeup accept\n");
2323 if (accepted
!= INVALID_SOCKET
)
2324 closesocket(accepted
);
2325 if (connector
!= INVALID_SOCKET
)
2326 closesocket(connector
);
2327 if (thread_handle
!= NULL
)
2328 CloseHandle(thread_handle
);
2329 if (server_ready
!= INVALID_HANDLE_VALUE
)
2330 CloseHandle(server_ready
);
2331 if (server_socket
!= INVALID_SOCKET
)
2332 closesocket(server_socket
);
2335 static void test_extendedSocketOptions(void)
2339 struct sockaddr_in sa
;
2340 int sa_len
= sizeof(struct sockaddr_in
);
2341 int optval
, optlen
= sizeof(int), ret
;
2345 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2346 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2350 memset(&sa
, 0, sa_len
);
2352 sa
.sin_family
= AF_INET
;
2353 sa
.sin_port
= htons(0);
2354 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2356 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2357 trace("Creating the socket failed: %d\n", WSAGetLastError());
2362 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2363 trace("Failed to bind socket: %d\n", WSAGetLastError());
2369 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2371 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
2372 ok((optval
== 65507) || (optval
== 65527),
2373 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
2375 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2376 SetLastError(0xdeadbeef);
2377 optval
= 0xdeadbeef;
2378 optlen
= sizeof(int);
2379 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2380 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2381 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2382 ret
, WSAGetLastError(), optval
, optval
);
2384 /* more invalid values for level */
2385 SetLastError(0xdeadbeef);
2386 optval
= 0xdeadbeef;
2387 optlen
= sizeof(int);
2388 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2389 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2390 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2391 ret
, WSAGetLastError(), optval
, optval
);
2393 SetLastError(0xdeadbeef);
2394 optval
= 0xdeadbeef;
2395 optlen
= sizeof(int);
2396 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2397 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2398 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2399 ret
, WSAGetLastError(), optval
, optval
);
2401 SetLastError(0xdeadbeef);
2402 optval
= 0xdeadbeef;
2403 optlen
= sizeof(int);
2404 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2405 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2406 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2407 ret
, WSAGetLastError(), optval
, optval
);
2409 SetLastError(0xdeadbeef);
2410 optval
= 0xdeadbeef;
2411 optlen
= sizeof(int);
2412 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2413 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2414 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2415 ret
, WSAGetLastError(), optval
, optval
);
2417 optlen
= sizeof(LINGER
);
2418 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2420 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
2425 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2426 trace("Creating the socket failed: %d\n", WSAGetLastError());
2431 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2432 trace("Failed to bind socket: %d\n", WSAGetLastError());
2438 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2439 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
2441 optlen
= sizeof(BOOL
);
2442 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
2443 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
2444 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
2445 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2446 bool_opt_val
, linger_val
.l_onoff
);
2452 static void test_getsockname(void)
2456 struct sockaddr_in sa_set
, sa_get
;
2457 int sa_set_len
= sizeof(struct sockaddr_in
);
2458 int sa_get_len
= sa_set_len
;
2459 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
2462 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2463 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2467 memset(&sa_set
, 0, sa_set_len
);
2469 sa_set
.sin_family
= AF_INET
;
2470 sa_set
.sin_port
= htons(0);
2471 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2473 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2474 trace("Creating the socket failed: %d\n", WSAGetLastError());
2479 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
2480 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
2481 ok(0, "getsockname on unbound socket should fail\n");
2483 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
2484 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
2485 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
2486 "failed getsockname modified sockaddr when it shouldn't\n");
2489 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
2490 trace("Failed to bind socket: %d\n", WSAGetLastError());
2496 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
2497 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2503 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
2504 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
2505 "getsockname did not zero the sockaddr_in structure\n");
2511 static void test_dns(void)
2515 h
= gethostbyname("");
2516 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
2519 /* Our winsock headers don't define gethostname because it conflicts with the
2520 * definition in unistd.h. Define it here to get rid of the warning. */
2522 int WINAPI
gethostname(char *name
, int namelen
);
2524 static void test_gethostbyname_hack(void)
2528 static BYTE loopback
[] = {127, 0, 0, 1};
2529 static BYTE magic_loopback
[] = {127, 12, 34, 56};
2532 ret
= gethostname(name
, 256);
2533 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
2535 he
= gethostbyname("localhost");
2536 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
2539 if(he
->h_length
!= 4)
2541 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2545 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
2546 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2547 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
2548 he
->h_addr_list
[0][3]);
2551 if(strcmp(name
, "localhost") == 0)
2553 skip("hostname seems to be \"localhost\", skipping test.\n");
2558 he
= gethostbyname(name
);
2559 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
2562 if(he
->h_length
!= 4)
2564 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2568 if (he
->h_addr_list
[0][0] == 127)
2570 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
2571 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2572 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
2573 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
2577 gethostbyname("nonexistent.winehq.org");
2578 /* Don't check for the return value, as some braindead ISPs will kindly
2579 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2582 static void test_inet_addr(void)
2586 addr
= inet_addr(NULL
);
2587 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
2590 static void test_addr_to_print(void)
2596 struct in6_addr in6
;
2598 u_long addr0_Num
= 0x00000000;
2599 PCSTR addr0_Str
= "0.0.0.0";
2600 u_long addr1_Num
= 0x20201015;
2601 PCSTR addr1_Str
= "21.16.32.32";
2602 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2603 PCSTR addr2_Str
= "::fffe:cc98:bd74";
2604 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
2605 PCSTR addr3_Str
= "2030:a4b1::";
2606 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2607 PCSTR addr4_Str
= "::204.152.189.116";
2609 /* Test IPv4 addresses */
2610 in
.s_addr
= addr0_Num
;
2612 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2613 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2614 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
2616 /* Test that inet_ntoa and inet_ntop return the same value */
2617 in
.S_un
.S_addr
= addr1_Num
;
2618 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2619 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2620 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2622 /* InetNtop became available in Vista and Win2008 */
2625 win_skip("InetNtop not present, not executing tests\n");
2629 /* Second part of test */
2630 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
2631 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
2632 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2634 /* Test invalid parm conditions */
2635 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
2636 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2637 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
2639 /* Test Null destination */
2641 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
2642 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2643 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2644 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2646 /* Test zero length passed */
2649 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
2650 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2651 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2652 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2654 /* Test length one shorter than the address length */
2657 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
2658 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2659 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2660 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2662 /* Test longer length is ok */
2665 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
2666 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2667 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2669 /* Test the IPv6 addresses */
2671 /* Test an zero prefixed IPV6 address */
2672 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2673 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2674 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2675 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
2677 /* Test an zero suffixed IPV6 address */
2678 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
2679 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2680 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2681 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
2683 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2684 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
2685 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2686 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2687 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
2689 /* Test invalid parm conditions */
2690 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2692 /* Test Null destination */
2694 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
2695 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2696 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2697 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2699 /* Test zero length passed */
2702 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
2703 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2704 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2705 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2707 /* Test length one shorter than the address length */
2710 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
2711 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2712 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2713 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2715 /* Test longer length is ok */
2718 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
2719 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2722 static void test_ioctlsocket(void)
2726 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
2730 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2731 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
2732 if(sock
== INVALID_SOCKET
)
2734 skip("Can't continue without a socket.\n");
2738 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
2740 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2741 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
2742 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
2743 ret
= WSAGetLastError();
2744 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
2747 /* A fresh and not connected socket has no urgent data, this test shows
2748 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2750 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
2751 if(ret
!= SOCKET_ERROR
)
2752 todo_wine
ok(arg
, "expected a non-zero value\n");
2755 static int drain_pause
=0;
2756 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
2759 SOCKET sock
= *(SOCKET
*)arg
;
2762 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
2766 if (WSAGetLastError() == WSAEWOULDBLOCK
)
2770 FD_SET(sock
, &readset
);
2771 select(0, &readset
, NULL
, NULL
, NULL
);
2782 static void test_send(void)
2784 SOCKET src
= INVALID_SOCKET
;
2785 SOCKET dst
= INVALID_SOCKET
;
2786 HANDLE hThread
= NULL
;
2787 const int buflen
= 1024*1024;
2788 char *buffer
= NULL
;
2789 int ret
, i
, zero
= 0;
2793 DWORD id
, bytes_sent
, dwRet
;
2795 memset(&ov
, 0, sizeof(ov
));
2797 if (tcp_socketpair(&src
, &dst
) != 0)
2799 ok(0, "creating socket pair failed, skipping test\n");
2803 set_blocking(dst
, FALSE
);
2804 /* force disable buffering so we can get a pending overlapped request */
2805 ret
= setsockopt(dst
, SOL_SOCKET
, SO_SNDBUF
, (char *) &zero
, sizeof(zero
));
2806 ok(!ret
, "setsockopt SO_SNDBUF failed: %d - %d\n", ret
, GetLastError());
2808 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2809 if (hThread
== NULL
)
2811 ok(0, "CreateThread failed, error %d\n", GetLastError());
2815 buffer
= HeapAlloc(GetProcessHeap(), 0, buflen
);
2818 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2822 /* fill the buffer with some nonsense */
2823 for (i
= 0; i
< buflen
; ++i
)
2825 buffer
[i
] = (char) i
;
2828 ret
= send(src
, buffer
, buflen
, 0);
2830 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
2832 ok(0, "send failed, error %d\n", WSAGetLastError());
2837 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2838 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
2843 ret
= WSASend(dst
, &buf
, 1, &bytes_sent
, 0, &ov
, NULL
);
2844 ok((ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
) || broken(bytes_sent
== buflen
),
2845 "Failed to start overlapped send %d - %d - %d/%d\n", ret
, WSAGetLastError(), bytes_sent
, buflen
);
2847 /* don't check for completion yet, we may need to drain the buffer while still sending */
2848 set_blocking(src
, FALSE
);
2849 for (i
= 0; i
< buflen
; ++i
)
2853 ret
= recv(src
, buffer
, 1, 0);
2854 while (ret
== SOCKET_ERROR
&& GetLastError() == WSAEWOULDBLOCK
&& j
< 100)
2858 ret
= recv(src
, buffer
, 1, 0);
2861 ok(ret
== 1, "Failed to receive data %d - %d (got %d/%d)\n", ret
, GetLastError(), i
, buflen
);
2865 ok(buffer
[0] == (char) i
, "Received bad data at position %d\n", i
);
2868 dwRet
= WaitForSingleObject(ov
.hEvent
, 1000);
2869 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
2870 if (dwRet
== WAIT_OBJECT_0
)
2872 bret
= GetOverlappedResult((HANDLE
)dst
, &ov
, &bytes_sent
, FALSE
);
2873 ok((bret
&& bytes_sent
== buflen
) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
2874 "Got %d instead of %d (%d - %d)\n", bytes_sent
, buflen
, bret
, GetLastError());
2878 if (src
!= INVALID_SOCKET
)
2880 if (dst
!= INVALID_SOCKET
)
2882 if (hThread
!= NULL
)
2883 CloseHandle(hThread
);
2885 CloseHandle(ov
.hEvent
);
2886 HeapFree(GetProcessHeap(), 0, buffer
);
2889 typedef struct async_message
2893 struct async_message
*next
;
2896 static struct async_message
*messages_received
;
2898 #define WM_SOCKET (WM_USER+100)
2899 static LRESULT CALLBACK
ws2_test_WndProc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
2901 struct async_message
*message
;
2906 message
= HeapAlloc(GetProcessHeap(), 0, sizeof(*message
));
2907 message
->socket
= (SOCKET
) wparam
;
2908 message
->lparam
= lparam
;
2909 message
->next
= NULL
;
2911 if (messages_received
)
2913 struct async_message
*last
= messages_received
;
2914 while (last
->next
) last
= last
->next
;
2915 last
->next
= message
;
2918 messages_received
= message
;
2922 return DefWindowProc(hwnd
, msg
, wparam
, lparam
);
2925 static void get_event_details(int event
, int *bit
, char *name
)
2930 if (bit
) *bit
= FD_ACCEPT_BIT
;
2931 if (name
) strcpy(name
, "FD_ACCEPT");
2934 if (bit
) *bit
= FD_CONNECT_BIT
;
2935 if (name
) strcpy(name
, "FD_CONNECT");
2938 if (bit
) *bit
= FD_READ_BIT
;
2939 if (name
) strcpy(name
, "FD_READ");
2942 if (bit
) *bit
= FD_OOB_BIT
;
2943 if (name
) strcpy(name
, "FD_OOB");
2946 if (bit
) *bit
= FD_WRITE_BIT
;
2947 if (name
) strcpy(name
, "FD_WRITE");
2950 if (bit
) *bit
= FD_CLOSE_BIT
;
2951 if (name
) strcpy(name
, "FD_CLOSE");
2955 if (name
) sprintf(name
, "bad%x", event
);
2959 static const char *dbgstr_event_seq(const LPARAM
*seq
)
2961 static char message
[1024];
2969 get_event_details(WSAGETSELECTEVENT(*seq
), NULL
, name
);
2970 len
+= sprintf(message
+ len
, "%s(%d) ", name
, WSAGETSELECTERROR(*seq
));
2974 strcpy( message
+ len
, "]" );
2978 static char *dbgstr_event_seq_result(SOCKET s
, WSANETWORKEVENTS
*netEvents
)
2980 static char message
[1024];
2981 struct async_message
*curr
= messages_received
;
2982 int index
, error
, bit
= 0;
2992 if (bit
>= FD_MAX_EVENTS
) break;
2993 if ( !(netEvents
->lNetworkEvents
& (1 << bit
)) )
2998 get_event_details(1 << bit
, &index
, name
);
2999 error
= netEvents
->iErrorCode
[index
];
3005 if (curr
->socket
!= s
)
3010 get_event_details(WSAGETSELECTEVENT(curr
->lparam
), NULL
, name
);
3011 error
= WSAGETSELECTERROR(curr
->lparam
);
3015 len
+= sprintf(message
+ len
, "%s(%d) ", name
, error
);
3018 strcpy( message
+ len
, "]" );
3022 static void flush_events(SOCKET s
, HANDLE hEvent
)
3024 WSANETWORKEVENTS netEvents
;
3025 struct async_message
*prev
= NULL
, *curr
= messages_received
;
3029 if (hEvent
!= INVALID_HANDLE_VALUE
)
3031 dwRet
= WaitForSingleObject(hEvent
, 100);
3032 if (dwRet
== WAIT_OBJECT_0
)
3034 ret
= WSAEnumNetworkEvents(s
, hEvent
, &netEvents
);
3036 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
3043 if (curr
->socket
== s
)
3045 if (prev
) prev
->next
= curr
->next
;
3046 else messages_received
= curr
->next
;
3048 HeapFree(GetProcessHeap(), 0, curr
);
3050 if (prev
) curr
= prev
->next
;
3051 else curr
= messages_received
;
3062 static int match_event_sequence(SOCKET s
, WSANETWORKEVENTS
*netEvents
, const LPARAM
*seq
)
3064 int event
, index
, error
, events
;
3065 struct async_message
*curr
;
3069 events
= netEvents
->lNetworkEvents
;
3072 event
= WSAGETSELECTEVENT(*seq
);
3073 error
= WSAGETSELECTERROR(*seq
);
3074 get_event_details(event
, &index
, NULL
);
3076 if (!(events
& event
) && index
!= -1)
3078 if (events
& event
&& index
!= -1)
3080 if (netEvents
->iErrorCode
[index
] != error
)
3091 curr
= messages_received
;
3094 if (curr
->socket
== s
)
3096 if (!*seq
) return 0;
3097 if (*seq
!= curr
->lparam
) return 0;
3108 /* checks for a sequence of events, (order only checked if window is used) */
3109 static void ok_event_sequence(SOCKET s
, HANDLE hEvent
, const LPARAM
*seq
, const LPARAM
**broken_seqs
, int completelyBroken
)
3112 WSANETWORKEVENTS events
, *netEvents
= NULL
;
3116 if (hEvent
!= INVALID_HANDLE_VALUE
)
3118 netEvents
= &events
;
3120 dwRet
= WaitForSingleObject(hEvent
, 200);
3121 if (dwRet
== WAIT_OBJECT_0
)
3123 ret
= WSAEnumNetworkEvents(s
, hEvent
, netEvents
);
3126 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
3131 memset(netEvents
, 0, sizeof(*netEvents
));
3136 /* Run the message loop a little */
3137 while (PeekMessageA( &msg
, 0, 0, 0, PM_REMOVE
))
3139 DispatchMessageA(&msg
);
3143 if (match_event_sequence(s
, netEvents
, seq
))
3145 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq
));
3146 flush_events(s
, hEvent
);
3152 for (; *broken_seqs
; broken_seqs
++)
3154 if (match_event_sequence(s
, netEvents
, *broken_seqs
))
3156 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s
, netEvents
), dbgstr_event_seq(seq
));
3157 flush_events(s
, hEvent
);
3163 winetest_ok(broken(completelyBroken
), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq
),
3164 dbgstr_event_seq_result(s
, netEvents
));
3165 flush_events(s
, hEvent
);
3168 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
3170 static void test_events(int useMessages
)
3172 SOCKET server
= INVALID_SOCKET
;
3173 SOCKET src
= INVALID_SOCKET
, src2
= INVALID_SOCKET
;
3174 SOCKET dst
= INVALID_SOCKET
, dst2
= INVALID_SOCKET
;
3175 struct sockaddr_in addr
;
3176 HANDLE hThread
= NULL
;
3177 HANDLE hEvent
= INVALID_HANDLE_VALUE
, hEvent2
= INVALID_HANDLE_VALUE
;
3178 WNDCLASSEX wndclass
;
3180 char *buffer
= NULL
;
3181 int bufferSize
= 1024*1024;
3185 DWORD bytesReturned
;
3191 static char szClassName
[] = "wstestclass";
3192 const LPARAM
*broken_seq
[3];
3193 static const LPARAM empty_seq
[] = { 0 };
3194 static const LPARAM close_seq
[] = { WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
3195 static const LPARAM write_seq
[] = { WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
3196 static const LPARAM read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
3197 static const LPARAM oob_seq
[] = { WSAMAKESELECTREPLY(FD_OOB
, 0), 0 };
3198 static const LPARAM connect_seq
[] = { WSAMAKESELECTREPLY(FD_CONNECT
, 0),
3199 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
3200 static const LPARAM read_read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
3201 WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
3202 static const LPARAM read_write_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
3203 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
3204 static const LPARAM read_close_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
3205 WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
3207 memset(&ov
, 0, sizeof(ov
));
3208 memset(&ov2
, 0, sizeof(ov2
));
3210 /* don't use socketpair, we want connection event */
3211 src
= socket(AF_INET
, SOCK_STREAM
, 0);
3212 if (src
== INVALID_SOCKET
)
3214 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3218 src2
= socket(AF_INET
, SOCK_STREAM
, 0);
3219 if (src2
== INVALID_SOCKET
)
3221 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3226 if (getsockopt(src
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&bret
, &len
) == SOCKET_ERROR
)
3228 ok(0, "failed to get oobinline status, %d\n", GetLastError());
3231 ok(bret
== FALSE
, "OOB not inline\n");
3235 trace("Event test using messages\n");
3237 wndclass
.cbSize
= sizeof(wndclass
);
3238 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
3239 wndclass
.lpfnWndProc
= ws2_test_WndProc
;
3240 wndclass
.cbClsExtra
= 0;
3241 wndclass
.cbWndExtra
= 0;
3242 wndclass
.hInstance
= GetModuleHandle(NULL
);
3243 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
3244 wndclass
.hIconSm
= LoadIcon(NULL
, IDI_APPLICATION
);
3245 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
3246 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
3247 wndclass
.lpszClassName
= szClassName
;
3248 wndclass
.lpszMenuName
= NULL
;
3249 RegisterClassEx(&wndclass
);
3251 hWnd
= CreateWindow(szClassName
, "WS2Test", WS_OVERLAPPEDWINDOW
, 0, 0, 500, 500, NULL
, NULL
, GetModuleHandle(NULL
), NULL
);
3254 ok(0, "failed to create window: %d\n", GetLastError());
3258 ret
= WSAAsyncSelect(src
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
3261 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
3265 ret
= WSAAsyncSelect(src2
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
3268 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
3274 trace("Event test using events\n");
3276 hEvent
= WSACreateEvent();
3277 if (hEvent
== INVALID_HANDLE_VALUE
)
3279 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3283 hEvent2
= WSACreateEvent();
3284 if (hEvent2
== INVALID_HANDLE_VALUE
)
3286 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
3290 ret
= WSAEventSelect(src
, hEvent
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
3293 ok(0, "WSAEventSelect failed, error %d\n", ret
);
3297 ret
= WSAEventSelect(src2
, hEvent2
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
3300 ok(0, "WSAEventSelect failed, error %d\n", ret
);
3305 server
= socket(AF_INET
, SOCK_STREAM
, 0);
3306 if (server
== INVALID_SOCKET
)
3308 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3312 memset(&addr
, 0, sizeof(addr
));
3313 addr
.sin_family
= AF_INET
;
3314 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3315 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
3318 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3323 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
3326 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3330 ret
= listen(server
, 2);
3333 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3337 ret
= connect(src
, (struct sockaddr
*)&addr
, sizeof(addr
));
3338 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3340 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3344 ret
= connect(src2
, (struct sockaddr
*)&addr
, sizeof(addr
));
3345 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3347 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3352 dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3353 if (dst
== INVALID_SOCKET
)
3355 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3360 dst2
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3361 if (dst2
== INVALID_SOCKET
)
3363 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3367 closesocket(server
);
3368 server
= INVALID_SOCKET
;
3370 /* On Windows it seems when a non-blocking socket sends to a
3371 blocking socket on the same host, the send() is BLOCKING,
3372 so make both sockets non-blocking. src is already non-blocking
3373 from the async select */
3375 if (set_blocking(dst
, FALSE
))
3377 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3381 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
3384 ok(0, "could not allocate memory for test\n");
3388 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3389 if (ov
.hEvent
== NULL
)
3391 ok(0, "could not create event object, errno = %d\n", GetLastError());
3395 ov2
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3396 if (ov2
.hEvent
== NULL
)
3398 ok(0, "could not create event object, errno = %d\n", GetLastError());
3402 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3403 ok_event_seq(src
, hEvent
, connect_seq
, NULL
, 1);
3404 ok_event_seq(src2
, hEvent2
, connect_seq
, NULL
, 1);
3405 /* broken on all windows - FD_CONNECT error is garbage */
3407 /* Test simple send/recv */
3408 ret
= send(dst
, buffer
, 100, 0);
3409 ok(ret
== 100, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3410 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3412 ret
= recv(src
, buffer
, 50, 0);
3413 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3414 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3416 ret
= recv(src
, buffer
, 50, 0);
3417 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3418 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3420 /* fun fact - events are reenabled even on failure, but only for messages */
3421 ret
= send(dst
, "1", 1, 0);
3422 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3423 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3425 ret
= recv(src
, buffer
, -1, 0);
3426 ok(ret
== SOCKET_ERROR
&& (GetLastError() == WSAEFAULT
|| GetLastError() == WSAENOBUFS
),
3427 "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3430 broken_seq
[0] = empty_seq
; /* win9x */
3431 broken_seq
[1] = NULL
;
3432 todo_wine
ok_event_seq(src
, hEvent
, read_seq
, broken_seq
, 0);
3435 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3437 ret
= recv(src
, buffer
, 1, 0);
3438 ok(ret
== 1, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3439 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3441 /* Interaction with overlapped */
3442 bufs
.len
= sizeof(char);
3444 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3445 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3446 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3448 bufs
.len
= sizeof(char);
3449 bufs
.buf
= buffer
+1;
3450 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov2
, NULL
);
3451 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3452 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3454 ret
= send(dst
, "12", 2, 0);
3455 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3456 broken_seq
[0] = read_read_seq
; /* win9x */
3457 broken_seq
[1] = NULL
;
3458 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3460 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3461 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3462 if (dwRet
== WAIT_OBJECT_0
)
3464 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3465 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3466 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3467 ok(buffer
[0] == '1', "Got %c instead of 1\n", buffer
[0]);
3470 dwRet
= WaitForSingleObject(ov2
.hEvent
, 100);
3471 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3472 if (dwRet
== WAIT_OBJECT_0
)
3474 bret
= GetOverlappedResult((HANDLE
)src
, &ov2
, &bytesReturned
, FALSE
);
3475 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3476 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3477 ok(buffer
[1] == '2', "Got %c instead of 2\n", buffer
[1]);
3480 ret
= send(dst
, "1", 1, 0);
3481 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3482 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3484 ret
= recv(src
, buffer
, 1, 0);
3485 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3486 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3488 /* Notifications are delivered as soon as possible, blocked only on
3489 * async requests on the same type */
3490 bufs
.len
= sizeof(char);
3492 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3493 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3494 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3497 ret
= send(dst
, "1", 1, MSG_OOB
);
3498 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3499 ok_event_seq(src
, hEvent
, oob_seq
, NULL
, 0);
3502 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3503 ok(dwRet
== WAIT_TIMEOUT
, "OOB message activated read?: %d - %d\n", dwRet
, GetLastError());
3505 ret
= send(dst
, "2", 1, 0);
3506 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3507 broken_seq
[0] = read_seq
; /* win98 */
3508 broken_seq
[1] = NULL
;
3509 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3511 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3512 ok(dwRet
== WAIT_OBJECT_0
|| broken(dwRet
== WAIT_TIMEOUT
),
3513 "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3514 if (dwRet
== WAIT_OBJECT_0
)
3516 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3517 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3518 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3519 ok(buffer
[0] == '2', "Got %c instead of 2\n", buffer
[0]);
3521 else if (dwRet
== WAIT_TIMEOUT
)
3523 /* this happens on win98. We get an FD_READ later on the next test */
3524 CancelIo((HANDLE
) src
);
3528 ret
= recv(src
, buffer
, 1, MSG_OOB
);
3529 todo_wine
ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3530 /* We get OOB notification, but no data on wine */
3531 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3534 /* Flood the send queue */
3535 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
3536 if (hThread
== NULL
)
3538 ok(0, "CreateThread failed, error %d\n", GetLastError());
3542 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3543 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3545 /* Now if we send a ton of data and the 'server' does not drain it fast
3546 * enough (set drain_pause to be sure), the socket send buffer will only
3547 * take some of it, and we will get a short write. This will trigger
3548 * another FD_WRITE event as soon as data is sent and more space becomes
3549 * available, but not any earlier. */
3553 ret
= send(src
, buffer
, bufferSize
, 0);
3554 } while (ret
== bufferSize
);
3556 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
3558 Sleep(400); /* win9x */
3559 broken_seq
[0] = read_write_seq
;
3560 broken_seq
[1] = NULL
;
3561 ok_event_seq(src
, hEvent
, write_seq
, broken_seq
, 0);
3565 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3568 /* Test how FD_CLOSE is handled */
3569 ret
= send(dst
, "12", 2, 0);
3570 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3572 /* Wait a little and let the send complete */
3575 dst
= INVALID_SOCKET
;
3578 /* We can never implement this in wine, best we can hope for is
3579 sending FD_CLOSE after the reads complete */
3580 broken_seq
[0] = read_seq
; /* win9x */
3581 broken_seq
[1] = NULL
;
3582 todo_wine
ok_event_seq(src
, hEvent
, read_close_seq
, broken_seq
, 0);
3584 ret
= recv(src
, buffer
, 1, 0);
3585 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3586 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3588 ret
= recv(src
, buffer
, 1, 0);
3589 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3590 /* want it? it's here, but you can't have it */
3591 broken_seq
[0] = close_seq
; /* win9x */
3592 broken_seq
[1] = NULL
;
3593 todo_wine
ok_event_seq(src
, hEvent
, empty_seq
, /* wine sends FD_CLOSE here */
3596 /* Test how FD_CLOSE is handled */
3597 ret
= send(dst2
, "12", 2, 0);
3598 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3601 shutdown(dst2
, SD_SEND
);
3604 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3605 regressions, don't mark them as todo_wine, and mark windows as broken */
3606 broken_seq
[0] = read_close_seq
;
3607 broken_seq
[1] = close_seq
;
3608 broken_seq
[2] = NULL
;
3609 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3611 ret
= recv(src2
, buffer
, 1, 0);
3612 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3613 broken_seq
[0] = close_seq
; /* win98 */
3614 broken_seq
[1] = NULL
;
3615 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3617 ret
= recv(src2
, buffer
, 1, 0);
3618 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3619 broken_seq
[0] = empty_seq
;
3620 broken_seq
[1] = NULL
;
3621 ok_event_seq(src2
, hEvent2
, close_seq
, broken_seq
, 0);
3623 ret
= send(src2
, "1", 1, 0);
3624 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3625 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3627 ret
= send(src2
, "1", 1, 0);
3628 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3629 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3632 if (src
!= INVALID_SOCKET
)
3634 flush_events(src
, hEvent
);
3637 if (src2
!= INVALID_SOCKET
)
3639 flush_events(src2
, hEvent2
);
3642 HeapFree(GetProcessHeap(), 0, buffer
);
3643 if (server
!= INVALID_SOCKET
)
3644 closesocket(server
);
3645 if (dst
!= INVALID_SOCKET
)
3647 if (dst2
!= INVALID_SOCKET
)
3649 if (hThread
!= NULL
)
3650 CloseHandle(hThread
);
3654 CloseHandle(hEvent
);
3655 if (hEvent2
!= NULL
)
3656 CloseHandle(hEvent2
);
3657 if (ov
.hEvent
!= NULL
)
3658 CloseHandle(ov
.hEvent
);
3659 if (ov2
.hEvent
!= NULL
)
3660 CloseHandle(ov2
.hEvent
);
3663 static void test_ipv6only(void)
3665 SOCKET v4
= INVALID_SOCKET
,
3666 v6
= INVALID_SOCKET
;
3667 struct sockaddr_in sin4
;
3668 struct sockaddr_in6 sin6
;
3671 memset(&sin4
, 0, sizeof(sin4
));
3672 sin4
.sin_family
= AF_INET
;
3673 sin4
.sin_port
= htons(SERVERPORT
);
3675 memset(&sin6
, 0, sizeof(sin6
));
3676 sin6
.sin6_family
= AF_INET6
;
3677 sin6
.sin6_port
= htons(SERVERPORT
);
3679 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
3680 if (v6
== INVALID_SOCKET
) {
3681 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3682 WSAGetLastError(), WSAEAFNOSUPPORT
);
3685 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
3687 skip("Could not bind IPv6 address (LastError: %d).\n",
3692 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
3693 if (v4
== INVALID_SOCKET
) {
3694 skip("Could not create IPv4 socket (LastError: %d).\n",
3698 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
3699 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
3700 WSAGetLastError(), WSAEADDRINUSE
);
3703 if (v4
!= INVALID_SOCKET
)
3705 if (v6
!= INVALID_SOCKET
)
3709 static void test_WSASendTo(void)
3712 struct sockaddr_in addr
;
3713 char buf
[12] = "hello world";
3717 addr
.sin_family
= AF_INET
;
3718 addr
.sin_port
= htons(139);
3719 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3720 data_buf
.len
= sizeof(buf
);
3723 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
3724 ok(0, "socket() failed error: %d\n", WSAGetLastError());
3728 WSASetLastError(12345);
3729 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
3730 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
3733 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
3734 "a successful call to WSASendTo()\n");
3737 static void test_WSARecv(void)
3743 DWORD bytesReturned
;
3750 tcp_socketpair(&src
, &dest
);
3751 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
3753 skip("failed to create sockets\n");
3757 bufs
.len
= sizeof(buf
);
3761 memset(&ov
, 0, sizeof(ov
));
3762 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3763 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
3769 iret
= setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
));
3770 ok(!iret
, "Failed to set linger %d\n", GetLastError());
3772 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3773 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
3776 src
= INVALID_SOCKET
;
3778 dwret
= WaitForSingleObject(ov
.hEvent
, 1000);
3779 ok(dwret
== WAIT_OBJECT_0
, "Waiting for disconnect event failed with %d + errno %d\n", dwret
, GetLastError());
3781 bret
= GetOverlappedResult((HANDLE
)dest
, &ov
, &bytesReturned
, FALSE
);
3782 todo_wine
ok(!bret
&& (GetLastError() == ERROR_NETNAME_DELETED
|| broken(GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */),
3783 "Did not get disconnect event: %d, error %d\n", bret
, GetLastError());
3784 ok(bytesReturned
== 0, "Bytes received is %d\n", bytesReturned
);
3787 if (dest
!= INVALID_SOCKET
)
3789 if (src
!= INVALID_SOCKET
)
3792 WSACloseEvent(ov
.hEvent
);
3795 static void test_GetAddrInfoW(void)
3797 static const WCHAR port
[] = {'8','0',0};
3798 static const WCHAR empty
[] = {0};
3799 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
3802 ADDRINFOW
*result
, hint
;
3804 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
3806 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
3810 memset(&hint
, 0, sizeof(ADDRINFOW
));
3812 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
3813 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
3816 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
3819 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3820 ok(result
!= NULL
, "GetAddrInfoW failed\n");
3822 pFreeAddrInfoW(result
);
3824 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
3825 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3826 pFreeAddrInfoW(result
);
3828 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
3829 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3830 pFreeAddrInfoW(result
);
3832 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
3833 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3834 pFreeAddrInfoW(result
);
3837 static void test_ConnectEx(void)
3839 SOCKET listener
= INVALID_SOCKET
;
3840 SOCKET acceptor
= INVALID_SOCKET
;
3841 SOCKET connector
= INVALID_SOCKET
;
3842 struct sockaddr_in address
, conaddress
;
3844 OVERLAPPED overlapped
;
3845 LPFN_CONNECTEX pConnectEx
;
3846 GUID connectExGuid
= WSAID_CONNECTEX
;
3847 DWORD bytesReturned
;
3853 memset(&overlapped
, 0, sizeof(overlapped
));
3855 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
3856 if (listener
== INVALID_SOCKET
) {
3857 skip("could not create listener socket, error %d\n", WSAGetLastError());
3861 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3862 if (connector
== INVALID_SOCKET
) {
3863 skip("could not create connector socket, error %d\n", WSAGetLastError());
3867 memset(&address
, 0, sizeof(address
));
3868 address
.sin_family
= AF_INET
;
3869 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3870 iret
= bind(listener
, (struct sockaddr
*)&address
, sizeof(address
));
3872 skip("failed to bind, error %d\n", WSAGetLastError());
3876 addrlen
= sizeof(address
);
3877 iret
= getsockname(listener
, (struct sockaddr
*)&address
, &addrlen
);
3879 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
3883 if (set_blocking(listener
, TRUE
)) {
3884 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3888 iret
= WSAIoctl(connector
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &connectExGuid
, sizeof(connectExGuid
),
3889 &pConnectEx
, sizeof(pConnectEx
), &bytesReturned
, NULL
, NULL
);
3891 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret
, WSAGetLastError());
3895 bret
= pConnectEx(INVALID_SOCKET
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3896 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "ConnectEx on invalid socket "
3897 "returned %d + errno %d\n", bret
, WSAGetLastError());
3899 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3900 todo_wine
ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "ConnectEx on a unbound socket "
3901 "returned %d + errno %d\n", bret
, WSAGetLastError());
3902 if (bret
== TRUE
|| WSAGetLastError() != WSAEINVAL
)
3904 acceptor
= accept(listener
, NULL
, NULL
);
3905 if (acceptor
!= INVALID_SOCKET
) {
3906 closesocket(acceptor
);
3907 acceptor
= INVALID_SOCKET
;
3910 closesocket(connector
);
3911 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3912 if (connector
== INVALID_SOCKET
) {
3913 skip("could not create connector socket, error %d\n", WSAGetLastError());
3918 /* ConnectEx needs a bound socket */
3919 memset(&conaddress
, 0, sizeof(conaddress
));
3920 conaddress
.sin_family
= AF_INET
;
3921 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3922 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
3924 skip("failed to bind, error %d\n", WSAGetLastError());
3928 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, NULL
);
3929 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "ConnectEx on a NULL overlapped "
3930 "returned %d + errno %d\n", bret
, WSAGetLastError());
3932 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3933 if (overlapped
.hEvent
== NULL
) {
3934 skip("could not create event object, errno = %d\n", GetLastError());
3938 iret
= listen(listener
, 1);
3940 skip("listening failed, errno = %d\n", WSAGetLastError());
3944 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3945 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
3946 "returned %d + errno %d\n", bret
, WSAGetLastError());
3947 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
3948 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
3950 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
3951 ok(bret
, "Connecting failed, error %d\n", GetLastError());
3952 ok(bytesReturned
== 0, "Bytes sent is %d\n", bytesReturned
);
3954 closesocket(connector
);
3955 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3956 if (connector
== INVALID_SOCKET
) {
3957 skip("could not create connector socket, error %d\n", WSAGetLastError());
3960 /* ConnectEx needs a bound socket */
3961 memset(&conaddress
, 0, sizeof(conaddress
));
3962 conaddress
.sin_family
= AF_INET
;
3963 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3964 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
3966 skip("failed to bind, error %d\n", WSAGetLastError());
3970 acceptor
= accept(listener
, NULL
, NULL
);
3971 if (acceptor
!= INVALID_SOCKET
) {
3972 closesocket(acceptor
);
3978 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, buffer
, 3, &bytesReturned
, &overlapped
);
3979 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
3980 "returned %d + errno %d\n", bret
, WSAGetLastError());
3981 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
3982 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
3984 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
3985 ok(bret
, "Connecting failed, error %d\n", GetLastError());
3986 ok(bytesReturned
== 3, "Bytes sent is %d\n", bytesReturned
);
3988 acceptor
= accept(listener
, NULL
, NULL
);
3989 ok(acceptor
!= INVALID_SOCKET
, "could not accept socket error %d\n", WSAGetLastError());
3991 bytesReturned
= recv(acceptor
, buffer
, 3, 0);
3993 ok(bytesReturned
== 3, "Didn't get all sent data, got only %d\n", bytesReturned
);
3994 ok(buffer
[0] == '1' && buffer
[1] == '2' && buffer
[2] == '3',
3995 "Failed to get the right data, expected '123', got '%s'\n", buffer
);
3997 closesocket(connector
);
3998 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3999 if (connector
== INVALID_SOCKET
) {
4000 skip("could not create connector socket, error %d\n", WSAGetLastError());
4003 /* ConnectEx needs a bound socket */
4004 memset(&conaddress
, 0, sizeof(conaddress
));
4005 conaddress
.sin_family
= AF_INET
;
4006 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
4007 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
4009 skip("failed to bind, error %d\n", WSAGetLastError());
4013 if (acceptor
!= INVALID_SOCKET
) {
4014 closesocket(acceptor
);
4015 acceptor
= INVALID_SOCKET
;
4018 /* Connect with error */
4019 closesocket(listener
);
4020 listener
= INVALID_SOCKET
;
4022 address
.sin_port
= htons(1);
4024 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
4025 ok(bret
== FALSE
&& GetLastError(), "ConnectEx to bad destination failed: "
4026 "returned %d + errno %d\n", bret
, GetLastError());
4028 if (GetLastError() == ERROR_IO_PENDING
)
4030 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
4031 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
4033 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
4034 ok(bret
== FALSE
&& GetLastError() == ERROR_CONNECTION_REFUSED
,
4035 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
4038 ok(GetLastError() == WSAECONNREFUSED
,
4039 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
4043 if (overlapped
.hEvent
)
4044 WSACloseEvent(overlapped
.hEvent
);
4045 if (listener
!= INVALID_SOCKET
)
4046 closesocket(listener
);
4047 if (acceptor
!= INVALID_SOCKET
)
4048 closesocket(acceptor
);
4049 if (connector
!= INVALID_SOCKET
)
4050 closesocket(connector
);
4053 static void test_AcceptEx(void)
4055 SOCKET listener
= INVALID_SOCKET
;
4056 SOCKET acceptor
= INVALID_SOCKET
;
4057 SOCKET connector
= INVALID_SOCKET
;
4058 SOCKET connector2
= INVALID_SOCKET
;
4059 struct sockaddr_in bindAddress
;
4061 GUID acceptExGuid
= WSAID_ACCEPTEX
;
4062 LPFN_ACCEPTEX pAcceptEx
= NULL
;
4063 fd_set fds_accept
, fds_send
;
4064 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
4066 DWORD bytesReturned
;
4068 OVERLAPPED overlapped
;
4073 memset(&overlapped
, 0, sizeof(overlapped
));
4075 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
4076 if (listener
== INVALID_SOCKET
) {
4077 skip("could not create listener socket, error %d\n", WSAGetLastError());
4081 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4082 if (acceptor
== INVALID_SOCKET
) {
4083 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4087 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
4088 if (connector
== INVALID_SOCKET
) {
4089 skip("could not create connector socket, error %d\n", WSAGetLastError());
4093 memset(&bindAddress
, 0, sizeof(bindAddress
));
4094 bindAddress
.sin_family
= AF_INET
;
4095 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
4096 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4098 skip("failed to bind, error %d\n", WSAGetLastError());
4102 socklen
= sizeof(bindAddress
);
4103 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
4105 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
4109 if (set_blocking(listener
, FALSE
)) {
4110 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4114 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
4115 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
4117 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
4121 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4122 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4123 &bytesReturned
, &overlapped
);
4124 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
4125 "returned %d + errno %d\n", bret
, WSAGetLastError());
4127 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4128 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4129 &bytesReturned
, &overlapped
);
4130 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on invalid accepting socket "
4131 "returned %d + errno %d\n", bret
, WSAGetLastError());
4133 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4134 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4135 &bytesReturned
, &overlapped
);
4137 (WSAGetLastError() == WSAEINVAL
||
4138 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
4139 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
4141 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
4142 &bytesReturned
, &overlapped
);
4143 todo_wine
ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small local address size "
4144 "returned %d + errno %d\n", bret
, WSAGetLastError());
4146 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
4147 &bytesReturned
, &overlapped
);
4148 todo_wine
ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small remote address size "
4149 "returned %d + errno %d\n", bret
, WSAGetLastError());
4151 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4152 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4153 &bytesReturned
, NULL
);
4154 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
4155 "returned %d + errno %d\n", bret
, WSAGetLastError());
4157 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4158 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4159 &bytesReturned
, &overlapped
);
4160 todo_wine
ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
4161 "returned %d + errno %d\n", bret
, WSAGetLastError());
4163 iret
= listen(listener
, 5);
4165 skip("listening failed, errno = %d\n", WSAGetLastError());
4169 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
4170 if (overlapped
.hEvent
== NULL
) {
4171 skip("could not create event object, errno = %d\n", GetLastError());
4175 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4176 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4177 &bytesReturned
, &overlapped
);
4178 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4180 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4181 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4182 &bytesReturned
, &overlapped
);
4183 todo_wine
ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
4184 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
4185 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
4186 /* We need to cancel this call, otherwise things fail */
4187 bret
= CancelIo((HANDLE
) listener
);
4188 ok(bret
, "Failed to cancel failed test. Bailing...\n");
4190 WaitForSingleObject(overlapped
.hEvent
, 0);
4192 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4193 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4194 &bytesReturned
, &overlapped
);
4195 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4198 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4199 todo_wine
ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
4200 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
4201 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
4202 /* We need to cancel this call, otherwise things fail */
4203 closesocket(acceptor
);
4204 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4205 if (acceptor
== INVALID_SOCKET
) {
4206 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4210 bret
= CancelIo((HANDLE
) listener
);
4211 ok(bret
, "Failed to cancel failed test. Bailing...\n");
4214 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4215 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4216 &bytesReturned
, &overlapped
);
4217 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4220 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4221 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4223 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
4224 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4226 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4227 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4228 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4230 closesocket(connector
);
4231 connector
= INVALID_SOCKET
;
4232 closesocket(acceptor
);
4234 /* Test short reads */
4236 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4237 if (acceptor
== INVALID_SOCKET
) {
4238 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4241 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
4242 if (connector
== INVALID_SOCKET
) {
4243 skip("could not create connector socket, error %d\n", WSAGetLastError());
4246 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
4247 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4248 &bytesReturned
, &overlapped
);
4249 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4251 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4252 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4254 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
4255 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
4257 iret
= send(connector
, buffer
, 1, 0);
4258 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
4260 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4261 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4263 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4264 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4265 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4267 closesocket(connector
);
4268 connector
= INVALID_SOCKET
;
4269 closesocket(acceptor
);
4271 /* Test CF_DEFER & AcceptEx interaction */
4273 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4274 if (acceptor
== INVALID_SOCKET
) {
4275 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4278 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
4279 if (connector
== INVALID_SOCKET
) {
4280 skip("could not create connector socket, error %d\n", WSAGetLastError());
4283 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
4284 if (connector
== INVALID_SOCKET
) {
4285 skip("could not create connector socket, error %d\n", WSAGetLastError());
4289 if (set_blocking(connector
, FALSE
)) {
4290 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4294 if (set_blocking(connector2
, FALSE
)) {
4295 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
4299 /* Connect socket #1 */
4300 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4301 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4303 FD_ZERO ( &fds_accept
);
4304 FD_ZERO ( &fds_send
);
4306 FD_SET ( listener
, &fds_accept
);
4307 FD_SET ( connector
, &fds_send
);
4313 for (i
= 0; i
< 4000; ++i
)
4315 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
4317 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
4318 "acceptex test(%d): could not select on socket, errno %d\n" );
4320 /* check for incoming requests */
4321 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
4324 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
4325 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
4326 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4327 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4328 &bytesReturned
, &overlapped
);
4329 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4331 else if (got
== 2) {
4332 /* this should be socket #2 */
4333 SOCKET tmp
= accept(listener
, NULL
, NULL
);
4334 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
4338 ok(FALSE
, "Got more than 2 connections?\n");
4341 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
4342 /* Send data on second socket, and stop */
4343 send(connector2
, "2", 1, 0);
4344 FD_CLR ( connector2
, &fds_send
);
4348 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
4349 /* Once #1 is connected, allow #2 to connect */
4352 send(connector
, "1", 1, 0);
4353 FD_CLR ( connector
, &fds_send
);
4355 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4356 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4357 FD_SET ( connector2
, &fds_send
);
4361 ok (got
== 2 || broken(got
== 1) /* NT4 */,
4362 "Did not get both connections, got %d\n", got
);
4364 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
4365 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4367 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4368 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4369 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4371 set_blocking(acceptor
, TRUE
);
4372 iret
= recv( acceptor
, buffer
, 2, 0);
4373 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
4375 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
4377 closesocket(connector
);
4378 connector
= INVALID_SOCKET
;
4379 closesocket(acceptor
);
4381 /* clean up in case of failures */
4382 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
4383 closesocket(acceptor
);
4385 /* Disconnect during receive? */
4387 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4388 if (acceptor
== INVALID_SOCKET
) {
4389 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4392 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
4393 if (connector
== INVALID_SOCKET
) {
4394 skip("could not create connector socket, error %d\n", WSAGetLastError());
4397 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4398 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4399 &bytesReturned
, &overlapped
);
4400 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4402 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4403 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4405 closesocket(connector
);
4406 connector
= INVALID_SOCKET
;
4408 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4409 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4411 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4412 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4413 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4415 closesocket(acceptor
);
4417 /* Test closing with pending requests */
4419 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4420 if (acceptor
== INVALID_SOCKET
) {
4421 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4424 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4425 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4426 &bytesReturned
, &overlapped
);
4427 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4429 closesocket(acceptor
);
4431 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4432 todo_wine
ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
4433 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4435 if (dwret
!= WAIT_TIMEOUT
) {
4436 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4437 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
4440 bret
= CancelIo((HANDLE
) listener
);
4441 ok(bret
, "Failed to cancel failed test. Bailing...\n");
4443 WaitForSingleObject(overlapped
.hEvent
, 0);
4446 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4447 if (acceptor
== INVALID_SOCKET
) {
4448 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4451 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4452 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4453 &bytesReturned
, &overlapped
);
4454 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4456 CancelIo((HANDLE
) acceptor
);
4458 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4459 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
4461 closesocket(acceptor
);
4463 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4464 if (acceptor
== INVALID_SOCKET
) {
4465 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4468 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4469 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4470 &bytesReturned
, &overlapped
);
4471 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4473 closesocket(listener
);
4474 listener
= INVALID_SOCKET
;
4476 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4477 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4479 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4480 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
4483 if (overlapped
.hEvent
)
4484 WSACloseEvent(overlapped
.hEvent
);
4485 if (listener
!= INVALID_SOCKET
)
4486 closesocket(listener
);
4487 if (acceptor
!= INVALID_SOCKET
)
4488 closesocket(acceptor
);
4489 if (connector
!= INVALID_SOCKET
)
4490 closesocket(connector
);
4491 if (connector2
!= INVALID_SOCKET
)
4492 closesocket(connector2
);
4495 static void test_getpeername(void)
4498 struct sockaddr_in sa
, sa_out
;
4500 const char buf
[] = "hello world";
4503 /* Test the parameter validation order. */
4504 ret
= getpeername(INVALID_SOCKET
, NULL
, NULL
);
4505 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4506 ok(WSAGetLastError() == WSAENOTSOCK
,
4507 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
4509 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
4510 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
4511 if (sock
== INVALID_SOCKET
)
4513 skip("Socket creation failed with %d\n", WSAGetLastError());
4517 ret
= getpeername(sock
, NULL
, NULL
);
4518 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4519 ok(WSAGetLastError() == WSAENOTCONN
||
4520 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
4521 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4523 memset(&sa
, 0, sizeof(sa
));
4524 sa
.sin_family
= AF_INET
;
4525 sa
.sin_port
= htons(139);
4526 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
4528 /* sendto does not change a socket's connection state. */
4529 ret
= sendto(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sa
, sizeof(sa
));
4530 ok(ret
!= SOCKET_ERROR
,
4531 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4533 ret
= getpeername(sock
, NULL
, NULL
);
4534 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4535 ok(WSAGetLastError() == WSAENOTCONN
||
4536 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
4537 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4539 ret
= connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
));
4541 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4543 ret
= getpeername(sock
, NULL
, NULL
);
4544 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4545 ok(WSAGetLastError() == WSAEFAULT
,
4546 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4548 /* Test crashes on Wine. */
4551 ret
= getpeername(sock
, (void*)0xdeadbeef, (void*)0xcafebabe);
4552 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4553 ok(WSAGetLastError() == WSAEFAULT
,
4554 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4558 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4559 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
4560 ok(WSAGetLastError() == WSAEFAULT
,
4561 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4563 sa_len
= sizeof(sa_out
);
4564 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4565 ok(ret
== 0, "Expected getpeername to return 0, got %d\n", ret
);
4566 ok(!memcmp(&sa
, &sa_out
, sizeof(sa
)),
4567 "Expected the returned structure to be identical to the connect structure\n");
4572 static void test_sioRoutingInterfaceQuery(void)
4576 SOCKADDR_IN sin
= { 0 }, sout
= { 0 };
4577 DWORD bytesReturned
;
4579 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
4580 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
4581 if (sock
== INVALID_SOCKET
)
4583 skip("Socket creation failed with %d\n", WSAGetLastError());
4586 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, NULL
, 0, NULL
, 0, NULL
,
4588 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
4589 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4590 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
4591 NULL
, 0, NULL
, NULL
, NULL
);
4592 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
4593 "expected WSAEFAULT, got %d\n", WSAGetLastError());
4594 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
4595 NULL
, 0, &bytesReturned
, NULL
, NULL
);
4596 ok(ret
== SOCKET_ERROR
&&
4597 (WSAGetLastError() == WSAEFAULT
/* Win98 */ ||
4598 WSAGetLastError() == WSAEINVAL
/* NT4 */||
4599 WSAGetLastError() == WSAEAFNOSUPPORT
),
4600 "expected WSAEFAULT or WSAEINVAL or WSAEAFNOSUPPORT, got %d\n",
4602 sin
.sin_family
= AF_INET
;
4603 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
4604 NULL
, 0, &bytesReturned
, NULL
, NULL
);
4605 ok(ret
== SOCKET_ERROR
&&
4606 (WSAGetLastError() == WSAEFAULT
/* Win98 */ ||
4607 WSAGetLastError() == WSAEINVAL
),
4608 "expected WSAEFAULT or WSAEINVAL, got %d\n", WSAGetLastError());
4609 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
4610 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
4611 NULL
, 0, &bytesReturned
, NULL
, NULL
);
4612 ok(ret
== SOCKET_ERROR
&&
4613 (WSAGetLastError() == WSAEINVAL
/* NT4 */ ||
4614 WSAGetLastError() == WSAEFAULT
),
4615 "expected WSAEINVAL or WSAEFAULT, got %d\n", WSAGetLastError());
4616 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
4617 &sout
, sizeof(sout
), &bytesReturned
, NULL
, NULL
);
4618 ok(!ret
|| broken(WSAGetLastError() == WSAEINVAL
/* NT4 */),
4619 "WSAIoctl failed: %d\n", WSAGetLastError());
4622 ok(sout
.sin_family
== AF_INET
, "expected AF_INET, got %d\n",
4624 /* We expect the source address to be INADDR_LOOPBACK as well, but
4625 * there's no guarantee that a route to the loopback address exists,
4626 * so rather than introduce spurious test failures we do not test the
4634 /**************** Main program ***************/
4640 /* Leave these tests at the beginning. They depend on WSAStartup not having been
4641 * called, which is done by Init() below. */
4642 test_WithoutWSAStartup();
4643 test_WithWSAStartup();
4647 test_set_getsockopt();
4648 test_so_reuseaddr();
4650 test_extendedSocketOptions();
4652 for (i
= 0; i
< NUM_TESTS
; i
++)
4654 trace ( " **** STARTING TEST %d ****\n", i
);
4655 do_test ( &tests
[i
] );
4656 trace ( " **** TEST %d COMPLETE ****\n", i
);
4661 test_getservbyname();
4664 test_WSAAddressToStringA();
4665 test_WSAAddressToStringW();
4667 test_WSAStringToAddressA();
4668 test_WSAStringToAddressW();
4675 test_addr_to_print();
4678 test_gethostbyname_hack();
4687 test_GetAddrInfoW();
4692 test_sioRoutingInterfaceQuery();
4694 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */