Introduce ipAvailable network function

This commit is contained in:
sgourdas 2024-09-21 21:09:38 +03:00
parent f32ab92e85
commit 142bf834f0
3 changed files with 26 additions and 0 deletions

View File

@ -231,6 +231,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.

View File

@ -476,6 +476,11 @@ bool InternalServer::start() {
std::cerr << "IP address " << address << " is not a valid IP address" << 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) {

View File

@ -243,4 +243,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.empty4() && interfaceIps.addr == addr.addr) return true;
if (!interfaceIps.empty6() && interfaceIps.addr6 == addr.addr6) return true;
}
return false;
}
} // namespace kiwix