Merge remote-tracking branch 'linus/bug6363_only-ln'
[tor.git] / src / common / address.h
blob7a779d8880c14790fd2b8624c7a14e9a2b0d3523
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2012, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file address.h
8 * \brief Headers for address.h
9 **/
11 #ifndef _TOR_ADDRESS_H
12 #define _TOR_ADDRESS_H
14 #include "orconfig.h"
15 #include "torint.h"
16 #include "compat.h"
18 /** The number of bits from an address to consider while doing a masked
19 * comparison. */
20 typedef uint8_t maskbits_t;
22 struct in_addr;
23 /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
24 * sockaddr_storage.) */
25 typedef struct tor_addr_t
27 sa_family_t family;
28 union {
29 uint32_t dummy_; /* This field is here so we have something to initialize
30 * with a reliable cross-platform type. */
31 struct in_addr in_addr;
32 struct in6_addr in6_addr;
33 } addr;
34 } tor_addr_t;
36 /** Holds an IP address and a TCP/UDP port. */
37 typedef struct tor_addr_port_t
39 tor_addr_t addr;
40 uint16_t port;
41 } tor_addr_port_t;
43 #define TOR_ADDR_NULL {AF_UNSPEC, {0}}
45 static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
46 static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
47 static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
48 static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
49 static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
50 static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
51 static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
53 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
54 struct sockaddr *sa_out, socklen_t len);
55 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
56 uint16_t *port_out);
57 void tor_addr_make_unspec(tor_addr_t *a);
58 char *tor_sockaddr_to_str(const struct sockaddr *sa);
60 /** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
61 * an IPv6 address. */
62 static INLINE const struct in6_addr *
63 tor_addr_to_in6(const tor_addr_t *a)
65 return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
68 /** Given an IPv6 address <b>x</b>, yield it as an array of uint8_t.
70 * Requires that <b>x</b> is actually an IPv6 address.
72 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
73 /** Given an IPv6 address <b>x</b>, yield it as an array of uint16_t.
75 * Requires that <b>x</b> is actually an IPv6 address.
77 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
78 /** Given an IPv6 address <b>x</b>, yield it as an array of uint32_t.
80 * Requires that <b>x</b> is actually an IPv6 address.
82 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
84 /** Return an IPv4 address in network order for <b>a</b>, or 0 if
85 * <b>a</b> is not an IPv4 address. */
86 static INLINE uint32_t
87 tor_addr_to_ipv4n(const tor_addr_t *a)
89 return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
91 /** Return an IPv4 address in host order for <b>a</b>, or 0 if
92 * <b>a</b> is not an IPv4 address. */
93 static INLINE uint32_t
94 tor_addr_to_ipv4h(const tor_addr_t *a)
96 return ntohl(tor_addr_to_ipv4n(a));
98 /** Given an IPv6 address, return its mapped IPv4 address in host order, or
99 * 0 if <b>a</b> is not an IPv6 address.
101 * (Does not check whether the address is really a mapped address */
102 static INLINE uint32_t
103 tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
105 return a->family == AF_INET6 ? ntohl(tor_addr_to_in6_addr32(a)[3]) : 0;
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)
112 return a->family;
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)
119 return a->family == AF_INET ? &a->addr.in_addr : NULL;
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)
126 return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
129 /** Length of a buffer that you need to allocate to be sure you can encode
130 * any tor_addr_t.
132 * This allows enough space for
133 * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]",
134 * plus a terminating NUL.
136 #define TOR_ADDR_BUF_LEN 48
138 int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
139 char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
141 /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
142 * addresses. */
143 #define fmt_addr(a) fmt_addr_impl((a), 0)
144 /** Wrapper function of fmt_addr_impl(). It decorates IPv6
145 * addresses. */
146 #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
147 const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
148 const char * fmt_addr32(uint32_t addr);
149 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
151 /** Flag to specify how to do a comparison between addresses. In an "exact"
152 * comparison, addresses are equivalent only if they are in the same family
153 * with the same value. In a "semantic" comparison, IPv4 addresses match all
154 * IPv6 encodings of those addresses. */
155 typedef enum {
156 CMP_EXACT,
157 CMP_SEMANTIC,
158 } tor_addr_comparison_t;
160 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
161 tor_addr_comparison_t how);
162 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
163 maskbits_t mask, tor_addr_comparison_t how);
164 /** Return true iff a and b are the same address. The comparison is done
165 * "exactly". */
166 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
168 unsigned int tor_addr_hash(const tor_addr_t *addr);
169 int tor_addr_is_v4(const tor_addr_t *addr);
170 int tor_addr_is_internal(const tor_addr_t *ip, int for_listening);
172 /** Longest length that can be required for a reverse lookup name. */
173 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
174 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
175 int tor_addr_to_PTR_name(char *out, size_t outlen,
176 const tor_addr_t *addr);
177 int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
178 int family, int accept_regular);
180 int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
181 uint16_t *port_out);
182 int tor_addr_parse_mask_ports(const char *s,
183 tor_addr_t *addr_out, maskbits_t *mask_out,
184 uint16_t *port_min_out, uint16_t *port_max_out);
185 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
186 int decorate);
187 int tor_addr_parse(tor_addr_t *addr, const char *src);
188 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
189 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
190 /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
191 * order. */
192 #define tor_addr_from_ipv4h(dest, v4addr) \
193 tor_addr_from_ipv4n((dest), htonl(v4addr))
194 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
195 /** Set <b>dest</b> to the IPv4 address incoded in <b>in</b>. */
196 #define tor_addr_from_in(dest, in) \
197 tor_addr_from_ipv4n((dest), (in)->s_addr);
198 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
199 int tor_addr_is_null(const tor_addr_t *addr);
200 int tor_addr_is_loopback(const tor_addr_t *addr);
202 int tor_addr_port_split(int severity, const char *addrport,
203 char **address_out, uint16_t *port_out);
205 int tor_addr_hostname_is_local(const char *name);
207 /* IPv4 helpers */
208 int is_internal_IP(uint32_t ip, int for_listening);
209 int addr_port_lookup(int severity, const char *addrport, char **address,
210 uint32_t *addr, uint16_t *port_out);
211 int parse_port_range(const char *port, uint16_t *port_min_out,
212 uint16_t *port_max_out);
213 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
214 maskbits_t *maskbits_out, uint16_t *port_min_out,
215 uint16_t *port_max_out);
216 int addr_mask_get_bits(uint32_t mask);
217 int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
218 /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
219 #define INET_NTOA_BUF_LEN 16
220 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
221 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
222 int get_interface_address(int severity, uint32_t *addr);
224 tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port);
226 #endif