Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/h3_ip_vhost.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*) mod_http3: Select an IP-based virtual host by the address the client
sent to, as for TCP, and log that address as the local address. An
HTTP/3 request to the address of one IP-based virtual host could be
served by another. [Arturo Bernal]
46 changes: 32 additions & 14 deletions modules/http3/h3_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,27 @@ typedef struct
apr_off_t bytes_last_request;
} h3_logio_config_t;

/* The engine reports a raw sockaddr, so the conversion to APR belongs here. */
static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* pool, apr_sockaddr_t** addr, char** client_ip)
/* The engine reports raw sockaddrs, so the conversion to APR belongs here.
* A nonzero @p port replaces the port of the sockaddr. */
static int sockaddr_resolve(const struct sockaddr_storage* sa, socklen_t sa_len, apr_port_t port, apr_pool_t* pool, apr_sockaddr_t** addr, char** ip_str)
{
struct sockaddr_storage peer = {0};
socklen_t peer_len = 0;
if (!h3q_engine_peer_addr(engine, qconn, &peer, &peer_len))
{
return 0;
}

char host[NI_MAXHOST] = {0};
char serv[NI_MAXSERV] = {0};
if (getnameinfo((const struct sockaddr*)&peer, peer_len, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) != 0)
if (getnameinfo((const struct sockaddr*)sa, sa_len, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) != 0)
{
return 0;
}
char* end = NULL;
unsigned long port = strtoul(serv, &end, 10);
if (serv[0] == '\0' || !end || end[0] != '\0' || port > 65535)
unsigned long sa_port = strtoul(serv, &end, 10);
if (serv[0] == '\0' || !end || end[0] != '\0' || sa_port > 65535)
{
return 0;
}

/* Fill locals first: apr_sockaddr_info_get NULLs its out-param on failure. */
apr_sockaddr_t* resolved = NULL;
char* ip = NULL;
if (apr_sockaddr_info_get(&resolved, host, APR_UNSPEC, (apr_port_t)port, 0, pool) != APR_SUCCESS)
if (apr_sockaddr_info_get(&resolved, host, APR_UNSPEC, port ? port : (apr_port_t)sa_port, 0, pool) != APR_SUCCESS)
{
return 0;
}
Expand All @@ -99,10 +93,27 @@ static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* po
return 0;
}
*addr = resolved;
*client_ip = ip;
*ip_str = ip;
return 1;
}

static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* pool, apr_sockaddr_t** addr, char** client_ip)
{
struct sockaddr_storage peer = {0};
socklen_t peer_len = 0;
return h3q_engine_peer_addr(engine, qconn, &peer, &peer_len) && sockaddr_resolve(&peer, peer_len, 0, pool, addr, client_ip);
}

/* The address the client sent to, which selects an IP-based virtual host.
* It keeps the port of the host's TCP listener: an H3Port of its own does
* not make the request belong to another virtual host. */
static int local_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_port_t port, apr_pool_t* pool, apr_sockaddr_t** addr, char** local_ip)
{
struct sockaddr_storage local = {0};
socklen_t local_len = 0;
return h3q_engine_local_addr(engine, qconn, &local, &local_len) && sockaddr_resolve(&local, local_len, port, pool, addr, local_ip);
}

conn_rec* h3_synth_conn(h3_session* session)
{
CHECK(session);
Expand Down Expand Up @@ -132,6 +143,13 @@ conn_rec* h3_synth_conn(h3_session* session)
{
c->remote_host = NULL;
}
if (child_h3_io)
{
/* Else 0.0.0.0, which no IP-based virtual host matches. Like the
* client address, it is the path the connection was accepted on and
* is not updated if the connection migrates. */
local_addr_resolve(child_h3_io->qengine, session->qconn, vhost_port, cpool, &c->local_addr, &c->local_ip);
}
c->bucket_alloc = apr_bucket_alloc_create(cpool);
c->log = &s->log;
#if H3_DEVEL
Expand Down
11 changes: 11 additions & 0 deletions modules/http3/include/quic/h3q.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,15 @@ h3q_conn* h3q_engine_accept_conn(h3q_engine* engine);
*/
int h3q_engine_peer_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len);

