From 83101679a0f813fe7d1fb4ee83c5b4cf64213380 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Jun 2024 16:12:34 +0400 Subject: [PATCH 1/4] Backward compatible overload of getBestPublicIp() --- include/tools.h | 14 ++++++++++---- src/tools/networkTools.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/tools.h b/include/tools.h index 8ac60312e..a743b25f3 100644 --- a/include/tools.h +++ b/include/tools.h @@ -231,6 +231,12 @@ std::map getNetworkInterfaces(); */ std::string getBestPublicIp(bool ipv6); +/** Provides the best IPv4 adddress + * Equivalent to getBestPublicIp(false). Provided for backward compatibility + * with libkiwix v13.1.0. + */ +std::string getBestPublicIp(); + /** Converts file size to human readable format. * * This function will convert a number to its equivalent size using units. @@ -242,15 +248,15 @@ std::string beautifyFileSize(uint64_t number); /** * Load languages stored in an OPDS stream. - * + * * @param content the OPDS stream. - * @return vector containing pairs of language code and their corresponding full language name. + * @return vector containing pairs of language code and their corresponding full language name. */ FeedLanguages readLanguagesFromFeed(const std::string& content); /** * Load categories stored in an OPDS stream . - * + * * @param content the OPDS stream. * @return vector containing category strings. */ @@ -258,7 +264,7 @@ FeedCategories readCategoriesFromFeed(const std::string& content); /** * Retrieve the full language name associated with a given ISO 639-3 language code. - * + * * @param lang ISO 639-3 language code. * @return full language name. */ diff --git a/src/tools/networkTools.cpp b/src/tools/networkTools.cpp index c1b61335f..ca106d8b1 100644 --- a/src/tools/networkTools.cpp +++ b/src/tools/networkTools.cpp @@ -213,3 +213,9 @@ std::string kiwix::getBestPublicIp(bool ipv6) { } return ipv6 ? bestPublicIp.addr6 : bestPublicIp.addr; } + + +std::string kiwix::getBestPublicIp() +{ + return getBestPublicIp(false); +} From 135c6f875dae6e4ceaad8a0e09d417edc8553cbb Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Jun 2024 16:22:34 +0400 Subject: [PATCH 2/4] Hid some symbols in unnamed namespace --- src/tools/networkTools.cpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/tools/networkTools.cpp b/src/tools/networkTools.cpp index ca106d8b1..62c568a30 100644 --- a/src/tools/networkTools.cpp +++ b/src/tools/networkTools.cpp @@ -49,6 +49,12 @@ #include #endif +namespace kiwix +{ + +namespace +{ + size_t write_callback_to_iss(char* ptr, size_t size, size_t nmemb, void* userdata) { auto str = static_cast(userdata); @@ -56,7 +62,9 @@ size_t write_callback_to_iss(char* ptr, size_t size, size_t nmemb, void* userdat return nmemb; } -std::string kiwix::download(const std::string& url) { +} // unnamed namespace + +std::string download(const std::string& url) { auto curl = curl_easy_init(); std::stringstream ss; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); @@ -77,10 +85,14 @@ std::string kiwix::download(const std::string& url) { return ss.str(); } + +namespace +{ + #ifdef _WIN32 -std::map getNetworkInterfacesWin() { - std::map interfaces; +std::map getNetworkInterfacesWin() { + std::map interfaces; const int working_buffer_size = 15000; const int max_tries = 3; @@ -145,8 +157,8 @@ std::map getNetworkInterfacesWin() { #else -std::map getNetworkInterfacesPosix() { - std::map interfaces; +std::map getNetworkInterfacesPosix() { + std::map interfaces; struct ifaddrs *interfacesHead; if (getifaddrs(&interfacesHead) == -1) { @@ -177,7 +189,9 @@ std::map getNetworkInterfacesPosix() { #endif -std::map kiwix::getNetworkInterfaces() { +} // unnamed namespace + +std::map getNetworkInterfaces() { #ifdef _WIN32 return getNetworkInterfacesWin(); #else @@ -185,9 +199,9 @@ std::map kiwix::getNetworkInterfaces() { #endif } -std::string kiwix::getBestPublicIp(bool ipv6) { - kiwix::IpAddress bestPublicIp = kiwix::IpAddress{"127.0.0.1","::1"}; - std::map interfaces = getNetworkInterfaces(); +std::string getBestPublicIp(bool ipv6) { + IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"}; + std::map interfaces = getNetworkInterfaces(); #ifndef _WIN32 const char* const prioritizedNames[] = @@ -215,7 +229,9 @@ std::string kiwix::getBestPublicIp(bool ipv6) { } -std::string kiwix::getBestPublicIp() +std::string getBestPublicIp() { return getBestPublicIp(false); } + +} // namespace kiwix From 5927550a36b1367594a0c53b45f5a738b8fbf7f4 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Jun 2024 16:56:00 +0400 Subject: [PATCH 3/4] 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[] = From 75bddbf725306404095a386a261a6ce34ad0a089 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Jun 2024 17:02:33 +0400 Subject: [PATCH 4/4] "Unittests" for getBestPublicIp() & getNetworkInterfaces() The unit-tests only call the said functions and print their output which should then be examined by the maintainer. --- test/otherTools.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/otherTools.cpp b/test/otherTools.cpp index 3a3eb0477..1e38bbb1e 100644 --- a/test/otherTools.cpp +++ b/test/otherTools.cpp @@ -233,3 +233,30 @@ TEST(I18n, parseUserLanguagePreferences) "{fr, 1}{en, 0.5}" ); } + +#include "../include/tools.h" + +TEST(networkTools, getNetworkInterfacesIPv4Or6) +{ + for ( const auto& kv : kiwix::getNetworkInterfacesIPv4Or6() ) { + std::cout << kv.first << " : IPv4 addr = " << kv.second.addr + << " ; IPv6 addr = " << kv.second.addr6 + << std::endl; + } +} + +TEST(networkTools, getNetworkInterfaces) +{ + for ( const auto& kv : kiwix::getNetworkInterfaces() ) { + std::cout << kv.first << " : IPv4 addr = " << kv.second << std::endl; + } +} + +TEST(networkTools, getBestPublicIp) +{ + using kiwix::getBestPublicIp; + + std::cout << "getBestPublicIp(true) " << getBestPublicIp(true) << std::endl; + std::cout << "getBestPublicIp(false) " << getBestPublicIp(false) << std::endl; + std::cout << "getBestPublicIp() " << getBestPublicIp() << std::endl; +}