Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the toHostAddr interface to support host resolved (gethostbyname). #935

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,53 @@ std::string NetworkUtils::intToIPv4(IPv4 ip) {
return buf;
}

StatusOr<HostAddr> NetworkUtils::toHostAddr(folly::StringPiece ip, int32_t port) {
StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ip, int32_t port) {
monadbobo marked this conversation as resolved.
Show resolved Hide resolved
std::vector<HostAddr> addrs;
IPv4 ipV4;
if (!ipv4ToInt(ip.toString(), ipV4)) {
return Status::Error("Bad ip format:%s", ip.start());
std::string str = ip.toString();
if (ipv4ToInt(str, ipV4)) {
addrs.emplace_back(std::move(ipV4), port);
return addrs;
}
return std::make_pair(ipV4, port);

struct addrinfo hints, *res, *rp;
::memset(&hints, 0, sizeof(struct addrinfo));

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG;

if (getaddrinfo(str.c_str(), nullptr, &hints, &res) != 0) {
return Status::Error("host not found:%s", ip.start());
}

for (rp = res; rp != nullptr; rp = rp->ai_next) {
switch (rp->ai_family) {
case AF_INET:
break;
case AF_INET6:
VLOG(1) << "Currently does not support Ipv6 address";
continue;
default:
continue;
}

auto address = ((struct sockaddr_in*)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(htonl(std::move(address)), port);
}

freeaddrinfo(res);

if (addrs.empty()) {
return Status::Error("host not found: %s", str.c_str());
}

return addrs;
}

StatusOr<HostAddr> NetworkUtils::toHostAddr(folly::StringPiece ipPort) {
StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ipPort) {
auto pos = ipPort.find(':');
if (pos == folly::StringPiece::npos) {
return Status::Error("Bad peer format: %s", ipPort.start());
Expand All @@ -249,7 +287,8 @@ StatusOr<std::vector<HostAddr>> NetworkUtils::toHosts(const std::string& peersSt
if (!hostAddr.ok()) {
return hostAddr.status();
}
hosts.emplace_back(hostAddr.value());
hosts.insert(hosts.end(), std::make_move_iterator(hostAddr.value().begin()),
std::make_move_iterator(hostAddr.value().end()));
}
return hosts;
}
Expand Down Expand Up @@ -281,4 +320,3 @@ StatusOr<std::string> NetworkUtils::getLocalIP(std::string defaultIP) {

} // namespace network
} // namespace nebula

9 changes: 4 additions & 5 deletions src/common/network/NetworkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class NetworkUtils final {
// Get a dynamic port that is not in use
static uint16_t getAvailablePort();

// Convert the given IP (must be in the form of "xx.xx.xx.xx") and Port to a HostAddr
static StatusOr<HostAddr> toHostAddr(folly::StringPiece ip, int32_t port);
// Convert the given IP/Port (must be in the form of "xx.xx.xx.xx:pp") to a HostAddr
static StatusOr<HostAddr> toHostAddr(folly::StringPiece ipPort);
// Convert the given IP/HOST and Port to a HostAddr
static StatusOr<std::vector<HostAddr>> toHostAddr(folly::StringPiece ip, int32_t port);
// Convert the given (IP/HOST):Port to a HostAddr
static StatusOr<std::vector<HostAddr>> toHostAddr(folly::StringPiece ipPort);
// Retrieve the string-form IP from the given HostAddr
static std::string ipFromHostAddr(const HostAddr& host);
// Retrieve the port number from the given HostAddr
Expand Down Expand Up @@ -67,4 +67,3 @@ class NetworkUtils final {
} // namespace nebula

#endif // COMMON_NETWORK_NETWORKUTILS_H_

39 changes: 38 additions & 1 deletion src/common/network/test/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ TEST(NetworkUtils, getAvailablePort) {
ASSERT_GT(port, 0);
}

TEST(NetworkUtils, toHostAddr) {
auto s = NetworkUtils::toHostAddr("localhost:1200");
ASSERT_TRUE(s.ok());
auto addr = s.value();
IPv4 ip;
ASSERT_TRUE(NetworkUtils::ipv4ToInt("127.0.0.1", ip));
ASSERT_EQ(addr[0].first, ip);
ASSERT_EQ(addr[0].second, 1200);

s = NetworkUtils::toHostAddr("8.8.8.8:1300");
ASSERT_TRUE(s.ok());
addr = s.value();

ASSERT_TRUE(NetworkUtils::ipv4ToInt("8.8.8.8", ip));
ASSERT_EQ(addr[0].first, ip);
ASSERT_EQ(addr[0].second, 1300);

s = NetworkUtils::toHostAddr("a.b.c.d:a23");
ASSERT_FALSE(s.ok());
}

TEST(NetworkUtils, toHosts) {
auto s = NetworkUtils::toHosts("localhost:1200, 127.0.0.1:1200");
ASSERT_TRUE(s.ok());
auto addr = s.value();

IPv4 ip;
ASSERT_TRUE(NetworkUtils::ipv4ToInt("127.0.0.1", ip));
ASSERT_EQ(addr[0].first, ip);
ASSERT_EQ(addr[0].second, 1200);

ASSERT_EQ(addr[1].first, ip);
ASSERT_EQ(addr[1].second, 1200);

s = NetworkUtils::toHostAddr("1.1.2.3:123, a.b.c.d:a23");
ASSERT_FALSE(s.ok());
}

} // namespace network
} // namespace nebula

Expand All @@ -98,4 +136,3 @@ int main(int argc, char** argv) {

return RUN_ALL_TESTS();
}

4 changes: 2 additions & 2 deletions src/daemons/MetaDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ int main(int argc, char *argv[]) {
LOG(ERROR) << "Bad local host addr, status:" << hostAddrRet.status();
return EXIT_FAILURE;
}
auto& localhost = hostAddrRet.value();
auto& localhost = hostAddrRet.value()[0];
auto peersRet = nebula::network::NetworkUtils::toHosts(FLAGS_meta_server_addrs);
if (!peersRet.ok()) {
LOG(ERROR) << "Can't get peers address, status:" << peersRet.status();
return EXIT_FAILURE;
}

auto kvstore = initKV(peersRet.value(), hostAddrRet.value());
auto kvstore = initKV(peersRet.value(), localhost);
if (kvstore == nullptr) {
LOG(ERROR) << "Init kv failed!";
return EXIT_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion src/daemons/StorageDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

gStorageServer = std::make_unique<nebula::storage::StorageServer>(hostRet.value(),
gStorageServer = std::make_unique<nebula::storage::StorageServer>(hostRet.value()[0],
metaAddrsRet.value(),
paths);
if (!gStorageServer->start()) {
Expand Down
2 changes: 1 addition & 1 deletion src/graph/test/TestEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void TestEnv::SetUp() {
if (!hostRet.ok()) {
LOG(ERROR) << "Bad local host addr, status:" << hostRet.status();
}
auto& localhost = hostRet.value();
auto& localhost = hostRet.value()[0];

mClient_ = std::make_unique<meta::MetaClient>(threadPool,
std::move(addrsRet.value()),
Expand Down
2 changes: 1 addition & 1 deletion src/storage/test/StorageClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST(StorageClientTest, VerticesInterfacesTest) {
auto& addrs = addrsRet.value();
uint32_t localDataPort = network::NetworkUtils::getAvailablePort();
auto hostRet = nebula::network::NetworkUtils::toHostAddr("127.0.0.1", localDataPort);
auto& localHost = hostRet.value();
auto& localHost = hostRet.value()[0];
auto mClient = std::make_unique<meta::MetaClient>(threadPool,
std::move(addrs),
localHost,
Expand Down