address.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file address.h
  8. * \brief Headers for address.h
  9. **/
  10. #ifndef _TOR_ADDRESS_H
  11. #define _TOR_ADDRESS_H
  12. #define ADDRESS_H_ID "$Id$"
  13. #include "orconfig.h"
  14. #include "torint.h"
  15. #include "compat.h"
  16. /** The number of bits from an address to consider while doing a masked
  17. * comparison. */
  18. typedef uint8_t maskbits_t;
  19. struct in_addr;
  20. /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
  21. * sockaddr_storage.) */
  22. typedef struct tor_addr_t
  23. {
  24. sa_family_t family;
  25. union {
  26. struct in_addr in_addr;
  27. struct in6_addr in6_addr;
  28. } addr;
  29. } tor_addr_t;
  30. static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
  31. static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
  32. static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
  33. static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
  34. static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
  35. static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
  36. static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
  37. socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
  38. struct sockaddr *sa_out, socklen_t len);
  39. int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
  40. uint16_t *port_out);
  41. void tor_addr_make_unspec(tor_addr_t *a);
  42. /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  43. * an IPv6 address. */
  44. static INLINE const struct in6_addr *
  45. tor_addr_to_in6(const tor_addr_t *a)
  46. {
  47. return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
  48. }
  49. #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
  50. #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
  51. #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
  52. /** Return an IPv4 address in network order for <b>a</b>, or 0 if
  53. * <b>a</b> is not an IPv4 address. */
  54. static INLINE uint32_t
  55. tor_addr_to_ipv4n(const tor_addr_t *a)
  56. {
  57. return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
  58. }
  59. /** Return an IPv4 address in host order for <b>a</b>, or 0 if
  60. * <b>a</b> is not an IPv4 address. */
  61. static INLINE uint32_t
  62. tor_addr_to_ipv4h(const tor_addr_t *a)
  63. {
  64. return ntohl(tor_addr_to_ipv4n(a));
  65. }
  66. /* Given an IPv6 address, return its mapped IPv4 address in host order, or
  67. * 0 if <b>a</b> is not an IPv6 address.
  68. *
  69. * (Does not check whether the address is really a mapped address */
  70. static INLINE uint32_t
  71. tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
  72. {
  73. return a->family == AF_INET6 ? ntohl(tor_addr_to_in6_addr32(a)[3]) : 0;
  74. }
  75. /** Return the address family of <b>a</b>. Possible values are:
  76. * AF_INET6, AF_INET, AF_UNSPEC. */
  77. static INLINE sa_family_t
  78. tor_addr_family(const tor_addr_t *a)
  79. {
  80. return a->family;
  81. }
  82. /** Return an in_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
  83. * an IPv4 address. */
  84. static INLINE const struct in_addr *
  85. tor_addr_to_in(const tor_addr_t *a)
  86. {
  87. return a->family == AF_INET ? &a->addr.in_addr : NULL;
  88. }
  89. /** Return true iff <b>a</b> is an IPv4 address equal to the host-ordered
  90. * address in <b>u</b>. */
  91. static INLINE int
  92. tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  93. {
  94. return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
  95. }
  96. #define TOR_ADDR_BUF_LEN 48 /* [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]
  97. */
  98. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  99. char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
  100. void tor_addr_assign(tor_addr_t *dest, const tor_addr_t *src);
  101. const char *fmt_addr(const tor_addr_t *addr);
  102. int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
  103. /** Flag to specify how to do a comparison between addresses. In an "exact"
  104. * comparison, addresses are equivalent only if they are in the same family
  105. * with the same value. In a "semantic" comparison, IPv4 addresses match all
  106. * IPv6 encodings of those addresses. */
  107. typedef enum {
  108. CMP_EXACT,
  109. CMP_SEMANTIC,
  110. } tor_addr_comparison_t;
  111. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  112. tor_addr_comparison_t how);
  113. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  114. maskbits_t mask, tor_addr_comparison_t how);
  115. /** Return true iff a and b are the same address. The comparison is done
  116. * "exactly". */
  117. #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
  118. unsigned int tor_addr_hash(const tor_addr_t *addr);
  119. int tor_addr_is_v4(const tor_addr_t *addr);
  120. int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
  121. /** Longest length that can be required for a reverse lookup name. */
  122. /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
  123. #define REVERSE_LOOKUP_NAME_BUF_LEN 73
  124. int tor_addr_to_reverse_lookup_name(char *out, size_t outlen,
  125. const tor_addr_t *addr);
  126. int tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
  127. int family, int accept_regular);
  128. int tor_addr_port_parse(const char *s, tor_addr_t *addr_out,
  129. uint16_t *port_out);
  130. int tor_addr_parse_mask_ports(const char *s,
  131. tor_addr_t *addr_out, maskbits_t *mask_out,
  132. uint16_t *port_min_out, uint16_t *port_max_out);
  133. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len,
  134. int decorate);
  135. int tor_addr_from_str(tor_addr_t *addr, const char *src);
  136. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  137. void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
  138. /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
  139. * order. */
  140. #define tor_addr_from_ipv4h(dest, v4addr) \
  141. tor_addr_from_ipv4n((dest), htonl(v4addr))
  142. void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
  143. #define tor_addr_from_in(dest, in) \
  144. tor_addr_from_ipv4n((dest), (in)->s_addr);
  145. void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
  146. int tor_addr_is_null(const tor_addr_t *addr);
  147. int tor_addr_is_loopback(const tor_addr_t *addr);
  148. /* IPv4 helpers */
  149. int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
  150. int parse_addr_port(int severity, const char *addrport, char **address,
  151. uint32_t *addr, uint16_t *port_out);
  152. int parse_port_range(const char *port, uint16_t *port_min_out,
  153. uint16_t *port_max_out);
  154. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  155. maskbits_t *maskbits_out, uint16_t *port_min_out,
  156. uint16_t *port_max_out);
  157. int addr_mask_get_bits(uint32_t mask);
  158. int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
  159. /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
  160. #define INET_NTOA_BUF_LEN 16
  161. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  162. char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
  163. int get_interface_address(int severity, uint32_t *addr);
  164. #endif