src: Move ipv6_numeric_addr to util and add test

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-10 21:33:53 +09:00
parent 0069ca9ce8
commit bc17f95c80
6 changed files with 22 additions and 8 deletions

View File

@ -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)) {

View File

@ -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) {

View File

@ -32,6 +32,7 @@
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <cassert>
#include <cstdio>
@ -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

View File

@ -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

View File

@ -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

View File

@ -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