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 */
8 * \brief Headers for address.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"
20 #ifdef HAVE_NETINET_IN_H
21 #include <netinet/in.h>
31 #ifdef ADDRESS_PRIVATE
33 #if defined(HAVE_SYS_IOCTL_H)
34 #include <sys/ioctl.h>
37 #ifdef HAVE_GETIFADDRS
38 #define HAVE_IFADDRS_TO_SMARTLIST
42 #define HAVE_IP_ADAPTER_TO_SMARTLIST
45 #if defined(SIOCGIFCONF) && defined(HAVE_IOCTL)
46 #define HAVE_IFCONF_TO_SMARTLIST
49 #if defined(HAVE_NET_IF_H)
50 #include <net/if.h> // for struct ifconf
53 #if defined(HAVE_IFADDRS_TO_SMARTLIST)
57 // TODO win32 specific includes
58 #endif /* defined(ADDRESS_PRIVATE) */
60 /** The number of bits from an address to consider while doing a masked
62 typedef uint8_t maskbits_t
;
65 /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
66 * sockaddr_storage.) */
67 typedef struct tor_addr_t
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
;
78 /** Holds an IP address and a TCP/UDP port. */
79 typedef struct tor_addr_port_t
85 #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
87 /* XXXX To do: extract all of the functions here that can possibly invoke
88 * XXXX resolver, and make sure they have distinctive names. */
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(
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
);
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
,
104 void tor_addr_make_unspec(tor_addr_t
*a
);
105 void tor_addr_make_null(tor_addr_t
*a
, sa_family_t family
);
106 char *tor_sockaddr_to_str(const struct sockaddr
*sa
);
108 /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
109 * an IPv6 address. */
110 static inline const struct in6_addr
*
111 tor_addr_to_in6(const tor_addr_t
*a
)
113 return a
->family
== AF_INET6
? &a
->addr
.in6_addr
: NULL
;
116 /** As tor_addr_to_in6, but assert that the address truly is an IPv6
118 static inline const struct in6_addr
*
119 tor_addr_to_in6_assert(const tor_addr_t
*a
)
121 tor_assert(a
->family
== AF_INET6
);
122 return &a
->addr
.in6_addr
;
125 /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
127 * Requires that <b>x</b> is actually an IPv6 address.
129 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6_assert(x)->s6_addr
131 /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
133 * Requires that <b>x</b> is actually an IPv6 address.
135 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6_assert(x))
136 /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
138 * Requires that <b>x</b> is actually an IPv6 address.
140 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6_assert(x))
142 /** Return an IPv4 address in network order for <b>a</b>, or 0 if
143 * <b>a</b> is not an IPv4 address. */
144 static inline uint32_t
145 tor_addr_to_ipv4n(const tor_addr_t
*a
)
147 return a
->family
== AF_INET
? a
->addr
.in_addr
.s_addr
: 0;
149 /** Return an IPv4 address in host 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_ipv4h(const tor_addr_t
*a
)
154 return ntohl(tor_addr_to_ipv4n(a
));
156 /** Given an IPv6 address, return its mapped IPv4 address in host order, or
157 * 0 if <b>a</b> is not an IPv6 address.
159 * (Does not check whether the address is really a mapped address */
160 static inline uint32_t
161 tor_addr_to_mapped_ipv4h(const tor_addr_t
*a
)
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
171 return ntohl(addr32
[3]);
176 /** Return the address family of <b>a</b>. Possible values are:
177 * AF_INET6, AF_INET, AF_UNSPEC. */
178 static inline sa_family_t
179 tor_addr_family(const tor_addr_t
*a
)
183 /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
184 * an IPv4 address. */
185 static inline const struct in_addr
*
186 tor_addr_to_in(const tor_addr_t
*a
)
188 return a
->family
== AF_INET
? &a
->addr
.in_addr
: NULL
;
190 /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
191 * address in <b>u</b>. */
193 tor_addr_eq_ipv4h(const tor_addr_t
*a
, uint32_t u
)
195 return a
->family
== AF_INET
? (tor_addr_to_ipv4h(a
) == u
) : 0;
198 /** Length of a buffer that you need to allocate to be sure you can encode
201 * This allows enough space for
202 * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
203 * plus a terminating NUL.
205 #define TOR_ADDR_BUF_LEN 48
207 char *tor_addr_to_str_dup(const tor_addr_t
*addr
) ATTR_MALLOC
;
209 /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
211 #define fmt_addr(a) fmt_addr_impl((a), 0)
212 /** Wrapper function of fmt_addr_impl(). It decorates IPv6
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
);
219 MOCK_DECL(int,get_interface_address6
,(int severity
, sa_family_t family
,
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
,
227 int include_internal
));
229 /** Flag to specify how to do a comparison between addresses. In an "exact"
230 * comparison, addresses are equivalent only if they are in the same family
231 * with the same value. In a "semantic" comparison, IPv4 addresses match all
232 * IPv6 encodings of those addresses. */
236 } tor_addr_comparison_t
;
238 int tor_addr_compare(const tor_addr_t
*addr1
, const tor_addr_t
*addr2
,
239 tor_addr_comparison_t how
);
240 int tor_addr_compare_masked(const tor_addr_t
*addr1
, const tor_addr_t
*addr2
,
241 maskbits_t mask
, tor_addr_comparison_t how
);
242 /** Return true iff a and b are the same address. The comparison is done
244 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
246 uint64_t tor_addr_hash(const tor_addr_t
*addr
);
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
);
256 /** Longest length that can be required for a reverse lookup name. */
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
);
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
,
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
);
285 /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
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
);
290 /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
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
);
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)))
318 int tor_addr_port_split(int severity
, const char *addrport
,
319 char **address_out
, uint16_t *port_out
);
321 int tor_addr_port_parse(int severity
, const char *addrport
,
322 tor_addr_t
*address_out
, uint16_t *port_out
,
325 int tor_addr_hostname_is_local(const char *name
);
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 /** Return a smartlist of the IPv4 addresses of all interfaces on the server.
336 * Excludes loopback and multicast addresses. Only includes internal addresses
337 * if include_internal is true. (Note that a relay behind NAT may use an
338 * internal address to connect to the Internet.)
339 * An empty smartlist means that there are no IPv4 addresses.
340 * Returns NULL on failure.
341 * Use free_interface_address_list to free the returned list.
343 static inline struct smartlist_t
*
344 get_interface_address_list(int severity
, int include_internal
)
346 return get_interface_address6_list(severity
, AF_INET
, include_internal
);
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
);
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
);
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
,
365 #ifdef HAVE_IFADDRS_TO_SMARTLIST
366 STATIC
struct smartlist_t
*ifaddrs_to_smartlist(const struct ifaddrs
*ifa
,
368 STATIC
struct smartlist_t
*get_interface_addresses_ifaddrs(int severity
,
370 #endif /* defined(HAVE_IFADDRS_TO_SMARTLIST) */
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
,
377 #endif /* defined(HAVE_IP_ADAPTER_TO_SMARTLIST) */
379 #ifdef HAVE_IFCONF_TO_SMARTLIST
380 STATIC
struct smartlist_t
*ifreq_to_smartlist(char *ifr
,
382 STATIC
struct smartlist_t
*get_interface_addresses_ioctl(int severity
,
384 #endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
386 #endif /* defined(ADDRESS_PRIVATE) */
388 #endif /* !defined(TOR_ADDRESS_H) */