1 /* 2 * Unit test suite for winsock functions 3 * 4 * Copyright 2002 Martin Wilck 5 * Copyright 2005 Thomas Kho 6 * Copyright 2008 Jeff Zaroyko 7 * Copyright 2017 Dmitry Timoshkov 8 * 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. 13 * 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. 18 * 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 22 */ 23 24 #ifdef __REACTOS__ 25 #undef _WIN32_WINNT 26 #define _WIN32_WINNT 0x0600 27 #endif 28 29 #include <ntstatus.h> 30 #define WIN32_NO_STATUS 31 #include <winsock2.h> 32 #include <windows.h> 33 #include <winternl.h> 34 #include <ws2tcpip.h> 35 #include <wsipx.h> 36 #include <wsnwlink.h> 37 #include <mswsock.h> 38 #include <mstcpip.h> 39 #include <iphlpapi.h> 40 #include <stdio.h> 41 #include "wine/test.h" 42 43 // ReactOS: Wine has this in mstcpip.h, but it doesn't belong there 44 #define WSA_CMSG_ALIGN(len) (((len) + sizeof(SIZE_T) - 1) & ~(sizeof(SIZE_T) - 1)) 45 46 #define MAX_CLIENTS 4 /* Max number of clients */ 47 #define FIRST_CHAR 'A' /* First character in transferred pattern */ 48 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */ 49 #define BIND_TRIES 6 /* Number of bind() attempts */ 50 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads 51 after server initialization, if something hangs */ 52 53 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */ 54 55 #define NUM_THREADS 3 /* Number of threads to run getservbyname */ 56 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */ 57 58 #define SERVERIP "127.0.0.1" /* IP to bind to */ 59 #define SERVERPORT 9374 /* Port number to bind to */ 60 61 #define wsa_ok(op, cond, msg) \ 62 do { \ 63 int tmp, err = 0; \ 64 tmp = op; \ 65 if ( !(cond tmp) ) err = WSAGetLastError(); \ 66 ok ( cond tmp, msg, GetCurrentThreadId(), err); \ 67 } while (0); 68 69 #define make_keepalive(k, enable, time, interval) \ 70 k.onoff = enable; \ 71 k.keepalivetime = time; \ 72 k.keepaliveinterval = interval; 73 74 /* Function pointers */ 75 static void (WINAPI *pfreeaddrinfo)(struct addrinfo *); 76 static int (WINAPI *pgetaddrinfo)(LPCSTR,LPCSTR,const struct addrinfo *,struct addrinfo **); 77 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW); 78 static void (WINAPI *pFreeAddrInfoExW)(ADDRINFOEXW *ai); 79 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *); 80 static int (WINAPI *pGetAddrInfoExW)(const WCHAR *name, const WCHAR *servname, DWORD namespace, 81 GUID *namespace_id, const ADDRINFOEXW *hints, ADDRINFOEXW **result, 82 struct timeval *timeout, OVERLAPPED *overlapped, 83 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine, HANDLE *handle); 84 static int (WINAPI *pGetAddrInfoExOverlappedResult)(OVERLAPPED *overlapped); 85 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG); 86 static PCWSTR(WINAPI *pInetNtopW)(INT,LPVOID,LPWSTR,ULONG); 87 static int (WINAPI *pInetPtonA)(INT,LPCSTR,LPVOID); 88 static int (WINAPI *pInetPtonW)(INT,LPWSTR,LPVOID); 89 static int (WINAPI *pWSALookupServiceBeginW)(LPWSAQUERYSETW,DWORD,LPHANDLE); 90 static int (WINAPI *pWSALookupServiceEnd)(HANDLE); 91 static int (WINAPI *pWSALookupServiceNextW)(HANDLE,DWORD,LPDWORD,LPWSAQUERYSETW); 92 static int (WINAPI *pWSAEnumNameSpaceProvidersA)(LPDWORD,LPWSANAMESPACE_INFOA); 93 static int (WINAPI *pWSAEnumNameSpaceProvidersW)(LPDWORD,LPWSANAMESPACE_INFOW); 94 static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT); 95 96 /* Function pointers from ntdll */ 97 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS); 98 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS); 99 100 /* Function pointers from iphlpapi */ 101 static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG); 102 static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL); 103 104 /* Function pointers from ntdll */ 105 static DWORD (WINAPI *pNtClose)(HANDLE); 106 107 /**************** Structs and typedefs ***************/ 108 109 typedef struct thread_info 110 { 111 HANDLE thread; 112 DWORD id; 113 } thread_info; 114 115 /* Information in the server about open client connections */ 116 typedef struct sock_info 117 { 118 SOCKET s; 119 struct sockaddr_in addr; 120 struct sockaddr_in peer; 121 char *buf; 122 int n_recvd; 123 int n_sent; 124 } sock_info; 125 126 /* Test parameters for both server & client */ 127 typedef struct test_params 128 { 129 int sock_type; 130 int sock_prot; 131 const char *inet_addr; 132 short inet_port; 133 int chunk_size; 134 int n_chunks; 135 int n_clients; 136 } test_params; 137 138 /* server-specific test parameters */ 139 typedef struct server_params 140 { 141 test_params *general; 142 DWORD sock_flags; 143 int buflen; 144 } server_params; 145 146 /* client-specific test parameters */ 147 typedef struct client_params 148 { 149 test_params *general; 150 DWORD sock_flags; 151 int buflen; 152 } client_params; 153 154 /* This type combines all information for setting up a test scenario */ 155 typedef struct test_setup 156 { 157 test_params general; 158 LPVOID srv; 159 server_params srv_params; 160 LPVOID clt; 161 client_params clt_params; 162 } test_setup; 163 164 /* Thread local storage for server */ 165 typedef struct server_memory 166 { 167 SOCKET s; 168 struct sockaddr_in addr; 169 sock_info sock[MAX_CLIENTS]; 170 } server_memory; 171 172 /* Thread local storage for client */ 173 typedef struct client_memory 174 { 175 SOCKET s; 176 struct sockaddr_in addr; 177 char *send_buf; 178 char *recv_buf; 179 } client_memory; 180 181 /* SelectReadThread thread parameters */ 182 typedef struct select_thread_params 183 { 184 SOCKET s; 185 BOOL ReadKilled; 186 } select_thread_params; 187 188 /* Tests used in both getaddrinfo and GetAddrInfoW */ 189 static const struct addr_hint_tests 190 { 191 int family, socktype, protocol; 192 DWORD error; 193 } hinttests[] = { 194 {AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0 }, 195 {AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP, 0 }, 196 {AF_UNSPEC, SOCK_STREAM, IPPROTO_IPV6,0 }, 197 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_TCP, 0 }, 198 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0 }, 199 {AF_UNSPEC, SOCK_DGRAM, IPPROTO_IPV6,0 }, 200 {AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 }, 201 {AF_INET, SOCK_STREAM, IPPROTO_UDP, 0 }, 202 {AF_INET, SOCK_STREAM, IPPROTO_IPV6,0 }, 203 {AF_INET, SOCK_DGRAM, IPPROTO_TCP, 0 }, 204 {AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0 }, 205 {AF_INET, SOCK_DGRAM, IPPROTO_IPV6,0 }, 206 {AF_UNSPEC, 0, IPPROTO_TCP, 0 }, 207 {AF_UNSPEC, 0, IPPROTO_UDP, 0 }, 208 {AF_UNSPEC, 0, IPPROTO_IPV6,0 }, 209 {AF_UNSPEC, SOCK_STREAM, 0, 0 }, 210 {AF_UNSPEC, SOCK_DGRAM, 0, 0 }, 211 {AF_INET, 0, IPPROTO_TCP, 0 }, 212 {AF_INET, 0, IPPROTO_UDP, 0 }, 213 {AF_INET, 0, IPPROTO_IPV6,0 }, 214 {AF_INET, SOCK_STREAM, 0, 0 }, 215 {AF_INET, SOCK_DGRAM, 0, 0 }, 216 {AF_UNSPEC, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT }, 217 {AF_UNSPEC, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT }, 218 {AF_UNSPEC, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT }, 219 {AF_INET, 999, IPPROTO_TCP, WSAESOCKTNOSUPPORT }, 220 {AF_INET, 999, IPPROTO_UDP, WSAESOCKTNOSUPPORT }, 221 {AF_INET, 999, IPPROTO_IPV6,WSAESOCKTNOSUPPORT }, 222 {AF_UNSPEC, SOCK_STREAM, 999, 0 }, 223 {AF_UNSPEC, SOCK_STREAM, 999, 0 }, 224 {AF_INET, SOCK_DGRAM, 999, 0 }, 225 {AF_INET, SOCK_DGRAM, 999, 0 }, 226 }; 227 228 /**************** Static variables ***************/ 229 230 static DWORD tls; /* Thread local storage index */ 231 static HANDLE thread[1+MAX_CLIENTS]; 232 static DWORD thread_id[1+MAX_CLIENTS]; 233 static HANDLE server_ready; 234 static HANDLE client_ready[MAX_CLIENTS]; 235 static int client_id; 236 237 /**************** General utility functions ***************/ 238 239 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len); 240 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock); 241 242 static int tcp_socketpair(SOCKET *src, SOCKET *dst) 243 { 244 SOCKET server = INVALID_SOCKET; 245 struct sockaddr_in addr; 246 int len; 247 int ret; 248 249 *src = INVALID_SOCKET; 250 *dst = INVALID_SOCKET; 251 252 *src = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 253 if (*src == INVALID_SOCKET) 254 goto end; 255 256 server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 257 if (server == INVALID_SOCKET) 258 goto end; 259 260 memset(&addr, 0, sizeof(addr)); 261 addr.sin_family = AF_INET; 262 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 263 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr)); 264 if (ret != 0) 265 goto end; 266 267 len = sizeof(addr); 268 ret = getsockname(server, (struct sockaddr*)&addr, &len); 269 if (ret != 0) 270 goto end; 271 272 ret = listen(server, 1); 273 if (ret != 0) 274 goto end; 275 276 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr)); 277 if (ret != 0) 278 goto end; 279 280 len = sizeof(addr); 281 *dst = accept(server, (struct sockaddr*)&addr, &len); 282 283 end: 284 if (server != INVALID_SOCKET) 285 closesocket(server); 286 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET) 287 return 0; 288 closesocket(*src); 289 closesocket(*dst); 290 return -1; 291 } 292 293 static int tcp_socketpair_ovl(SOCKET *src, SOCKET *dst) 294 { 295 SOCKET server = INVALID_SOCKET; 296 struct sockaddr_in addr; 297 int len, ret; 298 299 *src = INVALID_SOCKET; 300 *dst = INVALID_SOCKET; 301 302 *src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 303 if (*src == INVALID_SOCKET) 304 goto end; 305 306 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 307 if (server == INVALID_SOCKET) 308 goto end; 309 310 memset(&addr, 0, sizeof(addr)); 311 addr.sin_family = AF_INET; 312 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 313 ret = bind(server, (struct sockaddr *)&addr, sizeof(addr)); 314 if (ret != 0) 315 goto end; 316 317 len = sizeof(addr); 318 ret = getsockname(server, (struct sockaddr *)&addr, &len); 319 if (ret != 0) 320 goto end; 321 322 ret = listen(server, 1); 323 if (ret != 0) 324 goto end; 325 326 ret = connect(*src, (struct sockaddr *)&addr, sizeof(addr)); 327 if (ret != 0) 328 goto end; 329 330 len = sizeof(addr); 331 *dst = accept(server, (struct sockaddr *)&addr, &len); 332 333 end: 334 if (server != INVALID_SOCKET) 335 closesocket(server); 336 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET) 337 return 0; 338 closesocket(*src); 339 closesocket(*dst); 340 return -1; 341 } 342 343 static void set_so_opentype ( BOOL overlapped ) 344 { 345 int optval = !overlapped, newval, len = sizeof (int); 346 347 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, 348 (LPVOID) &optval, sizeof (optval) ) == 0, 349 "setting SO_OPENTYPE failed\n" ); 350 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, 351 (LPVOID) &newval, &len ) == 0, 352 "getting SO_OPENTYPE failed\n" ); 353 ok ( optval == newval, "failed to set SO_OPENTYPE\n" ); 354 } 355 356 static int set_blocking ( SOCKET s, BOOL blocking ) 357 { 358 u_long val = !blocking; 359 return ioctlsocket ( s, FIONBIO, &val ); 360 } 361 362 static void fill_buffer ( char *buf, int chunk_size, int n_chunks ) 363 { 364 char c, *p; 365 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size ) 366 memset ( p, c, chunk_size ); 367 } 368 369 static int test_buffer ( char *buf, int chunk_size, int n_chunks ) 370 { 371 char c, *p; 372 int i; 373 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size ) 374 { 375 for ( i = 0; i < chunk_size; i++ ) 376 if ( p[i] != c ) return i; 377 } 378 return -1; 379 } 380 381 /* 382 * This routine is called when a client / server does not expect any more data, 383 * but needs to acknowledge the closing of the connection (by reading 0 bytes). 384 */ 385 static void read_zero_bytes ( SOCKET s ) 386 { 387 char buf[256]; 388 int tmp, n = 0; 389 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 ) 390 n += tmp; 391 ok ( n <= 0, "garbage data received: %d bytes\n", n ); 392 } 393 394 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int flags, int sendlen ) 395 { 396 char* last = buf + buflen, *p; 397 int n = 1; 398 for ( p = buf; n > 0 && p < last; ) 399 { 400 n = send ( s, p, min ( sendlen, last - p ), flags ); 401 if (n > 0) p += n; 402 } 403 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" ); 404 return p - buf; 405 } 406 407 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int flags, int recvlen ) 408 { 409 char* last = buf + buflen, *p; 410 int n = 1; 411 for ( p = buf; n > 0 && p < last; ) 412 { 413 n = recv ( s, p, min ( recvlen, last - p ), flags ); 414 if (n > 0) p += n; 415 } 416 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" ); 417 return p - buf; 418 } 419 420 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen, int flags, struct sockaddr *from, int *fromlen, int recvlen ) 421 { 422 char* last = buf + buflen, *p; 423 int n = 1; 424 for ( p = buf; n > 0 && p < last; ) 425 { 426 n = recvfrom ( s, p, min ( recvlen, last - p ), flags, from, fromlen ); 427 if (n > 0) p += n; 428 } 429 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" ); 430 return p - buf; 431 } 432 433 /* 434 * Call this routine right after thread startup. 435 * SO_OPENTYPE must by 0, regardless what the server did. 436 */ 437 static void check_so_opentype (void) 438 { 439 int tmp = 1, len; 440 len = sizeof (tmp); 441 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len ); 442 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp ); 443 } 444 445 static void compare_addrinfo (ADDRINFO *a, ADDRINFO *b) 446 { 447 for (; a && b ; a = a->ai_next, b = b->ai_next) 448 { 449 ok(a->ai_flags == b->ai_flags, 450 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags); 451 ok(a->ai_family == b->ai_family, 452 "Wrong family %d != %d\n", a->ai_family, b->ai_family); 453 ok(a->ai_socktype == b->ai_socktype, 454 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype); 455 ok(a->ai_protocol == b->ai_protocol, 456 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol); 457 ok(a->ai_addrlen == b->ai_addrlen, 458 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen); 459 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)), 460 "Wrong address data\n"); 461 if (a->ai_canonname && b->ai_canonname) 462 { 463 ok(!strcmp(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n", 464 a->ai_canonname, b->ai_canonname); 465 } 466 else 467 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n", 468 a->ai_canonname, b->ai_canonname); 469 } 470 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b); 471 } 472 473 static void compare_addrinfow (ADDRINFOW *a, ADDRINFOW *b) 474 { 475 for (; a && b ; a = a->ai_next, b = b->ai_next) 476 { 477 ok(a->ai_flags == b->ai_flags, 478 "Wrong flags %d != %d\n", a->ai_flags, b->ai_flags); 479 ok(a->ai_family == b->ai_family, 480 "Wrong family %d != %d\n", a->ai_family, b->ai_family); 481 ok(a->ai_socktype == b->ai_socktype, 482 "Wrong socktype %d != %d\n", a->ai_socktype, b->ai_socktype); 483 ok(a->ai_protocol == b->ai_protocol, 484 "Wrong protocol %d != %d\n", a->ai_protocol, b->ai_protocol); 485 ok(a->ai_addrlen == b->ai_addrlen, 486 "Wrong addrlen %lu != %lu\n", a->ai_addrlen, b->ai_addrlen); 487 ok(!memcmp(a->ai_addr, b->ai_addr, min(a->ai_addrlen, b->ai_addrlen)), 488 "Wrong address data\n"); 489 if (a->ai_canonname && b->ai_canonname) 490 { 491 ok(!lstrcmpW(a->ai_canonname, b->ai_canonname), "Wrong canonical name '%s' != '%s'\n", 492 wine_dbgstr_w(a->ai_canonname), wine_dbgstr_w(b->ai_canonname)); 493 } 494 else 495 ok(!a->ai_canonname && !b->ai_canonname, "Expected both names absent (%p != %p)\n", 496 a->ai_canonname, b->ai_canonname); 497 } 498 ok(!a && !b, "Expected both addresses null (%p != %p)\n", a, b); 499 } 500 501 /**************** Server utility functions ***************/ 502 503 /* 504 * Even if we have closed our server socket cleanly, 505 * the OS may mark the address "in use" for some time - 506 * this happens with native Linux apps, too. 507 */ 508 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen ) 509 { 510 int err, wsaerr = 0, n_try = BIND_TRIES; 511 512 while ( ( err = bind ( s, addr, addrlen ) ) != 0 && 513 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE && 514 n_try-- >= 0) 515 { 516 trace ( "address in use, waiting ...\n" ); 517 Sleep ( 1000 * BIND_SLEEP ); 518 } 519 ok ( err == 0, "failed to bind: %d\n", wsaerr ); 520 } 521 522 static void server_start ( server_params *par ) 523 { 524 int i; 525 test_params *gen = par->general; 526 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) ); 527 528 TlsSetValue ( tls, mem ); 529 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot, 530 NULL, 0, par->sock_flags ); 531 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" ); 532 533 mem->addr.sin_family = AF_INET; 534 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr ); 535 mem->addr.sin_port = htons ( gen->inet_port ); 536 537 for (i = 0; i < MAX_CLIENTS; i++) 538 { 539 mem->sock[i].s = INVALID_SOCKET; 540 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size ); 541 mem->sock[i].n_recvd = 0; 542 mem->sock[i].n_sent = 0; 543 } 544 545 if ( gen->sock_type == SOCK_STREAM ) 546 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) ); 547 } 548 549 static void server_stop (void) 550 { 551 int i; 552 server_memory *mem = TlsGetValue ( tls ); 553 554 for (i = 0; i < MAX_CLIENTS; i++ ) 555 { 556 LocalFree ( mem->sock[i].buf ); 557 if ( mem->sock[i].s != INVALID_SOCKET ) 558 closesocket ( mem->sock[i].s ); 559 } 560 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" ); 561 LocalFree ( mem ); 562 ExitThread ( GetCurrentThreadId () ); 563 } 564 565 /**************** Client utilitiy functions ***************/ 566 567 static void client_start ( client_params *par ) 568 { 569 test_params *gen = par->general; 570 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory)); 571 572 TlsSetValue ( tls, mem ); 573 574 WaitForSingleObject ( server_ready, INFINITE ); 575 576 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot, 577 NULL, 0, par->sock_flags ); 578 579 mem->addr.sin_family = AF_INET; 580 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr ); 581 mem->addr.sin_port = htons ( gen->inet_port ); 582 583 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" ); 584 585 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size ); 586 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size; 587 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks ); 588 589 SetEvent ( client_ready[client_id] ); 590 /* Wait for the other clients to come up */ 591 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE ); 592 } 593 594 static void client_stop (void) 595 { 596 client_memory *mem = TlsGetValue ( tls ); 597 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" ); 598 LocalFree ( mem->send_buf ); 599 LocalFree ( mem ); 600 ExitThread(0); 601 } 602 603 /**************** Servers ***************/ 604 605 /* 606 * simple_server: A very basic server doing synchronous IO. 607 */ 608 static VOID WINAPI simple_server ( server_params *par ) 609 { 610 test_params *gen = par->general; 611 server_memory *mem; 612 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i, 613 id = GetCurrentThreadId(); 614 615 trace ( "simple_server (%x) starting\n", id ); 616 617 set_so_opentype ( FALSE ); /* non-overlapped */ 618 server_start ( par ); 619 mem = TlsGetValue ( tls ); 620 621 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n"); 622 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n"); 623 624 trace ( "simple_server (%x) ready\n", id ); 625 SetEvent ( server_ready ); /* notify clients */ 626 627 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ ) 628 { 629 trace ( "simple_server (%x): waiting for client\n", id ); 630 631 /* accept a single connection */ 632 tmp = sizeof ( mem->sock[0].peer ); 633 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp ); 634 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" ); 635 636 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), 637 "simple_server (%x): strange peer address\n", id ); 638 639 /* Receive data & check it */ 640 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen ); 641 ok ( n_recvd == n_expected, 642 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); 643 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks ); 644 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos); 645 646 /* Echo data back */ 647 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen ); 648 ok ( n_sent == n_expected, 649 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 650 651 /* cleanup */ 652 read_zero_bytes ( mem->sock[0].s ); 653 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" ); 654 mem->sock[0].s = INVALID_SOCKET; 655 } 656 657 trace ( "simple_server (%x) exiting\n", id ); 658 server_stop (); 659 } 660 661 /* 662 * oob_server: A very basic server receiving out-of-band data. 663 */ 664 static VOID WINAPI oob_server ( server_params *par ) 665 { 666 test_params *gen = par->general; 667 server_memory *mem; 668 u_long atmark = 0; 669 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, tmp, 670 id = GetCurrentThreadId(); 671 672 trace ( "oob_server (%x) starting\n", id ); 673 674 set_so_opentype ( FALSE ); /* non-overlapped */ 675 server_start ( par ); 676 mem = TlsGetValue ( tls ); 677 678 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "oob_server (%x): failed to set blocking mode: %d\n"); 679 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "oob_server (%x): listen failed: %d\n"); 680 681 trace ( "oob_server (%x) ready\n", id ); 682 SetEvent ( server_ready ); /* notify clients */ 683 684 trace ( "oob_server (%x): waiting for client\n", id ); 685 686 /* accept a single connection */ 687 tmp = sizeof ( mem->sock[0].peer ); 688 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp ); 689 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "oob_server (%x): accept failed: %d\n" ); 690 691 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), 692 "oob_server (%x): strange peer address\n", id ); 693 694 /* check initial atmark state */ 695 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark ); 696 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark ); 697 698 /* Receive normal data */ 699 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen ); 700 ok ( n_recvd == n_expected, 701 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); 702 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks ); 703 ok ( pos == -1, "oob_server (%x): test pattern error: %d\n", id, pos); 704 705 /* check atmark state */ 706 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark ); 707 ok ( atmark == 1, "oob_server (%x): unexpectedly at the OOB mark: %i\n", id, atmark ); 708 709 /* Echo data back */ 710 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen ); 711 ok ( n_sent == n_expected, 712 "oob_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 713 714 /* Receive a part of the out-of-band data and print atmark state */ 715 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, 8, 0, par->buflen ); 716 ok ( n_recvd == 8, 717 "oob_server (%x): received less data than expected: %d of %d\n", id, n_recvd, 8 ); 718 n_expected -= 8; 719 720 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark ); 721 trace( "oob_server (%x): %s the OOB mark: %i\n", id, atmark == 1 ? "not at" : "at", atmark ); 722 723 /* Receive the rest of the out-of-band data and check atmark state */ 724 do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, 0, par->buflen ); 725 726 ioctlsocket ( mem->sock[0].s, SIOCATMARK, &atmark ); 727 todo_wine ok ( atmark == 0, "oob_server (%x): not at the OOB mark: %i\n", id, atmark ); 728 729 /* cleanup */ 730 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "oob_server (%x): closesocket error: %d\n" ); 731 mem->sock[0].s = INVALID_SOCKET; 732 733 trace ( "oob_server (%x) exiting\n", id ); 734 server_stop (); 735 } 736 737 /* 738 * select_server: A non-blocking server. 739 */ 740 static VOID WINAPI select_server ( server_params *par ) 741 { 742 test_params *gen = par->general; 743 server_memory *mem; 744 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i, 745 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd, 746 n_set, delta, n_ready; 747 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */ 748 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend; 749 750 trace ( "select_server (%x) starting\n", id ); 751 752 set_so_opentype ( FALSE ); /* non-overlapped */ 753 server_start ( par ); 754 mem = TlsGetValue ( tls ); 755 756 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n"); 757 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n"); 758 759 trace ( "select_server (%x) ready\n", id ); 760 SetEvent ( server_ready ); /* notify clients */ 761 762 FD_ZERO ( &fds_openrecv ); 763 FD_ZERO ( &fds_recv ); 764 FD_ZERO ( &fds_send ); 765 FD_ZERO ( &fds_opensend ); 766 767 FD_SET ( mem->s, &fds_openrecv ); 768 769 while(1) 770 { 771 fds_recv = fds_openrecv; 772 fds_send = fds_opensend; 773 774 n_set = 0; 775 776 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=, 777 "select_server (%x): select() failed: %d\n" ); 778 779 /* check for incoming requests */ 780 if ( FD_ISSET ( mem->s, &fds_recv ) ) { 781 n_set += 1; 782 783 trace ( "select_server (%x): accepting client connection\n", id ); 784 785 /* accept a single connection */ 786 tmp = sizeof ( mem->sock[n_connections].peer ); 787 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp ); 788 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" ); 789 790 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), 791 "select_server (%x): strange peer address\n", id ); 792 793 /* add to list of open connections */ 794 FD_SET ( mem->sock[n_connections].s, &fds_openrecv ); 795 FD_SET ( mem->sock[n_connections].s, &fds_opensend ); 796 797 n_connections++; 798 } 799 800 /* handle open requests */ 801 802 for ( i = 0; i < n_connections; i++ ) 803 { 804 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) { 805 n_set += 1; 806 807 if ( mem->sock[i].n_recvd < n_expected ) { 808 /* Receive data & check it */ 809 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 ); 810 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() ); 811 mem->sock[i].n_recvd += n_recvd; 812 813 if ( mem->sock[i].n_recvd == n_expected ) { 814 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks ); 815 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos ); 816 FD_CLR ( mem->sock[i].s, &fds_openrecv ); 817 } 818 819 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd ); 820 } 821 } 822 823 /* only echo back what we've received */ 824 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent; 825 826 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) { 827 n_set += 1; 828 829 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) { 830 /* Echo data back */ 831 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 ); 832 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() ); 833 mem->sock[i].n_sent += n_sent; 834 835 if ( mem->sock[i].n_sent == n_expected ) { 836 FD_CLR ( mem->sock[i].s, &fds_opensend ); 837 } 838 839 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent ); 840 } 841 } 842 } 843 844 /* check that select returned the correct number of ready sockets */ 845 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id ); 846 847 /* check if all clients are done */ 848 if ( ( fds_opensend.fd_count == 0 ) 849 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */ 850 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) { 851 break; 852 } 853 } 854 855 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ ) 856 { 857 /* cleanup */ 858 read_zero_bytes ( mem->sock[i].s ); 859 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" ); 860 mem->sock[i].s = INVALID_SOCKET; 861 } 862 863 trace ( "select_server (%x) exiting\n", id ); 864 server_stop (); 865 } 866 867 /**************** Clients ***************/ 868 869 /* 870 * simple_client: A very basic client doing synchronous IO. 871 */ 872 static VOID WINAPI simple_client ( client_params *par ) 873 { 874 test_params *gen = par->general; 875 client_memory *mem; 876 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id; 877 878 id = GetCurrentThreadId(); 879 trace ( "simple_client (%x): starting\n", id ); 880 /* wait here because we want to call set_so_opentype before creating a socket */ 881 WaitForSingleObject ( server_ready, INFINITE ); 882 trace ( "simple_client (%x): server ready\n", id ); 883 884 check_so_opentype (); 885 set_so_opentype ( FALSE ); /* non-overlapped */ 886 client_start ( par ); 887 mem = TlsGetValue ( tls ); 888 889 /* Connect */ 890 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ), 891 0 ==, "simple_client (%x): connect error: %d\n" ); 892 ok ( set_blocking ( mem->s, TRUE ) == 0, 893 "simple_client (%x): failed to set blocking mode\n", id ); 894 trace ( "simple_client (%x) connected\n", id ); 895 896 /* send data to server */ 897 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen ); 898 ok ( n_sent == n_expected, 899 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 900 901 /* shutdown send direction */ 902 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" ); 903 904 /* Receive data echoed back & check it */ 905 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen ); 906 ok ( n_recvd == n_expected, 907 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); 908 909 /* check data */ 910 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks ); 911 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos); 912 913 /* cleanup */ 914 read_zero_bytes ( mem->s ); 915 trace ( "simple_client (%x) exiting\n", id ); 916 client_stop (); 917 } 918 919 /* 920 * oob_client: A very basic client sending out-of-band data. 921 */ 922 static VOID WINAPI oob_client ( client_params *par ) 923 { 924 test_params *gen = par->general; 925 client_memory *mem; 926 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id; 927 928 id = GetCurrentThreadId(); 929 trace ( "oob_client (%x): starting\n", id ); 930 /* wait here because we want to call set_so_opentype before creating a socket */ 931 WaitForSingleObject ( server_ready, INFINITE ); 932 trace ( "oob_client (%x): server ready\n", id ); 933 934 check_so_opentype (); 935 set_so_opentype ( FALSE ); /* non-overlapped */ 936 client_start ( par ); 937 mem = TlsGetValue ( tls ); 938 939 /* Connect */ 940 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ), 941 0 ==, "oob_client (%x): connect error: %d\n" ); 942 ok ( set_blocking ( mem->s, TRUE ) == 0, 943 "oob_client (%x): failed to set blocking mode\n", id ); 944 trace ( "oob_client (%x) connected\n", id ); 945 946 /* send data to server */ 947 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen ); 948 ok ( n_sent == n_expected, 949 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 950 951 /* Receive data echoed back & check it */ 952 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, 0, par->buflen ); 953 ok ( n_recvd == n_expected, 954 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); 955 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks ); 956 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos); 957 958 /* send out-of-band data to server */ 959 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, MSG_OOB, par->buflen ); 960 ok ( n_sent == n_expected, 961 "oob_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 962 963 /* shutdown send direction */ 964 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" ); 965 966 /* cleanup */ 967 read_zero_bytes ( mem->s ); 968 trace ( "oob_client (%x) exiting\n", id ); 969 client_stop (); 970 } 971 972 /* 973 * simple_mixed_client: mixing send and recvfrom 974 */ 975 static VOID WINAPI simple_mixed_client ( client_params *par ) 976 { 977 test_params *gen = par->general; 978 client_memory *mem; 979 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id; 980 int fromLen = sizeof(mem->addr); 981 struct sockaddr test; 982 983 id = GetCurrentThreadId(); 984 trace ( "simple_client (%x): starting\n", id ); 985 /* wait here because we want to call set_so_opentype before creating a socket */ 986 WaitForSingleObject ( server_ready, INFINITE ); 987 trace ( "simple_client (%x): server ready\n", id ); 988 989 check_so_opentype (); 990 set_so_opentype ( FALSE ); /* non-overlapped */ 991 client_start ( par ); 992 mem = TlsGetValue ( tls ); 993 994 /* Connect */ 995 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ), 996 0 ==, "simple_client (%x): connect error: %d\n" ); 997 ok ( set_blocking ( mem->s, TRUE ) == 0, 998 "simple_client (%x): failed to set blocking mode\n", id ); 999 trace ( "simple_client (%x) connected\n", id ); 1000 1001 /* send data to server */ 1002 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, 0, par->buflen ); 1003 ok ( n_sent == n_expected, 1004 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); 1005 1006 /* shutdown send direction */ 1007 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" ); 1008 1009 /* this shouldn't change, since lpFrom, is not updated on 1010 connection oriented sockets - exposed by bug 11640 1011 */ 1012 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0"); 1013 1014 /* Receive data echoed back & check it */ 1015 n_recvd = do_synchronous_recvfrom ( mem->s, 1016 mem->recv_buf, 1017 n_expected, 1018 0, 1019 (struct sockaddr *)&test, 1020 &fromLen, 1021 par->buflen ); 1022 ok ( n_recvd == n_expected, 1023 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); 1024 1025 /* check that lpFrom was not updated */ 1026 ok(0 == 1027 strcmp( 1028 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr), 1029 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n"); 1030 1031 /* check data */ 1032 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks ); 1033 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos); 1034 1035 /* cleanup */ 1036 read_zero_bytes ( mem->s ); 1037 trace ( "simple_client (%x) exiting\n", id ); 1038 client_stop (); 1039 } 1040 1041 /* 1042 * event_client: An event-driven client 1043 */ 1044 static void WINAPI event_client ( client_params *par ) 1045 { 1046 test_params *gen = par->general; 1047 client_memory *mem; 1048 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size, 1049 tmp, err, n; 1050 HANDLE event; 1051 WSANETWORKEVENTS wsa_events; 1052 char *send_last, *recv_last, *send_p, *recv_p; 1053 LONG mask = FD_READ | FD_WRITE | FD_CLOSE; 1054 1055 trace ( "event_client (%x): starting\n", id ); 1056 client_start ( par ); 1057 trace ( "event_client (%x): server ready\n", id ); 1058 1059 mem = TlsGetValue ( tls ); 1060 1061 /* Prepare event notification for connect, makes socket nonblocking */ 1062 event = WSACreateEvent (); 1063 WSAEventSelect ( mem->s, event, FD_CONNECT ); 1064 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ); 1065 if ( tmp != 0 ) { 1066 err = WSAGetLastError (); 1067 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err ); 1068 tmp = WaitForSingleObject ( event, INFINITE ); 1069 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp ); 1070 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events ); 1071 ok ( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err ); 1072 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ]; 1073 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err ); 1074 if ( err ) goto out; 1075 } 1076 1077 trace ( "event_client (%x) connected\n", id ); 1078 1079 WSAEventSelect ( mem->s, event, mask ); 1080 1081 recv_p = mem->recv_buf; 1082 recv_last = mem->recv_buf + n_expected; 1083 send_p = mem->send_buf; 1084 send_last = mem->send_buf + n_expected; 1085 1086 while ( TRUE ) 1087 { 1088 err = WaitForSingleObject ( event, INFINITE ); 1089 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id ); 1090 1091 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events ); 1092 ok( err == 0, "event_client (%x): WSAEnumNetworkEvents error: %d\n", id, err ); 1093 1094 if ( wsa_events.lNetworkEvents & FD_WRITE ) 1095 { 1096 err = wsa_events.iErrorCode[ FD_WRITE_BIT ]; 1097 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err ); 1098 1099 if ( err== 0 ) 1100 do 1101 { 1102 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 ); 1103 if ( n < 0 ) 1104 { 1105 err = WSAGetLastError (); 1106 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err ); 1107 } 1108 else 1109 send_p += n; 1110 } 1111 while ( n >= 0 && send_p < send_last ); 1112 1113 if ( send_p == send_last ) 1114 { 1115 trace ( "event_client (%x): all data sent - shutdown\n", id ); 1116 shutdown ( mem->s, SD_SEND ); 1117 mask &= ~FD_WRITE; 1118 WSAEventSelect ( mem->s, event, mask ); 1119 } 1120 } 1121 if ( wsa_events.lNetworkEvents & FD_READ ) 1122 { 1123 err = wsa_events.iErrorCode[ FD_READ_BIT ]; 1124 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err ); 1125 if ( err != 0 ) break; 1126 1127 /* First read must succeed */ 1128 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 ); 1129 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" ); 1130 1131 while ( n >= 0 ) { 1132 recv_p += n; 1133 if ( recv_p == recv_last ) 1134 { 1135 mask &= ~FD_READ; 1136 trace ( "event_client (%x): all data received\n", id ); 1137 WSAEventSelect ( mem->s, event, mask ); 1138 break; 1139 } 1140 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 ); 1141 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK ) 1142 ok ( 0, "event_client (%x): read error: %d\n", id, err ); 1143 1144 } 1145 } 1146 if ( wsa_events.lNetworkEvents & FD_CLOSE ) 1147 { 1148 trace ( "event_client (%x): close event\n", id ); 1149 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ]; 1150 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err ); 1151 break; 1152 } 1153 } 1154 1155 n = send_p - mem->send_buf; 1156 ok ( send_p == send_last, 1157 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected ); 1158 n = recv_p - mem->recv_buf; 1159 ok ( recv_p == recv_last, 1160 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected ); 1161 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks ); 1162 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n); 1163 1164 out: 1165 WSACloseEvent ( event ); 1166 trace ( "event_client (%x) exiting\n", id ); 1167 client_stop (); 1168 } 1169 1170 /* Tests for WSAStartup */ 1171 static void test_WithoutWSAStartup(void) 1172 { 1173 DWORD err; 1174 1175 WSASetLastError(0xdeadbeef); 1176 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, "WSASocketA should have failed\n"); 1177 err = WSAGetLastError(); 1178 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err); 1179 1180 WSASetLastError(0xdeadbeef); 1181 ok(gethostbyname("localhost") == NULL, "gethostbyname() succeeded unexpectedly\n"); 1182 err = WSAGetLastError(); 1183 ok(err == WSANOTINITIALISED, "Expected 10093, received %d\n", err); 1184 } 1185 1186 static void test_WithWSAStartup(void) 1187 { 1188 WSADATA data; 1189 WORD version = MAKEWORD( 2, 2 ); 1190 INT res, socks, i, j; 1191 SOCKET sock; 1192 LPVOID ptr; 1193 struct 1194 { 1195 SOCKET src, dst, dup_src, dup_dst; 1196 } pairs[32]; 1197 DWORD error; 1198 1199 res = WSAStartup( version, &data ); 1200 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res); 1201 1202 ptr = gethostbyname("localhost"); 1203 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError()); 1204 1205 /* Alloc some sockets to check if they are destroyed on WSACleanup */ 1206 for (socks = 0; socks < sizeof(pairs) / sizeof(pairs[0]); socks++) 1207 { 1208 WSAPROTOCOL_INFOA info; 1209 if (tcp_socketpair(&pairs[socks].src, &pairs[socks].dst)) break; 1210 1211 memset(&info, 0, sizeof(info)); 1212 ok(!WSADuplicateSocketA(pairs[socks].src, GetCurrentProcessId(), &info), 1213 "WSADuplicateSocketA should have worked\n"); 1214 pairs[socks].dup_src = WSASocketA(0, 0, 0, &info, 0, 0); 1215 ok(pairs[socks].dup_src != SOCKET_ERROR, "expected != -1\n"); 1216 1217 memset(&info, 0, sizeof(info)); 1218 ok(!WSADuplicateSocketA(pairs[socks].dst, GetCurrentProcessId(), &info), 1219 "WSADuplicateSocketA should have worked\n"); 1220 pairs[socks].dup_dst = WSASocketA(0, 0, 0, &info, 0, 0); 1221 ok(pairs[socks].dup_dst != SOCKET_ERROR, "expected != -1\n"); 1222 } 1223 1224 res = send(pairs[0].src, "TEST", 4, 0); 1225 ok(res == 4, "send failed with error %d\n", WSAGetLastError()); 1226 1227 WSACleanup(); 1228 1229 res = WSAStartup( version, &data ); 1230 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res); 1231 1232 /* show that sockets are destroyed automatically after WSACleanup */ 1233 SetLastError(0xdeadbeef); 1234 res = send(pairs[0].src, "TEST", 4, 0); 1235 error = WSAGetLastError(); 1236 ok(res == SOCKET_ERROR, "send should have failed\n"); 1237 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error); 1238 1239 SetLastError(0xdeadbeef); 1240 res = send(pairs[0].dst, "TEST", 4, 0); 1241 error = WSAGetLastError(); 1242 ok(res == SOCKET_ERROR, "send should have failed\n"); 1243 todo_wine ok(error == WSAENOTSOCK, "expected 10038, got %d\n", error); 1244 1245 /* Check that all sockets were destroyed */ 1246 for (i = 0; i < socks; i++) 1247 { 1248 for (j = 0; j < 4; j++) 1249 { 1250 struct sockaddr_in saddr; 1251 int size = sizeof(saddr); 1252 switch(j) 1253 { 1254 case 0: sock = pairs[i].src; break; 1255 case 1: sock = pairs[i].dup_src; break; 1256 case 2: sock = pairs[i].dst; break; 1257 case 3: sock = pairs[i].dup_dst; break; 1258 } 1259 1260 SetLastError(0xdeadbeef); 1261 res = getsockname(sock, (struct sockaddr *)&saddr, &size); 1262 error = WSAGetLastError(); 1263 ok(res == SOCKET_ERROR, "Test[%d]: getsockname should have failed\n", i); 1264 todo_wine ok(error == WSAENOTSOCK, "Test[%d]: expected 10038, got %d\n", i, error); 1265 } 1266 } 1267 1268 /* While wine is not fixed, close all sockets manually */ 1269 for (i = 0; i < socks; i++) 1270 { 1271 closesocket(pairs[i].src); 1272 closesocket(pairs[i].dst); 1273 closesocket(pairs[i].dup_src); 1274 closesocket(pairs[i].dup_dst); 1275 } 1276 1277 res = WSACleanup(); 1278 ok(res == 0, "expected 0, got %d\n", res); 1279 WSASetLastError(0xdeadbeef); 1280 res = WSACleanup(); 1281 error = WSAGetLastError(); 1282 ok ( res == SOCKET_ERROR && error == WSANOTINITIALISED, 1283 "WSACleanup returned %d WSAGetLastError is %d\n", res, error); 1284 } 1285 1286 /**************** Main program utility functions ***************/ 1287 1288 static void Init (void) 1289 { 1290 WORD ver = MAKEWORD (2, 2); 1291 WSADATA data; 1292 HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), hiphlpapi, ntdll; 1293 1294 pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo"); 1295 pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo"); 1296 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW"); 1297 pFreeAddrInfoExW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoExW"); 1298 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW"); 1299 pGetAddrInfoExW = (void *)GetProcAddress(hws2_32, "GetAddrInfoExW"); 1300 pGetAddrInfoExOverlappedResult = (void *)GetProcAddress(hws2_32, "GetAddrInfoExOverlappedResult"); 1301 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop"); 1302 pInetNtopW = (void *)GetProcAddress(hws2_32, "InetNtopW"); 1303 pInetPtonA = (void *)GetProcAddress(hws2_32, "inet_pton"); 1304 pInetPtonW = (void *)GetProcAddress(hws2_32, "InetPtonW"); 1305 pWSALookupServiceBeginW = (void *)GetProcAddress(hws2_32, "WSALookupServiceBeginW"); 1306 pWSALookupServiceEnd = (void *)GetProcAddress(hws2_32, "WSALookupServiceEnd"); 1307 pWSALookupServiceNextW = (void *)GetProcAddress(hws2_32, "WSALookupServiceNextW"); 1308 pWSAEnumNameSpaceProvidersA = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersA"); 1309 pWSAEnumNameSpaceProvidersW = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersW"); 1310 pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll"); 1311 1312 hiphlpapi = LoadLibraryA("iphlpapi.dll"); 1313 if (hiphlpapi) 1314 { 1315 pGetIpForwardTable = (void *)GetProcAddress(hiphlpapi, "GetIpForwardTable"); 1316 pGetAdaptersInfo = (void *)GetProcAddress(hiphlpapi, "GetAdaptersInfo"); 1317 } 1318 1319 ntdll = LoadLibraryA("ntdll.dll"); 1320 if (ntdll) 1321 { 1322 pNtClose = (void *)GetProcAddress(ntdll, "NtClose"); 1323 pNtSetInformationFile = (void *)GetProcAddress(ntdll, "NtSetInformationFile"); 1324 pNtQueryInformationFile = (void *)GetProcAddress(ntdll, "NtQueryInformationFile"); 1325 } 1326 1327 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" ); 1328 tls = TlsAlloc(); 1329 } 1330 1331 static void Exit (void) 1332 { 1333 INT ret, err; 1334 TlsFree ( tls ); 1335 ret = WSACleanup(); 1336 err = WSAGetLastError(); 1337 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err); 1338 } 1339 1340 static void StartServer (LPTHREAD_START_ROUTINE routine, 1341 test_params *general, server_params *par) 1342 { 1343 par->general = general; 1344 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] ); 1345 ok ( thread[0] != NULL, "Failed to create server thread\n" ); 1346 } 1347 1348 static void StartClients (LPTHREAD_START_ROUTINE routine, 1349 test_params *general, client_params *par) 1350 { 1351 int i; 1352 par->general = general; 1353 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ ) 1354 { 1355 client_id = i - 1; 1356 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] ); 1357 ok ( thread[i] != NULL, "Failed to create client thread\n" ); 1358 /* Make sure the client is up and running */ 1359 WaitForSingleObject ( client_ready[client_id], INFINITE ); 1360 }; 1361 } 1362 1363 static void do_test( test_setup *test ) 1364 { 1365 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS); 1366 DWORD wait; 1367 1368 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL ); 1369 for (i = 0; i <= n; i++) 1370 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL ); 1371 1372 StartServer ( test->srv, &test->general, &test->srv_params ); 1373 StartClients ( test->clt, &test->general, &test->clt_params ); 1374 WaitForSingleObject ( server_ready, INFINITE ); 1375 1376 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT ); 1377 ok ( wait <= WAIT_OBJECT_0 + n , 1378 "some threads have not completed: %x\n", wait ); 1379 1380 if ( ! ( wait <= WAIT_OBJECT_0 + n ) ) 1381 { 1382 for (i = 0; i <= n; i++) 1383 { 1384 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 ) 1385 { 1386 trace ("terminating thread %08x\n", thread_id[i]); 1387 TerminateThread ( thread [i], 0 ); 1388 } 1389 } 1390 } 1391 CloseHandle ( server_ready ); 1392 for (i = 0; i <= n; i++) 1393 CloseHandle ( client_ready[i] ); 1394 } 1395 1396 /********* some tests for getsockopt(setsockopt(X)) == X ***********/ 1397 /* optname = SO_LINGER */ 1398 static const LINGER linger_testvals[] = { 1399 {0,0}, 1400 {0,73}, 1401 {1,0}, 1402 {5,189} 1403 }; 1404 1405 /* optname = SO_RCVTIMEO, SOSNDTIMEO */ 1406 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a 1407 bug in the linux kernel (fixed in 2.6.8) */ 1408 #define SOCKTIMEOUT2 997000 /* 997 seconds */ 1409 1410 static void test_set_getsockopt(void) 1411 { 1412 SOCKET s, s2; 1413 int i, err, lasterr; 1414 int timeout; 1415 LINGER lingval; 1416 int size; 1417 WSAPROTOCOL_INFOA infoA; 1418 WSAPROTOCOL_INFOW infoW; 1419 char providername[WSAPROTOCOL_LEN + 1]; 1420 DWORD value; 1421 struct _prottest 1422 { 1423 int family, type, proto; 1424 } prottest[] = { 1425 {AF_INET, SOCK_STREAM, IPPROTO_TCP}, 1426 {AF_INET, SOCK_DGRAM, IPPROTO_UDP}, 1427 {AF_INET6, SOCK_STREAM, IPPROTO_TCP}, 1428 {AF_INET6, SOCK_DGRAM, IPPROTO_UDP} 1429 }; 1430 union _csspace 1431 { 1432 CSADDR_INFO cs; 1433 char space[128]; 1434 } csinfoA, csinfoB; 1435 1436 s = socket(AF_INET, SOCK_STREAM, 0); 1437 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError()); 1438 if( s == INVALID_SOCKET) return; 1439 /* SO_RCVTIMEO */ 1440 timeout = SOCKTIMEOUT1; 1441 size = sizeof(timeout); 1442 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size); 1443 if( !err) 1444 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size); 1445 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError()); 1446 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout); 1447 1448 timeout = 0; 1449 size = sizeof(timeout); 1450 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size); 1451 if( !err) 1452 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size); 1453 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError()); 1454 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout); 1455 1456 /* SO_SNDTIMEO */ 1457 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */ 1458 size = sizeof(timeout); 1459 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size); 1460 if( !err) 1461 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size); 1462 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError()); 1463 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout); 1464 1465 /* SO_SNDBUF */ 1466 value = 4096; 1467 size = sizeof(value); 1468 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, size); 1469 ok( !err, "setsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() ); 1470 value = 0xdeadbeef; 1471 err = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&value, &size); 1472 ok( !err, "getsockopt(SO_SNDBUF) failed error: %u\n", WSAGetLastError() ); 1473 ok( value == 4096, "expected 4096, got %u\n", value ); 1474 1475 /* SO_RCVBUF */ 1476 value = 4096; 1477 size = sizeof(value); 1478 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, size); 1479 ok( !err, "setsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() ); 1480 value = 0xdeadbeef; 1481 err = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&value, &size); 1482 ok( !err, "getsockopt(SO_RCVBUF) failed error: %u\n", WSAGetLastError() ); 1483 ok( value == 4096, "expected 4096, got %u\n", value ); 1484 1485 /* SO_LINGER */ 1486 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) { 1487 size = sizeof(lingval); 1488 lingval = linger_testvals[i]; 1489 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size); 1490 if( !err) 1491 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size); 1492 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError()); 1493 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff && 1494 (lingval.l_linger == linger_testvals[i].l_linger || 1495 (!lingval.l_linger && !linger_testvals[i].l_onoff)) 1496 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i, 1497 lingval.l_onoff, lingval.l_linger, 1498 linger_testvals[i].l_onoff, linger_testvals[i].l_linger); 1499 } 1500 1501 size = sizeof(lingval); 1502 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, size); 1503 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 1504 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError()); 1505 err = setsockopt(s, SOL_SOCKET, SO_LINGER, NULL, 0); 1506 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 1507 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError()); 1508 1509 size = sizeof(BOOL); 1510 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, size); 1511 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 1512 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError()); 1513 err = setsockopt(s, SOL_SOCKET, SO_DONTLINGER, NULL, 0); 1514 ok(err == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 1515 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", err, WSAGetLastError()); 1516 1517 /* Test for erroneously passing a value instead of a pointer as optval */ 1518 size = sizeof(char); 1519 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size); 1520 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed " 1521 "instead of failing.\n"); 1522 lasterr = WSAGetLastError(); 1523 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value " 1524 "returned 0x%08x, not WSAEFAULT(0x%08x)\n", 1525 lasterr, WSAEFAULT); 1526 1527 /* SO_RCVTIMEO with invalid values for level */ 1528 size = sizeof(timeout); 1529 timeout = SOCKTIMEOUT1; 1530 SetLastError(0xdeadbeef); 1531 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size); 1532 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 1533 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n", 1534 err, WSAGetLastError()); 1535 1536 timeout = SOCKTIMEOUT1; 1537 SetLastError(0xdeadbeef); 1538 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size); 1539 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 1540 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL)\n", 1541 err, WSAGetLastError()); 1542 1543 /* Test SO_ERROR set/get */ 1544 SetLastError(0xdeadbeef); 1545 i = 1234; 1546 err = setsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, size); 1547 todo_wine 1548 ok( !err && !WSAGetLastError(), 1549 "got %d with %d (expected 0 with 0)\n", 1550 err, WSAGetLastError()); 1551 1552 SetLastError(0xdeadbeef); 1553 i = 4321; 1554 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size); 1555 todo_wine 1556 ok( !err && !WSAGetLastError(), 1557 "got %d with %d (expected 0 with 0)\n", 1558 err, WSAGetLastError()); 1559 todo_wine 1560 ok (i == 1234, "got %d (expected 1234)\n", i); 1561 1562 /* Test invalid optlen */ 1563 SetLastError(0xdeadbeef); 1564 size = 1; 1565 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size); 1566 todo_wine 1567 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEFAULT), 1568 "got %d with %d (expected SOCKET_ERROR with WSAEFAULT)\n", 1569 err, WSAGetLastError()); 1570 1571 closesocket(s); 1572 /* Test with the closed socket */ 1573 SetLastError(0xdeadbeef); 1574 size = sizeof(i); 1575 i = 1234; 1576 err = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &i, &size); 1577 todo_wine 1578 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAENOTSOCK), 1579 "got %d with %d (expected SOCKET_ERROR with WSAENOTSOCK)\n", 1580 err, WSAGetLastError()); 1581 ok (i == 1234, "expected 1234, got %d\n", i); 1582 1583 /* Test WS_IP_MULTICAST_TTL with 8, 16, 24 and 32 bits values */ 1584 s = socket(AF_INET, SOCK_DGRAM, 0); 1585 ok(s != INVALID_SOCKET, "Failed to create socket\n"); 1586 size = sizeof(i); 1587 i = 0x0000000a; 1588 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &i, size); 1589 if (!err) 1590 { 1591 for (i = 0; i < 4; i++) 1592 { 1593 int k, j; 1594 const int tests[] = {0xffffff0a, 0xffff000b, 0xff00000c, 0x0000000d}; 1595 err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &tests[i], i + 1); 1596 ok(!err, "Test [%d] Expected 0, got %d\n", i, err); 1597 err = getsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &k, &size); 1598 ok(!err, "Test [%d] Expected 0, got %d\n", i, err); 1599 j = i != 3 ? tests[i] & ((1 << (i + 1) * 8) - 1) : tests[i]; 1600 ok(k == j, "Test [%d] Expected 0x%x, got 0x%x\n", i, j, k); 1601 } 1602 } 1603 else 1604 win_skip("IP_MULTICAST_TTL is unsupported\n"); 1605 closesocket(s); 1606 1607 /* test SO_PROTOCOL_INFOA invalid parameters */ 1608 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL), 1609 "getsockopt should have failed\n"); 1610 err = WSAGetLastError(); 1611 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err); 1612 size = sizeof(WSAPROTOCOL_INFOA); 1613 ok(getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size), 1614 "getsockopt should have failed\n"); 1615 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size); 1616 err = WSAGetLastError(); 1617 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err); 1618 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 1619 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, NULL), 1620 "getsockopt should have failed\n"); 1621 err = WSAGetLastError(); 1622 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 1623 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, NULL), 1624 "getsockopt should have failed\n"); 1625 err = WSAGetLastError(); 1626 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 1627 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, NULL, &size), 1628 "getsockopt should have failed\n"); 1629 err = WSAGetLastError(); 1630 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 1631 size = sizeof(WSAPROTOCOL_INFOA) / 2; 1632 ok(getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size), 1633 "getsockopt should have failed\n"); 1634 err = WSAGetLastError(); 1635 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 1636 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size); 1637 size = sizeof(WSAPROTOCOL_INFOA) * 2; 1638 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size); 1639 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 1640 ok(size == sizeof(WSAPROTOCOL_INFOA) * 2, "got size %d\n", size); 1641 1642 closesocket(s); 1643 1644 /* test SO_PROTOCOL_INFO structure returned for different protocols */ 1645 for (i = 0; i < sizeof(prottest) / sizeof(prottest[0]); i++) 1646 { 1647 int k; 1648 1649 s = socket(prottest[i].family, prottest[i].type, prottest[i].proto); 1650 if (s == INVALID_SOCKET && prottest[i].family == AF_INET6) continue; 1651 1652 ok(s != INVALID_SOCKET, "Failed to create socket: %d\n", 1653 WSAGetLastError()); 1654 1655 /* compare both A and W version */ 1656 infoA.szProtocol[0] = 0; 1657 size = sizeof(WSAPROTOCOL_INFOA); 1658 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &infoA, &size); 1659 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 1660 ok(size == sizeof(WSAPROTOCOL_INFOA), "got size %d\n", size); 1661 1662 infoW.szProtocol[0] = 0; 1663 size = sizeof(WSAPROTOCOL_INFOW); 1664 err = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, (char *) &infoW, &size); 1665 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 1666 ok(size == sizeof(WSAPROTOCOL_INFOW), "got size %d\n", size); 1667 1668 trace("provider name '%s', family %d, type %d, proto %d\n", 1669 infoA.szProtocol, prottest[i].family, prottest[i].type, prottest[i].proto); 1670 1671 ok(infoA.szProtocol[0], "WSAPROTOCOL_INFOA was not filled\n"); 1672 ok(infoW.szProtocol[0], "WSAPROTOCOL_INFOW was not filled\n"); 1673 1674 WideCharToMultiByte(CP_ACP, 0, infoW.szProtocol, -1, 1675 providername, sizeof(providername), NULL, NULL); 1676 ok(!strcmp(infoA.szProtocol,providername), 1677 "different provider names '%s' != '%s'\n", infoA.szProtocol, providername); 1678 1679 ok(!memcmp(&infoA, &infoW, FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol)), 1680 "SO_PROTOCOL_INFO[A/W] comparison failed\n"); 1681 1682 /* Remove IF when WSAEnumProtocols support IPV6 data */ 1683 todo_wine_if (prottest[i].family == AF_INET6) 1684 ok(infoA.iAddressFamily == prottest[i].family, "socket family invalid, expected %d received %d\n", 1685 prottest[i].family, infoA.iAddressFamily); 1686 ok(infoA.iSocketType == prottest[i].type, "socket type invalid, expected %d received %d\n", 1687 prottest[i].type, infoA.iSocketType); 1688 ok(infoA.iProtocol == prottest[i].proto, "socket protocol invalid, expected %d received %d\n", 1689 prottest[i].proto, infoA.iProtocol); 1690 1691 /* IP_HDRINCL is supported only on SOCK_RAW but passed to SOCK_DGRAM by Impossible Creatures */ 1692 size = sizeof(i); 1693 k = 1; 1694 SetLastError(0xdeadbeef); 1695 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size); 1696 if (err == -1) /* >= Vista */ 1697 { 1698 todo_wine { 1699 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError()); 1700 k = 99; 1701 SetLastError(0xdeadbeef); 1702 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size); 1703 ok(err == -1, "Expected -1, got %d\n", err); 1704 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError()); 1705 ok(k == 99, "Expected 99, got %d\n", k); 1706 1707 size = sizeof(k); 1708 k = 0; 1709 SetLastError(0xdeadbeef); 1710 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size); 1711 } 1712 ok(err == -1, "Expected -1, got %d\n", err); 1713 todo_wine { 1714 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError()); 1715 k = 99; 1716 SetLastError(0xdeadbeef); 1717 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size); 1718 ok(err == -1, "Expected -1, got %d\n", err); 1719 ok(GetLastError() == WSAEINVAL, "Expected 10022, got %d\n", GetLastError()); 1720 ok(k == 99, "Expected 99, got %d\n", k); 1721 } 1722 } 1723 else /* <= 2003 the tests differ between TCP and UDP, UDP silently accepts */ 1724 { 1725 SetLastError(0xdeadbeef); 1726 k = 99; 1727 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size); 1728 if (prottest[i].type == SOCK_DGRAM) 1729 { 1730 ok(err == 0, "Expected 0, got %d\n", err); 1731 ok(k == 1, "Expected 1, got %d\n", k); 1732 } 1733 else 1734 { 1735 /* contratry to what we could expect the function returns error but k is changed */ 1736 ok(err == -1, "Expected -1, got %d\n", err); 1737 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError()); 1738 ok(k == 0, "Expected 0, got %d\n", k); 1739 } 1740 1741 k = 0; 1742 err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, size); 1743 ok(err == 0, "Expected 0, got %d\n", err); 1744 1745 k = 99; 1746 err = getsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *) &k, &size); 1747 if (prottest[i].type == SOCK_DGRAM) 1748 { 1749 ok(err == 0, "Expected 0, got %d\n", err); 1750 ok(k == 0, "Expected 0, got %d\n", k); 1751 } 1752 else 1753 { 1754 /* contratry to what we could expect the function returns error but k is changed */ 1755 ok(err == -1, "Expected -1, got %d\n", err); 1756 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError()); 1757 ok(k == 0, "Expected 0, got %d\n", k); 1758 } 1759 } 1760 1761 closesocket(s); 1762 } 1763 1764 /* Test SO_BSP_STATE - Present only in >= Win 2008 */ 1765 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 1766 ok(s != INVALID_SOCKET, "Failed to create socket\n"); 1767 s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 1768 ok(s2 != INVALID_SOCKET, "Failed to create socket\n"); 1769 1770 SetLastError(0xdeadbeef); 1771 size = sizeof(csinfoA); 1772 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1773 if (!err) 1774 { 1775 struct sockaddr_in saddr; 1776 memset(&saddr, 0, sizeof(saddr)); 1777 saddr.sin_family = AF_INET; 1778 saddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 1779 1780 /* Socket is not bound, no information provided */ 1781 ok(!csinfoA.cs.LocalAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.LocalAddr.iSockaddrLength); 1782 ok(csinfoA.cs.LocalAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.LocalAddr.lpSockaddr); 1783 /* Socket is not connected, no information provided */ 1784 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength); 1785 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr); 1786 1787 err = bind(s, (struct sockaddr*)&saddr, sizeof(saddr)); 1788 ok(!err, "Expected 0, got %d\n", err); 1789 size = sizeof(csinfoA); 1790 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1791 ok(!err, "Expected 0, got %d\n", err); 1792 1793 /* Socket is bound */ 1794 ok(csinfoA.cs.LocalAddr.iSockaddrLength, "Expected non-zero\n"); 1795 ok(csinfoA.cs.LocalAddr.lpSockaddr != NULL, "Expected non-null\n"); 1796 /* Socket is not connected, no information provided */ 1797 ok(!csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected 0, got %d\n", csinfoA.cs.RemoteAddr.iSockaddrLength); 1798 ok(csinfoA.cs.RemoteAddr.lpSockaddr == NULL, "Expected NULL, got %p\n", csinfoA.cs.RemoteAddr.lpSockaddr); 1799 1800 err = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr)); 1801 ok(!err, "Expected 0, got %d\n", err); 1802 err = getsockname(s2, (struct sockaddr *)&saddr, &size); 1803 ok(!err, "Expected 0, got %d\n", err); 1804 err = listen(s2, 1); 1805 ok(!err, "Expected 0, got %d\n", err); 1806 err = connect(s, (struct sockaddr*)&saddr, sizeof(saddr)); 1807 ok(!err, "Expected 0, got %d\n", err); 1808 size = sizeof(saddr); 1809 err = accept(s2, (struct sockaddr*)&saddr, &size); 1810 ok(err != INVALID_SOCKET, "Failed to accept socket\n"); 1811 closesocket(s2); 1812 s2 = err; 1813 1814 size = sizeof(csinfoA); 1815 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1816 ok(!err, "Expected 0, got %d\n", err); 1817 err = getsockopt(s2, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoB, &size); 1818 ok(!err, "Expected 0, got %d\n", err); 1819 ok(size == sizeof(csinfoA), "Got %d\n", size); 1820 size = sizeof(saddr); 1821 ok(size == csinfoA.cs.LocalAddr.iSockaddrLength, "Expected %d, got %d\n", size, 1822 csinfoA.cs.LocalAddr.iSockaddrLength); 1823 ok(size == csinfoA.cs.RemoteAddr.iSockaddrLength, "Expected %d, got %d\n", size, 1824 csinfoA.cs.RemoteAddr.iSockaddrLength); 1825 ok(!memcmp(csinfoA.cs.LocalAddr.lpSockaddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), 1826 "Expected matching addresses\n"); 1827 ok(!memcmp(csinfoB.cs.LocalAddr.lpSockaddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), 1828 "Expected matching addresses\n"); 1829 ok(csinfoA.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n"); 1830 ok(csinfoB.cs.iSocketType == SOCK_STREAM, "Wrong socket type\n"); 1831 ok(csinfoA.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n"); 1832 ok(csinfoB.cs.iProtocol == IPPROTO_TCP, "Wrong socket protocol\n"); 1833 1834 err = getpeername(s, (struct sockaddr *)&saddr, &size); 1835 ok(!err, "Expected 0, got %d\n", err); 1836 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n"); 1837 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n"); 1838 err = getpeername(s2, (struct sockaddr *)&saddr, &size); 1839 ok(!err, "Expected 0, got %d\n", err); 1840 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n"); 1841 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n"); 1842 err = getsockname(s, (struct sockaddr *)&saddr, &size); 1843 ok(!err, "Expected 0, got %d\n", err); 1844 ok(!memcmp(&saddr, csinfoA.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n"); 1845 ok(!memcmp(&saddr, csinfoB.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n"); 1846 err = getsockname(s2, (struct sockaddr *)&saddr, &size); 1847 ok(!err, "Expected 0, got %d\n", err); 1848 ok(!memcmp(&saddr, csinfoB.cs.LocalAddr.lpSockaddr, size), "Expected matching addresses\n"); 1849 ok(!memcmp(&saddr, csinfoA.cs.RemoteAddr.lpSockaddr, size), "Expected matching addresses\n"); 1850 1851 SetLastError(0xdeadbeef); 1852 size = sizeof(CSADDR_INFO); 1853 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1854 ok(err, "Expected non-zero\n"); 1855 ok(size == sizeof(CSADDR_INFO), "Got %d\n", size); 1856 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError()); 1857 1858 /* At least for IPv4 the size is exactly 56 bytes */ 1859 size = sizeof(*csinfoA.cs.LocalAddr.lpSockaddr) * 2 + sizeof(csinfoA.cs); 1860 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1861 ok(!err, "Expected 0, got %d\n", err); 1862 size--; 1863 SetLastError(0xdeadbeef); 1864 err = getsockopt(s, SOL_SOCKET, SO_BSP_STATE, (char *) &csinfoA, &size); 1865 ok(err, "Expected non-zero\n"); 1866 ok(GetLastError() == WSAEFAULT, "Expected 10014, got %d\n", GetLastError()); 1867 } 1868 else 1869 ok(GetLastError() == WSAENOPROTOOPT, "Expected 10042, got %d\n", GetLastError()); 1870 1871 closesocket(s); 1872 closesocket(s2); 1873 1874 for (i = 0; i < 2; i++) 1875 { 1876 int family, level; 1877 1878 if (i) 1879 { 1880 family = AF_INET6; 1881 level = IPPROTO_IPV6; 1882 } 1883 else 1884 { 1885 family = AF_INET; 1886 level = IPPROTO_IP; 1887 } 1888 1889 s = socket(family, SOCK_DGRAM, 0); 1890 if (s == INVALID_SOCKET && i) 1891 { 1892 skip("IPv6 is not supported\n"); 1893 break; 1894 } 1895 ok(s != INVALID_SOCKET, "socket failed with error %d\n", GetLastError()); 1896 1897 size = sizeof(value); 1898 value = 0xdead; 1899 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size); 1900 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError()); 1901 ok(value == 0, "Expected 0, got %d\n", value); 1902 1903 size = sizeof(value); 1904 value = 1; 1905 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size); 1906 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError()); 1907 1908 value = 0xdead; 1909 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size); 1910 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError()); 1911 ok(value == 1, "Expected 1, got %d\n", value); 1912 1913 size = sizeof(value); 1914 value = 0xdead; 1915 err = setsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, size); 1916 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError()); 1917 1918 err = getsockopt(s, level, IP_DONTFRAGMENT, (char *) &value, &size); 1919 ok(!err, "Expected 0, got %d with error %d\n", err, GetLastError()); 1920 ok(value == 1, "Expected 1, got %d\n", value); 1921 1922 closesocket(s); 1923 } 1924 } 1925 1926 static void test_so_reuseaddr(void) 1927 { 1928 struct sockaddr_in saddr; 1929 SOCKET s1,s2; 1930 unsigned int rc,reuse; 1931 int size; 1932 DWORD err; 1933 1934 saddr.sin_family = AF_INET; 1935 saddr.sin_port = htons(SERVERPORT+1); 1936 saddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 1937 1938 s1=socket(AF_INET, SOCK_STREAM, 0); 1939 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError()); 1940 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr)); 1941 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError()); 1942 1943 s2=socket(AF_INET, SOCK_STREAM, 0); 1944 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError()); 1945 1946 reuse=0x1234; 1947 size=sizeof(reuse); 1948 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size ); 1949 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse); 1950 1951 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr)); 1952 ok(rc==SOCKET_ERROR, "bind() succeeded\n"); 1953 1954 reuse = 1; 1955 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)); 1956 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError()); 1957 1958 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to 1959 * a port immediately after closing another socket on that port, so 1960 * basically following the BSD socket semantics here. */ 1961 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr)); 1962 if(rc==0) 1963 { 1964 int s3=socket(AF_INET, SOCK_STREAM, 0), s4; 1965 trace("<= Win XP behavior of SO_REUSEADDR\n"); 1966 1967 /* If we could bind again in the same port this is Windows version <= XP. 1968 * Lets test if we can really connect to one of them. */ 1969 set_blocking(s1, FALSE); 1970 set_blocking(s2, FALSE); 1971 rc = listen(s1, 1); 1972 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError()); 1973 rc = listen(s2, 1); 1974 ok(!rc, "listen() failed with error: %d\n", WSAGetLastError()); 1975 rc = connect(s3, (struct sockaddr*)&saddr, sizeof(saddr)); 1976 ok(!rc, "connecting to accepting socket failed %d\n", WSAGetLastError()); 1977 1978 /* the delivery of the connection is random so we need to try on both sockets */ 1979 size = sizeof(saddr); 1980 s4 = accept(s1, (struct sockaddr*)&saddr, &size); 1981 if(s4 == INVALID_SOCKET) 1982 s4 = accept(s2, (struct sockaddr*)&saddr, &size); 1983 ok(s4 != INVALID_SOCKET, "none of the listening sockets could get the connection\n"); 1984 1985 closesocket(s1); 1986 closesocket(s3); 1987 closesocket(s4); 1988 } 1989 else 1990 { 1991 trace(">= Win 2003 behavior of SO_REUSEADDR\n"); 1992 err = WSAGetLastError(); 1993 ok(err==WSAEACCES, "expected 10013, got %d\n", err); 1994 1995 closesocket(s1); 1996 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr)); 1997 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError()); 1998 } 1999 2000 closesocket(s2); 2001 } 2002 2003 #define IP_PKTINFO_LEN (sizeof(WSACMSGHDR) + WSA_CMSG_ALIGN(sizeof(struct in_pktinfo))) 2004 2005 static void test_ip_pktinfo(void) 2006 { 2007 ULONG addresses[2] = {inet_addr("127.0.0.1"), htonl(INADDR_ANY)}; 2008 char recvbuf[10], pktbuf[512], msg[] = "HELLO"; 2009 struct sockaddr_in s1addr, s2addr, s3addr; 2010 GUID WSARecvMsg_GUID = WSAID_WSARECVMSG; 2011 LPFN_WSARECVMSG pWSARecvMsg = NULL; 2012 unsigned int rc, yes = 1; 2013 BOOL foundhdr; 2014 DWORD dwBytes, dwSize, dwFlags; 2015 socklen_t addrlen; 2016 WSACMSGHDR *cmsg; 2017 WSAOVERLAPPED ov; 2018 WSABUF iovec[1]; 2019 SOCKET s1, s2; 2020 WSAMSG hdr; 2021 int i, err; 2022 2023 memset(&ov, 0, sizeof(ov)); 2024 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 2025 if (ov.hEvent == INVALID_HANDLE_VALUE) 2026 { 2027 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError()); 2028 return; 2029 } 2030 2031 memset(&hdr, 0x00, sizeof(hdr)); 2032 s1addr.sin_family = AF_INET; 2033 s1addr.sin_port = htons(0); 2034 /* Note: s1addr.sin_addr is set below */ 2035 iovec[0].buf = recvbuf; 2036 iovec[0].len = sizeof(recvbuf); 2037 hdr.name = (struct sockaddr*)&s3addr; 2038 hdr.namelen = sizeof(s3addr); 2039 hdr.lpBuffers = &iovec[0]; 2040 hdr.dwBufferCount = 1; 2041 hdr.Control.buf = pktbuf; 2042 /* Note: hdr.Control.len is set below */ 2043 hdr.dwFlags = 0; 2044 2045 for (i=0;i<sizeof(addresses)/sizeof(UINT32);i++) 2046 { 2047 s1addr.sin_addr.s_addr = addresses[i]; 2048 2049 /* Build "server" side socket */ 2050 s1=socket(AF_INET, SOCK_DGRAM, 0); 2051 if (s1 == INVALID_SOCKET) 2052 { 2053 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError()); 2054 goto cleanup; 2055 } 2056 2057 /* Obtain the WSARecvMsg function */ 2058 WSAIoctl(s1, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID), 2059 &pWSARecvMsg, sizeof(pWSARecvMsg), &dwBytes, NULL, NULL); 2060 if (!pWSARecvMsg) 2061 { 2062 win_skip("WSARecvMsg is unsupported, some tests will be skipped.\n"); 2063 closesocket(s1); 2064 goto cleanup; 2065 } 2066 2067 /* Setup the server side socket */ 2068 rc=bind(s1, (struct sockaddr*)&s1addr, sizeof(s1addr)); 2069 ok(rc != SOCKET_ERROR, "bind() failed error: %d\n", WSAGetLastError()); 2070 rc=setsockopt(s1, IPPROTO_IP, IP_PKTINFO, (const char*)&yes, sizeof(yes)); 2071 ok(rc == 0, "failed to set IPPROTO_IP flag IP_PKTINFO!\n"); 2072 2073 /* Build "client" side socket */ 2074 addrlen = sizeof(s2addr); 2075 if (getsockname(s1, (struct sockaddr *) &s2addr, &addrlen) != 0) 2076 { 2077 skip("Failed to call getsockname, some tests skipped: %d\n", WSAGetLastError()); 2078 closesocket(s1); 2079 goto cleanup; 2080 } 2081 s2addr.sin_addr.s_addr = addresses[0]; /* Always target the local adapter address */ 2082 s2=socket(AF_INET, SOCK_DGRAM, 0); 2083 if (s2 == INVALID_SOCKET) 2084 { 2085 skip("socket() failed error, some tests skipped: %d\n", WSAGetLastError()); 2086 closesocket(s1); 2087 goto cleanup; 2088 } 2089 2090 /* Test an empty message header */ 2091 rc=pWSARecvMsg(s1, NULL, NULL, NULL, NULL); 2092 err=WSAGetLastError(); 2093 ok(rc == SOCKET_ERROR && err == WSAEFAULT, "WSARecvMsg() failed error: %d (ret = %d)\n", err, rc); 2094 2095 /* 2096 * Send a packet from the client to the server and test for specifying 2097 * a short control header. 2098 */ 2099 SetLastError(0xdeadbeef); 2100 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr)); 2101 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError()); 2102 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 2103 hdr.Control.len = 1; 2104 rc=pWSARecvMsg(s1, &hdr, &dwSize, NULL, NULL); 2105 err=WSAGetLastError(); 2106 ok(rc == SOCKET_ERROR && err == WSAEMSGSIZE && (hdr.dwFlags & MSG_CTRUNC), 2107 "WSARecvMsg() failed error: %d (ret: %d, flags: %d)\n", err, rc, hdr.dwFlags); 2108 hdr.dwFlags = 0; /* Reset flags */ 2109 2110 /* Perform another short control header test, this time with an overlapped receive */ 2111 hdr.Control.len = 1; 2112 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL); 2113 err=WSAGetLastError(); 2114 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err); 2115 SetLastError(0xdeadbeef); 2116 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr)); 2117 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError()); 2118 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 2119 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0) 2120 { 2121 skip("Server side did not receive packet, some tests skipped.\n"); 2122 closesocket(s2); 2123 closesocket(s1); 2124 continue; 2125 } 2126 dwFlags = 0; 2127 WSAGetOverlappedResult(s1, &ov, NULL, FALSE, &dwFlags); 2128 ok(dwFlags == 0, 2129 "WSAGetOverlappedResult() returned unexpected flags %d!\n", dwFlags); 2130 ok(hdr.dwFlags == MSG_CTRUNC, 2131 "WSARecvMsg() overlapped operation set unexpected flags %d.\n", hdr.dwFlags); 2132 hdr.dwFlags = 0; /* Reset flags */ 2133 2134 /* 2135 * Setup an overlapped receive, send a packet, then wait for the packet to be retrieved 2136 * on the server end and check that the returned packet matches what was sent. 2137 */ 2138 hdr.Control.len = sizeof(pktbuf); 2139 rc=pWSARecvMsg(s1, &hdr, NULL, &ov, NULL); 2140 err=WSAGetLastError(); 2141 ok(rc != 0 && err == WSA_IO_PENDING, "WSARecvMsg() failed error: %d\n", err); 2142 ok(hdr.Control.len == sizeof(pktbuf), 2143 "WSARecvMsg() control length mismatch (%d != sizeof pktbuf).\n", hdr.Control.len); 2144 rc=sendto(s2, msg, sizeof(msg), 0, (struct sockaddr*)&s2addr, sizeof(s2addr)); 2145 ok(rc == sizeof(msg), "sendto() failed error: %d\n", WSAGetLastError()); 2146 if (WaitForSingleObject(ov.hEvent, 100) != WAIT_OBJECT_0) 2147 { 2148 skip("Server side did not receive packet, some tests skipped.\n"); 2149 closesocket(s2); 2150 closesocket(s1); 2151 continue; 2152 } 2153 dwSize = 0; 2154 WSAGetOverlappedResult(s1, &ov, &dwSize, FALSE, NULL); 2155 ok(dwSize == sizeof(msg), 2156 "WSARecvMsg() buffer length does not match transmitted data!\n"); 2157 ok(strncmp(iovec[0].buf, msg, sizeof(msg)) == 0, 2158 "WSARecvMsg() buffer does not match transmitted data!\n"); 2159 ok(hdr.Control.len == IP_PKTINFO_LEN, 2160 "WSARecvMsg() control length mismatch (%d).\n", hdr.Control.len); 2161 2162 /* Test for the expected IP_PKTINFO return information. */ 2163 foundhdr = FALSE; 2164 for (cmsg = WSA_CMSG_FIRSTHDR(&hdr); cmsg != NULL; cmsg = WSA_CMSG_NXTHDR(&hdr, cmsg)) 2165 { 2166 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) 2167 { 2168 struct in_pktinfo *pi = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg); 2169 2170 ok(pi->ipi_addr.s_addr == s2addr.sin_addr.s_addr, "destination ip mismatch!\n"); 2171 foundhdr = TRUE; 2172 } 2173 } 2174 ok(foundhdr, "IP_PKTINFO header information was not returned!\n"); 2175 2176 closesocket(s2); 2177 closesocket(s1); 2178 } 2179 2180 cleanup: 2181 CloseHandle(ov.hEvent); 2182 } 2183 2184 /************* Array containing the tests to run **********/ 2185 2186 #define STD_STREAM_SOCKET \ 2187 SOCK_STREAM, \ 2188 0, \ 2189 SERVERIP, \ 2190 SERVERPORT 2191 2192 static test_setup tests [] = 2193 { 2194 /* Test 0: synchronous client and server */ 2195 { 2196 { 2197 STD_STREAM_SOCKET, 2198 2048, 2199 16, 2200 2 2201 }, 2202 simple_server, 2203 { 2204 NULL, 2205 0, 2206 64 2207 }, 2208 simple_client, 2209 { 2210 NULL, 2211 0, 2212 128 2213 } 2214 }, 2215 /* Test 1: event-driven client, synchronous server */ 2216 { 2217 { 2218 STD_STREAM_SOCKET, 2219 2048, 2220 16, 2221 2 2222 }, 2223 simple_server, 2224 { 2225 NULL, 2226 0, 2227 64 2228 }, 2229 event_client, 2230 { 2231 NULL, 2232 WSA_FLAG_OVERLAPPED, 2233 128 2234 } 2235 }, 2236 /* Test 2: synchronous client, non-blocking server via select() */ 2237 { 2238 { 2239 STD_STREAM_SOCKET, 2240 2048, 2241 16, 2242 2 2243 }, 2244 select_server, 2245 { 2246 NULL, 2247 0, 2248 64 2249 }, 2250 simple_client, 2251 { 2252 NULL, 2253 0, 2254 128 2255 } 2256 }, 2257 /* Test 3: OOB client, OOB server */ 2258 { 2259 { 2260 STD_STREAM_SOCKET, 2261 128, 2262 16, 2263 1 2264 }, 2265 oob_server, 2266 { 2267 NULL, 2268 0, 2269 128 2270 }, 2271 oob_client, 2272 { 2273 NULL, 2274 0, 2275 128 2276 } 2277 }, 2278 /* Test 4: synchronous mixed client and server */ 2279 { 2280 { 2281 STD_STREAM_SOCKET, 2282 2048, 2283 16, 2284 2 2285 }, 2286 simple_server, 2287 { 2288 NULL, 2289 0, 2290 64 2291 }, 2292 simple_mixed_client, 2293 { 2294 NULL, 2295 0, 2296 128 2297 } 2298 } 2299 }; 2300 2301 static void test_UDP(void) 2302 { 2303 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is 2304 possible that this test fails due to dropped packets. */ 2305 2306 /* peer 0 receives data from all other peers */ 2307 struct sock_info peer[NUM_UDP_PEERS]; 2308 char buf[16]; 2309 int ss, i, n_recv, n_sent; 2310 2311 memset (buf,0,sizeof(buf)); 2312 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) { 2313 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" ); 2314 2315 peer[i].addr.sin_family = AF_INET; 2316 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP ); 2317 2318 if ( i == 0 ) { 2319 peer[i].addr.sin_port = htons ( SERVERPORT ); 2320 } else { 2321 peer[i].addr.sin_port = htons ( 0 ); 2322 } 2323 2324 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) ); 2325 2326 /* test getsockname() to get peer's port */ 2327 ss = sizeof ( peer[i].addr ); 2328 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" ); 2329 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" ); 2330 } 2331 2332 /* test getsockname() */ 2333 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" ); 2334 2335 for ( i = 1; i < NUM_UDP_PEERS; i++ ) { 2336 /* send client's ip */ 2337 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) ); 2338 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) ); 2339 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent ); 2340 } 2341 2342 for ( i = 1; i < NUM_UDP_PEERS; i++ ) { 2343 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss ); 2344 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv ); 2345 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" ); 2346 } 2347 } 2348 2349 static DWORD WINAPI do_getservbyname( void *param ) 2350 { 2351 struct { 2352 const char *name; 2353 const char *proto; 2354 int port; 2355 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} }; 2356 2357 HANDLE *starttest = param; 2358 int i, j; 2359 struct servent *pserv[2]; 2360 2361 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, 2362 "test_getservbyname: timeout waiting for start signal\n" ); 2363 2364 /* ensure that necessary buffer resizes are completed */ 2365 for ( j = 0; j < 2; j++) { 2366 pserv[j] = getservbyname ( serv[j].name, serv[j].proto ); 2367 } 2368 2369 for ( i = 0; i < NUM_QUERIES / 2; i++ ) { 2370 for ( j = 0; j < 2; j++ ) { 2371 pserv[j] = getservbyname ( serv[j].name, serv[j].proto ); 2372 ok ( pserv[j] != NULL || broken(pserv[j] == NULL) /* win8, fixed in win81 */, 2373 "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() ); 2374 if ( !pserv[j] ) continue; 2375 ok ( pserv[j]->s_port == htons(serv[j].port), 2376 "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) ); 2377 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ), 2378 "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto ); 2379 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ), 2380 "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name ); 2381 } 2382 2383 ok ( pserv[0] == pserv[1] || broken(pserv[0] != pserv[1]) /* win8, fixed in win81 */, 2384 "getservbyname: winsock resized servent buffer when not necessary\n" ); 2385 } 2386 2387 return 0; 2388 } 2389 2390 static void test_getservbyname(void) 2391 { 2392 int i; 2393 HANDLE starttest, thread[NUM_THREADS]; 2394 DWORD thread_id[NUM_THREADS]; 2395 2396 starttest = CreateEventA ( NULL, 1, 0, "test_getservbyname_starttest" ); 2397 2398 /* create threads */ 2399 for ( i = 0; i < NUM_THREADS; i++ ) { 2400 thread[i] = CreateThread ( NULL, 0, do_getservbyname, &starttest, 0, &thread_id[i] ); 2401 } 2402 2403 /* signal threads to start */ 2404 SetEvent ( starttest ); 2405 2406 for ( i = 0; i < NUM_THREADS; i++) { 2407 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 ); 2408 } 2409 } 2410 2411 static void test_WSASocket(void) 2412 { 2413 SOCKET sock = INVALID_SOCKET; 2414 WSAPROTOCOL_INFOA *pi; 2415 int wsaproviders[] = {IPPROTO_TCP, IPPROTO_IP}; 2416 int autoprotocols[] = {IPPROTO_TCP, IPPROTO_UDP}; 2417 int items, err, size, socktype, i, j; 2418 UINT pi_size; 2419 2420 SetLastError(0xdeadbeef); 2421 ok(WSASocketA(0, 0, 0, NULL, 0, 0) == INVALID_SOCKET, 2422 "WSASocketA should have failed\n"); 2423 err = WSAGetLastError(); 2424 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err); 2425 2426 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0); 2427 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2428 closesocket(sock); 2429 2430 sock = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0); 2431 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2432 closesocket(sock); 2433 2434 SetLastError(0xdeadbeef); 2435 ok(WSASocketA(0, SOCK_STREAM, -1, NULL, 0, 0) == INVALID_SOCKET, 2436 "WSASocketA should have failed\n"); 2437 err = WSAGetLastError(); 2438 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2439 2440 SetLastError(0xdeadbeef); 2441 ok(WSASocketA(0, -1, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET, 2442 "WSASocketA should have failed\n"); 2443 err = WSAGetLastError(); 2444 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err); 2445 2446 SetLastError(0xdeadbeef); 2447 ok(WSASocketA(0, -1, 0, NULL, 0, 0) == INVALID_SOCKET, 2448 "WSASocketA should have failed\n"); 2449 err = WSAGetLastError(); 2450 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err); 2451 2452 SetLastError(0xdeadbeef); 2453 ok(WSASocketA(AF_INET, -1, 0, NULL, 0, 0) == INVALID_SOCKET, 2454 "WSASocketA should have failed\n"); 2455 err = WSAGetLastError(); 2456 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err); 2457 2458 SetLastError(0xdeadbeef); 2459 ok(WSASocketA(AF_INET, 0, -1, NULL, 0, 0) == INVALID_SOCKET, 2460 "WSASocketA should have failed\n"); 2461 err = WSAGetLastError(); 2462 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2463 2464 SetLastError(0xdeadbeef); 2465 ok(WSASocketA(0, -1, -1, NULL, 0, 0) == INVALID_SOCKET, 2466 "WSASocketA should have failed\n"); 2467 err = WSAGetLastError(); 2468 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err); 2469 2470 SetLastError(0xdeadbeef); 2471 ok(WSASocketA(-1, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET, 2472 "WSASocketA should have failed\n"); 2473 err = WSAGetLastError(); 2474 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err); 2475 2476 sock = WSASocketA(AF_INET, 0, IPPROTO_TCP, NULL, 0, 0); 2477 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2478 closesocket(sock); 2479 2480 SetLastError(0xdeadbeef); 2481 ok(WSASocketA(0, SOCK_STREAM, 0, NULL, 0, 0) == INVALID_SOCKET, 2482 "WSASocketA should have failed\n"); 2483 err = WSAGetLastError(); 2484 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err); 2485 2486 SetLastError(0xdeadbeef); 2487 ok(WSASocketA(0, 0, 0xdead, NULL, 0, 0) == INVALID_SOCKET, 2488 "WSASocketA should have failed\n"); 2489 err = WSAGetLastError(); 2490 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2491 2492 SetLastError(0xdeadbeef); 2493 ok(WSASocketA(AF_INET, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET, 2494 "WSASocketA should have failed\n"); 2495 err = WSAGetLastError(); 2496 ok(err == WSAESOCKTNOSUPPORT, "Expected 10044, received %d\n", err); 2497 2498 SetLastError(0xdeadbeef); 2499 ok(WSASocketA(0, 0xdead, 0, NULL, 0, 0) == INVALID_SOCKET, 2500 "WSASocketA should have failed\n"); 2501 err = WSAGetLastError(); 2502 ok(err == WSAEINVAL, "Expected 10022, received %d\n", err); 2503 2504 sock = WSASocketA(0, 0, IPPROTO_TCP, NULL, 0, 0); 2505 ok(sock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2506 closesocket(sock); 2507 2508 /* SOCK_STREAM does not support IPPROTO_UDP */ 2509 SetLastError(0xdeadbeef); 2510 ok(WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_UDP, NULL, 0, 0) == INVALID_SOCKET, 2511 "WSASocketA should have failed\n"); 2512 err = WSAGetLastError(); 2513 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2514 2515 /* SOCK_DGRAM does not support IPPROTO_TCP */ 2516 SetLastError(0xdeadbeef); 2517 ok(WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_TCP, NULL, 0, 0) == INVALID_SOCKET, 2518 "WSASocketA should have failed\n"); 2519 err = WSAGetLastError(); 2520 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2521 2522 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA) 2523 * to avoid a crash on win98. 2524 */ 2525 pi_size = 0; 2526 items = WSAEnumProtocolsA(wsaproviders, NULL, &pi_size); 2527 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n", 2528 items); 2529 err = WSAGetLastError(); 2530 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n", 2531 err, WSAENOBUFS); 2532 2533 pi = HeapAlloc(GetProcessHeap(), 0, pi_size); 2534 ok(pi != NULL, "Failed to allocate memory\n"); 2535 if (pi == NULL) { 2536 skip("Can't continue without memory.\n"); 2537 return; 2538 } 2539 2540 items = WSAEnumProtocolsA(wsaproviders, pi, &pi_size); 2541 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n", 2542 WSAGetLastError()); 2543 2544 if (items == 0) { 2545 skip("No protocols enumerated.\n"); 2546 HeapFree(GetProcessHeap(), 0, pi); 2547 return; 2548 } 2549 2550 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, 2551 FROM_PROTOCOL_INFO, &pi[0], 0, 0); 2552 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2553 WSAGetLastError()); 2554 closesocket(sock); 2555 2556 /* find what parameters are used first: plain parameters or protocol info struct */ 2557 pi[0].iProtocol = -1; 2558 pi[0].iSocketType = -1; 2559 pi[0].iAddressFamily = -1; 2560 ok(WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0) == INVALID_SOCKET, 2561 "WSASocketA should have failed\n"); 2562 err = WSAGetLastError(); 2563 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err); 2564 2565 pi[0].iProtocol = 0; 2566 pi[0].iSocketType = 0; 2567 pi[0].iAddressFamily = 0; 2568 sock = WSASocketA(0, 0, IPPROTO_UDP, &pi[0], 0, 0); 2569 if(sock != INVALID_SOCKET) 2570 { 2571 win_skip("must work only in OS <= 2003\n"); 2572 closesocket(sock); 2573 } 2574 else 2575 { 2576 err = WSAGetLastError(); 2577 ok(err == WSAEAFNOSUPPORT, "Expected 10047, received %d\n", err); 2578 } 2579 2580 pi[0].iProtocol = IPPROTO_UDP; 2581 pi[0].iSocketType = SOCK_DGRAM; 2582 pi[0].iAddressFamily = AF_INET; 2583 sock = WSASocketA(0, 0, 0, &pi[0], 0, 0); 2584 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2585 WSAGetLastError()); 2586 size = sizeof(socktype); 2587 socktype = 0xdead; 2588 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2589 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 2590 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n", 2591 SOCK_DGRAM, socktype); 2592 closesocket(sock); 2593 2594 sock = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi[0], 0, 0); 2595 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2596 WSAGetLastError()); 2597 size = sizeof(socktype); 2598 socktype = 0xdead; 2599 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2600 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 2601 ok(socktype == SOCK_STREAM, "Wrong socket type, expected %d received %d\n", 2602 SOCK_STREAM, socktype); 2603 closesocket(sock); 2604 2605 HeapFree(GetProcessHeap(), 0, pi); 2606 2607 pi_size = 0; 2608 items = WSAEnumProtocolsA(NULL, NULL, &pi_size); 2609 ok(items == SOCKET_ERROR, "WSAEnumProtocolsA(NULL, NULL, 0) returned %d\n", 2610 items); 2611 err = WSAGetLastError(); 2612 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n", 2613 err, WSAENOBUFS); 2614 2615 pi = HeapAlloc(GetProcessHeap(), 0, pi_size); 2616 ok(pi != NULL, "Failed to allocate memory\n"); 2617 if (pi == NULL) { 2618 skip("Can't continue without memory.\n"); 2619 return; 2620 } 2621 2622 items = WSAEnumProtocolsA(NULL, pi, &pi_size); 2623 ok(items != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n", 2624 WSAGetLastError()); 2625 2626 /* when no protocol and socket type are specified the first entry 2627 * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO 2628 * is returned */ 2629 sock = WSASocketA(AF_INET, 0, 0, NULL, 0, 0); 2630 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2631 WSAGetLastError()); 2632 2633 size = sizeof(socktype); 2634 socktype = 0xdead; 2635 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2636 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2637 for(i = 0; i < items; i++) 2638 { 2639 if(pi[i].dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO) 2640 { 2641 ok(socktype == pi[i].iSocketType, "Wrong socket type, expected %d received %d\n", 2642 pi[i].iSocketType, socktype); 2643 break; 2644 } 2645 } 2646 ok(i != items, "Creating a socket without protocol and socket type didn't work\n"); 2647 closesocket(sock); 2648 2649 /* when no socket type is specified the first entry from WSAEnumProtocols 2650 * that matches the protocol is returned */ 2651 for (i = 0; i < sizeof(autoprotocols) / sizeof(autoprotocols[0]); i++) 2652 { 2653 sock = WSASocketA(0, 0, autoprotocols[i], NULL, 0, 0); 2654 ok(sock != INVALID_SOCKET, "Failed to create socket for protocol %d, received %d\n", 2655 autoprotocols[i], WSAGetLastError()); 2656 2657 size = sizeof(socktype); 2658 socktype = 0xdead; 2659 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2660 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2661 2662 for (err = 1, j = 0; j < items; j++) 2663 { 2664 if (pi[j].iProtocol == autoprotocols[i]) 2665 { 2666 if (socktype == pi[j].iSocketType) 2667 err = 0; 2668 else 2669 ok(0, "Wrong socket type, expected %d received %d\n", 2670 pi[j].iSocketType, socktype); 2671 break; 2672 } 2673 } 2674 ok(!err, "Protocol %d not found in WSAEnumProtocols\n", autoprotocols[i]); 2675 2676 closesocket(sock); 2677 } 2678 2679 HeapFree(GetProcessHeap(), 0, pi); 2680 2681 SetLastError(0xdeadbeef); 2682 /* starting on vista the socket function returns error during the socket 2683 creation and no longer in the socket operations (sendto, readfrom) */ 2684 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0); 2685 if (sock == INVALID_SOCKET) 2686 { 2687 err = WSAGetLastError(); 2688 ok(err == WSAEACCES, "Expected 10013, received %d\n", err); 2689 skip("SOCK_RAW is not supported\n"); 2690 } 2691 else 2692 { 2693 trace("SOCK_RAW is supported\n"); 2694 2695 size = sizeof(socktype); 2696 socktype = 0xdead; 2697 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2698 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2699 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n", 2700 SOCK_RAW, socktype); 2701 closesocket(sock); 2702 2703 todo_wine { 2704 sock = WSASocketA(0, 0, IPPROTO_RAW, NULL, 0, 0); 2705 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2706 WSAGetLastError()); 2707 size = sizeof(socktype); 2708 socktype = 0xdead; 2709 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2710 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2711 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n", 2712 SOCK_RAW, socktype); 2713 closesocket(sock); 2714 } 2715 2716 sock = WSASocketA(AF_INET, SOCK_RAW, IPPROTO_TCP, NULL, 0, 0); 2717 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2718 WSAGetLastError()); 2719 size = sizeof(socktype); 2720 socktype = 0xdead; 2721 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2722 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2723 ok(socktype == SOCK_RAW, "Wrong socket type, expected %d received %d\n", 2724 SOCK_RAW, socktype); 2725 closesocket(sock); 2726 } 2727 2728 /* IPX socket tests */ 2729 2730 SetLastError(0xdeadbeef); 2731 sock = WSASocketA(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, NULL, 0, 0); 2732 if (sock == INVALID_SOCKET) 2733 { 2734 err = WSAGetLastError(); 2735 todo_wine_if (err == WSAEPROTONOSUPPORT) 2736 ok(err == WSAEAFNOSUPPORT || broken(err == WSAEPROTONOSUPPORT), "Expected 10047, received %d\n", err); 2737 skip("IPX is not supported\n"); 2738 } 2739 else 2740 { 2741 WSAPROTOCOL_INFOA info; 2742 closesocket(sock); 2743 2744 trace("IPX is supported\n"); 2745 2746 sock = WSASocketA(0, 0, NSPROTO_IPX, NULL, 0, 0); 2747 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2748 WSAGetLastError()); 2749 2750 size = sizeof(socktype); 2751 socktype = 0xdead; 2752 err = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2753 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 2754 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n", 2755 SOCK_DGRAM, socktype); 2756 2757 /* check socket family, type and protocol */ 2758 size = sizeof(WSAPROTOCOL_INFOA); 2759 err = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *) &info, &size); 2760 ok(!err,"getsockopt failed with %d\n", WSAGetLastError()); 2761 ok(info.iProtocol == NSPROTO_IPX, "expected protocol %d, received %d\n", 2762 NSPROTO_IPX, info.iProtocol); 2763 ok(info.iAddressFamily == AF_IPX, "expected family %d, received %d\n", 2764 AF_IPX, info.iProtocol); 2765 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n", 2766 SOCK_DGRAM, info.iSocketType); 2767 closesocket(sock); 2768 2769 /* SOCK_STREAM does not support NSPROTO_IPX */ 2770 SetLastError(0xdeadbeef); 2771 ok(WSASocketA(AF_IPX, SOCK_STREAM, NSPROTO_IPX, NULL, 0, 0) == INVALID_SOCKET, 2772 "WSASocketA should have failed\n"); 2773 err = WSAGetLastError(); 2774 ok(err == WSAEPROTONOSUPPORT, "Expected 10043, received %d\n", err); 2775 2776 /* test extended IPX support - that is adding any number between 0 and 255 2777 * to the IPX protocol value will make it be used as IPX packet type */ 2778 for(i = 0;i <= 255;i += 17) 2779 { 2780 SetLastError(0xdeadbeef); 2781 sock = WSASocketA(0, 0, NSPROTO_IPX + i, NULL, 0, 0); 2782 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n", 2783 WSAGetLastError()); 2784 2785 size = sizeof(int); 2786 socktype = -1; 2787 err = getsockopt(sock, NSPROTO_IPX, IPX_PTYPE, (char *) &socktype, &size); 2788 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 2789 ok(socktype == i, "Wrong IPX packet type, expected %d received %d\n", 2790 i, socktype); 2791 2792 closesocket(sock); 2793 } 2794 } 2795 } 2796 2797 static void test_WSADuplicateSocket(void) 2798 { 2799 SOCKET source, dupsock; 2800 WSAPROTOCOL_INFOA info; 2801 DWORD err; 2802 struct sockaddr_in addr; 2803 int socktype, size, addrsize, ret; 2804 char teststr[] = "TEST", buffer[16]; 2805 2806 source = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0); 2807 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2808 2809 /* test invalid parameters */ 2810 SetLastError(0xdeadbeef); 2811 ok(WSADuplicateSocketA(0, 0, NULL), "WSADuplicateSocketA should have failed\n"); 2812 err = WSAGetLastError(); 2813 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err); 2814 2815 SetLastError(0xdeadbeef); 2816 ok(WSADuplicateSocketA(source, 0, NULL), 2817 "WSADuplicateSocketA should have failed\n"); 2818 err = WSAGetLastError(); 2819 ok(err == WSAEINVAL, "expected 10022, received %d\n", err); 2820 2821 SetLastError(0xdeadbeef); 2822 ok(WSADuplicateSocketA(source, ~0, &info), 2823 "WSADuplicateSocketA should have failed\n"); 2824 err = WSAGetLastError(); 2825 ok(err == WSAEINVAL, "expected 10022, received %d\n", err); 2826 2827 SetLastError(0xdeadbeef); 2828 ok(WSADuplicateSocketA(0, GetCurrentProcessId(), &info), 2829 "WSADuplicateSocketA should have failed\n"); 2830 err = WSAGetLastError(); 2831 ok(err == WSAENOTSOCK, "expected 10038, received %d\n", err); 2832 2833 SetLastError(0xdeadbeef); 2834 ok(WSADuplicateSocketA(source, GetCurrentProcessId(), NULL), 2835 "WSADuplicateSocketA should have failed\n"); 2836 err = WSAGetLastError(); 2837 ok(err == WSAEFAULT, "expected 10014, received %d\n", err); 2838 2839 /* test returned structure */ 2840 memset(&info, 0, sizeof(info)); 2841 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info), 2842 "WSADuplicateSocketA should have worked\n"); 2843 2844 ok(info.iProtocol == IPPROTO_TCP, "expected protocol %d, received %d\n", 2845 IPPROTO_TCP, info.iProtocol); 2846 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n", 2847 AF_INET, info.iProtocol); 2848 ok(info.iSocketType == SOCK_STREAM, "expected type %d, received %d\n", 2849 SOCK_STREAM, info.iSocketType); 2850 2851 dupsock = WSASocketA(0, 0, 0, &info, 0, 0); 2852 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2853 2854 closesocket(dupsock); 2855 closesocket(source); 2856 2857 /* create a socket, bind it, duplicate it then send data on source and 2858 * receive in the duplicated socket */ 2859 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); 2860 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2861 2862 memset(&info, 0, sizeof(info)); 2863 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info), 2864 "WSADuplicateSocketA should have worked\n"); 2865 2866 ok(info.iProtocol == IPPROTO_UDP, "expected protocol %d, received %d\n", 2867 IPPROTO_UDP, info.iProtocol); 2868 ok(info.iAddressFamily == AF_INET, "expected family %d, received %d\n", 2869 AF_INET, info.iProtocol); 2870 ok(info.iSocketType == SOCK_DGRAM, "expected type %d, received %d\n", 2871 SOCK_DGRAM, info.iSocketType); 2872 2873 memset(&addr, 0, sizeof(addr)); 2874 addr.sin_family = AF_INET; 2875 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 2876 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)), 2877 "bind should have worked\n"); 2878 2879 /* read address to find out the port number to be used in sendto */ 2880 memset(&addr, 0, sizeof(addr)); 2881 addrsize = sizeof(addr); 2882 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize), 2883 "getsockname should have worked\n"); 2884 ok(addr.sin_port, "socket port should be != 0\n"); 2885 2886 dupsock = WSASocketA(0, 0, 0, &info, 0, 0); 2887 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2888 2889 size = sizeof(int); 2890 ret = getsockopt(dupsock, SOL_SOCKET, SO_TYPE, (char *) &socktype, &size); 2891 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError()); 2892 ok(socktype == SOCK_DGRAM, "Wrong socket type, expected %d received %d\n", 2893 SOCK_DGRAM, socktype); 2894 2895 set_blocking(source, TRUE); 2896 2897 /* send data on source socket */ 2898 addrsize = sizeof(addr); 2899 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize); 2900 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError()); 2901 2902 /* receive on duplicated socket */ 2903 addrsize = sizeof(addr); 2904 memset(buffer, 0, sizeof(buffer)); 2905 size = recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize); 2906 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError()); 2907 buffer[sizeof(teststr) - 1] = 0; 2908 ok(!strcmp(buffer, teststr), "expected '%s', received '%s'\n", teststr, buffer); 2909 2910 closesocket(dupsock); 2911 closesocket(source); 2912 2913 /* show that the source socket need to be bound before the duplicated 2914 * socket is created */ 2915 source = WSASocketA(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); 2916 ok(source != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2917 2918 memset(&info, 0, sizeof(info)); 2919 ok(!WSADuplicateSocketA(source, GetCurrentProcessId(), &info), 2920 "WSADuplicateSocketA should have worked\n"); 2921 2922 dupsock = WSASocketA(0, 0, 0, &info, 0, 0); 2923 ok(dupsock != INVALID_SOCKET, "WSASocketA should have succeeded\n"); 2924 2925 memset(&addr, 0, sizeof(addr)); 2926 addr.sin_family = AF_INET; 2927 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 2928 ok(!bind(source, (struct sockaddr*)&addr, sizeof(addr)), 2929 "bind should have worked\n"); 2930 2931 /* read address to find out the port number to be used in sendto */ 2932 memset(&addr, 0, sizeof(addr)); 2933 addrsize = sizeof(addr); 2934 ok(!getsockname(source, (struct sockaddr *) &addr, &addrsize), 2935 "getsockname should have worked\n"); 2936 ok(addr.sin_port, "socket port should be != 0\n"); 2937 2938 set_blocking(source, TRUE); 2939 2940 addrsize = sizeof(addr); 2941 size = sendto(source, teststr, sizeof(teststr), 0, (struct sockaddr *) &addr, addrsize); 2942 ok(size == sizeof(teststr), "got %d (err %d)\n", size, WSAGetLastError()); 2943 2944 SetLastError(0xdeadbeef); 2945 addrsize = sizeof(addr); 2946 memset(buffer, 0, sizeof(buffer)); 2947 todo_wine { 2948 ok(recvfrom(dupsock, buffer, sizeof(teststr), 0, (struct sockaddr *) &addr, &addrsize) == -1, 2949 "recvfrom should have failed\n"); 2950 err = WSAGetLastError(); 2951 ok(err == WSAEINVAL, "expected 10022, received %d\n", err); 2952 } 2953 2954 closesocket(dupsock); 2955 closesocket(source); 2956 } 2957 2958 static void test_WSAEnumNetworkEvents(void) 2959 { 2960 SOCKET s, s2; 2961 int sock_type[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM}, i, j, k, l; 2962 struct sockaddr_in address; 2963 HANDLE event; 2964 WSANETWORKEVENTS net_events; 2965 2966 memset(&address, 0, sizeof(address)); 2967 address.sin_addr.s_addr = htonl(INADDR_ANY); 2968 address.sin_family = AF_INET; 2969 2970 /* This test follows the steps from bugs 10204 and 24946 */ 2971 for (l = 0; l < 2; l++) 2972 { 2973 for (i = 0; i < sizeof(sock_type) / sizeof(sock_type[0]); i++) 2974 { 2975 if (i == 2) 2976 ok(!tcp_socketpair(&s, &s2), "Test[%d]: creating socket pair failed\n", i); 2977 else 2978 { 2979 s = socket(AF_INET, sock_type[i], 0); 2980 ok (s != SOCKET_ERROR, "Test[%d]: failed to create socket\n", i); 2981 ok (!bind(s, (struct sockaddr*) &address, sizeof(address)), "Test[%d]: bind failed\n", i); 2982 } 2983 event = WSACreateEvent(); 2984 ok (event != NULL, "Test[%d]: failed to create event\n", i); 2985 for (j = 0; j < 5; j++) /* Repeat sometimes and the result must be the same */ 2986 { 2987 /* When the TCP socket is not connected NO events will be returned. 2988 * When connected and no data pending it will get the write event. 2989 * UDP sockets don't have connections so as soon as they are bound 2990 * they can read/write data. Since nobody is sendind us data only 2991 * the write event will be returned and ONLY once. 2992 */ 2993 ok (!WSAEventSelect(s, event, FD_READ | FD_WRITE), "Test[%d]: WSAEventSelect failed\n", i); 2994 memset(&net_events, 0xAB, sizeof(net_events)); 2995 ok (!WSAEnumNetworkEvents(s, l == 0 ? event : NULL, &net_events), 2996 "Test[%d]: WSAEnumNetworkEvents failed\n", i); 2997 if (i >= 1 && j == 0) /* FD_WRITE is SET on first try for UDP and connected TCP */ 2998 { 2999 todo_wine_if (i == 0 || net_events.lNetworkEvents == 0) 3000 ok (net_events.lNetworkEvents == FD_WRITE, "Test[%d]: expected 2, got %d\n", 3001 i, net_events.lNetworkEvents); 3002 } 3003 else 3004 { 3005 todo_wine_if (i != 0 && net_events.lNetworkEvents != 0) 3006 ok (net_events.lNetworkEvents == 0, "Test[%d]: expected 0, got %d\n", 3007 i, net_events.lNetworkEvents); 3008 } 3009 for (k = 0; k < FD_MAX_EVENTS; k++) 3010 { 3011 if (net_events.lNetworkEvents & (1 << k)) 3012 { 3013 ok (net_events.iErrorCode[k] == 0x0, "Test[%d][%d]: expected 0x0, got 0x%x\n", 3014 i, k, net_events.iErrorCode[k]); 3015 } 3016 else 3017 { 3018 /* Bits that are not set in lNetworkEvents MUST not be changed */ 3019 ok (net_events.iErrorCode[k] == 0xABABABAB, "Test[%d][%d]: expected 0xABABABAB, got 0x%x\n", 3020 i, k, net_events.iErrorCode[k]); 3021 } 3022 } 3023 } 3024 closesocket(s); 3025 WSACloseEvent(event); 3026 if (i == 2) closesocket(s2); 3027 } 3028 } 3029 } 3030 3031 static void test_WSAAddressToStringA(void) 3032 { 3033 SOCKET v6 = INVALID_SOCKET; 3034 INT ret; 3035 DWORD len; 3036 int GLE; 3037 SOCKADDR_IN sockaddr; 3038 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */ 3039 3040 CHAR expect1[] = "0.0.0.0"; 3041 CHAR expect2[] = "255.255.255.255"; 3042 CHAR expect3[] = "0.0.0.0:65535"; 3043 CHAR expect4[] = "255.255.255.255:65535"; 3044 3045 SOCKADDR_IN6 sockaddr6; 3046 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */ 3047 3048 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; 3049 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; 3050 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01}; 3051 3052 CHAR expect6_1[] = "::1"; 3053 CHAR expect6_2[] = "20ab::1"; 3054 CHAR expect6_3[] = "[20ab::2001]:33274"; 3055 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274"; 3056 CHAR expect6_3_3[] = "20ab::2001%4660"; 3057 3058 len = 0; 3059 3060 sockaddr.sin_family = AF_INET; 3061 sockaddr.sin_port = 0; 3062 sockaddr.sin_addr.s_addr = 0; 3063 3064 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3065 GLE = WSAGetLastError(); 3066 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0), 3067 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n", 3068 GLE, ret ); 3069 3070 len = sizeof(address); 3071 3072 sockaddr.sin_family = AF_INET; 3073 sockaddr.sin_port = 0; 3074 sockaddr.sin_addr.s_addr = 0; 3075 3076 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3077 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3078 3079 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address ); 3080 ok( len == sizeof( expect1 ), "Got size %d\n", len); 3081 3082 len = sizeof(address); 3083 3084 sockaddr.sin_family = AF_INET; 3085 sockaddr.sin_port = 0; 3086 sockaddr.sin_addr.s_addr = 0xffffffff; 3087 3088 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3089 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3090 3091 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address ); 3092 3093 len = sizeof(address); 3094 3095 sockaddr.sin_family = AF_INET; 3096 sockaddr.sin_port = 0xffff; 3097 sockaddr.sin_addr.s_addr = 0; 3098 3099 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3100 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3101 3102 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address ); 3103 3104 len = sizeof(address); 3105 3106 sockaddr.sin_family = AF_INET; 3107 sockaddr.sin_port = 0xffff; 3108 sockaddr.sin_addr.s_addr = 0xffffffff; 3109 3110 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3111 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3112 3113 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address ); 3114 ok( len == sizeof( expect4 ), "Got size %d\n", len); 3115 3116 /*check to see it IPv6 is available */ 3117 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 3118 if (v6 == INVALID_SOCKET) { 3119 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n", 3120 WSAGetLastError(), WSAEAFNOSUPPORT); 3121 goto end; 3122 } 3123 /* Test a short IPv6 address */ 3124 len = sizeof(address6); 3125 3126 sockaddr6.sin6_family = AF_INET6; 3127 sockaddr6.sin6_port = 0x0000; 3128 sockaddr6.sin6_scope_id = 0; 3129 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1)); 3130 3131 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3132 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3133 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 ); 3134 ok( len == sizeof(expect6_1), "Got size %d\n", len); 3135 3136 /* Test a longer IPv6 address */ 3137 len = sizeof(address6); 3138 3139 sockaddr6.sin6_family = AF_INET6; 3140 sockaddr6.sin6_port = 0x0000; 3141 sockaddr6.sin6_scope_id = 0; 3142 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2)); 3143 3144 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3145 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3146 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 ); 3147 ok( len == sizeof(expect6_2), "Got size %d\n", len); 3148 3149 /* Test IPv6 address and port number */ 3150 len = sizeof(address6); 3151 3152 sockaddr6.sin6_family = AF_INET6; 3153 sockaddr6.sin6_port = 0xfa81; 3154 sockaddr6.sin6_scope_id = 0; 3155 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3156 3157 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3158 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3159 ok( !strcmp( address6, expect6_3 ), "Expected: %s, got: %s\n", expect6_3, address6 ); 3160 ok( len == sizeof(expect6_3), "Got size %d\n", len ); 3161 3162 /* Test IPv6 address, port number and scope_id */ 3163 len = sizeof(address6); 3164 3165 sockaddr6.sin6_family = AF_INET6; 3166 sockaddr6.sin6_port = 0xfa81; 3167 sockaddr6.sin6_scope_id = 0x1234; 3168 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3169 3170 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3171 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3172 ok( !strcmp( address6, expect6_3_2 ), "Expected: %s, got: %s\n", expect6_3_2, address6 ); 3173 ok( len == sizeof(expect6_3_2), "Got size %d\n", len ); 3174 3175 /* Test IPv6 address and scope_id */ 3176 len = sizeof(address6); 3177 3178 sockaddr6.sin6_family = AF_INET6; 3179 sockaddr6.sin6_port = 0x0000; 3180 sockaddr6.sin6_scope_id = 0x1234; 3181 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3182 3183 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3184 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() ); 3185 ok( !strcmp( address6, expect6_3_3 ), "Expected: %s, got: %s\n", expect6_3_3, address6 ); 3186 ok( len == sizeof(expect6_3_3), "Got size %d\n", len ); 3187 3188 end: 3189 if (v6 != INVALID_SOCKET) 3190 closesocket(v6); 3191 } 3192 3193 static void test_WSAAddressToStringW(void) 3194 { 3195 SOCKET v6 = INVALID_SOCKET; 3196 INT ret; 3197 DWORD len; 3198 int GLE; 3199 SOCKADDR_IN sockaddr; 3200 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */ 3201 3202 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 }; 3203 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 }; 3204 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 }; 3205 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':', 3206 '6', '5', '5', '3', '5', 0 }; 3207 3208 SOCKADDR_IN6 sockaddr6; 3209 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */ 3210 3211 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; 3212 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; 3213 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01}; 3214 3215 WCHAR expect6_1[] = {':',':','1',0}; 3216 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0}; 3217 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0}; 3218 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0}; 3219 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0}; 3220 3221 len = 0; 3222 3223 sockaddr.sin_family = AF_INET; 3224 sockaddr.sin_port = 0; 3225 sockaddr.sin_addr.s_addr = 0; 3226 3227 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3228 GLE = WSAGetLastError(); 3229 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0), 3230 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n", 3231 GLE, ret ); 3232 3233 len = sizeof(address); 3234 3235 sockaddr.sin_family = AF_INET; 3236 sockaddr.sin_port = 0; 3237 sockaddr.sin_addr.s_addr = 0; 3238 3239 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3240 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3241 3242 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" ); 3243 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len); 3244 3245 len = sizeof(address); 3246 3247 sockaddr.sin_family = AF_INET; 3248 sockaddr.sin_port = 0; 3249 sockaddr.sin_addr.s_addr = 0xffffffff; 3250 3251 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3252 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3253 3254 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" ); 3255 3256 len = sizeof(address); 3257 3258 sockaddr.sin_family = AF_INET; 3259 sockaddr.sin_port = 0xffff; 3260 sockaddr.sin_addr.s_addr = 0; 3261 3262 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3263 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3264 3265 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" ); 3266 3267 len = sizeof(address); 3268 3269 sockaddr.sin_family = AF_INET; 3270 sockaddr.sin_port = 0xffff; 3271 sockaddr.sin_addr.s_addr = 0xffffffff; 3272 3273 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len ); 3274 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3275 3276 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" ); 3277 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len); 3278 3279 /*check to see it IPv6 is available */ 3280 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 3281 if (v6 == INVALID_SOCKET) { 3282 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n", 3283 WSAGetLastError(), WSAEAFNOSUPPORT); 3284 goto end; 3285 } 3286 3287 /* Test a short IPv6 address */ 3288 len = sizeof(address6)/sizeof(WCHAR); 3289 3290 sockaddr6.sin6_family = AF_INET6; 3291 sockaddr6.sin6_port = 0x0000; 3292 sockaddr6.sin6_scope_id = 0; 3293 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1)); 3294 3295 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3296 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3297 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" ); 3298 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len); 3299 3300 /* Test a longer IPv6 address */ 3301 len = sizeof(address6)/sizeof(WCHAR); 3302 3303 sockaddr6.sin6_family = AF_INET6; 3304 sockaddr6.sin6_port = 0x0000; 3305 sockaddr6.sin6_scope_id = 0; 3306 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2)); 3307 3308 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3309 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3310 3311 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" ); 3312 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len); 3313 3314 /* Test IPv6 address and port number */ 3315 len = sizeof(address6)/sizeof(WCHAR); 3316 3317 sockaddr6.sin6_family = AF_INET6; 3318 sockaddr6.sin6_port = 0xfa81; 3319 sockaddr6.sin6_scope_id = 0; 3320 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3321 3322 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3323 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3324 ok( !lstrcmpW( address6, expect6_3 ), 3325 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3), wine_dbgstr_w(address6) ); 3326 ok( len == sizeof(expect6_3)/sizeof(WCHAR), "Got %d\n", len ); 3327 3328 /* Test IPv6 address, port number and scope_id */ 3329 len = sizeof(address6)/sizeof(WCHAR); 3330 3331 sockaddr6.sin6_family = AF_INET6; 3332 sockaddr6.sin6_port = 0xfa81; 3333 sockaddr6.sin6_scope_id = 0x1234; 3334 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3335 3336 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3337 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3338 ok( !lstrcmpW( address6, expect6_3_2 ), 3339 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_2), wine_dbgstr_w(address6) ); 3340 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR), "Got %d\n", len ); 3341 3342 /* Test IPv6 address and scope_id */ 3343 len = sizeof(address6)/sizeof(WCHAR); 3344 3345 sockaddr6.sin6_family = AF_INET6; 3346 sockaddr6.sin6_port = 0x0000; 3347 sockaddr6.sin6_scope_id = 0xfffe; 3348 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3)); 3349 3350 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len ); 3351 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() ); 3352 ok( !lstrcmpW( address6, expect6_3_3 ), 3353 "Expected: %s, got: %s\n", wine_dbgstr_w(expect6_3_3), wine_dbgstr_w(address6) ); 3354 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR), "Got %d\n", len ); 3355 3356 end: 3357 if (v6 != INVALID_SOCKET) 3358 closesocket(v6); 3359 } 3360 3361 static void test_WSAStringToAddressA(void) 3362 { 3363 INT ret, len; 3364 SOCKADDR_IN sockaddr; 3365 SOCKADDR_IN6 sockaddr6; 3366 int GLE; 3367 3368 CHAR address1[] = "0.0.0.0"; 3369 CHAR address2[] = "127.127.127.127"; 3370 CHAR address3[] = "255.255.255.255"; 3371 CHAR address4[] = "127.127.127.127:65535"; 3372 CHAR address5[] = "255.255.255.255:65535"; 3373 CHAR address6[] = "::1"; 3374 CHAR address7[] = "[::1]"; 3375 CHAR address8[] = "[::1]:65535"; 3376 CHAR address9[] = "2001::1"; 3377 3378 len = 0; 3379 sockaddr.sin_family = AF_INET; 3380 3381 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3382 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n", 3383 WSAGetLastError() ); 3384 3385 len = sizeof(sockaddr); 3386 sockaddr.sin_port = 0; 3387 sockaddr.sin_addr.s_addr = 0; 3388 3389 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3390 ok( !ret && sockaddr.sin_addr.s_addr == 0, 3391 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() ); 3392 3393 len = sizeof(sockaddr); 3394 sockaddr.sin_port = 0; 3395 sockaddr.sin_addr.s_addr = 0; 3396 3397 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3398 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f, 3399 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() ); 3400 3401 len = sizeof(sockaddr); 3402 sockaddr.sin_port = 0; 3403 sockaddr.sin_addr.s_addr = 0; 3404 3405 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3406 GLE = WSAGetLastError(); 3407 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) || 3408 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)), 3409 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE ); 3410 3411 len = sizeof(sockaddr); 3412 sockaddr.sin_port = 0; 3413 sockaddr.sin_addr.s_addr = 0; 3414 3415 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3416 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff, 3417 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() ); 3418 3419 len = sizeof(sockaddr); 3420 sockaddr.sin_port = 0; 3421 sockaddr.sin_addr.s_addr = 0; 3422 3423 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3424 GLE = WSAGetLastError(); 3425 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) || 3426 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)), 3427 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE ); 3428 3429 len = sizeof(sockaddr); 3430 3431 ret = WSAStringToAddressA( address9, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3432 GLE = WSAGetLastError(); 3433 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3434 "WSAStringToAddressA() should have failed with %d\n", GLE ); 3435 3436 len = sizeof(sockaddr6); 3437 memset(&sockaddr6, 0, len); 3438 sockaddr6.sin6_family = AF_INET6; 3439 3440 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3441 &len ); 3442 if (ret == SOCKET_ERROR) 3443 { 3444 win_skip("IPv6 not supported\n"); 3445 return; 3446 } 3447 3448 GLE = WSAGetLastError(); 3449 ok( ret == 0, "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE); 3450 3451 len = sizeof(sockaddr6); 3452 memset(&sockaddr6, 0, len); 3453 sockaddr6.sin6_family = AF_INET6; 3454 3455 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3456 &len ); 3457 GLE = WSAGetLastError(); 3458 ok( ret == 0, "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE); 3459 3460 len = sizeof(sockaddr6); 3461 memset(&sockaddr6, 0, len); 3462 sockaddr6.sin6_family = AF_INET6; 3463 3464 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3465 &len ); 3466 GLE = WSAGetLastError(); 3467 ok( ret == 0 && sockaddr6.sin6_port == 0xffff, 3468 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE); 3469 3470 len = sizeof(sockaddr6); 3471 3472 ret = WSAStringToAddressA( address7 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len ); 3473 GLE = WSAGetLastError(); 3474 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3475 "WSAStringToAddressW() should have failed with %d\n", GLE ); 3476 3477 len = sizeof(sockaddr6); 3478 3479 ret = WSAStringToAddressA( address8 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len ); 3480 GLE = WSAGetLastError(); 3481 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3482 "WSAStringToAddressW() should have failed with %d\n", GLE ); 3483 } 3484 3485 static void test_WSAStringToAddressW(void) 3486 { 3487 INT ret, len; 3488 SOCKADDR_IN sockaddr, *sin; 3489 SOCKADDR_IN6 sockaddr6; 3490 SOCKADDR_STORAGE sockaddr_storage; 3491 int GLE; 3492 3493 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 }; 3494 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 }; 3495 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 }; 3496 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 3497 ':', '6', '5', '5', '3', '5', 0 }; 3498 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':', 3499 '6', '5', '5', '3', '5', 0 }; 3500 WCHAR address6[] = {':',':','1','\0'}; 3501 WCHAR address7[] = {'[',':',':','1',']','\0'}; 3502 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'}; 3503 WCHAR address9[] = {'2','0','0','1',':',':','1','\0'}; 3504 3505 len = 0; 3506 sockaddr.sin_family = AF_INET; 3507 3508 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3509 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n", 3510 WSAGetLastError() ); 3511 3512 len = sizeof(sockaddr); 3513 sockaddr.sin_port = 0; 3514 sockaddr.sin_addr.s_addr = 0; 3515 3516 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3517 ok( !ret && sockaddr.sin_addr.s_addr == 0, 3518 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() ); 3519 3520 len = sizeof(sockaddr); 3521 sockaddr.sin_port = 0; 3522 sockaddr.sin_addr.s_addr = 0; 3523 3524 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3525 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f, 3526 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() ); 3527 3528 len = sizeof(sockaddr); 3529 sockaddr.sin_port = 0; 3530 sockaddr.sin_addr.s_addr = 0; 3531 3532 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3533 GLE = WSAGetLastError(); 3534 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) || 3535 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)), 3536 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE ); 3537 3538 len = sizeof(sockaddr); 3539 sockaddr.sin_port = 0; 3540 sockaddr.sin_addr.s_addr = 0; 3541 3542 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3543 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff, 3544 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() ); 3545 3546 len = sizeof(sockaddr); 3547 sockaddr.sin_port = 0; 3548 sockaddr.sin_addr.s_addr = 0; 3549 3550 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3551 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) || 3552 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)), 3553 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE ); 3554 3555 /* Test with a larger buffer than necessary */ 3556 len = sizeof(sockaddr_storage); 3557 sin = (SOCKADDR_IN *)&sockaddr_storage; 3558 sin->sin_port = 0; 3559 sin->sin_addr.s_addr = 0; 3560 3561 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)sin, &len ); 3562 ok( (ret == 0 && sin->sin_addr.s_addr == 0xffffffff && sin->sin_port == 0xffff) || 3563 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)), 3564 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE ); 3565 ok( len == sizeof(SOCKADDR_IN), "unexpected length %d\n", len ); 3566 3567 len = sizeof(sockaddr); 3568 3569 ret = WSAStringToAddressW( address9, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len ); 3570 GLE = WSAGetLastError(); 3571 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3572 "WSAStringToAddressW() should have failed with %d\n", GLE ); 3573 3574 len = sizeof(sockaddr6); 3575 memset(&sockaddr6, 0, len); 3576 sockaddr6.sin6_family = AF_INET6; 3577 3578 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3579 &len ); 3580 if (ret == SOCKET_ERROR) 3581 { 3582 win_skip("IPv6 not supported\n"); 3583 return; 3584 } 3585 3586 GLE = WSAGetLastError(); 3587 ok( ret == 0, "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE); 3588 3589 len = sizeof(sockaddr6); 3590 memset(&sockaddr6, 0, len); 3591 sockaddr6.sin6_family = AF_INET6; 3592 3593 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3594 &len ); 3595 GLE = WSAGetLastError(); 3596 ok( ret == 0, "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE); 3597 3598 len = sizeof(sockaddr6); 3599 memset(&sockaddr6, 0, len); 3600 sockaddr6.sin6_family = AF_INET6; 3601 3602 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6, 3603 &len ); 3604 GLE = WSAGetLastError(); 3605 ok( ret == 0 && sockaddr6.sin6_port == 0xffff, 3606 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE); 3607 3608 len = sizeof(sockaddr6); 3609 3610 ret = WSAStringToAddressW( address7 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len ); 3611 GLE = WSAGetLastError(); 3612 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3613 "WSAStringToAddressW() should have failed with %d\n", GLE ); 3614 3615 len = sizeof(sockaddr6); 3616 3617 ret = WSAStringToAddressW( address8 + 1, AF_INET6, NULL, (SOCKADDR*)&sockaddr, &len ); 3618 GLE = WSAGetLastError(); 3619 ok( (ret == SOCKET_ERROR && GLE == WSAEINVAL), 3620 "WSAStringToAddressW() should have failed with %d\n", GLE ); 3621 } 3622 3623 static DWORD WINAPI SelectReadThread(void *param) 3624 { 3625 select_thread_params *par = param; 3626 fd_set readfds; 3627 int ret; 3628 struct sockaddr_in addr; 3629 struct timeval select_timeout; 3630 3631 memset(&readfds, 0, sizeof(readfds)); 3632 FD_ZERO(&readfds); 3633 FD_SET(par->s, &readfds); 3634 select_timeout.tv_sec=5; 3635 select_timeout.tv_usec=0; 3636 addr.sin_family = AF_INET; 3637 addr.sin_addr.s_addr = inet_addr(SERVERIP); 3638 addr.sin_port = htons(SERVERPORT); 3639 3640 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr)); 3641 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n"); 3642 3643 SetEvent(server_ready); 3644 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout); 3645 par->ReadKilled = (ret == 1); 3646 3647 return 0; 3648 } 3649 3650 static DWORD WINAPI SelectCloseThread(void *param) 3651 { 3652 SOCKET s = *(SOCKET*)param; 3653 Sleep(500); 3654 closesocket(s); 3655 return 0; 3656 } 3657 3658 static void test_errors(void) 3659 { 3660 SOCKET sock; 3661 SOCKADDR_IN SockAddr; 3662 int ret, err; 3663 3664 WSASetLastError(NO_ERROR); 3665 sock = socket(PF_INET, SOCK_STREAM, 0); 3666 ok( (sock != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3667 memset(&SockAddr, 0, sizeof(SockAddr)); 3668 SockAddr.sin_family = AF_INET; 3669 SockAddr.sin_port = htons(6924); 3670 SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 3671 3672 ret = connect(sock, (PSOCKADDR)&SockAddr, sizeof(SockAddr)); 3673 ok( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got: %d\n", ret ); 3674 if (ret == SOCKET_ERROR) 3675 { 3676 err = WSAGetLastError(); 3677 ok( (err == WSAECONNREFUSED), "expected WSAECONNREFUSED, got: %d\n", err ); 3678 } 3679 3680 { 3681 TIMEVAL timeval; 3682 fd_set set = {1, {sock}}; 3683 3684 timeval.tv_sec = 0; 3685 timeval.tv_usec = 50000; 3686 3687 ret = select(1, NULL, &set, NULL, &timeval); 3688 ok( (ret == 0), "expected 0 (timeout), got: %d\n", ret ); 3689 } 3690 3691 ret = closesocket(sock); 3692 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", WSAGetLastError()); 3693 } 3694 3695 static void test_listen(void) 3696 { 3697 SOCKET fdA, fdB; 3698 int ret, acceptc, olen = sizeof(acceptc); 3699 struct sockaddr_in address; 3700 3701 memset(&address, 0, sizeof(address)); 3702 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 3703 address.sin_family = AF_INET; 3704 address.sin_port = htons(SERVERPORT); 3705 3706 /* invalid socket tests */ 3707 SetLastError(0xdeadbeef); 3708 ok ((listen(0, 0) == SOCKET_ERROR), "listen did not fail\n"); 3709 ret = WSAGetLastError(); 3710 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret); 3711 3712 SetLastError(0xdeadbeef); 3713 ok ((listen(0xdeadbeef, 0) == SOCKET_ERROR), "listen did not fail\n"); 3714 ret = WSAGetLastError(); 3715 ok (ret == WSAENOTSOCK, "expected 10038, received %d\n", ret); 3716 3717 /* tcp tests */ 3718 fdA = socket(AF_INET, SOCK_STREAM, 0); 3719 ok ((fdA != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3720 3721 fdB = socket(AF_INET, SOCK_STREAM, 0); 3722 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3723 3724 SetLastError(0xdeadbeef); 3725 ok ((listen(fdA, -2) == SOCKET_ERROR), "listen did not fail\n"); 3726 ret = WSAGetLastError(); 3727 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret); 3728 3729 SetLastError(0xdeadbeef); 3730 ok ((listen(fdA, 1) == SOCKET_ERROR), "listen did not fail\n"); 3731 ret = WSAGetLastError(); 3732 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret); 3733 3734 SetLastError(0xdeadbeef); 3735 ok ((listen(fdA, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n"); 3736 ret = WSAGetLastError(); 3737 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret); 3738 3739 ok (!bind(fdA, (struct sockaddr*) &address, sizeof(address)), "bind failed\n"); 3740 3741 SetLastError(0xdeadbeef); 3742 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n"); 3743 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret); 3744 3745 acceptc = 0xdead; 3746 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen); 3747 ok (!ret, "getsockopt failed\n"); 3748 ok (acceptc == 0, "SO_ACCEPTCONN should be 0, received %d\n", acceptc); 3749 3750 ok (!listen(fdA, 0), "listen failed\n"); 3751 ok (!listen(fdA, SOMAXCONN), "double listen failed\n"); 3752 3753 acceptc = 0xdead; 3754 ret = getsockopt(fdA, SOL_SOCKET, SO_ACCEPTCONN, (char*)&acceptc, &olen); 3755 ok (!ret, "getsockopt failed\n"); 3756 ok (acceptc == 1, "SO_ACCEPTCONN should be 1, received %d\n", acceptc); 3757 3758 SetLastError(0xdeadbeef); 3759 ok ((listen(fdB, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n"); 3760 ret = WSAGetLastError(); 3761 ok (ret == WSAEINVAL, "expected 10022, received %d\n", ret); 3762 3763 ret = closesocket(fdB); 3764 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret); 3765 3766 fdB = socket(AF_INET, SOCK_STREAM, 0); 3767 ok ((fdB != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3768 3769 SetLastError(0xdeadbeef); 3770 ok (bind(fdB, (struct sockaddr*) &address, sizeof(address)), "bind should have failed\n"); 3771 ret = WSAGetLastError(); 3772 ok (ret == WSAEADDRINUSE, "expected 10048, received %d\n", ret); 3773 3774 ret = closesocket(fdA); 3775 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret); 3776 ret = closesocket(fdB); 3777 ok (ret == 0, "closesocket failed unexpectedly: %d\n", ret); 3778 } 3779 3780 #define FD_ZERO_ALL() { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); } 3781 #define FD_SET_ALL(s) { FD_SET(s, &readfds); FD_SET(s, &writefds); FD_SET(s, &exceptfds); } 3782 static void test_select(void) 3783 { 3784 static char tmp_buf[1024]; 3785 3786 SOCKET fdListen, fdRead, fdWrite; 3787 fd_set readfds, writefds, exceptfds; 3788 unsigned int maxfd; 3789 int ret, len; 3790 char buffer; 3791 struct timeval select_timeout; 3792 struct sockaddr_in address; 3793 select_thread_params thread_params; 3794 HANDLE thread_handle; 3795 DWORD ticks, id; 3796 3797 fdRead = socket(AF_INET, SOCK_STREAM, 0); 3798 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3799 fdWrite = socket(AF_INET, SOCK_STREAM, 0); 3800 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() ); 3801 3802 maxfd = fdRead; 3803 if (fdWrite > maxfd) 3804 maxfd = fdWrite; 3805 3806 FD_ZERO_ALL(); 3807 FD_SET_ALL(fdRead); 3808 FD_SET_ALL(fdWrite); 3809 select_timeout.tv_sec=0; 3810 select_timeout.tv_usec=0; 3811 3812 ticks = GetTickCount(); 3813 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3814 ticks = GetTickCount() - ticks; 3815 ok(ret == 0, "select should not return any socket handles\n"); 3816 ok(ticks < 10, "select was blocking for %u ms, expected < 10 ms\n", ticks); 3817 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n"); 3818 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n"); 3819 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n"); 3820 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n"); 3821 3822 FD_ZERO_ALL(); 3823 FD_SET_ALL(fdRead); 3824 FD_SET_ALL(fdWrite); 3825 select_timeout.tv_sec=0; 3826 select_timeout.tv_usec=500; 3827 3828 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3829 ok(ret == 0, "select should not return any socket handles\n"); 3830 ok(!FD_ISSET(fdRead, &readfds), "FD should not be set\n"); 3831 ok(!FD_ISSET(fdWrite, &writefds), "FD should not be set\n"); 3832 ok(!FD_ISSET(fdRead, &exceptfds), "FD should not be set\n"); 3833 ok(!FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n"); 3834 3835 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n"); 3836 ret = closesocket(fdWrite); 3837 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret); 3838 3839 thread_params.s = fdRead; 3840 thread_params.ReadKilled = FALSE; 3841 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL); 3842 thread_handle = CreateThread (NULL, 0, SelectReadThread, &thread_params, 0, &id ); 3843 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError()); 3844 3845 WaitForSingleObject (server_ready, INFINITE); 3846 Sleep(200); 3847 ret = closesocket(fdRead); 3848 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret); 3849 3850 WaitForSingleObject (thread_handle, 1000); 3851 ok ( thread_params.ReadKilled, "closesocket did not wake up select\n"); 3852 ret = recv(fdRead, &buffer, 1, MSG_PEEK); 3853 ok( (ret == -1), "peek at closed socket expected -1 got %d\n", ret); 3854 3855 /* Test selecting invalid handles */ 3856 FD_ZERO_ALL(); 3857 3858 SetLastError(0); 3859 ret = select(maxfd+1, 0, 0, 0, &select_timeout); 3860 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret); 3861 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError()); 3862 3863 SetLastError(0); 3864 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3865 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret); 3866 ok ( WSAGetLastError() == WSAEINVAL, "expected WSAEINVAL, got %i\n", WSAGetLastError()); 3867 3868 FD_SET(INVALID_SOCKET, &readfds); 3869 SetLastError(0); 3870 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3871 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret); 3872 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError()); 3873 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n"); 3874 3875 FD_ZERO(&readfds); 3876 FD_SET(INVALID_SOCKET, &writefds); 3877 SetLastError(0); 3878 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3879 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret); 3880 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError()); 3881 ok ( !FD_ISSET(fdRead, &writefds), "FD should not be set\n"); 3882 3883 FD_ZERO(&writefds); 3884 FD_SET(INVALID_SOCKET, &exceptfds); 3885 SetLastError(0); 3886 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout); 3887 ok ( (ret == SOCKET_ERROR), "expected SOCKET_ERROR, got %i\n", ret); 3888 ok ( WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %i\n", WSAGetLastError()); 3889 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n"); 3890 3891 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 3892 maxfd = fdRead; 3893 if(fdWrite > maxfd) maxfd = fdWrite; 3894 3895 FD_ZERO(&readfds); 3896 FD_SET(fdRead, &readfds); 3897 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout); 3898 ok(!ret, "select returned %d\n", ret); 3899 3900 FD_ZERO(&writefds); 3901 FD_SET(fdWrite, &writefds); 3902 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout); 3903 ok(ret == 1, "select returned %d\n", ret); 3904 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n"); 3905 3906 /* tests for overlapping fd_set pointers */ 3907 FD_ZERO(&readfds); 3908 FD_SET(fdWrite, &readfds); 3909 ret = select(fdWrite+1, &readfds, &readfds, NULL, &select_timeout); 3910 ok(ret == 1, "select returned %d\n", ret); 3911 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 3912 3913 FD_ZERO(&readfds); 3914 FD_SET(fdWrite, &readfds); 3915 FD_SET(fdRead, &readfds); 3916 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout); 3917 ok(ret == 2, "select returned %d\n", ret); 3918 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 3919 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 3920 3921 ok(send(fdWrite, "test", 4, 0) == 4, "failed to send data\n"); 3922 FD_ZERO(&readfds); 3923 FD_SET(fdRead, &readfds); 3924 ret = select(fdRead+1, &readfds, NULL, NULL, &select_timeout); 3925 ok(ret == 1, "select returned %d\n", ret); 3926 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 3927 3928 FD_ZERO(&readfds); 3929 FD_SET(fdWrite, &readfds); 3930 FD_SET(fdRead, &readfds); 3931 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout); 3932 ok(ret == 2, "select returned %d\n", ret); 3933 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 3934 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 3935 3936 #if ROSTESTS_233_IS_FIXED 3937 while(1) { 3938 FD_ZERO(&writefds); 3939 FD_SET(fdWrite, &writefds); 3940 ret = select(fdWrite+1, NULL, &writefds, NULL, &select_timeout); 3941 if(!ret) break; 3942 ok(send(fdWrite, tmp_buf, sizeof(tmp_buf), 0) > 0, "failed to send data\n"); 3943 } 3944 #endif /* ROSTESTS_233_IS_FIXED */ 3945 3946 FD_ZERO(&readfds); 3947 FD_SET(fdWrite, &readfds); 3948 FD_SET(fdRead, &readfds); 3949 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout); 3950 ok(ret == 1, "select returned %d\n", ret); 3951 ok(!FD_ISSET(fdWrite, &readfds), "fdWrite socket is in the set\n"); 3952 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 3953 3954 ok(send(fdRead, "test", 4, 0) == 4, "failed to send data\n"); 3955 Sleep(100); 3956 FD_ZERO(&readfds); 3957 FD_SET(fdWrite, &readfds); 3958 FD_SET(fdRead, &readfds); 3959 ret = select(maxfd+1, &readfds, &readfds, NULL, &select_timeout); 3960 ok(ret == 2, "select returned %d\n", ret); 3961 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 3962 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 3963 3964 closesocket(fdRead); 3965 closesocket(fdWrite); 3966 3967 /* select() works in 3 distinct states: 3968 * - to check if a connection attempt ended with success or error; 3969 * - to check if a pending connection is waiting for acceptance; 3970 * - to check for data to read, availability for write and OOB data 3971 * 3972 * The tests below ensure that all conditions are tested. 3973 */ 3974 memset(&address, 0, sizeof(address)); 3975 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 3976 address.sin_family = AF_INET; 3977 len = sizeof(address); 3978 fdListen = setup_server_socket(&address, &len); 3979 select_timeout.tv_sec = 1; 3980 select_timeout.tv_usec = 250000; 3981 3982 /* When no events are pending select returns 0 with no error */ 3983 FD_ZERO_ALL(); 3984 FD_SET_ALL(fdListen); 3985 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 3986 ok(ret == 0, "expected 0, got %d\n", ret); 3987 3988 /* When a socket is attempting to connect the listening socket receives the read descriptor */ 3989 fdWrite = setup_connector_socket(&address, len, TRUE); 3990 FD_ZERO_ALL(); 3991 FD_SET_ALL(fdListen); 3992 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 3993 ok(ret == 1, "expected 1, got %d\n", ret); 3994 ok(FD_ISSET(fdListen, &readfds), "fdListen socket is not in the set\n"); 3995 len = sizeof(address); 3996 fdRead = accept(fdListen, (struct sockaddr*) &address, &len); 3997 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n"); 3998 3999 /* The connector is signaled through the write descriptor */ 4000 FD_ZERO_ALL(); 4001 FD_SET_ALL(fdListen); 4002 FD_SET_ALL(fdRead); 4003 FD_SET_ALL(fdWrite); 4004 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4005 ok(ret == 2, "expected 2, got %d\n", ret); 4006 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n"); 4007 ok(FD_ISSET(fdRead, &writefds), "fdRead socket is not in the set\n"); 4008 len = sizeof(id); 4009 id = 0xdeadbeef; 4010 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len); 4011 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError()); 4012 ok(id == 0, "expected 0, got %d\n", id); 4013 4014 /* When data is received the receiver gets the read descriptor */ 4015 ret = send(fdWrite, "1234", 4, 0); 4016 ok(ret == 4, "expected 4, got %d\n", ret); 4017 FD_ZERO_ALL(); 4018 FD_SET_ALL(fdListen); 4019 FD_SET(fdRead, &readfds); 4020 FD_SET(fdRead, &exceptfds); 4021 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4022 ok(ret == 1, "expected 1, got %d\n", ret); 4023 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 4024 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0); 4025 ok(ret == 4, "expected 4, got %d\n", ret); 4026 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n"); 4027 4028 /* When OOB data is received the socket is set in the except descriptor */ 4029 ret = send(fdWrite, "A", 1, MSG_OOB); 4030 ok(ret == 1, "expected 1, got %d\n", ret); 4031 FD_ZERO_ALL(); 4032 FD_SET_ALL(fdListen); 4033 FD_SET(fdRead, &readfds); 4034 FD_SET(fdRead, &exceptfds); 4035 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4036 ok(ret == 1, "expected 1, got %d\n", ret); 4037 ok(FD_ISSET(fdRead, &exceptfds), "fdRead socket is not in the set\n"); 4038 tmp_buf[0] = 0xAF; 4039 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB); 4040 ok(ret == 1, "expected 1, got %d\n", ret); 4041 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]); 4042 4043 /* If the socket is OOBINLINED it will not receive the OOB in except fds */ 4044 ret = 1; 4045 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret)); 4046 ok(ret == 0, "expected 0, got %d\n", ret); 4047 ret = send(fdWrite, "A", 1, MSG_OOB); 4048 ok(ret == 1, "expected 1, got %d\n", ret); 4049 FD_ZERO_ALL(); 4050 FD_SET_ALL(fdListen); 4051 FD_SET(fdRead, &readfds); 4052 FD_SET(fdRead, &exceptfds); 4053 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4054 ok(ret == 1, "expected 1, got %d\n", ret); 4055 ok(FD_ISSET(fdRead, &readfds), "fdRead socket is not in the set\n"); 4056 tmp_buf[0] = 0xAF; 4057 SetLastError(0xdeadbeef); 4058 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB); 4059 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret); 4060 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError()); 4061 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0); 4062 ok(ret == 1, "expected 1, got %d\n", ret); 4063 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]); 4064 4065 /* When the connection is closed the socket is set in the read descriptor */ 4066 ret = closesocket(fdRead); 4067 ok(ret == 0, "expected 0, got %d\n", ret); 4068 FD_ZERO_ALL(); 4069 FD_SET_ALL(fdListen); 4070 FD_SET(fdWrite, &readfds); 4071 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4072 ok(ret == 1, "expected 1, got %d\n", ret); 4073 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 4074 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0); 4075 ok(ret == 0, "expected 0, got %d\n", ret); 4076 4077 /* When a connection is attempted to a non-listening socket it will get to the except descriptor */ 4078 ret = closesocket(fdWrite); 4079 ok(ret == 0, "expected 0, got %d\n", ret); 4080 ret = closesocket(fdListen); 4081 ok(ret == 0, "expected 0, got %d\n", ret); 4082 len = sizeof(address); 4083 fdWrite = setup_connector_socket(&address, len, TRUE); 4084 FD_ZERO_ALL(); 4085 FD_SET(fdWrite, &writefds); 4086 FD_SET(fdWrite, &exceptfds); 4087 select_timeout.tv_sec = 2; /* requires more time to realize it will not connect */ 4088 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4089 ok(ret == 1, "expected 1, got %d\n", ret); 4090 len = sizeof(id); 4091 id = 0xdeadbeef; 4092 ret = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len); 4093 ok(!ret, "getsockopt failed with %d\n", WSAGetLastError()); 4094 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id); 4095 ok(FD_ISSET(fdWrite, &exceptfds), "fdWrite socket is not in the set\n"); 4096 ok(select_timeout.tv_usec == 250000, "select timeout should not have changed\n"); 4097 closesocket(fdWrite); 4098 4099 /* Try select() on a closed socket after connection */ 4100 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 4101 closesocket(fdRead); 4102 FD_ZERO_ALL(); 4103 FD_SET_ALL(fdWrite); 4104 FD_SET_ALL(fdRead); 4105 SetLastError(0xdeadbeef); 4106 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout); 4107 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 4108 todo_wine 4109 ok(GetLastError() == WSAENOTSOCK, "expected 10038, got %d\n", GetLastError()); 4110 /* descriptor sets are unchanged */ 4111 ok(readfds.fd_count == 2, "expected 2, got %d\n", readfds.fd_count); 4112 ok(exceptfds.fd_count == 2, "expected 2, got %d\n", exceptfds.fd_count); 4113 closesocket(fdWrite); 4114 4115 #if ROSTESTS_233_IS_FIXED 4116 4117 /* Close the socket currently being selected in a thread - bug 38399 */ 4118 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 4119 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id); 4120 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError()); 4121 FD_ZERO_ALL(); 4122 FD_SET_ALL(fdWrite); 4123 ret = select(0, &readfds, NULL, &exceptfds, &select_timeout); 4124 ok(ret == 1, "expected 1, got %d\n", ret); 4125 ok(FD_ISSET(fdWrite, &readfds), "fdWrite socket is not in the set\n"); 4126 WaitForSingleObject (thread_handle, 1000); 4127 closesocket(fdRead); 4128 /* test again with only the except descriptor */ 4129 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 4130 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id); 4131 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError()); 4132 FD_ZERO_ALL(); 4133 FD_SET(fdWrite, &exceptfds); 4134 SetLastError(0xdeadbeef); 4135 ret = select(0, NULL, NULL, &exceptfds, &select_timeout); 4136 todo_wine 4137 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 4138 todo_wine 4139 ok(GetLastError() == WSAENOTSOCK, "expected 10038, got %d\n", GetLastError()); 4140 WaitForSingleObject (thread_handle, 1000); 4141 closesocket(fdRead); 4142 4143 #endif /* ROSTESTS_233_IS_FIXED */ 4144 4145 /* test UDP behavior of unbound sockets */ 4146 select_timeout.tv_sec = 0; 4147 select_timeout.tv_usec = 250000; 4148 fdWrite = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 4149 ok(fdWrite != INVALID_SOCKET, "socket call failed\n"); 4150 FD_ZERO_ALL(); 4151 FD_SET_ALL(fdWrite); 4152 ret = select(0, &readfds, &writefds, &exceptfds, &select_timeout); 4153 ok(ret == 1, "expected 1, got %d\n", ret); 4154 ok(FD_ISSET(fdWrite, &writefds), "fdWrite socket is not in the set\n"); 4155 closesocket(fdWrite); 4156 } 4157 #undef FD_SET_ALL 4158 #undef FD_ZERO_ALL 4159 4160 static DWORD WINAPI AcceptKillThread(void *param) 4161 { 4162 select_thread_params *par = param; 4163 struct sockaddr_in address; 4164 int len = sizeof(address); 4165 SOCKET client_socket; 4166 4167 SetEvent(server_ready); 4168 client_socket = accept(par->s, (struct sockaddr*) &address, &len); 4169 if (client_socket != INVALID_SOCKET) 4170 closesocket(client_socket); 4171 par->ReadKilled = (client_socket == INVALID_SOCKET); 4172 return 0; 4173 } 4174 4175 4176 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos, 4177 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData, 4178 GROUP *g, DWORD_PTR dwCallbackData) 4179 { 4180 return CF_DEFER; 4181 } 4182 4183 static SOCKET setup_server_socket(struct sockaddr_in *addr, int *len) 4184 { 4185 int ret, val; 4186 SOCKET server_socket; 4187 4188 server_socket = socket(AF_INET, SOCK_STREAM, 0); 4189 if (server_socket == INVALID_SOCKET) 4190 { 4191 trace("error creating server socket: %d\n", WSAGetLastError()); 4192 return INVALID_SOCKET; 4193 } 4194 4195 val = 1; 4196 ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); 4197 if (ret) 4198 { 4199 trace("error setting SO_REUSEADDR: %d\n", WSAGetLastError()); 4200 closesocket(server_socket); 4201 return INVALID_SOCKET; 4202 } 4203 4204 ret = bind(server_socket, (struct sockaddr *)addr, *len); 4205 if (ret) 4206 { 4207 trace("error binding server socket: %d\n", WSAGetLastError()); 4208 } 4209 4210 ret = getsockname(server_socket, (struct sockaddr *)addr, len); 4211 if (ret) 4212 { 4213 skip("failed to lookup bind address: %d\n", WSAGetLastError()); 4214 closesocket(server_socket); 4215 return INVALID_SOCKET; 4216 } 4217 4218 ret = listen(server_socket, 5); 4219 if (ret) 4220 { 4221 trace("error making server socket listen: %d\n", WSAGetLastError()); 4222 closesocket(server_socket); 4223 return INVALID_SOCKET; 4224 } 4225 4226 return server_socket; 4227 } 4228 4229 static SOCKET setup_connector_socket(struct sockaddr_in *addr, int len, BOOL nonblock) 4230 { 4231 int ret; 4232 SOCKET connector; 4233 4234 connector = socket(AF_INET, SOCK_STREAM, 0); 4235 ok(connector != INVALID_SOCKET, "failed to create connector socket %d\n", WSAGetLastError()); 4236 4237 if (nonblock) 4238 set_blocking(connector, !nonblock); 4239 4240 ret = connect(connector, (struct sockaddr *)addr, len); 4241 if (!nonblock) 4242 ok(!ret, "connecting to accepting socket failed %d\n", WSAGetLastError()); 4243 else if (ret == SOCKET_ERROR) 4244 { 4245 DWORD error = WSAGetLastError(); 4246 ok(error == WSAEWOULDBLOCK || error == WSAEINPROGRESS, 4247 "expected 10035 or 10036, got %d\n", error); 4248 } 4249 4250 return connector; 4251 } 4252 4253 static void test_accept(void) 4254 { 4255 int ret; 4256 SOCKET server_socket, accepted = INVALID_SOCKET, connector; 4257 struct sockaddr_in address; 4258 SOCKADDR_STORAGE ss, ss_empty; 4259 int socklen; 4260 select_thread_params thread_params; 4261 HANDLE thread_handle = NULL; 4262 DWORD id; 4263 4264 memset(&address, 0, sizeof(address)); 4265 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 4266 address.sin_family = AF_INET; 4267 4268 socklen = sizeof(address); 4269 server_socket = setup_server_socket(&address, &socklen); 4270 if (server_socket == INVALID_SOCKET) 4271 { 4272 trace("error creating server socket: %d\n", WSAGetLastError()); 4273 return; 4274 } 4275 4276 connector = setup_connector_socket(&address, socklen, FALSE); 4277 if (connector == INVALID_SOCKET) goto done; 4278 4279 trace("Blocking accept next\n"); 4280 4281 accepted = WSAAccept(server_socket, NULL, NULL, AlwaysDeferConditionFunc, 0); 4282 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError()); 4283 4284 accepted = accept(server_socket, NULL, 0); 4285 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError()); 4286 4287 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL); 4288 if (server_ready == INVALID_HANDLE_VALUE) 4289 { 4290 trace("error creating event: %d\n", GetLastError()); 4291 goto done; 4292 } 4293 4294 thread_params.s = server_socket; 4295 thread_params.ReadKilled = FALSE; 4296 thread_handle = CreateThread(NULL, 0, AcceptKillThread, &thread_params, 0, &id); 4297 if (thread_handle == NULL) 4298 { 4299 trace("error creating thread: %d\n", GetLastError()); 4300 goto done; 4301 } 4302 4303 WaitForSingleObject(server_ready, INFINITE); 4304 Sleep(200); 4305 ret = closesocket(server_socket); 4306 if (ret != 0) 4307 { 4308 trace("closesocket failed: %d\n", WSAGetLastError()); 4309 goto done; 4310 } 4311 4312 WaitForSingleObject(thread_handle, 1000); 4313 ok(thread_params.ReadKilled, "closesocket did not wake up accept\n"); 4314 4315 closesocket(accepted); 4316 closesocket(connector); 4317 accepted = connector = INVALID_SOCKET; 4318 4319 socklen = sizeof(address); 4320 server_socket = setup_server_socket(&address, &socklen); 4321 if (server_socket == INVALID_SOCKET) goto done; 4322 4323 connector = setup_connector_socket(&address, socklen, FALSE); 4324 if (connector == INVALID_SOCKET) goto done; 4325 4326 socklen = 0; 4327 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0); 4328 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError()); 4329 ok(!socklen, "got %d\n", socklen); 4330 closesocket(connector); 4331 connector = INVALID_SOCKET; 4332 4333 socklen = sizeof(address); 4334 connector = setup_connector_socket(&address, socklen, FALSE); 4335 if (connector == INVALID_SOCKET) goto done; 4336 4337 accepted = WSAAccept(server_socket, NULL, NULL, NULL, 0); 4338 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError()); 4339 closesocket(accepted); 4340 closesocket(connector); 4341 accepted = connector = INVALID_SOCKET; 4342 4343 socklen = sizeof(address); 4344 connector = setup_connector_socket(&address, socklen, FALSE); 4345 if (connector == INVALID_SOCKET) goto done; 4346 4347 socklen = sizeof(ss); 4348 memset(&ss, 0, sizeof(ss)); 4349 accepted = WSAAccept(server_socket, (struct sockaddr *)&ss, &socklen, NULL, 0); 4350 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError()); 4351 ok(socklen != sizeof(ss), "unexpected length\n"); 4352 ok(ss.ss_family, "family not set\n"); 4353 closesocket(accepted); 4354 closesocket(connector); 4355 accepted = connector = INVALID_SOCKET; 4356 4357 socklen = sizeof(address); 4358 connector = setup_connector_socket(&address, socklen, FALSE); 4359 if (connector == INVALID_SOCKET) goto done; 4360 4361 socklen = 0; 4362 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen); 4363 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSAEFAULT, "got %d\n", WSAGetLastError()); 4364 ok(!socklen, "got %d\n", socklen); 4365 closesocket(connector); 4366 accepted = connector = INVALID_SOCKET; 4367 4368 socklen = sizeof(address); 4369 connector = setup_connector_socket(&address, socklen, FALSE); 4370 if (connector == INVALID_SOCKET) goto done; 4371 4372 accepted = accept(server_socket, NULL, NULL); 4373 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError()); 4374 closesocket(accepted); 4375 closesocket(connector); 4376 accepted = connector = INVALID_SOCKET; 4377 4378 socklen = sizeof(address); 4379 connector = setup_connector_socket(&address, socklen, FALSE); 4380 if (connector == INVALID_SOCKET) goto done; 4381 4382 socklen = sizeof(ss); 4383 memset(&ss, 0, sizeof(ss)); 4384 accepted = accept(server_socket, (struct sockaddr *)&ss, &socklen); 4385 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError()); 4386 ok(socklen != sizeof(ss), "unexpected length\n"); 4387 ok(ss.ss_family, "family not set\n"); 4388 closesocket(accepted); 4389 closesocket(connector); 4390 accepted = connector = INVALID_SOCKET; 4391 4392 socklen = sizeof(address); 4393 connector = setup_connector_socket(&address, socklen, FALSE); 4394 if (connector == INVALID_SOCKET) goto done; 4395 4396 memset(&ss, 0, sizeof(ss)); 4397 memset(&ss_empty, 0, sizeof(ss_empty)); 4398 accepted = accept(server_socket, (struct sockaddr *)&ss, NULL); 4399 ok(accepted != INVALID_SOCKET, "Failed to accept connection, %d\n", WSAGetLastError()); 4400 ok(!memcmp(&ss, &ss_empty, sizeof(ss)), "structure is different\n"); 4401 4402 done: 4403 if (accepted != INVALID_SOCKET) 4404 closesocket(accepted); 4405 if (connector != INVALID_SOCKET) 4406 closesocket(connector); 4407 if (thread_handle != NULL) 4408 CloseHandle(thread_handle); 4409 if (server_ready != INVALID_HANDLE_VALUE) 4410 CloseHandle(server_ready); 4411 if (server_socket != INVALID_SOCKET) 4412 closesocket(server_socket); 4413 } 4414 4415 static void test_extendedSocketOptions(void) 4416 { 4417 WSADATA wsa; 4418 SOCKET sock; 4419 struct sockaddr_in sa; 4420 int sa_len = sizeof(struct sockaddr_in); 4421 int optval, optlen = sizeof(int), ret; 4422 BOOL bool_opt_val; 4423 LINGER linger_val; 4424 4425 if(WSAStartup(MAKEWORD(2,0), &wsa)){ 4426 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError()); 4427 return; 4428 } 4429 4430 memset(&sa, 0, sa_len); 4431 4432 sa.sin_family = AF_INET; 4433 sa.sin_port = htons(0); 4434 sa.sin_addr.s_addr = htonl(INADDR_ANY); 4435 4436 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) { 4437 trace("Creating the socket failed: %d\n", WSAGetLastError()); 4438 WSACleanup(); 4439 return; 4440 } 4441 4442 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){ 4443 trace("Failed to bind socket: %d\n", WSAGetLastError()); 4444 closesocket(sock); 4445 WSACleanup(); 4446 return; 4447 } 4448 4449 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4450 4451 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret); 4452 ok((optval == 65507) || (optval == 65527), 4453 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval); 4454 4455 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */ 4456 SetLastError(0xdeadbeef); 4457 optval = 0xdeadbeef; 4458 optlen = sizeof(int); 4459 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4460 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 4461 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n", 4462 ret, WSAGetLastError(), optval, optval); 4463 4464 /* more invalid values for level */ 4465 SetLastError(0xdeadbeef); 4466 optval = 0xdeadbeef; 4467 optlen = sizeof(int); 4468 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4469 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 4470 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n", 4471 ret, WSAGetLastError(), optval, optval); 4472 4473 SetLastError(0xdeadbeef); 4474 optval = 0xdeadbeef; 4475 optlen = sizeof(int); 4476 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4477 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 4478 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n", 4479 ret, WSAGetLastError(), optval, optval); 4480 4481 SetLastError(0xdeadbeef); 4482 optval = 0xdeadbeef; 4483 optlen = sizeof(int); 4484 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4485 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 4486 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n", 4487 ret, WSAGetLastError(), optval, optval); 4488 4489 SetLastError(0xdeadbeef); 4490 optval = 0xdeadbeef; 4491 optlen = sizeof(int); 4492 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); 4493 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL), 4494 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n", 4495 ret, WSAGetLastError(), optval, optval); 4496 4497 SetLastError(0xdeadbeef); 4498 optlen = sizeof(LINGER); 4499 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen); 4500 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAENOPROTOOPT), 4501 "getsockopt should fail for UDP sockets setting last error to WSAENOPROTOOPT, got %d with %d\n", 4502 ret, WSAGetLastError()); 4503 closesocket(sock); 4504 4505 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) { 4506 trace("Creating the socket failed: %d\n", WSAGetLastError()); 4507 WSACleanup(); 4508 return; 4509 } 4510 4511 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){ 4512 trace("Failed to bind socket: %d\n", WSAGetLastError()); 4513 closesocket(sock); 4514 WSACleanup(); 4515 return; 4516 } 4517 4518 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen); 4519 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret); 4520 4521 optlen = sizeof(BOOL); 4522 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen); 4523 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret); 4524 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val), 4525 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n", 4526 bool_opt_val, linger_val.l_onoff); 4527 4528 closesocket(sock); 4529 WSACleanup(); 4530 } 4531 4532 static void test_getsockname(void) 4533 { 4534 WSADATA wsa; 4535 SOCKET sock; 4536 struct sockaddr_in sa_set, sa_get; 4537 int sa_set_len = sizeof(struct sockaddr_in); 4538 int sa_get_len = sa_set_len; 4539 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0}; 4540 int ret; 4541 struct hostent *h; 4542 4543 if(WSAStartup(MAKEWORD(2,0), &wsa)){ 4544 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError()); 4545 return; 4546 } 4547 4548 memset(&sa_set, 0, sa_set_len); 4549 4550 sa_set.sin_family = AF_INET; 4551 sa_set.sin_port = htons(0); 4552 sa_set.sin_addr.s_addr = htonl(INADDR_ANY); 4553 4554 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) { 4555 trace("Creating the socket failed: %d\n", WSAGetLastError()); 4556 WSACleanup(); 4557 return; 4558 } 4559 4560 sa_get = sa_set; 4561 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0) 4562 ok(0, "getsockname on unbound socket should fail\n"); 4563 else { 4564 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket " 4565 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL); 4566 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0, 4567 "failed getsockname modified sockaddr when it shouldn't\n"); 4568 } 4569 4570 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){ 4571 trace("Failed to bind socket: %d\n", WSAGetLastError()); 4572 closesocket(sock); 4573 WSACleanup(); 4574 return; 4575 } 4576 4577 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){ 4578 trace("Failed to call getsockname: %d\n", WSAGetLastError()); 4579 closesocket(sock); 4580 WSACleanup(); 4581 return; 4582 } 4583 4584 ret = memcmp(sa_get.sin_zero, null_padding, 8); 4585 ok(ret == 0, "getsockname did not zero the sockaddr_in structure\n"); 4586 4587 closesocket(sock); 4588 4589 h = gethostbyname(""); 4590 if (h && h->h_length == 4) /* this test is only meaningful in IPv4 */ 4591 { 4592 int i; 4593 for (i = 0; h->h_addr_list[i]; i++) 4594 { 4595 char ipstr[32]; 4596 struct in_addr ip; 4597 ip.s_addr = *(ULONG *) h->h_addr_list[i]; 4598 4599 sock = socket(AF_INET, SOCK_DGRAM, 0); 4600 ok(sock != INVALID_SOCKET, "socket failed with %d\n", GetLastError()); 4601 4602 memset(&sa_set, 0, sizeof(sa_set)); 4603 sa_set.sin_family = AF_INET; 4604 sa_set.sin_addr.s_addr = ip.s_addr; 4605 /* The same address we bind must be the same address we get */ 4606 ret = bind(sock, (struct sockaddr*)&sa_set, sizeof(sa_set)); 4607 ok(ret == 0, "bind failed with %d\n", GetLastError()); 4608 sa_get_len = sizeof(sa_get); 4609 ret = getsockname(sock, (struct sockaddr*)&sa_get, &sa_get_len); 4610 ok(ret == 0, "getsockname failed with %d\n", GetLastError()); 4611 strcpy(ipstr, inet_ntoa(sa_get.sin_addr)); 4612 trace("testing bind on interface %s\n", ipstr); 4613 ok(sa_get.sin_addr.s_addr == sa_set.sin_addr.s_addr, 4614 "address does not match: %s != %s\n", ipstr, inet_ntoa(sa_set.sin_addr)); 4615 4616 closesocket(sock); 4617 } 4618 } 4619 4620 WSACleanup(); 4621 } 4622 4623 static void test_dns(void) 4624 { 4625 struct hostent *h; 4626 union memaddress 4627 { 4628 char *chr; 4629 void *mem; 4630 } addr; 4631 char **ptr; 4632 int acount; 4633 4634 h = gethostbyname(""); 4635 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno); 4636 4637 /* Use an address with valid alias names if possible */ 4638 h = gethostbyname("source.winehq.org"); 4639 if(!h) 4640 { 4641 skip("Can't test the hostent structure because gethostbyname failed\n"); 4642 return; 4643 } 4644 4645 /* The returned struct must be allocated in a very strict way. First we need to 4646 * count how many aliases there are because they must be located right after 4647 * the struct hostent size. Knowing the amount of aliases we know the exact 4648 * location of the first IP returned. Rule valid for >= XP, for older OS's 4649 * it's somewhat the opposite. */ 4650 addr.mem = h + 1; 4651 if(h->h_addr_list == addr.mem) /* <= W2K */ 4652 { 4653 win_skip("Skipping hostent tests since this OS is unsupported\n"); 4654 return; 4655 } 4656 4657 ok(h->h_aliases == addr.mem, 4658 "hostent->h_aliases should be in %p, it is in %p\n", addr.mem, h->h_aliases); 4659 4660 for(ptr = h->h_aliases, acount = 1; *ptr; ptr++) acount++; 4661 addr.chr += sizeof(*ptr) * acount; 4662 ok(h->h_addr_list == addr.mem, 4663 "hostent->h_addr_list should be in %p, it is in %p\n", addr.mem, h->h_addr_list); 4664 4665 for(ptr = h->h_addr_list, acount = 1; *ptr; ptr++) acount++; 4666 4667 addr.chr += sizeof(*ptr) * acount; 4668 ok(h->h_addr_list[0] == addr.mem, 4669 "hostent->h_addr_list[0] should be in %p, it is in %p\n", addr.mem, h->h_addr_list[0]); 4670 } 4671 4672 #ifndef __REACTOS__ 4673 /* Our winsock headers don't define gethostname because it conflicts with the 4674 * definition in unistd.h. Define it here to get rid of the warning. */ 4675 4676 int WINAPI gethostname(char *name, int namelen); 4677 #endif 4678 4679 static void test_gethostbyname(void) 4680 { 4681 struct hostent *he; 4682 struct in_addr **addr_list; 4683 char name[256], first_ip[16]; 4684 int ret, i, count; 4685 PMIB_IPFORWARDTABLE routes = NULL; 4686 PIP_ADAPTER_INFO adapters = NULL, k; 4687 DWORD adap_size = 0, route_size = 0; 4688 BOOL found_default = FALSE; 4689 BOOL local_ip = FALSE; 4690 4691 ret = gethostname(name, sizeof(name)); 4692 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError()); 4693 4694 he = gethostbyname(name); 4695 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError()); 4696 #ifdef __REACTOS__ /* ROSTESTS-233 */ 4697 count = 0; 4698 if (he != NULL) 4699 { 4700 #endif 4701 addr_list = (struct in_addr **)he->h_addr_list; 4702 strcpy(first_ip, inet_ntoa(*addr_list[0])); 4703 4704 trace("List of local IPs:\n"); 4705 for(count = 0; addr_list[count] != NULL; count++) 4706 { 4707 char *ip = inet_ntoa(*addr_list[count]); 4708 if (!strcmp(ip, "127.0.0.1")) 4709 local_ip = TRUE; 4710 trace("%s\n", ip); 4711 } 4712 #ifdef __REACTOS__ /* ROSTESTS-233 */ 4713 } 4714 #endif 4715 4716 if (local_ip) 4717 { 4718 ok (count == 1, "expected 127.0.0.1 to be the only IP returned\n"); 4719 skip("Only the loopback address is present, skipping tests\n"); 4720 return; 4721 } 4722 4723 if (!pGetAdaptersInfo || !pGetIpForwardTable) 4724 { 4725 win_skip("GetAdaptersInfo and/or GetIpForwardTable not found, skipping tests\n"); 4726 return; 4727 } 4728 4729 ret = pGetAdaptersInfo(NULL, &adap_size); 4730 ok (ret == ERROR_BUFFER_OVERFLOW, "GetAdaptersInfo failed with a different error: %d\n", ret); 4731 ret = pGetIpForwardTable(NULL, &route_size, FALSE); 4732 ok (ret == ERROR_INSUFFICIENT_BUFFER, "GetIpForwardTable failed with a different error: %d\n", ret); 4733 4734 adapters = HeapAlloc(GetProcessHeap(), 0, adap_size); 4735 routes = HeapAlloc(GetProcessHeap(), 0, route_size); 4736 4737 ret = pGetAdaptersInfo(adapters, &adap_size); 4738 ok (ret == NO_ERROR, "GetAdaptersInfo failed, error: %d\n", ret); 4739 ret = pGetIpForwardTable(routes, &route_size, FALSE); 4740 ok (ret == NO_ERROR, "GetIpForwardTable failed, error: %d\n", ret); 4741 4742 /* This test only has meaning if there is more than one IP configured */ 4743 if (adapters->Next == NULL && count == 1) 4744 { 4745 skip("Only one IP is present, skipping tests\n"); 4746 goto cleanup; 4747 } 4748 4749 for (i = 0; !found_default && i < routes->dwNumEntries; i++) 4750 { 4751 /* default route (ip 0.0.0.0) ? */ 4752 if (routes->table[i].dwForwardDest) continue; 4753 4754 for (k = adapters; k != NULL; k = k->Next) 4755 { 4756 char *ip; 4757 4758 if (k->Index != routes->table[i].dwForwardIfIndex) continue; 4759 4760 /* the first IP returned from gethostbyname must be a default route */ 4761 ip = k->IpAddressList.IpAddress.String; 4762 if (!strcmp(first_ip, ip)) 4763 { 4764 found_default = TRUE; 4765 break; 4766 } 4767 } 4768 } 4769 ok (found_default, "failed to find the first IP from gethostbyname!\n"); 4770 4771 cleanup: 4772 HeapFree(GetProcessHeap(), 0, adapters); 4773 HeapFree(GetProcessHeap(), 0, routes); 4774 } 4775 4776 static void test_gethostbyname_hack(void) 4777 { 4778 struct hostent *he; 4779 char name[256]; 4780 static BYTE loopback[] = {127, 0, 0, 1}; 4781 static BYTE magic_loopback[] = {127, 12, 34, 56}; 4782 int ret; 4783 4784 ret = gethostname(name, 256); 4785 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError()); 4786 4787 he = gethostbyname("localhost"); 4788 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno); 4789 if(he) 4790 { 4791 if(he->h_length != 4) 4792 { 4793 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length); 4794 return; 4795 } 4796 4797 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0, 4798 "gethostbyname(\"localhost\") returned %u.%u.%u.%u\n", 4799 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2], 4800 he->h_addr_list[0][3]); 4801 } 4802 4803 if(strcmp(name, "localhost") == 0) 4804 { 4805 skip("hostname seems to be \"localhost\", skipping test.\n"); 4806 return; 4807 } 4808 4809 he = gethostbyname(name); 4810 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno); 4811 if(he) 4812 { 4813 if(he->h_length != 4) 4814 { 4815 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length); 4816 return; 4817 } 4818 4819 if (he->h_addr_list[0][0] == 127) 4820 { 4821 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0, 4822 "gethostbyname(\"%s\") returned %u.%u.%u.%u not 127.12.34.56\n", 4823 name, he->h_addr_list[0][0], he->h_addr_list[0][1], 4824 he->h_addr_list[0][2], he->h_addr_list[0][3]); 4825 } 4826 } 4827 4828 gethostbyname("nonexistent.winehq.org"); 4829 /* Don't check for the return value, as some braindead ISPs will kindly 4830 * resolve nonexistent host names to addresses of the ISP's spam pages. */ 4831 } 4832 4833 static void test_gethostname(void) 4834 { 4835 struct hostent *he; 4836 char name[256]; 4837 int ret, len; 4838 4839 WSASetLastError(0xdeadbeef); 4840 ret = gethostname(NULL, 256); 4841 ok(ret == -1, "gethostname() returned %d\n", ret); 4842 ok(WSAGetLastError() == WSAEFAULT, "gethostname with null buffer " 4843 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT); 4844 4845 ret = gethostname(name, sizeof(name)); 4846 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError()); 4847 he = gethostbyname(name); 4848 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError()); 4849 4850 len = strlen(name); 4851 WSASetLastError(0xdeadbeef); 4852 strcpy(name, "deadbeef"); 4853 ret = gethostname(name, len); 4854 ok(ret == -1, "gethostname() returned %d\n", ret); 4855 ok(!strcmp(name, "deadbeef"), "name changed unexpected!\n"); 4856 ok(WSAGetLastError() == WSAEFAULT, "gethostname with insufficient length " 4857 "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT); 4858 4859 len++; 4860 ret = gethostname(name, len); 4861 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError()); 4862 he = gethostbyname(name); 4863 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError()); 4864 } 4865 4866 static void test_inet_addr(void) 4867 { 4868 u_long addr; 4869 4870 addr = inet_addr(NULL); 4871 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n"); 4872 } 4873 4874 static void test_addr_to_print(void) 4875 { 4876 char dst[16]; 4877 char dst6[64]; 4878 const char * pdst; 4879 struct in_addr in; 4880 struct in6_addr in6; 4881 4882 u_long addr0_Num = 0x00000000; 4883 PCSTR addr0_Str = "0.0.0.0"; 4884 u_long addr1_Num = 0x20201015; 4885 PCSTR addr1_Str = "21.16.32.32"; 4886 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74}; 4887 PCSTR addr2_Str = "::fffe:cc98:bd74"; 4888 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1}; 4889 PCSTR addr3_Str = "2030:a4b1::"; 4890 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74}; 4891 PCSTR addr4_Str = "::204.152.189.116"; 4892 4893 /* Test IPv4 addresses */ 4894 in.s_addr = addr0_Num; 4895 4896 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr)); 4897 ok(pdst != NULL, "inet_ntoa failed %s\n", dst); 4898 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str); 4899 4900 /* Test that inet_ntoa and inet_ntop return the same value */ 4901 in.S_un.S_addr = addr1_Num; 4902 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr)); 4903 ok(pdst != NULL, "inet_ntoa failed %s\n", dst); 4904 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str); 4905 4906 /* InetNtop became available in Vista and Win2008 */ 4907 if (!pInetNtop) 4908 { 4909 win_skip("InetNtop not present, not executing tests\n"); 4910 return; 4911 } 4912 4913 /* Second part of test */ 4914 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst)); 4915 ok(pdst != NULL, "InetNtop failed %s\n", dst); 4916 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str); 4917 4918 /* Test invalid parm conditions */ 4919 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst)); 4920 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4921 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n"); 4922 4923 /* Test Null destination */ 4924 pdst = NULL; 4925 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, NULL, sizeof(dst)); 4926 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4927 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4928 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4929 4930 /* Test zero length passed */ 4931 WSASetLastError(0); 4932 pdst = NULL; 4933 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 0); 4934 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4935 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4936 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4937 4938 /* Test length one shorter than the address length */ 4939 WSASetLastError(0); 4940 pdst = NULL; 4941 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 6); 4942 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4943 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4944 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4945 4946 /* Test longer length is ok */ 4947 WSASetLastError(0); 4948 pdst = NULL; 4949 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, sizeof(dst)+1); 4950 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst); 4951 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str); 4952 4953 /* Test the IPv6 addresses */ 4954 4955 /* Test an zero prefixed IPV6 address */ 4956 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num)); 4957 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6)); 4958 ok(pdst != NULL, "InetNtop failed %s\n", dst6); 4959 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str); 4960 4961 /* Test an zero suffixed IPV6 address */ 4962 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num)); 4963 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6)); 4964 ok(pdst != NULL, "InetNtop failed %s\n", dst6); 4965 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str); 4966 4967 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */ 4968 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num)); 4969 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, sizeof(dst6)); 4970 ok(pdst != NULL, "InetNtop failed %s\n", dst6); 4971 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str); 4972 4973 /* Test invalid parm conditions */ 4974 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num)); 4975 4976 /* Test Null destination */ 4977 pdst = NULL; 4978 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, NULL, sizeof(dst6)); 4979 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4980 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4981 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4982 4983 /* Test zero length passed */ 4984 WSASetLastError(0); 4985 pdst = NULL; 4986 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 0); 4987 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4988 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4989 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4990 4991 /* Test length one shorter than the address length */ 4992 WSASetLastError(0); 4993 pdst = NULL; 4994 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 16); 4995 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst); 4996 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */, 4997 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError()); 4998 4999 /* Test longer length is ok */ 5000 WSASetLastError(0); 5001 pdst = NULL; 5002 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 18); 5003 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst); 5004 } 5005 static void test_inet_pton(void) 5006 { 5007 struct TEST_DATA 5008 { 5009 int family, ret; 5010 DWORD err; 5011 const char *printable, *collapsed, *raw_data; 5012 } tests[] = { 5013 {AF_UNSPEC, -1, WSAEFAULT, /* Test 0 */ 5014 NULL, NULL, NULL}, 5015 {AF_INET, -1, WSAEFAULT, 5016 NULL, NULL, NULL}, 5017 {AF_INET6, -1, WSAEFAULT, 5018 NULL, NULL, NULL}, 5019 {AF_UNSPEC, -1, WSAEAFNOSUPPORT, 5020 "127.0.0.1", NULL, NULL}, 5021 {AF_INET, 1, 0, 5022 "127.0.0.1", "127.0.0.1", 5023 "\x7f\x00\x00\x01"}, 5024 {AF_INET6, 0, 0, 5025 "127.0.0.1", "127.0.0.1", NULL}, 5026 {AF_INET, 0, 0, 5027 "::1/128", NULL, NULL}, 5028 {AF_INET6, 0, 0, 5029 "::1/128", NULL, NULL}, 5030 {AF_UNSPEC, -1, WSAEAFNOSUPPORT, 5031 "broken", NULL, NULL}, 5032 {AF_INET, 0, 0, 5033 "broken", NULL, NULL}, 5034 {AF_INET6, 0, 0, /* Test 10 */ 5035 "broken", NULL, NULL}, 5036 {AF_UNSPEC, -1, WSAEAFNOSUPPORT, 5037 "177.32.45.20", NULL, NULL}, 5038 {AF_INET, 1, 0, 5039 "177.32.45.20", "177.32.45.20", 5040 "\xb1\x20\x2d\x14"}, 5041 {AF_INET6, 0, 0, 5042 "177.32.45.20", NULL, NULL}, 5043 {AF_INET, 0, 0, 5044 "2607:f0d0:1002:51::4", NULL, NULL}, 5045 {AF_INET6, 1, 0, 5046 "2607:f0d0:1002:51::4", "2607:f0d0:1002:51::4", 5047 "\x26\x07\xf0\xd0\x10\x02\x00\x51\x00\x00\x00\x00\x00\x00\x00\x04"}, 5048 {AF_INET, 0, 0, 5049 "::177.32.45.20", NULL, NULL}, 5050 {AF_INET6, 1, 0, 5051 "::177.32.45.20", "::177.32.45.20", 5052 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb1\x20\x2d\x14"}, 5053 {AF_INET, 0, 0, 5054 "fe80::0202:b3ff:fe1e:8329", NULL, NULL}, 5055 {AF_INET6, 1, 0, 5056 "fe80::0202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329", 5057 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"}, 5058 {AF_INET6, 1, 0, /* Test 20 */ 5059 "fe80::202:b3ff:fe1e:8329", "fe80::202:b3ff:fe1e:8329", 5060 "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"}, 5061 {AF_INET, 0, 0, 5062 "a", NULL, NULL}, 5063 {AF_INET, 0, 0, 5064 "a.b", NULL, NULL}, 5065 {AF_INET, 0, 0, 5066 "a.b.c", NULL, NULL}, 5067 {AF_INET, 0, 0, 5068 "a.b.c.d", NULL, NULL}, 5069 {AF_INET6, 1, 0, 5070 "2001:cdba:0000:0000:0000:0000:3257:9652", "2001:cdba::3257:9652", 5071 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"}, 5072 {AF_INET6, 1, 0, 5073 "2001:cdba::3257:9652", "2001:cdba::3257:9652", 5074 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"}, 5075 {AF_INET6, 1, 0, 5076 "2001:cdba:0:0:0:0:3257:9652", "2001:cdba::3257:9652", 5077 "\x20\x01\xcd\xba\x00\x00\x00\x00\x00\x00\x00\x00\x32\x57\x96\x52"} 5078 }; 5079 int i, ret; 5080 DWORD err; 5081 char buffer[64],str[64]; 5082 WCHAR printableW[64], collapsedW[64]; 5083 const char *ptr; 5084 const WCHAR *ptrW; 5085 5086 /* InetNtop and InetPton became available in Vista and Win2008 */ 5087 if (!pInetNtop || !pInetNtopW || !pInetPtonA || !pInetPtonW) 5088 { 5089 win_skip("InetNtop and/or InetPton not present, not executing tests\n"); 5090 return; 5091 } 5092 5093 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) 5094 { 5095 WSASetLastError(0xdeadbeef); 5096 ret = pInetPtonA(tests[i].family, tests[i].printable, buffer); 5097 ok (ret == tests[i].ret, "Test [%d]: Expected %d, got %d\n", i, tests[i].ret, ret); 5098 if (tests[i].ret == -1) 5099 { 5100 err = WSAGetLastError(); 5101 ok (tests[i].err == err, "Test [%d]: Expected 0x%x, got 0x%x\n", i, tests[i].err, err); 5102 } 5103 if (tests[i].ret != 1) continue; 5104 ok (memcmp(buffer, tests[i].raw_data, 5105 tests[i].family == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr)) == 0, 5106 "Test [%d]: Expected binary data differs\n", i); 5107 5108 /* Test the result from Pton with Ntop */ 5109 strcpy (str, "deadbeef"); 5110 ptr = pInetNtop(tests[i].family, buffer, str, sizeof(str)); 5111 ok (ptr != NULL, "Test [%d]: Failed with NULL\n", i); 5112 ok (ptr == str, "Test [%d]: Pointers differ (%p != %p)\n", i, ptr, str); 5113 if (!ptr) continue; 5114 ok (strcmp(ptr, tests[i].collapsed) == 0, "Test [%d]: Expected '%s', got '%s'\n", 5115 i, tests[i].collapsed, ptr); 5116 } 5117 5118 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) 5119 { 5120 if (tests[i].printable) 5121 MultiByteToWideChar(CP_ACP, 0, tests[i].printable, -1, printableW, 5122 sizeof(printableW) / sizeof(printableW[0])); 5123 WSASetLastError(0xdeadbeef); 5124 ret = pInetPtonW(tests[i].family, tests[i].printable ? printableW : NULL, buffer); 5125 ok(ret == tests[i].ret, "Test [%d]: Expected %d, got %d\n", i, tests[i].ret, ret); 5126 if (tests[i].ret == -1) 5127 { 5128 err = WSAGetLastError(); 5129 ok(tests[i].err == err, "Test [%d]: Expected 0x%x, got 0x%x\n", i, tests[i].err, err); 5130 } 5131 if (tests[i].ret != 1) continue; 5132 ok(memcmp(buffer, tests[i].raw_data, 5133 tests[i].family == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr)) == 0, 5134 "Test [%d]: Expected binary data differs\n", i); 5135 5136 /* Test the result from Pton with Ntop */ 5137 printableW[0] = 0xdead; 5138 ptrW = pInetNtopW(tests[i].family, buffer, printableW, sizeof(printableW) / sizeof(printableW[0])); 5139 ok (ptrW != NULL, "Test [%d]: Failed with NULL\n", i); 5140 ok (ptrW == printableW, "Test [%d]: Pointers differ (%p != %p)\n", i, ptrW, printableW); 5141 if (!ptrW) continue; 5142 5143 MultiByteToWideChar(CP_ACP, 0, tests[i].collapsed, -1, collapsedW, 5144 sizeof(collapsedW) / sizeof(collapsedW[0])); 5145 ok (lstrcmpW(ptrW, collapsedW) == 0, "Test [%d]: Expected '%s', got '%s'\n", 5146 i, tests[i].collapsed, wine_dbgstr_w(ptrW)); 5147 } 5148 } 5149 5150 static void test_ioctlsocket(void) 5151 { 5152 SOCKET sock, src, dst; 5153 struct tcp_keepalive kalive; 5154 struct sockaddr_in address; 5155 int ret, optval; 5156 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK}; 5157 UINT i, bytes_rec; 5158 char data; 5159 WSABUF bufs; 5160 u_long arg = 0; 5161 5162 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 5163 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError()); 5164 if(sock == INVALID_SOCKET) 5165 { 5166 skip("Can't continue without a socket.\n"); 5167 return; 5168 } 5169 5170 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++) 5171 { 5172 /* broken apps like defcon pass the argp value directly instead of a pointer to it */ 5173 ret = ioctlsocket(sock, cmds[i], (u_long *)1); 5174 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n"); 5175 ret = WSAGetLastError(); 5176 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret); 5177 } 5178 5179 /* A fresh and not connected socket has no urgent data, this test shows 5180 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */ 5181 5182 ret = ioctlsocket(sock, SIOCATMARK, &arg); 5183 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n"); 5184 ok(arg, "SIOCATMARK expected a non-zero value\n"); 5185 5186 /* when SO_OOBINLINE is set SIOCATMARK must always return TRUE */ 5187 optval = 1; 5188 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval)); 5189 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n"); 5190 arg = 0; 5191 ret = ioctlsocket(sock, SIOCATMARK, &arg); 5192 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n"); 5193 ok(arg, "SIOCATMARK expected a non-zero value\n"); 5194 5195 /* disable SO_OOBINLINE and get the same old behavior */ 5196 optval = 0; 5197 ret = setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval)); 5198 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n"); 5199 arg = 0; 5200 ret = ioctlsocket(sock, SIOCATMARK, &arg); 5201 ok(ret != SOCKET_ERROR, "ioctlsocket failed unexpectedly\n"); 5202 ok(arg, "SIOCATMARK expected a non-zero value\n"); 5203 5204 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &arg, 0, NULL, 0, &arg, NULL, NULL); 5205 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n"); 5206 ret = WSAGetLastError(); 5207 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret); 5208 5209 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, NULL, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5210 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n"); 5211 ret = WSAGetLastError(); 5212 ok(ret == WSAEFAULT || broken(ret == WSAEINVAL), "expected WSAEFAULT, got %d instead\n", ret); 5213 5214 make_keepalive(kalive, 0, 0, 0); 5215 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5216 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5217 5218 make_keepalive(kalive, 1, 0, 0); 5219 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5220 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5221 5222 make_keepalive(kalive, 1, 1000, 1000); 5223 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5224 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5225 5226 make_keepalive(kalive, 1, 10000, 10000); 5227 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5228 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5229 5230 make_keepalive(kalive, 1, 100, 100); 5231 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5232 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5233 5234 make_keepalive(kalive, 0, 100, 100); 5235 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &kalive, sizeof(struct tcp_keepalive), NULL, 0, &arg, NULL, NULL); 5236 ok(ret == 0, "WSAIoctl failed unexpectedly\n"); 5237 5238 closesocket(sock); 5239 5240 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 5241 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError()); 5242 if(sock == INVALID_SOCKET) 5243 { 5244 skip("Can't continue without a socket.\n"); 5245 return; 5246 } 5247 5248 /* test FIONREAD with a fresh and non-connected socket */ 5249 arg = 0xdeadbeef; 5250 ret = ioctlsocket(sock, FIONREAD, &arg); 5251 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError()); 5252 ok(arg == 0, "expected 0, got %u\n", arg); 5253 5254 memset(&address, 0, sizeof(address)); 5255 address.sin_family = AF_INET; 5256 address.sin_addr.s_addr = inet_addr( SERVERIP ); 5257 address.sin_port = htons( SERVERPORT ); 5258 ret = bind(sock, (struct sockaddr *)&address, sizeof(address)); 5259 ok(ret == 0, "bind failed unexpectedly with error %d\n", WSAGetLastError()); 5260 5261 ret = listen(sock, SOMAXCONN); 5262 ok(ret == 0, "listen failed unexpectedly with error %d\n", WSAGetLastError()); 5263 5264 /* test FIONREAD with listening socket */ 5265 arg = 0xdeadbeef; 5266 ret = ioctlsocket(sock, FIONREAD, &arg); 5267 ok(ret == 0, "ioctlsocket failed unexpectedly with error %d\n", WSAGetLastError()); 5268 ok(arg == 0, "expected 0, got %u\n", arg); 5269 5270 closesocket(sock); 5271 5272 if (tcp_socketpair(&src, &dst) != 0) 5273 { 5274 ok(0, "creating socket pair failed, skipping test\n"); 5275 return; 5276 } 5277 5278 /* test FIONREAD on TCP sockets */ 5279 optval = 0xdeadbeef; 5280 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL); 5281 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError()); 5282 ok(optval == 0, "FIONREAD should have returned 0 bytes, got %d instead\n", optval); 5283 5284 optval = 0xdeadbeef; 5285 ok(send(src, "TEST", 4, 0) == 4, "failed to send test data\n"); 5286 Sleep(100); 5287 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL); 5288 ok(ret == 0, "WSAIoctl failed unexpectedly with error %d\n", WSAGetLastError()); 5289 ok(optval == 4, "FIONREAD should have returned 4 bytes, got %d instead\n", optval); 5290 5291 /* trying to read from an OOB inlined socket with MSG_OOB results in WSAEINVAL */ 5292 set_blocking(dst, FALSE); 5293 i = MSG_OOB; 5294 SetLastError(0xdeadbeef); 5295 ret = recv(dst, &data, 1, i); 5296 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 5297 ret = GetLastError(); 5298 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret); 5299 bufs.len = sizeof(char); 5300 bufs.buf = &data; 5301 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL); 5302 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 5303 ret = GetLastError(); 5304 ok(ret == WSAEWOULDBLOCK, "expected 10035, got %d\n", ret); 5305 optval = 1; 5306 ret = setsockopt(dst, SOL_SOCKET, SO_OOBINLINE, (void *)&optval, sizeof(optval)); 5307 ok(ret != SOCKET_ERROR, "setsockopt failed unexpectedly\n"); 5308 i = MSG_OOB; 5309 SetLastError(0xdeadbeef); 5310 ret = recv(dst, &data, 1, i); 5311 ok(ret == SOCKET_ERROR, "expected SOCKET_ERROR, got %d\n", ret); 5312 ret = GetLastError(); 5313 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret); 5314 bufs.len = sizeof(char); 5315 bufs.buf = &data; 5316 ret = WSARecv(dst, &bufs, 1, &bytes_rec, &i, NULL, NULL); 5317 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 5318 ret = GetLastError(); 5319 ok(ret == WSAEINVAL, "expected 10022, got %d\n", ret); 5320 5321 closesocket(dst); 5322 optval = 0xdeadbeef; 5323 ret = WSAIoctl(dst, FIONREAD, NULL, 0, &optval, sizeof(optval), &arg, NULL, NULL); 5324 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n"); 5325 ok(optval == 0xdeadbeef, "FIONREAD should not have changed last error, got %d instead\n", optval); 5326 closesocket(src); 5327 } 5328 5329 static BOOL drain_pause = FALSE; 5330 static DWORD WINAPI drain_socket_thread(LPVOID arg) 5331 { 5332 char buffer[1024]; 5333 SOCKET sock = *(SOCKET*)arg; 5334 int ret; 5335 5336 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0) 5337 { 5338 if (ret < 0) 5339 { 5340 if (WSAGetLastError() == WSAEWOULDBLOCK) 5341 { 5342 fd_set readset; 5343 FD_ZERO(&readset); 5344 FD_SET(sock, &readset); 5345 select(sock+1, &readset, NULL, NULL, NULL); 5346 while (drain_pause) 5347 Sleep(100); 5348 } 5349 else 5350 break; 5351 } 5352 } 5353 return 0; 5354 } 5355 5356 static void test_send(void) 5357 { 5358 SOCKET src = INVALID_SOCKET; 5359 SOCKET dst = INVALID_SOCKET; 5360 HANDLE hThread = NULL; 5361 const int buflen = 1024*1024; 5362 char *buffer = NULL; 5363 int ret, i, zero = 0; 5364 WSABUF buf; 5365 OVERLAPPED ov; 5366 BOOL bret; 5367 DWORD id, bytes_sent, dwRet; 5368 DWORD expected_time, connect_time; 5369 socklen_t optlen; 5370 5371 memset(&ov, 0, sizeof(ov)); 5372 5373 if (tcp_socketpair(&src, &dst) != 0) 5374 { 5375 ok(0, "creating socket pair failed, skipping test\n"); 5376 return; 5377 } 5378 expected_time = GetTickCount(); 5379 5380 set_blocking(dst, FALSE); 5381 /* force disable buffering so we can get a pending overlapped request */ 5382 ret = setsockopt(dst, SOL_SOCKET, SO_SNDBUF, (char *) &zero, sizeof(zero)); 5383 ok(!ret, "setsockopt SO_SNDBUF failed: %d - %d\n", ret, GetLastError()); 5384 5385 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id); 5386 if (hThread == NULL) 5387 { 5388 ok(0, "CreateThread failed, error %d\n", GetLastError()); 5389 goto end; 5390 } 5391 5392 buffer = HeapAlloc(GetProcessHeap(), 0, buflen); 5393 if (buffer == NULL) 5394 { 5395 ok(0, "HeapAlloc failed, error %d\n", GetLastError()); 5396 goto end; 5397 } 5398 5399 /* fill the buffer with some nonsense */ 5400 for (i = 0; i < buflen; ++i) 5401 { 5402 buffer[i] = (char) i; 5403 } 5404 5405 ret = send(src, buffer, buflen, 0); 5406 if (ret >= 0) 5407 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret); 5408 else 5409 ok(0, "send failed, error %d\n", WSAGetLastError()); 5410 5411 buf.buf = buffer; 5412 buf.len = buflen; 5413 5414 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 5415 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError()); 5416 if (!ov.hEvent) 5417 goto end; 5418 5419 bytes_sent = 0; 5420 WSASetLastError(12345); 5421 ret = WSASend(dst, &buf, 1, &bytes_sent, 0, &ov, NULL); 5422 ok((ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING) || broken(bytes_sent == buflen), 5423 "Failed to start overlapped send %d - %d - %d/%d\n", ret, WSAGetLastError(), bytes_sent, buflen); 5424 5425 /* don't check for completion yet, we may need to drain the buffer while still sending */ 5426 set_blocking(src, FALSE); 5427 for (i = 0; i < buflen; ++i) 5428 { 5429 int j = 0; 5430 5431 ret = recv(src, buffer, 1, 0); 5432 while (ret == SOCKET_ERROR && GetLastError() == WSAEWOULDBLOCK && j < 100) 5433 { 5434 j++; 5435 Sleep(50); 5436 ret = recv(src, buffer, 1, 0); 5437 } 5438 5439 ok(ret == 1, "Failed to receive data %d - %d (got %d/%d)\n", ret, GetLastError(), i, buflen); 5440 if (ret != 1) 5441 break; 5442 5443 ok(buffer[0] == (char) i, "Received bad data at position %d\n", i); 5444 } 5445 5446 dwRet = WaitForSingleObject(ov.hEvent, 1000); 5447 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError()); 5448 if (dwRet == WAIT_OBJECT_0) 5449 { 5450 bret = GetOverlappedResult((HANDLE)dst, &ov, &bytes_sent, FALSE); 5451 ok(bret && bytes_sent == buflen, 5452 "Got %d instead of %d (%d - %d)\n", bytes_sent, buflen, bret, GetLastError()); 5453 } 5454 5455 WSASetLastError(12345); 5456 ret = WSASend(INVALID_SOCKET, &buf, 1, NULL, 0, &ov, NULL); 5457 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK, 5458 "WSASend failed %d - %d\n", ret, WSAGetLastError()); 5459 5460 WSASetLastError(12345); 5461 ret = WSASend(dst, &buf, 1, NULL, 0, &ov, NULL); 5462 ok(ret == SOCKET_ERROR && WSAGetLastError() == ERROR_IO_PENDING, 5463 "Failed to start overlapped send %d - %d\n", ret, WSAGetLastError()); 5464 5465 expected_time = (GetTickCount() - expected_time) / 1000; 5466 5467 connect_time = 0xdeadbeef; 5468 optlen = sizeof(connect_time); 5469 ret = getsockopt(dst, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen); 5470 ok(!ret, "getsockopt failed %d\n", WSAGetLastError()); 5471 ok(connect_time >= expected_time && connect_time <= expected_time + 1, 5472 "unexpected connect time %u, expected %u\n", connect_time, expected_time); 5473 5474 connect_time = 0xdeadbeef; 5475 optlen = sizeof(connect_time); 5476 ret = getsockopt(src, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen); 5477 ok(!ret, "getsockopt failed %d\n", WSAGetLastError()); 5478 ok(connect_time >= expected_time && connect_time <= expected_time + 1, 5479 "unexpected connect time %u, expected %u\n", connect_time, expected_time); 5480 5481 end: 5482 if (src != INVALID_SOCKET) 5483 closesocket(src); 5484 if (dst != INVALID_SOCKET) 5485 closesocket(dst); 5486 if (hThread != NULL) 5487 { 5488 dwRet = WaitForSingleObject(hThread, 500); 5489 ok(dwRet == WAIT_OBJECT_0, "failed to wait for thread termination: %d\n", GetLastError()); 5490 CloseHandle(hThread); 5491 } 5492 if (ov.hEvent) 5493 CloseHandle(ov.hEvent); 5494 HeapFree(GetProcessHeap(), 0, buffer); 5495 } 5496 5497 typedef struct async_message 5498 { 5499 SOCKET socket; 5500 LPARAM lparam; 5501 struct async_message *next; 5502 } async_message; 5503 5504 static struct async_message *messages_received; 5505 5506 #define WM_SOCKET (WM_USER+100) 5507 static LRESULT CALLBACK ws2_test_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 5508 { 5509 struct async_message *message; 5510 5511 switch (msg) 5512 { 5513 case WM_SOCKET: 5514 message = HeapAlloc(GetProcessHeap(), 0, sizeof(*message)); 5515 message->socket = (SOCKET) wparam; 5516 message->lparam = lparam; 5517 message->next = NULL; 5518 5519 if (messages_received) 5520 { 5521 struct async_message *last = messages_received; 5522 while (last->next) last = last->next; 5523 last->next = message; 5524 } 5525 else 5526 messages_received = message; 5527 return 0; 5528 } 5529 5530 return DefWindowProcA(hwnd, msg, wparam, lparam); 5531 } 5532 5533 static void get_event_details(int event, int *bit, char *name) 5534 { 5535 switch (event) 5536 { 5537 case FD_ACCEPT: 5538 if (bit) *bit = FD_ACCEPT_BIT; 5539 if (name) strcpy(name, "FD_ACCEPT"); 5540 break; 5541 case FD_CONNECT: 5542 if (bit) *bit = FD_CONNECT_BIT; 5543 if (name) strcpy(name, "FD_CONNECT"); 5544 break; 5545 case FD_READ: 5546 if (bit) *bit = FD_READ_BIT; 5547 if (name) strcpy(name, "FD_READ"); 5548 break; 5549 case FD_OOB: 5550 if (bit) *bit = FD_OOB_BIT; 5551 if (name) strcpy(name, "FD_OOB"); 5552 break; 5553 case FD_WRITE: 5554 if (bit) *bit = FD_WRITE_BIT; 5555 if (name) strcpy(name, "FD_WRITE"); 5556 break; 5557 case FD_CLOSE: 5558 if (bit) *bit = FD_CLOSE_BIT; 5559 if (name) strcpy(name, "FD_CLOSE"); 5560 break; 5561 default: 5562 if (bit) *bit = -1; 5563 if (name) sprintf(name, "bad%x", event); 5564 } 5565 } 5566 5567 static const char *dbgstr_event_seq(const LPARAM *seq) 5568 { 5569 static char message[1024]; 5570 char name[12]; 5571 int len = 1; 5572 5573 message[0] = '['; 5574 message[1] = 0; 5575 while (*seq) 5576 { 5577 get_event_details(WSAGETSELECTEVENT(*seq), NULL, name); 5578 len += sprintf(message + len, "%s(%d) ", name, WSAGETSELECTERROR(*seq)); 5579 seq++; 5580 } 5581 if (len > 1) len--; 5582 strcpy( message + len, "]" ); 5583 return message; 5584 } 5585 5586 static char *dbgstr_event_seq_result(SOCKET s, WSANETWORKEVENTS *netEvents) 5587 { 5588 static char message[1024]; 5589 struct async_message *curr = messages_received; 5590 int index, error, bit = 0; 5591 char name[12]; 5592 int len = 1; 5593 5594 message[0] = '['; 5595 message[1] = 0; 5596 while (1) 5597 { 5598 if (netEvents) 5599 { 5600 if (bit >= FD_MAX_EVENTS) break; 5601 if ( !(netEvents->lNetworkEvents & (1 << bit)) ) 5602 { 5603 bit++; 5604 continue; 5605 } 5606 get_event_details(1 << bit, &index, name); 5607 error = netEvents->iErrorCode[index]; 5608 bit++; 5609 } 5610 else 5611 { 5612 if (!curr) break; 5613 if (curr->socket != s) 5614 { 5615 curr = curr->next; 5616 continue; 5617 } 5618 get_event_details(WSAGETSELECTEVENT(curr->lparam), NULL, name); 5619 error = WSAGETSELECTERROR(curr->lparam); 5620 curr = curr->next; 5621 } 5622 5623 len += sprintf(message + len, "%s(%d) ", name, error); 5624 } 5625 if (len > 1) len--; 5626 strcpy( message + len, "]" ); 5627 return message; 5628 } 5629 5630 static void flush_events(SOCKET s, HANDLE hEvent) 5631 { 5632 WSANETWORKEVENTS netEvents; 5633 struct async_message *prev = NULL, *curr = messages_received; 5634 int ret; 5635 DWORD dwRet; 5636 5637 if (hEvent != INVALID_HANDLE_VALUE) 5638 { 5639 dwRet = WaitForSingleObject(hEvent, 100); 5640 if (dwRet == WAIT_OBJECT_0) 5641 { 5642 ret = WSAEnumNetworkEvents(s, hEvent, &netEvents); 5643 if (ret) 5644 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret); 5645 } 5646 } 5647 else 5648 { 5649 while (curr) 5650 { 5651 if (curr->socket == s) 5652 { 5653 if (prev) prev->next = curr->next; 5654 else messages_received = curr->next; 5655 5656 HeapFree(GetProcessHeap(), 0, curr); 5657 5658 if (prev) curr = prev->next; 5659 else curr = messages_received; 5660 } 5661 else 5662 { 5663 prev = curr; 5664 curr = curr->next; 5665 } 5666 } 5667 } 5668 } 5669 5670 static int match_event_sequence(SOCKET s, WSANETWORKEVENTS *netEvents, const LPARAM *seq) 5671 { 5672 int event, index, error, events; 5673 struct async_message *curr; 5674 5675 if (netEvents) 5676 { 5677 events = netEvents->lNetworkEvents; 5678 while (*seq) 5679 { 5680 event = WSAGETSELECTEVENT(*seq); 5681 error = WSAGETSELECTERROR(*seq); 5682 get_event_details(event, &index, NULL); 5683 5684 if (!(events & event) && index != -1) 5685 return 0; 5686 if (events & event && index != -1) 5687 { 5688 if (netEvents->iErrorCode[index] != error) 5689 return 0; 5690 } 5691 events &= ~event; 5692 seq++; 5693 } 5694 if (events) 5695 return 0; 5696 } 5697 else 5698 { 5699 curr = messages_received; 5700 while (curr) 5701 { 5702 if (curr->socket == s) 5703 { 5704 if (!*seq) return 0; 5705 if (*seq != curr->lparam) return 0; 5706 seq++; 5707 } 5708 curr = curr->next; 5709 } 5710 if (*seq) 5711 return 0; 5712 } 5713 return 1; 5714 } 5715 5716 /* checks for a sequence of events, (order only checked if window is used) */ 5717 static void ok_event_sequence(SOCKET s, HANDLE hEvent, const LPARAM *seq, const LPARAM **broken_seqs, int completelyBroken) 5718 { 5719 MSG msg; 5720 WSANETWORKEVENTS events, *netEvents = NULL; 5721 int ret; 5722 DWORD dwRet; 5723 5724 if (hEvent != INVALID_HANDLE_VALUE) 5725 { 5726 netEvents = &events; 5727 5728 dwRet = WaitForSingleObject(hEvent, 200); 5729 if (dwRet == WAIT_OBJECT_0) 5730 { 5731 ret = WSAEnumNetworkEvents(s, hEvent, netEvents); 5732 if (ret) 5733 { 5734 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret); 5735 return; 5736 } 5737 } 5738 else 5739 memset(netEvents, 0, sizeof(*netEvents)); 5740 } 5741 else 5742 { 5743 Sleep(200); 5744 /* Run the message loop a little */ 5745 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) 5746 { 5747 DispatchMessageA(&msg); 5748 } 5749 } 5750 5751 if (match_event_sequence(s, netEvents, seq)) 5752 { 5753 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq)); 5754 flush_events(s, hEvent); 5755 return; 5756 } 5757 5758 if (broken_seqs) 5759 { 5760 for (; *broken_seqs; broken_seqs++) 5761 { 5762 if (match_event_sequence(s, netEvents, *broken_seqs)) 5763 { 5764 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s, netEvents), dbgstr_event_seq(seq)); 5765 flush_events(s, hEvent); 5766 return; 5767 } 5768 } 5769 } 5770 5771 winetest_ok(broken(completelyBroken), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq), 5772 dbgstr_event_seq_result(s, netEvents)); 5773 flush_events(s, hEvent); 5774 } 5775 5776 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence 5777 5778 static void test_events(int useMessages) 5779 { 5780 SOCKET server = INVALID_SOCKET; 5781 SOCKET src = INVALID_SOCKET, src2 = INVALID_SOCKET; 5782 SOCKET dst = INVALID_SOCKET, dst2 = INVALID_SOCKET; 5783 struct sockaddr_in addr; 5784 HANDLE hThread = NULL; 5785 HANDLE hEvent = INVALID_HANDLE_VALUE, hEvent2 = INVALID_HANDLE_VALUE; 5786 WNDCLASSEXA wndclass; 5787 HWND hWnd = NULL; 5788 char *buffer = NULL; 5789 int bufferSize = 1024*1024; 5790 WSABUF bufs; 5791 OVERLAPPED ov, ov2; 5792 DWORD flags = 0; 5793 DWORD bytesReturned; 5794 DWORD id; 5795 int len; 5796 int ret; 5797 DWORD dwRet; 5798 BOOL bret; 5799 static char szClassName[] = "wstestclass"; 5800 const LPARAM *broken_seq[3]; 5801 static const LPARAM empty_seq[] = { 0 }; 5802 static const LPARAM close_seq[] = { WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 }; 5803 static const LPARAM write_seq[] = { WSAMAKESELECTREPLY(FD_WRITE, 0), 0 }; 5804 static const LPARAM read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 0 }; 5805 static const LPARAM oob_seq[] = { WSAMAKESELECTREPLY(FD_OOB, 0), 0 }; 5806 static const LPARAM connect_seq[] = { WSAMAKESELECTREPLY(FD_CONNECT, 0), 5807 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 }; 5808 static const LPARAM read_read_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 5809 WSAMAKESELECTREPLY(FD_READ, 0), 0 }; 5810 static const LPARAM read_write_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 5811 WSAMAKESELECTREPLY(FD_WRITE, 0), 0 }; 5812 static const LPARAM read_close_seq[] = { WSAMAKESELECTREPLY(FD_READ, 0), 5813 WSAMAKESELECTREPLY(FD_CLOSE, 0), 0 }; 5814 5815 memset(&ov, 0, sizeof(ov)); 5816 memset(&ov2, 0, sizeof(ov2)); 5817 5818 /* don't use socketpair, we want connection event */ 5819 src = socket(AF_INET, SOCK_STREAM, 0); 5820 if (src == INVALID_SOCKET) 5821 { 5822 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5823 goto end; 5824 } 5825 5826 ret = set_blocking(src, TRUE); 5827 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 5828 5829 src2 = socket(AF_INET, SOCK_STREAM, 0); 5830 if (src2 == INVALID_SOCKET) 5831 { 5832 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5833 goto end; 5834 } 5835 5836 ret = set_blocking(src2, TRUE); 5837 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 5838 5839 len = sizeof(BOOL); 5840 if (getsockopt(src, SOL_SOCKET, SO_OOBINLINE, (void *)&bret, &len) == SOCKET_ERROR) 5841 { 5842 ok(0, "failed to get oobinline status, %d\n", GetLastError()); 5843 goto end; 5844 } 5845 ok(bret == FALSE, "OOB not inline\n"); 5846 5847 if (useMessages) 5848 { 5849 trace("Event test using messages\n"); 5850 5851 wndclass.cbSize = sizeof(wndclass); 5852 wndclass.style = CS_HREDRAW | CS_VREDRAW; 5853 wndclass.lpfnWndProc = ws2_test_WndProc; 5854 wndclass.cbClsExtra = 0; 5855 wndclass.cbWndExtra = 0; 5856 wndclass.hInstance = GetModuleHandleA(NULL); 5857 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION); 5858 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION); 5859 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW); 5860 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 5861 wndclass.lpszClassName = szClassName; 5862 wndclass.lpszMenuName = NULL; 5863 RegisterClassExA(&wndclass); 5864 5865 hWnd = CreateWindowA(szClassName, "WS2Test", WS_OVERLAPPEDWINDOW, 5866 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL); 5867 if (!hWnd) 5868 { 5869 ok(0, "failed to create window: %d\n", GetLastError()); 5870 return; 5871 } 5872 5873 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE); 5874 if (ret) 5875 { 5876 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 5877 goto end; 5878 } 5879 5880 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n"); 5881 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError()); 5882 5883 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE); 5884 if (ret) 5885 { 5886 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 5887 goto end; 5888 } 5889 5890 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n"); 5891 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError()); 5892 } 5893 else 5894 { 5895 trace("Event test using events\n"); 5896 5897 hEvent = WSACreateEvent(); 5898 if (hEvent == INVALID_HANDLE_VALUE) 5899 { 5900 ok(0, "WSACreateEvent failed, error %d\n", GetLastError()); 5901 goto end; 5902 } 5903 5904 hEvent2 = WSACreateEvent(); 5905 if (hEvent2 == INVALID_HANDLE_VALUE) 5906 { 5907 ok(0, "WSACreateEvent failed, error %d\n", GetLastError()); 5908 goto end; 5909 } 5910 5911 ret = WSAEventSelect(src, hEvent, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE); 5912 if (ret) 5913 { 5914 ok(0, "WSAEventSelect failed, error %d\n", ret); 5915 goto end; 5916 } 5917 5918 ok(set_blocking(src, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n"); 5919 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError()); 5920 5921 ret = WSAEventSelect(src2, hEvent2, FD_CONNECT | FD_READ | FD_OOB | FD_WRITE | FD_CLOSE); 5922 if (ret) 5923 { 5924 ok(0, "WSAEventSelect failed, error %d\n", ret); 5925 goto end; 5926 } 5927 5928 ok(set_blocking(src2, TRUE) == SOCKET_ERROR, "set_blocking should failed!\n"); 5929 ok(WSAGetLastError() == WSAEINVAL, "expect WSAEINVAL, returned %x\n", WSAGetLastError()); 5930 } 5931 5932 server = socket(AF_INET, SOCK_STREAM, 0); 5933 if (server == INVALID_SOCKET) 5934 { 5935 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5936 goto end; 5937 } 5938 5939 memset(&addr, 0, sizeof(addr)); 5940 addr.sin_family = AF_INET; 5941 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 5942 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr)); 5943 if (ret != 0) 5944 { 5945 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5946 goto end; 5947 } 5948 5949 len = sizeof(addr); 5950 ret = getsockname(server, (struct sockaddr*)&addr, &len); 5951 if (ret != 0) 5952 { 5953 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5954 goto end; 5955 } 5956 5957 ret = listen(server, 2); 5958 if (ret != 0) 5959 { 5960 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5961 goto end; 5962 } 5963 5964 SetLastError(0xdeadbeef); 5965 ret = connect(src, NULL, 0); 5966 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 5967 ok(GetLastError() == WSAEFAULT, "expected 10014, got %d\n", GetLastError()); 5968 5969 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr)); 5970 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) 5971 { 5972 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5973 goto end; 5974 } 5975 5976 ret = connect(src2, (struct sockaddr*)&addr, sizeof(addr)); 5977 if (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) 5978 { 5979 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5980 goto end; 5981 } 5982 5983 len = sizeof(addr); 5984 dst = accept(server, (struct sockaddr*)&addr, &len); 5985 if (dst == INVALID_SOCKET) 5986 { 5987 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5988 goto end; 5989 } 5990 5991 len = sizeof(addr); 5992 dst2 = accept(server, (struct sockaddr*)&addr, &len); 5993 if (dst2 == INVALID_SOCKET) 5994 { 5995 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError()); 5996 goto end; 5997 } 5998 5999 closesocket(server); 6000 server = INVALID_SOCKET; 6001 6002 /* On Windows it seems when a non-blocking socket sends to a 6003 blocking socket on the same host, the send() is BLOCKING, 6004 so make both sockets non-blocking. src is already non-blocking 6005 from the async select */ 6006 6007 if (set_blocking(dst, FALSE)) 6008 { 6009 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError()); 6010 goto end; 6011 } 6012 6013 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize); 6014 if (buffer == NULL) 6015 { 6016 ok(0, "could not allocate memory for test\n"); 6017 goto end; 6018 } 6019 6020 ov.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 6021 if (ov.hEvent == NULL) 6022 { 6023 ok(0, "could not create event object, errno = %d\n", GetLastError()); 6024 goto end; 6025 } 6026 6027 ov2.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 6028 if (ov2.hEvent == NULL) 6029 { 6030 ok(0, "could not create event object, errno = %d\n", GetLastError()); 6031 goto end; 6032 } 6033 6034 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */ 6035 ok_event_seq(src, hEvent, connect_seq, NULL, 1); 6036 ok_event_seq(src2, hEvent2, connect_seq, NULL, 1); 6037 /* broken on all windows - FD_CONNECT error is garbage */ 6038 6039 /* Test simple send/recv */ 6040 SetLastError(0xdeadbeef); 6041 ret = send(dst, buffer, 100, 0); 6042 ok(ret == 100, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6043 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6044 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6045 6046 SetLastError(0xdeadbeef); 6047 ret = recv(src, buffer, 1, MSG_PEEK); 6048 ok(ret == 1, "Failed to peek at recv buffer %d err %d\n", ret, GetLastError()); 6049 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6050 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6051 6052 SetLastError(0xdeadbeef); 6053 ret = recv(src, buffer, 50, 0); 6054 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError()); 6055 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6056 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6057 6058 ret = recv(src, buffer, 50, 0); 6059 ok(ret == 50, "Failed to recv buffer %d err %d\n", ret, GetLastError()); 6060 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6061 6062 /* fun fact - events are re-enabled even on failure, but only for messages */ 6063 ret = send(dst, "1", 1, 0); 6064 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6065 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6066 6067 ret = recv(src, buffer, -1, 0); 6068 ok(ret == SOCKET_ERROR && (GetLastError() == WSAEFAULT || GetLastError() == WSAENOBUFS), 6069 "Failed to recv buffer %d err %d\n", ret, GetLastError()); 6070 if (useMessages) 6071 { 6072 broken_seq[0] = empty_seq; /* win9x */ 6073 broken_seq[1] = NULL; 6074 todo_wine ok_event_seq(src, hEvent, read_seq, broken_seq, 0); 6075 } 6076 else 6077 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6078 6079 SetLastError(0xdeadbeef); 6080 ret = recv(src, buffer, 1, 0); 6081 ok(ret == 1, "Failed to recv buffer %d err %d\n", ret, GetLastError()); 6082 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6083 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6084 6085 /* Interaction with overlapped */ 6086 bufs.len = sizeof(char); 6087 bufs.buf = buffer; 6088 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL); 6089 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, 6090 "WSARecv failed - %d error %d\n", ret, GetLastError()); 6091 6092 bufs.len = sizeof(char); 6093 bufs.buf = buffer+1; 6094 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov2, NULL); 6095 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, 6096 "WSARecv failed - %d error %d\n", ret, GetLastError()); 6097 6098 ret = send(dst, "12", 2, 0); 6099 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6100 broken_seq[0] = read_read_seq; /* win9x */ 6101 broken_seq[1] = NULL; 6102 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0); 6103 6104 dwRet = WaitForSingleObject(ov.hEvent, 100); 6105 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError()); 6106 if (dwRet == WAIT_OBJECT_0) 6107 { 6108 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE); 6109 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */, 6110 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError()); 6111 ok(buffer[0] == '1', "Got %c instead of 1\n", buffer[0]); 6112 } 6113 6114 dwRet = WaitForSingleObject(ov2.hEvent, 100); 6115 ok(dwRet == WAIT_OBJECT_0, "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError()); 6116 if (dwRet == WAIT_OBJECT_0) 6117 { 6118 bret = GetOverlappedResult((HANDLE)src, &ov2, &bytesReturned, FALSE); 6119 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */, 6120 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError()); 6121 ok(buffer[1] == '2', "Got %c instead of 2\n", buffer[1]); 6122 } 6123 6124 SetLastError(0xdeadbeef); 6125 ret = send(dst, "1", 1, 0); 6126 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6127 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6128 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6129 6130 ret = recv(src, buffer, 1, 0); 6131 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6132 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6133 6134 /* Notifications are delivered as soon as possible, blocked only on 6135 * async requests on the same type */ 6136 bufs.len = sizeof(char); 6137 bufs.buf = buffer; 6138 ret = WSARecv(src, &bufs, 1, &bytesReturned, &flags, &ov, NULL); 6139 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, 6140 "WSARecv failed - %d error %d\n", ret, GetLastError()); 6141 6142 if (0) { 6143 ret = send(dst, "1", 1, MSG_OOB); 6144 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6145 ok_event_seq(src, hEvent, oob_seq, NULL, 0); 6146 } 6147 6148 dwRet = WaitForSingleObject(ov.hEvent, 100); 6149 ok(dwRet == WAIT_TIMEOUT, "OOB message activated read?: %d - %d\n", dwRet, GetLastError()); 6150 6151 ret = send(dst, "2", 1, 0); 6152 ok(ret == 1, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6153 broken_seq[0] = read_seq; /* win98 */ 6154 broken_seq[1] = NULL; 6155 ok_event_seq(src, hEvent, empty_seq, broken_seq, 0); 6156 6157 dwRet = WaitForSingleObject(ov.hEvent, 100); 6158 ok(dwRet == WAIT_OBJECT_0 || broken(dwRet == WAIT_TIMEOUT), 6159 "Failed to wait for recv message: %d - %d\n", dwRet, GetLastError()); 6160 if (dwRet == WAIT_OBJECT_0) 6161 { 6162 bret = GetOverlappedResult((HANDLE)src, &ov, &bytesReturned, FALSE); 6163 ok((bret && bytesReturned == 1) || broken(!bret && GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */, 6164 "Got %d instead of 1 (%d - %d)\n", bytesReturned, bret, GetLastError()); 6165 ok(buffer[0] == '2', "Got %c instead of 2\n", buffer[0]); 6166 } 6167 6168 if (0) { 6169 ret = recv(src, buffer, 1, MSG_OOB); 6170 todo_wine ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6171 /* We get OOB notification, but no data on wine */ 6172 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6173 } 6174 6175 /* Flood the send queue */ 6176 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id); 6177 if (hThread == NULL) 6178 { 6179 ok(0, "CreateThread failed, error %d\n", GetLastError()); 6180 goto end; 6181 } 6182 6183 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */ 6184 ok_event_seq(src, hEvent, empty_seq, NULL, 0); 6185 6186 /* Now if we send a ton of data and the 'server' does not drain it fast 6187 * enough (set drain_pause to be sure), the socket send buffer will only 6188 * take some of it, and we will get a short write. This will trigger 6189 * another FD_WRITE event as soon as data is sent and more space becomes 6190 * available, but not any earlier. */ 6191 drain_pause = TRUE; 6192 do 6193 { 6194 ret = send(src, buffer, bufferSize, 0); 6195 } while (ret == bufferSize); 6196 drain_pause = FALSE; 6197 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK) 6198 { 6199 Sleep(400); /* win9x */ 6200 broken_seq[0] = read_write_seq; 6201 broken_seq[1] = NULL; 6202 ok_event_seq(src, hEvent, write_seq, broken_seq, 0); 6203 } 6204 else 6205 { 6206 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError()); 6207 } 6208 6209 /* Test how FD_CLOSE is handled */ 6210 ret = send(dst, "12", 2, 0); 6211 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6212 6213 /* Wait a little and let the send complete */ 6214 Sleep(100); 6215 closesocket(dst); 6216 dst = INVALID_SOCKET; 6217 Sleep(100); 6218 6219 /* We can never implement this in wine, best we can hope for is 6220 sending FD_CLOSE after the reads complete */ 6221 broken_seq[0] = read_seq; /* win9x */ 6222 broken_seq[1] = NULL; 6223 todo_wine ok_event_seq(src, hEvent, read_close_seq, broken_seq, 0); 6224 6225 ret = recv(src, buffer, 1, 0); 6226 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6227 ok_event_seq(src, hEvent, read_seq, NULL, 0); 6228 6229 ret = recv(src, buffer, 1, 0); 6230 ok(ret == 1, "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6231 /* want it? it's here, but you can't have it */ 6232 broken_seq[0] = close_seq; /* win9x */ 6233 broken_seq[1] = NULL; 6234 todo_wine ok_event_seq(src, hEvent, empty_seq, /* wine sends FD_CLOSE here */ 6235 broken_seq, 0); 6236 6237 /* Test how FD_CLOSE is handled */ 6238 ret = send(dst2, "12", 2, 0); 6239 ok(ret == 2, "Failed to send buffer %d err %d\n", ret, GetLastError()); 6240 6241 Sleep(200); 6242 shutdown(dst2, SD_SEND); 6243 Sleep(200); 6244 6245 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent 6246 regressions, don't mark them as todo_wine, and mark windows as broken */ 6247 broken_seq[0] = read_close_seq; 6248 broken_seq[1] = close_seq; 6249 broken_seq[2] = NULL; 6250 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0); 6251 6252 ret = recv(src2, buffer, 1, 0); 6253 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6254 broken_seq[0] = close_seq; /* win98 */ 6255 broken_seq[1] = NULL; 6256 ok_event_seq(src2, hEvent2, read_seq, broken_seq, 0); 6257 6258 ret = recv(src2, buffer, 1, 0); 6259 ok(ret == 1 || broken(!ret), "Failed to empty buffer: %d - %d\n", ret, GetLastError()); 6260 broken_seq[0] = empty_seq; 6261 broken_seq[1] = NULL; 6262 ok_event_seq(src2, hEvent2, close_seq, broken_seq, 0); 6263 6264 ret = send(src2, "1", 1, 0); 6265 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError()); 6266 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0); 6267 6268 ret = send(src2, "1", 1, 0); 6269 ok(ret == 1, "Sending to half-closed socket failed %d err %d\n", ret, GetLastError()); 6270 ok_event_seq(src2, hEvent2, empty_seq, NULL, 0); 6271 6272 if (useMessages) 6273 { 6274 ret = WSAAsyncSelect(src, hWnd, WM_SOCKET, 0); 6275 if (ret) 6276 { 6277 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 6278 goto end; 6279 } 6280 6281 ret = set_blocking(src, TRUE); 6282 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 6283 6284 ret = WSAAsyncSelect(src2, hWnd, WM_SOCKET, 0); 6285 if (ret) 6286 { 6287 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 6288 goto end; 6289 } 6290 6291 ret = set_blocking(src2, TRUE); 6292 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 6293 } 6294 else 6295 { 6296 ret = WSAEventSelect(src, hEvent2, 0); 6297 if (ret) 6298 { 6299 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 6300 goto end; 6301 } 6302 6303 ret = set_blocking(src, TRUE); 6304 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 6305 6306 ret = WSAEventSelect(src2, hEvent2, 0); 6307 if (ret) 6308 { 6309 ok(0, "WSAAsyncSelect failed, error %d\n", ret); 6310 goto end; 6311 } 6312 6313 ret = set_blocking(src2, TRUE); 6314 ok(!ret, "set_blocking failed, error %d\n", WSAGetLastError()); 6315 } 6316 6317 end: 6318 if (src != INVALID_SOCKET) 6319 { 6320 flush_events(src, hEvent); 6321 closesocket(src); 6322 } 6323 if (src2 != INVALID_SOCKET) 6324 { 6325 flush_events(src2, hEvent2); 6326 closesocket(src2); 6327 } 6328 HeapFree(GetProcessHeap(), 0, buffer); 6329 if (server != INVALID_SOCKET) 6330 closesocket(server); 6331 if (dst != INVALID_SOCKET) 6332 closesocket(dst); 6333 if (dst2 != INVALID_SOCKET) 6334 closesocket(dst2); 6335 if (hThread != NULL) 6336 CloseHandle(hThread); 6337 if (hWnd != NULL) 6338 DestroyWindow(hWnd); 6339 if (hEvent != NULL) 6340 CloseHandle(hEvent); 6341 if (hEvent2 != NULL) 6342 CloseHandle(hEvent2); 6343 if (ov.hEvent != NULL) 6344 CloseHandle(ov.hEvent); 6345 if (ov2.hEvent != NULL) 6346 CloseHandle(ov2.hEvent); 6347 } 6348 6349 static void test_ipv6only(void) 6350 { 6351 SOCKET v4 = INVALID_SOCKET, v6; 6352 struct sockaddr_in sin4; 6353 struct sockaddr_in6 sin6; 6354 int ret, enabled, len = sizeof(enabled); 6355 6356 memset(&sin4, 0, sizeof(sin4)); 6357 sin4.sin_family = AF_INET; 6358 sin4.sin_port = htons(SERVERPORT); 6359 6360 memset(&sin6, 0, sizeof(sin6)); 6361 sin6.sin6_family = AF_INET6; 6362 sin6.sin6_port = htons(SERVERPORT); 6363 6364 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 6365 if (v6 == INVALID_SOCKET) 6366 { 6367 skip("Could not create IPv6 socket (LastError: %d)\n", WSAGetLastError()); 6368 goto end; 6369 } 6370 6371 enabled = 2; 6372 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6373 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6374 ok(enabled == 1, "expected 1, got %d\n", enabled); 6375 6376 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6)); 6377 if (ret) 6378 { 6379 skip("Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError()); 6380 goto end; 6381 } 6382 6383 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 6384 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError()); 6385 6386 todo_wine { 6387 enabled = 2; 6388 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6389 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6390 ok(enabled == 1, "expected 1, got %d\n", enabled); 6391 } 6392 6393 enabled = 0; 6394 len = sizeof(enabled); 6395 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len); 6396 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6397 6398 todo_wine { 6399 enabled = 2; 6400 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6401 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6402 ok(!enabled, "expected 0, got %d\n", enabled); 6403 } 6404 6405 enabled = 1; 6406 len = sizeof(enabled); 6407 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len); 6408 ok(!ret, "setsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6409 6410 /* bind on IPv4 socket should succeed - IPV6_V6ONLY is enabled by default */ 6411 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4)); 6412 ok(!ret, "Could not bind IPv4 address (LastError: %d)\n", WSAGetLastError()); 6413 6414 todo_wine { 6415 enabled = 2; 6416 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6417 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6418 ok(enabled == 1, "expected 1, got %d\n", enabled); 6419 } 6420 6421 enabled = 0; 6422 len = sizeof(enabled); 6423 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len); 6424 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError()); 6425 6426 todo_wine { 6427 enabled = 0; 6428 ret = getsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6429 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6430 ok(enabled == 1, "expected 1, got %d\n", enabled); 6431 } 6432 6433 enabled = 1; 6434 len = sizeof(enabled); 6435 ret = setsockopt(v4, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len); 6436 ok(ret, "setsockopt(IPV6_ONLY) succeeded (LastError: %d)\n", WSAGetLastError()); 6437 6438 closesocket(v4); 6439 closesocket(v6); 6440 6441 /* Test again, this time disabling IPV6_V6ONLY. */ 6442 sin4.sin_port = htons(SERVERPORT+2); 6443 sin6.sin6_port = htons(SERVERPORT+2); 6444 6445 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 6446 ok(v6 != INVALID_SOCKET, "Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n", 6447 WSAGetLastError(), WSAEAFNOSUPPORT); 6448 6449 enabled = 0; 6450 ret = setsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, len); 6451 ok(!ret, "Could not disable IPV6_V6ONLY (LastError: %d).\n", WSAGetLastError()); 6452 6453 enabled = 2; 6454 ret = getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6455 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6456 ok(!enabled, "expected 0, got %d\n", enabled); 6457 6458 /* 6459 Observaition: 6460 On Windows, bind on both IPv4 and IPv6 with IPV6_V6ONLY disabled succeeds by default. 6461 Application must set SO_EXCLUSIVEADDRUSE on first socket to disallow another successful bind. 6462 In general, a standard application should not use SO_REUSEADDR. 6463 Setting both SO_EXCLUSIVEADDRUSE and SO_REUSEADDR on the same socket is not possible in 6464 either order, the later setsockopt call always fails. 6465 */ 6466 enabled = 1; 6467 ret = setsockopt(v6, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&enabled, len); 6468 ok(!ret, "Could not set SO_EXCLUSIVEADDRUSE on IPv6 socket (LastError: %d)\n", WSAGetLastError()); 6469 6470 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6)); 6471 ok(!ret, "Could not bind IPv6 address (LastError: %d)\n", WSAGetLastError()); 6472 6473 enabled = 2; 6474 len = sizeof(enabled); 6475 getsockopt(v6, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enabled, &len); 6476 ok(!ret, "getsockopt(IPV6_ONLY) failed (LastError: %d)\n", WSAGetLastError()); 6477 ok(!enabled, "IPV6_V6ONLY is enabled after bind\n"); 6478 6479 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 6480 ok(v4 != INVALID_SOCKET, "Could not create IPv4 socket (LastError: %d)\n", WSAGetLastError()); 6481 6482 enabled = 1; 6483 ret = setsockopt(v4, SOL_SOCKET, SO_REUSEADDR, (char*)&enabled, len); 6484 ok(!ret, "Could not set SO_REUSEADDR on IPv4 socket (LastError: %d)\n", WSAGetLastError()); 6485 6486 WSASetLastError(0xdeadbeef); 6487 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4)); 6488 ok(ret, "bind succeeded unexpectedly for the IPv4 socket\n"); 6489 ok(WSAGetLastError() == WSAEACCES, "Expected 10013, got %d\n", WSAGetLastError()); 6490 6491 end: 6492 if (v4 != INVALID_SOCKET) 6493 closesocket(v4); 6494 if (v6 != INVALID_SOCKET) 6495 closesocket(v6); 6496 } 6497 6498 static void test_WSASendMsg(void) 6499 { 6500 SOCKET sock, dst; 6501 struct sockaddr_in sendaddr, sockaddr; 6502 GUID WSASendMsg_GUID = WSAID_WSASENDMSG; 6503 LPFN_WSASENDMSG pWSASendMsg = NULL; 6504 char teststr[12] = "hello world", buffer[32]; 6505 WSABUF iovec[2]; 6506 WSAMSG msg; 6507 DWORD bytesSent, err; 6508 int ret, addrlen; 6509 6510 /* FIXME: Missing OVERLAPPED and OVERLAPPED COMPLETION ROUTINE tests */ 6511 6512 sock = socket(AF_INET, SOCK_DGRAM, 0); 6513 ok(sock != INVALID_SOCKET, "socket() failed\n"); 6514 6515 /* Obtain the WSASendMsg function */ 6516 WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &WSASendMsg_GUID, sizeof(WSASendMsg_GUID), 6517 &pWSASendMsg, sizeof(pWSASendMsg), &err, NULL, NULL); 6518 if (!pWSASendMsg) 6519 { 6520 closesocket(sock); 6521 win_skip("WSASendMsg is unsupported, some tests will be skipped.\n"); 6522 return; 6523 } 6524 6525 /* fake address for now */ 6526 sendaddr.sin_family = AF_INET; 6527 sendaddr.sin_port = htons(139); 6528 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6529 6530 memset(&msg, 0, sizeof(msg)); 6531 iovec[0].buf = teststr; 6532 iovec[0].len = sizeof(teststr); 6533 iovec[1].buf = teststr; 6534 iovec[1].len = sizeof(teststr) / 2; 6535 msg.name = (struct sockaddr *) &sendaddr; 6536 msg.namelen = sizeof(sendaddr); 6537 msg.lpBuffers = iovec; 6538 msg.dwBufferCount = 1; /* send only one buffer for now */ 6539 6540 WSASetLastError(0xdeadbeef); 6541 ret = pWSASendMsg(INVALID_SOCKET, &msg, 0, NULL, NULL, NULL); 6542 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6543 err = WSAGetLastError(); 6544 ok(err == WSAENOTSOCK, "expected 10038, got %d instead\n", err); 6545 6546 WSASetLastError(0xdeadbeef); 6547 ret = pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL); 6548 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6549 err = WSAGetLastError(); 6550 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 6551 6552 WSASetLastError(0xdeadbeef); 6553 ret = pWSASendMsg(sock, NULL, 0, &bytesSent, NULL, NULL); 6554 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6555 err = WSAGetLastError(); 6556 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 6557 6558 WSASetLastError(0xdeadbeef); 6559 ret = pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL); 6560 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6561 err = WSAGetLastError(); 6562 ok(err == WSAEFAULT, "expected 10014, got %d instead\n", err); 6563 6564 closesocket(sock); 6565 6566 sock = socket(AF_INET, SOCK_DGRAM, 0); 6567 ok(sock != INVALID_SOCKET, "socket() failed\n"); 6568 6569 dst = socket(AF_INET, SOCK_DGRAM, 0); 6570 ok(dst != INVALID_SOCKET, "socket() failed\n"); 6571 6572 memset(&sockaddr, 0, sizeof(sockaddr)); 6573 sockaddr.sin_family = AF_INET; 6574 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6575 ok(!bind(dst, (struct sockaddr*)&sockaddr, sizeof(sockaddr)), 6576 "bind should have worked\n"); 6577 6578 /* read address to find out the port number to be used in send */ 6579 memset(&sendaddr, 0, sizeof(sendaddr)); 6580 addrlen = sizeof(sendaddr); 6581 ok(!getsockname(dst, (struct sockaddr *) &sendaddr, &addrlen), 6582 "getsockname should have worked\n"); 6583 ok(sendaddr.sin_port, "socket port should be != 0\n"); 6584 6585 /* ensure the sending socket is not bound */ 6586 WSASetLastError(0xdeadbeef); 6587 addrlen = sizeof(sockaddr); 6588 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen); 6589 ok(ret == SOCKET_ERROR, "getsockname should have failed\n"); 6590 err = WSAGetLastError(); 6591 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err); 6592 6593 set_blocking(sock, TRUE); 6594 6595 bytesSent = 0; 6596 SetLastError(0xdeadbeef); 6597 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL); 6598 ok(!ret, "WSASendMsg should have worked\n"); 6599 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */, 6600 "Expected 0, got %d\n", GetLastError()); 6601 ok(bytesSent == iovec[0].len, "incorret bytes sent, expected %d, sent %d\n", 6602 iovec[0].len, bytesSent); 6603 6604 /* receive data */ 6605 addrlen = sizeof(sockaddr); 6606 memset(buffer, 0, sizeof(buffer)); 6607 SetLastError(0xdeadbeef); 6608 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen); 6609 ok(ret == bytesSent, "got %d, expected %d\n", 6610 ret, bytesSent); 6611 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6612 6613 /* A successful call to WSASendMsg must have bound the socket */ 6614 addrlen = sizeof(sockaddr); 6615 sockaddr.sin_port = 0; 6616 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6617 ret = getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen); 6618 ok(!ret, "getsockname should have worked\n"); 6619 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n", 6620 inet_ntoa(sockaddr.sin_addr)); 6621 ok(sockaddr.sin_port, "sin_port should be != 0\n"); 6622 6623 msg.dwBufferCount = 2; /* send both buffers */ 6624 6625 bytesSent = 0; 6626 SetLastError(0xdeadbeef); 6627 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL); 6628 ok(!ret, "WSASendMsg should have worked\n"); 6629 ok(bytesSent == iovec[0].len + iovec[1].len, "incorret bytes sent, expected %d, sent %d\n", 6630 iovec[0].len + iovec[1].len, bytesSent); 6631 ok(GetLastError() == 0 || broken(GetLastError() == 0xdeadbeef) /* Win <= 2008 */, 6632 "Expected 0, got %d\n", GetLastError()); 6633 6634 /* receive data */ 6635 addrlen = sizeof(sockaddr); 6636 memset(buffer, 0, sizeof(buffer)); 6637 SetLastError(0xdeadbeef); 6638 ret = recvfrom(dst, buffer, sizeof(buffer), 0, (struct sockaddr *) &sockaddr, &addrlen); 6639 ok(ret == bytesSent, "got %d, expected %d\n", 6640 ret, bytesSent); 6641 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6642 6643 closesocket(sock); 6644 closesocket(dst); 6645 6646 /* a bad call to WSASendMsg will also bind the socket */ 6647 addrlen = sizeof(sockaddr); 6648 sockaddr.sin_port = 0; 6649 sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6650 sock = socket(AF_INET, SOCK_DGRAM, 0); 6651 ok(sock != INVALID_SOCKET, "socket() failed\n"); 6652 ok(pWSASendMsg(sock, &msg, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6653 todo_wine { 6654 ok(!getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have worked\n"); 6655 ok(sockaddr.sin_addr.s_addr == htonl(INADDR_ANY), "expected 0.0.0.0, got %s\n", 6656 inet_ntoa(sockaddr.sin_addr)); 6657 ok(sockaddr.sin_port, "sin_port should be > 0\n"); 6658 } 6659 closesocket(sock); 6660 6661 /* a bad call without msg parameter will not trigger the auto-bind */ 6662 sock = socket(AF_INET, SOCK_DGRAM, 0); 6663 ok(sock != INVALID_SOCKET, "socket() failed\n"); 6664 ok(pWSASendMsg(sock, NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6665 ok(getsockname(sock, (struct sockaddr*)&sockaddr, &addrlen), "getsockname should have failed\n"); 6666 err = WSAGetLastError(); 6667 ok(err == WSAEINVAL, "expected 10022, got %d instead\n", err); 6668 closesocket(sock); 6669 6670 /* SOCK_STREAM sockets are not supported */ 6671 bytesSent = 0; 6672 sock = socket(AF_INET, SOCK_STREAM, 0); 6673 ok(sock != INVALID_SOCKET, "socket() failed\n"); 6674 SetLastError(0xdeadbeef); 6675 ret = pWSASendMsg(sock, &msg, 0, &bytesSent, NULL, NULL); 6676 ok(ret == SOCKET_ERROR, "WSASendMsg should have failed\n"); 6677 err = WSAGetLastError(); 6678 todo_wine 6679 ok(err == WSAEINVAL, "expected 10014, got %d instead\n", err); 6680 closesocket(sock); 6681 } 6682 6683 static void test_WSASendTo(void) 6684 { 6685 SOCKET s; 6686 struct sockaddr_in addr; 6687 char buf[12] = "hello world"; 6688 WSABUF data_buf; 6689 DWORD bytesSent; 6690 int ret; 6691 6692 addr.sin_family = AF_INET; 6693 addr.sin_port = htons(139); 6694 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6695 data_buf.len = sizeof(buf); 6696 data_buf.buf = buf; 6697 6698 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { 6699 ok(0, "socket() failed error: %d\n", WSAGetLastError()); 6700 return; 6701 } 6702 6703 WSASetLastError(12345); 6704 ret = WSASendTo(INVALID_SOCKET, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL); 6705 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK, 6706 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError()); 6707 6708 WSASetLastError(12345); 6709 ret = WSASendTo(s, &data_buf, 1, NULL, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL); 6710 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 6711 "WSASendTo() failed: %d/%d\n", ret, WSAGetLastError()); 6712 6713 WSASetLastError(12345); 6714 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) { 6715 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError()); 6716 return; 6717 } 6718 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after " 6719 "a successful call to WSASendTo()\n"); 6720 } 6721 6722 static DWORD WINAPI recv_thread(LPVOID arg) 6723 { 6724 SOCKET sock = *(SOCKET *)arg; 6725 char buffer[32]; 6726 WSABUF wsa; 6727 WSAOVERLAPPED ov; 6728 DWORD flags = 0; 6729 6730 wsa.buf = buffer; 6731 wsa.len = sizeof(buffer); 6732 ov.hEvent = WSACreateEvent(); 6733 WSARecv(sock, &wsa, 1, NULL, &flags, &ov, NULL); 6734 6735 WaitForSingleObject(ov.hEvent, 1000); 6736 WSACloseEvent(ov.hEvent); 6737 return 0; 6738 } 6739 6740 static int completion_called; 6741 6742 static void WINAPI io_completion(DWORD error, DWORD transferred, WSAOVERLAPPED *overlapped, DWORD flags) 6743 { 6744 completion_called++; 6745 } 6746 6747 static void test_WSARecv(void) 6748 { 6749 SOCKET src, dest, server = INVALID_SOCKET; 6750 char buf[20]; 6751 WSABUF bufs[2]; 6752 WSAOVERLAPPED ov; 6753 DWORD bytesReturned, flags, id; 6754 struct linger ling; 6755 struct sockaddr_in addr; 6756 int iret, len; 6757 DWORD dwret; 6758 BOOL bret; 6759 HANDLE thread, event = NULL, io_port; 6760 6761 tcp_socketpair(&src, &dest); 6762 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 6763 { 6764 skip("failed to create sockets\n"); 6765 goto end; 6766 } 6767 6768 memset(&ov, 0, sizeof(ov)); 6769 flags = 0; 6770 bufs[0].len = 2; 6771 bufs[0].buf = buf; 6772 6773 /* Send 4 bytes and receive in two calls of 2 */ 6774 SetLastError(0xdeadbeef); 6775 iret = send(src, "test", 4, 0); 6776 ok(iret == 4, "Expected 4, got %d\n", iret); 6777 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6778 SetLastError(0xdeadbeef); 6779 bytesReturned = 0xdeadbeef; 6780 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL); 6781 ok(!iret, "Expected 0, got %d\n", iret); 6782 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned); 6783 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6784 SetLastError(0xdeadbeef); 6785 bytesReturned = 0xdeadbeef; 6786 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL); 6787 ok(!iret, "Expected 0, got %d\n", iret); 6788 ok(bytesReturned == 2, "Expected 2, got %d\n", bytesReturned); 6789 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6790 6791 bufs[0].len = 4; 6792 SetLastError(0xdeadbeef); 6793 iret = send(src, "test", 4, 0); 6794 ok(iret == 4, "Expected 4, got %d\n", iret); 6795 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6796 SetLastError(0xdeadbeef); 6797 bytesReturned = 0xdeadbeef; 6798 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, NULL, NULL); 6799 ok(!iret, "Expected 0, got %d\n", iret); 6800 ok(bytesReturned == 4, "Expected 4, got %d\n", bytesReturned); 6801 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6802 6803 /* Test 2 buffers */ 6804 bufs[0].len = 4; 6805 bufs[1].len = 5; 6806 bufs[1].buf = buf + 10; 6807 SetLastError(0xdeadbeef); 6808 iret = send(src, "deadbeefs", 9, 0); 6809 ok(iret == 9, "Expected 9, got %d\n", iret); 6810 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6811 SetLastError(0xdeadbeef); 6812 bytesReturned = 0xdeadbeef; 6813 iret = WSARecv(dest, bufs, 2, &bytesReturned, &flags, NULL, NULL); 6814 ok(!iret, "Expected 0, got %d\n", iret); 6815 ok(bytesReturned == 9, "Expected 9, got %d\n", bytesReturned); 6816 bufs[0].buf[4] = '\0'; 6817 bufs[1].buf[5] = '\0'; 6818 ok(!strcmp(bufs[0].buf, "dead"), "buf[0] doesn't match: %s != dead\n", bufs[0].buf); 6819 ok(!strcmp(bufs[1].buf, "beefs"), "buf[1] doesn't match: %s != beefs\n", bufs[1].buf); 6820 ok(GetLastError() == ERROR_SUCCESS, "Expected 0, got %d\n", GetLastError()); 6821 6822 bufs[0].len = sizeof(buf); 6823 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL); 6824 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError()); 6825 if (!event) 6826 goto end; 6827 6828 ling.l_onoff = 1; 6829 ling.l_linger = 0; 6830 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling)); 6831 ok(!iret, "Failed to set linger %d\n", GetLastError()); 6832 6833 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, NULL); 6834 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError()); 6835 6836 iret = WSARecv(dest, bufs, 1, &bytesReturned, &flags, &ov, NULL); 6837 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError()); 6838 6839 closesocket(src); 6840 src = INVALID_SOCKET; 6841 6842 dwret = WaitForSingleObject(ov.hEvent, 1000); 6843 ok(dwret == WAIT_OBJECT_0, "Waiting for disconnect event failed with %d + errno %d\n", dwret, GetLastError()); 6844 6845 bret = GetOverlappedResult((HANDLE)dest, &ov, &bytesReturned, FALSE); 6846 todo_wine ok(!bret && (GetLastError() == ERROR_NETNAME_DELETED || broken(GetLastError() == ERROR_IO_INCOMPLETE) /* win9x */), 6847 "Did not get disconnect event: %d, error %d\n", bret, GetLastError()); 6848 ok(bytesReturned == 0, "Bytes received is %d\n", bytesReturned); 6849 closesocket(dest); 6850 dest = INVALID_SOCKET; 6851 6852 src = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0); 6853 ok(src != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError()); 6854 if (src == INVALID_SOCKET) goto end; 6855 6856 server = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 6857 ok(server != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError()); 6858 if (server == INVALID_SOCKET) goto end; 6859 6860 memset(&addr, 0, sizeof(addr)); 6861 addr.sin_family = AF_INET; 6862 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 6863 iret = bind(server, (struct sockaddr *)&addr, sizeof(addr)); 6864 if (iret) goto end; 6865 6866 len = sizeof(addr); 6867 iret = getsockname(server, (struct sockaddr *)&addr, &len); 6868 if (iret) goto end; 6869 6870 iret = listen(server, 1); 6871 if (iret) goto end; 6872 6873 iret = connect(src, (struct sockaddr *)&addr, sizeof(addr)); 6874 if (iret) goto end; 6875 6876 len = sizeof(addr); 6877 dest = accept(server, (struct sockaddr *)&addr, &len); 6878 ok(dest != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError()); 6879 if (dest == INVALID_SOCKET) goto end; 6880 6881 send(src, "test message", sizeof("test message"), 0); 6882 thread = CreateThread(NULL, 0, recv_thread, &dest, 0, &id); 6883 WaitForSingleObject(thread, 3000); 6884 CloseHandle(thread); 6885 6886 memset(&ov, 0, sizeof(ov)); 6887 ov.hEvent = event; 6888 ResetEvent(event); 6889 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion); 6890 ok(iret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, "WSARecv failed - %d error %d\n", iret, GetLastError()); 6891 send(src, "test message", sizeof("test message"), 0); 6892 6893 completion_called = 0; 6894 dwret = SleepEx(1000, TRUE); 6895 ok(dwret == WAIT_IO_COMPLETION, "got %u\n", dwret); 6896 ok(completion_called == 1, "completion not called\n"); 6897 6898 dwret = WaitForSingleObject(event, 1); 6899 ok(dwret == WAIT_TIMEOUT, "got %u\n", dwret); 6900 6901 io_port = CreateIoCompletionPort( (HANDLE)dest, NULL, 0, 0 ); 6902 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 6903 6904 /* Using completion function on socket associated with completion port is not allowed. */ 6905 memset(&ov, 0, sizeof(ov)); 6906 completion_called = 0; 6907 iret = WSARecv(dest, bufs, 1, NULL, &flags, &ov, io_completion); 6908 ok(iret == SOCKET_ERROR && GetLastError() == WSAEINVAL, "WSARecv failed - %d error %d\n", iret, GetLastError()); 6909 ok(!completion_called, "completion called\n"); 6910 6911 CloseHandle(io_port); 6912 6913 end: 6914 if (server != INVALID_SOCKET) 6915 closesocket(server); 6916 if (dest != INVALID_SOCKET) 6917 closesocket(dest); 6918 if (src != INVALID_SOCKET) 6919 closesocket(src); 6920 if (event) 6921 WSACloseEvent(event); 6922 } 6923 6924 struct write_watch_thread_args 6925 { 6926 int func; 6927 SOCKET dest; 6928 void *base; 6929 DWORD size; 6930 const char *expect; 6931 }; 6932 6933 static DWORD CALLBACK write_watch_thread( void *arg ) 6934 { 6935 struct write_watch_thread_args *args = arg; 6936 struct sockaddr addr; 6937 int addr_len = sizeof(addr), ret; 6938 DWORD bytes, flags = 0; 6939 WSABUF buf[1]; 6940 6941 switch (args->func) 6942 { 6943 case 0: 6944 ret = recv( args->dest, args->base, args->size, 0 ); 6945 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret ); 6946 ok( !strcmp( args->base, args->expect ), "wrong data\n" ); 6947 break; 6948 case 1: 6949 ret = recvfrom( args->dest, args->base, args->size, 0, &addr, &addr_len ); 6950 ok( ret == strlen(args->expect) + 1, "wrong len %d\n", ret ); 6951 ok( !strcmp( args->base, args->expect ), "wrong data\n" ); 6952 break; 6953 case 2: 6954 buf[0].len = args->size; 6955 buf[0].buf = args->base; 6956 ret = WSARecv( args->dest, buf, 1, &bytes, &flags, NULL, NULL ); 6957 ok( !ret, "WSARecv failed %u\n", GetLastError() ); 6958 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes ); 6959 ok( !strcmp( args->base, args->expect ), "wrong data\n" ); 6960 break; 6961 case 3: 6962 buf[0].len = args->size; 6963 buf[0].buf = args->base; 6964 ret = WSARecvFrom( args->dest, buf, 1, &bytes, &flags, &addr, &addr_len, NULL, NULL ); 6965 ok( !ret, "WSARecvFrom failed %u\n", GetLastError() ); 6966 ok( bytes == strlen(args->expect) + 1, "wrong len %d\n", bytes ); 6967 ok( !strcmp( args->base, args->expect ), "wrong data\n" ); 6968 break; 6969 } 6970 return 0; 6971 } 6972 6973 static void test_write_watch(void) 6974 { 6975 SOCKET src, dest; 6976 WSABUF bufs[2]; 6977 WSAOVERLAPPED ov; 6978 struct write_watch_thread_args args; 6979 DWORD bytesReturned, flags, size; 6980 struct sockaddr addr; 6981 int addr_len, ret; 6982 HANDLE thread, event; 6983 char *base; 6984 void *results[64]; 6985 ULONG_PTR count; 6986 ULONG pagesize; 6987 UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*); 6988 6989 pGetWriteWatch = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetWriteWatch" ); 6990 if (!pGetWriteWatch) 6991 { 6992 win_skip( "write watched not supported\n" ); 6993 return; 6994 } 6995 6996 tcp_socketpair(&src, &dest); 6997 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 6998 { 6999 skip("failed to create sockets\n"); 7000 return; 7001 } 7002 7003 memset(&ov, 0, sizeof(ov)); 7004 ov.hEvent = event = CreateEventA(NULL, FALSE, FALSE, NULL); 7005 ok(ov.hEvent != NULL, "could not create event object, errno = %d\n", GetLastError()); 7006 7007 flags = 0; 7008 7009 size = 0x10000; 7010 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE ); 7011 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() ); 7012 7013 #ifdef __REACTOS__ 7014 if (!base) 7015 { 7016 skip("VirtualAlloc(MEM_WRITE_WATCH) is not supported yet on ReactOS\n"); 7017 skip("Skipping tests due to hang. See ROSTESTS-385\n"); 7018 return; 7019 } 7020 #endif 7021 7022 memset( base, 0, size ); 7023 count = 64; 7024 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7025 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7026 ok( count == 16, "wrong count %lu\n", count ); 7027 7028 bufs[0].len = 5; 7029 bufs[0].buf = base; 7030 bufs[1].len = 0x8000; 7031 bufs[1].buf = base + 0x4000; 7032 7033 ret = WSARecv( dest, bufs, 2, NULL, &flags, &ov, NULL); 7034 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, 7035 "WSARecv failed - %d error %d\n", ret, GetLastError()); 7036 7037 count = 64; 7038 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7039 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7040 ok( count == 9, "wrong count %lu\n", count ); 7041 ok( !base[0], "data set\n" ); 7042 7043 send(src, "test message", sizeof("test message"), 0); 7044 7045 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE ); 7046 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() ); 7047 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned ); 7048 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base ); 7049 ok( !memcmp( base + 0x4000, "message", 8 ), "wrong data %s\n", base + 0x4000 ); 7050 7051 count = 64; 7052 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7053 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7054 ok( count == 0, "wrong count %lu\n", count ); 7055 7056 memset( base, 0, size ); 7057 count = 64; 7058 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7059 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7060 ok( count == 16, "wrong count %lu\n", count ); 7061 7062 bufs[1].len = 0x4000; 7063 bufs[1].buf = base + 0x2000; 7064 ret = WSARecvFrom( dest, bufs, 2, NULL, &flags, &addr, &addr_len, &ov, NULL); 7065 ok(ret == SOCKET_ERROR && GetLastError() == ERROR_IO_PENDING, 7066 "WSARecv failed - %d error %d\n", ret, GetLastError()); 7067 7068 count = 64; 7069 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7070 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7071 ok( count == 5, "wrong count %lu\n", count ); 7072 ok( !base[0], "data set\n" ); 7073 7074 send(src, "test message", sizeof("test message"), 0); 7075 7076 ret = GetOverlappedResult( (HANDLE)dest, &ov, &bytesReturned, TRUE ); 7077 ok( ret, "GetOverlappedResult failed %u\n", GetLastError() ); 7078 ok( bytesReturned == sizeof("test message"), "wrong size %u\n", bytesReturned ); 7079 ok( !memcmp( base, "test ", 5 ), "wrong data %s\n", base ); 7080 ok( !memcmp( base + 0x2000, "message", 8 ), "wrong data %s\n", base + 0x2000 ); 7081 7082 count = 64; 7083 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7084 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7085 ok( count == 0, "wrong count %lu\n", count ); 7086 7087 memset( base, 0, size ); 7088 count = 64; 7089 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7090 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7091 ok( count == 16, "wrong count %lu\n", count ); 7092 7093 args.dest = dest; 7094 args.base = base; 7095 args.size = 0x7002; 7096 args.expect = "test message"; 7097 for (args.func = 0; args.func < 4; args.func++) 7098 { 7099 thread = CreateThread( NULL, 0, write_watch_thread, &args, 0, NULL ); 7100 Sleep( 200 ); 7101 7102 count = 64; 7103 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7104 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7105 ok( count == 8, "wrong count %lu\n", count ); 7106 7107 send(src, "test message", sizeof("test message"), 0); 7108 WaitForSingleObject( thread, 10000 ); 7109 CloseHandle( thread ); 7110 7111 count = 64; 7112 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize ); 7113 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() ); 7114 ok( count == 0, "wrong count %lu\n", count ); 7115 } 7116 WSACloseEvent( event ); 7117 closesocket( dest ); 7118 closesocket( src ); 7119 VirtualFree( base, 0, MEM_FREE ); 7120 } 7121 7122 #define POLL_CLEAR() ix = 0 7123 #define POLL_SET(s, ev) {fds[ix].fd = s; fds[ix++].events = ev;} 7124 #define POLL_ISSET(s, rev) poll_isset(fds, ix, s, rev) 7125 static BOOL poll_isset(WSAPOLLFD *fds, int max, SOCKET s, int rev) 7126 { 7127 int k; 7128 for (k = 0; k < max; k++) 7129 if (fds[k].fd == s && (fds[k].revents == rev)) return TRUE; 7130 return FALSE; 7131 } 7132 7133 static void test_WSAPoll(void) 7134 { 7135 int ix, ret, err, poll_timeout; 7136 SOCKET fdListen, fdRead, fdWrite; 7137 struct sockaddr_in address; 7138 socklen_t len; 7139 static char tmp_buf[1024]; 7140 WSAPOLLFD fds[16]; 7141 HANDLE thread_handle; 7142 DWORD id; 7143 7144 if (!pWSAPoll) /* >= Vista */ 7145 { 7146 skip("WSAPoll is unsupported, some tests will be skipped.\n"); 7147 return; 7148 } 7149 7150 /* Invalid parameters test */ 7151 SetLastError(0xdeadbeef); 7152 ret = pWSAPoll(NULL, 0, 0); 7153 err = GetLastError(); 7154 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 7155 ok(err == WSAEINVAL, "expected 10022, got %d\n", err); 7156 SetLastError(0xdeadbeef); 7157 ret = pWSAPoll(NULL, 1, 0); 7158 err = GetLastError(); 7159 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 7160 ok(err == WSAEFAULT, "expected 10014, got %d\n", err); 7161 SetLastError(0xdeadbeef); 7162 ret = pWSAPoll(NULL, 0, 1); 7163 err = GetLastError(); 7164 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 7165 ok(err == WSAEINVAL, "expected 10022, got %d\n", err); 7166 SetLastError(0xdeadbeef); 7167 ret = pWSAPoll(NULL, 1, 1); 7168 err = GetLastError(); 7169 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 7170 ok(err == WSAEFAULT, "expected 10014, got %d\n", err); 7171 7172 /* WSAPoll() tries to mime the unix poll() call. The following tests do: 7173 * - check if a connection attempt ended with success or error; 7174 * - check if a pending connection is waiting for acceptance; 7175 * - check for data to read, availability for write and OOB data 7176 */ 7177 memset(&address, 0, sizeof(address)); 7178 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 7179 address.sin_family = AF_INET; 7180 len = sizeof(address); 7181 fdListen = setup_server_socket(&address, &len); 7182 poll_timeout = 100; 7183 7184 /* When no events are pending poll returns 0 with no error */ 7185 POLL_CLEAR(); 7186 POLL_SET(fdListen, POLLIN); 7187 ret = pWSAPoll(fds, ix, poll_timeout); 7188 ok(ret == 0, "expected 0, got %d\n", ret); 7189 7190 /* Test listening socket connection attempt notifications */ 7191 fdWrite = setup_connector_socket(&address, len, TRUE); 7192 POLL_CLEAR(); 7193 POLL_SET(fdListen, POLLIN | POLLOUT); 7194 ret = pWSAPoll(fds, ix, poll_timeout); 7195 ok(ret == 1, "expected 1, got %d\n", ret); 7196 ok(POLL_ISSET(fdListen, POLLRDNORM), "fdListen socket events incorrect\n"); 7197 len = sizeof(address); 7198 fdRead = accept(fdListen, (struct sockaddr*) &address, &len); 7199 ok(fdRead != INVALID_SOCKET, "expected a valid socket\n"); 7200 7201 /* Test client side connection attempt notifications */ 7202 POLL_CLEAR(); 7203 POLL_SET(fdListen, POLLIN | POLLOUT); 7204 POLL_SET(fdRead, POLLIN | POLLOUT); 7205 POLL_SET(fdWrite, POLLIN | POLLOUT); 7206 ret = pWSAPoll(fds, ix, poll_timeout); 7207 ok(ret == 2, "expected 2, got %d\n", ret); 7208 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n"); 7209 ok(POLL_ISSET(fdRead, POLLWRNORM), "fdRead socket events incorrect\n"); 7210 len = sizeof(id); 7211 id = 0xdeadbeef; 7212 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len); 7213 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 7214 ok(id == 0, "expected 0, got %d\n", id); 7215 7216 /* Test data receiving notifications */ 7217 ret = send(fdWrite, "1234", 4, 0); 7218 ok(ret == 4, "expected 4, got %d\n", ret); 7219 POLL_CLEAR(); 7220 POLL_SET(fdListen, POLLIN | POLLOUT); 7221 POLL_SET(fdRead, POLLIN); 7222 ret = pWSAPoll(fds, ix, poll_timeout); 7223 ok(ret == 1, "expected 1, got %d\n", ret); 7224 ok(POLL_ISSET(fdRead, POLLRDNORM), "fdRead socket events incorrect\n"); 7225 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0); 7226 ok(ret == 4, "expected 4, got %d\n", ret); 7227 ok(!strcmp(tmp_buf, "1234"), "data received differs from sent\n"); 7228 7229 /* Test OOB data notifications */ 7230 ret = send(fdWrite, "A", 1, MSG_OOB); 7231 ok(ret == 1, "expected 1, got %d\n", ret); 7232 POLL_CLEAR(); 7233 POLL_SET(fdListen, POLLIN | POLLOUT); 7234 POLL_SET(fdRead, POLLIN); 7235 ret = pWSAPoll(fds, ix, poll_timeout); 7236 ok(ret == 1, "expected 1, got %d\n", ret); 7237 ok(POLL_ISSET(fdRead, POLLRDBAND), "fdRead socket events incorrect\n"); 7238 tmp_buf[0] = 0xAF; 7239 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB); 7240 ok(ret == 1, "expected 1, got %d\n", ret); 7241 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]); 7242 7243 /* If the socket is OOBINLINED the notification is like normal data */ 7244 ret = 1; 7245 ret = setsockopt(fdRead, SOL_SOCKET, SO_OOBINLINE, (char*) &ret, sizeof(ret)); 7246 ok(ret == 0, "expected 0, got %d\n", ret); 7247 ret = send(fdWrite, "A", 1, MSG_OOB); 7248 ok(ret == 1, "expected 1, got %d\n", ret); 7249 POLL_CLEAR(); 7250 POLL_SET(fdListen, POLLIN | POLLOUT); 7251 POLL_SET(fdRead, POLLIN | POLLOUT); 7252 ret = pWSAPoll(fds, ix, poll_timeout); 7253 ok(ret == 1, "expected 1, got %d\n", ret); 7254 tmp_buf[0] = 0xAF; 7255 SetLastError(0xdeadbeef); 7256 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), MSG_OOB); 7257 ok(ret == SOCKET_ERROR, "expected -1, got %d\n", ret); 7258 ok(GetLastError() == WSAEINVAL, "expected 10022, got %d\n", GetLastError()); 7259 ret = recv(fdRead, tmp_buf, sizeof(tmp_buf), 0); 7260 ok(ret == 1, "expected 1, got %d\n", ret); 7261 ok(tmp_buf[0] == 'A', "expected 'A', got 0x%02X\n", tmp_buf[0]); 7262 7263 /* Test connection closed notifications */ 7264 ret = closesocket(fdRead); 7265 ok(ret == 0, "expected 0, got %d\n", ret); 7266 POLL_CLEAR(); 7267 POLL_SET(fdListen, POLLIN | POLLOUT); 7268 POLL_SET(fdWrite, POLLIN); 7269 ret = pWSAPoll(fds, ix, poll_timeout); 7270 ok(ret == 1, "expected 1, got %d\n", ret); 7271 ok(POLL_ISSET(fdWrite, POLLHUP), "fdWrite socket events incorrect\n"); 7272 ret = recv(fdWrite, tmp_buf, sizeof(tmp_buf), 0); 7273 ok(ret == 0, "expected 0, got %d\n", ret); 7274 7275 /* When a connection is attempted to a non-listening socket due to a bug 7276 * in the MS code it will never be notified. This is a long standing issue 7277 * that will never be fixed for compatibility reasons so we have to deal 7278 * with it manually. */ 7279 ret = closesocket(fdWrite); 7280 ok(ret == 0, "expected 0, got %d\n", ret); 7281 ret = closesocket(fdListen); 7282 ok(ret == 0, "expected 0, got %d\n", ret); 7283 len = sizeof(address); 7284 fdWrite = setup_connector_socket(&address, len, TRUE); 7285 POLL_CLEAR(); 7286 POLL_SET(fdWrite, POLLIN | POLLOUT); 7287 poll_timeout = 2000; 7288 ret = pWSAPoll(fds, ix, poll_timeout); 7289 todo_wine 7290 ok(ret == 0, "expected 0, got %d\n", ret); 7291 len = sizeof(id); 7292 id = 0xdeadbeef; 7293 err = getsockopt(fdWrite, SOL_SOCKET, SO_ERROR, (char*)&id, &len); 7294 ok(!err, "getsockopt failed with %d\n", WSAGetLastError()); 7295 ok(id == WSAECONNREFUSED, "expected 10061, got %d\n", id); 7296 closesocket(fdWrite); 7297 7298 /* Try poll() on a closed socket after connection */ 7299 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 7300 closesocket(fdRead); 7301 POLL_CLEAR(); 7302 POLL_SET(fdWrite, POLLIN | POLLOUT); 7303 POLL_SET(fdRead, POLLIN | POLLOUT); 7304 ret = pWSAPoll(fds, ix, poll_timeout); 7305 ok(ret == 1, "expected 1, got %d\n", ret); 7306 ok(POLL_ISSET(fdRead, POLLNVAL), "fdRead socket events incorrect\n"); 7307 POLL_CLEAR(); 7308 POLL_SET(fdWrite, POLLIN | POLLOUT); 7309 ret = pWSAPoll(fds, ix, poll_timeout); 7310 ok(ret == 1, "expected 1, got %d\n", ret); 7311 todo_wine 7312 ok(POLL_ISSET(fdWrite, POLLWRNORM | POLLHUP) || broken(POLL_ISSET(fdWrite, POLLWRNORM)) /* <= 2008 */, 7313 "fdWrite socket events incorrect\n"); 7314 closesocket(fdWrite); 7315 7316 /* Close the socket currently being polled in a thread */ 7317 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 7318 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id); 7319 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError()); 7320 POLL_CLEAR(); 7321 POLL_SET(fdWrite, POLLIN | POLLOUT); 7322 ret = pWSAPoll(fds, ix, poll_timeout); 7323 ok(ret == 1, "expected 1, got %d\n", ret); 7324 ok(POLL_ISSET(fdWrite, POLLWRNORM), "fdWrite socket events incorrect\n"); 7325 WaitForSingleObject (thread_handle, 1000); 7326 closesocket(fdRead); 7327 /* test again with less flags - behavior changes */ 7328 ok(!tcp_socketpair(&fdRead, &fdWrite), "creating socket pair failed\n"); 7329 thread_handle = CreateThread(NULL, 0, SelectCloseThread, &fdWrite, 0, &id); 7330 ok(thread_handle != NULL, "CreateThread failed unexpectedly: %d\n", GetLastError()); 7331 POLL_CLEAR(); 7332 POLL_SET(fdWrite, POLLIN); 7333 ret = pWSAPoll(fds, ix, poll_timeout); 7334 ok(ret == 1, "expected 1, got %d\n", ret); 7335 ok(POLL_ISSET(fdWrite, POLLNVAL), "fdWrite socket events incorrect\n"); 7336 WaitForSingleObject (thread_handle, 1000); 7337 closesocket(fdRead); 7338 } 7339 #undef POLL_SET 7340 #undef POLL_ISSET 7341 #undef POLL_CLEAR 7342 7343 static void test_GetAddrInfoW(void) 7344 { 7345 static const WCHAR port[] = {'8','0',0}; 7346 static const WCHAR empty[] = {0}; 7347 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0}; 7348 static const WCHAR nxdomain[] = 7349 {'n','x','d','o','m','a','i','n','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0}; 7350 static const WCHAR zero[] = {'0',0}; 7351 int i, ret; 7352 ADDRINFOW *result, *result2, *p, hint; 7353 WCHAR name[256]; 7354 DWORD size = sizeof(name)/sizeof(WCHAR); 7355 /* te su to.winehq.org written in katakana */ 7356 static const WCHAR idn_domain[] = 7357 {0x30C6,0x30B9,0x30C8,'.','w','i','n','e','h','q','.','o','r','g',0}; 7358 static const WCHAR idn_punycode[] = 7359 {'x','n','-','-','z','c','k','z','a','h','.','w','i','n','e','h','q','.','o','r','g',0}; 7360 7361 if (!pGetAddrInfoW || !pFreeAddrInfoW) 7362 { 7363 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n"); 7364 return; 7365 } 7366 memset(&hint, 0, sizeof(ADDRINFOW)); 7367 name[0] = 0; 7368 GetComputerNameExW( ComputerNamePhysicalDnsHostname, name, &size ); 7369 7370 result = (ADDRINFOW *)0xdeadbeef; 7371 WSASetLastError(0xdeadbeef); 7372 ret = pGetAddrInfoW(NULL, NULL, NULL, &result); 7373 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7374 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7375 ok(result == NULL, "got %p\n", result); 7376 7377 result = NULL; 7378 WSASetLastError(0xdeadbeef); 7379 ret = pGetAddrInfoW(empty, NULL, NULL, &result); 7380 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7381 ok(result != NULL, "GetAddrInfoW failed\n"); 7382 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7383 pFreeAddrInfoW(result); 7384 7385 result = NULL; 7386 ret = pGetAddrInfoW(NULL, zero, NULL, &result); 7387 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7388 ok(result != NULL, "GetAddrInfoW failed\n"); 7389 7390 result2 = NULL; 7391 ret = pGetAddrInfoW(NULL, empty, NULL, &result2); 7392 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7393 ok(result2 != NULL, "GetAddrInfoW failed\n"); 7394 compare_addrinfow(result, result2); 7395 pFreeAddrInfoW(result); 7396 pFreeAddrInfoW(result2); 7397 7398 result = NULL; 7399 ret = pGetAddrInfoW(empty, zero, NULL, &result); 7400 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7401 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7402 ok(result != NULL, "GetAddrInfoW failed\n"); 7403 7404 result2 = NULL; 7405 ret = pGetAddrInfoW(empty, empty, NULL, &result2); 7406 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7407 ok(result2 != NULL, "GetAddrInfoW failed\n"); 7408 compare_addrinfow(result, result2); 7409 pFreeAddrInfoW(result); 7410 pFreeAddrInfoW(result2); 7411 7412 result = NULL; 7413 ret = pGetAddrInfoW(localhost, NULL, NULL, &result); 7414 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7415 pFreeAddrInfoW(result); 7416 7417 result = NULL; 7418 ret = pGetAddrInfoW(localhost, empty, NULL, &result); 7419 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7420 pFreeAddrInfoW(result); 7421 7422 result = NULL; 7423 ret = pGetAddrInfoW(localhost, zero, NULL, &result); 7424 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7425 pFreeAddrInfoW(result); 7426 7427 result = NULL; 7428 ret = pGetAddrInfoW(localhost, port, NULL, &result); 7429 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7430 pFreeAddrInfoW(result); 7431 7432 result = NULL; 7433 ret = pGetAddrInfoW(localhost, NULL, &hint, &result); 7434 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7435 pFreeAddrInfoW(result); 7436 7437 result = NULL; 7438 SetLastError(0xdeadbeef); 7439 ret = pGetAddrInfoW(localhost, port, &hint, &result); 7440 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7441 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7442 pFreeAddrInfoW(result); 7443 7444 /* try to get information from the computer name, result is the same 7445 * as if requesting with an empty host name. */ 7446 ret = pGetAddrInfoW(name, NULL, NULL, &result); 7447 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7448 ok(result != NULL, "GetAddrInfoW failed\n"); 7449 7450 ret = pGetAddrInfoW(empty, NULL, NULL, &result2); 7451 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7452 #ifdef __REACTOS__ 7453 ok(result2 != NULL, "GetAddrInfoW failed\n"); 7454 #else 7455 ok(result != NULL, "GetAddrInfoW failed\n"); 7456 #endif 7457 compare_addrinfow(result, result2); 7458 pFreeAddrInfoW(result); 7459 pFreeAddrInfoW(result2); 7460 7461 ret = pGetAddrInfoW(name, empty, NULL, &result); 7462 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7463 ok(result != NULL, "GetAddrInfoW failed\n"); 7464 7465 ret = pGetAddrInfoW(empty, empty, NULL, &result2); 7466 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError()); 7467 #ifdef __REACTOS__ 7468 ok(result2 != NULL, "GetAddrInfoW failed\n"); 7469 #else 7470 ok(result != NULL, "GetAddrInfoW failed\n"); 7471 #endif 7472 compare_addrinfow(result, result2); 7473 pFreeAddrInfoW(result); 7474 pFreeAddrInfoW(result2); 7475 7476 result = (ADDRINFOW *)0xdeadbeef; 7477 WSASetLastError(0xdeadbeef); 7478 ret = pGetAddrInfoW(NULL, NULL, NULL, &result); 7479 if(ret == 0) 7480 { 7481 skip("nxdomain returned success. Broken ISP redirects?\n"); 7482 return; 7483 } 7484 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7485 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7486 ok(result == NULL, "got %p\n", result); 7487 7488 result = (ADDRINFOW *)0xdeadbeef; 7489 WSASetLastError(0xdeadbeef); 7490 ret = pGetAddrInfoW(nxdomain, NULL, NULL, &result); 7491 if(ret == 0) 7492 { 7493 skip("nxdomain returned success. Broken ISP redirects?\n"); 7494 return; 7495 } 7496 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7497 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7498 ok(result == NULL, "got %p\n", result); 7499 7500 for (i = 0;i < (sizeof(hinttests) / sizeof(hinttests[0]));i++) 7501 { 7502 hint.ai_family = hinttests[i].family; 7503 hint.ai_socktype = hinttests[i].socktype; 7504 hint.ai_protocol = hinttests[i].protocol; 7505 7506 result = NULL; 7507 SetLastError(0xdeadbeef); 7508 ret = pGetAddrInfoW(localhost, NULL, &hint, &result); 7509 if (!ret) 7510 { 7511 if (hinttests[i].error) 7512 ok(0, "test %d: GetAddrInfoW succeeded unexpectedly\n", i); 7513 else 7514 { 7515 p = result; 7516 do 7517 { 7518 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */ 7519 if (hinttests[i].family == AF_UNSPEC) 7520 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6, 7521 "test %d: expected AF_INET or AF_INET6, got %d\n", 7522 i, p->ai_family); 7523 else 7524 ok(p->ai_family == hinttests[i].family, 7525 "test %d: expected family %d, got %d\n", 7526 i, hinttests[i].family, p->ai_family); 7527 7528 ok(p->ai_socktype == hinttests[i].socktype, 7529 "test %d: expected type %d, got %d\n", 7530 i, hinttests[i].socktype, p->ai_socktype); 7531 ok(p->ai_protocol == hinttests[i].protocol, 7532 "test %d: expected protocol %d, got %d\n", 7533 i, hinttests[i].protocol, p->ai_protocol); 7534 p = p->ai_next; 7535 } 7536 while (p); 7537 } 7538 pFreeAddrInfoW(result); 7539 } 7540 else 7541 { 7542 DWORD err = WSAGetLastError(); 7543 if (hinttests[i].error) 7544 ok(hinttests[i].error == err, "test %d: GetAddrInfoW failed with error %d, expected %d\n", 7545 i, err, hinttests[i].error); 7546 else 7547 ok(0, "test %d: GetAddrInfoW failed with %d (err %d)\n", i, ret, err); 7548 } 7549 } 7550 7551 /* Test IDN resolution (Internationalized Domain Names) present since Windows 8 */ 7552 trace("Testing punycode IDN %s\n", wine_dbgstr_w(idn_punycode)); 7553 result = NULL; 7554 ret = pGetAddrInfoW(idn_punycode, NULL, NULL, &result); 7555 ok(!ret, "got %d expected success\n", ret); 7556 ok(result != NULL, "got %p\n", result); 7557 pFreeAddrInfoW(result); 7558 7559 hint.ai_family = AF_INET; 7560 hint.ai_socktype = 0; 7561 hint.ai_protocol = 0; 7562 hint.ai_flags = 0; 7563 7564 result = NULL; 7565 ret = pGetAddrInfoW(idn_punycode, NULL, &hint, &result); 7566 ok(!ret, "got %d expected success\n", ret); 7567 ok(result != NULL, "got %p\n", result); 7568 7569 trace("Testing unicode IDN %s\n", wine_dbgstr_w(idn_domain)); 7570 result2 = NULL; 7571 ret = pGetAddrInfoW(idn_domain, NULL, NULL, &result2); 7572 if (ret == WSAHOST_NOT_FOUND && broken(1)) 7573 { 7574 pFreeAddrInfoW(result); 7575 win_skip("IDN resolution not supported in Win <= 7\n"); 7576 return; 7577 } 7578 7579 ok(!ret, "got %d expected success\n", ret); 7580 ok(result2 != NULL, "got %p\n", result2); 7581 pFreeAddrInfoW(result2); 7582 7583 hint.ai_family = AF_INET; 7584 hint.ai_socktype = 0; 7585 hint.ai_protocol = 0; 7586 hint.ai_flags = 0; 7587 7588 result2 = NULL; 7589 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2); 7590 ok(!ret, "got %d expected success\n", ret); 7591 ok(result2 != NULL, "got %p\n", result2); 7592 7593 /* ensure manually resolved punycode and unicode hosts result in same data */ 7594 compare_addrinfow(result, result2); 7595 7596 pFreeAddrInfoW(result); 7597 pFreeAddrInfoW(result2); 7598 7599 hint.ai_family = AF_INET; 7600 hint.ai_socktype = 0; 7601 hint.ai_protocol = 0; 7602 hint.ai_flags = 0; 7603 7604 result2 = NULL; 7605 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2); 7606 ok(!ret, "got %d expected success\n", ret); 7607 ok(result2 != NULL, "got %p\n", result2); 7608 pFreeAddrInfoW(result2); 7609 7610 /* Disable IDN resolution and test again*/ 7611 hint.ai_family = AF_INET; 7612 hint.ai_socktype = 0; 7613 hint.ai_protocol = 0; 7614 hint.ai_flags = AI_DISABLE_IDN_ENCODING; 7615 7616 SetLastError(0xdeadbeef); 7617 result2 = NULL; 7618 ret = pGetAddrInfoW(idn_domain, NULL, &hint, &result2); 7619 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7620 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7621 ok(result2 == NULL, "got %p\n", result2); 7622 } 7623 7624 static void test_GetAddrInfoExW(void) 7625 { 7626 static const WCHAR empty[] = {0}; 7627 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0}; 7628 static const WCHAR winehq[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0}; 7629 ADDRINFOEXW *result; 7630 OVERLAPPED overlapped; 7631 HANDLE event; 7632 int ret; 7633 7634 if (!pGetAddrInfoExW || !pGetAddrInfoExOverlappedResult) 7635 { 7636 win_skip("GetAddrInfoExW and/or GetAddrInfoExOverlappedResult not present\n"); 7637 return; 7638 } 7639 7640 event = WSACreateEvent(); 7641 7642 result = (ADDRINFOEXW *)0xdeadbeef; 7643 WSASetLastError(0xdeadbeef); 7644 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL); 7645 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7646 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7647 ok(result == NULL, "got %p\n", result); 7648 7649 result = NULL; 7650 WSASetLastError(0xdeadbeef); 7651 ret = pGetAddrInfoExW(empty, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL); 7652 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError()); 7653 ok(result != NULL, "GetAddrInfoW failed\n"); 7654 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7655 pFreeAddrInfoExW(result); 7656 7657 result = NULL; 7658 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, NULL, NULL, NULL); 7659 ok(!ret, "GetAddrInfoExW failed with %d\n", WSAGetLastError()); 7660 pFreeAddrInfoExW(result); 7661 7662 result = (void*)0xdeadbeef; 7663 memset(&overlapped, 0xcc, sizeof(overlapped)); 7664 overlapped.hEvent = event; 7665 ResetEvent(event); 7666 ret = pGetAddrInfoExW(localhost, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL); 7667 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError()); 7668 ok(!result, "result != NULL\n"); 7669 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n"); 7670 ret = pGetAddrInfoExOverlappedResult(&overlapped); 7671 ok(!ret, "overlapped result is %d\n", ret); 7672 pFreeAddrInfoExW(result); 7673 7674 result = (void*)0xdeadbeef; 7675 memset(&overlapped, 0xcc, sizeof(overlapped)); 7676 ResetEvent(event); 7677 overlapped.hEvent = event; 7678 WSASetLastError(0xdeadbeef); 7679 ret = pGetAddrInfoExW(winehq, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL); 7680 ok(ret == ERROR_IO_PENDING, "GetAddrInfoExW failed with %d\n", WSAGetLastError()); 7681 ok(WSAGetLastError() == ERROR_IO_PENDING, "expected 11001, got %d\n", WSAGetLastError()); 7682 ret = overlapped.Internal; 7683 ok(ret == WSAEINPROGRESS || ret == ERROR_SUCCESS, "overlapped.Internal = %u\n", ret); 7684 ok(WaitForSingleObject(event, 1000) == WAIT_OBJECT_0, "wait failed\n"); 7685 ret = pGetAddrInfoExOverlappedResult(&overlapped); 7686 ok(!ret, "overlapped result is %d\n", ret); 7687 ok(overlapped.hEvent == event, "hEvent changed %p\n", overlapped.hEvent); 7688 ok(overlapped.Internal == ERROR_SUCCESS, "overlapped.Internal = %lx\n", overlapped.Internal); 7689 ok(overlapped.Pointer == &result, "overlapped.Pointer != &result\n"); 7690 ok(result != NULL, "result == NULL\n"); 7691 if (result != NULL) 7692 { 7693 ok(!result->ai_blob, "ai_blob != NULL\n"); 7694 ok(!result->ai_bloblen, "ai_bloblen != 0\n"); 7695 ok(!result->ai_provider, "ai_provider = %s\n", wine_dbgstr_guid(result->ai_provider)); 7696 pFreeAddrInfoExW(result); 7697 } 7698 7699 result = (void*)0xdeadbeef; 7700 memset(&overlapped, 0xcc, sizeof(overlapped)); 7701 ResetEvent(event); 7702 overlapped.hEvent = event; 7703 ret = pGetAddrInfoExW(NULL, NULL, NS_DNS, NULL, NULL, &result, NULL, &overlapped, NULL, NULL); 7704 todo_wine 7705 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7706 todo_wine 7707 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7708 ok(result == NULL, "got %p\n", result); 7709 ret = WaitForSingleObject(event, 0); 7710 todo_wine_if(ret != WAIT_TIMEOUT) /* Remove when abowe todo_wines are fixed */ 7711 ok(ret == WAIT_TIMEOUT, "wait failed\n"); 7712 7713 WSACloseEvent(event); 7714 } 7715 7716 static void verify_ipv6_addrinfo(ADDRINFOA *result, const char *expectedIp) 7717 { 7718 SOCKADDR_IN6 *sockaddr6; 7719 char ipBuffer[256]; 7720 const char *ret; 7721 7722 ok(result->ai_family == AF_INET6, "ai_family == %d\n", result->ai_family); 7723 ok(result->ai_addrlen >= sizeof(struct sockaddr_in6), "ai_addrlen == %d\n", (int)result->ai_addrlen); 7724 ok(result->ai_addr != NULL, "ai_addr == NULL\n"); 7725 7726 if (result->ai_addr != NULL) 7727 { 7728 sockaddr6 = (SOCKADDR_IN6 *)result->ai_addr; 7729 ok(sockaddr6->sin6_family == AF_INET6, "ai_addr->sin6_family == %d\n", sockaddr6->sin6_family); 7730 ok(sockaddr6->sin6_port == 0, "ai_addr->sin6_port == %d\n", sockaddr6->sin6_port); 7731 7732 ZeroMemory(ipBuffer, sizeof(ipBuffer)); 7733 ret = pInetNtop(AF_INET6, &sockaddr6->sin6_addr, ipBuffer, sizeof(ipBuffer)); 7734 ok(ret != NULL, "inet_ntop failed (%d)\n", WSAGetLastError()); 7735 ok(strcmp(ipBuffer, expectedIp) == 0, "ai_addr->sin6_addr == '%s' (expected '%s')\n", ipBuffer, expectedIp); 7736 } 7737 } 7738 7739 static void test_getaddrinfo(void) 7740 { 7741 int i, ret; 7742 ADDRINFOA *result, *result2, *p, hint; 7743 SOCKADDR_IN *sockaddr; 7744 CHAR name[256], *ip; 7745 DWORD size = sizeof(name); 7746 7747 if (!pgetaddrinfo || !pfreeaddrinfo) 7748 { 7749 win_skip("getaddrinfo and/or freeaddrinfo not present\n"); 7750 return; 7751 } 7752 memset(&hint, 0, sizeof(ADDRINFOA)); 7753 GetComputerNameExA( ComputerNamePhysicalDnsHostname, name, &size ); 7754 7755 result = (ADDRINFOA *)0xdeadbeef; 7756 WSASetLastError(0xdeadbeef); 7757 ret = pgetaddrinfo(NULL, NULL, NULL, &result); 7758 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7759 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7760 ok(result == NULL, "got %p\n", result); 7761 7762 result = NULL; 7763 WSASetLastError(0xdeadbeef); 7764 ret = pgetaddrinfo("", NULL, NULL, &result); 7765 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7766 ok(result != NULL, "getaddrinfo failed\n"); 7767 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7768 pfreeaddrinfo(result); 7769 7770 result = NULL; 7771 ret = pgetaddrinfo(NULL, "0", NULL, &result); 7772 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7773 ok(result != NULL, "getaddrinfo failed\n"); 7774 7775 result2 = NULL; 7776 ret = pgetaddrinfo(NULL, "", NULL, &result2); 7777 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7778 ok(result2 != NULL, "getaddrinfo failed\n"); 7779 compare_addrinfo(result, result2); 7780 pfreeaddrinfo(result); 7781 pfreeaddrinfo(result2); 7782 7783 result = NULL; 7784 WSASetLastError(0xdeadbeef); 7785 ret = pgetaddrinfo("", "0", NULL, &result); 7786 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7787 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7788 ok(result != NULL, "getaddrinfo failed\n"); 7789 7790 result2 = NULL; 7791 ret = pgetaddrinfo("", "", NULL, &result2); 7792 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7793 ok(result2 != NULL, "getaddrinfo failed\n"); 7794 compare_addrinfo(result, result2); 7795 pfreeaddrinfo(result); 7796 pfreeaddrinfo(result2); 7797 7798 result = NULL; 7799 ret = pgetaddrinfo("localhost", NULL, NULL, &result); 7800 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7801 pfreeaddrinfo(result); 7802 7803 result = NULL; 7804 ret = pgetaddrinfo("localhost", "", NULL, &result); 7805 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7806 pfreeaddrinfo(result); 7807 7808 result = NULL; 7809 ret = pgetaddrinfo("localhost", "0", NULL, &result); 7810 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7811 pfreeaddrinfo(result); 7812 7813 result = NULL; 7814 ret = pgetaddrinfo("localhost", "80", NULL, &result); 7815 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7816 pfreeaddrinfo(result); 7817 7818 result = NULL; 7819 ret = pgetaddrinfo("localhost", NULL, &hint, &result); 7820 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7821 pfreeaddrinfo(result); 7822 7823 result = NULL; 7824 WSASetLastError(0xdeadbeef); 7825 ret = pgetaddrinfo("localhost", "80", &hint, &result); 7826 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7827 ok(WSAGetLastError() == 0, "expected 0, got %d\n", WSAGetLastError()); 7828 pfreeaddrinfo(result); 7829 7830 hint.ai_flags = AI_NUMERICHOST; 7831 result = (void*)0xdeadbeef; 7832 ret = pgetaddrinfo("localhost", "80", &hint, &result); 7833 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", WSAGetLastError()); 7834 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected WSAHOST_NOT_FOUND, got %d\n", WSAGetLastError()); 7835 ok(!result, "result = %p\n", result); 7836 hint.ai_flags = 0; 7837 7838 /* try to get information from the computer name, result is the same 7839 * as if requesting with an empty host name. */ 7840 ret = pgetaddrinfo(name, NULL, NULL, &result); 7841 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7842 #ifdef __REACTOS__ 7843 ok(result != NULL, "getaddrinfo failed\n"); 7844 #else 7845 ok(result != NULL, "GetAddrInfoW failed\n"); 7846 #endif 7847 7848 ret = pgetaddrinfo("", NULL, NULL, &result2); 7849 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7850 #ifdef __REACTOS__ 7851 ok(result2 != NULL, "getaddrinfo failed\n"); 7852 #else 7853 ok(result != NULL, "GetAddrInfoW failed\n"); 7854 #endif 7855 compare_addrinfo(result, result2); 7856 pfreeaddrinfo(result); 7857 pfreeaddrinfo(result2); 7858 7859 ret = pgetaddrinfo(name, "", NULL, &result); 7860 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7861 #ifdef __REACTOS__ 7862 ok(result != NULL, "getaddrinfo failed\n"); 7863 #else 7864 ok(result != NULL, "GetAddrInfoW failed\n"); 7865 #endif 7866 7867 ret = pgetaddrinfo("", "", NULL, &result2); 7868 ok(!ret, "getaddrinfo failed with %d\n", WSAGetLastError()); 7869 #ifdef __REACTOS__ 7870 ok(result2 != NULL, "getaddrinfo failed\n"); 7871 #else 7872 ok(result != NULL, "GetAddrInfoW failed\n"); 7873 #endif 7874 compare_addrinfo(result, result2); 7875 pfreeaddrinfo(result); 7876 pfreeaddrinfo(result2); 7877 7878 result = (ADDRINFOA *)0xdeadbeef; 7879 WSASetLastError(0xdeadbeef); 7880 ret = pgetaddrinfo("nxdomain.codeweavers.com", NULL, NULL, &result); 7881 if(ret == 0) 7882 { 7883 skip("nxdomain returned success. Broken ISP redirects?\n"); 7884 return; 7885 } 7886 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret); 7887 ok(WSAGetLastError() == WSAHOST_NOT_FOUND, "expected 11001, got %d\n", WSAGetLastError()); 7888 ok(result == NULL, "got %p\n", result); 7889 7890 /* Test IPv4 address conversion */ 7891 result = NULL; 7892 ret = pgetaddrinfo("192.168.1.253", NULL, NULL, &result); 7893 ok(!ret, "getaddrinfo failed with %d\n", ret); 7894 ok(result->ai_family == AF_INET, "ai_family == %d\n", result->ai_family); 7895 ok(result->ai_addrlen >= sizeof(struct sockaddr_in), "ai_addrlen == %d\n", (int)result->ai_addrlen); 7896 ok(result->ai_addr != NULL, "ai_addr == NULL\n"); 7897 sockaddr = (SOCKADDR_IN *)result->ai_addr; 7898 ok(sockaddr->sin_family == AF_INET, "ai_addr->sin_family == %d\n", sockaddr->sin_family); 7899 ok(sockaddr->sin_port == 0, "ai_addr->sin_port == %d\n", sockaddr->sin_port); 7900 7901 ip = inet_ntoa(sockaddr->sin_addr); 7902 ok(strcmp(ip, "192.168.1.253") == 0, "sockaddr->ai_addr == '%s'\n", ip); 7903 pfreeaddrinfo(result); 7904 7905 /* Test IPv4 address conversion with port */ 7906 result = NULL; 7907 hint.ai_flags = AI_NUMERICHOST; 7908 ret = pgetaddrinfo("192.168.1.253:1024", NULL, &hint, &result); 7909 hint.ai_flags = 0; 7910 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo returned unexpected result: %d\n", ret); 7911 ok(result == NULL, "expected NULL, got %p\n", result); 7912 7913 /* Test IPv6 address conversion */ 7914 result = NULL; 7915 SetLastError(0xdeadbeef); 7916 ret = pgetaddrinfo("2a00:2039:dead:beef:cafe::6666", NULL, NULL, &result); 7917 7918 if (result != NULL) 7919 { 7920 ok(!ret, "getaddrinfo failed with %d\n", ret); 7921 verify_ipv6_addrinfo(result, "2a00:2039:dead:beef:cafe::6666"); 7922 pfreeaddrinfo(result); 7923 7924 /* Test IPv6 address conversion with brackets */ 7925 result = NULL; 7926 ret = pgetaddrinfo("[beef::cafe]", NULL, NULL, &result); 7927 ok(!ret, "getaddrinfo failed with %d\n", ret); 7928 verify_ipv6_addrinfo(result, "beef::cafe"); 7929 pfreeaddrinfo(result); 7930 7931 /* Test IPv6 address conversion with brackets and hints */ 7932 memset(&hint, 0, sizeof(ADDRINFOA)); 7933 hint.ai_flags = AI_NUMERICHOST; 7934 hint.ai_family = AF_INET6; 7935 result = NULL; 7936 ret = pgetaddrinfo("[beef::cafe]", NULL, &hint, &result); 7937 ok(!ret, "getaddrinfo failed with %d\n", ret); 7938 verify_ipv6_addrinfo(result, "beef::cafe"); 7939 pfreeaddrinfo(result); 7940 7941 memset(&hint, 0, sizeof(ADDRINFOA)); 7942 hint.ai_flags = AI_NUMERICHOST; 7943 hint.ai_family = AF_INET; 7944 result = NULL; 7945 ret = pgetaddrinfo("[beef::cafe]", NULL, &hint, &result); 7946 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret); 7947 7948 /* Test IPv6 address conversion with brackets and port */ 7949 result = NULL; 7950 ret = pgetaddrinfo("[beef::cafe]:10239", NULL, NULL, &result); 7951 ok(!ret, "getaddrinfo failed with %d\n", ret); 7952 verify_ipv6_addrinfo(result, "beef::cafe"); 7953 pfreeaddrinfo(result); 7954 7955 /* Test IPv6 address conversion with unmatched brackets */ 7956 result = NULL; 7957 hint.ai_flags = AI_NUMERICHOST; 7958 ret = pgetaddrinfo("[beef::cafe", NULL, &hint, &result); 7959 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret); 7960 7961 ret = pgetaddrinfo("beef::cafe]", NULL, &hint, &result); 7962 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret); 7963 } 7964 else 7965 { 7966 ok(ret == WSAHOST_NOT_FOUND, "getaddrinfo failed with %d\n", ret); 7967 win_skip("getaddrinfo does not support IPV6\n"); 7968 } 7969 7970 hint.ai_flags = 0; 7971 7972 for (i = 0;i < (sizeof(hinttests) / sizeof(hinttests[0]));i++) 7973 { 7974 hint.ai_family = hinttests[i].family; 7975 hint.ai_socktype = hinttests[i].socktype; 7976 hint.ai_protocol = hinttests[i].protocol; 7977 7978 result = NULL; 7979 SetLastError(0xdeadbeef); 7980 ret = pgetaddrinfo("localhost", NULL, &hint, &result); 7981 if(!ret) 7982 { 7983 if (hinttests[i].error) 7984 ok(0, "test %d: getaddrinfo succeeded unexpectedly\n", i); 7985 else 7986 { 7987 p = result; 7988 do 7989 { 7990 /* when AF_UNSPEC is used the return will be either AF_INET or AF_INET6 */ 7991 if (hinttests[i].family == AF_UNSPEC) 7992 ok(p->ai_family == AF_INET || p->ai_family == AF_INET6, 7993 "test %d: expected AF_INET or AF_INET6, got %d\n", 7994 i, p->ai_family); 7995 else 7996 ok(p->ai_family == hinttests[i].family, 7997 "test %d: expected family %d, got %d\n", 7998 i, hinttests[i].family, p->ai_family); 7999 8000 ok(p->ai_socktype == hinttests[i].socktype, 8001 "test %d: expected type %d, got %d\n", 8002 i, hinttests[i].socktype, p->ai_socktype); 8003 ok(p->ai_protocol == hinttests[i].protocol, 8004 "test %d: expected protocol %d, got %d\n", 8005 i, hinttests[i].protocol, p->ai_protocol); 8006 p = p->ai_next; 8007 } 8008 while (p); 8009 } 8010 pfreeaddrinfo(result); 8011 } 8012 else 8013 { 8014 DWORD err = WSAGetLastError(); 8015 if (hinttests[i].error) 8016 ok(hinttests[i].error == err, "test %d: getaddrinfo failed with error %d, expected %d\n", 8017 i, err, hinttests[i].error); 8018 else 8019 ok(0, "test %d: getaddrinfo failed with %d (err %d)\n", i, ret, err); 8020 } 8021 } 8022 } 8023 8024 static void test_ConnectEx(void) 8025 { 8026 SOCKET listener = INVALID_SOCKET; 8027 SOCKET acceptor = INVALID_SOCKET; 8028 SOCKET connector = INVALID_SOCKET; 8029 struct sockaddr_in address, conaddress; 8030 int addrlen; 8031 OVERLAPPED overlapped; 8032 LPFN_CONNECTEX pConnectEx; 8033 GUID connectExGuid = WSAID_CONNECTEX; 8034 DWORD bytesReturned; 8035 char buffer[1024]; 8036 BOOL bret; 8037 DWORD dwret; 8038 int iret; 8039 8040 memset(&overlapped, 0, sizeof(overlapped)); 8041 8042 listener = socket(AF_INET, SOCK_STREAM, 0); 8043 if (listener == INVALID_SOCKET) { 8044 skip("could not create listener socket, error %d\n", WSAGetLastError()); 8045 goto end; 8046 } 8047 8048 connector = socket(AF_INET, SOCK_STREAM, 0); 8049 if (connector == INVALID_SOCKET) { 8050 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8051 goto end; 8052 } 8053 8054 memset(&address, 0, sizeof(address)); 8055 address.sin_family = AF_INET; 8056 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 8057 iret = bind(listener, (struct sockaddr*)&address, sizeof(address)); 8058 if (iret != 0) { 8059 skip("failed to bind, error %d\n", WSAGetLastError()); 8060 goto end; 8061 } 8062 8063 addrlen = sizeof(address); 8064 iret = getsockname(listener, (struct sockaddr*)&address, &addrlen); 8065 if (iret != 0) { 8066 skip("failed to lookup bind address, error %d\n", WSAGetLastError()); 8067 goto end; 8068 } 8069 8070 if (set_blocking(listener, TRUE)) { 8071 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 8072 goto end; 8073 } 8074 8075 bytesReturned = 0xdeadbeef; 8076 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &connectExGuid, sizeof(connectExGuid), 8077 &pConnectEx, sizeof(pConnectEx), &bytesReturned, NULL, NULL); 8078 if (iret) { 8079 win_skip("WSAIoctl failed to get ConnectEx with ret %d + errno %d\n", iret, WSAGetLastError()); 8080 goto end; 8081 } 8082 8083 ok(bytesReturned == sizeof(pConnectEx), "expected sizeof(pConnectEx), got %u\n", bytesReturned); 8084 8085 bret = pConnectEx(INVALID_SOCKET, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped); 8086 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "ConnectEx on invalid socket " 8087 "returned %d + errno %d\n", bret, WSAGetLastError()); 8088 8089 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped); 8090 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "ConnectEx on a unbound socket " 8091 "returned %d + errno %d\n", bret, WSAGetLastError()); 8092 if (bret == TRUE || WSAGetLastError() != WSAEINVAL) 8093 { 8094 acceptor = accept(listener, NULL, NULL); 8095 if (acceptor != INVALID_SOCKET) { 8096 closesocket(acceptor); 8097 acceptor = INVALID_SOCKET; 8098 } 8099 8100 closesocket(connector); 8101 connector = socket(AF_INET, SOCK_STREAM, 0); 8102 if (connector == INVALID_SOCKET) { 8103 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8104 goto end; 8105 } 8106 } 8107 8108 /* ConnectEx needs a bound socket */ 8109 memset(&conaddress, 0, sizeof(conaddress)); 8110 conaddress.sin_family = AF_INET; 8111 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 8112 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress)); 8113 if (iret != 0) { 8114 skip("failed to bind, error %d\n", WSAGetLastError()); 8115 goto end; 8116 } 8117 8118 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, NULL); 8119 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "ConnectEx on a NULL overlapped " 8120 "returned %d + errno %d\n", bret, WSAGetLastError()); 8121 8122 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 8123 if (overlapped.hEvent == NULL) { 8124 skip("could not create event object, errno = %d\n", GetLastError()); 8125 goto end; 8126 } 8127 8128 iret = listen(listener, 1); 8129 if (iret != 0) { 8130 skip("listening failed, errno = %d\n", WSAGetLastError()); 8131 goto end; 8132 } 8133 8134 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped); 8135 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: " 8136 "returned %d + errno %d\n", bret, WSAGetLastError()); 8137 dwret = WaitForSingleObject(overlapped.hEvent, 15000); 8138 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError()); 8139 8140 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE); 8141 ok(bret, "Connecting failed, error %d\n", GetLastError()); 8142 ok(bytesReturned == 0, "Bytes sent is %d\n", bytesReturned); 8143 8144 closesocket(connector); 8145 connector = socket(AF_INET, SOCK_STREAM, 0); 8146 if (connector == INVALID_SOCKET) { 8147 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8148 goto end; 8149 } 8150 /* ConnectEx needs a bound socket */ 8151 memset(&conaddress, 0, sizeof(conaddress)); 8152 conaddress.sin_family = AF_INET; 8153 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 8154 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress)); 8155 if (iret != 0) { 8156 skip("failed to bind, error %d\n", WSAGetLastError()); 8157 goto end; 8158 } 8159 8160 acceptor = accept(listener, NULL, NULL); 8161 if (acceptor != INVALID_SOCKET) { 8162 closesocket(acceptor); 8163 } 8164 8165 buffer[0] = '1'; 8166 buffer[1] = '2'; 8167 buffer[2] = '3'; 8168 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, buffer, 3, &bytesReturned, &overlapped); 8169 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "ConnectEx failed: " 8170 "returned %d + errno %d\n", bret, WSAGetLastError()); 8171 dwret = WaitForSingleObject(overlapped.hEvent, 15000); 8172 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError()); 8173 8174 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE); 8175 ok(bret, "Connecting failed, error %d\n", GetLastError()); 8176 ok(bytesReturned == 3, "Bytes sent is %d\n", bytesReturned); 8177 8178 acceptor = accept(listener, NULL, NULL); 8179 ok(acceptor != INVALID_SOCKET, "could not accept socket error %d\n", WSAGetLastError()); 8180 8181 bytesReturned = recv(acceptor, buffer, 3, 0); 8182 buffer[4] = 0; 8183 ok(bytesReturned == 3, "Didn't get all sent data, got only %d\n", bytesReturned); 8184 ok(buffer[0] == '1' && buffer[1] == '2' && buffer[2] == '3', 8185 "Failed to get the right data, expected '123', got '%s'\n", buffer); 8186 8187 closesocket(connector); 8188 connector = socket(AF_INET, SOCK_STREAM, 0); 8189 if (connector == INVALID_SOCKET) { 8190 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8191 goto end; 8192 } 8193 /* ConnectEx needs a bound socket */ 8194 memset(&conaddress, 0, sizeof(conaddress)); 8195 conaddress.sin_family = AF_INET; 8196 conaddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 8197 iret = bind(connector, (struct sockaddr*)&conaddress, sizeof(conaddress)); 8198 if (iret != 0) { 8199 skip("failed to bind, error %d\n", WSAGetLastError()); 8200 goto end; 8201 } 8202 8203 if (acceptor != INVALID_SOCKET) { 8204 closesocket(acceptor); 8205 acceptor = INVALID_SOCKET; 8206 } 8207 8208 /* Connect with error */ 8209 closesocket(listener); 8210 listener = INVALID_SOCKET; 8211 8212 address.sin_port = htons(1); 8213 8214 bret = pConnectEx(connector, (struct sockaddr*)&address, addrlen, NULL, 0, &bytesReturned, &overlapped); 8215 ok(bret == FALSE && GetLastError() == ERROR_IO_PENDING, "ConnectEx to bad destination failed: " 8216 "returned %d + errno %d\n", bret, GetLastError()); 8217 dwret = WaitForSingleObject(overlapped.hEvent, 15000); 8218 ok(dwret == WAIT_OBJECT_0, "Waiting for connect event failed with %d + errno %d\n", dwret, GetLastError()); 8219 8220 bret = GetOverlappedResult((HANDLE)connector, &overlapped, &bytesReturned, FALSE); 8221 ok(bret == FALSE && GetLastError() == ERROR_CONNECTION_REFUSED, 8222 "Connecting to a disconnected host returned error %d - %d\n", bret, WSAGetLastError()); 8223 8224 end: 8225 if (overlapped.hEvent) 8226 WSACloseEvent(overlapped.hEvent); 8227 if (listener != INVALID_SOCKET) 8228 closesocket(listener); 8229 if (acceptor != INVALID_SOCKET) 8230 closesocket(acceptor); 8231 if (connector != INVALID_SOCKET) 8232 closesocket(connector); 8233 } 8234 8235 static void test_AcceptEx(void) 8236 { 8237 SOCKET listener = INVALID_SOCKET; 8238 SOCKET acceptor = INVALID_SOCKET; 8239 SOCKET connector = INVALID_SOCKET; 8240 SOCKET connector2 = INVALID_SOCKET; 8241 struct sockaddr_in bindAddress, peerAddress, *readBindAddress, *readRemoteAddress; 8242 int socklen, optlen; 8243 GUID acceptExGuid = WSAID_ACCEPTEX, getAcceptExGuid = WSAID_GETACCEPTEXSOCKADDRS; 8244 LPFN_ACCEPTEX pAcceptEx = NULL; 8245 LPFN_GETACCEPTEXSOCKADDRS pGetAcceptExSockaddrs = NULL; 8246 fd_set fds_accept, fds_send; 8247 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */ 8248 int got, conn1, i; 8249 DWORD bytesReturned, connect_time; 8250 char buffer[1024], ipbuffer[32]; 8251 OVERLAPPED overlapped; 8252 int iret, localSize = sizeof(struct sockaddr_in), remoteSize = localSize; 8253 BOOL bret; 8254 DWORD dwret; 8255 8256 memset(&overlapped, 0, sizeof(overlapped)); 8257 8258 listener = socket(AF_INET, SOCK_STREAM, 0); 8259 if (listener == INVALID_SOCKET) { 8260 skip("could not create listener socket, error %d\n", WSAGetLastError()); 8261 goto end; 8262 } 8263 8264 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8265 if (acceptor == INVALID_SOCKET) { 8266 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8267 goto end; 8268 } 8269 8270 connector = socket(AF_INET, SOCK_STREAM, 0); 8271 if (connector == INVALID_SOCKET) { 8272 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8273 goto end; 8274 } 8275 8276 memset(&bindAddress, 0, sizeof(bindAddress)); 8277 bindAddress.sin_family = AF_INET; 8278 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 8279 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8280 if (iret != 0) { 8281 skip("failed to bind, error %d\n", WSAGetLastError()); 8282 goto end; 8283 } 8284 8285 socklen = sizeof(bindAddress); 8286 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen); 8287 if (iret != 0) { 8288 skip("failed to lookup bind address, error %d\n", WSAGetLastError()); 8289 goto end; 8290 } 8291 8292 if (set_blocking(listener, FALSE)) { 8293 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 8294 goto end; 8295 } 8296 8297 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid), 8298 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL); 8299 if (iret) { 8300 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError()); 8301 goto end; 8302 } 8303 8304 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &getAcceptExGuid, sizeof(getAcceptExGuid), 8305 &pGetAcceptExSockaddrs, sizeof(pGetAcceptExSockaddrs), &bytesReturned, NULL, NULL); 8306 if (iret) { 8307 skip("WSAIoctl failed to get GetAcceptExSockaddrs with ret %d + errno %d\n", iret, WSAGetLastError()); 8308 goto end; 8309 } 8310 8311 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8312 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8313 &bytesReturned, &overlapped); 8314 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket " 8315 "returned %d + errno %d\n", bret, WSAGetLastError()); 8316 8317 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8318 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8319 &bytesReturned, &overlapped); 8320 todo_wine 8321 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket " 8322 "returned %d + errno %d\n", bret, WSAGetLastError()); 8323 8324 iret = listen(listener, 5); 8325 if (iret != 0) { 8326 skip("listening failed, errno = %d\n", WSAGetLastError()); 8327 goto end; 8328 } 8329 8330 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8331 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8332 &bytesReturned, &overlapped); 8333 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid accepting socket " 8334 "returned %d + errno %d\n", bret, WSAGetLastError()); 8335 8336 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8337 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8338 &bytesReturned, &overlapped); 8339 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEFAULT, 8340 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError()); 8341 8342 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16, 8343 &bytesReturned, &overlapped); 8344 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, 8345 "AcceptEx on too small local address size returned %d + errno %d\n", 8346 bret, WSAGetLastError()); 8347 bret = CancelIo((HANDLE) listener); 8348 ok(bret, "Failed to cancel pending accept socket\n"); 8349 8350 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 15, 8351 sizeof(struct sockaddr_in) + 16, &bytesReturned, &overlapped); 8352 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx on too small local address " 8353 "size returned %d + errno %d\n", 8354 bret, WSAGetLastError()); 8355 bret = CancelIo((HANDLE) listener); 8356 ok(bret, "Failed to cancel pending accept socket\n"); 8357 8358 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0, 8359 &bytesReturned, &overlapped); 8360 ok(bret == FALSE && WSAGetLastError() == WSAEFAULT, 8361 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError()); 8362 8363 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 8364 sizeof(struct sockaddr_in) + 15, &bytesReturned, &overlapped); 8365 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, 8366 "AcceptEx on too small remote address size returned %d + errno %d\n", bret, WSAGetLastError()); 8367 bret = CancelIo((HANDLE) listener); 8368 ok(bret, "Failed to cancel pending accept socket\n"); 8369 8370 bret = pAcceptEx(listener, acceptor, buffer, 0, 8371 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8372 &bytesReturned, NULL); 8373 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped " 8374 "returned %d + errno %d\n", bret, WSAGetLastError()); 8375 8376 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, 0, &bytesReturned, NULL); 8377 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped " 8378 "returned %d + errno %d\n", bret, WSAGetLastError()); 8379 8380 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 8381 if (overlapped.hEvent == NULL) { 8382 skip("could not create event object, errno = %d\n", GetLastError()); 8383 goto end; 8384 } 8385 8386 bret = pAcceptEx(listener, acceptor, buffer, 0, 8387 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8388 &bytesReturned, &overlapped); 8389 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8390 8391 bret = pAcceptEx(listener, acceptor, buffer, 0, 8392 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8393 &bytesReturned, &overlapped); 8394 todo_wine ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, 8395 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError()); 8396 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) { 8397 /* We need to cancel this call, otherwise things fail */ 8398 bret = CancelIo((HANDLE) listener); 8399 ok(bret, "Failed to cancel failed test. Bailing...\n"); 8400 if (!bret) return; 8401 WaitForSingleObject(overlapped.hEvent, 0); 8402 8403 bret = pAcceptEx(listener, acceptor, buffer, 0, 8404 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8405 &bytesReturned, &overlapped); 8406 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8407 } 8408 8409 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8410 todo_wine ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL, 8411 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError()); 8412 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) { 8413 /* We need to cancel this call, otherwise things fail */ 8414 closesocket(acceptor); 8415 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8416 if (acceptor == INVALID_SOCKET) { 8417 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8418 goto end; 8419 } 8420 8421 bret = CancelIo((HANDLE) listener); 8422 ok(bret, "Failed to cancel failed test. Bailing...\n"); 8423 if (!bret) return; 8424 8425 bret = pAcceptEx(listener, acceptor, buffer, 0, 8426 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8427 &bytesReturned, &overlapped); 8428 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8429 } 8430 8431 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8432 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError()); 8433 8434 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE); 8435 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8436 8437 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8438 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError()); 8439 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned); 8440 8441 closesocket(connector); 8442 connector = INVALID_SOCKET; 8443 closesocket(acceptor); 8444 8445 /* Test short reads */ 8446 8447 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8448 if (acceptor == INVALID_SOCKET) { 8449 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8450 goto end; 8451 } 8452 connector = socket(AF_INET, SOCK_STREAM, 0); 8453 if (connector == INVALID_SOCKET) { 8454 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8455 goto end; 8456 } 8457 bret = pAcceptEx(listener, acceptor, buffer, 2, 8458 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8459 &bytesReturned, &overlapped); 8460 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8461 8462 connect_time = 0xdeadbeef; 8463 optlen = sizeof(connect_time); 8464 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen); 8465 ok(!iret, "getsockopt failed %d\n", WSAGetLastError()); 8466 ok(connect_time == ~0u, "unexpected connect time %u\n", connect_time); 8467 8468 /* AcceptEx() still won't complete until we send data */ 8469 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8470 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError()); 8471 8472 connect_time = 0xdeadbeef; 8473 optlen = sizeof(connect_time); 8474 iret = getsockopt(connector, SOL_SOCKET, SO_CONNECT_TIME, (char *)&connect_time, &optlen); 8475 ok(!iret, "getsockopt failed %d\n", WSAGetLastError()); 8476 ok(connect_time < 0xdeadbeef, "unexpected connect time %u\n", connect_time); 8477 8478 dwret = WaitForSingleObject(overlapped.hEvent, 0); 8479 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError()); 8480 8481 iret = getsockname( connector, (struct sockaddr *)&peerAddress, &remoteSize); 8482 ok( !iret, "getsockname failed.\n"); 8483 8484 /* AcceptEx() could complete any time now */ 8485 iret = send(connector, buffer, 1, 0); 8486 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError()); 8487 8488 dwret = WaitForSingleObject(overlapped.hEvent, 1000); 8489 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8490 8491 /* Check if the buffer from AcceptEx is decoded correctly */ 8492 pGetAcceptExSockaddrs(buffer, 2, sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8493 (struct sockaddr **)&readBindAddress, &localSize, 8494 (struct sockaddr **)&readRemoteAddress, &remoteSize); 8495 strcpy( ipbuffer, inet_ntoa(readBindAddress->sin_addr)); 8496 ok( readBindAddress->sin_addr.s_addr == bindAddress.sin_addr.s_addr, 8497 "Local socket address is different %s != %s\n", 8498 ipbuffer, inet_ntoa(bindAddress.sin_addr)); 8499 ok( readBindAddress->sin_port == bindAddress.sin_port, 8500 "Local socket port is different: %d != %d\n", 8501 readBindAddress->sin_port, bindAddress.sin_port); 8502 strcpy( ipbuffer, inet_ntoa(readRemoteAddress->sin_addr)); 8503 ok( readRemoteAddress->sin_addr.s_addr == peerAddress.sin_addr.s_addr, 8504 "Remote socket address is different %s != %s\n", 8505 ipbuffer, inet_ntoa(peerAddress.sin_addr)); 8506 ok( readRemoteAddress->sin_port == peerAddress.sin_port, 8507 "Remote socket port is different: %d != %d\n", 8508 readRemoteAddress->sin_port, peerAddress.sin_port); 8509 8510 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8511 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError()); 8512 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned); 8513 8514 closesocket(connector); 8515 connector = INVALID_SOCKET; 8516 closesocket(acceptor); 8517 8518 /* Test CF_DEFER & AcceptEx interaction */ 8519 8520 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8521 if (acceptor == INVALID_SOCKET) { 8522 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8523 goto end; 8524 } 8525 connector = socket(AF_INET, SOCK_STREAM, 0); 8526 if (connector == INVALID_SOCKET) { 8527 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8528 goto end; 8529 } 8530 connector2 = socket(AF_INET, SOCK_STREAM, 0); 8531 if (connector == INVALID_SOCKET) { 8532 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8533 goto end; 8534 } 8535 8536 if (set_blocking(connector, FALSE)) { 8537 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 8538 goto end; 8539 } 8540 8541 if (set_blocking(connector2, FALSE)) { 8542 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 8543 goto end; 8544 } 8545 8546 /* Connect socket #1 */ 8547 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8548 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError()); 8549 8550 FD_ZERO ( &fds_accept ); 8551 FD_ZERO ( &fds_send ); 8552 8553 FD_SET ( listener, &fds_accept ); 8554 FD_SET ( connector, &fds_send ); 8555 8556 buffer[0] = '0'; 8557 got = 0; 8558 conn1 = 0; 8559 8560 for (i = 0; i < 4000; ++i) 8561 { 8562 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send; 8563 8564 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=, 8565 "acceptex test(%d): could not select on socket, errno %d\n" ); 8566 8567 /* check for incoming requests */ 8568 if ( FD_ISSET ( listener, &fds_openaccept ) ) { 8569 got++; 8570 if (got == 1) { 8571 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0); 8572 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError()); 8573 bret = pAcceptEx(listener, acceptor, buffer, 0, 8574 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8575 &bytesReturned, &overlapped); 8576 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8577 } 8578 else if (got == 2) { 8579 /* this should be socket #2 */ 8580 SOCKET tmp = accept(listener, NULL, NULL); 8581 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError()); 8582 closesocket(tmp); 8583 } 8584 else { 8585 ok(FALSE, "Got more than 2 connections?\n"); 8586 } 8587 } 8588 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) { 8589 /* Send data on second socket, and stop */ 8590 send(connector2, "2", 1, 0); 8591 FD_CLR ( connector2, &fds_send ); 8592 8593 break; 8594 } 8595 if ( FD_ISSET ( connector, &fds_opensend ) ) { 8596 /* Once #1 is connected, allow #2 to connect */ 8597 conn1 = 1; 8598 8599 send(connector, "1", 1, 0); 8600 FD_CLR ( connector, &fds_send ); 8601 8602 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8603 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError()); 8604 FD_SET ( connector2, &fds_send ); 8605 } 8606 } 8607 8608 ok (got == 2 || broken(got == 1) /* NT4 */, 8609 "Did not get both connections, got %d\n", got); 8610 8611 dwret = WaitForSingleObject(overlapped.hEvent, 0); 8612 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8613 8614 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8615 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError()); 8616 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned); 8617 8618 set_blocking(acceptor, TRUE); 8619 iret = recv( acceptor, buffer, 2, 0); 8620 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError()); 8621 8622 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]); 8623 8624 closesocket(connector); 8625 connector = INVALID_SOCKET; 8626 closesocket(acceptor); 8627 8628 /* clean up in case of failures */ 8629 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET) 8630 closesocket(acceptor); 8631 8632 /* Disconnect during receive? */ 8633 8634 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8635 if (acceptor == INVALID_SOCKET) { 8636 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8637 goto end; 8638 } 8639 connector = socket(AF_INET, SOCK_STREAM, 0); 8640 if (connector == INVALID_SOCKET) { 8641 skip("could not create connector socket, error %d\n", WSAGetLastError()); 8642 goto end; 8643 } 8644 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8645 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8646 &bytesReturned, &overlapped); 8647 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8648 8649 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8650 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError()); 8651 8652 closesocket(connector); 8653 connector = INVALID_SOCKET; 8654 8655 dwret = WaitForSingleObject(overlapped.hEvent, 1000); 8656 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8657 8658 bytesReturned = 123456; 8659 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8660 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError()); 8661 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned); 8662 8663 closesocket(acceptor); 8664 8665 /* Test closing with pending requests */ 8666 8667 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8668 if (acceptor == INVALID_SOCKET) { 8669 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8670 goto end; 8671 } 8672 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8673 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8674 &bytesReturned, &overlapped); 8675 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8676 8677 closesocket(acceptor); 8678 8679 dwret = WaitForSingleObject(overlapped.hEvent, 1000); 8680 todo_wine ok(dwret == WAIT_OBJECT_0, 8681 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8682 8683 if (dwret != WAIT_TIMEOUT) { 8684 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8685 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError()); 8686 } 8687 else { 8688 bret = CancelIo((HANDLE) listener); 8689 ok(bret, "Failed to cancel failed test. Bailing...\n"); 8690 if (!bret) return; 8691 WaitForSingleObject(overlapped.hEvent, 0); 8692 } 8693 8694 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8695 if (acceptor == INVALID_SOCKET) { 8696 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8697 goto end; 8698 } 8699 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8700 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8701 &bytesReturned, &overlapped); 8702 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8703 8704 CancelIo((HANDLE) acceptor); 8705 8706 dwret = WaitForSingleObject(overlapped.hEvent, 1000); 8707 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError()); 8708 8709 closesocket(acceptor); 8710 8711 acceptor = socket(AF_INET, SOCK_STREAM, 0); 8712 if (acceptor == INVALID_SOCKET) { 8713 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8714 goto end; 8715 } 8716 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16), 8717 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 8718 &bytesReturned, &overlapped); 8719 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError()); 8720 8721 closesocket(listener); 8722 listener = INVALID_SOCKET; 8723 8724 dwret = WaitForSingleObject(overlapped.hEvent, 1000); 8725 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError()); 8726 8727 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE); 8728 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError()); 8729 8730 end: 8731 if (overlapped.hEvent) 8732 WSACloseEvent(overlapped.hEvent); 8733 if (listener != INVALID_SOCKET) 8734 closesocket(listener); 8735 if (acceptor != INVALID_SOCKET) 8736 closesocket(acceptor); 8737 if (connector != INVALID_SOCKET) 8738 closesocket(connector); 8739 if (connector2 != INVALID_SOCKET) 8740 closesocket(connector2); 8741 } 8742 8743 static void test_DisconnectEx(void) 8744 { 8745 SOCKET listener, acceptor, connector; 8746 LPFN_DISCONNECTEX pDisconnectEx; 8747 GUID disconnectExGuid = WSAID_DISCONNECTEX; 8748 struct sockaddr_in address; 8749 DWORD num_bytes, flags; 8750 OVERLAPPED overlapped; 8751 int addrlen, iret; 8752 BOOL bret; 8753 8754 connector = socket(AF_INET, SOCK_STREAM, 0); 8755 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError()); 8756 8757 iret = WSAIoctl(connector, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectExGuid, sizeof(disconnectExGuid), 8758 &pDisconnectEx, sizeof(pDisconnectEx), &num_bytes, NULL, NULL); 8759 if (iret) 8760 { 8761 win_skip("WSAIoctl failed to get DisconnectEx, error %d\n", WSAGetLastError()); 8762 closesocket(connector); 8763 return; 8764 } 8765 8766 listener = socket(AF_INET, SOCK_STREAM, 0); 8767 ok(listener != INVALID_SOCKET, "failed to create listener socket, error %d\n", WSAGetLastError()); 8768 8769 memset(&address, 0, sizeof(address)); 8770 address.sin_family = AF_INET; 8771 address.sin_addr.s_addr = inet_addr("127.0.0.1"); 8772 iret = bind(listener, (struct sockaddr *)&address, sizeof(address)); 8773 ok(iret == 0, "failed to bind, error %d\n", WSAGetLastError()); 8774 8775 addrlen = sizeof(address); 8776 iret = getsockname(listener, (struct sockaddr *)&address, &addrlen); 8777 ok(iret == 0, "failed to lookup bind address, error %d\n", WSAGetLastError()); 8778 8779 iret = listen(listener, 1); 8780 ok(iret == 0, "failed to listen, error %d\n", WSAGetLastError()); 8781 8782 set_blocking(listener, TRUE); 8783 8784 memset(&overlapped, 0, sizeof(overlapped)); 8785 bret = pDisconnectEx(INVALID_SOCKET, &overlapped, 0, 0); 8786 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n"); 8787 ok(WSAGetLastError() == WSAENOTSOCK, "expected WSAENOTSOCK, got %d\n", WSAGetLastError()); 8788 8789 memset(&overlapped, 0, sizeof(overlapped)); 8790 bret = pDisconnectEx(connector, &overlapped, 0, 0); 8791 ok(bret == FALSE, "DisconnectEx unexpectedly succeeded\n"); 8792 todo_wine ok(WSAGetLastError() == WSAENOTCONN, "expected WSAENOTCONN, got %d\n", WSAGetLastError()); 8793 8794 iret = connect(connector, (struct sockaddr *)&address, addrlen); 8795 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError()); 8796 8797 acceptor = accept(listener, NULL, NULL); 8798 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError()); 8799 8800 memset(&overlapped, 0, sizeof(overlapped)); 8801 overlapped.hEvent = WSACreateEvent(); 8802 ok(overlapped.hEvent != WSA_INVALID_EVENT, "WSACreateEvent failed, error %d\n", WSAGetLastError()); 8803 bret = pDisconnectEx(connector, &overlapped, 0, 0); 8804 if (bret) 8805 ok(overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", overlapped.Internal); 8806 else if (WSAGetLastError() == ERROR_IO_PENDING) 8807 bret = WSAGetOverlappedResult(connector, &overlapped, &num_bytes, TRUE, &flags); 8808 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError()); 8809 WSACloseEvent(overlapped.hEvent); 8810 8811 iret = connect(connector, (struct sockaddr *)&address, sizeof(address)); 8812 ok(iret != 0, "connect unexpectedly succeeded\n"); 8813 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError()); 8814 8815 closesocket(acceptor); 8816 closesocket(connector); 8817 8818 connector = socket(AF_INET, SOCK_STREAM, 0); 8819 ok(connector != INVALID_SOCKET, "failed to create connector socket, error %d\n", WSAGetLastError()); 8820 8821 iret = connect(connector, (struct sockaddr *)&address, addrlen); 8822 ok(iret == 0, "failed to connect, error %d\n", WSAGetLastError()); 8823 8824 acceptor = accept(listener, NULL, NULL); 8825 ok(acceptor != INVALID_SOCKET, "could not accept socket, error %d\n", WSAGetLastError()); 8826 8827 bret = pDisconnectEx(connector, NULL, 0, 0); 8828 ok(bret, "DisconnectEx failed, error %d\n", WSAGetLastError()); 8829 8830 iret = connect(connector, (struct sockaddr *)&address, sizeof(address)); 8831 ok(iret != 0, "connect unexpectedly succeeded\n"); 8832 ok(WSAGetLastError() == WSAEISCONN, "expected WSAEISCONN, got %d\n", WSAGetLastError()); 8833 8834 closesocket(acceptor); 8835 closesocket(connector); 8836 closesocket(listener); 8837 } 8838 8839 #define compare_file(h,s,o) compare_file2(h,s,o,__FILE__,__LINE__) 8840 8841 static void compare_file2(HANDLE handle, SOCKET sock, int offset, const char *file, int line) 8842 { 8843 char buf1[256], buf2[256]; 8844 BOOL success; 8845 int i = 0; 8846 8847 SetFilePointer(handle, offset, NULL, FILE_BEGIN); 8848 while (1) 8849 { 8850 DWORD n1 = 0, n2 = 0; 8851 8852 success = ReadFile(handle, buf1, sizeof(buf1), &n1, NULL); 8853 ok_(file,line)(success, "Failed to read from file.\n"); 8854 if (success && n1 == 0) 8855 break; 8856 else if(!success) 8857 return; 8858 n2 = recv(sock, buf2, n1, 0); 8859 ok_(file,line)(n1 == n2, "Block %d size mismatch (%d != %d)\n", i, n1, n2); 8860 ok_(file,line)(memcmp(buf1, buf2, n2) == 0, "Block %d failed\n", i); 8861 i++; 8862 } 8863 } 8864 8865 static void test_TransmitFile(void) 8866 { 8867 DWORD num_bytes, err, file_size, total_sent; 8868 GUID transmitFileGuid = WSAID_TRANSMITFILE; 8869 LPFN_TRANSMITFILE pTransmitFile = NULL; 8870 HANDLE file = INVALID_HANDLE_VALUE; 8871 char header_msg[] = "hello world"; 8872 char footer_msg[] = "goodbye!!!"; 8873 char system_ini_path[MAX_PATH]; 8874 struct sockaddr_in bindAddress; 8875 TRANSMIT_FILE_BUFFERS buffers; 8876 SOCKET client, server, dest; 8877 WSAOVERLAPPED ov; 8878 char buf[256]; 8879 int iret, len; 8880 BOOL bret; 8881 8882 memset( &ov, 0, sizeof(ov) ); 8883 8884 /* Setup sockets for testing TransmitFile */ 8885 client = socket(AF_INET, SOCK_STREAM, 0); 8886 server = socket(AF_INET, SOCK_STREAM, 0); 8887 if (client == INVALID_SOCKET || server == INVALID_SOCKET) 8888 { 8889 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 8890 goto cleanup; 8891 } 8892 iret = WSAIoctl(client, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmitFileGuid, sizeof(transmitFileGuid), 8893 &pTransmitFile, sizeof(pTransmitFile), &num_bytes, NULL, NULL); 8894 if (iret) 8895 { 8896 skip("WSAIoctl failed to get TransmitFile with ret %d + errno %d\n", iret, WSAGetLastError()); 8897 goto cleanup; 8898 } 8899 GetSystemWindowsDirectoryA(system_ini_path, MAX_PATH ); 8900 strcat(system_ini_path, "\\system.ini"); 8901 file = CreateFileA(system_ini_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0x0, NULL); 8902 if (file == INVALID_HANDLE_VALUE) 8903 { 8904 skip("Unable to open a file to transmit.\n"); 8905 goto cleanup; 8906 } 8907 file_size = GetFileSize(file, NULL); 8908 8909 /* Test TransmitFile with an invalid socket */ 8910 bret = pTransmitFile(INVALID_SOCKET, file, 0, 0, NULL, NULL, 0); 8911 err = WSAGetLastError(); 8912 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 8913 ok(err == WSAENOTSOCK, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTSOCK); 8914 8915 /* Test a bogus TransmitFile without a connected socket */ 8916 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, TF_REUSE_SOCKET); 8917 err = WSAGetLastError(); 8918 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 8919 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN); 8920 8921 /* Setup a properly connected socket for transfers */ 8922 memset(&bindAddress, 0, sizeof(bindAddress)); 8923 bindAddress.sin_family = AF_INET; 8924 bindAddress.sin_port = htons(SERVERPORT+1); 8925 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 8926 iret = bind(server, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8927 if (iret != 0) 8928 { 8929 skip("failed to bind(), error %d\n", WSAGetLastError()); 8930 goto cleanup; 8931 } 8932 iret = listen(server, 1); 8933 if (iret != 0) 8934 { 8935 skip("failed to listen(), error %d\n", WSAGetLastError()); 8936 goto cleanup; 8937 } 8938 iret = connect(client, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 8939 if (iret != 0) 8940 { 8941 skip("failed to connect(), error %d\n", WSAGetLastError()); 8942 goto cleanup; 8943 } 8944 len = sizeof(bindAddress); 8945 dest = accept(server, (struct sockaddr*)&bindAddress, &len); 8946 if (dest == INVALID_SOCKET) 8947 { 8948 skip("failed to accept(), error %d\n", WSAGetLastError()); 8949 goto cleanup; 8950 } 8951 if (set_blocking(dest, FALSE)) 8952 { 8953 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 8954 goto cleanup; 8955 } 8956 8957 /* Test TransmitFile with no possible buffer */ 8958 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0); 8959 ok(bret, "TransmitFile failed unexpectedly.\n"); 8960 iret = recv(dest, buf, sizeof(buf), 0); 8961 ok(iret == -1, "Returned an unexpected buffer from TransmitFile (%d != -1).\n", iret); 8962 8963 /* Test TransmitFile with only buffer data */ 8964 buffers.Head = &header_msg[0]; 8965 buffers.HeadLength = sizeof(header_msg); 8966 buffers.Tail = &footer_msg[0]; 8967 buffers.TailLength = sizeof(footer_msg); 8968 bret = pTransmitFile(client, NULL, 0, 0, NULL, &buffers, 0); 8969 ok(bret, "TransmitFile failed unexpectedly.\n"); 8970 iret = recv(dest, buf, sizeof(buf), 0); 8971 ok(iret == sizeof(header_msg)+sizeof(footer_msg), 8972 "Returned an unexpected buffer from TransmitFile: %d\n", iret ); 8973 ok(memcmp(&buf[0], &header_msg[0], sizeof(header_msg)) == 0, 8974 "TransmitFile header buffer did not match!\n"); 8975 ok(memcmp(&buf[sizeof(header_msg)], &footer_msg[0], sizeof(footer_msg)) == 0, 8976 "TransmitFile footer buffer did not match!\n"); 8977 8978 /* Test TransmitFile with only file data */ 8979 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, 0); 8980 ok(bret, "TransmitFile failed unexpectedly.\n"); 8981 compare_file(file, dest, 0); 8982 8983 /* Test TransmitFile with both file and buffer data */ 8984 buffers.Head = &header_msg[0]; 8985 buffers.HeadLength = sizeof(header_msg); 8986 buffers.Tail = &footer_msg[0]; 8987 buffers.TailLength = sizeof(footer_msg); 8988 SetFilePointer(file, 0, NULL, FILE_BEGIN); 8989 bret = pTransmitFile(client, file, 0, 0, NULL, &buffers, 0); 8990 ok(bret, "TransmitFile failed unexpectedly.\n"); 8991 iret = recv(dest, buf, sizeof(header_msg), 0); 8992 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0, 8993 "TransmitFile header buffer did not match!\n"); 8994 compare_file(file, dest, 0); 8995 iret = recv(dest, buf, sizeof(footer_msg), 0); 8996 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0, 8997 "TransmitFile footer buffer did not match!\n"); 8998 8999 /* Test overlapped TransmitFile */ 9000 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL); 9001 if (ov.hEvent == INVALID_HANDLE_VALUE) 9002 { 9003 skip("Could not create event object, some tests will be skipped. errno = %d\n", 9004 GetLastError()); 9005 goto cleanup; 9006 } 9007 SetFilePointer(file, 0, NULL, FILE_BEGIN); 9008 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0); 9009 err = WSAGetLastError(); 9010 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 9011 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", 9012 err, ERROR_IO_PENDING); 9013 iret = WaitForSingleObject(ov.hEvent, 2000); 9014 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n"); 9015 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL); 9016 ok(total_sent == file_size, 9017 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n", 9018 total_sent, file_size); 9019 compare_file(file, dest, 0); 9020 9021 /* Test overlapped TransmitFile w/ start offset */ 9022 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL); 9023 if (ov.hEvent == INVALID_HANDLE_VALUE) 9024 { 9025 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError()); 9026 goto cleanup; 9027 } 9028 SetFilePointer(file, 0, NULL, FILE_BEGIN); 9029 ov.Offset = 10; 9030 bret = pTransmitFile(client, file, 0, 0, &ov, NULL, 0); 9031 err = WSAGetLastError(); 9032 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 9033 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING); 9034 iret = WaitForSingleObject(ov.hEvent, 2000); 9035 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n"); 9036 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL); 9037 ok(total_sent == (file_size - ov.Offset), 9038 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n", 9039 total_sent, file_size - ov.Offset); 9040 compare_file(file, dest, ov.Offset); 9041 9042 /* Test overlapped TransmitFile w/ file and buffer data */ 9043 ov.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL); 9044 if (ov.hEvent == INVALID_HANDLE_VALUE) 9045 { 9046 skip("Could not create event object, some tests will be skipped. errno = %d\n", GetLastError()); 9047 goto cleanup; 9048 } 9049 buffers.Head = &header_msg[0]; 9050 buffers.HeadLength = sizeof(header_msg); 9051 buffers.Tail = &footer_msg[0]; 9052 buffers.TailLength = sizeof(footer_msg); 9053 SetFilePointer(file, 0, NULL, FILE_BEGIN); 9054 ov.Offset = 0; 9055 bret = pTransmitFile(client, file, 0, 0, &ov, &buffers, 0); 9056 err = WSAGetLastError(); 9057 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 9058 ok(err == ERROR_IO_PENDING, "TransmitFile triggered unexpected errno (%d != %d)\n", err, ERROR_IO_PENDING); 9059 iret = WaitForSingleObject(ov.hEvent, 2000); 9060 ok(iret == WAIT_OBJECT_0, "Overlapped TransmitFile failed.\n"); 9061 WSAGetOverlappedResult(client, &ov, &total_sent, FALSE, NULL); 9062 ok(total_sent == (file_size + buffers.HeadLength + buffers.TailLength), 9063 "Overlapped TransmitFile sent an unexpected number of bytes (%d != %d).\n", 9064 total_sent, file_size + buffers.HeadLength + buffers.TailLength); 9065 iret = recv(dest, buf, sizeof(header_msg), 0); 9066 ok(memcmp(buf, &header_msg[0], sizeof(header_msg)) == 0, 9067 "TransmitFile header buffer did not match!\n"); 9068 compare_file(file, dest, 0); 9069 iret = recv(dest, buf, sizeof(footer_msg), 0); 9070 ok(memcmp(buf, &footer_msg[0], sizeof(footer_msg)) == 0, 9071 "TransmitFile footer buffer did not match!\n"); 9072 9073 /* Test TransmitFile w/ TF_DISCONNECT */ 9074 SetFilePointer(file, 0, NULL, FILE_BEGIN); 9075 bret = pTransmitFile(client, file, 0, 0, NULL, NULL, TF_DISCONNECT); 9076 ok(bret, "TransmitFile failed unexpectedly.\n"); 9077 compare_file(file, dest, 0); 9078 closesocket(client); 9079 ok(send(client, "test", 4, 0) == -1, "send() after TF_DISCONNECT succeeded unexpectedly.\n"); 9080 err = WSAGetLastError(); 9081 todo_wine ok(err == WSAENOTSOCK, "send() after TF_DISCONNECT triggered unexpected errno (%d != %d)\n", 9082 err, WSAENOTSOCK); 9083 9084 /* Test TransmitFile with a UDP datagram socket */ 9085 client = socket(AF_INET, SOCK_DGRAM, 0); 9086 bret = pTransmitFile(client, NULL, 0, 0, NULL, NULL, 0); 9087 err = WSAGetLastError(); 9088 ok(!bret, "TransmitFile succeeded unexpectedly.\n"); 9089 ok(err == WSAENOTCONN, "TransmitFile triggered unexpected errno (%d != %d)\n", err, WSAENOTCONN); 9090 9091 cleanup: 9092 CloseHandle(file); 9093 CloseHandle(ov.hEvent); 9094 closesocket(client); 9095 closesocket(server); 9096 } 9097 9098 static void test_getpeername(void) 9099 { 9100 SOCKET sock; 9101 struct sockaddr_in sa, sa_out; 9102 SOCKADDR_STORAGE ss; 9103 int sa_len; 9104 const char buf[] = "hello world"; 9105 int ret; 9106 9107 /* Test the parameter validation order. */ 9108 ret = getpeername(INVALID_SOCKET, NULL, NULL); 9109 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret); 9110 ok(WSAGetLastError() == WSAENOTSOCK, 9111 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError()); 9112 9113 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 9114 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n"); 9115 if (sock == INVALID_SOCKET) 9116 { 9117 skip("Socket creation failed with %d\n", WSAGetLastError()); 9118 return; 9119 } 9120 9121 ret = getpeername(sock, NULL, NULL); 9122 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret); 9123 ok(WSAGetLastError() == WSAENOTCONN, 9124 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError()); 9125 9126 memset(&sa, 0, sizeof(sa)); 9127 sa.sin_family = AF_INET; 9128 sa.sin_port = htons(139); 9129 sa.sin_addr.s_addr = inet_addr("127.0.0.1"); 9130 9131 /* sendto does not change a socket's connection state. */ 9132 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa)); 9133 ok(ret != SOCKET_ERROR, 9134 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError()); 9135 9136 ret = getpeername(sock, NULL, NULL); 9137 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret); 9138 ok(WSAGetLastError() == WSAENOTCONN, 9139 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError()); 9140 9141 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa)); 9142 ok(ret == 0, 9143 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError()); 9144 9145 ret = getpeername(sock, NULL, NULL); 9146 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret); 9147 ok(WSAGetLastError() == WSAEFAULT, 9148 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError()); 9149 9150 /* Test crashes on Wine. */ 9151 if (0) 9152 { 9153 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe); 9154 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret); 9155 ok(WSAGetLastError() == WSAEFAULT, 9156 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError()); 9157 } 9158 9159 ret = getpeername(sock, (struct sockaddr*)&sa_out, NULL); 9160 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret); 9161 ok(WSAGetLastError() == WSAEFAULT, 9162 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError()); 9163 9164 sa_len = 0; 9165 ret = getpeername(sock, NULL, &sa_len); 9166 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret); 9167 ok(WSAGetLastError() == WSAEFAULT, 9168 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError()); 9169 ok(!sa_len, "got %d\n", sa_len); 9170 9171 sa_len = 0; 9172 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len); 9173 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret); 9174 ok(WSAGetLastError() == WSAEFAULT, 9175 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError()); 9176 ok(!sa_len, "got %d\n", sa_len); 9177 9178 sa_len = sizeof(ss); 9179 ret = getpeername(sock, (struct sockaddr *)&ss, &sa_len); 9180 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret); 9181 ok(!memcmp(&sa, &ss, sizeof(sa)), 9182 "Expected the returned structure to be identical to the connect structure\n"); 9183 ok(sa_len == sizeof(sa), "got %d\n", sa_len); 9184 9185 closesocket(sock); 9186 } 9187 9188 static void test_sioRoutingInterfaceQuery(void) 9189 { 9190 int ret; 9191 SOCKET sock; 9192 SOCKADDR_IN sin = { 0 }, sout = { 0 }; 9193 DWORD bytesReturned; 9194 9195 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 9196 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n"); 9197 if (sock == INVALID_SOCKET) 9198 { 9199 skip("Socket creation failed with %d\n", WSAGetLastError()); 9200 return; 9201 } 9202 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, NULL, 0, NULL, 0, NULL, 9203 NULL, NULL); 9204 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 9205 "expected WSAEFAULT, got %d\n", WSAGetLastError()); 9206 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin), 9207 NULL, 0, NULL, NULL, NULL); 9208 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 9209 "expected WSAEFAULT, got %d\n", WSAGetLastError()); 9210 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin), 9211 NULL, 0, &bytesReturned, NULL, NULL); 9212 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEAFNOSUPPORT, 9213 "expected WSAEAFNOSUPPORT, got %d\n", WSAGetLastError()); 9214 sin.sin_family = AF_INET; 9215 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin), 9216 NULL, 0, &bytesReturned, NULL, NULL); 9217 todo_wine ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL, 9218 "expected WSAEINVAL, got %d\n", WSAGetLastError()); 9219 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 9220 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin), 9221 NULL, 0, &bytesReturned, NULL, NULL); 9222 ok(ret == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT, 9223 "expected WSAEFAULT, got %d\n", WSAGetLastError()); 9224 ret = WSAIoctl(sock, SIO_ROUTING_INTERFACE_QUERY, &sin, sizeof(sin), 9225 &sout, sizeof(sout), &bytesReturned, NULL, NULL); 9226 ok(!ret, "WSAIoctl failed: %d\n", WSAGetLastError()); 9227 ok(sout.sin_family == AF_INET, "expected AF_INET, got %d\n", sout.sin_family); 9228 /* We expect the source address to be INADDR_LOOPBACK as well, but 9229 * there's no guarantee that a route to the loopback address exists, 9230 * so rather than introduce spurious test failures we do not test the 9231 * source address. 9232 */ 9233 closesocket(sock); 9234 } 9235 9236 static void test_sioAddressListChange(void) 9237 { 9238 struct sockaddr_in bindAddress; 9239 struct in_addr net_address; 9240 WSAOVERLAPPED overlapped, *olp; 9241 struct hostent *h; 9242 DWORD num_bytes, error, tick; 9243 SOCKET sock, sock2, sock3; 9244 WSAEVENT event2, event3; 9245 HANDLE io_port; 9246 ULONG_PTR key; 9247 int acount; 9248 BOOL bret; 9249 int ret; 9250 9251 /* Use gethostbyname to find the list of local network interfaces */ 9252 h = gethostbyname(""); 9253 if (!h) 9254 { 9255 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, gethostbyname failed with %u\n", 9256 WSAGetLastError()); 9257 return; 9258 } 9259 for (acount = 0; h->h_addr_list[acount]; acount++); 9260 if (acount == 0) 9261 { 9262 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, test requires a network card.\n"); 9263 return; 9264 } 9265 9266 net_address.s_addr = *(ULONG *) h->h_addr_list[0]; 9267 9268 sock = socket(AF_INET, 0, IPPROTO_TCP); 9269 ok(sock != INVALID_SOCKET, "socket() failed\n"); 9270 9271 memset(&bindAddress, 0, sizeof(bindAddress)); 9272 bindAddress.sin_family = AF_INET; 9273 bindAddress.sin_addr.s_addr = net_address.s_addr; 9274 SetLastError(0xdeadbeef); 9275 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9276 ok (!ret, "bind() failed with error %d\n", GetLastError()); 9277 set_blocking(sock, FALSE); 9278 9279 memset(&overlapped, 0, sizeof(overlapped)); 9280 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 9281 SetLastError(0xdeadbeef); 9282 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9283 error = GetLastError(); 9284 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error); 9285 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error); 9286 9287 CloseHandle(overlapped.hEvent); 9288 closesocket(sock); 9289 9290 sock = socket(AF_INET, 0, IPPROTO_TCP); 9291 ok(sock != INVALID_SOCKET, "socket() failed\n"); 9292 9293 SetLastError(0xdeadbeef); 9294 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9295 ok (!ret, "bind() failed with error %d\n", GetLastError()); 9296 set_blocking(sock, TRUE); 9297 9298 memset(&overlapped, 0, sizeof(overlapped)); 9299 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 9300 SetLastError(0xdeadbeef); 9301 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9302 error = GetLastError(); 9303 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error); 9304 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error); 9305 9306 CloseHandle(overlapped.hEvent); 9307 closesocket(sock); 9308 9309 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9310 ok(sock != INVALID_SOCKET, "socket() failed\n"); 9311 9312 SetLastError(0xdeadbeef); 9313 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9314 ok (!ret, "bind() failed with error %d\n", GetLastError()); 9315 set_blocking(sock, FALSE); 9316 9317 memset(&overlapped, 0, sizeof(overlapped)); 9318 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 9319 SetLastError(0xdeadbeef); 9320 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9321 error = GetLastError(); 9322 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error); 9323 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error); 9324 9325 CloseHandle(overlapped.hEvent); 9326 closesocket(sock); 9327 9328 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9329 ok(sock != INVALID_SOCKET, "socket() failed\n"); 9330 9331 SetLastError(0xdeadbeef); 9332 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9333 ok (!ret, "bind() failed with error %d\n", GetLastError()); 9334 set_blocking(sock, TRUE); 9335 9336 memset(&overlapped, 0, sizeof(overlapped)); 9337 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 9338 SetLastError(0xdeadbeef); 9339 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9340 error = GetLastError(); 9341 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error); 9342 ok (error == ERROR_IO_PENDING, "expected 0x3e5, got 0x%x\n", error); 9343 9344 CloseHandle(overlapped.hEvent); 9345 closesocket(sock); 9346 9347 /* When the socket is overlapped non-blocking and the list change is requested without 9348 * an overlapped structure the error will be different. */ 9349 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9350 ok(sock != INVALID_SOCKET, "socket() failed\n"); 9351 9352 SetLastError(0xdeadbeef); 9353 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9354 ok (!ret, "bind() failed with error %d\n", GetLastError()); 9355 set_blocking(sock, FALSE); 9356 9357 SetLastError(0xdeadbeef); 9358 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL); 9359 error = GetLastError(); 9360 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %d\n", error); 9361 ok (error == WSAEWOULDBLOCK, "expected 10035, got %d\n", error); 9362 9363 io_port = CreateIoCompletionPort( (HANDLE)sock, NULL, 0, 0 ); 9364 ok (io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9365 9366 set_blocking(sock, FALSE); 9367 memset(&overlapped, 0, sizeof(overlapped)); 9368 SetLastError(0xdeadbeef); 9369 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9370 error = GetLastError(); 9371 ok (ret == SOCKET_ERROR, "WSAIoctl(SIO_ADDRESS_LIST_CHANGE) failed with error %u\n", error); 9372 ok (error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error); 9373 9374 olp = (WSAOVERLAPPED *)0xdeadbeef; 9375 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 ); 9376 ok(!bret, "failed to get completion status %u\n", bret); 9377 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 9378 ok(!olp, "Overlapped structure is at %p\n", olp); 9379 9380 closesocket(sock); 9381 9382 olp = (WSAOVERLAPPED *)0xdeadbeef; 9383 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 0 ); 9384 ok(!bret, "failed to get completion status %u\n", bret); 9385 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %u\n", GetLastError()); 9386 ok(olp == &overlapped, "Overlapped structure is at %p\n", olp); 9387 9388 CloseHandle(io_port); 9389 9390 /* Misuse of the API by using a blocking socket and not using an overlapped structure, 9391 * this leads to a hang forever. */ 9392 if (0) 9393 { 9394 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9395 9396 SetLastError(0xdeadbeef); 9397 bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9398 9399 set_blocking(sock, TRUE); 9400 WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL); 9401 /* hang */ 9402 9403 closesocket(sock); 9404 } 9405 9406 if (!winetest_interactive) 9407 { 9408 skip("Cannot test SIO_ADDRESS_LIST_CHANGE, interactive tests must be enabled\n"); 9409 return; 9410 } 9411 9412 /* Bind an overlapped socket to the first found network interface */ 9413 sock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9414 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n"); 9415 sock2 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9416 ok(sock2 != INVALID_SOCKET, "Expected socket to return a valid socket\n"); 9417 sock3 = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); 9418 ok(sock3 != INVALID_SOCKET, "Expected socket to return a valid socket\n"); 9419 9420 ret = bind(sock, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9421 ok(!ret, "bind failed unexpectedly\n"); 9422 ret = bind(sock2, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9423 ok(!ret, "bind failed unexpectedly\n"); 9424 ret = bind(sock3, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 9425 ok(!ret, "bind failed unexpectedly\n"); 9426 9427 set_blocking(sock2, FALSE); 9428 set_blocking(sock3, FALSE); 9429 9430 /* Wait for address changes, request that the user connects/disconnects an interface */ 9431 memset(&overlapped, 0, sizeof(overlapped)); 9432 overlapped.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL); 9433 ret = WSAIoctl(sock, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, &overlapped, NULL); 9434 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n"); 9435 ok(WSAGetLastError() == WSA_IO_PENDING, "Expected pending last error, got %d\n", WSAGetLastError()); 9436 9437 ret = WSAIoctl(sock2, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &num_bytes, NULL, NULL); 9438 ok(ret == SOCKET_ERROR, "WSAIoctl succeeded unexpectedly\n"); 9439 ok(WSAGetLastError() == WSAEWOULDBLOCK, "Expected would block last error, got %d\n", WSAGetLastError()); 9440 9441 event2 = WSACreateEvent(); 9442 event3 = WSACreateEvent(); 9443 ret = WSAEventSelect (sock2, event2, FD_ADDRESS_LIST_CHANGE); 9444 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError()); 9445 /* sock3 did not request SIO_ADDRESS_LIST_CHANGE but it is trying to wait anyway */ 9446 ret = WSAEventSelect (sock3, event3, FD_ADDRESS_LIST_CHANGE); 9447 ok(!ret, "WSAEventSelect failed with %d\n", WSAGetLastError()); 9448 9449 trace("Testing socket-based ipv4 address list change notification. Please connect/disconnect or" 9450 " change the ipv4 address of any of the local network interfaces (15 second timeout).\n"); 9451 tick = GetTickCount(); 9452 ret = WaitForSingleObject(overlapped.hEvent, 15000); 9453 ok(ret == WAIT_OBJECT_0, "failed to get overlapped event %u\n", ret); 9454 9455 ret = WaitForSingleObject(event2, 500); 9456 todo_wine 9457 ok(ret == WAIT_OBJECT_0, "failed to get change event %u\n", ret); 9458 9459 ret = WaitForSingleObject(event3, 500); 9460 ok(ret == WAIT_TIMEOUT, "unexpected change event\n"); 9461 9462 trace("Spent %d ms waiting.\n", GetTickCount() - tick); 9463 9464 WSACloseEvent(event2); 9465 WSACloseEvent(event3); 9466 9467 closesocket(sock); 9468 closesocket(sock2); 9469 closesocket(sock3); 9470 } 9471 9472 static void test_synchronous_WSAIoctl(void) 9473 { 9474 HANDLE previous_port, io_port; 9475 WSAOVERLAPPED overlapped, *olp; 9476 SOCKET socket; 9477 ULONG on; 9478 ULONG_PTR key; 9479 DWORD num_bytes; 9480 BOOL ret; 9481 int res; 9482 9483 previous_port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 ); 9484 ok( previous_port != NULL, "failed to create completion port %u\n", GetLastError() ); 9485 9486 socket = WSASocketW( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED ); 9487 ok( socket != INVALID_SOCKET, "failed to create socket %d\n", WSAGetLastError() ); 9488 9489 io_port = CreateIoCompletionPort( (HANDLE)socket, previous_port, 0, 0 ); 9490 ok( io_port != NULL, "failed to create completion port %u\n", GetLastError() ); 9491 9492 on = 1; 9493 memset( &overlapped, 0, sizeof(overlapped) ); 9494 res = WSAIoctl( socket, FIONBIO, &on, sizeof(on), NULL, 0, &num_bytes, &overlapped, NULL ); 9495 ok( !res, "WSAIoctl failed %d\n", WSAGetLastError() ); 9496 9497 ret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 10000 ); 9498 ok( ret, "failed to get completion status %u\n", GetLastError() ); 9499 9500 CloseHandle( io_port ); 9501 closesocket( socket ); 9502 CloseHandle( previous_port ); 9503 } 9504 9505 #define WM_ASYNCCOMPLETE (WM_USER + 100) 9506 static HWND create_async_message_window(void) 9507 { 9508 static const char class_name[] = "ws2_32 async message window class"; 9509 9510 WNDCLASSEXA wndclass; 9511 HWND hWnd; 9512 9513 wndclass.cbSize = sizeof(wndclass); 9514 wndclass.style = CS_HREDRAW | CS_VREDRAW; 9515 wndclass.lpfnWndProc = DefWindowProcA; 9516 wndclass.cbClsExtra = 0; 9517 wndclass.cbWndExtra = 0; 9518 wndclass.hInstance = GetModuleHandleA(NULL); 9519 wndclass.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION); 9520 wndclass.hIconSm = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION); 9521 wndclass.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW); 9522 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 9523 wndclass.lpszClassName = class_name; 9524 wndclass.lpszMenuName = NULL; 9525 9526 RegisterClassExA(&wndclass); 9527 9528 hWnd = CreateWindowA(class_name, "ws2_32 async message window", WS_OVERLAPPEDWINDOW, 9529 0, 0, 500, 500, NULL, NULL, GetModuleHandleA(NULL), NULL); 9530 if (!hWnd) 9531 { 9532 ok(0, "failed to create window: %u\n", GetLastError()); 9533 return NULL; 9534 } 9535 9536 return hWnd; 9537 } 9538 9539 static void wait_for_async_message(HWND hwnd, HANDLE handle) 9540 { 9541 BOOL ret; 9542 MSG msg; 9543 9544 while ((ret = GetMessageA(&msg, 0, 0, 0)) && 9545 !(msg.hwnd == hwnd && msg.message == WM_ASYNCCOMPLETE)) 9546 { 9547 TranslateMessage(&msg); 9548 DispatchMessageA(&msg); 9549 } 9550 9551 ok(ret, "did not expect WM_QUIT message\n"); 9552 ok(msg.wParam == (WPARAM)handle, "expected wParam = %p, got %lx\n", handle, msg.wParam); 9553 } 9554 9555 static void test_WSAAsyncGetServByPort(void) 9556 { 9557 HWND hwnd = create_async_message_window(); 9558 HANDLE ret; 9559 char buffer[MAXGETHOSTSTRUCT]; 9560 9561 if (!hwnd) 9562 return; 9563 9564 /* FIXME: The asynchronous window messages should be tested. */ 9565 9566 /* Parameters are not checked when initiating the asynchronous operation. */ 9567 ret = WSAAsyncGetServByPort(NULL, 0, 0, NULL, NULL, 0); 9568 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n"); 9569 9570 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, 0, NULL, NULL, 0); 9571 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n"); 9572 wait_for_async_message(hwnd, ret); 9573 9574 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, NULL, 0); 9575 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n"); 9576 wait_for_async_message(hwnd, ret); 9577 9578 ret = WSAAsyncGetServByPort(hwnd, WM_ASYNCCOMPLETE, htons(80), NULL, buffer, MAXGETHOSTSTRUCT); 9579 ok(ret != NULL, "WSAAsyncGetServByPort returned NULL\n"); 9580 wait_for_async_message(hwnd, ret); 9581 9582 DestroyWindow(hwnd); 9583 } 9584 9585 static void test_WSAAsyncGetServByName(void) 9586 { 9587 HWND hwnd = create_async_message_window(); 9588 HANDLE ret; 9589 char buffer[MAXGETHOSTSTRUCT]; 9590 9591 if (!hwnd) 9592 return; 9593 9594 /* FIXME: The asynchronous window messages should be tested. */ 9595 9596 /* Parameters are not checked when initiating the asynchronous operation. */ 9597 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", NULL, NULL, 0); 9598 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n"); 9599 wait_for_async_message(hwnd, ret); 9600 9601 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "", "", buffer, MAXGETHOSTSTRUCT); 9602 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n"); 9603 wait_for_async_message(hwnd, ret); 9604 9605 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", NULL, NULL, 0); 9606 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n"); 9607 wait_for_async_message(hwnd, ret); 9608 9609 ret = WSAAsyncGetServByName(hwnd, WM_ASYNCCOMPLETE, "http", "tcp", buffer, MAXGETHOSTSTRUCT); 9610 ok(ret != NULL, "WSAAsyncGetServByName returned NULL\n"); 9611 wait_for_async_message(hwnd, ret); 9612 9613 DestroyWindow(hwnd); 9614 } 9615 9616 /* 9617 * Provide consistent initialization for the AcceptEx IOCP tests. 9618 */ 9619 static SOCKET setup_iocp_src(struct sockaddr_in *bindAddress) 9620 { 9621 SOCKET src, ret = INVALID_SOCKET; 9622 int iret, socklen; 9623 9624 src = socket(AF_INET, SOCK_STREAM, 0); 9625 if (src == INVALID_SOCKET) 9626 { 9627 skip("could not create listener socket, error %d\n", WSAGetLastError()); 9628 goto end; 9629 } 9630 9631 memset(bindAddress, 0, sizeof(*bindAddress)); 9632 bindAddress->sin_family = AF_INET; 9633 bindAddress->sin_addr.s_addr = inet_addr("127.0.0.1"); 9634 iret = bind(src, (struct sockaddr*)bindAddress, sizeof(*bindAddress)); 9635 if (iret != 0) 9636 { 9637 skip("failed to bind, error %d\n", WSAGetLastError()); 9638 goto end; 9639 } 9640 9641 socklen = sizeof(*bindAddress); 9642 iret = getsockname(src, (struct sockaddr*)bindAddress, &socklen); 9643 if (iret != 0) { 9644 skip("failed to lookup bind address, error %d\n", WSAGetLastError()); 9645 goto end; 9646 } 9647 9648 if (set_blocking(src, FALSE)) 9649 { 9650 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError()); 9651 goto end; 9652 } 9653 9654 iret = listen(src, 5); 9655 if (iret != 0) 9656 { 9657 skip("listening failed, errno = %d\n", WSAGetLastError()); 9658 goto end; 9659 } 9660 9661 ret = src; 9662 end: 9663 if (src != ret && ret == INVALID_SOCKET) 9664 closesocket(src); 9665 return ret; 9666 } 9667 9668 static void test_completion_port(void) 9669 { 9670 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION io_info; 9671 HANDLE previous_port, io_port; 9672 WSAOVERLAPPED ov, *olp; 9673 SOCKET src, dest, dup, connector = INVALID_SOCKET; 9674 WSAPROTOCOL_INFOA info; 9675 IO_STATUS_BLOCK io; 9676 NTSTATUS status; 9677 char buf[1024]; 9678 WSABUF bufs; 9679 DWORD num_bytes, flags; 9680 struct linger ling; 9681 int iret; 9682 BOOL bret; 9683 ULONG_PTR key; 9684 struct sockaddr_in bindAddress; 9685 GUID acceptExGuid = WSAID_ACCEPTEX; 9686 LPFN_ACCEPTEX pAcceptEx = NULL; 9687 fd_set fds_recv; 9688 9689 memset(buf, 0, sizeof(buf)); 9690 previous_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); 9691 ok( previous_port != NULL, "Failed to create completion port %u\n", GetLastError()); 9692 9693 memset(&ov, 0, sizeof(ov)); 9694 9695 tcp_socketpair(&src, &dest); 9696 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 9697 { 9698 skip("failed to create sockets\n"); 9699 goto end; 9700 } 9701 9702 bufs.len = sizeof(buf); 9703 bufs.buf = buf; 9704 flags = 0; 9705 9706 ling.l_onoff = 1; 9707 ling.l_linger = 0; 9708 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling)); 9709 ok(!iret, "Failed to set linger %d\n", GetLastError()); 9710 9711 io_port = CreateIoCompletionPort( (HANDLE)dest, previous_port, 125, 0 ); 9712 ok(io_port != NULL, "Failed to create completion port %u\n", GetLastError()); 9713 9714 SetLastError(0xdeadbeef); 9715 9716 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL); 9717 ok(iret == SOCKET_ERROR, "WSARecv returned %d\n", iret); 9718 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 9719 9720 Sleep(100); 9721 9722 closesocket(src); 9723 src = INVALID_SOCKET; 9724 9725 SetLastError(0xdeadbeef); 9726 key = 0xdeadbeef; 9727 num_bytes = 0xdeadbeef; 9728 olp = (WSAOVERLAPPED *)0xdeadbeef; 9729 9730 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 9731 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret); 9732 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED, "Last error was %d\n", GetLastError()); 9733 ok(key == 125, "Key is %lu\n", key); 9734 ok(num_bytes == 0, "Number of bytes received is %u\n", num_bytes); 9735 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 9736 9737 SetLastError(0xdeadbeef); 9738 key = 0xdeadbeef; 9739 num_bytes = 0xdeadbeef; 9740 olp = (WSAOVERLAPPED *)0xdeadbeef; 9741 9742 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 9743 ok(bret == FALSE, "GetQueuedCompletionStatus returned %d\n", bret ); 9744 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 9745 ok(key == 0xdeadbeef, "Key is %lu\n", key); 9746 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 9747 ok(!olp, "Overlapped structure is at %p\n", olp); 9748 9749 if (dest != INVALID_SOCKET) 9750 closesocket(dest); 9751 9752 memset(&ov, 0, sizeof(ov)); 9753 9754 tcp_socketpair(&src, &dest); 9755 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 9756 { 9757 skip("failed to create sockets\n"); 9758 goto end; 9759 } 9760 9761 bufs.len = sizeof(buf); 9762 bufs.buf = buf; 9763 flags = 0; 9764 9765 ling.l_onoff = 1; 9766 ling.l_linger = 0; 9767 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling)); 9768 ok(!iret, "Failed to set linger %d\n", GetLastError()); 9769 9770 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0); 9771 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9772 9773 set_blocking(dest, FALSE); 9774 9775 closesocket(src); 9776 src = INVALID_SOCKET; 9777 9778 Sleep(100); 9779 9780 num_bytes = 0xdeadbeef; 9781 SetLastError(0xdeadbeef); 9782 9783 iret = WSASend(dest, &bufs, 1, &num_bytes, 0, &ov, NULL); 9784 ok(iret == SOCKET_ERROR, "WSASend failed - %d\n", iret); 9785 ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError()); 9786 ok(num_bytes == 0xdeadbeef, "Managed to send %d\n", num_bytes); 9787 9788 SetLastError(0xdeadbeef); 9789 key = 0xdeadbeef; 9790 num_bytes = 0xdeadbeef; 9791 olp = (WSAOVERLAPPED *)0xdeadbeef; 9792 9793 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 9794 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret ); 9795 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 9796 ok(key == 0xdeadbeef, "Key is %lu\n", key); 9797 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 9798 ok(!olp, "Overlapped structure is at %p\n", olp); 9799 9800 if (dest != INVALID_SOCKET) 9801 closesocket(dest); 9802 9803 /* Test IOCP response on successful immediate read. */ 9804 tcp_socketpair(&src, &dest); 9805 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 9806 { 9807 skip("failed to create sockets\n"); 9808 goto end; 9809 } 9810 9811 bufs.len = sizeof(buf); 9812 bufs.buf = buf; 9813 flags = 0; 9814 SetLastError(0xdeadbeef); 9815 9816 iret = WSASend(src, &bufs, 1, &num_bytes, 0, &ov, NULL); 9817 ok(!iret, "WSASend failed - %d, last error %u\n", iret, GetLastError()); 9818 ok(num_bytes == sizeof(buf), "Managed to send %d\n", num_bytes); 9819 9820 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0); 9821 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9822 set_blocking(dest, FALSE); 9823 9824 FD_ZERO(&fds_recv); 9825 FD_SET(dest, &fds_recv); 9826 select(dest + 1, &fds_recv, NULL, NULL, NULL); 9827 9828 num_bytes = 0xdeadbeef; 9829 flags = 0; 9830 9831 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL); 9832 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError()); 9833 ok(num_bytes == sizeof(buf), "Managed to read %d\n", num_bytes); 9834 9835 SetLastError(0xdeadbeef); 9836 key = 0xdeadbeef; 9837 num_bytes = 0xdeadbeef; 9838 olp = (WSAOVERLAPPED *)0xdeadbeef; 9839 9840 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 9841 ok(bret == TRUE, "failed to get completion status %u\n", bret); 9842 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError()); 9843 ok(key == 125, "Key is %lu\n", key); 9844 ok(num_bytes == sizeof(buf), "Number of bytes transferred is %u\n", num_bytes); 9845 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 9846 9847 /* Test IOCP response on graceful shutdown. */ 9848 closesocket(src); 9849 9850 FD_ZERO(&fds_recv); 9851 FD_SET(dest, &fds_recv); 9852 select(dest + 1, &fds_recv, NULL, NULL, NULL); 9853 9854 num_bytes = 0xdeadbeef; 9855 flags = 0; 9856 memset(&ov, 0, sizeof(ov)); 9857 9858 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL); 9859 ok(!iret, "WSARecv failed - %d, last error %u\n", iret, GetLastError()); 9860 ok(!num_bytes, "Managed to read %d\n", num_bytes); 9861 9862 SetLastError(0xdeadbeef); 9863 key = 0xdeadbeef; 9864 num_bytes = 0xdeadbeef; 9865 olp = (WSAOVERLAPPED *)0xdeadbeef; 9866 9867 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 9868 ok(bret == TRUE, "failed to get completion status %u\n", bret); 9869 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError()); 9870 ok(key == 125, "Key is %lu\n", key); 9871 ok(!num_bytes, "Number of bytes transferred is %u\n", num_bytes); 9872 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 9873 9874 closesocket(src); 9875 src = INVALID_SOCKET; 9876 closesocket(dest); 9877 dest = INVALID_SOCKET; 9878 9879 /* Test IOCP response on hard shutdown. This was the condition that triggered 9880 * a crash in an actual app (bug 38980). */ 9881 tcp_socketpair(&src, &dest); 9882 if (src == INVALID_SOCKET || dest == INVALID_SOCKET) 9883 { 9884 skip("failed to create sockets\n"); 9885 goto end; 9886 } 9887 9888 bufs.len = sizeof(buf); 9889 bufs.buf = buf; 9890 flags = 0; 9891 memset(&ov, 0, sizeof(ov)); 9892 9893 ling.l_onoff = 1; 9894 ling.l_linger = 0; 9895 iret = setsockopt (src, SOL_SOCKET, SO_LINGER, (char *) &ling, sizeof(ling)); 9896 ok(!iret, "Failed to set linger %d\n", GetLastError()); 9897 9898 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0); 9899 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9900 set_blocking(dest, FALSE); 9901 9902 closesocket(src); 9903 src = INVALID_SOCKET; 9904 9905 FD_ZERO(&fds_recv); 9906 FD_SET(dest, &fds_recv); 9907 select(dest + 1, &fds_recv, NULL, NULL, NULL); 9908 9909 num_bytes = 0xdeadbeef; 9910 SetLastError(0xdeadbeef); 9911 9912 /* Somehow a hard shutdown doesn't work on my Linux box. It seems SO_LINGER is ignored. */ 9913 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL); 9914 todo_wine ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret); 9915 todo_wine ok(GetLastError() == WSAECONNRESET, "Last error was %d\n", GetLastError()); 9916 todo_wine ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes); 9917 9918 SetLastError(0xdeadbeef); 9919 key = 0xdeadbeef; 9920 num_bytes = 0xdeadbeef; 9921 olp = (WSAOVERLAPPED *)0xdeadbeef; 9922 9923 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 9924 todo_wine ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret ); 9925 todo_wine ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 9926 todo_wine ok(key == 0xdeadbeef, "Key is %lu\n", key); 9927 todo_wine ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 9928 todo_wine ok(!olp, "Overlapped structure is at %p\n", olp); 9929 9930 closesocket(dest); 9931 9932 /* Test reading from a non-connected socket, mostly because the above test is marked todo. */ 9933 dest = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 9934 ok(dest != INVALID_SOCKET, "socket() failed\n"); 9935 9936 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 125, 0); 9937 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9938 set_blocking(dest, FALSE); 9939 9940 num_bytes = 0xdeadbeef; 9941 SetLastError(0xdeadbeef); 9942 memset(&ov, 0, sizeof(ov)); 9943 9944 iret = WSARecv(dest, &bufs, 1, &num_bytes, &flags, &ov, NULL); 9945 ok(iret == SOCKET_ERROR, "WSARecv failed - %d\n", iret); 9946 ok(GetLastError() == WSAENOTCONN, "Last error was %d\n", GetLastError()); 9947 ok(num_bytes == 0xdeadbeef, "Managed to read %d\n", num_bytes); 9948 9949 SetLastError(0xdeadbeef); 9950 key = 0xdeadbeef; 9951 num_bytes = 0xdeadbeef; 9952 olp = (WSAOVERLAPPED *)0xdeadbeef; 9953 9954 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 9955 ok(bret == FALSE, "GetQueuedCompletionStatus returned %u\n", bret ); 9956 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 9957 ok(key == 0xdeadbeef, "Key is %lu\n", key); 9958 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 9959 ok(!olp, "Overlapped structure is at %p\n", olp); 9960 9961 num_bytes = 0xdeadbeef; 9962 closesocket(dest); 9963 9964 dest = socket(AF_INET, SOCK_STREAM, 0); 9965 if (dest == INVALID_SOCKET) 9966 { 9967 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 9968 goto end; 9969 } 9970 9971 iret = WSAIoctl(dest, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid), 9972 &pAcceptEx, sizeof(pAcceptEx), &num_bytes, NULL, NULL); 9973 if (iret) 9974 { 9975 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError()); 9976 goto end; 9977 } 9978 9979 /* Test IOCP response on socket close (IOCP created after AcceptEx) */ 9980 9981 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 9982 goto end; 9983 9984 SetLastError(0xdeadbeef); 9985 9986 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 9987 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 9988 &num_bytes, &ov); 9989 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 9990 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 9991 9992 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 9993 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 9994 9995 closesocket(src); 9996 src = INVALID_SOCKET; 9997 9998 SetLastError(0xdeadbeef); 9999 key = 0xdeadbeef; 10000 num_bytes = 0xdeadbeef; 10001 olp = (WSAOVERLAPPED *)0xdeadbeef; 10002 10003 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10004 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10005 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError()); 10006 ok(key == 125, "Key is %lu\n", key); 10007 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10008 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10009 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0); 10010 10011 SetLastError(0xdeadbeef); 10012 key = 0xdeadbeef; 10013 num_bytes = 0xdeadbeef; 10014 olp = (WSAOVERLAPPED *)0xdeadbeef; 10015 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10016 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10017 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10018 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10019 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10020 ok(!olp, "Overlapped structure is at %p\n", olp); 10021 10022 /* Test IOCP response on socket close (IOCP created before AcceptEx) */ 10023 10024 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10025 goto end; 10026 10027 SetLastError(0xdeadbeef); 10028 10029 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10030 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10031 10032 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10033 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10034 &num_bytes, &ov); 10035 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10036 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10037 10038 closesocket(src); 10039 src = INVALID_SOCKET; 10040 10041 SetLastError(0xdeadbeef); 10042 key = 0xdeadbeef; 10043 num_bytes = 0xdeadbeef; 10044 olp = (WSAOVERLAPPED *)0xdeadbeef; 10045 10046 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10047 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10048 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError()); 10049 ok(key == 125, "Key is %lu\n", key); 10050 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10051 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10052 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0); 10053 10054 SetLastError(0xdeadbeef); 10055 key = 0xdeadbeef; 10056 num_bytes = 0xdeadbeef; 10057 olp = (WSAOVERLAPPED *)0xdeadbeef; 10058 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10059 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10060 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10061 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10062 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10063 ok(!olp, "Overlapped structure is at %p\n", olp); 10064 10065 /* Test IOCP with duplicated handle */ 10066 10067 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10068 goto end; 10069 10070 SetLastError(0xdeadbeef); 10071 10072 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10073 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10074 10075 WSADuplicateSocketA( src, GetCurrentProcessId(), &info ); 10076 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED); 10077 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n"); 10078 10079 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10080 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10081 &num_bytes, &ov); 10082 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10083 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10084 10085 SetLastError(0xdeadbeef); 10086 key = 0xdeadbeef; 10087 num_bytes = 0xdeadbeef; 10088 olp = (WSAOVERLAPPED *)0xdeadbeef; 10089 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10090 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10091 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10092 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10093 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10094 ok(!olp, "Overlapped structure is at %p\n", olp); 10095 10096 closesocket(src); 10097 src = INVALID_SOCKET; 10098 closesocket(dup); 10099 dup = INVALID_SOCKET; 10100 10101 SetLastError(0xdeadbeef); 10102 key = 0xdeadbeef; 10103 num_bytes = 0xdeadbeef; 10104 olp = (WSAOVERLAPPED *)0xdeadbeef; 10105 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10106 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10107 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError()); 10108 ok(key == 125, "Key is %lu\n", key); 10109 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10110 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10111 ok(olp && olp->Internal == (ULONG)STATUS_CANCELLED, "Internal status is %lx\n", olp ? olp->Internal : 0); 10112 10113 SetLastError(0xdeadbeef); 10114 key = 0xdeadbeef; 10115 num_bytes = 0xdeadbeef; 10116 olp = (WSAOVERLAPPED *)0xdeadbeef; 10117 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10118 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10119 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10120 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10121 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10122 ok(!olp, "Overlapped structure is at %p\n", olp); 10123 10124 /* Test IOCP with duplicated handle (closing duplicated handle) */ 10125 10126 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10127 goto end; 10128 10129 SetLastError(0xdeadbeef); 10130 10131 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10132 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10133 10134 WSADuplicateSocketA( src, GetCurrentProcessId(), &info ); 10135 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED); 10136 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n"); 10137 10138 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10139 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10140 &num_bytes, &ov); 10141 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10142 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10143 10144 closesocket(dup); 10145 dup = INVALID_SOCKET; 10146 10147 SetLastError(0xdeadbeef); 10148 key = 0xdeadbeef; 10149 num_bytes = 0xdeadbeef; 10150 olp = (WSAOVERLAPPED *)0xdeadbeef; 10151 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10152 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10153 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10154 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10155 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10156 ok(!olp, "Overlapped structure is at %p\n", olp); 10157 10158 SetLastError(0xdeadbeef); 10159 key = 0xdeadbeef; 10160 num_bytes = 0xdeadbeef; 10161 olp = (WSAOVERLAPPED *)0xdeadbeef; 10162 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10163 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10164 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10165 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10166 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10167 ok(!olp, "Overlapped structure is at %p\n", olp); 10168 10169 closesocket(src); 10170 src = INVALID_SOCKET; 10171 10172 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10173 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10174 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError()); 10175 ok(key == 125, "Key is %lu\n", key); 10176 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10177 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10178 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0); 10179 10180 SetLastError(0xdeadbeef); 10181 key = 0xdeadbeef; 10182 num_bytes = 0xdeadbeef; 10183 olp = (WSAOVERLAPPED *)0xdeadbeef; 10184 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10185 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10186 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10187 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10188 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10189 ok(!olp, "Overlapped structure is at %p\n", olp); 10190 10191 /* Test IOCP with duplicated handle (closing original handle) */ 10192 10193 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10194 goto end; 10195 10196 SetLastError(0xdeadbeef); 10197 10198 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10199 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10200 10201 WSADuplicateSocketA( src, GetCurrentProcessId(), &info ); 10202 dup = WSASocketA(AF_INET, SOCK_STREAM, 0, &info, 0, WSA_FLAG_OVERLAPPED); 10203 ok(dup != INVALID_SOCKET, "failed to duplicate socket!\n"); 10204 10205 bret = pAcceptEx(dup, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10206 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10207 &num_bytes, &ov); 10208 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10209 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10210 10211 closesocket(src); 10212 src = INVALID_SOCKET; 10213 10214 SetLastError(0xdeadbeef); 10215 key = 0xdeadbeef; 10216 num_bytes = 0xdeadbeef; 10217 olp = (WSAOVERLAPPED *)0xdeadbeef; 10218 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10219 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10220 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10221 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10222 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10223 ok(!olp, "Overlapped structure is at %p\n", olp); 10224 10225 closesocket(dup); 10226 dup = INVALID_SOCKET; 10227 10228 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10229 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10230 ok(GetLastError() == ERROR_OPERATION_ABORTED, "Last error was %d\n", GetLastError()); 10231 ok(key == 125, "Key is %lu\n", key); 10232 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10233 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10234 ok(olp && (olp->Internal == (ULONG)STATUS_CANCELLED), "Internal status is %lx\n", olp ? olp->Internal : 0); 10235 10236 SetLastError(0xdeadbeef); 10237 key = 0xdeadbeef; 10238 num_bytes = 0xdeadbeef; 10239 olp = (WSAOVERLAPPED *)0xdeadbeef; 10240 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10241 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10242 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10243 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10244 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10245 ok(!olp, "Overlapped structure is at %p\n", olp); 10246 10247 /* Test IOCP without AcceptEx */ 10248 10249 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10250 goto end; 10251 10252 SetLastError(0xdeadbeef); 10253 10254 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10255 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10256 10257 closesocket(src); 10258 src = INVALID_SOCKET; 10259 10260 SetLastError(0xdeadbeef); 10261 key = 0xdeadbeef; 10262 num_bytes = 0xdeadbeef; 10263 olp = (WSAOVERLAPPED *)0xdeadbeef; 10264 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10265 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10266 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10267 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10268 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10269 ok(!olp, "Overlapped structure is at %p\n", olp); 10270 10271 /* */ 10272 10273 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10274 goto end; 10275 10276 connector = socket(AF_INET, SOCK_STREAM, 0); 10277 if (connector == INVALID_SOCKET) { 10278 skip("could not create connector socket, error %d\n", WSAGetLastError()); 10279 goto end; 10280 } 10281 10282 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10283 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10284 10285 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0); 10286 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10287 10288 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10289 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10290 &num_bytes, &ov); 10291 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10292 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10293 10294 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 10295 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError()); 10296 10297 closesocket(connector); 10298 connector = INVALID_SOCKET; 10299 10300 SetLastError(0xdeadbeef); 10301 key = 0xdeadbeef; 10302 num_bytes = 0xdeadbeef; 10303 olp = (WSAOVERLAPPED *)0xdeadbeef; 10304 10305 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10306 ok(bret == TRUE, "failed to get completion status %u\n", bret); 10307 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError()); 10308 ok(key == 125, "Key is %lu\n", key); 10309 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10310 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10311 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0); 10312 10313 SetLastError(0xdeadbeef); 10314 key = 0xdeadbeef; 10315 num_bytes = 0xdeadbeef; 10316 olp = (WSAOVERLAPPED *)0xdeadbeef; 10317 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10318 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10319 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10320 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10321 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10322 ok(!olp, "Overlapped structure is at %p\n", olp); 10323 10324 if (dest != INVALID_SOCKET) 10325 closesocket(dest); 10326 if (src != INVALID_SOCKET) 10327 closesocket(dest); 10328 10329 /* */ 10330 10331 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10332 goto end; 10333 10334 dest = socket(AF_INET, SOCK_STREAM, 0); 10335 if (dest == INVALID_SOCKET) 10336 { 10337 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 10338 goto end; 10339 } 10340 10341 connector = socket(AF_INET, SOCK_STREAM, 0); 10342 if (connector == INVALID_SOCKET) { 10343 skip("could not create connector socket, error %d\n", WSAGetLastError()); 10344 goto end; 10345 } 10346 10347 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10348 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10349 10350 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0); 10351 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10352 10353 io_info.Flags = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS; 10354 status = pNtSetInformationFile((HANDLE)src, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation); 10355 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */, 10356 "expected STATUS_SUCCESS, got %08x\n", status); 10357 10358 io_info.Flags = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS; 10359 status = pNtSetInformationFile((HANDLE)dest, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation); 10360 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */, 10361 "expected STATUS_SUCCESS, got %08x\n", status); 10362 10363 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10364 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10365 &num_bytes, &ov); 10366 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10367 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10368 10369 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 10370 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError()); 10371 10372 iret = send(connector, buf, 1, 0); 10373 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError()); 10374 10375 Sleep(100); 10376 10377 closesocket(dest); 10378 dest = INVALID_SOCKET; 10379 10380 SetLastError(0xdeadbeef); 10381 key = 0xdeadbeef; 10382 num_bytes = 0xdeadbeef; 10383 olp = (WSAOVERLAPPED *)0xdeadbeef; 10384 10385 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10386 ok(bret == TRUE, "failed to get completion status %u\n", bret); 10387 ok(GetLastError() == 0xdeadbeef, "Last error was %d\n", GetLastError()); 10388 ok(key == 125, "Key is %lu\n", key); 10389 ok(num_bytes == 1, "Number of bytes transferred is %u\n", num_bytes); 10390 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10391 ok(olp && (olp->Internal == (ULONG)STATUS_SUCCESS), "Internal status is %lx\n", olp ? olp->Internal : 0); 10392 10393 io_info.Flags = 0; 10394 status = pNtQueryInformationFile((HANDLE)dest, &io, &io_info, sizeof(io_info), FileIoCompletionNotificationInformation); 10395 ok(status == STATUS_SUCCESS || broken(status == STATUS_INVALID_INFO_CLASS) /* XP */, 10396 "expected STATUS_SUCCESS, got %08x\n", status); 10397 if (status == STATUS_SUCCESS) 10398 ok((io_info.Flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS) != 0, "got %08x\n", io_info.Flags); 10399 10400 SetLastError(0xdeadbeef); 10401 key = 0xdeadbeef; 10402 num_bytes = 0xdeadbeef; 10403 olp = (WSAOVERLAPPED *)0xdeadbeef; 10404 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10405 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10406 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10407 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10408 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10409 ok(!olp, "Overlapped structure is at %p\n", olp); 10410 10411 if (src != INVALID_SOCKET) 10412 closesocket(src); 10413 if (connector != INVALID_SOCKET) 10414 closesocket(connector); 10415 10416 /* */ 10417 10418 if ((src = setup_iocp_src(&bindAddress)) == INVALID_SOCKET) 10419 goto end; 10420 10421 dest = socket(AF_INET, SOCK_STREAM, 0); 10422 if (dest == INVALID_SOCKET) 10423 { 10424 skip("could not create acceptor socket, error %d\n", WSAGetLastError()); 10425 goto end; 10426 } 10427 10428 connector = socket(AF_INET, SOCK_STREAM, 0); 10429 if (connector == INVALID_SOCKET) { 10430 skip("could not create connector socket, error %d\n", WSAGetLastError()); 10431 goto end; 10432 } 10433 10434 io_port = CreateIoCompletionPort((HANDLE)src, previous_port, 125, 0); 10435 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10436 10437 io_port = CreateIoCompletionPort((HANDLE)dest, previous_port, 236, 0); 10438 ok(io_port != NULL, "failed to create completion port %u\n", GetLastError()); 10439 10440 bret = pAcceptEx(src, dest, buf, sizeof(buf) - 2*(sizeof(struct sockaddr_in) + 16), 10441 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16, 10442 &num_bytes, &ov); 10443 ok(bret == FALSE, "AcceptEx returned %d\n", bret); 10444 ok(GetLastError() == ERROR_IO_PENDING, "Last error was %d\n", GetLastError()); 10445 10446 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress)); 10447 ok(iret == 0, "connecting to accepting socket failed, error %d\n", GetLastError()); 10448 10449 closesocket(dest); 10450 dest = INVALID_SOCKET; 10451 10452 SetLastError(0xdeadbeef); 10453 key = 0xdeadbeef; 10454 num_bytes = 0xdeadbeef; 10455 olp = (WSAOVERLAPPED *)0xdeadbeef; 10456 10457 bret = GetQueuedCompletionStatus(io_port, &num_bytes, &key, &olp, 100); 10458 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10459 todo_wine ok(GetLastError() == ERROR_NETNAME_DELETED || 10460 GetLastError() == ERROR_OPERATION_ABORTED || 10461 GetLastError() == ERROR_CONNECTION_ABORTED || 10462 GetLastError() == ERROR_PIPE_NOT_CONNECTED /* win 2000 */, 10463 "Last error was %d\n", GetLastError()); 10464 ok(key == 125, "Key is %lu\n", key); 10465 ok(num_bytes == 0, "Number of bytes transferred is %u\n", num_bytes); 10466 ok(olp == &ov, "Overlapped structure is at %p\n", olp); 10467 todo_wine ok(olp && (olp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT || 10468 olp->Internal == (ULONG)STATUS_CANCELLED || 10469 olp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || 10470 olp->Internal == (ULONG)STATUS_PIPE_DISCONNECTED /* win 2000 */), 10471 "Internal status is %lx\n", olp ? olp->Internal : 0); 10472 10473 SetLastError(0xdeadbeef); 10474 key = 0xdeadbeef; 10475 num_bytes = 0xdeadbeef; 10476 olp = (WSAOVERLAPPED *)0xdeadbeef; 10477 bret = GetQueuedCompletionStatus( io_port, &num_bytes, &key, &olp, 200 ); 10478 ok(bret == FALSE, "failed to get completion status %u\n", bret); 10479 ok(GetLastError() == WAIT_TIMEOUT, "Last error was %d\n", GetLastError()); 10480 ok(key == 0xdeadbeef, "Key is %lu\n", key); 10481 ok(num_bytes == 0xdeadbeef, "Number of bytes transferred is %u\n", num_bytes); 10482 ok(!olp, "Overlapped structure is at %p\n", olp); 10483 10484 10485 end: 10486 if (dest != INVALID_SOCKET) 10487 closesocket(dest); 10488 if (src != INVALID_SOCKET) 10489 closesocket(src); 10490 if (connector != INVALID_SOCKET) 10491 closesocket(connector); 10492 CloseHandle(previous_port); 10493 } 10494 10495 static void test_address_list_query(void) 10496 { 10497 SOCKET_ADDRESS_LIST *address_list; 10498 DWORD bytes_returned, size; 10499 unsigned int i; 10500 SOCKET s; 10501 int ret; 10502 10503 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 10504 ok(s != INVALID_SOCKET, "Failed to create socket, error %d.\n", WSAGetLastError()); 10505 10506 bytes_returned = 0; 10507 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0, &bytes_returned, NULL, NULL); 10508 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret); 10509 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError()); 10510 ok(bytes_returned >= FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]), 10511 "Got unexpected bytes_returned %u.\n", bytes_returned); 10512 10513 size = bytes_returned; 10514 bytes_returned = 0; 10515 address_list = HeapAlloc(GetProcessHeap(), 0, size * 2); 10516 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size * 2, &bytes_returned, NULL, NULL); 10517 ok(!ret, "Got unexpected ret %d, error %d.\n", ret, WSAGetLastError()); 10518 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size); 10519 10520 bytes_returned = FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[address_list->iAddressCount]); 10521 for (i = 0; i < address_list->iAddressCount; ++i) 10522 { 10523 bytes_returned += address_list->Address[i].iSockaddrLength; 10524 } 10525 ok(size == bytes_returned, "Got unexpected size %u, expected %u.\n", size, bytes_returned); 10526 10527 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, size, NULL, NULL, NULL); 10528 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret); 10529 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError()); 10530 10531 bytes_returned = 0xdeadbeef; 10532 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, size, &bytes_returned, NULL, NULL); 10533 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret); 10534 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError()); 10535 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size); 10536 10537 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, 1, &bytes_returned, NULL, NULL); 10538 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret); 10539 ok(WSAGetLastError() == WSAEINVAL, "Got unexpected error %d.\n", WSAGetLastError()); 10540 ok(bytes_returned == 0, "Got unexpected bytes_returned %u.\n", bytes_returned); 10541 10542 ret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, address_list, 10543 FIELD_OFFSET(SOCKET_ADDRESS_LIST, Address[0]), &bytes_returned, NULL, NULL); 10544 ok(ret == SOCKET_ERROR, "Got unexpected ret %d.\n", ret); 10545 ok(WSAGetLastError() == WSAEFAULT, "Got unexpected error %d.\n", WSAGetLastError()); 10546 ok(bytes_returned == size, "Got unexpected bytes_returned %u, expected %u.\n", bytes_returned, size); 10547 10548 HeapFree(GetProcessHeap(), 0, address_list); 10549 closesocket(s); 10550 } 10551 10552 static DWORD WINAPI inet_ntoa_thread_proc(void *param) 10553 { 10554 ULONG addr; 10555 const char *str; 10556 HANDLE *event = param; 10557 10558 addr = inet_addr("4.3.2.1"); 10559 ok(addr == htonl(0x04030201), "expected 0x04030201, got %08x\n", addr); 10560 str = inet_ntoa(*(struct in_addr *)&addr); 10561 ok(!strcmp(str, "4.3.2.1"), "expected 4.3.2.1, got %s\n", str); 10562 10563 SetEvent(event[0]); 10564 WaitForSingleObject(event[1], 3000); 10565 10566 return 0; 10567 } 10568 10569 static void test_inet_ntoa(void) 10570 { 10571 ULONG addr; 10572 const char *str; 10573 HANDLE thread, event[2]; 10574 DWORD tid; 10575 10576 addr = inet_addr("1.2.3.4"); 10577 ok(addr == htonl(0x01020304), "expected 0x01020304, got %08x\n", addr); 10578 str = inet_ntoa(*(struct in_addr *)&addr); 10579 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str); 10580 10581 event[0] = CreateEventW(NULL, TRUE, FALSE, NULL); 10582 event[1] = CreateEventW(NULL, TRUE, FALSE, NULL); 10583 10584 thread = CreateThread(NULL, 0, inet_ntoa_thread_proc, event, 0, &tid); 10585 WaitForSingleObject(event[0], 3000); 10586 10587 ok(!strcmp(str, "1.2.3.4"), "expected 1.2.3.4, got %s\n", str); 10588 10589 SetEvent(event[1]); 10590 WaitForSingleObject(thread, 3000); 10591 10592 CloseHandle(event[0]); 10593 CloseHandle(event[1]); 10594 CloseHandle(thread); 10595 } 10596 10597 static void test_WSALookupService(void) 10598 { 10599 char buffer[4096], strbuff[128]; 10600 WSAQUERYSETW *qs = NULL; 10601 HANDLE hnd; 10602 PNLA_BLOB netdata; 10603 int ret; 10604 DWORD error, offset, bsize; 10605 10606 if (!pWSALookupServiceBeginW || !pWSALookupServiceEnd || !pWSALookupServiceNextW) 10607 { 10608 win_skip("WSALookupServiceBeginW or WSALookupServiceEnd or WSALookupServiceNextW not found\n"); 10609 return; 10610 } 10611 10612 qs = (WSAQUERYSETW *)buffer; 10613 memset(qs, 0, sizeof(*qs)); 10614 10615 /* invalid parameter tests */ 10616 ret = pWSALookupServiceBeginW(NULL, 0, &hnd); 10617 error = WSAGetLastError(); 10618 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n"); 10619 todo_wine 10620 ok(error == WSAEFAULT, "expected 10014, got %d\n", error); 10621 10622 ret = pWSALookupServiceBeginW(qs, 0, NULL); 10623 error = WSAGetLastError(); 10624 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n"); 10625 todo_wine 10626 ok(error == WSAEFAULT, "expected 10014, got %d\n", error); 10627 10628 ret = pWSALookupServiceBeginW(qs, 0, &hnd); 10629 error = WSAGetLastError(); 10630 ok(ret == SOCKET_ERROR, "WSALookupServiceBeginW should have failed\n"); 10631 todo_wine 10632 ok(error == WSAEINVAL 10633 || broken(error == ERROR_INVALID_PARAMETER) /* == XP */ 10634 || broken(error == WSAEFAULT) /* == NT */ 10635 || broken(error == WSASERVICE_NOT_FOUND) /* == 2000 */, 10636 "expected 10022, got %d\n", error); 10637 10638 ret = pWSALookupServiceEnd(NULL); 10639 error = WSAGetLastError(); 10640 todo_wine 10641 ok(ret == SOCKET_ERROR, "WSALookupServiceEnd should have failed\n"); 10642 todo_wine 10643 ok(error == ERROR_INVALID_HANDLE, "expected 6, got %d\n", error); 10644 10645 /* standard network list query */ 10646 qs->dwSize = sizeof(*qs); 10647 hnd = (HANDLE)0xdeadbeef; 10648 ret = pWSALookupServiceBeginW(qs, LUP_RETURN_ALL | LUP_DEEP, &hnd); 10649 error = WSAGetLastError(); 10650 if(ret && error == ERROR_INVALID_PARAMETER) 10651 { 10652 win_skip("the current WSALookupServiceBeginW test is not supported in win <= 2000\n"); 10653 return; 10654 } 10655 10656 todo_wine 10657 ok(!ret, "WSALookupServiceBeginW failed unexpectedly with error %d\n", error); 10658 todo_wine 10659 ok(hnd != (HANDLE)0xdeadbeef, "Handle was not filled\n"); 10660 10661 offset = 0; 10662 do 10663 { 10664 memset(qs, 0, sizeof(*qs)); 10665 bsize = sizeof(buffer); 10666 10667 if (pWSALookupServiceNextW(hnd, 0, &bsize, qs) == SOCKET_ERROR) 10668 { 10669 error = WSAGetLastError(); 10670 if (error == WSA_E_NO_MORE) break; 10671 ok(0, "Error %d happened while listing services\n", error); 10672 break; 10673 } 10674 10675 WideCharToMultiByte(CP_ACP, 0, qs->lpszServiceInstanceName, -1, 10676 strbuff, sizeof(strbuff), NULL, NULL); 10677 trace("Network Name: %s\n", strbuff); 10678 10679 /* network data is written in the blob field */ 10680 if (qs->lpBlob) 10681 { 10682 /* each network may have multiple NLA_BLOB information structures */ 10683 do 10684 { 10685 netdata = (PNLA_BLOB) &qs->lpBlob->pBlobData[offset]; 10686 switch (netdata->header.type) 10687 { 10688 case NLA_RAW_DATA: 10689 trace("\tNLA Data Type: NLA_RAW_DATA\n"); 10690 break; 10691 case NLA_INTERFACE: 10692 trace("\tNLA Data Type: NLA_INTERFACE\n"); 10693 trace("\t\tType: %d\n", netdata->data.interfaceData.dwType); 10694 trace("\t\tSpeed: %d\n", netdata->data.interfaceData.dwSpeed); 10695 trace("\t\tAdapter Name: %s\n", netdata->data.interfaceData.adapterName); 10696 break; 10697 case NLA_802_1X_LOCATION: 10698 trace("\tNLA Data Type: NLA_802_1X_LOCATION\n"); 10699 trace("\t\tInformation: %s\n", netdata->data.locationData.information); 10700 break; 10701 case NLA_CONNECTIVITY: 10702 switch (netdata->data.connectivity.type) 10703 { 10704 case NLA_NETWORK_AD_HOC: 10705 trace("\t\tNetwork Type: AD HOC\n"); 10706 break; 10707 case NLA_NETWORK_MANAGED: 10708 trace("\t\tNetwork Type: Managed\n"); 10709 break; 10710 case NLA_NETWORK_UNMANAGED: 10711 trace("\t\tNetwork Type: Unmanaged\n"); 10712 break; 10713 case NLA_NETWORK_UNKNOWN: 10714 trace("\t\tNetwork Type: Unknown\n"); 10715 } 10716 switch (netdata->data.connectivity.internet) 10717 { 10718 case NLA_INTERNET_NO: 10719 trace("\t\tInternet connectivity: No\n"); 10720 break; 10721 case NLA_INTERNET_YES: 10722 trace("\t\tInternet connectivity: Yes\n"); 10723 break; 10724 case NLA_INTERNET_UNKNOWN: 10725 trace("\t\tInternet connectivity: Unknown\n"); 10726 break; 10727 } 10728 break; 10729 case NLA_ICS: 10730 trace("\tNLA Data Type: NLA_ICS\n"); 10731 trace("\t\tSpeed: %d\n", 10732 netdata->data.ICS.remote.speed); 10733 trace("\t\tType: %d\n", 10734 netdata->data.ICS.remote.type); 10735 trace("\t\tState: %d\n", 10736 netdata->data.ICS.remote.state); 10737 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.machineName, -1, 10738 strbuff, sizeof(strbuff), NULL, NULL); 10739 trace("\t\tMachine Name: %s\n", strbuff); 10740 WideCharToMultiByte(CP_ACP, 0, netdata->data.ICS.remote.sharedAdapterName, -1, 10741 strbuff, sizeof(strbuff), NULL, NULL); 10742 trace("\t\tShared Adapter Name: %s\n", strbuff); 10743 break; 10744 default: 10745 trace("\tNLA Data Type: Unknown\n"); 10746 break; 10747 } 10748 } 10749 while (offset); 10750 } 10751 } 10752 while (1); 10753 10754 ret = pWSALookupServiceEnd(hnd); 10755 ok(!ret, "WSALookupServiceEnd failed unexpectedly\n"); 10756 } 10757 10758 static void test_WSAEnumNameSpaceProvidersA(void) 10759 { 10760 LPWSANAMESPACE_INFOA name = NULL; 10761 DWORD ret, error, blen = 0, i; 10762 if (!pWSAEnumNameSpaceProvidersA) 10763 { 10764 win_skip("WSAEnumNameSpaceProvidersA not found\n"); 10765 return; 10766 } 10767 10768 SetLastError(0xdeadbeef); 10769 ret = pWSAEnumNameSpaceProvidersA(&blen, name); 10770 error = WSAGetLastError(); 10771 todo_wine 10772 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10773 todo_wine 10774 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10775 10776 /* Invalid parameter tests */ 10777 SetLastError(0xdeadbeef); 10778 ret = pWSAEnumNameSpaceProvidersA(NULL, name); 10779 error = WSAGetLastError(); 10780 todo_wine 10781 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10782 todo_wine 10783 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10784 10785 SetLastError(0xdeadbeef); 10786 ret = pWSAEnumNameSpaceProvidersA(NULL, NULL); 10787 error = WSAGetLastError(); 10788 todo_wine 10789 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10790 todo_wine 10791 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10792 10793 SetLastError(0xdeadbeef); 10794 ret = pWSAEnumNameSpaceProvidersA(&blen, NULL); 10795 error = WSAGetLastError(); 10796 todo_wine 10797 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10798 todo_wine 10799 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10800 10801 #ifdef __REACTOS__ /* ROSTESTS-233 */ 10802 if (!blen) 10803 { 10804 skip("Failed to get length needed for name space providers.\n"); 10805 return; 10806 } 10807 #endif 10808 10809 name = HeapAlloc(GetProcessHeap(), 0, blen); 10810 if (!name) 10811 { 10812 skip("Failed to alloc memory\n"); 10813 return; 10814 } 10815 10816 ret = pWSAEnumNameSpaceProvidersA(&blen, name); 10817 todo_wine 10818 ok(ret > 0, "Expected more than zero name space providers\n"); 10819 10820 for (i = 0;i < ret; i++) 10821 { 10822 trace("Name space Identifier (%p): %s\n", name[i].lpszIdentifier, 10823 name[i].lpszIdentifier); 10824 switch (name[i].dwNameSpace) 10825 { 10826 case NS_DNS: 10827 trace("\tName space ID: NS_DNS (%u)\n", name[i].dwNameSpace); 10828 break; 10829 case NS_NLA: 10830 trace("\tName space ID: NS_NLA (%u)\n", name[i].dwNameSpace); 10831 break; 10832 default: 10833 trace("\tName space ID: Unknown (%u)\n", name[i].dwNameSpace); 10834 break; 10835 } 10836 trace("\tActive: %d\n", name[i].fActive); 10837 trace("\tVersion: %d\n", name[i].dwVersion); 10838 } 10839 10840 HeapFree(GetProcessHeap(), 0, name); 10841 } 10842 10843 static void test_WSAEnumNameSpaceProvidersW(void) 10844 { 10845 LPWSANAMESPACE_INFOW name = NULL; 10846 DWORD ret, error, blen = 0, i; 10847 if (!pWSAEnumNameSpaceProvidersW) 10848 { 10849 win_skip("WSAEnumNameSpaceProvidersW not found\n"); 10850 return; 10851 } 10852 10853 SetLastError(0xdeadbeef); 10854 ret = pWSAEnumNameSpaceProvidersW(&blen, name); 10855 error = WSAGetLastError(); 10856 todo_wine 10857 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10858 todo_wine 10859 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10860 10861 /* Invalid parameter tests */ 10862 SetLastError(0xdeadbeef); 10863 ret = pWSAEnumNameSpaceProvidersW(NULL, name); 10864 error = WSAGetLastError(); 10865 todo_wine 10866 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10867 todo_wine 10868 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10869 10870 SetLastError(0xdeadbeef); 10871 ret = pWSAEnumNameSpaceProvidersW(NULL, NULL); 10872 error = WSAGetLastError(); 10873 todo_wine 10874 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10875 todo_wine 10876 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10877 10878 SetLastError(0xdeadbeef); 10879 ret = pWSAEnumNameSpaceProvidersW(&blen, NULL); 10880 error = WSAGetLastError(); 10881 todo_wine 10882 ok(ret == SOCKET_ERROR, "Expected failure, got %u\n", ret); 10883 todo_wine 10884 ok(error == WSAEFAULT, "Expected 10014, got %u\n", error); 10885 10886 #ifdef __REACTOS__ /* ROSTESTS-233 */ 10887 if (!blen) 10888 { 10889 skip("Failed to get length needed for name space providers.\n"); 10890 return; 10891 } 10892 #endif 10893 10894 name = HeapAlloc(GetProcessHeap(), 0, blen); 10895 if (!name) 10896 { 10897 skip("Failed to alloc memory\n"); 10898 return; 10899 } 10900 10901 ret = pWSAEnumNameSpaceProvidersW(&blen, name); 10902 todo_wine 10903 ok(ret > 0, "Expected more than zero name space providers\n"); 10904 10905 for (i = 0;i < ret; i++) 10906 { 10907 trace("Name space Identifier (%p): %s\n", name[i].lpszIdentifier, 10908 wine_dbgstr_w(name[i].lpszIdentifier)); 10909 switch (name[i].dwNameSpace) 10910 { 10911 case NS_DNS: 10912 trace("\tName space ID: NS_DNS (%u)\n", name[i].dwNameSpace); 10913 break; 10914 case NS_NLA: 10915 trace("\tName space ID: NS_NLA (%u)\n", name[i].dwNameSpace); 10916 break; 10917 default: 10918 trace("\tName space ID: Unknown (%u)\n", name[i].dwNameSpace); 10919 break; 10920 } 10921 trace("\tActive: %d\n", name[i].fActive); 10922 trace("\tVersion: %d\n", name[i].dwVersion); 10923 } 10924 10925 HeapFree(GetProcessHeap(), 0, name); 10926 } 10927 10928 static void sync_read(SOCKET src, SOCKET dst) 10929 { 10930 int ret; 10931 char data[512]; 10932 10933 ret = send(dst, "Hello World!", 12, 0); 10934 ok(ret == 12, "send returned %d\n", ret); 10935 10936 memset(data, 0, sizeof(data)); 10937 ret = recv(src, data, sizeof(data), 0); 10938 ok(ret == 12, "expected 12, got %d\n", ret); 10939 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", ret, ret, data); 10940 } 10941 10942 static void iocp_async_read(SOCKET src, SOCKET dst) 10943 { 10944 HANDLE port; 10945 WSAOVERLAPPED ovl, *ovl_iocp; 10946 WSABUF buf; 10947 int ret; 10948 char data[512]; 10949 DWORD flags, bytes; 10950 ULONG_PTR key; 10951 10952 memset(data, 0, sizeof(data)); 10953 memset(&ovl, 0, sizeof(ovl)); 10954 10955 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0); 10956 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError()); 10957 10958 buf.len = sizeof(data); 10959 buf.buf = data; 10960 bytes = 0xdeadbeef; 10961 flags = 0; 10962 SetLastError(0xdeadbeef); 10963 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL); 10964 ok(ret == SOCKET_ERROR, "got %d\n", ret); 10965 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError()); 10966 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 10967 10968 bytes = 0xdeadbeef; 10969 key = 0xdeadbeef; 10970 ovl_iocp = (void *)0xdeadbeef; 10971 SetLastError(0xdeadbeef); 10972 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 10973 ok(!ret, "got %d\n", ret); 10974 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 10975 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 10976 ok(key == 0xdeadbeef, "got key %#lx\n", key); 10977 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 10978 10979 ret = send(dst, "Hello World!", 12, 0); 10980 ok(ret == 12, "send returned %d\n", ret); 10981 10982 bytes = 0xdeadbeef; 10983 key = 0xdeadbeef; 10984 ovl_iocp = NULL; 10985 SetLastError(0xdeadbeef); 10986 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 10987 ok(ret, "got %d\n", ret); 10988 ok(bytes == 12, "got bytes %u\n", bytes); 10989 ok(key == 0x12345678, "got key %#lx\n", key); 10990 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp); 10991 if (ovl_iocp) 10992 { 10993 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh); 10994 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal); 10995 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data); 10996 } 10997 10998 bytes = 0xdeadbeef; 10999 key = 0xdeadbeef; 11000 ovl_iocp = (void *)0xdeadbeef; 11001 SetLastError(0xdeadbeef); 11002 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11003 ok(!ret, "got %d\n", ret); 11004 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11005 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11006 ok(key == 0xdeadbeef, "got key %#lx\n", key); 11007 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11008 11009 CloseHandle(port); 11010 } 11011 11012 static void iocp_async_read_closesocket(SOCKET src, int how_to_close) 11013 { 11014 HANDLE port; 11015 WSAOVERLAPPED ovl, *ovl_iocp; 11016 WSABUF buf; 11017 int ret; 11018 char data[512]; 11019 DWORD flags, bytes; 11020 ULONG_PTR key; 11021 HWND hwnd; 11022 MSG msg; 11023 11024 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 11025 0, 0, 0, 0, NULL, NULL, 0, NULL); 11026 ok(hwnd != 0, "CreateWindowEx failed\n"); 11027 11028 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); 11029 ok(!ret, "got %d\n", ret); 11030 11031 Sleep(100); 11032 memset(&msg, 0, sizeof(msg)); 11033 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11034 ok(ret, "got %d\n", ret); 11035 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11036 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11037 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11038 ok(msg.lParam == 2, "got %08lx\n", msg.lParam); 11039 11040 memset(data, 0, sizeof(data)); 11041 memset(&ovl, 0, sizeof(ovl)); 11042 11043 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0); 11044 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError()); 11045 11046 Sleep(100); 11047 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11048 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11049 11050 buf.len = sizeof(data); 11051 buf.buf = data; 11052 bytes = 0xdeadbeef; 11053 flags = 0; 11054 SetLastError(0xdeadbeef); 11055 ret = WSARecv(src, &buf, 1, &bytes, &flags, &ovl, NULL); 11056 ok(ret == SOCKET_ERROR, "got %d\n", ret); 11057 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError()); 11058 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11059 11060 Sleep(100); 11061 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11062 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11063 11064 bytes = 0xdeadbeef; 11065 key = 0xdeadbeef; 11066 ovl_iocp = (void *)0xdeadbeef; 11067 SetLastError(0xdeadbeef); 11068 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11069 ok(!ret, "got %d\n", ret); 11070 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11071 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11072 ok(key == 0xdeadbeef, "got key %#lx\n", key); 11073 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11074 11075 Sleep(100); 11076 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11077 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11078 11079 switch (how_to_close) 11080 { 11081 case 0: 11082 closesocket(src); 11083 break; 11084 case 1: 11085 CloseHandle((HANDLE)src); 11086 break; 11087 case 2: 11088 pNtClose((HANDLE)src); 11089 break; 11090 default: 11091 ok(0, "wrong value %d\n", how_to_close); 11092 break; 11093 } 11094 11095 Sleep(200); 11096 memset(&msg, 0, sizeof(msg)); 11097 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11098 switch (how_to_close) 11099 { 11100 case 0: 11101 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11102 break; 11103 case 1: 11104 case 2: 11105 todo_wine 11106 { 11107 ok(ret, "got %d\n", ret); 11108 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11109 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11110 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11111 ok(msg.lParam == 0x20, "got %08lx\n", msg.lParam); 11112 } 11113 break; 11114 default: 11115 ok(0, "wrong value %d\n", how_to_close); 11116 break; 11117 } 11118 11119 bytes = 0xdeadbeef; 11120 key = 0xdeadbeef; 11121 ovl_iocp = NULL; 11122 SetLastError(0xdeadbeef); 11123 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11124 ok(!ret, "got %d\n", ret); 11125 todo_wine 11126 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError()); 11127 ok(!bytes, "got bytes %u\n", bytes); 11128 ok(key == 0x12345678, "got key %#lx\n", key); 11129 ok(ovl_iocp == &ovl, "got ovl %p\n", ovl_iocp); 11130 if (ovl_iocp) 11131 { 11132 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh); 11133 todo_wine 11134 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal); 11135 } 11136 11137 bytes = 0xdeadbeef; 11138 key = 0xdeadbeef; 11139 ovl_iocp = (void *)0xdeadbeef; 11140 SetLastError(0xdeadbeef); 11141 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11142 ok(!ret, "got %d\n", ret); 11143 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11144 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11145 ok(key == 0xdeadbeef, "got key %#lx\n", key); 11146 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11147 11148 CloseHandle(port); 11149 11150 DestroyWindow(hwnd); 11151 } 11152 11153 static void iocp_async_closesocket(SOCKET src) 11154 { 11155 HANDLE port; 11156 WSAOVERLAPPED *ovl_iocp; 11157 int ret; 11158 DWORD bytes; 11159 ULONG_PTR key; 11160 HWND hwnd; 11161 MSG msg; 11162 11163 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 11164 0, 0, 0, 0, NULL, NULL, 0, NULL); 11165 ok(hwnd != 0, "CreateWindowEx failed\n"); 11166 11167 ret = WSAAsyncSelect(src, hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); 11168 ok(!ret, "got %d\n", ret); 11169 11170 Sleep(100); 11171 memset(&msg, 0, sizeof(msg)); 11172 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11173 ok(ret, "got %d\n", ret); 11174 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11175 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11176 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11177 ok(msg.lParam == 2, "got %08lx\n", msg.lParam); 11178 11179 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0); 11180 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError()); 11181 11182 Sleep(100); 11183 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11184 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11185 11186 bytes = 0xdeadbeef; 11187 key = 0xdeadbeef; 11188 ovl_iocp = (void *)0xdeadbeef; 11189 SetLastError(0xdeadbeef); 11190 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11191 ok(!ret, "got %d\n", ret); 11192 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11193 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11194 ok(key == 0xdeadbeef, "got key %lu\n", key); 11195 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11196 11197 Sleep(100); 11198 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11199 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11200 11201 closesocket(src); 11202 11203 Sleep(100); 11204 memset(&msg, 0, sizeof(msg)); 11205 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11206 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11207 11208 bytes = 0xdeadbeef; 11209 key = 0xdeadbeef; 11210 ovl_iocp = (void *)0xdeadbeef; 11211 SetLastError(0xdeadbeef); 11212 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11213 ok(!ret, "got %d\n", ret); 11214 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11215 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11216 ok(key == 0xdeadbeef, "got key %lu\n", key); 11217 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11218 11219 CloseHandle(port); 11220 11221 DestroyWindow(hwnd); 11222 } 11223 11224 struct wsa_async_select_info 11225 { 11226 SOCKET sock; 11227 HWND hwnd; 11228 }; 11229 11230 static DWORD WINAPI wsa_async_select_thread(void *param) 11231 { 11232 struct wsa_async_select_info *info = param; 11233 int ret; 11234 11235 ret = WSAAsyncSelect(info->sock, info->hwnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); 11236 ok(!ret, "got %d\n", ret); 11237 11238 return 0; 11239 } 11240 11241 struct wsa_recv_info 11242 { 11243 SOCKET sock; 11244 WSABUF wsa_buf; 11245 WSAOVERLAPPED ovl; 11246 }; 11247 11248 static DWORD WINAPI wsa_recv_thread(void *param) 11249 { 11250 struct wsa_recv_info *info = param; 11251 int ret; 11252 DWORD flags, bytes; 11253 11254 bytes = 0xdeadbeef; 11255 flags = 0; 11256 SetLastError(0xdeadbeef); 11257 ret = WSARecv(info->sock, &info->wsa_buf, 1, &bytes, &flags, &info->ovl, NULL); 11258 ok(ret == SOCKET_ERROR, "got %d\n", ret); 11259 ok(GetLastError() == ERROR_IO_PENDING, "got %u\n", GetLastError()); 11260 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11261 11262 return 0; 11263 } 11264 11265 static void iocp_async_read_thread_closesocket(SOCKET src) 11266 { 11267 struct wsa_async_select_info select_info; 11268 struct wsa_recv_info recv_info; 11269 HANDLE port, thread; 11270 WSAOVERLAPPED *ovl_iocp; 11271 int ret; 11272 char data[512]; 11273 DWORD bytes, tid; 11274 ULONG_PTR key; 11275 HWND hwnd; 11276 MSG msg; 11277 11278 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 11279 0, 0, 0, 0, NULL, NULL, 0, NULL); 11280 ok(hwnd != 0, "CreateWindowEx failed\n"); 11281 11282 select_info.sock = src; 11283 select_info.hwnd = hwnd; 11284 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid); 11285 ok(thread != 0, "CreateThread error %u\n", GetLastError()); 11286 ret = WaitForSingleObject(thread, 10000); 11287 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n"); 11288 11289 Sleep(100); 11290 memset(&msg, 0, sizeof(msg)); 11291 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11292 ok(ret, "got %d\n", ret); 11293 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11294 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11295 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11296 ok(msg.lParam == 2, "got %08lx\n", msg.lParam); 11297 11298 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0); 11299 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError()); 11300 11301 Sleep(100); 11302 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11303 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11304 11305 memset(data, 0, sizeof(data)); 11306 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl)); 11307 recv_info.sock = src; 11308 recv_info.wsa_buf.len = sizeof(data); 11309 recv_info.wsa_buf.buf = data; 11310 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid); 11311 ok(thread != 0, "CreateThread error %u\n", GetLastError()); 11312 ret = WaitForSingleObject(thread, 10000); 11313 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n"); 11314 11315 Sleep(100); 11316 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11317 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11318 11319 bytes = 0xdeadbeef; 11320 key = 0xdeadbeef; 11321 ovl_iocp = (void *)0xdeadbeef; 11322 SetLastError(0xdeadbeef); 11323 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11324 ok(!ret, "got %d\n", ret); 11325 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */, 11326 "got %u\n", GetLastError()); 11327 if (GetLastError() == WAIT_TIMEOUT) 11328 { 11329 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11330 ok(key == 0xdeadbeef, "got key %lx\n", key); 11331 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11332 } 11333 else /* document XP behaviour */ 11334 { 11335 ok(!bytes, "got bytes %u\n", bytes); 11336 ok(key == 0x12345678, "got key %#lx\n", key); 11337 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp); 11338 if (ovl_iocp) 11339 { 11340 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh); 11341 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal); 11342 } 11343 11344 closesocket(src); 11345 goto xp_is_broken; 11346 } 11347 11348 Sleep(100); 11349 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11350 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11351 11352 closesocket(src); 11353 11354 Sleep(100); 11355 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11356 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11357 11358 bytes = 0xdeadbeef; 11359 key = 0xdeadbeef; 11360 ovl_iocp = NULL; 11361 SetLastError(0xdeadbeef); 11362 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11363 ok(!ret, "got %d\n", ret); 11364 todo_wine 11365 ok(GetLastError() == ERROR_CONNECTION_ABORTED || GetLastError() == ERROR_NETNAME_DELETED /* XP */, "got %u\n", GetLastError()); 11366 ok(!bytes, "got bytes %u\n", bytes); 11367 ok(key == 0x12345678, "got key %#lx\n", key); 11368 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp); 11369 if (ovl_iocp) 11370 { 11371 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh); 11372 todo_wine 11373 ok(ovl_iocp->Internal == (ULONG)STATUS_CONNECTION_ABORTED || ovl_iocp->Internal == (ULONG)STATUS_LOCAL_DISCONNECT /* XP */, "got %#lx\n", ovl_iocp->Internal); 11374 } 11375 11376 xp_is_broken: 11377 bytes = 0xdeadbeef; 11378 key = 0xdeadbeef; 11379 ovl_iocp = (void *)0xdeadbeef; 11380 SetLastError(0xdeadbeef); 11381 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11382 ok(!ret, "got %d\n", ret); 11383 ok(GetLastError() == WAIT_TIMEOUT, "got %u\n", GetLastError()); 11384 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11385 ok(key == 0xdeadbeef, "got key %lu\n", key); 11386 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11387 11388 CloseHandle(port); 11389 11390 DestroyWindow(hwnd); 11391 } 11392 11393 static void iocp_async_read_thread(SOCKET src, SOCKET dst) 11394 { 11395 struct wsa_async_select_info select_info; 11396 struct wsa_recv_info recv_info; 11397 HANDLE port, thread; 11398 WSAOVERLAPPED *ovl_iocp; 11399 int ret; 11400 char data[512]; 11401 DWORD bytes, tid; 11402 ULONG_PTR key; 11403 HWND hwnd; 11404 MSG msg; 11405 11406 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 11407 0, 0, 0, 0, NULL, NULL, 0, NULL); 11408 ok(hwnd != 0, "CreateWindowEx failed\n"); 11409 11410 select_info.sock = src; 11411 select_info.hwnd = hwnd; 11412 thread = CreateThread(NULL, 0, wsa_async_select_thread, &select_info, 0, &tid); 11413 ok(thread != 0, "CreateThread error %u\n", GetLastError()); 11414 ret = WaitForSingleObject(thread, 10000); 11415 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n"); 11416 11417 Sleep(100); 11418 memset(&msg, 0, sizeof(msg)); 11419 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11420 ok(ret, "got %d\n", ret); 11421 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11422 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11423 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11424 ok(msg.lParam == 2, "got %08lx\n", msg.lParam); 11425 11426 port = CreateIoCompletionPort((HANDLE)src, 0, 0x12345678, 0); 11427 ok(port != 0, "CreateIoCompletionPort error %u\n", GetLastError()); 11428 11429 Sleep(100); 11430 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11431 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11432 11433 memset(data, 0, sizeof(data)); 11434 memset(&recv_info.ovl, 0, sizeof(recv_info.ovl)); 11435 recv_info.sock = src; 11436 recv_info.wsa_buf.len = sizeof(data); 11437 recv_info.wsa_buf.buf = data; 11438 thread = CreateThread(NULL, 0, wsa_recv_thread, &recv_info, 0, &tid); 11439 ok(thread != 0, "CreateThread error %u\n", GetLastError()); 11440 ret = WaitForSingleObject(thread, 10000); 11441 ok(ret == WAIT_OBJECT_0, "thread failed to terminate\n"); 11442 11443 Sleep(100); 11444 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11445 ok(!ret, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11446 11447 bytes = 0xdeadbeef; 11448 key = 0xdeadbeef; 11449 ovl_iocp = (void *)0xdeadbeef; 11450 SetLastError(0xdeadbeef); 11451 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11452 ok(!ret, "got %d\n", ret); 11453 ok(GetLastError() == WAIT_TIMEOUT || broken(GetLastError() == ERROR_OPERATION_ABORTED) /* XP */, "got %u\n", GetLastError()); 11454 if (GetLastError() == WAIT_TIMEOUT) 11455 { 11456 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11457 ok(key == 0xdeadbeef, "got key %lu\n", key); 11458 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11459 } 11460 else /* document XP behaviour */ 11461 { 11462 ok(bytes == 0, "got bytes %u\n", bytes); 11463 ok(key == 0x12345678, "got key %#lx\n", key); 11464 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp); 11465 if (ovl_iocp) 11466 { 11467 ok(!ovl_iocp->InternalHigh, "got %#lx\n", ovl_iocp->InternalHigh); 11468 ok(ovl_iocp->Internal == STATUS_CANCELLED, "got %#lx\n", ovl_iocp->Internal); 11469 } 11470 } 11471 11472 Sleep(100); 11473 memset(&msg, 0, sizeof(msg)); 11474 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11475 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11476 if (ret) /* document XP behaviour */ 11477 { 11478 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11479 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11480 ok(msg.lParam == 1, "got %08lx\n", msg.lParam); 11481 } 11482 11483 ret = send(dst, "Hello World!", 12, 0); 11484 ok(ret == 12, "send returned %d\n", ret); 11485 11486 Sleep(100); 11487 memset(&msg, 0, sizeof(msg)); 11488 ret = PeekMessageA(&msg, hwnd, WM_SOCKET, WM_SOCKET, PM_REMOVE); 11489 ok(!ret || broken(msg.hwnd == hwnd) /* XP */, "got %04x,%08lx,%08lx\n", msg.message, msg.wParam, msg.lParam); 11490 if (ret) /* document XP behaviour */ 11491 { 11492 ok(msg.hwnd == hwnd, "got %p\n", msg.hwnd); 11493 ok(msg.message == WM_SOCKET, "got %04x\n", msg.message); 11494 ok(msg.wParam == src, "got %08lx\n", msg.wParam); 11495 ok(msg.lParam == 1, "got %08lx\n", msg.lParam); 11496 } 11497 11498 bytes = 0xdeadbeef; 11499 key = 0xdeadbeef; 11500 ovl_iocp = (void *)0xdeadbeef; 11501 SetLastError(0xdeadbeef); 11502 ret = GetQueuedCompletionStatus(port, &bytes, &key, &ovl_iocp, 100); 11503 ok(ret || broken(GetLastError() == WAIT_TIMEOUT) /* XP */, "got %u\n", GetLastError()); 11504 if (ret) 11505 { 11506 ok(bytes == 12, "got bytes %u\n", bytes); 11507 ok(key == 0x12345678, "got key %#lx\n", key); 11508 ok(ovl_iocp == &recv_info.ovl, "got ovl %p\n", ovl_iocp); 11509 if (ovl_iocp) 11510 { 11511 ok(ovl_iocp->InternalHigh == 12, "got %#lx\n", ovl_iocp->InternalHigh); 11512 ok(!ovl_iocp->Internal , "got %#lx\n", ovl_iocp->Internal); 11513 ok(!memcmp(data, "Hello World!", 12), "got %u bytes (%*s)\n", bytes, bytes, data); 11514 } 11515 } 11516 else /* document XP behaviour */ 11517 { 11518 ok(bytes == 0xdeadbeef, "got bytes %u\n", bytes); 11519 ok(key == 0xdeadbeef, "got key %lu\n", key); 11520 ok(!ovl_iocp, "got ovl %p\n", ovl_iocp); 11521 } 11522 11523 CloseHandle(port); 11524 11525 DestroyWindow(hwnd); 11526 } 11527 11528 static void test_iocp(void) 11529 { 11530 SOCKET src, dst; 11531 int i, ret; 11532 11533 ret = tcp_socketpair_ovl(&src, &dst); 11534 ok(!ret, "creating socket pair failed\n"); 11535 sync_read(src, dst); 11536 iocp_async_read(src, dst); 11537 closesocket(src); 11538 closesocket(dst); 11539 11540 ret = tcp_socketpair_ovl(&src, &dst); 11541 ok(!ret, "creating socket pair failed\n"); 11542 iocp_async_read_thread(src, dst); 11543 closesocket(src); 11544 closesocket(dst); 11545 11546 for (i = 0; i <= 2; i++) 11547 { 11548 ret = tcp_socketpair_ovl(&src, &dst); 11549 ok(!ret, "creating socket pair failed\n"); 11550 iocp_async_read_closesocket(src, i); 11551 closesocket(dst); 11552 } 11553 11554 ret = tcp_socketpair_ovl(&src, &dst); 11555 ok(!ret, "creating socket pair failed\n"); 11556 iocp_async_closesocket(src); 11557 closesocket(dst); 11558 11559 ret = tcp_socketpair_ovl(&src, &dst); 11560 ok(!ret, "creating socket pair failed\n"); 11561 iocp_async_read_thread_closesocket(src); 11562 closesocket(dst); 11563 } 11564 11565 START_TEST( sock ) 11566 { 11567 int i; 11568 11569 /* Leave these tests at the beginning. They depend on WSAStartup not having been 11570 * called, which is done by Init() below. */ 11571 test_WithoutWSAStartup(); 11572 test_WithWSAStartup(); 11573 11574 Init(); 11575 11576 test_inet_ntoa(); 11577 test_inet_pton(); 11578 test_set_getsockopt(); 11579 test_so_reuseaddr(); 11580 test_ip_pktinfo(); 11581 test_extendedSocketOptions(); 11582 11583 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) 11584 { 11585 trace ( " **** STARTING TEST %d ****\n", i ); 11586 do_test ( &tests[i] ); 11587 trace ( " **** TEST %d COMPLETE ****\n", i ); 11588 } 11589 11590 test_UDP(); 11591 11592 test_getservbyname(); 11593 test_WSASocket(); 11594 test_WSADuplicateSocket(); 11595 test_WSAEnumNetworkEvents(); 11596 11597 test_WSAAddressToStringA(); 11598 test_WSAAddressToStringW(); 11599 11600 test_WSAStringToAddressA(); 11601 test_WSAStringToAddressW(); 11602 11603 test_errors(); 11604 test_listen(); 11605 test_select(); 11606 test_accept(); 11607 test_getpeername(); 11608 test_getsockname(); 11609 test_inet_addr(); 11610 test_addr_to_print(); 11611 test_ioctlsocket(); 11612 test_dns(); 11613 test_gethostbyname(); 11614 test_gethostbyname_hack(); 11615 test_gethostname(); 11616 11617 test_WSASendMsg(); 11618 test_WSASendTo(); 11619 test_WSARecv(); 11620 test_WSAPoll(); 11621 test_write_watch(); 11622 test_iocp(); 11623 11624 test_events(0); 11625 test_events(1); 11626 11627 test_ipv6only(); 11628 test_TransmitFile(); 11629 test_GetAddrInfoW(); 11630 test_GetAddrInfoExW(); 11631 test_getaddrinfo(); 11632 11633 #ifdef __REACTOS__ 11634 if (!winetest_interactive) 11635 { 11636 skip("WSPAcceptEx(), WSPConnectEx() and WSPDisconnectEx() are UNIMPLEMENTED on ReactOS\n"); 11637 skip("Skipping tests due to hang. See ROSTESTS-385\n"); 11638 } 11639 else 11640 { 11641 #endif 11642 test_AcceptEx(); 11643 test_ConnectEx(); 11644 test_DisconnectEx(); 11645 #ifdef __REACTOS__ 11646 } 11647 #endif 11648 11649 test_sioRoutingInterfaceQuery(); 11650 test_sioAddressListChange(); 11651 11652 test_WSALookupService(); 11653 test_WSAEnumNameSpaceProvidersA(); 11654 test_WSAEnumNameSpaceProvidersW(); 11655 11656 test_WSAAsyncGetServByPort(); 11657 test_WSAAsyncGetServByName(); 11658 11659 test_completion_port(); 11660 test_address_list_query(); 11661 11662 /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */ 11663 test_send(); 11664 test_synchronous_WSAIoctl(); 11665 11666 Exit(); 11667 } 11668