client/tests/network_startup.c (31185B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #include <winsock2.h> 7 #else 8 #include "linux_network.h" 9 #include "linux_sync.h" 10 #include "guest_memory.h" 11 #include <sys/utsname.h> 12 #include <pthread.h> 13 #include <arpa/inet.h> 14 #include <unistd.h> 15 #include <time.h> 16 17 static uint32_t call_function(recomp_func_t function, unsigned count, const uint32_t *arguments) 18 { 19 g_esp = g_fs_base + 0xef00; 20 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i]; 21 g_ebx = 0x12345678; 22 g_esi = 0x23456789; 23 g_edi = 0x3456789a; 24 g_ebp = 0x456789ab; 25 function(); 26 assert(g_esp == g_fs_base + 0xef00 + (count + 1) * 4); 27 assert(g_ebx == 0x12345678 && g_esi == 0x23456789); 28 assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); 29 return g_eax; 30 } 31 32 static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments) 33 { 34 return call_function(linux_network_lookup(NULL, ordinal), count, arguments); 35 } 36 #endif 37 38 static uint32_t digest = 2166136261u; 39 static void hash(uint32_t value) { digest = (digest ^ value) * 16777619u; } 40 41 static void set_error(uint32_t value) 42 { 43 #ifdef _WIN32 44 WSASetLastError(value); 45 assert(GetLastError() == value); 46 #else 47 call(112, 1, &value); 48 assert(MEM32(g_fs_base + 0x34) == value); 49 #endif 50 } 51 52 static uint32_t get_error(void) 53 { 54 #ifdef _WIN32 55 return WSAGetLastError(); 56 #else 57 return call(111, 0, NULL); 58 #endif 59 } 60 61 static int startup(uint16_t version, uint32_t expected_error) 62 { 63 unsigned char bytes[408]; 64 memset(bytes, 0xa5, sizeof(bytes)); 65 set_error(777); 66 #ifndef _WIN32 67 memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes)); 68 uint32_t args[] = {version, 0x50003004u}; 69 #endif 70 71 #ifdef _WIN32 72 int result = WSAStartup(version, (WSADATA *)(bytes + 4)); 73 #else 74 int result = call(115, 2, args); 75 memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes)); 76 #endif 77 78 assert(result == ((version & 0xff) ? 0 : 10092)); 79 assert(get_error() == expected_error); 80 assert(bytes[0] == 0xa5 && bytes[3] == 0xa5 && bytes[404] == 0xa5 && bytes[407] == 0xa5); 81 assert(!strcmp((char *)bytes + 8, "WinSock 2.0")); 82 assert(!strcmp((char *)bytes + 265, "Running")); 83 hash(result); 84 for (unsigned i = 0; i < sizeof(bytes); ++i) hash(bytes[i]); 85 return result; 86 } 87 88 static void cleanup(int expected) 89 { 90 set_error(777); 91 92 #ifdef _WIN32 93 int result = WSACleanup(); 94 #else 95 int result = call(116, 0, NULL); 96 #endif 97 98 assert(result == expected); 99 assert(get_error() == (expected ? 10093 : 0)); 100 hash(result); 101 hash(get_error()); 102 } 103 104 static void hostname_queries(int initialized) 105 { 106 char expected_name[256]; 107 #ifdef _WIN32 108 DWORD size = sizeof(expected_name); 109 assert(GetComputerNameExA(ComputerNameDnsHostname, expected_name, &size)); 110 #else 111 struct utsname system; 112 assert(uname(&system) == 0); 113 strcpy(expected_name, system.nodename); 114 #endif 115 int length = strlen(expected_name); 116 assert(length > 0 && length < 256); 117 const struct { int size; int null_buffer; int error; } cases[] = { 118 {-1, 0, 10014}, {0, 0, 10014}, {1, 0, 10014}, {length, 0, 10014}, 119 {length + 1, 0, 0}, {256, 0, 0}, {256, 1, 10014}, {0, 1, 10014}, 120 }; 121 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 122 unsigned char bytes[264], expected[264]; 123 memset(bytes, 0xa5, sizeof(bytes)); 124 memset(expected, 0xa5, sizeof(expected)); 125 int error = initialized ? cases[i].error : 10093; 126 if (!error) memcpy(expected + 4, expected_name, length + 1); 127 set_error(777); 128 #ifndef _WIN32 129 memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes)); 130 uint32_t args[] = {cases[i].null_buffer ? 0 : 0x50003004u, cases[i].size}; 131 #endif 132 133 #ifdef _WIN32 134 int result = gethostname(cases[i].null_buffer ? NULL : (char *)bytes + 4, cases[i].size); 135 #else 136 int result = call(57, 2, args); 137 memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes)); 138 #endif 139 140 assert(result == (error ? -1 : 0)); 141 assert(get_error() == (uint32_t)error); 142 assert(!memcmp(bytes, expected, sizeof(bytes))); 143 } 144 } 145 146 static uint32_t socket_create(int type) 147 { 148 #ifdef _WIN32 149 return socket(AF_INET, type, 0); 150 #else 151 uint32_t arguments[] = {2, type, 0}; 152 return call(23, 3, arguments); 153 #endif 154 } 155 156 static void socket_set_membership(void) 157 { 158 const uint32_t handles[] = {0, 17, 23, UINT32_MAX}; 159 const unsigned counts[] = {0, 1, 2, 64}; 160 for (unsigned c = 0; c < 4; ++c) { 161 for (unsigned h = 0; h < 4; ++h) { 162 #ifdef _WIN32 163 fd_set set; 164 set.fd_count = counts[c]; 165 for (unsigned i = 0; i < counts[c]; ++i) set.fd_array[i] = i ? 23 : 17; 166 #else 167 MEM32(0x50000600) = counts[c]; 168 for (unsigned i = 0; i < counts[c]; ++i) MEM32(0x50000604 + i * 4) = i ? 23 : 17; 169 uint32_t arguments[] = {handles[h], 0x50000600}; 170 #endif 171 set_error(777); 172 173 #ifdef _WIN32 174 int result = __WSAFDIsSet(handles[h], &set); 175 #else 176 int result = call(151, 2, arguments); 177 #endif 178 179 assert(result == ((counts[c] && h == 1) || (counts[c] > 1 && h == 2))); 180 assert(get_error() == 777); 181 } 182 } 183 } 184 185 static int socket_close(uint32_t handle) 186 { 187 #ifdef _WIN32 188 return closesocket(handle); 189 #else 190 return call(3, 1, &handle); 191 #endif 192 } 193 194 static int socket_shutdown(uint32_t handle, uint32_t how) 195 { 196 #ifdef _WIN32 197 return shutdown(handle, how); 198 #else 199 uint32_t arguments[] = {handle, how}; 200 return call(22, 2, arguments); 201 #endif 202 } 203 204 static int socket_name(int peer, uint32_t handle, unsigned char *bytes, int32_t *length) 205 { 206 #ifdef _WIN32 207 return peer ? getpeername(handle, (struct sockaddr *)bytes, (int *)length) : 208 getsockname(handle, (struct sockaddr *)bytes, (int *)length); 209 #else 210 MEM32(0x50000b00) = 0x12345678; 211 MEM32(0x50000b1c) = 0x87654321; 212 if (bytes) memcpy((void *)ADDR(0x50000b04), bytes, 24); 213 if (length) MEM32(0x50000b20) = *length; 214 uint32_t arguments[] = {handle, bytes ? 0x50000b04 : 0, length ? 0x50000b20 : 0}; 215 int result = call(peer ? 5 : 6, 3, arguments); 216 if (bytes) memcpy(bytes, (void *)ADDR(0x50000b04), 24); 217 if (length) *length = MEM32(0x50000b20); 218 assert(MEM32(0x50000b00) == 0x12345678 && MEM32(0x50000b1c) == 0x87654321); 219 return result; 220 #endif 221 } 222 223 static void socket_addresses(uint32_t handle, const struct sockaddr_in *local, 224 const struct sockaddr_in *remote) 225 { 226 for (int peer = 0; peer < 2; ++peer) { 227 for (unsigned size = 0; size < 4; ++size) { 228 const int32_t lengths[] = {0, 15, 16, 24}; 229 int32_t length = lengths[size]; 230 unsigned char bytes[24]; 231 memset(bytes, 0xa5, sizeof(bytes)); 232 set_error(777); 233 234 int result = socket_name(peer, handle, bytes, &length); 235 236 if (size < 2) { 237 assert(result == -1 && get_error() == 10014 && length == lengths[size]); 238 for (unsigned i = 0; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); 239 } else { 240 assert(result == 0 && get_error() == 0 && length == 16); 241 assert(!memcmp(bytes, peer ? remote : local, 16)); 242 for (unsigned i = 16; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); 243 } 244 } 245 int32_t length = 16; 246 assert(socket_name(peer, handle, NULL, &length) == -1 && get_error() == 10014); 247 unsigned char bytes[24]; 248 memset(bytes, 0xa5, sizeof(bytes)); 249 assert(socket_name(peer, handle, bytes, NULL) == -1 && get_error() == 10014); 250 } 251 } 252 253 static uint32_t create_network_event(void) 254 { 255 #ifdef _WIN32 256 return (uintptr_t)CreateEventA(NULL, TRUE, FALSE, NULL); 257 #else 258 uint32_t arguments[] = {0, 1, 0, 0}; 259 return call_function(linux_sync_lookup("CreateEventA"), 4, arguments); 260 #endif 261 } 262 263 static uint32_t wait_network_event(uint32_t event, uint32_t timeout) 264 { 265 #ifdef _WIN32 266 return WaitForSingleObject((HANDLE)(uintptr_t)event, timeout); 267 #else 268 uint32_t arguments[] = {event, timeout}; 269 return call_function(linux_sync_lookup("WaitForSingleObject"), 2, arguments); 270 #endif 271 } 272 273 static void close_network_event(uint32_t event) 274 { 275 #ifdef _WIN32 276 assert(CloseHandle((HANDLE)(uintptr_t)event)); 277 #else 278 assert(call_function(linux_sync_lookup("CloseHandle"), 1, &event)); 279 #endif 280 } 281 282 static int select_network_events(uint32_t handle, uint32_t event, uint32_t mask) 283 { 284 #ifdef _WIN32 285 return WSAEventSelect(handle, (HANDLE)(uintptr_t)event, mask); 286 #else 287 uint32_t arguments[] = {handle, event, mask}; 288 return call_function(linux_network_lookup("WSAEventSelect", 0), 3, arguments); 289 #endif 290 } 291 292 static uint32_t network_events(uint32_t handle, uint32_t event) 293 { 294 uint32_t output[11]; 295 memset(output, 0xa5, sizeof(output)); 296 #ifndef _WIN32 297 MEM32(0x50000c00) = 0x12345678; 298 memcpy((void *)ADDR(0x50000c04), output, sizeof(output)); 299 MEM32(0x50000c30) = 0x87654321; 300 uint32_t arguments[] = {handle, event, 0x50000c04}; 301 #endif 302 set_error(777); 303 304 #ifdef _WIN32 305 int result = WSAEnumNetworkEvents(handle, (HANDLE)(uintptr_t)event, (WSANETWORKEVENTS *)output); 306 #else 307 int result = call_function(linux_network_lookup("WSAEnumNetworkEvents", 0), 3, arguments); 308 memcpy(output, (void *)ADDR(0x50000c04), sizeof(output)); 309 assert(MEM32(0x50000c00) == 0x12345678 && MEM32(0x50000c30) == 0x87654321); 310 #endif 311 312 assert(result == 0 && get_error() == 0); 313 for (unsigned i = 0; i < 10; ++i) assert(output[i + 1] == (output[0] & (1u << i) ? 0 : 0xa5a5a5a5)); 314 return output[0]; 315 } 316 317 static int socket_control(uint32_t handle, uint32_t command, uint32_t *value) 318 { 319 #ifdef _WIN32 320 return ioctlsocket(handle, command, (u_long *)value); 321 #else 322 MEM32(0x50000400) = 0x12345678; 323 MEM32(0x50000404) = value ? *value : 0; 324 MEM32(0x50000408) = 0x87654321; 325 uint32_t arguments[] = {handle, command, value ? 0x50000404 : 0}; 326 int result = call(10, 3, arguments); 327 if (value) *value = MEM32(0x50000404); 328 assert(MEM32(0x50000400) == 0x12345678 && MEM32(0x50000408) == 0x87654321); 329 return result; 330 #endif 331 } 332 333 static int socket_connect(uint32_t handle, const struct sockaddr_in *address, int length) 334 { 335 #ifdef _WIN32 336 return connect(handle, (const struct sockaddr *)address, length); 337 #else 338 if (address) memcpy((void *)ADDR(0x50000500), address, 16); 339 uint32_t arguments[] = {handle, address ? 0x50000500 : 0, length}; 340 return call(4, 3, arguments); 341 #endif 342 } 343 344 struct socket_set { uint32_t count, handles[4]; }; 345 346 static int socket_transfer(int sending, uint32_t handle, unsigned char *bytes, int length, int flags) 347 { 348 #ifdef _WIN32 349 return sending ? send(handle, (const char *)bytes, length, flags) : recv(handle, (char *)bytes, length, flags); 350 #else 351 MEM32(0x50000a00) = 0x12345678; 352 MEM32(0x50000a14) = 0x87654321; 353 if (bytes) memcpy((void *)ADDR(0x50000a04), bytes, 16); 354 uint32_t arguments[] = {handle, bytes ? 0x50000a04 : 0, length, flags}; 355 int result = call(sending ? 19 : 16, 4, arguments); 356 if (bytes) memcpy(bytes, (void *)ADDR(0x50000a04), 16); 357 assert(MEM32(0x50000a00) == 0x12345678 && MEM32(0x50000a14) == 0x87654321); 358 return result; 359 #endif 360 } 361 362 static double monotonic_seconds(void) 363 { 364 #ifdef _WIN32 365 LARGE_INTEGER counter, frequency; 366 assert(QueryPerformanceCounter(&counter) && QueryPerformanceFrequency(&frequency)); 367 return (double)counter.QuadPart / frequency.QuadPart; 368 #else 369 struct timespec now; 370 assert(!clock_gettime(CLOCK_MONOTONIC, &now)); 371 return now.tv_sec + now.tv_nsec / 1000000000.0; 372 #endif 373 } 374 375 static int socket_select(struct socket_set *read, struct socket_set *write, 376 struct socket_set *except, const int32_t *timeout) 377 { 378 struct socket_set *sets[] = {read, write, except}; 379 #ifdef _WIN32 380 fd_set native[3]; 381 for (unsigned i = 0; i < 3; ++i) 382 if (sets[i]) memcpy(&native[i], sets[i], sizeof(*sets[i])); 383 int result = select(-1, read ? &native[0] : NULL, write ? &native[1] : NULL, 384 except ? &native[2] : NULL, (const struct timeval *)timeout); 385 for (unsigned i = 0; i < 3; ++i) 386 if (sets[i]) memcpy(sets[i], &native[i], sizeof(*sets[i])); 387 #else 388 uint32_t arguments[] = {UINT32_MAX, 0, 0, 0, timeout ? 0x50000900 : 0}; 389 for (unsigned i = 0; i < 3; ++i) { 390 if (!sets[i]) continue; 391 arguments[i + 1] = 0x50000804 + i * 32; 392 MEM32(arguments[i + 1] - 4) = 0x12345678; 393 MEM32(arguments[i + 1] + 20) = 0x87654321; 394 memcpy((void *)ADDR(arguments[i + 1]), sets[i], 20); 395 } 396 if (timeout) memcpy((void *)ADDR(0x50000900), timeout, 8); 397 int result = call(18, 5, arguments); 398 for (unsigned i = 0; i < 3; ++i) { 399 if (!sets[i]) continue; 400 memcpy(sets[i], (void *)ADDR(arguments[i + 1]), 20); 401 assert(MEM32(arguments[i + 1] - 4) == 0x12345678); 402 assert(MEM32(arguments[i + 1] + 20) == 0x87654321); 403 } 404 if (timeout) assert(!memcmp((void *)ADDR(0x50000900), timeout, 8)); 405 #endif 406 return result; 407 } 408 409 static void loopback_connect(int nonblocking, int listening) 410 { 411 uint32_t listener = socket(AF_INET, SOCK_STREAM, 0); 412 assert(listener != UINT32_MAX); 413 struct sockaddr_in address = {0}; 414 address.sin_family = AF_INET; 415 address.sin_addr.s_addr = htonl(0x7f000001); 416 assert(!bind(listener, (const struct sockaddr *)&address, sizeof(address))); 417 #ifdef _WIN32 418 int length = sizeof(address); 419 #else 420 socklen_t length = sizeof(address); 421 #endif 422 assert(!getsockname(listener, (struct sockaddr *)&address, &length)); 423 if (listening) assert(!listen(listener, 1)); 424 uint32_t handle = socket_create(1), mode = nonblocking; 425 assert(handle != UINT32_MAX); 426 assert(socket_control(handle, 0x8004667e, &mode) == 0); 427 set_error(777); 428 429 int result = socket_connect(handle, &address, sizeof(address)); 430 uint32_t error = get_error(); 431 432 if (nonblocking) assert(result == -1 && error == 10035); 433 else if (listening) assert(result == 0 && error == 0); 434 else assert(result == -1 && error == 10061); 435 if (nonblocking) { 436 struct socket_set write = {1, {handle}}, except = {1, {handle}}; 437 int32_t timeout[] = {10, 0}; 438 int selected = socket_select(NULL, &write, &except, timeout); 439 assert(selected == 1); 440 assert(write.count == (unsigned)listening && except.count == (unsigned)!listening); 441 assert((listening ? write.handles[0] : except.handles[0]) == handle); 442 assert(get_error() == 0 && timeout[0] == 10 && timeout[1] == 0); 443 if (!listening) { 444 write.count = 1; 445 timeout[0] = 0; 446 timeout[1] = 20000; 447 double start = monotonic_seconds(); 448 assert(socket_select(NULL, &write, NULL, timeout) == 0); 449 assert(monotonic_seconds() - start >= 0.015 && write.count == 0); 450 } 451 } 452 if (listening) { 453 struct sockaddr_in peer; 454 length = sizeof(peer); 455 uint32_t accepted = accept(listener, (struct sockaddr *)&peer, &length); 456 assert(accepted != UINT32_MAX && peer.sin_addr.s_addr == htonl(0x7f000001)); 457 socket_addresses(handle, &peer, &address); 458 uint32_t event = create_network_event(); 459 assert(event && !select_network_events(handle, event, 0x21)); 460 assert(network_events(handle, event) == 0); 461 assert(wait_network_event(event, 0) == 258); 462 mode = 0; 463 assert(socket_control(handle, 0x8004667e, &mode) == -1 && get_error() == 10022); 464 struct socket_set read = {1, {handle}}, write = {1, {handle}}, except = {1, {handle}}; 465 int32_t timeout[] = {0, 10000}; 466 double start = monotonic_seconds(); 467 assert(socket_select(&read, NULL, NULL, timeout) == 0 && read.count == 0); 468 assert(monotonic_seconds() - start >= 0.005); 469 assert(timeout[0] == 0 && timeout[1] == 10000 && get_error() == 0); 470 assert(send(accepted, "abc", 3, 0) == 3); 471 if (nonblocking) assert(!shutdown(accepted, 1)); 472 assert(wait_network_event(event, 2000) == 0); 473 uint32_t events = network_events(handle, event); 474 if (nonblocking && events != 33) { 475 assert(wait_network_event(event, 2000) == 0); 476 events |= network_events(handle, event); 477 } 478 assert(events == (nonblocking ? 33 : 1)); 479 assert(wait_network_event(event, 0) == 258); 480 read.count = 1; 481 assert(socket_select(&read, NULL, NULL, NULL) == 1 && read.count == 1); 482 timeout[1] = 0; 483 assert(socket_select(&read, &write, &except, timeout) == 2); 484 assert(read.count == 1 && write.count == 1 && except.count == 0); 485 assert(read.handles[0] == handle && write.handles[0] == handle); 486 uint32_t unread = 0; 487 assert(!socket_control(handle, 0x4004667f, &unread) && unread == 3); 488 489 unsigned char bytes[16]; 490 memset(bytes, 0xa5, sizeof(bytes)); 491 assert(socket_transfer(1, handle, bytes, -1, 0) == -1 && get_error() == 10014); 492 assert(socket_transfer(1, handle, NULL, 1, 0) == -1 && get_error() == 10014); 493 assert(socket_transfer(0, handle, bytes, -1, 0) == -1 && get_error() == 10014); 494 assert(wait_network_event(event, 10) == 258); 495 496 assert(socket_transfer(0, handle, bytes, 2, 2) == 2); 497 498 assert(!memcmp(bytes, "ab", 2) && get_error() == 0); 499 assert(wait_network_event(event, 2000) == 0); 500 assert(network_events(handle, event) == 1); 501 assert(wait_network_event(event, 0) == 258); 502 for (unsigned i = 2; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); 503 assert(!socket_control(handle, 0x4004667f, &unread) && unread == 3); 504 assert(socket_transfer(0, handle, bytes, 1, 0) == 1 && bytes[0] == 'a'); 505 assert(wait_network_event(event, 2000) == 0); 506 assert(network_events(handle, event) == 1); 507 assert(socket_transfer(0, handle, bytes, 16, 0) == 2 && !memcmp(bytes, "bc", 2)); 508 mode = 1; 509 assert(!socket_control(handle, 0x8004667e, &mode)); 510 assert(socket_transfer(0, handle, bytes, 1, 0) == (nonblocking ? 0 : -1)); 511 assert(get_error() == (nonblocking ? 0 : 10035)); 512 assert(!memcmp(bytes, "bc", 2)); 513 assert(wait_network_event(event, 10) == 258); 514 assert(socket_transfer(0, handle, bytes, -1, 0) == -1 && get_error() == 10014); 515 const unsigned char response[] = {0, 1, 0xfe, 0xff}; 516 memcpy(bytes, response, sizeof(response)); 517 assert(socket_transfer(1, handle, bytes, 4, 0) == 4 && get_error() == 0); 518 assert(!memcmp(bytes, response, 4)); 519 memset(bytes, 0, sizeof(bytes)); 520 assert(recv(accepted, (char *)bytes, 4, MSG_WAITALL) == 4 && !memcmp(bytes, response, 4)); 521 assert(socket_shutdown(handle, 3) == -1 && get_error() == 10022); 522 assert(socket_shutdown(handle, 1) == 0 && get_error() == 0); 523 assert(recv(accepted, (char *)bytes, 1, 0) == 0); 524 assert(socket_transfer(1, handle, bytes, 1, 0) == -1 && get_error() == 10058); 525 if (!nonblocking) { 526 assert(!shutdown(accepted, 1)); /* SD_SEND and SHUT_WR are both 1. */ 527 assert(wait_network_event(event, 2000) == 0); 528 assert(network_events(handle, event) == 32); 529 } 530 assert(wait_network_event(event, 0) == 258); 531 read.count = 1; 532 assert(socket_select(&read, NULL, NULL, NULL) == 1); 533 assert(socket_transfer(0, handle, bytes, 16, 0) == 0 && get_error() == 0); 534 assert(socket_shutdown(handle, nonblocking ? 2 : 0) == 0 && get_error() == 0); 535 assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10058); 536 assert(!select_network_events(handle, 0, 0)); 537 mode = 0; 538 assert(!socket_control(handle, 0x8004667e, &mode)); 539 close_network_event(event); 540 #ifdef _WIN32 541 assert(!closesocket(accepted)); 542 #else 543 assert(!close(accepted)); 544 #endif 545 } 546 assert(!socket_close(handle)); 547 #ifdef _WIN32 548 assert(!closesocket(listener)); 549 #else 550 assert(!close(listener)); 551 #endif 552 } 553 554 static void reset_notification(void) 555 { 556 uint32_t listener = socket(AF_INET, SOCK_STREAM, 0); 557 assert(listener != UINT32_MAX); 558 struct sockaddr_in address = {0}; 559 address.sin_family = AF_INET; 560 address.sin_addr.s_addr = htonl(0x7f000001); 561 assert(!bind(listener, (const struct sockaddr *)&address, sizeof(address))); 562 #ifdef _WIN32 563 int length = sizeof(address); 564 #else 565 socklen_t length = sizeof(address); 566 #endif 567 assert(!getsockname(listener, (struct sockaddr *)&address, &length)); 568 assert(!listen(listener, 1)); 569 uint32_t handle = socket_create(1); 570 assert(!socket_connect(handle, &address, sizeof(address))); 571 uint32_t accepted = accept(listener, NULL, NULL); 572 assert(accepted != UINT32_MAX); 573 uint32_t event = create_network_event(); 574 assert(!select_network_events(handle, event, 0x21)); 575 struct linger option = {1, 0}; 576 assert(!setsockopt(accepted, SOL_SOCKET, SO_LINGER, (const char *)&option, sizeof(option))); 577 578 #ifdef _WIN32 579 assert(!closesocket(accepted)); 580 #else 581 assert(!close(accepted)); 582 #endif 583 assert(wait_network_event(event, 2000) == 0); 584 uint32_t output[11]; 585 memset(output, 0xa5, sizeof(output)); 586 #ifdef _WIN32 587 assert(!WSAEnumNetworkEvents(handle, (HANDLE)(uintptr_t)event, (WSANETWORKEVENTS *)output)); 588 #else 589 memcpy((void *)ADDR(0x50000c04), output, sizeof(output)); 590 uint32_t arguments[] = {handle, event, 0x50000c04}; 591 assert(!call_function(linux_network_lookup("WSAEnumNetworkEvents", 0), 3, arguments)); 592 memcpy(output, (void *)ADDR(0x50000c04), sizeof(output)); 593 #endif 594 595 /* Local abortive close is reported differently by the two TCP stacks. */ 596 #ifdef _WIN32 597 uint32_t reset_error = 10053; 598 #else 599 uint32_t reset_error = 10054; 600 #endif 601 assert(output[0] == 32 && output[6] == reset_error); 602 unsigned char bytes[16] = {0}; 603 assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10054); 604 assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10054); 605 close_network_event(event); 606 assert(!socket_close(handle)); 607 #ifdef _WIN32 608 assert(!closesocket(listener)); 609 #else 610 assert(!close(listener)); 611 #endif 612 } 613 614 static void socket_lifetime(int initialized) 615 { 616 uint32_t value = 1; 617 set_error(777); 618 619 int result = socket_control(UINT32_MAX, 0x8004667e, &value); 620 621 assert(result == -1 && get_error() == (initialized ? 10038 : 10093)); 622 assert(value == 1); 623 assert(socket_close(UINT32_MAX) == -1); 624 assert(get_error() == (initialized ? 10038 : 10093)); 625 assert(socket_connect(UINT32_MAX, NULL, 0) == -1); 626 assert(get_error() == (initialized ? 10038 : 10093)); 627 assert(socket_shutdown(UINT32_MAX, 1) == -1); 628 assert(get_error() == (initialized ? 10038 : 10093)); 629 struct socket_set read = {1, {UINT32_MAX}}; 630 int32_t timeout[] = {0, 0}; 631 assert(socket_select(&read, NULL, NULL, timeout) == -1); 632 assert(get_error() == (initialized ? 10038 : 10093)); 633 assert(read.count == 1 && read.handles[0] == UINT32_MAX); 634 read.count = 0; 635 assert(socket_select(&read, NULL, NULL, timeout) == -1); 636 assert(get_error() == (initialized ? 10022 : 10093)); 637 for (int type = 1; type <= 2; ++type) { 638 set_error(777); 639 640 uint32_t handle = socket_create(type); 641 642 if (!initialized) { 643 assert(handle == UINT32_MAX && get_error() == 10093); 644 continue; 645 } 646 assert(handle != UINT32_MAX && get_error() == 0); 647 unsigned char local[24]; 648 memset(local, 0xa5, sizeof(local)); 649 int32_t local_length = sizeof(local); 650 assert(socket_name(0, handle, local, &local_length) == -1 && get_error() == 10022); 651 assert(socket_name(1, handle, local, &local_length) == -1 && get_error() == 10057); 652 assert(socket_connect(handle, NULL, 16) == -1 && get_error() == 10014); 653 struct sockaddr_in address = {0}; 654 assert(socket_connect(handle, &address, 15) == -1 && get_error() == 10014); 655 assert(socket_connect(handle, &address, 16) == -1 && get_error() == 10047); 656 const uint32_t modes[] = {1, 0xffffffff, 0, 1}; 657 for (unsigned i = 0; i < 4; ++i) { 658 value = modes[i]; 659 set_error(777); 660 assert(socket_control(handle, 0x8004667e, &value) == 0); 661 assert(value == modes[i] && get_error() == 0); 662 } 663 value = 0xa5a5a5a5; 664 assert(socket_control(handle, 0x4004667f, &value) == 0); 665 assert(value == 0 && get_error() == 0); 666 assert(socket_control(handle, 0x8004667e, NULL) == -1); 667 assert(get_error() == 10014); 668 assert(socket_close(handle) == 0 && get_error() == 0); 669 assert(socket_close(handle) == -1 && get_error() == 10038); 670 assert(socket_control(handle, 0x4004667f, &value) == -1 && get_error() == 10038); 671 } 672 } 673 674 static const char *format_address(uint32_t address) 675 { 676 #ifdef _WIN32 677 struct in_addr input; 678 input.s_addr = address; 679 return inet_ntoa(input); 680 #else 681 return (char *)ADDR(call(12, 1, &address)); 682 #endif 683 } 684 685 #ifdef _WIN32 686 static DWORD WINAPI format_worker(void *argument) 687 #else 688 static void *format_worker(void *argument) 689 #endif 690 { 691 #ifndef _WIN32 692 g_fs_base = 0x50010000; 693 #endif 694 assert(!strcmp(format_address(0xffffffff), "255.255.255.255")); 695 return 0; 696 } 697 698 static void address_strings(void) 699 { 700 const struct { uint32_t address; const char *text; } cases[] = { 701 {0, "0.0.0.0"}, {0xffffffff, "255.255.255.255"}, 702 {0x0100007f, "127.0.0.1"}, {0x2a6433c6, "198.51.100.42"}, 703 {0x010200c0, "192.0.2.1"}, 704 }; 705 for (unsigned i = 0; i < 5; ++i) { 706 set_error(777); 707 708 const char *text = format_address(cases[i].address); 709 710 assert(text && !strcmp(text, cases[i].text)); 711 assert(get_error() == 0); 712 } 713 714 const char *main_text = format_address(0x0100007f); 715 716 #ifdef _WIN32 717 HANDLE thread = CreateThread(NULL, 0, format_worker, NULL, 0, NULL); 718 assert(thread && WaitForSingleObject(thread, 5000) == WAIT_OBJECT_0); 719 assert(CloseHandle(thread)); 720 #else 721 pthread_t thread; 722 assert(!pthread_create(&thread, NULL, format_worker, NULL)); 723 assert(!pthread_join(thread, NULL)); 724 #endif 725 726 assert(!strcmp(main_text, "127.0.0.1")); 727 assert(format_address(0) == main_text); 728 assert(!strcmp(main_text, "0.0.0.0")); 729 } 730 731 static void byte_order(void) 732 { 733 const char *names[] = {"htonl", "htons", "ntohl", "ntohs"}; 734 const unsigned ordinals[] = {8, 9, 14, 15}; 735 const uint32_t longs[] = {0, 1, 0x7f000001, 0x12345678, 0x80000000, 0xffffffff}; 736 const uint32_t expected[] = {0, 0x01000000, 0x0100007f, 0x78563412, 0x80, 0xffffffff}; 737 for (unsigned operation = 0; operation < 4; ++operation) { 738 #ifdef _WIN32 739 HMODULE library = GetModuleHandleA("ws2_32.dll"); 740 FARPROC function = GetProcAddress(library, names[operation]); 741 assert(function == GetProcAddress(library, (const char *)(uintptr_t)ordinals[operation])); 742 #else 743 assert(linux_network_lookup(names[operation], 0) == linux_network_lookup(NULL, ordinals[operation])); 744 #endif 745 for (unsigned i = 0; i < (operation & 1 ? 65536 : 6); ++i) { 746 uint32_t value = operation & 1 ? i : longs[i]; 747 set_error(777); 748 749 #ifdef _WIN32 750 uint32_t result = operation & 1 751 ? ((u_short (WSAAPI *)(u_short))function)(value) 752 : ((u_long (WSAAPI *)(u_long))function)(value); 753 #else 754 uint32_t result = call(ordinals[operation], 1, &value); 755 #endif 756 757 assert(result == (operation & 1 ? ((i & 255) << 8) | (i >> 8) : expected[i])); 758 assert(get_error() == 777); 759 } 760 } 761 } 762 763 int main(void) 764 { 765 #ifdef _WIN32 766 assert(sizeof(WSADATA) == 400); 767 HMODULE library = LoadLibraryA("ws2_32.dll"); 768 assert(GetProcAddress(library, "WSAStartup") == GetProcAddress(library, (const char *)115)); 769 assert(GetProcAddress(library, "gethostname") == GetProcAddress(library, (const char *)57)); 770 assert(GetProcAddress(library, "inet_ntoa") == GetProcAddress(library, (const char *)12)); 771 #else 772 assert(guest_map("network test memory", 0x50000000u, 0x10000u)); 773 assert(guest_map("network worker memory", 0x50010000u, 0x10000u)); 774 g_fs_base = 0x50001000u; 775 assert(linux_network_lookup("WSAStartup", 0) == linux_network_lookup(NULL, 115)); 776 assert(linux_network_lookup("gethostname", 0) == linux_network_lookup(NULL, 57)); 777 assert(linux_network_lookup("inet_ntoa", 0) == linux_network_lookup(NULL, 12)); 778 #endif 779 byte_order(); 780 address_strings(); 781 socket_lifetime(0); 782 socket_set_membership(); 783 hostname_queries(0); 784 cleanup(-1); 785 const struct { uint16_t version; uint32_t error; } versions[] = { 786 {0, 777}, {0x0100, 777}, {1, 0}, {0x0101, 777}, {0xff01, 777}, 787 {2, 777}, {0x0102, 777}, {0x0202, 777}, {0x0302, 777}, {3, 777}, {0xffff, 777}, 788 }; 789 unsigned initialized = 0; 790 for (unsigned i = 0; i < 11; ++i) initialized += startup(versions[i].version, versions[i].error) == 0; 791 assert(initialized == 9); 792 hostname_queries(1); 793 byte_order(); 794 address_strings(); 795 socket_lifetime(1); 796 socket_set_membership(); 797 loopback_connect(0, 1); 798 loopback_connect(1, 1); 799 loopback_connect(0, 0); 800 loopback_connect(1, 0); 801 reset_notification(); 802 uint32_t orphan = socket_create(1); 803 assert(orphan != UINT32_MAX); 804 for (unsigned i = 0; i < initialized; ++i) cleanup(0); 805 cleanup(-1); 806 assert(startup(2, 0) == 0); 807 assert(socket_close(orphan) == -1 && get_error() == 10038); 808 cleanup(0); 809 cleanup(-1); 810 assert(digest == 0xd6dc7661); 811 hostname_queries(0); 812 printf("network startup: 12 negotiations, WSADATA32 layout, nested cleanup and errors, digest %08x\n", digest); 813 puts("hostname: 24 buffer and initialization cases, native host name and guest ABI passed"); 814 puts("byte order: all 16-bit values and six 32-bit cases, before/after startup, errors preserved"); 815 puts("inet_ntoa: five addresses, buffer reuse, per-thread storage and cleared errors before/after startup"); 816 puts("sockets: TCP/UDP creation, nonblocking control, unread count, close, cleanup, invalid handles and guest ABI passed"); 817 puts("connect: blocking/nonblocking loopback, refused connection, invalid addresses and guest ABI passed"); 818 puts("fd_set: empty/single/multiple/full membership, duplicate entries and preserved errors before/after startup passed"); 819 puts("select: pending connect success/failure, read/write readiness, timeout, infinite wait, invalid sets and unread bytes passed"); 820 puts("transport: binary send/recv, peek, partial reads, would-block, graceful close, invalid lengths and canaries passed"); 821 puts("shutdown: send/receive half-close, peer EOF, disabled directions, invalid handles/modes and guest ABI passed"); 822 puts("socket addresses: local/peer endpoints, 16-byte IPv4 layout, buffer lengths, nulls, unbound sockets and canaries passed"); 823 puts("network events: asynchronous read/close signals, enum/reset, peek/partial-read rearming and blocking-mode restriction passed"); 824 return 0; 825 }