Skip to content

Commit

Permalink
fix validateHostOrIp fail when one device has more than one ip (#4896)
Browse files Browse the repository at this point in the history
Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com>
  • Loading branch information
critical27 and Sophie-Xie authored Nov 17, 2022
1 parent d59ee99 commit 6f240e3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ StatusOr<std::string> NetworkUtils::getIPv4FromDevice(const std::string& device)
if (!result.ok()) {
return std::move(result).status();
}
auto iter = result.value().find(device);
auto iter = std::find_if(result.value().begin(),
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());
}
Expand All @@ -56,9 +58,9 @@ StatusOr<std::vector<std::string>> NetworkUtils::listIPv4s() {
return ipv4s;
}

StatusOr<std::unordered_map<std::string, std::string>> NetworkUtils::listDeviceAndIPv4s() {
StatusOr<std::vector<std::pair<std::string, std::string>>> NetworkUtils::listDeviceAndIPv4s() {
struct ifaddrs* iflist;
std::unordered_map<std::string, std::string> dev2ipv4s;
std::vector<std::pair<std::string, std::string>> dev2ipv4s;
if (::getifaddrs(&iflist) != 0) {
return Status::Error("%s", ::strerror(errno));
}
Expand All @@ -70,7 +72,7 @@ StatusOr<std::unordered_map<std::string, std::string>> NetworkUtils::listDeviceA
auto* addr = reinterpret_cast<struct sockaddr_in*>(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[ifa->ifa_name] = ::inet_ntoa(addr->sin_addr);
dev2ipv4s.emplace_back(ifa->ifa_name, ::inet_ntoa(addr->sin_addr));
}
::freeifaddrs(iflist);
if (dev2ipv4s.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/common/network/NetworkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NetworkUtils final {
// List out all Ipv4 addresses, including the loopback one.
static StatusOr<std::vector<std::string>> listIPv4s();
// List out all network devices and its corresponding Ipv4 address.
static StatusOr<std::unordered_map<std::string, std::string>> listDeviceAndIPv4s();
static StatusOr<std::vector<std::pair<std::string, std::string>>> listDeviceAndIPv4s();

// Get the local dynamic port range [low, high], only works for IPv4
static bool getDynamicPortRange(uint16_t& low, uint16_t& high);
Expand Down
5 changes: 4 additions & 1 deletion src/common/network/test/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ TEST(NetworkUtils, listDeviceAndIPv4s) {
auto result = NetworkUtils::listDeviceAndIPv4s();
ASSERT_TRUE(result.ok()) << result.status();
ASSERT_FALSE(result.value().empty());
ASSERT_NE(result.value().end(), result.value().find("lo"));
ASSERT_NE(result.value().end(),
std::find_if(result.value().begin(), result.value().end(), [](const auto& deviceAndIp) {
return deviceAndIp.first == "lo";
}));
}

TEST(NetworkUtils, getDynamicPortRange) {
Expand Down

0 comments on commit 6f240e3

Please sign in to comment.