00001
00002
00003
00004
00005
00011 #ifndef _TOR_ADDRESS_H
00012 #define _TOR_ADDRESS_H
00013
00014 #include "orconfig.h"
00015 #include "torint.h"
00016 #include "compat.h"
00017
00020 typedef uint8_t maskbits_t;
00021
00022 struct in_addr;
00025 typedef struct tor_addr_t
00026 {
00027 sa_family_t family;
00028 union {
00029 struct in_addr in_addr;
00030 struct in6_addr in6_addr;
00031 } addr;
00032 } tor_addr_t;
00033
00034 static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
00035 static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
00036 static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
00037 static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
00038 static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
00039 static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
00040 static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
00041
00042 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
00043 struct sockaddr *sa_out, socklen_t len);
00044 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
00045 uint16_t *port_out);
00046 void tor_addr_make_unspec(tor_addr_t *a);
00047
00050 static INLINE const struct in6_addr *
00051 tor_addr_to_in6(const tor_addr_t *a)
00052 {
00053 return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
00054 }
00055
00056 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
00057 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
00058 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
00059
00062 static INLINE uint32_t
00063 tor_addr_to_ipv4n(const tor_addr_t *a)
00064 {
00065 return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
00066 }
00069 static INLINE uint32_t
00070 tor_addr_to_ipv4h(const tor_addr_t *a)
00071 {
00072 return ntohl(tor_addr_to_ipv4n(a));
00073 }
00074
00075
00076
00077
00078 static INLINE uint32_t
00079 tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
00080 {
00081 return a->family == AF_INET6 ? ntohl(tor_addr_to_in6_addr32(a)[3]) : 0;
00082 }
00085 static INLINE sa_family_t
00086 tor_addr_family(const tor_addr_t *a)
00087 {
00088 return a->family;
00089 }
00092 static INLINE const struct in_addr *
00093 tor_addr_to_in(const tor_addr_t *a)
00094 {
00095 return a->family == AF_INET ? &a->addr.in_addr : NULL;
00096 }
00099 static INLINE int
00100 tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
00101 {
00102 return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
00103 }
00104
00105 #define TOR_ADDR_BUF_LEN 48
00106
00107
00108 int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
00109 char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
00110 const char *fmt_addr(const tor_addr_t *addr);
00111 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
00112
00117 typedef enum {
00118 CMP_EXACT,
00119 CMP_SEMANTIC,
00120 } tor_addr_comparison_t;
00121
00122 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
00123 tor_addr_comparison_t how);
00124 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
00125 maskbits_t mask, tor_addr_comparison_t how);
00128 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
00129
00130 unsigned int tor_addr_hash(const tor_addr_t *addr);
00131 int tor_addr_is_v4(const tor_addr_t *addr);
00132 int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
00133
00135
00136 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
00137 int tor_addr_to_reverse_lookup_name(char *out, size_t outlen,
00138 const tor_addr_t *addr);
00139 int tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
00140 int family, int accept_regular);
00141
00142 int tor_addr_port_parse(const char *s, tor_addr_t *addr_out,
00143 uint16_t *port_out);
00144 int tor_addr_parse_mask_ports(const char *s,
00145 tor_addr_t *addr_out, maskbits_t *mask_out,
00146 uint16_t *port_min_out, uint16_t *port_max_out);
00147 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len,
00148 int decorate);
00149 int tor_addr_from_str(tor_addr_t *addr, const char *src);
00150 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
00151 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
00154 #define tor_addr_from_ipv4h(dest, v4addr) \
00155 tor_addr_from_ipv4n((dest), htonl(v4addr))
00156 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
00157 #define tor_addr_from_in(dest, in) \
00158 tor_addr_from_ipv4n((dest), (in)->s_addr);
00159 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
00160 int tor_addr_is_null(const tor_addr_t *addr);
00161 int tor_addr_is_loopback(const tor_addr_t *addr);
00162
00163
00164 int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
00165 int parse_addr_port(int severity, const char *addrport, char **address,
00166 uint32_t *addr, uint16_t *port_out);
00167 int parse_port_range(const char *port, uint16_t *port_min_out,
00168 uint16_t *port_max_out);
00169 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
00170 maskbits_t *maskbits_out, uint16_t *port_min_out,
00171 uint16_t *port_max_out);
00172 int addr_mask_get_bits(uint32_t mask);
00173 int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
00175 #define INET_NTOA_BUF_LEN 16
00176 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
00177 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
00178 int get_interface_address(int severity, uint32_t *addr);
00179
00180 #endif
00181