tor  master
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-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #ifndef TOR_ADDRESS_H
12 #define TOR_ADDRESS_H
13 
14 //#include <sys/sockio.h>
15 #include "orconfig.h"
16 #include "torint.h"
17 #include "compat.h"
18 #include "container.h"
19 
20 #ifdef ADDRESS_PRIVATE
21 
22 #if defined(HAVE_SYS_IOCTL_H)
23 #include <sys/ioctl.h>
24 #endif
25 
26 #ifdef HAVE_GETIFADDRS
27 #define HAVE_IFADDRS_TO_SMARTLIST
28 #endif
29 
30 #ifdef _WIN32
31 #define HAVE_IP_ADAPTER_TO_SMARTLIST
32 #endif
33 
34 #if defined(SIOCGIFCONF) && defined(HAVE_IOCTL)
35 #define HAVE_IFCONF_TO_SMARTLIST
36 #endif
37 
38 #if defined(HAVE_NET_IF_H)
39 #include <net/if.h> // for struct ifconf
40 #endif
41 
42 #if defined(HAVE_IFADDRS_TO_SMARTLIST)
43 #include <ifaddrs.h>
44 #endif
45 
46 // TODO win32 specific includes
47 #endif /* defined(ADDRESS_PRIVATE) */
48 
51 typedef uint8_t maskbits_t;
52 
53 struct in_addr;
56 typedef struct tor_addr_t
57 {
58  sa_family_t family;
59  union {
60  uint32_t dummy_; /* This field is here so we have something to initialize
61  * with a reliable cross-platform type. */
62  struct in_addr in_addr;
63  struct in6_addr in6_addr;
64  } addr;
65 } tor_addr_t;
66 
68 typedef struct tor_addr_port_t
69 {
70  tor_addr_t addr;
71  uint16_t port;
73 
74 #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
75 
76 static inline const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
77 static inline const struct in6_addr *tor_addr_to_in6_assert(
78  const tor_addr_t *a);
79 static inline uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
80 static inline uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
81 static inline uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
82 static inline sa_family_t tor_addr_family(const tor_addr_t *a);
83 static inline const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
84 static inline int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
85 
86 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
87  struct sockaddr *sa_out, socklen_t len);
88 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
89  uint16_t *port_out);
91 void tor_addr_make_null(tor_addr_t *a, sa_family_t family);
92 char *tor_sockaddr_to_str(const struct sockaddr *sa);
93 
96 static inline const struct in6_addr *
97 tor_addr_to_in6(const tor_addr_t *a)
98 {
99  return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
100 }
101 
104 static inline const struct in6_addr *
105 tor_addr_to_in6_assert(const tor_addr_t *a)
106 {
107  tor_assert(a->family == AF_INET6);
108  return &a->addr.in6_addr;
109 }
110 
115 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6_assert(x)->s6_addr
116 
121 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6_assert(x))
122 
126 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6_assert(x))
127 
130 static inline uint32_t
131 tor_addr_to_ipv4n(const tor_addr_t *a)
132 {
133  return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
134 }
137 static inline uint32_t
138 tor_addr_to_ipv4h(const tor_addr_t *a)
139 {
140  return ntohl(tor_addr_to_ipv4n(a));
141 }
146 static inline uint32_t
147 tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
148 {
149  if (a->family == AF_INET6) {
150  uint32_t *addr32 = NULL;
151  // Work around an incorrect NULL pointer dereference warning in
152  // "clang --analyze" due to limited analysis depth
153  addr32 = tor_addr_to_in6_addr32(a);
154  // To improve performance, wrap this assertion in:
155  // #if !defined(__clang_analyzer__) || PARANOIA
156  tor_assert(addr32);
157  return ntohl(addr32[3]);
158  } else {
159  return 0;
160  }
161 }
164 static inline sa_family_t
165 tor_addr_family(const tor_addr_t *a)
166 {
167  return a->family;
168 }
171 static inline const struct in_addr *
172 tor_addr_to_in(const tor_addr_t *a)
173 {
174  return a->family == AF_INET ? &a->addr.in_addr : NULL;
175 }
178 static inline int
179 tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
180 {
181  return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
182 }
183 
191 #define TOR_ADDR_BUF_LEN 48
192 
193 MOCK_DECL(int, tor_addr_lookup,(const char *name, uint16_t family,
194  tor_addr_t *addr_out));
195 char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC;
196 
199 #define fmt_addr(a) fmt_addr_impl((a), 0)
200 
202 #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
203 const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
204 const char *fmt_addrport(const tor_addr_t *addr, uint16_t port);
205 const char * fmt_addr32(uint32_t addr);
206 
207 MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
208 tor_addr_t *addr));
209 void interface_address6_list_free_(smartlist_t * addrs);// XXXX
210 #define interface_address6_list_free(addrs) \
211  FREE_AND_NULL(smartlist_t, interface_address6_list_free_, (addrs))
212 MOCK_DECL(smartlist_t *,get_interface_address6_list,(int severity,
213  sa_family_t family,
214  int include_internal));
215 
220 typedef enum {
221  CMP_EXACT,
222  CMP_SEMANTIC,
224 
225 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
226  tor_addr_comparison_t how);
227 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
228  maskbits_t mask, tor_addr_comparison_t how);
231 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
232 
233 uint64_t tor_addr_hash(const tor_addr_t *addr);
234 struct sipkey;
235 uint64_t tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr);
236 int tor_addr_is_v4(const tor_addr_t *addr);
237 int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening,
238  const char *filename, int lineno);
239 #define tor_addr_is_internal(addr, for_listening) \
240  tor_addr_is_internal_((addr), (for_listening), SHORT_FILE__, __LINE__)
241 int tor_addr_is_multicast(const tor_addr_t *a);
242 
244 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
245 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
246 int tor_addr_to_PTR_name(char *out, size_t outlen,
247  const tor_addr_t *addr);
248 int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
249  int family, int accept_regular);
250 
251 int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
252  uint16_t *port_out);
253 
254 /* Does the address * yield an AF_UNSPEC wildcard address (1),
255  * which expands to corresponding wildcard IPv4 and IPv6 rules, and do we
256  * allow *4 and *6 for IPv4 and IPv6 wildcards, respectively;
257  * or does the address * yield IPv4 wildcard address (0). */
258 #define TAPMP_EXTENDED_STAR 1
259 /* Does the address * yield an IPv4 wildcard address rule (1);
260  * or does it yield wildcard IPv4 and IPv6 rules (0) */
261 #define TAPMP_STAR_IPV4_ONLY (1 << 1)
262 /* Does the address * yield an IPv6 wildcard address rule (1);
263  * or does it yield wildcard IPv4 and IPv6 rules (0) */
264 #define TAPMP_STAR_IPV6_ONLY (1 << 2)
265 /* TAPMP_STAR_IPV4_ONLY and TAPMP_STAR_IPV6_ONLY are mutually exclusive. */
266 int tor_addr_parse_mask_ports(const char *s, unsigned flags,
267  tor_addr_t *addr_out, maskbits_t *mask_out,
268  uint16_t *port_min_out, uint16_t *port_max_out);
269 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
270  int decorate);
271 int tor_addr_parse(tor_addr_t *addr, const char *src);
272 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
273 void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src);
274 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
277 #define tor_addr_from_ipv4h(dest, v4addr) \
278  tor_addr_from_ipv4n((dest), htonl(v4addr))
279 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
281 #define tor_addr_from_in(dest, in) \
282  tor_addr_from_ipv4n((dest), (in)->s_addr);
283 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
284 int tor_addr_is_null(const tor_addr_t *addr);
285 int tor_addr_is_loopback(const tor_addr_t *addr);
286 
287 int tor_addr_is_valid(const tor_addr_t *addr, int for_listening);
288 int tor_addr_is_valid_ipv4n(uint32_t v4n_addr, int for_listening);
289 #define tor_addr_is_valid_ipv4h(v4h_addr, for_listening) \
290  tor_addr_is_valid_ipv4n(htonl(v4h_addr), (for_listening))
291 int tor_port_is_valid(uint16_t port, int for_listening);
292 /* Are addr and port both valid? */
293 #define tor_addr_port_is_valid(addr, port, for_listening) \
294  (tor_addr_is_valid((addr), (for_listening)) && \
295  tor_port_is_valid((port), (for_listening)))
296 /* Are ap->addr and ap->port both valid? */
297 #define tor_addr_port_is_valid_ap(ap, for_listening) \
298  tor_addr_port_is_valid(&(ap)->addr, (ap)->port, (for_listening))
299 /* Are the network-order v4addr and port both valid? */
300 #define tor_addr_port_is_valid_ipv4n(v4n_addr, port, for_listening) \
301  (tor_addr_is_valid_ipv4n((v4n_addr), (for_listening)) && \
302  tor_port_is_valid((port), (for_listening)))
303 /* Are the host-order v4addr and port both valid? */
304 #define tor_addr_port_is_valid_ipv4h(v4h_addr, port, for_listening) \
305  (tor_addr_is_valid_ipv4h((v4h_addr), (for_listening)) && \
306  tor_port_is_valid((port), (for_listening)))
307 
308 int tor_addr_port_split(int severity, const char *addrport,
309  char **address_out, uint16_t *port_out);
310 
311 int tor_addr_port_parse(int severity, const char *addrport,
312  tor_addr_t *address_out, uint16_t *port_out,
313  int default_port);
314 
315 int tor_addr_hostname_is_local(const char *name);
316 
317 /* IPv4 helpers */
318 int addr_port_lookup(int severity, const char *addrport, char **address,
319  uint32_t *addr, uint16_t *port_out);
320 int parse_port_range(const char *port, uint16_t *port_min_out,
321  uint16_t *port_max_out);
322 int addr_mask_get_bits(uint32_t mask);
324 #define INET_NTOA_BUF_LEN 16
325 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
326 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
327 MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
328 #define interface_address_list_free(lst)\
329  interface_address6_list_free(lst)
330 
338 static inline smartlist_t *
339 get_interface_address_list(int severity, int include_internal)
340 {
341  return get_interface_address6_list(severity, AF_INET, include_internal);
342 }
343 
344 tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
345 int tor_addr_port_eq(const tor_addr_port_t *a,
346  const tor_addr_port_t *b);
347 
348 #ifdef ADDRESS_PRIVATE
349 MOCK_DECL(smartlist_t *,get_interface_addresses_raw,(int severity,
350  sa_family_t family));
351 MOCK_DECL(int,get_interface_address6_via_udp_socket_hack,(int severity,
352  sa_family_t family,
353  tor_addr_t *addr));
354 
355 #ifdef HAVE_IFADDRS_TO_SMARTLIST
356 STATIC smartlist_t *ifaddrs_to_smartlist(const struct ifaddrs *ifa,
357  sa_family_t family);
358 STATIC smartlist_t *get_interface_addresses_ifaddrs(int severity,
359  sa_family_t family);
360 #endif /* defined(HAVE_IFADDRS_TO_SMARTLIST) */
361 
362 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
363 STATIC smartlist_t *ip_adapter_addresses_to_smartlist(
364  const IP_ADAPTER_ADDRESSES *addresses);
365 STATIC smartlist_t *get_interface_addresses_win32(int severity,
366  sa_family_t family);
367 #endif /* defined(HAVE_IP_ADAPTER_TO_SMARTLIST) */
368 
369 #ifdef HAVE_IFCONF_TO_SMARTLIST
370 STATIC smartlist_t *ifreq_to_smartlist(char *ifr,
371  size_t buflen);
372 STATIC smartlist_t *get_interface_addresses_ioctl(int severity,
373  sa_family_t family);
374 #endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
375 
376 #endif /* defined(ADDRESS_PRIVATE) */
377 
378 #endif /* !defined(TOR_ADDRESS_H) */
379 
char * tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC
Definition: address.c:1234
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes)
Definition: address.c:985
Definition: address.h:56
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
Definition: address.c:156
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:1938
void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
Definition: address.c:974
char * tor_dup_ip(uint32_t addr) ATTR_MALLOC
Definition: address.c:2110
#define tor_assert(expr)
Definition: util_bug.h:68
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
Definition: address.c:429
MOCK_DECL(int, router_have_minimum_dir_info,(void))
int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address, int family, int accept_regular)
Definition: address.c:480
int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening, const char *filename, int lineno)
Definition: address.c:358
Definition: container.h:18
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
Definition: address.c:1054
Header file to define uint32_t and friends.
void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
Definition: address.c:996
int tor_addr_is_v4(const tor_addr_t *addr)
Definition: address.c:849
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition: address.c:226
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1269
Definition: address.h:68
uint8_t maskbits_t
Definition: address.h:51
int addr_port_lookup(int severity, const char *addrport, char **address, uint32_t *addr, uint16_t *port_out)
Definition: address.c:1967
int tor_addr_port_eq(const tor_addr_port_t *a, const tor_addr_port_t *b)
Definition: address.c:2168
int addr_mask_get_bits(uint32_t mask)
Definition: address.c:2023
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:643
tor_addr_comparison_t
Definition: address.h:220
Definition: compat.h:524
char * tor_sockaddr_to_str(const struct sockaddr *sa)
Definition: address.c:189
uint64_t tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr)
Definition: address.c:1212
void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:1018
int tor_addr_is_multicast(const tor_addr_t *a)
Definition: address.c:1675
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:870
int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
Definition: address.c:2096
int parse_port_range(const char *port, uint16_t *port_min_out, uint16_t *port_max_out)
Definition: address.c:2042
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:1075
const char * fmt_addr_impl(const tor_addr_t *addr, int decorate)
Definition: address.c:1254
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1297
Definition: siphash.h:4
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:1004
int tor_addr_hostname_is_local(const char *name)
Definition: address.c:2147
#define tor_addr_to_in6_addr32(x)
Definition: address.h:126
socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port, struct sockaddr *sa_out, socklen_t len)
Definition: address.c:104
int tor_addr_to_PTR_name(char *out, size_t outlen, const tor_addr_t *addr)
Definition: address.c:570
int tor_addr_is_loopback(const tor_addr_t *addr)
Definition: address.c:895
tor_addr_port_t * tor_addr_port_new(const tor_addr_t *addr, uint16_t port)
Definition: address.c:2157
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:1899
int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
Definition: address.c:1326
void interface_address6_list_free_(smartlist_t *addrs)
Definition: address.c:1804
struct tor_addr_port_t tor_addr_port_t
uint64_t tor_addr_hash(const tor_addr_t *addr)
Definition: address.c:1193
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:216
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1281