address.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #ifdef ADDRESS_PRIVATE
  16. #if defined(HAVE_SYS_IOCTL_H)
  17. #include <sys/ioctl.h>
  18. #endif
  19. #ifdef HAVE_GETIFADDRS
  20. #define HAVE_IFADDRS_TO_SMARTLIST
  21. #endif
  22. #ifdef _WIN32
  23. #define HAVE_IP_ADAPTER_TO_SMARTLIST
  24. #endif
  25. #if defined(SIOCGIFCONF) && defined(HAVE_IOCTL)
  26. #define HAVE_IFCONF_TO_SMARTLIST
  27. #endif
  28. #if defined(HAVE_NET_IF_H)
  29. #include <net/if.h> // for struct ifconf
  30. #endif
  31. #if defined(HAVE_IFADDRS_TO_SMARTLIST)
  32. #include <ifaddrs.h>
  33. #endif
  34. // TODO win32 specific includes
  35. #include "container.h"
  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 uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
  62. static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
  63. static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
  64. static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
  65. static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
  66. static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
  67. socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
  68. struct sockaddr *sa_out, socklen_t len);
  69. int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
  70. uint16_t *port_out);
  71. void tor_addr_make_unspec(tor_addr_t *a);
  72. void tor_addr_make_null(tor_addr_t *a, sa_family_t family);
  73. char *tor_sockaddr_to_str(const struct sockaddr *sa);
  74. /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  75. * an IPv6 address. */
  76. static INLINE const struct in6_addr *
  77. tor_addr_to_in6(const tor_addr_t *a)
  78. {
  79. return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
  80. }
  81. /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
  82. *
  83. * Requires that <b>x</b> is actually an IPv6 address.
  84. */
  85. #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
  86. /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
  87. *
  88. * Requires that <b>x</b> is actually an IPv6 address.
  89. */
  90. #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
  91. /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
  92. *
  93. * Requires that <b>x</b> is actually an IPv6 address.
  94. */
  95. #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
  96. /** Return an IPv4 address in network order for <b>a</b>, or 0 if
  97. * <b>a</b> is not an IPv4 address. */
  98. static INLINE uint32_t
  99. tor_addr_to_ipv4n(const tor_addr_t *a)
  100. {
  101. return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
  102. }
  103. /** Return an IPv4 address in host order for <b>a</b>, or 0 if
  104. * <b>a</b> is not an IPv4 address. */
  105. static INLINE uint32_t
  106. tor_addr_to_ipv4h(const tor_addr_t *a)
  107. {
  108. return ntohl(tor_addr_to_ipv4n(a));
  109. }
  110. /** Given an IPv6 address, return its mapped IPv4 address in host order, or
  111. * 0 if <b>a</b> is not an IPv6 address.
  112. *
  113. * (Does not check whether the address is really a mapped address */
  114. static INLINE uint32_t
  115. tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
  116. {
  117. if (a->family == AF_INET6) {
  118. uint32_t *addr32 = NULL;
  119. // Work around an incorrect NULL pointer dereference warning in
  120. // "clang --analyze" due to limited analysis depth
  121. addr32 = tor_addr_to_in6_addr32(a);
  122. // To improve performance, wrap this assertion in:
  123. // #if !defined(__clang_analyzer__) || PARANOIA
  124. tor_assert(addr32);
  125. return ntohl(addr32[3]);
  126. } else {
  127. return 0;
  128. }
  129. }
  130. /** Return the address family of <b>a</b>. Possible values are:
  131. * AF_INET6, AF_INET, AF_UNSPEC. */
  132. static INLINE sa_family_t
  133. tor_addr_family(const tor_addr_t *a)
  134. {
  135. return a->family;
  136. }
  137. /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  138. * an IPv4 address. */
  139. static INLINE const struct in_addr *
  140. tor_addr_to_in(const tor_addr_t *a)
  141. {
  142. return a->family == AF_INET ? &a->addr.in_addr : NULL;
  143. }
  144. /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
  145. * address in <b>u</b>. */
  146. static INLINE int
  147. tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  148. {
  149. return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
  150. }
  151. /** Length of a buffer that you need to allocate to be sure you can encode
  152. * any tor_addr_t.
  153. *
  154. * This allows enough space for
  155. * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
  156. * plus a terminating NUL.
  157. */
  158. #define TOR_ADDR_BUF_LEN 48
  159. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  160. char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
  161. /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
  162. * addresses. */
  163. #define fmt_addr(a) fmt_addr_impl((a), 0)
  164. /** Wrapper function of fmt_addr_impl(). It decorates IPv6
  165. * addresses. */
  166. #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
  167. const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
  168. const char *fmt_addrport(const tor_addr_t *addr, uint16_t port);
  169. const char * fmt_addr32(uint32_t addr);
  170. MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
  171. tor_addr_t *addr));
  172. /** Flag to specify how to do a comparison between addresses. In an "exact"
  173. * comparison, addresses are equivalent only if they are in the same family
  174. * with the same value. In a "semantic" comparison, IPv4 addresses match all
  175. * IPv6 encodings of those addresses. */
  176. typedef enum {
  177. CMP_EXACT,
  178. CMP_SEMANTIC,
  179. } tor_addr_comparison_t;
  180. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  181. tor_addr_comparison_t how);
  182. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  183. maskbits_t mask, tor_addr_comparison_t how);
  184. /** Return true iff a and b are the same address. The comparison is done
  185. * "exactly". */
  186. #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
  187. uint64_t tor_addr_hash(const tor_addr_t *addr);
  188. int tor_addr_is_v4(const tor_addr_t *addr);
  189. int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening,
  190. const char *filename, int lineno);
  191. #define tor_addr_is_internal(addr, for_listening) \
  192. tor_addr_is_internal_((addr), (for_listening), SHORT_FILE__, __LINE__)
  193. /** Longest length that can be required for a reverse lookup name. */
  194. /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
  195. #define REVERSE_LOOKUP_NAME_BUF_LEN 73
  196. int tor_addr_to_PTR_name(char *out, size_t outlen,
  197. const tor_addr_t *addr);
  198. int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
  199. int family, int accept_regular);
  200. int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
  201. uint16_t *port_out);
  202. #define TAPMP_EXTENDED_STAR 1
  203. int tor_addr_parse_mask_ports(const char *s, unsigned flags,
  204. tor_addr_t *addr_out, maskbits_t *mask_out,
  205. uint16_t *port_min_out, uint16_t *port_max_out);
  206. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
  207. int decorate);
  208. int tor_addr_parse(tor_addr_t *addr, const char *src);
  209. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  210. void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src);
  211. void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
  212. /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
  213. * order. */
  214. #define tor_addr_from_ipv4h(dest, v4addr) \
  215. tor_addr_from_ipv4n((dest), htonl(v4addr))
  216. void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
  217. /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
  218. #define tor_addr_from_in(dest, in) \
  219. tor_addr_from_ipv4n((dest), (in)->s_addr);
  220. void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
  221. int tor_addr_is_null(const tor_addr_t *addr);
  222. int tor_addr_is_loopback(const tor_addr_t *addr);
  223. int tor_addr_port_split(int severity, const char *addrport,
  224. char **address_out, uint16_t *port_out);
  225. int tor_addr_port_parse(int severity, const char *addrport,
  226. tor_addr_t *address_out, uint16_t *port_out,
  227. int default_port);
  228. int tor_addr_hostname_is_local(const char *name);
  229. /* IPv4 helpers */
  230. int addr_port_lookup(int severity, const char *addrport, char **address,
  231. uint32_t *addr, uint16_t *port_out);
  232. int parse_port_range(const char *port, uint16_t *port_min_out,
  233. uint16_t *port_max_out);
  234. int addr_mask_get_bits(uint32_t mask);
  235. /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
  236. #define INET_NTOA_BUF_LEN 16
  237. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  238. char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
  239. MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
  240. tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
  241. #ifdef ADDRESS_PRIVATE
  242. STATIC smartlist_t *get_interface_addresses_raw(int severity);
  243. STATIC int get_interface_address6_via_udp_socket_hack(int severity,
  244. sa_family_t family,
  245. tor_addr_t *addr);
  246. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  247. STATIC smartlist_t *ifaddrs_to_smartlist(const struct ifaddrs *ifa);
  248. STATIC smartlist_t *get_interface_addresses_ifaddrs(int severity);
  249. #endif
  250. #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
  251. STATIC smartlist_t *ip_adapter_addresses_to_smartlist(
  252. const IP_ADAPTER_ADDRESSES *addresses);
  253. STATIC smartlist_t *get_interface_addresses_win32(int severity);
  254. #endif
  255. #ifdef HAVE_IFCONF_TO_SMARTLIST
  256. STATIC smartlist_t *ifreq_to_smartlist(char *ifr,
  257. size_t buflen);
  258. STATIC smartlist_t *get_interface_addresses_ioctl(int severity);
  259. #endif
  260. #endif // ADDRESS_PRIVATE
  261. #endif