kiwix::getNetworkInterfacesIPv4Or6()

- Restored kiwix::getNetworkInterfaces() API to the version before
  support for IPv6 was introduced

- Renamed the new API method to kiwix::getNetworkInterfacesIPv4Or6()
This commit is contained in:
Veloman Yunkan 2024-06-03 16:56:00 +04:00 committed by Kelson
parent 135c6f875d
commit 5927550a36
2 changed files with 34 additions and 8 deletions

View File

@ -25,10 +25,13 @@
#include <map> #include <map>
#include <cstdint> #include <cstdint>
namespace kiwix { namespace kiwix
struct IpAddress{ {
std::string addr;
std::string addr6; struct IpAddress
{
std::string addr; // IPv4 address
std::string addr6; // IPv6 address
}; };
typedef std::pair<std::string, std::string> LangNameCodePair; typedef std::pair<std::string, std::string> LangNameCodePair;
@ -223,11 +226,21 @@ std::string getMimeTypeForFile(const std::string& filename);
/** Provides all available network interfaces /** Provides all available network interfaces
* *
* This function provides the available IPv4 and IPv6 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<std::string,IpAddress> getNetworkInterfaces(); std::map<std::string, IpAddress> 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<std::string, std::string> getNetworkInterfaces();
/** Provides the best IP address /** 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); std::string getBestPublicIp(bool ipv6);

View File

@ -191,7 +191,7 @@ std::map<std::string, IpAddress> getNetworkInterfacesPosix() {
} // unnamed namespace } // unnamed namespace
std::map<std::string, IpAddress> getNetworkInterfaces() { std::map<std::string, IpAddress> getNetworkInterfacesIPv4Or6() {
#ifdef _WIN32 #ifdef _WIN32
return getNetworkInterfacesWin(); return getNetworkInterfacesWin();
#else #else
@ -199,9 +199,22 @@ std::map<std::string, IpAddress> getNetworkInterfaces() {
#endif #endif
} }
std::map<std::string, std::string> getNetworkInterfaces() {
std::map<std::string, std::string> 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) { std::string getBestPublicIp(bool ipv6) {
IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"}; IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"};
std::map<std::string, IpAddress> interfaces = getNetworkInterfaces(); std::map<std::string, IpAddress> interfaces = getNetworkInterfacesIPv4Or6();
#ifndef _WIN32 #ifndef _WIN32
const char* const prioritizedNames[] = const char* const prioritizedNames[] =