tor-commits
Threads by month
- ----- 2025 -----
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
March 2019
- 20 participants
- 3265 discussions

[tor/maint-0.4.0] Merge branch 'tor-github/pr/763' into maint-0.4.0
by asn@torproject.org 12 Mar '19
by asn@torproject.org 12 Mar '19
12 Mar '19
commit 0ce32affc2d617e487fbedaac1582358fa6fb85c
Merge: 9c9214f2c 08ddf1f62
Author: George Kadianakis <desnacked(a)riseup.net>
Date: Tue Mar 12 18:11:35 2019 +0200
Merge branch 'tor-github/pr/763' into maint-0.4.0
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
1
0
commit 18a4f535d3ecf56d70fe585bbdb031bd67a96374
Merge: 61adcb22c 0ce32affc
Author: George Kadianakis <desnacked(a)riseup.net>
Date: Tue Mar 12 18:11:53 2019 +0200
Merge branch 'maint-0.4.0'
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
1
0

12 Mar '19
commit 0ce32affc2d617e487fbedaac1582358fa6fb85c
Merge: 9c9214f2c 08ddf1f62
Author: George Kadianakis <desnacked(a)riseup.net>
Date: Tue Mar 12 18:11:35 2019 +0200
Merge branch 'tor-github/pr/763' into maint-0.4.0
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
1
0

[tor/release-0.4.0] Merge branch 'tor-github/pr/763' into maint-0.4.0
by asn@torproject.org 12 Mar '19
by asn@torproject.org 12 Mar '19
12 Mar '19
commit 0ce32affc2d617e487fbedaac1582358fa6fb85c
Merge: 9c9214f2c 08ddf1f62
Author: George Kadianakis <desnacked(a)riseup.net>
Date: Tue Mar 12 18:11:35 2019 +0200
Merge branch 'tor-github/pr/763' into maint-0.4.0
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
1
0

