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 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
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 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
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 */
838 /* This should fail. WSAStartup should be called before any network function is used. */
839 static void test_WithoutWSAStartup(void)
843 WSASetLastError(0xdeadbeef);
844 ptr
= gethostbyname("localhost");
846 todo_wine
ok(ptr
== NULL
, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
847 todo_wine
ok(WSAGetLastError() == WSANOTINITIALISED
, "gethostbyname() failed with unexpected error: %d\n",
851 static void test_WithWSAStartup(void)
854 WORD version
= MAKEWORD( 2, 2 );
858 res
= WSAStartup( version
, &data
);
859 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
861 ptr
= gethostbyname("localhost");
862 ok(ptr
!= NULL
, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
867 /**************** Main program utility functions ***************/
869 static void Init (void)
871 WORD ver
= MAKEWORD (2, 2);
873 HMODULE hws2_32
= GetModuleHandle("ws2_32.dll");
875 pFreeAddrInfoW
= (void *)GetProcAddress(hws2_32
, "FreeAddrInfoW");
876 pGetAddrInfoW
= (void *)GetProcAddress(hws2_32
, "GetAddrInfoW");
877 pInetNtop
= (void *)GetProcAddress(hws2_32
, "inet_ntop");
879 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
883 static void Exit (void)
888 err
= WSAGetLastError();
889 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
891 err
= WSAGetLastError();
892 ok ( (ret
== SOCKET_ERROR
&& err
== WSANOTINITIALISED
) ||
893 broken(ret
== 0), /* WinME */
894 "WSACleanup returned %d GetLastError is %d\n", ret
, err
);
897 static void StartServer (LPTHREAD_START_ROUTINE routine
,
898 test_params
*general
, server_params
*par
)
900 par
->general
= general
;
901 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
902 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
905 static void StartClients (LPTHREAD_START_ROUTINE routine
,
906 test_params
*general
, client_params
*par
)
909 par
->general
= general
;
910 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
913 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
914 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
915 /* Make sure the client is up and running */
916 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
920 static void do_test( test_setup
*test
)
922 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
925 server_ready
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
926 for (i
= 0; i
<= n
; i
++)
927 client_ready
[i
] = CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
929 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
930 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
931 WaitForSingleObject ( server_ready
, INFINITE
);
933 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
934 ok ( wait
<= WAIT_OBJECT_0
+ n
,
935 "some threads have not completed: %x\n", wait
);
937 if ( ! ( wait
<= WAIT_OBJECT_0
+ n
) )
939 for (i
= 0; i
<= n
; i
++)
941 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
943 trace ("terminating thread %08x\n", thread_id
[i
]);
944 TerminateThread ( thread
[i
], 0 );
948 CloseHandle ( server_ready
);
949 for (i
= 0; i
<= n
; i
++)
950 CloseHandle ( client_ready
[i
] );
953 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
954 /* optname = SO_LINGER */
955 LINGER linger_testvals
[] = {
962 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
963 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
964 bug in the linux kernel (fixed in 2.6.8) */
965 #define SOCKTIMEOUT2 997000 /* 997 seconds */
967 static void test_set_getsockopt(void)
975 s
= socket(AF_INET
, SOCK_STREAM
, 0);
976 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
977 if( s
== INVALID_SOCKET
) return;
979 timeout
= SOCKTIMEOUT1
;
980 size
= sizeof(timeout
);
981 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
983 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
984 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
985 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
988 size
= sizeof(timeout
);
989 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
991 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
992 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
993 ok( timeout
== 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
996 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
997 size
= sizeof(timeout
);
998 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
1000 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
1001 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1002 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
1004 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
1005 size
= sizeof(lingval
);
1006 lingval
= linger_testvals
[i
];
1007 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1009 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1010 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1011 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1012 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1013 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1014 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1015 lingval
.l_onoff
, lingval
.l_linger
,
1016 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1018 /* Test for erroneously passing a value instead of a pointer as optval */
1019 size
= sizeof(char);
1020 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1021 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1022 "instead of failing.\n");
1023 lasterr
= WSAGetLastError();
1024 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1025 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1026 lasterr
, WSAEFAULT
);
1028 /* SO_RCVTIMEO with invalid values for level */
1029 size
= sizeof(timeout
);
1030 timeout
= SOCKTIMEOUT1
;
1031 SetLastError(0xdeadbeef);
1032 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1033 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1034 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1035 err
, WSAGetLastError());
1037 timeout
= SOCKTIMEOUT1
;
1038 SetLastError(0xdeadbeef);
1039 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1040 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1041 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1042 err
, WSAGetLastError());
1047 static void test_so_reuseaddr(void)
1049 struct sockaddr_in saddr
;
1051 unsigned int rc
,reuse
;
1054 saddr
.sin_family
= AF_INET
;
1055 saddr
.sin_port
= htons(9375);
1056 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1058 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1059 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1060 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1061 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1063 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1064 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1068 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1069 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1071 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1072 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1075 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1076 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1078 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1079 * a port immediately after closing another socket on that port, so
1080 * basically following the BSD socket semantics here. */
1082 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1083 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1088 /************* Array containing the tests to run **********/
1090 #define STD_STREAM_SOCKET \
1096 static test_setup tests
[NUM_TESTS
] =
1098 /* Test 0: synchronous client and server */
1119 /* Test 1: event-driven client, synchronous server */
1136 WSA_FLAG_OVERLAPPED
,
1140 /* Test 2: synchronous client, non-blocking server via select() */
1161 /* Test 3: synchronous mixed client and server */
1175 simple_mixed_client
,
1184 static void test_UDP(void)
1186 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1187 possible that this test fails due to dropped packets. */
1189 /* peer 0 receives data from all other peers */
1190 struct sock_info peer
[NUM_UDP_PEERS
];
1192 int ss
, i
, n_recv
, n_sent
;
1194 memset (buf
,0,sizeof(buf
));
1195 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1196 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1198 peer
[i
].addr
.sin_family
= AF_INET
;
1199 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1202 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1204 peer
[i
].addr
.sin_port
= htons ( 0 );
1207 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1209 /* test getsockname() to get peer's port */
1210 ss
= sizeof ( peer
[i
].addr
);
1211 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1212 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1215 /* test getsockname() */
1216 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1218 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1219 /* send client's ip */
1220 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1221 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1222 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1225 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1226 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1227 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1228 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1232 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1238 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1241 struct servent
*pserv
[2];
1243 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1245 /* ensure that necessary buffer resizes are completed */
1246 for ( j
= 0; j
< 2; j
++) {
1247 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1250 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1251 for ( j
= 0; j
< 2; j
++ ) {
1252 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1253 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1254 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
) );
1255 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
);
1256 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
);
1259 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1263 static void test_getservbyname(void)
1266 HANDLE starttest
, thread
[NUM_THREADS
];
1267 DWORD thread_id
[NUM_THREADS
];
1269 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1271 /* create threads */
1272 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1273 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1276 /* signal threads to start */
1277 SetEvent ( starttest
);
1279 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1280 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1284 static void test_WSASocket(void)
1286 SOCKET sock
= INVALID_SOCKET
;
1287 WSAPROTOCOL_INFOA
*pi
;
1288 int providers
[] = {6, 0};
1292 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1293 * to avoid a crash on win98.
1296 ret
= WSAEnumProtocolsA(providers
, NULL
, &pi_size
);
1297 ok(ret
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1299 err
= WSAGetLastError();
1300 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1303 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
1304 ok(pi
!= NULL
, "Failed to allocate memory\n");
1306 skip("Can't continue without memory.\n");
1310 ret
= WSAEnumProtocolsA(providers
, pi
, &pi_size
);
1311 ok(ret
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
1315 skip("No protocols enumerated.\n");
1316 HeapFree(GetProcessHeap(), 0, pi
);
1320 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
1321 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
1322 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1326 HeapFree(GetProcessHeap(), 0, pi
);
1329 static void test_WSAAddressToStringA(void)
1331 SOCKET v6
= INVALID_SOCKET
;
1335 SOCKADDR_IN sockaddr
;
1336 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1338 CHAR expect1
[] = "0.0.0.0";
1339 CHAR expect2
[] = "255.255.255.255";
1340 CHAR expect3
[] = "0.0.0.0:65535";
1341 CHAR expect4
[] = "255.255.255.255:65535";
1343 SOCKADDR_IN6 sockaddr6
;
1344 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1346 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1347 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1348 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1350 CHAR expect6_1
[] = "::1";
1351 CHAR expect6_2
[] = "20ab::1";
1352 CHAR expect6_3
[] = "[20ab::2001]:33274";
1353 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
1354 CHAR expect6_3_3
[] = "20ab::2001%4660";
1358 sockaddr
.sin_family
= AF_INET
;
1359 sockaddr
.sin_port
= 0;
1360 sockaddr
.sin_addr
.s_addr
= 0;
1362 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1363 GLE
= WSAGetLastError();
1364 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1365 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1368 len
= sizeof(address
);
1370 sockaddr
.sin_family
= AF_INET
;
1371 sockaddr
.sin_port
= 0;
1372 sockaddr
.sin_addr
.s_addr
= 0;
1374 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1375 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1377 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1378 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
1380 len
= sizeof(address
);
1382 sockaddr
.sin_family
= AF_INET
;
1383 sockaddr
.sin_port
= 0;
1384 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1386 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1387 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1389 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1391 len
= sizeof(address
);
1393 sockaddr
.sin_family
= AF_INET
;
1394 sockaddr
.sin_port
= 0xffff;
1395 sockaddr
.sin_addr
.s_addr
= 0;
1397 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1398 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1400 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1402 len
= sizeof(address
);
1404 sockaddr
.sin_family
= AF_INET
;
1405 sockaddr
.sin_port
= 0xffff;
1406 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1408 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1409 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1411 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1412 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
1414 /*check to see it IPv6 is available */
1415 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1416 if (v6
== INVALID_SOCKET
) {
1417 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1418 WSAGetLastError(), WSAEAFNOSUPPORT
);
1421 /* Test a short IPv6 address */
1422 len
= sizeof(address6
);
1424 sockaddr6
.sin6_family
= AF_INET6
;
1425 sockaddr6
.sin6_port
= 0x0000;
1426 sockaddr6
.sin6_scope_id
= 0;
1427 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1429 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1430 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1431 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
1432 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
1434 /* Test a longer IPv6 address */
1435 len
= sizeof(address6
);
1437 sockaddr6
.sin6_family
= AF_INET6
;
1438 sockaddr6
.sin6_port
= 0x0000;
1439 sockaddr6
.sin6_scope_id
= 0;
1440 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1442 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1443 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1444 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
1445 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
1447 /* Test IPv6 address and port number */
1448 len
= sizeof(address6
);
1450 sockaddr6
.sin6_family
= AF_INET6
;
1451 sockaddr6
.sin6_port
= 0xfa81;
1452 sockaddr6
.sin6_scope_id
= 0;
1453 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1455 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1456 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1457 ok( !strcmp( address6
, expect6_3
), "Expected: %s, got: %s\n", expect6_3
, address6
);
1458 ok( len
== sizeof(expect6_3
), "Got size %d\n", len
);
1460 /* Test IPv6 address, port number and scope_id */
1461 len
= sizeof(address6
);
1463 sockaddr6
.sin6_family
= AF_INET6
;
1464 sockaddr6
.sin6_port
= 0xfa81;
1465 sockaddr6
.sin6_scope_id
= 0x1234;
1466 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1468 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1469 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1470 ok( !strcmp( address6
, expect6_3_2
), "Expected: %s, got: %s\n", expect6_3_2
, address6
);
1471 ok( len
== sizeof(expect6_3_2
), "Got size %d\n", len
);
1473 /* Test IPv6 address and scope_id */
1474 len
= sizeof(address6
);
1476 sockaddr6
.sin6_family
= AF_INET6
;
1477 sockaddr6
.sin6_port
= 0x0000;
1478 sockaddr6
.sin6_scope_id
= 0x1234;
1479 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1481 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1482 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1483 ok( !strcmp( address6
, expect6_3_3
), "Expected: %s, got: %s\n", expect6_3_3
, address6
);
1484 ok( len
== sizeof(expect6_3_3
), "Got size %d\n", len
);
1487 if (v6
!= INVALID_SOCKET
)
1491 static void test_WSAAddressToStringW(void)
1493 SOCKET v6
= INVALID_SOCKET
;
1497 SOCKADDR_IN sockaddr
;
1498 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1500 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1501 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1502 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1503 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1504 '6', '5', '5', '3', '5', 0 };
1506 SOCKADDR_IN6 sockaddr6
;
1507 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1509 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1510 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1511 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1513 WCHAR expect6_1
[] = {':',':','1',0};
1514 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
1515 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1516 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1517 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1521 sockaddr
.sin_family
= AF_INET
;
1522 sockaddr
.sin_port
= 0;
1523 sockaddr
.sin_addr
.s_addr
= 0;
1525 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1526 GLE
= WSAGetLastError();
1527 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1528 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1531 len
= sizeof(address
);
1533 sockaddr
.sin_family
= AF_INET
;
1534 sockaddr
.sin_port
= 0;
1535 sockaddr
.sin_addr
.s_addr
= 0;
1537 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1538 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1540 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1541 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
1543 len
= sizeof(address
);
1545 sockaddr
.sin_family
= AF_INET
;
1546 sockaddr
.sin_port
= 0;
1547 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1549 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1550 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1552 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1554 len
= sizeof(address
);
1556 sockaddr
.sin_family
= AF_INET
;
1557 sockaddr
.sin_port
= 0xffff;
1558 sockaddr
.sin_addr
.s_addr
= 0;
1560 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1561 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1563 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1565 len
= sizeof(address
);
1567 sockaddr
.sin_family
= AF_INET
;
1568 sockaddr
.sin_port
= 0xffff;
1569 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1571 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1572 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1574 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1575 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
1577 /*check to see it IPv6 is available */
1578 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1579 if (v6
== INVALID_SOCKET
) {
1580 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1581 WSAGetLastError(), WSAEAFNOSUPPORT
);
1585 /* Test a short IPv6 address */
1586 len
= sizeof(address6
)/sizeof(WCHAR
);
1588 sockaddr6
.sin6_family
= AF_INET6
;
1589 sockaddr6
.sin6_port
= 0x0000;
1590 sockaddr6
.sin6_scope_id
= 0;
1591 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1593 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1594 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1595 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
1596 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
1598 /* Test a longer IPv6 address */
1599 len
= sizeof(address6
)/sizeof(WCHAR
);
1601 sockaddr6
.sin6_family
= AF_INET6
;
1602 sockaddr6
.sin6_port
= 0x0000;
1603 sockaddr6
.sin6_scope_id
= 0;
1604 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1606 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1607 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1609 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
1610 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1612 /* Test IPv6 address and port number */
1613 len
= sizeof(address6
)/sizeof(WCHAR
);
1615 sockaddr6
.sin6_family
= AF_INET6
;
1616 sockaddr6
.sin6_port
= 0xfa81;
1617 sockaddr6
.sin6_scope_id
= 0;
1618 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1620 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1621 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1622 ok( !lstrcmpW( address6
, expect6_3
), "Wrong string returned\n" );
1623 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1625 /* Test IPv6 address, port number and scope_id */
1626 len
= sizeof(address6
)/sizeof(WCHAR
);
1628 sockaddr6
.sin6_family
= AF_INET6
;
1629 sockaddr6
.sin6_port
= 0xfa81;
1630 sockaddr6
.sin6_scope_id
= 0x1234;
1631 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1633 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1634 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1635 ok( !lstrcmpW( address6
, expect6_3_2
), "Wrong string returned\n" );
1636 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1638 /* Test IPv6 address and scope_id */
1639 len
= sizeof(address6
)/sizeof(WCHAR
);
1641 sockaddr6
.sin6_family
= AF_INET6
;
1642 sockaddr6
.sin6_port
= 0x0000;
1643 sockaddr6
.sin6_scope_id
= 0xfffe;
1644 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1646 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1647 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1648 ok( !lstrcmpW( address6
, expect6_3_3
), "Wrong string returned\n" );
1649 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1652 if (v6
!= INVALID_SOCKET
)
1656 static void test_WSAStringToAddressA(void)
1659 SOCKADDR_IN sockaddr
;
1660 SOCKADDR_IN6 sockaddr6
;
1663 CHAR address1
[] = "0.0.0.0";
1664 CHAR address2
[] = "127.127.127.127";
1665 CHAR address3
[] = "255.255.255.255";
1666 CHAR address4
[] = "127.127.127.127:65535";
1667 CHAR address5
[] = "255.255.255.255:65535";
1668 CHAR address6
[] = "::1";
1669 CHAR address7
[] = "[::1]";
1670 CHAR address8
[] = "[::1]:65535";
1673 sockaddr
.sin_family
= AF_INET
;
1675 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1676 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1677 WSAGetLastError() );
1679 len
= sizeof(sockaddr
);
1680 sockaddr
.sin_port
= 0;
1681 sockaddr
.sin_addr
.s_addr
= 0;
1683 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1684 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1685 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1687 len
= sizeof(sockaddr
);
1688 sockaddr
.sin_port
= 0;
1689 sockaddr
.sin_addr
.s_addr
= 0;
1691 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1692 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1693 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1695 len
= sizeof(sockaddr
);
1696 sockaddr
.sin_port
= 0;
1697 sockaddr
.sin_addr
.s_addr
= 0;
1699 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1700 GLE
= WSAGetLastError();
1701 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1702 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1703 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1705 len
= sizeof(sockaddr
);
1706 sockaddr
.sin_port
= 0;
1707 sockaddr
.sin_addr
.s_addr
= 0;
1709 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1710 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1711 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1713 len
= sizeof(sockaddr
);
1714 sockaddr
.sin_port
= 0;
1715 sockaddr
.sin_addr
.s_addr
= 0;
1717 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1718 GLE
= WSAGetLastError();
1719 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1720 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1721 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1723 len
= sizeof(sockaddr6
);
1724 memset(&sockaddr6
, 0, len
);
1725 sockaddr6
.sin6_family
= AF_INET6
;
1727 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1729 GLE
= WSAGetLastError();
1730 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1731 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1733 len
= sizeof(sockaddr6
);
1734 memset(&sockaddr6
, 0, len
);
1735 sockaddr6
.sin6_family
= AF_INET6
;
1737 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1739 GLE
= WSAGetLastError();
1740 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1741 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1743 len
= sizeof(sockaddr6
);
1744 memset(&sockaddr6
, 0, len
);
1745 sockaddr6
.sin6_family
= AF_INET6
;
1747 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1749 GLE
= WSAGetLastError();
1750 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1751 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1752 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1756 static void test_WSAStringToAddressW(void)
1759 SOCKADDR_IN sockaddr
;
1760 SOCKADDR_IN6 sockaddr6
;
1763 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1764 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1765 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1766 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1767 ':', '6', '5', '5', '3', '5', 0 };
1768 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1769 '6', '5', '5', '3', '5', 0 };
1770 WCHAR address6
[] = {':',':','1','\0'};
1771 WCHAR address7
[] = {'[',':',':','1',']','\0'};
1772 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1775 sockaddr
.sin_family
= AF_INET
;
1777 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1778 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1779 WSAGetLastError() );
1781 len
= sizeof(sockaddr
);
1782 sockaddr
.sin_port
= 0;
1783 sockaddr
.sin_addr
.s_addr
= 0;
1785 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1786 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1787 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1789 len
= sizeof(sockaddr
);
1790 sockaddr
.sin_port
= 0;
1791 sockaddr
.sin_addr
.s_addr
= 0;
1793 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1794 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1795 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1797 len
= sizeof(sockaddr
);
1798 sockaddr
.sin_port
= 0;
1799 sockaddr
.sin_addr
.s_addr
= 0;
1801 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1802 GLE
= WSAGetLastError();
1803 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1804 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1805 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1807 len
= sizeof(sockaddr
);
1808 sockaddr
.sin_port
= 0;
1809 sockaddr
.sin_addr
.s_addr
= 0;
1811 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1812 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1813 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1815 len
= sizeof(sockaddr
);
1816 sockaddr
.sin_port
= 0;
1817 sockaddr
.sin_addr
.s_addr
= 0;
1819 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1820 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1821 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1822 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1824 len
= sizeof(sockaddr6
);
1825 memset(&sockaddr6
, 0, len
);
1826 sockaddr6
.sin6_family
= AF_INET6
;
1828 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1830 GLE
= WSAGetLastError();
1831 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1832 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1834 len
= sizeof(sockaddr6
);
1835 memset(&sockaddr6
, 0, len
);
1836 sockaddr6
.sin6_family
= AF_INET6
;
1838 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1840 GLE
= WSAGetLastError();
1841 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1842 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1844 len
= sizeof(sockaddr6
);
1845 memset(&sockaddr6
, 0, len
);
1846 sockaddr6
.sin6_family
= AF_INET6
;
1848 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1850 GLE
= WSAGetLastError();
1851 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1852 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1853 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1857 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
1861 struct sockaddr_in addr
;
1862 struct timeval select_timeout
;
1865 FD_SET(par
->s
, &readfds
);
1866 select_timeout
.tv_sec
=5;
1867 select_timeout
.tv_usec
=0;
1868 addr
.sin_family
= AF_INET
;
1869 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
1870 addr
.sin_port
= htons(SERVERPORT
);
1872 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
1873 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1875 SetEvent(server_ready
);
1876 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
1877 par
->ReadKilled
= (ret
== 1);
1880 static void test_select(void)
1882 SOCKET fdRead
, fdWrite
;
1883 fd_set readfds
, writefds
, exceptfds
;
1886 struct timeval select_timeout
;
1887 select_thread_params thread_params
;
1888 HANDLE thread_handle
;
1891 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
1892 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1893 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
1894 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1898 FD_ZERO(&exceptfds
);
1899 FD_SET(fdRead
, &readfds
);
1900 FD_SET(fdWrite
, &writefds
);
1901 FD_SET(fdRead
, &exceptfds
);
1902 FD_SET(fdWrite
, &exceptfds
);
1903 select_timeout
.tv_sec
=0;
1904 select_timeout
.tv_usec
=500;
1907 if (fdWrite
> maxfd
)
1911 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
1912 ok ( (ret
== 0), "select should not return any socket handles\n");
1913 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
1914 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
1917 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
1918 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
1921 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
1923 ret
= closesocket(fdWrite
);
1924 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1926 thread_params
.s
= fdRead
;
1927 thread_params
.ReadKilled
= FALSE
;
1928 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1929 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
1930 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
1932 WaitForSingleObject (server_ready
, INFINITE
);
1934 ret
= closesocket(fdRead
);
1935 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1937 WaitForSingleObject (thread_handle
, 1000);
1938 ok ( (thread_params
.ReadKilled
) ||
1939 broken(thread_params
.ReadKilled
== 0), /*Win98*/
1940 "closesocket did not wakeup select\n");
1944 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
1946 struct sockaddr_in address
;
1947 int len
= sizeof(address
);
1948 SOCKET client_socket
;
1950 SetEvent(server_ready
);
1951 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
1952 if (client_socket
!= INVALID_SOCKET
)
1953 closesocket(client_socket
);
1954 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
1959 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
1960 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
1961 GROUP FAR
* g
, DWORD_PTR dwCallbackData
)
1966 static void test_accept(void)
1969 SOCKET server_socket
= INVALID_SOCKET
, accepted
= INVALID_SOCKET
, connector
= INVALID_SOCKET
;
1970 struct sockaddr_in address
;
1972 select_thread_params thread_params
;
1973 HANDLE thread_handle
= NULL
;
1976 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
1977 if (server_socket
== INVALID_SOCKET
)
1979 trace("error creating server socket: %d\n", WSAGetLastError());
1983 memset(&address
, 0, sizeof(address
));
1984 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1985 address
.sin_family
= AF_INET
;
1986 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
1989 trace("error binding server socket: %d\n", WSAGetLastError());
1993 socklen
= sizeof(address
);
1994 ret
= getsockname(server_socket
, (struct sockaddr
*)&address
, &socklen
);
1996 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2000 ret
= listen(server_socket
, 5);
2003 trace("error making server socket listen: %d\n", WSAGetLastError());
2007 trace("Blocking accept next\n");
2009 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2010 ok(connector
!= INVALID_SOCKET
, "Failed to create connector socket, error %d\n", WSAGetLastError());
2012 ret
= connect(connector
, (struct sockaddr
*)&address
, sizeof(address
));
2013 ok(ret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2015 accepted
= WSAAccept(server_socket
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
2016 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
2018 accepted
= accept(server_socket
, NULL
, 0);
2019 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2021 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2022 if (server_ready
== INVALID_HANDLE_VALUE
)
2024 trace("error creating event: %d\n", GetLastError());
2028 thread_params
.s
= server_socket
;
2029 thread_params
.ReadKilled
= FALSE
;
2030 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
2031 &thread_params
, 0, &id
);
2032 if (thread_handle
== NULL
)
2034 trace("error creating thread: %d\n", GetLastError());
2038 WaitForSingleObject(server_ready
, INFINITE
);
2040 ret
= closesocket(server_socket
);
2043 trace("closesocket failed: %d\n", WSAGetLastError());
2047 WaitForSingleObject(thread_handle
, 1000);
2048 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
2049 "closesocket did not wakeup accept\n");
2052 if (accepted
!= INVALID_SOCKET
)
2053 closesocket(accepted
);
2054 if (connector
!= INVALID_SOCKET
)
2055 closesocket(connector
);
2056 if (thread_handle
!= NULL
)
2057 CloseHandle(thread_handle
);
2058 if (server_ready
!= INVALID_HANDLE_VALUE
)
2059 CloseHandle(server_ready
);
2060 if (server_socket
!= INVALID_SOCKET
)
2061 closesocket(server_socket
);
2064 static void test_extendedSocketOptions(void)
2068 struct sockaddr_in sa
;
2069 int sa_len
= sizeof(struct sockaddr_in
);
2070 int optval
, optlen
= sizeof(int), ret
;
2074 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2075 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2079 memset(&sa
, 0, sa_len
);
2081 sa
.sin_family
= AF_INET
;
2082 sa
.sin_port
= htons(0);
2083 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2085 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2086 trace("Creating the socket failed: %d\n", WSAGetLastError());
2091 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2092 trace("Failed to bind socket: %d\n", WSAGetLastError());
2098 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2100 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
2101 ok((optval
== 65507) || (optval
== 65527),
2102 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
2104 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2105 SetLastError(0xdeadbeef);
2106 optval
= 0xdeadbeef;
2107 optlen
= sizeof(int);
2108 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2109 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2110 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2111 ret
, WSAGetLastError(), optval
, optval
);
2113 /* more invalid values for level */
2114 SetLastError(0xdeadbeef);
2115 optval
= 0xdeadbeef;
2116 optlen
= sizeof(int);
2117 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2118 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2119 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2120 ret
, WSAGetLastError(), optval
, optval
);
2122 SetLastError(0xdeadbeef);
2123 optval
= 0xdeadbeef;
2124 optlen
= sizeof(int);
2125 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2126 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2127 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2128 ret
, WSAGetLastError(), optval
, optval
);
2130 SetLastError(0xdeadbeef);
2131 optval
= 0xdeadbeef;
2132 optlen
= sizeof(int);
2133 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2134 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2135 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2136 ret
, WSAGetLastError(), optval
, optval
);
2138 SetLastError(0xdeadbeef);
2139 optval
= 0xdeadbeef;
2140 optlen
= sizeof(int);
2141 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2142 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2143 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2144 ret
, WSAGetLastError(), optval
, optval
);
2146 optlen
= sizeof(LINGER
);
2147 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2149 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
2154 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2155 trace("Creating the socket failed: %d\n", WSAGetLastError());
2160 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2161 trace("Failed to bind socket: %d\n", WSAGetLastError());
2167 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2168 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
2170 optlen
= sizeof(BOOL
);
2171 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
2172 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
2173 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
2174 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2175 bool_opt_val
, linger_val
.l_onoff
);
2181 static void test_getsockname(void)
2185 struct sockaddr_in sa_set
, sa_get
;
2186 int sa_set_len
= sizeof(struct sockaddr_in
);
2187 int sa_get_len
= sa_set_len
;
2188 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
2191 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2192 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2196 memset(&sa_set
, 0, sa_set_len
);
2198 sa_set
.sin_family
= AF_INET
;
2199 sa_set
.sin_port
= htons(0);
2200 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2202 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2203 trace("Creating the socket failed: %d\n", WSAGetLastError());
2208 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
2209 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
2210 ok(0, "getsockname on unbound socket should fail\n");
2212 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
2213 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
2214 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
2215 "failed getsockname modified sockaddr when it shouldn't\n");
2218 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
2219 trace("Failed to bind socket: %d\n", WSAGetLastError());
2225 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
2226 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2232 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
2233 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
2234 "getsockname did not zero the sockaddr_in structure\n");
2240 static void test_dns(void)
2244 h
= gethostbyname("");
2245 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
2248 /* Our winsock headers don't define gethostname because it conflicts with the
2249 * definition in unistd.h. Define it here to get rid of the warning. */
2251 int WINAPI
gethostname(char *name
, int namelen
);
2253 static void test_gethostbyname_hack(void)
2257 static BYTE loopback
[] = {127, 0, 0, 1};
2258 static BYTE magic_loopback
[] = {127, 12, 34, 56};
2261 ret
= gethostname(name
, 256);
2262 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
2264 he
= gethostbyname("localhost");
2265 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
2268 if(he
->h_length
!= 4)
2270 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2274 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
2275 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2276 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
2277 he
->h_addr_list
[0][3]);
2280 /* No reason to test further with NULL hostname */
2284 if(strcmp(name
, "localhost") == 0)
2286 skip("hostname seems to be \"localhost\", skipping test.\n");
2291 he
= gethostbyname(name
);
2292 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
2295 if(he
->h_length
!= 4)
2297 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2301 if (he
->h_addr_list
[0][0] == 127)
2303 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
2304 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2305 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
2306 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
2311 he
= gethostbyname("nonexistent.winehq.org");
2312 /* Don't check for the return value, as some braindead ISPs will kindly
2313 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2316 static void test_inet_addr(void)
2320 addr
= inet_addr(NULL
);
2321 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
2324 static void test_addr_to_print(void)
2330 struct in6_addr in6
;
2332 u_long addr0_Num
= 0x00000000;
2333 PCSTR addr0_Str
= "0.0.0.0";
2334 u_long addr1_Num
= 0x20201015;
2335 PCSTR addr1_Str
= "21.16.32.32";
2336 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2337 PCSTR addr2_Str
= "::fffe:cc98:bd74";
2338 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
2339 PCSTR addr3_Str
= "2030:a4b1::";
2340 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2341 PCSTR addr4_Str
= "::204.152.189.116";
2343 /* Test IPv4 addresses */
2344 in
.s_addr
= addr0_Num
;
2346 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2347 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2348 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
2350 /* Test that inet_ntoa and inet_ntop return the same value */
2351 in
.S_un
.S_addr
= addr1_Num
;
2352 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2353 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2354 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2356 /* InetNtop became available in Vista and Win2008 */
2359 win_skip("InetNtop not present, not executing tests\n");
2363 /* Second part of test */
2364 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
2365 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
2366 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2368 /* Test invalid parm conditions */
2369 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
2370 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2371 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
2373 /* Test Null destination */
2375 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
2376 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2377 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2378 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2380 /* Test zero length passed */
2383 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
2384 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2385 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2386 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2388 /* Test length one shorter than the address length */
2391 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
2392 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2393 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2394 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2396 /* Test longer length is ok */
2399 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
2400 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2401 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2403 /* Test the IPv6 addresses */
2405 /* Test an zero prefixed IPV6 address */
2406 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2407 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2408 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2409 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
2411 /* Test an zero suffixed IPV6 address */
2412 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
2413 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2414 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2415 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
2417 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2418 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
2419 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2420 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2421 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
2423 /* Test invalid parm conditions */
2424 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2426 /* Test Null destination */
2428 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
2429 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2430 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2431 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2433 /* Test zero length passed */
2436 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
2437 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2438 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2439 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2441 /* Test length one shorter than the address length */
2444 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
2445 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2446 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2447 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2449 /* Test longer length is ok */
2452 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
2453 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2456 static void test_ioctlsocket(void)
2460 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
2464 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2465 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
2466 if(sock
== INVALID_SOCKET
)
2468 skip("Can't continue without a socket.\n");
2472 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
2474 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2475 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
2476 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
2477 ret
= WSAGetLastError();
2478 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
2481 /* A fresh and not connected socket has no urgent data, this test shows
2482 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2484 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
2485 if(ret
!= SOCKET_ERROR
)
2486 todo_wine
ok(arg
, "expected a non-zero value\n");
2489 static int drain_pause
=0;
2490 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
2493 SOCKET sock
= *(SOCKET
*)arg
;
2496 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
2500 if (WSAGetLastError() == WSAEWOULDBLOCK
)
2504 FD_SET(sock
, &readset
);
2505 select(0, &readset
, NULL
, NULL
, NULL
);
2516 static void test_send(void)
2518 SOCKET src
= INVALID_SOCKET
;
2519 SOCKET dst
= INVALID_SOCKET
;
2520 HANDLE hThread
= NULL
;
2521 const int buflen
= 1024*1024;
2522 char *buffer
= NULL
;
2526 if (tcp_socketpair(&src
, &dst
) != 0)
2528 ok(0, "creating socket pair failed, skipping test\n");
2532 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2533 if (hThread
== NULL
)
2535 ok(0, "CreateThread failed, error %d\n", GetLastError());
2539 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buflen
);
2542 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2546 ret
= send(src
, buffer
, buflen
, 0);
2548 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
2550 ok(0, "send failed, error %d\n", WSAGetLastError());
2553 if (src
!= INVALID_SOCKET
)
2555 if (dst
!= INVALID_SOCKET
)
2557 if (hThread
!= NULL
)
2558 CloseHandle(hThread
);
2559 HeapFree(GetProcessHeap(), 0, buffer
);
2562 typedef struct async_message
2566 struct async_message
*next
;
2569 static struct async_message
*messages_received
;
2571 #define WM_SOCKET (WM_USER+100)
2572 static LRESULT CALLBACK
ws2_test_WndProc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
2574 struct async_message
*message
;
2579 message
= HeapAlloc(GetProcessHeap(), 0, sizeof(*message
));
2580 message
->socket
= (SOCKET
) wparam
;
2581 message
->lparam
= lparam
;
2582 message
->next
= NULL
;
2584 if (messages_received
)
2586 struct async_message
*last
= messages_received
;
2587 while (last
->next
) last
= last
->next
;
2588 last
->next
= message
;
2591 messages_received
= message
;
2595 return DefWindowProc(hwnd
, msg
, wparam
, lparam
);
2598 static void get_event_details(int event
, int *bit
, char *name
)
2603 if (bit
) *bit
= FD_ACCEPT_BIT
;
2604 if (name
) strcpy(name
, "FD_ACCEPT");
2607 if (bit
) *bit
= FD_CONNECT_BIT
;
2608 if (name
) strcpy(name
, "FD_CONNECT");
2611 if (bit
) *bit
= FD_READ_BIT
;
2612 if (name
) strcpy(name
, "FD_READ");
2615 if (bit
) *bit
= FD_OOB_BIT
;
2616 if (name
) strcpy(name
, "FD_OOB");
2619 if (bit
) *bit
= FD_WRITE_BIT
;
2620 if (name
) strcpy(name
, "FD_WRITE");
2623 if (bit
) *bit
= FD_CLOSE_BIT
;
2624 if (name
) strcpy(name
, "FD_CLOSE");
2628 if (name
) sprintf(name
, "bad%x", event
);
2632 static char *dbgstr_event_seq(const LPARAM
*seq
)
2634 static char message
[1024];
2641 get_event_details(WSAGETSELECTEVENT(*seq
), NULL
, name
);
2643 sprintf(message
, "%s%s%s(%d)", message
, message
[1] == 0 ? "" : " ",
2644 name
, WSAGETSELECTERROR(*seq
));
2648 strcat(message
, "]");
2652 static char *dbgstr_event_seq_result(SOCKET s
, WSANETWORKEVENTS
*netEvents
)
2654 static char message
[1024];
2655 struct async_message
*curr
= messages_received
;
2656 int index
, error
, bit
= 0;
2665 if (bit
>= FD_MAX_EVENTS
) break;
2666 if ( !(netEvents
->lNetworkEvents
& (1 << bit
)) )
2671 get_event_details(1 << bit
, &index
, name
);
2672 error
= netEvents
->iErrorCode
[index
];
2678 if (curr
->socket
!= s
)
2683 get_event_details(WSAGETSELECTEVENT(curr
->lparam
), NULL
, name
);
2684 error
= WSAGETSELECTERROR(curr
->lparam
);
2688 sprintf(message
, "%s%s%s(%d)", message
, message
[1] == 0 ? "" : " ",
2691 strcat(message
, "]");
2695 static void flush_events(SOCKET s
, HANDLE hEvent
)
2697 WSANETWORKEVENTS netEvents
;
2698 struct async_message
*prev
= NULL
, *curr
= messages_received
;
2702 if (hEvent
!= INVALID_HANDLE_VALUE
)
2704 dwRet
= WaitForSingleObject(hEvent
, 100);
2705 if (dwRet
== WAIT_OBJECT_0
)
2707 ret
= WSAEnumNetworkEvents(s
, hEvent
, &netEvents
);
2709 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2716 if (curr
->socket
== s
)
2718 if (prev
) prev
->next
= curr
->next
;
2719 else messages_received
= curr
->next
;
2721 HeapFree(GetProcessHeap(), 0, curr
);
2723 if (prev
) curr
= prev
->next
;
2724 else curr
= messages_received
;
2735 static int match_event_sequence(SOCKET s
, WSANETWORKEVENTS
*netEvents
, const LPARAM
*seq
)
2737 int event
, index
, error
, events
;
2738 struct async_message
*curr
;
2742 events
= netEvents
->lNetworkEvents
;
2745 event
= WSAGETSELECTEVENT(*seq
);
2746 error
= WSAGETSELECTERROR(*seq
);
2747 get_event_details(event
, &index
, NULL
);
2749 if (!(events
& event
) && index
!= -1)
2751 if (events
& event
&& index
!= -1)
2753 if (netEvents
->iErrorCode
[index
] != error
)
2764 curr
= messages_received
;
2767 if (curr
->socket
== s
)
2769 if (!*seq
) return 0;
2770 if (*seq
!= curr
->lparam
) return 0;
2781 /* checks for a sequence of events, (order only checked if window is used) */
2782 static void ok_event_sequence(SOCKET s
, HANDLE hEvent
, const LPARAM
*seq
, const LPARAM
**broken_seqs
, int completelyBroken
)
2785 WSANETWORKEVENTS events
, *netEvents
= NULL
;
2789 if (hEvent
!= INVALID_HANDLE_VALUE
)
2791 netEvents
= &events
;
2793 dwRet
= WaitForSingleObject(hEvent
, 200);
2794 if (dwRet
== WAIT_OBJECT_0
)
2796 ret
= WSAEnumNetworkEvents(s
, hEvent
, netEvents
);
2799 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2804 memset(netEvents
, 0, sizeof(*netEvents
));
2809 /* Run the message loop a little */
2810 while (PeekMessageA( &msg
, 0, 0, 0, PM_REMOVE
))
2812 DispatchMessageA(&msg
);
2816 if (match_event_sequence(s
, netEvents
, seq
))
2818 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq
));
2819 flush_events(s
, hEvent
);
2825 for (; *broken_seqs
; broken_seqs
++)
2827 if (match_event_sequence(s
, netEvents
, *broken_seqs
))
2829 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s
, netEvents
), dbgstr_event_seq(seq
));
2830 flush_events(s
, hEvent
);
2836 winetest_ok(broken(completelyBroken
), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq
),
2837 dbgstr_event_seq_result(s
, netEvents
));
2838 flush_events(s
, hEvent
);
2841 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
2843 static void test_events(int useMessages
)
2845 SOCKET server
= INVALID_SOCKET
;
2846 SOCKET src
= INVALID_SOCKET
, src2
= INVALID_SOCKET
;
2847 SOCKET dst
= INVALID_SOCKET
, dst2
= INVALID_SOCKET
;
2848 struct sockaddr_in addr
;
2849 HANDLE hThread
= NULL
;
2850 HANDLE hEvent
= INVALID_HANDLE_VALUE
, hEvent2
= INVALID_HANDLE_VALUE
;
2851 WNDCLASSEX wndclass
;
2853 char *buffer
= NULL
;
2854 int bufferSize
= 1024*1024;
2858 DWORD bytesReturned
;
2864 static char szClassName
[] = "wstestclass";
2865 const LPARAM
*broken_seq
[3];
2866 static const LPARAM empty_seq
[] = { 0 };
2867 static const LPARAM close_seq
[] = { WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
2868 static const LPARAM write_seq
[] = { WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2869 static const LPARAM read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
2870 static const LPARAM oob_seq
[] = { WSAMAKESELECTREPLY(FD_OOB
, 0), 0 };
2871 static const LPARAM connect_seq
[] = { WSAMAKESELECTREPLY(FD_CONNECT
, 0),
2872 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2873 static const LPARAM read_read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2874 WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
2875 static const LPARAM read_write_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2876 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2877 static const LPARAM read_close_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2878 WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
2880 memset(&ov
, 0, sizeof(ov
));
2881 memset(&ov2
, 0, sizeof(ov2
));
2883 /* don't use socketpair, we want connection event */
2884 src
= socket(AF_INET
, SOCK_STREAM
, 0);
2885 if (src
== INVALID_SOCKET
)
2887 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2891 src2
= socket(AF_INET
, SOCK_STREAM
, 0);
2892 if (src2
== INVALID_SOCKET
)
2894 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2899 if (getsockopt(src
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&bret
, &len
) == SOCKET_ERROR
)
2901 ok(0, "failed to get oobinline status, %d\n", GetLastError());
2904 ok(bret
== FALSE
, "OOB not inline\n");
2908 trace("Event test using messages\n");
2910 wndclass
.cbSize
= sizeof(wndclass
);
2911 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
2912 wndclass
.lpfnWndProc
= ws2_test_WndProc
;
2913 wndclass
.cbClsExtra
= 0;
2914 wndclass
.cbWndExtra
= 0;
2915 wndclass
.hInstance
= GetModuleHandle(NULL
);
2916 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
2917 wndclass
.hIconSm
= LoadIcon(NULL
, IDI_APPLICATION
);
2918 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2919 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
2920 wndclass
.lpszClassName
= szClassName
;
2921 wndclass
.lpszMenuName
= NULL
;
2922 RegisterClassEx(&wndclass
);
2924 hWnd
= CreateWindow(szClassName
, "WS2Test", WS_OVERLAPPEDWINDOW
, 0, 0, 500, 500, NULL
, NULL
, GetModuleHandle(NULL
), NULL
);
2927 ok(0, "failed to create window: %d\n", GetLastError());
2931 ret
= WSAAsyncSelect(src
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2934 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
2938 ret
= WSAAsyncSelect(src2
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2941 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
2947 trace("Event test using events\n");
2949 hEvent
= WSACreateEvent();
2950 if (hEvent
== INVALID_HANDLE_VALUE
)
2952 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
2956 hEvent2
= WSACreateEvent();
2957 if (hEvent2
== INVALID_HANDLE_VALUE
)
2959 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
2963 ret
= WSAEventSelect(src
, hEvent
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2966 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2970 ret
= WSAEventSelect(src2
, hEvent2
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2973 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2978 server
= socket(AF_INET
, SOCK_STREAM
, 0);
2979 if (server
== INVALID_SOCKET
)
2981 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2985 memset(&addr
, 0, sizeof(addr
));
2986 addr
.sin_family
= AF_INET
;
2987 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2988 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
2991 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2996 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
2999 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3003 ret
= listen(server
, 2);
3006 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3010 ret
= connect(src
, (struct sockaddr
*)&addr
, sizeof(addr
));
3011 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3013 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3017 ret
= connect(src2
, (struct sockaddr
*)&addr
, sizeof(addr
));
3018 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3020 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3025 dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3026 if (dst
== INVALID_SOCKET
)
3028 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3033 dst2
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3034 if (dst2
== INVALID_SOCKET
)
3036 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3040 closesocket(server
);
3041 server
= INVALID_SOCKET
;
3043 /* On Windows it seems when a non-blocking socket sends to a
3044 blocking socket on the same host, the send() is BLOCKING,
3045 so make both sockets non-blocking. src is already non-blocking
3046 from the async select */
3048 if (set_blocking(dst
, FALSE
))
3050 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3054 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
3057 ok(0, "could not allocate memory for test\n");
3061 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3062 if (ov
.hEvent
== NULL
)
3064 ok(0, "could not create event object, errno = %d\n", GetLastError());
3068 ov2
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3069 if (ov2
.hEvent
== NULL
)
3071 ok(0, "could not create event object, errno = %d\n", GetLastError());
3075 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3076 ok_event_seq(src
, hEvent
, connect_seq
, NULL
, 1);
3077 ok_event_seq(src2
, hEvent2
, connect_seq
, NULL
, 1);
3078 /* broken on all windows - FD_CONNECT error is garbage */
3080 /* Test simple send/recv */
3081 ret
= send(dst
, buffer
, 100, 0);
3082 ok(ret
== 100, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3083 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3085 ret
= recv(src
, buffer
, 50, 0);
3086 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3087 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3089 ret
= recv(src
, buffer
, 50, 0);
3090 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3091 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3093 /* fun fact - events are reenabled even on failure, but only for messages */
3094 ret
= send(dst
, "1", 1, 0);
3095 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3096 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3098 ret
= recv(src
, buffer
, -1, 0);
3099 ok(ret
== SOCKET_ERROR
&& (GetLastError() == WSAEFAULT
|| GetLastError() == WSAENOBUFS
),
3100 "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3103 broken_seq
[0] = empty_seq
; /* win9x */
3104 broken_seq
[1] = NULL
;
3105 todo_wine
ok_event_seq(src
, hEvent
, read_seq
, broken_seq
, 0);
3108 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3110 ret
= recv(src
, buffer
, 1, 0);
3111 ok(ret
== 1, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3112 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3114 /* Interaction with overlapped */
3115 bufs
.len
= sizeof(char);
3117 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3118 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3119 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3121 bufs
.len
= sizeof(char);
3122 bufs
.buf
= buffer
+1;
3123 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov2
, NULL
);
3124 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3125 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3127 ret
= send(dst
, "12", 2, 0);
3128 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3129 broken_seq
[0] = read_read_seq
; /* win9x */
3130 broken_seq
[1] = NULL
;
3131 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3133 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3134 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3135 if (dwRet
== WAIT_OBJECT_0
)
3137 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3138 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3139 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3140 ok(buffer
[0] == '1', "Got %c instead of 1\n", buffer
[0]);
3143 dwRet
= WaitForSingleObject(ov2
.hEvent
, 100);
3144 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3145 if (dwRet
== WAIT_OBJECT_0
)
3147 bret
= GetOverlappedResult((HANDLE
)src
, &ov2
, &bytesReturned
, FALSE
);
3148 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3149 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3150 ok(buffer
[1] == '2', "Got %c instead of 2\n", buffer
[1]);
3153 ret
= send(dst
, "1", 1, 0);
3154 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3155 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3157 ret
= recv(src
, buffer
, 1, 0);
3158 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3159 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3161 /* Notifications are delivered as soon as possible, blocked only on
3162 * async requests on the same type */
3163 bufs
.len
= sizeof(char);
3165 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3166 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3167 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3170 ret
= send(dst
, "1", 1, MSG_OOB
);
3171 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3172 ok_event_seq(src
, hEvent
, oob_seq
, NULL
, 0);
3175 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3176 ok(dwRet
== WAIT_TIMEOUT
, "OOB message activated read?: %d - %d\n", dwRet
, GetLastError());
3178 ret
= send(dst
, "2", 1, 0);
3179 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3180 broken_seq
[0] = read_seq
; /* win98 */
3181 broken_seq
[1] = NULL
;
3182 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3184 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3185 ok(dwRet
== WAIT_OBJECT_0
|| broken(dwRet
== WAIT_TIMEOUT
),
3186 "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3187 if (dwRet
== WAIT_OBJECT_0
)
3189 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3190 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3191 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3192 ok(buffer
[0] == '2', "Got %c instead of 2\n", buffer
[0]);
3194 else if (dwRet
== WAIT_TIMEOUT
)
3196 /* this happens on win98. We get an FD_READ later on the next test */
3197 CancelIo((HANDLE
) src
);
3201 ret
= recv(src
, buffer
, 1, MSG_OOB
);
3202 todo_wine
ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3203 /* We get OOB notification, but no data on wine */
3204 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3207 /* Flood the send queue */
3208 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
3209 if (hThread
== NULL
)
3211 ok(0, "CreateThread failed, error %d\n", GetLastError());
3215 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3216 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3218 /* Now if we send a ton of data and the 'server' does not drain it fast
3219 * enough (set drain_pause to be sure), the socket send buffer will only
3220 * take some of it, and we will get a short write. This will trigger
3221 * another FD_WRITE event as soon as data is sent and more space becomes
3222 * available, but not any earlier. */
3226 ret
= send(src
, buffer
, bufferSize
, 0);
3227 } while (ret
== bufferSize
);
3229 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
3231 Sleep(400); /* win9x */
3232 broken_seq
[0] = read_write_seq
;
3233 broken_seq
[1] = NULL
;
3234 ok_event_seq(src
, hEvent
, write_seq
, broken_seq
, 0);
3238 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3241 /* Test how FD_CLOSE is handled */
3242 ret
= send(dst
, "12", 2, 0);
3243 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3245 /* Wait a little and let the send complete */
3248 dst
= INVALID_SOCKET
;
3251 /* We can never implement this in wine, best we can hope for is
3252 sending FD_CLOSE after the reads complete */
3253 broken_seq
[0] = read_seq
; /* win9x */
3254 broken_seq
[1] = NULL
;
3255 todo_wine
ok_event_seq(src
, hEvent
, read_close_seq
, broken_seq
, 0);
3257 ret
= recv(src
, buffer
, 1, 0);
3258 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3259 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3261 ret
= recv(src
, buffer
, 1, 0);
3262 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3263 /* want it? it's here, but you can't have it */
3264 broken_seq
[0] = close_seq
; /* win9x */
3265 broken_seq
[1] = NULL
;
3266 todo_wine
ok_event_seq(src
, hEvent
, empty_seq
, /* wine sends FD_CLOSE here */
3269 /* Test how FD_CLOSE is handled */
3270 ret
= send(dst2
, "12", 2, 0);
3271 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3274 shutdown(dst2
, SD_SEND
);
3277 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3278 regressions, don't mark them as todo_wine, and mark windows as broken */
3279 broken_seq
[0] = read_close_seq
;
3280 broken_seq
[1] = close_seq
;
3281 broken_seq
[2] = NULL
;
3282 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3284 ret
= recv(src2
, buffer
, 1, 0);
3285 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3286 broken_seq
[0] = close_seq
; /* win98 */
3287 broken_seq
[1] = NULL
;
3288 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3290 ret
= recv(src2
, buffer
, 1, 0);
3291 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3292 broken_seq
[0] = empty_seq
;
3293 broken_seq
[1] = NULL
;
3294 ok_event_seq(src2
, hEvent2
, close_seq
, broken_seq
, 0);
3296 ret
= send(src2
, "1", 1, 0);
3297 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3298 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3300 ret
= send(src2
, "1", 1, 0);
3301 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3302 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3305 if (src
!= INVALID_SOCKET
)
3307 flush_events(src
, hEvent
);
3310 if (src2
!= INVALID_SOCKET
)
3312 flush_events(src2
, hEvent2
);
3315 HeapFree(GetProcessHeap(), 0, buffer
);
3316 if (server
!= INVALID_SOCKET
)
3317 closesocket(server
);
3318 if (dst
!= INVALID_SOCKET
)
3320 if (dst2
!= INVALID_SOCKET
)
3322 if (hThread
!= NULL
)
3323 CloseHandle(hThread
);
3327 CloseHandle(hEvent
);
3328 if (hEvent2
!= NULL
)
3329 CloseHandle(hEvent2
);
3330 if (ov
.hEvent
!= NULL
)
3331 CloseHandle(ov
.hEvent
);
3332 if (ov2
.hEvent
!= NULL
)
3333 CloseHandle(ov2
.hEvent
);
3336 static void test_ipv6only(void)
3338 SOCKET v4
= INVALID_SOCKET
,
3339 v6
= INVALID_SOCKET
;
3340 struct sockaddr_in sin4
;
3341 struct sockaddr_in6 sin6
;
3344 memset(&sin4
, 0, sizeof(sin4
));
3345 sin4
.sin_family
= AF_INET
;
3346 sin4
.sin_port
= htons(SERVERPORT
);
3348 memset(&sin6
, 0, sizeof(sin6
));
3349 sin6
.sin6_family
= AF_INET6
;
3350 sin6
.sin6_port
= htons(SERVERPORT
);
3352 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
3353 if (v6
== INVALID_SOCKET
) {
3354 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3355 WSAGetLastError(), WSAEAFNOSUPPORT
);
3358 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
3360 skip("Could not bind IPv6 address (LastError: %d).\n",
3365 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
3366 if (v4
== INVALID_SOCKET
) {
3367 skip("Could not create IPv4 socket (LastError: %d).\n",
3371 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
3372 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
3373 WSAGetLastError(), WSAEADDRINUSE
);
3376 if (v4
!= INVALID_SOCKET
)
3378 if (v6
!= INVALID_SOCKET
)
3382 static void test_WSASendTo(void)
3385 struct sockaddr_in addr
;
3386 char buf
[12] = "hello world";
3390 addr
.sin_family
= AF_INET
;
3391 addr
.sin_port
= htons(139);
3392 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3393 data_buf
.len
= sizeof(buf
);
3396 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
3397 ok(0, "socket() failed error: %d\n", WSAGetLastError());
3401 WSASetLastError(12345);
3402 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
3403 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
3406 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
3407 "a successful call to WSASendTo()\n");
3410 static void test_WSARecv(void)
3416 DWORD bytesReturned
;
3423 tcp_socketpair(&src
, &dest
);
3424 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
3426 skip("failed to create sockets\n");
3430 bufs
.len
= sizeof(buf
);
3434 memset(&ov
, 0, sizeof(ov
));
3435 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3436 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
3442 ok(!setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
)), "Failed to set linger %d\n", GetLastError());
3444 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3445 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
3448 src
= INVALID_SOCKET
;
3450 dwret
= WaitForSingleObject(ov
.hEvent
, 1000);
3451 ok(dwret
== WAIT_OBJECT_0
, "Waiting for disconnect event failed with %d + errno %d\n", dwret
, GetLastError());
3453 bret
= GetOverlappedResult((HANDLE
)dest
, &ov
, &bytesReturned
, FALSE
);
3454 todo_wine
ok(!bret
&& (GetLastError() == ERROR_NETNAME_DELETED
|| broken(GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */),
3455 "Did not get disconnect event: %d, error %d\n", bret
, GetLastError());
3456 ok(bytesReturned
== 0, "Bytes received is %d\n", bytesReturned
);
3459 if (dest
!= INVALID_SOCKET
)
3461 if (src
!= INVALID_SOCKET
)
3464 WSACloseEvent(ov
.hEvent
);
3467 static void test_GetAddrInfoW(void)
3469 static const WCHAR port
[] = {'8','0',0};
3470 static const WCHAR empty
[] = {0};
3471 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
3474 ADDRINFOW
*result
, hint
;
3476 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
3478 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
3482 memset(&hint
, 0, sizeof(ADDRINFOW
));
3484 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
3485 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
3488 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
3491 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3492 ok(result
!= NULL
, "GetAddrInfoW failed\n");
3494 pFreeAddrInfoW(result
);
3496 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
3497 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3498 pFreeAddrInfoW(result
);
3500 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
3501 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3502 pFreeAddrInfoW(result
);
3504 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
3505 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3506 pFreeAddrInfoW(result
);
3509 static void test_ConnectEx(void)
3511 SOCKET listener
= INVALID_SOCKET
;
3512 SOCKET acceptor
= INVALID_SOCKET
;
3513 SOCKET connector
= INVALID_SOCKET
;
3514 struct sockaddr_in address
, conaddress
;
3516 OVERLAPPED overlapped
;
3517 LPFN_CONNECTEX pConnectEx
;
3518 GUID connectExGuid
= WSAID_CONNECTEX
;
3519 DWORD bytesReturned
;
3525 memset(&overlapped
, 0, sizeof(overlapped
));
3527 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
3528 if (listener
== INVALID_SOCKET
) {
3529 skip("could not create listener socket, error %d\n", WSAGetLastError());
3533 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3534 if (connector
== INVALID_SOCKET
) {
3535 skip("could not create connector socket, error %d\n", WSAGetLastError());
3539 memset(&address
, 0, sizeof(address
));
3540 address
.sin_family
= AF_INET
;
3541 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3542 iret
= bind(listener
, (struct sockaddr
*)&address
, sizeof(address
));
3544 skip("failed to bind, error %d\n", WSAGetLastError());
3548 addrlen
= sizeof(address
);
3549 iret
= getsockname(listener
, (struct sockaddr
*)&address
, &addrlen
);
3551 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
3555 if (set_blocking(listener
, TRUE
)) {
3556 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3560 iret
= WSAIoctl(connector
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &connectExGuid
, sizeof(connectExGuid
),
3561 &pConnectEx
, sizeof(pConnectEx
), &bytesReturned
, NULL
, NULL
);
3563 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret
, WSAGetLastError());
3567 bret
= pConnectEx(INVALID_SOCKET
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3568 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "ConnectEx on invalid socket "
3569 "returned %d + errno %d\n", bret
, WSAGetLastError());
3571 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3572 todo_wine
ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "ConnectEx on a unbound socket "
3573 "returned %d + errno %d\n", bret
, WSAGetLastError());
3574 if (bret
== TRUE
|| WSAGetLastError() != WSAEINVAL
)
3576 acceptor
= accept(listener
, NULL
, NULL
);
3577 if (acceptor
!= INVALID_SOCKET
) {
3578 closesocket(acceptor
);
3579 acceptor
= INVALID_SOCKET
;
3582 closesocket(connector
);
3583 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3584 if (connector
== INVALID_SOCKET
) {
3585 skip("could not create connector socket, error %d\n", WSAGetLastError());
3590 /* ConnectEx needs a bound socket */
3591 memset(&conaddress
, 0, sizeof(conaddress
));
3592 conaddress
.sin_family
= AF_INET
;
3593 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3594 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
3596 skip("failed to bind, error %d\n", WSAGetLastError());
3600 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, NULL
);
3601 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "ConnectEx on a NULL overlapped "
3602 "returned %d + errno %d\n", bret
, WSAGetLastError());
3604 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3605 if (overlapped
.hEvent
== NULL
) {
3606 skip("could not create event object, errno = %d\n", GetLastError());
3610 iret
= listen(listener
, 1);
3612 skip("listening failed, errno = %d\n", WSAGetLastError());
3616 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3617 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
3618 "returned %d + errno %d\n", bret
, WSAGetLastError());
3619 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
3620 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
3622 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
3623 ok(bret
, "Connecting failed, error %d\n", GetLastError());
3624 ok(bytesReturned
== 0, "Bytes sent is %d\n", bytesReturned
);
3626 closesocket(connector
);
3627 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3628 if (connector
== INVALID_SOCKET
) {
3629 skip("could not create connector socket, error %d\n", WSAGetLastError());
3632 /* ConnectEx needs a bound socket */
3633 memset(&conaddress
, 0, sizeof(conaddress
));
3634 conaddress
.sin_family
= AF_INET
;
3635 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3636 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
3638 skip("failed to bind, error %d\n", WSAGetLastError());
3642 acceptor
= accept(listener
, NULL
, NULL
);
3643 if (acceptor
!= INVALID_SOCKET
) {
3644 closesocket(acceptor
);
3645 acceptor
= INVALID_SOCKET
;
3651 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, buffer
, 3, &bytesReturned
, &overlapped
);
3652 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
3653 "returned %d + errno %d\n", bret
, WSAGetLastError());
3654 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
3655 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
3657 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
3658 ok(bret
, "Connecting failed, error %d\n", GetLastError());
3659 ok(bytesReturned
== 3, "Bytes sent is %d\n", bytesReturned
);
3661 acceptor
= accept(listener
, NULL
, NULL
);
3662 ok(acceptor
!= INVALID_SOCKET
, "could not accept socket error %d\n", WSAGetLastError());
3664 bytesReturned
= recv(acceptor
, buffer
, 3, 0);
3666 ok(bytesReturned
== 3, "Didn't get all sent data, got only %d\n", bytesReturned
);
3667 ok(buffer
[0] == '1' && buffer
[1] == '2' && buffer
[2] == '3',
3668 "Failed to get the right data, expected '123', got '%s'\n", buffer
);
3670 closesocket(connector
);
3671 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3672 if (connector
== INVALID_SOCKET
) {
3673 skip("could not create connector socket, error %d\n", WSAGetLastError());
3676 /* ConnectEx needs a bound socket */
3677 memset(&conaddress
, 0, sizeof(conaddress
));
3678 conaddress
.sin_family
= AF_INET
;
3679 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3680 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
3682 skip("failed to bind, error %d\n", WSAGetLastError());
3686 if (acceptor
!= INVALID_SOCKET
) {
3687 closesocket(acceptor
);
3688 acceptor
= INVALID_SOCKET
;
3691 /* Connect with error */
3692 closesocket(listener
);
3693 listener
= INVALID_SOCKET
;
3695 address
.sin_port
= 1;
3697 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
3698 ok(bret
== FALSE
&& GetLastError(), "ConnectEx to bad destination failed: "
3699 "returned %d + errno %d\n", bret
, GetLastError());
3701 if (GetLastError() == ERROR_IO_PENDING
)
3703 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
3704 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
3706 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
3707 ok(bret
== FALSE
&& GetLastError() == ERROR_CONNECTION_REFUSED
,
3708 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
3711 ok(GetLastError() == WSAECONNREFUSED
,
3712 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
3716 if (overlapped
.hEvent
)
3717 WSACloseEvent(overlapped
.hEvent
);
3718 if (listener
!= INVALID_SOCKET
)
3719 closesocket(listener
);
3720 if (acceptor
!= INVALID_SOCKET
)
3721 closesocket(acceptor
);
3722 if (connector
!= INVALID_SOCKET
)
3723 closesocket(connector
);
3726 static void test_AcceptEx(void)
3728 SOCKET listener
= INVALID_SOCKET
;
3729 SOCKET acceptor
= INVALID_SOCKET
;
3730 SOCKET connector
= INVALID_SOCKET
;
3731 SOCKET connector2
= INVALID_SOCKET
;
3732 struct sockaddr_in bindAddress
;
3734 GUID acceptExGuid
= WSAID_ACCEPTEX
;
3735 LPFN_ACCEPTEX pAcceptEx
= NULL
;
3736 fd_set fds_accept
, fds_send
;
3737 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
3739 DWORD bytesReturned
;
3741 OVERLAPPED overlapped
;
3746 memset(&overlapped
, 0, sizeof(overlapped
));
3748 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
3749 if (listener
== INVALID_SOCKET
) {
3750 skip("could not create listener socket, error %d\n", WSAGetLastError());
3754 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3755 if (acceptor
== INVALID_SOCKET
) {
3756 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3760 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3761 if (connector
== INVALID_SOCKET
) {
3762 skip("could not create connector socket, error %d\n", WSAGetLastError());
3766 memset(&bindAddress
, 0, sizeof(bindAddress
));
3767 bindAddress
.sin_family
= AF_INET
;
3768 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3769 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3771 skip("failed to bind, error %d\n", WSAGetLastError());
3775 socklen
= sizeof(bindAddress
);
3776 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
3778 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
3782 if (set_blocking(listener
, FALSE
)) {
3783 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3787 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
3788 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
3790 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
3794 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3795 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3796 &bytesReturned
, &overlapped
);
3797 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
3798 "returned %d + errno %d\n", bret
, WSAGetLastError());
3800 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3801 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3802 &bytesReturned
, &overlapped
);
3803 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on invalid accepting socket "
3804 "returned %d + errno %d\n", bret
, WSAGetLastError());
3806 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3807 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3808 &bytesReturned
, &overlapped
);
3810 (WSAGetLastError() == WSAEINVAL
||
3811 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
3812 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
3814 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
3815 &bytesReturned
, &overlapped
);
3816 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small local address size "
3817 "returned %d + errno %d\n", bret
, WSAGetLastError());
3819 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
3820 &bytesReturned
, &overlapped
);
3821 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small remote address size "
3822 "returned %d + errno %d\n", bret
, WSAGetLastError());
3824 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3825 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3826 &bytesReturned
, NULL
);
3827 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
3828 "returned %d + errno %d\n", bret
, WSAGetLastError());
3830 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3831 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3832 &bytesReturned
, &overlapped
);
3833 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
3834 "returned %d + errno %d\n", bret
, WSAGetLastError());
3836 iret
= listen(listener
, 5);
3838 skip("listening failed, errno = %d\n", WSAGetLastError());
3842 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3843 if (overlapped
.hEvent
== NULL
) {
3844 skip("could not create event object, errno = %d\n", GetLastError());
3848 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3849 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3850 &bytesReturned
, &overlapped
);
3851 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3853 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3854 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3855 &bytesReturned
, &overlapped
);
3856 ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
3857 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
3858 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
3859 /* We need to cancel this call, otherwise things fail */
3860 bret
= CancelIo((HANDLE
) listener
);
3861 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3864 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3865 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3866 &bytesReturned
, &overlapped
);
3867 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3870 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3871 ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
3872 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
3873 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
3874 /* We need to cancel this call, otherwise things fail */
3875 closesocket(acceptor
);
3876 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3877 if (acceptor
== INVALID_SOCKET
) {
3878 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3882 bret
= CancelIo((HANDLE
) listener
);
3883 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3886 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3887 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3888 &bytesReturned
, &overlapped
);
3889 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3892 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3893 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3895 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
3896 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3898 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3899 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3900 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3902 closesocket(connector
);
3903 connector
= INVALID_SOCKET
;
3904 closesocket(acceptor
);
3905 acceptor
= INVALID_SOCKET
;
3907 /* Test short reads */
3909 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3910 if (acceptor
== INVALID_SOCKET
) {
3911 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3914 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3915 if (connector
== INVALID_SOCKET
) {
3916 skip("could not create connector socket, error %d\n", WSAGetLastError());
3919 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
3920 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3921 &bytesReturned
, &overlapped
);
3922 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3924 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3925 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3927 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3928 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
3930 iret
= send(connector
, buffer
, 1, 0);
3931 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
3933 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3934 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3936 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3937 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3938 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3940 closesocket(connector
);
3941 connector
= INVALID_SOCKET
;
3942 closesocket(acceptor
);
3943 acceptor
= INVALID_SOCKET
;
3945 /* Test CF_DEFER & AcceptEx interaction */
3947 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3948 if (acceptor
== INVALID_SOCKET
) {
3949 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3952 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3953 if (connector
== INVALID_SOCKET
) {
3954 skip("could not create connector socket, error %d\n", WSAGetLastError());
3957 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
3958 if (connector
== INVALID_SOCKET
) {
3959 skip("could not create connector socket, error %d\n", WSAGetLastError());
3963 if (set_blocking(connector
, FALSE
)) {
3964 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3968 if (set_blocking(connector2
, FALSE
)) {
3969 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3973 /* Connect socket #1 */
3974 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3975 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3977 FD_ZERO ( &fds_accept
);
3978 FD_ZERO ( &fds_send
);
3980 FD_SET ( listener
, &fds_accept
);
3981 FD_SET ( connector
, &fds_send
);
3987 for (i
= 0; i
< 4000; ++i
)
3989 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
3991 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
3992 "acceptex test(%d): could not select on socket, errno %d\n" );
3994 /* check for incoming requests */
3995 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
3998 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
3999 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
4000 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
4001 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4002 &bytesReturned
, &overlapped
);
4003 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4005 else if (got
== 2) {
4006 /* this should be socket #2 */
4007 SOCKET tmp
= accept(listener
, NULL
, NULL
);
4008 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
4012 ok(FALSE
, "Got more than 2 connections?\n");
4015 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
4016 /* Send data on second socket, and stop */
4017 send(connector2
, "2", 1, 0);
4018 FD_CLR ( connector2
, &fds_send
);
4022 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
4023 /* Once #1 is connected, allow #2 to connect */
4026 send(connector
, "1", 1, 0);
4027 FD_CLR ( connector
, &fds_send
);
4029 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4030 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4031 FD_SET ( connector2
, &fds_send
);
4035 ok (got
== 2 || broken(got
== 1) /* NT4 */,
4036 "Did not get both connections, got %d\n", got
);
4038 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
4039 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4041 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4042 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4043 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4045 set_blocking(acceptor
, TRUE
);
4046 iret
= recv( acceptor
, buffer
, 2, 0);
4047 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
4049 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
4051 closesocket(connector
);
4052 connector
= INVALID_SOCKET
;
4053 closesocket(acceptor
);
4054 acceptor
= INVALID_SOCKET
;
4056 /* clean up in case of failures */
4057 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
4058 closesocket(acceptor
);
4060 /* Disconnect during receive? */
4062 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4063 if (acceptor
== INVALID_SOCKET
) {
4064 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4067 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
4068 if (connector
== INVALID_SOCKET
) {
4069 skip("could not create connector socket, error %d\n", WSAGetLastError());
4072 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4073 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4074 &bytesReturned
, &overlapped
);
4075 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4077 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
4078 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
4080 closesocket(connector
);
4081 connector
= INVALID_SOCKET
;
4083 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4084 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4086 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4087 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
4088 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
4090 closesocket(acceptor
);
4091 acceptor
= INVALID_SOCKET
;
4093 /* Test closing with pending requests */
4095 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4096 if (acceptor
== INVALID_SOCKET
) {
4097 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4100 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4101 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4102 &bytesReturned
, &overlapped
);
4103 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4105 closesocket(acceptor
);
4106 acceptor
= INVALID_SOCKET
;
4108 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4109 ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
4110 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4112 if (dwret
!= WAIT_TIMEOUT
) {
4113 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4114 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
4117 bret
= CancelIo((HANDLE
) listener
);
4118 ok(bret
, "Failed to cancel failed test. Bailing...\n");
4122 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4123 if (acceptor
== INVALID_SOCKET
) {
4124 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4127 bret
= pAcceptEx(listener
, acceptor
, 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() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4132 CancelIo((HANDLE
) acceptor
);
4134 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4135 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
4137 closesocket(acceptor
);
4138 acceptor
= INVALID_SOCKET
;
4140 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
4141 if (acceptor
== INVALID_SOCKET
) {
4142 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
4145 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
4146 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
4147 &bytesReturned
, &overlapped
);
4148 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
4150 closesocket(listener
);
4151 listener
= INVALID_SOCKET
;
4153 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
4154 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
4156 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
4157 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
4160 if (overlapped
.hEvent
)
4161 WSACloseEvent(overlapped
.hEvent
);
4162 if (listener
!= INVALID_SOCKET
)
4163 closesocket(listener
);
4164 if (acceptor
!= INVALID_SOCKET
)
4165 closesocket(acceptor
);
4166 if (connector
!= INVALID_SOCKET
)
4167 closesocket(connector
);
4168 if (connector2
!= INVALID_SOCKET
)
4169 closesocket(connector2
);
4172 static void test_getpeername(void)
4175 struct sockaddr_in sa
, sa_out
;
4177 const char buf
[] = "hello world";
4180 /* Test the parameter validation order. */
4181 ret
= getpeername(INVALID_SOCKET
, NULL
, NULL
);
4182 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4183 ok(WSAGetLastError() == WSAENOTSOCK
,
4184 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
4186 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
4187 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
4188 if (sock
== INVALID_SOCKET
)
4190 skip("Socket creation failed with %d\n", WSAGetLastError());
4194 ret
= getpeername(sock
, NULL
, NULL
);
4195 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4196 ok(WSAGetLastError() == WSAENOTCONN
||
4197 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
4198 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4200 memset(&sa
, 0, sizeof(sa
));
4201 sa
.sin_family
= AF_INET
;
4202 sa
.sin_port
= htons(139);
4203 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
4205 /* sendto does not change a socket's connection state. */
4206 ret
= sendto(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sa
, sizeof(sa
));
4207 ok(ret
!= SOCKET_ERROR
,
4208 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4210 ret
= getpeername(sock
, NULL
, NULL
);
4211 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4212 ok(WSAGetLastError() == WSAENOTCONN
||
4213 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
4214 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4216 ret
= connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
));
4218 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4220 ret
= getpeername(sock
, NULL
, NULL
);
4221 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4222 ok(WSAGetLastError() == WSAEFAULT
,
4223 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4225 /* Test crashes on Wine. */
4228 ret
= getpeername(sock
, (void*)0xdeadbeef, (void*)0xcafebabe);
4229 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4230 ok(WSAGetLastError() == WSAEFAULT
,
4231 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4235 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4236 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
4237 ok(WSAGetLastError() == WSAEFAULT
,
4238 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4240 sa_len
= sizeof(sa_out
);
4241 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4242 ok(ret
== 0, "Expected getpeername to return 0, got %d\n", ret
);
4243 ok(!memcmp(&sa
, &sa_out
, sizeof(sa
)),
4244 "Expected the returned structure to be identical to the connect structure\n");
4249 /**************** Main program ***************/
4255 /* Leave these tests at the beginning. They depend on WSAStartup not having been
4256 * called, which is done by Init() below. */
4257 test_WithoutWSAStartup();
4258 test_WithWSAStartup();
4262 test_set_getsockopt();
4263 test_so_reuseaddr();
4264 test_extendedSocketOptions();
4266 for (i
= 0; i
< NUM_TESTS
; i
++)
4268 trace ( " **** STARTING TEST %d ****\n", i
);
4269 do_test ( &tests
[i
] );
4270 trace ( " **** TEST %d COMPLETE ****\n", i
);
4275 test_getservbyname();
4278 test_WSAAddressToStringA();
4279 test_WSAAddressToStringW();
4281 test_WSAStringToAddressA();
4282 test_WSAStringToAddressW();
4289 test_addr_to_print();
4292 test_gethostbyname_hack();
4301 test_GetAddrInfoW();
4306 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */