Commit 714c13d8 authored by Dennis Jenkins's avatar Dennis Jenkins

Move static utility method for detecting ipv6 support from Listener (server)...

Move static utility method for detecting ipv6 support from Listener (server) to Net (common), to make it cleaner to use in the client.
parent 2be5c97d
......@@ -49,8 +49,6 @@ public:
int backlog = Const::MaxBacklog);
void setHandler(const std::shared_ptr<Handler>& handler);
static bool systemSupportsIpv6();
void bind();
void bind(const Address& address);
......
......@@ -109,6 +109,9 @@ public:
std::string toString() const;
void toNetwork(in6_addr*) const;
// Returns 'true' if the kernel/libc support IPV6, false if not.
static bool supported();
private:
uint16_t a;
uint16_t b;
......
......@@ -9,6 +9,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <iostream>
#include <pistache/net.h>
......@@ -130,6 +131,32 @@ void Ipv6::toNetwork(in6_addr *addr6) const {
memcpy(addr6->s6_addr16, remap_ip6, 16);
}
bool Ipv6::supported() {
struct ifaddrs *ifaddr = nullptr;
struct ifaddrs *ifa = nullptr;
int family, n;
bool supportsIpv6 = false;
if (getifaddrs(&ifaddr) == -1) {
throw std::runtime_error("Call to getifaddrs() failed");
}
for (ifa = ifaddr, n = 0; ifa != nullptr; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == nullptr) {
continue;
}
family = ifa->ifa_addr->sa_family;
if (family == AF_INET6) {
supportsIpv6 = true;
continue;
}
}
freeifaddrs(ifaddr);
return supportsIpv6;
}
Address::Address()
: host_("")
, port_(0)
......
......@@ -15,7 +15,6 @@
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
......@@ -147,29 +146,6 @@ Listener::pinWorker(size_t worker, const CpuSet& set)
#endif
}
bool
Listener::systemSupportsIpv6(){
struct ifaddrs *ifaddr, *ifa;
int family, n;
bool supportsIpv6 = false;
if (getifaddrs(&ifaddr) == -1) {
throw std::runtime_error("Call to getifaddrs() failed");
}
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET6) {
supportsIpv6 = true;
continue;
}
}
freeifaddrs(ifaddr);
return supportsIpv6;
}
void
Listener::bind() {
bind(addr_);
......
......@@ -198,7 +198,7 @@ TEST(listener_test, listener_bind_ephemeral_v4_port) {
TEST(listener_test, listener_bind_ephemeral_v6_port) {
Pistache::Tcp::Listener listener;
if (listener.systemSupportsIpv6()) {
if (Pistache::Ipv6::supported()) {
Pistache::Port port(0);
Pistache::Address address(Pistache::Ipv6::any(), port);
......
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