Skip to content

Commit

Permalink
Refactor getBestPublicIp for both protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Sep 19, 2024
1 parent 766816b commit 97ad823
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 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);
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
9 changes: 8 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 All @@ -32,6 +33,12 @@ struct IpAddress
{
std::string addr; // IPv4 address
std::string addr6; // IPv6 address

bool empty() const { return addr.empty() && addr6.empty(); }
bool empty4() const { return addr.empty(); }
bool empty6() const { return addr6.empty(); }
bool valid4(IpMode mode) const { return (mode == IpMode::ipv4 || mode == IpMode::all) && !this->empty4(); }
bool valid6(IpMode mode) const { return (mode == IpMode::ipv6 || mode == IpMode::all) && !this->empty6(); }
};

typedef std::pair<std::string, std::string> LangNameCodePair;
Expand Down Expand Up @@ -216,7 +223,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(IpMode mode);

/** Provides the best IPv4 adddress
* Equivalent to getBestPublicIp(false). Provided for backward compatibility
Expand Down
15 changes: 14 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,25 @@ void Server::setRoot(const std::string& root)
}
}

void Server::setAddress(const std::string& addr)
{
if (addr.empty())
return;

Check warning on line 81 in src/server.cpp

View check run for this annotation

Codecov / codecov/patch

src/server.cpp#L81

Added line #L81 was not covered by tests

if (addr.find(':') != std::string::npos) { // IPv6
for (char ch : addr) if (ch == '[' or ch == ']') ch = '\0'; // Clean possible brackets
m_addr.addr6 = addr;

Check warning on line 85 in src/server.cpp

View check run for this annotation

Codecov / codecov/patch

src/server.cpp#L85

Added line #L85 was not covered by tests
} else {
m_addr.addr = addr;
}
}

int Server::getPort() const

Check warning on line 91 in src/server.cpp

View check run for this annotation

Codecov / codecov/patch

src/server.cpp#L91

Added line #L91 was not covered by tests
{
return mp_server->getPort();
}

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

Check warning on line 96 in src/server.cpp

View check run for this annotation

Codecov / codecov/patch

src/server.cpp#L96

Added line #L96 was not covered by tests
{
return mp_server->getAddress();
}
Expand Down
23 changes: 13 additions & 10 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 @@ -462,18 +462,21 @@ bool InternalServer::start() {
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);
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(m_ipMode);
} 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.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 473 in src/server/internalServer.cpp

View check run for this annotation

Codecov / codecov/patch

src/server/internalServer.cpp#L473

Added line #L473 was not covered by tests
} else if (!ipv4) {
std::cerr << "Ip address " << m_addr << " is not a valid ip address" << std::endl;
if(!m_addr.addr.empty())
std::cerr << "Ip address " << m_addr.addr << " is not a valid ip address" << std::endl;
else
std::cerr << "Ip address " << m_addr.addr6 << " 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
29 changes: 13 additions & 16 deletions src/tools/networkTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,39 +211,36 @@ std::map<std::string, std::string> getNetworkInterfaces() {
return result;
}


std::string getBestPublicIp(bool ipv6) {
IpAddress bestPublicIp = IpAddress{"127.0.0.1","::1"};
IpAddress getBestPublicIps(IpMode mode) {
IpAddress bestPublicIps = IpAddress{"127.0.0.1","::1"};
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;
}
const auto it = interfaces.find(name);
if(it == interfaces.end()) continue;
const IpAddress& interfaceIps = (*it).second;
if(!bestPublicIps.empty4() && interfaceIps.valid4(mode)) bestPublicIps.addr = interfaceIps.addr;
if(!bestPublicIps.empty6() && interfaceIps.valid6(mode)) bestPublicIps.addr6 = interfaceIps.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;
}
const IpAddress& interfaceIps = itr.second;
if(!bestPublicIps.empty4() && interfaceIps.valid4(mode) && interfaceIps.addr.find(prefix) == 0) bestPublicIps.addr = interfaceIps.addr;
if(!bestPublicIps.empty6() && interfaceIps.valid6(mode)) bestPublicIps.addr6 = interfaceIps.addr6;
}
}
return ipv6 ? bestPublicIp.addr6 : bestPublicIp.addr;
}

return bestPublicIps;
}

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

} // namespace kiwix
12 changes: 8 additions & 4 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,14 @@ TEST(networkTools, getNetworkInterfaces)
}
}

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

std::cout << "getBestPublicIp(true) " << getBestPublicIp(true) << std::endl;
std::cout << "getBestPublicIp(false) " << getBestPublicIp(false) << std::endl;
std::cout << "getBestPublicIp() " << getBestPublicIp() << std::endl;
std::cout << "getBestPublicIps(true) : [" << getBestPublicIps(IpMode::ipv4).addr << ", " << getBestPublicIps(IpMode::ipv4).addr6 << "]" << std::endl;
std::cout << "getBestPublicIps(false) : [" << getBestPublicIps(IpMode::ipv6).addr << ", " << getBestPublicIps(IpMode::ipv6).addr6 << "]" << std::endl;
std::cout << "getBestPublicIps(false) : [" << getBestPublicIps(IpMode::all).addr << ", " << getBestPublicIps(IpMode::all).addr6 << "]" << std::endl;
std::cout << "getBestPublicIp() : " << getBestPublicIp() << std::endl;
}

0 comments on commit 97ad823

Please sign in to comment.