Commit 245be394 authored by Dominik Charousset's avatar Dominik Charousset

Fix handling of INADDR_ANY in tcp_accept_socket

parent dbb68b45
......@@ -81,6 +81,14 @@ public:
return bytes_;
}
// -- factories --------------------------------------------------------------
/// Returns `INADDR_ANY`, i.e., `0.0.0.0`.
static ipv4_address any() noexcept;
/// Returns `INADDR_LOOPBACK`, i.e., `127.0.0.1`.
static ipv4_address loopback() noexcept;
// -- comparison -------------------------------------------------------------
/// Returns a negative number if `*this < other`, zero if `*this == other`
......
......@@ -99,6 +99,14 @@ public:
return half_segments_[0] == 0 && half_segments_[1] == 0;
}
// -- factories --------------------------------------------------------------
/// Returns `INADDR6_ANY`, i.e., `::`.
static ipv6_address any() noexcept;
/// Returns `INADDR6_LOOPBACK`, i.e., `::1`.
static ipv6_address loopback() noexcept;
// -- inspection -------------------------------------------------------------
template <class Inspector>
......
......@@ -63,6 +63,16 @@ bool ipv4_address::is_multicast() const noexcept {
return (bits_ & net_order(0xF0000000)) == net_order(0xE0000000);
}
// -- factories ----------------------------------------------------------------
ipv4_address ipv4_address::any() noexcept {
return make_ipv4_address(0, 0, 0, 0);
}
ipv4_address ipv4_address::loopback() noexcept {
return make_ipv4_address(127, 0, 0, 1);
}
// -- related free functions ---------------------------------------------------
ipv4_address make_ipv4_address(uint8_t oct1, uint8_t oct2, uint8_t oct3,
......
......@@ -137,6 +137,16 @@ bool ipv6_address::is_loopback() const noexcept {
: half_segments_[0] == 0 && half_segments_[1] == net_order_64(1u);
}
// -- factories ----------------------------------------------------------------
ipv6_address ipv6_address::any() noexcept {
return ip_address{{0}, {0}};
}
ipv6_address ipv6_address::loopback() noexcept {
return ip_address{{0}, {0x1}};
}
// -- related free functions ---------------------------------------------------
namespace {
......
......@@ -187,8 +187,8 @@ std::vector<ip_address> local_addresses(string_view host) {
for_each_adapter(
[&](string_view, ip_address ip) { results.push_back(ip); });
} else if (host == localhost) {
auto v6_local = ip_address{{0}, {0x1}};
auto v4_local = ip_address{make_ipv4_address(127, 0, 0, 1)};
auto v6_local = ip_address::loopback();
auto v4_local = ip_address{ipv4_address::loopback()};
for_each_adapter([&](string_view, ip_address ip) {
if (ip == v4_local || ip == v6_local)
results.push_back(ip);
......@@ -205,10 +205,8 @@ std::vector<ip_address> local_addresses(string_view host) {
}
std::vector<ip_address> local_addresses(ip_address host) {
static auto v6_any = ip_address{{0}, {0}};
static auto v4_any = ip_address{make_ipv4_address(0, 0, 0, 0)};
if (host == v4_any || host == v6_any)
return resolve("");
if (host == ipv6_address::any() || host == ipv4_address::any())
return {host};
auto link_local = ip_address({0xfe, 0x8, 0x0, 0x0}, {0x0, 0x0, 0x0, 0x0});
auto ll_prefix = ip_subnet(link_local, 10);
// Unless explicitly specified we are going to skip link-local addresses.
......
......@@ -127,7 +127,15 @@ make_tcp_accept_socket(const uri::authority_type& node,
tcp_accept_socket_operator fn) {
if (auto ip = get_if<ip_address>(&node.host))
return make_tcp_accept_socket(ip_endpoint{*ip, node.port}, fn);
auto host = get<std::string>(node.host);
const auto& host = get<std::string>(node.host);
if (host.empty()) {
// For empty strings, try IPv6::any and use IPv4::any as fallback.
auto v6_any = ip_address{{0}, {0}};
auto v4_any = ip_address{make_ipv4_address(0, 0, 0, 0)};
if (auto sock = make_tcp_accept_socket(ip_endpoint{v6_any, node.port}, fn))
return *sock;
return make_tcp_accept_socket(ip_endpoint{v4_any, node.port}, fn);
}
auto addrs = ip::local_addresses(host);
if (addrs.empty())
return make_error(sec::cannot_open_port, "no local interface available",
......
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