address.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 __ADDRESS_H
  11. #define __ADDRESS_H
  12. #define ADDRESS_H_ID "$Id$"
  13. #include "orconfig.h"
  14. #include "torint.h"
  15. #include "compat.h"
  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. /* DOCDOC*/
  29. static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
  30. static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
  31. static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
  32. static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
  33. static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
  34. static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
  35. static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
  36. socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
  37. struct sockaddr *sa_out, socklen_t len);
  38. void tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa);
  39. static INLINE const struct in6_addr *
  40. tor_addr_to_in6(const tor_addr_t *a)
  41. {
  42. return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
  43. }
  44. #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
  45. #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
  46. #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
  47. static INLINE uint32_t
  48. tor_addr_to_ipv4n(const tor_addr_t *a)
  49. {
  50. return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
  51. }
  52. static INLINE uint32_t
  53. tor_addr_to_ipv4h(const tor_addr_t *a)
  54. {
  55. return ntohl(tor_addr_to_ipv4n(a));
  56. }
  57. static INLINE uint32_t
  58. tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
  59. {
  60. return ntohl(tor_addr_to_in6_addr32(a)[3]);
  61. }
  62. static INLINE sa_family_t
  63. tor_addr_family(const tor_addr_t *a)
  64. {
  65. return a->family;
  66. }
  67. static INLINE const struct in_addr *
  68. tor_addr_to_in(const tor_addr_t *a)
  69. {
  70. return a->family == AF_INET ? &a->addr.in_addr : NULL;
  71. }
  72. static INLINE int
  73. tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  74. {
  75. return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
  76. }
  77. #define TOR_ADDR_BUF_LEN 48 /* [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]
  78. */
  79. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  80. char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
  81. int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
  82. /** DOCDOC */
  83. typedef enum {
  84. CMP_EXACT,
  85. CMP_SEMANTIC,
  86. } tor_addr_comparison_t;
  87. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  88. tor_addr_comparison_t how);
  89. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  90. maskbits_t mask, tor_addr_comparison_t how);
  91. unsigned int tor_addr_hash(const tor_addr_t *addr);
  92. int tor_addr_is_v4(const tor_addr_t *addr);
  93. int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
  94. int tor_addr_parse_mask_ports(const char *s,
  95. tor_addr_t *addr_out, maskbits_t *mask_out,
  96. uint16_t *port_min_out, uint16_t *port_max_out);
  97. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len,
  98. int decorate);
  99. int tor_addr_from_str(tor_addr_t *addr, const char *src);
  100. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  101. void tor_addr_from_ipv4h(tor_addr_t *dest, uint32_t v4addr);
  102. int tor_addr_is_null(const tor_addr_t *addr);
  103. int tor_addr_is_loopback(const tor_addr_t *addr);
  104. /* IPv4 helpers */
  105. int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
  106. int parse_addr_port(int severity, const char *addrport, char **address,
  107. uint32_t *addr, uint16_t *port_out);
  108. int parse_port_range(const char *port, uint16_t *port_min_out,
  109. uint16_t *port_max_out);
  110. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  111. maskbits_t *maskbits_out, uint16_t *port_min_out,
  112. uint16_t *port_max_out);
  113. int addr_mask_get_bits(uint32_t mask);
  114. int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
  115. #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
  116. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  117. char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
  118. int get_interface_address(int severity, uint32_t *addr);
  119. #endif