[tor/release-0.4.0] Merge remote-tracking branch 'bug28525_029' into maint-0.3.5
by asn@torproject.org 12 Mar '19
by asn@torproject.org 12 Mar '19
12 Mar '19
commit 08ddf1f62bf62aa3dc044e0570fb368477223688
Merge: d6f77b99d 63b404911
Author: teor <teor(a)torproject.org>
Date: Fri Mar 8 12:32:04 2019 +1000
Merge remote-tracking branch 'bug28525_029' into maint-0.3.5
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
diff --cc src/lib/net/address.c
index 28c8e3f50,000000000..a2d234b74
mode 100644,000000..100644
--- a/src/lib/net/address.c
+++ b/src/lib/net/address.c
@@@ -1,2046 -1,0 +1,2057 @@@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file address.c
+ * \brief Functions to use and manipulate the tor_addr_t structure.
+ *
+ * This module doesn't have any support for the libc resolver: that is all in
+ * resolve.c.
+ **/
+
+#define ADDRESS_PRIVATE
+
+#include "orconfig.h"
+
+#ifdef _WIN32
+/* For access to structs needed by GetAdaptersAddresses */
+#ifndef WIN32_LEAN_AND_MEAN
+#error "orconfig.h didn't define WIN32_LEAN_AND_MEAN"
+#endif
+#ifndef WINVER
+#error "orconfig.h didn't define WINVER"
+#endif
+#ifndef _WIN32_WINNT
+#error "orconfig.h didn't define _WIN32_WINNT"
+#endif
+#if WINVER < 0x0501
+#error "winver too low"
+#endif
+#if _WIN32_WINNT < 0x0501
+#error "winver too low"
+#endif
+#include <winsock2.h>
+#include <process.h>
+#include <windows.h>
+#include <iphlpapi.h>
+#endif /* defined(_WIN32) */
+
+#include "lib/net/address.h"
+#include "lib/net/socket.h"
+#include "lib/container/smartlist.h"
+#include "lib/ctime/di_ops.h"
+#include "lib/log/log.h"
+#include "lib/log/escape.h"
+#include "lib/malloc/malloc.h"
+#include "lib/net/inaddr.h"
+#include "lib/string/compat_ctype.h"
+#include "lib/string/compat_string.h"
+#include "lib/string/parse_int.h"
+#include "lib/string/printf.h"
+#include "lib/string/util_string.h"
+
+#include "siphash.h"
+
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h> /* FreeBSD needs this to know what version it is */
+#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
+#ifdef HAVE_NET_IF_H
+#include <net/if.h>
+#endif
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* tor_addr_is_null() and maybe other functions rely on AF_UNSPEC being 0 to
+ * work correctly. Bail out here if we've found a platform where AF_UNSPEC
+ * isn't 0. */
+#if AF_UNSPEC != 0
+#error We rely on AF_UNSPEC being 0. Let us know about your platform, please!
+#endif
+
+/** Convert the tor_addr_t in <b>a</b>, with port in <b>port</b>, into a
+ * sockaddr object in *<b>sa_out</b> of object size <b>len</b>. If not enough
+ * room is available in sa_out, or on error, return 0. On success, return
+ * the length of the sockaddr.
+ *
+ * Interface note: ordinarily, we return -1 for error. We can't do that here,
+ * since socklen_t is unsigned on some platforms.
+ **/
+socklen_t
+tor_addr_to_sockaddr(const tor_addr_t *a,
+ uint16_t port,
+ struct sockaddr *sa_out,
+ socklen_t len)
+{
+ memset(sa_out, 0, len);
+
+ sa_family_t family = tor_addr_family(a);
+ if (family == AF_INET) {
+ struct sockaddr_in *sin;
+ if (len < (int)sizeof(struct sockaddr_in))
+ return 0;
+ sin = (struct sockaddr_in *)sa_out;
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+ sin->sin_len = sizeof(struct sockaddr_in);
+#endif
+ sin->sin_family = AF_INET;
+ sin->sin_port = htons(port);
+ sin->sin_addr.s_addr = tor_addr_to_ipv4n(a);
+ return sizeof(struct sockaddr_in);
+ } else if (family == AF_INET6) {
+ struct sockaddr_in6 *sin6;
+ if (len < (int)sizeof(struct sockaddr_in6))
+ return 0;
+ sin6 = (struct sockaddr_in6 *)sa_out;
+#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
+ sin6->sin6_len = sizeof(struct sockaddr_in6);
+#endif
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = htons(port);
+ memcpy(&sin6->sin6_addr, tor_addr_to_in6_assert(a),
+ sizeof(struct in6_addr));
+ return sizeof(struct sockaddr_in6);
+ } else {
+ return 0;
+ }
+}
+
+/** Set address <b>a</b> to zero. This address belongs to
+ * the AF_UNIX family. */
+static void
+tor_addr_make_af_unix(tor_addr_t *a)
+{
+ memset(a, 0, sizeof(*a));
+ a->family = AF_UNIX;
+}
+
+/** Set the tor_addr_t in <b>a</b> to contain the socket address contained in
+ * <b>sa</b>. IF <b>port_out</b> is non-NULL and <b>sa</b> contains a port,
+ * set *<b>port_out</b> to that port. Return 0 on success and -1 on
+ * failure. */
+int
+tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
+ uint16_t *port_out)
+{
+ tor_assert(a);
+ tor_assert(sa);
+
+ /* This memset is redundant; leaving it in to avoid any future accidents,
+ however. */
+ memset(a, 0, sizeof(*a));
+
+ if (sa->sa_family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in *) sa;
+ tor_addr_from_ipv4n(a, sin->sin_addr.s_addr);
+ if (port_out)
+ *port_out = ntohs(sin->sin_port);
+ } else if (sa->sa_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
+ tor_addr_from_in6(a, &sin6->sin6_addr);
+ if (port_out)
+ *port_out = ntohs(sin6->sin6_port);
+ } else if (sa->sa_family == AF_UNIX) {
+ tor_addr_make_af_unix(a);
+ return 0;
+ } else {
+ tor_addr_make_unspec(a);
+ return -1;
+ }
+ return 0;
+}
+
+/** Return a newly allocated string holding the address described in
+ * <b>sa</b>. AF_UNIX, AF_UNSPEC, AF_INET, and AF_INET6 are supported. */
+char *
+tor_sockaddr_to_str(const struct sockaddr *sa)
+{
+ char address[TOR_ADDR_BUF_LEN];
+ char *result;
+ tor_addr_t addr;
+ uint16_t port;
+#ifdef HAVE_SYS_UN_H
+ if (sa->sa_family == AF_UNIX) {
+ struct sockaddr_un *s_un = (struct sockaddr_un *)sa;
+ tor_asprintf(&result, "unix:%s", s_un->sun_path);
+ return result;
+ }
+#endif /* defined(HAVE_SYS_UN_H) */
+ if (sa->sa_family == AF_UNSPEC)
+ return tor_strdup("unspec");
+
+ if (tor_addr_from_sockaddr(&addr, sa, &port) < 0)
+ return NULL;
+ if (! tor_addr_to_str(address, &addr, sizeof(address), 1))
+ return NULL;
+ tor_asprintf(&result, "%s:%d", address, (int)port);
+ return result;
+}
+
+/** Set address <b>a</b> to the unspecified address. This address belongs to
+ * no family. */
+void
+tor_addr_make_unspec(tor_addr_t *a)
+{
+ memset(a, 0, sizeof(*a));
+ a->family = AF_UNSPEC;
+}
+
+/** Set address <b>a</b> to the null address in address family <b>family</b>.
+ * The null address for AF_INET is 0.0.0.0. The null address for AF_INET6 is
+ * [::]. AF_UNSPEC is all null. */
+void
+tor_addr_make_null(tor_addr_t *a, sa_family_t family)
+{
+ memset(a, 0, sizeof(*a));
+ a->family = family;
+}
+
- /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks
- * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is
- * also treated as internal for now.)
++/** Return true iff <b>ip</b> is an IP reserved to localhost or local networks.
++ *
++ * If <b>ip</b> is in RFC1918 or RFC4193 or RFC4291, we will return true.
++ * (fec0::/10, deprecated by RFC3879, is also treated as internal for now
++ * and will return true.)
++ *
++ * If <b>ip</b> is 0.0.0.0 or 100.64.0.0/10 (RFC6598), we will act as:
++ * - Internal if <b>for_listening</b> is 0, as these addresses are not
++ * routable on the internet and we won't be publicly accessible to clients.
++ * - External if <b>for_listening</b> is 1, as clients could connect to us
++ * from the internet (in the case of 0.0.0.0) or a service provider's
++ * internal network (in the case of RFC6598).
+ */
+int
+tor_addr_is_internal_(const tor_addr_t *addr, int for_listening,
+ const char *filename, int lineno)
+{
+ uint32_t iph4 = 0;
+ uint32_t iph6[4];
+
+ tor_assert(addr);
+ sa_family_t v_family = tor_addr_family(addr);
+
+ if (v_family == AF_INET) {
+ iph4 = tor_addr_to_ipv4h(addr);
+ } else if (v_family == AF_INET6) {
+ if (tor_addr_is_v4(addr)) { /* v4-mapped */
+ uint32_t *addr32 = NULL;
+ v_family = AF_INET;
+ // Work around an incorrect NULL pointer dereference warning in
+ // "clang --analyze" due to limited analysis depth
+ addr32 = tor_addr_to_in6_addr32(addr);
+ // To improve performance, wrap this assertion in:
+ // #if !defined(__clang_analyzer__) || PARANOIA
+ tor_assert(addr32);
+ iph4 = ntohl(addr32[3]);
+ }
+ }
+
+ if (v_family == AF_INET6) {
+ const uint32_t *a32 = tor_addr_to_in6_addr32(addr);
+ iph6[0] = ntohl(a32[0]);
+ iph6[1] = ntohl(a32[1]);
+ iph6[2] = ntohl(a32[2]);
+ iph6[3] = ntohl(a32[3]);
+ if (for_listening && !iph6[0] && !iph6[1] && !iph6[2] && !iph6[3]) /* :: */
+ return 0;
+
+ if (((iph6[0] & 0xfe000000) == 0xfc000000) || /* fc00/7 - RFC4193 */
+ ((iph6[0] & 0xffc00000) == 0xfe800000) || /* fe80/10 - RFC4291 */
+ ((iph6[0] & 0xffc00000) == 0xfec00000)) /* fec0/10 D- RFC3879 */
+ return 1;
+
+ if (!iph6[0] && !iph6[1] && !iph6[2] &&
+ ((iph6[3] & 0xfffffffe) == 0x00000000)) /* ::/127 */
+ return 1;
+
+ return 0;
+ } else if (v_family == AF_INET) {
- if (for_listening && !iph4) /* special case for binding to 0.0.0.0 */
++ /* special case for binding to 0.0.0.0 or 100.64/10 (RFC6598) */
++ if (for_listening && (!iph4 || ((iph4 & 0xffc00000) == 0x64400000)))
+ return 0;
+ if (((iph4 & 0xff000000) == 0x0a000000) || /* 10/8 */
+ ((iph4 & 0xff000000) == 0x00000000) || /* 0/8 */
+ ((iph4 & 0xff000000) == 0x7f000000) || /* 127/8 */
++ ((iph4 & 0xffc00000) == 0x64400000) || /* 100.64/10 */
+ ((iph4 & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
+ ((iph4 & 0xfff00000) == 0xac100000) || /* 172.16/12 */
+ ((iph4 & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
+ return 1;
+ return 0;
+ }
+
+ /* unknown address family... assume it's not safe for external use */
+ /* rather than tor_assert(0) */
+ log_warn(LD_BUG, "tor_addr_is_internal() called from %s:%d with a "
+ "non-IP address of type %d", filename, lineno, (int)v_family);
+ tor_fragile_assert();
+ return 1;
+}
+
+/** Convert a tor_addr_t <b>addr</b> into a string, and store it in
+ * <b>dest</b> of size <b>len</b>. Returns a pointer to dest on success,
+ * or NULL on failure. If <b>decorate</b>, surround IPv6 addresses with
+ * brackets.
+ */
+const char *
+tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
+{
+ const char *ptr;
+ tor_assert(addr && dest);
+
+ switch (tor_addr_family(addr)) {
+ case AF_INET:
+ /* Shortest addr x.x.x.x + \0 */
+ if (len < 8)
+ return NULL;
+ ptr = tor_inet_ntop(AF_INET, &addr->addr.in_addr, dest, len);
+ break;
+ case AF_INET6:
+ /* Shortest addr [ :: ] + \0 */
+ if (len < (3 + (decorate ? 2 : 0)))
+ return NULL;
+
+ if (decorate)
+ ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest+1, len-2);
+ else
+ ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest, len);
+
+ if (ptr && decorate) {
+ *dest = '[';
+ memcpy(dest+strlen(dest), "]", 2);
+ tor_assert(ptr == dest+1);
+ ptr = dest;
+ }
+ break;
+ case AF_UNIX:
+ tor_snprintf(dest, len, "AF_UNIX");
+ ptr = dest;
+ break;
+ default:
+ return NULL;
+ }
+ return ptr;
+}
+
+/** Parse an .in-addr.arpa or .ip6.arpa address from <b>address</b>. Return 0
+ * if this is not an .in-addr.arpa address or an .ip6.arpa address. Return -1
+ * if this is an ill-formed .in-addr.arpa address or an .ip6.arpa address.
+ * Also return -1 if <b>family</b> is not AF_UNSPEC, and the parsed address
+ * family does not match <b>family</b>. On success, return 1, and store the
+ * result, if any, into <b>result</b>, if provided.
+ *
+ * If <b>accept_regular</b> is set and the address is in neither recognized
+ * reverse lookup hostname format, try parsing the address as a regular
+ * IPv4 or IPv6 address too.
+ */
+int
+tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
+ int family, int accept_regular)
+{
+ if (!strcasecmpend(address, ".in-addr.arpa")) {
+ /* We have an in-addr.arpa address. */
+ char buf[INET_NTOA_BUF_LEN];
+ size_t len;
+ struct in_addr inaddr;
+ if (family == AF_INET6)
+ return -1;
+
+ len = strlen(address) - strlen(".in-addr.arpa");
+ if (len >= INET_NTOA_BUF_LEN)
+ return -1; /* Too long. */
+
+ memcpy(buf, address, len);
+ buf[len] = '\0';
+ if (tor_inet_aton(buf, &inaddr) == 0)
+ return -1; /* malformed. */
+
+ /* reverse the bytes */
+ inaddr.s_addr = (uint32_t)
+ (((inaddr.s_addr & 0x000000ff) << 24)
+ |((inaddr.s_addr & 0x0000ff00) << 8)
+ |((inaddr.s_addr & 0x00ff0000) >> 8)
+ |((inaddr.s_addr & 0xff000000) >> 24));
+
+ if (result) {
+ tor_addr_from_in(result, &inaddr);
+ }
+ return 1;
+ }
+
+ if (!strcasecmpend(address, ".ip6.arpa")) {
+ const char *cp;
+ int n0, n1;
+ struct in6_addr in6;
+
+ if (family == AF_INET)
+ return -1;
+
+ cp = address;
+ for (int i = 0; i < 16; ++i) {
+ n0 = hex_decode_digit(*cp++); /* The low-order nybble appears first. */
+ if (*cp++ != '.') return -1; /* Then a dot. */
+ n1 = hex_decode_digit(*cp++); /* The high-order nybble appears first. */
+ if (*cp++ != '.') return -1; /* Then another dot. */
+ if (n0<0 || n1 < 0) /* Both nybbles must be hex. */
+ return -1;
+
+ /* We don't check the length of the string in here. But that's okay,
+ * since we already know that the string ends with ".ip6.arpa", and
+ * there is no way to frameshift .ip6.arpa so it fits into the pattern
+ * of hexdigit, period, hexdigit, period that we enforce above.
+ */
+
+ /* Assign from low-byte to high-byte. */
+ in6.s6_addr[15-i] = n0 | (n1 << 4);
+ }
+ if (strcasecmp(cp, "ip6.arpa"))
+ return -1;
+
+ if (result) {
+ tor_addr_from_in6(result, &in6);
+ }
+ return 1;
+ }
+
+ if (accept_regular) {
+ tor_addr_t tmp;
+ int r = tor_addr_parse(&tmp, address);
+ if (r < 0)
+ return 0;
+ if (r != family && family != AF_UNSPEC)
+ return -1;
+
+ if (result)
+ memcpy(result, &tmp, sizeof(tor_addr_t));
+
+ return 1;
+ }
+
+ return 0;
+}
+
+/** Convert <b>addr</b> to an in-addr.arpa name or a .ip6.arpa name,
+ * and store the result in the <b>outlen</b>-byte buffer at
+ * <b>out</b>. Returns a non-negative integer on success.
+ * Returns -1 on failure. */
+int
+tor_addr_to_PTR_name(char *out, size_t outlen,
+ const tor_addr_t *addr)
+{
+ tor_assert(out);
+ tor_assert(addr);
+
+ if (addr->family == AF_INET) {
+ uint32_t a = tor_addr_to_ipv4h(addr);
+
+ return tor_snprintf(out, outlen, "%d.%d.%d.%d.in-addr.arpa",
+ (int)(uint8_t)((a )&0xff),
+ (int)(uint8_t)((a>>8 )&0xff),
+ (int)(uint8_t)((a>>16)&0xff),
+ (int)(uint8_t)((a>>24)&0xff));
+ } else if (addr->family == AF_INET6) {
+ int i;
+ char *cp = out;
+ const uint8_t *bytes = tor_addr_to_in6_addr8(addr);
+ if (outlen < REVERSE_LOOKUP_NAME_BUF_LEN)
+ return -1;
+ for (i = 15; i >= 0; --i) {
+ uint8_t byte = bytes[i];
+ *cp++ = "0123456789abcdef"[byte & 0x0f];
+ *cp++ = '.';
+ *cp++ = "0123456789abcdef"[byte >> 4];
+ *cp++ = '.';
+ }
+ memcpy(cp, "ip6.arpa", 9); /* 8 characters plus NUL */
+ return 32 * 2 + 8;
+ }
+ return -1;
+}
+
+/** Parse a string <b>s</b> containing an IPv4/IPv6 address, and possibly
+ * a mask and port or port range. Store the parsed address in
+ * <b>addr_out</b>, a mask (if any) in <b>mask_out</b>, and port(s) (if any)
+ * in <b>port_min_out</b> and <b>port_max_out</b>.
+ *
+ * The syntax is:
+ * Address OptMask OptPortRange
+ * Address ::= IPv4Address / "[" IPv6Address "]" / "*"
+ * OptMask ::= "/" Integer /
+ * OptPortRange ::= ":*" / ":" Integer / ":" Integer "-" Integer /
+ *
+ * - If mask, minport, or maxport are NULL, we do not want these
+ * options to be set; treat them as an error if present.
+ * - If the string has no mask, the mask is set to /32 (IPv4) or /128 (IPv6).
+ * - If the string has one port, it is placed in both min and max port
+ * variables.
+ * - If the string has no port(s), port_(min|max)_out are set to 1 and 65535.
+ *
+ * Return an address family on success, or -1 if an invalid address string is
+ * provided.
+ *
+ * If 'flags & TAPMP_EXTENDED_STAR' is false, then the wildcard address '*'
+ * yield an IPv4 wildcard.
+ *
+ * If 'flags & TAPMP_EXTENDED_STAR' is true, then the wildcard address '*'
+ * yields an AF_UNSPEC wildcard address, which expands to corresponding
+ * wildcard IPv4 and IPv6 rules, and the following change is made
+ * in the grammar above:
+ * Address ::= IPv4Address / "[" IPv6Address "]" / "*" / "*4" / "*6"
+ * with the new "*4" and "*6" productions creating a wildcard to match
+ * IPv4 or IPv6 addresses.
+ *
+ * If 'flags & TAPMP_EXTENDED_STAR' and 'flags & TAPMP_STAR_IPV4_ONLY' are
+ * both true, then the wildcard address '*' yields an IPv4 wildcard.
+ *
+ * If 'flags & TAPMP_EXTENDED_STAR' and 'flags & TAPMP_STAR_IPV6_ONLY' are
+ * both true, then the wildcard address '*' yields an IPv6 wildcard.
+ *
+ * TAPMP_STAR_IPV4_ONLY and TAPMP_STAR_IPV6_ONLY are mutually exclusive. */
+int
+tor_addr_parse_mask_ports(const char *s,
+ unsigned flags,
+ tor_addr_t *addr_out,
+ maskbits_t *maskbits_out,
+ uint16_t *port_min_out, uint16_t *port_max_out)
+{
+ char *base = NULL, *address, *mask = NULL, *port = NULL, *rbracket = NULL;
+ char *endptr;
+ int any_flag=0, v4map=0;
+ sa_family_t family;
+ struct in6_addr in6_tmp;
+ struct in_addr in_tmp = { .s_addr = 0 };
+
+ tor_assert(s);
+ tor_assert(addr_out);
+ /* We can either only want an IPv4 address or only want an IPv6 address,
+ * but we can't only want IPv4 & IPv6 at the same time. */
+ tor_assert(!((flags & TAPMP_STAR_IPV4_ONLY)
+ && (flags & TAPMP_STAR_IPV6_ONLY)));
+
+ /** Longest possible length for an address, mask, and port-range combination.
+ * Includes IP, [], /mask, :, ports */
+#define MAX_ADDRESS_LENGTH (TOR_ADDR_BUF_LEN+2+(1+INET_NTOA_BUF_LEN)+12+1)
+
+ if (strlen(s) > MAX_ADDRESS_LENGTH) {
+ log_warn(LD_GENERAL, "Impossibly long IP %s; rejecting", escaped(s));
+ goto err;
+ }
+ base = tor_strdup(s);
+
+ /* Break 'base' into separate strings. */
+ address = base;
+ if (*address == '[') { /* Probably IPv6 */
+ address++;
+ rbracket = strchr(address, ']');
+ if (!rbracket) {
+ log_warn(LD_GENERAL,
+ "No closing IPv6 bracket in address pattern; rejecting.");
+ goto err;
+ }
+ }
+ mask = strchr((rbracket?rbracket:address),'/');
+ port = strchr((mask?mask:(rbracket?rbracket:address)), ':');
+ if (port)
+ *port++ = '\0';
+ if (mask)
+ *mask++ = '\0';
+ if (rbracket)
+ *rbracket = '\0';
+ if (port && mask)
+ tor_assert(port > mask);
+ if (mask && rbracket)
+ tor_assert(mask > rbracket);
+
+ /* Now "address" is the a.b.c.d|'*'|abcd::1 part...
+ * "mask" is the Mask|Maskbits part...
+ * and "port" is the *|port|min-max part.
+ */
+
+ /* Process the address portion */
+ memset(addr_out, 0, sizeof(tor_addr_t));
+
+ if (!strcmp(address, "*")) {
+ if (flags & TAPMP_EXTENDED_STAR) {
+ if (flags & TAPMP_STAR_IPV4_ONLY) {
+ family = AF_INET;
+ tor_addr_from_ipv4h(addr_out, 0);
+ } else if (flags & TAPMP_STAR_IPV6_ONLY) {
+ static char nil_bytes[16] = { [0]=0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
+ family = AF_INET6;
+ tor_addr_from_ipv6_bytes(addr_out, nil_bytes);
+ } else {
+ family = AF_UNSPEC;
+ tor_addr_make_unspec(addr_out);
+ log_info(LD_GENERAL,
+ "'%s' expands into rules which apply to all IPv4 and IPv6 "
+ "addresses. (Use accept/reject *4:* for IPv4 or "
+ "accept[6]/reject[6] *6:* for IPv6.)", s);
+ }
+ } else {
+ family = AF_INET;
+ tor_addr_from_ipv4h(addr_out, 0);
+ }
+ any_flag = 1;
+ } else if (!strcmp(address, "*4") && (flags & TAPMP_EXTENDED_STAR)) {
+ family = AF_INET;
+ tor_addr_from_ipv4h(addr_out, 0);
+ any_flag = 1;
+ } else if (!strcmp(address, "*6") && (flags & TAPMP_EXTENDED_STAR)) {
+ static char nil_bytes[16] = { [0]=0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
+ family = AF_INET6;
+ tor_addr_from_ipv6_bytes(addr_out, nil_bytes);
+ any_flag = 1;
+ } else if (tor_inet_pton(AF_INET6, address, &in6_tmp) > 0) {
+ family = AF_INET6;
+ tor_addr_from_in6(addr_out, &in6_tmp);
+ } else if (tor_inet_pton(AF_INET, address, &in_tmp) > 0) {
+ family = AF_INET;
+ tor_addr_from_in(addr_out, &in_tmp);
+ } else {
+ log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
+ escaped(address));
+ goto err;
+ }
+
+ v4map = tor_addr_is_v4(addr_out);
+
+ /* Parse mask */
+ if (maskbits_out) {
+ int bits = 0;
+ struct in_addr v4mask;
+
+ if (mask) { /* the caller (tried to) specify a mask */
+ bits = (int) strtol(mask, &endptr, 10);
+ if (!*endptr) { /* strtol converted everything, so it was an integer */
+ if ((bits<0 || bits>128) ||
+ (family == AF_INET && bits > 32)) {
+ log_warn(LD_GENERAL,
+ "Bad number of mask bits (%d) on address range; rejecting.",
+ bits);
+ goto err;
+ }
+ } else { /* mask might still be an address-style mask */
+ if (tor_inet_pton(AF_INET, mask, &v4mask) > 0) {
+ bits = addr_mask_get_bits(ntohl(v4mask.s_addr));
+ if (bits < 0) {
+ log_warn(LD_GENERAL,
+ "IPv4-style mask %s is not a prefix address; rejecting.",
+ escaped(mask));
+ goto err;
+ }
+ } else { /* Not IPv4; we don't do address-style IPv6 masks. */
+ log_warn(LD_GENERAL,
+ "Malformed mask on address range %s; rejecting.",
+ escaped(s));
+ goto err;
+ }
+ }
+ if (family == AF_INET6 && v4map) {
+ if (bits > 32 && bits < 96) { /* Crazy */
+ log_warn(LD_GENERAL,
+ "Bad mask bits %d for V4-mapped V6 address; rejecting.",
+ bits);
+ goto err;
+ }
+ /* XXXX_IP6 is this really what we want? */
+ bits = 96 + bits%32; /* map v4-mapped masks onto 96-128 bits */
+ }
+ if (any_flag) {
+ log_warn(LD_GENERAL,
+ "Found bit prefix with wildcard address; rejecting");
+ goto err;
+ }
+ } else { /* pick an appropriate mask, as none was given */
+ if (any_flag)
+ bits = 0; /* This is okay whether it's V6 or V4 (FIX V4-mapped V6!) */
+ else if (tor_addr_family(addr_out) == AF_INET)
+ bits = 32;
+ else if (tor_addr_family(addr_out) == AF_INET6)
+ bits = 128;
+ }
+ *maskbits_out = (maskbits_t) bits;
+ } else {
+ if (mask) {
+ log_warn(LD_GENERAL,
+ "Unexpected mask in address %s; rejecting", escaped(s));
+ goto err;
+ }
+ }
+
+ /* Parse port(s) */
+ if (port_min_out) {
+ uint16_t port2;
+ if (!port_max_out) /* caller specified one port; fake the second one */
+ port_max_out = &port2;
+
+ if (parse_port_range(port, port_min_out, port_max_out) < 0) {
+ goto err;
+ } else if ((*port_min_out != *port_max_out) && port_max_out == &port2) {
+ log_warn(LD_GENERAL,
+ "Wanted one port from address range, but there are two.");
+
+ port_max_out = NULL; /* caller specified one port, so set this back */
+ goto err;
+ }
+ } else {
+ if (port) {
+ log_warn(LD_GENERAL,
+ "Unexpected ports in address %s; rejecting", escaped(s));
+ goto err;
+ }
+ }
+
+ tor_free(base);
+ return tor_addr_family(addr_out);
+ err:
+ tor_free(base);
+ return -1;
+}
+
+/** Determine whether an address is IPv4, either native or IPv4-mapped IPv6.
+ * Note that this is about representation only, as any decent stack will
+ * reject IPv4-mapped addresses received on the wire (and won't use them
+ * on the wire either).
+ */
+int
+tor_addr_is_v4(const tor_addr_t *addr)
+{
+ tor_assert(addr);
+
+ if (tor_addr_family(addr) == AF_INET)
+ return 1;
+
+ if (tor_addr_family(addr) == AF_INET6) {
+ /* First two don't need to be ordered */
+ uint32_t *a32 = tor_addr_to_in6_addr32(addr);
+ if (a32[0] == 0 && a32[1] == 0 && ntohl(a32[2]) == 0x0000ffffu)
+ return 1;
+ }
+
+ return 0; /* Not IPv4 - unknown family or a full-blood IPv6 address */
+}
+
+/** Determine whether an address <b>addr</b> is null, either all zeroes or
+ * belonging to family AF_UNSPEC.
+ */
+int
+tor_addr_is_null(const tor_addr_t *addr)
+{
+ tor_assert(addr);
+
+ switch (tor_addr_family(addr)) {
+ case AF_INET6: {
+ uint32_t *a32 = tor_addr_to_in6_addr32(addr);
+ return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) && (a32[3] == 0);
+ }
+ case AF_INET:
+ return (tor_addr_to_ipv4n(addr) == 0);
+ case AF_UNIX:
+ return 1;
+ case AF_UNSPEC:
+ return 1;
+ default:
+ log_warn(LD_BUG, "Called with unknown address family %d",
+ (int)tor_addr_family(addr));
+ return 0;
+ }
+ //return 1;
+}
+
+/** Return true iff <b>addr</b> is a loopback address */
+int
+tor_addr_is_loopback(const tor_addr_t *addr)
+{
+ tor_assert(addr);
+ switch (tor_addr_family(addr)) {
+ case AF_INET6: {
+ /* ::1 */
+ uint32_t *a32 = tor_addr_to_in6_addr32(addr);
+ return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) &&
+ (ntohl(a32[3]) == 1);
+ }
+ case AF_INET:
+ /* 127.0.0.1 */
+ return (tor_addr_to_ipv4h(addr) & 0xff000000) == 0x7f000000;
+ case AF_UNSPEC:
+ return 0;
+ /* LCOV_EXCL_START */
+ default:
+ tor_fragile_assert();
+ return 0;
+ /* LCOV_EXCL_STOP */
+ }
+}
+
+/* Is addr valid?
+ * Checks that addr is non-NULL and not tor_addr_is_null().
+ * If for_listening is true, IPv4 addr 0.0.0.0 is allowed.
+ * It means "bind to all addresses on the local machine". */
+int
+tor_addr_is_valid(const tor_addr_t *addr, int for_listening)
+{
+ /* NULL addresses are invalid regardless of for_listening */
+ if (addr == NULL) {
+ return 0;
+ }
+
+ /* Only allow IPv4 0.0.0.0 for_listening. */
+ if (for_listening && addr->family == AF_INET
+ && tor_addr_to_ipv4h(addr) == 0) {
+ return 1;
+ }
+
+ /* Otherwise, the address is valid if it's not tor_addr_is_null() */
+ return !tor_addr_is_null(addr);
+}
+
+/* Is the network-order IPv4 address v4n_addr valid?
+ * Checks that addr is not zero.
+ * Except if for_listening is true, where IPv4 addr 0.0.0.0 is allowed. */
+int
+tor_addr_is_valid_ipv4n(uint32_t v4n_addr, int for_listening)
+{
+ /* Any IPv4 address is valid with for_listening. */
+ if (for_listening) {
+ return 1;
+ }
+
+ /* Otherwise, zero addresses are invalid. */
+ return v4n_addr != 0;
+}
+
+/* Is port valid?
+ * Checks that port is not 0.
+ * Except if for_listening is true, where port 0 is allowed.
+ * It means "OS chooses a port". */
+int
+tor_port_is_valid(uint16_t port, int for_listening)
+{
+ /* Any port value is valid with for_listening. */
+ if (for_listening) {
+ return 1;
+ }
+
+ /* Otherwise, zero ports are invalid. */
+ return port != 0;
+}
+
+/** Set <b>dest</b> to equal the IPv4 address in <b>v4addr</b> (given in
+ * network order). */
+void
+tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
+{
+ tor_assert(dest);
+ memset(dest, 0, sizeof(tor_addr_t));
+ dest->family = AF_INET;
+ dest->addr.in_addr.s_addr = v4addr;
+}
+
+/** Set <b>dest</b> to equal the IPv6 address in the 16 bytes at
+ * <b>ipv6_bytes</b>. */
+void
+tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *ipv6_bytes)
+{
+ tor_assert(dest);
+ tor_assert(ipv6_bytes);
+ memset(dest, 0, sizeof(tor_addr_t));
+ dest->family = AF_INET6;
+ memcpy(dest->addr.in6_addr.s6_addr, ipv6_bytes, 16);
+}
+
+/** Set <b>dest</b> equal to the IPv6 address in the in6_addr <b>in6</b>. */
+void
+tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
+{
+ tor_addr_from_ipv6_bytes(dest, (const char*)in6->s6_addr);
+}
+
+/** Copy a tor_addr_t from <b>src</b> to <b>dest</b>.
+ */
+void
+tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
+{
+ if (src == dest)
+ return;
+ tor_assert(src);
+ tor_assert(dest);
+ memcpy(dest, src, sizeof(tor_addr_t));
+}
+
+/** Copy a tor_addr_t from <b>src</b> to <b>dest</b>, taking extra care to
+ * copy only the well-defined portions. Used for computing hashes of
+ * addresses.
+ */
+void
+tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src)
+{
+ tor_assert(src != dest);
+ tor_assert(src);
+ tor_assert(dest);
+ memset(dest, 0, sizeof(tor_addr_t));
+ dest->family = src->family;
+ switch (tor_addr_family(src))
+ {
+ case AF_INET:
+ dest->addr.in_addr.s_addr = src->addr.in_addr.s_addr;
+ break;
+ case AF_INET6:
+ memcpy(dest->addr.in6_addr.s6_addr, src->addr.in6_addr.s6_addr, 16);
+ case AF_UNSPEC:
+ break;
+ // LCOV_EXCL_START
+ default:
+ tor_fragile_assert();
+ // LCOV_EXCL_STOP
+ }
+}
+
+/** Given two addresses <b>addr1</b> and <b>addr2</b>, return 0 if the two
+ * addresses are equivalent under the mask mbits, less than 0 if addr1
+ * precedes addr2, and greater than 0 otherwise.
+ *
+ * Different address families (IPv4 vs IPv6) are always considered unequal if
+ * <b>how</b> is CMP_EXACT; otherwise, IPv6-mapped IPv4 addresses are
+ * considered equivalent to their IPv4 equivalents.
+ *
+ * As a special case, all pointer-wise distinct AF_UNIX addresses are always
+ * considered unequal since tor_addr_t currently does not contain the
+ * information required to make the comparison.
+ */
+int
+tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
+ tor_addr_comparison_t how)
+{
+ return tor_addr_compare_masked(addr1, addr2, 128, how);
+}
+
+/** As tor_addr_compare(), but only looks at the first <b>mask</b> bits of
+ * the address.
+ *
+ * Reduce over-specific masks (>128 for ipv6, >32 for ipv4) to 128 or 32.
+ *
+ * The mask is interpreted relative to <b>addr1</b>, so that if a is
+ * \::ffff:1.2.3.4, and b is 3.4.5.6,
+ * tor_addr_compare_masked(a,b,100,CMP_SEMANTIC) is the same as
+ * -tor_addr_compare_masked(b,a,4,CMP_SEMANTIC).
+ *
+ * We guarantee that the ordering from tor_addr_compare_masked is a total
+ * order on addresses, but not that it is any particular order, or that it
+ * will be the same from one version to the next.
+ */
+int
+tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
+ maskbits_t mbits, tor_addr_comparison_t how)
+{
+ /** Helper: Evaluates to -1 if a is less than b, 0 if a equals b, or 1 if a
+ * is greater than b. May evaluate a and b more than once. */
+#define TRISTATE(a,b) (((a)<(b))?-1: (((a)==(b))?0:1))
+ sa_family_t family1, family2, v_family1, v_family2;
+
+ tor_assert(addr1 && addr2);
+
+ v_family1 = family1 = tor_addr_family(addr1);
+ v_family2 = family2 = tor_addr_family(addr2);
+
+ if (family1==family2) {
+ /* When the families are the same, there's only one way to do the
+ * comparison: exactly. */
+ int r;
+ switch (family1) {
+ case AF_UNSPEC:
+ return 0; /* All unspecified addresses are equal */
+ case AF_INET: {
+ uint32_t a1 = tor_addr_to_ipv4h(addr1);
+ uint32_t a2 = tor_addr_to_ipv4h(addr2);
+ if (mbits <= 0)
+ return 0;
+ if (mbits > 32)
+ mbits = 32;
+ a1 >>= (32-mbits);
+ a2 >>= (32-mbits);
+ r = TRISTATE(a1, a2);
+ return r;
+ }
+ case AF_INET6: {
+ if (mbits > 128)
+ mbits = 128;
+
+ const uint8_t *a1 = tor_addr_to_in6_addr8(addr1);
+ const uint8_t *a2 = tor_addr_to_in6_addr8(addr2);
+ const int bytes = mbits >> 3;
+ const int leftover_bits = mbits & 7;
+ if (bytes && (r = tor_memcmp(a1, a2, bytes))) {
+ return r;
+ } else if (leftover_bits) {
+ uint8_t b1 = a1[bytes] >> (8-leftover_bits);
+ uint8_t b2 = a2[bytes] >> (8-leftover_bits);
+ return TRISTATE(b1, b2);
+ } else {
+ return 0;
+ }
+ }
+ case AF_UNIX:
+ /* HACKHACKHACKHACKHACK:
+ * tor_addr_t doesn't contain a copy of sun_path, so it's not
+ * possible to compare this at all.
+ *
+ * Since the only time we currently actually should be comparing
+ * 2 AF_UNIX addresses is when dealing with ISO_CLIENTADDR (which
+ * is disabled for AF_UNIX SocksPorts anyway), this just does
+ * a pointer comparison.
+ *
+ * See: #20261.
+ */
+ if (addr1 < addr2)
+ return -1;
+ else if (addr1 == addr2)
+ return 0;
+ else
+ return 1;
+ /* LCOV_EXCL_START */
+ default:
+ tor_fragile_assert();
+ return 0;
+ /* LCOV_EXCL_STOP */
+ }
+ } else if (how == CMP_EXACT) {
+ /* Unequal families and an exact comparison? Stop now! */
+ return TRISTATE(family1, family2);
+ }
+
+ if (mbits == 0)
+ return 0;
+
+ if (family1 == AF_INET6 && tor_addr_is_v4(addr1))
+ v_family1 = AF_INET;
+ if (family2 == AF_INET6 && tor_addr_is_v4(addr2))
+ v_family2 = AF_INET;
+ if (v_family1 == v_family2) {
+ /* One or both addresses are a mapped ipv4 address. */
+ uint32_t a1, a2;
+ if (family1 == AF_INET6) {
+ a1 = tor_addr_to_mapped_ipv4h(addr1);
+ if (mbits <= 96)
+ return 0;
+ mbits -= 96; /* We just decided that the first 96 bits of a1 "match". */
+ } else {
+ a1 = tor_addr_to_ipv4h(addr1);
+ }
+ if (family2 == AF_INET6) {
+ a2 = tor_addr_to_mapped_ipv4h(addr2);
+ } else {
+ a2 = tor_addr_to_ipv4h(addr2);
+ }
+ if (mbits > 32) mbits = 32;
+ a1 >>= (32-mbits);
+ a2 >>= (32-mbits);
+ return TRISTATE(a1, a2);
+ } else {
+ /* Unequal families, and semantic comparison, and no semantic family
+ * matches. */
+ return TRISTATE(family1, family2);
+ }
+}
+
+/** Input for siphash, to produce some output for an unspec value. */
+static const uint32_t unspec_hash_input[] = { 0x4e4df09f, 0x92985342 };
+
+/** Return a hash code based on the address addr. DOCDOC extra */
+uint64_t
+tor_addr_hash(const tor_addr_t *addr)
+{
+ switch (tor_addr_family(addr)) {
+ case AF_INET:
+ return siphash24g(&addr->addr.in_addr.s_addr, 4);
+ case AF_UNSPEC:
+ return siphash24g(unspec_hash_input, sizeof(unspec_hash_input));
+ case AF_INET6:
+ return siphash24g(&addr->addr.in6_addr.s6_addr, 16);
+ /* LCOV_EXCL_START */
+ default:
+ tor_fragile_assert();
+ return 0;
+ /* LCOV_EXCL_STOP */
+ }
+}
+
+/** As tor_addr_hash, but use a particular siphash key. */
+uint64_t
+tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr)
+{
+ /* This is duplicate code with tor_addr_hash, since this function needs to
+ * be backportable all the way to 0.2.9. */
+
+ switch (tor_addr_family(addr)) {
+ case AF_INET:
+ return siphash24(&addr->addr.in_addr.s_addr, 4, key);
+ case AF_UNSPEC:
+ return siphash24(unspec_hash_input, sizeof(unspec_hash_input), key);
+ case AF_INET6:
+ return siphash24(&addr->addr.in6_addr.s6_addr, 16, key);
+ default:
+ /* LCOV_EXCL_START */
+ tor_fragile_assert();
+ return 0;
+ /* LCOV_EXCL_STOP */
+ }
+}
+
+/** Return a newly allocated string with a representation of <b>addr</b>. */
+char *
+tor_addr_to_str_dup(const tor_addr_t *addr)
+{
+ char buf[TOR_ADDR_BUF_LEN];
+ if (tor_addr_to_str(buf, addr, sizeof(buf), 0)) {
+ return tor_strdup(buf);
+ } else {
+ return tor_strdup("<unknown address type>");
+ }
+}
+
+/** Return a string representing the address <b>addr</b>. This string
+ * is statically allocated, and must not be freed. Each call to
+ * <b>fmt_addr_impl</b> invalidates the last result of the function.
+ * This function is not thread-safe. If <b>decorate</b> is set, add
+ * brackets to IPv6 addresses.
+ *
+ * It's better to use the wrapper macros of this function:
+ * <b>fmt_addr()</b> and <b>fmt_and_decorate_addr()</b>.
+ */
+const char *
+fmt_addr_impl(const tor_addr_t *addr, int decorate)
+{
+ static char buf[TOR_ADDR_BUF_LEN];
+ if (!addr) return "<null>";
+ if (tor_addr_to_str(buf, addr, sizeof(buf), decorate))
+ return buf;
+ else
+ return "???";
+}
+
+/** Return a string representing the pair <b>addr</b> and <b>port</b>.
+ * This calls fmt_and_decorate_addr internally, so IPv6 addresses will
+ * have brackets, and the caveats of fmt_addr_impl apply.
+ */
+const char *
+fmt_addrport(const tor_addr_t *addr, uint16_t port)
+{
+ /* Add space for a colon and up to 5 digits. */
+ static char buf[TOR_ADDR_BUF_LEN + 6];
+ tor_snprintf(buf, sizeof(buf), "%s:%u", fmt_and_decorate_addr(addr), port);
+ return buf;
+}
+
+/** Like fmt_addr(), but takes <b>addr</b> as a host-order IPv4
+ * addresses. Also not thread-safe, also clobbers its return buffer on
+ * repeated calls. */
+const char *
+fmt_addr32(uint32_t addr)
+{
+ static char buf[INET_NTOA_BUF_LEN];
+ struct in_addr in;
+ in.s_addr = htonl(addr);
+ tor_inet_ntoa(&in, buf, sizeof(buf));
+ return buf;
+}
+
+/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>. The string
+ * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
+ * square brackets.
+ *
+ * Return an address family on success, or -1 if an invalid address string is
+ * provided. */
+int
+tor_addr_parse(tor_addr_t *addr, const char *src)
+{
+ /* Holds substring of IPv6 address after removing square brackets */
+ char *tmp = NULL;
+ int result;
+ struct in_addr in_tmp;
+ struct in6_addr in6_tmp;
+ tor_assert(addr && src);
+ if (src[0] == '[' && src[1])
+ src = tmp = tor_strndup(src+1, strlen(src)-2);
+
+ if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
+ result = AF_INET6;
+ tor_addr_from_in6(addr, &in6_tmp);
+ } else if (tor_inet_pton(AF_INET, src, &in_tmp) > 0) {
+ result = AF_INET;
+ tor_addr_from_in(addr, &in_tmp);
+ } else {
+ result = -1;
+ }
+
+ tor_free(tmp);
+ return result;
+}
+
+#ifdef HAVE_IFADDRS_TO_SMARTLIST
+/*
+ * Convert a linked list consisting of <b>ifaddrs</b> structures
+ * into smartlist of <b>tor_addr_t</b> structures.
+ */
+STATIC smartlist_t *
+ifaddrs_to_smartlist(const struct ifaddrs *ifa, sa_family_t family)
+{
+ smartlist_t *result = smartlist_new();
+ const struct ifaddrs *i;
+
+ for (i = ifa; i; i = i->ifa_next) {
+ tor_addr_t tmp;
+ if ((i->ifa_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
+ continue;
+ if (!i->ifa_addr)
+ continue;
+ if (i->ifa_addr->sa_family != AF_INET &&
+ i->ifa_addr->sa_family != AF_INET6)
+ continue;
+ if (family != AF_UNSPEC && i->ifa_addr->sa_family != family)
+ continue;
+ if (tor_addr_from_sockaddr(&tmp, i->ifa_addr, NULL) < 0)
+ continue;
+ smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
+ }
+
+ return result;
+}
+
+/** Use getiffaddrs() function to get list of current machine
+ * network interface addresses. Represent the result by smartlist of
+ * <b>tor_addr_t</b> structures.
+ */
+STATIC smartlist_t *
+get_interface_addresses_ifaddrs(int severity, sa_family_t family)
+{
+
+ /* Most free Unixy systems provide getifaddrs, which gives us a linked list
+ * of struct ifaddrs. */
+ struct ifaddrs *ifa = NULL;
+ smartlist_t *result;
+ if (getifaddrs(&ifa) < 0) {
+ log_fn(severity, LD_NET, "Unable to call getifaddrs(): %s",
+ strerror(errno));
+ return NULL;
+ }
+
+ result = ifaddrs_to_smartlist(ifa, family);
+
+ freeifaddrs(ifa);
+
+ return result;
+}
+#endif /* defined(HAVE_IFADDRS_TO_SMARTLIST) */
+
+#ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
+
+/** Convert a Windows-specific <b>addresses</b> linked list into smartlist
+ * of <b>tor_addr_t</b> structures.
+ */
+
+STATIC smartlist_t *
+ip_adapter_addresses_to_smartlist(const IP_ADAPTER_ADDRESSES *addresses)
+{
+ smartlist_t *result = smartlist_new();
+ const IP_ADAPTER_ADDRESSES *address;
+
+ for (address = addresses; address; address = address->Next) {
+ const IP_ADAPTER_UNICAST_ADDRESS *a;
+ for (a = address->FirstUnicastAddress; a; a = a->Next) {
+ /* Yes, it's a linked list inside a linked list */
+ const struct sockaddr *sa = a->Address.lpSockaddr;
+ tor_addr_t tmp;
+ if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
+ continue;
+ if (tor_addr_from_sockaddr(&tmp, sa, NULL) < 0)
+ continue;
+ smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
+ }
+ }
+
+ return result;
+}
+
+/** Windows only: use GetAdaptersAddresses() to retrieve the network interface
+ * addresses of the current machine.
+ * Returns a smartlist of <b>tor_addr_t</b> structures.
+ */
+STATIC smartlist_t *
+get_interface_addresses_win32(int severity, sa_family_t family)
+{
+ smartlist_t *result = NULL;
+ ULONG size, res;
+ IP_ADAPTER_ADDRESSES *addresses = NULL;
+
+ (void) severity;
+
+#define FLAGS (GAA_FLAG_SKIP_ANYCAST | \
+ GAA_FLAG_SKIP_MULTICAST | \
+ GAA_FLAG_SKIP_DNS_SERVER)
+
+ /* Guess how much space we need. */
+ size = 15*1024;
+ addresses = tor_malloc(size);
+ /* Exists in windows XP and later. */
+ res = GetAdaptersAddresses(family, FLAGS, NULL, addresses, &size);
+ if (res == ERROR_BUFFER_OVERFLOW) {
+ /* we didn't guess that we needed enough space; try again */
+ tor_free(addresses);
+ addresses = tor_malloc(size);
+ res = GetAdaptersAddresses(AF_UNSPEC, FLAGS, NULL, addresses, &size);
+ }
+ if (res != NO_ERROR) {
+ log_fn(severity, LD_NET, "GetAdaptersAddresses failed (result: %lu)", res);
+ goto done;
+ }
+
+ result = ip_adapter_addresses_to_smartlist(addresses);
+
+ done:
+ tor_free(addresses);
+ return result;
+}
+
+#endif /* defined(HAVE_IP_ADAPTER_TO_SMARTLIST) */
+
+#ifdef HAVE_IFCONF_TO_SMARTLIST
+
+/* Guess how much space we need. There shouldn't be any struct ifreqs
+ * larger than this, even on OS X where the struct's size is dynamic. */
+#define IFREQ_SIZE 4096
+
+/* This is defined on Mac OS X */
+#ifndef _SIZEOF_ADDR_IFREQ
+#define _SIZEOF_ADDR_IFREQ sizeof
+#endif
+
+/* Free ifc->ifc_buf safely. */
+static void
+ifconf_free_ifc_buf(struct ifconf *ifc)
+{
+ /* On macOS, tor_free() takes the address of ifc.ifc_buf, which leads to
+ * undefined behaviour, because pointer-to-pointers are expected to be
+ * aligned at 8-bytes, but the ifconf structure is packed. So we use
+ * raw_free() instead. */
+ raw_free(ifc->ifc_buf);
+ ifc->ifc_buf = NULL;
+}
+
+/** Convert <b>*buf</b>, an ifreq structure array of size <b>buflen</b>,
+ * into smartlist of <b>tor_addr_t</b> structures.
+ */
+STATIC smartlist_t *
+ifreq_to_smartlist(char *buf, size_t buflen)
+{
+ smartlist_t *result = smartlist_new();
+ char *end = buf + buflen;
+
+ /* These acrobatics are due to alignment issues which trigger
+ * undefined behaviour traps on OSX. */
+ struct ifreq *r = tor_malloc(IFREQ_SIZE);
+
+ while (buf < end) {
+ /* Copy up to IFREQ_SIZE bytes into the struct ifreq, but don't overrun
+ * buf. */
+ memcpy(r, buf, end - buf < IFREQ_SIZE ? end - buf : IFREQ_SIZE);
+
+ const struct sockaddr *sa = &r->ifr_addr;
+ tor_addr_t tmp;
+ int valid_sa_family = (sa->sa_family == AF_INET ||
+ sa->sa_family == AF_INET6);
+
+ int conversion_success = (tor_addr_from_sockaddr(&tmp, sa, NULL) == 0);
+
+ if (valid_sa_family && conversion_success)
+ smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
+
+ buf += _SIZEOF_ADDR_IFREQ(*r);
+ }
+
+ tor_free(r);
+ return result;
+}
+
+/** Use ioctl(.,SIOCGIFCONF,.) to get a list of current machine
+ * network interface addresses. Represent the result by smartlist of
+ * <b>tor_addr_t</b> structures.
+ */
+STATIC smartlist_t *
+get_interface_addresses_ioctl(int severity, sa_family_t family)
+{
+ /* Some older unixy systems make us use ioctl(SIOCGIFCONF) */
+ struct ifconf ifc;
+ ifc.ifc_buf = NULL;
+ int fd;
+ smartlist_t *result = NULL;
+
+ /* This interface, AFAICT, only supports AF_INET addresses,
+ * except on AIX. For Solaris, we could use SIOCGLIFCONF. */
+
+ /* Bail out if family is neither AF_INET nor AF_UNSPEC since
+ * ioctl() technique supports non-IPv4 interface addresses on
+ * a small number of niche systems only. If family is AF_UNSPEC,
+ * fall back to getting AF_INET addresses only. */
+ if (family == AF_UNSPEC)
+ family = AF_INET;
+ else if (family != AF_INET)
+ return NULL;
+
+ fd = socket(family, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ tor_log(severity, LD_NET, "socket failed: %s", strerror(errno));
+ goto done;
+ }
+
+ int mult = 1;
+ do {
+ mult *= 2;
+ ifc.ifc_len = mult * IFREQ_SIZE;
+ ifc.ifc_buf = tor_realloc(ifc.ifc_buf, ifc.ifc_len);
+
+ tor_assert(ifc.ifc_buf);
+
+ if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
+ tor_log(severity, LD_NET, "ioctl failed: %s", strerror(errno));
+ goto done;
+ }
+ /* Ensure we have least IFREQ_SIZE bytes unused at the end. Otherwise, we
+ * don't know if we got everything during ioctl. */
+ } while (mult * IFREQ_SIZE - ifc.ifc_len <= IFREQ_SIZE);
+ result = ifreq_to_smartlist(ifc.ifc_buf, ifc.ifc_len);
+
+ done:
+ if (fd >= 0)
+ close(fd);
+ ifconf_free_ifc_buf(&ifc);
+ return result;
+}
+#endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
+
+/** Try to ask our network interfaces what addresses they are bound to.
+ * Return a new smartlist of tor_addr_t on success, and NULL on failure.
+ * (An empty smartlist indicates that we successfully learned that we have no
+ * addresses.) Log failure messages at <b>severity</b>. Only return the
+ * interface addresses of requested <b>family</b> and ignore the addresses
+ * of other address families. */
+MOCK_IMPL(smartlist_t *,
+get_interface_addresses_raw,(int severity, sa_family_t family))
+{
+ smartlist_t *result = NULL;
+#if defined(HAVE_IFADDRS_TO_SMARTLIST)
+ if ((result = get_interface_addresses_ifaddrs(severity, family)))
+ return result;
+#endif
+#if defined(HAVE_IP_ADAPTER_TO_SMARTLIST)
+ if ((result = get_interface_addresses_win32(severity, family)))
+ return result;
+#endif
+#if defined(HAVE_IFCONF_TO_SMARTLIST)
+ if ((result = get_interface_addresses_ioctl(severity, family)))
+ return result;
+#endif
+ (void) severity;
+ (void) result;
+ return NULL;
+}
+
+/** Return true iff <b>a</b> is a multicast address. */
+int
+tor_addr_is_multicast(const tor_addr_t *a)
+{
+ sa_family_t family = tor_addr_family(a);
+ if (family == AF_INET) {
+ uint32_t ipv4h = tor_addr_to_ipv4h(a);
+ if ((ipv4h >> 24) == 0xe0)
+ return 1; /* Multicast */
+ } else if (family == AF_INET6) {
+ const uint8_t *a32 = tor_addr_to_in6_addr8(a);
+ if (a32[0] == 0xff)
+ return 1;
+ }
+ return 0;
+}
+
+/** Attempt to retrieve IP address of current host by utilizing some
+ * UDP socket trickery. Only look for address of given <b>family</b>
+ * (only AF_INET and AF_INET6 are supported). Set result to *<b>addr</b>.
+ * Return 0 on success, -1 on failure.
+ */
+MOCK_IMPL(int,
+get_interface_address6_via_udp_socket_hack,(int severity,
+ sa_family_t family,
+ tor_addr_t *addr))
+{
+ struct sockaddr_storage target_addr;
+ int sock=-1, r=-1;
+ socklen_t addr_len;
+
+ memset(addr, 0, sizeof(tor_addr_t));
+ memset(&target_addr, 0, sizeof(target_addr));
+
+ /* Don't worry: no packets are sent. We just need to use a real address
+ * on the actual Internet. */
+ if (family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&target_addr;
+ /* Use the "discard" service port */
+ sin6->sin6_port = htons(9);
+ sock = tor_open_socket(PF_INET6,SOCK_DGRAM,IPPROTO_UDP);
+ addr_len = (socklen_t)sizeof(struct sockaddr_in6);
+ sin6->sin6_family = AF_INET6;
+ S6_ADDR16(sin6->sin6_addr)[0] = htons(0x2002); /* 2002:: */
+ } else if (family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in*)&target_addr;
+ /* Use the "discard" service port */
+ sin->sin_port = htons(9);
+ sock = tor_open_socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
+ addr_len = (socklen_t)sizeof(struct sockaddr_in);
+ sin->sin_family = AF_INET;
+ sin->sin_addr.s_addr = htonl(0x12000001); /* 18.0.0.1 */
+ } else {
+ return -1;
+ }
+
+ if (sock < 0) {
+ int e = tor_socket_errno(-1);
+ log_fn(severity, LD_NET, "unable to create socket: %s",
+ tor_socket_strerror(e));
+ goto err;
+ }
+
+ if (tor_connect_socket(sock,(struct sockaddr *)&target_addr,
+ addr_len) < 0) {
+ int e = tor_socket_errno(sock);
+ log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e));
+ goto err;
+ }
+
+ if (tor_addr_from_getsockname(addr, sock) < 0) {
+ int e = tor_socket_errno(sock);
+ log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s",
+ tor_socket_strerror(e));
+ goto err;
+ }
+
+ if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) {
+ log_fn(severity, LD_NET, "Address that we determined via UDP socket"
+ " magic is unsuitable for public comms.");
+ } else {
+ r=0;
+ }
+
+ err:
+ if (sock >= 0)
+ tor_close_socket(sock);
+ if (r == -1)
+ memset(addr, 0, sizeof(tor_addr_t));
+ return r;
+}
+
+/** Set *<b>addr</b> to an arbitrary IP address (if any) of an interface that
+ * connects to the Internet. Prefer public IP addresses to internal IP
+ * addresses. This address should only be used in checking whether our
+ * address has changed, as it may be an internal IP address. Return 0 on
+ * success, -1 on failure.
+ * Prefer get_interface_address6_list for a list of all addresses on all
+ * interfaces which connect to the Internet.
+ */
+MOCK_IMPL(int,
+get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr))
+{
+ smartlist_t *addrs;
+ int rv = -1;
+ tor_assert(addr);
+
+ memset(addr, 0, sizeof(tor_addr_t));
+
+ /* Get a list of public or internal IPs in arbitrary order */
+ addrs = get_interface_address6_list(severity, family, 1);
+
+ /* Find the first non-internal address, or the last internal address
+ * Ideally, we want the default route, see #12377 for details */
+ SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, a) {
+ tor_addr_copy(addr, a);
+ rv = 0;
+
+ /* If we found a non-internal address, declare success. Otherwise,
+ * keep looking. */
+ if (!tor_addr_is_internal(a, 0))
+ break;
+ } SMARTLIST_FOREACH_END(a);
+
+ interface_address6_list_free(addrs);
+ return rv;
+}
+
+/** Free a smartlist of IP addresses returned by get_interface_address6_list.
+ */
+void
+interface_address6_list_free_(smartlist_t *addrs)
+{
+ if (addrs != NULL) {
+ SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a));
+ smartlist_free(addrs);
+ }
+}
+
+/** Return a smartlist of the IP addresses of type family from all interfaces
+ * on the server. Excludes loopback and multicast addresses. Only includes
+ * internal addresses if include_internal is true. (Note that a relay behind
+ * NAT may use an internal address to connect to the Internet.)
+ * An empty smartlist means that there are no addresses of the selected type
+ * matching these criteria.
+ * Returns NULL on failure.
+ * Use interface_address6_list_free to free the returned list.
+ */
+MOCK_IMPL(smartlist_t *,
+get_interface_address6_list,(int severity,
+ sa_family_t family,
+ int include_internal))
+{
+ smartlist_t *addrs;
+ tor_addr_t addr;
+
+ /* Try to do this the smart way if possible. */
+ if ((addrs = get_interface_addresses_raw(severity, family))) {
+ SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, a)
+ {
+ if (tor_addr_is_loopback(a) ||
+ tor_addr_is_multicast(a)) {
+ SMARTLIST_DEL_CURRENT_KEEPORDER(addrs, a);
+ tor_free(a);
+ continue;
+ }
+
+ if (!include_internal && tor_addr_is_internal(a, 0)) {
+ SMARTLIST_DEL_CURRENT_KEEPORDER(addrs, a);
+ tor_free(a);
+ continue;
+ }
+ } SMARTLIST_FOREACH_END(a);
+ }
+
+ if (addrs && smartlist_len(addrs) > 0) {
+ return addrs;
+ }
+
+ /* if we removed all entries as unsuitable */
+ if (addrs) {
+ smartlist_free(addrs);
+ }
+
+ /* Okay, the smart way is out. */
+ addrs = smartlist_new();
+
+ if (family == AF_INET || family == AF_UNSPEC) {
+ if (get_interface_address6_via_udp_socket_hack(severity,AF_INET,
+ &addr) == 0) {
+ if (include_internal || !tor_addr_is_internal(&addr, 0)) {
+ smartlist_add(addrs, tor_memdup(&addr, sizeof(addr)));
+ }
+ }
+ }
+
+ if (family == AF_INET6 || family == AF_UNSPEC) {
+ if (get_interface_address6_via_udp_socket_hack(severity,AF_INET6,
+ &addr) == 0) {
+ if (include_internal || !tor_addr_is_internal(&addr, 0)) {
+ smartlist_add(addrs, tor_memdup(&addr, sizeof(addr)));
+ }
+ }
+ }
+
+ return addrs;
+}
+
+/* ======
+ * IPv4 helpers
+ * XXXX IPv6 deprecate some of these.
+ */
+
+/** Given an address of the form "ip:port", try to divide it into its
+ * ip and port portions, setting *<b>address_out</b> to a newly
+ * allocated string holding the address portion and *<b>port_out</b>
+ * to the port.
+ *
+ * Don't do DNS lookups and don't allow domain names in the "ip" field.
+ *
+ * If <b>default_port</b> is less than 0, don't accept <b>addrport</b> of the
+ * form "ip" or "ip:0". Otherwise, accept those forms, and set
+ * *<b>port_out</b> to <b>default_port</b>.
+ *
+ * Return 0 on success, -1 on failure. */
+int
+tor_addr_port_parse(int severity, const char *addrport,
+ tor_addr_t *address_out, uint16_t *port_out,
+ int default_port)
+{
+ int retval = -1;
+ int r;
+ char *addr_tmp = NULL;
+
+ tor_assert(addrport);
+ tor_assert(address_out);
+ tor_assert(port_out);
+
+ r = tor_addr_port_split(severity, addrport, &addr_tmp, port_out);
+ if (r < 0)
+ goto done;
+
+ if (!*port_out) {
+ if (default_port >= 0)
+ *port_out = default_port;
+ else
+ goto done;
+ }
+
+ /* make sure that address_out is an IP address */
+ if (tor_addr_parse(address_out, addr_tmp) < 0)
+ goto done;
+
+ retval = 0;
+
+ done:
+ tor_free(addr_tmp);
+ return retval;
+}
+
+/** Given an address of the form "host[:port]", try to divide it into its host
+ * and port portions, setting *<b>address_out</b> to a newly allocated string
+ * holding the address portion and *<b>port_out</b> to the port (or 0 if no
+ * port is given). Return 0 on success, -1 on failure. */
+int
+tor_addr_port_split(int severity, const char *addrport,
+ char **address_out, uint16_t *port_out)
+{
+ tor_addr_t a_tmp;
+ tor_assert(addrport);
+ tor_assert(address_out);
+ tor_assert(port_out);
+ /* We need to check for IPv6 manually because the logic below doesn't
+ * do a good job on IPv6 addresses that lack a port. */
+ if (tor_addr_parse(&a_tmp, addrport) == AF_INET6) {
+ *port_out = 0;
+ *address_out = tor_strdup(addrport);
+ return 0;
+ }
+
+ const char *colon;
+ char *address_ = NULL;
+ int port_;
+ int ok = 1;
+
+ colon = strrchr(addrport, ':');
+ if (colon) {
+ address_ = tor_strndup(addrport, colon-addrport);
+ port_ = (int) tor_parse_long(colon+1,10,1,65535,NULL,NULL);
+ if (!port_) {
+ log_fn(severity, LD_GENERAL, "Port %s out of range", escaped(colon+1));
+ ok = 0;
+ }
+ if (!port_out) {
+ char *esc_addrport = esc_for_log(addrport);
+ log_fn(severity, LD_GENERAL,
+ "Port %s given on %s when not required",
+ escaped(colon+1), esc_addrport);
+ tor_free(esc_addrport);
+ ok = 0;
+ }
+ } else {
+ address_ = tor_strdup(addrport);
+ port_ = 0;
+ }
+
+ if (ok) {
+ *address_out = address_;
+ } else {
+ *address_out = NULL;
+ tor_free(address_);
+ }
+
+ if (port_out)
+ *port_out = ok ? ((uint16_t) port_) : 0;
+
+ return ok ? 0 : -1;
+}
+
+/** If <b>mask</b> is an address mask for a bit-prefix, return the number of
+ * bits. Otherwise, return -1. */
+int
+addr_mask_get_bits(uint32_t mask)
+{
+ int i;
+ if (mask == 0)
+ return 0;
+ if (mask == 0xFFFFFFFFu)
+ return 32;
+ for (i=1; i<=32; ++i) {
+ if (mask == (uint32_t) ~((1u<<(32-i))-1)) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+/** Parse a string <b>s</b> in the format of (*|port(-maxport)?)?, setting the
+ * various *out pointers as appropriate. Return 0 on success, -1 on failure.
+ */
+int
+parse_port_range(const char *port, uint16_t *port_min_out,
+ uint16_t *port_max_out)
+{
+ int port_min, port_max, ok;
+ tor_assert(port_min_out);
+ tor_assert(port_max_out);
+
+ if (!port || *port == '\0' || strcmp(port, "*") == 0) {
+ port_min = 1;
+ port_max = 65535;
+ } else {
+ char *endptr = NULL;
+ port_min = (int)tor_parse_long(port, 10, 0, 65535, &ok, &endptr);
+ if (!ok) {
+ log_warn(LD_GENERAL,
+ "Malformed port %s on address range; rejecting.",
+ escaped(port));
+ return -1;
+ } else if (endptr && *endptr == '-') {
+ port = endptr+1;
+ endptr = NULL;
+ port_max = (int)tor_parse_long(port, 10, 1, 65535, &ok, &endptr);
+ if (!ok) {
+ log_warn(LD_GENERAL,
+ "Malformed port %s on address range; rejecting.",
+ escaped(port));
+ return -1;
+ }
+ } else {
+ port_max = port_min;
+ }
+ if (port_min > port_max) {
+ log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
+ return -1;
+ }
+ }
+
+ if (port_min < 1)
+ port_min = 1;
+ if (port_max > 65535)
+ port_max = 65535;
+
+ *port_min_out = (uint16_t) port_min;
+ *port_max_out = (uint16_t) port_max;
+
+ return 0;
+}
+
+/** Given a host-order <b>addr</b>, call tor_inet_ntop() on it
+ * and return a strdup of the resulting address.
+ */
+char *
+tor_dup_ip(uint32_t addr)
+{
+ char buf[TOR_ADDR_BUF_LEN];
+ struct in_addr in;
+
+ in.s_addr = htonl(addr);
+ tor_inet_ntop(AF_INET, &in, buf, sizeof(buf));
+ return tor_strdup(buf);
+}
+
+/**
+ * Set *<b>addr</b> to a host-order IPv4 address (if any) of an
+ * interface that connects to the Internet. Prefer public IP addresses to
+ * internal IP addresses. This address should only be used in checking
+ * whether our address has changed, as it may be an internal IPv4 address.
+ * Return 0 on success, -1 on failure.
+ * Prefer get_interface_address_list6 for a list of all IPv4 and IPv6
+ * addresses on all interfaces which connect to the Internet.
+ */
+MOCK_IMPL(int,
+get_interface_address,(int severity, uint32_t *addr))
+{
+ tor_addr_t local_addr;
+ int r;
+
+ memset(addr, 0, sizeof(uint32_t));
+
+ r = get_interface_address6(severity, AF_INET, &local_addr);
+ if (r>=0)
+ *addr = tor_addr_to_ipv4h(&local_addr);
+ return r;
+}
+
+/** Return true if we can tell that <b>name</b> is a canonical name for the
+ * loopback address. Return true also for *.local hostnames, which are
+ * multicast DNS names for hosts on the local network. */
+int
+tor_addr_hostname_is_local(const char *name)
+{
+ return !strcasecmp(name, "localhost") ||
+ !strcasecmp(name, "local") ||
+ !strcasecmpend(name, ".local");
+}
+
+/** Return a newly allocated tor_addr_port_t with <b>addr</b> and
+ <b>port</b> filled in. */
+tor_addr_port_t *
+tor_addr_port_new(const tor_addr_t *addr, uint16_t port)
+{
+ tor_addr_port_t *ap = tor_malloc_zero(sizeof(tor_addr_port_t));
+ if (addr)
+ tor_addr_copy(&ap->addr, addr);
+ ap->port = port;
+ return ap;
+}
+
+/** Return true iff <a>a</b> and <b>b</b> are the same address and port */
+int
+tor_addr_port_eq(const tor_addr_port_t *a,
+ const tor_addr_port_t *b)
+{
+ return tor_addr_eq(&a->addr, &b->addr) && a->port == b->port;
+}
+
+/** Return true if <b>string</b> represents a valid IPv4 adddress in
+ * 'a.b.c.d' form.
+ */
+int
+string_is_valid_ipv4_address(const char *string)
+{
+ struct in_addr addr;
+
+ return (tor_inet_pton(AF_INET,string,&addr) == 1);
+}
+
+/** Return true if <b>string</b> represents a valid IPv6 address in
+ * a form that inet_pton() can parse.
+ */
+int
+string_is_valid_ipv6_address(const char *string)
+{
+ struct in6_addr addr;
+
+ return (tor_inet_pton(AF_INET6,string,&addr) == 1);
+}
+
+/** Return true iff <b>string</b> is a valid destination address,
+ * i.e. either a DNS hostname or IPv4/IPv6 address string.
+ */
+int
+string_is_valid_dest(const char *string)
+{
+ char *tmp = NULL;
+ int retval;
+ size_t len;
+
+ if (string == NULL)
+ return 0;
+
+ len = strlen(string);
+
+ if (len == 0)
+ return 0;
+
+ if (string[0] == '[' && string[len - 1] == ']')
+ string = tmp = tor_strndup(string + 1, len - 2);
+
+ retval = string_is_valid_ipv4_address(string) ||
+ string_is_valid_ipv6_address(string) ||
+ string_is_valid_nonrfc_hostname(string);
+
+ tor_free(tmp);
+
+ return retval;
+}
+
+/** Return true iff <b>string</b> matches a pattern of DNS names
+ * that we allow Tor clients to connect to.
+ *
+ * Note: This allows certain technically invalid characters ('_') to cope
+ * with misconfigured zones that have been encountered in the wild.
+ */
+int
+string_is_valid_nonrfc_hostname(const char *string)
+{
+ int result = 1;
+ int has_trailing_dot;
+ char *last_label;
+ smartlist_t *components;
+
+ if (!string || strlen(string) == 0)
+ return 0;
+
+ if (string_is_valid_ipv4_address(string))
+ return 0;
+
+ components = smartlist_new();
+
+ smartlist_split_string(components,string,".",0,0);
+
+ if (BUG(smartlist_len(components) == 0))
+ return 0; // LCOV_EXCL_LINE should be impossible given the earlier checks.
+
+ /* Allow a single terminating '.' used rarely to indicate domains
+ * are FQDNs rather than relative. */
+ last_label = (char *)smartlist_get(components,
+ smartlist_len(components) - 1);
+ has_trailing_dot = (last_label[0] == '\0');
+ if (has_trailing_dot) {
+ smartlist_pop_last(components);
+ tor_free(last_label);
+ last_label = NULL;
+ }
+
+ SMARTLIST_FOREACH_BEGIN(components, char *, c) {
+ if ((c[0] == '-') || (*c == '_')) {
+ result = 0;
+ break;
+ }
+
+ do {
+ result = (TOR_ISALNUM(*c) || (*c == '-') || (*c == '_'));
+ c++;
+ } while (result && *c);
+
+ if (result == 0) {
+ break;
+ }
+ } SMARTLIST_FOREACH_END(c);
+
+ SMARTLIST_FOREACH_BEGIN(components, char *, c) {
+ tor_free(c);
+ } SMARTLIST_FOREACH_END(c);
+
+ smartlist_free(components);
+
+ return result;
+}
diff --cc src/test/test_addr.c
index 985f43b3f,5c4b6449c..8868edce2
--- a/src/test/test_addr.c
+++ b/src/test/test_addr.c
@@@ -1203,5 -1093,7 +1220,6 @@@ struct testcase_t addr_tests[] =
{ "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
{ "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
{ "make_null", test_addr_make_null, 0, NULL, NULL },
+ { "rfc6598", test_addr_rfc6598, 0, NULL, NULL },
END_OF_TESTCASES
};
-
1
0

[tor/release-0.4.0] Make tor_addr_is_internal_() RFC6598 (Carrier Grade NAT) aware
by asn@torproject.org 12 Mar '19
by asn@torproject.org 12 Mar '19
12 Mar '19
commit 63b404911441a7691949c475a374569d668a1b32
Author: Neel Chauhan <neel(a)neelc.org>
Date: Sat Mar 2 21:25:35 2019 -0500
Make tor_addr_is_internal_() RFC6598 (Carrier Grade NAT) aware
Fixes 28525.
---
changes/bug28525 | 8 ++++++++
src/common/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/changes/bug28525 b/changes/bug28525
new file mode 100644
index 000000000..392a9265e
--- /dev/null
+++ b/changes/bug28525
@@ -0,0 +1,8 @@
+ o Minor bugfixes (address selection):
+ - Make Tor aware of the RFC 6598 (Carrier Grade NAT) IP range, which is the
+ subnet 100.64.0.0/10. This is deployed by many ISPs as an alternative to
+ RFC 1918 that does not break existing internal networks. This patch fixes
+ security issues caused by RFC 6518 by blocking control ports on these
+ addresses and warns users if client ports or ExtORPorts are listening on
+ a RFC 6598 address. Fixes bug 28525; bugfix on 0.4.1.1-alpha. Patch by
+ Neel Chauhan.
diff --git a/src/common/address.c b/src/common/address.c
index 794345a13..71d380538 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -348,9 +348,18 @@ tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
}
}
-/** Return true iff <b>ip</b> is an IP reserved to localhost or local networks
- * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is
- * also treated as internal for now.)
+/** Return true iff <b>ip</b> is an IP reserved to localhost or local networks.
+ *
+ * If <b>ip</b> is in RFC1918 or RFC4193 or RFC4291, we will return true.
+ * (fec0::/10, deprecated by RFC3879, is also treated as internal for now
+ * and will return true.)
+ *
+ * If <b>ip</b> is 0.0.0.0 or 100.64.0.0/10 (RFC6598), we will act as:
+ * - Internal if <b>for_listening</b> is 0, as these addresses are not
+ * routable on the internet and we won't be publicly accessible to clients.
+ * - External if <b>for_listening</b> is 1, as clients could connect to us
+ * from the internet (in the case of 0.0.0.0) or a service provider's
+ * internal network (in the case of RFC6598).
*/
int
tor_addr_is_internal_(const tor_addr_t *addr, int for_listening,
@@ -398,11 +407,13 @@ tor_addr_is_internal_(const tor_addr_t *addr, int for_listening,
return 0;
} else if (v_family == AF_INET) {
- if (for_listening && !iph4) /* special case for binding to 0.0.0.0 */
+ /* special case for binding to 0.0.0.0 or 100.64/10 (RFC6598) */
+ if (for_listening && (!iph4 || ((iph4 & 0xffc00000) == 0x64400000)))
return 0;
if (((iph4 & 0xff000000) == 0x0a000000) || /* 10/8 */
((iph4 & 0xff000000) == 0x00000000) || /* 0/8 */
((iph4 & 0xff000000) == 0x7f000000) || /* 127/8 */
+ ((iph4 & 0xffc00000) == 0x64400000) || /* 100.64/10 */
((iph4 & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
((iph4 & 0xfff00000) == 0xac100000) || /* 172.16/12 */
((iph4 & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
diff --git a/src/test/test_addr.c b/src/test/test_addr.c
index be440a092..5c4b6449c 100644
--- a/src/test/test_addr.c
+++ b/src/test/test_addr.c
@@ -1063,6 +1063,23 @@ test_addr_make_null(void *data)
tor_free(zeros);
}
+#define TEST_ADDR_INTERNAL(a, for_listening, rv) STMT_BEGIN \
+ tor_addr_t t; \
+ tt_int_op(tor_inet_pton(AF_INET, a, &t.addr.in_addr), OP_EQ, 1); \
+ t.family = AF_INET; \
+ tt_int_op(tor_addr_is_internal(&t, for_listening), OP_EQ, rv); \
+ STMT_END;
+
+static void
+test_addr_rfc6598(void *arg)
+{
+ (void)arg;
+ TEST_ADDR_INTERNAL("100.64.0.1", 0, 1);
+ TEST_ADDR_INTERNAL("100.64.0.1", 1, 0);
+ done:
+ ;
+}
+
#define ADDR_LEGACY(name) \
{ #name, test_addr_ ## name , 0, NULL, NULL }
@@ -1076,6 +1093,7 @@ struct testcase_t addr_tests[] = {
{ "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
{ "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
{ "make_null", test_addr_make_null, 0, NULL, NULL },
+ { "rfc6598", test_addr_rfc6598, 0, NULL, NULL },
END_OF_TESTCASES
};
1
0

12 Mar '19
commit ab34f49be949db051a64e70cc68e24cf6de576ec
Merge: feda2231a 0ce32affc
Author: George Kadianakis <desnacked(a)riseup.net>
Date: Tue Mar 12 18:11:53 2019 +0200
Merge branch 'maint-0.4.0' into release-0.4.0
changes/bug28525 | 8 ++++++++
src/lib/net/address.c | 19 +++++++++++++++----
src/test/test_addr.c | 18 ++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
1
0

12 Mar '19
commit cdda3dc48457af31a9d1b3df1222d449b736f3b1
Author: teor <teor(a)torproject.org>
Date: Wed Jan 30 15:15:41 2019 +0100
hs: Move get_lspecs_from_node to nodelist.c
Also:
* rename to node_get_link_specifier_smartlist
* rewrite to return a smartlist
* add link_specifier_smartlist_free
Part of 23576.
---
src/feature/hs/hs_circuit.c | 86 ++----------------------------------
src/feature/nodelist/nodelist.c | 96 +++++++++++++++++++++++++++++++++++++++++
src/feature/nodelist/nodelist.h | 5 +++
3 files changed, 105 insertions(+), 82 deletions(-)
diff --git a/src/feature/hs/hs_circuit.c b/src/feature/hs/hs_circuit.c
index e3873d2f1..253c24d64 100644
--- a/src/feature/hs/hs_circuit.c
+++ b/src/feature/hs/hs_circuit.c
@@ -565,81 +565,6 @@ retry_service_rendezvous_point(const origin_circuit_t *circ)
return;
}
-/* Add all possible link specifiers in node to lspecs:
- * - legacy ID is mandatory thus MUST be present in node;
- * - include ed25519 link specifier if present in the node, and the node
- * supports ed25519 link authentication, even if its link versions are not
- * compatible with us;
- * - include IPv4 link specifier, if the primary address is not IPv4, log a
- * BUG() warning, and return an empty smartlist;
- * - include IPv6 link specifier if present in the node. */
-static void
-get_lspecs_from_node(const node_t *node, smartlist_t *lspecs)
-{
- link_specifier_t *ls;
- tor_addr_port_t ap;
-
- tor_assert(node);
- tor_assert(lspecs);
-
- /* Get the relay's IPv4 address. */
- node_get_prim_orport(node, &ap);
-
- /* We expect the node's primary address to be a valid IPv4 address.
- * This conforms to the protocol, which requires either an IPv4 or IPv6
- * address (or both). */
- if (BUG(!tor_addr_is_v4(&ap.addr)) ||
- BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
- return;
- }
-
- ls = link_specifier_new();
- link_specifier_set_ls_type(ls, LS_IPV4);
- link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
- link_specifier_set_un_ipv4_port(ls, ap.port);
- /* Four bytes IPv4 and two bytes port. */
- link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
- sizeof(ap.port));
- smartlist_add(lspecs, ls);
-
- /* Legacy ID is mandatory and will always be present in node. */
- ls = link_specifier_new();
- link_specifier_set_ls_type(ls, LS_LEGACY_ID);
- memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
- link_specifier_getlen_un_legacy_id(ls));
- link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
- smartlist_add(lspecs, ls);
-
- /* ed25519 ID is only included if the node has it, and the node declares a
- protocol version that supports ed25519 link authentication, even if that
- link version is not compatible with us. (We are sending the ed25519 key
- to another tor, which may support different link versions.) */
- if (!ed25519_public_key_is_zero(&node->ed25519_id) &&
- node_supports_ed25519_link_authentication(node, 0)) {
- ls = link_specifier_new();
- link_specifier_set_ls_type(ls, LS_ED25519_ID);
- memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
- link_specifier_getlen_un_ed25519_id(ls));
- link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
- smartlist_add(lspecs, ls);
- }
-
- /* Check for IPv6. If so, include it as well. */
- if (node_has_ipv6_orport(node)) {
- ls = link_specifier_new();
- node_get_pref_ipv6_orport(node, &ap);
- link_specifier_set_ls_type(ls, LS_IPV6);
- size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
- const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
- uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
- memcpy(ipv6_array, in6_addr, addr_len);
- link_specifier_set_un_ipv6_port(ls, ap.port);
- /* Sixteen bytes IPv6 and two bytes port. */
- link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
- smartlist_add(lspecs, ls);
- }
-}
-
/* Using the given descriptor intro point ip, the node of the
* rendezvous point rp_node and the service's subcredential, populate the
* already allocated intro1_data object with the needed key material and link
@@ -662,10 +587,9 @@ setup_introduce1_data(const hs_desc_intro_point_t *ip,
tor_assert(subcredential);
tor_assert(intro1_data);
- /* Build the link specifiers from the extend information of the rendezvous
- * circuit that we've picked previously. */
- rp_lspecs = smartlist_new();
- get_lspecs_from_node(rp_node, rp_lspecs);
+ /* Build the link specifiers from the node at the end of the rendezvous
+ * circuit that we opened for this introduction. */
+ rp_lspecs = node_get_link_specifier_smartlist(rp_node, 0);
if (smartlist_len(rp_lspecs) == 0) {
/* We can't rendezvous without link specifiers. */
smartlist_free(rp_lspecs);
@@ -1044,9 +968,7 @@ hs_circ_handle_introduce2(const hs_service_t *service,
ret = 0;
done:
- SMARTLIST_FOREACH(data.link_specifiers, link_specifier_t *, lspec,
- link_specifier_free(lspec));
- smartlist_free(data.link_specifiers);
+ link_specifier_smartlist_free(data.link_specifiers);
memwipe(&data, 0, sizeof(data));
return ret;
}
diff --git a/src/feature/nodelist/nodelist.c b/src/feature/nodelist/nodelist.c
index 9a2770180..32ee2677b 100644
--- a/src/feature/nodelist/nodelist.c
+++ b/src/feature/nodelist/nodelist.c
@@ -1189,6 +1189,102 @@ node_get_rsa_id_digest(const node_t *node)
return (const uint8_t*)node->identity;
}
+/* Returns a new smartlist with all possible link specifiers from node:
+ * - legacy ID is mandatory thus MUST be present in node;
+ * - include ed25519 link specifier if present in the node, and the node
+ * supports ed25519 link authentication, and:
+ * - if direct_conn is true, its link versions are compatible with us,
+ * - if direct_conn is false, regardless of its link versions;
+ * - include IPv4 link specifier, if the primary address is not IPv4, log a
+ * BUG() warning, and return an empty smartlist;
+ * - include IPv6 link specifier if present in the node.
+ *
+ * If node is NULL, returns an empty smartlist.
+ *
+ * The smartlist must be freed using link_specifier_smartlist_free(). */
+smartlist_t *
+node_get_link_specifier_smartlist(const node_t *node, bool direct_conn)
+{
+ link_specifier_t *ls;
+ tor_addr_port_t ap;
+ smartlist_t *lspecs = smartlist_new();
+
+ if (!node)
+ return lspecs;
+
+ /* Get the relay's IPv4 address. */
+ node_get_prim_orport(node, &ap);
+
+ /* We expect the node's primary address to be a valid IPv4 address.
+ * This conforms to the protocol, which requires either an IPv4 or IPv6
+ * address (or both). */
+ if (BUG(!tor_addr_is_v4(&ap.addr)) ||
+ BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
+ return lspecs;
+ }
+
+ ls = link_specifier_new();
+ link_specifier_set_ls_type(ls, LS_IPV4);
+ link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
+ link_specifier_set_un_ipv4_port(ls, ap.port);
+ /* Four bytes IPv4 and two bytes port. */
+ link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
+ sizeof(ap.port));
+ smartlist_add(lspecs, ls);
+
+ /* Legacy ID is mandatory and will always be present in node. */
+ ls = link_specifier_new();
+ link_specifier_set_ls_type(ls, LS_LEGACY_ID);
+ memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
+ link_specifier_getlen_un_legacy_id(ls));
+ link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
+ smartlist_add(lspecs, ls);
+
+ /* ed25519 ID is only included if the node has it, and the node declares a
+ protocol version that supports ed25519 link authentication.
+ If direct_conn is true, we also require that the node's link version is
+ compatible with us. (Otherwise, we will be sending the ed25519 key
+ to another tor, which may support different link versions.) */
+ if (!ed25519_public_key_is_zero(&node->ed25519_id) &&
+ node_supports_ed25519_link_authentication(node, direct_conn)) {
+ ls = link_specifier_new();
+ link_specifier_set_ls_type(ls, LS_ED25519_ID);
+ memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
+ link_specifier_getlen_un_ed25519_id(ls));
+ link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
+ smartlist_add(lspecs, ls);
+ }
+
+ /* Check for IPv6. If so, include it as well. */
+ if (node_has_ipv6_orport(node)) {
+ ls = link_specifier_new();
+ node_get_pref_ipv6_orport(node, &ap);
+ link_specifier_set_ls_type(ls, LS_IPV6);
+ size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
+ const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
+ uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
+ memcpy(ipv6_array, in6_addr, addr_len);
+ link_specifier_set_un_ipv6_port(ls, ap.port);
+ /* Sixteen bytes IPv6 and two bytes port. */
+ link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
+ smartlist_add(lspecs, ls);
+ }
+
+ return lspecs;
+}
+
+/* Free a link specifier list. */
+void
+link_specifier_smartlist_free_(smartlist_t *ls_list)
+{
+ if (!ls_list)
+ return;
+
+ SMARTLIST_FOREACH(ls_list, link_specifier_t *, lspec,
+ link_specifier_free(lspec));
+ smartlist_free(ls_list);
+}
+
/** Return the nickname of <b>node</b>, or NULL if we can't find one. */
const char *
node_get_nickname(const node_t *node)
diff --git a/src/feature/nodelist/nodelist.h b/src/feature/nodelist/nodelist.h
index 342095961..a3d65347a 100644
--- a/src/feature/nodelist/nodelist.h
+++ b/src/feature/nodelist/nodelist.h
@@ -77,6 +77,11 @@ int node_supports_v3_hsdir(const node_t *node);
int node_supports_ed25519_hs_intro(const node_t *node);
int node_supports_v3_rendezvous_point(const node_t *node);
const uint8_t *node_get_rsa_id_digest(const node_t *node);
+smartlist_t *node_get_link_specifier_smartlist(const node_t *node,
+ bool direct_conn);
+void link_specifier_smartlist_free_(smartlist_t *ls_list);
+#define link_specifier_smartlist_free(ls_list) \
+ FREE_AND_NULL(smartlist_t, link_specifier_smartlist_free_, (ls_list))
int node_has_ipv6_addr(const node_t *node);
int node_has_ipv6_orport(const node_t *node);
1
0
commit 680b2afd84a318e11f79526bfedff29870307ed0
Author: teor <teor(a)torproject.org>
Date: Fri Mar 8 16:47:20 2019 +1000
hs: abolish hs_desc_link_specifier_dup()
The previous commits introduced link_specifier_dup(), which is
implemented using trunnel's opaque interfaces. So we can now
remove hs_desc_link_specifier_dup().
Cleanup after bug 22781.
---
src/feature/hs/hs_cell.c | 2 +-
src/feature/hs/hs_common.c | 18 ------------------
src/feature/hs/hs_common.h | 2 --
src/trunnel/ed25519_cert.trunnel | 6 ------
4 files changed, 1 insertion(+), 27 deletions(-)
diff --git a/src/feature/hs/hs_cell.c b/src/feature/hs/hs_cell.c
index e24520da4..1dae9c79c 100644
--- a/src/feature/hs/hs_cell.c
+++ b/src/feature/hs/hs_cell.c
@@ -759,7 +759,7 @@ hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
if (BUG(!lspec)) {
goto done;
}
- link_specifier_t *lspec_dup = hs_link_specifier_dup(lspec);
+ link_specifier_t *lspec_dup = link_specifier_dup(lspec);
if (BUG(!lspec_dup)) {
goto done;
}
diff --git a/src/feature/hs/hs_common.c b/src/feature/hs/hs_common.c
index a1dc0a629..2cc23a418 100644
--- a/src/feature/hs/hs_common.c
+++ b/src/feature/hs/hs_common.c
@@ -1009,24 +1009,6 @@ hs_build_address(const ed25519_public_key_t *key, uint8_t version,
tor_assert(hs_address_is_valid(addr_out));
}
-/* Return a newly allocated copy of lspec. */
-link_specifier_t *
-hs_link_specifier_dup(const link_specifier_t *lspec)
-{
- link_specifier_t *result = link_specifier_new();
- memcpy(result, lspec, sizeof(*result));
- /* The unrecognized field is a dynamic array so make sure to copy its
- * content and not the pointer. */
- link_specifier_setlen_un_unrecognized(
- result, link_specifier_getlen_un_unrecognized(lspec));
- if (link_specifier_getlen_un_unrecognized(result)) {
- memcpy(link_specifier_getarray_un_unrecognized(result),
- link_specifier_getconstarray_un_unrecognized(lspec),
- link_specifier_getlen_un_unrecognized(result));
- }
- return result;
-}
-
/* From a given ed25519 public key pk and an optional secret, compute a
* blinded public key and put it in blinded_pk_out. This is only useful to
* the client side because the client only has access to the identity public
diff --git a/src/feature/hs/hs_common.h b/src/feature/hs/hs_common.h
index 544e63e59..abf39fa43 100644
--- a/src/feature/hs/hs_common.h
+++ b/src/feature/hs/hs_common.h
@@ -217,8 +217,6 @@ uint64_t hs_get_time_period_num(time_t now);
uint64_t hs_get_next_time_period_num(time_t now);
time_t hs_get_start_time_of_next_time_period(time_t now);
-link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
-
MOCK_DECL(int, hs_in_period_between_tp_and_srv,
(const networkstatus_t *consensus, time_t now));
diff --git a/src/trunnel/ed25519_cert.trunnel b/src/trunnel/ed25519_cert.trunnel
index 8d6483d55..e424ce546 100644
--- a/src/trunnel/ed25519_cert.trunnel
+++ b/src/trunnel/ed25519_cert.trunnel
@@ -28,12 +28,6 @@ const LS_IPV6 = 0x01;
const LS_LEGACY_ID = 0x02;
const LS_ED25519_ID = 0x03;
-// XXX hs_link_specifier_dup() violates the opaqueness of link_specifier_t by
-// taking its sizeof(). If we ever want to turn on TRUNNEL_OPAQUE, or
-// if we ever make link_specifier contain other types, we will
-// need to refactor that function to do the copy by encoding and decoding the
-// object.
-
// amended from tor.trunnel
struct link_specifier {
u8 ls_type;
1
0
commit bb98bc8562c59c0357e8911fa8952ed5ee20206d
Author: teor <teor(a)torproject.org>
Date: Thu Aug 23 19:21:26 2018 +1000
hs: abolish hs_desc_link_specifier_t
The previous commits for 23576 confused hs_desc_link_specifier_t
and link_specifier_t. Removing hs_desc_link_specifier_t fixes this
confusion.
Fixes bug 22781; bugfix on 0.3.2.1-alpha.
---
changes/bug22781 | 4 +
src/feature/hs/hs_cell.c | 9 ++-
src/feature/hs/hs_client.c | 21 ++---
src/feature/hs/hs_common.c | 39 +++++++++
src/feature/hs/hs_common.h | 2 +
src/feature/hs/hs_descriptor.c | 178 ++++-------------------------------------
src/feature/hs/hs_descriptor.h | 29 +------
src/feature/hs/hs_intropoint.c | 4 +-
src/feature/hs/hs_service.c | 50 ++++++------
src/test/hs_test_helpers.c | 85 ++++++++++++++------
src/test/test_hs_client.c | 4 +
src/test/test_hs_descriptor.c | 111 -------------------------
src/test/test_hs_service.c | 20 ++---
13 files changed, 175 insertions(+), 381 deletions(-)
diff --git a/changes/bug22781 b/changes/bug22781
new file mode 100644
index 000000000..5606dfa5e
--- /dev/null
+++ b/changes/bug22781
@@ -0,0 +1,4 @@
+ o Code simplification and refactoring:
+ - Replace hs_desc_link_specifier_t with link_specifier_t,
+ and remove all hs_desc_link_specifier_t-specific code.
+ Fixes bug 22781; bugfix on 0.3.2.1-alpha.
diff --git a/src/feature/hs/hs_cell.c b/src/feature/hs/hs_cell.c
index 597982b34..e24520da4 100644
--- a/src/feature/hs/hs_cell.c
+++ b/src/feature/hs/hs_cell.c
@@ -756,7 +756,14 @@ hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
link_specifier_t *lspec =
trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
- smartlist_add(data->link_specifiers, hs_link_specifier_dup(lspec));
+ if (BUG(!lspec)) {
+ goto done;
+ }
+ link_specifier_t *lspec_dup = hs_link_specifier_dup(lspec);
+ if (BUG(!lspec_dup)) {
+ goto done;
+ }
+ smartlist_add(data->link_specifiers, lspec_dup);
}
/* Success. */
diff --git a/src/feature/hs/hs_client.c b/src/feature/hs/hs_client.c
index d4eee50bb..9d0dee712 100644
--- a/src/feature/hs/hs_client.c
+++ b/src/feature/hs/hs_client.c
@@ -528,13 +528,15 @@ find_desc_intro_point_by_legacy_id(const char *legacy_id,
SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
hs_desc_intro_point_t *, ip) {
SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
- const hs_desc_link_specifier_t *, lspec) {
+ const link_specifier_t *, lspec) {
/* Not all tor node have an ed25519 identity key so we still rely on the
* legacy identity digest. */
- if (lspec->type != LS_LEGACY_ID) {
+ if (link_specifier_get_ls_type(lspec) != LS_LEGACY_ID) {
continue;
}
- if (fast_memneq(legacy_id, lspec->u.legacy_id, DIGEST_LEN)) {
+ if (fast_memneq(legacy_id,
+ link_specifier_getconstarray_un_legacy_id(lspec),
+ DIGEST_LEN)) {
break;
}
/* Found it. */
@@ -753,24 +755,13 @@ STATIC extend_info_t *
desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
{
extend_info_t *ei;
- smartlist_t *lspecs = smartlist_new();
tor_assert(ip);
- /* We first encode the descriptor link specifiers into the binary
- * representation which is a trunnel object. */
- SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
- const hs_desc_link_specifier_t *, desc_lspec) {
- link_specifier_t *lspec = hs_desc_lspec_to_trunnel(desc_lspec);
- smartlist_add(lspecs, lspec);
- } SMARTLIST_FOREACH_END(desc_lspec);
-
/* Explicitly put the direct connection option to 0 because this is client
* side and there is no such thing as a non anonymous client. */
- ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
+ ei = hs_get_extend_info_from_lspecs(ip->link_specifiers, &ip->onion_key, 0);
- SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
- smartlist_free(lspecs);
return ei;
}
diff --git a/src/feature/hs/hs_common.c b/src/feature/hs/hs_common.c
index 5d8f54230..a1dc0a629 100644
--- a/src/feature/hs/hs_common.c
+++ b/src/feature/hs/hs_common.c
@@ -1843,3 +1843,42 @@ hs_inc_rdv_stream_counter(origin_circuit_t *circ)
tor_assert_nonfatal_unreached();
}
}
+
+/* Return a newly allocated link specifier object that is a copy of dst. */
+link_specifier_t *
+link_specifier_dup(const link_specifier_t *src)
+{
+ link_specifier_t *dup = NULL;
+ uint8_t *buf = NULL;
+
+ if (BUG(!src)) {
+ goto err;
+ }
+
+ ssize_t encoded_len_alloc = link_specifier_encoded_len(src);
+ if (BUG(encoded_len_alloc < 0)) {
+ goto err;
+ }
+
+ buf = tor_malloc_zero(encoded_len_alloc);
+ ssize_t encoded_len_data = link_specifier_encode(buf,
+ encoded_len_alloc,
+ src);
+ if (BUG(encoded_len_data < 0)) {
+ goto err;
+ }
+
+ ssize_t parsed_len = link_specifier_parse(&dup, buf, encoded_len_alloc);
+ if (BUG(parsed_len < 0)) {
+ goto err;
+ }
+
+ goto done;
+
+ err:
+ dup = NULL;
+
+ done:
+ tor_free(buf);
+ return dup;
+}
diff --git a/src/feature/hs/hs_common.h b/src/feature/hs/hs_common.h
index a44505930..544e63e59 100644
--- a/src/feature/hs/hs_common.h
+++ b/src/feature/hs/hs_common.h
@@ -262,6 +262,8 @@ extend_info_t *hs_get_extend_info_from_lspecs(const smartlist_t *lspecs,
const struct curve25519_public_key_t *onion_key,
int direct_conn);
+link_specifier_t *link_specifier_dup(const link_specifier_t *src);
+
#ifdef HS_COMMON_PRIVATE
STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out);
diff --git a/src/feature/hs/hs_descriptor.c b/src/feature/hs/hs_descriptor.c
index b09d50e01..8f7bdf86e 100644
--- a/src/feature/hs/hs_descriptor.c
+++ b/src/feature/hs/hs_descriptor.c
@@ -324,12 +324,11 @@ encode_link_specifiers(const smartlist_t *specs)
link_specifier_list_set_n_spec(lslist, smartlist_len(specs));
- SMARTLIST_FOREACH_BEGIN(specs, const hs_desc_link_specifier_t *,
+ SMARTLIST_FOREACH_BEGIN(specs, const link_specifier_t *,
spec) {
- link_specifier_t *ls = hs_desc_lspec_to_trunnel(spec);
- if (ls) {
- link_specifier_list_add_spec(lslist, ls);
- }
+ link_specifier_t *ls = link_specifier_dup(spec);
+ tor_assert(ls);
+ link_specifier_list_add_spec(lslist, ls);
} SMARTLIST_FOREACH_END(spec);
{
@@ -1190,52 +1189,22 @@ decode_link_specifiers(const char *encoded)
results = smartlist_new();
for (i = 0; i < link_specifier_list_getlen_spec(specs); i++) {
- hs_desc_link_specifier_t *hs_spec;
link_specifier_t *ls = link_specifier_list_get_spec(specs, i);
- tor_assert(ls);
-
- hs_spec = tor_malloc_zero(sizeof(*hs_spec));
- hs_spec->type = link_specifier_get_ls_type(ls);
- switch (hs_spec->type) {
- case LS_IPV4:
- tor_addr_from_ipv4h(&hs_spec->u.ap.addr,
- link_specifier_get_un_ipv4_addr(ls));
- hs_spec->u.ap.port = link_specifier_get_un_ipv4_port(ls);
- break;
- case LS_IPV6:
- tor_addr_from_ipv6_bytes(&hs_spec->u.ap.addr, (const char *)
- link_specifier_getarray_un_ipv6_addr(ls));
- hs_spec->u.ap.port = link_specifier_get_un_ipv6_port(ls);
- break;
- case LS_LEGACY_ID:
- /* Both are known at compile time so let's make sure they are the same
- * else we can copy memory out of bound. */
- tor_assert(link_specifier_getlen_un_legacy_id(ls) ==
- sizeof(hs_spec->u.legacy_id));
- memcpy(hs_spec->u.legacy_id, link_specifier_getarray_un_legacy_id(ls),
- sizeof(hs_spec->u.legacy_id));
- break;
- case LS_ED25519_ID:
- /* Both are known at compile time so let's make sure they are the same
- * else we can copy memory out of bound. */
- tor_assert(link_specifier_getlen_un_ed25519_id(ls) ==
- sizeof(hs_spec->u.ed25519_id));
- memcpy(hs_spec->u.ed25519_id,
- link_specifier_getconstarray_un_ed25519_id(ls),
- sizeof(hs_spec->u.ed25519_id));
- break;
- default:
- tor_free(hs_spec);
+ if (BUG(!ls)) {
goto err;
}
-
- smartlist_add(results, hs_spec);
+ link_specifier_t *ls_dup = link_specifier_dup(ls);
+ if (BUG(!ls_dup)) {
+ goto err;
+ }
+ smartlist_add(results, ls_dup);
}
goto done;
err:
if (results) {
- SMARTLIST_FOREACH(results, hs_desc_link_specifier_t *, s, tor_free(s));
+ SMARTLIST_FOREACH(results, link_specifier_t *, s,
+ link_specifier_free(s));
smartlist_free(results);
results = NULL;
}
@@ -2878,8 +2847,8 @@ hs_desc_intro_point_free_(hs_desc_intro_point_t *ip)
return;
}
if (ip->link_specifiers) {
- SMARTLIST_FOREACH(ip->link_specifiers, hs_desc_link_specifier_t *,
- ls, hs_desc_link_specifier_free(ls));
+ SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *,
+ ls, link_specifier_free(ls));
smartlist_free(ip->link_specifiers);
}
tor_cert_free(ip->auth_key_cert);
@@ -2972,69 +2941,6 @@ hs_desc_authorized_client_free_(hs_desc_authorized_client_t *client)
tor_free(client);
}
-/* Free the given descriptor link specifier. */
-void
-hs_desc_link_specifier_free_(hs_desc_link_specifier_t *ls)
-{
- if (ls == NULL) {
- return;
- }
- tor_free(ls);
-}
-
-/* Return a newly allocated descriptor link specifier using the given extend
- * info and requested type. Return NULL on error. */
-hs_desc_link_specifier_t *
-hs_desc_link_specifier_new(const extend_info_t *info, uint8_t type)
-{
- hs_desc_link_specifier_t *ls = NULL;
-
- tor_assert(info);
-
- ls = tor_malloc_zero(sizeof(*ls));
- ls->type = type;
- switch (ls->type) {
- case LS_IPV4:
- if (info->addr.family != AF_INET) {
- goto err;
- }
- tor_addr_copy(&ls->u.ap.addr, &info->addr);
- ls->u.ap.port = info->port;
- break;
- case LS_IPV6:
- if (info->addr.family != AF_INET6) {
- goto err;
- }
- tor_addr_copy(&ls->u.ap.addr, &info->addr);
- ls->u.ap.port = info->port;
- break;
- case LS_LEGACY_ID:
- /* Bug out if the identity digest is not set */
- if (BUG(tor_mem_is_zero(info->identity_digest,
- sizeof(info->identity_digest)))) {
- goto err;
- }
- memcpy(ls->u.legacy_id, info->identity_digest, sizeof(ls->u.legacy_id));
- break;
- case LS_ED25519_ID:
- /* ed25519 keys are optional for intro points */
- if (ed25519_public_key_is_zero(&info->ed_identity)) {
- goto err;
- }
- memcpy(ls->u.ed25519_id, info->ed_identity.pubkey,
- sizeof(ls->u.ed25519_id));
- break;
- default:
- /* Unknown type is code flow error. */
- tor_assert(0);
- }
-
- return ls;
- err:
- tor_free(ls);
- return NULL;
-}
-
/* From the given descriptor, remove and free every introduction point. */
void
hs_descriptor_clear_intro_points(hs_descriptor_t *desc)
@@ -3050,59 +2956,3 @@ hs_descriptor_clear_intro_points(hs_descriptor_t *desc)
smartlist_clear(ips);
}
}
-
-/* From a descriptor link specifier object spec, returned a newly allocated
- * link specifier object that is the encoded representation of spec. Return
- * NULL on error. */
-link_specifier_t *
-hs_desc_lspec_to_trunnel(const hs_desc_link_specifier_t *spec)
-{
- tor_assert(spec);
-
- link_specifier_t *ls = link_specifier_new();
- link_specifier_set_ls_type(ls, spec->type);
-
- switch (spec->type) {
- case LS_IPV4:
- link_specifier_set_un_ipv4_addr(ls,
- tor_addr_to_ipv4h(&spec->u.ap.addr));
- link_specifier_set_un_ipv4_port(ls, spec->u.ap.port);
- /* Four bytes IPv4 and two bytes port. */
- link_specifier_set_ls_len(ls, sizeof(spec->u.ap.addr.addr.in_addr) +
- sizeof(spec->u.ap.port));
- break;
- case LS_IPV6:
- {
- size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
- const uint8_t *in6_addr = tor_addr_to_in6_addr8(&spec->u.ap.addr);
- uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
- memcpy(ipv6_array, in6_addr, addr_len);
- link_specifier_set_un_ipv6_port(ls, spec->u.ap.port);
- /* Sixteen bytes IPv6 and two bytes port. */
- link_specifier_set_ls_len(ls, addr_len + sizeof(spec->u.ap.port));
- break;
- }
- case LS_LEGACY_ID:
- {
- size_t legacy_id_len = link_specifier_getlen_un_legacy_id(ls);
- uint8_t *legacy_id_array = link_specifier_getarray_un_legacy_id(ls);
- memcpy(legacy_id_array, spec->u.legacy_id, legacy_id_len);
- link_specifier_set_ls_len(ls, legacy_id_len);
- break;
- }
- case LS_ED25519_ID:
- {
- size_t ed25519_id_len = link_specifier_getlen_un_ed25519_id(ls);
- uint8_t *ed25519_id_array = link_specifier_getarray_un_ed25519_id(ls);
- memcpy(ed25519_id_array, spec->u.ed25519_id, ed25519_id_len);
- link_specifier_set_ls_len(ls, ed25519_id_len);
- break;
- }
- default:
- tor_assert_nonfatal_unreached();
- link_specifier_free(ls);
- ls = NULL;
- }
-
- return ls;
-}
diff --git a/src/feature/hs/hs_descriptor.h b/src/feature/hs/hs_descriptor.h
index 04a8e16d6..dbe0cb1c9 100644
--- a/src/feature/hs/hs_descriptor.h
+++ b/src/feature/hs/hs_descriptor.h
@@ -69,28 +69,10 @@ typedef enum {
HS_DESC_AUTH_ED25519 = 1
} hs_desc_auth_type_t;
-/* Link specifier object that contains information on how to extend to the
- * relay that is the address, port and handshake type. */
-typedef struct hs_desc_link_specifier_t {
- /* Indicate the type of link specifier. See trunnel ed25519_cert
- * specification. */
- uint8_t type;
-
- /* It must be one of these types, can't be more than one. */
- union {
- /* IP address and port of the relay use to extend. */
- tor_addr_port_t ap;
- /* Legacy identity. A 20-byte SHA1 identity fingerprint. */
- uint8_t legacy_id[DIGEST_LEN];
- /* ed25519 identity. A 32-byte key. */
- uint8_t ed25519_id[ED25519_PUBKEY_LEN];
- } u;
-} hs_desc_link_specifier_t;
-
/* Introduction point information located in a descriptor. */
typedef struct hs_desc_intro_point_t {
/* Link specifier(s) which details how to extend to the relay. This list
- * contains hs_desc_link_specifier_t object. It MUST have at least one. */
+ * contains link_specifier_t objects. It MUST have at least one. */
smartlist_t *link_specifiers;
/* Onion key of the introduction point used to extend to it for the ntor
@@ -261,12 +243,6 @@ void hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc);
#define hs_desc_encrypted_data_free(desc) \
FREE_AND_NULL(hs_desc_encrypted_data_t, hs_desc_encrypted_data_free_, (desc))
-void hs_desc_link_specifier_free_(hs_desc_link_specifier_t *ls);
-#define hs_desc_link_specifier_free(ls) \
- FREE_AND_NULL(hs_desc_link_specifier_t, hs_desc_link_specifier_free_, (ls))
-
-hs_desc_link_specifier_t *hs_desc_link_specifier_new(
- const extend_info_t *info, uint8_t type);
void hs_descriptor_clear_intro_points(hs_descriptor_t *desc);
MOCK_DECL(int,
@@ -299,9 +275,6 @@ void hs_desc_authorized_client_free_(hs_desc_authorized_client_t *client);
FREE_AND_NULL(hs_desc_authorized_client_t, \
hs_desc_authorized_client_free_, (client))
-link_specifier_t *hs_desc_lspec_to_trunnel(
- const hs_desc_link_specifier_t *spec);
-
hs_desc_authorized_client_t *hs_desc_build_fake_authorized_client(void);
void hs_desc_build_authorized_client(const uint8_t *subcredential,
const curve25519_public_key_t *
diff --git a/src/feature/hs/hs_intropoint.c b/src/feature/hs/hs_intropoint.c
index b28a5c2b8..c9cd3a041 100644
--- a/src/feature/hs/hs_intropoint.c
+++ b/src/feature/hs/hs_intropoint.c
@@ -601,8 +601,8 @@ hs_intropoint_clear(hs_intropoint_t *ip)
return;
}
tor_cert_free(ip->auth_key_cert);
- SMARTLIST_FOREACH(ip->link_specifiers, hs_desc_link_specifier_t *, ls,
- hs_desc_link_specifier_free(ls));
+ SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *, ls,
+ link_specifier_free(ls));
smartlist_free(ip->link_specifiers);
memset(ip, 0, sizeof(hs_intropoint_t));
}
diff --git a/src/feature/hs/hs_service.c b/src/feature/hs/hs_service.c
index 6e4da8856..8cd863eef 100644
--- a/src/feature/hs/hs_service.c
+++ b/src/feature/hs/hs_service.c
@@ -280,9 +280,10 @@ describe_intro_point(const hs_service_intro_point_t *ip)
const char *legacy_id = NULL;
SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
- const hs_desc_link_specifier_t *, lspec) {
- if (lspec->type == LS_LEGACY_ID) {
- legacy_id = (const char *) lspec->u.legacy_id;
+ const link_specifier_t *, lspec) {
+ if (link_specifier_get_ls_type(lspec) == LS_LEGACY_ID) {
+ legacy_id = (const char *)
+ link_specifier_getconstarray_un_legacy_id(lspec);
break;
}
} SMARTLIST_FOREACH_END(lspec);
@@ -623,16 +624,16 @@ get_objects_from_ident(const hs_ident_circuit_t *ident,
* encountered in the link specifier list. Return NULL if it can't be found.
*
* The caller does NOT have ownership of the object, the intro point does. */
-static hs_desc_link_specifier_t *
+static link_specifier_t *
get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
{
- hs_desc_link_specifier_t *lnk_spec = NULL;
+ link_specifier_t *lnk_spec = NULL;
tor_assert(ip);
SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
- hs_desc_link_specifier_t *, ls) {
- if (ls->type == type) {
+ link_specifier_t *, ls) {
+ if (link_specifier_get_ls_type(ls) == type) {
lnk_spec = ls;
goto end;
}
@@ -648,7 +649,7 @@ get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
STATIC const node_t *
get_node_from_intro_point(const hs_service_intro_point_t *ip)
{
- const hs_desc_link_specifier_t *ls;
+ const link_specifier_t *ls;
tor_assert(ip);
@@ -657,7 +658,8 @@ get_node_from_intro_point(const hs_service_intro_point_t *ip)
return NULL;
}
/* XXX In the future, we want to only use the ed25519 ID (#22173). */
- return node_get_by_id((const char *) ls->u.legacy_id);
+ return node_get_by_id(
+ (const char *) link_specifier_getconstarray_un_legacy_id(ls));
}
/* Given a service intro point, return the extend_info_t for it. This can
@@ -1523,7 +1525,7 @@ remember_failing_intro_point(const hs_service_intro_point_t *ip,
hs_service_descriptor_t *desc, time_t now)
{
time_t *time_of_failure, *prev_ptr;
- const hs_desc_link_specifier_t *legacy_ls;
+ const link_specifier_t *legacy_ls;
tor_assert(ip);
tor_assert(desc);
@@ -1532,22 +1534,13 @@ remember_failing_intro_point(const hs_service_intro_point_t *ip,
*time_of_failure = now;
legacy_ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
tor_assert(legacy_ls);
- prev_ptr = digestmap_set(desc->intro_points.failed_id,
- (const char *) legacy_ls->u.legacy_id,
- time_of_failure);
+ prev_ptr = digestmap_set(
+ desc->intro_points.failed_id,
+ (const char *) link_specifier_getconstarray_un_legacy_id(legacy_ls),
+ time_of_failure);
tor_free(prev_ptr);
}
-/* Copy the descriptor link specifier object from src to dst. */
-static void
-link_specifier_copy(hs_desc_link_specifier_t *dst,
- const hs_desc_link_specifier_t *src)
-{
- tor_assert(dst);
- tor_assert(src);
- memcpy(dst, src, sizeof(hs_desc_link_specifier_t));
-}
-
/* Using a given descriptor signing keypair signing_kp, a service intro point
* object ip and the time now, setup the content of an already allocated
* descriptor intro desc_ip.
@@ -1582,9 +1575,14 @@ setup_desc_intro_point(const ed25519_keypair_t *signing_kp,
/* Copy link specifier(s). */
SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
- const hs_desc_link_specifier_t *, ls) {
- hs_desc_link_specifier_t *copy = tor_malloc_zero(sizeof(*copy));
- link_specifier_copy(copy, ls);
+ const link_specifier_t *, ls) {
+ if (BUG(!ls)) {
+ goto done;
+ }
+ link_specifier_t *copy = link_specifier_dup(ls);
+ if (BUG(!copy)) {
+ goto done;
+ }
smartlist_add(desc_ip->link_specifiers, copy);
} SMARTLIST_FOREACH_END(ls);
diff --git a/src/test/hs_test_helpers.c b/src/test/hs_test_helpers.c
index f2ae8398d..1c47c7d7d 100644
--- a/src/test/hs_test_helpers.c
+++ b/src/test/hs_test_helpers.c
@@ -21,22 +21,33 @@ hs_helper_build_intro_point(const ed25519_keypair_t *signing_kp, time_t now,
/* For a usable intro point we need at least two link specifiers: One legacy
* keyid and one ipv4 */
{
- hs_desc_link_specifier_t *ls_legacy = tor_malloc_zero(sizeof(*ls_legacy));
- hs_desc_link_specifier_t *ls_v4 = tor_malloc_zero(sizeof(*ls_v4));
- ls_legacy->type = LS_LEGACY_ID;
- memcpy(ls_legacy->u.legacy_id, "0299F268FCA9D55CD157976D39AE92B4B455B3A8",
- DIGEST_LEN);
- ls_v4->u.ap.port = 9001;
- int family = tor_addr_parse(&ls_v4->u.ap.addr, addr);
+ tor_addr_t a;
+ tor_addr_make_unspec(&a);
+ link_specifier_t *ls_legacy = link_specifier_new();
+ /* TODO: this name is wrong: it is sometimes an IPv6 address */
+ link_specifier_t *ls_v4 = link_specifier_new();
+ link_specifier_set_ls_type(ls_legacy, LS_LEGACY_ID);
+ memcpy(link_specifier_getarray_un_legacy_id(ls_legacy),
+ /* TODO: this code is wrong: it copies hex into binary */
+ "0299F268FCA9D55CD157976D39AE92B4B455B3A8",
+ link_specifier_getlen_un_legacy_id(ls_legacy));
+ int family = tor_addr_parse(&a, addr);
switch (family) {
case AF_INET:
- ls_v4->type = LS_IPV4;
+ link_specifier_set_ls_type(ls_v4, LS_IPV4);
+ link_specifier_set_un_ipv4_addr(ls_v4, tor_addr_to_ipv4h(&a));
+ link_specifier_set_un_ipv4_port(ls_v4, 9001);
break;
case AF_INET6:
- ls_v4->type = LS_IPV6;
+ link_specifier_set_ls_type(ls_v4, LS_IPV6);
+ memcpy(link_specifier_getarray_un_ipv6_addr(ls_v4),
+ tor_addr_to_in6_addr8(&a),
+ link_specifier_getlen_un_ipv6_addr(ls_v4));
+ link_specifier_set_un_ipv6_port(ls_v4, 9001);
break;
default:
/* Stop the test, not suppose to have an error. */
+ /* TODO: just tt_fail(), because this code is confusing */
tt_int_op(family, OP_EQ, AF_INET);
}
smartlist_add(ip->link_specifiers, ls_legacy);
@@ -153,8 +164,11 @@ hs_helper_build_hs_desc_impl(unsigned int no_ip,
/* Add four intro points. */
smartlist_add(desc->encrypted_data.intro_points,
hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
+/* IPv6-only introduction points are not supported yet, see #23588 */
+#if 0
smartlist_add(desc->encrypted_data.intro_points,
hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
+#endif
smartlist_add(desc->encrypted_data.intro_points,
hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
smartlist_add(desc->encrypted_data.intro_points,
@@ -202,7 +216,6 @@ void
hs_helper_desc_equal(const hs_descriptor_t *desc1,
const hs_descriptor_t *desc2)
{
- char *addr1 = NULL, *addr2 = NULL;
/* Plaintext data section. */
tt_int_op(desc1->plaintext_data.version, OP_EQ,
desc2->plaintext_data.version);
@@ -291,35 +304,57 @@ hs_helper_desc_equal(const hs_descriptor_t *desc1,
tt_int_op(smartlist_len(ip1->link_specifiers), ==,
smartlist_len(ip2->link_specifiers));
for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
- hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
- *ls2 = smartlist_get(ip2->link_specifiers, j);
- tt_int_op(ls1->type, ==, ls2->type);
- switch (ls1->type) {
+ link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
+ *ls2 = smartlist_get(ip2->link_specifiers, j);
+ tt_int_op(link_specifier_get_ls_type(ls1), ==,
+ link_specifier_get_ls_type(ls2));
+ switch (link_specifier_get_ls_type(ls1)) {
case LS_IPV4:
+ {
+ uint32_t addr1 = link_specifier_get_un_ipv4_addr(ls1);
+ uint32_t addr2 = link_specifier_get_un_ipv4_addr(ls2);
+ tt_int_op(addr1, OP_EQ, addr2);
+ uint16_t port1 = link_specifier_get_un_ipv4_port(ls1);
+ uint16_t port2 = link_specifier_get_un_ipv4_port(ls2);
+ tt_int_op(port1, ==, port2);
+ }
+ break;
case LS_IPV6:
{
- addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
- addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
- tt_str_op(addr1, OP_EQ, addr2);
- tor_free(addr1);
- tor_free(addr2);
- tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
+ const uint8_t *addr1 =
+ link_specifier_getconstarray_un_ipv6_addr(ls1);
+ const uint8_t *addr2 =
+ link_specifier_getconstarray_un_ipv6_addr(ls2);
+ tt_int_op(link_specifier_getlen_un_ipv6_addr(ls1), OP_EQ,
+ link_specifier_getlen_un_ipv6_addr(ls2));
+ tt_mem_op(addr1, OP_EQ, addr2,
+ link_specifier_getlen_un_ipv6_addr(ls1));
+ uint16_t port1 = link_specifier_get_un_ipv6_port(ls1);
+ uint16_t port2 = link_specifier_get_un_ipv6_port(ls2);
+ tt_int_op(port1, ==, port2);
}
break;
case LS_LEGACY_ID:
- tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
- sizeof(ls1->u.legacy_id));
+ {
+ const uint8_t *id1 =
+ link_specifier_getconstarray_un_legacy_id(ls1);
+ const uint8_t *id2 =
+ link_specifier_getconstarray_un_legacy_id(ls2);
+ tt_int_op(link_specifier_getlen_un_legacy_id(ls1), OP_EQ,
+ link_specifier_getlen_un_legacy_id(ls2));
+ tt_mem_op(id1, OP_EQ, id2,
+ link_specifier_getlen_un_legacy_id(ls1));
+ }
break;
default:
/* Unknown type, caught it and print its value. */
- tt_int_op(ls1->type, OP_EQ, -1);
+ tt_int_op(link_specifier_get_ls_type(ls1), OP_EQ, -1);
}
}
}
}
done:
- tor_free(addr1);
- tor_free(addr2);
+ ;
}
diff --git a/src/test/test_hs_client.c b/src/test/test_hs_client.c
index 2f2bb4558..8362b6cbd 100644
--- a/src/test/test_hs_client.c
+++ b/src/test/test_hs_client.c
@@ -403,6 +403,9 @@ test_client_pick_intro(void *arg)
/* 2) Mark all intro points except _the chosen one_ as failed. Then query the
* desc and get a random intro: check that we got _the chosen one_. */
{
+ /* Tell hs_get_extend_info_from_lspecs() to skip the private address check.
+ */
+ get_options_mutable()->ExtendAllowPrivateAddresses = 1;
/* Pick the chosen intro point and get its ei */
hs_desc_intro_point_t *chosen_intro_point =
smartlist_get(desc->encrypted_data.intro_points, 0);
@@ -476,6 +479,7 @@ test_client_pick_intro(void *arg)
SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
hs_desc_intro_point_t *, ip) {
extend_info_t *intro_ei = desc_intro_point_to_extend_info(ip);
+ tt_assert(intro_ei);
if (intro_ei) {
const char *ptr;
char ip_addr[TOR_ADDR_BUF_LEN];
diff --git a/src/test/test_hs_descriptor.c b/src/test/test_hs_descriptor.c
index de584ed47..09c6c3e70 100644
--- a/src/test/test_hs_descriptor.c
+++ b/src/test/test_hs_descriptor.c
@@ -179,115 +179,6 @@ test_descriptor_padding(void *arg)
}
static void
-test_link_specifier(void *arg)
-{
- ssize_t ret;
- hs_desc_link_specifier_t spec;
- smartlist_t *link_specifiers = smartlist_new();
- char buf[256];
- char *b64 = NULL;
- link_specifier_t *ls = NULL;
-
- (void) arg;
-
- /* Always this port. */
- spec.u.ap.port = 42;
- smartlist_add(link_specifiers, &spec);
-
- /* Test IPv4 for starter. */
- {
- uint32_t ipv4;
-
- spec.type = LS_IPV4;
- ret = tor_addr_parse(&spec.u.ap.addr, "1.2.3.4");
- tt_int_op(ret, OP_EQ, AF_INET);
- b64 = encode_link_specifiers(link_specifiers);
- tt_assert(b64);
-
- /* Decode it and validate the format. */
- ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
- tt_int_op(ret, OP_GT, 0);
- /* First byte is the number of link specifier. */
- tt_int_op(get_uint8(buf), OP_EQ, 1);
- ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
- tt_int_op(ret, OP_EQ, 8);
- /* Should be 2 bytes for port and 4 bytes for IPv4. */
- tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, 6);
- ipv4 = link_specifier_get_un_ipv4_addr(ls);
- tt_int_op(tor_addr_to_ipv4h(&spec.u.ap.addr), OP_EQ, ipv4);
- tt_int_op(link_specifier_get_un_ipv4_port(ls), OP_EQ, spec.u.ap.port);
-
- link_specifier_free(ls);
- ls = NULL;
- tor_free(b64);
- }
-
- /* Test IPv6. */
- {
- uint8_t ipv6[16];
-
- spec.type = LS_IPV6;
- ret = tor_addr_parse(&spec.u.ap.addr, "[1:2:3:4::]");
- tt_int_op(ret, OP_EQ, AF_INET6);
- b64 = encode_link_specifiers(link_specifiers);
- tt_assert(b64);
-
- /* Decode it and validate the format. */
- ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
- tt_int_op(ret, OP_GT, 0);
- /* First byte is the number of link specifier. */
- tt_int_op(get_uint8(buf), OP_EQ, 1);
- ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
- tt_int_op(ret, OP_EQ, 20);
- /* Should be 2 bytes for port and 16 bytes for IPv6. */
- tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, 18);
- for (unsigned int i = 0; i < sizeof(ipv6); i++) {
- ipv6[i] = link_specifier_get_un_ipv6_addr(ls, i);
- }
- tt_mem_op(tor_addr_to_in6_addr8(&spec.u.ap.addr), OP_EQ, ipv6,
- sizeof(ipv6));
- tt_int_op(link_specifier_get_un_ipv6_port(ls), OP_EQ, spec.u.ap.port);
-
- link_specifier_free(ls);
- ls = NULL;
- tor_free(b64);
- }
-
- /* Test legacy. */
- {
- uint8_t *id;
-
- spec.type = LS_LEGACY_ID;
- memset(spec.u.legacy_id, 'Y', sizeof(spec.u.legacy_id));
- b64 = encode_link_specifiers(link_specifiers);
- tt_assert(b64);
-
- /* Decode it and validate the format. */
- ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
- tt_int_op(ret, OP_GT, 0);
- /* First byte is the number of link specifier. */
- tt_int_op(get_uint8(buf), OP_EQ, 1);
- ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
- /* 20 bytes digest + 1 byte type + 1 byte len. */
- tt_int_op(ret, OP_EQ, 22);
- tt_int_op(link_specifier_getlen_un_legacy_id(ls), OP_EQ, DIGEST_LEN);
- /* Digest length is 20 bytes. */
- tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, DIGEST_LEN);
- id = link_specifier_getarray_un_legacy_id(ls);
- tt_mem_op(spec.u.legacy_id, OP_EQ, id, DIGEST_LEN);
-
- link_specifier_free(ls);
- ls = NULL;
- tor_free(b64);
- }
-
- done:
- link_specifier_free(ls);
- tor_free(b64);
- smartlist_free(link_specifiers);
-}
-
-static void
test_encode_descriptor(void *arg)
{
int ret;
@@ -932,8 +823,6 @@ struct testcase_t hs_descriptor[] = {
/* Encoding tests. */
{ "cert_encoding", test_cert_encoding, TT_FORK,
NULL, NULL },
- { "link_specifier", test_link_specifier, TT_FORK,
- NULL, NULL },
{ "encode_descriptor", test_encode_descriptor, TT_FORK,
NULL, NULL },
{ "descriptor_padding", test_descriptor_padding, TT_FORK,
diff --git a/src/test/test_hs_service.c b/src/test/test_hs_service.c
index e8bdcd86e..57132e619 100644
--- a/src/test/test_hs_service.c
+++ b/src/test/test_hs_service.c
@@ -328,17 +328,18 @@ helper_create_service_with_clients(int num_clients)
static hs_service_intro_point_t *
helper_create_service_ip(void)
{
- hs_desc_link_specifier_t *ls;
+ link_specifier_t *ls;
hs_service_intro_point_t *ip = service_intro_point_new(NULL);
tor_assert(ip);
/* Add a first unused link specifier. */
- ls = tor_malloc_zero(sizeof(*ls));
- ls->type = LS_IPV4;
+ ls = link_specifier_new();
+ link_specifier_set_ls_type(ls, LS_IPV4);
smartlist_add(ip->base.link_specifiers, ls);
/* Add a second link specifier used by a test. */
- ls = tor_malloc_zero(sizeof(*ls));
- ls->type = LS_LEGACY_ID;
- memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
+ ls = link_specifier_new();
+ link_specifier_set_ls_type(ls, LS_LEGACY_ID);
+ memset(link_specifier_getarray_un_legacy_id(ls), 'A',
+ link_specifier_getlen_un_legacy_id(ls));
smartlist_add(ip->base.link_specifiers, ls);
return ip;
@@ -811,10 +812,11 @@ test_helper_functions(void *arg)
const node_t *node = get_node_from_intro_point(ip);
tt_ptr_op(node, OP_EQ, &mock_node);
SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
- hs_desc_link_specifier_t *, ls) {
- if (ls->type == LS_LEGACY_ID) {
+ link_specifier_t *, ls) {
+ if (link_specifier_get_ls_type(ls) == LS_LEGACY_ID) {
/* Change legacy id in link specifier which is not the mock node. */
- memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
+ memset(link_specifier_getarray_un_legacy_id(ls), 'B',
+ link_specifier_getlen_un_legacy_id(ls));
}
} SMARTLIST_FOREACH_END(ls);
node = get_node_from_intro_point(ip);
1
0