From e1d7b00519c9f8d76bb59b143743be1a0035cb31 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Wed, 29 Nov 2023 19:48:22 +0800 Subject: [PATCH 1/6] feat: add IPv6 support close: #4955 Now it should support dual stack for some friends in community. This roughly change is targeted to create a branch for their quick testing on IPv6 only env(that doesnt come with a DNS) Note intToIPv4 is not handled due to it's probabbly for metad of NebulaGraph v1 only. --- src/common/network/NetworkUtils.cpp | 105 ++++++++++++------- src/common/network/NetworkUtils.h | 12 +-- src/common/network/test/NetworkUtilsTest.cpp | 52 ++++++--- 3 files changed, 112 insertions(+), 57 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index 43d67d892be..0b4073e3daa 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "common/base/Base.h" #include "common/fs/FileUtils.h" @@ -29,11 +30,11 @@ std::string NetworkUtils::getHostname() { return std::string(hn); } -StatusOr NetworkUtils::getIPv4FromDevice(const std::string& device) { +StatusOr NetworkUtils::getIPFromDevice(const std::string& device) { if (device == "any") { return "0.0.0.0"; } - auto result = listDeviceAndIPv4s(); + auto result = listDeviceAndIPs(); if (!result.ok()) { return std::move(result).status(); } @@ -41,47 +42,51 @@ StatusOr NetworkUtils::getIPv4FromDevice(const std::string& device) result.value().end(), [&](const auto& deviceAndIp) { return deviceAndIp.first == device; }); if (iter == result.value().end()) { - return Status::Error("No IPv4 address found for `%s'", device.c_str()); + return Status::Error("No IP address found for `%s'", device.c_str()); } return iter->second; } -StatusOr> NetworkUtils::listIPv4s() { - auto result = listDeviceAndIPv4s(); +StatusOr> NetworkUtils::listIPs() { + auto result = listDeviceAndIPs(); if (!result.ok()) { return std::move(result).status(); } auto getval = [](const auto& entry) { return entry.second; }; - std::vector ipv4s; - ipv4s.resize(result.value().size()); - std::transform(result.value().begin(), result.value().end(), ipv4s.begin(), getval); - return ipv4s; + std::vector ips; + ips.resize(result.value().size()); + std::transform(result.value().begin(), result.value().end(), ips.begin(), getval); + return ips; } -StatusOr>> NetworkUtils::listDeviceAndIPv4s() { +StatusOr>> NetworkUtils::listDeviceAndIPs() { struct ifaddrs* iflist; - std::vector> dev2ipv4s; + std::vector> dev2ips; if (::getifaddrs(&iflist) != 0) { return Status::Error("%s", ::strerror(errno)); } for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { - // Skip non-IPv4 devices - if (ifa->ifa_addr->sa_family != AF_INET) { + char ip[INET6_ADDRSTRLEN]; + if (ifa->ifa_addr->sa_family == AF_INET) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN); + } else { continue; } - auto* addr = reinterpret_cast(ifa->ifa_addr); - // inet_ntoa is thread safe but not re-entrant, - // we could use inet_ntop instead when we need support for IPv6 - dev2ipv4s.emplace_back(ifa->ifa_name, ::inet_ntoa(addr->sin_addr)); + dev2ips.emplace_back(ifa->ifa_name, ip); } ::freeifaddrs(iflist); - if (dev2ipv4s.empty()) { - return Status::Error("No IPv4 devices found"); + if (dev2ips.empty()) { + return Status::Error("No IP devices found"); } - return dev2ipv4s; + return dev2ips; } bool NetworkUtils::getDynamicPortRange(uint16_t& low, uint16_t& high) { + // Note: this function is capable of getting the dynamic port range even for IPv6 FILE* pipe = popen("cat /proc/sys/net/ipv4/ip_local_port_range", "r"); if (!pipe) { LOG(ERROR) << "Failed to open /proc/sys/net/ipv4/ip_local_port_range: " << strerror(errno); @@ -201,20 +206,22 @@ StatusOr> NetworkUtils::resolveHost(const std::string& hos } for (rp = res; rp != nullptr; rp = rp->ai_next) { + char ip[INET6_ADDRSTRLEN]; switch (rp->ai_family) { - case AF_INET: + case AF_INET: { + auto address = (reinterpret_cast(rp->ai_addr))->sin_addr.s_addr; + ::inet_ntop(AF_INET, &address, ip, INET_ADDRSTRLEN); break; - case AF_INET6: - VLOG(1) << "Currently does not support Ipv6 address"; - continue; + } + case AF_INET6: { + auto address = (reinterpret_cast(rp->ai_addr))->sin6_addr; + ::inet_ntop(AF_INET6, &address, ip, INET6_ADDRSTRLEN); + break; + } default: continue; } - - auto address = (reinterpret_cast(rp->ai_addr))->sin_addr.s_addr; - // We need to match the integer byte order generated by ipv4ToInt, - // so we need to convert here. - addrs.emplace_back(intToIPv4(htonl(std::move(address))), port); + addrs.emplace_back(ip, port); } freeaddrinfo(res); @@ -278,31 +285,46 @@ StatusOr> NetworkUtils::toHosts(const std::string& peersSt hosts.reserve(peers.size()); for (auto& peerStr : peers) { auto addrPort = folly::trimWhitespace(peerStr); - auto pos = addrPort.find(':'); - if (pos == folly::StringPiece::npos) { + std::string addr; + int32_t port; + + // Handle strings that may contain IPv6 addresses + auto lastColonPos = addrPort.rfind(':'); + if (lastColonPos == folly::StringPiece::npos) { return Status::Error("Bad peer format: %s", addrPort.start()); } - int32_t port; try { - port = folly::to(addrPort.subpiece(pos + 1)); + port = folly::to(addrPort.subpiece(lastColonPos + 1)); } catch (const std::exception& ex) { return Status::Error("Bad port number, error: %s", ex.what()); } - auto addr = addrPort.subpiece(0, pos).toString(); + addr = addrPort.subpiece(0, lastColonPos).toString(); + // remove the square brackets if present in IPv6 addresses + if (!addr.empty() && addr.front() == '[' && addr.back() == ']') { + addr = addr.substr(1, addr.size() - 2); + } + hosts.emplace_back(std::move(addr), port); } return hosts; } std::string NetworkUtils::toHostsStr(const std::vector& hosts) { - std::string hostsString = ""; + std::string hostsString; for (auto& host : hosts) { if (!hostsString.empty()) { hostsString.append(", "); } - hostsString += folly::stringPrintf("%s:%d", host.host.c_str(), host.port); + // Whether the host is IPv6 address + if (host.host.find(':') != std::string::npos) { + // if IPv6, add square brackets + hostsString += folly::stringPrintf("[%s]:%d", host.host.c_str(), host.port); + } else { + // IPv4 or hostname + hostsString += folly::stringPrintf("%s:%d", host.host.c_str(), host.port); + } } return hostsString; } @@ -314,13 +336,18 @@ Status NetworkUtils::validateHostOrIp(const std::string& hostOrIp) { const std::regex ipv4( "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"); - if (std::regex_match(hostOrIp, ipv4)) { + const std::regex ipv6( + "([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|" + "(([0-9a-fA-F]{1,4}:){1,6}:)|" + "(:(([0-9a-fA-F]{1,4}:){1,6}))"); + if (std::regex_match(hostOrIp, ipv4) || std::regex_match(hostOrIp, ipv6)) { const std::regex loopbackOrAny( - "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|(0\\.){3}0"); + "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|" + "([0:]+1)|((0\\.){3}0)"); if (std::regex_match(hostOrIp, loopbackOrAny)) { return Status::OK(); } - auto ipsStatus = listIPv4s(); + auto ipsStatus = listIPs(); NG_RETURN_IF_ERROR(ipsStatus); const auto& ips = ipsStatus.value(); auto result = std::find(ips.begin(), ips.end(), hostOrIp); diff --git a/src/common/network/NetworkUtils.h b/src/common/network/NetworkUtils.h index f5cd7921113..8a8a8a7273b 100644 --- a/src/common/network/NetworkUtils.h +++ b/src/common/network/NetworkUtils.h @@ -19,13 +19,13 @@ class NetworkUtils final { static std::string getHostname(); - // Get the Ipv4 address bound to a specific net device. + // Get the IP address bound to a specific net device. // If given "any", it returns "0.0.0.0". - static StatusOr getIPv4FromDevice(const std::string& device); - // List out all Ipv4 addresses, including the loopback one. - static StatusOr> listIPv4s(); - // List out all network devices and its corresponding Ipv4 address. - static StatusOr>> listDeviceAndIPv4s(); + static StatusOr getIPFromDevice(const std::string& device); + // List out all IP addresses, including the loopback one. + static StatusOr> listIPs(); + // List out all network devices and its corresponding IP address. + static StatusOr>> listDeviceAndIPs(); // Get the local dynamic port range [low, high], only works for IPv4 static bool getDynamicPortRange(uint16_t& low, uint16_t& high); diff --git a/src/common/network/test/NetworkUtilsTest.cpp b/src/common/network/test/NetworkUtilsTest.cpp index 9c5c5a99f30..802d8c4a580 100644 --- a/src/common/network/test/NetworkUtilsTest.cpp +++ b/src/common/network/test/NetworkUtilsTest.cpp @@ -22,44 +22,53 @@ TEST(NetworkUtils, getHostname) { EXPECT_EQ(std::string(buffer), hostname); } -TEST(NetworkUtils, getIPv4FromDevice) { +TEST(NetworkUtils, getIPFromDevice) { { - auto result = NetworkUtils::getIPv4FromDevice("lo"); + auto result = NetworkUtils::getIPFromDevice("lo"); ASSERT_TRUE(result.ok()) << result.status(); - ASSERT_EQ("127.0.0.1", result.value()); + ASSERT_TRUE(result.value() == "127.0.0.1" || result.value() == "::1"); } { - auto result = NetworkUtils::getIPv4FromDevice("any"); + auto result = NetworkUtils::getIPFromDevice("any"); ASSERT_TRUE(result.ok()) << result.status(); ASSERT_EQ("0.0.0.0", result.value()); } { - auto result = NetworkUtils::getIPv4FromDevice("nonexistent"); + auto result = NetworkUtils::getIPFromDevice("nonexistent"); ASSERT_FALSE(result.ok()) << result.status(); } } -TEST(NetworkUtils, listIPv4s) { - auto result = NetworkUtils::listIPv4s(); +TEST(NetworkUtils, listIPs) { + auto result = NetworkUtils::listIPs(); ASSERT_TRUE(result.ok()) << result.status(); ASSERT_FALSE(result.value().empty()); - auto found = false; + auto foundIPv4 = false; + auto foundIPv6 = false; for (auto& ip : result.value()) { if (ip == "127.0.0.1") { - found = true; + foundIPv4 = true; + } + if (ip == "::1") { + foundIPv6 = true; } } - ASSERT_TRUE(found); + ASSERT_TRUE(foundIPv4); + ASSERT_TRUE(foundIPv6); // This may fail on some OS without IPv6 support } -TEST(NetworkUtils, listDeviceAndIPv4s) { - auto result = NetworkUtils::listDeviceAndIPv4s(); +TEST(NetworkUtils, listDeviceAndIPs) { + auto result = NetworkUtils::listDeviceAndIPs(); ASSERT_TRUE(result.ok()) << result.status(); ASSERT_FALSE(result.value().empty()); ASSERT_NE(result.value().end(), std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { return deviceAndIp.first == "lo"; })); + ASSERT_NE(result.value().end(), + std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { + return deviceAndIp.second == "::1"; + })); } TEST(NetworkUtils, getDynamicPortRange) { @@ -86,6 +95,9 @@ TEST(NetworkUtils, toHosts) { s = NetworkUtils::toHosts("1.1.2.3:123, a.b.c.d:a23"); ASSERT_FALSE(s.ok()); + + s = NetworkUtils::toHosts("[::1]:1200, localhost:1200"); + ASSERT_TRUE(s.ok()); } TEST(NetworkUtils, ValidateHostOrIp) { @@ -120,6 +132,22 @@ TEST(NetworkUtils, ValidateHostOrIp) { hostOrIp = "NonvalidHostName"; result = NetworkUtils::validateHostOrIp(hostOrIp); EXPECT_FALSE(result.ok()); + + hostOrIp = "lab.vesoft-inc.com"; + result = NetworkUtils::validateHostOrIp(hostOrIp); + EXPECT_TRUE(result.ok()); + + hostOrIp = "::1"; + result = NetworkUtils::validateHostOrIp(hostOrIp); + EXPECT_TRUE(result.ok()); + + hostOrIp = "2001:db8::1"; + result = NetworkUtils::validateHostOrIp(hostOrIp); + EXPECT_TRUE(result.ok()); + + hostOrIp = "::g"; + result = NetworkUtils::validateHostOrIp(hostOrIp); + EXPECT_FALSE(result.ok()); } } // namespace network From f605095bc9acde301aab65120f00f689f3c2b236 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Wed, 29 Nov 2023 21:08:19 +0800 Subject: [PATCH 2/6] ipv6 utils relies on physical env, comment them out --- src/common/network/NetworkUtils.cpp | 17 ++++++++++----- src/common/network/test/NetworkUtilsTest.cpp | 23 +++++++++----------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index 0b4073e3daa..a4578e38101 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -337,13 +337,20 @@ Status NetworkUtils::validateHostOrIp(const std::string& hostOrIp) { "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"); const std::regex ipv6( - "([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|" - "(([0-9a-fA-F]{1,4}:){1,6}:)|" - "(:(([0-9a-fA-F]{1,4}:){1,6}))"); + "([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})|" + "(([0-9a-fA-F]{1,4}:){1,7}:)|" + "(:{1,2}([0-9a-fA-F]{1,4}:){1,6})|" + "(([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2})|" + "(([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3})|" + "(([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4})|" + "(([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5})|" + "([0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6}))|" + "(:(:([0-9a-fA-F]{1,4}){1,7}))|" + "^::1$|(::)"); if (std::regex_match(hostOrIp, ipv4) || std::regex_match(hostOrIp, ipv6)) { const std::regex loopbackOrAny( - "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|" - "([0:]+1)|((0\\.){3}0)"); + "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|" + "^::1$|((0\\.){3}0)"); if (std::regex_match(hostOrIp, loopbackOrAny)) { return Status::OK(); } diff --git a/src/common/network/test/NetworkUtilsTest.cpp b/src/common/network/test/NetworkUtilsTest.cpp index 802d8c4a580..701887f0628 100644 --- a/src/common/network/test/NetworkUtilsTest.cpp +++ b/src/common/network/test/NetworkUtilsTest.cpp @@ -44,17 +44,17 @@ TEST(NetworkUtils, listIPs) { ASSERT_TRUE(result.ok()) << result.status(); ASSERT_FALSE(result.value().empty()); auto foundIPv4 = false; - auto foundIPv6 = false; + // auto foundIPv6 = false; for (auto& ip : result.value()) { if (ip == "127.0.0.1") { foundIPv4 = true; } - if (ip == "::1") { - foundIPv6 = true; - } + // if (ip == "::1") { + // foundIPv6 = true; + // } } ASSERT_TRUE(foundIPv4); - ASSERT_TRUE(foundIPv6); // This may fail on some OS without IPv6 support + // ASSERT_TRUE(foundIPv6); // This may fail on some OS without IPv6 support } TEST(NetworkUtils, listDeviceAndIPs) { @@ -65,10 +65,11 @@ TEST(NetworkUtils, listDeviceAndIPs) { std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { return deviceAndIp.first == "lo"; })); - ASSERT_NE(result.value().end(), - std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { - return deviceAndIp.second == "::1"; - })); + // Requires IPv6 env for testing + // ASSERT_NE(result.value().end(), + // std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { + // return deviceAndIp.second == "::1"; + // })); } TEST(NetworkUtils, getDynamicPortRange) { @@ -141,10 +142,6 @@ TEST(NetworkUtils, ValidateHostOrIp) { result = NetworkUtils::validateHostOrIp(hostOrIp); EXPECT_TRUE(result.ok()); - hostOrIp = "2001:db8::1"; - result = NetworkUtils::validateHostOrIp(hostOrIp); - EXPECT_TRUE(result.ok()); - hostOrIp = "::g"; result = NetworkUtils::validateHostOrIp(hostOrIp); EXPECT_FALSE(result.ok()); From 604d85bec8474a6b9cec9d278ef44b290ed33424 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Wed, 29 Nov 2023 22:14:26 +0800 Subject: [PATCH 3/6] fix crash with ipv6 address being binded --- src/common/network/NetworkUtils.cpp | 62 +++++++++++++++++------------ 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index a4578e38101..cc242696ced 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -50,39 +50,49 @@ StatusOr NetworkUtils::getIPFromDevice(const std::string& device) { StatusOr> NetworkUtils::listIPs() { auto result = listDeviceAndIPs(); if (!result.ok()) { - return std::move(result).status(); + return std::move(result).status(); } - auto getval = [](const auto& entry) { return entry.second; }; std::vector ips; - ips.resize(result.value().size()); - std::transform(result.value().begin(), result.value().end(), ips.begin(), getval); + ips.reserve(result.value().size()); + for (const auto& entry : result.value()) { + ips.push_back(entry.second); + } return ips; } StatusOr>> NetworkUtils::listDeviceAndIPs() { - struct ifaddrs* iflist; - std::vector> dev2ips; - if (::getifaddrs(&iflist) != 0) { - return Status::Error("%s", ::strerror(errno)); - } - for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { - char ip[INET6_ADDRSTRLEN]; - if (ifa->ifa_addr->sa_family == AF_INET) { - auto* addr = reinterpret_cast(ifa->ifa_addr); - ::inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - auto* addr = reinterpret_cast(ifa->ifa_addr); - ::inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN); - } else { - continue; + struct ifaddrs* iflist; + std::vector> dev2ips; + if (::getifaddrs(&iflist) != 0) { + return Status::Error("%s", ::strerror(errno)); } - dev2ips.emplace_back(ifa->ifa_name, ip); - } - ::freeifaddrs(iflist); - if (dev2ips.empty()) { - return Status::Error("No IP devices found"); - } - return dev2ips; + for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) { + continue; + } + char ip[INET6_ADDRSTRLEN]; + if (ifa->ifa_addr->sa_family == AF_INET) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN); + // Handle scope ID for IPv6 if necessary + if (addr->sin6_scope_id) { + std::string ipWithScopeId = std::string(ip) + "%" + ifa->ifa_name; + dev2ips.emplace_back(ifa->ifa_name, ipWithScopeId); + continue; + } + } else { + continue; + } + dev2ips.emplace_back(ifa->ifa_name, ip); + } + ::freeifaddrs(iflist); + if (dev2ips.empty()) { + return Status::Error("No IP devices found"); + } + return dev2ips; } bool NetworkUtils::getDynamicPortRange(uint16_t& low, uint16_t& high) { From 427a504ccfbf8765abfdcf97f90035ddd12765ea Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Fri, 1 Dec 2023 11:05:59 +0800 Subject: [PATCH 4/6] chore: make linter happy export CPPLINT_EXTENS=cpp,h export CPPLINT_FILTER=-whitespace/indent,-build/include_what_you_use,-readability/todo,-build/include,-build/header_guard,-runtime/references,-build/c++11 .linters/cpp/cpplint.py --quiet --extensions=$CPPLINT_EXTENS \ --filter=$CPPLINT_FILTER --linelength=100 src/common/network/NetworkUtils.cpp --- src/common/network/test/NetworkUtilsTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/network/test/NetworkUtilsTest.cpp b/src/common/network/test/NetworkUtilsTest.cpp index 701887f0628..10238adf99c 100644 --- a/src/common/network/test/NetworkUtilsTest.cpp +++ b/src/common/network/test/NetworkUtilsTest.cpp @@ -67,7 +67,8 @@ TEST(NetworkUtils, listDeviceAndIPs) { })); // Requires IPv6 env for testing // ASSERT_NE(result.value().end(), - // std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { + // std::find_if( + // result.value().begin(), result.value().end(), [](const auto& deviceAndIp) { // return deviceAndIp.second == "::1"; // })); } From 52409295287427a291dcf6e9cd459978c090b4f4 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Fri, 1 Dec 2023 11:11:31 +0800 Subject: [PATCH 5/6] chore: make formatter happy patch.diff from formatter cd src git apply --reject --whitespace=fix ../patch.diff --- src/common/network/NetworkUtils.cpp | 68 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index cc242696ced..2d09ad7eb84 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -50,49 +50,49 @@ StatusOr NetworkUtils::getIPFromDevice(const std::string& device) { StatusOr> NetworkUtils::listIPs() { auto result = listDeviceAndIPs(); if (!result.ok()) { - return std::move(result).status(); + return std::move(result).status(); } std::vector ips; ips.reserve(result.value().size()); for (const auto& entry : result.value()) { - ips.push_back(entry.second); + ips.push_back(entry.second); } return ips; } StatusOr>> NetworkUtils::listDeviceAndIPs() { - struct ifaddrs* iflist; - std::vector> dev2ips; - if (::getifaddrs(&iflist) != 0) { - return Status::Error("%s", ::strerror(errno)); - } - for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { - if (!ifa->ifa_addr) { - continue; - } - char ip[INET6_ADDRSTRLEN]; - if (ifa->ifa_addr->sa_family == AF_INET) { - auto* addr = reinterpret_cast(ifa->ifa_addr); - ::inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - auto* addr = reinterpret_cast(ifa->ifa_addr); - ::inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN); - // Handle scope ID for IPv6 if necessary - if (addr->sin6_scope_id) { - std::string ipWithScopeId = std::string(ip) + "%" + ifa->ifa_name; - dev2ips.emplace_back(ifa->ifa_name, ipWithScopeId); - continue; - } - } else { - continue; - } - dev2ips.emplace_back(ifa->ifa_name, ip); + struct ifaddrs* iflist; + std::vector> dev2ips; + if (::getifaddrs(&iflist) != 0) { + return Status::Error("%s", ::strerror(errno)); + } + for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) { + continue; } - ::freeifaddrs(iflist); - if (dev2ips.empty()) { - return Status::Error("No IP devices found"); + char ip[INET6_ADDRSTRLEN]; + if (ifa->ifa_addr->sa_family == AF_INET) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto* addr = reinterpret_cast(ifa->ifa_addr); + ::inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN); + // Handle scope ID for IPv6 if necessary + if (addr->sin6_scope_id) { + std::string ipWithScopeId = std::string(ip) + "%" + ifa->ifa_name; + dev2ips.emplace_back(ifa->ifa_name, ipWithScopeId); + continue; + } + } else { + continue; } - return dev2ips; + dev2ips.emplace_back(ifa->ifa_name, ip); + } + ::freeifaddrs(iflist); + if (dev2ips.empty()) { + return Status::Error("No IP devices found"); + } + return dev2ips; } bool NetworkUtils::getDynamicPortRange(uint16_t& low, uint16_t& high) { @@ -359,8 +359,8 @@ Status NetworkUtils::validateHostOrIp(const std::string& hostOrIp) { "^::1$|(::)"); if (std::regex_match(hostOrIp, ipv4) || std::regex_match(hostOrIp, ipv6)) { const std::regex loopbackOrAny( - "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|" - "^::1$|((0\\.){3}0)"); + "^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|" + "^::1$|((0\\.){3}0)"); if (std::regex_match(hostOrIp, loopbackOrAny)) { return Status::OK(); } From 9da0cc4f10a4d786b8e618328b2d2d753966734b Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Fri, 1 Dec 2023 11:43:08 +0800 Subject: [PATCH 6/6] ut: removing cpptest for domain names it turned out this case should be resolved properly in running env, as it's not a mock call. Thus we remove it. --- src/common/network/test/NetworkUtilsTest.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/common/network/test/NetworkUtilsTest.cpp b/src/common/network/test/NetworkUtilsTest.cpp index 10238adf99c..562cf84cd5e 100644 --- a/src/common/network/test/NetworkUtilsTest.cpp +++ b/src/common/network/test/NetworkUtilsTest.cpp @@ -135,10 +135,6 @@ TEST(NetworkUtils, ValidateHostOrIp) { result = NetworkUtils::validateHostOrIp(hostOrIp); EXPECT_FALSE(result.ok()); - hostOrIp = "lab.vesoft-inc.com"; - result = NetworkUtils::validateHostOrIp(hostOrIp); - EXPECT_TRUE(result.ok()); - hostOrIp = "::1"; result = NetworkUtils::validateHostOrIp(hostOrIp); EXPECT_TRUE(result.ok());