Commit 886553e2 authored by Joseph Noir's avatar Joseph Noir

Cleanup ip resovle

parent d44b4acc
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* | |___ / ___ \| _| Framework * * | |___ / ___ \| _| Framework *
* \____/_/ \_|_| * * \____/_/ \_|_| *
* * * *
* Copyright 2011-2018 Dominik Charousset * * Copyright 2011-2019 Dominik Charousset *
* * * *
* Distributed under the terms and conditions of the BSD 3-Clause License or * * Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software * * (at your option) under the terms and conditions of the Boost Software *
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* | |___ / ___ \| _| Framework * * | |___ / ___ \| _| Framework *
* \____/_/ \_|_| * * \____/_/ \_|_| *
* * * *
* Copyright 2011-2018 Dominik Charousset * * Copyright 2011-2019 Dominik Charousset *
* * * *
* Distributed under the terms and conditions of the BSD 3-Clause License or * * Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software * * (at your option) under the terms and conditions of the Boost Software *
...@@ -34,12 +34,14 @@ ...@@ -34,12 +34,14 @@
# define _WIN32_WINNT 0x0600 # define _WIN32_WINNT 0x0600
# endif # endif
# include <iphlpapi.h> # include <iphlpapi.h>
# include <winsock.h>
#else #else
# include <sys/types.h>
# include <arpa/inet.h>
# include <net/if.h> # include <net/if.h>
# include <netdb.h> # include <netdb.h>
# include <ifaddrs.h> # include <ifaddrs.h>
# include <sys/ioctl.h> # include <sys/ioctl.h>
# include <sys/types.h>
#endif #endif
// clang-format on // clang-format on
...@@ -63,14 +65,12 @@ void* fetch_in_addr(int family, sockaddr* addr) { ...@@ -63,14 +65,12 @@ void* fetch_in_addr(int family, sockaddr* addr) {
return &reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr; return &reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr;
} }
// TODO: Use getnameinfo instead? int fetch_addr_str(char (&buf)[INET6_ADDRSTRLEN], sockaddr* addr) {
int fetch_addr_str(bool get_ipv4, bool get_ipv6, char (&buf)[INET6_ADDRSTRLEN],
sockaddr* addr) {
if (addr == nullptr) if (addr == nullptr)
return AF_UNSPEC; return AF_UNSPEC;
auto family = addr->sa_family; auto family = addr->sa_family;
auto in_addr = fetch_in_addr(family, addr); auto in_addr = fetch_in_addr(family, addr);
return ((family == AF_INET && get_ipv4) || (family == AF_INET6 && get_ipv6)) return (family == AF_INET || family == AF_INET6)
&& inet_ntop(family, in_addr, buf, INET6_ADDRSTRLEN) == buf && inet_ntop(family, in_addr, buf, INET6_ADDRSTRLEN) == buf
? family ? family
: AF_UNSPEC; : AF_UNSPEC;
...@@ -93,7 +93,7 @@ std::vector<ip_address> resolve(string_view host) { ...@@ -93,7 +93,7 @@ std::vector<ip_address> resolve(string_view host) {
char buffer[INET6_ADDRSTRLEN]; char buffer[INET6_ADDRSTRLEN];
std::vector<ip_address> results; std::vector<ip_address> results;
for (auto i = addrs.get(); i != nullptr; i = i->ai_next) { for (auto i = addrs.get(); i != nullptr; i = i->ai_next) {
auto family = fetch_addr_str(true, true, buffer, i->ai_addr); auto family = fetch_addr_str(buffer, i->ai_addr);
if (family != AF_UNSPEC) { if (family != AF_UNSPEC) {
ip_address ip; ip_address ip;
if (auto err = parse(buffer, ip)) if (auto err = parse(buffer, ip))
...@@ -110,27 +110,13 @@ std::vector<ip_address> resolve(string_view host) { ...@@ -110,27 +110,13 @@ std::vector<ip_address> resolve(string_view host) {
return results; return results;
} }
#ifdef CAF_WINDOWS
std::string hostname() {
TCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = MAX_COMPUTERNAME_LENGTH;
GetComputerName(buf, &size);
return buf;
}
#else // CAF_WINDOWS
std::string hostname() { std::string hostname() {
char buf[HOST_NAME_MAX + 1]; char buf[HOST_NAME_MAX + 1];
buf[HOST_NAME_MAX] = '\0'; buf[HOST_NAME_MAX] = '\0';
gethostname(buf, HOST_NAME_MAX); gethostname(buf, HOST_NAME_MAX);
gethostbyname(buf);
return buf; return buf;
} }
#endif // CAF_WINDOWS
} // namespace ip } // namespace ip
} // namespace net } // namespace net
} // namespace caf } // namespace caf
...@@ -20,20 +20,27 @@ ...@@ -20,20 +20,27 @@
#include "caf/net/ip.hpp" #include "caf/net/ip.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/ip_address.hpp"
#include "caf/test/dsl.hpp" #include "caf/test/dsl.hpp"
#include "host_fixture.hpp"
#include "caf/ip_address.hpp"
#include "caf/ipv4_address.hpp"
using namespace caf; using namespace caf;
using namespace caf::net; using namespace caf::net;
CAF_TEST_FIXTURE_SCOPE(ip_tests, host_fixture)
CAF_TEST(resolve) { CAF_TEST(resolve) {
ip_address v4_local{make_ipv4_address(127, 0, 0, 1)}; ip_address v4_local{make_ipv4_address(127, 0, 0, 1)};
ip_address v6_local{{0}, {0x1}}; ip_address v6_local{{0}, {0x1}};
auto addrs = ip::resolve("localhost"); auto addrs = ip::resolve("localhost");
CAF_CHECK(!addrs.empty()); CAF_CHECK(!addrs.empty());
auto contains = [&](ip_address x) { auto contains = [&](ip_address x) {
return std::find(std::begin(addrs), std::end(addrs), x) != std::end(addrs); return std::count(addrs.begin(), addrs.end(), x) > 0;
}; };
CAF_CHECK(contains(v4_local) || contains(v6_local)); CAF_CHECK(contains(v4_local) || contains(v6_local));
} }
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