Skip to content

Commit 901d43f

Browse files
benoit-nexthopmeta-codesync[bot]
authored andcommitted
enable fboss2 CLI tests in the OSS build
Summary: **Pre-submission checklist** - [x] I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running `pip install -r requirements-dev.txt && pre-commit install` - [x] `pre-commit run` The `fboss2` CLI has a number of unit tests, however none of them are being built/run in the OSS build. Turn the CLI into a library in the cmake build, just like how it’s done in the Buck build, and add a new cmake file for the unit tests that leverages this library. Various unit tests were including `nettools/common/TestUtils.h`, which is not open source, however they generally didn’t need that header and only needed a header from thrift from the most part. Some simple stubs were incomplete. The BGP server is taken out when building with `IS_OSS`. This change tries to keep the OSS build as close as possible to the internal build, with minimal usage of `#ifdef`s. X-link: facebook/fboss#628 Reviewed By: shiva-menta Differential Revision: D89410954 Pulled By: joseph5wu fbshipit-source-id: ee163fdba31b2a0d6768cd927681daba27d4c303
1 parent efd871e commit 901d43f

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

common/network/NetworkUtil.h

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,89 @@
1111
#ifndef COMMON_NETWORKUTIL_H
1212
#define COMMON_NETWORKUTIL_H 1
1313

14+
#include <arpa/inet.h>
15+
#include <netdb.h>
16+
#include <sys/socket.h>
17+
#include <cstring>
18+
#include <string>
19+
1420
namespace facebook {
1521
namespace network {
1622

17-
/* Stub util for OSS build */
23+
/* Network utility functions for OSS build */
1824
class NetworkUtil {
1925
public:
26+
// Resolve hostname to IP address
2027
static std::string getHostByName(
2128
const std::string& host,
2229
bool disableIpv6 = false) {
23-
return "";
30+
struct addrinfo hints, *result = nullptr;
31+
std::memset(&hints, 0, sizeof(hints));
32+
hints.ai_family = disableIpv6 ? AF_INET : AF_UNSPEC;
33+
hints.ai_socktype = SOCK_STREAM;
34+
35+
int status = getaddrinfo(host.c_str(), nullptr, &hints, &result);
36+
if (status != 0 || result == nullptr) {
37+
return "";
38+
}
39+
40+
char ipstr[INET6_ADDRSTRLEN];
41+
void* addr = nullptr;
42+
43+
if (result->ai_family == AF_INET) {
44+
struct sockaddr_in* ipv4 = (struct sockaddr_in*)result->ai_addr;
45+
addr = &(ipv4->sin_addr);
46+
} else if (result->ai_family == AF_INET6) {
47+
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)result->ai_addr;
48+
addr = &(ipv6->sin6_addr);
49+
}
50+
51+
std::string ipAddress;
52+
if (addr != nullptr &&
53+
inet_ntop(result->ai_family, addr, ipstr, sizeof(ipstr)) != nullptr) {
54+
ipAddress = ipstr;
55+
}
56+
57+
freeaddrinfo(result);
58+
return ipAddress;
2459
}
60+
61+
// Reverse DNS lookup: IP address to hostname
2562
static std::string getHostByAddr(const std::string& ip) {
26-
return "";
63+
struct sockaddr_in sa4;
64+
struct sockaddr_in6 sa6;
65+
struct sockaddr* sa = nullptr;
66+
socklen_t salen = 0;
67+
68+
// Try IPv4 first
69+
if (inet_pton(AF_INET, ip.c_str(), &(sa4.sin_addr)) == 1) {
70+
sa4.sin_family = AF_INET;
71+
sa = (struct sockaddr*)&sa4;
72+
salen = sizeof(sa4);
73+
} else if (inet_pton(AF_INET6, ip.c_str(), &(sa6.sin6_addr)) == 1) {
74+
sa6.sin6_family = AF_INET6;
75+
sa = (struct sockaddr*)&sa6;
76+
salen = sizeof(sa6);
77+
} else {
78+
// Invalid IP address
79+
return "";
80+
}
81+
82+
char hostname[NI_MAXHOST];
83+
// Use NI_NAMEREQD to ensure we get a name (not just the IP back)
84+
// and default flags to get FQDN
85+
int status = getnameinfo(
86+
sa, salen, hostname, sizeof(hostname), nullptr, 0, NI_NAMEREQD);
87+
if (status != 0) {
88+
// If NI_NAMEREQD fails, try without it (will return IP if no name found)
89+
status =
90+
getnameinfo(sa, salen, hostname, sizeof(hostname), nullptr, 0, 0);
91+
if (status != 0) {
92+
return "";
93+
}
94+
}
95+
96+
return std::string(hostname);
2797
}
2898
};
2999
} // namespace network

0 commit comments

Comments
 (0)