diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index d38acb5a..1e9cda83 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -126,6 +126,8 @@ int main(int argc, char *argv[]) { !CU_add_test(pSuite, "util_utox", shrpx::test_util_utox) || !CU_add_test(pSuite, "util_http_date", shrpx::test_util_http_date) || !CU_add_test(pSuite, "util_select_h2", shrpx::test_util_select_h2) || + !CU_add_test(pSuite, "util_ipv6_numeric_addr", + shrpx::test_util_ipv6_numeric_addr) || !CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) || !CU_add_test(pSuite, "ringbuf_write", nghttp2::test_ringbuf_write) || !CU_add_test(pSuite, "ringbuf_iovec", nghttp2::test_ringbuf_iovec)) { diff --git a/src/shrpx.cc b/src/shrpx.cc index 15140d69..9f14c91e 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -84,13 +84,6 @@ const int GRACEFUL_SHUTDOWN_SIGNAL = SIGQUIT; // binary is listening to. #define ENV_PORT "NGHTTPX_PORT" -namespace { -bool is_ipv6_numeric_addr(const char *host) { - uint8_t dst[16]; - return inet_pton(AF_INET6, host, dst) == 1; -} -} // namespace - namespace { int resolve_hostname(sockaddr_union *addr, size_t *addrlen, const char *hostname, uint16_t port, int family) { @@ -1805,7 +1798,7 @@ int main(int argc, char **argv) { } for (auto &addr : mod_config()->downstream_addrs) { - auto ipv6 = is_ipv6_numeric_addr(addr.host.get()); + auto ipv6 = util::ipv6_numeric_addr(addr.host.get()); std::string hostport; if (ipv6) { diff --git a/src/util.cc b/src/util.cc index a4ab3f46..4016d955 100644 --- a/src/util.cc +++ b/src/util.cc @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -881,6 +882,11 @@ bool check_socket_connected(int fd) { return true; } +bool ipv6_numeric_addr(const char *host) { + uint8_t dst[16]; + return inet_pton(AF_INET6, host, dst) == 1; +} + } // namespace util } // namespace nghttp2 diff --git a/src/util.h b/src/util.h index a8150abf..8efd4e4b 100644 --- a/src/util.h +++ b/src/util.h @@ -474,6 +474,9 @@ int create_nonblock_socket(int family); bool check_socket_connected(int fd); +// Returns true if |host| is IPv6 numeric address (e.g., ::1) +bool ipv6_numeric_addr(const char *host); + } // namespace util } // namespace nghttp2 diff --git a/src/util_test.cc b/src/util_test.cc index cbfb692c..12d3bbb6 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -165,4 +165,13 @@ void test_util_select_h2(void) { CU_ASSERT(NGHTTP2_H2_PROTO_ALIAS_LEN == outlen); } +void test_util_ipv6_numeric_addr(void) { + CU_ASSERT(util::ipv6_numeric_addr("::1")); + CU_ASSERT(util::ipv6_numeric_addr("2001:0db8:85a3:0042:1000:8a2e:0370:7334")); + // IPv4 + CU_ASSERT(!util::ipv6_numeric_addr("127.0.0.1")); + // not numeric address + CU_ASSERT(!util::ipv6_numeric_addr("localhost")); +} + } // namespace shrpx diff --git a/src/util_test.h b/src/util_test.h index 9d258d00..1fe123a7 100644 --- a/src/util_test.h +++ b/src/util_test.h @@ -36,6 +36,7 @@ void test_util_quote_string(void); void test_util_utox(void); void test_util_http_date(void); void test_util_select_h2(void); +void test_util_ipv6_numeric_addr(void); } // namespace shrpx