src: Rename and rewrite numeric_hostport as to_numeric_addr and support AF_UNIX path

This commit is contained in:
Tatsuhiro Tsujikawa 2016-02-21 15:28:11 +09:00
parent 11c8803b92
commit dfc02843b6
10 changed files with 91 additions and 41 deletions

View File

@ -62,7 +62,7 @@ HELPER_OBJECTS = util.cc \
http2.cc timegm.c app_helper.cc nghttp2_gzip.c http2.cc timegm.c app_helper.cc nghttp2_gzip.c
HELPER_HFILES = util.h \ HELPER_HFILES = util.h \
http2.h timegm.h app_helper.h nghttp2_config.h \ http2.h timegm.h app_helper.h nghttp2_config.h \
nghttp2_gzip.h nghttp2_gzip.h network.h
HTML_PARSER_OBJECTS = HTML_PARSER_OBJECTS =
HTML_PARSER_HFILES = HtmlParser.h HTML_PARSER_HFILES = HtmlParser.h

61
src/network.h Normal file
View File

@ -0,0 +1,61 @@
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2016 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NETWORK_H
#define NETWORK_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif // HAVE_SYS_SOCKET_H
#include <sys/un.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif // HAVE_NETINET_IN_H
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif // HAVE_ARPA_INET_H
namespace nghttp2 {
union sockaddr_union {
sockaddr_storage storage;
sockaddr sa;
sockaddr_in6 in6;
sockaddr_in in;
sockaddr_un un;
};
struct Address {
size_t len;
union sockaddr_union su;
};
} // namespace nghttp2
#endif // NETWORK_H

View File

@ -2201,7 +2201,7 @@ void process_options(
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
LOG(NOTICE) << "Resolved backend address: " << hostport << " -> " LOG(NOTICE) << "Resolved backend address: " << hostport << " -> "
<< util::numeric_hostport(&addr.addr.su.sa, addr.addr.len); << util::to_numeric_addr(&addr.addr);
} }
} }
@ -2214,7 +2214,7 @@ void process_options(
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
LOG(NOTICE) << "Backend HTTP proxy address: " << hostport << " -> " LOG(NOTICE) << "Backend HTTP proxy address: " << hostport << " -> "
<< util::numeric_hostport(&proxy.addr.su.sa, proxy.addr.len); << util::to_numeric_addr(&proxy.addr);
} }
{ {
@ -2230,8 +2230,7 @@ void process_options(
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
LOG(NOTICE) << "Memcached address for TLS session cache: " << hostport LOG(NOTICE) << "Memcached address for TLS session cache: " << hostport
<< " -> " << util::numeric_hostport(&memcachedconf.addr.su.sa, << " -> " << util::to_numeric_addr(&memcachedconf.addr);
memcachedconf.addr.len);
} }
} }
@ -2247,8 +2246,7 @@ void process_options(
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
LOG(NOTICE) << "Memcached address for TLS ticket key: " << hostport LOG(NOTICE) << "Memcached address for TLS ticket key: " << hostport
<< " -> " << util::numeric_hostport(&memcachedconf.addr.su.sa, << " -> " << util::to_numeric_addr(&memcachedconf.addr);
memcachedconf.addr.len);
} }
} }

View File

