Skip to content

Commit

Permalink
Address comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
CPWstatic committed Oct 18, 2021
1 parent 3c79ee5 commit 1aee757
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,31 +311,25 @@ Status NetworkUtils::validateHostOrIp(const std::string& hostOrIp) {
return Status::Error("local_ip is empty, need to config it through config file.");
}
const std::regex ipv4(
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
"((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 loopbackOrAny(
"^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|0\\.0\\.0\\.0");
"^127(?:\\.[0-9]+){0,2}\\.[0-9]+$|^(?:0*\\:)*?:?0*1$|(0\\.){3}0");
if (std::regex_match(hostOrIp, loopbackOrAny)) {
return Status::OK();
}
auto ipsStatus = listIPv4s();
NG_RETURN_IF_ERROR(ipsStatus);
const auto& ips = ipsStatus.value();
auto result = std::find_if(
ips.begin(), ips.end(), [hostOrIp](const auto& val) { return hostOrIp == val; });
auto result = std::find(ips.begin(), ips.end(), hostOrIp);
if (result == ips.end()) {
return Status::Error("%s is not a valid ip in current host, candidates: %s",
hostOrIp.c_str(),
folly::join(",", ips).c_str());
}
} else {
auto hostStatus = resolveHost(hostOrIp, 0);
if (!hostStatus.ok()) {
return hostStatus.status();
}
NG_RETURN_IF_ERROR(resolveHost(hostOrIp, 0));
}
return Status::OK();
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/network/test/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ TEST(NetworkUtils, ValidateHostOrIp) {
result = NetworkUtils::validateHostOrIp(hostOrIp);
EXPECT_TRUE(result.ok());

hostOrIp = "000.000.000.000";
result = NetworkUtils::validateHostOrIp(hostOrIp);
EXPECT_FALSE(result.ok());

hostOrIp = "0.0.0.0.0";
result = NetworkUtils::validateHostOrIp(hostOrIp);
EXPECT_FALSE(result.ok());
Expand Down

0 comments on commit 1aee757

Please sign in to comment.