diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 1bcd9bd1..34d24037 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -1133,6 +1133,13 @@ const std::string &ClientHandler::get_forwarded_by() { return local_hostport_; } + auto &listenerconf = get_config()->conn.listener; + + // For UNIX domain socket listener, just return empty string. + if (listenerconf.host_unix) { + return local_hostport_; + } + int rv; sockaddr_union su; socklen_t addrlen = sizeof(su); @@ -1158,7 +1165,7 @@ const std::string &ClientHandler::get_forwarded_by() { local_hostport_ += ':'; } - local_hostport_ += util::utos(get_config()->conn.listener.port); + local_hostport_ += util::utos(listenerconf.port); return local_hostport_; } @@ -1168,6 +1175,10 @@ const std::string &ClientHandler::get_forwarded_for() const { return forwarded_for_obfuscated_; } + if (get_config()->conn.listener.host_unix) { + return EMPTY_STRING; + } + return ipaddr_; } diff --git a/src/shrpx_client_handler.h b/src/shrpx_client_handler.h index 95c902a5..a236b09c 100644 --- a/src/shrpx_client_handler.h +++ b/src/shrpx_client_handler.h @@ -146,6 +146,8 @@ private: ev_timer reneg_shutdown_timer_; std::unique_ptr upstream_; std::unique_ptr> pinned_http2sessions_; + // IP address of client. If UNIX domain socket is used, this is + // "localhost". std::string ipaddr_; std::string port_; // The ALPN identifier negotiated for this connection. diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index b22461f9..dcc7231e 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -71,6 +71,8 @@ Config *mod_config() { return config; } void create_config() { config = new Config(); } +std::string EMPTY_STRING; + TicketKeys::~TicketKeys() { /* Erase keys from memory */ for (auto &key : keys) { diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 7963828f..41fd1714 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -227,6 +227,10 @@ enum shrpx_forwarded_node_type { FORWARDED_NODE_IP, }; +// Used inside function if it has to return const reference to empty +// string without defining empty string each time. +extern std::string EMPTY_STRING; + struct AltSvc { AltSvc() : port(0) {} diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index 20bc9f67..c818f056 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -698,14 +698,10 @@ bool Downstream::get_http2_upgrade_request() const { response_state_ == INITIAL; } -namespace { -const std::string EMPTY; -} // namespace - const std::string &Downstream::get_http2_settings() const { auto http2_settings = req_.fs.header(http2::HD_HTTP2_SETTINGS); if (!http2_settings) { - return EMPTY; + return EMPTY_STRING; } return http2_settings->value; } diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index b35f31cc..bf7b5800 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -751,15 +751,19 @@ ClientHandler *accept_connection(Worker *worker, int fd, sockaddr *addr, char host[NI_MAXHOST]; char service[NI_MAXSERV]; int rv; - rv = getnameinfo(addr, addrlen, host, sizeof(host), service, sizeof(service), - NI_NUMERICHOST | NI_NUMERICSERV); - if (rv != 0) { - LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv); - return nullptr; - } + if (addr->sa_family == AF_UNIX) { + std::copy_n("localhost", sizeof("localhost"), host); + service[0] = '\0'; + } else { + rv = getnameinfo(addr, addrlen, host, sizeof(host), service, + sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV); + if (rv != 0) { + LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv); + + return nullptr; + } - if (addr->sa_family != AF_UNIX) { rv = util::make_socket_nodelay(fd); if (rv == -1) { LOG(WARN) << "Setting option TCP_NODELAY failed: errno=" << errno;