Moving nghttp's numeric_name function to util.cc and using this to generate the address name in HttpServer.cc

This commit is contained in:
Brian Card 2015-02-24 09:50:18 -05:00
parent 933b9636e5
commit b773d63b92
4 changed files with 18 additions and 18 deletions

View File

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

View File

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

View File

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

View File

@ -29,6 +29,7 @@
#include <unistd.h>
#include <getopt.h>
#include <netdb.h>
#include <cstring>
#include <cassert>
@ -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.