Skip to content

Commit 8099deb

Browse files
trondndaverigby
authored andcommitted
[Refactor] use cb::net::getIpAddresses()
Moved the implementation to cb::net and allow for getting the loopback addresses so that it may be used from other test programs Change-Id: If184bfbb47d9a73be02d0042419546c25e605191 Reviewed-on: http://review.couchbase.org/c/kv_engine/+/141230 Tested-by: Build Bot <[email protected]> Reviewed-by: Dave Rigby <[email protected]>
1 parent 8518d9b commit 8099deb

File tree

3 files changed

+2
-125
lines changed

3 files changed

+2
-125
lines changed

cluster_framework/bucket.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Bucket::Bucket(const Cluster& cluster,
7979

8080
};
8181

82-
auto [ipv4, ipv6] = cb::test::Cluster::getIpAddresses();
82+
auto [ipv4, ipv6] = cb::net::getIpAddresses(true);
8383
(void)ipv6; // currently not used
8484
const auto& hostname = ipv4.front();
8585
cluster.iterateNodes([this, &hostname](const cb::test::Node& node) {

cluster_framework/cluster.cc

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,9 @@
1919
#include "bucket.h"
2020
#include "node.h"
2121

22-
#ifdef WIN32
23-
#include <winsock2.h>
24-
25-
#include <iphlpapi.h>
26-
#pragma comment(lib, "IPHLPAPI.lib")
27-
#else
28-
#include <arpa/inet.h>
29-
#include <ifaddrs.h>
30-
#include <netinet/in.h>
31-
#endif
32-
3322
#include <platform/dirutils.h>
3423
#include <platform/uuid.h>
3524
#include <protocol/connection/client_mcbp_commands.h>
36-
#include <array>
3725
#include <iostream>
3826
#include <string>
3927
#include <system_error>
@@ -55,7 +43,7 @@ class ClusterImpl : public Cluster {
5543
manifest = {{"rev", 0},
5644
{"clusterCapabilities", nlohmann::json::object()},
5745
{"clusterCapabilitiesVer", {1, 0}}};
58-
auto [ipv4, ipv6] = cb::test::Cluster::getIpAddresses();
46+
auto [ipv4, ipv6] = cb::net::getIpAddresses(true);
5947
(void)ipv6; // currently not used
6048
const auto& hostname = ipv4.front();
6149
for (const auto& n : nodes) {
@@ -409,104 +397,4 @@ nlohmann::json Cluster::getUninitializedJson() {
409397
return ret;
410398
}
411399

412-
std::pair<std::vector<std::string>, std::vector<std::string>>
413-
Cluster::getIpAddresses() {
414-
std::array<char, 1024> buffer;
415-
std::pair<std::vector<std::string>, std::vector<std::string>> ret;
416-
417-
#ifdef WIN32
418-
std::vector<char> blob(1024 * 1024);
419-
auto* addresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(blob.data());
420-
ULONG dataSize = blob.size();
421-
auto rw = GetAdaptersAddresses(
422-
AF_UNSPEC,
423-
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
424-
GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
425-
nullptr,
426-
addresses,
427-
&dataSize);
428-
if (rw != ERROR_SUCCESS) {
429-
throw std::system_error(
430-
rw, std::system_category(), "GetAdaptersAddresses()");
431-
}
432-
for (auto* iff = addresses; iff != nullptr; iff = iff->Next) {
433-
for (auto* addr = iff->FirstUnicastAddress; addr != nullptr;
434-
addr = addr->Next) {
435-
auto family = addr->Address.lpSockaddr->sa_family;
436-
if (family == AF_INET) {
437-
inet_ntop(AF_INET,
438-
&reinterpret_cast<sockaddr_in*>(
439-
addr->Address.lpSockaddr)
440-
->sin_addr,
441-
buffer.data(),
442-
buffer.size());
443-
std::string address(buffer.data());
444-
if (address != "127.0.0.1") {
445-
// Ignore localhost address
446-
ret.first.emplace_back(std::move(address));
447-
}
448-
} else if (family == AF_INET6) {
449-
inet_ntop(AF_INET6,
450-
&reinterpret_cast<sockaddr_in6*>(
451-
addr->Address.lpSockaddr)
452-
->sin6_addr,
453-
buffer.data(),
454-
buffer.size());
455-
std::string address(buffer.data());
456-
if (address != "::1") {
457-
// Ignore localhost address
458-
ret.second.emplace_back(std::move(address));
459-
}
460-
} else {
461-
// Skip all other types of addresses
462-
continue;
463-
}
464-
}
465-
}
466-
#else
467-
ifaddrs* interfaces;
468-
if (getifaddrs(&interfaces) != 0) {
469-
throw std::system_error(errno, std::system_category(), "getifaddrs()");
470-
}
471-
472-
if (interfaces == nullptr) {
473-
return {};
474-
}
475-
476-
// Iterate over all the interfaces and pick out the IPv4 and IPv6 addresses
477-
for (auto* ifa = interfaces; ifa != nullptr; ifa = ifa->ifa_next) {
478-
if (!ifa->ifa_addr) {
479-
continue;
480-
}
481-
482-
if (ifa->ifa_addr->sa_family == AF_INET) {
483-
inet_ntop(AF_INET,
484-
&reinterpret_cast<sockaddr_in*>(ifa->ifa_addr)->sin_addr,
485-
buffer.data(),
486-
buffer.size());
487-
std::string address(buffer.data());
488-
if (address != "127.0.0.1") {
489-
// Ignore localhost address
490-
ret.first.emplace_back(std::move(address));
491-
}
492-
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
493-
inet_ntop(
494-
AF_INET6,
495-
&reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr)->sin6_addr,
496-
buffer.data(),
497-
buffer.size());
498-
std::string address(buffer.data());
499-
if (address != "::1") {
500-
// Ignore localhost address
501-
ret.second.emplace_back(std::move(address));
502-
}
503-
}
504-
}
505-
506-
freeifaddrs(interfaces);
507-
#endif
508-
509-
return ret;
510-
}
511-
512400
} // namespace cb::test

cluster_framework/cluster.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,6 @@ class Cluster {
122122
static std::unique_ptr<Cluster> create(
123123
size_t nodes, std::optional<std::string> directory = {});
124124

125-
/**
126-
* Get all IPv4 and IPv6 addresses configured on this machine (skipping
127-
* loopback interface such as 127.0.0.1 and ::1)
128-
*
129-
* @return the first vector contains IPv4 addresses, the second Iv6
130-
* addresses
131-
* @throws std::system_error if we fail to get the interface description
132-
*/
133-
static std::pair<std::vector<std::string>, std::vector<std::string>>
134-
getIpAddresses();
135-
136125
/// Get a JSON representation for the cluster before the
137126
/// cluster is initialized
138127
static nlohmann::json getUninitializedJson();

0 commit comments

Comments
 (0)