-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor getBestPublicIp for all valid ips #1132
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,8 +462,8 @@ | |
sockAddr6.sin6_port = htons(m_port); | ||
|
||
if (m_addr.addr.empty() && m_addr.addr6.empty()) { // No specific ip address provided | ||
sockAddr6.sin6_addr = in6addr_any; | ||
sockAddr4.sin_addr.s_addr = htonl(INADDR_ANY); | ||
m_addr = kiwix::getBestPublicIps(m_ipMode); | ||
} else { | ||
bool ipv6 = inet_pton(AF_INET6, m_addr.addr6.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1; | ||
|
@@ -472,8 +472,13 @@ | |
if (!ipv4 && !ipv6) { | ||
const std::string& addr = m_addr.addr.empty() ? m_addr.addr6 : m_addr.addr; | ||
std::cerr << "IP address " << addr << " is not valid" << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please restore the previous text of the error message |
||
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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,39 +211,51 @@ | |
return result; | ||
} | ||
|
||
IpAddress getBestPublicIps(IpMode mode) { | ||
IpAddress bestPublicIps = IpAddress{"127.0.0.1", "::1"}; | ||
std::map<std::string, IpAddress> interfaces = getNetworkInterfacesIPv4Or6(); | ||
bool validMode4 = (mode == IpMode::ipv4 || mode == IpMode::all); // Mode is valid to support ipv4 | ||
bool validMode6 = (mode == IpMode::ipv6 || mode == IpMode::all); // Mode is valid to support ipv6 | ||
#ifndef _WIN32 | ||
const char* const prioritizedNames[] = { "eth0", "eth1", "wlan0", "wlan1", "en0", "en1" }; | ||
for(const auto& name: prioritizedNames) { | ||
const auto it = interfaces.find(name); | ||
if(it == interfaces.end()) continue; | ||
const IpAddress& interfaceIps = it->second; | ||
if(!bestPublicIps.addr.empty() && validMode4 && !interfaceIps.addr.empty()) | ||
bestPublicIps.addr = interfaceIps.addr; | ||
if(!bestPublicIps.addr6.empty() && validMode6 && !interfaceIps.addr6.empty()) | ||
bestPublicIps.addr6 = interfaceIps.addr6; | ||
} | ||
#endif | ||
const char* const prefixes[] = { "192.168", "172.16.", "10.0" }; | ||
for (const auto& prefix : prefixes) { | ||
for (const auto& [_, interfaceIps] : interfaces) { | ||
if (!bestPublicIps.addr.empty() && validMode4 && !interfaceIps.addr.empty() && interfaceIps.addr.find(prefix) == 0) | ||
bestPublicIps.addr = interfaceIps.addr; | ||
if (!bestPublicIps.addr6.empty() && validMode6 && !interfaceIps.addr6.empty()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the reason it is omitted is because we only provide v4 prefixes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be beneficial to provide some for v6? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The purpose of this piece of code is to select an IP address from a local network, though I am not sure why the
According to https://en.wikipedia.org/wiki/Link-local_address#IPv6
however, unfortunately, it seems like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok so if I understand correctly, for this point, I should just add |
||
bestPublicIps.addr6 = interfaceIps.addr6; | ||
} | ||
} | ||
|
||
return bestPublicIps; | ||
} | ||
|
||
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.addr.empty() && interfaceIps.addr == addr.addr) return true; | ||
if (!interfaceIps.addr6.empty() && interfaceIps.addr6 == addr.addr6) return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} // namespace kiwix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IpAddress
can be a pair of addresses (one IPv4 and the other IPv6). How is this function supposed to work if both components are non-empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be a useful function to have available
Current use case was to check only one ip (in the case of the user giving one). I will refactor it to take as a parameter the mode as well and check accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When dealing with public API that's not a good argument. Public API is a commitment. Exposing something just because it's useful may lead to unjustified maintenance overhead (like having to deal with backward compatibility issues). Let's not bloat our public API without necessity.