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
0 commit comments