address.h 15 KB

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