Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanA101a committed Apr 23, 2024
1 parent bd17e90 commit 13c5dbd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 37 deletions.
1 change: 1 addition & 0 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace kiwix
void setIPv6(bool ipv6) { m_ipv6 = ipv6; }
int getPort();
std::string getAddress();
bool isAddressIPv6();

protected:
std::shared_ptr<Library> mp_library;
Expand Down
14 changes: 13 additions & 1 deletion include/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ bool fileReadable(const std::string& path);
*/
std::string getMimeTypeForFile(const std::string& filename);

/** Provides all available network interfaces on Windows
*
* This function provides the available IPv4 and IPv6 network interfaces
*/
std::map<std::string,ip_addr> getNetworkInterfacesWin();

/** Provides all available network interfaces on Posix
*
* This function provides the available IPv4 and IPv6 network interfaces
*/
std::map<std::string,ip_addr> getNetworkInterfacesPosix();

/** Provides all available network interfaces
*
* This function provides the available IPv4 and IPv6 network interfaces
Expand All @@ -223,7 +235,7 @@ std::map<std::string,ip_addr> getNetworkInterfaces();
/** Provides the best IP address
* This function provides the best IP address from the list given by getNetworkInterfaces
*/
ip_addr getBestPublicIp(bool ipv6);
std::string getBestPublicIp(bool ipv6);

/** Converts file size to human readable format.
*
Expand Down
5 changes: 5 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ std::string Server::getAddress()
return mp_server->getAddress();
}

bool Server::isAddressIPv6()
{
return mp_server->isAddressIPv6();
}

}
49 changes: 20 additions & 29 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,40 +455,31 @@ bool InternalServer::start() {


struct sockaddr_in sockAddr4={0};
sockAddr4.sin_family = AF_INET;
sockAddr4.sin_port = htons(m_port);
struct sockaddr_in6 sockAddr6={0};
sockAddr6.sin6_family = AF_INET6;
sockAddr6.sin6_port = htons(m_port);


if (m_ipv6) {
flags|=MHD_USE_DUAL_STACK;
sockAddr6.sin6_family = AF_INET6;
sockAddr6.sin6_port = htons(m_port);
if (m_addr.empty()) {
if (0 != INADDR_ANY) {
sockAddr6.sin6_addr = in6addr_any;
}
m_addr = (kiwix::getBestPublicIp(m_ipv6)).addr6;
} else {
if (inet_pton(AF_INET6, m_addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 0) {
std::cerr << "Ip address " << m_addr << " is not a valid ipv6 address" << std::endl;
return false;
}
}
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_ipv6);
} else if (inet_pton(AF_INET6, m_addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1 ) {
m_ipv6 = true;
} else if (inet_pton(AF_INET, m_addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 1 && m_ipv6) {
std::cerr << "Ip address " << m_addr << " is not a valid ipv6 address" << std::endl;
return false;
} else {
sockAddr4.sin_family = AF_INET;
sockAddr4.sin_port = htons(m_port);
if (m_addr.empty()) {
if (0 != INADDR_ANY) {
sockAddr4.sin_addr.s_addr = htonl(INADDR_ANY);
}
m_addr = (kiwix::getBestPublicIp(m_ipv6)).addr;
} else{
if (inet_pton(AF_INET, m_addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 0){
std::cerr << "Ip address " << m_addr << " is not a valid ipv4 address" << std::endl;
return false;
}
}
std::cerr << "Ip address " << m_addr << " is not a valid ip address" << std::endl;
return false;
}

if (m_ipv6)
flags|=MHD_USE_DUAL_STACK;

mp_daemon = MHD_start_daemon(flags,
m_port,
NULL,
Expand Down
1 change: 1 addition & 0 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class InternalServer {
void stop();
std::string getAddress() { return m_addr; }
int getPort() { return m_port; }
bool isAddressIPv6() { return m_ipv6; }

private: // functions
std::unique_ptr<Response> handle_request(const RequestContext& request);
Expand Down
37 changes: 30 additions & 7 deletions src/tools/networkTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ std::string kiwix::download(const std::string& url) {
return ss.str();
}

std::map<std::string,ip_addr> kiwix::getNetworkInterfaces() {
#ifdef _WIN32

std::map<std::string,ip_addr> kiwix::getNetworkInterfacesWin() {
std::map<std::string,ip_addr> interfaces;

#ifdef _WIN32
const int working_buffer_size = 15000;
const int max_tries = 3;

Expand Down Expand Up @@ -141,8 +142,14 @@ std::map<std::string,ip_addr> kiwix::getNetworkInterfaces() {

if (interfacesHead) free(interfacesHead);

return interfaces;
}

#else

std::map<std::string,ip_addr> kiwix::getNetworkInterfacesPosix() {
std::map<std::string,ip_addr> interfaces;

struct ifaddrs *interfacesHead;
if (getifaddrs(&interfacesHead) == -1) {
perror("getifaddrs");
Expand All @@ -166,11 +173,25 @@ std::map<std::string,ip_addr> kiwix::getNetworkInterfaces() {
}

freeifaddrs(interfacesHead);
#endif

return interfaces;
}

ip_addr kiwix::getBestPublicIp(bool ipv6) {
#endif

std::map<std::string,ip_addr> kiwix::getNetworkInterfaces() {
std::map<std::string,ip_addr> interfaces;

#ifdef _WIN32
return getNetworkInterfacesWin();
#else
return getNetworkInterfacesPosix();
#endif

Check notice on line 190 in src/tools/networkTools.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/tools/networkTools.cpp#L190

Redundant blank line at the end of a code block should be deleted. (whitespace/blank_line)
}

std::string kiwix::getBestPublicIp(bool ipv6) {
ip_addr bestPublicIp = ip_addr{"127.0.0.1","::1"};
std::map<std::string,ip_addr> interfaces = getNetworkInterfaces();

#ifndef _WIN32
Expand All @@ -179,7 +200,8 @@ ip_addr kiwix::getBestPublicIp(bool ipv6) {
for(auto name: prioritizedNames) {
auto it=interfaces.find(name);
if(it != interfaces.end() && !(ipv6 && (*it).second.addr6.empty())) {
return (*it).second;
bestPublicIp = (*it).second;
break;
}
}
#endif
Expand All @@ -189,9 +211,10 @@ ip_addr kiwix::getBestPublicIp(bool ipv6) {
for(auto& itr : interfaces) {
std::string interfaceIp(itr.second.addr);
if (interfaceIp.find(prefix) == 0 && !(ipv6 && itr.second.addr6.empty())) {
return itr.second;
bestPublicIp = itr.second;
break;
}
}
}
return ip_addr{"127.0.0.1","::1"};
return ipv6 ? bestPublicIp.addr6 : bestPublicIp.addr;
}

0 comments on commit 13c5dbd

Please sign in to comment.