tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

address.h (16102B)


      1 /* Copyright (c) 2003-2004, Roger Dingledine
      2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
      3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      4 /* See LICENSE for licensing information */
      5 
      6 /**
      7 * \file address.h
      8 * \brief Headers for address.h
      9 **/
     10 
     11 #ifndef TOR_ADDRESS_H
     12 #define TOR_ADDRESS_H
     13 
     14 #include "orconfig.h"
     15 #include "lib/cc/torint.h"
     16 #include "lib/log/util_bug.h"
     17 #include "lib/net/inaddr_st.h"
     18 #include "lib/net/nettypes.h"
     19 
     20 #ifdef HAVE_NETINET_IN_H
     21 #include <netinet/in.h>
     22 #endif
     23 #ifdef _WIN32
     24 #include <winsock2.h>
     25 #include <windows.h>
     26 #endif
     27 
     28 #include <stddef.h>
     29 #include <stdlib.h>
     30 
     31 #ifdef ADDRESS_PRIVATE
     32 
     33 #if defined(HAVE_SYS_IOCTL_H)
     34 #include <sys/ioctl.h>
     35 #endif
     36 
     37 #ifdef HAVE_GETIFADDRS
     38 #define HAVE_IFADDRS_TO_SMARTLIST
     39 #endif
     40 
     41 #ifdef _WIN32
     42 #define HAVE_IP_ADAPTER_TO_SMARTLIST
     43 #endif
     44 
     45 #if defined(SIOCGIFCONF) && defined(HAVE_IOCTL)
     46 #define HAVE_IFCONF_TO_SMARTLIST
     47 #endif
     48 
     49 #if defined(HAVE_NET_IF_H)
     50 #include <net/if.h> // for struct ifconf
     51 #endif
     52 
     53 #if defined(HAVE_IFADDRS_TO_SMARTLIST)
     54 #include <ifaddrs.h>
     55 #endif
     56 
     57 // TODO win32 specific includes
     58 #endif /* defined(ADDRESS_PRIVATE) */
     59 
     60 /** The number of bits from an address to consider while doing a masked
     61 * comparison. */
     62 typedef uint8_t maskbits_t;
     63 
     64 struct in_addr;
     65 
     66 /** Holds an IPv4 or IPv6 address.  (Uses less memory than struct
     67 * sockaddr_storage.) */
     68 typedef struct tor_addr_t
     69 {
     70  sa_family_t family;
     71  union {
     72    uint32_t dummy_; /* This field is here so we have something to initialize
     73                      * with a reliable cross-platform type. */
     74    struct in_addr in_addr;
     75    struct in6_addr in6_addr;
     76  } addr;
     77 } tor_addr_t;
     78 
     79 /** Holds an IP address and a TCP/UDP port.  */
     80 typedef struct tor_addr_port_t
     81 {
     82  tor_addr_t addr;
     83  uint16_t port;
     84 } tor_addr_port_t;
     85 
     86 #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
     87 
     88 /* XXXX To do: extract all of the functions here that can possibly invoke
     89 * XXXX resolver, and make sure they have distinctive names. */
     90 
     91 static inline const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
     92 static inline const struct in6_addr *tor_addr_to_in6_assert(
     93    const tor_addr_t *a);
     94 static inline uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
     95 static inline uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
     96 static inline uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
     97 static inline sa_family_t tor_addr_family(const tor_addr_t *a);
     98 static inline bool tor_addr_is_unspec(const tor_addr_t *a);
     99 static inline const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
    100 static inline int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
    101 
    102 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
    103                               struct sockaddr *sa_out, socklen_t len);
    104 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
    105                           uint16_t *port_out);
    106 void tor_addr_make_unspec(tor_addr_t *a);
    107 void tor_addr_make_null(tor_addr_t *a, sa_family_t family);
    108 #define tor_addr_port_make_null(addr, port, family) \
    109  (void)(tor_addr_make_null(addr, family), (port) = 0)
    110 #define tor_addr_port_make_null_ap(ap, family) \
    111  tor_addr_port_make_null(&(ap)->addr, (ap)->port, family)
    112 char *tor_sockaddr_to_str(const struct sockaddr *sa);
    113 
    114 /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
    115 * an IPv6 address. */
    116 static inline const struct in6_addr *
    117 tor_addr_to_in6(const tor_addr_t *a)
    118 {
    119  return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
    120 }
    121 
    122 /** As tor_addr_to_in6, but assert that the address truly is an IPv6
    123 * address. */
    124 static inline const struct in6_addr *
    125 tor_addr_to_in6_assert(const tor_addr_t *a)
    126 {
    127  tor_assert(a->family == AF_INET6);
    128  return &a->addr.in6_addr;
    129 }
    130 
    131 /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
    132 *
    133 * Requires that <b>x</b> is actually an IPv6 address.
    134 */
    135 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6_assert(x)->s6_addr
    136 
    137 /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
    138 *
    139 * Requires that <b>x</b> is actually an IPv6 address.
    140 */
    141 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6_assert(x))
    142 
    143 /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
    144 *
    145 * Requires that <b>x</b> is actually an IPv6 address.
    146 */
    147 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6_assert(x))
    148 
    149 /** Return an IPv4 address in network order for <b>a</b>, or 0 if
    150 * <b>a</b> is not an IPv4 address. */
    151 static inline uint32_t
    152 tor_addr_to_ipv4n(const tor_addr_t *a)
    153 {
    154  return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
    155 }
    156 
    157 /** Return an IPv4 address in host order for <b>a</b>, or 0 if
    158 * <b>a</b> is not an IPv4 address. */
    159 static inline uint32_t
    160 tor_addr_to_ipv4h(const tor_addr_t *a)
    161 {
    162  return ntohl(tor_addr_to_ipv4n(a));
    163 }
    164 
    165 /** Given an IPv6 address, return its mapped IPv4 address in host order, or
    166 * 0 if <b>a</b> is not an IPv6 address.
    167 *
    168 * (Does not check whether the address is really a mapped address.) */
    169 static inline uint32_t
    170 tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
    171 {
    172  if (a->family == AF_INET6) {
    173    uint32_t *addr32 = NULL;
    174    // Work around an incorrect NULL pointer dereference warning in
    175    // "clang --analyze" due to limited analysis depth
    176    addr32 = tor_addr_to_in6_addr32(a);
    177    tor_assert(addr32);
    178    return ntohl(addr32[3]);
    179  } else {
    180    return 0;
    181  }
    182 }
    183 
    184 /** Return the address family of <b>a</b>.  Possible values are:
    185 * AF_INET6, AF_INET, AF_UNSPEC, AF_UNIX. */
    186 static inline sa_family_t
    187 tor_addr_family(const tor_addr_t *a)
    188 {
    189  return a->family;
    190 }
    191 
    192 /**
    193 * Return true if the address @a is in the UNSPEC family.
    194 **/
    195 static inline bool
    196 tor_addr_is_unspec(const tor_addr_t *a)
    197 {
    198  return a->family == AF_UNSPEC;
    199 }
    200 
    201 /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
    202 * an IPv4 address. */
    203 static inline const struct in_addr *
    204 tor_addr_to_in(const tor_addr_t *a)
    205 {
    206  return a->family == AF_INET ? &a->addr.in_addr : NULL;
    207 }
    208 
    209 /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
    210 * address in <b>u</b>. */
    211 static inline int
    212 tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
    213 {
    214  return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
    215 }
    216 
    217 /** Length of a buffer that you need to allocate to be sure you can encode
    218 * any tor_addr_t.
    219 *
    220 * This allows enough space for
    221 *   "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
    222 * plus a terminating NUL.
    223 */
    224 #define TOR_ADDR_BUF_LEN 48
    225 
    226 /** Length of a buffer containing an IP address along with a port number and
    227 * a separating colon.
    228 *
    229 * This allows enough space for
    230 *   "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]:12345",
    231 * plus a terminating NUL.
    232 */
    233 #define TOR_ADDRPORT_BUF_LEN (TOR_ADDR_BUF_LEN + 6)
    234 
    235 char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC;
    236 
    237 /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
    238 *  addresses. */
    239 #define fmt_addr(a) fmt_addr_impl((a), 0)
    240 
    241 /** Wrapper function of fmt_addr_impl(). It decorates IPv6
    242 *  addresses. */
    243 #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
    244 
    245 const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
    246 const char *fmt_addrport(const tor_addr_t *addr, uint16_t port);
    247 #define fmt_addrport_ap(ap) fmt_addrport(&(ap)->addr, (ap)->port)
    248 const char *fmt_addr32(uint32_t addr);
    249 const char *fmt_addr32_port(uint32_t addr, uint16_t port);
    250 const char *fmt_af_family(sa_family_t family);
    251 const char *fmt_addr_family(const tor_addr_t *addr);
    252 
    253 MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
    254 tor_addr_t *addr));
    255 
    256 struct smartlist_t;
    257 void interface_address6_list_free_(struct smartlist_t * addrs);
    258 #define interface_address6_list_free(addrs) \
    259  FREE_AND_NULL(struct smartlist_t, interface_address6_list_free_, (addrs))
    260 
    261 MOCK_DECL(struct smartlist_t *,get_interface_address6_list,(int severity,
    262                                                     sa_family_t family,
    263                                                     int include_internal));
    264 
    265 /** Flag to specify how to do a comparison between addresses.  In an "exact"
    266 * comparison, addresses are equivalent only if they are in the same family
    267 * with the same value.  In a "semantic" comparison, IPv4 addresses match all
    268 * IPv6 encodings of those addresses. */
    269 typedef enum {
    270  CMP_EXACT,
    271  CMP_SEMANTIC,
    272 } tor_addr_comparison_t;
    273 
    274 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
    275                     tor_addr_comparison_t how);
    276 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
    277                            maskbits_t mask, tor_addr_comparison_t how);
    278 /** Return true iff a and b are the same address.  The comparison is done
    279 * "exactly". */
    280 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
    281 
    282 uint64_t tor_addr_hash(const tor_addr_t *addr);
    283 struct sipkey;
    284 uint64_t tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr);
    285 
    286 int tor_addr_is_v4(const tor_addr_t *addr);
    287 int tor_addr_is_v6(const tor_addr_t *addr);
    288 int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening,
    289                          const char *filename, int lineno);
    290 #define tor_addr_is_internal(addr, for_listening) \
    291  tor_addr_is_internal_((addr), (for_listening), SHORT_FILE__, __LINE__)
    292 int tor_addr_is_multicast(const tor_addr_t *a);
    293 
    294 /** Longest length that can be required for a reverse lookup name. */
    295 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
    296 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
    297 int tor_addr_to_PTR_name(char *out, size_t outlen,
    298                                    const tor_addr_t *addr);
    299 int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
    300                                       int family, int accept_regular);
    301 
    302 /* Does the address * yield an AF_UNSPEC wildcard address (1),
    303 * which expands to corresponding wildcard IPv4 and IPv6 rules, and do we
    304 * allow *4 and *6 for IPv4 and IPv6 wildcards, respectively;
    305 * or does the address * yield IPv4 wildcard address (0).  */
    306 #define TAPMP_EXTENDED_STAR 1
    307 /* Does the address * yield an IPv4 wildcard address rule (1);
    308 * or does it yield wildcard IPv4 and IPv6 rules (0) */
    309 #define TAPMP_STAR_IPV4_ONLY     (1 << 1)
    310 /* Does the address * yield an IPv6 wildcard address rule (1);
    311 * or does it yield wildcard IPv4 and IPv6 rules (0) */
    312 #define TAPMP_STAR_IPV6_ONLY     (1 << 2)
    313 /* TAPMP_STAR_IPV4_ONLY and TAPMP_STAR_IPV6_ONLY are mutually exclusive. */
    314 int tor_addr_parse_mask_ports(const char *s, unsigned flags,
    315                              tor_addr_t *addr_out, maskbits_t *mask_out,
    316                              uint16_t *port_min_out, uint16_t *port_max_out);
    317 
    318 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
    319                             int decorate);
    320 int tor_addr_parse(tor_addr_t *addr, const char *src);
    321 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
    322 void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src);
    323 
    324 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
    325 /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
    326 * order. */
    327 #define tor_addr_from_ipv4h(dest, v4addr)       \
    328  tor_addr_from_ipv4n((dest), htonl(v4addr))
    329 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const uint8_t *bytes);
    330 /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
    331 #define tor_addr_from_in(dest, in) \
    332  tor_addr_from_ipv4n((dest), (in)->s_addr);
    333 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
    334 void tor_addr_copy_ipv6_bytes(uint8_t *dest, const tor_addr_t *src);
    335 
    336 int tor_addr_is_null(const tor_addr_t *addr);
    337 int tor_addr_is_loopback(const tor_addr_t *addr);
    338 
    339 int tor_addr_is_valid(const tor_addr_t *addr, int for_listening);
    340 int tor_addr_is_valid_ipv4n(uint32_t v4n_addr, int for_listening);
    341 #define tor_addr_is_valid_ipv4h(v4h_addr, for_listening) \
    342        tor_addr_is_valid_ipv4n(htonl(v4h_addr), (for_listening))
    343 int tor_port_is_valid(uint16_t port, int for_listening);
    344 
    345 /* Are addr and port both valid? */
    346 #define tor_addr_port_is_valid(addr, port, for_listening) \
    347        (tor_addr_is_valid((addr), (for_listening)) &&    \
    348         tor_port_is_valid((port), (for_listening)))
    349 /* Are ap->addr and ap->port both valid? */
    350 #define tor_addr_port_is_valid_ap(ap, for_listening) \
    351        tor_addr_port_is_valid(&(ap)->addr, (ap)->port, (for_listening))
    352 /* Are the network-order v4addr and port both valid? */
    353 #define tor_addr_port_is_valid_ipv4n(v4n_addr, port, for_listening) \
    354        (tor_addr_is_valid_ipv4n((v4n_addr), (for_listening)) &&    \
    355         tor_port_is_valid((port), (for_listening)))
    356 /* Are the host-order v4addr and port both valid? */
    357 #define tor_addr_port_is_valid_ipv4h(v4h_addr, port, for_listening) \
    358        (tor_addr_is_valid_ipv4h((v4h_addr), (for_listening)) &&    \
    359         tor_port_is_valid((port), (for_listening)))
    360 
    361 int tor_addr_port_split(int severity, const char *addrport,
    362                        char **address_out, uint16_t *port_out);
    363 
    364 int tor_addr_port_parse(int severity, const char *addrport,
    365                        tor_addr_t *address_out, uint16_t *port_out,
    366                        int default_port);
    367 
    368 int tor_addr_hostname_is_local(const char *name);
    369 
    370 /* IPv4 helpers */
    371 int parse_port_range(const char *port, uint16_t *port_min_out,
    372                     uint16_t *port_max_out);
    373 int addr_mask_get_bits(uint32_t mask);
    374 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
    375 
    376 MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
    377 #define interface_address_list_free(lst)\
    378  interface_address6_list_free(lst)
    379 
    380 /** Return a smartlist of the IPv4 addresses of all interfaces on the server.
    381 * Excludes loopback and multicast addresses. Only includes internal addresses
    382 * if include_internal is true. (Note that a relay behind NAT may use an
    383 * internal address to connect to the Internet.)
    384 * An empty smartlist means that there are no IPv4 addresses.
    385 * Returns NULL on failure.
    386 * Use free_interface_address_list to free the returned list.
    387 */
    388 static inline struct smartlist_t *
    389 get_interface_address_list(int severity, int include_internal)
    390 {
    391  return get_interface_address6_list(severity, AF_INET, include_internal);
    392 }
    393 
    394 tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
    395 int tor_addr_port_eq(const tor_addr_port_t *a,
    396                     const tor_addr_port_t *b);
    397 void tor_addr_port_copy(tor_addr_port_t *dest, const tor_addr_port_t *source);
    398 
    399 int string_is_valid_dest(const char *string);
    400 int string_is_valid_nonrfc_hostname(const char *string);
    401 int string_is_valid_ipv4_address(const char *string);
    402 int string_is_valid_ipv6_address(const char *string);
    403 
    404 #ifdef ADDRESS_PRIVATE
    405 MOCK_DECL(struct smartlist_t *,get_interface_addresses_raw,(int severity,
    406                                                     sa_family_t family));
    407 MOCK_DECL(int,get_interface_address6_via_udp_socket_hack,(int severity,
    408                                                          sa_family_t family,
    409                                                          tor_addr_t *addr));
    410 
    411 #ifdef HAVE_IFADDRS_TO_SMARTLIST
    412 STATIC struct smartlist_t *ifaddrs_to_smartlist(const struct ifaddrs *ifa,
    413                                         sa_family_t family);
    414 STATIC struct smartlist_t *get_interface_addresses_ifaddrs(int severity,
    415                                                    sa_family_t family);
    416 #endif /* defined(HAVE_IFADDRS_TO_SMARTLIST) */
    417 
    418 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
    419 STATIC struct smartlist_t *ip_adapter_addresses_to_smartlist(
    420                                        const IP_ADAPTER_ADDRESSES *addresses);
    421 STATIC struct smartlist_t *get_interface_addresses_win32(int severity,
    422                                                  sa_family_t family);
    423 #endif /* defined(HAVE_IP_ADAPTER_TO_SMARTLIST) */
    424 
    425 #ifdef HAVE_IFCONF_TO_SMARTLIST
    426 STATIC struct smartlist_t *ifreq_to_smartlist(const uint8_t *ifr,
    427                                              size_t buflen);
    428 STATIC struct smartlist_t *get_interface_addresses_ioctl(int severity,
    429                                                  sa_family_t family);
    430 #endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
    431 
    432 #endif /* defined(ADDRESS_PRIVATE) */
    433 
    434 #endif /* !defined(TOR_ADDRESS_H) */