MCPcopy Create free account
hub / github.com/ceph/ceph / _win_socketpair

Function _win_socketpair

src/common/compat.cc:430–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

428}
429
430int _win_socketpair(int socks[2])
431{
432 union {
433 struct sockaddr_in inaddr;
434 struct sockaddr addr;
435 } a;
436 SOCKET listener;
437 int e;
438 socklen_t addrlen = sizeof(a.inaddr);
439 int reuse = 1;
440
441 if (socks == 0) {
442 WSASetLastError(WSAEINVAL);
443 return -1;
444 }
445
446 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
447 if (listener == INVALID_SOCKET) {
448 return -1;
449 }
450
451 memset(&a, 0, sizeof(a));
452 a.inaddr.sin_family = AF_INET;
453 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
454 a.inaddr.sin_port = 0;
455
456 socks[0] = socks[1] = -1;
457 SOCKET s[2] = { INVALID_SOCKET, INVALID_SOCKET };
458
459 do {
460 if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
461 (char*) &reuse, (socklen_t) sizeof(reuse)) == -1)
462 break;
463 if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
464 break;
465 if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
466 break;
467 if (listen(listener, 1) == SOCKET_ERROR)
468 break;
469 s[0] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
470 if (s[0] == INVALID_SOCKET)
471 break;
472 if (connect(s[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
473 break;
474 s[1] = accept(listener, NULL, NULL);
475 if (s[1] == INVALID_SOCKET)
476 break;
477
478 closesocket(listener);
479
480 // The Windows socket API is mostly compatible with the Berkeley
481 // API, with a few exceptions. The Windows socket functions use
482 // SOCKET instead of int. The issue is that on x64 systems,
483 // SOCKET uses 64b while int uses 32b. There's been much debate
484 // whether casting a Windows socket to an int is safe or not.
485 // Worth noting that Windows kernel objects use 32b. For now,
486 // we're just adding a check.
487 //

Callers 1

win_socketpairFunction · 0.85

Calls 2

bindClass · 0.85
connectFunction · 0.50

Tested by

no test coverage detected