@ -53,6 +53,7 @@
#include "shrpx_router.h" #include "shrpx_router.h"
#include "template.h" #include "template.h"
#include "http2.h" #include "http2.h"
#include "network.h"
using namespace nghttp2; using namespace nghttp2;
@ -231,19 +232,6 @@ constexpr char SHRPX_OPT_BACKEND_ADDRESS_FAMILY[] = "backend-address-family";
constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8; constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8;
union sockaddr_union {
sockaddr_storage storage;
sockaddr sa;
sockaddr_in6 in6;
sockaddr_in in;
sockaddr_un un;
};
struct Address {
size_t len;
union sockaddr_union su;
};
enum shrpx_proto { PROTO_HTTP2, PROTO_HTTP }; enum shrpx_proto { PROTO_HTTP2, PROTO_HTTP };
enum shrpx_forwarded_param { enum shrpx_forwarded_param {

View File

@ -35,12 +35,13 @@
#include "shrpx_connection.h" #include "shrpx_connection.h"
#include "buffer.h" #include "buffer.h"
#include "network.h"
using namespace nghttp2; using namespace nghttp2;
namespace shrpx { namespace shrpx {
struct MemcachedRequest; struct MemcachedRequest;
struct Address;
enum { enum {
MEMCACHED_PARSE_HEADER24, MEMCACHED_PARSE_HEADER24,

View File

@ -34,12 +34,12 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include "memchunk.h" #include "memchunk.h"
#include "network.h"
namespace shrpx { namespace shrpx {
struct MemcachedRequest; struct MemcachedRequest;
class MemcachedConnection; class MemcachedConnection;
struct Address;
class MemcachedDispatcher { class MemcachedDispatcher {
public: public:

View File

@ -39,6 +39,8 @@
#include <neverbleed.h> #include <neverbleed.h>
#endif // HAVE_NEVERBLEED #endif // HAVE_NEVERBLEED
#include "network.h"
namespace shrpx { namespace shrpx {
class ClientHandler; class ClientHandler;
@ -46,7 +48,6 @@ class Worker;
class DownstreamConnectionPool; class DownstreamConnectionPool;
struct DownstreamAddr; struct DownstreamAddr;
struct UpstreamAddr; struct UpstreamAddr;
struct Address;
namespace ssl { namespace ssl {

View File

@ -329,9 +329,8 @@ void Worker::cache_client_tls_session(const Address *addr, SSL_SESSION *session,
if (it == std::end(client_tls_session_cache_)) { if (it == std::end(client_tls_session_cache_)) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Create cache entry for SSL_SESSION=" << session LOG(INFO) << "Create cache entry for SSL_SESSION=" << session
<< ", addr=" << util::numeric_hostport(&addr->su.sa, addr->len) << ", addr=" << util::to_numeric_addr(addr) << "(" << addr
<< "(" << addr << "), timestamp=" << std::fixed << "), timestamp=" << std::fixed << std::setprecision(6) << t;
<< std::setprecision(6) << t;
} }
client_tls_session_cache_.emplace( client_tls_session_cache_.emplace(
addr, SessionCacheEntry{serialize_ssl_session(session), t}); addr, SessionCacheEntry{serialize_ssl_session(session), t});
@ -341,8 +340,7 @@ void Worker::cache_client_tls_session(const Address *addr, SSL_SESSION *session,
auto &ent = (*it).second; auto &ent = (*it).second;
if (ent.last_updated + 1_min > t) { if (ent.last_updated + 1_min > t) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Cache for addr=" LOG(INFO) << "Cache for addr=" << util::to_numeric_addr(addr) << "("
<< util::numeric_hostport(&addr->su.sa, addr->len) << "("
<< addr << ") is still host. Not updating."; << addr << ") is still host. Not updating.";
} }
return; return;
@ -350,9 +348,8 @@ void Worker::cache_client_tls_session(const Address *addr, SSL_SESSION *session,
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Update cache entry for SSL_SESSION=" << session LOG(INFO) << "Update cache entry for SSL_SESSION=" << session
<< ", addr=" << util::numeric_hostport(&addr->su.sa, addr->len) << ", addr=" << util::to_numeric_addr(addr) << "(" << addr
<< "(" << addr << "), timestamp=" << std::fixed << "), timestamp=" << std::fixed << std::setprecision(6) << t;
<< std::setprecision(6) << t;
} }
ent.session_data = serialize_ssl_session(session); ent.session_data = serialize_ssl_session(session);

View File

@ -650,15 +650,17 @@ std::string numeric_name(const struct sockaddr *sa, socklen_t salen) {
return host.data(); return host.data();
} }
std::string numeric_hostport(const struct sockaddr *sa, socklen_t salen) { std::string to_numeric_addr(const Address *addr) {
if (sa->sa_family == AF_UNIX) { auto family = addr->su.storage.ss_family;
return "localhost"; if (family == AF_UNIX) {
return addr->su.un.sun_path;
} }
std::array<char, NI_MAXHOST> host; std::array<char, NI_MAXHOST> host;
std::array<char, NI_MAXSERV> serv; std::array<char, NI_MAXSERV> serv;
auto rv = getnameinfo(sa, salen, host.data(), host.size(), serv.data(), auto rv =
serv.size(), NI_NUMERICHOST | NI_NUMERICSERV); getnameinfo(&addr->su.sa, addr->len, host.data(), host.size(),
serv.data(), serv.size(), NI_NUMERICHOST | NI_NUMERICSERV);
if (rv != 0) { if (rv != 0) {
return "unknown"; return "unknown";
} }
@ -668,7 +670,7 @@ std::string numeric_hostport(const struct sockaddr *sa, socklen_t salen) {
std::string s; std::string s;
char *p; char *p;
if (sa->sa_family == AF_INET6) { if (family == AF_INET6) {
s.resize(hostlen + servlen + 2 + 1); s.resize(hostlen + servlen + 2 + 1);
p = &s[0]; p = &s[0];
*p++ = '['; *p++ = '[';

View File

@ -50,6 +50,7 @@
#include "http-parser/http_parser.h" #include "http-parser/http_parser.h"
#include "template.h" #include "template.h"
#include "network.h"
namespace nghttp2 { namespace nghttp2 {
@ -461,10 +462,11 @@ bool numeric_host(const char *hostname, int family);
// failed, "unknown" is returned. // failed, "unknown" is returned.
std::string numeric_name(const struct sockaddr *sa, socklen_t salen); std::string numeric_name(const struct sockaddr *sa, socklen_t salen);
// Returns string representation of numeric address and port of |addr| // Returns string representation of numeric address and port of
// of length |salen|. The format is like <HOST>:<PORT>. For IPv6 // |addr|. If address family is AF_UNIX, this return path to UNIX
// address, address is enclosed by square brackets ([]). // domain socket. Otherwise, the format is like <HOST>:<PORT>. For
std::string numeric_hostport(const struct sockaddr *sa, socklen_t salen); // IPv6 address, address is enclosed by square brackets ([]).
std::string to_numeric_addr(const Address *addr);
// Makes internal copy of stderr (and possibly stdout in the future), // Makes internal copy of stderr (and possibly stdout in the future),
// which is then used as pointer to /dev/stderr or /proc/self/fd/2 // which is then used as pointer to /dev/stderr or /proc/self/fd/2