diff --git a/src/HttpServer.cc b/src/HttpServer.cc index ca8ee39d..1c986410 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -1536,8 +1536,7 @@ int start_listen(struct ev_loop *loop, Sessions *sessions, new ListenEventHandler(sessions, fd, acceptor); if (config->verbose) { - char s[INET6_ADDRSTRLEN]; - get_ip_str((struct sockaddr *)rp->ai_addr, s, sizeof s); + std::string s = util::numeric_name(rp); std::cout << (rp->ai_family == AF_INET ? "IPv4" : "IPv6") << ": listen " << s << ":" << config->port << std::endl; } diff --git a/src/nghttp.cc b/src/nghttp.cc index 1bc0392d..ccff2567 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -107,20 +107,6 @@ std::string strip_fragment(const char *raw_uri) { } } // namespace -namespace { -// Returns numeric address string of |addr|. If getnameinfo() is -// failed, "unknown" is returned. -std::string numeric_name(addrinfo *addr) { - char host[NI_MAXHOST]; - auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), - nullptr, 0, NI_NUMERICHOST); - if (rv != 0) { - return "unknown"; - } - return host; -} -} // namespace - Request::Request(const std::string &uri, const http_parser_url &u, const nghttp2_data_provider *data_prd, int64_t data_length, const nghttp2_priority_spec &pri_spec, @@ -688,13 +674,13 @@ int HttpClient::noop() { return 0; } void HttpClient::on_connect_fail() { if (state == STATE_IDLE) { std::cerr << "[ERROR] Could not connect to the address " - << numeric_name(cur_addr) << std::endl; + << util::numeric_name(cur_addr) << std::endl; } auto cur_state = state; disconnect(); if (cur_state == STATE_IDLE) { if (initiate_connection() == 0) { - std::cerr << "Trying next address " << numeric_name(cur_addr) + std::cerr << "Trying next address " << util::numeric_name(cur_addr) << std::endl; } } diff --git a/src/util.cc b/src/util.cc index eafe13b9..8dea595b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -686,6 +686,18 @@ bool numeric_host(const char *hostname) { return true; } +// Returns numeric address string of |addr|. If getnameinfo() is +// failed, "unknown" is returned. +std::string numeric_name(addrinfo *addr) { + char host[NI_MAXHOST]; + auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), + nullptr, 0, NI_NUMERICHOST); + if (rv != 0) { + return "unknown"; + } + return host; +} + int reopen_log_file(const char *path) { #if defined(__ANDROID__) || defined(ANDROID) int fd; diff --git a/src/util.h b/src/util.h index 8f63fe37..f4246d40 100644 --- a/src/util.h +++ b/src/util.h @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -417,6 +418,8 @@ void write_uri_field(std::ostream &o, const char *uri, const http_parser_url &u, bool numeric_host(const char *hostname); +std::string numeric_name(addrinfo *addr); + // Opens |path| with O_APPEND enabled. If file does not exist, it is // created first. This function returns file descriptor referring the // opened file if it succeeds, or -1.