address.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, 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. struct in_addr in_addr;
  25. struct in6_addr in6_addr;
  26. } addr;
  27. } tor_addr_t;
  28. /** Holds an IP address and a TCP/UDP port. */
  29. typedef struct tor_addr_port_t
  30. {
  31. tor_addr_t addr;
  32. uint16_t port;
  33. } tor_addr_port_t;
  34. static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
  35. static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
  36. static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
  37. static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
  38. static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
  39. static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
  40. static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
  41. socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
  42. struct sockaddr *sa_out, socklen_t len);
  43. int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
  44. uint16_t *port_out);
  45. void tor_addr_make_unspec(tor_addr_t *a);
  46. char *tor_sockaddr_to_str(const struct sockaddr *sa);
  47. /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  48. * an IPv6 address. */
  49. static INLINE const struct in6_addr *
  50. tor_addr_to_in6(const tor_addr_t *a)
  51. {
  52. return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
  53. }
  54. /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
  55. *
  56. * Requires that <b>x</b> is actually an IPv6 address.
  57. */
  58. #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
  59. /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
  60. *
  61. * Requires that <b>x</b> is actually an IPv6 address.
  62. */
  63. #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
  64. /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
  65. *
  66. * Requires that <b>x</b> is actually an IPv6 address.
  67. */
  68. #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
  69. /** Return an IPv4 address in network order for <b>a</b>, or 0 if
  70. * <b>a</b> is not an IPv4 address. */
  71. static INLINE uint32_t
  72. tor_addr_to_ipv4n(const tor_addr_t *a)
  73. {
  74. return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
  75. }
  76. /** Return an IPv4 address in host order for <b>a</b>, or 0 if
  77. * <b>a</b> is not an IPv4 address. */
  78. static INLINE uint32_t
  79. tor_addr_to_ipv4h(const tor_addr_t *a)
  80. {
  81. return ntohl(tor_addr_to_ipv4n(a));
  82. }
  83. /** Given an IPv6 address, return its mapped IPv4 address in host order, or
  84. * 0 if <b>a</b> is not an IPv6 address.
  85. *
  86. * (Does not check whether the address is really a mapped address */
  87. static INLINE uint32_t
  88. tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
  89. {
  90. return a->family == AF_INET6 ? ntohl(tor_addr_to_in6_addr32(a)[3]) : 0;
  91. }
  92. /** Return the address family of <b>a</b>. Possible values are:
  93. * AF_INET6, AF_INET, AF_UNSPEC. */
  94. static INLINE sa_family_t
  95. tor_addr_family(const tor_addr_t *a)
  96. {
  97. return a->family;
  98. }
  99. /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  100. * an IPv4 address. */
  101. static INLINE const struct in_addr *
  102. tor_addr_to_in(const tor_addr_t *a)
  103. {
  104. return a->family == AF_INET ? &a->addr.in_addr : NULL;
  105. }
  106. /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
  107. * address in <b>u</b>. */
  108. static INLINE int
  109. tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  110. {
  111. return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
  112. }
  113. /** Length of a buffer that you need to allocate to be sure you can encode
  114. * any tor_addr_t.
  115. *
  116. * This allows enough space for
  117. * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
  118. * plus a terminating NUL.
  119. */
  120. #define TOR_ADDR_BUF_LEN 48
  121. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  122. char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
  123. const char *fmt_addr(const tor_addr_t *addr);
  124. const char * fmt_addr32(uint32_t addr);
  125. int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
  126. /** Flag to specify how to do a comparison between addresses. In an "exact"
  127. * comparison, addresses are equivalent only if they are in the same family
  128. * with the same value. In a "semantic" comparison, IPv4 addresses match all
  129. * IPv6 encodings of those addresses. */
  130. typedef enum {
  131. CMP_EXACT,
  132. CMP_SEMANTIC,
  133. } tor_addr_comparison_t;
  134. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  135. tor_addr_comparison_t how);
  136. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  137. maskbits_t mask, tor_addr_comparison_t how);
  138. /** Return true iff a and b are the same address. The comparison is done
  139. * "exactly". */
  140. #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
  141. unsigned int tor_addr_hash(const tor_addr_t *addr);
  142. int tor_addr_is_v4(const tor_addr_t *addr);
  143. int tor_addr_is_internal(const tor_addr_t *ip, int for_listening);
  144. /** Longest length that can be required for a reverse lookup name. */
  145. /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
  146. #define REVERSE_LOOKUP_NAME_BUF_LEN 73
  147. int tor_addr_to_PTR_name(char *out, size_t outlen,
  148. const tor_addr_t *addr);
  149. int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
  150. int family, int accept_regular);
  151. int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
  152. uint16_t *port_out);
  153. int tor_addr_parse_mask_ports(const char *s,
  154. tor_addr_t *addr_out, maskbits_t *mask_out,
  155. uint16_t *port_min_out, uint16_t *port_max_out);
  156. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
  157. int decorate);
  158. int tor_addr_parse(tor_addr_t *addr, const char *src);
  159. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  160. void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
  161. /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
  162. * order. */
  163. #define tor_addr_from_ipv4h(dest, v4addr) \
  164. tor_addr_from_ipv4n((dest), htonl(v4addr))
  165. void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
  166. /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
  167. #define tor_addr_from_in(dest, in) \
  168. tor_addr_from_ipv4n((dest), (in)->s_addr);
  169. void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
  170. int tor_addr_is_null(const tor_addr_t *addr);
  171. int tor_addr_is_loopback(const tor_addr_t *addr);
  172. int tor_addr_port_split(int severity, const char *addrport,
  173. char **address_out, uint16_t *port_out);
  174. /* IPv4 helpers */
  175. int is_internal_IP(uint32_t ip, int for_listening);
  176. int addr_port_lookup(int severity, const char *addrport, char **address,
  177. uint32_t *addr, uint16_t *port_out);
  178. int parse_port_range(const char *port, uint16_t *port_min_out,
  179. uint16_t *port_max_out);
  180. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  181. maskbits_t *maskbits_out, uint16_t *port_min_out,
  182. uint16_t *port_max_out);
  183. int addr_mask_get_bits(uint32_t mask);
  184. int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
  185. /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
  186. #define INET_NTOA_BUF_LEN 16
  187. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  188. char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
  189. int get_interface_address(int severity, uint32_t *addr);
  190. #endif