From 5927550a36b1367594a0c53b45f5a738b8fbf7f4 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Jun 2024 16:56:00 +0400 Subject: [PATCH] kiwix::getNetworkInterfacesIPv4Or6() - Restored kiwix::getNetworkInterfaces() API to the version before support for IPv6 was introduced - Renamed the new API method to kiwix::getNetworkInterfacesIPv4Or6() --- include/tools.h | 25 +++++++++++++++++++------ src/tools/networkTools.cpp | 17 +++++++++++++++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/tools.h b/include/tools.h index a743b25f3..13931e876 100644 --- a/include/tools.h +++ b/include/tools.h @@ -25,10 +25,13 @@ #include #include -namespace kiwix { -struct IpAddress{ - std::string addr; - std::string addr6; +namespace kiwix +{ + +struct IpAddress +{ + std::string addr; // IPv4 address + std::string addr6; // IPv6 address }; typedef std::pair LangNameCodePair; @@ -223,11 +226,21 @@ std::string getMimeTypeForFile(const std::string& filename); /** Provides all available network interfaces * * This function provides the available IPv4 and IPv6 network interfaces + * as a map from the interface name to its IPv4 and/or IPv6 address(es). */ -std::map getNetworkInterfaces(); +std::map getNetworkInterfacesIPv4Or6(); + +/** Provides all available IPv4 network interfaces + * + * This function provides the available IPv4 network interfaces + * as a map from the interface name to its IPv4 address. + * + * Provided for backward compatibility with libkiwix v13.1.0. + */ +std::map getNetworkInterfaces(); /** Provides the best IP address - * This function provides the best IP address from the list given by getNetworkInterfaces + * This function provides the best IP address from the list given by getNetworkInterfacesIPv4Or6() */ std::string getBestPublicIp(bool ipv6); diff --git a/src/tools/networkTools.cpp b/src/tools/networkTools.cpp index 62c568a30..04555f661 100644 --- a/src/tools/networkTools.cpp +++ b/src/tools/networkTools.cpp @@ -191,7 +191,7 @@ std::map getNetworkInterfacesPosix() { } // unnamed namespace -std::map getNetworkInterfaces() { +std::map getNetworkInterfacesIPv4Or6() { #ifdef _WIN32 return getNetworkInterfacesWin(); #else @@ -199,9 +199,22 @@ std::map getNetworkInterfaces() { #endif } +std::map getNetworkInterfaces() { + std::map result; + for ( const auto& kv : getNetworkInterfacesIPv4Or6() ) { + const std::string& interfaceName = kv.first; + const auto& ipAddresses = kv.second; + if ( !ipAddresses.addr.empty() ) { + result[interfaceName] = ipAddresses.addr; + } + } + return result; +} + + std::string getBestPublicIp(bool ipv6) { IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"}; - std::map interfaces = getNetworkInterfaces(); + std::map interfaces = getNetworkInterfacesIPv4Or6(); #ifndef _WIN32 const char* const prioritizedNames[] =