Skip to content

Commit

Permalink
Add ip availability check in server start
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Sep 24, 2024
1 parent 443ca52 commit 31ef041
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ IpAddress getBestPublicIps(IpMode mode);
*/
std::string getBestPublicIp();

/** Checks if IP address is available
*
* Check if the given IP address is configured on any network interface of the system
*
* @param addr the IP address to check.
* @return true if the IP address is available on the system.
*/
bool ipAvailable(const IpAddress& addr);

/** Converts file size to human readable format.
*
* This function will convert a number to its equivalent size using units.
Expand Down
5 changes: 5 additions & 0 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ bool InternalServer::start() {
std::cerr << "IP address " << addr << " is not valid" << std::endl;
return false;
}

if (!ipAvailable(m_addr)) {
std::cerr << "IP address " << (m_addr.addr.empty() ? m_addr.addr6 : m_addr.addr) << " is not available on this system" << std::endl;
return false;
}
}

if (m_ipMode == IpMode::ALL) {
Expand Down
12 changes: 12 additions & 0 deletions src/tools/networkTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,16 @@ std::string getBestPublicIp()
return getBestPublicIps(IpMode::ipv4).addr;
}

bool ipAvailable(const IpAddress& addr)
{
auto interfaces = kiwix::getNetworkInterfacesIPv4Or6();
for (const auto& interface : interfaces) {
IpAddress interfaceIps = interface.second;
if (!interfaceIps.addr.empty() && interfaceIps.addr == addr.addr) return true;
if (!interfaceIps.addr6.empty() && interfaceIps.addr6 == addr.addr6) return true;
}

return false;
}

} // namespace kiwix

0 comments on commit 31ef041

Please sign in to comment.