tor  0.4.0.1-alpha
address.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
62 typedef uint8_t maskbits_t;
63 
64 struct in_addr;
67 typedef struct tor_addr_t
68 {
69  sa_family_t family;
70  union {
71  uint32_t dummy_; /* This field is here so we have something to initialize
72  * with a reliable cross-platform type. */
73  struct in_addr in_addr;
74  struct in6_addr in6_addr;
75  } addr;
76 } tor_addr_t;
77 
79 typedef struct tor_addr_port_t
80 {
81  tor_addr_t addr;
82  uint16_t port;
84 
85 #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
86 
87 /* XXXX To do: extract all of the functions here that can possibly invoke
88  * XXXX resolver, and make sure they have distinctive names. */
89 
90 static inline const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
91 static inline const struct in6_addr *tor_addr_to_in6_assert(
92  const tor_addr_t *a);
93 static inline uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
94 static inline uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
95 static inline uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
96 static inline sa_family_t tor_addr_family(const tor_addr_t *a);
97 static inline const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
98 static inline int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
99 
100 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
101  struct sockaddr *sa_out, socklen_t len);
102 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
103  uint16_t *port_out);
105 void tor_addr_make_null(tor_addr_t *a, sa_family_t family);
106 char *tor_sockaddr_to_str(const struct sockaddr *sa);
107 
110 static inline const struct in6_addr *
112 {
113  return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
114 }
115 
118 static inline const struct in6_addr *
120 {
121  tor_assert(a->family == AF_INET6);
122  return &a->addr.in6_addr;
123 }
124 
129 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6_assert(x)->s6_addr
130 
135 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6_assert(x))
136 
140 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6_assert(x))
141 
144 static inline uint32_t
146 {
147  return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
148 }
151 static inline uint32_t
153 {
154  return ntohl(tor_addr_to_ipv4n(a));
155 }
160 static inline uint32_t
162 {
163  if (a->family == AF_INET6) {
164  uint32_t *addr32 = NULL;
165  // Work around an incorrect NULL pointer dereference warning in
166  // "clang --analyze" due to limited analysis depth
167  addr32 = tor_addr_to_in6_addr32(a);
168  // To improve performance, wrap this assertion in:
169  // #if !defined(__clang_analyzer__) || PARANOIA
170  tor_assert(addr32);
171  return ntohl(addr32[3]);
172  } else {
173  return 0;
174  }
175 }
178 static inline sa_family_t
180 {
181  return a->family;
182 }
185 static inline const struct in_addr *
187 {
188  return a->family == AF_INET ? &a->addr.in_addr : NULL;
189 }
192 static inline int
193 tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
194 {
195  return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
196 }
197 
205 #define TOR_ADDR_BUF_LEN 48
206 
207 char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC;
208 
211 #define fmt_addr(a) fmt_addr_impl((a), 0)
212 
214 #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
215 const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
216 const char *fmt_addrport(const tor_addr_t *addr, uint16_t port);
217 const char * fmt_addr32(uint32_t addr);
218 
219 MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
220 tor_addr_t *addr));
221 struct smartlist_t;
222 void interface_address6_list_free_(struct smartlist_t * addrs);// XXXX
223 #define interface_address6_list_free(addrs) \
224  FREE_AND_NULL(struct smartlist_t, interface_address6_list_free_, (addrs))
225 MOCK_DECL(struct smartlist_t *,get_interface_address6_list,(int severity,
226  sa_family_t family,
227  int include_internal));
228 
233 typedef enum {
234  CMP_EXACT,
235  CMP_SEMANTIC,
237 
238 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
240 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
244 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
245 
246 uint64_t tor_addr_hash(const tor_addr_t *addr);
247 struct sipkey;
248 uint64_t tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr);
249 int tor_addr_is_v4(const tor_addr_t *addr);
250 int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening,
251  const char *filename, int lineno);
252 #define tor_addr_is_internal(addr, for_listening) \
253  tor_addr_is_internal_((addr), (for_listening), SHORT_FILE__, __LINE__)
254 int tor_addr_is_multicast(const tor_addr_t *a);
255 
257 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
258 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
259 int tor_addr_to_PTR_name(char *out, size_t outlen,
260  const tor_addr_t *addr);
261 int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
262  int family, int accept_regular);
263 
264 /* Does the address * yield an AF_UNSPEC wildcard address (1),
265  * which expands to corresponding wildcard IPv4 and IPv6 rules, and do we
266  * allow *4 and *6 for IPv4 and IPv6 wildcards, respectively;
267  * or does the address * yield IPv4 wildcard address (0). */
268 #define TAPMP_EXTENDED_STAR 1
269 /* Does the address * yield an IPv4 wildcard address rule (1);
270  * or does it yield wildcard IPv4 and IPv6 rules (0) */
271 #define TAPMP_STAR_IPV4_ONLY (1 << 1)
272 /* Does the address * yield an IPv6 wildcard address rule (1);
273  * or does it yield wildcard IPv4 and IPv6 rules (0) */
274 #define TAPMP_STAR_IPV6_ONLY (1 << 2)
275 /* TAPMP_STAR_IPV4_ONLY and TAPMP_STAR_IPV6_ONLY are mutually exclusive. */
276 int tor_addr_parse_mask_ports(const char *s, unsigned flags,
277  tor_addr_t *addr_out, maskbits_t *mask_out,
278  uint16_t *port_min_out, uint16_t *port_max_out);
279 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
280  int decorate);
281 int tor_addr_parse(tor_addr_t *addr, const char *src);
282 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
283 void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src);
284 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
287 #define tor_addr_from_ipv4h(dest, v4addr) \
288  tor_addr_from_ipv4n((dest), htonl(v4addr))
289 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
291 #define tor_addr_from_in(dest, in) \
292  tor_addr_from_ipv4n((dest), (in)->s_addr);
293 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
294 int tor_addr_is_null(const tor_addr_t *addr);
295 int tor_addr_is_loopback(const tor_addr_t *addr);
296 
297 int tor_addr_is_valid(const tor_addr_t *addr, int for_listening);
298 int tor_addr_is_valid_ipv4n(uint32_t v4n_addr, int for_listening);
299 #define tor_addr_is_valid_ipv4h(v4h_addr, for_listening) \
300  tor_addr_is_valid_ipv4n(htonl(v4h_addr), (for_listening))
301 int tor_port_is_valid(uint16_t port, int for_listening);
302 /* Are addr and port both valid? */
303 #define tor_addr_port_is_valid(addr, port, for_listening) \
304  (tor_addr_is_valid((addr), (for_listening)) && \
305  tor_port_is_valid((port), (for_listening)))
306 /* Are ap->addr and ap->port both valid? */
307 #define tor_addr_port_is_valid_ap(ap, for_listening) \
308  tor_addr_port_is_valid(&(ap)->addr, (ap)->port, (for_listening))
309 /* Are the network-order v4addr and port both valid? */
310 #define tor_addr_port_is_valid_ipv4n(v4n_addr, port, for_listening) \
311  (tor_addr_is_valid_ipv4n((v4n_addr), (for_listening)) && \
312  tor_port_is_valid((port), (for_listening)))
313 /* Are the host-order v4addr and port both valid? */
314 #define tor_addr_port_is_valid_ipv4h(v4h_addr, port, for_listening) \
315  (tor_addr_is_valid_ipv4h((v4h_addr), (for_listening)) && \
316  tor_port_is_valid((port), (for_listening)))
317 
318 int tor_addr_port_split(int severity, const char *addrport,
319  char **address_out, uint16_t *port_out);
320 
321 int tor_addr_port_parse(int severity, const char *addrport,
322  tor_addr_t *address_out, uint16_t *port_out,
323  int default_port);
324 
325 int tor_addr_hostname_is_local(const char *name);
326 
327 /* IPv4 helpers */
328 int parse_port_range(const char *port, uint16_t *port_min_out,
329  uint16_t *port_max_out);
330 int addr_mask_get_bits(uint32_t mask);
331 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
332 MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
333 #define interface_address_list_free(lst)\
334  interface_address6_list_free(lst)
335 
343 static inline struct smartlist_t *
344 get_interface_address_list(int severity, int include_internal)
345 {
346  return get_interface_address6_list(severity, AF_INET, include_internal);
347 }
348 
349 tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
350 int tor_addr_port_eq(const tor_addr_port_t *a,
351  const tor_addr_port_t *b);
352 
353 int string_is_valid_dest(const char *string);
354 int string_is_valid_nonrfc_hostname(const char *string);
355 int string_is_valid_ipv4_address(const char *string);
356 int string_is_valid_ipv6_address(const char *string);
357 
358 #ifdef ADDRESS_PRIVATE
359 MOCK_DECL(struct smartlist_t *,get_interface_addresses_raw,(int severity,
360  sa_family_t family));
361 MOCK_DECL(int,get_interface_address6_via_udp_socket_hack,(int severity,
362  sa_family_t family,
363  tor_addr_t *addr));
364 
365 #ifdef HAVE_IFADDRS_TO_SMARTLIST
366 STATIC struct smartlist_t *ifaddrs_to_smartlist(const struct ifaddrs *ifa,
367  sa_family_t family);
368 STATIC struct smartlist_t *get_interface_addresses_ifaddrs(int severity,
369  sa_family_t family);
370 #endif /* defined(HAVE_IFADDRS_TO_SMARTLIST) */
371 
372 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
373 STATIC struct smartlist_t *ip_adapter_addresses_to_smartlist(
374  const IP_ADAPTER_ADDRESSES *addresses);
375 STATIC struct smartlist_t *get_interface_addresses_win32(int severity,
376  sa_family_t family);
377 #endif /* defined(HAVE_IP_ADAPTER_TO_SMARTLIST) */
378 
379 #ifdef HAVE_IFCONF_TO_SMARTLIST
380 STATIC struct smartlist_t *ifreq_to_smartlist(char *ifr,
381  size_t buflen);
382 STATIC struct smartlist_t *get_interface_addresses_ioctl(int severity,
383  sa_family_t family);
384 #endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
385 
386 #endif /* defined(ADDRESS_PRIVATE) */
387 
388 #endif /* !defined(TOR_ADDRESS_H) */
static struct smartlist_t * get_interface_address_list(int severity, int include_internal)
Definition: address.h:344
char * tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC
Definition: address.c:1122
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes)
Definition: address.c:873
uint16_t sa_family_t
Definition: inaddr_st.h:77
static int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
Definition: address.h:193
static uint32_t tor_addr_to_ipv4n(const tor_addr_t *a)
Definition: address.h:145
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
Definition: address.c:165
struct tor_addr_t tor_addr_t
int tor_addr_port_split(int severity, const char *addrport, char **address_out, uint16_t *port_out)
Definition: address.c:1751
void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
Definition: address.c:862
char * tor_dup_ip(uint32_t addr) ATTR_MALLOC
Definition: address.c:1879
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:152
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
Definition: address.c:317
int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address, int family, int accept_regular)
Definition: address.c:368
int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening, const char *filename, int lineno)
Definition: address.c:246
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
Definition: address.c:942
Integer definitions used throughout Tor.
void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
Definition: address.c:884
int tor_addr_is_v4(const tor_addr_t *addr)
Definition: address.c:737
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition: address.c:235
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1157
Define in6_addr, its members, and related types on platforms that lack it.
uint8_t maskbits_t
Definition: address.h:62
static const struct in_addr * tor_addr_to_in(const tor_addr_t *a)
Definition: address.h:186
int tor_addr_port_eq(const tor_addr_port_t *a, const tor_addr_port_t *b)
Definition: address.c:1937
int addr_mask_get_bits(uint32_t mask)
Definition: address.c:1808
tor_assert(buffer)
static uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
Definition: address.h:161
int tor_addr_parse_mask_ports(const char *s, unsigned flags, tor_addr_t *addr_out, maskbits_t *mask_out, uint16_t *port_min_out, uint16_t *port_max_out)
Definition: address.c:531
tor_addr_comparison_t
Definition: address.h:233
char * tor_sockaddr_to_str(const struct sockaddr *sa)
Definition: address.c:198
uint64_t tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr)
Definition: address.c:1100
void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:906
int tor_addr_is_multicast(const tor_addr_t *a)
Definition: address.c:1488
int string_is_valid_ipv4_address(const char *string)
Definition: address.c:1947
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:758
int parse_port_range(const char *port, uint16_t *port_min_out, uint16_t *port_max_out)
Definition: address.c:1827
Declarations for types used throughout the Tor networking system.
int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2, maskbits_t mask, tor_addr_comparison_t how)
Definition: address.c:963
const char * fmt_addr_impl(const tor_addr_t *addr, int decorate)
Definition: address.c:1142
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1185
static const struct in6_addr * tor_addr_to_in6(const tor_addr_t *a)
Definition: address.h:111
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:892
int tor_addr_hostname_is_local(const char *name)
Definition: address.c:1916
int string_is_valid_ipv6_address(const char *string)
Definition: address.c:1958
#define tor_addr_to_in6_addr32(x)
Definition: address.h:140
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
static const struct in6_addr * tor_addr_to_in6_assert(const tor_addr_t *a)
Definition: address.h:119
socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port, struct sockaddr *sa_out, socklen_t len)
Definition: address.c:113
int tor_addr_to_PTR_name(char *out, size_t outlen, const tor_addr_t *addr)
Definition: address.c:458
int tor_addr_is_loopback(const tor_addr_t *addr)
Definition: address.c:783
int string_is_valid_nonrfc_hostname(const char *string)
Definition: address.c:2002
tor_addr_port_t * tor_addr_port_new(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1926
int tor_addr_port_parse(int severity, const char *addrport, tor_addr_t *address_out, uint16_t *port_out, int default_port)
Definition: address.c:1712
int string_is_valid_dest(const char *string)
Definition: address.c:1969
Macros to manage assertions, fatal and non-fatal.
struct tor_addr_port_t tor_addr_port_t
void interface_address6_list_free_(struct smartlist_t *addrs)
Definition: address.c:1617
uint64_t tor_addr_hash(const tor_addr_t *addr)
Definition: address.c:1081
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1169
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179