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
HELPER_HFILES = util.h \
http2.h timegm.h app_helper.h nghttp2_config.h \
nghttp2_gzip.h
nghttp2_gzip.h network.h
HTML_PARSER_OBJECTS =
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);
}
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);
}
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);
}
LOG(NOTICE) << "Memcached address for TLS session cache: " << hostport
<< " -> " << util::numeric_hostport(&memcachedconf.addr.su.sa,
memcachedconf.addr.len);
<< " -> " << util::to_numeric_addr(&memcachedconf.addr);
}
}
@ -2247,8 +2246,7 @@ void process_options(
exit(EXIT_FAILURE);
}
LOG(NOTICE) << "Memcached address for TLS ticket key: " << hostport
<< " -> " << util::numeric_hostport(&memcachedconf.addr.su.sa,
memcachedconf.addr.len);
<< " -> " << util::to_numeric_addr(&memcachedconf.addr);
}
}

View File

@ -53,6 +53,7 @@
#include "shrpx_router.h"
#include "template.h"
#include "http2.h"
#include "network.h"
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;
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_forwarded_param {

View File

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

View File

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

View File

@ -39,6 +39,8 @@
#include <neverbleed.h>
#endif // HAVE_NEVERBLEED
#include "network.h"
namespace shrpx {
class ClientHandler;
@ -46,7 +48,6 @@ class Worker;
class DownstreamConnectionPool;
struct DownstreamAddr;
struct UpstreamAddr;
struct Address;
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 (LOG_ENABLED(INFO)) {
LOG(INFO) << "Create cache entry for SSL_SESSION=" << session
<< ", addr=" << util::numeric_hostport(&addr->su.sa, addr->len)
<< "(" << addr << "), timestamp=" << std::fixed
<< std::setprecision(6) << t;
<< ", addr=" << util::to_numeric_addr(addr) << "(" << addr
<< "), timestamp=" << std::fixed << std::setprecision(6) << t;
}
client_tls_session_cache_.emplace(
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;
if (ent.last_updated + 1_min > t) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Cache for addr="
<< util::numeric_hostport(&addr->su.sa, addr->len) << "("
LOG(INFO) << "Cache for addr=" << util::to_numeric_addr(addr) << "("
<< addr << ") is still host. Not updating.";
}
return;
@ -350,9 +348,8 @@ void Worker::cache_client_tls_session(const Address *addr, SSL_SESSION *session,
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Update cache entry for SSL_SESSION=" << session
<< ", addr=" << util::numeric_hostport(&addr->su.sa, addr->len)
<< "(" << addr << "), timestamp=" << std::fixed
<< std::setprecision(6) << t;
<< ", addr=" << util::to_numeric_addr(addr) << "(" << addr
<< "), timestamp=" << std::fixed << std::setprecision(6) << t;
}
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();
}
std::string numeric_hostport(const struct sockaddr *sa, socklen_t salen) {
if (sa->sa_family == AF_UNIX) {
return "localhost";
std::string to_numeric_addr(const Address *addr) {
auto family = addr->su.storage.ss_family;
if (family == AF_UNIX) {
return addr->su.un.sun_path;
}
std::array<char, NI_MAXHOST> host;
std::array<char, NI_MAXSERV> serv;
auto rv = getnameinfo(sa, salen, host.data(), host.size(), serv.data(),
serv.size(), NI_NUMERICHOST | NI_NUMERICSERV);
auto rv =
getnameinfo(&addr->su.sa, addr->len, host.data(), host.size(),
serv.data(), serv.size(), NI_NUMERICHOST | NI_NUMERICSERV);
if (rv != 0) {
return "unknown";
}
@ -668,7 +670,7 @@ std::string numeric_hostport(const struct sockaddr *sa, socklen_t salen) {
std::string s;
char *p;
if (sa->sa_family == AF_INET6) {
if (family == AF_INET6) {
s.resize(hostlen + servlen + 2 + 1);
p = &s[0];
*p++ = '[';

View File

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