Commit 4bbac29d authored by hydratim's avatar hydratim

Added support for v4-only and improved tests

parent ac7e6118
......@@ -48,6 +48,8 @@ public:
Flags<Options> options = Options::None,
int backlog = Const::MaxBacklog);
void setHandler(const std::shared_ptr<Handler>& handler);
static bool systemSupportsIpv6();
void bind();
void bind(const Address& address);
......
......@@ -14,6 +14,8 @@
#include <netinet/in.h>
#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>
......@@ -145,6 +147,27 @@ 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)
continue;
}
freeifaddrs(ifaddr);
return supportsIpv6;
}
void
Listener::bind() {
bind(addr_);
......
......@@ -123,57 +123,17 @@ TEST(listener_test, listener_bind_port_free) {
ASSERT_TRUE(true);
}
TEST(listener_test, listener_bind_v4_port) {
uint16_t port_nb;
// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper s = bind_free_port();
port_nb = s.port();
}
if (port_nb == 0) {
FAIL() << "Could not find a free port. Abort test.\n";
}
Pistache::Port port(port_nb);
Pistache::Address address(Pistache::Ipv4::loopback(), port);
TEST(listener_test, listener_bind_ephemeral_v6_port) {
Pistache::Port port(0);
Pistache::Address address(Pistache::Ipv6::any(), port);
Pistache::Tcp::Listener listener;
Pistache::Flags<Pistache::Tcp::Options> options;
listener.init(1, options);
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
ASSERT_TRUE(true);
}
TEST(listener_test, listener_bind_v6_port) {
uint16_t port_nb;
// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper s = bind_free_port();
port_nb = s.port();
}
if (port_nb == 0) {
FAIL() << "Could not find a free port. Abort test.\n";
if (not listener.systemSupportsIpv6()) {
Pistache::Flags<Pistache::Tcp::Options> options;
listener.init(1, options);
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
}
Pistache::Port port(port_nb);
Pistache::Address address(Pistache::Ipv6::loopback(), port);
Pistache::Tcp::Listener listener;
Pistache::Flags<Pistache::Tcp::Options> options;
listener.init(1, options);
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
ASSERT_TRUE(true);
}
......
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