From ec22d26e38624133a6039b2f90389d35d4cd20a1 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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index cee7c194a..06ff30565 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -85,6 +85,19 @@ 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() == '/' ) @@ -484,6 +497,11 @@ bool InternalServer::start() { return false; } + if (!ipAvailable(addr)) { + std::cerr << "ERROR: IP address is not available on this system: " << addr << std::endl; + return false; + } + m_ipMode = !m_addr.addr.empty() ? IpMode::IPV4 : IpMode::IPV6; }