/**
* Copy the local address from the connection's network path: the address
* the peer sent to where the socket reports it, else the bound address.
* @param engine Engine owning @p conn; NULL reports failure.
* @param conn Connection to inspect.
* @param addr Out: local socket address.
* @param addr_len Out: bytes of @p addr that are meaningful.
* @return 1 if the address was recovered, 0 otherwise.
*/
int h3q_engine_local_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len);

#endif /* H3Q_H */
12 changes: 12 additions & 0 deletions modules/http3/quic/h3q.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,15 @@ int h3q_engine_peer_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_sto
*addr_len = (socklen_t)conn->path.path.remote.addrlen;
return 1;
}

int h3q_engine_local_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len)
{
(void)engine;
if (!conn || !addr || !addr_len || conn->path.path.local.addrlen == 0 || conn->path.path.local.addrlen > sizeof(*addr))
{
return 0;
}
memcpy(addr, conn->path.path.local.addr, conn->path.path.local.addrlen);
*addr_len = (socklen_t)conn->path.path.local.addrlen;
return 1;
}
92 changes: 92 additions & 0 deletions test/modules/http3/test_018_ip_vhost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import socket
import sys

import pytest

from .env import H3Conf, known_bug

# Two IP-based virtual hosts on one port, told apart only by the address the
# client connects to. All of 127.0.0.0/8 is local on Linux.
HOSTS = [("127.0.0.1", "test1"), ("127.0.0.2", "test2")]


def free_port(env) -> int:
"""A port free for UDP and for TCP on 127.0.0.1."""
for _ in range(20):
port = env.free_udp_port()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("127.0.0.1", port))
return port
except OSError:
continue
finally:
s.close()
pytest.fail("no free port for TCP and UDP")


def get(env, port, addr, h3):
"""GET /alive.json on port, connecting to addr."""
url = f"https://{env.h3_domain}:{port}/alive.json"
options = ["--connect-to", f"{env.h3_domain}:{port}:{addr}:{port}"]
if h3:
return env.h3_get(url, options=options)
return env.curl_raw([url], options=["--http1.1"] + options)


@pytest.mark.skipif(sys.platform != "linux",
reason="needs a second loopback address")
class TestIpVhost:

@pytest.fixture(autouse=True, scope='class')
@classmethod
def _class_scope(cls, env):
env.require_curl_http3()

@staticmethod
def install_ip_vhosts(env, port):
conf = H3Conf(env)
conf.add(f"Listen {port}")
for addr, docs in HOSTS:
conf.add([
f"<VirtualHost {addr}:{port}>",
f"ServerName {env.h3_domain}",
f"DocumentRoot htdocs/{docs}",
"SSLEngine on",
])
for cred in env.get_credentials_for_name(env.h3_domain):
conf.add_certificate(cred.cert_file, cred.pkey_file)
conf.add_h3_protocol()
conf.add("</VirtualHost>")
conf.install()
assert env.apache_restart() == 0
assert env.wait_udp_port(port)

# HTTP/3 selects the IP-based virtual host by the address the client
# sent to, as TCP does
def test_h3_018_01_by_address(self, env):
port = free_port(env)
self.install_ip_vhosts(env, port)
for addr, docs in HOSTS:
for h3 in [False, True]:
r = get(env, port, addr, h3)
proto = "HTTP/3" if h3 else "HTTP/1.1"
assert r.exit_code == 0, f"{addr} {proto}: {r}"
assert r.response["status"] == 200
assert r.json["host"] == docs, \
f"{addr} over {proto} served by {r.json['host']}"

# an address no IP-based virtual host names: TCP falls back to the main
# server, which has no TLS here, so the HTTP/3 virtual host must not
# serve it either (421, as test_h3_103_04 asks for a vhost whose TLS
# setup differs from the HTTP/3 one)
def test_h3_018_02_unmatched_address(self, env):
port = free_port(env)
self.install_ip_vhosts(env, port)
r = get(env, port, "127.0.0.3", h3=True)
known_bug(r.exit_code == 0 and r.response["status"] == 200
and r.json["host"] == "test1",
"an address no IP-based vhost names is served by the first "
"HTTP/3 vhost")
# refused at the handshake, or 421 on the request
assert r.exit_code != 0 or r.response["status"] == 421, f"{r}"
Loading