From 90686f30ce5231ff965a4d102885acae205e4f3c Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Wed, 17 Jan 2018 23:32:14 +0300 Subject: [PATCH] Support IPv6 addresses when whitelisting hosts (see #468) --- libtransmission/rpc-server.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/libtransmission/rpc-server.c b/libtransmission/rpc-server.c index c80a629ca..60a8320f7 100644 --- a/libtransmission/rpc-server.c +++ b/libtransmission/rpc-server.c @@ -590,6 +590,16 @@ isAddressAllowed (const tr_rpc_server * server, const char * address) return false; } +static bool +isIPAddressWithOptionalPort (const char * host) +{ + struct sockaddr_storage address; + int address_len = sizeof (address); + + /* TODO: move to net.{c,h} */ + return evutil_parse_sockaddr_port (host, (struct sockaddr *) &address, &address_len) != -1; +} + static bool isHostnameAllowed (const tr_rpc_server * server, struct evhttp_request * req) { @@ -607,11 +617,15 @@ isHostnameAllowed (const tr_rpc_server * server, struct evhttp_request * req) if (host == NULL) return false; + /* IP address is always acceptable. */ + if (isIPAddressWithOptionalPort (host)) + return true; + /* Host header might include the port. */ char * const hostname = tr_strndup (host, strcspn (host, ":")); - /* localhost or ipaddress is always acceptable. */ - if (strcmp (hostname, "localhost") == 0 || strcmp (hostname, "localhost.") == 0 || tr_addressIsIP (hostname)) + /* localhost is always acceptable. */ + if (strcmp (hostname, "localhost") == 0 || strcmp (hostname, "localhost.") == 0) { tr_free (hostname); return true; -- 2.40.0