From b2203bb05aa241a3dd00719c8afd07d82900ba3d Mon Sep 17 00:00:00 2001 From: Daniel Ottiger Date: Tue, 5 May 2020 04:13:12 +0200 Subject: [PATCH] server: support dual-stack server socket (#450) According to RFC 3493 the socket option IPV6_V6ONLY should be off by default, see https://tools.ietf.org/html/rfc3493#page-22 (chapter 5.3). However this does not seem to be the case on all systems. For instance on any Windows OS, the option is on by default. Therefore clear this option in order to allow an server socket which can support IPv6 and IPv4 at the same time. --- httplib.h | 5 +++++ test/test.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/httplib.h b/httplib.h index 7816df8..b5bc38f 100644 --- a/httplib.h +++ b/httplib.h @@ -1469,6 +1469,11 @@ socket_t create_socket(const char *host, int port, Fn fn, int yes = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)); + + int no = 0; + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), + sizeof(no)); + #ifdef SO_REUSEPORT setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), sizeof(yes)); diff --git a/test/test.cc b/test/test.cc index a9dcdbd..a7ea78d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -724,6 +724,39 @@ TEST(RedirectToDifferentPort, Redirect) { } #endif +TEST(Server, BindDualStack) { + Server svr; + + svr.Get("/1", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen("::", PORT); }); + + // Give GET time to get a few messages. + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli("127.0.0.1", PORT); + + auto res = cli.Get("/1"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ(res->body, "Hello World!"); + } + { + Client cli("::1", PORT); + + auto res = cli.Get("/1"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ(res->body, "Hello World!"); + } + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); +} + TEST(Server, BindAndListenSeparately) { Server svr; int port = svr.bind_to_any_port("0.0.0.0");