From d71a005ce9183d67e45a9e6012042faa7f006e64 Mon Sep 17 00:00:00 2001 From: sgourdas Date: Mon, 23 Sep 2024 19:18:57 +0300 Subject: [PATCH] Add ip availability check in server start --- src/server/internalServer.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index e51d2ed4d..2ecdb7ff7 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -85,6 +85,16 @@ namespace kiwix { namespace { +bool ipAvailable(const std::string addr) +{ + auto interfaces = kiwix::getNetworkInterfacesIPv4Or6(); + + for (const auto& [_, interfaceIps] : interfaces) + if ((interfaceIps.addr == addr) || (interfaceIps.addr6 == addr)) return true; + + return false; +} + inline std::string normalizeRootUrl(std::string rootUrl) { while ( !rootUrl.empty() && rootUrl.back() == '/' ) @@ -483,6 +493,10 @@ bool InternalServer::start() { std::cerr << "IP address " << addr << " is not a valid ip address." << std::endl; return false; } + if (!ipAvailable(addr)) { + std::cerr << "IP address " << addr << " is not available on this system." << std::endl; + return false; + } m_ipMode = !m_addr.addr.empty() ? IpMode::IPV4 : IpMode::IPV6; }