Unverified Commit c08a983d authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #23

Resolve empty strings to "any" address
parents f11da3a1 aa2bd7b9
......@@ -55,6 +55,9 @@ namespace ip {
namespace {
// Dummy port to resolve empty string with getaddrinfo.
constexpr auto dummy_port = "42";
void* fetch_in_addr(int family, sockaddr* addr) {
if (family == AF_INET)
return &reinterpret_cast<sockaddr_in*>(addr)->sin_addr;
......@@ -83,7 +86,9 @@ std::vector<ip_address> resolve(string_view host) {
hint.ai_flags = AI_PASSIVE;
addrinfo* tmp = nullptr;
std::string host_str{host.begin(), host.end()};
if (getaddrinfo(host_str.c_str(), nullptr, &hint, &tmp) != 0)
if (getaddrinfo(host.empty() ? nullptr : host_str.c_str(),
host.empty() ? dummy_port : nullptr, &hint, &tmp)
!= 0)
return {};
std::unique_ptr<addrinfo, decltype(freeaddrinfo)*> addrs{tmp, freeaddrinfo};
char buffer[INET6_ADDRSTRLEN];
......
......@@ -32,7 +32,7 @@ using namespace caf::net;
CAF_TEST_FIXTURE_SCOPE(ip_tests, host_fixture)
CAF_TEST(resolve) {
CAF_TEST(resolve localhost) {
ip_address v4_local{make_ipv4_address(127, 0, 0, 1)};
ip_address v6_local{{0}, {0x1}};
auto addrs = ip::resolve("localhost");
......@@ -43,4 +43,15 @@ CAF_TEST(resolve) {
CAF_CHECK(contains(v4_local) || contains(v6_local));
}
CAF_TEST(resolve any) {
ip_address v4_any{make_ipv4_address(0, 0, 0, 0)};
ip_address v6_any{{0}, {0}};
auto addrs = ip::resolve("");
CAF_CHECK(!addrs.empty());
auto contains = [&](ip_address x) {
return std::count(addrs.begin(), addrs.end(), x) > 0;
};
CAF_CHECK(contains(v4_any) || contains(v6_any));
}
CAF_TEST_FIXTURE_SCOPE_END()
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