PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/common/address.h

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