diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/network/pair.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/network/pair.cpp')
-rw-r--r-- | util/network/pair.cpp | 154 |
1 files changed, 77 insertions, 77 deletions
diff --git a/util/network/pair.cpp b/util/network/pair.cpp index 9751ef5c96..2e410b842e 100644 --- a/util/network/pair.cpp +++ b/util/network/pair.cpp @@ -1,82 +1,82 @@ -#include "pair.h" - +#include "pair.h" + int SocketPair(SOCKET socks[2], bool overlapped, bool cloexec) { -#if defined(_win_) - struct sockaddr_in addr; - SOCKET listener; - int e; - int addrlen = sizeof(addr); +#if defined(_win_) + struct sockaddr_in addr; + SOCKET listener; + int e; + int addrlen = sizeof(addr); DWORD flags = (overlapped ? WSA_FLAG_OVERLAPPED : 0) | (cloexec ? WSA_FLAG_NO_HANDLE_INHERIT : 0); - - if (socks == 0) { - WSASetLastError(WSAEINVAL); - - return SOCKET_ERROR; - } - - socks[0] = INVALID_SOCKET; - socks[1] = INVALID_SOCKET; - - if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { - return SOCKET_ERROR; - } - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(0x7f000001); - addr.sin_port = 0; - - e = bind(listener, (const struct sockaddr*)&addr, sizeof(addr)); - - if (e == SOCKET_ERROR) { - e = WSAGetLastError(); - closesocket(listener); - WSASetLastError(e); - - return SOCKET_ERROR; - } - - e = getsockname(listener, (struct sockaddr*)&addr, &addrlen); - - if (e == SOCKET_ERROR) { - e = WSAGetLastError(); - closesocket(listener); - WSASetLastError(e); - - return SOCKET_ERROR; - } - - do { - if (listen(listener, 1) == SOCKET_ERROR) - break; - + + if (socks == 0) { + WSASetLastError(WSAEINVAL); + + return SOCKET_ERROR; + } + + socks[0] = INVALID_SOCKET; + socks[1] = INVALID_SOCKET; + + if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + return SOCKET_ERROR; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(0x7f000001); + addr.sin_port = 0; + + e = bind(listener, (const struct sockaddr*)&addr, sizeof(addr)); + + if (e == SOCKET_ERROR) { + e = WSAGetLastError(); + closesocket(listener); + WSASetLastError(e); + + return SOCKET_ERROR; + } + + e = getsockname(listener, (struct sockaddr*)&addr, &addrlen); + + if (e == SOCKET_ERROR) { + e = WSAGetLastError(); + closesocket(listener); + WSASetLastError(e); + + return SOCKET_ERROR; + } + + do { + if (listen(listener, 1) == SOCKET_ERROR) + break; + if ((socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, nullptr, 0, flags)) == INVALID_SOCKET) - break; - - if (connect(socks[0], (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) - break; - + break; + + if (connect(socks[0], (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) + break; + if ((socks[1] = accept(listener, nullptr, nullptr)) == INVALID_SOCKET) - break; - - closesocket(listener); - - return 0; - } while (0); - - e = WSAGetLastError(); - closesocket(listener); - closesocket(socks[0]); - closesocket(socks[1]); - WSASetLastError(e); - - return SOCKET_ERROR; -#else - (void)overlapped; - - #if defined(_linux_) + break; + + closesocket(listener); + + return 0; + } while (0); + + e = WSAGetLastError(); + closesocket(listener); + closesocket(socks[0]); + closesocket(socks[1]); + WSASetLastError(e); + + return SOCKET_ERROR; +#else + (void)overlapped; + + #if defined(_linux_) return socketpair(AF_LOCAL, SOCK_STREAM | (cloexec ? SOCK_CLOEXEC : 0), 0, socks); - #else + #else int r = socketpair(AF_LOCAL, SOCK_STREAM, 0, socks); // Non-atomic wrt exec if (r == 0 && cloexec) { @@ -92,6 +92,6 @@ int SocketPair(SOCKET socks[2], bool overlapped, bool cloexec) { } } return r; - #endif -#endif -} + #endif +#endif +} |