diff options
| author | Konstantin Bogomolov <bogomolov@google.com> | 2024-07-17 20:01:03 +0000 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2024-07-18 07:49:51 +0000 |
| commit | 849d34e10b614b59a381a1f265f73a831bb962c3 (patch) | |
| tree | 40e6084ac4ed20d402a1f10d48a7648e0299dd78 /executor/conn.h | |
| parent | 0f90262589ccdf81ff02549bb70e898886f75852 (diff) | |
executor: fix localhost handling for machines with only ipv6
In case only ipv6 is supported, we should try ipv4-localhost first and see if it
fails, and then go on to trying ipv6.
Diffstat (limited to 'executor/conn.h')
| -rw-r--r-- | executor/conn.h | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/executor/conn.h b/executor/conn.h index cf099346c..605124f55 100644 --- a/executor/conn.h +++ b/executor/conn.h @@ -97,6 +97,7 @@ private: { int port = atoi(ports); bool localhost = !strcmp(addr, "localhost"); + int fd; if (!strcmp(addr, "stdin")) return STDIN_FILENO; if (port == 0) @@ -106,20 +107,25 @@ private: saddr4.sin_port = htons(port); if (localhost) addr = "127.0.0.1"; - if (inet_pton(AF_INET, addr, &saddr4.sin_addr)) - return Connect(&saddr4, &saddr4.sin_addr, port); + if (inet_pton(AF_INET, addr, &saddr4.sin_addr)) { + fd = Connect(&saddr4, &saddr4.sin_addr, port); + if (fd != -1 || !localhost) + return fd; + } sockaddr_in6 saddr6 = {}; saddr6.sin6_family = AF_INET6; saddr6.sin6_port = htons(port); if (localhost) addr = "0:0:0:0:0:0:0:1"; - if (inet_pton(AF_INET6, addr, &saddr6.sin6_addr)) - return Connect(&saddr6, &saddr6.sin6_addr, port); + if (inet_pton(AF_INET6, addr, &saddr6.sin6_addr)) { + fd = Connect(&saddr6, &saddr6.sin6_addr, port); + if (fd != -1 || !localhost) + return fd; + } auto* hostent = gethostbyname(addr); if (!hostent) failmsg("failed to resolve manager addr", "addr=%s h_errno=%d", addr, h_errno); for (char** addr = hostent->h_addr_list; *addr; addr++) { - int fd; if (hostent->h_addrtype == AF_INET) { memcpy(&saddr4.sin_addr, *addr, std::min<size_t>(hostent->h_length, sizeof(saddr4.sin_addr))); fd = Connect(&saddr4, &saddr4.sin_addr, port); @@ -140,8 +146,10 @@ private: { auto* saddr = reinterpret_cast<sockaddr*>(addr); int fd = socket(saddr->sa_family, SOCK_STREAM, IPPROTO_TCP); - if (fd == -1) - fail("failed to create socket"); + if (fd == -1) { + printf("failed to create socket for address family %d", saddr->sa_family); + return -1; + } char str[128] = {}; inet_ntop(saddr->sa_family, ip, str, sizeof(str)); if (connect(fd, saddr, sizeof(*addr))) { |
