diff --git a/include/tools.h b/include/tools.h index 57e79c34..11cd219d 100644 --- a/include/tools.h +++ b/include/tools.h @@ -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. diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 6db6ae1a..c333c509 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -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) { diff --git a/src/tools/networkTools.cpp b/src/tools/networkTools.cpp index b37488aa..ba58fd27 100644 --- a/src/tools/networkTools.cpp +++ b/src/tools/networkTools.cpp @@ -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