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
34 #include "wine/test.h"
36 #define MAX_CLIENTS 4 /* Max number of clients */
37 #define FIRST_CHAR 'A' /* First character in transferred pattern */
38 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
39 #define BIND_TRIES 6 /* Number of bind() attempts */
40 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
41 after server initialization, if something hangs */
43 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
45 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
46 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
48 #define SERVERIP "127.0.0.1" /* IP to bind to */
49 #define SERVERPORT 9374 /* Port number to bind to */
51 #define wsa_ok(op, cond, msg) \
55 if ( !(cond tmp) ) err = WSAGetLastError(); \
56 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
59 #define make_keepalive(k, enable, time, interval) \
61 k.keepalivetime = time; \
62 k.keepaliveinterval = interval;
64 /* Function pointers */
65 static void (WINAPI
*pfreeaddrinfo
)(struct addrinfo
*);
66 static int (WINAPI
*pgetaddrinfo
)(LPCSTR
,LPCSTR
,const struct addrinfo
*,struct addrinfo
**);
67 static void (WINAPI
*pFreeAddrInfoW
)(PADDRINFOW
);
68 static int (WINAPI
*pGetAddrInfoW
)(LPCWSTR
,LPCWSTR
,const ADDRINFOW
*,PADDRINFOW
*);
69 static PCSTR (WINAPI
*pInetNtop
)(INT
,LPVOID
,LPSTR
,ULONG
);
70 static int (WINAPI
*pInetPton
)(INT
,LPSTR
,LPVOID
);
71 static int (WINAPI
*pWSALookupServiceBeginW
)(LPWSAQUERYSETW
,DWORD
,LPHANDLE
);
72 static int (WINAPI
*pWSALookupServiceEnd
)(HANDLE
);
73 static int (WINAPI
*pWSALookupServiceNextW
)(HANDLE
,DWORD
,LPDWORD
,LPWSAQUERYSETW
);
75 /**************** Structs and typedefs ***************/
77 typedef struct thread_info
83 /* Information in the server about open client connections */
84 typedef struct sock_info
87 struct sockaddr_in addr
;
88 struct sockaddr_in peer
;
94 /* Test parameters for both server & client */
95 typedef struct test_params
99 const char *inet_addr
;
106 /* server-specific test parameters */
107 typedef struct server_params
109 test_params
*general
;
114 /* client-specific test parameters */
115 typedef struct client_params
117 test_params
*general
;
122 /* This type combines all information for setting up a test scenario */
123 typedef struct test_setup
127 server_params srv_params
;
129 client_params clt_params
;
132 /* Thread local storage for server */
133 typedef struct server_memory
136 struct sockaddr_in addr
;
137 sock_info sock
[MAX_CLIENTS
];
140 /* Thread local storage for client */
141 typedef struct client_memory
144 struct sockaddr_in addr
;
149 /* SelectReadThread thread parameters */
150 typedef struct select_thread_params
154 } select_thread_params
;
156 /* Tests used in both getaddrinfo and GetAddrInfoW */
157 static const struct addr_hint_tests
159 int family
, socktype
, protocol
;
162 {AF_UNSPEC
, SOCK_STREAM
, IPPROTO_TCP
, 0 },
163 {AF_UNSPEC
, SOCK_STREAM
, IPPROTO_UDP
, 0 },
164 {AF_UNSPEC
, SOCK_DGRAM
, IPPROTO_TCP
, 0 },
165 {AF_UNSPEC
, SOCK_DGRAM
, IPPROTO_UDP
, 0 },
166 {AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, 0 },
167 {AF_INET
, SOCK_STREAM
, IPPROTO_UDP
, 0 },
168 {AF_INET
, SOCK_DGRAM
, IPPROTO_TCP
, 0 },
169 {AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, 0 },
170 {AF_UNSPEC
, 0, IPPROTO_TCP
, 0 },
171 {AF_UNSPEC
, 0, IPPROTO_UDP
, 0 },
172 {AF_UNSPEC
, SOCK_STREAM
, 0, 0 },
173 {AF_UNSPEC
, SOCK_DGRAM
, 0, 0 },
174 {AF_INET
, 0, IPPROTO_TCP
, 0 },
175 {AF_INET
, 0, IPPROTO_UDP
, 0 },
176 {AF_INET
, SOCK_STREAM
, 0, 0 },
177 {AF_INET
, SOCK_DGRAM
, 0, 0 },
178 {AF_UNSPEC
, 999, IPPROTO_TCP
, WSAESOCKTNOSUPPORT
},
179 {AF_UNSPEC
, 999, IPPROTO_UDP
, WSAESOCKTNOSUPPORT
},
180 {AF_INET
, 999, IPPROTO_TCP
, WSAESOCKTNOSUPPORT
},
181 {AF_INET
, 999, IPPROTO_UDP
, WSAESOCKTNOSUPPORT
},
182 {AF_UNSPEC
, SOCK_STREAM
, 999, 0 },
183 {AF_UNSPEC
, SOCK_STREAM
, 999, 0 },
184 {AF_INET
, SOCK_DGRAM
, 999, 0 },
185 {AF_INET
, SOCK_DGRAM
, 999, 0 },
188 /**************** Static variables ***************/
190 static DWORD tls
; /* Thread local storage index */
191 static HANDLE thread
[1+MAX_CLIENTS
];
192 static DWORD thread_id
[1+MAX_CLIENTS
];
193 static HANDLE server_ready
;
194 static HANDLE client_ready
[MAX_CLIENTS
];
195 static int client_id
;
197 /**************** General utility functions ***************/
199 static SOCKET
setup_server_socket(struct sockaddr_in
*addr
, int *len
);
200 static SOCKET
setup_connector_socket(struct sockaddr_in
*addr
, int len
, BOOL nonblock
);
202 static int tcp_socketpair(SOCKET
*src
, SOCKET
*dst
)
204 SOCKET server
= INVALID_SOCKET
;
205 struct sockaddr_in addr
;
209 *src
= INVALID_SOCKET
;
210 *dst
= INVALID_SOCKET
;
212 *src
= socket(AF_INET
, SOCK_STREAM
, 0);
213 if (*src
== INVALID_SOCKET
)
216 server
= socket(AF_INET
, SOCK_STREAM
, 0);
217 if (server
== INVALID_SOCKET
)
220 memset(&addr
, 0, sizeof(addr
));
221 addr
.sin_family
= AF_INET
;
222 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
223 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
228 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
232 ret
= listen(server
, 1);
236 ret
= connect(*src
, (struct sockaddr
*)&addr
, sizeof(addr
));
241 *dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
244 if (server
!= INVALID_SOCKET
)
246 if (*src
!= INVALID_SOCKET
&& *dst
!= INVALID_SOCKET
)
253 static void set_so_opentype ( BOOL overlapped
)
255 int optval
= !overlapped
, newval
, len
= sizeof (int);
257 ok ( setsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
258 (LPVOID
) &optval
, sizeof (optval
) ) == 0,
259 "setting SO_OPENTYPE failed\n" );
260 ok ( getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
261 (LPVOID
) &newval
, &len
) == 0,
262 "getting SO_OPENTYPE failed\n" );
263 ok ( optval
== newval
, "failed to set SO_OPENTYPE\n" );
266 static int set_blocking ( SOCKET s
, BOOL blocking
)
268 u_long val
= !blocking
;
269 return ioctlsocket ( s
, FIONBIO
, &val
);
272 static void fill_buffer ( char *buf
, int chunk_size
, int n_chunks
)
275 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
276 memset ( p
, c
, chunk_size
);
279 static int test_buffer ( char *buf
, int chunk_size
, int n_chunks
)
283 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
285 for ( i
= 0; i
< chunk_size
; i
++ )
286 if ( p
[i
] != c
) return i
;
292 * This routine is called when a client / server does not expect any more data,
293 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
295 static void read_zero_bytes ( SOCKET s
)
299 while ( ( tmp
= recv ( s
, buf
, 256, 0 ) ) > 0 )
301 ok ( n
<= 0, "garbage data received: %d bytes\n", n
);
304 static int do_synchronous_send ( SOCKET s
, char *buf
, int buflen
, int flags
, int sendlen
)
306 char* last
= buf
+ buflen
, *p
;
308 for ( p
= buf
; n
> 0 && p
< last
; )
310 n
= send ( s
, p
, min ( sendlen
, last
- p
), flags
);
313 wsa_ok ( n
, 0 <=, "do_synchronous_send (%x): error %d\n" );
317 static int do_synchronous_recv ( SOCKET s
, char *buf
, int buflen
, int flags
, int recvlen
)
319 char* last
= buf
+ buflen
, *p
;
321 for ( p
= buf
; n
> 0 && p
< last
; )
323 n
= recv ( s
, p
, min ( recvlen
, last
- p
), flags
);
326 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
330 static int do_synchronous_recvfrom ( SOCKET s
, char *buf
, int buflen
, int flags
, struct sockaddr
*from
, int *fromlen
, int recvlen
)
332 char* last
= buf
+ buflen
, *p
;
334 for ( p
= buf
; n
> 0 && p
< last
; )
336 n
= recvfrom ( s
, p
, min ( recvlen
, last
- p
), flags
, from
, fromlen
);
339 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
344 * Call this routine right after thread startup.
345 * SO_OPENTYPE must by 0, regardless what the server did.
347 static void check_so_opentype (void)
351 getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (LPVOID
) &tmp
, &len
);
352 ok ( tmp
== 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp
);
355 static void compare_addrinfo (ADDRINFO
*a
, ADDRINFO
*b
)
357 for (; a
&& b
; a
= a
->ai_next
, b
= b
->ai_next
)
359 ok(a
->ai_flags
== b
->ai_flags
,
360 "Wrong flags %d != %d\n", a
->ai_flags
, b
->ai_flags
);
361 ok(a
->ai_family
== b
->ai_family
,
362 "Wrong family %d != %d\n", a
->ai_family
, b
->ai_family
);
363 ok(a
->ai_socktype
== b
->ai_socktype
,
364 "Wrong socktype %d != %d\n", a
->ai_socktype
, b
->ai_socktype
);
365 ok(a
->ai_protocol
== b
->ai_protocol
,
366 "Wrong protocol %d != %d\n", a
->ai_protocol
, b
->ai_protocol
);
367 ok(a
->ai_addrlen
== b
->ai_addrlen
,
368 "Wrong addrlen %lu != %lu\n", a
->ai_addrlen
, b
->ai_addrlen
);
369 ok(!memcmp(a
->ai_addr
, b
->ai_addr
, min(a
->ai_addrlen
, b
->ai_addrlen
)),
370 "Wrong address data\n");
371 if (a
->ai_canonname
&& b
->ai_canonname
)
373 ok(!strcmp(a
->ai_canonname
, b
->ai_canonname
), "Wrong canonical name '%s' != '%s'\n",
374 a
->ai_canonname
, b
->ai_canonname
);
377 ok(!a
->ai_canonname
&& !b
->ai_canonname
, "Expected both names absent (%p != %p)\n",
378 a
->ai_canonname
, b
->ai_canonname
);
380 ok(!a
&& !b
, "Expected both addresses null (%p != %p)\n", a
, b
);
383 static void compare_addrinfow (ADDRINFOW
*a
, ADDRINFOW
*b
)
385 for (; a
&& b
; a
= a
->ai_next
, b
= b
->ai_next
)
387 ok(a
->ai_flags
== b
->ai_flags
,
388 "Wrong flags %d != %d\n", a
->ai_flags
, b
->ai_flags
);
389 ok(a
->ai_family
== b
->ai_family
,
390 "Wrong family %d != %d\n", a
->ai_family
, b
->ai_family
);
391 ok(a
->ai_socktype
== b
->ai_socktype
,
392 "Wrong socktype %d != %d\n", a
->ai_socktype
, b
->ai_socktype
);
393 ok(a
->ai_protocol
== b
->ai_protocol
,
394 "Wrong protocol %d != %d\n", a
->ai_protocol
, b
->ai_protocol
);
395 ok(a
->ai_addrlen
== b
->ai_addrlen
,
396 "Wrong addrlen %lu != %lu\n", a
->ai_addrlen
, b
->ai_addrlen
);
397 ok(!memcmp(a
->ai_addr
, b
->ai_addr
, min(a
->ai_addrlen
, b
->ai_addrlen
)),
398 "Wrong address data\n");
399 if (a
->ai_canonname
&& b
->ai_canonname
)
401 ok(!lstrcmpW(a
->ai_canonname
, b
->ai_canonname
), "Wrong canonical name '%s' != '%s'\n",
402 wine_dbgstr_w(a
->ai_canonname
), wine_dbgstr_w(b
->ai_canonname
));
405 ok(!a
->ai_canonname
&& !b
->ai_canonname
, "Expected both names absent (%p != %p)\n",
406 a
->ai_canonname
, b
->ai_canonname
);
408 ok(!a
&& !b
, "Expected both addresses null (%p != %p)\n", a
, b
);
411 /**************** Server utility functions ***************/
414 * Even if we have closed our server socket cleanly,
415 * the OS may mark the address "in use" for some time -
416 * this happens with native Linux apps, too.
418 static void do_bind ( SOCKET s
, struct sockaddr
* addr
, int addrlen
)
420 int err
, wsaerr
= 0, n_try
= BIND_TRIES
;
422 while ( ( err
= bind ( s
, addr
, addrlen
) ) != 0 &&
423 ( wsaerr
= WSAGetLastError () ) == WSAEADDRINUSE
&&
426 trace ( "address in use, waiting ...\n" );
427 Sleep ( 1000 * BIND_SLEEP
);
429 ok ( err
== 0, "failed to bind: %d\n", wsaerr
);
432 static void server_start ( server_params
*par
)
435 test_params
*gen
= par
->general
;
436 server_memory
*mem
= LocalAlloc ( LPTR
, sizeof ( server_memory
) );
438 TlsSetValue ( tls
, mem
);
439 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
440 NULL
, 0, par
->sock_flags
);
441 ok ( mem
->s
!= INVALID_SOCKET
, "Server: WSASocket failed\n" );
443 mem
->addr
.sin_family
= AF_INET
;
444 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
445 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
447 for (i
= 0; i
< MAX_CLIENTS
; i
++)
449 mem
->sock
[i
].s
= INVALID_SOCKET
;
450 mem
->sock
[i
].buf
= LocalAlloc ( LPTR
, gen
->n_chunks
* gen
->chunk_size
);
451 mem
->sock
[i
].n_recvd
= 0;
452 mem
->sock
[i
].n_sent
= 0;
455 if ( gen
->sock_type
== SOCK_STREAM
)
456 do_bind ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof (mem
->addr
) );
459 static void server_stop (void)
462 server_memory
*mem
= TlsGetValue ( tls
);
464 for (i
= 0; i
< MAX_CLIENTS
; i
++ )
466 LocalFree ( mem
->sock
[i
].buf
);
467 if ( mem
->sock
[i
].s
!= INVALID_SOCKET
)
468 closesocket ( mem
->sock
[i
].s
);
470 ok ( closesocket ( mem
->s
) == 0, "closesocket failed\n" );
472 ExitThread ( GetCurrentThreadId () );
475 /**************** Client utilitiy functions ***************/
477 static void client_start ( client_params
*par
)
479 test_params
*gen
= par
->general
;
480 client_memory
*mem
= LocalAlloc (LPTR
, sizeof (client_memory
));
482 TlsSetValue ( tls
, mem
);
484 WaitForSingleObject ( server_ready
, INFINITE
);
486 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
487 NULL
, 0, par
->sock_flags
);
489 mem
->addr
.sin_family
= AF_INET
;
490 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
491 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
493 ok ( mem
->s
!= INVALID_SOCKET
, "Client: WSASocket failed\n" );
495 mem
->send_buf
= LocalAlloc ( LPTR
, 2 * gen
->n_chunks
* gen
->chunk_size
);
496 mem
->recv_buf
= mem
->send_buf
+ gen
->n_chunks
* gen
->chunk_size
;
497 fill_buffer ( mem
->send_buf
, gen
->chunk_size
, gen
->n_chunks
);
499 SetEvent ( client_ready
[client_id
] );
500 /* Wait for the other clients to come up */
501 WaitForMultipleObjects ( min ( gen
->n_clients
, MAX_CLIENTS
), client_ready
, TRUE
, INFINITE
);
504 static void client_stop (void)
506 client_memory
*mem
= TlsGetValue ( tls
);
507 wsa_ok ( closesocket ( mem
->s
), 0 ==, "closesocket error (%x): %d\n" );
508 LocalFree ( mem
->send_buf
);
513 /**************** Servers ***************/
516 * simple_server: A very basic server doing synchronous IO.
518 static VOID WINAPI
simple_server ( server_params
*par
)
520 test_params
*gen
= par
->general
;
522 int pos
, n_recvd
, n_sent
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
523 id
= GetCurrentThreadId();
525 trace ( "simple_server (%x) starting\n", id
);
527 set_so_opentype ( FALSE
); /* non-overlapped */
528 server_start ( par
);
529 mem
= TlsGetValue ( tls
);
531 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
532 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "simple_server (%x): listen failed: %d\n");
534 trace ( "simple_server (%x) ready\n", id
);
535 SetEvent ( server_ready
); /* notify clients */
537 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
539 trace ( "simple_server (%x): waiting for client\n", id
);
541 /* accept a single connection */
542 tmp
= sizeof ( mem
->sock
[0].peer
);
543 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
544 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "simple_server (%x): accept failed: %d\n" );
546 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
547 "simple_server (%x): strange peer address\n", id
);
549 /* Receive data & check it */
550 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, 0, par
->buflen
);
551 ok ( n_recvd
== n_expected
,
552 "simple_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
553 pos
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
554 ok ( pos
== -1, "simple_server (%x): test pattern error: %d\n", id
, pos
);
557 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, 0, par
->buflen
);
558 ok ( n_sent
== n_expected
,
559 "simple_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
562 read_zero_bytes ( mem
->sock
[0].s
);
563 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "simple_server (%x): closesocket error: %d\n" );
564 mem
->sock
[0].s
= INVALID_SOCKET
;
567 trace ( "simple_server (%x) exiting\n", id
);
572 * oob_server: A very basic server receiving out-of-band data.
574 static VOID WINAPI
oob_server ( server_params
*par
)
576 test_params
*gen
= par
->general
;
579 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
,
580 id
= GetCurrentThreadId();
582 trace ( "oob_server (%x) starting\n", id
);
584 set_so_opentype ( FALSE
); /* non-overlapped */
585 server_start ( par
);
586 mem
= TlsGetValue ( tls
);
588 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
589 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "oob_server (%x): listen failed: %d\n");
591 trace ( "oob_server (%x) ready\n", id
);
592 SetEvent ( server_ready
); /* notify clients */
594 trace ( "oob_server (%x): waiting for client\n", id
);
596 /* accept a single connection */
597 tmp
= sizeof ( mem
->sock
[0].peer
);
598 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
599 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "oob_server (%x): accept failed: %d\n" );
601 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
602 "oob_server (%x): strange peer address\n", id
);
604 /* check initial atmark state */
605 ioctlsocket ( mem
->sock
[0].s
, SIOCATMARK
, &atmark
);
606 ok ( atmark
== 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id
, atmark
);
608 /* Receive normal data */
609 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, 0, par
->buflen
);
610 ok ( n_recvd
== n_expected
,
611 "oob_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
612 pos
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
613 ok ( pos
== -1, "oob_server (%x): test pattern error: %d\n", id
, pos
);
615 /* check atmark state */
616 ioctlsocket ( mem
->sock
[0].s
, SIOCATMARK
, &atmark
);
617 ok ( atmark
== 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id
, atmark
);
620 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, 0, par
->buflen
);
621 ok ( n_sent
== n_expected
,
622 "oob_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
624 /* Receive a part of the out-of-band data and check atmark state */
625 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, 8, 0, par
->buflen
);
627 "oob_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, 8 );
630 ioctlsocket ( mem
->sock
[0].s
, SIOCATMARK
, &atmark
);
631 todo_wine
ok ( atmark
== 0, "oob_server (%x): not at the OOB mark: %i\n", id
, atmark
);
633 /* Receive the rest of the out-of-band data and check atmark state */
634 do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, 0, par
->buflen
);
636 ioctlsocket ( mem
->sock
[0].s
, SIOCATMARK
, &atmark
);
637 todo_wine
ok ( atmark
== 0, "oob_server (%x): not at the OOB mark: %i\n", id
, atmark
);
640 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "oob_server (%x): closesocket error: %d\n" );
641 mem
->sock
[0].s
= INVALID_SOCKET
;
643 trace ( "oob_server (%x) exiting\n", id
);
648 * select_server: A non-blocking server.
650 static VOID WINAPI
select_server ( server_params
*par
)
652 test_params
*gen
= par
->general
;
654 int n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
655 id
= GetCurrentThreadId(), n_connections
= 0, n_sent
, n_recvd
,
656 n_set
, delta
, n_ready
;
657 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
658 fd_set fds_recv
, fds_send
, fds_openrecv
, fds_opensend
;
660 trace ( "select_server (%x) starting\n", id
);
662 set_so_opentype ( FALSE
); /* non-overlapped */
663 server_start ( par
);
664 mem
= TlsGetValue ( tls
);
666 wsa_ok ( set_blocking ( mem
->s
, FALSE
), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
667 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "select_server (%x): listen failed: %d\n");
669 trace ( "select_server (%x) ready\n", id
);
670 SetEvent ( server_ready
); /* notify clients */
672 FD_ZERO ( &fds_openrecv
);
673 FD_ZERO ( &fds_recv
);
674 FD_ZERO ( &fds_send
);
675 FD_ZERO ( &fds_opensend
);
677 FD_SET ( mem
->s
, &fds_openrecv
);
681 fds_recv
= fds_openrecv
;
682 fds_send
= fds_opensend
;
686 wsa_ok ( ( n_ready
= select ( 0, &fds_recv
, &fds_send
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
687 "select_server (%x): select() failed: %d\n" );
689 /* check for incoming requests */
690 if ( FD_ISSET ( mem
->s
, &fds_recv
) ) {
693 trace ( "select_server (%x): accepting client connection\n", id
);
695 /* accept a single connection */
696 tmp
= sizeof ( mem
->sock
[n_connections
].peer
);
697 mem
->sock
[n_connections
].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[n_connections
].peer
, &tmp
);
698 wsa_ok ( mem
->sock
[n_connections
].s
, INVALID_SOCKET
!=, "select_server (%x): accept() failed: %d\n" );
700 ok ( mem
->sock
[n_connections
].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
701 "select_server (%x): strange peer address\n", id
);
703 /* add to list of open connections */
704 FD_SET ( mem
->sock
[n_connections
].s
, &fds_openrecv
);
705 FD_SET ( mem
->sock
[n_connections
].s
, &fds_opensend
);
710 /* handle open requests */
712 for ( i
= 0; i
< n_connections
; i
++ )
714 if ( FD_ISSET( mem
->sock
[i
].s
, &fds_recv
) ) {
717 if ( mem
->sock
[i
].n_recvd
< n_expected
) {
718 /* Receive data & check it */
719 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 );
720 ok ( n_recvd
!= SOCKET_ERROR
, "select_server (%x): error in recv(): %d\n", id
, WSAGetLastError() );
721 mem
->sock
[i
].n_recvd
+= n_recvd
;
723 if ( mem
->sock
[i
].n_recvd
== n_expected
) {
724 int pos
= test_buffer ( mem
->sock
[i
].buf
, gen
->chunk_size
, gen
->n_chunks
);
725 ok ( pos
== -1, "select_server (%x): test pattern error: %d\n", id
, pos
);
726 FD_CLR ( mem
->sock
[i
].s
, &fds_openrecv
);
729 ok ( mem
->sock
[i
].n_recvd
<= n_expected
, "select_server (%x): received too many bytes: %d\n", id
, mem
->sock
[i
].n_recvd
);
733 /* only echo back what we've received */
734 delta
= mem
->sock
[i
].n_recvd
- mem
->sock
[i
].n_sent
;
736 if ( FD_ISSET ( mem
->sock
[i
].s
, &fds_send
) ) {
739 if ( ( delta
> 0 ) && ( mem
->sock
[i
].n_sent
< n_expected
) ) {
741 n_sent
= send ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_sent
, min ( delta
, par
->buflen
), 0 );
742 ok ( n_sent
!= SOCKET_ERROR
, "select_server (%x): error in send(): %d\n", id
, WSAGetLastError() );
743 mem
->sock
[i
].n_sent
+= n_sent
;
745 if ( mem
->sock
[i
].n_sent
== n_expected
) {
746 FD_CLR ( mem
->sock
[i
].s
, &fds_opensend
);
749 ok ( mem
->sock
[i
].n_sent
<= n_expected
, "select_server (%x): sent too many bytes: %d\n", id
, mem
->sock
[i
].n_sent
);
754 /* check that select returned the correct number of ready sockets */
755 ok ( ( n_set
== n_ready
), "select_server (%x): select() returns wrong number of ready sockets\n", id
);
757 /* check if all clients are done */
758 if ( ( fds_opensend
.fd_count
== 0 )
759 && ( fds_openrecv
.fd_count
== 1 ) /* initial socket that accepts clients */
760 && ( n_connections
== min ( gen
->n_clients
, MAX_CLIENTS
) ) ) {
765 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
768 read_zero_bytes ( mem
->sock
[i
].s
);
769 wsa_ok ( closesocket ( mem
->sock
[i
].s
), 0 ==, "select_server (%x): closesocket error: %d\n" );
770 mem
->sock
[i
].s
= INVALID_SOCKET
;
773 trace ( "select_server (%x) exiting\n", id
);
777 /**************** Clients ***************/
780 * simple_client: A very basic client doing synchronous IO.
782 static VOID WINAPI
simple_client ( client_params
*par
)
784 test_params
*gen
= par
->general
;
786 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
788 id
= GetCurrentThreadId();
789 trace ( "simple_client (%x): starting\n", id
);
790 /* wait here because we want to call set_so_opentype before creating a socket */
791 WaitForSingleObject ( server_ready
, INFINITE
);
792 trace ( "simple_client (%x): server ready\n", id
);
794 check_so_opentype ();
795 set_so_opentype ( FALSE
); /* non-overlapped */
796 client_start ( par
);
797 mem
= TlsGetValue ( tls
);
800 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
801 0 ==, "simple_client (%x): connect error: %d\n" );
802 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
803 "simple_client (%x): failed to set blocking mode\n", id
);
804 trace ( "simple_client (%x) connected\n", id
);
806 /* send data to server */
807 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, 0, par
->buflen
);
808 ok ( n_sent
== n_expected
,
809 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
811 /* shutdown send direction */
812 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
814 /* Receive data echoed back & check it */
815 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, 0, par
->buflen
);
816 ok ( n_recvd
== n_expected
,
817 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
820 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
821 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
824 read_zero_bytes ( mem
->s
);
825 trace ( "simple_client (%x) exiting\n", id
);
830 * oob_client: A very basic client sending out-of-band data.
832 static VOID WINAPI
oob_client ( client_params
*par
)
834 test_params
*gen
= par
->general
;
836 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
838 id
= GetCurrentThreadId();
839 trace ( "oob_client (%x): starting\n", id
);
840 /* wait here because we want to call set_so_opentype before creating a socket */
841 WaitForSingleObject ( server_ready
, INFINITE
);
842 trace ( "oob_client (%x): server ready\n", id
);
844 check_so_opentype ();
845 set_so_opentype ( FALSE
); /* non-overlapped */
846 client_start ( par
);
847 mem
= TlsGetValue ( tls
);
850 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
851 0 ==, "oob_client (%x): connect error: %d\n" );
852 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
853 "oob_client (%x): failed to set blocking mode\n", id
);
854 trace ( "oob_client (%x) connected\n", id
);
856 /* send data to server */
857 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, 0, par
->buflen
);
858 ok ( n_sent
== n_expected
,
859 "oob_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
861 /* Receive data echoed back & check it */
862 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, 0, par
->buflen
);
863 ok ( n_recvd
== n_expected
,
864 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
865 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
866 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
868 /* send out-of-band data to server */
869 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, MSG_OOB
, par
->buflen
);
870 ok ( n_sent
== n_expected
,
871 "oob_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
873 /* shutdown send direction */
874 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
877 read_zero_bytes ( mem
->s
);
878 trace ( "oob_client (%x) exiting\n", id
);
883 * simple_mixed_client: mixing send and recvfrom
885 static VOID WINAPI
simple_mixed_client ( client_params
*par
)
887 test_params
*gen
= par
->general
;
889 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
890 int fromLen
= sizeof(mem
->addr
);
891 struct sockaddr test
;
893 id
= GetCurrentThreadId();
894 trace ( "simple_client (%x): starting\n", id
);
895 /* wait here because we want to call set_so_opentype before creating a socket */
896 WaitForSingleObject ( server_ready
, INFINITE
);
897 trace ( "simple_client (%x): server ready\n", id
);
899 check_so_opentype ();
900 set_so_opentype ( FALSE
); /* non-overlapped */
901 client_start ( par
);
902 mem
= TlsGetValue ( tls
);
905 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
906 0 ==, "simple_client (%x): connect error: %d\n" );
907 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
908 "simple_client (%x): failed to set blocking mode\n", id
);
909 trace ( "simple_client (%x) connected\n", id
);
911 /* send data to server */
912 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, 0, par
->buflen
);
913 ok ( n_sent
== n_expected
,
914 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
916 /* shutdown send direction */
917 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
919 /* this shouldn't change, since lpFrom, is not updated on
920 connection oriented sockets - exposed by bug 11640
922 ((struct sockaddr_in
*)&test
)->sin_addr
.s_addr
= inet_addr("0.0.0.0");
924 /* Receive data echoed back & check it */
925 n_recvd
= do_synchronous_recvfrom ( mem
->s
,
929 (struct sockaddr
*)&test
,
932 ok ( n_recvd
== n_expected
,
933 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
935 /* check that lpFrom was not updated */
938 inet_ntoa(((struct sockaddr_in
*)&test
)->sin_addr
),
939 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
942 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
943 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
946 read_zero_bytes ( mem
->s
);
947 trace ( "simple_client (%x) exiting\n", id
);
952 * event_client: An event-driven client
954 static void WINAPI
event_client ( client_params
*par
)
956 test_params
*gen
= par
->general
;
958 int id
= GetCurrentThreadId(), n_expected
= gen
->n_chunks
* gen
->chunk_size
,
961 WSANETWORKEVENTS wsa_events
;
962 char *send_last
, *recv_last
, *send_p
, *recv_p
;
963 LONG mask
= FD_READ
| FD_WRITE
| FD_CLOSE
;
965 trace ( "event_client (%x): starting\n", id
);
966 client_start ( par
);
967 trace ( "event_client (%x): server ready\n", id
);
969 mem
= TlsGetValue ( tls
);
971 /* Prepare event notification for connect, makes socket nonblocking */
972 event
= WSACreateEvent ();
973 WSAEventSelect ( mem
->s
, event
, FD_CONNECT
);
974 tmp
= connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) );
976 err
= WSAGetLastError ();
977 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): connect error: %d\n", id
, err
);
978 tmp
= WaitForSingleObject ( event
, INFINITE
);
979 ok ( tmp
== WAIT_OBJECT_0
, "event_client (%x): wait for connect event failed: %d\n", id
, tmp
);
980 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
981 ok ( err
== 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id
, err
);
982 err
= wsa_events
.iErrorCode
[ FD_CONNECT_BIT
];
983 ok ( err
== 0, "event_client (%x): connect error: %d\n", id
, err
);
987 trace ( "event_client (%x) connected\n", id
);
989 WSAEventSelect ( mem
->s
, event
, mask
);
991 recv_p
= mem
->recv_buf
;
992 recv_last
= mem
->recv_buf
+ n_expected
;
993 send_p
= mem
->send_buf
;
994 send_last
= mem
->send_buf
+ n_expected
;
998 err
= WaitForSingleObject ( event
, INFINITE
);
999 ok ( err
== WAIT_OBJECT_0
, "event_client (%x): wait failed\n", id
);
1001 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
1002 ok( err
== 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id
, err
);
1004 if ( wsa_events
.lNetworkEvents
& FD_WRITE
)
1006 err
= wsa_events
.iErrorCode
[ FD_WRITE_BIT
];
1007 ok ( err
== 0, "event_client (%x): FD_WRITE error code: %d\n", id
, err
);
1012 n
= send ( mem
->s
, send_p
, min ( send_last
- send_p
, par
->buflen
), 0 );
1015 err
= WSAGetLastError ();
1016 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): send error: %d\n", id
, err
);
1021 while ( n
>= 0 && send_p
< send_last
);
1023 if ( send_p
== send_last
)
1025 trace ( "event_client (%x): all data sent - shutdown\n", id
);
1026 shutdown ( mem
->s
, SD_SEND
);
1028 WSAEventSelect ( mem
->s
, event
, mask
);
1031 if ( wsa_events
.lNetworkEvents
& FD_READ
)
1033 err
= wsa_events
.iErrorCode
[ FD_READ_BIT
];
1034 ok ( err
== 0, "event_client (%x): FD_READ error code: %d\n", id
, err
);
1035 if ( err
!= 0 ) break;
1037 /* First read must succeed */
1038 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
1039 wsa_ok ( n
, 0 <=, "event_client (%x): recv error: %d\n" );
1043 if ( recv_p
== recv_last
)
1046 trace ( "event_client (%x): all data received\n", id
);
1047 WSAEventSelect ( mem
->s
, event
, mask
);
1050 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
1051 if ( n
< 0 && ( err
= WSAGetLastError()) != WSAEWOULDBLOCK
)
1052 ok ( 0, "event_client (%x): read error: %d\n", id
, err
);
1056 if ( wsa_events
.lNetworkEvents
& FD_CLOSE
)
1058 trace ( "event_client (%x): close event\n", id
);
1059 err
= wsa_events
.iErrorCode
[ FD_CLOSE_BIT
];
1060 ok ( err
== 0, "event_client (%x): FD_CLOSE error code: %d\n", id
, err
);
1065 n
= send_p
- mem
->send_buf
;
1066 ok ( send_p
== send_last
,
1067 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n
, n_expected
);
1068 n
= recv_p
- mem
->recv_buf
;
1069 ok ( recv_p
== recv_last
,
1070 "simple_client (%x): received less data than expected: %d of %d\n", id
, n
, n_expected
);
1071 n
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
1072 ok ( n
== -1, "event_client (%x): test pattern error: %d\n", id
, n
);
1075 WSACloseEvent ( event
);
1076 trace ( "event_client (%x) exiting\n", id
);
1080 /* Tests for WSAStartup */
1081 static void test_WithoutWSAStartup(void)
1085 WSASetLastError(0xdeadbeef);
1086 ok(WSASocketA(0, 0, 0, NULL
, 0, 0) == INVALID_SOCKET
, "WSASocketA should have failed\n");
1087 err
= WSAGetLastError();
1088 ok(err
== WSANOTINITIALISED
, "Expected 10093, received %d\n", err
);
1090 WSASetLastError(0xdeadbeef);
1091 ok(gethostbyname("localhost") == NULL
, "gethostbyname() succeeded unexpectedly\n");
1092 err
= WSAGetLastError();
1093 ok(err
== WSANOTINITIALISED
, "Expected 10093, received %d\n", err
);
1096 static void test_WithWSAStartup(void)
1099 WORD version
= MAKEWORD( 2, 2 );
1105 res
= WSAStartup( version
, &data
);
1106 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
1108 ptr
= gethostbyname("localhost");
1109 ok(ptr
!= NULL
, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
1111 ok(!tcp_socketpair(&src
, &dst
), "creating socket pair failed\n");
1113 res
= send(src
, "TEST", 4, 0);
1114 ok(res
== 4, "send failed with error %d\n", WSAGetLastError());
1118 res
= WSAStartup( version
, &data
);
1119 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
1121 /* show that sockets are destroyed automatically after WSACleanup */
1123 SetLastError(0xdeadbeef);
1124 res
= send(src
, "TEST", 4, 0);
1125 error
= WSAGetLastError();
1126 ok(res
== SOCKET_ERROR
, "send should have failed\n");
1127 ok(error
== WSAENOTSOCK
, "expected 10038, got %d\n", error
);
1129 SetLastError(0xdeadbeef);
1130 res
= closesocket(dst
);
1131 error
= WSAGetLastError();
1132 ok(res
== SOCKET_ERROR
, "closesocket should have failed\n");
1133 ok(error
== WSAENOTSOCK
, "expected 10038, got %d\n", error
);
1140 ok(res
== 0, "expected 0, got %d\n", res
);
1141 WSASetLastError(0xdeadbeef);
1143 error
= WSAGetLastError();
1144 ok ( (res
== SOCKET_ERROR
&& error
== WSANOTINITIALISED
) ||
1145 broken(res
== 0), /* WinME */
1146 "WSACleanup returned %d WSAGetLastError is %d\n", res
, error
);
1149 /**************** Main program utility functions ***************/
1151 static void Init (void)
1153 WORD ver
= MAKEWORD (2, 2);
1155 HMODULE hws2_32
= GetModuleHandleA("ws2_32.dll");
1157 pfreeaddrinfo
= (void *)GetProcAddress(hws2_32
, "freeaddrinfo");
1158 pgetaddrinfo
= (void *)GetProcAddress(hws2_32
, "getaddrinfo");
1159 pFreeAddrInfoW
= (void *)GetProcAddress(hws2_32
, "FreeAddrInfoW");
1160 pGetAddrInfoW
= (void *)GetProcAddress(hws2_32
, "GetAddrInfoW");
1161 pInetNtop
= (void *)GetProcAddress(hws2_32
, "inet_ntop");
1162 pInetPton
= (void *)GetProcAddress(hws2_32
, "inet_pton");
1163 pWSALookupServiceBeginW
= (void *)GetProcAddress(hws2_32
, "WSALookupServiceBeginW");
1164 pWSALookupServiceEnd
= (void *)GetProcAddress(hws2_32
, "WSALookupServiceEnd");
1165 pWSALookupServiceNextW
= (void *)GetProcAddress(hws2_32
, "WSALookupServiceNextW");
1167 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
1171 static void Exit (void)
1176 err
= WSAGetLastError();
1177 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
1180 static void StartServer (LPTHREAD_START_ROUTINE routine
,
1181 test_params
*general
, server_params
*par
)
1183 par
->general
= general
;
1184 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
1185 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
1188 static void StartClients (LPTHREAD_START_ROUTINE routine
,
1189 test_params
*general
, client_params
*par
)
1192 par
->general
= general
;
1193 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
1196 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
1197 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
1198 /* Make sure the client is up and running */
1199 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
1203 static void do_test( test_setup
*test
)
1205 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
1208 server_ready
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
1209 for (i
= 0; i
<= n
; i
++)
1210 client_ready
[i
] = CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
1212 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
1213 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
1214 WaitForSingleObject ( server_ready
, INFINITE
);
1216 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
1217 ok ( wait
<= WAIT_OBJECT_0
+ n
,
1218 "some threads have not completed: %x\n", wait
);
1220 if ( ! ( wait
<= WAIT_OBJECT_0
+ n
) )
1222 for (i
= 0; i
<= n
; i
++)
1224 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
1226 trace ("terminating thread %08x\n", thread_id
[i
]);
1227 TerminateThread ( thread
[i
], 0 );
1231 CloseHandle ( server_ready
);
1232 for (i
= 0; i
<= n
; i
++)
1233 CloseHandle ( client_ready
[i
] );
1236 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1237 /* optname = SO_LINGER */
1238 static const LINGER linger_testvals
[] = {
1245 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1246 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1247 bug in the linux kernel (fixed in 2.6.8) */
1248 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1250 static void test_set_getsockopt(void)
1253 int i
, err
, lasterr
;
1257 WSAPROTOCOL_INFOA infoA
;
1258 WSAPROTOCOL_INFOW infoW
;
1259 char providername
[WSAPROTOCOL_LEN
+ 1];
1262 int family
, type
, proto
;
1264 {AF_INET
, SOCK_STREAM
, IPPROTO_TCP
},
1265 {AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
},
1266 {AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
},
1267 {AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
}
1275 s
= socket(AF_INET
, SOCK_STREAM
, 0);
1276 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1277 if( s
== INVALID_SOCKET
) return;
1279 timeout
= SOCKTIMEOUT1
;
1280 size
= sizeof(timeout
);
1281 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
1283 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
1284 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1285 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
1288 size
= sizeof(timeout
);
1289 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
1291 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
1292 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1293 ok( timeout
== 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
1296 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
1297 size
= sizeof(timeout
);
1298 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
1300 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
1301 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1302 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
1304 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
1305 size
= sizeof(lingval
);
1306 lingval
= linger_testvals
[i
];
1307 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1309 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1310 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1311 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1312 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1313 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1314 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1315 lingval
.l_onoff
, lingval
.l_linger
,
1316 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1319 size
= sizeof(lingval
);
1320 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, NULL
, size
);
1321 ok(err
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
1322 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err
, WSAGetLastError());
1323 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, NULL
, 0);
1324 ok(err
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
1325 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err
, WSAGetLastError());
1327 size
= sizeof(BOOL
);
1328 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTLINGER
, NULL
, size
);
1329 ok(err
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
1330 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err
, WSAGetLastError());
1331 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTLINGER
, NULL
, 0);
1332 ok(err
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
1333 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err
, WSAGetLastError());
1335 /* Test for erroneously passing a value instead of a pointer as optval */
1336 size
= sizeof(char);
1337 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1338 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1339 "instead of failing.\n");
1340 lasterr
= WSAGetLastError();
1341 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1342 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1343 lasterr
, WSAEFAULT
);
1345 /* SO_RCVTIMEO with invalid values for level */
1346 size
= sizeof(timeout
);
1347 timeout
= SOCKTIMEOUT1
;
1348 SetLastError(0xdeadbeef);
1349 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1350 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1351 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1352 err
, WSAGetLastError());
1354 timeout
= SOCKTIMEOUT1
;
1355 SetLastError(0xdeadbeef);
1356 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1357 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1358 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1359 err
, WSAGetLastError());
1361 /* Test SO_ERROR set/get */
1362 SetLastError(0xdeadbeef);
1364 err
= setsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &i
, size
);
1366 ok( !err
&& !WSAGetLastError(),
1367 "got %d with %d (expected 0 with 0)\n",
1368 err
, WSAGetLastError());
1370 SetLastError(0xdeadbeef);
1372 err
= getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &i
, &size
);
1374 ok( !err
&& !WSAGetLastError(),
1375 "got %d with %d (expected 0 with 0)\n",
1376 err
, WSAGetLastError());
1378 ok (i
== 1234, "got %d (expected 1234)\n", i
);
1380 /* Test invalid optlen */
1381 SetLastError(0xdeadbeef);
1383 err
= getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &i
, &size
);
1385 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEFAULT
),
1386 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1387 err
, WSAGetLastError());
1390 /* Test with the closed socket */
1391 SetLastError(0xdeadbeef);
1394 err
= getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &i
, &size
);
1396 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAENOTSOCK
),
1397 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1398 err
, WSAGetLastError());
1399 ok (i
== 1234, "expected 1234, got %d\n", i
);
1401 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1402 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
1403 ok(s
!= INVALID_SOCKET
, "Failed to create socket\n");
1406 err
= setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
, (char *) &i
, size
);
1409 for (i
= 0; i
< 4; i
++)
1412 const int tests
[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1413 err
= setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
, (char *) &tests
[i
], i
+ 1);
1414 ok(!err
, "Test [%d] Expected 0, got %d\n", i
, err
);
1415 err
= getsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
, (char *) &k
, &size
);
1416 ok(!err
, "Test [%d] Expected 0, got %d\n", i
, err
);
1417 j
= i
!= 3 ? tests
[i
] & ((1 << (i
+ 1) * 8) - 1) : tests
[i
];
1418 ok(k
== j
, "Test [%d] Expected 0x%x, got 0x%x\n", i
, j
, k
);
1422 win_skip("IP_MULTICAST_TTL is unsupported\n");
1425 /* test SO_PROTOCOL_INFOA invalid parameters */
1426 ok(getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, NULL
, NULL
),
1427 "getsockopt should have failed\n");
1428 err
= WSAGetLastError();
1429 ok(err
== WSAENOTSOCK
, "expected 10038, got %d instead\n", err
);
1430 size
= sizeof(WSAPROTOCOL_INFOA
);
1431 ok(getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &infoA
, &size
),
1432 "getsockopt should have failed\n");
1433 ok(size
== sizeof(WSAPROTOCOL_INFOA
), "got size %d\n", size
);
1434 err
= WSAGetLastError();
1435 ok(err
== WSAENOTSOCK
, "expected 10038, got %d instead\n", err
);
1436 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1437 ok(getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, NULL
, NULL
),
1438 "getsockopt should have failed\n");
1439 err
= WSAGetLastError();
1440 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
1441 ok(getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &infoA
, NULL
),
1442 "getsockopt should have failed\n");
1443 err
= WSAGetLastError();
1444 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
1445 ok(getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, NULL
, &size
),
1446 "getsockopt should have failed\n");
1447 err
= WSAGetLastError();
1448 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
1449 size
= sizeof(WSAPROTOCOL_INFOA
) / 2;
1450 ok(getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &infoA
, &size
),
1451 "getsockopt should have failed\n");
1452 err
= WSAGetLastError();
1453 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
1454 ok(size
== sizeof(WSAPROTOCOL_INFOA
), "got size %d\n", size
);
1455 size
= sizeof(WSAPROTOCOL_INFOA
) * 2;
1456 err
= getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &infoA
, &size
);
1457 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
1458 ok(size
== sizeof(WSAPROTOCOL_INFOA
) * 2, "got size %d\n", size
);
1462 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1463 for (i
= 0; i
< sizeof(prottest
) / sizeof(prottest
[0]); i
++)
1465 s
= socket(prottest
[i
].family
, prottest
[i
].type
, prottest
[i
].proto
);
1466 if (s
== INVALID_SOCKET
&& prottest
[i
].family
== AF_INET6
) continue;
1468 ok(s
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1471 /* compare both A and W version */
1472 infoA
.szProtocol
[0] = 0;
1473 size
= sizeof(WSAPROTOCOL_INFOA
);
1474 err
= getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &infoA
, &size
);
1475 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
1476 ok(size
== sizeof(WSAPROTOCOL_INFOA
), "got size %d\n", size
);
1478 infoW
.szProtocol
[0] = 0;
1479 size
= sizeof(WSAPROTOCOL_INFOW
);
1480 err
= getsockopt(s
, SOL_SOCKET
, SO_PROTOCOL_INFOW
, (char *) &infoW
, &size
);
1481 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
1482 ok(size
== sizeof(WSAPROTOCOL_INFOW
), "got size %d\n", size
);
1484 trace("provider name '%s', family %d, type %d, proto %d\n",
1485 infoA
.szProtocol
, prottest
[i
].family
, prottest
[i
].type
, prottest
[i
].proto
);
1487 ok(infoA
.szProtocol
[0] || broken(!infoA
.szProtocol
[0]) /* NT4 */,
1488 "WSAPROTOCOL_INFOA was not filled\n");
1489 ok(infoW
.szProtocol
[0] || broken(!infoA
.szProtocol
[0]) /* NT4 */,
1490 "WSAPROTOCOL_INFOW was not filled\n");
1492 WideCharToMultiByte(CP_ACP
, 0, infoW
.szProtocol
, -1,
1493 providername
, sizeof(providername
), NULL
, NULL
);
1494 ok(!strcmp(infoA
.szProtocol
,providername
),
1495 "different provider names '%s' != '%s'\n", infoA
.szProtocol
, providername
);
1497 ok(!memcmp(&infoA
, &infoW
, FIELD_OFFSET(WSAPROTOCOL_INFOA
, szProtocol
)),
1498 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1500 /* Remove IF when WSAEnumProtocols support IPV6 data */
1501 if (prottest
[i
].family
== AF_INET6
)
1504 ok(infoA
.iAddressFamily
== prottest
[i
].family
, "socket family invalid, expected %d received %d\n",
1505 prottest
[i
].family
, infoA
.iAddressFamily
);
1509 ok(infoA
.iAddressFamily
== prottest
[i
].family
, "socket family invalid, expected %d received %d\n",
1510 prottest
[i
].family
, infoA
.iAddressFamily
);
1511 } ok(infoA
.iSocketType
== prottest
[i
].type
, "socket type invalid, expected %d received %d\n",
1512 prottest
[i
].type
, infoA
.iSocketType
);
1513 ok(infoA
.iProtocol
== prottest
[i
].proto
, "socket protocol invalid, expected %d received %d\n",
1514 prottest
[i
].proto
, infoA
.iProtocol
);
1519 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1520 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1521 ok(s
!= INVALID_SOCKET
, "Failed to create socket\n");
1522 s2
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1523 ok(s2
!= INVALID_SOCKET
, "Failed to create socket\n");
1525 SetLastError(0xdeadbeef);
1526 size
= sizeof(csinfoA
);
1527 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1530 struct sockaddr_in saddr
;
1531 memset(&saddr
, 0, sizeof(saddr
));
1532 saddr
.sin_family
= AF_INET
;
1533 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1535 /* Socket is not bound, no information provided */
1536 ok(!csinfoA
.cs
.LocalAddr
.iSockaddrLength
, "Expected 0, got %d\n", csinfoA
.cs
.LocalAddr
.iSockaddrLength
);
1537 ok(csinfoA
.cs
.LocalAddr
.lpSockaddr
== NULL
, "Expected NULL, got %p\n", csinfoA
.cs
.LocalAddr
.lpSockaddr
);
1538 /* Socket is not connected, no information provided */
1539 ok(!csinfoA
.cs
.RemoteAddr
.iSockaddrLength
, "Expected 0, got %d\n", csinfoA
.cs
.LocalAddr
.iSockaddrLength
);
1540 ok(csinfoA
.cs
.RemoteAddr
.lpSockaddr
== NULL
, "Expected NULL, got %p\n", csinfoA
.cs
.LocalAddr
.lpSockaddr
);
1542 err
= bind(s
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1543 ok(!err
, "Expected 0, got %d\n", err
);
1544 size
= sizeof(csinfoA
);
1545 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1546 ok(!err
, "Expected 0, got %d\n", err
);
1548 /* Socket is bound */
1549 ok(csinfoA
.cs
.LocalAddr
.iSockaddrLength
, "Expected non-zero\n");
1550 ok(csinfoA
.cs
.LocalAddr
.lpSockaddr
!= NULL
, "Expected non-null\n");
1551 /* Socket is not connected, no information provided */
1552 ok(!csinfoA
.cs
.RemoteAddr
.iSockaddrLength
, "Expected 0, got %d\n", csinfoA
.cs
.LocalAddr
.iSockaddrLength
);
1553 ok(csinfoA
.cs
.RemoteAddr
.lpSockaddr
== NULL
, "Expected NULL, got %p\n", csinfoA
.cs
.LocalAddr
.lpSockaddr
);
1555 err
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1556 ok(!err
, "Expected 0, got %d\n", err
);
1557 err
= getsockname(s2
, (struct sockaddr
*)&saddr
, &size
);
1558 ok(!err
, "Expected 0, got %d\n", err
);
1559 err
= listen(s2
, 1);
1560 ok(!err
, "Expected 0, got %d\n", err
);
1561 err
= connect(s
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1562 ok(!err
, "Expected 0, got %d\n", err
);
1563 size
= sizeof(saddr
);
1564 err
= accept(s2
, (struct sockaddr
*)&saddr
, &size
);
1565 ok(err
!= INVALID_SOCKET
, "Failed to accept socket\n");
1569 size
= sizeof(csinfoA
);
1570 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1571 ok(!err
, "Expected 0, got %d\n", err
);
1572 err
= getsockopt(s2
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoB
, &size
);
1573 ok(!err
, "Expected 0, got %d\n", err
);
1574 ok(size
== sizeof(csinfoA
), "Got %d\n", size
);
1575 size
= sizeof(saddr
);
1576 ok(size
== csinfoA
.cs
.LocalAddr
.iSockaddrLength
, "Expected %d, got %d\n", size
,
1577 csinfoA
.cs
.LocalAddr
.iSockaddrLength
);
1578 ok(size
== csinfoA
.cs
.RemoteAddr
.iSockaddrLength
, "Expected %d, got %d\n", size
,
1579 csinfoA
.cs
.RemoteAddr
.iSockaddrLength
);
1580 ok(!memcmp(csinfoA
.cs
.LocalAddr
.lpSockaddr
, csinfoB
.cs
.RemoteAddr
.lpSockaddr
, size
),
1581 "Expected matching addresses\n");
1582 ok(!memcmp(csinfoB
.cs
.LocalAddr
.lpSockaddr
, csinfoA
.cs
.RemoteAddr
.lpSockaddr
, size
),
1583 "Expected matching addresses\n");
1584 ok(csinfoA
.cs
.iSocketType
== SOCK_STREAM
, "Wrong socket type\n");
1585 ok(csinfoB
.cs
.iSocketType
== SOCK_STREAM
, "Wrong socket type\n");
1586 ok(csinfoA
.cs
.iProtocol
== IPPROTO_TCP
, "Wrong socket protocol\n");
1587 ok(csinfoB
.cs
.iProtocol
== IPPROTO_TCP
, "Wrong socket protocol\n");
1589 err
= getpeername(s
, (struct sockaddr
*)&saddr
, &size
);
1590 ok(!err
, "Expected 0, got %d\n", err
);
1591 ok(!memcmp(&saddr
, csinfoA
.cs
.RemoteAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1592 ok(!memcmp(&saddr
, csinfoB
.cs
.LocalAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1593 err
= getpeername(s2
, (struct sockaddr
*)&saddr
, &size
);
1594 ok(!err
, "Expected 0, got %d\n", err
);
1595 ok(!memcmp(&saddr
, csinfoB
.cs
.RemoteAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1596 ok(!memcmp(&saddr
, csinfoA
.cs
.LocalAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1597 err
= getsockname(s
, (struct sockaddr
*)&saddr
, &size
);
1598 ok(!err
, "Expected 0, got %d\n", err
);
1599 ok(!memcmp(&saddr
, csinfoA
.cs
.LocalAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1600 ok(!memcmp(&saddr
, csinfoB
.cs
.RemoteAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1601 err
= getsockname(s2
, (struct sockaddr
*)&saddr
, &size
);
1602 ok(!err
, "Expected 0, got %d\n", err
);
1603 ok(!memcmp(&saddr
, csinfoB
.cs
.LocalAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1604 ok(!memcmp(&saddr
, csinfoA
.cs
.RemoteAddr
.lpSockaddr
, size
), "Expected matching addresses\n");
1606 SetLastError(0xdeadbeef);
1607 size
= sizeof(CSADDR_INFO
);
1608 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1609 ok(err
, "Expected non-zero\n");
1610 ok(size
== sizeof(CSADDR_INFO
), "Got %d\n", size
);
1611 ok(GetLastError() == WSAEFAULT
, "Expected 10014, got %d\n", GetLastError());
1613 /* At least for IPv4 the size is exactly 56 bytes */
1614 size
= sizeof(*csinfoA
.cs
.LocalAddr
.lpSockaddr
) * 2 + sizeof(csinfoA
.cs
);
1615 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1616 ok(!err
, "Expected 0, got %d\n", err
);
1618 SetLastError(0xdeadbeef);
1619 err
= getsockopt(s
, SOL_SOCKET
, SO_BSP_STATE
, (char *) &csinfoA
, &size
);
1620 ok(err
, "Expected non-zero\n");
1621 ok(GetLastError() == WSAEFAULT
, "Expected 10014, got %d\n", GetLastError());
1624 ok(GetLastError() == WSAENOPROTOOPT
, "Expected 10042, got %d\n", GetLastError());
1630 static void test_so_reuseaddr(void)
1632 struct sockaddr_in saddr
;
1634 unsigned int rc
,reuse
;
1638 saddr
.sin_family
= AF_INET
;
1639 saddr
.sin_port
= htons(9375);
1640 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1642 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1643 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1644 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1645 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1647 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1648 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1652 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1653 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1655 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1656 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1659 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1660 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1662 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1663 * a port immediately after closing another socket on that port, so
1664 * basically following the BSD socket semantics here. */
1665 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1668 int s3
=socket(AF_INET
, SOCK_STREAM
, 0), s4
;
1669 trace("<= Win XP behavior of SO_REUSEADDR\n");
1671 /* If we could bind again in the same port this is Windows version <= XP.
1672 * Lets test if we can really connect to one of them. */
1673 set_blocking(s1
, FALSE
);
1674 set_blocking(s2
, FALSE
);
1676 ok(!rc
, "listen() failed with error: %d\n", WSAGetLastError());
1678 ok(!rc
, "listen() failed with error: %d\n", WSAGetLastError());
1679 rc
= connect(s3
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1680 ok(!rc
, "connecting to accepting socket failed %d\n", WSAGetLastError());
1682 /* the delivery of the connection is random so we need to try on both sockets */
1683 size
= sizeof(saddr
);
1684 s4
= accept(s1
, (struct sockaddr
*)&saddr
, &size
);
1685 if(s4
== INVALID_SOCKET
)
1686 s4
= accept(s2
, (struct sockaddr
*)&saddr
, &size
);
1687 ok(s4
!= INVALID_SOCKET
, "none of the listening sockets could get the connection\n");
1695 trace(">= Win 2003 behavior of SO_REUSEADDR\n");
1696 err
= WSAGetLastError();
1697 ok(err
==WSAEACCES
, "expected 10013, got %d\n", err
);
1700 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1701 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1707 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1709 static void test_ip_pktinfo(void)
1711 ULONG addresses
[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY
)};
1712 char recvbuf
[10], pktbuf
[512], msg
[] = "HELLO";
1713 struct sockaddr_in s1addr
, s2addr
, s3addr
;
1714 GUID WSARecvMsg_GUID
= WSAID_WSARECVMSG
;
1715 LPFN_WSARECVMSG pWSARecvMsg
= NULL
;
1716 unsigned int rc
, yes
= 1;
1718 DWORD dwBytes
, dwSize
, dwFlags
;
1727 memset(&ov
, 0, sizeof(ov
));
1728 ov
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1729 if (ov
.hEvent
== INVALID_HANDLE_VALUE
)
1731 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError());
1735 memset(&hdr
, 0x00, sizeof(hdr
));
1736 s1addr
.sin_family
= AF_INET
;
1737 s1addr
.sin_port
= htons(0);
1738 /* Note: s1addr.sin_addr is set below */
1739 iovec
[0].buf
= recvbuf
;
1740 iovec
[0].len
= sizeof(recvbuf
);
1741 hdr
.name
= (struct sockaddr
*)&s3addr
;
1742 hdr
.namelen
= sizeof(s3addr
);
1743 hdr
.lpBuffers
= &iovec
[0];
1744 hdr
.dwBufferCount
= 1;
1745 hdr
.Control
.buf
= pktbuf
;
1746 /* Note: hdr.Control.len is set below */
1749 for (i
=0;i
<sizeof(addresses
)/sizeof(UINT32
);i
++)
1751 s1addr
.sin_addr
.s_addr
= addresses
[i
];
1753 /* Build "server" side socket */
1754 s1
=socket(AF_INET
, SOCK_DGRAM
, 0);
1755 if (s1
== INVALID_SOCKET
)
1757 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1761 /* Obtain the WSARecvMsg function */
1762 WSAIoctl(s1
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &WSARecvMsg_GUID
, sizeof(WSARecvMsg_GUID
),
1763 &pWSARecvMsg
, sizeof(pWSARecvMsg
), &dwBytes
, NULL
, NULL
);
1766 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n");
1771 /* Setup the server side socket */
1772 rc
=bind(s1
, (struct sockaddr
*)&s1addr
, sizeof(s1addr
));
1773 ok(rc
!= SOCKET_ERROR
, "bind() failed error: %d\n", WSAGetLastError());
1774 rc
=setsockopt(s1
, IPPROTO_IP
, IP_PKTINFO
, (const char*)&yes
, sizeof(yes
));
1775 ok(rc
== 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1777 /* Build "client" side socket */
1778 addrlen
= sizeof(s2addr
);
1779 if (getsockname(s1
, (struct sockaddr
*) &s2addr
, &addrlen
) != 0)
1781 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError());
1785 s2addr
.sin_addr
.s_addr
= addresses
[0]; /* Always target the local adapter address */
1786 s2
=socket(AF_INET
, SOCK_DGRAM
, 0);
1787 if (s2
== INVALID_SOCKET
)
1789 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError());
1794 /* Test an empty message header */
1795 rc
=pWSARecvMsg(s1
, NULL
, NULL
, NULL
, NULL
);
1796 err
=WSAGetLastError();
1797 ok(rc
== SOCKET_ERROR
&& err
== WSAEFAULT
, "WSARecvMsg() failed error: %d (ret = %d)\n", err
, rc
);
1800 * Send a packet from the client to the server and test for specifying
1801 * a short control header.
1803 SetLastError(0xdeadbeef);
1804 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1805 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1806 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
1807 hdr
.Control
.len
= 1;
1808 rc
=pWSARecvMsg(s1
, &hdr
, &dwSize
, NULL
, NULL
);
1809 err
=WSAGetLastError();
1810 ok(rc
== SOCKET_ERROR
&& err
== WSAEMSGSIZE
&& (hdr
.dwFlags
& MSG_CTRUNC
),
1811 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err
, rc
, hdr
.dwFlags
);
1812 hdr
.dwFlags
= 0; /* Reset flags */
1814 /* Perform another short control header test, this time with an overlapped receive */
1815 hdr
.Control
.len
= 1;
1816 rc
=pWSARecvMsg(s1
, &hdr
, NULL
, &ov
, NULL
);
1817 err
=WSAGetLastError();
1818 ok(rc
!= 0 && err
== WSA_IO_PENDING
, "WSARecvMsg() failed error: %d\n", err
);
1819 SetLastError(0xdeadbeef);
1820 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1821 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1822 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
1823 if (WaitForSingleObject(ov
.hEvent
, 100) != WAIT_OBJECT_0
)
1825 skip("Server side did not receive packet, some tests skipped.\n");
1831 WSAGetOverlappedResult(s1
, &ov
, NULL
, FALSE
, &dwFlags
);
1833 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags
);
1834 ok(hdr
.dwFlags
== MSG_CTRUNC
,
1835 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr
.dwFlags
);
1836 hdr
.dwFlags
= 0; /* Reset flags */
1839 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1840 * on the server end and check that the returned packet matches what was sent.
1842 hdr
.Control
.len
= sizeof(pktbuf
);
1843 rc
=pWSARecvMsg(s1
, &hdr
, NULL
, &ov
, NULL
);
1844 err
=WSAGetLastError();
1845 ok(rc
!= 0 && err
== WSA_IO_PENDING
, "WSARecvMsg() failed error: %d\n", err
);
1846 ok(hdr
.Control
.len
== sizeof(pktbuf
),
1847 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr
.Control
.len
);
1848 rc
=sendto(s2
, msg
, sizeof(msg
), 0, (struct sockaddr
*)&s2addr
, sizeof(s2addr
));
1849 ok(rc
== sizeof(msg
), "sendto() failed error: %d\n", WSAGetLastError());
1850 if (WaitForSingleObject(ov
.hEvent
, 100) != WAIT_OBJECT_0
)
1852 skip("Server side did not receive packet, some tests skipped.\n");
1858 WSAGetOverlappedResult(s1
, &ov
, &dwSize
, FALSE
, NULL
);
1859 ok(dwSize
== sizeof(msg
),
1860 "WSARecvMsg() buffer length does not match transmitted data!\n");
1861 ok(strncmp(iovec
[0].buf
, msg
, sizeof(msg
)) == 0,
1862 "WSARecvMsg() buffer does not match transmitted data!\n");
1863 ok(hdr
.Control
.len
== IP_PKTINFO_LEN
,
1864 "WSARecvMsg() control length mismatch (%d).\n", hdr
.Control
.len
);
1866 /* Test for the expected IP_PKTINFO return information. */
1868 for (cmsg
= WSA_CMSG_FIRSTHDR(&hdr
); cmsg
!= NULL
; cmsg
= WSA_CMSG_NXTHDR(&hdr
, cmsg
))
1870 if (cmsg
->cmsg_level
== IPPROTO_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
1872 struct in_pktinfo
*pi
= (struct in_pktinfo
*)WSA_CMSG_DATA(cmsg
);
1874 ok(pi
->ipi_addr
.s_addr
== s2addr
.sin_addr
.s_addr
, "destination ip mismatch!\n");
1878 ok(foundhdr
, "IP_PKTINFO header information was not returned!\n");
1885 CloseHandle(ov
.hEvent
);
1888 /************* Array containing the tests to run **********/
1890 #define STD_STREAM_SOCKET \
1896 static test_setup tests
[] =
1898 /* Test 0: synchronous client and server */
1919 /* Test 1: event-driven client, synchronous server */
1936 WSA_FLAG_OVERLAPPED
,
1940 /* Test 2: synchronous client, non-blocking server via select() */
1961 /* Test 3: OOB client, OOB server */
1982 /* Test 4: synchronous mixed client and server */
1996 simple_mixed_client
,
2005 static void test_UDP(void)
2007 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2008 possible that this test fails due to dropped packets. */
2010 /* peer 0 receives data from all other peers */
2011 struct sock_info peer
[NUM_UDP_PEERS
];
2013 int ss
, i
, n_recv
, n_sent
;
2015 memset (buf
,0,sizeof(buf
));
2016 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
2017 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
2019 peer
[i
].addr
.sin_family
= AF_INET
;
2020 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
2023 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
2025 peer
[i
].addr
.sin_port
= htons ( 0 );
2028 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
2030 /* test getsockname() to get peer's port */
2031 ss
= sizeof ( peer
[i
].addr
);
2032 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
2033 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
2036 /* test getsockname() */
2037 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
2039 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
2040 /* send client's ip */
2041 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
2042 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
2043 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
2046 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
2047 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
2048 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
2049 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
2053 static DWORD WINAPI
do_getservbyname( void *param
)
2059 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
2061 HANDLE
*starttest
= param
;
2063 struct servent
*pserv
[2];
2065 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
,
2066 "test_getservbyname: timeout waiting for start signal\n" );
2068 /* ensure that necessary buffer resizes are completed */
2069 for ( j
= 0; j
< 2; j
++) {
2070 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
2073 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
2074 for ( j
= 0; j
< 2; j
++ ) {
2075 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
2076 ok ( pserv
[j
] != NULL
|| broken(pserv
[j
] == NULL
) /* win8, fixed in win81 */,
2077 "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
2078 if ( !pserv
[j
] ) continue;
2079 ok ( pserv
[j
]->s_port
== htons(serv
[j
].port
),
2080 "getservbyname returned the wrong port for %s: %d\n", serv
[j
].name
, ntohs(pserv
[j
]->s_port
) );
2081 ok ( !strcmp ( pserv
[j
]->s_proto
, serv
[j
].proto
),
2082 "getservbyname returned the wrong protocol for %s: %s\n", serv
[j
].name
, pserv
[j
]->s_proto
);
2083 ok ( !strcmp ( pserv
[j
]->s_name
, serv
[j
].name
),
2084 "getservbyname returned the wrong name for %s: %s\n", serv
[j
].name
, pserv
[j
]->s_name
);
2087 ok ( pserv
[0] == pserv
[1] || broken(pserv
[0] != pserv
[1]) /* win8, fixed in win81 */,
2088 "getservbyname: winsock resized servent buffer when not necessary\n" );
2094 static void test_getservbyname(void)
2097 HANDLE starttest
, thread
[NUM_THREADS
];
2098 DWORD thread_id
[NUM_THREADS
];
2100 starttest
= CreateEventA ( NULL
, 1, 0, "test_getservbyname_starttest" );
2102 /* create threads */
2103 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
2104 thread
[i
] = CreateThread ( NULL
, 0, do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
2107 /* signal threads to start */
2108 SetEvent ( starttest
);
2110 for ( i
= 0; i
< NUM_THREADS
; i
++) {
2111 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
2115 static void test_WSASocket(void)
2117 SOCKET sock
= INVALID_SOCKET
;
2118 WSAPROTOCOL_INFOA
*pi
;
2119 int wsaproviders
[] = {IPPROTO_TCP
, IPPROTO_IP
};
2120 int autoprotocols
[] = {IPPROTO_TCP
, IPPROTO_UDP
};
2121 int items
, err
, size
, socktype
, i
, j
;
2124 SetLastError(0xdeadbeef);
2125 ok(WSASocketA(0, 0, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2126 "WSASocketA should have failed\n");
2127 err
= WSAGetLastError();
2128 ok(err
== WSAEINVAL
, "Expected 10022, received %d\n", err
);
2130 sock
= WSASocketA(AF_INET
, 0, 0, NULL
, 0, 0);
2131 ok(sock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2134 sock
= WSASocketA(AF_INET
, SOCK_STREAM
, 0, NULL
, 0, 0);
2135 ok(sock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2138 SetLastError(0xdeadbeef);
2139 ok(WSASocketA(0, SOCK_STREAM
, -1, NULL
, 0, 0) == INVALID_SOCKET
,
2140 "WSASocketA should have failed\n");
2141 err
= WSAGetLastError();
2142 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2144 SetLastError(0xdeadbeef);
2145 ok(WSASocketA(0, -1, IPPROTO_UDP
, NULL
, 0, 0) == INVALID_SOCKET
,
2146 "WSASocketA should have failed\n");
2147 err
= WSAGetLastError();
2148 ok(err
== WSAESOCKTNOSUPPORT
, "Expected 10044, received %d\n", err
);
2150 SetLastError(0xdeadbeef);
2151 ok(WSASocketA(0, -1, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2152 "WSASocketA should have failed\n");
2153 err
= WSAGetLastError();
2154 ok(err
== WSAEINVAL
, "Expected 10022, received %d\n", err
);
2156 SetLastError(0xdeadbeef);
2157 ok(WSASocketA(AF_INET
, -1, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2158 "WSASocketA should have failed\n");
2159 err
= WSAGetLastError();
2160 ok(err
== WSAESOCKTNOSUPPORT
, "Expected 10044, received %d\n", err
);
2162 SetLastError(0xdeadbeef);
2163 ok(WSASocketA(AF_INET
, 0, -1, NULL
, 0, 0) == INVALID_SOCKET
,
2164 "WSASocketA should have failed\n");
2165 err
= WSAGetLastError();
2166 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2168 SetLastError(0xdeadbeef);
2169 ok(WSASocketA(0, -1, -1, NULL
, 0, 0) == INVALID_SOCKET
,
2170 "WSASocketA should have failed\n");
2171 err
= WSAGetLastError();
2172 ok(err
== WSAESOCKTNOSUPPORT
, "Expected 10044, received %d\n", err
);
2174 SetLastError(0xdeadbeef);
2175 ok(WSASocketA(-1, SOCK_STREAM
, IPPROTO_UDP
, NULL
, 0, 0) == INVALID_SOCKET
,
2176 "WSASocketA should have failed\n");
2177 err
= WSAGetLastError();
2178 ok(err
== WSAEAFNOSUPPORT
, "Expected 10047, received %d\n", err
);
2180 sock
= WSASocketA(AF_INET
, 0, IPPROTO_TCP
, NULL
, 0, 0);
2181 ok(sock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2184 SetLastError(0xdeadbeef);
2185 ok(WSASocketA(0, SOCK_STREAM
, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2186 "WSASocketA should have failed\n");
2187 err
= WSAGetLastError();
2188 ok(err
== WSAEINVAL
, "Expected 10022, received %d\n", err
);
2190 SetLastError(0xdeadbeef);
2191 ok(WSASocketA(0, 0, 0xdead, NULL
, 0, 0) == INVALID_SOCKET
,
2192 "WSASocketA should have failed\n");
2193 err
= WSAGetLastError();
2194 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2196 SetLastError(0xdeadbeef);
2197 ok(WSASocketA(AF_INET
, 0xdead, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2198 "WSASocketA should have failed\n");
2199 err
= WSAGetLastError();
2200 ok(err
== WSAESOCKTNOSUPPORT
, "Expected 10044, received %d\n", err
);
2202 SetLastError(0xdeadbeef);
2203 ok(WSASocketA(0, 0xdead, 0, NULL
, 0, 0) == INVALID_SOCKET
,
2204 "WSASocketA should have failed\n");
2205 err
= WSAGetLastError();
2206 ok(err
== WSAEINVAL
, "Expected 10022, received %d\n", err
);
2208 sock
= WSASocketA(0, 0, IPPROTO_TCP
, NULL
, 0, 0);
2209 ok(sock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2212 /* SOCK_STREAM does not support IPPROTO_UDP */
2213 SetLastError(0xdeadbeef);
2214 ok(WSASocketA(AF_INET
, SOCK_STREAM
, IPPROTO_UDP
, NULL
, 0, 0) == INVALID_SOCKET
,
2215 "WSASocketA should have failed\n");
2216 err
= WSAGetLastError();
2217 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2219 /* SOCK_DGRAM does not support IPPROTO_TCP */
2220 SetLastError(0xdeadbeef);
2221 ok(WSASocketA(AF_INET
, SOCK_DGRAM
, IPPROTO_TCP
, NULL
, 0, 0) == INVALID_SOCKET
,
2222 "WSASocketA should have failed\n");
2223 err
= WSAGetLastError();
2224 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2226 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2227 * to avoid a crash on win98.
2230 items
= WSAEnumProtocolsA(wsaproviders
, NULL
, &pi_size
);
2231 ok(items
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2233 err
= WSAGetLastError();
2234 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2237 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
2238 ok(pi
!= NULL
, "Failed to allocate memory\n");
2240 skip("Can't continue without memory.\n");
2244 items
= WSAEnumProtocolsA(wsaproviders
, pi
, &pi_size
);
2245 ok(items
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
2249 skip("No protocols enumerated.\n");
2250 HeapFree(GetProcessHeap(), 0, pi
);
2254 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
2255 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
2256 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2260 /* find what parameters are used first: plain parameters or protocol info struct */
2261 pi
[0].iProtocol
= -1;
2262 pi
[0].iSocketType
= -1;
2263 pi
[0].iAddressFamily
= -1;
2264 ok(WSASocketA(0, 0, IPPROTO_UDP
, &pi
[0], 0, 0) == INVALID_SOCKET
,
2265 "WSASocketA should have failed\n");
2266 err
= WSAGetLastError();
2267 ok(err
== WSAEAFNOSUPPORT
, "Expected 10047, received %d\n", err
);
2269 pi
[0].iProtocol
= 0;
2270 pi
[0].iSocketType
= 0;
2271 pi
[0].iAddressFamily
= 0;
2272 sock
= WSASocketA(0, 0, IPPROTO_UDP
, &pi
[0], 0, 0);
2273 if(sock
!= INVALID_SOCKET
)
2275 win_skip("must work only in OS <= 2003\n");
2280 err
= WSAGetLastError();
2281 ok(err
== WSAEAFNOSUPPORT
, "Expected 10047, received %d\n", err
);
2284 pi
[0].iProtocol
= IPPROTO_UDP
;
2285 pi
[0].iSocketType
= SOCK_DGRAM
;
2286 pi
[0].iAddressFamily
= AF_INET
;
2287 sock
= WSASocketA(0, 0, 0, &pi
[0], 0, 0);
2288 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2290 size
= sizeof(socktype
);
2292 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2293 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
2294 ok(socktype
== SOCK_DGRAM
, "Wrong socket type, expected %d received %d\n",
2295 SOCK_DGRAM
, socktype
);
2298 sock
= WSASocketA(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &pi
[0], 0, 0);
2299 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2301 size
= sizeof(socktype
);
2303 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2304 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
2305 ok(socktype
== SOCK_STREAM
, "Wrong socket type, expected %d received %d\n",
2306 SOCK_STREAM
, socktype
);
2309 HeapFree(GetProcessHeap(), 0, pi
);
2312 items
= WSAEnumProtocolsA(NULL
, NULL
, &pi_size
);
2313 ok(items
== SOCKET_ERROR
, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2315 err
= WSAGetLastError();
2316 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2319 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
2320 ok(pi
!= NULL
, "Failed to allocate memory\n");
2322 skip("Can't continue without memory.\n");
2326 items
= WSAEnumProtocolsA(NULL
, pi
, &pi_size
);
2327 ok(items
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
2330 /* when no protocol and socket type are specified the first entry
2331 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2333 sock
= WSASocketA(AF_INET
, 0, 0, NULL
, 0, 0);
2334 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2337 size
= sizeof(socktype
);
2339 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2340 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2341 for(i
= 0; i
< items
; i
++)
2343 if(pi
[i
].dwProviderFlags
& PFL_MATCHES_PROTOCOL_ZERO
)
2345 ok(socktype
== pi
[i
].iSocketType
, "Wrong socket type, expected %d received %d\n",
2346 pi
[i
].iSocketType
, socktype
);
2350 ok(i
!= items
, "Creating a socket without protocol and socket type didn't work\n");
2353 /* when no socket type is specified the first entry from WSAEnumProtocols
2354 * that matches the protocol is returned */
2355 for (i
= 0; i
< sizeof(autoprotocols
) / sizeof(autoprotocols
[0]); i
++)
2357 sock
= WSASocketA(0, 0, autoprotocols
[i
], NULL
, 0, 0);
2358 ok(sock
!= INVALID_SOCKET
, "Failed to create socket for protocol %d, received %d\n",
2359 autoprotocols
[i
], WSAGetLastError());
2361 size
= sizeof(socktype
);
2363 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2364 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2366 for (err
= 1, j
= 0; j
< items
; j
++)
2368 if (pi
[j
].iProtocol
== autoprotocols
[i
])
2370 if (socktype
== pi
[j
].iSocketType
)
2373 ok(0, "Wrong socket type, expected %d received %d\n",
2374 pi
[j
].iSocketType
, socktype
);
2378 ok(!err
, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols
[i
]);
2383 HeapFree(GetProcessHeap(), 0, pi
);
2385 SetLastError(0xdeadbeef);
2386 /* starting on vista the socket function returns error during the socket
2387 creation and no longer in the socket operations (sendto, readfrom) */
2388 sock
= WSASocketA(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
, NULL
, 0, 0);
2389 if (sock
== INVALID_SOCKET
)
2391 err
= WSAGetLastError();
2392 ok(err
== WSAEACCES
, "Expected 10013, received %d\n", err
);
2393 skip("SOCK_RAW is not supported\n");
2397 trace("SOCK_RAW is supported\n");
2399 size
= sizeof(socktype
);
2401 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2402 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2403 ok(socktype
== SOCK_RAW
, "Wrong socket type, expected %d received %d\n",
2404 SOCK_RAW
, socktype
);
2408 sock
= WSASocketA(0, 0, IPPROTO_RAW
, NULL
, 0, 0);
2409 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2411 size
= sizeof(socktype
);
2413 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2414 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2415 ok(socktype
== SOCK_RAW
, "Wrong socket type, expected %d received %d\n",
2416 SOCK_RAW
, socktype
);
2420 sock
= WSASocketA(AF_INET
, SOCK_RAW
, IPPROTO_TCP
, NULL
, 0, 0);
2421 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2423 size
= sizeof(socktype
);
2425 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2426 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2427 ok(socktype
== SOCK_RAW
, "Wrong socket type, expected %d received %d\n",
2428 SOCK_RAW
, socktype
);
2432 /* IPX socket tests */
2434 SetLastError(0xdeadbeef);
2435 sock
= WSASocketA(AF_IPX
, SOCK_DGRAM
, NSPROTO_IPX
, NULL
, 0, 0);
2436 if (sock
== INVALID_SOCKET
)
2438 err
= WSAGetLastError();
2439 ok(err
== WSAEAFNOSUPPORT
|| broken(err
== WSAEPROTONOSUPPORT
), "Expected 10047, received %d\n", err
);
2440 skip("IPX is not supported\n");
2444 WSAPROTOCOL_INFOA info
;
2447 trace("IPX is supported\n");
2449 sock
= WSASocketA(0, 0, NSPROTO_IPX
, NULL
, 0, 0);
2450 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2453 size
= sizeof(socktype
);
2455 err
= getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2456 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
2457 ok(socktype
== SOCK_DGRAM
, "Wrong socket type, expected %d received %d\n",
2458 SOCK_DGRAM
, socktype
);
2460 /* check socket family, type and protocol */
2461 size
= sizeof(WSAPROTOCOL_INFOA
);
2462 err
= getsockopt(sock
, SOL_SOCKET
, SO_PROTOCOL_INFOA
, (char *) &info
, &size
);
2463 ok(!err
,"getsockopt failed with %d\n", WSAGetLastError());
2464 ok(info
.iProtocol
== NSPROTO_IPX
, "expected protocol %d, received %d\n",
2465 NSPROTO_IPX
, info
.iProtocol
);
2466 ok(info
.iAddressFamily
== AF_IPX
, "expected family %d, received %d\n",
2467 AF_IPX
, info
.iProtocol
);
2468 ok(info
.iSocketType
== SOCK_DGRAM
, "expected type %d, received %d\n",
2469 SOCK_DGRAM
, info
.iSocketType
);
2472 /* SOCK_STREAM does not support NSPROTO_IPX */
2473 SetLastError(0xdeadbeef);
2474 ok(WSASocketA(AF_IPX
, SOCK_STREAM
, NSPROTO_IPX
, NULL
, 0, 0) == INVALID_SOCKET
,
2475 "WSASocketA should have failed\n");
2476 err
= WSAGetLastError();
2477 ok(err
== WSAEPROTONOSUPPORT
, "Expected 10043, received %d\n", err
);
2479 /* test extended IPX support - that is adding any number between 0 and 255
2480 * to the IPX protocol value will make it be used as IPX packet type */
2481 for(i
= 0;i
<= 255;i
+= 17)
2483 SetLastError(0xdeadbeef);
2484 sock
= WSASocketA(0, 0, NSPROTO_IPX
+ i
, NULL
, 0, 0);
2485 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
2490 err
= getsockopt(sock
, NSPROTO_IPX
, IPX_PTYPE
, (char *) &socktype
, &size
);
2491 ok(!err
, "getsockopt failed with %d\n", WSAGetLastError());
2492 ok(socktype
== i
, "Wrong IPX packet type, expected %d received %d\n",
2500 static void test_WSADuplicateSocket(void)
2502 SOCKET source
, dupsock
;
2503 WSAPROTOCOL_INFOA info
;
2505 struct sockaddr_in addr
;
2506 int socktype
, size
, addrsize
, ret
;
2507 char teststr
[] = "TEST", buffer
[16];
2509 source
= WSASocketA(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0, 0);
2510 ok(source
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2512 /* test invalid parameters */
2513 SetLastError(0xdeadbeef);
2514 ok(WSADuplicateSocketA(0, 0, NULL
), "WSADuplicateSocketA should have failed\n");
2515 err
= WSAGetLastError();
2516 ok(err
== WSAENOTSOCK
, "expected 10038, received %d\n", err
);
2518 SetLastError(0xdeadbeef);
2519 ok(WSADuplicateSocketA(source
, 0, NULL
),
2520 "WSADuplicateSocketA should have failed\n");
2521 err
= WSAGetLastError();
2522 ok(err
== WSAEINVAL
, "expected 10022, received %d\n", err
);
2524 SetLastError(0xdeadbeef);
2525 ok(WSADuplicateSocketA(source
, ~0, &info
),
2526 "WSADuplicateSocketA should have failed\n");
2527 err
= WSAGetLastError();
2528 ok(err
== WSAEINVAL
, "expected 10022, received %d\n", err
);
2530 SetLastError(0xdeadbeef);
2531 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info
),
2532 "WSADuplicateSocketA should have failed\n");
2533 err
= WSAGetLastError();
2534 ok(err
== WSAENOTSOCK
, "expected 10038, received %d\n", err
);
2536 SetLastError(0xdeadbeef);
2537 ok(WSADuplicateSocketA(source
, GetCurrentProcessId(), NULL
),
2538 "WSADuplicateSocketA should have failed\n");
2539 err
= WSAGetLastError();
2540 ok(err
== WSAEFAULT
, "expected 10014, received %d\n", err
);
2542 /* test returned structure */
2543 memset(&info
, 0, sizeof(info
));
2544 ok(!WSADuplicateSocketA(source
, GetCurrentProcessId(), &info
),
2545 "WSADuplicateSocketA should have worked\n");
2547 ok(info
.iProtocol
== IPPROTO_TCP
, "expected protocol %d, received %d\n",
2548 IPPROTO_TCP
, info
.iProtocol
);
2549 ok(info
.iAddressFamily
== AF_INET
, "expected family %d, received %d\n",
2550 AF_INET
, info
.iProtocol
);
2551 ok(info
.iSocketType
== SOCK_STREAM
, "expected type %d, received %d\n",
2552 SOCK_STREAM
, info
.iSocketType
);
2554 dupsock
= WSASocketA(0, 0, 0, &info
, 0, 0);
2555 ok(dupsock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2557 closesocket(dupsock
);
2558 closesocket(source
);
2560 /* create a socket, bind it, duplicate it then send data on source and
2561 * receve in the duplicated socket */
2562 source
= WSASocketA(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, NULL
, 0, 0);
2563 ok(source
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2565 memset(&info
, 0, sizeof(info
));
2566 ok(!WSADuplicateSocketA(source
, GetCurrentProcessId(), &info
),
2567 "WSADuplicateSocketA should have worked\n");
2569 ok(info
.iProtocol
== IPPROTO_UDP
, "expected protocol %d, received %d\n",
2570 IPPROTO_UDP
, info
.iProtocol
);
2571 ok(info
.iAddressFamily
== AF_INET
, "expected family %d, received %d\n",
2572 AF_INET
, info
.iProtocol
);
2573 ok(info
.iSocketType
== SOCK_DGRAM
, "expected type %d, received %d\n",
2574 SOCK_DGRAM
, info
.iSocketType
);
2576 memset(&addr
, 0, sizeof(addr
));
2577 addr
.sin_family
= AF_INET
;
2578 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2579 ok(!bind(source
, (struct sockaddr
*)&addr
, sizeof(addr
)),
2580 "bind should have worked\n");
2582 /* read address to find out the port number to be used in sendto */
2583 memset(&addr
, 0, sizeof(addr
));
2584 addrsize
= sizeof(addr
);
2585 ok(!getsockname(source
, (struct sockaddr
*) &addr
, &addrsize
),
2586 "getsockname should have worked\n");
2587 ok(addr
.sin_port
, "socket port should be != 0\n");
2589 dupsock
= WSASocketA(0, 0, 0, &info
, 0, 0);
2590 ok(dupsock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2593 ret
= getsockopt(dupsock
, SOL_SOCKET
, SO_TYPE
, (char *) &socktype
, &size
);
2594 ok(!ret
, "getsockopt failed with %d\n", WSAGetLastError());
2595 ok(socktype
== SOCK_DGRAM
, "Wrong socket type, expected %d received %d\n",
2596 SOCK_DGRAM
, socktype
);
2598 set_blocking(source
, TRUE
);
2600 /* send data on source socket */
2601 addrsize
= sizeof(addr
);
2602 size
= sendto(source
, teststr
, sizeof(teststr
), 0, (struct sockaddr
*) &addr
, addrsize
);
2603 ok(size
== sizeof(teststr
), "got %d (err %d)\n", size
, WSAGetLastError());
2605 /* receive on duplicated socket */
2606 addrsize
= sizeof(addr
);
2607 memset(buffer
, 0, sizeof(buffer
));
2608 size
= recvfrom(dupsock
, buffer
, sizeof(teststr
), 0, (struct sockaddr
*) &addr
, &addrsize
);
2609 ok(size
== sizeof(teststr
), "got %d (err %d)\n", size
, WSAGetLastError());
2610 buffer
[sizeof(teststr
) - 1] = 0;
2611 ok(!strcmp(buffer
, teststr
), "expected '%s', received '%s'\n", teststr
, buffer
);
2613 closesocket(dupsock
);
2614 closesocket(source
);
2616 /* show that the source socket need to be bound before the duplicated
2617 * socket is created */
2618 source
= WSASocketA(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, NULL
, 0, 0);
2619 ok(source
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2621 memset(&info
, 0, sizeof(info
));
2622 ok(!WSADuplicateSocketA(source
, GetCurrentProcessId(), &info
),
2623 "WSADuplicateSocketA should have worked\n");
2625 dupsock
= WSASocketA(0, 0, 0, &info
, 0, 0);
2626 ok(dupsock
!= INVALID_SOCKET
, "WSASocketA should have succeeded\n");
2628 memset(&addr
, 0, sizeof(addr
));
2629 addr
.sin_family
= AF_INET
;
2630 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2631 ok(!bind(source
, (struct sockaddr
*)&addr
, sizeof(addr
)),
2632 "bind should have worked\n");
2634 /* read address to find out the port number to be used in sendto */
2635 memset(&addr
, 0, sizeof(addr
));
2636 addrsize
= sizeof(addr
);
2637 ok(!getsockname(source
, (struct sockaddr
*) &addr
, &addrsize
),
2638 "getsockname should have worked\n");
2639 ok(addr
.sin_port
, "socket port should be != 0\n");
2641 set_blocking(source
, TRUE
);
2643 addrsize
= sizeof(addr
);
2644 size
= sendto(source
, teststr
, sizeof(teststr
), 0, (struct sockaddr
*) &addr
, addrsize
);
2645 ok(size
== sizeof(teststr
), "got %d (err %d)\n", size
, WSAGetLastError());
2647 SetLastError(0xdeadbeef);
2648 addrsize
= sizeof(addr
);
2649 memset(buffer
, 0, sizeof(buffer
));
2651 ok(recvfrom(dupsock
, buffer
, sizeof(teststr
), 0, (struct sockaddr
*) &addr
, &addrsize
) == -1,
2652 "recvfrom should have failed\n");
2653 err
= WSAGetLastError();
2654 ok(err
== WSAEINVAL
, "expected 10022, received %d\n", err
);
2657 closesocket(dupsock
);
2658 closesocket(source
);
2661 static void test_WSAEnumNetworkEvents(void)
2664 int sock_type
[] = {SOCK_STREAM
, SOCK_DGRAM
, SOCK_STREAM
}, i
, j
, k
, l
;
2665 struct sockaddr_in address
;
2667 WSANETWORKEVENTS net_events
;
2668 /* Windows 2000 Pro without SP installed (testbot) will crash if
2669 * WSAEnumNetworkEvents have a NULL event, so skip this test in <= 2000 */
2670 DWORD ver
= GetVersion() & 0xFFFF;
2671 BOOL supports_null
= ((ver
& 0xFF) << 8 | (ver
>> 8)) > 0x0500;
2673 memset(&address
, 0, sizeof(address
));
2674 address
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2675 address
.sin_family
= AF_INET
;
2677 /* This test follows the steps from bugs 10204 and 24946 */
2678 for (l
= 0; l
< 2; l
++)
2680 if (l
== 1 && !supports_null
&& broken(1)) continue;
2682 for (i
= 0; i
< sizeof(sock_type
) / sizeof(sock_type
[0]); i
++)
2685 ok(!tcp_socketpair(&s
, &s2
), "Test[%d]: creating socket pair failed\n", i
);
2688 s
= socket(AF_INET
, sock_type
[i
], 0);
2689 ok (s
!= SOCKET_ERROR
, "Test[%d]: failed to create socket\n", i
);
2690 ok (!bind(s
, (struct sockaddr
*) &address
, sizeof(address
)), "Test[%d]: bind failed\n", i
);
2692 event
= WSACreateEvent();
2693 ok (event
!= NULL
, "Test[%d]: failed to create event\n", i
);
2694 for (j
= 0; j
< 5; j
++) /* Repeat sometimes and the result must be the same */
2696 /* When the TCP socket is not connected NO events will be returned.
2697 * When connected and no data pending it will get the write event.
2698 * UDP sockets don't have connections so as soon as they are bound
2699 * they can read/write data. Since nobody is sendind us data only
2700 * the write event will be returned and ONLY once.
2702 ok (!WSAEventSelect(s
, event
, FD_READ
| FD_WRITE
), "Test[%d]: WSAEventSelect failed\n", i
);
2703 memset(&net_events
, 0xAB, sizeof(net_events
));
2704 ok (!WSAEnumNetworkEvents(s
, l
== 0 ? event
: NULL
, &net_events
),
2705 "Test[%d]: WSAEnumNetworkEvents failed\n", i
);
2706 if (i
>= 1 && j
== 0) /* FD_WRITE is SET on first try for UDP and connected TCP */
2708 if (i
== 0) /* Remove when fixed */
2711 ok (net_events
.lNetworkEvents
== FD_WRITE
, "Test[%d]: expected 2, got %d\n",
2712 i
, net_events
.lNetworkEvents
);
2715 ok (net_events
.lNetworkEvents
== FD_WRITE
, "Test[%d]: expected 2, got %d\n",
2716 i
, net_events
.lNetworkEvents
);
2720 if (i
!= 0) /* Remove when fixed */
2723 ok (net_events
.lNetworkEvents
== 0, "Test[%d]: expected 0, got %d\n",
2724 i
, net_events
.lNetworkEvents
);
2727 ok (net_events
.lNetworkEvents
== 0, "Test[%d]: expected 0, got %d\n",
2728 i
, net_events
.lNetworkEvents
);
2730 for (k
= 0; k
< FD_MAX_EVENTS
; k
++)
2732 if (i
>= 1 && j
== 0 && k
== 1) /* first UDP and connected TCP test, FD_WRITE bit no error*/
2734 ok (net_events
.iErrorCode
[k
] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n",
2735 i
, k
, net_events
.iErrorCode
[k
]);
2739 /* Bits that are not set in lNetworkEvents MUST not be changed */
2741 ok (net_events
.iErrorCode
[k
] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n",
2742 i
, k
, net_events
.iErrorCode
[k
]);
2747 WSACloseEvent(event
);
2748 if (i
== 2) closesocket(s2
);
2753 static void test_WSAAddressToStringA(void)
2755 SOCKET v6
= INVALID_SOCKET
;
2759 SOCKADDR_IN sockaddr
;
2760 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
2762 CHAR expect1
[] = "0.0.0.0";
2763 CHAR expect2
[] = "255.255.255.255";
2764 CHAR expect3
[] = "0.0.0.0:65535";
2765 CHAR expect4
[] = "255.255.255.255:65535";
2767 SOCKADDR_IN6 sockaddr6
;
2768 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
2770 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
2771 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
2772 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
2774 CHAR expect6_1
[] = "::1";
2775 CHAR expect6_2
[] = "20ab::1";
2776 CHAR expect6_3
[] = "[20ab::2001]:33274";
2777 CHAR expect6_3_nt
[] = "20ab::2001@33274";
2778 CHAR expect6_3_w2k
[] = "20ab::2001";
2779 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
2780 CHAR expect6_3_2_nt
[] = "4660/20ab::2001@33274";
2781 CHAR expect6_3_2_w2k
[] = "20ab::2001%4660";
2782 CHAR expect6_3_3
[] = "20ab::2001%4660";
2783 CHAR expect6_3_3_nt
[] = "4660/20ab::2001";
2787 sockaddr
.sin_family
= AF_INET
;
2788 sockaddr
.sin_port
= 0;
2789 sockaddr
.sin_addr
.s_addr
= 0;
2791 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2792 GLE
= WSAGetLastError();
2793 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
2794 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
2797 len
= sizeof(address
);
2799 sockaddr
.sin_family
= AF_INET
;
2800 sockaddr
.sin_port
= 0;
2801 sockaddr
.sin_addr
.s_addr
= 0;
2803 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2804 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2806 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
2807 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
2809 len
= sizeof(address
);
2811 sockaddr
.sin_family
= AF_INET
;
2812 sockaddr
.sin_port
= 0;
2813 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
2815 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2816 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2818 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
2820 len
= sizeof(address
);
2822 sockaddr
.sin_family
= AF_INET
;
2823 sockaddr
.sin_port
= 0xffff;
2824 sockaddr
.sin_addr
.s_addr
= 0;
2826 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2827 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2829 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
2831 len
= sizeof(address
);
2833 sockaddr
.sin_family
= AF_INET
;
2834 sockaddr
.sin_port
= 0xffff;
2835 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
2837 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2838 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2840 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
2841 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
2843 /*check to see it IPv6 is available */
2844 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
2845 if (v6
== INVALID_SOCKET
) {
2846 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2847 WSAGetLastError(), WSAEAFNOSUPPORT
);
2850 /* Test a short IPv6 address */
2851 len
= sizeof(address6
);
2853 sockaddr6
.sin6_family
= AF_INET6
;
2854 sockaddr6
.sin6_port
= 0x0000;
2855 sockaddr6
.sin6_scope_id
= 0;
2856 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
2858 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
2859 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2860 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
2861 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
2863 /* Test a longer IPv6 address */
2864 len
= sizeof(address6
);
2866 sockaddr6
.sin6_family
= AF_INET6
;
2867 sockaddr6
.sin6_port
= 0x0000;
2868 sockaddr6
.sin6_scope_id
= 0;
2869 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
2871 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
2872 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2873 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
2874 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
2876 /* Test IPv6 address and port number */
2877 len
= sizeof(address6
);
2879 sockaddr6
.sin6_family
= AF_INET6
;
2880 sockaddr6
.sin6_port
= 0xfa81;
2881 sockaddr6
.sin6_scope_id
= 0;
2882 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
2884 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
2885 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2886 ok( !strcmp( address6
, expect6_3
) ||
2887 broken( !strcmp( address6
, expect6_3_nt
) ) || /* NT4 */
2888 broken( !strcmp( address6
, expect6_3_w2k
) ), /* Win2000 */
2889 "Expected: %s, got: %s\n", expect6_3
, address6
);
2890 ok( len
== sizeof(expect6_3
) ||
2891 broken( len
== sizeof(expect6_3_nt
) ) || /* NT4 */
2892 broken( len
== sizeof(expect6_3_w2k
) ), /* Win2000 */
2893 "Got size %d\n", len
);
2895 /* Test IPv6 address, port number and scope_id */
2896 len
= sizeof(address6
);
2898 sockaddr6
.sin6_family
= AF_INET6
;
2899 sockaddr6
.sin6_port
= 0xfa81;
2900 sockaddr6
.sin6_scope_id
= 0x1234;
2901 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
2903 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
2904 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2905 ok( !strcmp( address6
, expect6_3_2
) ||
2906 broken( !strcmp( address6
, expect6_3_2_nt
) ) || /* NT4 */
2907 broken( !strcmp( address6
, expect6_3_2_w2k
) ), /* Win2000 */
2908 "Expected: %s, got: %s\n", expect6_3_2
, address6
);
2909 ok( len
== sizeof(expect6_3_2
) ||
2910 broken( len
== sizeof(expect6_3_2_nt
) ) || /* NT4 */
2911 broken( len
== sizeof(expect6_3_2_w2k
) ), /* Win2000 */
2912 "Got size %d\n", len
);
2914 /* Test IPv6 address and scope_id */
2915 len
= sizeof(address6
);
2917 sockaddr6
.sin6_family
= AF_INET6
;
2918 sockaddr6
.sin6_port
= 0x0000;
2919 sockaddr6
.sin6_scope_id
= 0x1234;
2920 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
2922 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
2923 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
2924 ok( !strcmp( address6
, expect6_3_3
) ||
2925 broken( !strcmp( address6
, expect6_3_3_nt
) ), /* NT4 */
2926 "Expected: %s, got: %s\n", expect6_3_3
, address6
);
2927 ok( len
== sizeof(expect6_3_3
) ||
2928 broken( len
== sizeof(expect6_3_3_nt
) ), /* NT4 */
2929 "Got size %d\n", len
);
2932 if (v6
!= INVALID_SOCKET
)
2936 static void test_WSAAddressToStringW(void)
2938 SOCKET v6
= INVALID_SOCKET
;
2942 SOCKADDR_IN sockaddr
;
2943 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
2945 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
2946 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
2947 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
2948 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
2949 '6', '5', '5', '3', '5', 0 };
2951 SOCKADDR_IN6 sockaddr6
;
2952 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
2954 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
2955 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
2956 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
2958 WCHAR expect6_1
[] = {':',':','1',0};
2959 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
2960 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
2961 WCHAR expect6_3_nt
[] = {'2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
2962 WCHAR expect6_3_w2k
[] = {'2','0','a','b',':',':','2','0','0','1',0};
2963 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
2964 WCHAR expect6_3_2_nt
[] = {'4','6','6','0','/','2','0','a','b',':',':','2','0','0','1','@','3','3','2','7','4',0};
2965 WCHAR expect6_3_2_w2k
[] = {'2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',0};
2966 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
2967 WCHAR expect6_3_3_nt
[] = {'6','5','5','3','4','/','2','0','a','b',':',':','2','0','0','1',0};
2971 sockaddr
.sin_family
= AF_INET
;
2972 sockaddr
.sin_port
= 0;
2973 sockaddr
.sin_addr
.s_addr
= 0;
2975 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2976 GLE
= WSAGetLastError();
2977 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
2978 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
2981 len
= sizeof(address
);
2983 sockaddr
.sin_family
= AF_INET
;
2984 sockaddr
.sin_port
= 0;
2985 sockaddr
.sin_addr
.s_addr
= 0;
2987 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
2988 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
2990 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
2991 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
2993 len
= sizeof(address
);
2995 sockaddr
.sin_family
= AF_INET
;
2996 sockaddr
.sin_port
= 0;
2997 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
2999 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
3000 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3002 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
3004 len
= sizeof(address
);
3006 sockaddr
.sin_family
= AF_INET
;
3007 sockaddr
.sin_port
= 0xffff;
3008 sockaddr
.sin_addr
.s_addr
= 0;
3010 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
3011 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3013 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
3015 len
= sizeof(address
);
3017 sockaddr
.sin_family
= AF_INET
;
3018 sockaddr
.sin_port
= 0xffff;
3019 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
3021 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
3022 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3024 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
3025 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
3027 /*check to see it IPv6 is available */
3028 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
3029 if (v6
== INVALID_SOCKET
) {
3030 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3031 WSAGetLastError(), WSAEAFNOSUPPORT
);
3035 /* Test a short IPv6 address */
3036 len
= sizeof(address6
)/sizeof(WCHAR
);
3038 sockaddr6
.sin6_family
= AF_INET6
;
3039 sockaddr6
.sin6_port
= 0x0000;
3040 sockaddr6
.sin6_scope_id
= 0;
3041 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
3043 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
3044 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3045 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
3046 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
3048 /* Test a longer IPv6 address */
3049 len
= sizeof(address6
)/sizeof(WCHAR
);
3051 sockaddr6
.sin6_family
= AF_INET6
;
3052 sockaddr6
.sin6_port
= 0x0000;
3053 sockaddr6
.sin6_scope_id
= 0;
3054 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
3056 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
3057 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3059 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
3060 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
3062 /* Test IPv6 address and port number */
3063 len
= sizeof(address6
)/sizeof(WCHAR
);
3065 sockaddr6
.sin6_family
= AF_INET6
;
3066 sockaddr6
.sin6_port
= 0xfa81;
3067 sockaddr6
.sin6_scope_id
= 0;
3068 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
3070 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
3071 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3072 ok( !lstrcmpW( address6
, expect6_3
) ||
3073 broken( !lstrcmpW( address6
, expect6_3_nt
) ) || /* NT4 */
3074 broken( !lstrcmpW( address6
, expect6_3_w2k
) ), /* Win2000 */
3075 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3
),
3076 wine_dbgstr_w(address6
) );
3077 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
) ||
3078 broken(len
== sizeof(expect6_3_nt
)/sizeof(WCHAR
) ) || /* NT4 */
3079 broken(len
== sizeof(expect6_3_w2k
)/sizeof(WCHAR
) ), /* Win2000 */
3082 /* Test IPv6 address, port number and scope_id */
3083 len
= sizeof(address6
)/sizeof(WCHAR
);
3085 sockaddr6
.sin6_family
= AF_INET6
;
3086 sockaddr6
.sin6_port
= 0xfa81;
3087 sockaddr6
.sin6_scope_id
= 0x1234;
3088 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
3090 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
3091 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3092 ok( !lstrcmpW( address6
, expect6_3_2
) ||
3093 broken( !lstrcmpW( address6
, expect6_3_2_nt
) ) || /* NT4 */
3094 broken( !lstrcmpW( address6
, expect6_3_2_w2k
) ), /* Win2000 */
3095 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2
),
3096 wine_dbgstr_w(address6
) );
3097 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
) ||
3098 broken( len
== sizeof(expect6_3_2_nt
)/sizeof(WCHAR
) ) || /* NT4 */
3099 broken( len
== sizeof(expect6_3_2_w2k
)/sizeof(WCHAR
) ), /* Win2000 */
3102 /* Test IPv6 address and scope_id */
3103 len
= sizeof(address6
)/sizeof(WCHAR
);
3105 sockaddr6
.sin6_family
= AF_INET6
;
3106 sockaddr6
.sin6_port
= 0x0000;
3107 sockaddr6
.sin6_scope_id
= 0xfffe;
3108 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
3110 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
3111 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
3112 ok( !lstrcmpW( address6
, expect6_3_3
) ||
3113 broken( !lstrcmpW( address6
, expect6_3_3_nt
) ), /* NT4 */
3114 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3
),
3115 wine_dbgstr_w(address6
) );
3116 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
) ||
3117 broken( len
== sizeof(expect6_3_3_nt
)/sizeof(WCHAR
) ), /* NT4 */
3121 if (v6
!= INVALID_SOCKET
)
3125 static void test_WSAStringToAddressA(void)
3128 SOCKADDR_IN sockaddr
;
3129 SOCKADDR_IN6 sockaddr6
;
3132 CHAR address1
[] = "0.0.0.0";
3133 CHAR address2
[] = "127.127.127.127";
3134 CHAR address3
[] = "255.255.255.255";
3135 CHAR address4
[] = "127.127.127.127:65535";
3136 CHAR address5
[] = "255.255.255.255:65535";
3137 CHAR address6
[] = "::1";
3138 CHAR address7
[] = "[::1]";
3139 CHAR address8
[] = "[::1]:65535";
3140 CHAR address9
[] = "2001::1";
3143 sockaddr
.sin_family
= AF_INET
;
3145 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3146 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
3147 WSAGetLastError() );
3149 len
= sizeof(sockaddr
);
3150 sockaddr
.sin_port
= 0;
3151 sockaddr
.sin_addr
.s_addr
= 0;
3153 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3154 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
3155 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3157 len
= sizeof(sockaddr
);
3158 sockaddr
.sin_port
= 0;
3159 sockaddr
.sin_addr
.s_addr
= 0;
3161 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3162 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
3163 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3165 len
= sizeof(sockaddr
);
3166 sockaddr
.sin_port
= 0;
3167 sockaddr
.sin_addr
.s_addr
= 0;
3169 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3170 GLE
= WSAGetLastError();
3171 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
3172 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
3173 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
3175 len
= sizeof(sockaddr
);
3176 sockaddr
.sin_port
= 0;
3177 sockaddr
.sin_addr
.s_addr
= 0;
3179 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3180 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
3181 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
3183 len
= sizeof(sockaddr
);
3184 sockaddr
.sin_port
= 0;
3185 sockaddr
.sin_addr
.s_addr
= 0;
3187 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3188 GLE
= WSAGetLastError();
3189 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
3190 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
3191 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
3193 len
= sizeof(sockaddr6
);
3194 memset(&sockaddr6
, 0, len
);
3195 sockaddr6
.sin6_family
= AF_INET6
;
3197 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3199 GLE
= WSAGetLastError();
3200 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3201 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
3203 len
= sizeof(sockaddr6
);
3204 memset(&sockaddr6
, 0, len
);
3205 sockaddr6
.sin6_family
= AF_INET6
;
3207 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3209 GLE
= WSAGetLastError();
3210 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3211 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
3213 len
= sizeof(sockaddr6
);
3214 memset(&sockaddr6
, 0, len
);
3215 sockaddr6
.sin6_family
= AF_INET6
;
3217 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3219 GLE
= WSAGetLastError();
3220 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
3221 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3222 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
3224 len
= sizeof(sockaddr
);
3226 ret
= WSAStringToAddressA( address9
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3227 GLE
= WSAGetLastError();
3228 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3229 "WSAStringToAddressA() should have failed with %d\n", GLE
);
3232 static void test_WSAStringToAddressW(void)
3235 SOCKADDR_IN sockaddr
, *sin
;
3236 SOCKADDR_IN6 sockaddr6
;
3237 SOCKADDR_STORAGE sockaddr_storage
;
3240 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
3241 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
3242 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
3243 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
3244 ':', '6', '5', '5', '3', '5', 0 };
3245 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
3246 '6', '5', '5', '3', '5', 0 };
3247 WCHAR address6
[] = {':',':','1','\0'};
3248 WCHAR address7
[] = {'[',':',':','1',']','\0'};
3249 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
3250 WCHAR address9
[] = {'2','0','0','1',':',':','1','\0'};
3253 sockaddr
.sin_family
= AF_INET
;
3255 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3256 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
3257 WSAGetLastError() );
3259 len
= sizeof(sockaddr
);
3260 sockaddr
.sin_port
= 0;
3261 sockaddr
.sin_addr
.s_addr
= 0;
3263 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3264 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
3265 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3267 len
= sizeof(sockaddr
);
3268 sockaddr
.sin_port
= 0;
3269 sockaddr
.sin_addr
.s_addr
= 0;
3271 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3272 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
3273 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3275 len
= sizeof(sockaddr
);
3276 sockaddr
.sin_port
= 0;
3277 sockaddr
.sin_addr
.s_addr
= 0;
3279 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3280 GLE
= WSAGetLastError();
3281 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
3282 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
3283 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
3285 len
= sizeof(sockaddr
);
3286 sockaddr
.sin_port
= 0;
3287 sockaddr
.sin_addr
.s_addr
= 0;
3289 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3290 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
3291 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
3293 len
= sizeof(sockaddr
);
3294 sockaddr
.sin_port
= 0;
3295 sockaddr
.sin_addr
.s_addr
= 0;
3297 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3298 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
3299 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
3300 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
3302 /* Test with a larger buffer than necessary */
3303 len
= sizeof(sockaddr_storage
);
3304 sin
= (SOCKADDR_IN
*)&sockaddr_storage
;
3306 sin
->sin_addr
.s_addr
= 0;
3308 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)sin
, &len
);
3309 ok( (ret
== 0 && sin
->sin_addr
.s_addr
== 0xffffffff && sin
->sin_port
== 0xffff) ||
3310 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
3311 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
3312 ok( len
== sizeof(SOCKADDR_IN
) ||
3313 broken(len
== sizeof(SOCKADDR_STORAGE
)) /* NT4/2k */,
3314 "unexpected length %d\n", len
);
3316 len
= sizeof(sockaddr6
);
3317 memset(&sockaddr6
, 0, len
);
3318 sockaddr6
.sin6_family
= AF_INET6
;
3320 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3322 GLE
= WSAGetLastError();
3323 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3324 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
3326 len
= sizeof(sockaddr6
);
3327 memset(&sockaddr6
, 0, len
);
3328 sockaddr6
.sin6_family
= AF_INET6
;
3330 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3332 GLE
= WSAGetLastError();
3333 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3334 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
3336 len
= sizeof(sockaddr6
);
3337 memset(&sockaddr6
, 0, len
);
3338 sockaddr6
.sin6_family
= AF_INET6
;
3340 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
3342 GLE
= WSAGetLastError();
3343 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
3344 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3345 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
3347 len
= sizeof(sockaddr
);
3349 ret
= WSAStringToAddressW( address9
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
3350 GLE
= WSAGetLastError();
3351 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
3352 "WSAStringToAddressW() should have failed with %d\n", GLE
);
3355 static DWORD WINAPI
SelectReadThread(void *param
)
3357 select_thread_params
*par
= param
;
3360 struct sockaddr_in addr
;
3361 struct timeval select_timeout
;
3364 FD_SET(par
->s
, &readfds
);
3365 select_timeout
.tv_sec
=5;
3366 select_timeout
.tv_usec
=0;
3367 addr
.sin_family
= AF_INET
;
3368 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
3369 addr
.sin_port
= htons(SERVERPORT
);
3371 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
3372 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
3374 SetEvent(server_ready
);
3375 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
3376 par
->ReadKilled
= (ret
== 1);
3381 static DWORD WINAPI
SelectCloseThread(void *param
)
3383 SOCKET s
= *(SOCKET
*)param
;
3389 static void test_errors(void)
3392 SOCKADDR_IN SockAddr
;
3395 WSASetLastError(NO_ERROR
);
3396 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
3397 ok( (sock
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3398 memset(&SockAddr
, 0, sizeof(SockAddr
));
3399 SockAddr
.sin_family
= AF_INET
;
3400 SockAddr
.sin_port
= htons(6924);
3401 SockAddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3403 ret
= connect(sock
, (PSOCKADDR
)&SockAddr
, sizeof(SockAddr
));
3404 ok( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got: %d\n", ret
);
3405 if (ret
== SOCKET_ERROR
)
3407 err
= WSAGetLastError();
3408 ok( (err
== WSAECONNREFUSED
), "expected WSAECONNREFUSED, got: %d\n", err
);
3413 fd_set set
= {1, {sock
}};
3416 timeval
.tv_usec
= 50000;
3418 ret
= select(1, NULL
, &set
, NULL
, &timeval
);
3419 ok( (ret
== 0), "expected 0 (timeout), got: %d\n", ret
);
3422 ret
= closesocket(sock
);
3423 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
3426 static void test_listen(void)
3429 int ret
, acceptc
, olen
= sizeof(acceptc
);
3430 struct sockaddr_in address
;
3432 memset(&address
, 0, sizeof(address
));
3433 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3434 address
.sin_family
= AF_INET
;
3435 address
.sin_port
= htons(SERVERPORT
);
3437 /* invalid socket tests */
3438 SetLastError(0xdeadbeef);
3439 ok ((listen(0, 0) == SOCKET_ERROR
), "listen did not fail\n");
3440 ret
= WSAGetLastError();
3441 ok (ret
== WSAENOTSOCK
, "expected 10038, received %d\n", ret
);
3443 SetLastError(0xdeadbeef);
3444 ok ((listen(0xdeadbeef, 0) == SOCKET_ERROR
), "listen did not fail\n");
3445 ret
= WSAGetLastError();
3446 ok (ret
== WSAENOTSOCK
, "expected 10038, received %d\n", ret
);
3449 fdA
= socket(AF_INET
, SOCK_STREAM
, 0);
3450 ok ((fdA
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3452 fdB
= socket(AF_INET
, SOCK_STREAM
, 0);
3453 ok ((fdB
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3455 SetLastError(0xdeadbeef);
3456 ok ((listen(fdA
, -2) == SOCKET_ERROR
), "listen did not fail\n");
3457 ret
= WSAGetLastError();
3458 ok (ret
== WSAEINVAL
, "expected 10022, received %d\n", ret
);
3460 SetLastError(0xdeadbeef);
3461 ok ((listen(fdA
, 1) == SOCKET_ERROR
), "listen did not fail\n");
3462 ret
= WSAGetLastError();
3463 ok (ret
== WSAEINVAL
, "expected 10022, received %d\n", ret
);
3465 SetLastError(0xdeadbeef);
3466 ok ((listen(fdA
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
3467 ret
= WSAGetLastError();
3468 ok (ret
== WSAEINVAL
, "expected 10022, received %d\n", ret
);
3470 ok (!bind(fdA
, (struct sockaddr
*) &address
, sizeof(address
)), "bind failed\n");
3472 SetLastError(0xdeadbeef);
3473 ok (bind(fdB
, (struct sockaddr
*) &address
, sizeof(address
)), "bind should have failed\n");
3474 ok (ret
== WSAEINVAL
, "expected 10022, received %d\n", ret
);
3477 ret
= getsockopt(fdA
, SOL_SOCKET
, SO_ACCEPTCONN
, (char*)&acceptc
, &olen
);
3478 ok (!ret
, "getsockopt failed\n");
3479 ok (acceptc
== 0, "SO_ACCEPTCONN should be 0, received %d\n", acceptc
);
3481 ok (!listen(fdA
, 0), "listen failed\n");
3482 ok (!listen(fdA
, SOMAXCONN
), "double listen failed\n");
3485 ret
= getsockopt(fdA
, SOL_SOCKET
, SO_ACCEPTCONN
, (char*)&acceptc
, &olen
);
3486 ok (!ret
, "getsockopt failed\n");
3487 ok (acceptc
== 1, "SO_ACCEPTCONN should be 1, received %d\n", acceptc
);
3489 SetLastError(0xdeadbeef);
3490 ok ((listen(fdB
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
3491 ret
= WSAGetLastError();
3492 ok (ret
== WSAEINVAL
, "expected 10022, received %d\n", ret
);
3494 ret
= closesocket(fdB
);
3495 ok (ret
== 0, "closesocket failed unexpectedly: %d\n", ret
);
3497 fdB
= socket(AF_INET
, SOCK_STREAM
, 0);
3498 ok ((fdB
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3500 SetLastError(0xdeadbeef);
3501 ok (bind(fdB
, (struct sockaddr
*) &address
, sizeof(address
)), "bind should have failed\n");
3502 ret
= WSAGetLastError();
3503 ok (ret
== WSAEADDRINUSE
, "expected 10048, received %d\n", ret
);
3505 ret
= closesocket(fdA
);
3506 ok (ret
== 0, "closesocket failed unexpectedly: %d\n", ret
);
3507 ret
= closesocket(fdB
);
3508 ok (ret
== 0, "closesocket failed unexpectedly: %d\n", ret
);
3511 #define FD_ZERO_ALL() { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); }
3512 #define FD_SET_ALL(s) { FD_SET(s, &readfds); FD_SET(s, &writefds); FD_SET(s, &exceptfds); }
3513 static void test_select(void)
3515 static char tmp_buf
[1024];
3517 SOCKET fdListen
, fdRead
, fdWrite
;
3518 fd_set readfds
, writefds
, exceptfds
;
3522 struct timeval select_timeout
;
3523 struct sockaddr_in address
;
3524 select_thread_params thread_params
;
3525 HANDLE thread_handle
;
3528 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
3529 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3530 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
3531 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
3535 FD_SET_ALL(fdWrite
);
3536 select_timeout
.tv_sec
=0;
3537 select_timeout
.tv_usec
=500;
3540 if (fdWrite
> maxfd
)
3543 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3544 ok ( (ret
== 0), "select should not return any socket handles\n");
3545 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
3546 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
3547 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
3548 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
3550 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
3551 ret
= closesocket(fdWrite
);
3552 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
3554 thread_params
.s
= fdRead
;
3555 thread_params
.ReadKilled
= FALSE
;
3556 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
3557 thread_handle
= CreateThread (NULL
, 0, SelectReadThread
, &thread_params
, 0, &id
);
3558 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
3560 WaitForSingleObject (server_ready
, INFINITE
);
3562 ret
= closesocket(fdRead
);
3563 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
3565 WaitForSingleObject (thread_handle
, 1000);
3566 ok ( (thread_params
.ReadKilled
) ||
3567 broken(thread_params
.ReadKilled
== 0), /*Win98*/
3568 "closesocket did not wakeup select\n");
3569 ret
= recv(fdRead
, &buffer
, 1, MSG_PEEK
);
3570 ok( (ret
== -1), "peek at closed socket expected -1 got %d\n", ret
);
3572 /* Test selecting invalid handles */
3576 ret
= select(maxfd
+1, 0, 0, 0, &select_timeout
);
3577 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
3578 ok ( WSAGetLastError() == WSAEINVAL
, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3581 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3582 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
3583 ok ( WSAGetLastError() == WSAEINVAL
, "expected WSAEINVAL, got %i\n", WSAGetLastError());
3585 FD_SET(INVALID_SOCKET
, &readfds
);
3587 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3588 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
3589 ok ( WSAGetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3590 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
3593 FD_SET(INVALID_SOCKET
, &writefds
);
3595 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3596 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
3597 ok ( WSAGetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3598 ok ( !FD_ISSET(fdRead
, &writefds
), "FD should not be set\n");
3601 FD_SET(INVALID_SOCKET
, &exceptfds
);
3603 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3604 ok ( (ret
== SOCKET_ERROR
), "expected SOCKET_ERROR, got %i\n", ret
);
3605 ok ( WSAGetLastError() == WSAENOTSOCK
, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
3606 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
3608 ok(!tcp_socketpair(&fdRead
, &fdWrite
), "creating socket pair failed\n");
3610 if(fdWrite
> maxfd
) maxfd
= fdWrite
;
3613 FD_SET(fdRead
, &readfds
);
3614 ret
= select(fdRead
+1, &readfds
, NULL
, NULL
, &select_timeout
);
3615 ok(!ret
, "select returned %d\n", ret
);
3618 FD_SET(fdWrite
, &writefds
);
3619 ret
= select(fdWrite
+1, NULL
, &writefds
, NULL
, &select_timeout
);
3620 ok(ret
== 1, "select returned %d\n", ret
);
3621 ok(FD_ISSET(fdWrite
, &writefds
), "fdWrite socket is not in the set\n");
3623 /* tests for overlapping fd_set pointers */
3625 FD_SET(fdWrite
, &readfds
);
3626 ret
= select(fdWrite
+1, &readfds
, &readfds
, NULL
, &select_timeout
);
3627 ok(ret
== 1, "select returned %d\n", ret
);
3628 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3631 FD_SET(fdWrite
, &readfds
);
3632 FD_SET(fdRead
, &readfds
);
3633 ret
= select(maxfd
+1, &readfds
, &readfds
, NULL
, &select_timeout
);
3634 ok(ret
== 2, "select returned %d\n", ret
);
3635 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3636 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3638 ok(send(fdWrite
, "test", 4, 0) == 4, "failed to send data\n");
3640 FD_SET(fdRead
, &readfds
);
3641 ret
= select(fdRead
+1, &readfds
, NULL
, NULL
, &select_timeout
);
3642 ok(ret
== 1, "select returned %d\n", ret
);
3643 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3646 FD_SET(fdWrite
, &readfds
);
3647 FD_SET(fdRead
, &readfds
);
3648 ret
= select(maxfd
+1, &readfds
, &readfds
, NULL
, &select_timeout
);
3649 ok(ret
== 2, "select returned %d\n", ret
);
3650 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3651 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3655 FD_SET(fdWrite
, &writefds
);
3656 ret
= select(fdWrite
+1, NULL
, &writefds
, NULL
, &select_timeout
);
3658 ok(send(fdWrite
, tmp_buf
, sizeof(tmp_buf
), 0) > 0, "failed to send data\n");
3661 FD_SET(fdWrite
, &readfds
);
3662 FD_SET(fdRead
, &readfds
);
3663 ret
= select(maxfd
+1, &readfds
, &readfds
, NULL
, &select_timeout
);
3664 ok(ret
== 1, "select returned %d\n", ret
);
3665 ok(!FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is in the set\n");
3666 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3668 ok(send(fdRead
, "test", 4, 0) == 4, "failed to send data\n");
3671 FD_SET(fdWrite
, &readfds
);
3672 FD_SET(fdRead
, &readfds
);
3673 ret
= select(maxfd
+1, &readfds
, &readfds
, NULL
, &select_timeout
);
3674 ok(ret
== 2, "select returned %d\n", ret
);
3675 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3676 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3678 closesocket(fdRead
);
3679 closesocket(fdWrite
);
3681 /* select() works in 3 distinct states:
3682 * - to check if a connection attempt ended with success or error;
3683 * - to check if a pending connection is waiting for acceptance;
3684 * - to check for data to read, availability for write and OOB data
3686 * The tests below ensure that all conditions are tested.
3688 memset(&address
, 0, sizeof(address
));
3689 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3690 address
.sin_family
= AF_INET
;
3691 len
= sizeof(address
);
3692 fdListen
= setup_server_socket(&address
, &len
);
3693 select_timeout
.tv_sec
= 1;
3694 select_timeout
.tv_usec
= 250000;
3696 /* When no events are pending select returns 0 with no error */
3698 FD_SET_ALL(fdListen
);
3699 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3700 ok(ret
== 0, "expected 0, got %d\n", ret
);
3702 /* When a socket is attempting to connect the listening socket receives the read descriptor */
3703 fdWrite
= setup_connector_socket(&address
, len
, TRUE
);
3705 FD_SET_ALL(fdListen
);
3706 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3707 ok(ret
== 1, "expected 1, got %d\n", ret
);
3708 ok(FD_ISSET(fdListen
, &readfds
), "fdListen socket is not in the set\n");
3709 len
= sizeof(address
);
3710 fdRead
= accept(fdListen
, (struct sockaddr
*) &address
, &len
);
3711 ok(fdRead
!= INVALID_SOCKET
, "expected a valid socket\n");
3713 /* The connector is signaled through the write descriptor */
3715 FD_SET_ALL(fdListen
);
3717 FD_SET_ALL(fdWrite
);
3718 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3719 ok(ret
== 2, "expected 2, got %d\n", ret
);
3720 ok(FD_ISSET(fdWrite
, &writefds
), "fdWrite socket is not in the set\n");
3721 ok(FD_ISSET(fdRead
, &writefds
), "fdRead socket is not in the set\n");
3724 ok(!getsockopt(fdWrite
, SOL_SOCKET
, SO_ERROR
, (char*)&id
, &len
), "getsockopt failed with %d\n",WSAGetLastError());
3725 ok(id
== 0, "expected 0, got %d\n", id
);
3727 /* When data is received the receiver gets the read descriptor */
3728 ret
= send(fdWrite
, "1234", 4, 0);
3729 ok(ret
== 4, "expected 4, got %d\n", ret
);
3731 FD_SET_ALL(fdListen
);
3732 FD_SET(fdRead
, &readfds
);
3733 FD_SET(fdRead
, &exceptfds
);
3734 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3735 ok(ret
== 1, "expected 1, got %d\n", ret
);
3736 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3737 ret
= recv(fdRead
, tmp_buf
, sizeof(tmp_buf
), 0);
3738 ok(ret
== 4, "expected 4, got %d\n", ret
);
3739 ok(!strcmp(tmp_buf
, "1234"), "data received differs from sent\n");
3741 /* When OOB data is received the socket is set in the except descriptor */
3742 ret
= send(fdWrite
, "A", 1, MSG_OOB
);
3743 ok(ret
== 1, "expected 1, got %d\n", ret
);
3745 FD_SET_ALL(fdListen
);
3746 FD_SET(fdRead
, &readfds
);
3747 FD_SET(fdRead
, &exceptfds
);
3748 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3749 ok(ret
== 1, "expected 1, got %d\n", ret
);
3750 ok(FD_ISSET(fdRead
, &exceptfds
), "fdRead socket is not in the set\n");
3752 ret
= recv(fdRead
, tmp_buf
, sizeof(tmp_buf
), MSG_OOB
);
3753 ok(ret
== 1, "expected 1, got %d\n", ret
);
3754 ok(tmp_buf
[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf
[0]);
3756 /* If the socket is OOBINLINED it will not receive the OOB in except fds */
3758 ret
= setsockopt(fdRead
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &ret
, sizeof(ret
));
3759 ok(ret
== 0, "expected 0, got %d\n", ret
);
3760 ret
= send(fdWrite
, "A", 1, MSG_OOB
);
3761 ok(ret
== 1, "expected 1, got %d\n", ret
);
3763 FD_SET_ALL(fdListen
);
3764 FD_SET(fdRead
, &readfds
);
3765 FD_SET(fdRead
, &exceptfds
);
3766 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3767 ok(ret
== 1, "expected 1, got %d\n", ret
);
3768 ok(FD_ISSET(fdRead
, &readfds
), "fdRead socket is not in the set\n");
3770 SetLastError(0xdeadbeef);
3771 ret
= recv(fdRead
, tmp_buf
, sizeof(tmp_buf
), MSG_OOB
);
3772 if (ret
== SOCKET_ERROR
) /* can't recv with MSG_OOB if OOBINLINED */
3774 ok(GetLastError() == WSAEINVAL
, "expected 10022, got %d\n", GetLastError());
3775 ret
= recv(fdRead
, tmp_buf
, sizeof(tmp_buf
), 0);
3776 ok(ret
== 1, "expected 1, got %d\n", ret
);
3777 ok(tmp_buf
[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf
[0]);
3779 else ok(broken(ret
== 1) /* <= NT4 */, "expected error, got 1\n");
3781 /* When the connection is closed the socket is set in the read descriptor */
3782 ret
= closesocket(fdRead
);
3783 ok(ret
== 0, "expected 0, got %d\n", ret
);
3785 FD_SET_ALL(fdListen
);
3786 FD_SET(fdWrite
, &readfds
);
3787 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3788 ok(ret
== 1, "expected 1, got %d\n", ret
);
3789 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3790 ret
= recv(fdWrite
, tmp_buf
, sizeof(tmp_buf
), 0);
3791 ok(ret
== 0, "expected 0, got %d\n", ret
);
3793 /* When a connection is attempted to a non-listening socket it will get to the except descriptor */
3794 ret
= closesocket(fdWrite
);
3795 ok(ret
== 0, "expected 0, got %d\n", ret
);
3796 ret
= closesocket(fdListen
);
3797 ok(ret
== 0, "expected 0, got %d\n", ret
);
3798 len
= sizeof(address
);
3799 fdWrite
= setup_connector_socket(&address
, len
, TRUE
);
3801 FD_SET(fdWrite
, &writefds
);
3802 FD_SET(fdWrite
, &exceptfds
);
3803 select_timeout
.tv_sec
= 2; /* requires more time to realize it will not connect */
3804 ret
= select(0, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
3805 ok(ret
== 1, "expected 1, got %d\n", ret
);
3808 ok(!getsockopt(fdWrite
, SOL_SOCKET
, SO_ERROR
, (char*)&id
, &len
), "getsockopt failed with %d\n",WSAGetLastError());
3809 ok(id
== WSAECONNREFUSED
, "expected 10061, got %d\n", id
);
3810 ok(FD_ISSET(fdWrite
, &exceptfds
), "fdWrite socket is not in the set\n");
3811 ok(select_timeout
.tv_usec
== 250000, "select timeout should not have changed\n");
3812 closesocket(fdWrite
);
3814 /* Try select() on a closed socket after connection */
3815 ok(!tcp_socketpair(&fdRead
, &fdWrite
), "creating socket pair failed\n");
3816 closesocket(fdRead
);
3818 FD_SET_ALL(fdWrite
);
3820 SetLastError(0xdeadbeef);
3821 ret
= select(0, &readfds
, NULL
, &exceptfds
, &select_timeout
);
3822 ok(ret
== SOCKET_ERROR
, "expected 1, got %d\n", ret
);
3824 ok(GetLastError() == WSAENOTSOCK
, "expected 10038, got %d\n", GetLastError());
3825 /* descriptor sets are unchanged */
3826 ok(readfds
.fd_count
== 2, "expected 2, got %d\n", readfds
.fd_count
);
3827 ok(exceptfds
.fd_count
== 2, "expected 2, got %d\n", exceptfds
.fd_count
);
3828 closesocket(fdWrite
);
3830 /* Close the socket currently being selected in a thread - bug 38399 */
3831 ok(!tcp_socketpair(&fdRead
, &fdWrite
), "creating socket pair failed\n");
3832 thread_handle
= CreateThread(NULL
, 0, SelectCloseThread
, &fdWrite
, 0, &id
);
3833 ok(thread_handle
!= NULL
, "CreateThread failed unexpectedly: %d\n", GetLastError());
3835 FD_SET_ALL(fdWrite
);
3836 ret
= select(0, &readfds
, NULL
, &exceptfds
, &select_timeout
);
3837 ok(ret
== 1, "expected 1, got %d\n", ret
);
3838 ok(FD_ISSET(fdWrite
, &readfds
), "fdWrite socket is not in the set\n");
3839 WaitForSingleObject (thread_handle
, 1000);
3840 closesocket(fdRead
);
3841 /* test again with only the except descriptor */
3842 ok(!tcp_socketpair(&fdRead
, &fdWrite
), "creating socket pair failed\n");
3843 thread_handle
= CreateThread(NULL
, 0, SelectCloseThread
, &fdWrite
, 0, &id
);
3844 ok(thread_handle
!= NULL
, "CreateThread failed unexpectedly: %d\n", GetLastError());
3846 FD_SET(fdWrite
, &exceptfds
);
3847 SetLastError(0xdeadbeef);
3848 ret
= select(0, NULL
, NULL
, &exceptfds
, &select_timeout
);
3850 ok(ret
== SOCKET_ERROR
, "expected 1, got %d\n", ret
);
3852 ok(GetLastError() == WSAENOTSOCK
, "expected 10038, got %d\n", GetLastError());
3853 WaitForSingleObject (thread_handle
, 1000);
3854 closesocket(fdRead
);
3859 static DWORD WINAPI
AcceptKillThread(void *param
)
3861 select_thread_params
*par
= param
;
3862 struct sockaddr_in address
;
3863 int len
= sizeof(address
);
3864 SOCKET client_socket
;
3866 SetEvent(server_ready
);
3867 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
3868 if (client_socket
!= INVALID_SOCKET
)
3869 closesocket(client_socket
);
3870 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
3875 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
3876 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
3877 GROUP
*g
, DWORD_PTR dwCallbackData
)
3882 static SOCKET
setup_server_socket(struct sockaddr_in
*addr
, int *len
)
3885 SOCKET server_socket
;
3887 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
3888 if (server_socket
== INVALID_SOCKET
)
3890 trace("error creating server socket: %d\n", WSAGetLastError());
3891 return INVALID_SOCKET
;
3895 ret
= setsockopt(server_socket
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
3898 trace("error setting SO_REUSEADDR: %d\n", WSAGetLastError());
3899 closesocket(server_socket
);
3900 return INVALID_SOCKET
;
3903 ret
= bind(server_socket
, (struct sockaddr
*)addr
, *len
);
3906 trace("error binding server socket: %d\n", WSAGetLastError());
3909 ret
= getsockname(server_socket
, (struct sockaddr
*)addr
, len
);
3912 skip("failed to lookup bind address: %d\n", WSAGetLastError());
3913 closesocket(server_socket
);
3914 return INVALID_SOCKET
;
3917 ret
= listen(server_socket
, 5);
3920 trace("error making server socket listen: %d\n", WSAGetLastError());
3921 closesocket(server_socket
);
3922 return INVALID_SOCKET
;
3925 return server_socket
;
3928 static SOCKET
setup_connector_socket(struct sockaddr_in
*addr
, int len
, BOOL nonblock
)
3933 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3934 ok(connector
!= INVALID_SOCKET
, "failed to create connector socket %d\n", WSAGetLastError());
3937 set_blocking(connector
, !nonblock
);
3939 ret
= connect(connector
, (struct sockaddr
*)addr
, len
);
3941 ok(!ret
, "connecting to accepting socket failed %d\n", WSAGetLastError());
3942 else if (ret
== SOCKET_ERROR
)
3944 DWORD error
= WSAGetLastError();
3945 ok(error
== WSAEWOULDBLOCK
|| error
== WSAEINPROGRESS
,
3946 "expected 10035 or 10036, got %d\n", error
);
3952 static void test_accept(void)
3955 SOCKET server_socket
, accepted
= INVALID_SOCKET
, connector
;
3956 struct sockaddr_in address
;
3957 SOCKADDR_STORAGE ss
;
3959 select_thread_params thread_params
;
3960 HANDLE thread_handle
= NULL
;
3963 memset(&address
, 0, sizeof(address
));
3964 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3965 address
.sin_family
= AF_INET
;
3967 socklen
= sizeof(address
);
3968 server_socket
= setup_server_socket(&address
, &socklen
);
3969 if (server_socket
== INVALID_SOCKET
)
3971 trace("error creating server socket: %d\n", WSAGetLastError());
3975 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
3976 if (connector
== INVALID_SOCKET
) goto done
;
3978 trace("Blocking accept next\n");
3980 accepted
= WSAAccept(server_socket
, NULL
, NULL
, AlwaysDeferConditionFunc
, 0);
3981 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
3983 accepted
= accept(server_socket
, NULL
, 0);
3984 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
3986 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
3987 if (server_ready
== INVALID_HANDLE_VALUE
)
3989 trace("error creating event: %d\n", GetLastError());
3993 thread_params
.s
= server_socket
;
3994 thread_params
.ReadKilled
= FALSE
;
3995 thread_handle
= CreateThread(NULL
, 0, AcceptKillThread
, &thread_params
, 0, &id
);
3996 if (thread_handle
== NULL
)
3998 trace("error creating thread: %d\n", GetLastError());
4002 WaitForSingleObject(server_ready
, INFINITE
);
4004 ret
= closesocket(server_socket
);
4007 trace("closesocket failed: %d\n", WSAGetLastError());
4011 WaitForSingleObject(thread_handle
, 1000);
4012 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
4013 "closesocket did not wakeup accept\n");
4015 closesocket(accepted
);
4016 closesocket(connector
);
4017 accepted
= connector
= INVALID_SOCKET
;
4019 socklen
= sizeof(address
);
4020 server_socket
= setup_server_socket(&address
, &socklen
);
4021 if (server_socket
== INVALID_SOCKET
) goto done
;
4023 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4024 if (connector
== INVALID_SOCKET
) goto done
;
4027 accepted
= WSAAccept(server_socket
, (struct sockaddr
*)&ss
, &socklen
, NULL
, 0);
4028 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSAEFAULT
, "got %d\n", WSAGetLastError());
4029 ok(!socklen
, "got %d\n", socklen
);
4030 closesocket(connector
);
4031 connector
= INVALID_SOCKET
;
4033 socklen
= sizeof(address
);
4034 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4035 if (connector
== INVALID_SOCKET
) goto done
;
4037 accepted
= WSAAccept(server_socket
, NULL
, NULL
, NULL
, 0);
4038 ok(accepted
!= INVALID_SOCKET
, "Failed to accept connection, %d\n", WSAGetLastError());
4039 closesocket(accepted
);
4040 closesocket(connector
);
4041 accepted
= connector
= INVALID_SOCKET
;
4043 socklen
= sizeof(address
);
4044 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4045 if (connector
== INVALID_SOCKET
) goto done
;
4047 socklen
= sizeof(ss
);
4048 memset(&ss
, 0, sizeof(ss
));
4049 accepted
= WSAAccept(server_socket
, (struct sockaddr
*)&ss
, &socklen
, NULL
, 0);
4050 ok(accepted
!= INVALID_SOCKET
, "Failed to accept connection, %d\n", WSAGetLastError());
4051 ok(socklen
!= sizeof(ss
), "unexpected length\n");
4052 ok(ss
.ss_family
, "family not set\n");
4053 closesocket(accepted
);
4054 closesocket(connector
);
4055 accepted
= connector
= INVALID_SOCKET
;
4057 socklen
= sizeof(address
);
4058 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4059 if (connector
== INVALID_SOCKET
) goto done
;
4062 accepted
= accept(server_socket
, (struct sockaddr
*)&ss
, &socklen
);
4063 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSAEFAULT
, "got %d\n", WSAGetLastError());
4064 ok(!socklen
, "got %d\n", socklen
);
4065 closesocket(connector
);
4066 accepted
= connector
= INVALID_SOCKET
;
4068 socklen
= sizeof(address
);
4069 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4070 if (connector
== INVALID_SOCKET
) goto done
;
4072 accepted
= accept(server_socket
, NULL
, NULL
);
4073 ok(accepted
!= INVALID_SOCKET
, "Failed to accept connection, %d\n", WSAGetLastError());
4074 closesocket(accepted
);
4075 closesocket(connector
);
4076 accepted
= connector
= INVALID_SOCKET
;
4078 socklen
= sizeof(address
);
4079 connector
= setup_connector_socket(&address
, socklen
, FALSE
);
4080 if (connector
== INVALID_SOCKET
) goto done
;
4082 socklen
= sizeof(ss
);
4083 memset(&ss
, 0, sizeof(ss
));
4084 accepted
= accept(server_socket
, (struct sockaddr
*)&ss
, &socklen
);
4085 ok(accepted
!= INVALID_SOCKET
, "Failed to accept connection, %d\n", WSAGetLastError());
4086 ok(socklen
!= sizeof(ss
), "unexpected length\n");
4087 ok(ss
.ss_family
, "family not set\n");
4090 if (accepted
!= INVALID_SOCKET
)
4091 closesocket(accepted
);
4092 if (connector
!= INVALID_SOCKET
)
4093 closesocket(connector
);
4094 if (thread_handle
!= NULL
)
4095 CloseHandle(thread_handle
);
4096 if (server_ready
!= INVALID_HANDLE_VALUE
)
4097 CloseHandle(server_ready
);
4098 if (server_socket
!= INVALID_SOCKET
)
4099 closesocket(server_socket
);
4102 static void test_extendedSocketOptions(void)
4106 struct sockaddr_in sa
;
4107 int sa_len
= sizeof(struct sockaddr_in
);
4108 int optval
, optlen
= sizeof(int), ret
;
4112 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
4113 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
4117 memset(&sa
, 0, sa_len
);
4119 sa
.sin_family
= AF_INET
;
4120 sa
.sin_port
= htons(0);
4121 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
4123 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
4124 trace("Creating the socket failed: %d\n", WSAGetLastError());
4129 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
4130 trace("Failed to bind socket: %d\n", WSAGetLastError());
4136 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4138 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
4139 ok((optval
== 65507) || (optval
== 65527),
4140 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
4142 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
4143 SetLastError(0xdeadbeef);
4144 optval
= 0xdeadbeef;
4145 optlen
= sizeof(int);
4146 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4147 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
4148 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4149 ret
, WSAGetLastError(), optval
, optval
);
4151 /* more invalid values for level */
4152 SetLastError(0xdeadbeef);
4153 optval
= 0xdeadbeef;
4154 optlen
= sizeof(int);
4155 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4156 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
4157 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4158 ret
, WSAGetLastError(), optval
, optval
);
4160 SetLastError(0xdeadbeef);
4161 optval
= 0xdeadbeef;
4162 optlen
= sizeof(int);
4163 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4164 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
4165 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4166 ret
, WSAGetLastError(), optval
, optval
);
4168 SetLastError(0xdeadbeef);
4169 optval
= 0xdeadbeef;
4170 optlen
= sizeof(int);
4171 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4172 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
4173 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4174 ret
, WSAGetLastError(), optval
, optval
);
4176 SetLastError(0xdeadbeef);
4177 optval
= 0xdeadbeef;
4178 optlen
= sizeof(int);
4179 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
4180 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
4181 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
4182 ret
, WSAGetLastError(), optval
, optval
);
4184 SetLastError(0xdeadbeef);
4185 optlen
= sizeof(LINGER
);
4186 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
4187 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAENOPROTOOPT
),
4188 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
4189 ret
, WSAGetLastError());
4192 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
4193 trace("Creating the socket failed: %d\n", WSAGetLastError());
4198 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
4199 trace("Failed to bind socket: %d\n", WSAGetLastError());
4205 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
4206 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
4208 optlen
= sizeof(BOOL
);
4209 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
4210 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
4211 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
4212 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
4213 bool_opt_val
, linger_val
.l_onoff
);
4219 static void test_getsockname(void)
4223 struct sockaddr_in sa_set
, sa_get
;
4224 int sa_set_len
= sizeof(struct sockaddr_in
);
4225 int sa_get_len
= sa_set_len
;
4226 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
4230 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
4231 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
4235 memset(&sa_set
, 0, sa_set_len
);
4237 sa_set
.sin_family
= AF_INET
;
4238 sa_set
.sin_port
= htons(0);
4239 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
4241 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
4242 trace("Creating the socket failed: %d\n", WSAGetLastError());
4248 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
4249 ok(0, "getsockname on unbound socket should fail\n");
4251 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
4252 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
4253 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
4254 "failed getsockname modified sockaddr when it shouldn't\n");
4257 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
4258 trace("Failed to bind socket: %d\n", WSAGetLastError());
4264 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
4265 trace("Failed to call getsockname: %d\n", WSAGetLastError());
4271 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
4272 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
4273 "getsockname did not zero the sockaddr_in structure\n");
4277 h
= gethostbyname("");
4278 if (h
&& h
->h_length
== 4) /* this test is only meaningful in IPv4 */
4281 for (i
= 0; h
->h_addr_list
[i
]; i
++)
4285 ip
.s_addr
= *(ULONG
*) h
->h_addr_list
[i
];
4287 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
4288 ok(sock
!= INVALID_SOCKET
, "socket failed with %d\n", GetLastError());
4290 memset(&sa_set
, 0, sizeof(sa_set
));
4291 sa_set
.sin_family
= AF_INET
;
4292 sa_set
.sin_addr
.s_addr
= ip
.s_addr
;
4293 /* The same address we bind must be the same address we get */
4294 ret
= bind(sock
, (struct sockaddr
*)&sa_set
, sizeof(sa_set
));
4295 ok(ret
== 0, "bind failed with %d\n", GetLastError());
4296 sa_get_len
= sizeof(sa_get
);
4297 ret
= getsockname(sock
, (struct sockaddr
*)&sa_get
, &sa_get_len
);
4298 ok(ret
== 0, "getsockname failed with %d\n", GetLastError());
4299 strcpy(ipstr
, inet_ntoa(sa_get
.sin_addr
));
4300 trace("testing bind on interface %s\n", ipstr
);
4301 ok(sa_get
.sin_addr
.s_addr
== sa_set
.sin_addr
.s_addr
,
4302 "address does not match: %s != %s\n", ipstr
, inet_ntoa(sa_set
.sin_addr
));
4311 static void test_dns(void)
4322 h
= gethostbyname("");
4323 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
4325 /* Use an address with valid alias names if possible */
4326 h
= gethostbyname("source.winehq.org");
4329 skip("Can't test the hostent structure because gethostbyname failed\n");
4333 /* The returned struct must be allocated in a very strict way. First we need to
4334 * count how many aliases there are because they must be located right after
4335 * the struct hostent size. Knowing the amount of aliases we know the exact
4336 * location of the first IP returned. Rule valid for >= XP, for older OS's
4337 * it's somewhat the opposite. */
4339 if(h
->h_addr_list
== addr
.mem
) /* <= W2K */
4341 win_skip("Skipping hostent tests since this OS is unsupported\n");
4345 ok(h
->h_aliases
== addr
.mem
,
4346 "hostent->h_aliases should be in %p, it is in %p\n", addr
.mem
, h
->h_aliases
);
4348 for(ptr
= h
->h_aliases
, acount
= 1; *ptr
; ptr
++) acount
++;
4349 addr
.chr
+= sizeof(*ptr
) * acount
;
4350 ok(h
->h_addr_list
== addr
.mem
,
4351 "hostent->h_addr_list should be in %p, it is in %p\n", addr
.mem
, h
->h_addr_list
);
4353 for(ptr
= h
->h_addr_list
, acount
= 1; *ptr
; ptr
++) acount
++;
4355 addr
.chr
+= sizeof(*ptr
) * acount
;
4356 ok(h
->h_addr_list
[0] == addr
.mem
,
4357 "hostent->h_addr_list[0] should be in %p, it is in %p\n", addr
.mem
, h
->h_addr_list
[0]);
4360 /* Our winsock headers don't define gethostname because it conflicts with the
4361 * definition in unistd.h. Define it here to get rid of the warning. */
4363 int WINAPI
gethostname(char *name
, int namelen
);
4365 static void test_gethostbyname_hack(void)
4369 static BYTE loopback
[] = {127, 0, 0, 1};
4370 static BYTE magic_loopback
[] = {127, 12, 34, 56};
4373 ret
= gethostname(name
, 256);
4374 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
4376 he
= gethostbyname("localhost");
4377 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
4380 if(he
->h_length
!= 4)
4382 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
4386 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
4387 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
4388 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
4389 he
->h_addr_list
[0][3]);
4392 if(strcmp(name
, "localhost") == 0)
4394 skip("hostname seems to be \"localhost\", skipping test.\n");
4399 he
= gethostbyname(name
);
4400 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
4403 if(he
->h_length
!= 4)
4405 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
4409 if (he
->h_addr_list
[0][0] == 127)
4411 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
4412 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
4413 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
4414 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
4418 gethostbyname("nonexistent.winehq.org");
4419 /* Don't check for the return value, as some braindead ISPs will kindly
4420 * resolve nonexistent host names to addresses of the ISP's spam pages. */
4423 static void test_gethostname(void)
4429 WSASetLastError(0xdeadbeef);
4430 ret
= gethostname(NULL
, 256);
4431 ok(ret
== -1, "gethostname() returned %d\n", ret
);
4432 ok(WSAGetLastError() == WSAEFAULT
, "gethostname with null buffer "
4433 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT
);
4435 ret
= gethostname(name
, sizeof(name
));
4436 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
4437 he
= gethostbyname(name
);
4438 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, WSAGetLastError());
4441 WSASetLastError(0xdeadbeef);
4442 strcpy(name
, "deadbeef");
4443 ret
= gethostname(name
, len
);
4444 ok(ret
== -1, "gethostname() returned %d\n", ret
);
4445 ok(!strcmp(name
, "deadbeef"), "name changed unexpected!\n");
4446 ok(WSAGetLastError() == WSAEFAULT
, "gethostname with insufficient length "
4447 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT
);
4450 ret
= gethostname(name
, len
);
4451 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
4452 he
= gethostbyname(name
);
4453 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, WSAGetLastError());
4456 static void test_inet_addr(void)
4460 addr
= inet_addr(NULL
);
4461 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
4464 static void test_addr_to_print(void)
4470 struct in6_addr in6
;
4472 u_long addr0_Num
= 0x00000000;
4473 PCSTR addr0_Str
= "0.0.0.0";
4474 u_long addr1_Num
= 0x20201015;
4475 PCSTR addr1_Str
= "21.16.32.32";
4476 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
4477 PCSTR addr2_Str
= "::fffe:cc98:bd74";
4478 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
4479 PCSTR addr3_Str
= "2030:a4b1::";
4480 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
4481 PCSTR addr4_Str
= "::204.152.189.116";
4483 /* Test IPv4 addresses */
4484 in
.s_addr
= addr0_Num
;
4486 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
4487 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
4488 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
4490 /* Test that inet_ntoa and inet_ntop return the same value */
4491 in
.S_un
.S_addr
= addr1_Num
;
4492 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
4493 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
4494 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
4496 /* InetNtop became available in Vista and Win2008 */
4499 win_skip("InetNtop not present, not executing tests\n");
4503 /* Second part of test */
4504 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
4505 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
4506 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
4508 /* Test invalid parm conditions */
4509 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
4510 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4511 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
4513 /* Test Null destination */
4515 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
4516 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4517 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4518 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4520 /* Test zero length passed */
4523 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
4524 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4525 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4526 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4528 /* Test length one shorter than the address length */
4531 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
4532 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4533 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4534 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4536 /* Test longer length is ok */
4539 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
4540 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
4541 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
4543 /* Test the IPv6 addresses */
4545 /* Test an zero prefixed IPV6 address */
4546 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
4547 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
4548 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
4549 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
4551 /* Test an zero suffixed IPV6 address */
4552 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
4553 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
4554 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
4555 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
4557 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
4558 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
4559 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
4560 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
4561 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
4563 /* Test invalid parm conditions */
4564 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
4566 /* Test Null destination */
4568 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
4569 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4570 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4571 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4573 /* Test zero length passed */
4576 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
4577 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4578 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4579 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4581 /* Test length one shorter than the address length */
4584 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
4585 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
4586 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
4587 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
4589 /* Test longer length is ok */
4592 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
4593 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
4595 static void test_inet_pton(void)
4601 const char *printable
, *collapsed
, *raw_data
;
4603 {AF_UNSPEC
, -1, WSAEFAULT
, /* Test 0 */
4605 {AF_INET
, -1, WSAEFAULT
,
4607 {AF_INET6
, -1, WSAEFAULT
,
4609 {AF_UNSPEC
, -1, WSAEAFNOSUPPORT
,
4610 "127.0.0.1", NULL
, NULL
},
4612 "127.0.0.1", "127.0.0.1",
4613 "\x7f\x00\x00\x01"},
4615 "127.0.0.1", "127.0.0.1", NULL
},
4617 "::1/128", NULL
, NULL
},
4619 "::1/128", NULL
, NULL
},
4620 {AF_UNSPEC
, -1, WSAEAFNOSUPPORT
,
4621 "broken", NULL
, NULL
},
4623 "broken", NULL
, NULL
},
4624 {AF_INET6
, 0, 0, /* Test 10 */
4625 "broken", NULL
, NULL
},
4626 {AF_UNSPEC
, -1, WSAEAFNOSUPPORT
,
4627 "177.32.45.20", NULL
, NULL
},
4629 "177.32.45.20", "177.32.45.20",
4630 "\xb1\x20\x2d\x14"},
4632 "177.32.45.20", NULL
, NULL
},
4634 "2607:f0d0:1002:51::4", NULL
, NULL
},
4636 "2607:f0d0:1002:51::4", "2607:f0d0:1002:51::4",
4637 "\x26\x07\xf0\xd0\x10\x02\x00\x51\x00\x00\x00\x00\x00\x00\x00\x04"},
4639 "::177.32.45.20", NULL
, NULL
},
4641 "::177.32.45.20", "::177.32.45.20",
4642 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb1\x20\x2d\x14"},
4644 "fe80::0202:b3ff:fe1e:8329", NULL
, NULL
},
4646 "fe80::0202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329",
4647 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"},
4648 {AF_INET6
, 1, 0, /* Test 20 */
4649 "fe80::202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329",
4650 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"},
4656 "a.b.c", NULL
, NULL
},
4658 "a.b.c.d", NULL
, NULL
},
4660 "2001:cdba:0000:0000:0000:0000:3257:9652", "2001:cdba::3257:9652",
4661 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"},
4663 "2001:cdba::3257:9652", "2001:cdba::3257:9652",
4664 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"},
4666 "2001:cdba:0:0:0:0:3257:9652", "2001:cdba::3257:9652",
4667 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"}
4671 char buffer
[64],str
[64];
4674 /* InetNtop and InetPton became available in Vista and Win2008 */
4675 if (!pInetNtop
|| !pInetPton
)
4677 win_skip("InetNtop and/or InetPton not present, not executing tests\n");
4681 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
4683 WSASetLastError(0xdeadbeef);
4684 ret
= pInetPton(tests
[i
].family
, (char *)tests
[i
].printable
, buffer
);
4685 ok (ret
== tests
[i
].ret
, "Test [%d]: Expected %d, got %d\n", i
, tests
[i
].ret
, ret
);
4686 if (tests
[i
].ret
== -1)
4688 err
= WSAGetLastError();
4689 ok (tests
[i
].err
== err
, "Test [%d]: Expected 0x%x, got 0x%x\n", i
, tests
[i
].err
, err
);
4691 if (tests
[i
].ret
!= 1) continue;
4692 ok (memcmp(buffer
, tests
[i
].raw_data
,
4693 tests
[i
].family
== AF_INET
? sizeof(struct in_addr
) : sizeof(struct in6_addr
)) == 0,
4694 "Test [%d]: Expected binary data differs\n", i
);
4696 /* Test the result from Pton with Ntop */
4697 strcpy (str
, "deadbeef");
4698 ptr
= pInetNtop(tests
[i
].family
, buffer
, str
, sizeof(str
));
4699 ok (ptr
!= NULL
, "Test [%d]: Failed with NULL\n", i
);
4700 ok (ptr
== str
, "Test [%d]: Pointers differ (%p != %p)\n", i
, ptr
, str
);
4702 ok (strcmp(ptr
, tests
[i
].collapsed
) == 0, "Test [%d]: Expected '%s', got '%s'\n",
4703 i
, tests
[i
].collapsed
, ptr
);
4707 static void test_ioctlsocket(void)
4709 SOCKET sock
, src
, dst
;
4710 struct tcp_keepalive kalive
;
4712 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
4718 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4719 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
4720 if(sock
== INVALID_SOCKET
)
4722 skip("Can't continue without a socket.\n");
4726 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
4728 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
4729 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
4730 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
4731 ret
= WSAGetLastError();
4732 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
4735 /* A fresh and not connected socket has no urgent data, this test shows
4736 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
4738 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
4739 ok(ret
!= SOCKET_ERROR
, "ioctlsocket failed unexpectedly\n");
4740 ok(arg
, "SIOCATMARK expected a non-zero value\n");
4742 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
4744 ret
= setsockopt(sock
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&optval
, sizeof(optval
));
4745 ok(ret
!= SOCKET_ERROR
, "setsockopt failed unexpectedly\n");
4747 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
4748 ok(ret
!= SOCKET_ERROR
, "ioctlsocket failed unexpectedly\n");
4749 ok(arg
, "SIOCATMARK expected a non-zero value\n");
4751 /* disable SO_OOBINLINE and get the same old behavior */
4753 ret
= setsockopt(sock
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&optval
, sizeof(optval
));
4754 ok(ret
!= SOCKET_ERROR
, "setsockopt failed unexpectedly\n");
4756 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
4757 ok(ret
!= SOCKET_ERROR
, "ioctlsocket failed unexpectedly\n");
4758 ok(arg
, "SIOCATMARK expected a non-zero value\n");
4760 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &arg
, 0, NULL
, 0, &arg
, NULL
, NULL
);
4761 ok(ret
== SOCKET_ERROR
, "WSAIoctl succeeded unexpectedly\n");
4762 ret
= WSAGetLastError();
4763 ok(ret
== WSAEFAULT
|| broken(ret
== WSAEINVAL
), "expected WSAEFAULT, got %d instead\n", ret
);
4765 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, NULL
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4766 ok(ret
== SOCKET_ERROR
, "WSAIoctl succeeded unexpectedly\n");
4767 ret
= WSAGetLastError();
4768 ok(ret
== WSAEFAULT
|| broken(ret
== WSAEINVAL
), "expected WSAEFAULT, got %d instead\n", ret
);
4770 /* broken used to catch W95, W98, NT4 */
4771 make_keepalive(kalive
, 0, 0, 0);
4772 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4773 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4775 make_keepalive(kalive
, 1, 0, 0);
4776 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4777 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4779 make_keepalive(kalive
, 1, 1000, 1000);
4780 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4781 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4783 make_keepalive(kalive
, 1, 10000, 10000);
4784 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4785 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4787 make_keepalive(kalive
, 1, 100, 100);
4788 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4789 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4791 make_keepalive(kalive
, 0, 100, 100);
4792 ret
= WSAIoctl(sock
, SIO_KEEPALIVE_VALS
, &kalive
, sizeof(struct tcp_keepalive
), NULL
, 0, &arg
, NULL
, NULL
);
4793 ok(ret
== 0 || broken(ret
== SOCKET_ERROR
), "WSAIoctl failed unexpectedly\n");
4797 if (tcp_socketpair(&src
, &dst
) != 0)
4799 ok(0, "creating socket pair failed, skipping test\n");
4803 /* test FIONREAD on TCP sockets */
4804 optval
= 0xdeadbeef;
4805 ret
= WSAIoctl(dst
, FIONREAD
, NULL
, 0, &optval
, sizeof(optval
), &arg
, NULL
, NULL
);
4806 ok(ret
== 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
4807 ok(optval
== 0, "FIONREAD should have returned 0 bytes, got %d instead\n", optval
);
4809 optval
= 0xdeadbeef;
4810 ok(send(src
, "TEST", 4, 0) == 4, "failed to send test data\n");
4812 ret
= WSAIoctl(dst
, FIONREAD
, NULL
, 0, &optval
, sizeof(optval
), &arg
, NULL
, NULL
);
4813 ok(ret
== 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
4814 ok(optval
== 4, "FIONREAD should have returned 4 bytes, got %d instead\n", optval
);
4816 /* trying to read from an OOB inlined socket with MSG_OOB results in WSAEINVAL */
4817 set_blocking(dst
, FALSE
);
4819 SetLastError(0xdeadbeef);
4820 ret
= recv(dst
, &data
, 1, i
);
4821 ok(ret
== SOCKET_ERROR
, "expected -1, got %d\n", ret
);
4822 ret
= GetLastError();
4823 ok(ret
== WSAEWOULDBLOCK
, "expected 10035, got %d\n", ret
);
4824 bufs
.len
= sizeof(char);
4826 ret
= WSARecv(dst
, &bufs
, 1, &bytes_rec
, &i
, NULL
, NULL
);
4827 ok(ret
== SOCKET_ERROR
, "expected -1, got %d\n", ret
);
4828 ret
= GetLastError();
4829 ok(ret
== WSAEWOULDBLOCK
, "expected 10035, got %d\n", ret
);
4831 ret
= setsockopt(dst
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&optval
, sizeof(optval
));
4832 ok(ret
!= SOCKET_ERROR
, "setsockopt failed unexpectedly\n");
4834 SetLastError(0xdeadbeef);
4835 ret
= recv(dst
, &data
, 1, i
);
4836 if (ret
== SOCKET_ERROR
)
4838 ret
= GetLastError();
4839 ok(ret
== WSAEINVAL
, "expected 10022, got %d\n", ret
);
4840 bufs
.len
= sizeof(char);
4842 ret
= WSARecv(dst
, &bufs
, 1, &bytes_rec
, &i
, NULL
, NULL
);
4843 ok(ret
== SOCKET_ERROR
, "expected -1, got %d\n", ret
);
4844 ret
= GetLastError();
4845 ok(ret
== WSAEINVAL
, "expected 10022, got %d\n", ret
);
4847 else ok(broken(ret
== 1) /* <= NT4 */, "expected error, got 1\n");
4850 optval
= 0xdeadbeef;
4851 ret
= WSAIoctl(dst
, FIONREAD
, NULL
, 0, &optval
, sizeof(optval
), &arg
, NULL
, NULL
);
4852 ok(ret
== SOCKET_ERROR
, "WSAIoctl succeeded unexpectedly\n");
4853 ok(optval
== 0xdeadbeef, "FIONREAD should not have changed last error, got %d instead\n", optval
);
4857 static BOOL drain_pause
= FALSE
;
4858 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
4861 SOCKET sock
= *(SOCKET
*)arg
;
4864 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
4868 if (WSAGetLastError() == WSAEWOULDBLOCK
)
4872 FD_SET(sock
, &readset
);
4873 select(sock
+1, &readset
, NULL
, NULL
, NULL
);
4884 static void test_send(void)
4886 SOCKET src
= INVALID_SOCKET
;
4887 SOCKET dst
= INVALID_SOCKET
;
4888 HANDLE hThread
= NULL
;
4889 const int buflen
= 1024*1024;
4890 char *buffer
= NULL
;
4891 int ret
, i
, zero
= 0;
4895 DWORD id
, bytes_sent
, dwRet
;
4897 memset(&ov
, 0, sizeof(ov
));
4899 if (tcp_socketpair(&src
, &dst
) != 0)
4901 ok(0, "creating socket pair failed, skipping test\n");
4905 set_blocking(dst
, FALSE
);
4906 /* force disable buffering so we can get a pending overlapped request */
4907 ret
= setsockopt(dst
, SOL_SOCKET
, SO_SNDBUF
, (char *) &zero
, sizeof(zero
));
4908 ok(!ret
, "setsockopt SO_SNDBUF failed: %d - %d\n", ret
, GetLastError());
4910 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
4911 if (hThread
== NULL
)
4913 ok(0, "CreateThread failed, error %d\n", GetLastError());
4917 buffer
= HeapAlloc(GetProcessHeap(), 0, buflen
);
4920 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
4924 /* fill the buffer with some nonsense */
4925 for (i
= 0; i
< buflen
; ++i
)
4927 buffer
[i
] = (char) i
;
4930 ret
= send(src
, buffer
, buflen
, 0);
4932 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
4934 ok(0, "send failed, error %d\n", WSAGetLastError());
4939 ov
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
4940 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
4945 WSASetLastError(12345);
4946 ret
= WSASend(dst
, &buf
, 1, &bytes_sent
, 0, &ov
, NULL
);
4947 ok((ret
== SOCKET_ERROR
&& WSAGetLastError() == ERROR_IO_PENDING
) || broken(bytes_sent
== buflen
),
4948 "Failed to start overlapped send %d - %d - %d/%d\n", ret
, WSAGetLastError(), bytes_sent
, buflen
);
4950 /* don't check for completion yet, we may need to drain the buffer while still sending */
4951 set_blocking(src
, FALSE
);
4952 for (i
= 0; i
< buflen
; ++i
)
4956 ret
= recv(src
, buffer
, 1, 0);
4957 while (ret
== SOCKET_ERROR
&& GetLastError() == WSAEWOULDBLOCK
&& j
< 100)
4961 ret
= recv(src
, buffer
, 1, 0);
4964 ok(ret
== 1, "Failed to receive data %d - %d (got %d/%d)\n", ret
, GetLastError(), i
, buflen
);
4968 ok(buffer
[0] == (char) i
, "Received bad data at position %d\n", i
);
4971 dwRet
= WaitForSingleObject(ov
.hEvent
, 1000);
4972 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
4973 if (dwRet
== WAIT_OBJECT_0
)
4975 bret
= GetOverlappedResult((HANDLE
)dst
, &ov
, &bytes_sent
, FALSE
);
4976 ok((bret
&& bytes_sent
== buflen
) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
4977 "Got %d instead of %d (%d - %d)\n", bytes_sent
, buflen
, bret
, GetLastError());
4980 WSASetLastError(12345);
4981 ret
= WSASend(INVALID_SOCKET
, &buf
, 1, NULL
, 0, &ov
, NULL
);
4982 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAENOTSOCK
,
4983 "WSASend failed %d - %d\n", ret
, WSAGetLastError());
4985 WSASetLastError(12345);
4986 ret
= WSASend(dst
, &buf
, 1, NULL
, 0, &ov
, NULL
);
4987 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == ERROR_IO_PENDING
,
4988 "Failed to start overlapped send %d - %d\n", ret
, WSAGetLastError());
4991 if (src
!= INVALID_SOCKET
)
4993 if (dst
!= INVALID_SOCKET
)
4995 if (hThread
!= NULL
)
4997 dwRet
= WaitForSingleObject(hThread
, 500);
4998 ok(dwRet
== WAIT_OBJECT_0
, "failed to wait for thread termination: %d\n", GetLastError());
4999 CloseHandle(hThread
);
5002 CloseHandle(ov
.hEvent
);
5003 HeapFree(GetProcessHeap(), 0, buffer
);
5006 typedef struct async_message
5010 struct async_message
*next
;
5013 static struct async_message
*messages_received
;
5015 #define WM_SOCKET (WM_USER+100)
5016 static LRESULT CALLBACK
ws2_test_WndProc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
5018 struct async_message
*message
;
5023 message
= HeapAlloc(GetProcessHeap(), 0, sizeof(*message
));
5024 message
->socket
= (SOCKET
) wparam
;
5025 message
->lparam
= lparam
;
5026 message
->next
= NULL
;
5028 if (messages_received
)
5030 struct async_message
*last
= messages_received
;
5031 while (last
->next
) last
= last
->next
;
5032 last
->next
= message
;
5035 messages_received
= message
;
5039 return DefWindowProcA(hwnd
, msg
, wparam
, lparam
);
5042 static void get_event_details(int event
, int *bit
, char *name
)
5047 if (bit
) *bit
= FD_ACCEPT_BIT
;
5048 if (name
) strcpy(name
, "FD_ACCEPT");
5051 if (bit
) *bit
= FD_CONNECT_BIT
;
5052 if (name
) strcpy(name
, "FD_CONNECT");
5055 if (bit
) *bit
= FD_READ_BIT
;
5056 if (name
) strcpy(name
, "FD_READ");
5059 if (bit
) *bit
= FD_OOB_BIT
;
5060 if (name
) strcpy(name
, "FD_OOB");
5063 if (bit
) *bit
= FD_WRITE_BIT
;
5064 if (name
) strcpy(name
, "FD_WRITE");
5067 if (bit
) *bit
= FD_CLOSE_BIT
;
5068 if (name
) strcpy(name
, "FD_CLOSE");
5072 if (name
) sprintf(name
, "bad%x", event
);
5076 static const char *dbgstr_event_seq(const LPARAM
*seq
)
5078 static char message
[1024];
5086 get_event_details(WSAGETSELECTEVENT(*seq
), NULL
, name
);
5087 len
+= sprintf(message
+ len
, "%s(%d) ", name
, WSAGETSELECTERROR(*seq
));
5091 strcpy( message
+ len
, "]" );
5095 static char *dbgstr_event_seq_result(SOCKET s
, WSANETWORKEVENTS
*netEvents
)
5097 static char message
[1024];
5098 struct async_message
*curr
= messages_received
;
5099 int index
, error
, bit
= 0;
5109 if (bit
>= FD_MAX_EVENTS
) break;
5110 if ( !(netEvents
->lNetworkEvents
& (1 << bit
)) )
5115 get_event_details(1 << bit
, &index
, name
);
5116 error
= netEvents
->iErrorCode
[index
];
5122 if (curr
->socket
!= s
)
5127 get_event_details(WSAGETSELECTEVENT(curr
->lparam
), NULL
, name
);
5128 error
= WSAGETSELECTERROR(curr
->lparam
);
5132 len
+= sprintf(message
+ len
, "%s(%d) ", name
, error
);
5135 strcpy( message
+ len
, "]" );
5139 static void flush_events(SOCKET s
, HANDLE hEvent
)
5141 WSANETWORKEVENTS netEvents
;
5142 struct async_message
*prev
= NULL
, *curr
= messages_received
;
5146 if (hEvent
!= INVALID_HANDLE_VALUE
)
5148 dwRet
= WaitForSingleObject(hEvent
, 100);
5149 if (dwRet
== WAIT_OBJECT_0
)
5151 ret
= WSAEnumNetworkEvents(s
, hEvent
, &netEvents
);
5153 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
5160 if (curr
->socket
== s
)
5162 if (prev
) prev
->next
= curr
->next
;
5163 else messages_received
= curr
->next
;
5165 HeapFree(GetProcessHeap(), 0, curr
);
5167 if (prev
) curr
= prev
->next
;
5168 else curr
= messages_received
;
5179 static int match_event_sequence(SOCKET s
, WSANETWORKEVENTS
*netEvents
, const LPARAM
*seq
)
5181 int event
, index
, error
, events
;
5182 struct async_message
*curr
;
5186 events
= netEvents
->lNetworkEvents
;
5189 event
= WSAGETSELECTEVENT(*seq
);
5190 error
= WSAGETSELECTERROR(*seq
);
5191 get_event_details(event
, &index
, NULL
);
5193 if (!(events
& event
) && index
!= -1)
5195 if (events
& event
&& index
!= -1)
5197 if (netEvents
->iErrorCode
[index
] != error
)
5208 curr
= messages_received
;
5211 if (curr
->socket
== s
)
5213 if (!*seq
) return 0;
5214 if (*seq
!= curr
->lparam
) return 0;
5225 /* checks for a sequence of events, (order only checked if window is used) */
5226 static void ok_event_sequence(SOCKET s
, HANDLE hEvent
, const LPARAM
*seq
, const LPARAM
**broken_seqs
, int completelyBroken
)
5229 WSANETWORKEVENTS events
, *netEvents
= NULL
;
5233 if (hEvent
!= INVALID_HANDLE_VALUE
)
5235 netEvents
= &events
;
5237 dwRet
= WaitForSingleObject(hEvent
, 200);
5238 if (dwRet
== WAIT_OBJECT_0
)
5240 ret
= WSAEnumNetworkEvents(s
, hEvent
, netEvents
);
5243 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
5248 memset(netEvents
, 0, sizeof(*netEvents
));
5253 /* Run the message loop a little */
5254 while (PeekMessageA( &msg
, 0, 0, 0, PM_REMOVE
))
5256 DispatchMessageA(&msg
);
5260 if (match_event_sequence(s
, netEvents
, seq
))
5262 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq
));
5263 flush_events(s
, hEvent
);
5269 for (; *broken_seqs
; broken_seqs
++)
5271 if (match_event_sequence(s
, netEvents
, *broken_seqs
))
5273 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s
, netEvents
), dbgstr_event_seq(seq
));
5274 flush_events(s
, hEvent
);
5280 winetest_ok(broken(completelyBroken
), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq
),
5281 dbgstr_event_seq_result(s
, netEvents
));
5282 flush_events(s
, hEvent
);
5285 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
5287 static void test_events(int useMessages
)
5289 SOCKET server
= INVALID_SOCKET
;
5290 SOCKET src
= INVALID_SOCKET
, src2
= INVALID_SOCKET
;
5291 SOCKET dst
= INVALID_SOCKET
, dst2
= INVALID_SOCKET
;
5292 struct sockaddr_in addr
;
5293 HANDLE hThread
= NULL
;
5294 HANDLE hEvent
= INVALID_HANDLE_VALUE
, hEvent2
= INVALID_HANDLE_VALUE
;
5295 WNDCLASSEXA wndclass
;
5297 char *buffer
= NULL
;
5298 int bufferSize
= 1024*1024;
5302 DWORD bytesReturned
;
5308 static char szClassName
[] = "wstestclass";
5309 const LPARAM
*broken_seq
[3];
5310 static const LPARAM empty_seq
[] = { 0 };
5311 static const LPARAM close_seq
[] = { WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
5312 static const LPARAM write_seq
[] = { WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
5313 static const LPARAM read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
5314 static const LPARAM oob_seq
[] = { WSAMAKESELECTREPLY(FD_OOB
, 0), 0 };
5315 static const LPARAM connect_seq
[] = { WSAMAKESELECTREPLY(FD_CONNECT
, 0),
5316 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
5317 static const LPARAM read_read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
5318 WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
5319 static const LPARAM read_write_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
5320 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
5321 static const LPARAM read_close_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
5322 WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
5324 memset(&ov
, 0, sizeof(ov
));
5325 memset(&ov2
, 0, sizeof(ov2
));
5327 /* don't use socketpair, we want connection event */
5328 src
= socket(AF_INET
, SOCK_STREAM
, 0);
5329 if (src
== INVALID_SOCKET
)
5331 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5335 ret
= set_blocking(src
, TRUE
);
5336 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5338 src2
= socket(AF_INET
, SOCK_STREAM
, 0);
5339 if (src2
== INVALID_SOCKET
)
5341 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5345 ret
= set_blocking(src2
, TRUE
);
5346 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5349 if (getsockopt(src
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&bret
, &len
) == SOCKET_ERROR
)
5351 ok(0, "failed to get oobinline status, %d\n", GetLastError());
5354 ok(bret
== FALSE
, "OOB not inline\n");
5358 trace("Event test using messages\n");
5360 wndclass
.cbSize
= sizeof(wndclass
);
5361 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
5362 wndclass
.lpfnWndProc
= ws2_test_WndProc
;
5363 wndclass
.cbClsExtra
= 0;
5364 wndclass
.cbWndExtra
= 0;
5365 wndclass
.hInstance
= GetModuleHandleA(NULL
);
5366 wndclass
.hIcon
= LoadIconA(NULL
, (LPCSTR
)IDI_APPLICATION
);
5367 wndclass
.hIconSm
= LoadIconA(NULL
, (LPCSTR
)IDI_APPLICATION
);
5368 wndclass
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)IDC_ARROW
);
5369 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
5370 wndclass
.lpszClassName
= szClassName
;
5371 wndclass
.lpszMenuName
= NULL
;
5372 RegisterClassExA(&wndclass
);
5374 hWnd
= CreateWindowA(szClassName
, "WS2Test", WS_OVERLAPPEDWINDOW
,
5375 0, 0, 500, 500, NULL
, NULL
, GetModuleHandleA(NULL
), NULL
);
5378 ok(0, "failed to create window: %d\n", GetLastError());
5382 ret
= WSAAsyncSelect(src
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
5385 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5389 ok(set_blocking(src
, TRUE
) == SOCKET_ERROR
, "set_blocking should failed!\n");
5390 ok(WSAGetLastError() == WSAEINVAL
, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5392 ret
= WSAAsyncSelect(src2
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
5395 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5399 ok(set_blocking(src2
, TRUE
) == SOCKET_ERROR
, "set_blocking should failed!\n");
5400 ok(WSAGetLastError() == WSAEINVAL
, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5404 trace("Event test using events\n");
5406 hEvent
= WSACreateEvent();
5407 if (hEvent
== INVALID_HANDLE_VALUE
)
5409 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
5413 hEvent2
= WSACreateEvent();
5414 if (hEvent2
== INVALID_HANDLE_VALUE
)
5416 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
5420 ret
= WSAEventSelect(src
, hEvent
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
5423 ok(0, "WSAEventSelect failed, error %d\n", ret
);
5427 ok(set_blocking(src
, TRUE
) == SOCKET_ERROR
, "set_blocking should failed!\n");
5428 ok(WSAGetLastError() == WSAEINVAL
, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5430 ret
= WSAEventSelect(src2
, hEvent2
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
5433 ok(0, "WSAEventSelect failed, error %d\n", ret
);
5437 ok(set_blocking(src2
, TRUE
) == SOCKET_ERROR
, "set_blocking should failed!\n");
5438 ok(WSAGetLastError() == WSAEINVAL
, "expect WSAEINVAL, returned %x\n", WSAGetLastError());
5441 server
= socket(AF_INET
, SOCK_STREAM
, 0);
5442 if (server
== INVALID_SOCKET
)
5444 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5448 memset(&addr
, 0, sizeof(addr
));
5449 addr
.sin_family
= AF_INET
;
5450 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
5451 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
5454 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5459 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
5462 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5466 ret
= listen(server
, 2);
5469 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5473 ret
= connect(src
, (struct sockaddr
*)&addr
, sizeof(addr
));
5474 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
5476 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5480 ret
= connect(src2
, (struct sockaddr
*)&addr
, sizeof(addr
));
5481 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
5483 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5488 dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
5489 if (dst
== INVALID_SOCKET
)
5491 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5496 dst2
= accept(server
, (struct sockaddr
*)&addr
, &len
);
5497 if (dst2
== INVALID_SOCKET
)
5499 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
5503 closesocket(server
);
5504 server
= INVALID_SOCKET
;
5506 /* On Windows it seems when a non-blocking socket sends to a
5507 blocking socket on the same host, the send() is BLOCKING,
5508 so make both sockets non-blocking. src is already non-blocking
5509 from the async select */
5511 if (set_blocking(dst
, FALSE
))
5513 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
5517 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
5520 ok(0, "could not allocate memory for test\n");
5524 ov
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
5525 if (ov
.hEvent
== NULL
)
5527 ok(0, "could not create event object, errno = %d\n", GetLastError());
5531 ov2
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
5532 if (ov2
.hEvent
== NULL
)
5534 ok(0, "could not create event object, errno = %d\n", GetLastError());
5538 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
5539 ok_event_seq(src
, hEvent
, connect_seq
, NULL
, 1);
5540 ok_event_seq(src2
, hEvent2
, connect_seq
, NULL
, 1);
5541 /* broken on all windows - FD_CONNECT error is garbage */
5543 /* Test simple send/recv */
5544 SetLastError(0xdeadbeef);
5545 ret
= send(dst
, buffer
, 100, 0);
5546 ok(ret
== 100, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5547 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
5548 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5550 SetLastError(0xdeadbeef);
5551 ret
= recv(src
, buffer
, 1, MSG_PEEK
);
5552 ok(ret
== 1, "Failed to peek at recv buffer %d err %d\n", ret
, GetLastError());
5553 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
5554 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5556 SetLastError(0xdeadbeef);
5557 ret
= recv(src
, buffer
, 50, 0);
5558 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
5559 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
5560 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5562 ret
= recv(src
, buffer
, 50, 0);
5563 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
5564 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5566 /* fun fact - events are re-enabled even on failure, but only for messages */
5567 ret
= send(dst
, "1", 1, 0);
5568 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5569 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5571 ret
= recv(src
, buffer
, -1, 0);
5572 ok(ret
== SOCKET_ERROR
&& (GetLastError() == WSAEFAULT
|| GetLastError() == WSAENOBUFS
),
5573 "Failed to recv buffer %d err %d\n", ret
, GetLastError());
5576 broken_seq
[0] = empty_seq
; /* win9x */
5577 broken_seq
[1] = NULL
;
5578 todo_wine
ok_event_seq(src
, hEvent
, read_seq
, broken_seq
, 0);
5581 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5583 SetLastError(0xdeadbeef);
5584 ret
= recv(src
, buffer
, 1, 0);
5585 ok(ret
== 1, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
5586 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
5587 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5589 /* Interaction with overlapped */
5590 bufs
.len
= sizeof(char);
5592 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
5593 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
5594 "WSARecv failed - %d error %d\n", ret
, GetLastError());
5596 bufs
.len
= sizeof(char);
5597 bufs
.buf
= buffer
+1;
5598 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov2
, NULL
);
5599 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
5600 "WSARecv failed - %d error %d\n", ret
, GetLastError());
5602 ret
= send(dst
, "12", 2, 0);
5603 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5604 broken_seq
[0] = read_read_seq
; /* win9x */
5605 broken_seq
[1] = NULL
;
5606 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
5608 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
5609 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
5610 if (dwRet
== WAIT_OBJECT_0
)
5612 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
5613 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
5614 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
5615 ok(buffer
[0] == '1', "Got %c instead of 1\n", buffer
[0]);
5618 dwRet
= WaitForSingleObject(ov2
.hEvent
, 100);
5619 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
5620 if (dwRet
== WAIT_OBJECT_0
)
5622 bret
= GetOverlappedResult((HANDLE
)src
, &ov2
, &bytesReturned
, FALSE
);
5623 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
5624 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
5625 ok(buffer
[1] == '2', "Got %c instead of 2\n", buffer
[1]);
5628 SetLastError(0xdeadbeef);
5629 ret
= send(dst
, "1", 1, 0);
5630 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5631 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
5632 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5634 ret
= recv(src
, buffer
, 1, 0);
5635 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5636 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5638 /* Notifications are delivered as soon as possible, blocked only on
5639 * async requests on the same type */
5640 bufs
.len
= sizeof(char);
5642 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
5643 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
5644 "WSARecv failed - %d error %d\n", ret
, GetLastError());
5647 ret
= send(dst
, "1", 1, MSG_OOB
);
5648 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5649 ok_event_seq(src
, hEvent
, oob_seq
, NULL
, 0);
5652 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
5653 ok(dwRet
== WAIT_TIMEOUT
, "OOB message activated read?: %d - %d\n", dwRet
, GetLastError());
5655 ret
= send(dst
, "2", 1, 0);
5656 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5657 broken_seq
[0] = read_seq
; /* win98 */
5658 broken_seq
[1] = NULL
;
5659 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
5661 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
5662 ok(dwRet
== WAIT_OBJECT_0
|| broken(dwRet
== WAIT_TIMEOUT
),
5663 "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
5664 if (dwRet
== WAIT_OBJECT_0
)
5666 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
5667 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
5668 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
5669 ok(buffer
[0] == '2', "Got %c instead of 2\n", buffer
[0]);
5671 else if (dwRet
== WAIT_TIMEOUT
)
5673 /* this happens on win98. We get an FD_READ later on the next test */
5674 CancelIo((HANDLE
) src
);
5678 ret
= recv(src
, buffer
, 1, MSG_OOB
);
5679 todo_wine
ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5680 /* We get OOB notification, but no data on wine */
5681 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5684 /* Flood the send queue */
5685 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
5686 if (hThread
== NULL
)
5688 ok(0, "CreateThread failed, error %d\n", GetLastError());
5692 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
5693 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
5695 /* Now if we send a ton of data and the 'server' does not drain it fast
5696 * enough (set drain_pause to be sure), the socket send buffer will only
5697 * take some of it, and we will get a short write. This will trigger
5698 * another FD_WRITE event as soon as data is sent and more space becomes
5699 * available, but not any earlier. */
5703 ret
= send(src
, buffer
, bufferSize
, 0);
5704 } while (ret
== bufferSize
);
5705 drain_pause
= FALSE
;
5706 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
5708 Sleep(400); /* win9x */
5709 broken_seq
[0] = read_write_seq
;
5710 broken_seq
[1] = NULL
;
5711 ok_event_seq(src
, hEvent
, write_seq
, broken_seq
, 0);
5715 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
5718 /* Test how FD_CLOSE is handled */
5719 ret
= send(dst
, "12", 2, 0);
5720 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5722 /* Wait a little and let the send complete */
5725 dst
= INVALID_SOCKET
;
5728 /* We can never implement this in wine, best we can hope for is
5729 sending FD_CLOSE after the reads complete */
5730 broken_seq
[0] = read_seq
; /* win9x */
5731 broken_seq
[1] = NULL
;
5732 todo_wine
ok_event_seq(src
, hEvent
, read_close_seq
, broken_seq
, 0);
5734 ret
= recv(src
, buffer
, 1, 0);
5735 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5736 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
5738 ret
= recv(src
, buffer
, 1, 0);
5739 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5740 /* want it? it's here, but you can't have it */
5741 broken_seq
[0] = close_seq
; /* win9x */
5742 broken_seq
[1] = NULL
;
5743 todo_wine
ok_event_seq(src
, hEvent
, empty_seq
, /* wine sends FD_CLOSE here */
5746 /* Test how FD_CLOSE is handled */
5747 ret
= send(dst2
, "12", 2, 0);
5748 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
5751 shutdown(dst2
, SD_SEND
);
5754 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
5755 regressions, don't mark them as todo_wine, and mark windows as broken */
5756 broken_seq
[0] = read_close_seq
;
5757 broken_seq
[1] = close_seq
;
5758 broken_seq
[2] = NULL
;
5759 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
5761 ret
= recv(src2
, buffer
, 1, 0);
5762 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5763 broken_seq
[0] = close_seq
; /* win98 */
5764 broken_seq
[1] = NULL
;
5765 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
5767 ret
= recv(src2
, buffer
, 1, 0);
5768 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
5769 broken_seq
[0] = empty_seq
;
5770 broken_seq
[1] = NULL
;
5771 ok_event_seq(src2
, hEvent2
, close_seq
, broken_seq
, 0);
5773 ret
= send(src2
, "1", 1, 0);
5774 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
5775 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
5777 ret
= send(src2
, "1", 1, 0);
5778 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
5779 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
5783 ret
= WSAAsyncSelect(src
, hWnd
, WM_SOCKET
, 0);
5786 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5790 ret
= set_blocking(src
, TRUE
);
5791 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5793 ret
= WSAAsyncSelect(src2
, hWnd
, WM_SOCKET
, 0);
5796 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5800 ret
= set_blocking(src2
, TRUE
);
5801 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5805 ret
= WSAEventSelect(src
, hEvent2
, 0);
5808 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5812 ret
= set_blocking(src
, TRUE
);
5813 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5815 ret
= WSAEventSelect(src2
, hEvent2
, 0);
5818 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
5822 ret
= set_blocking(src2
, TRUE
);
5823 ok(!ret
, "set_blocking failed, error %d\n", WSAGetLastError());
5827 if (src
!= INVALID_SOCKET
)
5829 flush_events(src
, hEvent
);
5832 if (src2
!= INVALID_SOCKET
)
5834 flush_events(src2
, hEvent2
);
5837 HeapFree(GetProcessHeap(), 0, buffer
);
5838 if (server
!= INVALID_SOCKET
)
5839 closesocket(server
);
5840 if (dst
!= INVALID_SOCKET
)
5842 if (dst2
!= INVALID_SOCKET
)
5844 if (hThread
!= NULL
)
5845 CloseHandle(hThread
);
5847 DestroyWindow(hWnd
);
5849 CloseHandle(hEvent
);
5850 if (hEvent2
!= NULL
)
5851 CloseHandle(hEvent2
);
5852 if (ov
.hEvent
!= NULL
)
5853 CloseHandle(ov
.hEvent
);
5854 if (ov2
.hEvent
!= NULL
)
5855 CloseHandle(ov2
.hEvent
);
5858 static void test_ipv6only(void)
5860 SOCKET v4
= INVALID_SOCKET
,
5861 v6
= INVALID_SOCKET
;
5862 struct sockaddr_in sin4
;
5863 struct sockaddr_in6 sin6
;
5866 memset(&sin4
, 0, sizeof(sin4
));
5867 sin4
.sin_family
= AF_INET
;
5868 sin4
.sin_port
= htons(SERVERPORT
);
5870 memset(&sin6
, 0, sizeof(sin6
));
5871 sin6
.sin6_family
= AF_INET6
;
5872 sin6
.sin6_port
= htons(SERVERPORT
);
5874 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
5875 if (v6
== INVALID_SOCKET
) {
5876 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
5877 WSAGetLastError(), WSAEAFNOSUPPORT
);
5880 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
5882 skip("Could not bind IPv6 address (LastError: %d).\n",
5887 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
5888 if (v4
== INVALID_SOCKET
) {
5889 skip("Could not create IPv4 socket (LastError: %d).\n",
5893 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
5894 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
5895 WSAGetLastError(), WSAEADDRINUSE
);
5898 if (v4
!= INVALID_SOCKET
)
5900 if (v6
!= INVALID_SOCKET
)
5904 static void test_WSASendMsg(void)
5907 struct sockaddr_in sendaddr
, sockaddr
;
5908 GUID WSASendMsg_GUID
= WSAID_WSASENDMSG
;
5909 LPFN_WSASENDMSG pWSASendMsg
= NULL
;
5910 char teststr
[12] = "hello world", buffer
[32];
5913 DWORD bytesSent
, err
;
5916 /* FIXME: Missing OVERLAPPED and OVERLAPPED COMPLETION ROUTINE tests */
5918 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
5919 ok(sock
!= INVALID_SOCKET
, "socket() failed\n");
5921 /* Obtain the WSASendMsg function */
5922 WSAIoctl(sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &WSASendMsg_GUID
, sizeof(WSASendMsg_GUID
),
5923 &pWSASendMsg
, sizeof(pWSASendMsg
), &err
, NULL
, NULL
);
5927 win_skip("WSASendMsg is unsupported, some tests will be skipped.\n");
5931 /* fake address for now */
5932 sendaddr
.sin_family
= AF_INET
;
5933 sendaddr
.sin_port
= htons(139);
5934 sendaddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
5936 memset(&msg
, 0, sizeof(msg
));
5937 iovec
[0].buf
= teststr
;
5938 iovec
[0].len
= sizeof(teststr
);
5939 iovec
[1].buf
= teststr
;
5940 iovec
[1].len
= sizeof(teststr
) / 2;
5941 msg
.name
= (struct sockaddr
*) &sendaddr
;
5942 msg
.namelen
= sizeof(sendaddr
);
5943 msg
.lpBuffers
= iovec
;
5944 msg
.dwBufferCount
= 1; /* send only one buffer for now */
5946 WSASetLastError(0xdeadbeef);
5947 ret
= pWSASendMsg(INVALID_SOCKET
, &msg
, 0, NULL
, NULL
, NULL
);
5948 ok(ret
== SOCKET_ERROR
, "WSASendMsg should have failed\n");
5949 err
= WSAGetLastError();
5950 ok(err
== WSAENOTSOCK
, "expected 10038, got %d instead\n", err
);
5952 WSASetLastError(0xdeadbeef);
5953 ret
= pWSASendMsg(sock
, NULL
, 0, NULL
, NULL
, NULL
);
5954 ok(ret
== SOCKET_ERROR
, "WSASendMsg should have failed\n");
5955 err
= WSAGetLastError();
5956 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
5958 WSASetLastError(0xdeadbeef);
5959 ret
= pWSASendMsg(sock
, NULL
, 0, &bytesSent
, NULL
, NULL
);
5960 ok(ret
== SOCKET_ERROR
, "WSASendMsg should have failed\n");
5961 err
= WSAGetLastError();
5962 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
5964 WSASetLastError(0xdeadbeef);
5965 ret
= pWSASendMsg(sock
, &msg
, 0, NULL
, NULL
, NULL
);
5966 ok(ret
== SOCKET_ERROR
, "WSASendMsg should have failed\n");
5967 err
= WSAGetLastError();
5968 ok(err
== WSAEFAULT
, "expected 10014, got %d instead\n", err
);
5972 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
5973 ok(sock
!= INVALID_SOCKET
, "socket() failed\n");
5975 dst
= socket(AF_INET
, SOCK_DGRAM
, 0);
5976 ok(dst
!= INVALID_SOCKET
, "socket() failed\n");
5978 memset(&sockaddr
, 0, sizeof(sockaddr
));
5979 sockaddr
.sin_family
= AF_INET
;
5980 sockaddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
5981 ok(!bind(dst
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
)),
5982 "bind should have worked\n");
5984 /* read address to find out the port number to be used in send */
5985 memset(&sendaddr
, 0, sizeof(sendaddr
));
5986 addrlen
= sizeof(sendaddr
);
5987 ok(!getsockname(dst
, (struct sockaddr
*) &sendaddr
, &addrlen
),
5988 "getsockname should have worked\n");
5989 ok(sendaddr
.sin_port
, "socket port should be != 0\n");
5991 /* ensure the sending socket is not bound */
5992 WSASetLastError(0xdeadbeef);
5993 addrlen
= sizeof(sockaddr
);
5994 ret
= getsockname(sock
, (struct sockaddr
*)&sockaddr
, &addrlen
);
5995 ok(ret
== SOCKET_ERROR
, "getsockname should have failed\n");
5996 err
= WSAGetLastError();
5997 ok(err
== WSAEINVAL
, "expected 10022, got %d instead\n", err
);
5999 set_blocking(sock
, TRUE
);
6002 SetLastError(0xdeadbeef);
6003 ret
= pWSASendMsg(sock
, &msg
, 0, &bytesSent
, NULL
, NULL
);
6004 ok(!ret
, "WSASendMsg should have worked\n");
6005 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
6006 "Expected 0, got %d\n", GetLastError());
6007 ok(bytesSent
== iovec
[0].len
, "incorret bytes sent, expected %d, sent %d\n",
6008 iovec
[0].len
, bytesSent
);
6011 addrlen
= sizeof(sockaddr
);
6012 memset(buffer
, 0, sizeof(buffer
));
6013 SetLastError(0xdeadbeef);
6014 ret
= recvfrom(dst
, buffer
, sizeof(buffer
), 0, (struct sockaddr
*) &sockaddr
, &addrlen
);
6015 ok(ret
== bytesSent
, "got %d, expected %d\n",
6017 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6019 /* A successful call to WSASendMsg must have bound the socket */
6020 addrlen
= sizeof(sockaddr
);
6021 sockaddr
.sin_port
= 0;
6022 sockaddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6023 ret
= getsockname(sock
, (struct sockaddr
*)&sockaddr
, &addrlen
);
6024 ok(!ret
, "getsockname should have worked\n");
6025 ok(sockaddr
.sin_addr
.s_addr
== htonl(INADDR_ANY
), "expected 0.0.0.0, got %s\n",
6026 inet_ntoa(sockaddr
.sin_addr
));
6027 ok(sockaddr
.sin_port
, "sin_port should be != 0\n");
6029 msg
.dwBufferCount
= 2; /* send both buffers */
6032 SetLastError(0xdeadbeef);
6033 ret
= pWSASendMsg(sock
, &msg
, 0, &bytesSent
, NULL
, NULL
);
6034 ok(!ret
, "WSASendMsg should have worked\n");
6035 ok(bytesSent
== iovec
[0].len
+ iovec
[1].len
, "incorret bytes sent, expected %d, sent %d\n",
6036 iovec
[0].len
+ iovec
[1].len
, bytesSent
);
6037 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
6038 "Expected 0, got %d\n", GetLastError());
6041 addrlen
= sizeof(sockaddr
);
6042 memset(buffer
, 0, sizeof(buffer
));
6043 SetLastError(0xdeadbeef);
6044 ret
= recvfrom(dst
, buffer
, sizeof(buffer
), 0, (struct sockaddr
*) &sockaddr
, &addrlen
);
6045 ok(ret
== bytesSent
, "got %d, expected %d\n",
6047 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6052 /* a bad call to WSASendMsg will also bind the socket */
6053 addrlen
= sizeof(sockaddr
);
6054 sockaddr
.sin_port
= 0;
6055 sockaddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6056 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
6057 ok(sock
!= INVALID_SOCKET
, "socket() failed\n");
6058 ok(pWSASendMsg(sock
, &msg
, 0, NULL
, NULL
, NULL
) == SOCKET_ERROR
, "WSASendMsg should have failed\n");
6060 ok(!getsockname(sock
, (struct sockaddr
*)&sockaddr
, &addrlen
), "getsockname should have worked\n");
6061 ok(sockaddr
.sin_addr
.s_addr
== htonl(INADDR_ANY
), "expected 0.0.0.0, got %s\n",
6062 inet_ntoa(sockaddr
.sin_addr
));
6063 ok(sockaddr
.sin_port
, "sin_port should be > 0\n");
6067 /* a bad call without msg parameter will not trigger the auto-bind */
6068 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
6069 ok(sock
!= INVALID_SOCKET
, "socket() failed\n");
6070 ok(pWSASendMsg(sock
, NULL
, 0, NULL
, NULL
, NULL
) == SOCKET_ERROR
, "WSASendMsg should have failed\n");
6071 ok(getsockname(sock
, (struct sockaddr
*)&sockaddr
, &addrlen
), "getsockname should have failed\n");
6072 err
= WSAGetLastError();
6073 ok(err
== WSAEINVAL
, "expected 10022, got %d instead\n", err
);
6076 /* SOCK_STREAM sockets are not supported */
6078 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
6079 ok(sock
!= INVALID_SOCKET
, "socket() failed\n");
6080 SetLastError(0xdeadbeef);
6081 ret
= pWSASendMsg(sock
, &msg
, 0, &bytesSent
, NULL
, NULL
);
6082 ok(ret
== SOCKET_ERROR
, "WSASendMsg should have failed\n");
6083 err
= WSAGetLastError();
6085 ok(err
== WSAEINVAL
, "expected 10014, got %d instead\n", err
);
6089 static void test_WSASendTo(void)
6092 struct sockaddr_in addr
;
6093 char buf
[12] = "hello world";
6098 addr
.sin_family
= AF_INET
;
6099 addr
.sin_port
= htons(139);
6100 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6101 data_buf
.len
= sizeof(buf
);
6104 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
6105 ok(0, "socket() failed error: %d\n", WSAGetLastError());
6109 WSASetLastError(12345);
6110 ret
= WSASendTo(INVALID_SOCKET
, &data_buf
, 1, NULL
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
);
6111 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAENOTSOCK
,
6112 "WSASendTo() failed: %d/%d\n", ret
, WSAGetLastError());
6114 WSASetLastError(12345);
6115 ret
= WSASendTo(s
, &data_buf
, 1, NULL
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
);
6116 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
6117 "WSASendTo() failed: %d/%d\n", ret
, WSAGetLastError());
6119 WSASetLastError(12345);
6120 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
6121 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
6124 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
6125 "a successful call to WSASendTo()\n");
6128 static DWORD WINAPI
recv_thread(LPVOID arg
)
6130 SOCKET sock
= *(SOCKET
*)arg
;
6137 wsa
.len
= sizeof(buffer
);
6138 ov
.hEvent
= WSACreateEvent();
6139 WSARecv(sock
, &wsa
, 1, NULL
, &flags
, &ov
, NULL
);
6141 WaitForSingleObject(ov
.hEvent
, 1000);
6142 WSACloseEvent(ov
.hEvent
);
6146 static void test_WSARecv(void)
6148 SOCKET src
, dest
, server
= INVALID_SOCKET
;
6152 DWORD bytesReturned
, flags
, id
;
6154 struct sockaddr_in addr
;
6160 tcp_socketpair(&src
, &dest
);
6161 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
6163 skip("failed to create sockets\n");
6167 memset(&ov
, 0, sizeof(ov
));
6172 /* Send 4 bytes and receive in two calls of 2 */
6173 SetLastError(0xdeadbeef);
6174 iret
= send(src
, "test", 4, 0);
6175 ok(iret
== 4, "Expected 4, got %d\n", iret
);
6176 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6177 SetLastError(0xdeadbeef);
6178 bytesReturned
= 0xdeadbeef;
6179 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, NULL
, NULL
);
6180 ok(!iret
, "Expected 0, got %d\n", iret
);
6181 ok(bytesReturned
, "Expected 2, got %d\n", bytesReturned
);
6182 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6183 SetLastError(0xdeadbeef);
6184 bytesReturned
= 0xdeadbeef;
6185 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, NULL
, NULL
);
6186 ok(!iret
, "Expected 0, got %d\n", iret
);
6187 ok(bytesReturned
, "Expected 2, got %d\n", bytesReturned
);
6188 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6191 SetLastError(0xdeadbeef);
6192 iret
= send(src
, "test", 4, 0);
6193 ok(iret
== 4, "Expected 4, got %d\n", iret
);
6194 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6195 SetLastError(0xdeadbeef);
6196 bytesReturned
= 0xdeadbeef;
6197 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, NULL
, NULL
);
6198 ok(!iret
, "Expected 0, got %d\n", iret
);
6199 ok(bytesReturned
, "Expected 4, got %d\n", bytesReturned
);
6200 ok(GetLastError() == ERROR_SUCCESS
, "Expected 0, got %d\n", GetLastError());
6202 bufs
.len
= sizeof(buf
);
6204 ov
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
6205 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
6211 iret
= setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
));
6212 ok(!iret
, "Failed to set linger %d\n", GetLastError());
6214 iret
= WSARecv(dest
, &bufs
, 1, NULL
, &flags
, &ov
, NULL
);
6215 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
6217 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
6218 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
6221 src
= INVALID_SOCKET
;
6223 dwret
= WaitForSingleObject(ov
.hEvent
, 1000);
6224 ok(dwret
== WAIT_OBJECT_0
, "Waiting for disconnect event failed with %d + errno %d\n", dwret
, GetLastError());
6226 bret
= GetOverlappedResult((HANDLE
)dest
, &ov
, &bytesReturned
, FALSE
);
6227 todo_wine
ok(!bret
&& (GetLastError() == ERROR_NETNAME_DELETED
|| broken(GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */),
6228 "Did not get disconnect event: %d, error %d\n", bret
, GetLastError());
6229 ok(bytesReturned
== 0, "Bytes received is %d\n", bytesReturned
);
6231 dest
= INVALID_SOCKET
;
6233 src
= WSASocketW(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0, 0);
6234 ok(src
!= INVALID_SOCKET
, "failed to create socket %d\n", WSAGetLastError());
6235 if (src
== INVALID_SOCKET
) goto end
;
6237 server
= WSASocketW(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0, WSA_FLAG_OVERLAPPED
);
6238 ok(server
!= INVALID_SOCKET
, "failed to create socket %d\n", WSAGetLastError());
6239 if (server
== INVALID_SOCKET
) goto end
;
6241 memset(&addr
, 0, sizeof(addr
));
6242 addr
.sin_family
= AF_INET
;
6243 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6244 iret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
6248 iret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
6251 iret
= listen(server
, 1);
6254 iret
= connect(src
, (struct sockaddr
*)&addr
, sizeof(addr
));
6258 dest
= accept(server
, (struct sockaddr
*)&addr
, &len
);
6259 ok(dest
!= INVALID_SOCKET
, "failed to create socket %d\n", WSAGetLastError());
6260 if (dest
== INVALID_SOCKET
) goto end
;
6262 send(src
, "test message", sizeof("test message"), 0);
6263 thread
= CreateThread(NULL
, 0, recv_thread
, &dest
, 0, &id
);
6264 WaitForSingleObject(thread
, 3000);
6265 CloseHandle(thread
);
6268 if (server
!= INVALID_SOCKET
)
6269 closesocket(server
);
6270 if (dest
!= INVALID_SOCKET
)
6272 if (src
!= INVALID_SOCKET
)
6275 WSACloseEvent(ov
.hEvent
);
6278 static void test_GetAddrInfoW(void)
6280 static const WCHAR port
[] = {'8','0',0};
6281 static const WCHAR empty
[] = {0};
6282 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
6283 static const WCHAR nxdomain
[] =
6284 {'n','x','d','o','m','a','i','n','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
6285 static const WCHAR zero
[] = {'0',0};
6287 ADDRINFOW
*result
, *result2
, *p
, hint
;
6289 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
6291 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
6294 memset(&hint
, 0, sizeof(ADDRINFOW
));
6296 result
= (ADDRINFOW
*)0xdeadbeef;
6297 WSASetLastError(0xdeadbeef);
6298 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
6299 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
6300 ok(WSAGetLastError() == WSAHOST_NOT_FOUND
, "expected 11001, got %d\n", WSAGetLastError());
6301 ok(result
== NULL
, "got %p\n", result
);
6304 WSASetLastError(0xdeadbeef);
6305 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
6306 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6307 ok(result
!= NULL
, "GetAddrInfoW failed\n");
6308 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6309 pFreeAddrInfoW(result
);
6312 ret
= pGetAddrInfoW(NULL
, zero
, NULL
, &result
);
6313 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6314 ok(result
!= NULL
, "GetAddrInfoW failed\n");
6317 ret
= pGetAddrInfoW(NULL
, empty
, NULL
, &result2
);
6318 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6319 ok(result2
!= NULL
, "GetAddrInfoW failed\n");
6320 compare_addrinfow(result
, result2
);
6321 pFreeAddrInfoW(result
);
6322 pFreeAddrInfoW(result2
);
6325 ret
= pGetAddrInfoW(empty
, zero
, NULL
, &result
);
6326 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6327 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6328 ok(result
!= NULL
, "GetAddrInfoW failed\n");
6331 ret
= pGetAddrInfoW(empty
, empty
, NULL
, &result2
);
6332 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6333 ok(result2
!= NULL
, "GetAddrInfoW failed\n");
6334 compare_addrinfow(result
, result2
);
6335 pFreeAddrInfoW(result
);
6336 pFreeAddrInfoW(result2
);
6339 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
6340 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6341 pFreeAddrInfoW(result
);
6344 ret
= pGetAddrInfoW(localhost
, empty
, NULL
, &result
);
6345 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6346 pFreeAddrInfoW(result
);
6349 ret
= pGetAddrInfoW(localhost
, zero
, NULL
, &result
);
6350 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6351 pFreeAddrInfoW(result
);
6354 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
6355 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6356 pFreeAddrInfoW(result
);
6359 ret
= pGetAddrInfoW(localhost
, NULL
, &hint
, &result
);
6360 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6361 pFreeAddrInfoW(result
);
6364 SetLastError(0xdeadbeef);
6365 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
6366 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
6367 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6368 pFreeAddrInfoW(result
);
6370 result
= (ADDRINFOW
*)0xdeadbeef;
6371 WSASetLastError(0xdeadbeef);
6372 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
6375 skip("nxdomain returned success. Broken ISP redirects?\n");
6378 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
6379 ok(WSAGetLastError() == WSAHOST_NOT_FOUND
, "expected 11001, got %d\n", WSAGetLastError());
6380 ok(result
== NULL
, "got %p\n", result
);
6382 result
= (ADDRINFOW
*)0xdeadbeef;
6383 WSASetLastError(0xdeadbeef);
6384 ret
= pGetAddrInfoW(nxdomain
, NULL
, NULL
, &result
);
6387 skip("nxdomain returned success. Broken ISP redirects?\n");
6390 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
6391 ok(WSAGetLastError() == WSAHOST_NOT_FOUND
, "expected 11001, got %d\n", WSAGetLastError());
6392 ok(result
== NULL
, "got %p\n", result
);
6394 for (i
= 0;i
< (sizeof(hinttests
) / sizeof(hinttests
[0]));i
++)
6396 hint
.ai_family
= hinttests
[i
].family
;
6397 hint
.ai_socktype
= hinttests
[i
].socktype
;
6398 hint
.ai_protocol
= hinttests
[i
].protocol
;
6401 SetLastError(0xdeadbeef);
6402 ret
= pGetAddrInfoW(localhost
, NULL
, &hint
, &result
);
6405 if (hinttests
[i
].error
)
6406 ok(0, "test %d: GetAddrInfoW succeeded unexpectedly\n", i
);
6412 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
6413 if (hinttests
[i
].family
== AF_UNSPEC
)
6414 ok(p
->ai_family
== AF_INET
|| p
->ai_family
== AF_INET6
,
6415 "test %d: expected AF_INET or AF_INET6, got %d\n",
6418 ok(p
->ai_family
== hinttests
[i
].family
,
6419 "test %d: expected family %d, got %d\n",
6420 i
, hinttests
[i
].family
, p
->ai_family
);
6422 ok(p
->ai_socktype
== hinttests
[i
].socktype
,
6423 "test %d: expected type %d, got %d\n",
6424 i
, hinttests
[i
].socktype
, p
->ai_socktype
);
6425 ok(p
->ai_protocol
== hinttests
[i
].protocol
,
6426 "test %d: expected protocol %d, got %d\n",
6427 i
, hinttests
[i
].protocol
, p
->ai_protocol
);
6432 pFreeAddrInfoW(result
);
6436 DWORD err
= WSAGetLastError();
6437 if (hinttests
[i
].error
)
6438 ok(hinttests
[i
].error
== err
, "test %d: GetAddrInfoW failed with error %d, expected %d\n",
6439 i
, err
, hinttests
[i
].error
);
6441 ok(0, "test %d: GetAddrInfoW failed with %d (err %d)\n", i
, ret
, err
);
6446 static void test_getaddrinfo(void)
6449 ADDRINFOA
*result
, *result2
, *p
, hint
;
6451 if (!pgetaddrinfo
|| !pfreeaddrinfo
)
6453 win_skip("getaddrinfo and/or freeaddrinfo not present\n");
6456 memset(&hint
, 0, sizeof(ADDRINFOA
));
6458 result
= (ADDRINFOA
*)0xdeadbeef;
6459 WSASetLastError(0xdeadbeef);
6460 ret
= pgetaddrinfo(NULL
, NULL
, NULL
, &result
);
6461 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
6462 ok(WSAGetLastError() == WSAHOST_NOT_FOUND
, "expected 11001, got %d\n", WSAGetLastError());
6463 ok(result
== NULL
, "got %p\n", result
);
6466 WSASetLastError(0xdeadbeef);
6467 ret
= pgetaddrinfo("", NULL
, NULL
, &result
);
6468 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6469 ok(result
!= NULL
, "getaddrinfo failed\n");
6470 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6471 pfreeaddrinfo(result
);
6474 ret
= pgetaddrinfo(NULL
, "0", NULL
, &result
);
6475 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6476 ok(result
!= NULL
, "getaddrinfo failed\n");
6479 ret
= pgetaddrinfo(NULL
, "", NULL
, &result2
);
6480 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6481 ok(result2
!= NULL
, "getaddrinfo failed\n");
6482 compare_addrinfo(result
, result2
);
6483 pfreeaddrinfo(result
);
6484 pfreeaddrinfo(result2
);
6487 WSASetLastError(0xdeadbeef);
6488 ret
= pgetaddrinfo("", "0", NULL
, &result
);
6489 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6490 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6491 ok(result
!= NULL
, "getaddrinfo failed\n");
6494 ret
= pgetaddrinfo("", "", NULL
, &result2
);
6495 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6496 ok(result2
!= NULL
, "getaddrinfo failed\n");
6497 compare_addrinfo(result
, result2
);
6498 pfreeaddrinfo(result
);
6499 pfreeaddrinfo(result2
);
6502 ret
= pgetaddrinfo("localhost", NULL
, NULL
, &result
);
6503 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6504 pfreeaddrinfo(result
);
6507 ret
= pgetaddrinfo("localhost", "", NULL
, &result
);
6508 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6509 pfreeaddrinfo(result
);
6512 ret
= pgetaddrinfo("localhost", "0", NULL
, &result
);
6513 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6514 pfreeaddrinfo(result
);
6517 ret
= pgetaddrinfo("localhost", "80", NULL
, &result
);
6518 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6519 pfreeaddrinfo(result
);
6522 ret
= pgetaddrinfo("localhost", NULL
, &hint
, &result
);
6523 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6524 pfreeaddrinfo(result
);
6527 WSASetLastError(0xdeadbeef);
6528 ret
= pgetaddrinfo("localhost", "80", &hint
, &result
);
6529 ok(!ret
, "getaddrinfo failed with %d\n", WSAGetLastError());
6530 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError());
6531 pfreeaddrinfo(result
);
6533 result
= (ADDRINFOA
*)0xdeadbeef;
6534 WSASetLastError(0xdeadbeef);
6535 ret
= pgetaddrinfo("nxdomain.codeweavers.com", NULL
, NULL
, &result
);
6538 skip("nxdomain returned success. Broken ISP redirects?\n");
6541 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
6542 ok(WSAGetLastError() == WSAHOST_NOT_FOUND
, "expected 11001, got %d\n", WSAGetLastError());
6543 ok(result
== NULL
, "got %p\n", result
);
6545 for (i
= 0;i
< (sizeof(hinttests
) / sizeof(hinttests
[0]));i
++)
6547 hint
.ai_family
= hinttests
[i
].family
;
6548 hint
.ai_socktype
= hinttests
[i
].socktype
;
6549 hint
.ai_protocol
= hinttests
[i
].protocol
;
6552 SetLastError(0xdeadbeef);
6553 ret
= pgetaddrinfo("localhost", NULL
, &hint
, &result
);
6556 if (hinttests
[i
].error
)
6557 ok(0, "test %d: getaddrinfo succeeded unexpectedly\n", i
);
6563 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */
6564 if (hinttests
[i
].family
== AF_UNSPEC
)
6565 ok(p
->ai_family
== AF_INET
|| p
->ai_family
== AF_INET6
,
6566 "test %d: expected AF_INET or AF_INET6, got %d\n",
6569 ok(p
->ai_family
== hinttests
[i
].family
,
6570 "test %d: expected family %d, got %d\n",
6571 i
, hinttests
[i
].family
, p
->ai_family
);
6573 ok(p
->ai_socktype
== hinttests
[i
].socktype
,
6574 "test %d: expected type %d, got %d\n",
6575 i
, hinttests
[i
].socktype
, p
->ai_socktype
);
6576 ok(p
->ai_protocol
== hinttests
[i
].protocol
,
6577 "test %d: expected protocol %d, got %d\n",
6578 i
, hinttests
[i
].protocol
, p
->ai_protocol
);
6583 pfreeaddrinfo(result
);
6587 DWORD err
= WSAGetLastError();
6588 if (hinttests
[i
].error
)
6589 ok(hinttests
[i
].error
== err
, "test %d: getaddrinfo failed with error %d, expected %d\n",
6590 i
, err
, hinttests
[i
].error
);
6592 ok(0, "test %d: getaddrinfo failed with %d (err %d)\n", i
, ret
, err
);
6597 static void test_ConnectEx(void)
6599 SOCKET listener
= INVALID_SOCKET
;
6600 SOCKET acceptor
= INVALID_SOCKET
;
6601 SOCKET connector
= INVALID_SOCKET
;
6602 struct sockaddr_in address
, conaddress
;
6604 OVERLAPPED overlapped
;
6605 LPFN_CONNECTEX pConnectEx
;
6606 GUID connectExGuid
= WSAID_CONNECTEX
;
6607 DWORD bytesReturned
;
6613 memset(&overlapped
, 0, sizeof(overlapped
));
6615 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
6616 if (listener
== INVALID_SOCKET
) {
6617 skip("could not create listener socket, error %d\n", WSAGetLastError());
6621 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
6622 if (connector
== INVALID_SOCKET
) {
6623 skip("could not create connector socket, error %d\n", WSAGetLastError());
6627 memset(&address
, 0, sizeof(address
));
6628 address
.sin_family
= AF_INET
;
6629 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6630 iret
= bind(listener
, (struct sockaddr
*)&address
, sizeof(address
));
6632 skip("failed to bind, error %d\n", WSAGetLastError());
6636 addrlen
= sizeof(address
);
6637 iret
= getsockname(listener
, (struct sockaddr
*)&address
, &addrlen
);
6639 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
6643 if (set_blocking(listener
, TRUE
)) {
6644 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
6648 iret
= WSAIoctl(connector
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &connectExGuid
, sizeof(connectExGuid
),
6649 &pConnectEx
, sizeof(pConnectEx
), &bytesReturned
, NULL
, NULL
);
6651 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret
, WSAGetLastError());
6655 bret
= pConnectEx(INVALID_SOCKET
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
6656 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "ConnectEx on invalid socket "
6657 "returned %d + errno %d\n", bret
, WSAGetLastError());
6659 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
6660 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "ConnectEx on a unbound socket "
6661 "returned %d + errno %d\n", bret
, WSAGetLastError());
6662 if (bret
== TRUE
|| WSAGetLastError() != WSAEINVAL
)
6664 acceptor
= accept(listener
, NULL
, NULL
);
6665 if (acceptor
!= INVALID_SOCKET
) {
6666 closesocket(acceptor
);
6667 acceptor
= INVALID_SOCKET
;
6670 closesocket(connector
);
6671 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
6672 if (connector
== INVALID_SOCKET
) {
6673 skip("could not create connector socket, error %d\n", WSAGetLastError());
6678 /* ConnectEx needs a bound socket */
6679 memset(&conaddress
, 0, sizeof(conaddress
));
6680 conaddress
.sin_family
= AF_INET
;
6681 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6682 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
6684 skip("failed to bind, error %d\n", WSAGetLastError());
6688 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, NULL
);
6689 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "ConnectEx on a NULL overlapped "
6690 "returned %d + errno %d\n", bret
, WSAGetLastError());
6692 overlapped
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
6693 if (overlapped
.hEvent
== NULL
) {
6694 skip("could not create event object, errno = %d\n", GetLastError());
6698 iret
= listen(listener
, 1);
6700 skip("listening failed, errno = %d\n", WSAGetLastError());
6704 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
6705 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
6706 "returned %d + errno %d\n", bret
, WSAGetLastError());
6707 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
6708 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
6710 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
6711 ok(bret
, "Connecting failed, error %d\n", GetLastError());
6712 ok(bytesReturned
== 0, "Bytes sent is %d\n", bytesReturned
);
6714 closesocket(connector
);
6715 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
6716 if (connector
== INVALID_SOCKET
) {
6717 skip("could not create connector socket, error %d\n", WSAGetLastError());
6720 /* ConnectEx needs a bound socket */
6721 memset(&conaddress
, 0, sizeof(conaddress
));
6722 conaddress
.sin_family
= AF_INET
;
6723 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6724 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
6726 skip("failed to bind, error %d\n", WSAGetLastError());
6730 acceptor
= accept(listener
, NULL
, NULL
);
6731 if (acceptor
!= INVALID_SOCKET
) {
6732 closesocket(acceptor
);
6738 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, buffer
, 3, &bytesReturned
, &overlapped
);
6739 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "ConnectEx failed: "
6740 "returned %d + errno %d\n", bret
, WSAGetLastError());
6741 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
6742 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
6744 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
6745 ok(bret
, "Connecting failed, error %d\n", GetLastError());
6746 ok(bytesReturned
== 3, "Bytes sent is %d\n", bytesReturned
);
6748 acceptor
= accept(listener
, NULL
, NULL
);
6749 ok(acceptor
!= INVALID_SOCKET
, "could not accept socket error %d\n", WSAGetLastError());
6751 bytesReturned
= recv(acceptor
, buffer
, 3, 0);
6753 ok(bytesReturned
== 3, "Didn't get all sent data, got only %d\n", bytesReturned
);
6754 ok(buffer
[0] == '1' && buffer
[1] == '2' && buffer
[2] == '3',
6755 "Failed to get the right data, expected '123', got '%s'\n", buffer
);
6757 closesocket(connector
);
6758 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
6759 if (connector
== INVALID_SOCKET
) {
6760 skip("could not create connector socket, error %d\n", WSAGetLastError());
6763 /* ConnectEx needs a bound socket */
6764 memset(&conaddress
, 0, sizeof(conaddress
));
6765 conaddress
.sin_family
= AF_INET
;
6766 conaddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6767 iret
= bind(connector
, (struct sockaddr
*)&conaddress
, sizeof(conaddress
));
6769 skip("failed to bind, error %d\n", WSAGetLastError());
6773 if (acceptor
!= INVALID_SOCKET
) {
6774 closesocket(acceptor
);
6775 acceptor
= INVALID_SOCKET
;
6778 /* Connect with error */
6779 closesocket(listener
);
6780 listener
= INVALID_SOCKET
;
6782 address
.sin_port
= htons(1);
6784 bret
= pConnectEx(connector
, (struct sockaddr
*)&address
, addrlen
, NULL
, 0, &bytesReturned
, &overlapped
);
6785 ok(bret
== FALSE
&& GetLastError(), "ConnectEx to bad destination failed: "
6786 "returned %d + errno %d\n", bret
, GetLastError());
6788 if (GetLastError() == ERROR_IO_PENDING
)
6790 dwret
= WaitForSingleObject(overlapped
.hEvent
, 15000);
6791 ok(dwret
== WAIT_OBJECT_0
, "Waiting for connect event failed with %d + errno %d\n", dwret
, GetLastError());
6793 bret
= GetOverlappedResult((HANDLE
)connector
, &overlapped
, &bytesReturned
, FALSE
);
6794 ok(bret
== FALSE
&& GetLastError() == ERROR_CONNECTION_REFUSED
,
6795 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
6798 ok(GetLastError() == WSAECONNREFUSED
,
6799 "Connecting to a disconnected host returned error %d - %d\n", bret
, WSAGetLastError());
6803 if (overlapped
.hEvent
)
6804 WSACloseEvent(overlapped
.hEvent
);
6805 if (listener
!= INVALID_SOCKET
)
6806 closesocket(listener
);
6807 if (acceptor
!= INVALID_SOCKET
)
6808 closesocket(acceptor
);
6809 if (connector
!= INVALID_SOCKET
)
6810 closesocket(connector
);
6813 static void test_AcceptEx(void)
6815 SOCKET listener
= INVALID_SOCKET
;
6816 SOCKET acceptor
= INVALID_SOCKET
;
6817 SOCKET connector
= INVALID_SOCKET
;
6818 SOCKET connector2
= INVALID_SOCKET
;
6819 struct sockaddr_in bindAddress
, peerAddress
, *readBindAddress
, *readRemoteAddress
;
6820 int socklen
, optlen
;
6821 GUID acceptExGuid
= WSAID_ACCEPTEX
, getAcceptExGuid
= WSAID_GETACCEPTEXSOCKADDRS
;
6822 LPFN_ACCEPTEX pAcceptEx
= NULL
;
6823 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs
= NULL
;
6824 fd_set fds_accept
, fds_send
;
6825 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
6827 DWORD bytesReturned
, connect_time
;
6828 char buffer
[1024], ipbuffer
[32];
6829 OVERLAPPED overlapped
;
6830 int iret
, localSize
= sizeof(struct sockaddr_in
), remoteSize
= localSize
;
6834 memset(&overlapped
, 0, sizeof(overlapped
));
6836 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
6837 if (listener
== INVALID_SOCKET
) {
6838 skip("could not create listener socket, error %d\n", WSAGetLastError());
6842 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
6843 if (acceptor
== INVALID_SOCKET
) {
6844 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
6848 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
6849 if (connector
== INVALID_SOCKET
) {
6850 skip("could not create connector socket, error %d\n", WSAGetLastError());
6854 memset(&bindAddress
, 0, sizeof(bindAddress
));
6855 bindAddress
.sin_family
= AF_INET
;
6856 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
6857 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
6859 skip("failed to bind, error %d\n", WSAGetLastError());
6863 socklen
= sizeof(bindAddress
);
6864 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
6866 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
6870 if (set_blocking(listener
, FALSE
)) {
6871 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
6875 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
6876 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
6878 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
6882 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &getAcceptExGuid
, sizeof(getAcceptExGuid
),
6883 &pGetAcceptExSockaddrs
, sizeof(pGetAcceptExSockaddrs
), &bytesReturned
, NULL
, NULL
);
6885 skip("WSAIoctl failed to get GetAcceptExSockaddrs with ret %d + errno %d\n", iret
, WSAGetLastError());
6889 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
6890 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6891 &bytesReturned
, &overlapped
);
6892 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
6893 "returned %d + errno %d\n", bret
, WSAGetLastError());
6895 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
6896 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6897 &bytesReturned
, &overlapped
);
6899 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
6900 "returned %d + errno %d\n", bret
, WSAGetLastError());
6902 iret
= listen(listener
, 5);
6904 skip("listening failed, errno = %d\n", WSAGetLastError());
6908 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
6909 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6910 &bytesReturned
, &overlapped
);
6911 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid accepting socket "
6912 "returned %d + errno %d\n", bret
, WSAGetLastError());
6914 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
6915 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6916 &bytesReturned
, &overlapped
);
6918 (WSAGetLastError() == WSAEINVAL
||
6919 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
6920 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
6922 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
6923 &bytesReturned
, &overlapped
);
6924 ok(bret
== FALSE
&& (WSAGetLastError() == ERROR_IO_PENDING
|| broken(WSAGetLastError() == WSAEINVAL
)) /* NT4 */,
6925 "AcceptEx on too small local address size returned %d + errno %d\n",
6926 bret
, WSAGetLastError());
6927 bret
= CancelIo((HANDLE
) listener
);
6928 ok(bret
, "Failed to cancel pending accept socket\n");
6930 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 15,
6931 sizeof(struct sockaddr_in
) + 16, &bytesReturned
, &overlapped
);
6932 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx on too small local address "
6933 "size returned %d + errno %d\n",
6934 bret
, WSAGetLastError());
6935 bret
= CancelIo((HANDLE
) listener
);
6936 ok(bret
, "Failed to cancel pending accept socket\n");
6938 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
6939 &bytesReturned
, &overlapped
);
6940 ok(bret
== FALSE
&& (WSAGetLastError() == WSAEFAULT
|| broken(WSAGetLastError() == WSAEINVAL
)) /* NT4 */,
6941 "AcceptEx on too small remote address size returned %d + errno %d\n", bret
, WSAGetLastError());
6943 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16,
6944 sizeof(struct sockaddr_in
) + 15, &bytesReturned
, &overlapped
);
6945 ok(bret
== FALSE
&& (WSAGetLastError() == ERROR_IO_PENDING
|| broken(WSAGetLastError() == WSAEINVAL
)) /* NT4 */,
6946 "AcceptEx on too small remote address size returned %d + errno %d\n", bret
, WSAGetLastError());
6947 bret
= CancelIo((HANDLE
) listener
);
6948 ok(bret
, "Failed to cancel pending accept socket\n");
6950 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
6951 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6952 &bytesReturned
, NULL
);
6953 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
6954 "returned %d + errno %d\n", bret
, WSAGetLastError());
6956 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, 0, &bytesReturned
, NULL
);
6957 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
6958 "returned %d + errno %d\n", bret
, WSAGetLastError());
6960 overlapped
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
6961 if (overlapped
.hEvent
== NULL
) {
6962 skip("could not create event object, errno = %d\n", GetLastError());
6966 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
6967 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6968 &bytesReturned
, &overlapped
);
6969 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
6971 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
6972 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6973 &bytesReturned
, &overlapped
);
6974 todo_wine
ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
6975 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
6976 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
6977 /* We need to cancel this call, otherwise things fail */
6978 bret
= CancelIo((HANDLE
) listener
);
6979 ok(bret
, "Failed to cancel failed test. Bailing...\n");
6981 WaitForSingleObject(overlapped
.hEvent
, 0);
6983 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
6984 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
6985 &bytesReturned
, &overlapped
);
6986 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
6989 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
6990 todo_wine
ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
6991 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
6992 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
6993 /* We need to cancel this call, otherwise things fail */
6994 closesocket(acceptor
);
6995 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
6996 if (acceptor
== INVALID_SOCKET
) {
6997 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7001 bret
= CancelIo((HANDLE
) listener
);
7002 ok(bret
, "Failed to cancel failed test. Bailing...\n");
7005 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
7006 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7007 &bytesReturned
, &overlapped
);
7008 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7011 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7012 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7014 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
7015 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7017 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7018 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
7019 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
7021 closesocket(connector
);
7022 connector
= INVALID_SOCKET
;
7023 closesocket(acceptor
);
7025 /* Test short reads */
7027 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7028 if (acceptor
== INVALID_SOCKET
) {
7029 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7032 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
7033 if (connector
== INVALID_SOCKET
) {
7034 skip("could not create connector socket, error %d\n", WSAGetLastError());
7037 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
7038 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7039 &bytesReturned
, &overlapped
);
7040 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7042 connect_time
= 0xdeadbeef;
7043 optlen
= sizeof(connect_time
);
7044 iret
= getsockopt(connector
, SOL_SOCKET
, SO_CONNECT_TIME
, (char *)&connect_time
, &optlen
);
7045 ok(!iret
, "getsockopt failed %d\n", WSAGetLastError());
7046 ok(connect_time
== ~0u, "unexpected connect time %u\n", connect_time
);
7048 /* AcceptEx() still won't complete until we send data */
7049 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7050 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7052 connect_time
= 0xdeadbeef;
7053 optlen
= sizeof(connect_time
);
7054 iret
= getsockopt(connector
, SOL_SOCKET
, SO_CONNECT_TIME
, (char *)&connect_time
, &optlen
);
7055 ok(!iret
, "getsockopt failed %d\n", WSAGetLastError());
7056 ok(connect_time
< 0xdeadbeef, "unexpected connect time %u\n", connect_time
);
7058 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
7059 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
7061 iret
= getsockname( connector
, (struct sockaddr
*)&peerAddress
, &remoteSize
);
7062 ok( !iret
, "getsockname failed.\n");
7064 /* AcceptEx() could complete any time now */
7065 iret
= send(connector
, buffer
, 1, 0);
7066 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
7068 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
7069 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7071 /* Check if the buffer from AcceptEx is decoded correctly */
7072 pGetAcceptExSockaddrs(buffer
, 2, sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7073 (struct sockaddr
**)&readBindAddress
, &localSize
,
7074 (struct sockaddr
**)&readRemoteAddress
, &remoteSize
);
7075 strcpy( ipbuffer
, inet_ntoa(readBindAddress
->sin_addr
));
7076 ok( readBindAddress
->sin_addr
.s_addr
== bindAddress
.sin_addr
.s_addr
,
7077 "Local socket address is different %s != %s\n",
7078 ipbuffer
, inet_ntoa(bindAddress
.sin_addr
));
7079 ok( readBindAddress
->sin_port
== bindAddress
.sin_port
,
7080 "Local socket port is different: %d != %d\n",
7081 readBindAddress
->sin_port
, bindAddress
.sin_port
);
7082 strcpy( ipbuffer
, inet_ntoa(readRemoteAddress
->sin_addr
));
7083 ok( readRemoteAddress
->sin_addr
.s_addr
== peerAddress
.sin_addr
.s_addr
,
7084 "Remote socket address is different %s != %s\n",
7085 ipbuffer
, inet_ntoa(peerAddress
.sin_addr
));
7086 ok( readRemoteAddress
->sin_port
== peerAddress
.sin_port
,
7087 "Remote socket port is different: %d != %d\n",
7088 readRemoteAddress
->sin_port
, peerAddress
.sin_port
);
7090 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7091 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
7092 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
7094 closesocket(connector
);
7095 connector
= INVALID_SOCKET
;
7096 closesocket(acceptor
);
7098 /* Test CF_DEFER & AcceptEx interaction */
7100 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7101 if (acceptor
== INVALID_SOCKET
) {
7102 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7105 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
7106 if (connector
== INVALID_SOCKET
) {
7107 skip("could not create connector socket, error %d\n", WSAGetLastError());
7110 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
7111 if (connector
== INVALID_SOCKET
) {
7112 skip("could not create connector socket, error %d\n", WSAGetLastError());
7116 if (set_blocking(connector
, FALSE
)) {
7117 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
7121 if (set_blocking(connector2
, FALSE
)) {
7122 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
7126 /* Connect socket #1 */
7127 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7128 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7130 FD_ZERO ( &fds_accept
);
7131 FD_ZERO ( &fds_send
);
7133 FD_SET ( listener
, &fds_accept
);
7134 FD_SET ( connector
, &fds_send
);
7140 for (i
= 0; i
< 4000; ++i
)
7142 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
7144 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
7145 "acceptex test(%d): could not select on socket, errno %d\n" );
7147 /* check for incoming requests */
7148 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
7151 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
7152 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
7153 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
7154 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7155 &bytesReturned
, &overlapped
);
7156 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7158 else if (got
== 2) {
7159 /* this should be socket #2 */
7160 SOCKET tmp
= accept(listener
, NULL
, NULL
);
7161 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
7165 ok(FALSE
, "Got more than 2 connections?\n");
7168 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
7169 /* Send data on second socket, and stop */
7170 send(connector2
, "2", 1, 0);
7171 FD_CLR ( connector2
, &fds_send
);
7175 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
7176 /* Once #1 is connected, allow #2 to connect */
7179 send(connector
, "1", 1, 0);
7180 FD_CLR ( connector
, &fds_send
);
7182 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7183 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7184 FD_SET ( connector2
, &fds_send
);
7188 ok (got
== 2 || broken(got
== 1) /* NT4 */,
7189 "Did not get both connections, got %d\n", got
);
7191 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
7192 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7194 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7195 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
7196 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
7198 set_blocking(acceptor
, TRUE
);
7199 iret
= recv( acceptor
, buffer
, 2, 0);
7200 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
7202 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
7204 closesocket(connector
);
7205 connector
= INVALID_SOCKET
;
7206 closesocket(acceptor
);
7208 /* clean up in case of failures */
7209 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
7210 closesocket(acceptor
);
7212 /* Disconnect during receive? */
7214 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7215 if (acceptor
== INVALID_SOCKET
) {
7216 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7219 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
7220 if (connector
== INVALID_SOCKET
) {
7221 skip("could not create connector socket, error %d\n", WSAGetLastError());
7224 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
7225 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7226 &bytesReturned
, &overlapped
);
7227 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7229 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7230 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
7232 closesocket(connector
);
7233 connector
= INVALID_SOCKET
;
7235 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
7236 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7238 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7239 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
7240 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
7242 closesocket(acceptor
);
7244 /* Test closing with pending requests */
7246 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7247 if (acceptor
== INVALID_SOCKET
) {
7248 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7251 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
7252 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7253 &bytesReturned
, &overlapped
);
7254 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7256 closesocket(acceptor
);
7258 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
7259 todo_wine
ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
7260 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7262 if (dwret
!= WAIT_TIMEOUT
) {
7263 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7264 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
7267 bret
= CancelIo((HANDLE
) listener
);
7268 ok(bret
, "Failed to cancel failed test. Bailing...\n");
7270 WaitForSingleObject(overlapped
.hEvent
, 0);
7273 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7274 if (acceptor
== INVALID_SOCKET
) {
7275 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7278 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
7279 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7280 &bytesReturned
, &overlapped
);
7281 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7283 CancelIo((HANDLE
) acceptor
);
7285 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
7286 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
7288 closesocket(acceptor
);
7290 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
7291 if (acceptor
== INVALID_SOCKET
) {
7292 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7295 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
7296 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7297 &bytesReturned
, &overlapped
);
7298 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
7300 closesocket(listener
);
7301 listener
= INVALID_SOCKET
;
7303 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
7304 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
7306 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
7307 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
7310 if (overlapped
.hEvent
)
7311 WSACloseEvent(overlapped
.hEvent
);
7312 if (listener
!= INVALID_SOCKET
)
7313 closesocket(listener
);
7314 if (acceptor
!= INVALID_SOCKET
)
7315 closesocket(acceptor
);
7316 if (connector
!= INVALID_SOCKET
)
7317 closesocket(connector
);
7318 if (connector2
!= INVALID_SOCKET
)
7319 closesocket(connector2
);
7322 static void test_getpeername(void)
7325 struct sockaddr_in sa
, sa_out
;
7326 SOCKADDR_STORAGE ss
;
7328 const char buf
[] = "hello world";
7331 /* Test the parameter validation order. */
7332 ret
= getpeername(INVALID_SOCKET
, NULL
, NULL
);
7333 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
7334 ok(WSAGetLastError() == WSAENOTSOCK
,
7335 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
7337 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
7338 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
7339 if (sock
== INVALID_SOCKET
)
7341 skip("Socket creation failed with %d\n", WSAGetLastError());
7345 ret
= getpeername(sock
, NULL
, NULL
);
7346 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
7347 ok(WSAGetLastError() == WSAENOTCONN
||
7348 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
7349 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
7351 memset(&sa
, 0, sizeof(sa
));
7352 sa
.sin_family
= AF_INET
;
7353 sa
.sin_port
= htons(139);
7354 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
7356 /* sendto does not change a socket's connection state. */
7357 ret
= sendto(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sa
, sizeof(sa
));
7358 ok(ret
!= SOCKET_ERROR
,
7359 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
7361 ret
= getpeername(sock
, NULL
, NULL
);
7362 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
7363 ok(WSAGetLastError() == WSAENOTCONN
||
7364 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
7365 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
7367 ret
= connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
));
7369 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
7371 ret
= getpeername(sock
, NULL
, NULL
);
7372 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
7373 ok(WSAGetLastError() == WSAEFAULT
,
7374 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
7376 /* Test crashes on Wine. */
7379 ret
= getpeername(sock
, (void*)0xdeadbeef, (void*)0xcafebabe);
7380 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
7381 ok(WSAGetLastError() == WSAEFAULT
,
7382 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
7385 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, NULL
);
7386 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
7387 ok(WSAGetLastError() == WSAEFAULT
,
7388 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
7391 ret
= getpeername(sock
, NULL
, &sa_len
);
7392 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
7393 ok(WSAGetLastError() == WSAEFAULT
,
7394 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
7395 ok(!sa_len
, "got %d\n", sa_len
);
7398 ret
= getpeername(sock
, (struct sockaddr
*)&ss
, &sa_len
);
7399 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
7400 ok(WSAGetLastError() == WSAEFAULT
,
7401 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
7402 ok(!sa_len
, "got %d\n", sa_len
);
7404 sa_len
= sizeof(ss
);
7405 ret
= getpeername(sock
, (struct sockaddr
*)&ss
, &sa_len
);
7406 ok(ret
== 0, "Expected getpeername to return 0, got %d\n", ret
);
7407 ok(!memcmp(&sa
, &ss
, sizeof(sa
)),
7408 "Expected the returned structure to be identical to the connect structure\n");
7409 ok(sa_len
== sizeof(sa
), "got %d\n", sa_len
);
7414 static void test_sioRoutingInterfaceQuery(void)
7418 SOCKADDR_IN sin
= { 0 }, sout
= { 0 };
7419 DWORD bytesReturned
;
7421 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
7422 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
7423 if (sock
== INVALID_SOCKET
)
7425 skip("Socket creation failed with %d\n", WSAGetLastError());
7428 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, NULL
, 0, NULL
, 0, NULL
,
7430 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
7431 "expected WSAEFAULT, got %d\n", WSAGetLastError());
7432 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
7433 NULL
, 0, NULL
, NULL
, NULL
);
7434 ok(ret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEFAULT
,
7435 "expected WSAEFAULT, got %d\n", WSAGetLastError());
7436 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
7437 NULL
, 0, &bytesReturned
, NULL
, NULL
);
7438 ok(ret
== SOCKET_ERROR
&&
7439 (WSAGetLastError() == WSAEFAULT
/* Win98 */ ||
7440 WSAGetLastError() == WSAEINVAL
/* NT4 */||
7441 WSAGetLastError() == WSAEAFNOSUPPORT
),
7442 "expected WSAEFAULT or WSAEINVAL or WSAEAFNOSUPPORT, got %d\n",
7444 sin
.sin_family
= AF_INET
;
7445 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
7446 NULL
, 0, &bytesReturned
, NULL
, NULL
);
7447 ok(ret
== SOCKET_ERROR
&&
7448 (WSAGetLastError() == WSAEFAULT
/* Win98 */ ||
7449 WSAGetLastError() == WSAEINVAL
),
7450 "expected WSAEFAULT or WSAEINVAL, got %d\n", WSAGetLastError());
7451 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
7452 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
7453 NULL
, 0, &bytesReturned
, NULL
, NULL
);
7454 ok(ret
== SOCKET_ERROR
&&
7455 (WSAGetLastError() == WSAEINVAL
/* NT4 */ ||
7456 WSAGetLastError() == WSAEFAULT
),
7457 "expected WSAEINVAL or WSAEFAULT, got %d\n", WSAGetLastError());
7458 ret
= WSAIoctl(sock
, SIO_ROUTING_INTERFACE_QUERY
, &sin
, sizeof(sin
),
7459 &sout
, sizeof(sout
), &bytesReturned
, NULL
, NULL
);
7460 ok(!ret
|| broken(WSAGetLastError() == WSAEINVAL
/* NT4 */),
7461 "WSAIoctl failed: %d\n", WSAGetLastError());
7464 ok(sout
.sin_family
== AF_INET
, "expected AF_INET, got %d\n",
7466 /* We expect the source address to be INADDR_LOOPBACK as well, but
7467 * there's no guarantee that a route to the loopback address exists,
7468 * so rather than introduce spurious test failures we do not test the
7475 static void test_sioAddressListChange(void)
7477 struct sockaddr_in bindAddress
;
7478 struct in_addr net_address
;
7479 WSAOVERLAPPED overlapped
;
7486 if (!winetest_interactive
)
7488 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, interactive tests must be enabled\n");
7492 /* Use gethostbyname to find the list of local network interfaces */
7493 h
= gethostbyname("");
7496 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, gethostbyname failed with %u\n",
7500 for (acount
= 0; h
->h_addr_list
[acount
]; acount
++);
7503 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, test requires a network card.\n");
7506 net_address
.s_addr
= *(ULONG
*) h
->h_addr_list
[0];
7508 /* Bind an overlapped socket to the first found network interface */
7509 sock
= WSASocketW(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0, WSA_FLAG_OVERLAPPED
);
7510 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
7511 if (sock
== INVALID_SOCKET
)
7513 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, socket creation failed with %u\n",
7517 memset(&bindAddress
, 0, sizeof(bindAddress
));
7518 bindAddress
.sin_family
= AF_INET
;
7519 bindAddress
.sin_addr
.s_addr
= net_address
.s_addr
;
7520 ret
= bind(sock
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
7523 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, failed to bind, error %u\n", WSAGetLastError());
7527 /* Wait for address changes, request that the user connects/disconnects an interface */
7528 memset(&overlapped
, 0, sizeof(overlapped
));
7529 overlapped
.hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
7530 ret
= WSAIoctl(sock
, SIO_ADDRESS_LIST_CHANGE
, NULL
, 0, NULL
, 0, &num_bytes
, &overlapped
, NULL
);
7531 ok(ret
== SOCKET_ERROR
, "WSAIoctl succeeded unexpectedly\n");
7532 ok(WSAGetLastError() == WSA_IO_PENDING
, "Expected pending last error %d\n", WSAGetLastError());
7533 trace("Testing socket-based ipv4 address list change notification. Please connect/disconnect or"
7534 " change the ipv4 address of any of the local network interfaces (10 second timeout).\n");
7535 ret
= WaitForSingleObject(overlapped
.hEvent
, 10000);
7536 ok(ret
== WAIT_OBJECT_0
, "failed to get overlapped event %u\n", ret
);
7542 static void test_synchronous_WSAIoctl(void)
7544 HANDLE previous_port
, io_port
;
7545 WSAOVERLAPPED overlapped
, *olp
;
7553 previous_port
= CreateIoCompletionPort( INVALID_HANDLE_VALUE
, NULL
, 0, 0 );
7554 ok( previous_port
!= NULL
, "failed to create completion port %u\n", GetLastError() );
7556 socket
= WSASocketW( AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0, WSA_FLAG_OVERLAPPED
);
7557 ok( socket
!= INVALID_SOCKET
, "failed to create socket %d\n", WSAGetLastError() );
7559 io_port
= CreateIoCompletionPort( (HANDLE
)socket
, previous_port
, 0, 0 );
7560 ok( io_port
!= NULL
, "failed to create completion port %u\n", GetLastError() );
7563 memset( &overlapped
, 0, sizeof(overlapped
) );
7564 res
= WSAIoctl( socket
, FIONBIO
, &on
, sizeof(on
), NULL
, 0, &num_bytes
, &overlapped
, NULL
);
7565 ok( !res
, "WSAIoctl failed %d\n", WSAGetLastError() );
7567 ret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 10000 );
7568 ok( ret
, "failed to get completion status %u\n", GetLastError() );
7570 CloseHandle( io_port
);
7571 closesocket( socket
);
7572 CloseHandle( previous_port
);
7575 #define WM_ASYNCCOMPLETE (WM_USER + 100)
7576 static HWND
create_async_message_window(void)
7578 static const char class_name
[] = "ws2_32 async message window class";
7580 WNDCLASSEXA wndclass
;
7583 wndclass
.cbSize
= sizeof(wndclass
);
7584 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
7585 wndclass
.lpfnWndProc
= DefWindowProcA
;
7586 wndclass
.cbClsExtra
= 0;
7587 wndclass
.cbWndExtra
= 0;
7588 wndclass
.hInstance
= GetModuleHandleA(NULL
);
7589 wndclass
.hIcon
= LoadIconA(NULL
, (LPCSTR
)IDI_APPLICATION
);
7590 wndclass
.hIconSm
= LoadIconA(NULL
, (LPCSTR
)IDI_APPLICATION
);
7591 wndclass
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)IDC_ARROW
);
7592 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
7593 wndclass
.lpszClassName
= class_name
;
7594 wndclass
.lpszMenuName
= NULL
;
7596 RegisterClassExA(&wndclass
);
7598 hWnd
= CreateWindowA(class_name
, "ws2_32 async message window", WS_OVERLAPPEDWINDOW
,
7599 0, 0, 500, 500, NULL
, NULL
, GetModuleHandleA(NULL
), NULL
);
7602 ok(0, "failed to create window: %u\n", GetLastError());
7609 static void test_WSAAsyncGetServByPort(void)
7611 HWND hwnd
= create_async_message_window();
7613 char buffer
[MAXGETHOSTSTRUCT
];
7618 /* FIXME: The asynchronous window messages should be tested. */
7620 /* Parameters are not checked when initiating the asynchronous operation. */
7621 ret
= WSAAsyncGetServByPort(NULL
, 0, 0, NULL
, NULL
, 0);
7622 ok(ret
!= NULL
, "WSAAsyncGetServByPort returned NULL\n");
7624 ret
= WSAAsyncGetServByPort(hwnd
, WM_ASYNCCOMPLETE
, 0, NULL
, NULL
, 0);
7625 ok(ret
!= NULL
, "WSAAsyncGetServByPort returned NULL\n");
7627 ret
= WSAAsyncGetServByPort(hwnd
, WM_ASYNCCOMPLETE
, htons(80), NULL
, NULL
, 0);
7628 ok(ret
!= NULL
, "WSAAsyncGetServByPort returned NULL\n");
7630 ret
= WSAAsyncGetServByPort(hwnd
, WM_ASYNCCOMPLETE
, htons(80), NULL
, buffer
, MAXGETHOSTSTRUCT
);
7631 ok(ret
!= NULL
, "WSAAsyncGetServByPort returned NULL\n");
7633 DestroyWindow(hwnd
);
7636 static void test_WSAAsyncGetServByName(void)
7638 HWND hwnd
= create_async_message_window();
7640 char buffer
[MAXGETHOSTSTRUCT
];
7645 /* FIXME: The asynchronous window messages should be tested. */
7647 /* Parameters are not checked when initiating the asynchronous operation. */
7648 ret
= WSAAsyncGetServByName(hwnd
, WM_ASYNCCOMPLETE
, "", NULL
, NULL
, 0);
7649 ok(ret
!= NULL
, "WSAAsyncGetServByName returned NULL\n");
7651 ret
= WSAAsyncGetServByName(hwnd
, WM_ASYNCCOMPLETE
, "", "", buffer
, MAXGETHOSTSTRUCT
);
7652 ok(ret
!= NULL
, "WSAAsyncGetServByName returned NULL\n");
7654 ret
= WSAAsyncGetServByName(hwnd
, WM_ASYNCCOMPLETE
, "http", NULL
, NULL
, 0);
7655 ok(ret
!= NULL
, "WSAAsyncGetServByName returned NULL\n");
7657 ret
= WSAAsyncGetServByName(hwnd
, WM_ASYNCCOMPLETE
, "http", "tcp", buffer
, MAXGETHOSTSTRUCT
);
7658 ok(ret
!= NULL
, "WSAAsyncGetServByName returned NULL\n");
7660 DestroyWindow(hwnd
);
7664 * Provide consistent initialization for the AcceptEx IOCP tests.
7666 static SOCKET
setup_iocp_src(struct sockaddr_in
*bindAddress
)
7668 SOCKET src
, ret
= INVALID_SOCKET
;
7671 src
= socket(AF_INET
, SOCK_STREAM
, 0);
7672 if (src
== INVALID_SOCKET
)
7674 skip("could not create listener socket, error %d\n", WSAGetLastError());
7678 memset(bindAddress
, 0, sizeof(*bindAddress
));
7679 bindAddress
->sin_family
= AF_INET
;
7680 bindAddress
->sin_addr
.s_addr
= inet_addr("127.0.0.1");
7681 iret
= bind(src
, (struct sockaddr
*)bindAddress
, sizeof(*bindAddress
));
7684 skip("failed to bind, error %d\n", WSAGetLastError());
7688 socklen
= sizeof(*bindAddress
);
7689 iret
= getsockname(src
, (struct sockaddr
*)bindAddress
, &socklen
);
7691 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
7695 if (set_blocking(src
, FALSE
))
7697 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
7701 iret
= listen(src
, 5);
7704 skip("listening failed, errno = %d\n", WSAGetLastError());
7710 if (src
!= ret
&& ret
== INVALID_SOCKET
)
7715 static void test_completion_port(void)
7717 HANDLE previous_port
, io_port
;
7718 WSAOVERLAPPED ov
, *olp
;
7719 SOCKET src
, dest
, dup
, connector
= INVALID_SOCKET
;
7720 WSAPROTOCOL_INFOA info
;
7723 DWORD num_bytes
, flags
;
7728 struct sockaddr_in bindAddress
;
7729 GUID acceptExGuid
= WSAID_ACCEPTEX
;
7730 LPFN_ACCEPTEX pAcceptEx
= NULL
;
7732 memset(buf
, 0, sizeof(buf
));
7733 previous_port
= CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL
, 0, 0);
7734 ok( previous_port
!= NULL
, "Failed to create completion port %u\n", GetLastError());
7736 memset(&ov
, 0, sizeof(ov
));
7738 tcp_socketpair(&src
, &dest
);
7739 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
7741 skip("failed to create sockets\n");
7745 bufs
.len
= sizeof(buf
);
7751 iret
= setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
));
7752 ok(!iret
, "Failed to set linger %d\n", GetLastError());
7754 io_port
= CreateIoCompletionPort( (HANDLE
)dest
, previous_port
, 125, 0 );
7755 ok(io_port
!= NULL
, "Failed to create completion port %u\n", GetLastError());
7757 SetLastError(0xdeadbeef);
7759 iret
= WSARecv(dest
, &bufs
, 1, &num_bytes
, &flags
, &ov
, NULL
);
7760 ok(iret
== SOCKET_ERROR
, "WSARecv returned %d\n", iret
);
7761 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
7766 src
= INVALID_SOCKET
;
7768 SetLastError(0xdeadbeef);
7770 num_bytes
= 0xdeadbeef;
7771 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7773 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
7774 todo_wine
ok(bret
== FALSE
, "GetQueuedCompletionStatus returned %d\n", bret
);
7775 todo_wine
ok(GetLastError() == ERROR_NETNAME_DELETED
, "Last error was %d\n", GetLastError());
7776 ok(key
== 125, "Key is %lu\n", key
);
7777 ok(num_bytes
== 0, "Number of bytes received is %u\n", num_bytes
);
7778 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
7780 SetLastError(0xdeadbeef);
7782 num_bytes
= 0xdeadbeef;
7783 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7785 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
7786 ok(bret
== FALSE
, "GetQueuedCompletionStatus returned %d\n", bret
);
7787 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
7788 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
7789 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
7790 ok(!olp
, "Overlapped structure is at %p\n", olp
);
7792 if (dest
!= INVALID_SOCKET
)
7795 memset(&ov
, 0, sizeof(ov
));
7797 tcp_socketpair(&src
, &dest
);
7798 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
7800 skip("failed to create sockets\n");
7804 bufs
.len
= sizeof(buf
);
7810 iret
= setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
));
7811 ok(!iret
, "Failed to set linger %d\n", GetLastError());
7813 io_port
= CreateIoCompletionPort((HANDLE
)dest
, previous_port
, 125, 0);
7814 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
7816 set_blocking(dest
, FALSE
);
7819 src
= INVALID_SOCKET
;
7823 num_bytes
= 0xdeadbeef;
7824 SetLastError(0xdeadbeef);
7826 iret
= WSASend(dest
, &bufs
, 1, &num_bytes
, 0, &ov
, NULL
);
7827 ok(iret
== SOCKET_ERROR
, "WSASend failed - %d\n", iret
);
7828 ok(GetLastError() == WSAECONNRESET
, "Last error was %d\n", GetLastError());
7829 ok(num_bytes
== 0xdeadbeef, "Managed to send %d\n", num_bytes
);
7831 SetLastError(0xdeadbeef);
7833 num_bytes
= 0xdeadbeef;
7834 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7836 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
7837 ok(bret
== FALSE
, "GetQueuedCompletionStatus returned %u\n", bret
);
7838 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
7839 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
7840 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
7841 ok(!olp
, "Overlapped structure is at %p\n", olp
);
7843 if (dest
!= INVALID_SOCKET
)
7846 dest
= socket(AF_INET
, SOCK_STREAM
, 0);
7847 if (dest
== INVALID_SOCKET
)
7849 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
7853 iret
= WSAIoctl(dest
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
7854 &pAcceptEx
, sizeof(pAcceptEx
), &num_bytes
, NULL
, NULL
);
7857 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
7861 /* Test IOCP response on socket close (IOCP created after AcceptEx) */
7863 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
7866 SetLastError(0xdeadbeef);
7868 bret
= pAcceptEx(src
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
7869 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7871 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
7872 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
7874 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
7875 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
7878 src
= INVALID_SOCKET
;
7880 SetLastError(0xdeadbeef);
7882 num_bytes
= 0xdeadbeef;
7883 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7885 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
7886 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7887 ok(GetLastError() == ERROR_OPERATION_ABORTED
, "Last error was %d\n", GetLastError());
7888 ok(key
== 125, "Key is %lu\n", key
);
7889 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
7890 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
7891 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_CANCELLED
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
7893 SetLastError(0xdeadbeef);
7895 num_bytes
= 0xdeadbeef;
7896 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7897 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
7898 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7899 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
7900 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
7901 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
7902 ok(!olp
, "Overlapped structure is at %p\n", olp
);
7904 /* Test IOCP response on socket close (IOCP created before AcceptEx) */
7906 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
7909 SetLastError(0xdeadbeef);
7911 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
7912 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
7914 bret
= pAcceptEx(src
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
7915 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7917 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
7918 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
7921 src
= INVALID_SOCKET
;
7923 SetLastError(0xdeadbeef);
7925 num_bytes
= 0xdeadbeef;
7926 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7928 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
7929 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7930 ok(GetLastError() == ERROR_OPERATION_ABORTED
, "Last error was %d\n", GetLastError());
7931 ok(key
== 125, "Key is %lu\n", key
);
7932 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
7933 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
7934 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_CANCELLED
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
7936 SetLastError(0xdeadbeef);
7938 num_bytes
= 0xdeadbeef;
7939 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7940 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
7941 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7942 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
7943 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
7944 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
7945 ok(!olp
, "Overlapped structure is at %p\n", olp
);
7947 /* Test IOCP with duplicated handle */
7949 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
7952 SetLastError(0xdeadbeef);
7954 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
7955 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
7957 WSADuplicateSocketA( src
, GetCurrentProcessId(), &info
);
7958 dup
= WSASocketA(AF_INET
, SOCK_STREAM
, 0, &info
, 0, WSA_FLAG_OVERLAPPED
);
7959 ok(dup
!= INVALID_SOCKET
, "failed to duplicate socket!\n");
7961 bret
= pAcceptEx(dup
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
7962 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
7964 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
7965 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
7967 SetLastError(0xdeadbeef);
7969 num_bytes
= 0xdeadbeef;
7970 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7971 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
7972 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7973 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
7974 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
7975 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
7976 ok(!olp
, "Overlapped structure is at %p\n", olp
);
7979 src
= INVALID_SOCKET
;
7981 dup
= INVALID_SOCKET
;
7983 SetLastError(0xdeadbeef);
7985 num_bytes
= 0xdeadbeef;
7986 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7987 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
7988 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
7989 ok(GetLastError() == ERROR_OPERATION_ABORTED
, "Last error was %d\n", GetLastError());
7990 ok(key
== 125, "Key is %lu\n", key
);
7991 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
7992 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
7993 ok(olp
&& olp
->Internal
== (ULONG
)STATUS_CANCELLED
, "Internal status is %lx\n", olp
? olp
->Internal
: 0);
7995 SetLastError(0xdeadbeef);
7997 num_bytes
= 0xdeadbeef;
7998 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
7999 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8000 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8001 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8002 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8003 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8004 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8006 /* Test IOCP with duplicated handle (closing duplicated handle) */
8008 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8011 SetLastError(0xdeadbeef);
8013 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8014 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8016 WSADuplicateSocketA( src
, GetCurrentProcessId(), &info
);
8017 dup
= WSASocketA(AF_INET
, SOCK_STREAM
, 0, &info
, 0, WSA_FLAG_OVERLAPPED
);
8018 ok(dup
!= INVALID_SOCKET
, "failed to duplicate socket!\n");
8020 bret
= pAcceptEx(dup
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
8021 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
8023 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
8024 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
8027 dup
= INVALID_SOCKET
;
8029 SetLastError(0xdeadbeef);
8031 num_bytes
= 0xdeadbeef;
8032 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8033 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8034 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8035 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8036 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8037 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8038 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8040 SetLastError(0xdeadbeef);
8042 num_bytes
= 0xdeadbeef;
8043 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8044 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8045 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8046 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8047 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8048 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8049 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8052 src
= INVALID_SOCKET
;
8054 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
8055 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8056 ok(GetLastError() == ERROR_OPERATION_ABORTED
, "Last error was %d\n", GetLastError());
8057 ok(key
== 125, "Key is %lu\n", key
);
8058 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
8059 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
8060 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_CANCELLED
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
8062 SetLastError(0xdeadbeef);
8064 num_bytes
= 0xdeadbeef;
8065 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8066 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8067 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8068 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8069 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8070 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8071 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8073 /* Test IOCP with duplicated handle (closing original handle) */
8075 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8078 SetLastError(0xdeadbeef);
8080 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8081 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8083 WSADuplicateSocketA( src
, GetCurrentProcessId(), &info
);
8084 dup
= WSASocketA(AF_INET
, SOCK_STREAM
, 0, &info
, 0, WSA_FLAG_OVERLAPPED
);
8085 ok(dup
!= INVALID_SOCKET
, "failed to duplicate socket!\n");
8087 bret
= pAcceptEx(dup
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
8088 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
8090 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
8091 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
8094 src
= INVALID_SOCKET
;
8096 SetLastError(0xdeadbeef);
8098 num_bytes
= 0xdeadbeef;
8099 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8100 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8101 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8102 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8103 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8104 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8105 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8108 dup
= INVALID_SOCKET
;
8110 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
8111 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8112 ok(GetLastError() == ERROR_OPERATION_ABORTED
, "Last error was %d\n", GetLastError());
8113 ok(key
== 125, "Key is %lu\n", key
);
8114 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
8115 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
8116 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_CANCELLED
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
8118 SetLastError(0xdeadbeef);
8120 num_bytes
= 0xdeadbeef;
8121 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8122 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8123 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8124 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8125 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8126 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8127 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8129 /* Test IOCP without AcceptEx */
8131 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8134 SetLastError(0xdeadbeef);
8136 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8137 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8140 src
= INVALID_SOCKET
;
8142 SetLastError(0xdeadbeef);
8144 num_bytes
= 0xdeadbeef;
8145 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8146 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8147 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8148 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8149 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8150 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8151 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8155 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8158 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
8159 if (connector
== INVALID_SOCKET
) {
8160 skip("could not create connector socket, error %d\n", WSAGetLastError());
8164 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8165 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8167 io_port
= CreateIoCompletionPort((HANDLE
)dest
, previous_port
, 236, 0);
8168 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8170 bret
= pAcceptEx(src
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
8171 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
8173 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
8174 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
8176 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
8177 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", GetLastError());
8179 closesocket(connector
);
8180 connector
= INVALID_SOCKET
;
8182 SetLastError(0xdeadbeef);
8184 num_bytes
= 0xdeadbeef;
8185 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8187 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
8188 ok(bret
== TRUE
, "failed to get completion status %u\n", bret
);
8189 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
8190 ok(key
== 125, "Key is %lu\n", key
);
8191 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
8192 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
8193 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_SUCCESS
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
8195 SetLastError(0xdeadbeef);
8197 num_bytes
= 0xdeadbeef;
8198 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8199 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8200 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8201 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8202 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8203 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8204 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8206 if (dest
!= INVALID_SOCKET
)
8208 if (src
!= INVALID_SOCKET
)
8213 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8216 dest
= socket(AF_INET
, SOCK_STREAM
, 0);
8217 if (dest
== INVALID_SOCKET
)
8219 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8223 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
8224 if (connector
== INVALID_SOCKET
) {
8225 skip("could not create connector socket, error %d\n", WSAGetLastError());
8229 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8230 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8232 io_port
= CreateIoCompletionPort((HANDLE
)dest
, previous_port
, 236, 0);
8233 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8235 bret
= pAcceptEx(src
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
8236 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
8238 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
8239 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
8241 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
8242 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", GetLastError());
8244 iret
= send(connector
, buf
, 1, 0);
8245 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
8250 dest
= INVALID_SOCKET
;
8252 SetLastError(0xdeadbeef);
8254 num_bytes
= 0xdeadbeef;
8255 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8257 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
8258 ok(bret
== TRUE
, "failed to get completion status %u\n", bret
);
8259 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
8260 ok(key
== 125, "Key is %lu\n", key
);
8261 ok(num_bytes
== 1, "Number of bytes transferred is %u\n", num_bytes
);
8262 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
8263 ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_SUCCESS
), "Internal status is %lx\n", olp
? olp
->Internal
: 0);
8265 SetLastError(0xdeadbeef);
8267 num_bytes
= 0xdeadbeef;
8268 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8269 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8270 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8271 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8272 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8273 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8274 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8276 if (src
!= INVALID_SOCKET
)
8278 if (connector
!= INVALID_SOCKET
)
8279 closesocket(connector
);
8283 if ((src
= setup_iocp_src(&bindAddress
)) == INVALID_SOCKET
)
8286 dest
= socket(AF_INET
, SOCK_STREAM
, 0);
8287 if (dest
== INVALID_SOCKET
)
8289 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
8293 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
8294 if (connector
== INVALID_SOCKET
) {
8295 skip("could not create connector socket, error %d\n", WSAGetLastError());
8299 io_port
= CreateIoCompletionPort((HANDLE
)src
, previous_port
, 125, 0);
8300 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8302 io_port
= CreateIoCompletionPort((HANDLE
)dest
, previous_port
, 236, 0);
8303 ok(io_port
!= NULL
, "failed to create completion port %u\n", GetLastError());
8305 bret
= pAcceptEx(src
, dest
, buf
, sizeof(buf
) - 2*(sizeof(struct sockaddr_in
) + 16),
8306 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
8308 ok(bret
== FALSE
, "AcceptEx returned %d\n", bret
);
8309 ok(GetLastError() == ERROR_IO_PENDING
, "Last error was %d\n", GetLastError());
8311 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
8312 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", GetLastError());
8315 dest
= INVALID_SOCKET
;
8317 SetLastError(0xdeadbeef);
8319 num_bytes
= 0xdeadbeef;
8320 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8322 bret
= GetQueuedCompletionStatus(io_port
, &num_bytes
, &key
, &olp
, 100);
8323 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8324 todo_wine
ok(GetLastError() == ERROR_NETNAME_DELETED
||
8325 GetLastError() == ERROR_OPERATION_ABORTED
||
8326 GetLastError() == ERROR_CONNECTION_ABORTED
||
8327 GetLastError() == ERROR_PIPE_NOT_CONNECTED
/* win 2000 */,
8328 "Last error was %d\n", GetLastError());
8329 ok(key
== 125, "Key is %lu\n", key
);
8330 ok(num_bytes
== 0, "Number of bytes transferred is %u\n", num_bytes
);
8331 ok(olp
== &ov
, "Overlapped structure is at %p\n", olp
);
8332 todo_wine
ok(olp
&& (olp
->Internal
== (ULONG
)STATUS_LOCAL_DISCONNECT
||
8333 olp
->Internal
== (ULONG
)STATUS_CANCELLED
||
8334 olp
->Internal
== (ULONG
)STATUS_CONNECTION_ABORTED
||
8335 olp
->Internal
== (ULONG
)STATUS_PIPE_DISCONNECTED
/* win 2000 */),
8336 "Internal status is %lx\n", olp
? olp
->Internal
: 0);
8338 SetLastError(0xdeadbeef);
8340 num_bytes
= 0xdeadbeef;
8341 olp
= (WSAOVERLAPPED
*)0xdeadbeef;
8342 bret
= GetQueuedCompletionStatus( io_port
, &num_bytes
, &key
, &olp
, 200 );
8343 ok(bret
== FALSE
, "failed to get completion status %u\n", bret
);
8344 ok(GetLastError() == WAIT_TIMEOUT
, "Last error was %d\n", GetLastError());
8345 ok(key
== 0xdeadbeef, "Key is %lu\n", key
);
8346 ok(num_bytes
== 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes
);
8347 ok(!olp
, "Overlapped structure is at %p\n", olp
);
8351 if (dest
!= INVALID_SOCKET
)
8353 if (src
!= INVALID_SOCKET
)
8355 if (connector
!= INVALID_SOCKET
)
8356 closesocket(connector
);
8357 CloseHandle(previous_port
);
8360 static void test_address_list_query(void)
8362 SOCKET_ADDRESS_LIST
*address_list
;
8363 DWORD bytes_returned
, size
;
8368 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
8369 ok(s
!= INVALID_SOCKET
, "Failed to create socket, error %d.\n", WSAGetLastError());
8372 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, NULL
, 0, &bytes_returned
, NULL
, NULL
);
8373 ok(ret
== SOCKET_ERROR
, "Got unexpected ret %d.\n", ret
);
8374 if(WSAGetLastError() == WSAEINVAL
)
8376 win_skip("Windows <= NT4 is not supported in this test\n");
8380 ok(WSAGetLastError() == WSAEFAULT
, "Got unexpected error %d.\n", WSAGetLastError());
8381 ok(bytes_returned
>= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]),
8382 "Got unexpected bytes_returned %u.\n", bytes_returned
);
8384 size
= bytes_returned
;
8386 address_list
= HeapAlloc(GetProcessHeap(), 0, size
* 2);
8387 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, address_list
, size
* 2, &bytes_returned
, NULL
, NULL
);
8388 ok(!ret
, "Got unexpected ret %d, error %d.\n", ret
, WSAGetLastError());
8389 ok(bytes_returned
== size
, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned
, size
);
8391 bytes_returned
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[address_list
->iAddressCount
]);
8392 for (i
= 0; i
< address_list
->iAddressCount
; ++i
)
8394 bytes_returned
+= address_list
->Address
[i
].iSockaddrLength
;
8396 ok(size
== bytes_returned
, "Got unexpected size %u, expected %u.\n", size
, bytes_returned
);
8398 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, address_list
, size
, NULL
, NULL
, NULL
);
8399 ok(ret
== SOCKET_ERROR
, "Got unexpected ret %d.\n", ret
);
8400 ok(WSAGetLastError() == WSAEFAULT
, "Got unexpected error %d.\n", WSAGetLastError());
8402 bytes_returned
= 0xdeadbeef;
8403 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, NULL
, size
, &bytes_returned
, NULL
, NULL
);
8404 ok(ret
== SOCKET_ERROR
, "Got unexpected ret %d.\n", ret
);
8405 ok(WSAGetLastError() == WSAEFAULT
, "Got unexpected error %d.\n", WSAGetLastError());
8406 ok(bytes_returned
== size
, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned
, size
);
8408 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, address_list
, 1, &bytes_returned
, NULL
, NULL
);
8409 ok(ret
== SOCKET_ERROR
, "Got unexpected ret %d.\n", ret
);
8410 ok(WSAGetLastError() == WSAEINVAL
, "Got unexpected error %d.\n", WSAGetLastError());
8411 ok(bytes_returned
== 0, "Got unexpected bytes_returned %u.\n", bytes_returned
);
8413 ret
= WSAIoctl(s
, SIO_ADDRESS_LIST_QUERY
, NULL
, 0, address_list
,
8414 FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]), &bytes_returned
, NULL
, NULL
);
8415 ok(ret
== SOCKET_ERROR
, "Got unexpected ret %d.\n", ret
);
8416 ok(WSAGetLastError() == WSAEFAULT
, "Got unexpected error %d.\n", WSAGetLastError());
8417 ok(bytes_returned
== size
, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned
, size
);
8419 HeapFree(GetProcessHeap(), 0, address_list
);
8423 static DWORD WINAPI
inet_ntoa_thread_proc(void *param
)
8427 HANDLE
*event
= param
;
8429 addr
= inet_addr("4.3.2.1");
8430 ok(addr
== htonl(0x04030201), "expected 0x04030201, got %08x\n", addr
);
8431 str
= inet_ntoa(*(struct in_addr
*)&addr
);
8432 ok(!strcmp(str
, "4.3.2.1"), "expected 4.3.2.1, got %s\n", str
);
8435 WaitForSingleObject(event
[1], 3000);
8440 static void test_inet_ntoa(void)
8444 HANDLE thread
, event
[2];
8447 addr
= inet_addr("1.2.3.4");
8448 ok(addr
== htonl(0x01020304), "expected 0x01020304, got %08x\n", addr
);
8449 str
= inet_ntoa(*(struct in_addr
*)&addr
);
8450 ok(!strcmp(str
, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str
);
8452 event
[0] = CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
8453 event
[1] = CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
8455 thread
= CreateThread(NULL
, 0, inet_ntoa_thread_proc
, event
, 0, &tid
);
8456 WaitForSingleObject(event
[0], 3000);
8458 ok(!strcmp(str
, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str
);
8461 WaitForSingleObject(thread
, 3000);
8463 CloseHandle(event
[0]);
8464 CloseHandle(event
[1]);
8465 CloseHandle(thread
);
8468 static void test_WSALookupService(void)
8470 char buffer
[4096], strbuff
[128];
8471 WSAQUERYSETW
*qs
= NULL
;
8475 DWORD error
, offset
, bsize
;
8477 if (!pWSALookupServiceBeginW
|| !pWSALookupServiceEnd
|| !pWSALookupServiceNextW
)
8479 win_skip("WSALookupServiceBeginW or WSALookupServiceEnd or WSALookupServiceNextW not found\n");
8483 qs
= (WSAQUERYSETW
*)buffer
;
8484 memset(qs
, 0, sizeof(*qs
));
8486 /* invalid parameter tests */
8487 ret
= pWSALookupServiceBeginW(NULL
, 0, &hnd
);
8488 error
= WSAGetLastError();
8489 ok(ret
== SOCKET_ERROR
, "WSALookupServiceBeginW should have failed\n");
8491 ok(error
== WSAEFAULT
, "expected 10014, got %d\n", error
);
8493 ret
= pWSALookupServiceBeginW(qs
, 0, NULL
);
8494 error
= WSAGetLastError();
8495 ok(ret
== SOCKET_ERROR
, "WSALookupServiceBeginW should have failed\n");
8497 ok(error
== WSAEFAULT
, "expected 10014, got %d\n", error
);
8499 ret
= pWSALookupServiceBeginW(qs
, 0, &hnd
);
8500 error
= WSAGetLastError();
8501 ok(ret
== SOCKET_ERROR
, "WSALookupServiceBeginW should have failed\n");
8503 ok(error
== WSAEINVAL
8504 || broken(error
== ERROR_INVALID_PARAMETER
) /* == XP */
8505 || broken(error
== WSAEFAULT
) /* == NT */
8506 || broken(error
== WSASERVICE_NOT_FOUND
) /* == 2000 */,
8507 "expected 10022, got %d\n", error
);
8509 ret
= pWSALookupServiceEnd(NULL
);
8510 error
= WSAGetLastError();
8512 ok(ret
== SOCKET_ERROR
, "WSALookupServiceEnd should have failed\n");
8514 ok(error
== ERROR_INVALID_HANDLE
, "expected 6, got %d\n", error
);
8516 /* standard network list query */
8517 qs
->dwSize
= sizeof(*qs
);
8518 hnd
= (HANDLE
)0xdeadbeef;
8519 ret
= pWSALookupServiceBeginW(qs
, LUP_RETURN_ALL
| LUP_DEEP
, &hnd
);
8520 error
= WSAGetLastError();
8521 if(ret
&& error
== ERROR_INVALID_PARAMETER
)
8523 win_skip("the current WSALookupServiceBeginW test is not supported in win <= 2000\n");
8528 ok(!ret
, "WSALookupServiceBeginW failed unexpectedly with error %d\n", error
);
8530 ok(hnd
!= (HANDLE
)0xdeadbeef, "Handle was not filled\n");
8535 memset(qs
, 0, sizeof(*qs
));
8536 bsize
= sizeof(buffer
);
8538 if (pWSALookupServiceNextW(hnd
, 0, &bsize
, qs
) == SOCKET_ERROR
)
8540 error
= WSAGetLastError();
8541 if (error
== WSA_E_NO_MORE
) break;
8542 ok(0, "Error %d happened while listing services\n", error
);
8546 WideCharToMultiByte(CP_ACP
, 0, qs
->lpszServiceInstanceName
, -1,
8547 strbuff
, sizeof(strbuff
), NULL
, NULL
);
8548 trace("Network Name: %s\n", strbuff
);
8550 /* network data is written in the blob field */
8553 /* each network may have multiple NLA_BLOB information structures */
8556 netdata
= (PNLA_BLOB
) &qs
->lpBlob
->pBlobData
[offset
];
8557 switch (netdata
->header
.type
)
8560 trace("\tNLA Data Type: NLA_RAW_DATA\n");
8563 trace("\tNLA Data Type: NLA_INTERFACE\n");
8564 trace("\t\tType: %d\n", netdata
->data
.interfaceData
.dwType
);
8565 trace("\t\tSpeed: %d\n", netdata
->data
.interfaceData
.dwSpeed
);
8566 trace("\t\tAdapter Name: %s\n", netdata
->data
.interfaceData
.adapterName
);
8568 case NLA_802_1X_LOCATION
:
8569 trace("\tNLA Data Type: NLA_802_1X_LOCATION\n");
8570 trace("\t\tInformation: %s\n", netdata
->data
.locationData
.information
);
8572 case NLA_CONNECTIVITY
:
8573 switch (netdata
->data
.connectivity
.type
)
8575 case NLA_NETWORK_AD_HOC
:
8576 trace("\t\tNetwork Type: AD HOC\n");
8578 case NLA_NETWORK_MANAGED
:
8579 trace("\t\tNetwork Type: Managed\n");
8581 case NLA_NETWORK_UNMANAGED
:
8582 trace("\t\tNetwork Type: Unmanaged\n");
8584 case NLA_NETWORK_UNKNOWN
:
8585 trace("\t\tNetwork Type: Unknown\n");
8587 switch (netdata
->data
.connectivity
.internet
)
8589 case NLA_INTERNET_NO
:
8590 trace("\t\tInternet connectivity: No\n");
8592 case NLA_INTERNET_YES
:
8593 trace("\t\tInternet connectivity: Yes\n");
8595 case NLA_INTERNET_UNKNOWN
:
8596 trace("\t\tInternet connectivity: Unknown\n");
8601 trace("\tNLA Data Type: NLA_ICS\n");
8602 trace("\t\tSpeed: %d\n",
8603 netdata
->data
.ICS
.remote
.speed
);
8604 trace("\t\tType: %d\n",
8605 netdata
->data
.ICS
.remote
.type
);
8606 trace("\t\tState: %d\n",
8607 netdata
->data
.ICS
.remote
.state
);
8608 WideCharToMultiByte(CP_ACP
, 0, netdata
->data
.ICS
.remote
.machineName
, -1,
8609 strbuff
, sizeof(strbuff
), NULL
, NULL
);
8610 trace("\t\tMachine Name: %s\n", strbuff
);
8611 WideCharToMultiByte(CP_ACP
, 0, netdata
->data
.ICS
.remote
.sharedAdapterName
, -1,
8612 strbuff
, sizeof(strbuff
), NULL
, NULL
);
8613 trace("\t\tShared Adapter Name: %s\n", strbuff
);
8616 trace("\tNLA Data Type: Unknown\n");
8625 ret
= pWSALookupServiceEnd(hnd
);
8626 ok(!ret
, "WSALookupServiceEnd failed unexpectedly\n");
8629 /**************** Main program ***************/
8635 /* Leave these tests at the beginning. They depend on WSAStartup not having been
8636 * called, which is done by Init() below. */
8637 test_WithoutWSAStartup();
8638 test_WithWSAStartup();
8644 test_set_getsockopt();
8645 test_so_reuseaddr();
8647 test_extendedSocketOptions();
8649 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++)
8651 trace ( " **** STARTING TEST %d ****\n", i
);
8652 do_test ( &tests
[i
] );
8653 trace ( " **** TEST %d COMPLETE ****\n", i
);
8658 test_getservbyname();
8660 test_WSADuplicateSocket();
8661 test_WSAEnumNetworkEvents();
8663 test_WSAAddressToStringA();
8664 test_WSAAddressToStringW();
8666 test_WSAStringToAddressA();
8667 test_WSAStringToAddressW();
8676 test_addr_to_print();
8679 test_gethostbyname_hack();
8690 test_GetAddrInfoW();
8695 test_sioRoutingInterfaceQuery();
8696 test_sioAddressListChange();
8698 test_WSALookupService();
8700 test_WSAAsyncGetServByPort();
8701 test_WSAAsyncGetServByName();
8703 test_completion_port();
8704 test_address_list_query();
8706 /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */
8708 test_synchronous_WSAIoctl();