address.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "orconfig.h"
  12. #include "torint.h"
  13. #include "compat.h"
  14. /** The number of bits from an address to consider while doing a masked
  15. * comparison. */
  16. typedef uint8_t maskbits_t;
  17. struct in_addr;
  18. /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
  19. * sockaddr_storage.) */
  20. typedef struct tor_addr_t
  21. {
  22. sa_family_t family;
  23. union {
  24. uint32_t dummy_; /* This field is here so we have something to initialize
  25. * with a reliable cross-platform type. */
  26. struct in_addr in_addr;
  27. struct in6_addr in6_addr;
  28. } addr;
  29. } tor_addr_t;
  30. /** Holds an IP address and a TCP/UDP port. */
  31. typedef struct tor_addr_port_t
  32. {
  33. tor_addr_t addr;
  34. uint16_t port;
  35. } tor_addr_port_t;
  36. #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
  37. static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
  38. static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
  39. static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
  40. static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
  41. static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
  42. static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
  43. static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
  44. socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
  45. struct sockaddr *sa_out, socklen_t len);
  46. int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
  47. uint16_t *port_out);
  48. void tor_addr_make_unspec(tor_addr_t *a);
  49. void tor_addr_make_null(tor_addr_t *a, sa_family_t family);
  50. char *tor_sockaddr_to_str(const struct sockaddr *sa);
  51. /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  52. * an IPv6 address. */
  53. static INLINE const struct in6_addr *
  54. tor_addr_to_in6(const tor_addr_t *a)
  55. {
  56. return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
  57. }
  58. /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
  59. *
  60. * Requires that <b>x</b> is actually an IPv6 address.
  61. */
  62. #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
  63. /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
  64. *
  65. * Requires that <b>x</b> is actually an IPv6 address.
  66. */
  67. #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
  68. /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
  69. *
  70. * Requires that <b>x</b> is actually an IPv6 address.
  71. */
  72. #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
  73. /** Return an IPv4 address in network order for <b>a</b>, or 0 if
  74. * <b>a</b> is not an IPv4 address. */
  75. static INLINE uint32_t
  76. tor_addr_to_ipv4n(const tor_addr_t *a)
  77. {
  78. return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
  79. }
  80. /** Return an IPv4 address in host order for <b>a</b>, or 0 if
  81. * <b>a</b> is not an IPv4 address. */
  82. static INLINE uint32_t
  83. tor_addr_to_ipv4h(const tor_addr_t *a)
  84. {
  85. return ntohl(tor_addr_to_ipv4n(a));
  86. }
  87. /** Given an IPv6 address, return its mapped IPv4 address in host order, or
  88. * 0 if <b>a</b> is not an IPv6 address.
  89. *
  90. * (Does not check whether the address is really a mapped address */
  91. static INLINE uint32_t
  92. tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
  93. {
  94. if (a->family == AF_INET6) {
  95. uint32_t *addr32 = NULL;
  96. // Work around an incorrect NULL pointer dereference warning in
  97. // "clang --analyze" due to limited analysis depth
  98. addr32 = tor_addr_to_in6_addr32(a);
  99. // To improve performance, wrap this assertion in:
  100. // #if !defined(__clang_analyzer__) || PARANOIA
  101. tor_assert(addr32);
  102. return ntohl(addr32[3]);
  103. } else {
  104. return 0;
  105. }
  106. }
  107. /** Return the address family of <b>a</b>. Possible values are:
  108. * AF_INET6, AF_INET, AF_UNSPEC. */
  109. static INLINE sa_family_t
  110. tor_addr_family(const tor_addr_t *a)
  111. {
  112. return a->family;
  113. }
  114. /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  115. * an IPv4 address. */
  116. static INLINE const struct in_addr *
  117. tor_addr_to_in(const tor_addr_t *a)
  118. {
  119. return a->family == AF_INET ? &a->addr.in_addr : NULL;
  120. }
  121. /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
  122. * address in <b>u</b>. */
  123. static INLINE int
  124. tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  125. {
  126. return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
  127. }
  128. /** Length of a buffer that you need to allocate to be sure you can encode
  129. * any tor_addr_t.
  130. *
  131. * This allows enough space for
  132. * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
  133. * plus a terminating NUL.
  134. */
  135. #define TOR_ADDR_BUF_LEN 48
  136. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  137. char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
  138. /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
  139. * addresses. */
  140. #define fmt_addr(a) fmt_addr_impl((a), 0)
  141. /** Wrapper function of fmt_addr_impl(). It decorates IPv6
  142. * addresses. */
  143. #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
  144. const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
  145. const char *fmt_addrport(const tor_addr_t *addr, uint16_t port);
  146. const char * fmt_addr32(uint32_t addr);
  147. MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
  148. tor_addr_t *addr));
  149. /** Flag to specify how to do a comparison between addresses. In an "exact"
  150. * comparison, addresses are equivalent only if they are in the same family
  151. * with the same value. In a "semantic" comparison, IPv4 addresses match all
  152. * IPv6 encodings of those addresses. */
  153. typedef enum {
  154. CMP_EXACT,
  155. CMP_SEMANTIC,
  156. } tor_addr_comparison_t;
  157. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  158. tor_addr_comparison_t how);
  159. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  160. maskbits_t mask, tor_addr_comparison_t how);
  161. /** Return true iff a and b are the same address. The comparison is done
  162. * "exactly". */
  163. #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
  164. uint64_t tor_addr_hash(const tor_addr_t *addr);
  165. int tor_addr_is_v4(const tor_addr_t *addr);
  166. int tor_addr_is_internal_(const tor_addr_t *ip, int for_listening,
  167. const char *filename, int lineno);
  168. #define tor_addr_is_internal(addr, for_listening) \
  169. tor_addr_is_internal_((addr), (for_listening), SHORT_FILE__, __LINE__)
  170. /** Longest length that can be required for a reverse lookup name. */
  171. /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
  172. #define REVERSE_LOOKUP_NAME_BUF_LEN 73
  173. int tor_addr_to_PTR_name(char *out, size_t outlen,
  174. const tor_addr_t *addr);
  175. int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
  176. int family, int accept_regular);
  177. int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
  178. uint16_t *port_out);
  179. #define TAPMP_EXTENDED_STAR 1
  180. int tor_addr_parse_mask_ports(const char *s, unsigned flags,
  181. tor_addr_t *addr_out, maskbits_t *mask_out,
  182. uint16_t *port_min_out, uint16_t *port_max_out);
  183. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
  184. int decorate);
  185. int tor_addr_parse(tor_addr_t *addr, const char *src);
  186. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  187. void tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src);
  188. void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
  189. /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
  190. * order. */
  191. #define tor_addr_from_ipv4h(dest, v4addr) \
  192. tor_addr_from_ipv4n((dest), htonl(v4addr))
  193. void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
  194. /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
  195. #define tor_addr_from_in(dest, in) \
  196. tor_addr_from_ipv4n((dest), (in)->s_addr);
  197. void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
  198. int tor_addr_is_null(const tor_addr_t *addr);
  199. int tor_addr_is_loopback(const tor_addr_t *addr);
  200. int tor_addr_port_split(int severity, const char *addrport,
  201. char **address_out, uint16_t *port_out);
  202. int tor_addr_port_parse(int severity, const char *addrport,
  203. tor_addr_t *address_out, uint16_t *port_out,
  204. int default_port);
  205. int tor_addr_hostname_is_local(const char *name);
  206. /* IPv4 helpers */
  207. int addr_port_lookup(int severity, const char *addrport, char **address,
  208. uint32_t *addr, uint16_t *port_out);
  209. int parse_port_range(const char *port, uint16_t *port_min_out,
  210. uint16_t *port_max_out);
  211. int addr_mask_get_bits(uint32_t mask);
  212. /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
  213. #define INET_NTOA_BUF_LEN 16
  214. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  215. char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
  216. MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
  217. tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
  218. #endif