ws2_32/tests: Clean up and expand tests for WSAPoll().
[wine.git] / dlls / ws2_32 / tests / sock.c
blobf16a17aa7b300456805fecff75e1c424782f3c58
1 /*
2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
7 * Copyright 2017 Dmitry Timoshkov
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <ntstatus.h>
25 #define WIN32_NO_STATUS
26 #include <winsock2.h>
27 #include <windows.h>
28 #include <winternl.h>
29 #include <ws2tcpip.h>
30 #include <wsipx.h>
31 #include <wsnwlink.h>
32 #include <mswsock.h>
33 #include <mstcpip.h>
34 #include <stdio.h>
35 #include "wine/test.h"
37 #define MAX_CLIENTS 4 /* Max number of clients */
38 #define FIRST_CHAR 'A' /* First character in transferred pattern */
39 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
40 #define BIND_TRIES 6 /* Number of bind() attempts */
41 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
42 after server initialization, if something hangs */
44 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
46 #define SERVERIP "127.0.0.1" /* IP to bind to */
47 #define SERVERPORT 9374 /* Port number to bind to */
49 #define wsa_ok(op, cond, msg) \
50 do { \
51 int tmp, err = 0; \
52 tmp = op; \
53 if ( !(cond tmp) ) err = WSAGetLastError(); \
54 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
55 } while (0);
57 #define make_keepalive(k, enable, time, interval) \
58 k.onoff = enable; \
59 k.keepalivetime = time; \
60 k.keepaliveinterval = interval;
62 /* Function pointers */
63 static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT);
65 /* Function pointers from ntdll */
66 static DWORD (WINAPI *pNtClose)(HANDLE);
68 /**************** Structs and typedefs ***************/
70 typedef struct thread_info
72 HANDLE thread;
73 DWORD id;
74 } thread_info;
76 /* Information in the server about open client connections */
77 typedef struct sock_info
79 SOCKET s;
80 struct sockaddr_in addr;
81 struct sockaddr_in peer;
82 char *buf;
83 int n_recvd;
84 int n_sent;
85 } sock_info;
87 /* Test parameters for both server & client */
88 typedef struct test_params
90 int sock_type;
91 int sock_prot;
92 const char *inet_addr;
93 short inet_port;
94 int chunk_size;
95 int n_chunks;
96 int n_clients;
97 } test_params;
99 /* server-specific test parameters */
100 typedef struct server_params
102 test_params *general;
103 DWORD sock_flags;
104 int buflen;
105 } server_params;
107 /* client-specific test parameters */
108 typedef struct client_params
110 test_params *general;
111 DWORD sock_flags;
112 int buflen;
113 } client_params;
115 /* This type combines all information for setting up a test scenario */
116 typedef struct test_setup
118 test_params general;
119 LPVOID srv;
120 server_params srv_params;
121 LPVOID clt;
122 client_params clt_params;
123 } test_setup;
125 /* Thread local storage for server */
126 typedef struct server_memory
128 SOCKET s;
129 struct sockaddr_in addr;
130 sock_info sock[MAX_CLIENTS];
131 } server_memory;
133 /* Thread local storage for client */
134 typedef struct client_memory
136 SOCKET s;
137 struct sockaddr_in addr;
138 char *send_buf;
139 char *recv_buf;
140 } client_memory;
142 /* SelectReadThread thread parameters */
143 typedef struct select_thread_params
145 SOCKET s;
146 BOOL ReadKilled;
147 } select_thread_params;
149 /**************** Static variables ***************/
151 static DWORD tls; /* Thread local storage index */
152 static HANDLE thread[1+MAX_CLIENTS];
153 static DWORD thread_id[1+MAX_CLIENTS];
154 static HANDLE server_ready;
155 static HANDLE client_ready[MAX_CLIENTS];
156 static int client_id;
158 /**************** General utility functions ***************/
160 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len);
161 static SOCKET setup_connector_socket(const struct sockaddr_in *addr, int len, BOOL nonblock);
163 static void tcp_socketpair_flags(SOCKET *src, SOCKET *dst, DWORD flags)
165 SOCKET server = INVALID_SOCKET;
166 struct sockaddr_in addr;
167 int len, ret;
169 *src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, flags);
170 ok(*src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
172 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, flags);
173 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
175 memset(&addr, 0, sizeof(addr));
176 addr.sin_family = AF_INET;
177 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
178 ret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
179 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
181 len = sizeof(addr);
182 ret = getsockname(server, (struct sockaddr *)&addr, &len);
183 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
185 ret = listen(server, 1);
186 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
188 ret = connect(*src, (struct sockaddr *)&addr, sizeof(addr));
189 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
191 len = sizeof(addr);
192 *dst = accept(server, (struct sockaddr *)&addr, &len);
193 ok(*dst != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
195 closesocket(server);
198 static void tcp_socketpair(SOCKET *src, SOCKET *dst)
200 tcp_socketpair_flags(src, dst, WSA_FLAG_OVERLAPPED);
203 static void set_so_opentype ( BOOL overlapped )
205 int optval = !overlapped, newval, len = sizeof (int);
207 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
208 (LPVOID) &optval, sizeof (optval) ) == 0,
209 "setting SO_OPENTYPE failed\n" );
210 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
211 (LPVOID) &newval, &len ) == 0,
212 "getting SO_OPENTYPE failed\n" );
213 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
216 static int set_blocking ( SOCKET s, BOOL blocking )
218 u_long val = !blocking;
219 return ioctlsocket ( s, FIONBIO, &val );
222 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
224 char c, *p;
225 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
226 memset ( p, c, chunk_size );
229 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
231 char c, *p;
232 int i;
233 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
235 for ( i = 0; i < chunk_size; i++ )
236 if ( p[i] != c ) return i;
238 return -1;
242 * This routine is called when a client / server does not expect any more data,
243 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
245 static void read_zero_bytes ( SOCKET s )
247 char buf[256];
248 int tmp, n = 0;
249 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
250 n += tmp;
251 ok ( n <= 0, "garbage data received: %d bytes\n", n );
254 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen )
256 char* last = buf + buflen, *p;
257 int n = 1;
258 for ( p = buf; n > 0 && p < last; )
260 n = send ( s, p, min ( sendlen, last - p ), flags );
261 if (n > 0) p += n;
263 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
264 return p - buf;
267 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen )
269 char* last = buf + buflen, *p;
270 int n = 1;
271 for ( p = buf; n > 0 && p < last; )
273 n = recv ( s, p, min ( recvlen, last - p ), flags );
274 if (n > 0) p += n;
276 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
277 return p - buf;
280 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen )
282 char* last = buf + buflen, *p;
283 int n = 1;
284 for ( p = buf; n > 0 && p < last; )
286 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen );
287 if (n > 0) p += n;
289 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
290 return p - buf;
294 * Call this routine right after thread startup.
295 * SO_OPENTYPE must by 0, regardless what the server did.
297 static void check_so_opentype (void)
299 int tmp = 1, len;
300 len = sizeof (tmp);
301 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
302 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
305 /**************** Server utility functions ***************/
308 * Even if we have closed our server socket cleanly,
309 * the OS may mark the address "in use" for some time -
310 * this happens with native Linux apps, too.
312 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
314 int err, wsaerr = 0, n_try = BIND_TRIES;
316 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
317 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
318 n_try-- >= 0)
320 trace ( "address in use, waiting ...\n" );
321 Sleep ( 1000 * BIND_SLEEP );
323 ok ( err == 0, "failed to bind: %d\n", wsaerr );
326 static void server_start ( server_params *par )
328 int i;
329 test_params *gen = par->general;
330 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
332 TlsSetValue ( tls, mem );
333 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
334 NULL, 0, par->sock_flags );
335 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
337 mem->addr.sin_family = AF_INET;
338 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
339 mem->addr.sin_port = htons ( gen->inet_port );
341 for (i = 0; i < MAX_CLIENTS; i++)
343 mem->sock[i].s = INVALID_SOCKET;
344 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
345 mem->sock[i].n_recvd = 0;
346 mem->sock[i].n_sent = 0;
349 if ( gen->sock_type == SOCK_STREAM )
350 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
353 static void server_stop (void)
355 int i;
356 server_memory *mem = TlsGetValue ( tls );
358 for (i = 0; i < MAX_CLIENTS; i++ )
360 LocalFree ( mem->sock[i].buf );
361 if ( mem->sock[i].s != INVALID_SOCKET )
362 closesocket ( mem->sock[i].s );
364 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
365 LocalFree ( mem );
366 ExitThread ( GetCurrentThreadId () );
369 /**************** Client utilitiy functions ***************/
371 static void client_start ( client_params *par )
373 test_params *gen = par->general;
374 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
376 TlsSetValue ( tls, mem );
378 WaitForSingleObject ( server_ready, INFINITE );
380 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
381 NULL, 0, par->sock_flags );
383 mem->addr.sin_family = AF_INET;
384 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
385 mem->addr.sin_port = htons ( gen->inet_port );
387 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
389 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
390 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
391 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
393 SetEvent ( client_ready[client_id] );
394 /* Wait for the other clients to come up */
395 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
398 static void client_stop (void)
400 client_memory *mem = TlsGetValue ( tls );
401 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
402 LocalFree ( mem->send_buf );
403 LocalFree ( mem );
404 ExitThread(0);
407 /**************** Servers ***************/
410 * simple_server: A very basic server doing synchronous IO.
412 static VOID WINAPI simple_server ( server_params *par )
414 test_params *gen = par->general;
415 server_memory *mem;
416 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
417 id = GetCurrentThreadId();
419 set_so_opentype ( FALSE ); /* non-overlapped */
420 server_start ( par );
421 mem = TlsGetValue ( tls );
423 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
424 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
426 SetEvent ( server_ready ); /* notify clients */
428 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
430 /* accept a single connection */
431 tmp = sizeof ( mem->sock[0].peer );
432 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
433 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
435 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
436 "simple_server (%x): strange peer address\n", id );
438 /* Receive data & check it */
439 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
440 ok ( n_recvd == n_expected,
441 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
442 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
443 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
445 /* Echo data back */
446 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
447 ok ( n_sent == n_expected,
448 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
450 /* cleanup */
451 read_zero_bytes ( mem->sock[0].s );
452 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
453 mem->sock[0].s = INVALID_SOCKET;
456 server_stop ();
460 * oob_server: A very basic server receiving out-of-band data.
462 static VOID WINAPI oob_server ( server_params *par )
464 test_params *gen = par->general;
465 server_memory *mem;
466 u_long atmark = 0;
467 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp,
468 id = GetCurrentThreadId();
470 set_so_opentype ( FALSE ); /* non-overlapped */
471 server_start ( par );
472 mem = TlsGetValue ( tls );
474 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n");
475 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n");
477 SetEvent ( server_ready ); /* notify clients */
479 /* accept a single connection */
480 tmp = sizeof ( mem->sock[0].peer );
481 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
482 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" );
484 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
485 "oob_server (%x): strange peer address\n", id );
487 /* check initial atmark state */
488 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
489 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
491 /* Receive normal data */
492 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
493 ok ( n_recvd == n_expected,
494 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
495 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
496 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos);
498 /* check atmark state */
499 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
500 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark );
502 /* Echo data back */
503 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
504 ok ( n_sent == n_expected,
505 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
507 /* Receive a part of the out-of-band data and print atmark state */
508 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen );
509 ok ( n_recvd == 8,
510 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 );
511 n_expected -= 8;
513 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
515 /* Receive the rest of the out-of-band data and check atmark state */
516 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen );
518 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark );
519 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark );
521 /* cleanup */
522 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" );
523 mem->sock[0].s = INVALID_SOCKET;
525 server_stop ();
529 * select_server: A non-blocking server.
531 static VOID WINAPI select_server ( server_params *par )
533 test_params *gen = par->general;
534 server_memory *mem;
535 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
536 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
537 n_set, delta, n_ready;
538 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
539 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
541 set_so_opentype ( FALSE ); /* non-overlapped */
542 server_start ( par );
543 mem = TlsGetValue ( tls );
545 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
546 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
548 SetEvent ( server_ready ); /* notify clients */
550 FD_ZERO ( &fds_openrecv );
551 FD_ZERO ( &fds_recv );
552 FD_ZERO ( &fds_send );
553 FD_ZERO ( &fds_opensend );
555 FD_SET ( mem->s, &fds_openrecv );
557 while(1)
559 fds_recv = fds_openrecv;
560 fds_send = fds_opensend;
562 n_set = 0;
564 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
565 "select_server (%x): select() failed: %d\n" );
567 /* check for incoming requests */
568 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
569 n_set += 1;
571 /* accept a single connection */
572 tmp = sizeof ( mem->sock[n_connections].peer );
573 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
574 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
576 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
577 "select_server (%x): strange peer address\n", id );
579 /* add to list of open connections */
580 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
581 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
583 n_connections++;
586 /* handle open requests */
588 for ( i = 0; i < n_connections; i++ )
590 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
591 n_set += 1;
593 if ( mem->sock[i].n_recvd < n_expected ) {
594 /* Receive data & check it */
595 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 );
596 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
597 mem->sock[i].n_recvd += n_recvd;
599 if ( mem->sock[i].n_recvd == n_expected ) {
600 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
601 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
602 FD_CLR ( mem->sock[i].s, &fds_openrecv );
605 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
609 /* only echo back what we've received */
610 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
612 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
613 n_set += 1;
615 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
616 /* Echo data back */
617 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
618 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
619 mem->sock[i].n_sent += n_sent;
621 if ( mem->sock[i].n_sent == n_expected ) {
622 FD_CLR ( mem->sock[i].s, &fds_opensend );
625 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
630 /* check that select returned the correct number of ready sockets */
631 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
633 /* check if all clients are done */
634 if ( ( fds_opensend.fd_count == 0 )
635 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
636 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
637 break;
641 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
643 /* cleanup */
644 read_zero_bytes ( mem->sock[i].s );
645 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
646 mem->sock[i].s = INVALID_SOCKET;
649 server_stop ();
652 /**************** Clients ***************/
655 * simple_client: A very basic client doing synchronous IO.
657 static VOID WINAPI simple_client ( client_params *par )
659 test_params *gen = par->general;
660 client_memory *mem;
661 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
663 id = GetCurrentThreadId();
664 /* wait here because we want to call set_so_opentype before creating a socket */
665 WaitForSingleObject ( server_ready, INFINITE );
667 check_so_opentype ();
668 set_so_opentype ( FALSE ); /* non-overlapped */
669 client_start ( par );
670 mem = TlsGetValue ( tls );
672 /* Connect */
673 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
674 0 ==, "simple_client (%x): connect error: %d\n" );
675 ok ( set_blocking ( mem->s, TRUE ) == 0,
676 "simple_client (%x): failed to set blocking mode\n", id );
678 /* send data to server */
679 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
680 ok ( n_sent == n_expected,
681 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
683 /* shutdown send direction */
684 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
686 /* Receive data echoed back & check it */
687 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
688 ok ( n_recvd == n_expected,
689 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
691 /* check data */
692 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
693 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
695 /* cleanup */
696 read_zero_bytes ( mem->s );
697 client_stop ();
701 * oob_client: A very basic client sending out-of-band data.
703 static VOID WINAPI oob_client ( client_params *par )
705 test_params *gen = par->general;
706 client_memory *mem;
707 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
709 id = GetCurrentThreadId();
710 /* wait here because we want to call set_so_opentype before creating a socket */
711 WaitForSingleObject ( server_ready, INFINITE );
713 check_so_opentype ();
714 set_so_opentype ( FALSE ); /* non-overlapped */
715 client_start ( par );
716 mem = TlsGetValue ( tls );
718 /* Connect */
719 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
720 0 ==, "oob_client (%x): connect error: %d\n" );
721 ok ( set_blocking ( mem->s, TRUE ) == 0,
722 "oob_client (%x): failed to set blocking mode\n", id );
724 /* send data to server */
725 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
726 ok ( n_sent == n_expected,
727 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
729 /* Receive data echoed back & check it */
730 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen );
731 ok ( n_recvd == n_expected,
732 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
733 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
734 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
736 /* send out-of-band data to server */
737 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen );
738 ok ( n_sent == n_expected,
739 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
741 /* shutdown send direction */
742 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
744 /* cleanup */
745 read_zero_bytes ( mem->s );
746 client_stop ();
750 * simple_mixed_client: mixing send and recvfrom
752 static VOID WINAPI simple_mixed_client ( client_params *par )
754 test_params *gen = par->general;
755 client_memory *mem;
756 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
757 int fromLen = sizeof(mem->addr);
758 struct sockaddr test;
760 id = GetCurrentThreadId();
761 /* wait here because we want to call set_so_opentype before creating a socket */
762 WaitForSingleObject ( server_ready, INFINITE );
764 check_so_opentype ();
765 set_so_opentype ( FALSE ); /* non-overlapped */
766 client_start ( par );
767 mem = TlsGetValue ( tls );
769 /* Connect */
770 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
771 0 ==, "simple_client (%x): connect error: %d\n" );
772 ok ( set_blocking ( mem->s, TRUE ) == 0,
773 "simple_client (%x): failed to set blocking mode\n", id );
775 /* send data to server */
776 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen );
777 ok ( n_sent == n_expected,
778 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
780 /* shutdown send direction */
781 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
783 /* this shouldn't change, since lpFrom, is not updated on
784 connection oriented sockets - exposed by bug 11640
786 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
788 /* Receive data echoed back & check it */
789 n_recvd = do_synchronous_recvfrom ( mem->s,
790 mem->recv_buf,
791 n_expected,
793 (struct sockaddr *)&test,
794 &fromLen,
795 par->buflen );
796 ok ( n_recvd == n_expected,
797 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
799 /* check that lpFrom was not updated */
800 ok(0 ==
801 strcmp(
802 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
803 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
805 /* check data */
806 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
807 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
809 /* cleanup */
810 read_zero_bytes ( mem->s );
811 client_stop ();
815 * event_client: An event-driven client
817 static void WINAPI event_client ( client_params *par )
819 test_params *gen = par->general;
820 client_memory *mem;
821 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
822 tmp, err, n;
823 HANDLE event;
824 WSANETWORKEVENTS wsa_events;
825 char *send_last, *recv_last, *send_p, *recv_p;
826 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
828 client_start ( par );
830 mem = TlsGetValue ( tls );
832 /* Prepare event notification for connect, makes socket nonblocking */
833 event = WSACreateEvent ();
834 WSAEventSelect ( mem->s, event, FD_CONNECT );
835 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
836 if ( tmp != 0 ) {
837 err = WSAGetLastError ();
838 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
839 tmp = WaitForSingleObject ( event, INFINITE );
840 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
841 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
842 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
843 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
844 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
845 if ( err ) goto out;
848 WSAEventSelect ( mem->s, event, mask );
850 recv_p = mem->recv_buf;
851 recv_last = mem->recv_buf + n_expected;
852 send_p = mem->send_buf;
853 send_last = mem->send_buf + n_expected;
855 while ( TRUE )
857 err = WaitForSingleObject ( event, INFINITE );
858 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
860 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
861 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err );
863 if ( wsa_events.lNetworkEvents & FD_WRITE )
865 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
866 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
868 if ( err== 0 )
871 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
872 if ( n < 0 )
874 err = WSAGetLastError ();
875 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
877 else
878 send_p += n;
880 while ( n >= 0 && send_p < send_last );
882 if ( send_p == send_last )
884 shutdown ( mem->s, SD_SEND );
885 mask &= ~FD_WRITE;
886 WSAEventSelect ( mem->s, event, mask );
889 if ( wsa_events.lNetworkEvents & FD_READ )
891 err = wsa_events.iErrorCode[ FD_READ_BIT ];
892 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
893 if ( err != 0 ) break;
895 /* First read must succeed */
896 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
897 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
899 while ( n >= 0 ) {
900 recv_p += n;
901 if ( recv_p == recv_last )
903 mask &= ~FD_READ;
904 WSAEventSelect ( mem->s, event, mask );
905 break;
907 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
908 ok(n >= 0 || WSAGetLastError() == WSAEWOULDBLOCK,
909 "event_client (%x): got error %u\n", id, WSAGetLastError());
913 if ( wsa_events.lNetworkEvents & FD_CLOSE )
915 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
916 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
917 break;
921 n = send_p - mem->send_buf;
922 ok ( send_p == send_last,
923 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
924 n = recv_p - mem->recv_buf;
925 ok ( recv_p == recv_last,
926 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
927 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
928 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
930 out:
931 WSACloseEvent ( event );
932 client_stop ();
935 /* Tests for WSAStartup */
936 static void test_WithoutWSAStartup(void)
938 DWORD err;
940 WSASetLastError(0xdeadbeef);
941 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n");
942 err = WSAGetLastError();
943 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
945 WSASetLastError(0xdeadbeef);
946 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n");
947 err = WSAGetLastError();
948 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err);
951 static void test_WithWSAStartup(void)
953 WSADATA data;
954 WORD version = MAKEWORD( 2, 2 );
955 INT res, socks, i, j;
956 SOCKET sock;
957 LPVOID ptr;
958 struct
960 SOCKET src, dst, dup_src, dup_dst;
961 } pairs[32];
962 DWORD error;
964 res = WSAStartup( version, &data );
965 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
967 ptr = gethostbyname("localhost");
968 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
970 /* Alloc some sockets to check if they are destroyed on WSACleanup */
971 for (socks = 0; socks < ARRAY_SIZE(pairs); socks++)
973 WSAPROTOCOL_INFOA info;
974 tcp_socketpair(&pairs[socks].src, &pairs[socks].dst);
976 memset(&info, 0, sizeof(info));
977 ok(!WSADuplicateSocketA(pairs[socks].src, GetCurrentProcessId(), &info),
978 "WSADuplicateSocketA should have worked\n");
979 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0);
980 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n");
982 memset(&info, 0, sizeof(info));
983 ok(!WSADuplicateSocketA(pairs[socks].dst, GetCurrentProcessId(), &info),
984 "WSADuplicateSocketA should have worked\n");
985 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0);
986 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n");
989 res = send(pairs[0].src, "TEST", 4, 0);
990 ok(res == 4, "send failed with error %d\n", WSAGetLastError());
992 WSACleanup();
994 res = WSAStartup( version, &data );
995 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
997 /* show that sockets are destroyed automatically after WSACleanup */
998 SetLastError(0xdeadbeef);
999 res = send(pairs[0].src, "TEST", 4, 0);
1000 error = WSAGetLastError();
1001 ok(res == SOCKET_ERROR, "send should have failed\n");
1002 ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1004 SetLastError(0xdeadbeef);
1005 res = send(pairs[0].dst, "TEST", 4, 0);
1006 error = WSAGetLastError();
1007 ok(res == SOCKET_ERROR, "send should have failed\n");
1008 ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error);
1010 /* Check that all sockets were destroyed */
1011 for (i = 0; i < socks; i++)
1013 for (j = 0; j < 4; j++)
1015 struct sockaddr_in saddr;
1016 int size = sizeof(saddr);
1017 switch(j)
1019 case 0: sock = pairs[i].src; break;
1020 case 1: sock = pairs[i].dup_src; break;
1021 case 2: sock = pairs[i].dst; break;
1022 case 3: sock = pairs[i].dup_dst; break;
1025 SetLastError(0xdeadbeef);
1026 res = getsockname(sock, (struct sockaddr *)&saddr, &size);
1027 error = WSAGetLastError();
1028 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i);
1029 if (res == SOCKET_ERROR)
1030 ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error);
1034 /* While wine is not fixed, close all sockets manually */
1035 for (i = 0; i < socks; i++)
1037 closesocket(pairs[i].src);
1038 closesocket(pairs[i].dst);
1039 closesocket(pairs[i].dup_src);
1040 closesocket(pairs[i].dup_dst);
1043 res = WSACleanup();
1044 ok(res == 0, "expected 0, got %d\n", res);
1045 WSASetLastError(0xdeadbeef);
1046 res = WSACleanup();
1047 error = WSAGetLastError();
1048 ok ( res == SOCKET_ERROR && error == WSANOTINITIALISED,
1049 "WSACleanup returned %d WSAGetLastError is %d\n", res, error);
1052 /**************** Main program utility functions ***************/
1054 static void Init (void)
1056 WORD ver = MAKEWORD (2, 2);
1057 WSADATA data;
1058 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), ntdll;
1060 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll");
1062 ntdll = LoadLibraryA("ntdll.dll");
1063 if (ntdll)
1064 pNtClose = (void *)GetProcAddress(ntdll, "NtClose");
1066 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
1067 tls = TlsAlloc();
1070 static void Exit (void)
1072 INT ret, err;
1073 TlsFree ( tls );
1074 ret = WSACleanup();
1075 err = WSAGetLastError();
1076 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
1079 static void StartServer (LPTHREAD_START_ROUTINE routine,
1080 test_params *general, server_params *par)
1082 par->general = general;
1083 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
1084 ok ( thread[0] != NULL, "Failed to create server thread\n" );
1087 static void StartClients (LPTHREAD_START_ROUTINE routine,
1088 test_params *general, client_params *par)
1090 int i;
1091 par->general = general;
1092 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
1094 client_id = i - 1;
1095 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
1096 ok ( thread[i] != NULL, "Failed to create client thread\n" );
1097 /* Make sure the client is up and running */
1098 WaitForSingleObject ( client_ready[client_id], INFINITE );
1102 static void do_test( test_setup *test )
1104 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
1105 DWORD wait;
1107 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
1108 for (i = 0; i <= n; i++)
1109 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
1111 StartServer ( test->srv, &test->general, &test->srv_params );
1112 StartClients ( test->clt, &test->general, &test->clt_params );
1113 WaitForSingleObject ( server_ready, INFINITE );
1115 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
1116 ok(!wait, "wait failed, error %u\n", wait);
1118 CloseHandle ( server_ready );
1119 for (i = 0; i <= n; i++)
1120 CloseHandle ( client_ready[i] );
1123 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
1124 /* optname = SO_LINGER */
1125 static const LINGER linger_testvals[] = {
1126 {0,0},
1127 {0,73},
1128 {1,0},
1129 {5,189}
1132 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
1133 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
1134 bug in the linux kernel (fixed in 2.6.8) */
1135 #define SOCKTIMEOUT2 997000 /* 997 seconds */
1137 static void test_set_getsockopt(void)
1139 SOCKET s, s2;
1140 int i, err, lasterr;
1141 int timeout;
1142 LINGER lingval;
1143 int size;
1144 WSAPROTOCOL_INFOA infoA;
1145 WSAPROTOCOL_INFOW infoW;
1146 char providername[WSAPROTOCOL_LEN + 1];
1147 DWORD value;
1148 struct _prottest
1150 int family, type, proto;
1151 } prottest[] = {
1152 {AF_INET, SOCK_STREAM, IPPROTO_TCP},
1153 {AF_INET, SOCK_DGRAM, IPPROTO_UDP},
1154 {AF_INET6, SOCK_STREAM, IPPROTO_TCP},
1155 {AF_INET6, SOCK_DGRAM, IPPROTO_UDP}
1157 union _csspace
1159 CSADDR_INFO cs;
1160 char space[128];
1161 } csinfoA, csinfoB;
1163 s = socket(AF_INET, SOCK_STREAM, 0);
1164 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1165 if( s == INVALID_SOCKET) return;
1166 /* SO_RCVTIMEO */
1167 timeout = SOCKTIMEOUT1;
1168 size = sizeof(timeout);
1169 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1170 if( !err)
1171 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1172 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1173 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1175 timeout = 0;
1176 size = sizeof(timeout);
1177 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
1178 if( !err)
1179 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
1180 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
1181 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
1183 /* SO_SNDTIMEO */
1184 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1185 size = sizeof(timeout);
1186 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1187 if( !err)
1188 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1189 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1190 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1192 /* SO_SNDBUF */
1193 value = 4096;
1194 size = sizeof(value);
1195 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size);
1196 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1197 value = 0xdeadbeef;
1198 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size);
1199 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() );
1200 todo_wine ok( value == 4096, "expected 4096, got %u\n", value );
1202 /* SO_RCVBUF */
1203 value = 4096;
1204 size = sizeof(value);
1205 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size);
1206 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1207 value = 0xdeadbeef;
1208 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size);
1209 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() );
1210 todo_wine ok( value == 4096, "expected 4096, got %u\n", value );
1212 /* SO_LINGER */
1213 for( i = 0; i < ARRAY_SIZE(linger_testvals);i++) {
1214 size = sizeof(lingval);
1215 lingval = linger_testvals[i];
1216 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingval, size);
1217 ok(!err, "Test %u: failed to set SO_LINGER, error %u\n", i, WSAGetLastError());
1218 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingval, &size);
1219 ok(!err, "Test %u: failed to get SO_LINGER, error %u\n", i, WSAGetLastError());
1220 ok(!lingval.l_onoff == !linger_testvals[i].l_onoff, "Test %u: expected %d, got %d\n",
1221 i, linger_testvals[i].l_onoff, lingval.l_onoff);
1222 if (lingval.l_onoff)
1223 ok(lingval.l_linger == linger_testvals[i].l_linger, "Test %u: expected %d, got %d\n",
1224 i, linger_testvals[i].l_linger, lingval.l_linger);
1227 size = sizeof(lingval);
1228 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size);
1229 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1230 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1231 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0);
1232 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1233 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1235 size = sizeof(BOOL);
1236 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size);
1237 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1238 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1239 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0);
1240 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
1241 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError());
1243 /* Test for erroneously passing a value instead of a pointer as optval */
1244 size = sizeof(char);
1245 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1246 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1247 "instead of failing.\n");
1248 lasterr = WSAGetLastError();
1249 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1250 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1251 lasterr, WSAEFAULT);
1253 /* SO_RCVTIMEO with invalid values for level */
1254 size = sizeof(timeout);
1255 timeout = SOCKTIMEOUT1;
1256 SetLastError(0xdeadbeef);
1257 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1258 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1259 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1260 err, WSAGetLastError());
1262 timeout = SOCKTIMEOUT1;
1263 SetLastError(0xdeadbeef);
1264 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1265 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1266 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n",
1267 err, WSAGetLastError());
1269 /* Test SO_ERROR set/get */
1270 SetLastError(0xdeadbeef);
1271 i = 1234;
1272 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size);
1273 todo_wine
1274 ok( !err && !WSAGetLastError(),
1275 "got %d with %d (expected 0 with 0)\n",
1276 err, WSAGetLastError());
1278 SetLastError(0xdeadbeef);
1279 i = 4321;
1280 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1281 todo_wine
1282 ok( !err && !WSAGetLastError(),
1283 "got %d with %d (expected 0 with 0)\n",
1284 err, WSAGetLastError());
1285 todo_wine
1286 ok (i == 1234, "got %d (expected 1234)\n", i);
1288 /* Test invalid optlen */
1289 SetLastError(0xdeadbeef);
1290 size = 1;
1291 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1292 todo_wine
1293 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEFAULT),
1294 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n",
1295 err, WSAGetLastError());
1297 closesocket(s);
1298 /* Test with the closed socket */
1299 SetLastError(0xdeadbeef);
1300 size = sizeof(i);
1301 i = 1234;
1302 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size);
1303 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAENOTSOCK),
1304 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n",
1305 err, WSAGetLastError());
1306 ok (i == 1234, "expected 1234, got %d\n", i);
1308 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */
1309 s = socket(AF_INET, SOCK_DGRAM, 0);
1310 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1311 size = sizeof(i);
1312 i = 0x0000000a;
1313 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size);
1314 if (!err)
1316 for (i = 0; i < 4; i++)
1318 int k, j;
1319 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d};
1320 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1);
1321 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1322 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size);
1323 ok(!err, "Test [%d] Expected 0, got %d\n", i, err);
1324 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i];
1325 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k);
1328 else
1329 win_skip("IP_MULTICAST_TTL is unsupported\n");
1330 closesocket(s);
1332 /* test SO_PROTOCOL_INFOA invalid parameters */
1333 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1334 "getsockopt should have failed\n");
1335 err = WSAGetLastError();
1336 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1337 size = sizeof(WSAPROTOCOL_INFOA);
1338 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1339 "getsockopt should have failed\n");
1340 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1341 err = WSAGetLastError();
1342 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
1343 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1344 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL),
1345 "getsockopt should have failed\n");
1346 err = WSAGetLastError();
1347 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1348 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, NULL),
1349 "getsockopt should have failed\n");
1350 err = WSAGetLastError();
1351 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1352 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, &size),
1353 "getsockopt should have failed\n");
1354 err = WSAGetLastError();
1355 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1356 size = sizeof(WSAPROTOCOL_INFOA) / 2;
1357 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size),
1358 "getsockopt should have failed\n");
1359 err = WSAGetLastError();
1360 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
1361 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1362 size = sizeof(WSAPROTOCOL_INFOA) * 2;
1363 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1364 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1365 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size);
1367 closesocket(s);
1369 /* test SO_PROTOCOL_INFO structure returned for different protocols */
1370 for (i = 0; i < ARRAY_SIZE(prottest); i++)
1372 int k;
1374 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto);
1375 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue;
1377 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n",
1378 WSAGetLastError());
1380 /* compare both A and W version */
1381 infoA.szProtocol[0] = 0;
1382 size = sizeof(WSAPROTOCOL_INFOA);
1383 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size);
1384 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1385 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size);
1387 infoW.szProtocol[0] = 0;
1388 size = sizeof(WSAPROTOCOL_INFOW);
1389 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &infoW, &size);
1390 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
1391 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size);
1393 ok(infoA.szProtocol[0], "WSAPROTOCOL_INFOA was not filled\n");
1394 ok(infoW.szProtocol[0], "WSAPROTOCOL_INFOW was not filled\n");
1396 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1,
1397 providername, sizeof(providername), NULL, NULL);
1398 ok(!strcmp(infoA.szProtocol,providername),
1399 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername);
1401 ok(!memcmp(&infoA, &infoW, FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol)),
1402 "SO_PROTOCOL_INFO[A/W] comparison failed\n");
1404 /* Remove IF when WSAEnumProtocols support IPV6 data */
1405 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n",
1406 prottest[i].family, infoA.iAddressFamily);
1407 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n",
1408 prottest[i].type, infoA.iSocketType);
1409 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n",
1410 prottest[i].proto, infoA.iProtocol);
1412 /* IP_HDRINCL is supported only on SOCK_RAW but passed to SOCK_DGRAM by Impossible Creatures */
1413 size = sizeof(i);
1414 k = 1;
1415 SetLastError(0xdeadbeef);
1416 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1417 if (err == -1) /* >= Vista */
1419 todo_wine {
1420 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1421 k = 99;
1422 SetLastError(0xdeadbeef);
1423 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1424 ok(err == -1, "Expected -1, got %d\n", err);
1425 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1426 ok(k == 99, "Expected 99, got %d\n", k);
1428 size = sizeof(k);
1429 k = 0;
1430 SetLastError(0xdeadbeef);
1431 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1433 ok(err == -1, "Expected -1, got %d\n", err);
1434 todo_wine {
1435 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1436 k = 99;
1437 SetLastError(0xdeadbeef);
1438 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1439 ok(err == -1, "Expected -1, got %d\n", err);
1440 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError());
1441 ok(k == 99, "Expected 99, got %d\n", k);
1444 else /* <= 2003 the tests differ between TCP and UDP, UDP silently accepts */
1446 SetLastError(0xdeadbeef);
1447 k = 99;
1448 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1449 if (prottest[i].type == SOCK_DGRAM)
1451 ok(err == 0, "Expected 0, got %d\n", err);
1452 ok(k == 1, "Expected 1, got %d\n", k);
1454 else
1456 /* contratry to what we could expect the function returns error but k is changed */
1457 ok(err == -1, "Expected -1, got %d\n", err);
1458 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1459 ok(k == 0, "Expected 0, got %d\n", k);
1462 k = 0;
1463 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size);
1464 ok(err == 0, "Expected 0, got %d\n", err);
1466 k = 99;
1467 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size);
1468 if (prottest[i].type == SOCK_DGRAM)
1470 ok(err == 0, "Expected 0, got %d\n", err);
1471 ok(k == 0, "Expected 0, got %d\n", k);
1473 else
1475 /* contratry to what we could expect the function returns error but k is changed */
1476 ok(err == -1, "Expected -1, got %d\n", err);
1477 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1478 ok(k == 0, "Expected 0, got %d\n", k);
1482 closesocket(s);
1485 /* Test SO_BSP_STATE - Present only in >= Win 2008 */
1486 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1487 ok(s != INVALID_SOCKET, "Failed to create socket\n");
1488 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1489 ok(s2 != INVALID_SOCKET, "Failed to create socket\n");
1491 SetLastError(0xdeadbeef);
1492 size = sizeof(csinfoA);
1493 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1494 if (!err)
1496 struct sockaddr_in saddr;
1497 memset(&saddr, 0, sizeof(saddr));
1498 saddr.sin_family = AF_INET;
1499 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1501 /* Socket is not bound, no information provided */
1502 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength);
1503 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr);
1504 /* Socket is not connected, no information provided */
1505 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1506 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1508 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr));
1509 ok(!err, "Expected 0, got %d\n", err);
1510 size = sizeof(csinfoA);
1511 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1512 ok(!err, "Expected 0, got %d\n", err);
1514 /* Socket is bound */
1515 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n");
1516 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n");
1517 /* Socket is not connected, no information provided */
1518 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength);
1519 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr);
1521 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1522 ok(!err, "Expected 0, got %d\n", err);
1523 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1524 ok(!err, "Expected 0, got %d\n", err);
1525 err = listen(s2, 1);
1526 ok(!err, "Expected 0, got %d\n", err);
1527 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr));
1528 ok(!err, "Expected 0, got %d\n", err);
1529 size = sizeof(saddr);
1530 err = accept(s2, (struct sockaddr*)&saddr, &size);
1531 ok(err != INVALID_SOCKET, "Failed to accept socket\n");
1532 closesocket(s2);
1533 s2 = err;
1535 size = sizeof(csinfoA);
1536 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1537 ok(!err, "Expected 0, got %d\n", err);
1538 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size);
1539 ok(!err, "Expected 0, got %d\n", err);
1540 ok(size == sizeof(csinfoA), "Got %d\n", size);
1541 size = sizeof(saddr);
1542 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1543 csinfoA.cs.LocalAddr.iSockaddrLength);
1544 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size,
1545 csinfoA.cs.RemoteAddr.iSockaddrLength);
1546 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size),
1547 "Expected matching addresses\n");
1548 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size),
1549 "Expected matching addresses\n");
1550 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1551 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n");
1552 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1553 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n");
1555 err = getpeername(s, (struct sockaddr *)&saddr, &size);
1556 ok(!err, "Expected 0, got %d\n", err);
1557 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1558 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1559 err = getpeername(s2, (struct sockaddr *)&saddr, &size);
1560 ok(!err, "Expected 0, got %d\n", err);
1561 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1562 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1563 err = getsockname(s, (struct sockaddr *)&saddr, &size);
1564 ok(!err, "Expected 0, got %d\n", err);
1565 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1566 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1567 err = getsockname(s2, (struct sockaddr *)&saddr, &size);
1568 ok(!err, "Expected 0, got %d\n", err);
1569 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n");
1570 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n");
1572 SetLastError(0xdeadbeef);
1573 size = sizeof(CSADDR_INFO);
1574 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1575 ok(err, "Expected non-zero\n");
1576 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size);
1577 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1579 /* At least for IPv4 the size is exactly 56 bytes */
1580 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs);
1581 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1582 ok(!err, "Expected 0, got %d\n", err);
1583 size--;
1584 SetLastError(0xdeadbeef);
1585 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size);
1586 ok(err, "Expected non-zero\n");
1587 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError());
1589 else
1590 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError());
1592 closesocket(s);
1593 closesocket(s2);
1595 for (i = 0; i < 2; i++)
1597 int family, level;
1599 if (i)
1601 family = AF_INET6;
1602 level = IPPROTO_IPV6;
1604 else
1606 family = AF_INET;
1607 level = IPPROTO_IP;
1610 s = socket(family, SOCK_DGRAM, 0);
1611 if (s == INVALID_SOCKET && i)
1613 skip("IPv6 is not supported\n");
1614 break;
1616 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1618 size = sizeof(value);
1619 value = 0xdead;
1620 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1621 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1622 ok(value == 0, "Expected 0, got %d\n", value);
1624 size = sizeof(value);
1625 value = 1;
1626 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1627 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1629 value = 0xdead;
1630 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1631 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1632 ok(value == 1, "Expected 1, got %d\n", value);
1634 size = sizeof(value);
1635 value = 0xdead;
1636 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1637 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1639 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1640 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1641 ok(value == 1, "Expected 1, got %d\n", value);
1643 closesocket(s);
1645 s = socket(family, SOCK_STREAM, 0);
1646 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1648 size = sizeof(value);
1649 value = 0xdead;
1650 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1651 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1652 ok(value == 1 || broken(value == 0) /* < vista */, "Expected 1, got %d\n", value);
1654 size = sizeof(value);
1655 value = 0;
1656 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1657 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1659 value = 0xdead;
1660 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1661 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1662 ok(value == 0, "Expected 0, got %d\n", value);
1664 closesocket(s);
1666 s = socket(family, SOCK_RAW, 0);
1667 if (s == INVALID_SOCKET)
1669 if (WSAGetLastError() == WSAEACCES) skip("SOCK_RAW is not available\n");
1670 else if (i) skip("IPv6 is not supported\n");
1671 break;
1673 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError());
1675 size = sizeof(value);
1676 value = 0xdead;
1677 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1678 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1679 ok(value == 0, "Expected 0, got %d\n", value);
1681 size = sizeof(value);
1682 value = 1;
1683 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size);
1684 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1686 value = 0xdead;
1687 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size);
1688 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError());
1689 ok(value == 1, "Expected 1, got %d\n", value);
1691 closesocket(s);
1695 static void test_so_reuseaddr(void)
1697 struct sockaddr_in saddr;
1698 SOCKET s1,s2;
1699 unsigned int rc,reuse;
1700 int size;
1701 DWORD err;
1703 saddr.sin_family = AF_INET;
1704 saddr.sin_port = htons(SERVERPORT+1);
1705 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1707 s1=socket(AF_INET, SOCK_STREAM, 0);
1708 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1709 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1710 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1712 s2=socket(AF_INET, SOCK_STREAM, 0);
1713 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1715 reuse=0x1234;
1716 size=sizeof(reuse);
1717 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1718 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1720 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1721 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1723 reuse = 1;
1724 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1725 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1727 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1728 * a port immediately after closing another socket on that port, so
1729 * basically following the BSD socket semantics here. */
1730 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1731 if(rc==0)
1733 int s3=socket(AF_INET, SOCK_STREAM, 0), s4;
1735 /* If we could bind again in the same port this is Windows version <= XP.
1736 * Lets test if we can really connect to one of them. */
1737 set_blocking(s1, FALSE);
1738 set_blocking(s2, FALSE);
1739 rc = listen(s1, 1);
1740 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1741 rc = listen(s2, 1);
1742 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError());
1743 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr));
1744 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError());
1746 /* the delivery of the connection is random so we need to try on both sockets */
1747 size = sizeof(saddr);
1748 s4 = accept(s1, (struct sockaddr*)&saddr, &size);
1749 if(s4 == INVALID_SOCKET)
1750 s4 = accept(s2, (struct sockaddr*)&saddr, &size);
1751 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n");
1753 closesocket(s1);
1754 closesocket(s3);
1755 closesocket(s4);
1757 else
1759 err = WSAGetLastError();
1760 ok(err==WSAEACCES, "expected 10013, got %d\n", err);
1762 closesocket(s1);
1763 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1764 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1767 closesocket(s2);
1770 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo)))
1772 static void test_ip_pktinfo(void)
1774 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)};
1775 char recvbuf[10], pktbuf[512], msg[] = "HELLO";
1776 struct sockaddr_in s1addr, s2addr, s3addr;
1777 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
1778 LPFN_WSARECVMSG pWSARecvMsg = NULL;
1779 unsigned int rc, yes = 1;
1780 BOOL foundhdr;
1781 DWORD dwBytes, dwSize, dwFlags;
1782 socklen_t addrlen;
1783 WSACMSGHDR *cmsg;
1784 WSAOVERLAPPED ov;
1785 WSABUF iovec[1];
1786 SOCKET s1, s2;
1787 WSAMSG hdr;
1788 int i, err;
1790 memset(&ov, 0, sizeof(ov));
1791 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1793 memset(&hdr, 0x00, sizeof(hdr));
1794 s1addr.sin_family = AF_INET;
1795 s1addr.sin_port = htons(0);
1796 /* Note: s1addr.sin_addr is set below */
1797 iovec[0].buf = recvbuf;
1798 iovec[0].len = sizeof(recvbuf);
1799 hdr.name = (struct sockaddr*)&s3addr;
1800 hdr.namelen = sizeof(s3addr);
1801 hdr.lpBuffers = &iovec[0];
1802 hdr.dwBufferCount = 1;
1803 hdr.Control.buf = pktbuf;
1804 /* Note: hdr.Control.len is set below */
1805 hdr.dwFlags = 0;
1807 for (i=0;i<ARRAY_SIZE(addresses);i++)
1809 s1addr.sin_addr.s_addr = addresses[i];
1811 /* Build "server" side socket */
1812 s1=socket(AF_INET, SOCK_DGRAM, 0);
1813 ok(s1 != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
1815 /* Obtain the WSARecvMsg function */
1816 rc = WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
1817 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL);
1818 ok(!rc, "failed to get WSARecvMsg, error %u\n", WSAGetLastError());
1820 /* Setup the server side socket */
1821 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr));
1822 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError());
1824 /* Build "client" side socket */
1825 addrlen = sizeof(s2addr);
1826 rc = getsockname(s1, (struct sockaddr *) &s2addr, &addrlen);
1827 ok(!rc, "failed to get address, error %u\n", WSAGetLastError());
1828 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */
1829 s2=socket(AF_INET, SOCK_DGRAM, 0);
1830 ok(s2 != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
1832 /* Test an empty message header */
1833 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL);
1834 err=WSAGetLastError();
1835 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc);
1837 /* Test that when no control data arrives, a 0-length NULL-valued control buffer should succeed */
1838 SetLastError(0xdeadbeef);
1839 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1840 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1841 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1842 hdr.Control.buf = NULL;
1843 hdr.Control.len = 0;
1844 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1845 ok(rc == 0, "WSARecvMsg() failed error: %d\n", WSAGetLastError());
1846 hdr.Control.buf = pktbuf;
1848 /* Now start IP_PKTINFO for future tests */
1849 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes));
1850 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n");
1853 * Send a packet from the client to the server and test for specifying
1854 * a short control header.
1856 SetLastError(0xdeadbeef);
1857 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1858 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1859 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1860 hdr.Control.len = 1;
1861 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL);
1862 err=WSAGetLastError();
1863 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC),
1864 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags);
1865 hdr.dwFlags = 0; /* Reset flags */
1867 /* Perform another short control header test, this time with an overlapped receive */
1868 hdr.Control.len = 1;
1869 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1870 err=WSAGetLastError();
1871 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1872 SetLastError(0xdeadbeef);
1873 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1874 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1875 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
1876 ok(!WaitForSingleObject(ov.hEvent, 100), "wait failed\n");
1877 dwFlags = 0;
1878 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags);
1879 ok(dwFlags == 0,
1880 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags);
1881 ok(hdr.dwFlags == MSG_CTRUNC,
1882 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags);
1883 hdr.dwFlags = 0; /* Reset flags */
1886 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved
1887 * on the server end and check that the returned packet matches what was sent.
1889 hdr.Control.len = sizeof(pktbuf);
1890 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL);
1891 err=WSAGetLastError();
1892 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err);
1893 ok(hdr.Control.len == sizeof(pktbuf),
1894 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len);
1895 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr));
1896 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError());
1897 ok(!WaitForSingleObject(ov.hEvent, 100), "wait failed\n");
1898 dwSize = 0;
1899 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL);
1900 ok(dwSize == sizeof(msg),
1901 "WSARecvMsg() buffer length does not match transmitted data!\n");
1902 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0,
1903 "WSARecvMsg() buffer does not match transmitted data!\n");
1904 ok(hdr.Control.len == IP_PKTINFO_LEN,
1905 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len);
1907 /* Test for the expected IP_PKTINFO return information. */
1908 foundhdr = FALSE;
1909 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg))
1911 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
1913 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
1915 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n");
1916 foundhdr = TRUE;
1919 ok(foundhdr, "IP_PKTINFO header information was not returned!\n");
1921 closesocket(s2);
1922 closesocket(s1);
1925 CloseHandle(ov.hEvent);
1928 /************* Array containing the tests to run **********/
1930 #define STD_STREAM_SOCKET \
1931 SOCK_STREAM, \
1932 0, \
1933 SERVERIP, \
1934 SERVERPORT
1936 static test_setup tests [] =
1938 /* Test 0: synchronous client and server */
1941 STD_STREAM_SOCKET,
1942 2048,
1946 simple_server,
1948 NULL,
1952 simple_client,
1954 NULL,
1959 /* Test 1: event-driven client, synchronous server */
1962 STD_STREAM_SOCKET,
1963 2048,
1967 simple_server,
1969 NULL,
1973 event_client,
1975 NULL,
1976 WSA_FLAG_OVERLAPPED,
1980 /* Test 2: synchronous client, non-blocking server via select() */
1983 STD_STREAM_SOCKET,
1984 2048,
1988 select_server,
1990 NULL,
1994 simple_client,
1996 NULL,
2001 /* Test 3: OOB client, OOB server */
2004 STD_STREAM_SOCKET,
2005 128,
2009 oob_server,
2011 NULL,
2015 oob_client,
2017 NULL,
2022 /* Test 4: synchronous mixed client and server */
2025 STD_STREAM_SOCKET,
2026 2048,
2030 simple_server,
2032 NULL,
2036 simple_mixed_client,
2038 NULL,
2045 static void test_UDP(void)
2047 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
2048 possible that this test fails due to dropped packets. */
2050 /* peer 0 receives data from all other peers */
2051 struct sock_info peer[NUM_UDP_PEERS];
2052 char buf[16];
2053 int ss, i, n_recv, n_sent;
2055 memset (buf,0,sizeof(buf));
2056 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
2057 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
2059 peer[i].addr.sin_family = AF_INET;
2060 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
2062 if ( i == 0 ) {
2063 peer[i].addr.sin_port = htons ( SERVERPORT );
2064 } else {
2065 peer[i].addr.sin_port = htons ( 0 );
2068 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
2070 /* test getsockname() to get peer's port */
2071 ss = sizeof ( peer[i].addr );
2072 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
2073 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
2076 /* test getsockname() */
2077 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
2079 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2080 /* send client's ip */
2081 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
2082 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
2083 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
2086 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
2087 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
2088 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
2089 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
2093 static void test_WSASocket(void)
2095 SOCKET sock = INVALID_SOCKET;
2096 WSAPROTOCOL_INFOA *pi;
2097 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP};
2098 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP};
2099 int items, err, size, socktype, i, j;
2100 UINT pi_size;
2102 static const struct
2104 int family, type, protocol;
2105 DWORD error;
2106 int ret_family, ret_type, ret_protocol;
2108 tests[] =
2110 /* 0 */
2111 {0xdead, SOCK_STREAM, IPPROTO_TCP, WSAEAFNOSUPPORT},
2112 {-1, SOCK_STREAM, IPPROTO_TCP, WSAEAFNOSUPPORT},
2113 {AF_INET, 0xdead, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2114 {AF_INET, -1, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2115 {AF_INET, SOCK_STREAM, 0xdead, WSAEPROTONOSUPPORT},
2116 {AF_INET, SOCK_STREAM, -1, WSAEPROTONOSUPPORT},
2117 {0xdead, 0xdead, IPPROTO_TCP, WSAESOCKTNOSUPPORT},
2118 {0xdead, SOCK_STREAM, 0xdead, WSAEAFNOSUPPORT},
2119 {AF_INET, 0xdead, 0xdead, WSAESOCKTNOSUPPORT},
2120 {0xdead, SOCK_STREAM, IPPROTO_UDP, WSAEAFNOSUPPORT},
2122 /* 10 */
2123 {AF_INET, SOCK_STREAM, 0, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2124 {AF_INET, SOCK_DGRAM, 0, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2125 {AF_INET, 0xdead, 0, WSAESOCKTNOSUPPORT},
2126 {AF_INET, 0, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2127 {AF_INET, 0, IPPROTO_UDP, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2128 {AF_INET, 0, 0xdead, WSAEPROTONOSUPPORT},
2129 {AF_INET, 0, 0, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2130 {AF_INET, SOCK_STREAM, IPPROTO_UDP, WSAEPROTONOSUPPORT},
2131 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, WSAEPROTONOSUPPORT},
2133 /* 19 */
2134 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2135 {AF_UNSPEC, SOCK_STREAM, 0xdead, WSAEPROTONOSUPPORT},
2136 {AF_UNSPEC, 0xdead, IPPROTO_UDP, WSAESOCKTNOSUPPORT},
2137 {AF_UNSPEC, SOCK_STREAM, 0, WSAEINVAL},
2138 {AF_UNSPEC, SOCK_DGRAM, 0, WSAEINVAL},
2139 {AF_UNSPEC, 0xdead, 0, WSAEINVAL},
2140 {AF_UNSPEC, 0, IPPROTO_TCP, 0, AF_INET, SOCK_STREAM, IPPROTO_TCP},
2141 {AF_UNSPEC, 0, IPPROTO_UDP, 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP},
2142 {AF_UNSPEC, 0, 0xdead, WSAEPROTONOSUPPORT},
2143 {AF_UNSPEC, 0, 0, WSAEINVAL},
2146 for (i = 0; i < ARRAY_SIZE(tests); ++i)
2148 SetLastError( 0xdeadbeef );
2149 sock = WSASocketA( tests[i].family, tests[i].type, tests[i].protocol, NULL, 0, 0 );
2150 todo_wine_if (!tests[i].error || i == 7)
2151 ok(WSAGetLastError() == tests[i].error, "Test %u: got wrong error %u\n", i, WSAGetLastError());
2152 if (tests[i].error)
2154 ok(sock == INVALID_SOCKET, "Test %u: expected failure\n", i);
2156 else
2158 WSAPROTOCOL_INFOA info;
2160 ok(sock != INVALID_SOCKET, "Text %u: expected success\n", i);
2162 size = sizeof(info);
2163 err = getsockopt( sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *)&info, &size );
2164 ok(!err, "Test %u: getsockopt failed, error %u\n", i, WSAGetLastError());
2165 ok(info.iAddressFamily == tests[i].ret_family, "Test %u: got wrong family %d\n", i, info.iAddressFamily);
2166 ok(info.iSocketType == tests[i].ret_type, "Test %u: got wrong type %d\n", i, info.iSocketType);
2167 ok(info.iProtocol == tests[i].ret_protocol, "Test %u: got wrong protocol %d\n", i, info.iProtocol);
2169 closesocket( sock );
2173 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
2174 * to avoid a crash on win98.
2176 pi_size = 0;
2177 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size);
2178 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
2179 items);
2180 err = WSAGetLastError();
2181 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2182 err, WSAENOBUFS);
2184 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2185 ok(pi != NULL, "Failed to allocate memory\n");
2187 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size);
2188 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2189 WSAGetLastError());
2191 if (items == 0) {
2192 skip("No protocols enumerated.\n");
2193 HeapFree(GetProcessHeap(), 0, pi);
2194 return;
2197 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
2198 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
2199 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2200 WSAGetLastError());
2201 closesocket(sock);
2203 /* find what parameters are used first: plain parameters or protocol info struct */
2204 pi[0].iProtocol = -1;
2205 pi[0].iSocketType = -1;
2206 pi[0].iAddressFamily = -1;
2207 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET,
2208 "WSASocketA should have failed\n");
2209 err = WSAGetLastError();
2210 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2212 pi[0].iProtocol = 0;
2213 pi[0].iSocketType = 0;
2214 pi[0].iAddressFamily = 0;
2215 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0);
2216 if(sock != INVALID_SOCKET)
2218 win_skip("must work only in OS <= 2003\n");
2219 closesocket(sock);
2221 else
2223 err = WSAGetLastError();
2224 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err);
2227 pi[0].iProtocol = IPPROTO_UDP;
2228 pi[0].iSocketType = SOCK_DGRAM;
2229 pi[0].iAddressFamily = AF_INET;
2230 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0);
2231 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2232 WSAGetLastError());
2233 size = sizeof(socktype);
2234 socktype = 0xdead;
2235 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2236 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2237 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2238 SOCK_DGRAM, socktype);
2239 closesocket(sock);
2241 sock = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi[0], 0, 0);
2242 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2243 WSAGetLastError());
2244 size = sizeof(socktype);
2245 socktype = 0xdead;
2246 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2247 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2248 ok(socktype == SOCK_STREAM, "Wrong socket type, expected %d received %d\n",
2249 SOCK_STREAM, socktype);
2250 closesocket(sock);
2252 HeapFree(GetProcessHeap(), 0, pi);
2254 pi_size = 0;
2255 items = WSAEnumProtocolsA(NULL, NULL, &pi_size);
2256 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n",
2257 items);
2258 err = WSAGetLastError();
2259 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
2260 err, WSAENOBUFS);
2262 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
2263 ok(pi != NULL, "Failed to allocate memory\n");
2265 items = WSAEnumProtocolsA(NULL, pi, &pi_size);
2266 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
2267 WSAGetLastError());
2269 /* when no protocol and socket type are specified the first entry
2270 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
2271 * is returned */
2272 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0);
2273 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2274 WSAGetLastError());
2276 size = sizeof(socktype);
2277 socktype = 0xdead;
2278 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2279 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2280 for(i = 0; i < items; i++)
2282 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO)
2284 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n",
2285 pi[i].iSocketType, socktype);
2286 break;
2289 ok(i != items, "Creating a socket without protocol and socket type didn't work\n");
2290 closesocket(sock);
2292 /* when no socket type is specified the first entry from WSAEnumProtocols
2293 * that matches the protocol is returned */
2294 for (i = 0; i < ARRAY_SIZE(autoprotocols); i++)
2296 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0);
2297 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n",
2298 autoprotocols[i], WSAGetLastError());
2300 size = sizeof(socktype);
2301 socktype = 0xdead;
2302 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2303 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2305 for (err = 1, j = 0; j < items; j++)
2307 if (pi[j].iProtocol == autoprotocols[i])
2309 ok(pi[j].iSocketType == socktype, "expected %d, got %d\n", socktype, pi[j].iSocketType);
2310 err = 0;
2311 break;
2314 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]);
2316 closesocket(sock);
2319 HeapFree(GetProcessHeap(), 0, pi);
2321 SetLastError(0xdeadbeef);
2322 /* starting on vista the socket function returns error during the socket
2323 creation and no longer in the socket operations (sendto, readfrom) */
2324 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
2325 if (sock == INVALID_SOCKET)
2327 err = WSAGetLastError();
2328 ok(err == WSAEACCES, "Expected 10013, received %d\n", err);
2329 skip("SOCK_RAW is not supported\n");
2331 else
2333 size = sizeof(socktype);
2334 socktype = 0xdead;
2335 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2336 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2337 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2338 SOCK_RAW, socktype);
2339 closesocket(sock);
2341 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0);
2342 if (sock != INVALID_SOCKET)
2344 todo_wine {
2345 size = sizeof(socktype);
2346 socktype = 0xdead;
2347 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2348 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2349 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2350 SOCK_RAW, socktype);
2351 closesocket(sock);
2354 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_TCP, NULL, 0, 0);
2355 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2356 WSAGetLastError());
2357 size = sizeof(socktype);
2358 socktype = 0xdead;
2359 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2360 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2361 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n",
2362 SOCK_RAW, socktype);
2363 closesocket(sock);
2365 else if (WSAGetLastError() == WSAEACCES)
2366 skip("SOCK_RAW is not available\n");
2367 else
2368 ok(0, "Failed to create socket: %d\n", WSAGetLastError());
2372 /* IPX socket tests */
2374 SetLastError(0xdeadbeef);
2375 sock = WSASocketA(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, NULL, 0, 0);
2376 if (sock == INVALID_SOCKET)
2378 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "got error %u\n", WSAGetLastError());
2379 skip("IPX is not supported\n");
2381 else
2383 WSAPROTOCOL_INFOA info;
2384 closesocket(sock);
2386 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0);
2387 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2388 WSAGetLastError());
2390 size = sizeof(socktype);
2391 socktype = 0xdead;
2392 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2393 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2394 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2395 SOCK_DGRAM, socktype);
2397 /* check socket family, type and protocol */
2398 size = sizeof(WSAPROTOCOL_INFOA);
2399 err = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &info, &size);
2400 ok(!err,"getsockopt failed with %d\n", WSAGetLastError());
2401 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n",
2402 NSPROTO_IPX, info.iProtocol);
2403 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n",
2404 AF_IPX, info.iProtocol);
2405 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2406 SOCK_DGRAM, info.iSocketType);
2407 closesocket(sock);
2409 /* SOCK_STREAM does not support NSPROTO_IPX */
2410 SetLastError(0xdeadbeef);
2411 ok(WSASocketA(AF_IPX, SOCK_STREAM, NSPROTO_IPX, NULL, 0, 0) == INVALID_SOCKET,
2412 "WSASocketA should have failed\n");
2413 err = WSAGetLastError();
2414 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err);
2416 /* test extended IPX support - that is adding any number between 0 and 255
2417 * to the IPX protocol value will make it be used as IPX packet type */
2418 for(i = 0;i <= 255;i += 17)
2420 SetLastError(0xdeadbeef);
2421 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0);
2422 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
2423 WSAGetLastError());
2425 size = sizeof(int);
2426 socktype = -1;
2427 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size);
2428 ok(!err, "getsockopt failed with %d\n", WSAGetLastError());
2429 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n",
2430 i, socktype);
2432 closesocket(sock);
2437 static void test_WSADuplicateSocket(void)
2439 SOCKET source, dupsock;
2440 WSAPROTOCOL_INFOA info;
2441 DWORD err;
2442 struct sockaddr_in addr;
2443 int socktype, size, addrsize, ret;
2444 char teststr[] = "TEST", buffer[16];
2446 source = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
2447 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2449 /* test invalid parameters */
2450 SetLastError(0xdeadbeef);
2451 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n");
2452 err = WSAGetLastError();
2453 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2455 SetLastError(0xdeadbeef);
2456 ok(WSADuplicateSocketA(source, 0, NULL),
2457 "WSADuplicateSocketA should have failed\n");
2458 err = WSAGetLastError();
2459 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2461 SetLastError(0xdeadbeef);
2462 ok(WSADuplicateSocketA(source, ~0, &info),
2463 "WSADuplicateSocketA should have failed\n");
2464 err = WSAGetLastError();
2465 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2467 SetLastError(0xdeadbeef);
2468 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info),
2469 "WSADuplicateSocketA should have failed\n");
2470 err = WSAGetLastError();
2471 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err);
2473 SetLastError(0xdeadbeef);
2474 ok(WSADuplicateSocketA(source, GetCurrentProcessId(), NULL),
2475 "WSADuplicateSocketA should have failed\n");
2476 err = WSAGetLastError();
2477 ok(err == WSAEFAULT, "expected 10014, received %d\n", err);
2479 /* test returned structure */
2480 memset(&info, 0, sizeof(info));
2481 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2482 "WSADuplicateSocketA should have worked\n");
2484 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n",
2485 IPPROTO_TCP, info.iProtocol);
2486 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2487 AF_INET, info.iProtocol);
2488 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n",
2489 SOCK_STREAM, info.iSocketType);
2491 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2492 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2494 closesocket(dupsock);
2495 closesocket(source);
2497 /* create a socket, bind it, duplicate it then send data on source and
2498 * receive in the duplicated socket */
2499 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2500 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2502 memset(&info, 0, sizeof(info));
2503 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2504 "WSADuplicateSocketA should have worked\n");
2506 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n",
2507 IPPROTO_UDP, info.iProtocol);
2508 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n",
2509 AF_INET, info.iProtocol);
2510 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n",
2511 SOCK_DGRAM, info.iSocketType);
2513 memset(&addr, 0, sizeof(addr));
2514 addr.sin_family = AF_INET;
2515 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2516 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2517 "bind should have worked\n");
2519 /* read address to find out the port number to be used in sendto */
2520 memset(&addr, 0, sizeof(addr));
2521 addrsize = sizeof(addr);
2522 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2523 "getsockname should have worked\n");
2524 ok(addr.sin_port, "socket port should be != 0\n");
2526 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2527 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2529 size = sizeof(int);
2530 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size);
2531 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
2532 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n",
2533 SOCK_DGRAM, socktype);
2535 set_blocking(source, TRUE);
2537 /* send data on source socket */
2538 addrsize = sizeof(addr);
2539 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2540 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2542 /* receive on duplicated socket */
2543 addrsize = sizeof(addr);
2544 memset(buffer, 0, sizeof(buffer));
2545 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize);
2546 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2547 buffer[sizeof(teststr) - 1] = 0;
2548 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer);
2550 closesocket(dupsock);
2551 closesocket(source);
2553 /* show that the source socket need to be bound before the duplicated
2554 * socket is created */
2555 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2556 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2558 memset(&info, 0, sizeof(info));
2559 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info),
2560 "WSADuplicateSocketA should have worked\n");
2562 dupsock = WSASocketA(0, 0, 0, &info, 0, 0);
2563 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n");
2565 memset(&addr, 0, sizeof(addr));
2566 addr.sin_family = AF_INET;
2567 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2568 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)),
2569 "bind should have worked\n");
2571 /* read address to find out the port number to be used in sendto */
2572 memset(&addr, 0, sizeof(addr));
2573 addrsize = sizeof(addr);
2574 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize),
2575 "getsockname should have worked\n");
2576 ok(addr.sin_port, "socket port should be != 0\n");
2578 set_blocking(source, TRUE);
2580 addrsize = sizeof(addr);
2581 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize);
2582 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError());
2584 SetLastError(0xdeadbeef);
2585 addrsize = sizeof(addr);
2586 memset(buffer, 0, sizeof(buffer));
2587 todo_wine {
2588 ok(recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize) == -1,
2589 "recvfrom should have failed\n");
2590 err = WSAGetLastError();
2591 ok(err == WSAEINVAL, "expected 10022, received %d\n", err);
2594 closesocket(dupsock);
2595 closesocket(source);
2598 static void test_WSAEnumNetworkEvents(void)
2600 SOCKET s, s2;
2601 int sock_type[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM}, i, j, k, l;
2602 struct sockaddr_in address;
2603 HANDLE event;
2604 WSANETWORKEVENTS net_events;
2606 memset(&address, 0, sizeof(address));
2607 address.sin_addr.s_addr = htonl(INADDR_ANY);
2608 address.sin_family = AF_INET;
2610 /* This test follows the steps from bugs 10204 and 24946 */
2611 for (l = 0; l < 2; l++)
2613 for (i = 0; i < ARRAY_SIZE(sock_type); i++)
2615 if (i == 2)
2616 tcp_socketpair(&s, &s2);
2617 else
2619 s = socket(AF_INET, sock_type[i], 0);
2620 ok (s != SOCKET_ERROR, "Test[%d]: failed to create socket\n", i);
2621 ok (!bind(s, (struct sockaddr*) &address, sizeof(address)), "Test[%d]: bind failed\n", i);
2623 event = WSACreateEvent();
2624 ok (event != NULL, "Test[%d]: failed to create event\n", i);
2625 for (j = 0; j < 5; j++) /* Repeat sometimes and the result must be the same */
2627 /* When the TCP socket is not connected NO events will be returned.
2628 * When connected and no data pending it will get the write event.
2629 * UDP sockets don't have connections so as soon as they are bound
2630 * they can read/write data. Since nobody is sendind us data only
2631 * the write event will be returned and ONLY once.
2633 ok (!WSAEventSelect(s, event, FD_READ | FD_WRITE), "Test[%d]: WSAEventSelect failed\n", i);
2634 memset(&net_events, 0xAB, sizeof(net_events));
2635 ok (!WSAEnumNetworkEvents(s, l == 0 ? event : NULL, &net_events),
2636 "Test[%d]: WSAEnumNetworkEvents failed\n", i);
2637 if (i >= 1 && j == 0) /* FD_WRITE is SET on first try for UDP and connected TCP */
2639 ok (net_events.lNetworkEvents == FD_WRITE, "Test[%d]: expected 2, got %d\n",
2640 i, net_events.lNetworkEvents);
2642 else
2644 todo_wine_if (i != 0) /* Remove when fixed */
2645 ok (net_events.lNetworkEvents == 0, "Test[%d]: expected 0, got %d\n",
2646 i, net_events.lNetworkEvents);
2648 for (k = 0; k < FD_MAX_EVENTS; k++)
2650 if (net_events.lNetworkEvents & (1 << k))
2652 ok (net_events.iErrorCode[k] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n",
2653 i, k, net_events.iErrorCode[k]);
2655 else
2657 /* Bits that are not set in lNetworkEvents MUST not be changed */
2658 ok (net_events.iErrorCode[k] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n",
2659 i, k, net_events.iErrorCode[k]);
2663 closesocket(s);
2664 WSACloseEvent(event);
2665 if (i == 2) closesocket(s2);
2670 static DWORD WINAPI SelectReadThread(void *param)
2672 select_thread_params *par = param;
2673 fd_set readfds;
2674 int ret;
2675 struct sockaddr_in addr;
2676 struct timeval select_timeout;
2678 FD_ZERO(&readfds);
2679 FD_SET(par->s, &readfds);
2680 select_timeout.tv_sec=5;
2681 select_timeout.tv_usec=0;
2682 addr.sin_family = AF_INET;
2683 addr.sin_addr.s_addr = inet_addr(SERVERIP);
2684 addr.sin_port = htons(SERVERPORT);
2686 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
2687 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
2689 SetEvent(server_ready);
2690 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
2691 par->ReadKilled = (ret == 1);
2693 return 0;
2696 static DWORD WINAPI SelectCloseThread(void *param)
2698 SOCKET s = *(SOCKET*)param;
2699 Sleep(500);
2700 closesocket(s);
2701 return 0;
2704 static void test_errors(void)
2706 SOCKET sock;
2707 SOCKADDR_IN SockAddr;
2708 int ret, err;
2710 WSASetLastError(NO_ERROR);
2711 sock = socket(PF_INET, SOCK_STREAM, 0);
2712 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2713 memset(&SockAddr, 0, sizeof(SockAddr));
2714 SockAddr.sin_family = AF_INET;
2715 SockAddr.sin_port = htons(6924);
2716 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
2718 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
2719 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret );
2720 if (ret == SOCKET_ERROR)
2722 err = WSAGetLastError();
2723 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err );
2727 TIMEVAL timeval;
2728 fd_set set = {1, {sock}};
2730 timeval.tv_sec = 0;
2731 timeval.tv_usec = 50000;
2733 ret = select(1, NULL, &set, NULL, &timeval);
2734 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret );
2737 ret = closesocket(sock);
2738 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError());
2741 static void test_listen(void)
2743 SOCKET fdA, fdB;
2744 int ret, acceptc, olen = sizeof(acceptc);
2745 struct sockaddr_in address;
2747 memset(&address, 0, sizeof(address));
2748 address.sin_addr.s_addr = inet_addr("127.0.0.1");
2749 address.sin_family = AF_INET;
2750 address.sin_port = htons(SERVERPORT);
2752 /* invalid socket tests */
2753 SetLastError(0xdeadbeef);
2754 ok ((listen(0, 0) == SOCKET_ERROR), "listen did not fail\n");
2755 ret = WSAGetLastError();
2756 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
2758 SetLastError(0xdeadbeef);
2759 ok ((listen(0xdeadbeef, 0) == SOCKET_ERROR), "listen did not fail\n");
2760 ret = WSAGetLastError();
2761 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret);
2763 /* tcp tests */
2764 fdA = socket(AF_INET, SOCK_STREAM, 0);
2765 ok ((fdA != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2767 fdB = socket(AF_INET, SOCK_STREAM, 0);
2768 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2770 SetLastError(0xdeadbeef);
2771 ok ((listen(fdA, -2) == SOCKET_ERROR), "listen did not fail\n");
2772 ret = WSAGetLastError();
2773 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
2775 SetLastError(0xdeadbeef);
2776 ok ((listen(fdA, 1) == SOCKET_ERROR), "listen did not fail\n");
2777 ret = WSAGetLastError();
2778 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
2780 SetLastError(0xdeadbeef);
2781 ok ((listen(fdA, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
2782 ret = WSAGetLastError();
2783 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
2785 ok (!bind(fdA, (struct sockaddr*) &address, sizeof(address)), "bind failed\n");
2787 SetLastError(0xdeadbeef);
2788 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
2789 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
2791 acceptc = 0xdead;
2792 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
2793 ok (!ret, "getsockopt failed\n");
2794 ok (acceptc == 0, "SO_ACCEPTCONN should be 0, received %d\n", acceptc);
2796 ok (!listen(fdA, 0), "listen failed\n");
2797 ok (!listen(fdA, SOMAXCONN), "double listen failed\n");
2799 acceptc = 0xdead;
2800 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen);
2801 ok (!ret, "getsockopt failed\n");
2802 ok (acceptc == 1, "SO_ACCEPTCONN should be 1, received %d\n", acceptc);
2804 SetLastError(0xdeadbeef);
2805 ok ((listen(fdB, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
2806 ret = WSAGetLastError();
2807 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret);
2809 ret = closesocket(fdB);
2810 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
2812 fdB = socket(AF_INET, SOCK_STREAM, 0);
2813 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2815 SetLastError(0xdeadbeef);
2816 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n");
2817 ret = WSAGetLastError();
2818 ok (ret == WSAEADDRINUSE, "expected 10048, received %d\n", ret);
2820 ret = closesocket(fdA);
2821 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
2822 ret = closesocket(fdB);
2823 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret);
2826 #define FD_ZERO_ALL() { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); }
2827 #define FD_SET_ALL(s) { FD_SET(s, &readfds); FD_SET(s, &writefds); FD_SET(s, &exceptfds); }
2828 static void test_select(void)
2830 static char tmp_buf[1024];
2832 SOCKET fdListen, fdRead, fdWrite;
2833 fd_set readfds, writefds, exceptfds;
2834 unsigned int maxfd;
2835 int ret, len;
2836 char buffer;
2837 struct timeval select_timeout;
2838 struct sockaddr_in address;
2839 select_thread_params thread_params;
2840 HANDLE thread_handle;
2841 DWORD ticks, id;
2843 fdRead = socket(AF_INET, SOCK_STREAM, 0);
2844 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2845 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
2846 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
2848 maxfd = fdRead;
2849 if (fdWrite > maxfd)
2850 maxfd = fdWrite;
2852 FD_ZERO_ALL();
2853 FD_SET_ALL(fdRead);
2854 FD_SET_ALL(fdWrite);
2855 select_timeout.tv_sec=0;
2856 select_timeout.tv_usec=0;
2858 ticks = GetTickCount();
2859 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2860 ticks = GetTickCount() - ticks;
2861 ok(ret == 0, "select should not return any socket handles\n");
2862 ok(ticks < 10, "select was blocking for %u ms, expected < 10 ms\n", ticks);
2863 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2864 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
2865 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2866 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
2868 FD_ZERO_ALL();
2869 FD_SET_ALL(fdRead);
2870 FD_SET_ALL(fdWrite);
2871 select_timeout.tv_sec=0;
2872 select_timeout.tv_usec=500;
2874 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2875 ok(ret == 0, "select should not return any socket handles\n");
2876 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2877 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
2878 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2879 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
2881 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
2882 ret = closesocket(fdWrite);
2883 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2885 thread_params.s = fdRead;
2886 thread_params.ReadKilled = FALSE;
2887 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2888 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id );
2889 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
2891 WaitForSingleObject (server_ready, INFINITE);
2892 Sleep(200);
2893 ret = closesocket(fdRead);
2894 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
2896 WaitForSingleObject (thread_handle, 1000);
2897 ok ( thread_params.ReadKilled, "closesocket did not wake up select\n");
2898 ret = recv(fdRead, &buffer, 1, MSG_PEEK);
2899 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret);
2901 /* Test selecting invalid handles */
2902 FD_ZERO_ALL();
2904 SetLastError(0);
2905 ret = select(maxfd+1, 0, 0, 0, &select_timeout);
2906 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2907 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2909 SetLastError(0);
2910 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2911 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2912 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError());
2914 FD_SET(INVALID_SOCKET, &readfds);
2915 SetLastError(0);
2916 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2917 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2918 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2919 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
2921 FD_ZERO(&readfds);
2922 FD_SET(INVALID_SOCKET, &writefds);
2923 SetLastError(0);
2924 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2925 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2926 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2927 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n");
2929 FD_ZERO(&writefds);
2930 FD_SET(INVALID_SOCKET, &exceptfds);
2931 SetLastError(0);
2932 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
2933 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret);
2934 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError());
2935 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
2937 tcp_socketpair(&fdRead, &fdWrite);
2938 maxfd = fdRead;
2939 if(fdWrite > maxfd) maxfd = fdWrite;
2941 FD_ZERO(&readfds);
2942 FD_SET(fdRead, &readfds);
2943 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
2944 ok(!ret, "select returned %d\n", ret);
2946 FD_ZERO(&writefds);
2947 FD_SET(fdWrite, &writefds);
2948 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
2949 ok(ret == 1, "select returned %d\n", ret);
2950 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
2952 /* tests for overlapping fd_set pointers */
2953 FD_ZERO(&readfds);
2954 FD_SET(fdWrite, &readfds);
2955 ret = select(fdWrite+1, &readfds, &readfds, NULL, &select_timeout);
2956 ok(ret == 1, "select returned %d\n", ret);
2957 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
2959 FD_ZERO(&readfds);
2960 FD_SET(fdWrite, &readfds);
2961 FD_SET(fdRead, &readfds);
2962 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
2963 ok(ret == 2, "select returned %d\n", ret);
2964 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
2965 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
2967 ok(send(fdWrite, "test", 4, 0) == 4, "failed to send data\n");
2968 FD_ZERO(&readfds);
2969 FD_SET(fdRead, &readfds);
2970 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout);
2971 ok(ret == 1, "select returned %d\n", ret);
2972 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
2974 FD_ZERO(&readfds);
2975 FD_SET(fdWrite, &readfds);
2976 FD_SET(fdRead, &readfds);
2977 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
2978 ok(ret == 2, "select returned %d\n", ret);
2979 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
2980 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
2982 while(1) {
2983 FD_ZERO(&writefds);
2984 FD_SET(fdWrite, &writefds);
2985 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout);
2986 if(!ret) break;
2987 ok(send(fdWrite, tmp_buf, sizeof(tmp_buf), 0) > 0, "failed to send data\n");
2989 FD_ZERO(&readfds);
2990 FD_SET(fdWrite, &readfds);
2991 FD_SET(fdRead, &readfds);
2992 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
2993 ok(ret == 1, "select returned %d\n", ret);
2994 ok(!FD_ISSET(fdWrite, &readfds), "fdWrite socket is in the set\n");
2995 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
2997 ok(send(fdRead, "test", 4, 0) == 4, "failed to send data\n");
2998 Sleep(100);
2999 FD_ZERO(&readfds);
3000 FD_SET(fdWrite, &readfds);
3001 FD_SET(fdRead, &readfds);
3002 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout);
3003 ok(ret == 2, "select returned %d\n", ret);
3004 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3005 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3007 closesocket(fdRead);
3008 closesocket(fdWrite);
3010 /* select() works in 3 distinct states:
3011 * - to check if a connection attempt ended with success or error;
3012 * - to check if a pending connection is waiting for acceptance;
3013 * - to check for data to read, availability for write and OOB data
3015 * The tests below ensure that all conditions are tested.
3017 memset(&address, 0, sizeof(address));
3018 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3019 address.sin_family = AF_INET;
3020 len = sizeof(address);
3021 fdListen = setup_server_socket(&address, &len);
3022 select_timeout.tv_sec = 1;
3023 select_timeout.tv_usec = 250000;
3025 /* When no events are pending select returns 0 with no error */
3026 FD_ZERO_ALL();
3027 FD_SET_ALL(fdListen);
3028 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3029 ok(ret == 0, "expected 0, got %d\n", ret);
3031 /* When a socket is attempting to connect the listening socket receives the read descriptor */
3032 fdWrite = setup_connector_socket(&address, len, TRUE);
3033 FD_ZERO_ALL();
3034 FD_SET_ALL(fdListen);
3035 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3036 ok(ret == 1, "expected 1, got %d\n", ret);
3037 ok(FD_ISSET(fdListen, &readfds), "fdListen socket is not in the set\n");
3038 len = sizeof(address);
3039 fdRead = accept(fdListen, (struct sockaddr*) &address, &len);
3040 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n");
3042 /* The connector is signaled through the write descriptor */
3043 FD_ZERO_ALL();
3044 FD_SET_ALL(fdListen);
3045 FD_SET_ALL(fdRead);
3046 FD_SET_ALL(fdWrite);
3047 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3048 ok(ret == 2, "expected 2, got %d\n", ret);
3049 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3050 ok(FD_ISSET(fdRead, &writefds), "fdRead socket is not in the set\n");
3051 len = sizeof(id);
3052 id = 0xdeadbeef;
3053 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
3054 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
3055 ok(id == 0, "expected 0, got %d\n", id);
3057 /* When data is received the receiver gets the read descriptor */
3058 ret = send(fdWrite, "1234", 4, 0);
3059 ok(ret == 4, "expected 4, got %d\n", ret);
3060 FD_ZERO_ALL();
3061 FD_SET_ALL(fdListen);
3062 FD_SET(fdRead, &readfds);
3063 FD_SET(fdRead, &exceptfds);
3064 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3065 ok(ret == 1, "expected 1, got %d\n", ret);
3066 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3067 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
3068 ok(ret == 4, "expected 4, got %d\n", ret);
3069 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n");
3071 /* When OOB data is received the socket is set in the except descriptor */
3072 ret = send(fdWrite, "A", 1, MSG_OOB);
3073 ok(ret == 1, "expected 1, got %d\n", ret);
3074 FD_ZERO_ALL();
3075 FD_SET_ALL(fdListen);
3076 FD_SET(fdRead, &readfds);
3077 FD_SET(fdRead, &exceptfds);
3078 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3079 ok(ret == 1, "expected 1, got %d\n", ret);
3080 ok(FD_ISSET(fdRead, &exceptfds), "fdRead socket is not in the set\n");
3081 tmp_buf[0] = 0xAF;
3082 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
3083 ok(ret == 1, "expected 1, got %d\n", ret);
3084 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
3086 /* If the socket is OOBINLINED it will not receive the OOB in except fds */
3087 ret = 1;
3088 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret));
3089 ok(ret == 0, "expected 0, got %d\n", ret);
3090 ret = send(fdWrite, "A", 1, MSG_OOB);
3091 ok(ret == 1, "expected 1, got %d\n", ret);
3092 FD_ZERO_ALL();
3093 FD_SET_ALL(fdListen);
3094 FD_SET(fdRead, &readfds);
3095 FD_SET(fdRead, &exceptfds);
3096 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3097 ok(ret == 1, "expected 1, got %d\n", ret);
3098 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n");
3099 tmp_buf[0] = 0xAF;
3100 SetLastError(0xdeadbeef);
3101 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB);
3102 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
3103 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError());
3104 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0);
3105 ok(ret == 1, "expected 1, got %d\n", ret);
3106 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]);
3108 /* When the connection is closed the socket is set in the read descriptor */
3109 ret = closesocket(fdRead);
3110 ok(ret == 0, "expected 0, got %d\n", ret);
3111 FD_ZERO_ALL();
3112 FD_SET_ALL(fdListen);
3113 FD_SET(fdWrite, &readfds);
3114 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3115 ok(ret == 1, "expected 1, got %d\n", ret);
3116 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3117 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0);
3118 ok(ret == 0, "expected 0, got %d\n", ret);
3119 ret = closesocket(fdWrite);
3120 ok(ret == 0, "expected 0, got %d\n", ret);
3121 ret = closesocket(fdListen);
3122 ok(ret == 0, "expected 0, got %d\n", ret);
3124 /* w10pro64 sometimes takes over 2 seconds for an error to be reported. */
3125 if (winetest_interactive)
3127 len = sizeof(address);
3128 fdWrite = setup_connector_socket(&address, len, TRUE);
3129 FD_ZERO_ALL();
3130 FD_SET(fdWrite, &writefds);
3131 FD_SET(fdWrite, &exceptfds);
3132 select_timeout.tv_sec = 10;
3133 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3134 ok(ret == 1, "expected 1, got %d\n", ret);
3135 len = sizeof(id);
3136 id = 0xdeadbeef;
3137 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len);
3138 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
3139 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id);
3140 ok(FD_ISSET(fdWrite, &exceptfds), "fdWrite socket is not in the set\n");
3141 ok(select_timeout.tv_usec == 250000, "select timeout should not have changed\n");
3142 closesocket(fdWrite);
3145 /* Try select() on a closed socket after connection */
3146 tcp_socketpair(&fdRead, &fdWrite);
3147 closesocket(fdRead);
3148 FD_ZERO_ALL();
3149 FD_SET_ALL(fdWrite);
3150 FD_SET_ALL(fdRead);
3151 SetLastError(0xdeadbeef);
3152 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
3153 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3154 ok(GetLastError() == WSAENOTSOCK, "got %d\n", GetLastError());
3155 /* descriptor sets are unchanged */
3156 ok(readfds.fd_count == 2, "expected 2, got %d\n", readfds.fd_count);
3157 ok(exceptfds.fd_count == 2, "expected 2, got %d\n", exceptfds.fd_count);
3158 closesocket(fdWrite);
3160 /* Close the socket currently being selected in a thread - bug 38399 */
3161 tcp_socketpair(&fdRead, &fdWrite);
3162 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
3163 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
3164 FD_ZERO_ALL();
3165 FD_SET_ALL(fdWrite);
3166 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout);
3167 ok(ret == 1, "expected 1, got %d\n", ret);
3168 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n");
3169 WaitForSingleObject (thread_handle, 1000);
3170 closesocket(fdRead);
3171 /* test again with only the except descriptor */
3172 tcp_socketpair(&fdRead, &fdWrite);
3173 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id);
3174 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError());
3175 FD_ZERO_ALL();
3176 FD_SET(fdWrite, &exceptfds);
3177 SetLastError(0xdeadbeef);
3178 ret = select(0, NULL, NULL, &exceptfds, &select_timeout);
3179 todo_wine
3180 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3181 ok(GetLastError() == WSAENOTSOCK, "got %d\n", GetLastError());
3182 WaitForSingleObject (thread_handle, 1000);
3183 closesocket(fdRead);
3185 /* test UDP behavior of unbound sockets */
3186 select_timeout.tv_sec = 0;
3187 select_timeout.tv_usec = 250000;
3188 fdWrite = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3189 ok(fdWrite != INVALID_SOCKET, "socket call failed\n");
3190 FD_ZERO_ALL();
3191 FD_SET_ALL(fdWrite);
3192 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout);
3193 ok(ret == 1, "expected 1, got %d\n", ret);
3194 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n");
3195 closesocket(fdWrite);
3197 #undef FD_SET_ALL
3198 #undef FD_ZERO_ALL
3200 static DWORD WINAPI AcceptKillThread(void *param)
3202 select_thread_params *par = param;
3203 struct sockaddr_in address;
3204 int len = sizeof(address);
3205 SOCKET client_socket;
3207 SetEvent(server_ready);
3208 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
3209 if (client_socket != INVALID_SOCKET)
3210 closesocket(client_socket);
3211 par->ReadKilled = (client_socket == INVALID_SOCKET);
3212 return 0;
3216 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
3217 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
3218 GROUP *g, DWORD_PTR dwCallbackData)
3220 return CF_DEFER;
3223 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len)
3225 int ret, val;
3226 SOCKET server_socket;
3228 server_socket = socket(AF_INET, SOCK_STREAM, 0);
3229 ok(server_socket != INVALID_SOCKET, "failed to bind socket, error %u\n", WSAGetLastError());
3231 val = 1;
3232 ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
3233 ok(!ret, "failed to set SO_REUSEADDR, error %u\n", WSAGetLastError());
3235 ret = bind(server_socket, (struct sockaddr *)addr, *len);
3236 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3238 ret = getsockname(server_socket, (struct sockaddr *)addr, len);
3239 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
3241 ret = listen(server_socket, 5);
3242 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
3244 return server_socket;
3247 static SOCKET setup_connector_socket(const struct sockaddr_in *addr, int len, BOOL nonblock)
3249 int ret;
3250 SOCKET connector;
3252 connector = socket(AF_INET, SOCK_STREAM, 0);
3253 ok(connector != INVALID_SOCKET, "failed to create connector socket %d\n", WSAGetLastError());
3255 if (nonblock)
3256 set_blocking(connector, !nonblock);
3258 ret = connect(connector, (const struct sockaddr *)addr, len);
3259 if (!nonblock)
3260 ok(!ret, "connecting to accepting socket failed %d\n", WSAGetLastError());
3261 else if (ret == SOCKET_ERROR)
3262 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
3264 return connector;
3267 static void test_accept(void)
3269 int ret;
3270 SOCKET server_socket, accepted = INVALID_SOCKET, connector;
3271 struct sockaddr_in address;
3272 SOCKADDR_STORAGE ss, ss_empty;
3273 int socklen;
3274 select_thread_params thread_params;
3275 HANDLE thread_handle = NULL;
3276 DWORD id;
3278 memset(&address, 0, sizeof(address));
3279 address.sin_addr.s_addr = inet_addr("127.0.0.1");
3280 address.sin_family = AF_INET;
3282 socklen = sizeof(address);
3283 server_socket = setup_server_socket(&address, &socklen);
3285 connector = setup_connector_socket(&address, socklen, FALSE);
3286 if (connector == INVALID_SOCKET) goto done;
3288 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0);
3289 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
3291 accepted = accept(server_socket, NULL, 0);
3292 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
3294 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
3296 thread_params.s = server_socket;
3297 thread_params.ReadKilled = FALSE;
3298 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id);
3300 WaitForSingleObject(server_ready, INFINITE);
3301 Sleep(200);
3302 ret = closesocket(server_socket);
3303 ok(!ret, "failed to close socket, error %u\n", WSAGetLastError());
3305 WaitForSingleObject(thread_handle, 1000);
3306 ok(thread_params.ReadKilled, "closesocket did not wake up accept\n");
3308 closesocket(accepted);
3309 closesocket(connector);
3310 accepted = connector = INVALID_SOCKET;
3312 socklen = sizeof(address);
3313 server_socket = setup_server_socket(&address, &socklen);
3315 connector = setup_connector_socket(&address, socklen, FALSE);
3316 if (connector == INVALID_SOCKET) goto done;
3318 socklen = 0;
3319 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
3320 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
3321 ok(!socklen, "got %d\n", socklen);
3322 closesocket(connector);
3323 connector = INVALID_SOCKET;
3325 socklen = sizeof(address);
3326 connector = setup_connector_socket(&address, socklen, FALSE);
3327 if (connector == INVALID_SOCKET) goto done;
3329 accepted = WSAAccept(server_socket, NULL, NULL, NULL, 0);
3330 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3331 closesocket(accepted);
3332 closesocket(connector);
3333 accepted = connector = INVALID_SOCKET;
3335 socklen = sizeof(address);
3336 connector = setup_connector_socket(&address, socklen, FALSE);
3337 if (connector == INVALID_SOCKET) goto done;
3339 socklen = sizeof(ss);
3340 memset(&ss, 0, sizeof(ss));
3341 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0);
3342 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3343 ok(socklen != sizeof(ss), "unexpected length\n");
3344 ok(ss.ss_family, "family not set\n");
3345 closesocket(accepted);
3346 closesocket(connector);
3347 accepted = connector = INVALID_SOCKET;
3349 socklen = sizeof(address);
3350 connector = setup_connector_socket(&address, socklen, FALSE);
3351 if (connector == INVALID_SOCKET) goto done;
3353 socklen = 0;
3354 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
3355 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError());
3356 ok(!socklen, "got %d\n", socklen);
3357 closesocket(connector);
3358 accepted = connector = INVALID_SOCKET;
3360 socklen = sizeof(address);
3361 connector = setup_connector_socket(&address, socklen, FALSE);
3362 if (connector == INVALID_SOCKET) goto done;
3364 accepted = accept(server_socket, NULL, NULL);
3365 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3366 closesocket(accepted);
3367 closesocket(connector);
3368 accepted = connector = INVALID_SOCKET;
3370 socklen = sizeof(address);
3371 connector = setup_connector_socket(&address, socklen, FALSE);
3372 if (connector == INVALID_SOCKET) goto done;
3374 socklen = sizeof(ss);
3375 memset(&ss, 0, sizeof(ss));
3376 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen);
3377 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3378 ok(socklen != sizeof(ss), "unexpected length\n");
3379 ok(ss.ss_family, "family not set\n");
3380 closesocket(accepted);
3381 closesocket(connector);
3382 accepted = connector = INVALID_SOCKET;
3384 socklen = sizeof(address);
3385 connector = setup_connector_socket(&address, socklen, FALSE);
3386 if (connector == INVALID_SOCKET) goto done;
3388 memset(&ss, 0, sizeof(ss));
3389 memset(&ss_empty, 0, sizeof(ss_empty));
3390 accepted = accept(server_socket, (struct sockaddr *)&ss, NULL);
3391 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError());
3392 ok(!memcmp(&ss, &ss_empty, sizeof(ss)), "structure is different\n");
3394 done:
3395 if (accepted != INVALID_SOCKET)
3396 closesocket(accepted);
3397 if (connector != INVALID_SOCKET)
3398 closesocket(connector);
3399 if (thread_handle != NULL)
3400 CloseHandle(thread_handle);
3401 if (server_ready != INVALID_HANDLE_VALUE)
3402 CloseHandle(server_ready);
3403 if (server_socket != INVALID_SOCKET)
3404 closesocket(server_socket);
3407 static void test_extendedSocketOptions(void)
3409 WSADATA wsa;
3410 SOCKET sock;
3411 struct sockaddr_in sa;
3412 int sa_len = sizeof(struct sockaddr_in);
3413 int optval, optlen = sizeof(int), ret;
3414 BOOL bool_opt_val;
3415 LINGER linger_val;
3417 ret = WSAStartup(MAKEWORD(2,0), &wsa);
3418 ok(!ret, "failed to startup, error %u\n", WSAGetLastError());
3420 memset(&sa, 0, sa_len);
3422 sa.sin_family = AF_INET;
3423 sa.sin_port = htons(0);
3424 sa.sin_addr.s_addr = htonl(INADDR_ANY);
3426 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
3427 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
3429 ret = bind(sock, (struct sockaddr *) &sa, sa_len);
3430 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3432 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3434 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
3435 ok((optval == 65507) || (optval == 65527),
3436 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
3438 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
3439 SetLastError(0xdeadbeef);
3440 optval = 0xdeadbeef;
3441 optlen = sizeof(int);
3442 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3443 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3444 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3445 ret, WSAGetLastError(), optval, optval);
3447 /* more invalid values for level */
3448 SetLastError(0xdeadbeef);
3449 optval = 0xdeadbeef;
3450 optlen = sizeof(int);
3451 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3452 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3453 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3454 ret, WSAGetLastError(), optval, optval);
3456 SetLastError(0xdeadbeef);
3457 optval = 0xdeadbeef;
3458 optlen = sizeof(int);
3459 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3460 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3461 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3462 ret, WSAGetLastError(), optval, optval);
3464 SetLastError(0xdeadbeef);
3465 optval = 0xdeadbeef;
3466 optlen = sizeof(int);
3467 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3468 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3469 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3470 ret, WSAGetLastError(), optval, optval);
3472 SetLastError(0xdeadbeef);
3473 optval = 0xdeadbeef;
3474 optlen = sizeof(int);
3475 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
3476 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
3477 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
3478 ret, WSAGetLastError(), optval, optval);
3480 SetLastError(0xdeadbeef);
3481 optlen = sizeof(LINGER);
3482 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
3483 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT),
3484 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n",
3485 ret, WSAGetLastError());
3486 closesocket(sock);
3488 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
3489 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
3491 ret = bind(sock, (struct sockaddr *) &sa, sa_len);
3492 ok(!ret, "failed to bind socket, error %u\n", WSAGetLastError());
3494 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
3495 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
3497 optlen = sizeof(BOOL);
3498 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
3499 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
3500 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
3501 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
3502 bool_opt_val, linger_val.l_onoff);
3504 closesocket(sock);
3505 WSACleanup();
3508 static void test_getsockname(void)
3510 WSADATA wsa;
3511 SOCKET sock;
3512 struct sockaddr_in sa_set, sa_get;
3513 int sa_set_len = sizeof(struct sockaddr_in);
3514 int sa_get_len = sa_set_len;
3515 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
3516 int ret;
3517 struct hostent *h;
3519 ret = WSAStartup(MAKEWORD(2,0), &wsa);
3520 ok(!ret, "failed to startup, error %u\n", WSAGetLastError());
3522 memset(&sa_set, 0, sa_set_len);
3524 sa_set.sin_family = AF_INET;
3525 sa_set.sin_port = htons(0);
3526 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
3528 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
3529 ok(sock != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
3531 sa_get = sa_set;
3532 ret = getsockname(sock, (struct sockaddr *)&sa_get, &sa_get_len);
3533 ok(ret == SOCKET_ERROR, "expected failure\n");
3534 ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
3535 ok(!memcmp(&sa_get, &sa_set, sizeof(sa_get)), "address should not be changed\n");
3537 ret = bind(sock, (struct sockaddr *) &sa_set, sa_set_len);
3538 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
3540 ret = getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len);
3541 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
3543 ret = memcmp(sa_get.sin_zero, null_padding, 8);
3544 ok(ret == 0, "getsockname did not zero the sockaddr_in structure\n");
3546 closesocket(sock);
3548 h = gethostbyname("");
3549 if (h && h->h_length == 4) /* this test is only meaningful in IPv4 */
3551 int i;
3552 for (i = 0; h->h_addr_list[i]; i++)
3554 char ipstr[32];
3555 struct in_addr ip;
3556 ip.s_addr = *(ULONG *) h->h_addr_list[i];
3558 sock = socket(AF_INET, SOCK_DGRAM, 0);
3559 ok(sock != INVALID_SOCKET, "socket failed with %d\n", GetLastError());
3561 memset(&sa_set, 0, sizeof(sa_set));
3562 sa_set.sin_family = AF_INET;
3563 sa_set.sin_addr.s_addr = ip.s_addr;
3564 /* The same address we bind must be the same address we get */
3565 ret = bind(sock, (struct sockaddr*)&sa_set, sizeof(sa_set));
3566 ok(ret == 0, "bind failed with %d\n", GetLastError());
3567 sa_get_len = sizeof(sa_get);
3568 ret = getsockname(sock, (struct sockaddr*)&sa_get, &sa_get_len);
3569 ok(ret == 0, "getsockname failed with %d\n", GetLastError());
3570 strcpy(ipstr, inet_ntoa(sa_get.sin_addr));
3571 ok(sa_get.sin_addr.s_addr == sa_set.sin_addr.s_addr,
3572 "address does not match: %s != %s\n", ipstr, inet_ntoa(sa_set.sin_addr));
3574 closesocket(sock);
3578 WSACleanup();
3581 static void test_ioctlsocket(void)
3583 SOCKET sock, src, dst;
3584 struct tcp_keepalive kalive;
3585 struct sockaddr_in address;
3586 int ret, optval;
3587 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
3588 UINT i, bytes_rec;
3589 char data;
3590 WSABUF bufs;
3591 u_long arg = 0;
3593 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3594 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
3596 for(i = 0; i < ARRAY_SIZE(cmds); i++)
3598 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
3599 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
3600 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
3601 ret = WSAGetLastError();
3602 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
3605 /* A fresh and not connected socket has no urgent data, this test shows
3606 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
3608 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3609 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
3610 ok(arg, "SIOCATMARK expected a non-zero value\n");
3612 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */
3613 optval = 1;
3614 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
3615 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
3616 arg = 0;
3617 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3618 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
3619 ok(arg, "SIOCATMARK expected a non-zero value\n");
3621 /* disable SO_OOBINLINE and get the same old behavior */
3622 optval = 0;
3623 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
3624 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
3625 arg = 0;
3626 ret = ioctlsocket(sock, SIOCATMARK, &arg);
3627 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n");
3628 ok(arg, "SIOCATMARK expected a non-zero value\n");
3630 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL);
3631 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
3632 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
3634 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3635 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
3636 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
3638 make_keepalive(kalive, 0, 0, 0);
3639 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3640 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3642 make_keepalive(kalive, 1, 0, 0);
3643 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3644 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3646 make_keepalive(kalive, 1, 1000, 1000);
3647 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3648 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3650 make_keepalive(kalive, 1, 10000, 10000);
3651 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3652 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3654 make_keepalive(kalive, 1, 100, 100);
3655 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3656 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3658 make_keepalive(kalive, 0, 100, 100);
3659 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL);
3660 ok(ret == 0, "WSAIoctl failed unexpectedly\n");
3662 closesocket(sock);
3664 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3665 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
3667 /* test FIONREAD with a fresh and non-connected socket */
3668 arg = 0xdeadbeef;
3669 ret = ioctlsocket(sock, FIONREAD, &arg);
3670 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
3671 ok(arg == 0, "expected 0, got %u\n", arg);
3673 memset(&address, 0, sizeof(address));
3674 address.sin_family = AF_INET;
3675 address.sin_addr.s_addr = inet_addr( SERVERIP );
3676 address.sin_port = htons( SERVERPORT );
3677 ret = bind(sock, (struct sockaddr *)&address, sizeof(address));
3678 ok(ret == 0, "bind failed unexpectedly with error %d\n", WSAGetLastError());
3680 ret = listen(sock, SOMAXCONN);
3681 ok(ret == 0, "listen failed unexpectedly with error %d\n", WSAGetLastError());
3683 /* test FIONREAD with listening socket */
3684 arg = 0xdeadbeef;
3685 ret = ioctlsocket(sock, FIONREAD, &arg);
3686 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError());
3687 ok(arg == 0, "expected 0, got %u\n", arg);
3689 closesocket(sock);
3691 tcp_socketpair(&src, &dst);
3693 /* test FIONREAD on TCP sockets */
3694 optval = 0xdeadbeef;
3695 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
3696 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
3697 ok(optval == 0, "FIONREAD should have returned 0 bytes, got %d instead\n", optval);
3699 optval = 0xdeadbeef;
3700 ok(send(src, "TEST", 4, 0) == 4, "failed to send test data\n");
3701 Sleep(100);
3702 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
3703 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError());
3704 ok(optval == 4, "FIONREAD should have returned 4 bytes, got %d instead\n", optval);
3706 /* trying to read from an OOB inlined socket with MSG_OOB results in WSAEINVAL */
3707 set_blocking(dst, FALSE);
3708 i = MSG_OOB;
3709 SetLastError(0xdeadbeef);
3710 ret = recv(dst, &data, 1, i);
3711 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3712 ret = GetLastError();
3713 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
3714 bufs.len = sizeof(char);
3715 bufs.buf = &data;
3716 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
3717 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3718 ret = GetLastError();
3719 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret);
3720 optval = 1;
3721 ret = setsockopt(dst, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval));
3722 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n");
3723 i = MSG_OOB;
3724 SetLastError(0xdeadbeef);
3725 ret = recv(dst, &data, 1, i);
3726 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret);
3727 ret = GetLastError();
3728 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
3729 bufs.len = sizeof(char);
3730 bufs.buf = &data;
3731 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL);
3732 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
3733 ret = GetLastError();
3734 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret);
3736 closesocket(dst);
3737 optval = 0xdeadbeef;
3738 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL);
3739 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
3740 ok(optval == 0xdeadbeef, "FIONREAD should not have changed last error, got %d instead\n", optval);
3741 closesocket(src);
3744 static BOOL drain_pause = FALSE;
3745 static DWORD WINAPI drain_socket_thread(LPVOID arg)
3747 char buffer[1024];
3748 SOCKET sock = *(SOCKET*)arg;
3749 int ret;
3751 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
3753 if (ret < 0)
3755 if (WSAGetLastError() == WSAEWOULDBLOCK)
3757 fd_set readset;
3758 FD_ZERO(&readset);
3759 FD_SET(sock, &readset);
3760 select(sock+1, &readset, NULL, NULL, NULL);
3761 while (drain_pause)
3762 Sleep(100);
3764 else
3765 break;
3768 return 0;
3771 static void test_send(void)
3773 SOCKET src = INVALID_SOCKET;
3774 SOCKET dst = INVALID_SOCKET;
3775 HANDLE hThread = NULL;
3776 const int buflen = 1024*1024;
3777 char *buffer = NULL;
3778 int ret, i, zero = 0;
3779 WSABUF buf;
3780 OVERLAPPED ov;
3781 BOOL bret;
3782 DWORD id, bytes_sent, dwRet;
3784 memset(&ov, 0, sizeof(ov));
3786 tcp_socketpair(&src, &dst);
3788 set_blocking(dst, FALSE);
3789 /* force disable buffering so we can get a pending overlapped request */
3790 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero));
3791 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError());
3793 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
3795 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
3797 /* fill the buffer with some nonsense */
3798 for (i = 0; i < buflen; ++i)
3800 buffer[i] = (char) i;
3803 ret = send(src, buffer, buflen, 0);
3804 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
3806 buf.buf = buffer;
3807 buf.len = buflen;
3809 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
3810 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
3811 if (!ov.hEvent)
3812 goto end;
3814 bytes_sent = 0;
3815 WSASetLastError(12345);
3816 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL);
3817 ok(ret == SOCKET_ERROR, "expected failure\n");
3818 ok(WSAGetLastError() == ERROR_IO_PENDING, "wrong error %u\n", WSAGetLastError());
3820 /* don't check for completion yet, we may need to drain the buffer while still sending */
3821 set_blocking(src, FALSE);
3822 for (i = 0; i < buflen; ++i)
3824 int j = 0;
3826 ret = recv(src, buffer, 1, 0);
3827 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100)
3829 j++;
3830 Sleep(50);
3831 ret = recv(src, buffer, 1, 0);
3834 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen);
3835 if (ret != 1)
3836 break;
3838 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i);
3841 dwRet = WaitForSingleObject(ov.hEvent, 1000);
3842 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError());
3843 if (dwRet == WAIT_OBJECT_0)
3845 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE);
3846 ok(bret && bytes_sent == buflen,
3847 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError());
3850 WSASetLastError(12345);
3851 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL);
3852 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
3853 "WSASend failed %d - %d\n", ret, WSAGetLastError());
3855 WSASetLastError(12345);
3856 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL);
3857 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING,
3858 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError());
3860 end:
3861 if (src != INVALID_SOCKET)
3862 closesocket(src);
3863 if (dst != INVALID_SOCKET)
3864 closesocket(dst);
3865 if (hThread != NULL)
3867 dwRet = WaitForSingleObject(hThread, 500);
3868 ok(dwRet == WAIT_OBJECT_0, "failed to wait for thread termination: %d\n", GetLastError());
3869 CloseHandle(hThread);
3871 if (ov.hEvent)
3872 CloseHandle(ov.hEvent);
3873 HeapFree(GetProcessHeap(), 0, buffer);
3876 #define WM_SOCKET (WM_USER+100)
3878 struct event_test_ctx
3880 int is_message;
3881 SOCKET socket;
3882 HANDLE event;
3883 HWND window;
3886 static void select_events(struct event_test_ctx *ctx, SOCKET socket, LONG events)
3888 int ret;
3890 if (ctx->is_message)
3891 ret = WSAAsyncSelect(socket, ctx->window, WM_USER, events);
3892 else
3893 ret = WSAEventSelect(socket, ctx->event, events);
3894 ok(!ret, "failed to select, error %u\n", WSAGetLastError());
3895 ctx->socket = socket;
3898 #define check_events(a, b, c, d) check_events_(__LINE__, a, b, c, d, FALSE, FALSE)
3899 #define check_events_todo(a, b, c, d) check_events_(__LINE__, a, b, c, d, TRUE, TRUE)
3900 #define check_events_todo_event(a, b, c, d) check_events_(__LINE__, a, b, c, d, TRUE, FALSE)
3901 #define check_events_todo_msg(a, b, c, d) check_events_(__LINE__, a, b, c, d, FALSE, TRUE)
3902 static void check_events_(int line, struct event_test_ctx *ctx,
3903 LONG flag1, LONG flag2, DWORD timeout, BOOL todo_event, BOOL todo_msg)
3905 int ret;
3907 if (ctx->is_message)
3909 BOOL any_fail = FALSE;
3910 MSG msg;
3912 if (flag1)
3914 ret = PeekMessageA(&msg, ctx->window, WM_USER, WM_USER, PM_REMOVE);
3915 while (!ret && !MsgWaitForMultipleObjects(0, NULL, FALSE, timeout, QS_POSTMESSAGE))
3916 ret = PeekMessageA(&msg, ctx->window, WM_USER, WM_USER, PM_REMOVE);
3917 todo_wine_if (todo_msg && !ret) ok_(__FILE__, line)(ret, "expected a message\n");
3918 if (ret)
3920 ok_(__FILE__, line)(msg.wParam == ctx->socket,
3921 "expected wparam %#Ix, got %#Ix\n", ctx->socket, msg.wParam);
3922 todo_wine_if (todo_msg && msg.lParam != flag1)
3923 ok_(__FILE__, line)(msg.lParam == flag1, "got first event %#Ix\n", msg.lParam);
3924 if (msg.lParam != flag1) any_fail = TRUE;
3926 else
3927 any_fail = TRUE;
3929 if (flag2)
3931 ret = PeekMessageA(&msg, ctx->window, WM_USER, WM_USER, PM_REMOVE);
3932 while (!ret && !MsgWaitForMultipleObjects(0, NULL, FALSE, timeout, QS_POSTMESSAGE))
3933 ret = PeekMessageA(&msg, ctx->window, WM_USER, WM_USER, PM_REMOVE);
3934 ok_(__FILE__, line)(ret, "expected a message\n");
3935 ok_(__FILE__, line)(msg.wParam == ctx->socket, "got wparam %#Ix\n", msg.wParam);
3936 todo_wine_if (todo_msg) ok_(__FILE__, line)(msg.lParam == flag2, "got second event %#Ix\n", msg.lParam);
3938 ret = PeekMessageA(&msg, ctx->window, WM_USER, WM_USER, PM_REMOVE);
3939 todo_wine_if (todo_msg && ret) ok_(__FILE__, line)(!ret, "got unexpected event %#Ix\n", msg.lParam);
3940 if (ret) any_fail = TRUE;
3942 /* catch tests which succeed */
3943 todo_wine_if (todo_msg) ok_(__FILE__, line)(!any_fail, "event series matches\n");
3945 else
3947 WSANETWORKEVENTS events;
3949 ret = WaitForSingleObject(ctx->event, timeout);
3950 if (flag1 | flag2)
3951 todo_wine_if (todo_event && ret) ok_(__FILE__, line)(!ret, "event wait timed out\n");
3952 else
3953 todo_wine_if (todo_event) ok_(__FILE__, line)(ret == WAIT_TIMEOUT, "expected timeout\n");
3954 ret = WSAEnumNetworkEvents(ctx->socket, ctx->event, &events);
3955 ok_(__FILE__, line)(!ret, "failed to get events, error %u\n", WSAGetLastError());
3956 todo_wine_if (todo_event)
3957 ok_(__FILE__, line)(events.lNetworkEvents == (flag1 | flag2), "got events %#x\n", events.lNetworkEvents);
3961 static void test_accept_events(struct event_test_ctx *ctx)
3963 const struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)};
3964 SOCKET listener, server, client, client2;
3965 struct sockaddr_in destaddr;
3966 int len, ret;
3968 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3969 ok(listener != -1, "failed to create socket, error %u\n", WSAGetLastError());
3971 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
3973 ret = bind(listener, (const struct sockaddr *)&addr, sizeof(addr));
3974 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
3975 len = sizeof(destaddr);
3976 ret = getsockname(listener, (struct sockaddr *)&destaddr, &len);
3977 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
3978 ret = listen(listener, 2);
3979 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
3981 check_events(ctx, 0, 0, 0);
3983 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3984 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
3985 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
3986 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
3988 check_events(ctx, FD_ACCEPT, 0, 200);
3989 check_events(ctx, 0, 0, 0);
3990 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
3991 if (ctx->is_message)
3992 check_events(ctx, FD_ACCEPT, 0, 200);
3993 check_events_todo_event(ctx, 0, 0, 0);
3994 select_events(ctx, listener, 0);
3995 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
3996 if (ctx->is_message)
3997 check_events(ctx, FD_ACCEPT, 0, 200);
3998 check_events_todo_event(ctx, 0, 0, 0);
4000 client2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4001 ok(client2 != -1, "failed to create socket, error %u\n", WSAGetLastError());
4002 ret = connect(client2, (struct sockaddr *)&destaddr, sizeof(destaddr));
4003 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4005 if (!ctx->is_message)
4006 check_events_todo(ctx, FD_ACCEPT, 0, 200);
4007 check_events(ctx, 0, 0, 0);
4009 server = accept(listener, NULL, NULL);
4010 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4011 closesocket(server);
4013 check_events(ctx, FD_ACCEPT, 0, 200);
4014 check_events(ctx, 0, 0, 0);
4016 server = accept(listener, NULL, NULL);
4017 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4018 closesocket(server);
4020 check_events(ctx, 0, 0, 0);
4022 closesocket(client2);
4023 closesocket(client);
4025 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4026 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4027 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4028 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4030 check_events(ctx, FD_ACCEPT, 0, 200);
4032 server = accept(listener, NULL, NULL);
4033 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4034 closesocket(server);
4035 closesocket(client);
4037 check_events(ctx, 0, 0, 200);
4039 closesocket(listener);
4041 /* Connect and then select. */
4043 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4044 ok(listener != -1, "failed to create socket, error %u\n", WSAGetLastError());
4045 ret = bind(listener, (const struct sockaddr *)&addr, sizeof(addr));
4046 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
4047 len = sizeof(destaddr);
4048 ret = getsockname(listener, (struct sockaddr *)&destaddr, &len);
4049 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
4050 ret = listen(listener, 2);
4051 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
4053 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4054 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4055 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4056 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4058 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
4060 check_events(ctx, FD_ACCEPT, 0, 200);
4062 server = accept(listener, NULL, NULL);
4063 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4064 closesocket(server);
4065 closesocket(client);
4067 /* As above, but select on a subset not containing FD_ACCEPT first. */
4069 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB);
4071 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4072 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4073 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4074 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4076 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
4077 check_events(ctx, FD_ACCEPT, 0, 200);
4079 server = accept(listener, NULL, NULL);
4080 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4081 closesocket(server);
4082 closesocket(client);
4084 /* As above, but call accept() before selecting. */
4086 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB);
4088 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4089 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4090 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4091 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4092 Sleep(200);
4093 server = accept(listener, NULL, NULL);
4094 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4096 select_events(ctx, listener, FD_CONNECT | FD_READ | FD_OOB | FD_ACCEPT);
4097 check_events(ctx, 0, 0, 200);
4099 closesocket(server);
4100 closesocket(client);
4102 closesocket(listener);
4105 static void test_connect_events(struct event_test_ctx *ctx)
4107 const struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)};
4108 SOCKET listener, server, client;
4109 struct sockaddr_in destaddr;
4110 int len, ret;
4112 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4113 ok(listener != -1, "failed to create socket, error %u\n", WSAGetLastError());
4114 ret = bind(listener, (const struct sockaddr *)&addr, sizeof(addr));
4115 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
4116 len = sizeof(destaddr);
4117 ret = getsockname(listener, (struct sockaddr *)&destaddr, &len);
4118 ok(!ret, "failed to get address, error %u\n", WSAGetLastError());
4119 ret = listen(listener, 2);
4120 ok(!ret, "failed to listen, error %u\n", WSAGetLastError());
4122 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4123 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4125 select_events(ctx, client, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4126 check_events(ctx, 0, 0, 0);
4128 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4129 ok(!ret || WSAGetLastError() == WSAEWOULDBLOCK, "failed to connect, error %u\n", WSAGetLastError());
4131 check_events(ctx, FD_CONNECT, FD_WRITE, 200);
4132 check_events(ctx, 0, 0, 0);
4133 select_events(ctx, client, 0);
4134 select_events(ctx, client, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4135 if (ctx->is_message)
4136 check_events(ctx, FD_WRITE, 0, 200);
4137 check_events_todo_event(ctx, 0, 0, 0);
4139 server = accept(listener, NULL, NULL);
4140 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4142 closesocket(client);
4143 closesocket(server);
4145 /* Connect and then select. */
4147 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4148 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4150 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4151 ok(!ret, "failed to connect, error %u\n", WSAGetLastError());
4153 server = accept(listener, NULL, NULL);
4154 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4156 ret = send(client, "data", 5, 0);
4157 ok(ret == 5, "got %d\n", ret);
4159 select_events(ctx, client, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4160 if (ctx->is_message)
4161 check_events(ctx, FD_WRITE, 0, 200);
4162 else
4163 check_events_todo(ctx, FD_CONNECT, FD_WRITE, 200);
4165 closesocket(client);
4166 closesocket(server);
4168 /* As above, but select on a subset not containing FD_CONNECT first. */
4170 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4171 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
4173 select_events(ctx, client, FD_ACCEPT | FD_CLOSE | FD_OOB | FD_READ | FD_WRITE);
4175 ret = connect(client, (struct sockaddr *)&destaddr, sizeof(destaddr));
4176 ok(!ret || WSAGetLastError() == WSAEWOULDBLOCK, "failed to connect, error %u\n", WSAGetLastError());
4178 server = accept(listener, NULL, NULL);
4179 ok(server != -1, "failed to accept, error %u\n", WSAGetLastError());
4181 check_events_todo_msg(ctx, FD_WRITE, 0, 200);
4183 select_events(ctx, client, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4185 if (ctx->is_message)
4186 check_events(ctx, FD_WRITE, 0, 200);
4187 else
4188 check_events_todo(ctx, FD_CONNECT, 0, 200);
4190 closesocket(client);
4191 closesocket(server);
4193 closesocket(listener);
4196 /* perform a blocking recv() even on a nonblocking socket */
4197 static int sync_recv(SOCKET s, void *buffer, int len, DWORD flags)
4199 OVERLAPPED overlapped = {0};
4200 WSABUF wsabuf;
4201 DWORD ret_len;
4202 int ret;
4204 overlapped.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
4205 wsabuf.buf = buffer;
4206 wsabuf.len = len;
4207 ret = WSARecv(s, &wsabuf, 1, &ret_len, &flags, &overlapped, NULL);
4208 if (ret == -1 && WSAGetLastError() == ERROR_IO_PENDING)
4210 ret = WaitForSingleObject(overlapped.hEvent, 1000);
4211 ok(!ret, "wait timed out\n");
4212 ret = WSAGetOverlappedResult(s, &overlapped, &ret_len, FALSE, &flags);
4213 ret = (ret ? 0 : -1);
4215 CloseHandle(overlapped.hEvent);
4216 if (!ret) return ret_len;
4217 return -1;
4220 static void test_write_events(struct event_test_ctx *ctx)
4222 static const int buffer_size = 1024 * 1024;
4223 SOCKET server, client;
4224 char *buffer;
4225 int ret;
4227 buffer = malloc(buffer_size);
4229 tcp_socketpair(&client, &server);
4230 set_blocking(client, FALSE);
4232 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4233 check_events(ctx, FD_WRITE, 0, 200);
4234 check_events(ctx, 0, 0, 0);
4235 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4236 if (ctx->is_message)
4237 check_events(ctx, FD_WRITE, 0, 200);
4238 check_events_todo_event(ctx, 0, 0, 0);
4239 select_events(ctx, server, 0);
4240 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4241 if (ctx->is_message)
4242 check_events(ctx, FD_WRITE, 0, 200);
4243 check_events_todo_event(ctx, 0, 0, 0);
4245 ret = send(server, "data", 5, 0);
4246 ok(ret == 5, "got %d\n", ret);
4248 check_events(ctx, 0, 0, 0);
4250 ret = sync_recv(client, buffer, buffer_size, 0);
4251 ok(ret == 5, "got %d\n", ret);
4253 check_events(ctx, 0, 0, 0);
4255 if (!broken(1))
4257 while (send(server, buffer, buffer_size, 0) == buffer_size);
4258 todo_wine ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
4260 while (recv(client, buffer, buffer_size, 0) > 0);
4261 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
4263 /* Broken on Windows versions older than win10v1607 (though sometimes
4264 * works regardless, for unclear reasons. */
4265 check_events(ctx, FD_WRITE, 0, 200);
4266 check_events(ctx, 0, 0, 0);
4267 select_events(ctx, server, 0);
4268 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4269 if (ctx->is_message)
4270 check_events(ctx, FD_WRITE, 0, 200);
4271 check_events_todo_event(ctx, 0, 0, 0);
4274 closesocket(server);
4275 closesocket(client);
4277 /* Despite the documentation, and unlike FD_ACCEPT and FD_RECV, calling
4278 * send() doesn't clear the FD_WRITE bit. */
4280 tcp_socketpair(&client, &server);
4282 select_events(ctx, server, FD_ACCEPT | FD_CONNECT | FD_OOB | FD_READ | FD_WRITE);
4284 ret = send(server, "data", 5, 0);
4285 ok(ret == 5, "got %d\n", ret);
4287 check_events(ctx, FD_WRITE, 0, 200);
4289 closesocket(server);
4290 closesocket(client);
4292 free(buffer);
4295 static void test_read_events(struct event_test_ctx *ctx)
4297 SOCKET server, client;
4298 unsigned int i;
4299 char buffer[8];
4300 int ret;
4302 tcp_socketpair(&client, &server);
4303 set_blocking(client, FALSE);
4305 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4306 check_events(ctx, 0, 0, 0);
4308 ret = send(client, "data", 5, 0);
4309 ok(ret == 5, "got %d\n", ret);
4311 check_events(ctx, FD_READ, 0, 200);
4312 check_events(ctx, 0, 0, 0);
4313 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4314 if (ctx->is_message)
4315 check_events(ctx, FD_READ, 0, 200);
4316 check_events_todo_event(ctx, 0, 0, 0);
4317 select_events(ctx, server, 0);
4318 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4319 if (ctx->is_message)
4320 check_events(ctx, FD_READ, 0, 200);
4321 check_events_todo_event(ctx, 0, 0, 0);
4323 ret = send(client, "data", 5, 0);
4324 ok(ret == 5, "got %d\n", ret);
4326 if (!ctx->is_message)
4327 check_events_todo(ctx, FD_READ, 0, 200);
4328 check_events(ctx, 0, 0, 0);
4330 ret = recv(server, buffer, 2, 0);
4331 ok(ret == 2, "got %d\n", ret);
4333 check_events(ctx, FD_READ, 0, 200);
4334 check_events(ctx, 0, 0, 0);
4336 ret = recv(server, buffer, -1, 0);
4337 ok(ret == -1, "got %d\n", ret);
4338 ok(WSAGetLastError() == WSAEFAULT || WSAGetLastError() == WSAENOBUFS /* < Windows 7 */,
4339 "got error %u\n", WSAGetLastError());
4341 if (ctx->is_message)
4342 check_events_todo_msg(ctx, FD_READ, 0, 200);
4343 check_events(ctx, 0, 0, 0);
4345 for (i = 0; i < 8; ++i)
4347 ret = sync_recv(server, buffer, 1, 0);
4348 ok(ret == 1, "got %d\n", ret);
4350 if (i < 7)
4351 check_events(ctx, FD_READ, 0, 200);
4352 check_events(ctx, 0, 0, 0);
4355 /* Send data while we're not selecting. */
4357 select_events(ctx, server, 0);
4358 ret = send(client, "data", 5, 0);
4359 ok(ret == 5, "got %d\n", ret);
4360 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4362 check_events(ctx, FD_READ, 0, 200);
4364 ret = recv(server, buffer, 5, 0);
4365 ok(ret == 5, "got %d\n", ret);
4367 select_events(ctx, server, 0);
4368 ret = send(client, "data", 5, 0);
4369 ok(ret == 5, "got %d\n", ret);
4370 ret = sync_recv(server, buffer, 5, 0);
4371 ok(ret == 5, "got %d\n", ret);
4372 select_events(ctx, server, FD_ACCEPT | FD_CONNECT | FD_OOB | FD_READ);
4374 check_events(ctx, 0, 0, 200);
4376 closesocket(server);
4377 closesocket(client);
4380 static void test_oob_events(struct event_test_ctx *ctx)
4382 SOCKET server, client;
4383 char buffer[1];
4384 int ret;
4386 tcp_socketpair(&client, &server);
4387 set_blocking(client, FALSE);
4389 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4390 check_events(ctx, 0, 0, 0);
4392 ret = send(client, "a", 1, MSG_OOB);
4393 ok(ret == 1, "got %d\n", ret);
4395 check_events_todo_msg(ctx, FD_OOB, 0, 200);
4396 check_events_todo(ctx, 0, 0, 0);
4397 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4398 if (ctx->is_message)
4399 check_events_todo_msg(ctx, FD_OOB, 0, 200);
4400 check_events_todo(ctx, 0, 0, 0);
4401 select_events(ctx, server, 0);
4402 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4403 if (ctx->is_message)
4404 check_events_todo_msg(ctx, FD_OOB, 0, 200);
4405 check_events_todo(ctx, 0, 0, 0);
4407 ret = send(client, "b", 1, MSG_OOB);
4408 ok(ret == 1, "got %d\n", ret);
4410 if (!ctx->is_message)
4411 check_events(ctx, FD_OOB, 0, 200);
4412 check_events_todo(ctx, 0, 0, 0);
4414 ret = recv(server, buffer, 1, MSG_OOB);
4415 ok(ret == 1, "got %d\n", ret);
4417 check_events_todo_msg(ctx, FD_OOB, 0, 200);
4418 check_events_todo_msg(ctx, 0, 0, 0);
4420 ret = recv(server, buffer, 1, MSG_OOB);
4421 todo_wine ok(ret == 1, "got %d\n", ret);
4423 check_events_todo_msg(ctx, 0, 0, 0);
4425 /* Send data while we're not selecting. */
4427 select_events(ctx, server, 0);
4428 ret = send(client, "a", 1, MSG_OOB);
4429 ok(ret == 1, "got %d\n", ret);
4430 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4432 check_events_todo_msg(ctx, FD_OOB, 0, 200);
4434 ret = recv(server, buffer, 1, MSG_OOB);
4435 ok(ret == 1, "got %d\n", ret);
4437 closesocket(server);
4438 closesocket(client);
4441 static void test_close_events(struct event_test_ctx *ctx)
4443 SOCKET server, client;
4444 char buffer[5];
4445 int ret;
4447 /* Test closesocket(). */
4449 tcp_socketpair(&client, &server);
4451 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4453 closesocket(client);
4455 check_events(ctx, FD_CLOSE, 0, 200);
4456 check_events(ctx, 0, 0, 0);
4457 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4458 if (ctx->is_message)
4459 check_events_todo_msg(ctx, FD_CLOSE, 0, 200);
4460 check_events(ctx, 0, 0, 0);
4461 select_events(ctx, server, 0);
4462 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4463 if (ctx->is_message)
4464 check_events_todo_msg(ctx, FD_CLOSE, 0, 200);
4465 check_events(ctx, 0, 0, 0);
4467 closesocket(server);
4469 /* Test shutdown(remote end, SD_SEND). */
4471 tcp_socketpair(&client, &server);
4473 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4475 shutdown(client, SD_SEND);
4477 check_events(ctx, FD_CLOSE, 0, 200);
4478 check_events(ctx, 0, 0, 0);
4480 closesocket(client);
4482 check_events(ctx, 0, 0, 0);
4484 closesocket(server);
4486 /* No other shutdown() call generates an event. */
4488 tcp_socketpair(&client, &server);
4490 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4492 shutdown(client, SD_RECEIVE);
4493 shutdown(server, SD_BOTH);
4495 check_events(ctx, 0, 0, 200);
4497 shutdown(client, SD_SEND);
4499 check_events_todo(ctx, FD_CLOSE, 0, 200);
4500 check_events(ctx, 0, 0, 0);
4502 closesocket(server);
4503 closesocket(client);
4505 /* Test sending data before calling closesocket(). */
4507 tcp_socketpair(&client, &server);
4509 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4511 ret = send(client, "data", 5, 0);
4512 ok(ret == 5, "got %d\n", ret);
4514 check_events(ctx, FD_READ, 0, 200);
4516 closesocket(client);
4518 check_events_todo(ctx, FD_CLOSE, 0, 200);
4520 ret = recv(server, buffer, 3, 0);
4521 ok(ret == 3, "got %d\n", ret);
4523 check_events(ctx, FD_READ, 0, 200);
4525 ret = recv(server, buffer, 5, 0);
4526 ok(ret == 2, "got %d\n", ret);
4528 check_events_todo(ctx, 0, 0, 0);
4530 closesocket(server);
4532 /* Close and then select. */
4534 tcp_socketpair(&client, &server);
4535 closesocket(client);
4537 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4538 check_events(ctx, FD_CLOSE, 0, 200);
4540 closesocket(server);
4542 /* As above, but select on a subset not containing FD_CLOSE first. */
4544 tcp_socketpair(&client, &server);
4546 select_events(ctx, server, FD_ACCEPT | FD_CONNECT | FD_OOB | FD_READ);
4548 closesocket(client);
4550 check_events(ctx, 0, 0, 200);
4551 select_events(ctx, server, FD_ACCEPT | FD_CLOSE | FD_CONNECT | FD_OOB | FD_READ);
4552 check_events_todo_event(ctx, FD_CLOSE, 0, 200);
4554 closesocket(server);
4557 static void test_events(void)
4559 struct event_test_ctx ctx;
4561 ctx.is_message = FALSE;
4562 ctx.event = CreateEventW(NULL, TRUE, FALSE, NULL);
4564 test_accept_events(&ctx);
4565 test_connect_events(&ctx);
4566 test_write_events(&ctx);
4567 test_read_events(&ctx);
4568 test_close_events(&ctx);
4569 test_oob_events(&ctx);
4571 CloseHandle(ctx.event);
4573 ctx.is_message = TRUE;
4574 ctx.window = CreateWindowA("Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
4576 test_accept_events(&ctx);
4577 test_connect_events(&ctx);
4578 test_write_events(&ctx);
4579 test_read_events(&ctx);
4580 test_close_events(&ctx);
4581 test_oob_events(&ctx);
4583 DestroyWindow(ctx.window);
4586 static void test_ipv6only(void)
4588 SOCKET v4 = INVALID_SOCKET, v6;
4589 struct sockaddr_in sin4;
4590 struct sockaddr_in6 sin6;
4591 int ret, enabled, len = sizeof(enabled);
4593 memset(&sin4, 0, sizeof(sin4));
4594 sin4.sin_family = AF_INET;
4595 sin4.sin_port = htons(SERVERPORT);
4597 memset(&sin6, 0, sizeof(sin6));
4598 sin6.sin6_family = AF_INET6;
4599 sin6.sin6_port = htons(SERVERPORT);
4601 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
4602 if (v6 == INVALID_SOCKET)
4604 skip("Could not create IPv6 socket (LastError: %d)\n", WSAGetLastError());
4605 goto end;
4608 enabled = 2;
4609 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4610 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4611 ok(enabled == 1, "expected 1, got %d\n", enabled);
4613 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
4614 ok(!ret, "failed to bind, error %u\n", WSAGetLastError());
4616 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4617 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
4619 todo_wine {
4620 enabled = 2;
4621 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4622 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4623 ok(enabled == 1, "expected 1, got %d\n", enabled);
4626 enabled = 0;
4627 len = sizeof(enabled);
4628 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
4629 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4631 todo_wine {
4632 enabled = 2;
4633 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4634 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4635 ok(!enabled, "expected 0, got %d\n", enabled);
4638 enabled = 1;
4639 len = sizeof(enabled);
4640 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
4641 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4643 /* bind on IPv4 socket should succeed - IPV6_V6ONLY is enabled by default */
4644 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
4645 ok(!ret, "Could not bind IPv4 address (LastError: %d)\n", WSAGetLastError());
4647 todo_wine {
4648 enabled = 2;
4649 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4650 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4651 ok(enabled == 1, "expected 1, got %d\n", enabled);
4654 enabled = 0;
4655 len = sizeof(enabled);
4656 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
4657 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
4659 todo_wine {
4660 enabled = 0;
4661 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4662 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4663 ok(enabled == 1, "expected 1, got %d\n", enabled);
4666 enabled = 1;
4667 len = sizeof(enabled);
4668 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
4669 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError());
4671 closesocket(v4);
4672 closesocket(v6);
4674 /* Test again, this time disabling IPV6_V6ONLY. */
4675 sin4.sin_port = htons(SERVERPORT+2);
4676 sin6.sin6_port = htons(SERVERPORT+2);
4678 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
4679 ok(v6 != INVALID_SOCKET, "Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
4680 WSAGetLastError(), WSAEAFNOSUPPORT);
4682 enabled = 0;
4683 ret = setsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len);
4684 ok(!ret, "Could not disable IPV6_V6ONLY (LastError: %d).\n", WSAGetLastError());
4686 enabled = 2;
4687 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4688 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4689 ok(!enabled, "expected 0, got %d\n", enabled);
4692 Observaition:
4693 On Windows, bind on both IPv4 and IPv6 with IPV6_V6ONLY disabled succeeds by default.
4694 Application must set SO_EXCLUSIVEADDRUSE on first socket to disallow another successful bind.
4695 In general, a standard application should not use SO_REUSEADDR.
4696 Setting both SO_EXCLUSIVEADDRUSE and SO_REUSEADDR on the same socket is not possible in
4697 either order, the later setsockopt call always fails.
4699 enabled = 1;
4700 ret = setsockopt(v6, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&enabled, len);
4701 ok(!ret, "Could not set SO_EXCLUSIVEADDRUSE on IPv6 socket (LastError: %d)\n", WSAGetLastError());
4703 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
4704 ok(!ret, "Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError());
4706 enabled = 2;
4707 len = sizeof(enabled);
4708 getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len);
4709 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError());
4710 ok(!enabled, "IPV6_V6ONLY is enabled after bind\n");
4712 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4713 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError());
4715 enabled = 1;
4716 ret = setsockopt(v4, SOL_SOCKET, SO_REUSEADDR, (char*)&enabled, len);
4717 ok(!ret, "Could not set SO_REUSEADDR on IPv4 socket (LastError: %d)\n", WSAGetLastError());
4719 WSASetLastError(0xdeadbeef);
4720 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
4721 ok(ret, "bind succeeded unexpectedly for the IPv4 socket\n");
4722 ok(WSAGetLastError() == WSAEACCES, "Expected 10013, got %d\n", WSAGetLastError());
4724 end:
4725 if (v4 != INVALID_SOCKET)
4726 closesocket(v4);
4727 if (v6 != INVALID_SOCKET)
4728 closesocket(v6);
4731 static void test_WSASendMsg(void)
4733 SOCKET sock, dst;
4734 struct sockaddr_in sendaddr, sockaddr;
4735 GUID WSASendMsg_GUID = WSAID_WSASENDMSG;
4736 LPFN_WSASENDMSG pWSASendMsg = NULL;
4737 char teststr[12] = "hello world", buffer[32];
4738 WSABUF iovec[2];
4739 WSAMSG msg;
4740 DWORD bytesSent, err;
4741 int ret, addrlen;
4743 /* FIXME: Missing OVERLAPPED and OVERLAPPED COMPLETION ROUTINE tests */
4745 sock = socket(AF_INET, SOCK_DGRAM, 0);
4746 ok(sock != INVALID_SOCKET, "socket() failed\n");
4748 /* Obtain the WSASendMsg function */
4749 WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSASendMsg_GUID, sizeof(WSASendMsg_GUID),
4750 &pWSASendMsg, sizeof(pWSASendMsg), &err, NULL, NULL);
4751 if (!pWSASendMsg)
4753 closesocket(sock);
4754 win_skip("WSASendMsg is unsupported, some tests will be skipped.\n");
4755 return;
4758 /* fake address for now */
4759 sendaddr.sin_family = AF_INET;
4760 sendaddr.sin_port = htons(139);
4761 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
4763 memset(&msg, 0, sizeof(msg));
4764 iovec[0].buf = teststr;
4765 iovec[0].len = sizeof(teststr);
4766 iovec[1].buf = teststr;
4767 iovec[1].len = sizeof(teststr) / 2;
4768 msg.name = (struct sockaddr *) &sendaddr;
4769 msg.namelen = sizeof(sendaddr);
4770 msg.lpBuffers = iovec;
4771 msg.dwBufferCount = 1; /* send only one buffer for now */
4773 WSASetLastError(0xdeadbeef);
4774 ret = pWSASendMsg(INVALID_SOCKET, &msg, 0, NULL, NULL, NULL);
4775 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
4776 err = WSAGetLastError();
4777 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err);
4779 WSASetLastError(0xdeadbeef);
4780 ret = pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL);
4781 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
4782 err = WSAGetLastError();
4783 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
4785 WSASetLastError(0xdeadbeef);
4786 ret = pWSASendMsg(sock, NULL, 0, &bytesSent, NULL, NULL);
4787 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
4788 err = WSAGetLastError();
4789 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
4791 WSASetLastError(0xdeadbeef);
4792 ret = pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL);
4793 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
4794 err = WSAGetLastError();
4795 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err);
4797 closesocket(sock);
4799 sock = socket(AF_INET, SOCK_DGRAM, 0);
4800 ok(sock != INVALID_SOCKET, "socket() failed\n");
4802 dst = socket(AF_INET, SOCK_DGRAM, 0);
4803 ok(dst != INVALID_SOCKET, "socket() failed\n");
4805 memset(&sockaddr, 0, sizeof(sockaddr));
4806 sockaddr.sin_family = AF_INET;
4807 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
4808 ok(!bind(dst, (struct sockaddr*)&sockaddr, sizeof(sockaddr)),
4809 "bind should have worked\n");
4811 /* read address to find out the port number to be used in send */
4812 memset(&sendaddr, 0, sizeof(sendaddr));
4813 addrlen = sizeof(sendaddr);
4814 ok(!getsockname(dst, (struct sockaddr *) &sendaddr, &addrlen),
4815 "getsockname should have worked\n");
4816 ok(sendaddr.sin_port, "socket port should be != 0\n");
4818 /* ensure the sending socket is not bound */
4819 WSASetLastError(0xdeadbeef);
4820 addrlen = sizeof(sockaddr);
4821 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
4822 ok(ret == SOCKET_ERROR, "getsockname should have failed\n");
4823 err = WSAGetLastError();
4824 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
4826 set_blocking(sock, TRUE);
4828 bytesSent = 0;
4829 SetLastError(0xdeadbeef);
4830 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
4831 ok(!ret, "WSASendMsg should have worked\n");
4832 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
4833 "Expected 0, got %d\n", GetLastError());
4834 ok(bytesSent == iovec[0].len, "incorrect bytes sent, expected %d, sent %d\n",
4835 iovec[0].len, bytesSent);
4837 /* receive data */
4838 addrlen = sizeof(sockaddr);
4839 memset(buffer, 0, sizeof(buffer));
4840 SetLastError(0xdeadbeef);
4841 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
4842 ok(ret == bytesSent, "got %d, expected %d\n",
4843 ret, bytesSent);
4844 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
4846 /* A successful call to WSASendMsg must have bound the socket */
4847 addrlen = sizeof(sockaddr);
4848 sockaddr.sin_port = 0;
4849 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
4850 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen);
4851 ok(!ret, "getsockname should have worked\n");
4852 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
4853 inet_ntoa(sockaddr.sin_addr));
4854 ok(sockaddr.sin_port, "sin_port should be != 0\n");
4856 msg.dwBufferCount = 2; /* send both buffers */
4858 bytesSent = 0;
4859 SetLastError(0xdeadbeef);
4860 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
4861 ok(!ret, "WSASendMsg should have worked\n");
4862 ok(bytesSent == iovec[0].len + iovec[1].len, "incorrect bytes sent, expected %d, sent %d\n",
4863 iovec[0].len + iovec[1].len, bytesSent);
4864 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */,
4865 "Expected 0, got %d\n", GetLastError());
4867 /* receive data */
4868 addrlen = sizeof(sockaddr);
4869 memset(buffer, 0, sizeof(buffer));
4870 SetLastError(0xdeadbeef);
4871 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen);
4872 ok(ret == bytesSent, "got %d, expected %d\n",
4873 ret, bytesSent);
4874 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
4876 closesocket(sock);
4877 closesocket(dst);
4879 /* a bad call to WSASendMsg will also bind the socket */
4880 addrlen = sizeof(sockaddr);
4881 sockaddr.sin_port = 0;
4882 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
4883 sock = socket(AF_INET, SOCK_DGRAM, 0);
4884 ok(sock != INVALID_SOCKET, "socket() failed\n");
4885 ok(pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
4886 todo_wine {
4887 ok(!getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have worked\n");
4888 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n",
4889 inet_ntoa(sockaddr.sin_addr));
4890 ok(sockaddr.sin_port, "sin_port should be > 0\n");
4892 closesocket(sock);
4894 /* a bad call without msg parameter will not trigger the auto-bind */
4895 sock = socket(AF_INET, SOCK_DGRAM, 0);
4896 ok(sock != INVALID_SOCKET, "socket() failed\n");
4897 ok(pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n");
4898 ok(getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have failed\n");
4899 err = WSAGetLastError();
4900 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err);
4901 closesocket(sock);
4903 /* SOCK_STREAM sockets are not supported */
4904 bytesSent = 0;
4905 sock = socket(AF_INET, SOCK_STREAM, 0);
4906 ok(sock != INVALID_SOCKET, "socket() failed\n");
4907 SetLastError(0xdeadbeef);
4908 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL);
4909 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n");
4910 err = WSAGetLastError();
4911 todo_wine
4912 ok(err == WSAEINVAL, "expected 10014, got %d instead\n", err);
4913 closesocket(sock);
4916 static void test_WSASendTo(void)
4918 SOCKET s;
4919 struct sockaddr_in addr;
4920 char buf[12] = "hello world";
4921 WSABUF data_buf;
4922 DWORD bytesSent;
4923 int ret;
4925 addr.sin_family = AF_INET;
4926 addr.sin_port = htons(139);
4927 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
4928 data_buf.len = sizeof(buf);
4929 data_buf.buf = buf;
4931 s = socket(AF_INET, SOCK_DGRAM, 0);
4932 ok(s != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
4934 WSASetLastError(12345);
4935 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4936 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK,
4937 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4939 WSASetLastError(12345);
4940 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL);
4941 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
4942 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError());
4944 WSASetLastError(12345);
4945 ret = WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr *)&addr, sizeof(addr), NULL, NULL);
4946 ok(!ret, "expected success\n");
4947 ok(!WSAGetLastError(), "got error %u\n", WSAGetLastError());
4950 static DWORD WINAPI recv_thread(LPVOID arg)
4952 SOCKET sock = *(SOCKET *)arg;
4953 char buffer[32];
4954 WSABUF wsa;
4955 WSAOVERLAPPED ov;
4956 DWORD flags = 0;
4958 wsa.buf = buffer;
4959 wsa.len = sizeof(buffer);
4960 ov.hEvent = WSACreateEvent();
4961 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL);
4963 WaitForSingleObject(ov.hEvent, 1000);
4964 WSACloseEvent(ov.hEvent);
4965 return 0;
4968 static int completion_called;
4970 static void WINAPI io_completion(DWORD error, DWORD transferred, WSAOVERLAPPED *overlapped, DWORD flags)
4972 completion_called++;
4975 static void test_WSARecv(void)
4977 SOCKET src, dest, server = INVALID_SOCKET;
4978 char buf[20];
4979 WSABUF bufs[2];
4980 WSAOVERLAPPED ov;
4981 DWORD bytesReturned, flags, id;
4982 struct linger ling;
4983 struct sockaddr_in addr;
4984 int iret, len;
4985 DWORD dwret;
4986 BOOL bret;
4987 HANDLE thread, event = NULL, io_port;
4989 tcp_socketpair(&src, &dest);
4991 memset(&ov, 0, sizeof(ov));
4992 flags = 0;
4993 bufs[0].len = 2;
4994 bufs[0].buf = buf;
4996 /* Send 4 bytes and receive in two calls of 2 */
4997 SetLastError(0xdeadbeef);
4998 iret = send(src, "test", 4, 0);
4999 ok(iret == 4, "Expected 4, got %d\n", iret);
5000 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5001 SetLastError(0xdeadbeef);
5002 bytesReturned = 0xdeadbeef;
5003 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5004 ok(!iret, "Expected 0, got %d\n", iret);
5005 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
5006 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5007 SetLastError(0xdeadbeef);
5008 bytesReturned = 0xdeadbeef;
5009 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5010 ok(!iret, "Expected 0, got %d\n", iret);
5011 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned);
5012 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5014 bufs[0].len = 4;
5015 SetLastError(0xdeadbeef);
5016 iret = send(src, "test", 4, 0);
5017 ok(iret == 4, "Expected 4, got %d\n", iret);
5018 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5019 SetLastError(0xdeadbeef);
5020 bytesReturned = 0xdeadbeef;
5021 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL);
5022 ok(!iret, "Expected 0, got %d\n", iret);
5023 ok(bytesReturned == 4, "Expected 4, got %d\n", bytesReturned);
5024 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5026 /* Test 2 buffers */
5027 bufs[0].len = 4;
5028 bufs[1].len = 5;
5029 bufs[1].buf = buf + 10;
5030 SetLastError(0xdeadbeef);
5031 iret = send(src, "deadbeefs", 9, 0);
5032 ok(iret == 9, "Expected 9, got %d\n", iret);
5033 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5034 SetLastError(0xdeadbeef);
5035 bytesReturned = 0xdeadbeef;
5036 iret = WSARecv(dest, bufs, 2, &bytesReturned, &flags, NULL, NULL);
5037 ok(!iret, "Expected 0, got %d\n", iret);
5038 ok(bytesReturned == 9, "Expected 9, got %d\n", bytesReturned);
5039 bufs[0].buf[4] = '\0';
5040 bufs[1].buf[5] = '\0';
5041 ok(!strcmp(bufs[0].buf, "dead"), "buf[0] doesn't match: %s != dead\n", bufs[0].buf);
5042 ok(!strcmp(bufs[1].buf, "beefs"), "buf[1] doesn't match: %s != beefs\n", bufs[1].buf);
5043 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError());
5045 bufs[0].len = sizeof(buf);
5046 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
5047 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
5048 if (!event)
5049 goto end;
5051 ling.l_onoff = 1;
5052 ling.l_linger = 0;
5053 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
5054 ok(!iret, "Failed to set linger %d\n", GetLastError());
5056 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, NULL);
5057 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
5059 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, &ov, NULL);
5060 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
5062 closesocket(src);
5063 src = INVALID_SOCKET;
5065 dwret = WaitForSingleObject(ov.hEvent, 1000);
5066 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError());
5068 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE);
5069 todo_wine ok(!bret, "expected failure\n");
5070 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "got error %u\n", GetLastError());
5071 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned);
5072 closesocket(dest);
5073 dest = INVALID_SOCKET;
5075 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
5076 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
5077 if (src == INVALID_SOCKET) goto end;
5079 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
5080 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
5081 if (server == INVALID_SOCKET) goto end;
5083 memset(&addr, 0, sizeof(addr));
5084 addr.sin_family = AF_INET;
5085 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
5086 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr));
5087 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5089 len = sizeof(addr);
5090 iret = getsockname(server, (struct sockaddr *)&addr, &len);
5091 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
5093 iret = listen(server, 1);
5094 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
5096 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr));
5097 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
5099 len = sizeof(addr);
5100 dest = accept(server, (struct sockaddr *)&addr, &len);
5101 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError());
5102 if (dest == INVALID_SOCKET) goto end;
5104 send(src, "test message", sizeof("test message"), 0);
5105 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id);
5106 WaitForSingleObject(thread, 3000);
5107 CloseHandle(thread);
5109 memset(&ov, 0, sizeof(ov));
5110 ov.hEvent = event;
5111 ResetEvent(event);
5112 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
5113 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError());
5114 send(src, "test message", sizeof("test message"), 0);
5116 completion_called = 0;
5117 dwret = SleepEx(1000, TRUE);
5118 ok(dwret == WAIT_IO_COMPLETION, "got %u\n", dwret);
5119 ok(completion_called == 1, "completion not called\n");
5121 dwret = WaitForSingleObject(event, 1);
5122 ok(dwret == WAIT_TIMEOUT, "got %u\n", dwret);
5124 io_port = CreateIoCompletionPort( (HANDLE)dest, NULL, 0, 0 );
5125 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5127 /* Using completion function on socket associated with completion port is not allowed. */
5128 memset(&ov, 0, sizeof(ov));
5129 completion_called = 0;
5130 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion);
5131 ok(iret == SOCKET_ERROR && GetLastError() == WSAEINVAL, "WSARecv failed - %d error %d\n", iret, GetLastError());
5132 ok(!completion_called, "completion called\n");
5134 CloseHandle(io_port);
5136 end:
5137 if (server != INVALID_SOCKET)
5138 closesocket(server);
5139 if (dest != INVALID_SOCKET)
5140 closesocket(dest);
5141 if (src != INVALID_SOCKET)
5142 closesocket(src);
5143 if (event)
5144 WSACloseEvent(event);
5147 struct write_watch_thread_args
5149 int func;
5150 SOCKET dest;
5151 void *base;
5152 DWORD size;
5153 const char *expect;
5156 static DWORD CALLBACK write_watch_thread( void *arg )
5158 struct write_watch_thread_args *args = arg;
5159 struct sockaddr addr;
5160 int addr_len = sizeof(addr), ret;
5161 DWORD bytes, flags = 0;
5162 WSABUF buf[1];
5164 switch (args->func)
5166 case 0:
5167 ret = recv( args->dest, args->base, args->size, 0 );
5168 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
5169 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
5170 break;
5171 case 1:
5172 ret = recvfrom( args->dest, args->base, args->size, 0, &addr, &addr_len );
5173 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret );
5174 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
5175 break;
5176 case 2:
5177 buf[0].len = args->size;
5178 buf[0].buf = args->base;
5179 ret = WSARecv( args->dest, buf, 1, &bytes, &flags, NULL, NULL );
5180 ok( !ret, "WSARecv failed %u\n", GetLastError() );
5181 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
5182 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
5183 break;
5184 case 3:
5185 buf[0].len = args->size;
5186 buf[0].buf = args->base;
5187 ret = WSARecvFrom( args->dest, buf, 1, &bytes, &flags, &addr, &addr_len, NULL, NULL );
5188 ok( !ret, "WSARecvFrom failed %u\n", GetLastError() );
5189 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes );
5190 ok( !strcmp( args->base, args->expect ), "wrong data\n" );
5191 break;
5193 return 0;
5196 static void test_write_watch(void)
5198 SOCKET src, dest;
5199 WSABUF bufs[2];
5200 WSAOVERLAPPED ov;
5201 struct write_watch_thread_args args;
5202 DWORD bytesReturned, flags, size;
5203 struct sockaddr addr;
5204 int addr_len, ret;
5205 HANDLE thread, event;
5206 char *base;
5207 void *results[64];
5208 ULONG_PTR count;
5209 ULONG pagesize;
5210 UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
5212 pGetWriteWatch = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetWriteWatch" );
5213 if (!pGetWriteWatch)
5215 win_skip( "write watched not supported\n" );
5216 return;
5219 tcp_socketpair(&src, &dest);
5221 memset(&ov, 0, sizeof(ov));
5222 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL);
5223 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError());
5225 flags = 0;
5227 size = 0x10000;
5228 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
5229 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
5231 memset( base, 0, size );
5232 count = 64;
5233 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5234 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5235 ok( count == 16, "wrong count %lu\n", count );
5237 bufs[0].len = 5;
5238 bufs[0].buf = base;
5239 bufs[1].len = 0x8000;
5240 bufs[1].buf = base + 0x4000;
5242 ret = WSARecv( dest, bufs, 2, NULL, &flags, &ov, NULL);
5243 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
5244 "WSARecv failed - %d error %d\n", ret, GetLastError());
5246 count = 64;
5247 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5248 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5249 ok( count == 9, "wrong count %lu\n", count );
5250 ok( !base[0], "data set\n" );
5252 send(src, "test message", sizeof("test message"), 0);
5254 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
5255 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
5256 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
5257 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
5258 ok( !memcmp( base + 0x4000, "message", 8 ), "wrong data %s\n", base + 0x4000 );
5260 count = 64;
5261 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5262 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5263 ok( count == 0, "wrong count %lu\n", count );
5265 memset( base, 0, size );
5266 count = 64;
5267 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5268 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5269 ok( count == 16, "wrong count %lu\n", count );
5271 bufs[1].len = 0x4000;
5272 bufs[1].buf = base + 0x2000;
5273 ret = WSARecvFrom( dest, bufs, 2, NULL, &flags, &addr, &addr_len, &ov, NULL);
5274 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING,
5275 "WSARecv failed - %d error %d\n", ret, GetLastError());
5277 count = 64;
5278 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5279 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5280 ok( count == 5, "wrong count %lu\n", count );
5281 ok( !base[0], "data set\n" );
5283 send(src, "test message", sizeof("test message"), 0);
5285 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE );
5286 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() );
5287 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned );
5288 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base );
5289 ok( !memcmp( base + 0x2000, "message", 8 ), "wrong data %s\n", base + 0x2000 );
5291 count = 64;
5292 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5293 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5294 ok( count == 0, "wrong count %lu\n", count );
5296 memset( base, 0, size );
5297 count = 64;
5298 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5299 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5300 ok( count == 16, "wrong count %lu\n", count );
5302 args.dest = dest;
5303 args.base = base;
5304 args.size = 0x7002;
5305 args.expect = "test message";
5306 for (args.func = 0; args.func < 4; args.func++)
5308 thread = CreateThread( NULL, 0, write_watch_thread, &args, 0, NULL );
5309 Sleep( 200 );
5311 count = 64;
5312 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5313 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5314 ok( count == 8, "wrong count %lu\n", count );
5316 send(src, "test message", sizeof("test message"), 0);
5317 WaitForSingleObject( thread, 10000 );
5318 CloseHandle( thread );
5320 count = 64;
5321 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
5322 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
5323 ok( count == 0, "wrong count %lu\n", count );
5325 WSACloseEvent( event );
5326 closesocket( dest );
5327 closesocket( src );
5328 VirtualFree( base, 0, MEM_FREE );
5331 #define check_poll(a, b) check_poll_(__LINE__, a, POLLRDNORM | POLLRDBAND | POLLWRNORM, b, FALSE)
5332 #define check_poll_todo(a, b) check_poll_(__LINE__, a, POLLRDNORM | POLLRDBAND | POLLWRNORM, b, TRUE)
5333 #define check_poll_mask(a, b, c) check_poll_(__LINE__, a, b, c, FALSE)
5334 #define check_poll_mask_todo(a, b, c) check_poll_(__LINE__, a, b, c, TRUE)
5335 static void check_poll_(int line, SOCKET s, short mask, short expect, BOOL todo)
5337 WSAPOLLFD pollfd;
5338 int ret;
5340 pollfd.fd = s;
5341 pollfd.events = mask;
5342 pollfd.revents = 0xdead;
5343 ret = pWSAPoll(&pollfd, 1, 1000);
5344 ok_(__FILE__, line)(ret == (pollfd.revents ? 1 : 0), "WSAPoll() returned %d\n", ret);
5345 todo_wine_if (todo) ok_(__FILE__, line)(pollfd.revents == expect, "got wrong events %#x\n", pollfd.revents);
5348 static void test_WSAPoll(void)
5350 const struct sockaddr_in bind_addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)};
5351 int ret, err, len;
5352 SOCKET listener, server, client;
5353 struct sockaddr_in address;
5354 WSAPOLLFD fds[16];
5355 HANDLE thread_handle;
5356 unsigned int i;
5357 char buffer[6];
5359 static const short invalid_flags[] =
5360 {POLLERR, POLLHUP, POLLNVAL, 0x8, POLLWRBAND, 0x40, 0x80, POLLPRI, 0x800, 0x1000, 0x2000, 0x4000, 0x8000};
5362 if (!pWSAPoll) /* >= Vista */
5364 win_skip("WSAPoll is unsupported, some tests will be skipped.\n");
5365 return;
5368 /* Invalid parameters test */
5369 SetLastError(0xdeadbeef);
5370 ret = pWSAPoll(NULL, 0, 0);
5371 err = GetLastError();
5372 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5373 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
5374 SetLastError(0xdeadbeef);
5375 ret = pWSAPoll(NULL, 1, 0);
5376 err = GetLastError();
5377 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5378 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
5379 SetLastError(0xdeadbeef);
5380 ret = pWSAPoll(NULL, 0, 1);
5381 err = GetLastError();
5382 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5383 ok(err == WSAEINVAL, "expected 10022, got %d\n", err);
5384 SetLastError(0xdeadbeef);
5385 ret = pWSAPoll(NULL, 1, 1);
5386 err = GetLastError();
5387 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret);
5388 ok(err == WSAEFAULT, "expected 10014, got %d\n", err);
5390 memset(&address, 0, sizeof(address));
5391 address.sin_addr.s_addr = inet_addr("127.0.0.1");
5392 address.sin_family = AF_INET;
5393 len = sizeof(address);
5394 listener = setup_server_socket(&address, &len);
5396 for (i = 0; i < ARRAY_SIZE(invalid_flags); ++i)
5398 fds[0].fd = listener;
5399 fds[0].events = invalid_flags[i];
5400 fds[0].revents = 0xdead;
5401 WSASetLastError(0xdeadbeef);
5402 ret = pWSAPoll(fds, 1, 0);
5403 todo_wine ok(ret == -1, "got %d\n", ret);
5404 todo_wine ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
5407 /* When no events are pending poll returns 0 with no error */
5408 fds[0].fd = listener;
5409 fds[0].events = POLLRDNORM | POLLRDBAND | POLLWRNORM;
5410 fds[0].revents = 0xdead;
5411 ret = pWSAPoll(fds, 1, 0);
5412 ok(ret == 0, "got %d\n", ret);
5413 ok(!fds[0].revents, "got events %#x\n", fds[0].revents);
5415 fds[0].fd = -1;
5416 fds[0].events = POLLERR;
5417 fds[0].revents = 0xdead;
5418 fds[1].fd = listener;
5419 fds[1].events = POLLIN;
5420 fds[1].revents = 0xdead;
5421 WSASetLastError(0xdeadbeef);
5422 ret = pWSAPoll(fds, 2, 0);
5423 ok(!ret, "got %d\n", ret);
5424 todo_wine ok(!WSAGetLastError(), "got error %u\n", WSAGetLastError());
5425 ok(fds[0].revents == POLLNVAL, "got events %#x\n", fds[0].revents);
5426 ok(!fds[1].revents, "got events %#x\n", fds[1].revents);
5428 fds[0].fd = listener;
5429 fds[0].events = POLLIN;
5430 fds[0].revents = 0xdead;
5431 fds[1].fd = 0xabacab;
5432 fds[1].events = POLLIN;
5433 fds[1].revents = 0xdead;
5434 WSASetLastError(0xdeadbeef);
5435 ret = pWSAPoll(fds, 2, 0);
5436 ok(!ret, "got %d\n", ret);
5437 todo_wine ok(!WSAGetLastError(), "got error %u\n", WSAGetLastError());
5438 ok(!fds[0].revents, "got events %#x\n", fds[0].revents);
5439 ok(fds[1].revents == POLLNVAL, "got events %#x\n", fds[1].revents);
5441 fds[0].fd = listener;
5442 fds[0].events = POLLIN;
5443 fds[0].revents = 0xdead;
5444 fds[1].fd = 0xabacab;
5445 fds[1].events = POLLERR;
5446 fds[1].revents = 0xdead;
5447 WSASetLastError(0xdeadbeef);
5448 ret = pWSAPoll(fds, 2, 0);
5449 todo_wine ok(ret == -1, "got %d\n", ret);
5450 todo_wine ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
5451 ok(!fds[0].revents, "got events %#x\n", fds[0].revents);
5452 todo_wine ok(!fds[1].revents, "got events %#x\n", fds[1].revents);
5454 fds[0].fd = -1;
5455 fds[0].events = POLLERR;
5456 fds[0].revents = 0xdead;
5457 fds[1].fd = 0xabacab;
5458 fds[1].events = POLLERR;
5459 fds[1].revents = 0xdead;
5460 WSASetLastError(0xdeadbeef);
5461 ret = pWSAPoll(fds, 2, 0);
5462 todo_wine ok(ret == -1, "got %d\n", ret);
5463 ok(WSAGetLastError() == WSAENOTSOCK, "got error %u\n", WSAGetLastError());
5464 ok(fds[0].revents == POLLNVAL, "got events %#x\n", fds[0].revents);
5465 ok(fds[1].revents == POLLNVAL, "got events %#x\n", fds[1].revents);
5467 /* Test listening socket connection attempt notifications */
5468 client = setup_connector_socket(&address, len, TRUE);
5470 fds[0].fd = listener;
5471 fds[0].events = POLLIN;
5472 fds[0].revents = 0xdead;
5473 ret = pWSAPoll(fds, 1, 100);
5474 ok(ret == 1, "got %d\n", ret);
5475 ok(fds[0].revents == POLLRDNORM, "got events %#x\n", fds[0].revents);
5477 fds[0].revents = 0xdead;
5478 ret = pWSAPoll(fds, 1, 0);
5479 ok(ret == 1, "got %d\n", ret);
5480 ok(fds[0].revents == POLLRDNORM, "got events %#x\n", fds[0].revents);
5482 fds[0].events = POLLRDBAND | POLLWRNORM;
5483 fds[0].revents = 0xdead;
5484 ret = pWSAPoll(fds, 1, 0);
5485 ok(ret == 0, "got %d\n", ret);
5486 ok(!fds[0].revents, "got events %#x\n", fds[0].revents);
5488 server = accept(listener, NULL, NULL);
5489 ok(server != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
5490 set_blocking(client, FALSE);
5491 set_blocking(server, FALSE);
5493 for (i = 0; i < ARRAY_SIZE(invalid_flags); ++i)
5495 fds[0].fd = server;
5496 fds[0].events = invalid_flags[i];
5497 fds[0].revents = 0xdead;
5498 WSASetLastError(0xdeadbeef);
5499 ret = pWSAPoll(fds, 1, 0);
5500 todo_wine ok(ret == -1, "got %d\n", ret);
5501 todo_wine ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
5504 /* Test flags exposed by connected sockets. */
5506 fds[0].fd = listener;
5507 fds[0].events = POLLRDNORM | POLLRDBAND | POLLWRNORM;
5508 fds[0].revents = 0xdead;
5509 fds[1].fd = server;
5510 fds[1].events = POLLRDNORM | POLLRDBAND | POLLWRNORM;
5511 fds[1].revents = 0xdead;
5512 fds[2].fd = client;
5513 fds[2].events = POLLRDNORM | POLLRDBAND | POLLWRNORM;
5514 fds[2].revents = 0xdead;
5515 ret = pWSAPoll(fds, 3, 0);
5516 ok(ret == 2, "got %d\n", ret);
5517 ok(!fds[0].revents, "got events %#x\n", fds[0].revents);
5518 ok(fds[1].revents == POLLWRNORM, "got events %#x\n", fds[1].revents);
5519 ok(fds[2].revents == POLLWRNORM, "got events %#x\n", fds[2].revents);
5521 /* Test data receiving notifications */
5523 ret = send(server, "1234", 4, 0);
5524 ok(ret == 4, "got %d\n", ret);
5526 check_poll_mask(client, POLLRDNORM | POLLRDBAND, POLLRDNORM);
5527 check_poll(client, POLLRDNORM | POLLWRNORM);
5528 check_poll(server, POLLWRNORM);
5530 ret = sync_recv(client, buffer, sizeof(buffer), 0);
5531 ok(ret == 4, "got %d\n", ret);
5533 check_poll(client, POLLWRNORM);
5534 check_poll(server, POLLWRNORM);
5536 /* Because the kernel asynchronously buffers data, this test is not reliable. */
5538 if (0)
5540 static const int large_buffer_size = 1024 * 1024;
5541 char *large_buffer = malloc(large_buffer_size);
5543 while (send(server, large_buffer, large_buffer_size, 0) == large_buffer_size);
5545 check_poll(client, POLLWRNORM | POLLRDNORM);
5546 check_poll(server, 0);
5548 while (recv(client, large_buffer, large_buffer_size, 0) > 0);
5550 check_poll(client, POLLWRNORM);
5551 check_poll(server, POLLWRNORM);
5553 free(large_buffer);
5556 /* Test OOB data notifications */
5558 ret = send(client, "A", 1, MSG_OOB);
5559 ok(ret == 1, "got %d\n", ret);
5561 check_poll(client, POLLWRNORM);
5562 check_poll_mask(server, POLLRDNORM | POLLRDBAND, POLLRDBAND);
5563 check_poll(server, POLLWRNORM | POLLRDBAND);
5565 buffer[0] = 0xcc;
5566 ret = recv(server, buffer, 1, MSG_OOB);
5567 ok(ret == 1, "got %d\n", ret);
5568 ok(buffer[0] == 'A', "got %#x\n", buffer[0]);
5570 check_poll(client, POLLWRNORM);
5571 check_poll(server, POLLWRNORM);
5573 /* If the socket is OOBINLINED the notification is like normal data */
5575 ret = 1;
5576 ret = setsockopt(server, SOL_SOCKET, SO_OOBINLINE, (char *)&ret, sizeof(ret));
5577 ok(!ret, "got error %u\n", WSAGetLastError());
5578 ret = send(client, "A", 1, MSG_OOB);
5579 ok(ret == 1, "got %d\n", ret);
5581 check_poll(client, POLLWRNORM);
5582 check_poll_mask_todo(server, POLLRDNORM | POLLRDBAND, POLLRDNORM);
5583 check_poll_todo(server, POLLWRNORM | POLLRDNORM);
5585 buffer[0] = 0xcc;
5586 ret = recv(server, buffer, 1, 0);
5587 ok(ret == 1, "got %d\n", ret);
5588 ok(buffer[0] == 'A', "got %#x\n", buffer[0]);
5590 check_poll(client, POLLWRNORM);
5591 check_poll_todo(server, POLLWRNORM);
5593 /* Test shutdown. */
5595 ret = shutdown(client, SD_RECEIVE);
5596 ok(!ret, "got error %u\n", WSAGetLastError());
5598 check_poll(client, POLLWRNORM);
5599 check_poll_todo(server, POLLWRNORM);
5601 ret = shutdown(client, SD_SEND);
5602 ok(!ret, "got error %u\n", WSAGetLastError());
5604 check_poll(client, POLLWRNORM);
5605 check_poll_mask_todo(server, 0, POLLHUP);
5606 check_poll_todo(server, POLLWRNORM | POLLHUP);
5608 closesocket(client);
5609 closesocket(server);
5611 /* Test shutdown via closesocket(). */
5613 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5614 ret = connect(client, (struct sockaddr *)&address, sizeof(address));
5615 ok(!ret, "got error %u\n", WSAGetLastError());
5616 server = accept(listener, NULL, NULL);
5617 ok(server != -1, "got error %u\n", WSAGetLastError());
5619 closesocket(client);
5621 check_poll_mask_todo(server, 0, POLLHUP);
5622 check_poll_todo(server, POLLWRNORM | POLLHUP);
5624 closesocket(server);
5626 /* Test shutdown with data in the pipe. */
5628 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5629 ret = connect(client, (struct sockaddr *)&address, sizeof(address));
5630 ok(!ret, "got error %u\n", WSAGetLastError());
5631 server = accept(listener, NULL, NULL);
5632 ok(server != -1, "got error %u\n", WSAGetLastError());
5634 ret = send(client, "data", 5, 0);
5635 ok(ret == 5, "got %d\n", ret);
5637 check_poll(client, POLLWRNORM);
5638 check_poll_mask(server, POLLRDNORM | POLLRDBAND, POLLRDNORM);
5639 check_poll(server, POLLWRNORM | POLLRDNORM);
5641 ret = shutdown(client, SD_SEND);
5643 check_poll(client, POLLWRNORM);
5644 check_poll_mask_todo(server, 0, POLLHUP);
5645 check_poll_todo(server, POLLWRNORM | POLLRDNORM | POLLHUP);
5647 closesocket(client);
5648 closesocket(server);
5650 /* Test closing a socket while selecting on it. */
5652 tcp_socketpair(&client, &server);
5654 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &client, 0, NULL);
5655 fds[0].fd = client;
5656 fds[0].events = POLLRDNORM | POLLRDBAND;
5657 fds[0].revents = 0xdead;
5658 ret = pWSAPoll(fds, 1, 2000);
5659 ok(ret == 1, "got %d\n", ret);
5660 ok(fds[0].revents == POLLNVAL, "got events %#x\n", fds[0].revents);
5661 ret = WaitForSingleObject(thread_handle, 1000);
5662 ok(!ret, "wait failed\n");
5663 CloseHandle(thread_handle);
5665 closesocket(server);
5667 /* Test a failed connection.
5669 * The following WSAPoll() call times out on versions older than w10pro64,
5670 * but even on w10pro64 it takes over 2 seconds for an error to be reported,
5671 * so make the test interactive-only. */
5672 if (winetest_interactive)
5674 const struct sockaddr_in invalid_addr = {.sin_family = AF_INET, .sin_addr.s_addr = inet_addr("192.0.2.0")};
5676 client = setup_connector_socket(&invalid_addr, sizeof(invalid_addr), TRUE);
5678 fds[0].fd = client;
5679 fds[0].events = POLLRDNORM | POLLRDBAND | POLLWRNORM;
5680 fds[0].revents = 0xdead;
5681 ret = pWSAPoll(fds, 1, 10000);
5682 ok(ret == 1, "got %d\n", ret);
5683 ok(fds[0].revents == POLLERR, "got events %#x\n", fds[0].revents);
5685 len = sizeof(err);
5686 err = 0xdeadbeef;
5687 ret = getsockopt(client, SOL_SOCKET, SO_ERROR, (char *)&err, &len);
5688 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError());
5689 ok(err == WSAECONNREFUSED, "expected 10061, got %d\n", err);
5690 closesocket(client);
5693 closesocket(listener);
5695 /* Test UDP sockets. */
5697 client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
5698 server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
5700 check_poll(client, POLLWRNORM);
5701 check_poll(server, POLLWRNORM);
5703 ret = bind(client, (const struct sockaddr *)&bind_addr, sizeof(bind_addr));
5704 ok(!ret, "got error %u\n", WSAGetLastError());
5705 len = sizeof(address);
5706 ret = getsockname(client, (struct sockaddr *)&address, &len);
5707 ok(!ret, "got error %u\n", WSAGetLastError());
5709 check_poll(client, POLLWRNORM);
5710 check_poll(server, POLLWRNORM);
5712 ret = sendto(server, "data", 5, 0, (struct sockaddr *)&address, sizeof(address));
5713 ok(ret == 5, "got %d\n", ret);
5715 check_poll_mask(client, POLLRDNORM | POLLRDBAND, POLLRDNORM);
5716 check_poll(client, POLLWRNORM | POLLRDNORM);
5717 check_poll(server, POLLWRNORM);
5719 closesocket(client);
5720 closesocket(server);
5723 static void test_ConnectEx(void)
5725 SOCKET listener = INVALID_SOCKET;
5726 SOCKET acceptor = INVALID_SOCKET;
5727 SOCKET connector = INVALID_SOCKET;
5728 struct sockaddr_in address, conaddress;
5729 int addrlen;
5730 OVERLAPPED overlapped, *olp;
5731 LPFN_CONNECTEX pConnectEx;
5732 GUID connectExGuid = WSAID_CONNECTEX;
5733 HANDLE previous_port, io_port;
5734 DWORD bytesReturned;
5735 char buffer[1024];
5736 ULONG_PTR key;
5737 BOOL bret;
5738 DWORD dwret;
5739 int iret;
5741 memset(&overlapped, 0, sizeof(overlapped));
5743 listener = socket(AF_INET, SOCK_STREAM, 0);
5744 ok(listener != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5746 connector = socket(AF_INET, SOCK_STREAM, 0);
5747 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5749 memset(&address, 0, sizeof(address));
5750 address.sin_family = AF_INET;
5751 address.sin_addr.s_addr = inet_addr("127.0.0.1");
5752 iret = bind(listener, (struct sockaddr*)&address, sizeof(address));
5753 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5755 addrlen = sizeof(address);
5756 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen);
5757 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
5759 iret = set_blocking(listener, TRUE);
5760 ok(!iret, "failed to set nonblocking, error %u\n", WSAGetLastError());
5762 bytesReturned = 0xdeadbeef;
5763 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid),
5764 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL);
5765 ok(!iret, "failed to get ConnectEx, error %u\n", WSAGetLastError());
5767 ok(bytesReturned == sizeof(pConnectEx), "expected sizeof(pConnectEx), got %u\n", bytesReturned);
5769 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
5770 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket "
5771 "returned %d + errno %d\n", bret, WSAGetLastError());
5773 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
5774 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket "
5775 "returned %d + errno %d\n", bret, WSAGetLastError());
5777 /* ConnectEx needs a bound socket */
5778 memset(&conaddress, 0, sizeof(conaddress));
5779 conaddress.sin_family = AF_INET;
5780 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
5781 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
5782 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5784 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL);
5785 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped "
5786 "returned %d + errno %d\n", bret, WSAGetLastError());
5788 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
5790 iret = listen(listener, 1);
5791 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
5793 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
5794 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
5795 "returned %d + errno %d\n", bret, WSAGetLastError());
5796 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
5797 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
5799 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
5800 ok(bret, "Connecting failed, error %d\n", GetLastError());
5801 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned);
5803 closesocket(connector);
5804 connector = socket(AF_INET, SOCK_STREAM, 0);
5805 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5806 /* ConnectEx needs a bound socket */
5807 memset(&conaddress, 0, sizeof(conaddress));
5808 conaddress.sin_family = AF_INET;
5809 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
5810 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
5811 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5813 acceptor = accept(listener, NULL, NULL);
5814 ok(acceptor != INVALID_SOCKET, "failed to accept socket, error %u\n", WSAGetLastError());
5816 buffer[0] = '1';
5817 buffer[1] = '2';
5818 buffer[2] = '3';
5819 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped);
5820 memset(buffer, 0, 3);
5821 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: "
5822 "returned %d + errno %d\n", bret, WSAGetLastError());
5823 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
5824 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
5826 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
5827 ok(bret, "Connecting failed, error %d\n", GetLastError());
5828 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned);
5830 acceptor = accept(listener, NULL, NULL);
5831 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError());
5833 bytesReturned = recv(acceptor, buffer, 3, 0);
5834 buffer[4] = 0;
5835 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned);
5836 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3',
5837 "Failed to get the right data, expected '123', got '%s'\n", buffer);
5839 closesocket(connector);
5840 connector = socket(AF_INET, SOCK_STREAM, 0);
5841 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5842 /* ConnectEx needs a bound socket */
5843 memset(&conaddress, 0, sizeof(conaddress));
5844 conaddress.sin_family = AF_INET;
5845 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
5846 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress));
5847 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5849 closesocket(acceptor);
5850 closesocket(listener);
5852 /* Connect with error */
5854 address.sin_port = htons(1);
5856 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
5857 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError());
5859 io_port = CreateIoCompletionPort((HANDLE)connector, previous_port, 125, 0);
5860 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
5862 bret = SetFileCompletionNotificationModes((HANDLE)connector, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS);
5863 ok(bret, "Got unexpected bret %#x, GetLastError() %u.\n", bret, GetLastError());
5865 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped);
5866 ok(bret == FALSE && GetLastError() == ERROR_IO_PENDING, "ConnectEx to bad destination failed: "
5867 "returned %d + errno %d\n", bret, GetLastError());
5868 dwret = WaitForSingleObject(overlapped.hEvent, 15000);
5869 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError());
5871 bytesReturned = 0xdeadbeef;
5872 bret = GetQueuedCompletionStatus( io_port, &bytesReturned, &key, &olp, 200 );
5873 ok(!bret && GetLastError() == ERROR_CONNECTION_REFUSED, "Got unexpected bret %#x, GetLastError() %u.\n",
5874 bret, GetLastError());
5875 ok(key == 125, "Key is %lu\n", key);
5876 ok(!bytesReturned, "Number of bytes transferred is %u\n", bytesReturned);
5877 ok(olp == &overlapped, "Overlapped structure is at %p\n", olp);
5879 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE);
5880 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED,
5881 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError());
5883 CloseHandle(io_port);
5885 WSACloseEvent(overlapped.hEvent);
5886 closesocket(connector);
5888 CloseHandle(previous_port);
5891 static void test_AcceptEx(void)
5893 SOCKET listener, acceptor, acceptor2, connector, connector2;
5894 struct sockaddr_in bindAddress, peerAddress, *readBindAddress, *readRemoteAddress;
5895 int socklen, optlen;
5896 GUID acceptExGuid = WSAID_ACCEPTEX, getAcceptExGuid = WSAID_GETACCEPTEXSOCKADDRS;
5897 LPFN_ACCEPTEX pAcceptEx = NULL;
5898 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs = NULL;
5899 fd_set fds_accept, fds_send;
5900 static const struct timeval timeout = {1, 0};
5901 DWORD bytesReturned, connect_time;
5902 char buffer[1024], ipbuffer[32];
5903 OVERLAPPED overlapped;
5904 int iret, localSize = sizeof(struct sockaddr_in), remoteSize = localSize;
5905 BOOL bret;
5906 DWORD dwret;
5908 memset(&overlapped, 0, sizeof(overlapped));
5909 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
5911 listener = socket(AF_INET, SOCK_STREAM, 0);
5912 ok(listener != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5914 acceptor = socket(AF_INET, SOCK_STREAM, 0);
5915 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
5917 memset(&bindAddress, 0, sizeof(bindAddress));
5918 bindAddress.sin_family = AF_INET;
5919 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
5920 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
5921 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
5923 socklen = sizeof(bindAddress);
5924 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
5925 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
5927 iret = set_blocking(listener, FALSE);
5928 ok(!iret, "Failed to set nonblocking, error %u\n", WSAGetLastError());
5930 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
5931 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
5932 ok(!iret, "Failed to get AcceptEx, error %u\n", WSAGetLastError());
5934 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &getAcceptExGuid, sizeof(getAcceptExGuid),
5935 &pGetAcceptExSockaddrs, sizeof(pGetAcceptExSockaddrs), &bytesReturned, NULL, NULL);
5936 ok(!iret, "Failed to get GetAcceptExSockaddrs, error %u\n", WSAGetLastError());
5938 overlapped.Internal = 0xdeadbeef;
5939 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
5940 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5941 &bytesReturned, &overlapped);
5942 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
5943 "returned %d + errno %d\n", bret, WSAGetLastError());
5944 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
5946 overlapped.Internal = 0xdeadbeef;
5947 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
5948 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5949 &bytesReturned, &overlapped);
5950 todo_wine
5951 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
5952 "returned %d + errno %d\n", bret, WSAGetLastError());
5953 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
5954 if (!bret && WSAGetLastError() == ERROR_IO_PENDING)
5955 CancelIo((HANDLE)listener);
5957 iret = listen(listener, 5);
5958 ok(!iret, "failed to listen, error %u\n", GetLastError());
5960 overlapped.Internal = 0xdeadbeef;
5961 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
5962 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5963 &bytesReturned, &overlapped);
5964 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid accepting socket "
5965 "returned %d + errno %d\n", bret, WSAGetLastError());
5966 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
5968 overlapped.Internal = 0xdeadbeef;
5969 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
5970 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
5971 &bytesReturned, &overlapped);
5972 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
5973 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
5974 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
5976 overlapped.Internal = 0xdeadbeef;
5977 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
5978 &bytesReturned, &overlapped);
5979 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
5980 "AcceptEx on too small local address size returned %d + errno %d\n",
5981 bret, WSAGetLastError());
5982 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
5984 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
5985 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
5986 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
5987 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
5988 iret = getsockname(connector, (struct sockaddr *)&peerAddress, &remoteSize);
5989 ok(!iret, "getsockname failed, error %u\n", WSAGetLastError());
5991 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
5992 ok(!dwret, "wait failed\n");
5993 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
5994 ok(bret, "got error %u\n", GetLastError());
5995 ok(!(NTSTATUS)overlapped.Internal, "got %#lx\n", overlapped.Internal);
5996 ok(!bytesReturned, "got size %u\n", bytesReturned);
5998 readBindAddress = readRemoteAddress = (struct sockaddr_in *)0xdeadbeef;
5999 localSize = remoteSize = 0xdeadbeef;
6000 pGetAcceptExSockaddrs(buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
6001 (struct sockaddr **)&readBindAddress, &localSize, (struct sockaddr **)&readRemoteAddress, &remoteSize);
6002 todo_wine ok(readBindAddress == (struct sockaddr_in *)0xdeadbeef, "got local addr %p\n", readBindAddress);
6003 ok(!memcmp(readRemoteAddress, &peerAddress, sizeof(peerAddress)), "remote addr didn't match\n");
6004 todo_wine ok(localSize == 0xdeadbeef, "got local size %u\n", localSize);
6005 ok(remoteSize == sizeof(struct sockaddr_in), "got remote size %u\n", remoteSize);
6007 closesocket(connector);
6008 closesocket(acceptor);
6010 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6011 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
6013 overlapped.Internal = 0xdeadbeef;
6014 bret = pAcceptEx(listener, acceptor, buffer, 0, 3,
6015 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
6016 ok(!bret && WSAGetLastError() == ERROR_IO_PENDING, "got %d, error %u\n", bret, WSAGetLastError());
6017 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "got %#lx\n", overlapped.Internal);
6019 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6020 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
6021 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
6022 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
6024 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6025 ok(!dwret, "wait failed\n");
6026 bytesReturned = 0xdeadbeef;
6027 SetLastError(0xdeadbeef);
6028 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6029 ok(!bret, "expected failure\n");
6030 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got error %u\n", GetLastError());
6031 ok((NTSTATUS)overlapped.Internal == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", overlapped.Internal);
6032 ok(!bytesReturned, "got size %u\n", bytesReturned);
6034 closesocket(acceptor);
6036 /* The above connection request is not accepted. */
6037 acceptor = accept(listener, NULL, NULL);
6038 todo_wine ok(acceptor != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
6039 closesocket(acceptor);
6041 closesocket(connector);
6043 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6044 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
6046 overlapped.Internal = 0xdeadbeef;
6047 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 4,
6048 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
6049 ok(!bret && WSAGetLastError() == ERROR_IO_PENDING, "got %d, error %u\n", bret, WSAGetLastError());
6050 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "got %#lx\n", overlapped.Internal);
6052 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6053 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
6054 iret = connect(connector, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
6055 ok(!iret, "failed to connect, error %u\n", WSAGetLastError());
6057 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6058 ok(!dwret, "wait failed\n");
6059 bytesReturned = 0xdeadbeef;
6060 SetLastError(0xdeadbeef);
6061 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6062 todo_wine ok(!bret, "expected failure\n");
6063 todo_wine ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got error %u\n", GetLastError());
6064 todo_wine ok((NTSTATUS)overlapped.Internal == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", overlapped.Internal);
6065 ok(!bytesReturned, "got size %u\n", bytesReturned);
6067 closesocket(acceptor);
6069 /* The above connection request is not accepted. */
6070 acceptor = accept(listener, NULL, NULL);
6071 todo_wine ok(acceptor != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
6072 closesocket(acceptor);
6074 closesocket(connector);
6076 acceptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6077 ok(acceptor != -1, "failed to create socket, error %u\n", WSAGetLastError());
6079 overlapped.Internal = 0xdeadbeef;
6080 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 15,
6081 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
6082 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx on too small local address "
6083 "size returned %d + errno %d\n",
6084 bret, WSAGetLastError());
6085 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6086 bret = CancelIo((HANDLE) listener);
6087 ok(bret, "Failed to cancel pending accept socket\n");
6089 overlapped.Internal = 0xdeadbeef;
6090 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
6091 &bytesReturned, &overlapped);
6092 ok(bret == FALSE && WSAGetLastError() == WSAEFAULT,
6093 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
6094 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6096 overlapped.Internal = 0xdeadbeef;
6097 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16,
6098 sizeof(struct sockaddr_in) + 15, &bytesReturned, &overlapped);
6099 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING,
6100 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError());
6101 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6102 bret = CancelIo((HANDLE) listener);
6103 ok(bret, "Failed to cancel pending accept socket\n");
6105 bret = pAcceptEx(listener, acceptor, buffer, 0,
6106 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6107 &bytesReturned, NULL);
6108 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
6109 "returned %d + errno %d\n", bret, WSAGetLastError());
6111 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, 0, &bytesReturned, NULL);
6112 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
6113 "returned %d + errno %d\n", bret, WSAGetLastError());
6115 overlapped.Internal = 0xdeadbeef;
6116 bret = pAcceptEx(listener, acceptor, buffer, 0,
6117 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6118 &bytesReturned, &overlapped);
6119 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6120 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6122 overlapped.Internal = 0xdeadbeef;
6123 bret = pAcceptEx(listener, acceptor, buffer, 0,
6124 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6125 &bytesReturned, &overlapped);
6126 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL,
6127 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
6128 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6130 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6131 todo_wine ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
6132 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
6133 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
6134 /* We need to cancel this call, otherwise things fail */
6135 closesocket(acceptor);
6136 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6137 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6139 bret = CancelIo((HANDLE) listener);
6140 ok(bret, "Failed to cancel failed test. Bailing...\n");
6141 if (!bret) return;
6143 overlapped.Internal = 0xdeadbeef;
6144 bret = pAcceptEx(listener, acceptor, buffer, 0,
6145 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6146 &bytesReturned, &overlapped);
6147 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6148 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6151 connector = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
6152 ok(connector != -1, "failed to create socket, error %u\n", WSAGetLastError());
6153 overlapped.Internal = 0xdeadbeef;
6154 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6155 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
6157 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
6158 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6159 ok(overlapped.Internal == STATUS_SUCCESS, "got %08x\n", (ULONG)overlapped.Internal);
6161 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6162 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
6163 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
6165 closesocket(connector);
6166 connector = INVALID_SOCKET;
6167 closesocket(acceptor);
6169 /* Test short reads */
6171 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6172 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6173 connector = socket(AF_INET, SOCK_STREAM, 0);
6174 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6175 overlapped.Internal = 0xdeadbeef;
6176 bret = pAcceptEx(listener, acceptor, buffer, 2,
6177 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6178 &bytesReturned, &overlapped);
6179 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6180 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6182 connect_time = 0xdeadbeef;
6183 optlen = sizeof(connect_time);
6184 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
6185 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
6186 ok(connect_time == ~0u, "unexpected connect time %u\n", connect_time);
6188 /* AcceptEx() still won't complete until we send data */
6189 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6190 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
6192 connect_time = 0xdeadbeef;
6193 optlen = sizeof(connect_time);
6194 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen);
6195 ok(!iret, "getsockopt failed %d\n", WSAGetLastError());
6196 ok(connect_time < 0xdeadbeef, "unexpected connect time %u\n", connect_time);
6198 dwret = WaitForSingleObject(overlapped.hEvent, 0);
6199 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
6200 ok(overlapped.Internal == STATUS_PENDING, "got %08x\n", (ULONG)overlapped.Internal);
6202 iret = getsockname( connector, (struct sockaddr *)&peerAddress, &remoteSize);
6203 ok( !iret, "getsockname failed.\n");
6205 /* AcceptEx() could complete any time now */
6206 iret = send(connector, buffer, 1, 0);
6207 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
6209 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6210 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6211 ok(overlapped.Internal == STATUS_SUCCESS, "got %08x\n", (ULONG)overlapped.Internal);
6213 /* Check if the buffer from AcceptEx is decoded correctly */
6214 pGetAcceptExSockaddrs(buffer, 2, sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6215 (struct sockaddr **)&readBindAddress, &localSize,
6216 (struct sockaddr **)&readRemoteAddress, &remoteSize);
6217 strcpy( ipbuffer, inet_ntoa(readBindAddress->sin_addr));
6218 ok( readBindAddress->sin_addr.s_addr == bindAddress.sin_addr.s_addr,
6219 "Local socket address is different %s != %s\n",
6220 ipbuffer, inet_ntoa(bindAddress.sin_addr));
6221 ok( readBindAddress->sin_port == bindAddress.sin_port,
6222 "Local socket port is different: %d != %d\n",
6223 readBindAddress->sin_port, bindAddress.sin_port);
6224 strcpy( ipbuffer, inet_ntoa(readRemoteAddress->sin_addr));
6225 ok( readRemoteAddress->sin_addr.s_addr == peerAddress.sin_addr.s_addr,
6226 "Remote socket address is different %s != %s\n",
6227 ipbuffer, inet_ntoa(peerAddress.sin_addr));
6228 ok( readRemoteAddress->sin_port == peerAddress.sin_port,
6229 "Remote socket port is different: %d != %d\n",
6230 readRemoteAddress->sin_port, peerAddress.sin_port);
6232 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6233 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
6234 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
6236 closesocket(connector);
6237 connector = INVALID_SOCKET;
6238 closesocket(acceptor);
6240 /* Test CF_DEFER & AcceptEx interaction */
6242 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6243 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6244 connector = socket(AF_INET, SOCK_STREAM, 0);
6245 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6246 connector2 = socket(AF_INET, SOCK_STREAM, 0);
6247 ok(connector2 != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6249 iret = set_blocking(connector, FALSE);
6250 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
6251 iret = set_blocking(connector2, FALSE);
6252 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
6254 /* Connect socket #1 */
6255 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6256 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
6258 buffer[0] = '0';
6260 FD_ZERO(&fds_accept);
6261 FD_SET(listener, &fds_accept);
6262 iret = select(0, &fds_accept, NULL, NULL, &timeout);
6263 ok(iret == 1, "wait timed out\n");
6265 acceptor2 = WSAAccept(listener, NULL, NULL, AlwaysDeferConditionFunc, 0);
6266 ok(acceptor2 == INVALID_SOCKET, "expected failure\n");
6267 ok(WSAGetLastError() == WSATRY_AGAIN, "got error %u\n", WSAGetLastError());
6268 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16,
6269 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped);
6270 ok(!bret, "expected failure\n");
6271 ok(WSAGetLastError() == ERROR_IO_PENDING, "got error %u\n", WSAGetLastError());
6273 FD_ZERO(&fds_send);
6274 FD_SET(connector, &fds_send);
6275 iret = select(0, NULL, &fds_send, NULL, &timeout);
6276 ok(iret == 1, "wait timed out\n");
6278 iret = send(connector, "1", 1, 0);
6279 ok(iret == 1, "got ret %d, error %u\n", iret, WSAGetLastError());
6281 iret = connect(connector2, (struct sockaddr *)&bindAddress, sizeof(bindAddress));
6282 ok(iret == SOCKET_ERROR, "expected failure\n");
6283 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
6285 iret = select(0, &fds_accept, NULL, NULL, &timeout);
6286 ok(iret == 1, "wait timed out\n");
6288 acceptor2 = accept(listener, NULL, NULL);
6289 ok(acceptor2 != INVALID_SOCKET, "failed to accept, error %u\n", WSAGetLastError());
6290 closesocket(acceptor2);
6292 FD_ZERO(&fds_send);
6293 FD_SET(connector2, &fds_send);
6294 iret = select(0, NULL, &fds_send, NULL, &timeout);
6295 ok(iret == 1, "wait timed out\n");
6297 iret = send(connector2, "2", 1, 0);
6298 ok(iret == 1, "got ret %d, error %u\n", iret, WSAGetLastError());
6300 dwret = WaitForSingleObject(overlapped.hEvent, 0);
6301 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6303 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6304 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
6305 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
6307 set_blocking(acceptor, TRUE);
6308 iret = recv( acceptor, buffer, 2, 0);
6309 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
6310 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
6312 closesocket(connector);
6313 closesocket(connector2);
6314 closesocket(acceptor);
6316 /* clean up in case of failures */
6317 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
6318 closesocket(acceptor);
6320 /* Disconnect during receive? */
6322 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6323 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6324 connector = socket(AF_INET, SOCK_STREAM, 0);
6325 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6326 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
6327 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6328 &bytesReturned, &overlapped);
6329 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6331 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6332 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
6334 closesocket(connector);
6335 connector = INVALID_SOCKET;
6337 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6338 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6340 bytesReturned = 123456;
6341 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6342 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
6343 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
6345 closesocket(acceptor);
6347 /* Test closing with pending requests */
6349 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6350 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6351 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
6352 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6353 &bytesReturned, &overlapped);
6354 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6356 closesocket(acceptor);
6358 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6359 ok(dwret == WAIT_OBJECT_0,
6360 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6361 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6362 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
6364 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6365 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6366 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
6367 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6368 &bytesReturned, &overlapped);
6369 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6371 CancelIo((HANDLE) acceptor);
6373 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6374 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
6376 closesocket(acceptor);
6378 acceptor = socket(AF_INET, SOCK_STREAM, 0);
6379 ok(acceptor != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6380 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
6381 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
6382 &bytesReturned, &overlapped);
6383 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
6385 closesocket(listener);
6387 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
6388 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
6390 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
6391 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
6393 WSACloseEvent(overlapped.hEvent);
6394 closesocket(acceptor);
6395 closesocket(connector2);
6398 static void test_DisconnectEx(void)
6400 SOCKET listener, acceptor, connector;
6401 LPFN_DISCONNECTEX pDisconnectEx;
6402 GUID disconnectExGuid = WSAID_DISCONNECTEX;
6403 struct sockaddr_in address;
6404 DWORD num_bytes, flags;
6405 OVERLAPPED overlapped;
6406 int addrlen, iret;
6407 BOOL bret;
6409 connector = socket(AF_INET, SOCK_STREAM, 0);
6410 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
6412 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectExGuid, sizeof(disconnectExGuid),
6413 &pDisconnectEx, sizeof(pDisconnectEx), &num_bytes, NULL, NULL);
6414 if (iret)
6416 win_skip("WSAIoctl failed to get DisconnectEx, error %d\n", WSAGetLastError());
6417 closesocket(connector);
6418 return;
6421 listener = socket(AF_INET, SOCK_STREAM, 0);
6422 ok(listener != INVALID_SOCKET, "failed to create listener socket, error %d\n", WSAGetLastError());
6424 memset(&address, 0, sizeof(address));
6425 address.sin_family = AF_INET;
6426 address.sin_addr.s_addr = inet_addr("127.0.0.1");
6427 iret = bind(listener, (struct sockaddr *)&address, sizeof(address));
6428 ok(iret == 0, "failed to bind, error %d\n", WSAGetLastError());
6430 addrlen = sizeof(address);
6431 iret = getsockname(listener, (struct sockaddr *)&address, &addrlen);
6432 ok(iret == 0, "failed to lookup bind address, error %d\n", WSAGetLastError());
6434 iret = listen(listener, 1);
6435 ok(iret == 0, "failed to listen, error %d\n", WSAGetLastError());
6437 set_blocking(listener, TRUE);
6439 memset(&overlapped, 0, sizeof(overlapped));
6440 bret = pDisconnectEx(INVALID_SOCKET, &overlapped, 0, 0);
6441 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
6442 ok(WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %d\n", WSAGetLastError());
6444 memset(&overlapped, 0, sizeof(overlapped));
6445 bret = pDisconnectEx(connector, &overlapped, 0, 0);
6446 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n");
6447 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "expected WSAENOTCONN, got %d\n", WSAGetLastError());
6449 iret = connect(connector, (struct sockaddr *)&address, addrlen);
6450 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
6452 acceptor = accept(listener, NULL, NULL);
6453 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
6455 memset(&overlapped, 0, sizeof(overlapped));
6456 overlapped.hEvent = WSACreateEvent();
6457 ok(overlapped.hEvent != WSA_INVALID_EVENT, "WSACreateEvent failed, error %d\n", WSAGetLastError());
6458 bret = pDisconnectEx(connector, &overlapped, 0, 0);
6459 if (bret)
6460 ok(overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", overlapped.Internal);
6461 else if (WSAGetLastError() == ERROR_IO_PENDING)
6462 bret = WSAGetOverlappedResult(connector, &overlapped, &num_bytes, TRUE, &flags);
6463 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
6464 WSACloseEvent(overlapped.hEvent);
6466 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
6467 ok(iret != 0, "connect unexpectedly succeeded\n");
6468 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
6470 closesocket(acceptor);
6471 closesocket(connector);
6473 connector = socket(AF_INET, SOCK_STREAM, 0);
6474 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError());
6476 iret = connect(connector, (struct sockaddr *)&address, addrlen);
6477 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError());
6479 acceptor = accept(listener, NULL, NULL);
6480 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError());
6482 bret = pDisconnectEx(connector, NULL, 0, 0);
6483 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError());
6485 iret = connect(connector, (struct sockaddr *)&address, sizeof(address));
6486 ok(iret != 0, "connect unexpectedly succeeded\n");
6487 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError());
6489 closesocket(acceptor);
6490 closesocket(connector);
6491 closesocket(listener);
6494 #define compare_file(h,s,o) compare_file2(h,s,o,__FILE__,__LINE__)
6496 static void compare_file2(HANDLE handle, SOCKET sock, int offset, const char *file, int line)
6498 char buf1[256], buf2[256];
6499 BOOL success;
6500 int i = 0;
6502 SetFilePointer(handle, offset, NULL, FILE_BEGIN);
6503 while (1)
6505 DWORD n1 = 0, n2 = 0;
6507 success = ReadFile(handle, buf1, sizeof(buf1), &n1, NULL);
6508 ok_(file,line)(success, "Failed to read from file.\n");
6509 if (success && n1 == 0)
6510 break;
6511 else if(!success)
6512 return;
6513 n2 = recv(sock, buf2, n1, 0);
6514 ok_(file,line)(n1 == n2, "Block %d size mismatch (%d != %d)\n", i, n1, n2);
6515 ok_(file,line)(memcmp(buf1, buf2, n2) == 0, "Block %d failed\n", i);
6516 i++;
6520 static void test_TransmitFile(void)
6522 DWORD num_bytes, err, file_size, total_sent;
6523 GUID transmitFileGuid = WSAID_TRANSMITFILE;
6524 LPFN_TRANSMITFILE pTransmitFile = NULL;
6525 HANDLE file = INVALID_HANDLE_VALUE;
6526 char header_msg[] = "hello world";
6527 char footer_msg[] = "goodbye!!!";
6528 char system_ini_path[MAX_PATH];
6529 struct sockaddr_in bindAddress;
6530 TRANSMIT_FILE_BUFFERS buffers;
6531 SOCKET client, server, dest;
6532 WSAOVERLAPPED ov;
6533 char buf[256];
6534 int iret, len;
6535 BOOL bret;
6537 memset( &ov, 0, sizeof(ov) );
6539 /* Setup sockets for testing TransmitFile */
6540 client = socket(AF_INET, SOCK_STREAM, 0);
6541 ok(client != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6542 server = socket(AF_INET, SOCK_STREAM, 0);
6543 ok(server != INVALID_SOCKET, "failed to create socket, error %u\n", GetLastError());
6544 iret = WSAIoctl(client, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmitFileGuid, sizeof(transmitFileGuid),
6545 &pTransmitFile, sizeof(pTransmitFile), &num_bytes, NULL, NULL);
6546 ok(!iret, "failed to get TransmitFile, error %u\n", GetLastError());
6547 GetSystemWindowsDirectoryA(system_ini_path, MAX_PATH );
6548 strcat(system_ini_path, "\\system.ini");
6549 file = CreateFileA(system_ini_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0x0, NULL);
6550 ok(file != INVALID_HANDLE_VALUE, "failed to open file, error %u\n", GetLastError());
6551 file_size = GetFileSize(file, NULL);
6553 /* Test TransmitFile with an invalid socket */
6554 bret = pTransmitFile(INVALID_SOCKET, file, 0, 0, NULL, NULL, 0);
6555 err = WSAGetLastError();
6556 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6557 ok(err == WSAENOTSOCK, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTSOCK);
6559 /* Test a bogus TransmitFile without a connected socket */
6560 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, TF_REUSE_SOCKET);
6561 err = WSAGetLastError();
6562 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6563 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
6565 /* Setup a properly connected socket for transfers */
6566 memset(&bindAddress, 0, sizeof(bindAddress));
6567 bindAddress.sin_family = AF_INET;
6568 bindAddress.sin_port = htons(SERVERPORT+1);
6569 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
6570 iret = bind(server, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6571 ok(!iret, "failed to bind socket, error %u\n", GetLastError());
6572 iret = listen(server, 1);
6573 ok(!iret, "failed to listen, error %u\n", GetLastError());
6574 iret = connect(client, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6575 ok(!iret, "failed to connect, error %u\n", GetLastError());
6576 len = sizeof(bindAddress);
6577 dest = accept(server, (struct sockaddr*)&bindAddress, &len);
6578 ok(dest != INVALID_SOCKET, "failed to accept, error %u\n", GetLastError());
6579 iret = set_blocking(dest, FALSE);
6580 ok(!iret, "failed to set nonblocking, error %u\n", GetLastError());
6582 /* Test TransmitFile with no possible buffer */
6583 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
6584 ok(bret, "TransmitFile failed unexpectedly.\n");
6585 iret = recv(dest, buf, sizeof(buf), 0);
6586 ok(iret == -1, "Returned an unexpected buffer from TransmitFile (%d != -1).\n", iret);
6588 /* Test TransmitFile with only buffer data */
6589 buffers.Head = &header_msg[0];
6590 buffers.HeadLength = sizeof(header_msg);
6591 buffers.Tail = &footer_msg[0];
6592 buffers.TailLength = sizeof(footer_msg);
6593 bret = pTransmitFile(client, NULL, 0, 0, NULL, &buffers, 0);
6594 ok(bret, "TransmitFile failed unexpectedly.\n");
6595 iret = recv(dest, buf, sizeof(buf), 0);
6596 ok(iret == sizeof(header_msg)+sizeof(footer_msg),
6597 "Returned an unexpected buffer from TransmitFile: %d\n", iret );
6598 ok(memcmp(&buf[0], &header_msg[0], sizeof(header_msg)) == 0,
6599 "TransmitFile header buffer did not match!\n");
6600 ok(memcmp(&buf[sizeof(header_msg)], &footer_msg[0], sizeof(footer_msg)) == 0,
6601 "TransmitFile footer buffer did not match!\n");
6603 /* Test TransmitFile with only file data */
6604 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, 0);
6605 ok(bret, "TransmitFile failed unexpectedly.\n");
6606 compare_file(file, dest, 0);
6608 /* Test TransmitFile with both file and buffer data */
6609 buffers.Head = &header_msg[0];
6610 buffers.HeadLength = sizeof(header_msg);
6611 buffers.Tail = &footer_msg[0];
6612 buffers.TailLength = sizeof(footer_msg);
6613 SetFilePointer(file, 0, NULL, FILE_BEGIN);
6614 bret = pTransmitFile(client, file, 0, 0, NULL, &buffers, 0);
6615 ok(bret, "TransmitFile failed unexpectedly.\n");
6616 iret = recv(dest, buf, sizeof(header_msg), 0);
6617 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
6618 "TransmitFile header buffer did not match!\n");
6619 compare_file(file, dest, 0);
6620 iret = recv(dest, buf, sizeof(footer_msg), 0);
6621 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
6622 "TransmitFile footer buffer did not match!\n");
6624 /* Test overlapped TransmitFile */
6625 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
6626 SetFilePointer(file, 0, NULL, FILE_BEGIN);
6627 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
6628 err = WSAGetLastError();
6629 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6630 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n",
6631 err, ERROR_IO_PENDING);
6632 iret = WaitForSingleObject(ov.hEvent, 2000);
6633 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
6634 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
6635 ok(total_sent == file_size,
6636 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
6637 total_sent, file_size);
6638 compare_file(file, dest, 0);
6640 /* Test overlapped TransmitFile w/ start offset */
6641 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
6642 SetFilePointer(file, 0, NULL, FILE_BEGIN);
6643 ov.Offset = 10;
6644 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0);
6645 err = WSAGetLastError();
6646 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6647 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
6648 iret = WaitForSingleObject(ov.hEvent, 2000);
6649 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
6650 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
6651 ok(total_sent == (file_size - ov.Offset),
6652 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
6653 total_sent, file_size - ov.Offset);
6654 compare_file(file, dest, ov.Offset);
6656 /* Test overlapped TransmitFile w/ file and buffer data */
6657 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
6658 buffers.Head = &header_msg[0];
6659 buffers.HeadLength = sizeof(header_msg);
6660 buffers.Tail = &footer_msg[0];
6661 buffers.TailLength = sizeof(footer_msg);
6662 SetFilePointer(file, 0, NULL, FILE_BEGIN);
6663 ov.Offset = 0;
6664 bret = pTransmitFile(client, file, 0, 0, &ov, &buffers, 0);
6665 err = WSAGetLastError();
6666 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6667 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING);
6668 iret = WaitForSingleObject(ov.hEvent, 2000);
6669 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n");
6670 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL);
6671 ok(total_sent == (file_size + buffers.HeadLength + buffers.TailLength),
6672 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n",
6673 total_sent, file_size + buffers.HeadLength + buffers.TailLength);
6674 iret = recv(dest, buf, sizeof(header_msg), 0);
6675 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0,
6676 "TransmitFile header buffer did not match!\n");
6677 compare_file(file, dest, 0);
6678 iret = recv(dest, buf, sizeof(footer_msg), 0);
6679 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0,
6680 "TransmitFile footer buffer did not match!\n");
6682 /* Test TransmitFile with a UDP datagram socket */
6683 closesocket(client);
6684 client = socket(AF_INET, SOCK_DGRAM, 0);
6685 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0);
6686 err = WSAGetLastError();
6687 ok(!bret, "TransmitFile succeeded unexpectedly.\n");
6688 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN);
6690 CloseHandle(file);
6691 CloseHandle(ov.hEvent);
6692 closesocket(client);
6693 closesocket(server);
6696 static void test_getpeername(void)
6698 SOCKET sock;
6699 struct sockaddr_in sa, sa_out;
6700 SOCKADDR_STORAGE ss;
6701 int sa_len;
6702 const char buf[] = "hello world";
6703 int ret;
6705 /* Test the parameter validation order. */
6706 ret = getpeername(INVALID_SOCKET, NULL, NULL);
6707 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
6708 ok(WSAGetLastError() == WSAENOTSOCK,
6709 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
6711 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
6712 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
6714 ret = getpeername(sock, NULL, NULL);
6715 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
6716 ok(WSAGetLastError() == WSAENOTCONN,
6717 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
6719 memset(&sa, 0, sizeof(sa));
6720 sa.sin_family = AF_INET;
6721 sa.sin_port = htons(139);
6722 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
6724 /* sendto does not change a socket's connection state. */
6725 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
6726 ok(ret != SOCKET_ERROR,
6727 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
6729 ret = getpeername(sock, NULL, NULL);
6730 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
6731 ok(WSAGetLastError() == WSAENOTCONN,
6732 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
6734 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
6735 ok(ret == 0,
6736 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
6738 ret = getpeername(sock, NULL, NULL);
6739 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
6740 ok(WSAGetLastError() == WSAEFAULT,
6741 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
6743 /* Test crashes on Wine. */
6744 if (0)
6746 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
6747 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
6748 ok(WSAGetLastError() == WSAEFAULT,
6749 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
6752 ret = getpeername(sock, (struct sockaddr*)&sa_out, NULL);
6753 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
6754 ok(WSAGetLastError() == WSAEFAULT,
6755 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
6757 sa_len = 0;
6758 ret = getpeername(sock, NULL, &sa_len);
6759 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
6760 ok(WSAGetLastError() == WSAEFAULT,
6761 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
6762 ok(!sa_len, "got %d\n", sa_len);
6764 sa_len = 0;
6765 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
6766 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
6767 ok(WSAGetLastError() == WSAEFAULT,
6768 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
6769 ok(!sa_len, "got %d\n", sa_len);
6771 sa_len = sizeof(ss);
6772 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len);
6773 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
6774 ok(!memcmp(&sa, &ss, sizeof(sa)),
6775 "Expected the returned structure to be identical to the connect structure\n");
6776 ok(sa_len == sizeof(sa), "got %d\n", sa_len);
6778 closesocket(sock);
6781 static void test_sioRoutingInterfaceQuery(void)
6783 int ret;
6784 SOCKET sock;
6785 SOCKADDR_IN sin = { 0 }, sout = { 0 };
6786 DWORD bytesReturned;
6788 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
6789 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
6790 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL,
6791 NULL, NULL);
6792 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
6793 "expected WSAEFAULT, got %d\n", WSAGetLastError());
6794 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
6795 NULL, 0, NULL, NULL, NULL);
6796 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
6797 "expected WSAEFAULT, got %d\n", WSAGetLastError());
6798 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
6799 NULL, 0, &bytesReturned, NULL, NULL);
6800 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEAFNOSUPPORT,
6801 "expected WSAEAFNOSUPPORT, got %d\n", WSAGetLastError());
6802 sin.sin_family = AF_INET;
6803 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
6804 NULL, 0, &bytesReturned, NULL, NULL);
6805 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL,
6806 "expected WSAEINVAL, got %d\n", WSAGetLastError());
6807 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
6808 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
6809 NULL, 0, &bytesReturned, NULL, NULL);
6810 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT,
6811 "expected WSAEFAULT, got %d\n", WSAGetLastError());
6812 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin),
6813 &sout, sizeof(sout), &bytesReturned, NULL, NULL);
6814 ok(!ret, "WSAIoctl failed: %d\n", WSAGetLastError());
6815 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n", sout.sin_family);
6816 /* We expect the source address to be INADDR_LOOPBACK as well, but
6817 * there's no guarantee that a route to the loopback address exists,
6818 * so rather than introduce spurious test failures we do not test the
6819 * source address.
6821 closesocket(sock);
6824 static void test_sioAddressListChange(void)
6826 struct sockaddr_in bindAddress;
6827 struct in_addr net_address;
6828 WSAOVERLAPPED overlapped, *olp;
6829 struct hostent *h;
6830 DWORD num_bytes, error, tick;
6831 SOCKET sock, sock2, sock3;
6832 WSAEVENT event2, event3;
6833 HANDLE io_port;
6834 ULONG_PTR key;
6835 int acount;
6836 BOOL bret;
6837 int ret;
6839 /* Use gethostbyname to find the list of local network interfaces */
6840 h = gethostbyname("");
6841 ok(!!h, "failed to get interface list, error %u\n", WSAGetLastError());
6842 for (acount = 0; h->h_addr_list[acount]; acount++);
6843 if (acount == 0)
6845 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, test requires a network card.\n");
6846 return;
6849 net_address.s_addr = *(ULONG *) h->h_addr_list[0];
6851 sock = socket(AF_INET, 0, IPPROTO_TCP);
6852 ok(sock != INVALID_SOCKET, "socket() failed\n");
6854 memset(&bindAddress, 0, sizeof(bindAddress));
6855 bindAddress.sin_family = AF_INET;
6856 bindAddress.sin_addr.s_addr = net_address.s_addr;
6857 SetLastError(0xdeadbeef);
6858 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6859 ok (!ret, "bind() failed with error %d\n", GetLastError());
6860 set_blocking(sock, FALSE);
6862 memset(&overlapped, 0, sizeof(overlapped));
6863 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6864 SetLastError(0xdeadbeef);
6865 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
6866 error = GetLastError();
6867 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
6868 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
6870 CloseHandle(overlapped.hEvent);
6871 closesocket(sock);
6873 sock = socket(AF_INET, 0, IPPROTO_TCP);
6874 ok(sock != INVALID_SOCKET, "socket() failed\n");
6876 SetLastError(0xdeadbeef);
6877 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6878 ok (!ret, "bind() failed with error %d\n", GetLastError());
6879 set_blocking(sock, TRUE);
6881 memset(&overlapped, 0, sizeof(overlapped));
6882 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6883 SetLastError(0xdeadbeef);
6884 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
6885 error = GetLastError();
6886 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
6887 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
6889 CloseHandle(overlapped.hEvent);
6890 closesocket(sock);
6892 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6893 ok(sock != INVALID_SOCKET, "socket() failed\n");
6895 SetLastError(0xdeadbeef);
6896 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6897 ok (!ret, "bind() failed with error %d\n", GetLastError());
6898 set_blocking(sock, FALSE);
6900 memset(&overlapped, 0, sizeof(overlapped));
6901 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6902 SetLastError(0xdeadbeef);
6903 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
6904 error = GetLastError();
6905 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
6906 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
6908 CloseHandle(overlapped.hEvent);
6909 closesocket(sock);
6911 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6912 ok(sock != INVALID_SOCKET, "socket() failed\n");
6914 SetLastError(0xdeadbeef);
6915 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6916 ok (!ret, "bind() failed with error %d\n", GetLastError());
6917 set_blocking(sock, TRUE);
6919 memset(&overlapped, 0, sizeof(overlapped));
6920 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
6921 SetLastError(0xdeadbeef);
6922 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
6923 error = GetLastError();
6924 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
6925 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error);
6927 CloseHandle(overlapped.hEvent);
6928 closesocket(sock);
6930 /* When the socket is overlapped non-blocking and the list change is requested without
6931 * an overlapped structure the error will be different. */
6932 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6933 ok(sock != INVALID_SOCKET, "socket() failed\n");
6935 SetLastError(0xdeadbeef);
6936 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6937 ok (!ret, "bind() failed with error %d\n", GetLastError());
6938 set_blocking(sock, FALSE);
6940 SetLastError(0xdeadbeef);
6941 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
6942 error = GetLastError();
6943 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error);
6944 ok (error == WSAEWOULDBLOCK, "expected 10035, got %d\n", error);
6946 io_port = CreateIoCompletionPort( (HANDLE)sock, NULL, 0, 0 );
6947 ok (io_port != NULL, "failed to create completion port %u\n", GetLastError());
6949 set_blocking(sock, FALSE);
6950 memset(&overlapped, 0, sizeof(overlapped));
6951 SetLastError(0xdeadbeef);
6952 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
6953 error = GetLastError();
6954 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %u\n", error);
6955 ok (error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error);
6957 olp = (WSAOVERLAPPED *)0xdeadbeef;
6958 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
6959 ok(!bret, "failed to get completion status %u\n", bret);
6960 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
6961 ok(!olp, "Overlapped structure is at %p\n", olp);
6963 closesocket(sock);
6965 olp = (WSAOVERLAPPED *)0xdeadbeef;
6966 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 );
6967 ok(!bret, "failed to get completion status %u\n", bret);
6968 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %u\n", GetLastError());
6969 ok(olp == &overlapped, "Overlapped structure is at %p\n", olp);
6971 CloseHandle(io_port);
6973 /* Misuse of the API by using a blocking socket and not using an overlapped structure,
6974 * this leads to a hang forever. */
6975 if (0)
6977 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6979 SetLastError(0xdeadbeef);
6980 bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
6982 set_blocking(sock, TRUE);
6983 WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
6984 /* hang */
6986 closesocket(sock);
6989 if (!winetest_interactive)
6991 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, interactive tests must be enabled\n");
6992 return;
6995 /* Bind an overlapped socket to the first found network interface */
6996 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6997 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
6998 sock2 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
6999 ok(sock2 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
7000 sock3 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
7001 ok(sock3 != INVALID_SOCKET, "Expected socket to return a valid socket\n");
7003 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7004 ok(!ret, "bind failed unexpectedly\n");
7005 ret = bind(sock2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7006 ok(!ret, "bind failed unexpectedly\n");
7007 ret = bind(sock3, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7008 ok(!ret, "bind failed unexpectedly\n");
7010 set_blocking(sock2, FALSE);
7011 set_blocking(sock3, FALSE);
7013 /* Wait for address changes, request that the user connects/disconnects an interface */
7014 memset(&overlapped, 0, sizeof(overlapped));
7015 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
7016 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL);
7017 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
7018 ok(WSAGetLastError() == WSA_IO_PENDING, "Expected pending last error, got %d\n", WSAGetLastError());
7020 ret = WSAIoctl(sock2, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL);
7021 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n");
7022 ok(WSAGetLastError() == WSAEWOULDBLOCK, "Expected would block last error, got %d\n", WSAGetLastError());
7024 event2 = WSACreateEvent();
7025 event3 = WSACreateEvent();
7026 ret = WSAEventSelect (sock2, event2, FD_ADDRESS_LIST_CHANGE);
7027 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
7028 /* sock3 did not request SIO_ADDRESS_LIST_CHANGE but it is trying to wait anyway */
7029 ret = WSAEventSelect (sock3, event3, FD_ADDRESS_LIST_CHANGE);
7030 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError());
7032 trace("Testing socket-based ipv4 address list change notification. Please connect/disconnect or"
7033 " change the ipv4 address of any of the local network interfaces (15 second timeout).\n");
7034 tick = GetTickCount();
7035 ret = WaitForSingleObject(overlapped.hEvent, 15000);
7036 ok(ret == WAIT_OBJECT_0, "failed to get overlapped event %u\n", ret);
7038 ret = WaitForSingleObject(event2, 500);
7039 todo_wine
7040 ok(ret == WAIT_OBJECT_0, "failed to get change event %u\n", ret);
7042 ret = WaitForSingleObject(event3, 500);
7043 ok(ret == WAIT_TIMEOUT, "unexpected change event\n");
7045 trace("Spent %d ms waiting.\n", GetTickCount() - tick);
7047 WSACloseEvent(event2);
7048 WSACloseEvent(event3);
7050 closesocket(sock);
7051 closesocket(sock2);
7052 closesocket(sock3);
7055 static void test_synchronous_WSAIoctl(void)
7057 HANDLE previous_port, io_port;
7058 WSAOVERLAPPED overlapped, *olp;
7059 SOCKET socket;
7060 ULONG on;
7061 ULONG_PTR key;
7062 DWORD num_bytes;
7063 BOOL ret;
7064 int res;
7066 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
7067 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() );
7069 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
7070 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() );
7072 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 );
7073 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() );
7075 on = 1;
7076 memset( &overlapped, 0, sizeof(overlapped) );
7077 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL );
7078 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() );
7080 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 );
7081 ok( ret, "failed to get completion status %u\n", GetLastError() );
7083 CloseHandle( io_port );
7084 closesocket( socket );
7085 CloseHandle( previous_port );
7089 * Provide consistent initialization for the AcceptEx IOCP tests.
7091 static SOCKET setup_iocp_src(struct sockaddr_in *bindAddress)
7093 SOCKET src;
7094 int iret, socklen;
7096 src = socket(AF_INET, SOCK_STREAM, 0);
7097 ok(src != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7099 memset(bindAddress, 0, sizeof(*bindAddress));
7100 bindAddress->sin_family = AF_INET;
7101 bindAddress->sin_addr.s_addr = inet_addr("127.0.0.1");
7102 iret = bind(src, (struct sockaddr*)bindAddress, sizeof(*bindAddress));
7103 ok(!iret, "failed to bind, error %u\n", WSAGetLastError());
7105 socklen = sizeof(*bindAddress);
7106 iret = getsockname(src, (struct sockaddr*)bindAddress, &socklen);
7107 ok(!iret, "failed to get address, error %u\n", WSAGetLastError());
7109 iret = set_blocking(src, FALSE);
7110 ok(!iret, "failed to make socket non-blocking, error %u\n", WSAGetLastError());
7112 iret = listen(src, 5);
7113 ok(!iret, "failed to listen, error %u\n", WSAGetLastError());
7115 return src;
7118 static void test_completion_port(void)
7120 HANDLE previous_port, io_port;
7121 WSAOVERLAPPED ov, *olp;
7122 SOCKET src, dest, dup, connector = INVALID_SOCKET;
7123 WSAPROTOCOL_INFOA info;
7124 char buf[1024];
7125 WSABUF bufs;
7126 DWORD num_bytes, flags;
7127 struct linger ling;
7128 int iret;
7129 BOOL bret;
7130 ULONG_PTR key;
7131 struct sockaddr_in bindAddress;
7132 GUID acceptExGuid = WSAID_ACCEPTEX;
7133 LPFN_ACCEPTEX pAcceptEx = NULL;
7134 fd_set fds_recv;
7136 memset(buf, 0, sizeof(buf));
7137 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
7138 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError());
7140 memset(&ov, 0, sizeof(ov));
7142 tcp_socketpair(&src, &dest);
7144 bufs.len = sizeof(buf);
7145 bufs.buf = buf;
7146 flags = 0;
7148 ling.l_onoff = 1;
7149 ling.l_linger = 0;
7150 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
7151 ok(!iret, "Failed to set linger %d\n", GetLastError());
7153 io_port = CreateIoCompletionPort( (HANDLE)dest, previous_port, 125, 0 );
7154 ok(io_port != NULL, "Failed to create completion port %u\n", GetLastError());
7156 SetLastError(0xdeadbeef);
7158 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
7159 ok(iret == SOCKET_ERROR, "WSARecv returned %d\n", iret);
7160 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7162 Sleep(100);
7164 closesocket(src);
7165 src = INVALID_SOCKET;
7167 SetLastError(0xdeadbeef);
7168 key = 0xdeadbeef;
7169 num_bytes = 0xdeadbeef;
7170 olp = (WSAOVERLAPPED *)0xdeadbeef;
7172 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7173 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret);
7174 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "Last error was %d\n", GetLastError());
7175 ok(key == 125, "Key is %lu\n", key);
7176 ok(num_bytes == 0, "Number of bytes received is %u\n", num_bytes);
7177 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7179 SetLastError(0xdeadbeef);
7180 key = 0xdeadbeef;
7181 num_bytes = 0xdeadbeef;
7182 olp = (WSAOVERLAPPED *)0xdeadbeef;
7184 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7185 ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret );
7186 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7187 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7188 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7189 ok(!olp, "Overlapped structure is at %p\n", olp);
7191 if (dest != INVALID_SOCKET)
7192 closesocket(dest);
7194 memset(&ov, 0, sizeof(ov));
7196 tcp_socketpair(&src, &dest);
7198 bufs.len = sizeof(buf);
7199 bufs.buf = buf;
7200 flags = 0;
7202 ling.l_onoff = 1;
7203 ling.l_linger = 0;
7204 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
7205 ok(!iret, "Failed to set linger %d\n", GetLastError());
7207 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
7208 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7210 set_blocking(dest, FALSE);
7212 closesocket(src);
7213 src = INVALID_SOCKET;
7215 Sleep(100);
7217 num_bytes = 0xdeadbeef;
7218 SetLastError(0xdeadbeef);
7220 iret = WSASend(dest, &bufs, 1, &num_bytes, 0, &ov, NULL);
7221 ok(iret == SOCKET_ERROR, "WSASend failed - %d\n", iret);
7222 ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
7223 ok(num_bytes == 0xdeadbeef, "Managed to send %d\n", num_bytes);
7225 SetLastError(0xdeadbeef);
7226 key = 0xdeadbeef;
7227 num_bytes = 0xdeadbeef;
7228 olp = (WSAOVERLAPPED *)0xdeadbeef;
7230 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7231 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
7232 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7233 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7234 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7235 ok(!olp, "Overlapped structure is at %p\n", olp);
7237 if (dest != INVALID_SOCKET)
7238 closesocket(dest);
7240 /* Test IOCP response on successful immediate read. */
7241 tcp_socketpair(&src, &dest);
7243 bufs.len = sizeof(buf);
7244 bufs.buf = buf;
7245 flags = 0;
7246 SetLastError(0xdeadbeef);
7248 iret = WSASend(src, &bufs, 1, &num_bytes, 0, &ov, NULL);
7249 ok(!iret, "WSASend failed - %d, last error %u\n", iret, GetLastError());
7250 ok(num_bytes == sizeof(buf), "Managed to send %d\n", num_bytes);
7252 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
7253 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7254 set_blocking(dest, FALSE);
7256 FD_ZERO(&fds_recv);
7257 FD_SET(dest, &fds_recv);
7258 select(dest + 1, &fds_recv, NULL, NULL, NULL);
7260 num_bytes = 0xdeadbeef;
7261 flags = 0;
7263 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
7264 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
7265 ok(num_bytes == sizeof(buf), "Managed to read %d\n", num_bytes);
7267 SetLastError(0xdeadbeef);
7268 key = 0xdeadbeef;
7269 num_bytes = 0xdeadbeef;
7270 olp = (WSAOVERLAPPED *)0xdeadbeef;
7272 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7273 ok(bret == TRUE, "failed to get completion status %u\n", bret);
7274 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
7275 ok(key == 125, "Key is %lu\n", key);
7276 ok(num_bytes == sizeof(buf), "Number of bytes transferred is %u\n", num_bytes);
7277 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7279 /* Test IOCP response on graceful shutdown. */
7280 closesocket(src);
7282 FD_ZERO(&fds_recv);
7283 FD_SET(dest, &fds_recv);
7284 select(dest + 1, &fds_recv, NULL, NULL, NULL);
7286 num_bytes = 0xdeadbeef;
7287 flags = 0;
7288 memset(&ov, 0, sizeof(ov));
7290 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
7291 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError());
7292 ok(!num_bytes, "Managed to read %d\n", num_bytes);
7294 SetLastError(0xdeadbeef);
7295 key = 0xdeadbeef;
7296 num_bytes = 0xdeadbeef;
7297 olp = (WSAOVERLAPPED *)0xdeadbeef;
7299 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7300 ok(bret == TRUE, "failed to get completion status %u\n", bret);
7301 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
7302 ok(key == 125, "Key is %lu\n", key);
7303 ok(!num_bytes, "Number of bytes transferred is %u\n", num_bytes);
7304 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7306 closesocket(src);
7307 src = INVALID_SOCKET;
7308 closesocket(dest);
7309 dest = INVALID_SOCKET;
7311 /* Test IOCP response on hard shutdown. This was the condition that triggered
7312 * a crash in an actual app (bug 38980). */
7313 tcp_socketpair(&src, &dest);
7315 bufs.len = sizeof(buf);
7316 bufs.buf = buf;
7317 flags = 0;
7318 memset(&ov, 0, sizeof(ov));
7320 ling.l_onoff = 1;
7321 ling.l_linger = 0;
7322 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling));
7323 ok(!iret, "Failed to set linger %d\n", GetLastError());
7325 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
7326 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7327 set_blocking(dest, FALSE);
7329 closesocket(src);
7330 src = INVALID_SOCKET;
7332 FD_ZERO(&fds_recv);
7333 FD_SET(dest, &fds_recv);
7334 select(dest + 1, &fds_recv, NULL, NULL, NULL);
7336 num_bytes = 0xdeadbeef;
7337 SetLastError(0xdeadbeef);
7339 /* Somehow a hard shutdown doesn't work on my Linux box. It seems SO_LINGER is ignored. */
7340 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
7341 todo_wine ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
7342 todo_wine ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError());
7343 todo_wine ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
7345 SetLastError(0xdeadbeef);
7346 key = 0xdeadbeef;
7347 num_bytes = 0xdeadbeef;
7348 olp = (WSAOVERLAPPED *)0xdeadbeef;
7350 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7351 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
7352 todo_wine ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7353 todo_wine ok(key == 0xdeadbeef, "Key is %lu\n", key);
7354 todo_wine ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7355 todo_wine ok(!olp, "Overlapped structure is at %p\n", olp);
7357 closesocket(dest);
7359 /* Test reading from a non-connected socket, mostly because the above test is marked todo. */
7360 dest = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7361 ok(dest != INVALID_SOCKET, "socket() failed\n");
7363 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0);
7364 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7365 set_blocking(dest, FALSE);
7367 num_bytes = 0xdeadbeef;
7368 SetLastError(0xdeadbeef);
7369 memset(&ov, 0, sizeof(ov));
7371 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL);
7372 ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret);
7373 ok(GetLastError() == WSAENOTCONN, "Last error was %d\n", GetLastError());
7374 ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes);
7376 SetLastError(0xdeadbeef);
7377 key = 0xdeadbeef;
7378 num_bytes = 0xdeadbeef;
7379 olp = (WSAOVERLAPPED *)0xdeadbeef;
7381 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7382 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret );
7383 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7384 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7385 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7386 ok(!olp, "Overlapped structure is at %p\n", olp);
7388 num_bytes = 0xdeadbeef;
7389 closesocket(dest);
7391 dest = socket(AF_INET, SOCK_STREAM, 0);
7392 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7394 iret = WSAIoctl(dest, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
7395 &pAcceptEx, sizeof(pAcceptEx), &num_bytes, NULL, NULL);
7396 ok(!iret, "failed to get AcceptEx, error %u\n", WSAGetLastError());
7398 /* Test IOCP response on socket close (IOCP created after AcceptEx) */
7400 src = setup_iocp_src(&bindAddress);
7402 SetLastError(0xdeadbeef);
7404 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7405 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7406 &num_bytes, &ov);
7407 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7408 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7410 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7411 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7413 closesocket(src);
7414 src = INVALID_SOCKET;
7416 SetLastError(0xdeadbeef);
7417 key = 0xdeadbeef;
7418 num_bytes = 0xdeadbeef;
7419 olp = (WSAOVERLAPPED *)0xdeadbeef;
7421 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7422 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7423 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
7424 ok(key == 125, "Key is %lu\n", key);
7425 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7426 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7427 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
7429 SetLastError(0xdeadbeef);
7430 key = 0xdeadbeef;
7431 num_bytes = 0xdeadbeef;
7432 olp = (WSAOVERLAPPED *)0xdeadbeef;
7433 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7434 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7435 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7436 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7437 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7438 ok(!olp, "Overlapped structure is at %p\n", olp);
7440 /* Test IOCP response on socket close (IOCP created before AcceptEx) */
7442 src = setup_iocp_src(&bindAddress);
7444 SetLastError(0xdeadbeef);
7446 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7447 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7449 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7450 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7451 &num_bytes, &ov);
7452 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7453 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7455 closesocket(src);
7456 src = INVALID_SOCKET;
7458 SetLastError(0xdeadbeef);
7459 key = 0xdeadbeef;
7460 num_bytes = 0xdeadbeef;
7461 olp = (WSAOVERLAPPED *)0xdeadbeef;
7463 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7464 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7465 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
7466 ok(key == 125, "Key is %lu\n", key);
7467 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7468 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7469 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
7471 SetLastError(0xdeadbeef);
7472 key = 0xdeadbeef;
7473 num_bytes = 0xdeadbeef;
7474 olp = (WSAOVERLAPPED *)0xdeadbeef;
7475 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7476 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7477 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7478 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7479 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7480 ok(!olp, "Overlapped structure is at %p\n", olp);
7482 /* Test IOCP with duplicated handle */
7484 src = setup_iocp_src(&bindAddress);
7486 SetLastError(0xdeadbeef);
7488 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7489 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7491 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
7492 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
7493 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
7495 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7496 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7497 &num_bytes, &ov);
7498 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7499 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7501 SetLastError(0xdeadbeef);
7502 key = 0xdeadbeef;
7503 num_bytes = 0xdeadbeef;
7504 olp = (WSAOVERLAPPED *)0xdeadbeef;
7505 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7506 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7507 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7508 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7509 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7510 ok(!olp, "Overlapped structure is at %p\n", olp);
7512 closesocket(src);
7513 src = INVALID_SOCKET;
7514 closesocket(dup);
7515 dup = INVALID_SOCKET;
7517 SetLastError(0xdeadbeef);
7518 key = 0xdeadbeef;
7519 num_bytes = 0xdeadbeef;
7520 olp = (WSAOVERLAPPED *)0xdeadbeef;
7521 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7522 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7523 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
7524 ok(key == 125, "Key is %lu\n", key);
7525 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7526 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7527 ok(olp && olp->Internal == (ULONG)STATUS_CANCELLED, "Internal status is %lx\n", olp ? olp->Internal : 0);
7529 SetLastError(0xdeadbeef);
7530 key = 0xdeadbeef;
7531 num_bytes = 0xdeadbeef;
7532 olp = (WSAOVERLAPPED *)0xdeadbeef;
7533 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7534 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7535 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7536 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7537 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7538 ok(!olp, "Overlapped structure is at %p\n", olp);
7540 /* Test IOCP with duplicated handle (closing duplicated handle) */
7542 src = setup_iocp_src(&bindAddress);
7544 SetLastError(0xdeadbeef);
7546 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7547 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7549 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
7550 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
7551 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
7553 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7554 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7555 &num_bytes, &ov);
7556 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7557 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7559 closesocket(dup);
7560 dup = INVALID_SOCKET;
7562 SetLastError(0xdeadbeef);
7563 key = 0xdeadbeef;
7564 num_bytes = 0xdeadbeef;
7565 olp = (WSAOVERLAPPED *)0xdeadbeef;
7566 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7567 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7568 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7569 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7570 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7571 ok(!olp, "Overlapped structure is at %p\n", olp);
7573 SetLastError(0xdeadbeef);
7574 key = 0xdeadbeef;
7575 num_bytes = 0xdeadbeef;
7576 olp = (WSAOVERLAPPED *)0xdeadbeef;
7577 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7578 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7579 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7580 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7581 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7582 ok(!olp, "Overlapped structure is at %p\n", olp);
7584 closesocket(src);
7585 src = INVALID_SOCKET;
7587 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7588 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7589 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
7590 ok(key == 125, "Key is %lu\n", key);
7591 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7592 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7593 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
7595 SetLastError(0xdeadbeef);
7596 key = 0xdeadbeef;
7597 num_bytes = 0xdeadbeef;
7598 olp = (WSAOVERLAPPED *)0xdeadbeef;
7599 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7600 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7601 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7602 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7603 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7604 ok(!olp, "Overlapped structure is at %p\n", olp);
7606 /* Test IOCP with duplicated handle (closing original handle) */
7608 src = setup_iocp_src(&bindAddress);
7610 SetLastError(0xdeadbeef);
7612 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7613 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7615 WSADuplicateSocketA( src, GetCurrentProcessId(), &info );
7616 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED);
7617 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n");
7619 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7620 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7621 &num_bytes, &ov);
7622 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7623 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7625 closesocket(src);
7626 src = INVALID_SOCKET;
7628 SetLastError(0xdeadbeef);
7629 key = 0xdeadbeef;
7630 num_bytes = 0xdeadbeef;
7631 olp = (WSAOVERLAPPED *)0xdeadbeef;
7632 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7633 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7634 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7635 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7636 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7637 ok(!olp, "Overlapped structure is at %p\n", olp);
7639 closesocket(dup);
7640 dup = INVALID_SOCKET;
7642 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7643 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7644 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError());
7645 ok(key == 125, "Key is %lu\n", key);
7646 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7647 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7648 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0);
7650 SetLastError(0xdeadbeef);
7651 key = 0xdeadbeef;
7652 num_bytes = 0xdeadbeef;
7653 olp = (WSAOVERLAPPED *)0xdeadbeef;
7654 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7655 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7656 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7657 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7658 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7659 ok(!olp, "Overlapped structure is at %p\n", olp);
7661 /* Test IOCP without AcceptEx */
7663 src = setup_iocp_src(&bindAddress);
7665 SetLastError(0xdeadbeef);
7667 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7668 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7670 closesocket(src);
7671 src = INVALID_SOCKET;
7673 SetLastError(0xdeadbeef);
7674 key = 0xdeadbeef;
7675 num_bytes = 0xdeadbeef;
7676 olp = (WSAOVERLAPPED *)0xdeadbeef;
7677 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7678 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7679 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7680 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7681 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7682 ok(!olp, "Overlapped structure is at %p\n", olp);
7684 /* */
7686 src = setup_iocp_src(&bindAddress);
7688 connector = socket(AF_INET, SOCK_STREAM, 0);
7689 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7691 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7692 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7694 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
7695 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7697 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7698 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7699 &num_bytes, &ov);
7700 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7701 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7703 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7704 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
7706 closesocket(connector);
7707 connector = INVALID_SOCKET;
7709 SetLastError(0xdeadbeef);
7710 key = 0xdeadbeef;
7711 num_bytes = 0xdeadbeef;
7712 olp = (WSAOVERLAPPED *)0xdeadbeef;
7714 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7715 ok(bret == TRUE, "failed to get completion status %u\n", bret);
7716 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
7717 ok(key == 125, "Key is %lu\n", key);
7718 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7719 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7720 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
7722 SetLastError(0xdeadbeef);
7723 key = 0xdeadbeef;
7724 num_bytes = 0xdeadbeef;
7725 olp = (WSAOVERLAPPED *)0xdeadbeef;
7726 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7727 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7728 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7729 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7730 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7731 ok(!olp, "Overlapped structure is at %p\n", olp);
7733 if (dest != INVALID_SOCKET)
7734 closesocket(dest);
7735 if (src != INVALID_SOCKET)
7736 closesocket(dest);
7738 /* */
7740 src = setup_iocp_src(&bindAddress);
7742 dest = socket(AF_INET, SOCK_STREAM, 0);
7743 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7745 connector = socket(AF_INET, SOCK_STREAM, 0);
7746 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7748 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7749 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7751 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
7752 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7754 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7755 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7756 &num_bytes, &ov);
7757 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7758 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7760 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7761 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
7763 iret = send(connector, buf, 1, 0);
7764 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
7766 Sleep(100);
7768 closesocket(dest);
7769 dest = INVALID_SOCKET;
7771 SetLastError(0xdeadbeef);
7772 key = 0xdeadbeef;
7773 num_bytes = 0xdeadbeef;
7774 olp = (WSAOVERLAPPED *)0xdeadbeef;
7776 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7777 ok(bret == TRUE, "failed to get completion status %u\n", bret);
7778 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError());
7779 ok(key == 125, "Key is %lu\n", key);
7780 ok(num_bytes == 1, "Number of bytes transferred is %u\n", num_bytes);
7781 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7782 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0);
7784 SetLastError(0xdeadbeef);
7785 key = 0xdeadbeef;
7786 num_bytes = 0xdeadbeef;
7787 olp = (WSAOVERLAPPED *)0xdeadbeef;
7788 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7789 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7790 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7791 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7792 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7793 ok(!olp, "Overlapped structure is at %p\n", olp);
7795 if (src != INVALID_SOCKET)
7796 closesocket(src);
7797 if (connector != INVALID_SOCKET)
7798 closesocket(connector);
7800 /* */
7802 src = setup_iocp_src(&bindAddress);
7804 dest = socket(AF_INET, SOCK_STREAM, 0);
7805 ok(dest != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7807 connector = socket(AF_INET, SOCK_STREAM, 0);
7808 ok(connector != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
7810 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0);
7811 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7813 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0);
7814 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError());
7816 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16),
7817 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
7818 &num_bytes, &ov);
7819 ok(bret == FALSE, "AcceptEx returned %d\n", bret);
7820 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError());
7822 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
7823 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError());
7825 closesocket(dest);
7827 SetLastError(0xdeadbeef);
7828 key = 0xdeadbeef;
7829 num_bytes = 0xdeadbeef;
7830 olp = (WSAOVERLAPPED *)0xdeadbeef;
7832 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100);
7833 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7834 ok(GetLastError() == ERROR_OPERATION_ABORTED
7835 || GetLastError() == ERROR_CONNECTION_ABORTED, "got error %u\n", GetLastError());
7836 ok(key == 125, "Key is %lu\n", key);
7837 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes);
7838 ok(olp == &ov, "Overlapped structure is at %p\n", olp);
7839 ok((NTSTATUS)olp->Internal == STATUS_CANCELLED
7840 || (NTSTATUS)olp->Internal == STATUS_CONNECTION_ABORTED, "got status %#lx\n", olp->Internal);
7842 SetLastError(0xdeadbeef);
7843 key = 0xdeadbeef;
7844 num_bytes = 0xdeadbeef;
7845 olp = (WSAOVERLAPPED *)0xdeadbeef;
7846 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 );
7847 ok(bret == FALSE, "failed to get completion status %u\n", bret);
7848 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError());
7849 ok(key == 0xdeadbeef, "Key is %lu\n", key);
7850 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes);
7851 ok(!olp, "Overlapped structure is at %p\n", olp);
7854 closesocket(src);
7855 closesocket(connector);
7856 CloseHandle(previous_port);
7859 static void test_address_list_query(void)
7861 SOCKET_ADDRESS_LIST *address_list;
7862 DWORD bytes_returned, size;
7863 unsigned int i;
7864 SOCKET s;
7865 int ret;
7867 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
7868 ok(s != INVALID_SOCKET, "Failed to create socket, error %d.\n", WSAGetLastError());
7870 bytes_returned = 0;
7871 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0, &bytes_returned, NULL, NULL);
7872 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
7873 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
7874 ok(bytes_returned >= FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]),
7875 "Got unexpected bytes_returned %u.\n", bytes_returned);
7877 size = bytes_returned;
7878 bytes_returned = 0;
7879 address_list = HeapAlloc(GetProcessHeap(), 0, size * 2);
7880 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size * 2, &bytes_returned, NULL, NULL);
7881 ok(!ret, "Got unexpected ret %d, error %d.\n", ret, WSAGetLastError());
7882 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
7884 bytes_returned = FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[address_list->iAddressCount]);
7885 for (i = 0; i < address_list->iAddressCount; ++i)
7887 bytes_returned += address_list->Address[i].iSockaddrLength;
7889 ok(size == bytes_returned, "Got unexpected size %u, expected %u.\n", size, bytes_returned);
7891 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size, NULL, NULL, NULL);
7892 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
7893 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
7895 bytes_returned = 0xdeadbeef;
7896 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, size, &bytes_returned, NULL, NULL);
7897 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
7898 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
7899 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
7901 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, 1, &bytes_returned, NULL, NULL);
7902 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
7903 ok(WSAGetLastError() == WSAEINVAL, "Got unexpected error %d.\n", WSAGetLastError());
7904 ok(bytes_returned == 0, "Got unexpected bytes_returned %u.\n", bytes_returned);
7906 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list,
7907 FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]), &bytes_returned, NULL, NULL);
7908 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret);
7909 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
7910 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size);
7912 HeapFree(GetProcessHeap(), 0, address_list);
7913 closesocket(s);
7916 static void sync_read(SOCKET src, SOCKET dst)
7918 int ret;
7919 char data[512];
7921 ret = send(dst, "Hello World!", 12, 0);
7922 ok(ret == 12, "send returned %d\n", ret);
7924 memset(data, 0, sizeof(data));
7925 ret = recv(src, data, sizeof(data), 0);
7926 ok(ret == 12, "expected 12, got %d\n", ret);
7927 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", ret, ret, data);
7930 static void iocp_async_read(SOCKET src, SOCKET dst)
7932 HANDLE port;
7933 WSAOVERLAPPED ovl, *ovl_iocp;
7934 WSABUF buf;
7935 int ret;
7936 char data[512];
7937 DWORD flags, bytes;
7938 ULONG_PTR key;
7940 memset(data, 0, sizeof(data));
7941 memset(&ovl, 0, sizeof(ovl));
7943 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
7944 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
7946 buf.len = sizeof(data);
7947 buf.buf = data;
7948 bytes = 0xdeadbeef;
7949 flags = 0;
7950 SetLastError(0xdeadbeef);
7951 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
7952 ok(ret == SOCKET_ERROR, "got %d\n", ret);
7953 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
7954 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
7956 bytes = 0xdeadbeef;
7957 key = 0xdeadbeef;
7958 ovl_iocp = (void *)0xdeadbeef;
7959 SetLastError(0xdeadbeef);
7960 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
7961 ok(!ret, "got %d\n", ret);
7962 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
7963 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
7964 ok(key == 0xdeadbeef, "got key %#lx\n", key);
7965 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
7967 ret = send(dst, "Hello World!", 12, 0);
7968 ok(ret == 12, "send returned %d\n", ret);
7970 bytes = 0xdeadbeef;
7971 key = 0xdeadbeef;
7972 ovl_iocp = NULL;
7973 SetLastError(0xdeadbeef);
7974 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
7975 ok(ret, "got %d\n", ret);
7976 ok(bytes == 12, "got bytes %u\n", bytes);
7977 ok(key == 0x12345678, "got key %#lx\n", key);
7978 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
7979 if (ovl_iocp)
7981 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
7982 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
7983 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
7986 bytes = 0xdeadbeef;
7987 key = 0xdeadbeef;
7988 ovl_iocp = (void *)0xdeadbeef;
7989 SetLastError(0xdeadbeef);
7990 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
7991 ok(!ret, "got %d\n", ret);
7992 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
7993 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
7994 ok(key == 0xdeadbeef, "got key %#lx\n", key);
7995 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
7997 CloseHandle(port);
8000 static void iocp_async_read_closesocket(SOCKET src, int how_to_close)
8002 HANDLE port;
8003 WSAOVERLAPPED ovl, *ovl_iocp;
8004 WSABUF buf;
8005 int ret;
8006 char data[512];
8007 DWORD flags, bytes;
8008 ULONG_PTR key;
8009 HWND hwnd;
8010 MSG msg;
8012 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
8013 0, 0, 0, 0, NULL, NULL, 0, NULL);
8014 ok(hwnd != 0, "CreateWindowEx failed\n");
8016 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
8017 ok(!ret, "got %d\n", ret);
8019 Sleep(100);
8020 memset(&msg, 0, sizeof(msg));
8021 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8022 ok(ret, "got %d\n", ret);
8023 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8024 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8025 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8026 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
8028 memset(data, 0, sizeof(data));
8029 memset(&ovl, 0, sizeof(ovl));
8031 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
8032 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
8034 Sleep(100);
8035 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8036 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8038 buf.len = sizeof(data);
8039 buf.buf = data;
8040 bytes = 0xdeadbeef;
8041 flags = 0;
8042 SetLastError(0xdeadbeef);
8043 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL);
8044 ok(ret == SOCKET_ERROR, "got %d\n", ret);
8045 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
8046 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8048 Sleep(100);
8049 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8050 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8052 bytes = 0xdeadbeef;
8053 key = 0xdeadbeef;
8054 ovl_iocp = (void *)0xdeadbeef;
8055 SetLastError(0xdeadbeef);
8056 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8057 ok(!ret, "got %d\n", ret);
8058 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
8059 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8060 ok(key == 0xdeadbeef, "got key %#lx\n", key);
8061 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8063 Sleep(100);
8064 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8065 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8067 switch (how_to_close)
8069 case 0:
8070 closesocket(src);
8071 break;
8072 case 1:
8073 CloseHandle((HANDLE)src);
8074 break;
8075 case 2:
8076 pNtClose((HANDLE)src);
8077 break;
8078 default:
8079 ok(0, "wrong value %d\n", how_to_close);
8080 break;
8083 Sleep(200);
8084 memset(&msg, 0, sizeof(msg));
8085 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8086 switch (how_to_close)
8088 case 0:
8089 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8090 break;
8091 case 1:
8092 case 2:
8093 todo_wine
8095 ok(ret, "got %d\n", ret);
8096 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8097 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8098 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8099 ok(msg.lParam == 0x20, "got %08lx\n", msg.lParam);
8101 break;
8102 default:
8103 ok(0, "wrong value %d\n", how_to_close);
8104 break;
8107 bytes = 0xdeadbeef;
8108 key = 0xdeadbeef;
8109 ovl_iocp = NULL;
8110 SetLastError(0xdeadbeef);
8111 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8112 ok(!ret, "got %d\n", ret);
8113 todo_wine
8114 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
8115 ok(!bytes, "got bytes %u\n", bytes);
8116 ok(key == 0x12345678, "got key %#lx\n", key);
8117 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp);
8118 if (ovl_iocp)
8120 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
8121 todo_wine
8122 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
8125 bytes = 0xdeadbeef;
8126 key = 0xdeadbeef;
8127 ovl_iocp = (void *)0xdeadbeef;
8128 SetLastError(0xdeadbeef);
8129 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8130 ok(!ret, "got %d\n", ret);
8131 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
8132 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8133 ok(key == 0xdeadbeef, "got key %#lx\n", key);
8134 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8136 CloseHandle(port);
8138 DestroyWindow(hwnd);
8141 static void iocp_async_closesocket(SOCKET src)
8143 HANDLE port;
8144 WSAOVERLAPPED *ovl_iocp;
8145 int ret;
8146 DWORD bytes;
8147 ULONG_PTR key;
8148 HWND hwnd;
8149 MSG msg;
8151 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
8152 0, 0, 0, 0, NULL, NULL, 0, NULL);
8153 ok(hwnd != 0, "CreateWindowEx failed\n");
8155 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
8156 ok(!ret, "got %d\n", ret);
8158 Sleep(100);
8159 memset(&msg, 0, sizeof(msg));
8160 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8161 ok(ret, "got %d\n", ret);
8162 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8163 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8164 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8165 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
8167 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
8168 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
8170 Sleep(100);
8171 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8172 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8174 bytes = 0xdeadbeef;
8175 key = 0xdeadbeef;
8176 ovl_iocp = (void *)0xdeadbeef;
8177 SetLastError(0xdeadbeef);
8178 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8179 ok(!ret, "got %d\n", ret);
8180 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
8181 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8182 ok(key == 0xdeadbeef, "got key %lu\n", key);
8183 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8185 Sleep(100);
8186 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8187 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8189 closesocket(src);
8191 Sleep(100);
8192 memset(&msg, 0, sizeof(msg));
8193 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8194 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8196 bytes = 0xdeadbeef;
8197 key = 0xdeadbeef;
8198 ovl_iocp = (void *)0xdeadbeef;
8199 SetLastError(0xdeadbeef);
8200 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8201 ok(!ret, "got %d\n", ret);
8202 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
8203 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8204 ok(key == 0xdeadbeef, "got key %lu\n", key);
8205 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8207 CloseHandle(port);
8209 DestroyWindow(hwnd);
8212 struct wsa_async_select_info
8214 SOCKET sock;
8215 HWND hwnd;
8218 static DWORD WINAPI wsa_async_select_thread(void *param)
8220 struct wsa_async_select_info *info = param;
8221 int ret;
8223 ret = WSAAsyncSelect(info->sock, info->hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
8224 ok(!ret, "got %d\n", ret);
8226 return 0;
8229 struct wsa_recv_info
8231 SOCKET sock;
8232 WSABUF wsa_buf;
8233 WSAOVERLAPPED ovl;
8236 static DWORD WINAPI wsa_recv_thread(void *param)
8238 struct wsa_recv_info *info = param;
8239 int ret;
8240 DWORD flags, bytes;
8242 bytes = 0xdeadbeef;
8243 flags = 0;
8244 SetLastError(0xdeadbeef);
8245 ret = WSARecv(info->sock, &info->wsa_buf, 1, &bytes, &flags, &info->ovl, NULL);
8246 ok(ret == SOCKET_ERROR, "got %d\n", ret);
8247 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError());
8248 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8250 return 0;
8253 static void iocp_async_read_thread_closesocket(SOCKET src)
8255 struct wsa_async_select_info select_info;
8256 struct wsa_recv_info recv_info;
8257 HANDLE port, thread;
8258 WSAOVERLAPPED *ovl_iocp;
8259 int ret;
8260 char data[512];
8261 DWORD bytes, tid;
8262 ULONG_PTR key;
8263 HWND hwnd;
8264 MSG msg;
8266 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
8267 0, 0, 0, 0, NULL, NULL, 0, NULL);
8268 ok(hwnd != 0, "CreateWindowEx failed\n");
8270 select_info.sock = src;
8271 select_info.hwnd = hwnd;
8272 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
8273 ok(thread != 0, "CreateThread error %u\n", GetLastError());
8274 ret = WaitForSingleObject(thread, 10000);
8275 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
8277 Sleep(100);
8278 memset(&msg, 0, sizeof(msg));
8279 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8280 ok(ret, "got %d\n", ret);
8281 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8282 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8283 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8284 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
8286 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
8287 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
8289 Sleep(100);
8290 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8291 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8293 memset(data, 0, sizeof(data));
8294 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
8295 recv_info.sock = src;
8296 recv_info.wsa_buf.len = sizeof(data);
8297 recv_info.wsa_buf.buf = data;
8298 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
8299 ok(thread != 0, "CreateThread error %u\n", GetLastError());
8300 ret = WaitForSingleObject(thread, 10000);
8301 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
8303 Sleep(100);
8304 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8305 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8307 bytes = 0xdeadbeef;
8308 key = 0xdeadbeef;
8309 ovl_iocp = (void *)0xdeadbeef;
8310 SetLastError(0xdeadbeef);
8311 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8312 ok(!ret, "got %d\n", ret);
8313 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */,
8314 "got %u\n", GetLastError());
8315 if (GetLastError() == WAIT_TIMEOUT)
8317 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8318 ok(key == 0xdeadbeef, "got key %lx\n", key);
8319 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8321 else /* document XP behaviour */
8323 ok(!bytes, "got bytes %u\n", bytes);
8324 ok(key == 0x12345678, "got key %#lx\n", key);
8325 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
8326 if (ovl_iocp)
8328 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
8329 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
8332 closesocket(src);
8333 goto xp_is_broken;
8336 Sleep(100);
8337 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8338 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8340 closesocket(src);
8342 Sleep(100);
8343 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8344 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8346 bytes = 0xdeadbeef;
8347 key = 0xdeadbeef;
8348 ovl_iocp = NULL;
8349 SetLastError(0xdeadbeef);
8350 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8351 ok(!ret, "got %d\n", ret);
8352 todo_wine
8353 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError());
8354 ok(!bytes, "got bytes %u\n", bytes);
8355 ok(key == 0x12345678, "got key %#lx\n", key);
8356 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
8357 if (ovl_iocp)
8359 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
8360 todo_wine
8361 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal);
8364 xp_is_broken:
8365 bytes = 0xdeadbeef;
8366 key = 0xdeadbeef;
8367 ovl_iocp = (void *)0xdeadbeef;
8368 SetLastError(0xdeadbeef);
8369 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8370 ok(!ret, "got %d\n", ret);
8371 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError());
8372 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8373 ok(key == 0xdeadbeef, "got key %lu\n", key);
8374 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8376 CloseHandle(port);
8378 DestroyWindow(hwnd);
8381 static void iocp_async_read_thread(SOCKET src, SOCKET dst)
8383 struct wsa_async_select_info select_info;
8384 struct wsa_recv_info recv_info;
8385 HANDLE port, thread;
8386 WSAOVERLAPPED *ovl_iocp;
8387 int ret;
8388 char data[512];
8389 DWORD bytes, tid;
8390 ULONG_PTR key;
8391 HWND hwnd;
8392 MSG msg;
8394 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
8395 0, 0, 0, 0, NULL, NULL, 0, NULL);
8396 ok(hwnd != 0, "CreateWindowEx failed\n");
8398 select_info.sock = src;
8399 select_info.hwnd = hwnd;
8400 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid);
8401 ok(thread != 0, "CreateThread error %u\n", GetLastError());
8402 ret = WaitForSingleObject(thread, 10000);
8403 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
8405 Sleep(100);
8406 memset(&msg, 0, sizeof(msg));
8407 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8408 ok(ret, "got %d\n", ret);
8409 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8410 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8411 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8412 ok(msg.lParam == 2, "got %08lx\n", msg.lParam);
8414 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0);
8415 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError());
8417 Sleep(100);
8418 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8419 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8421 memset(data, 0, sizeof(data));
8422 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl));
8423 recv_info.sock = src;
8424 recv_info.wsa_buf.len = sizeof(data);
8425 recv_info.wsa_buf.buf = data;
8426 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid);
8427 ok(thread != 0, "CreateThread error %u\n", GetLastError());
8428 ret = WaitForSingleObject(thread, 10000);
8429 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n");
8431 Sleep(100);
8432 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8433 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8435 bytes = 0xdeadbeef;
8436 key = 0xdeadbeef;
8437 ovl_iocp = (void *)0xdeadbeef;
8438 SetLastError(0xdeadbeef);
8439 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8440 ok(!ret, "got %d\n", ret);
8441 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */, "got %u\n", GetLastError());
8442 if (GetLastError() == WAIT_TIMEOUT)
8444 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8445 ok(key == 0xdeadbeef, "got key %lu\n", key);
8446 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8448 else /* document XP behaviour */
8450 ok(bytes == 0, "got bytes %u\n", bytes);
8451 ok(key == 0x12345678, "got key %#lx\n", key);
8452 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
8453 if (ovl_iocp)
8455 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh);
8456 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal);
8460 Sleep(100);
8461 memset(&msg, 0, sizeof(msg));
8462 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8463 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8464 if (ret) /* document XP behaviour */
8466 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8467 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8468 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
8471 ret = send(dst, "Hello World!", 12, 0);
8472 ok(ret == 12, "send returned %d\n", ret);
8474 Sleep(100);
8475 memset(&msg, 0, sizeof(msg));
8476 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE);
8477 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam);
8478 if (ret) /* document XP behaviour */
8480 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd);
8481 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message);
8482 ok(msg.wParam == src, "got %08lx\n", msg.wParam);
8483 ok(msg.lParam == 1, "got %08lx\n", msg.lParam);
8486 bytes = 0xdeadbeef;
8487 key = 0xdeadbeef;
8488 ovl_iocp = (void *)0xdeadbeef;
8489 SetLastError(0xdeadbeef);
8490 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100);
8491 ok(ret || broken(GetLastError() == WAIT_TIMEOUT) /* XP */, "got %u\n", GetLastError());
8492 if (ret)
8494 ok(bytes == 12, "got bytes %u\n", bytes);
8495 ok(key == 0x12345678, "got key %#lx\n", key);
8496 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp);
8497 if (ovl_iocp)
8499 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh);
8500 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal);
8501 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data);
8504 else /* document XP behaviour */
8506 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes);
8507 ok(key == 0xdeadbeef, "got key %lu\n", key);
8508 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp);
8511 CloseHandle(port);
8513 DestroyWindow(hwnd);
8516 static void test_iocp(void)
8518 SOCKET src, dst;
8519 int i;
8521 tcp_socketpair(&src, &dst);
8522 sync_read(src, dst);
8523 iocp_async_read(src, dst);
8524 closesocket(src);
8525 closesocket(dst);
8527 tcp_socketpair(&src, &dst);
8528 iocp_async_read_thread(src, dst);
8529 closesocket(src);
8530 closesocket(dst);
8532 for (i = 0; i <= 2; i++)
8534 tcp_socketpair(&src, &dst);
8535 iocp_async_read_closesocket(src, i);
8536 closesocket(dst);
8539 tcp_socketpair(&src, &dst);
8540 iocp_async_closesocket(src);
8541 closesocket(dst);
8543 tcp_socketpair(&src, &dst);
8544 iocp_async_read_thread_closesocket(src);
8545 closesocket(dst);
8548 static void test_wsaioctl(void)
8550 unsigned int i, count;
8551 INTERFACE_INFO *info;
8552 BOOL loopback_found;
8553 char buffer[4096];
8554 DWORD size;
8555 SOCKET s;
8556 int ret;
8558 s = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
8559 ok(s != INVALID_SOCKET, "failed to create socket, error %u\n", WSAGetLastError());
8561 size = 0xdeadbeef;
8562 ret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, buffer, sizeof(buffer), &size, NULL, NULL);
8563 ok(!ret, "Got unexpected ret %d.\n", ret);
8564 ok(size && size != 0xdeadbeef && !(size % sizeof(INTERFACE_INFO)), "Got unexpected size %u.\n", size);
8566 info = (INTERFACE_INFO *)buffer;
8567 count = size / sizeof(INTERFACE_INFO);
8568 loopback_found = FALSE;
8569 for (i = 0; i < count; ++i)
8571 if (info[i].iiFlags & IFF_LOOPBACK)
8572 loopback_found = TRUE;
8574 ok(info[i].iiAddress.AddressIn.sin_family == AF_INET, "Got unexpected sin_family %#x.\n",
8575 info[i].iiAddress.AddressIn.sin_family);
8576 ok(info[i].iiNetmask.AddressIn.sin_family == AF_INET, "Got unexpected sin_family %#x.\n",
8577 info[i].iiNetmask.AddressIn.sin_family);
8578 ok(info[i].iiBroadcastAddress.AddressIn.sin_family
8579 == (info[i].iiFlags & IFF_BROADCAST) ? AF_INET : 0, "Got unexpected sin_family %#x.\n",
8580 info[i].iiBroadcastAddress.AddressIn.sin_family);
8581 ok(info[i].iiAddress.AddressIn.sin_addr.S_un.S_addr, "Got zero iiAddress.\n");
8582 ok(info[i].iiNetmask.AddressIn.sin_addr.S_un.S_addr, "Got zero iiNetmask.\n");
8583 ok((info[i].iiFlags & IFF_BROADCAST) ? info[i].iiBroadcastAddress.AddressIn.sin_addr.S_un.S_addr
8584 : !info[i].iiBroadcastAddress.AddressIn.sin_addr.S_un.S_addr,
8585 "Got unexpected iiBroadcastAddress %s.\n", inet_ntoa(info[i].iiBroadcastAddress.AddressIn.sin_addr));
8588 ok(loopback_found, "Loopback interface not found.\n");
8590 size = 0xdeadbeef;
8591 ret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, buffer, sizeof(INTERFACE_INFO) - 1, &size, NULL, NULL);
8592 ok(ret == -1, "Got unexpected ret %d.\n", ret);
8593 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
8594 ok(!size, "Got unexpected size %u.\n", size);
8596 ret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, buffer, sizeof(buffer), NULL, NULL, NULL);
8597 ok(ret == -1, "Got unexpected ret %d.\n", ret);
8598 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError());
8600 closesocket(s);
8603 static void test_bind(void)
8605 const struct sockaddr_in invalid_addr = {.sin_family = AF_INET, .sin_addr.s_addr = inet_addr("192.0.2.0")};
8606 const struct sockaddr_in bind_addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)};
8607 struct sockaddr addr;
8608 SOCKET s, s2;
8609 int ret, len;
8611 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
8613 WSASetLastError(0xdeadbeef);
8614 ret = bind(s, NULL, 0);
8615 ok(ret == -1, "expected failure\n");
8616 todo_wine ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
8618 addr.sa_family = 0xdead;
8619 WSASetLastError(0xdeadbeef);
8620 ret = bind(s, &addr, sizeof(addr));
8621 ok(ret == -1, "expected failure\n");
8622 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "got error %u\n", WSAGetLastError());
8624 WSASetLastError(0xdeadbeef);
8625 ret = bind(s, (const struct sockaddr *)&bind_addr, sizeof(bind_addr) - 1);
8626 ok(ret == -1, "expected failure\n");
8627 ok(WSAGetLastError() == WSAEFAULT, "got error %u\n", WSAGetLastError());
8629 WSASetLastError(0xdeadbeef);
8630 ret = bind(s, (const struct sockaddr *)&invalid_addr, sizeof(invalid_addr));
8631 ok(ret == -1, "expected failure\n");
8632 todo_wine ok(WSAGetLastError() == WSAEADDRNOTAVAIL, "got error %u\n", WSAGetLastError());
8634 WSASetLastError(0xdeadbeef);
8635 ret = bind(s, (const struct sockaddr *)&bind_addr, sizeof(bind_addr));
8636 ok(!ret, "expected success\n");
8637 ok(!WSAGetLastError() || WSAGetLastError() == 0xdeadbeef /* win <7 */, "got error %u\n", WSAGetLastError());
8639 WSASetLastError(0xdeadbeef);
8640 ret = bind(s, (const struct sockaddr *)&bind_addr, sizeof(bind_addr));
8641 ok(ret == -1, "expected failure\n");
8642 ok(WSAGetLastError() == WSAEINVAL, "got error %u\n", WSAGetLastError());
8644 len = sizeof(addr);
8645 ret = getsockname(s, &addr, &len);
8646 ok(!ret, "got error %u\n", WSAGetLastError());
8648 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
8650 WSASetLastError(0xdeadbeef);
8651 ret = bind(s2, &addr, sizeof(addr));
8652 ok(ret == -1, "expected failure\n");
8653 ok(WSAGetLastError() == WSAEADDRINUSE, "got error %u\n", WSAGetLastError());
8655 closesocket(s2);
8656 closesocket(s);
8658 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
8660 WSASetLastError(0xdeadbeef);
8661 ret = bind(s, (const struct sockaddr *)&bind_addr, sizeof(bind_addr));
8662 ok(!ret, "expected success\n");
8663 ok(!WSAGetLastError() || WSAGetLastError() == 0xdeadbeef /* win <7 */, "got error %u\n", WSAGetLastError());
8665 closesocket(s);
8668 /* Test calling methods on a socket which is currently connecting. */
8669 static void test_connecting_socket(void)
8671 const struct sockaddr_in invalid_addr =
8673 .sin_family = AF_INET,
8674 .sin_addr.s_addr = inet_addr("192.0.2.0"),
8675 .sin_port = 255
8677 struct sockaddr_in addr;
8678 char buffer[4];
8679 SOCKET client;
8680 int ret, len;
8682 client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
8683 ok(client != -1, "failed to create socket, error %u\n", WSAGetLastError());
8684 set_blocking(client, FALSE);
8686 ret = connect(client, (struct sockaddr *)&invalid_addr, sizeof(invalid_addr));
8687 ok(ret == -1, "got %d\n", ret);
8688 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got %u\n", WSAGetLastError());
8690 len = sizeof(addr);
8691 ret = getsockname(client, (struct sockaddr *)&addr, &len);
8692 ok(!ret, "got error %u\n", WSAGetLastError());
8693 ok(addr.sin_family == AF_INET, "got family %u\n", addr.sin_family);
8694 ok(addr.sin_port, "expected nonzero port\n");
8696 len = sizeof(addr);
8697 ret = getpeername(client, (struct sockaddr *)&addr, &len);
8698 todo_wine ok(!ret, "got error %u\n", WSAGetLastError());
8699 if (!ret)
8701 ok(addr.sin_family == AF_INET, "got family %u\n", addr.sin_family);
8702 ok(addr.sin_addr.s_addr == inet_addr("192.0.2.0"), "got address %#08x\n", addr.sin_addr.s_addr);
8703 ok(addr.sin_port == 255, "expected nonzero port\n");
8706 ret = recv(client, buffer, sizeof(buffer), 0);
8707 ok(ret == -1, "got %d\n", ret);
8708 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "got %u\n", WSAGetLastError());
8710 ret = send(client, "data", 5, 0);
8711 ok(ret == -1, "got %d\n", ret);
8712 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "got %u\n", WSAGetLastError());
8714 closesocket(client);
8717 static DWORD map_status( NTSTATUS status )
8719 static const struct
8721 NTSTATUS status;
8722 DWORD error;
8724 errors[] =
8726 {STATUS_PENDING, ERROR_IO_INCOMPLETE},
8728 {STATUS_BUFFER_OVERFLOW, WSAEMSGSIZE},
8730 {STATUS_NOT_IMPLEMENTED, WSAEOPNOTSUPP},
8731 {STATUS_ACCESS_VIOLATION, WSAEFAULT},
8732 {STATUS_PAGEFILE_QUOTA, WSAENOBUFS},
8733 {STATUS_INVALID_HANDLE, WSAENOTSOCK},
8734 {STATUS_NO_SUCH_DEVICE, WSAENETDOWN},
8735 {STATUS_NO_SUCH_FILE, WSAENETDOWN},
8736 {STATUS_NO_MEMORY, WSAENOBUFS},
8737 {STATUS_CONFLICTING_ADDRESSES, WSAENOBUFS},
8738 {STATUS_ACCESS_DENIED, WSAEACCES},
8739 {STATUS_BUFFER_TOO_SMALL, WSAEFAULT},
8740 {STATUS_OBJECT_TYPE_MISMATCH, WSAENOTSOCK},
8741 {STATUS_OBJECT_NAME_NOT_FOUND, WSAENETDOWN},
8742 {STATUS_OBJECT_PATH_NOT_FOUND, WSAENETDOWN},
8743 {STATUS_SHARING_VIOLATION, WSAEADDRINUSE},
8744 {STATUS_QUOTA_EXCEEDED, WSAENOBUFS},
8745 {STATUS_TOO_MANY_PAGING_FILES, WSAENOBUFS},
8746 {STATUS_INSUFFICIENT_RESOURCES, WSAENOBUFS},
8747 {STATUS_WORKING_SET_QUOTA, WSAENOBUFS},
8748 {STATUS_DEVICE_NOT_READY, WSAEWOULDBLOCK},
8749 {STATUS_PIPE_DISCONNECTED, WSAESHUTDOWN},
8750 {STATUS_IO_TIMEOUT, WSAETIMEDOUT},
8751 {STATUS_NOT_SUPPORTED, WSAEOPNOTSUPP},
8752 {STATUS_REMOTE_NOT_LISTENING, WSAECONNREFUSED},
8753 {STATUS_BAD_NETWORK_PATH, WSAENETUNREACH},
8754 {STATUS_NETWORK_BUSY, WSAENETDOWN},
8755 {STATUS_INVALID_NETWORK_RESPONSE, WSAENETDOWN},
8756 {STATUS_UNEXPECTED_NETWORK_ERROR, WSAENETDOWN},
8757 {STATUS_REQUEST_NOT_ACCEPTED, WSAEWOULDBLOCK},
8758 {STATUS_CANCELLED, ERROR_OPERATION_ABORTED},
8759 {STATUS_COMMITMENT_LIMIT, WSAENOBUFS},
8760 {STATUS_LOCAL_DISCONNECT, WSAECONNABORTED},
8761 {STATUS_REMOTE_DISCONNECT, WSAECONNRESET},
8762 {STATUS_REMOTE_RESOURCES, WSAENOBUFS},
8763 {STATUS_LINK_FAILED, WSAECONNRESET},
8764 {STATUS_LINK_TIMEOUT, WSAETIMEDOUT},
8765 {STATUS_INVALID_CONNECTION, WSAENOTCONN},
8766 {STATUS_INVALID_ADDRESS, WSAEADDRNOTAVAIL},
8767 {STATUS_INVALID_BUFFER_SIZE, WSAEMSGSIZE},
8768 {STATUS_INVALID_ADDRESS_COMPONENT, WSAEADDRNOTAVAIL},
8769 {STATUS_TOO_MANY_ADDRESSES, WSAENOBUFS},
8770 {STATUS_ADDRESS_ALREADY_EXISTS, WSAEADDRINUSE},
8771 {STATUS_CONNECTION_DISCONNECTED, WSAECONNRESET},
8772 {STATUS_CONNECTION_RESET, WSAECONNRESET},
8773 {STATUS_TRANSACTION_ABORTED, WSAECONNABORTED},
8774 {STATUS_CONNECTION_REFUSED, WSAECONNREFUSED},
8775 {STATUS_GRACEFUL_DISCONNECT, WSAEDISCON},
8776 {STATUS_CONNECTION_ACTIVE, WSAEISCONN},
8777 {STATUS_NETWORK_UNREACHABLE, WSAENETUNREACH},
8778 {STATUS_HOST_UNREACHABLE, WSAEHOSTUNREACH},
8779 {STATUS_PROTOCOL_UNREACHABLE, WSAENETUNREACH},
8780 {STATUS_PORT_UNREACHABLE, WSAECONNRESET},
8781 {STATUS_REQUEST_ABORTED, WSAEINTR},
8782 {STATUS_CONNECTION_ABORTED, WSAECONNABORTED},
8783 {STATUS_DATATYPE_MISALIGNMENT_ERROR,WSAEFAULT},
8784 {STATUS_HOST_DOWN, WSAEHOSTDOWN},
8785 {0x80070000 | ERROR_IO_INCOMPLETE, ERROR_IO_INCOMPLETE},
8786 {0xc0010000 | ERROR_IO_INCOMPLETE, ERROR_IO_INCOMPLETE},
8787 {0xc0070000 | ERROR_IO_INCOMPLETE, ERROR_IO_INCOMPLETE},
8790 unsigned int i;
8792 for (i = 0; i < ARRAY_SIZE(errors); ++i)
8794 if (errors[i].status == status)
8795 return errors[i].error;
8798 return NT_SUCCESS(status) ? RtlNtStatusToDosErrorNoTeb(status) : WSAEINVAL;
8801 static void test_WSAGetOverlappedResult(void)
8803 OVERLAPPED overlapped = {0};
8804 DWORD size, flags;
8805 NTSTATUS status;
8806 unsigned int i;
8807 SOCKET s;
8808 BOOL ret;
8810 static const NTSTATUS ranges[][2] =
8812 {0x0, 0x10000},
8813 {0x40000000, 0x40001000},
8814 {0x80000000, 0x80001000},
8815 {0x80070000, 0x80080000},
8816 {0xc0000000, 0xc0001000},
8817 {0xc0070000, 0xc0080000},
8818 {0xd0000000, 0xd0001000},
8819 {0xd0070000, 0xd0080000},
8822 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
8824 for (i = 0; i < ARRAY_SIZE(ranges); ++i)
8826 for (status = ranges[i][0]; status < ranges[i][1]; ++status)
8828 BOOL expect_ret = NT_SUCCESS(status) && status != STATUS_PENDING;
8829 DWORD expect = map_status(status);
8831 overlapped.Internal = status;
8832 WSASetLastError(0xdeadbeef);
8833 ret = WSAGetOverlappedResult(s, &overlapped, &size, FALSE, &flags);
8834 ok(ret == expect_ret, "status %#x: expected %d, got %d\n", status, expect_ret, ret);
8835 if (ret)
8837 ok(WSAGetLastError() == expect /* >= win10 1809 */
8838 || !WSAGetLastError() /* < win10 1809 */
8839 || WSAGetLastError() == 0xdeadbeef, /* < win7 */
8840 "status %#x: expected error %u, got %u\n", status, expect, WSAGetLastError());
8842 else
8844 ok(WSAGetLastError() == expect
8845 || (status == (0xc0070000 | ERROR_IO_INCOMPLETE) && WSAGetLastError() == WSAEINVAL), /* < win8 */
8846 "status %#x: expected error %u, got %u\n", status, expect, WSAGetLastError());
8851 closesocket(s);
8854 struct nonblocking_async_recv_params
8856 SOCKET client;
8857 HANDLE event;
8860 static DWORD CALLBACK nonblocking_async_recv_thread(void *arg)
8862 const struct nonblocking_async_recv_params *params = arg;
8863 OVERLAPPED overlapped = {0};
8864 DWORD flags = 0, size;
8865 char buffer[5];
8866 WSABUF wsabuf;
8867 int ret;
8869 overlapped.hEvent = params->event;
8870 wsabuf.buf = buffer;
8871 wsabuf.len = sizeof(buffer);
8872 memset(buffer, 0, sizeof(buffer));
8873 ret = WSARecv(params->client, &wsabuf, 1, NULL, &flags, &overlapped, NULL);
8874 todo_wine_if (!params->event) ok(!ret, "got %d\n", ret);
8875 ret = GetOverlappedResult((HANDLE)params->client, &overlapped, &size, FALSE);
8876 ok(ret, "got error %u\n", GetLastError());
8877 todo_wine ok(size == 4, "got size %u\n", size);
8878 todo_wine_if (!params->event) ok(!strcmp(buffer, "data"), "got %s\n", debugstr_an(buffer, size));
8880 return 0;
8883 static void test_nonblocking_async_recv(void)
8885 struct nonblocking_async_recv_params params;
8886 OVERLAPPED overlapped = {0};
8887 SOCKET client, server;
8888 DWORD flags = 0, size;
8889 HANDLE thread, event;
8890 char buffer[5];
8891 WSABUF wsabuf;
8892 int ret;
8894 event = CreateEventW(NULL, TRUE, FALSE, NULL);
8895 wsabuf.buf = buffer;
8896 wsabuf.len = sizeof(buffer);
8898 tcp_socketpair(&client, &server);
8899 set_blocking(client, FALSE);
8900 set_blocking(server, FALSE);
8902 WSASetLastError(0xdeadbeef);
8903 ret = recv(client, buffer, sizeof(buffer), 0);
8904 ok(ret == -1, "got %d\n", ret);
8905 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
8907 WSASetLastError(0xdeadbeef);
8908 overlapped.Internal = 0xdeadbeef;
8909 ret = WSARecv(client, &wsabuf, 1, &size, &flags, NULL, NULL);
8910 ok(ret == -1, "got %d\n", ret);
8911 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
8912 ok(overlapped.Internal == 0xdeadbeef, "got status %#x\n", (NTSTATUS)overlapped.Internal);
8914 /* Overlapped, with a NULL event. */
8916 overlapped.hEvent = NULL;
8918 memset(buffer, 0, sizeof(buffer));
8919 WSASetLastError(0xdeadbeef);
8920 ret = WSARecv(client, &wsabuf, 1, NULL, &flags, &overlapped, NULL);
8921 ok(ret == -1, "got %d\n", ret);
8922 ok(WSAGetLastError() == ERROR_IO_PENDING, "got error %u\n", WSAGetLastError());
8923 ret = WaitForSingleObject((HANDLE)client, 0);
8924 ok(ret == WAIT_TIMEOUT, "expected timeout\n");
8926 ret = send(server, "data", 4, 0);
8927 ok(ret == 4, "got %d\n", ret);
8929 ret = WaitForSingleObject((HANDLE)client, 1000);
8930 ok(!ret, "wait timed out\n");
8931 ret = GetOverlappedResult((HANDLE)client, &overlapped, &size, FALSE);
8932 ok(ret, "got error %u\n", GetLastError());
8933 ok(size == 4, "got size %u\n", size);
8934 ok(!strcmp(buffer, "data"), "got %s\n", debugstr_an(buffer, size));
8936 /* Overlapped, with a non-NULL event. */
8938 overlapped.hEvent = event;
8940 memset(buffer, 0, sizeof(buffer));
8941 WSASetLastError(0xdeadbeef);
8942 ret = WSARecv(client, &wsabuf, 1, NULL, &flags, &overlapped, NULL);
8943 ok(ret == -1, "got %d\n", ret);
8944 ok(WSAGetLastError() == ERROR_IO_PENDING, "got error %u\n", WSAGetLastError());
8945 ret = WaitForSingleObject(event, 0);
8946 ok(ret == WAIT_TIMEOUT, "expected timeout\n");
8948 ret = send(server, "data", 4, 0);
8949 ok(ret == 4, "got %d\n", ret);
8951 ret = WaitForSingleObject(event, 1000);
8952 ok(!ret, "wait timed out\n");
8953 ret = GetOverlappedResult((HANDLE)client, &overlapped, &size, FALSE);
8954 ok(ret, "got error %u\n", GetLastError());
8955 ok(size == 4, "got size %u\n", size);
8956 ok(!strcmp(buffer, "data"), "got %s\n", debugstr_an(buffer, size));
8958 /* With data already in the pipe; usually this does return 0 (but not
8959 * reliably). */
8961 ret = send(server, "data", 4, 0);
8962 ok(ret == 4, "got %d\n", ret);
8964 memset(buffer, 0, sizeof(buffer));
8965 ret = WSARecv(client, &wsabuf, 1, NULL, &flags, &overlapped, NULL);
8966 ok(!ret || WSAGetLastError() == ERROR_IO_PENDING, "got error %u\n", WSAGetLastError());
8967 ret = WaitForSingleObject(event, 1000);
8968 ok(!ret, "wait timed out\n");
8969 ret = GetOverlappedResult((HANDLE)client, &overlapped, &size, FALSE);
8970 ok(ret, "got error %u\n", GetLastError());
8971 ok(size == 4, "got size %u\n", size);
8972 ok(!strcmp(buffer, "data"), "got %s\n", debugstr_an(buffer, size));
8974 closesocket(client);
8975 closesocket(server);
8977 /* With a non-overlapped socket, WSARecv() always blocks when passed an
8978 * overlapped structure, but returns WSAEWOULDBLOCK otherwise. */
8980 tcp_socketpair_flags(&client, &server, 0);
8981 set_blocking(client, FALSE);
8982 set_blocking(server, FALSE);
8984 WSASetLastError(0xdeadbeef);
8985 ret = recv(client, buffer, sizeof(buffer), 0);
8986 ok(ret == -1, "got %d\n", ret);
8987 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
8989 WSASetLastError(0xdeadbeef);
8990 overlapped.Internal = 0xdeadbeef;
8991 ret = WSARecv(client, &wsabuf, 1, &size, &flags, NULL, NULL);
8992 ok(ret == -1, "got %d\n", ret);
8993 ok(WSAGetLastError() == WSAEWOULDBLOCK, "got error %u\n", WSAGetLastError());
8994 ok(overlapped.Internal == 0xdeadbeef, "got status %#x\n", (NTSTATUS)overlapped.Internal);
8996 /* Overlapped, with a NULL event. */
8998 params.client = client;
8999 params.event = NULL;
9000 thread = CreateThread(NULL, 0, nonblocking_async_recv_thread, &params, 0, NULL);
9002 ret = WaitForSingleObject(thread, 200);
9003 todo_wine ok(ret == WAIT_TIMEOUT, "expected timeout\n");
9005 ret = send(server, "data", 4, 0);
9006 ok(ret == 4, "got %d\n", ret);
9008 ret = WaitForSingleObject(thread, 200);
9009 ok(!ret, "wait timed out\n");
9010 CloseHandle(thread);
9012 /* Overlapped, with a non-NULL event. */
9014 params.client = client;
9015 params.event = event;
9016 thread = CreateThread(NULL, 0, nonblocking_async_recv_thread, &params, 0, NULL);
9018 ret = WaitForSingleObject(thread, 200);
9019 todo_wine ok(ret == WAIT_TIMEOUT, "expected timeout\n");
9021 ret = send(server, "data", 4, 0);
9022 ok(ret == 4, "got %d\n", ret);
9024 ret = WaitForSingleObject(thread, 200);
9025 ok(!ret, "wait timed out\n");
9026 CloseHandle(thread);
9028 /* With data already in the pipe. */
9030 ret = send(server, "data", 4, 0);
9031 ok(ret == 4, "got %d\n", ret);
9033 memset(buffer, 0, sizeof(buffer));
9034 ret = WSARecv(client, &wsabuf, 1, NULL, &flags, &overlapped, NULL);
9035 ok(!ret, "got %d\n", ret);
9036 ret = GetOverlappedResult((HANDLE)client, &overlapped, &size, FALSE);
9037 todo_wine ok(ret, "got error %u\n", GetLastError());
9038 ok(size == 4, "got size %u\n", size);
9039 todo_wine ok(!strcmp(buffer, "data"), "got %s\n", debugstr_an(buffer, size));
9041 closesocket(client);
9042 closesocket(server);
9044 CloseHandle(overlapped.hEvent);
9047 START_TEST( sock )
9049 int i;
9051 /* Leave these tests at the beginning. They depend on WSAStartup not having been
9052 * called, which is done by Init() below. */
9053 test_WithoutWSAStartup();
9054 test_WithWSAStartup();
9056 Init();
9058 test_set_getsockopt();
9059 test_so_reuseaddr();
9060 test_ip_pktinfo();
9061 test_extendedSocketOptions();
9063 for (i = 0; i < ARRAY_SIZE(tests); i++)
9064 do_test(&tests[i]);
9066 test_UDP();
9068 test_WSASocket();
9069 test_WSADuplicateSocket();
9070 test_WSAEnumNetworkEvents();
9072 test_errors();
9073 test_listen();
9074 test_select();
9075 test_accept();
9076 test_getpeername();
9077 test_getsockname();
9078 test_ioctlsocket();
9080 test_WSASendMsg();
9081 test_WSASendTo();
9082 test_WSARecv();
9083 test_WSAPoll();
9084 test_write_watch();
9085 test_iocp();
9087 test_events();
9089 test_ipv6only();
9090 test_TransmitFile();
9091 test_AcceptEx();
9092 test_ConnectEx();
9093 test_DisconnectEx();
9095 test_sioRoutingInterfaceQuery();
9096 test_sioAddressListChange();
9098 test_completion_port();
9099 test_address_list_query();
9100 test_bind();
9101 test_connecting_socket();
9102 test_WSAGetOverlappedResult();
9103 test_nonblocking_async_recv();
9105 /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */
9106 test_send();
9107 test_synchronous_WSAIoctl();
9108 test_wsaioctl();
9110 Exit();