Skip to content

Commit

Permalink
Refactor getBestPublicIp for all valid ips
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Sep 18, 2024
1 parent e32e9e7 commit 654b657
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <string>
#include <memory>
#include "common.h"
#include "tools.h"

namespace kiwix
{
Expand Down Expand Up @@ -52,7 +52,7 @@ namespace kiwix
void stop();

void setRoot(const std::string& root);
void setAddress(const std::string& addr) { m_addr = addr; }
void setAddress(const std::string& addr) { if (!addr.empty()) (addr.find(':') != std::string::npos) ? m_addr.addr6 = addr : m_addr.addr = addr; }
void setPort(int port) { m_port = port; }
void setNbThreads(int threads) { m_nbThreads = threads; }
void setMultiZimSearchLimit(unsigned int limit) { m_multizimSearchLimit = limit; }
Expand All @@ -72,7 +72,7 @@ namespace kiwix
std::shared_ptr<Library> mp_library;
std::shared_ptr<NameMapper> mp_nameMapper;
std::string m_root = "";
std::string m_addr = "";
IpAddress m_addr;
std::string m_indexTemplateString = "";
int m_port = 80;
int m_nbThreads = 1;
Expand Down
3 changes: 2 additions & 1 deletion include/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>
#include <map>
#include <cstdint>
#include "common.h"

namespace kiwix
{
Expand Down Expand Up @@ -216,7 +217,7 @@ std::map<std::string, std::string> getNetworkInterfaces();
/** Provides the best IP address
* This function provides the best IP address from the list given by getNetworkInterfacesIPv4Or6()
*/
std::string getBestPublicIp(bool ipv6);
IpAddress getBestPublicIps();

/** Provides the best IPv4 adddress
* Equivalent to getBestPublicIp(false). Provided for backward compatibility
Expand Down
2 changes: 1 addition & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int Server::getPort() const
return mp_server->getPort();
}

std::string Server::getAddress()
IpAddress Server::getAddress() const

Check warning on line 83 in src/server.cpp

View check run for this annotation

Codecov / codecov/patch

src/server.cpp#L83

Added line #L83 was not covered by tests
{
return mp_server->getAddress();
}
Expand Down
22 changes: 11 additions & 11 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class InternalServer::CustomizedResources : public std::map<std::string, Customi

InternalServer::InternalServer(LibraryPtr library,
std::shared_ptr<NameMapper> nameMapper,
std::string addr,
IpAddress addr,
int port,
std::string root,
int nbThreads,
Expand Down Expand Up @@ -461,19 +461,19 @@ bool InternalServer::start() {
sockAddr6.sin6_family = AF_INET6;
sockAddr6.sin6_port = htons(m_port);

if (m_addr.empty()) {
if (0 != INADDR_ANY) {
sockAddr6.sin6_addr = in6addr_any;
sockAddr4.sin_addr.s_addr = htonl(INADDR_ANY);
}
m_addr = kiwix::getBestPublicIp(m_ipMode == IpMode::ipv6 || m_ipMode == IpMode::all);
if (m_addr.addr.empty() && m_addr.addr6.empty()) { // No specific ip address was provided
sockAddr6.sin6_addr = in6addr_any;
sockAddr4.sin_addr.s_addr = htonl(INADDR_ANY);

Check warning on line 466 in src/server/internalServer.cpp

View check run for this annotation

Codecov / codecov/patch

src/server/internalServer.cpp#L465-L466

Added lines #L465 - L466 were not covered by tests
m_addr = kiwix::getBestPublicIps();
} else {
bool ipv6 = inet_pton(AF_INET6, m_addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1;
bool ipv4 = inet_pton(AF_INET, m_addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 1;
bool ipv6 = inet_pton(AF_INET6, m_addr.addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1;
if (!ipv6) ipv6 = inet_pton(AF_INET6, m_addr.addr6.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1;
bool ipv4 = inet_pton(AF_INET, m_addr.addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 1;

if (ipv6){
m_ipMode = IpMode::all;
m_ipMode = IpMode::ipv6;

Check warning on line 474 in src/server/internalServer.cpp

View check run for this annotation

Codecov / codecov/patch

src/server/internalServer.cpp#L474

Added line #L474 was not covered by tests
} else if (!ipv4) {
std::cerr << "Ip address " << m_addr << " is not a valid ip address" << std::endl;
std::cerr << "Ip address " << m_addr.addr << " is not a valid ip address" << std::endl;
return false;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {

#include "library.h"
#include "name_mapper.h"
#include "tools.h"

#include <zim/search.h>
#include <zim/suggestion.h>
Expand Down Expand Up @@ -94,7 +95,7 @@ class InternalServer {
public:
InternalServer(LibraryPtr library,
std::shared_ptr<NameMapper> nameMapper,
std::string addr,
IpAddress addr,
int port,
std::string root,
int nbThreads,
Expand Down Expand Up @@ -166,7 +167,7 @@ class InternalServer {
typedef ConcurrentCache<std::string, std::shared_ptr<LockableSuggestionSearcher>> SuggestionSearcherCache;

private: // data
std::string m_addr;
IpAddress m_addr;
int m_port;
std::string m_root; // URI-encoded
std::string m_rootPrefixOfDecodedURL; // URI-decoded
Expand Down
27 changes: 14 additions & 13 deletions src/tools/networkTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,39 +212,40 @@ std::map<std::string, std::string> getNetworkInterfaces() {
}


std::string getBestPublicIp(bool ipv6) {
IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"};
IpAddress getBestPublicIps() {
IpAddress bestIp;
std::map<std::string, IpAddress> interfaces = getNetworkInterfacesIPv4Or6();

#ifndef _WIN32
const char* const prioritizedNames[] =
{ "eth0", "eth1", "wlan0", "wlan1", "en0", "en1" };
for(auto name: prioritizedNames) {
auto it=interfaces.find(name);
if(it != interfaces.end() && !(ipv6 && (*it).second.addr6.empty())) {
bestPublicIp = (*it).second;
break;
if (it != interfaces.end()) {
if (it->second.addr.find(':') == std::string::npos) bestIp.addr = it->second.addr;
else bestIp.addr6 = it->second.addr6;
}
}
#endif

const char* const prefixes[] = { "192.168", "172.16.", "10.0" };
for(auto prefix : prefixes){
for(auto& itr : interfaces) {
std::string interfaceIp(itr.second.addr);
if (interfaceIp.find(prefix) == 0 && !(ipv6 && itr.second.addr6.empty())) {
bestPublicIp = itr.second;
break;
}
if (itr.second.addr.find(':') == std::string::npos && itr.second.addr.find(prefix) == 0)
bestIp.addr = itr.second.addr;
if (itr.second.addr6.find(':') != std::string::npos)
bestIp.addr6 = itr.second.addr6;
const auto& interface = itr.second;
std::cout << "v4: " << interface.addr << " v6:" << interface.addr6 << std::endl;
}
}
return ipv6 ? bestPublicIp.addr6 : bestPublicIp.addr;
}

return bestIp;
}

std::string getBestPublicIp()
{
return getBestPublicIp(false);
return getBestPublicIps().addr;

Check warning on line 248 in src/tools/networkTools.cpp

View check run for this annotation

Codecov / codecov/patch

src/tools/networkTools.cpp#L248

Added line #L248 was not covered by tests
}

} // namespace kiwix
10 changes: 5 additions & 5 deletions test/otherTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "gtest/gtest.h"
#include "../include/tools.h"
#include "../src/tools/otherTools.h"
#include "zim/suggestion_iterator.h"
#include "../src/server/i18n_utils.h"
Expand Down Expand Up @@ -252,11 +253,10 @@ TEST(networkTools, getNetworkInterfaces)
}
}

TEST(networkTools, getBestPublicIp)
TEST(networkTools, getBestPublicIps)
{
using kiwix::getBestPublicIp;
using kiwix::getBestPublicIps;

std::cout << "getBestPublicIp(true) " << getBestPublicIp(true) << std::endl;
std::cout << "getBestPublicIp(false) " << getBestPublicIp(false) << std::endl;
std::cout << "getBestPublicIp() " << getBestPublicIp() << std::endl;
kiwix::IpAddress bestIps = getBestPublicIps();
std::cout << "getBestPublicIps(): ("<< bestIps.addr << ", " << bestIps.addr6 << ")" << std::endl;
}

0 comments on commit 654b657

Please sign in to comment.