mirror of
https://github.com/kiwix/libkiwix.git
synced 2025-09-09 15:18:55 -04:00
Introduce ipAvailable network function
This commit is contained in:
parent
f32ab92e85
commit
142bf834f0
@ -231,6 +231,15 @@ IpAddress getBestPublicIps(IpMode mode);
|
|||||||
*/
|
*/
|
||||||
std::string getBestPublicIp();
|
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.
|
/** Converts file size to human readable format.
|
||||||
*
|
*
|
||||||
* This function will convert a number to its equivalent size using units.
|
* This function will convert a number to its equivalent size using units.
|
||||||
|
@ -476,6 +476,11 @@ bool InternalServer::start() {
|
|||||||
std::cerr << "IP address " << address << " is not a valid IP address" << std::endl;
|
std::cerr << "IP address " << address << " is not a valid IP address" << std::endl;
|
||||||
return false;
|
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) {
|
if (m_ipMode == IpMode::all) {
|
||||||
|
@ -243,4 +243,16 @@ std::string getBestPublicIp()
|
|||||||
return getBestPublicIps(IpMode::ipv4).addr;
|
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
|
} // namespace kiwix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user