Commit 61411cde authored by Joseph Noir's avatar Joseph Noir

Use resolve for local addr lookup for interop

parent aa96b5d1
......@@ -30,9 +30,6 @@ namespace ip {
/// Returns the ip addresses assigned to `host`.
std::vector<ip_address> resolve(const std::string& host);
/// Returns the ip addresses for the local hostname.
std::vector<ip_address> local_addrs(const std::string& host);
/// Returns the hostname of this device.
std::string hostname();
......
......@@ -60,10 +60,6 @@ namespace ip {
namespace {
constexpr auto v4_any_addr = "0.0.0.0";
constexpr auto v6_any_addr = "::";
constexpr auto localhost = "localhost";
template <class T>
void* vptr(T* ptr) {
return static_cast<void*>(ptr);
......@@ -88,20 +84,6 @@ int fetch_addr_str(bool get_ipv4, bool get_ipv6, char (&buf)[INET6_ADDRSTRLEN],
: AF_UNSPEC;
}
void find_by_name(const vector<pair<string, ip_address>>& interfaces,
const string& name, vector<ip_address>& results) {
for (auto& p : interfaces)
if (p.first == name)
results.push_back(p.second);
}
void find_by_addr(const vector<pair<string, ip_address>>& interfaces,
ip_address addr, vector<ip_address>& results) {
for (auto& p : interfaces)
if (p.second == addr)
results.push_back(p.second);
}
} // namespace
std::vector<ip_address> resolve(const std::string& host) {
......@@ -136,47 +118,17 @@ std::vector<ip_address> resolve(const std::string& host) {
return results;
}
std::vector<ip_address> local_addrs(const std::string& host) {
std::vector<ip_address> results;
// The string is not returned by getifaddrs, let's just do that ourselves.
if (host == localhost)
return {ip_address{{0}, {0x1}},
ipv6_address{make_ipv4_address(127, 0, 0, 1)}};
if (host == v4_any_addr || host == v6_any_addr)
return {ip_address{}};
ifaddrs* tmp = nullptr;
if (getifaddrs(&tmp) != 0)
return {};
std::unique_ptr<ifaddrs, decltype(freeifaddrs)*> addrs{tmp, freeifaddrs};
char buffer[INET6_ADDRSTRLEN];
// Unless explicitly specified we are going to skip link-local addresses.
auto is_link_local = starts_with(host, "fe80:");
std::vector<std::pair<std::string, ip_address>> interfaces;
for (auto i = addrs.get(); i != nullptr; i = i->ifa_next) {
auto family = fetch_addr_str(true, true, buffer, i->ifa_addr);
if (family != AF_UNSPEC) {
ip_address ip;
if (!is_link_local && starts_with(buffer, "fe80:")) {
CAF_LOG_DEBUG("skipping link-local address: " << buffer);
continue;
} else if (auto err = parse(buffer, ip)) {
CAF_LOG_ERROR("could not parse into ip address " << buffer);
continue;
}
interfaces.emplace_back(std::string{i->ifa_name}, ip);
}
}
ip_address host_addr;
if (host.empty() || host == v4_any_addr || host == v6_any_addr)
for (auto& p : interfaces)
results.push_back(std::move(p.second));
else if (auto err = parse(host, host_addr))
find_by_name(interfaces, host, results);
else
find_by_addr(interfaces, host_addr, results);
return results;
#ifdef CAF_WINDOWS
std::string hostname() {
TCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = MAX_COMPUTERNAME_LENGTH;
GetComputerName(buf, &size);
return std::string{buf};
}
#else // CAF_WINDOWS
std::string hostname() {
char buf[HOST_NAME_MAX + 1];
buf[HOST_NAME_MAX] = '\0';
......@@ -185,6 +137,8 @@ std::string hostname() {
return std::string{buf};
}
#endif // CAF_WINDOWS
} // namespace ip
} // namespace net
} // namespace caf
......@@ -116,7 +116,7 @@ expected<tcp_accept_socket> make_accept_socket(const uri::authority_type& auth,
if (auto ip = get_if<ip_address>(&auth.host))
return make_accept_socket(*ip, auth.port, reuse_addr);
auto host = get<std::string>(auth.host);
auto addrs = ip::local_addrs(host);
auto addrs = ip::resolve(host);
if (addrs.empty())
return make_error(sec::cannot_open_port, "No local interface available",
to_string(auth));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment