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 */
9 * \brief Headers for address.h
14 #define ADDRESS_H_ID "$Id$"
20 typedef uint8_t maskbits_t
;
22 /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
23 * sockaddr_storage.) */
24 typedef struct tor_addr_t
28 struct in_addr in_addr
;
29 struct in6_addr in6_addr
;
34 static INLINE
uint32_t tor_addr_to_ipv4n(const tor_addr_t
*a
);
35 static INLINE
uint32_t tor_addr_to_ipv4h(const tor_addr_t
*a
);
36 static INLINE
uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t
*a
);
37 static INLINE sa_family_t
tor_addr_family(const tor_addr_t
*a
);
38 static INLINE
const struct in_addr
*tor_addr_to_in(const tor_addr_t
*a
);
39 static INLINE
const struct in6_addr
*tor_addr_to_in6(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
,
45 void tor_addr_make_unspec(tor_addr_t
*a
);
47 static INLINE
const struct in6_addr
*
48 tor_addr_to_in6(const tor_addr_t
*a
)
50 return a
->family
== AF_INET6
? &a
->addr
.in6_addr
: NULL
;
53 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
54 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
55 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
57 static INLINE
uint32_t
58 tor_addr_to_ipv4n(const tor_addr_t
*a
)
60 return a
->family
== AF_INET
? a
->addr
.in_addr
.s_addr
: 0;
62 static INLINE
uint32_t
63 tor_addr_to_ipv4h(const tor_addr_t
*a
)
65 return ntohl(tor_addr_to_ipv4n(a
));
67 static INLINE
uint32_t
68 tor_addr_to_mapped_ipv4h(const tor_addr_t
*a
)
70 return ntohl(tor_addr_to_in6_addr32(a
)[3]);
72 static INLINE sa_family_t
73 tor_addr_family(const tor_addr_t
*a
)
77 static INLINE
const struct in_addr
*
78 tor_addr_to_in(const tor_addr_t
*a
)
80 return a
->family
== AF_INET
? &a
->addr
.in_addr
: NULL
;
83 tor_addr_eq_ipv4h(const tor_addr_t
*a
, uint32_t u
)
85 return a
->family
== AF_INET
? (tor_addr_to_ipv4h(a
) == u
) : 0;
88 #define TOR_ADDR_BUF_LEN 48 /* [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]
91 int tor_addr_lookup(const char *name
, uint16_t family
, tor_addr_t
*addr_out
);
92 char *tor_dup_addr(const tor_addr_t
*addr
) ATTR_MALLOC
;
93 const char *fmt_addr(const tor_addr_t
*addr
);
94 int get_interface_address6(int severity
, sa_family_t family
, tor_addr_t
*addr
);
96 /** Flag to specify how to do a comparison between addresses. In an "exact"
97 * comparison, addresses are equivalent only if they are in the same family
98 * with the same value. In a "semantic" comparison, IPv4 addresses match all
99 * IPv6 encodings of those addresses. */
103 } tor_addr_comparison_t
;
105 int tor_addr_compare(const tor_addr_t
*addr1
, const tor_addr_t
*addr2
,
106 tor_addr_comparison_t how
);
107 int tor_addr_compare_masked(const tor_addr_t
*addr1
, const tor_addr_t
*addr2
,
108 maskbits_t mask
, tor_addr_comparison_t how
);
109 /** Return true iff a and b are the same address. The comparison is done
111 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
113 unsigned int tor_addr_hash(const tor_addr_t
*addr
);
114 int tor_addr_is_v4(const tor_addr_t
*addr
);
115 int tor_addr_is_internal(const tor_addr_t
*ip
, int for_listening
) ATTR_PURE
;
116 int tor_addr_port_parse(const char *s
, tor_addr_t
*addr_out
,
118 int tor_addr_parse_mask_ports(const char *s
,
119 tor_addr_t
*addr_out
, maskbits_t
*mask_out
,
120 uint16_t *port_min_out
, uint16_t *port_max_out
);
121 const char * tor_addr_to_str(char *dest
, const tor_addr_t
*addr
, int len
,
123 int tor_addr_from_str(tor_addr_t
*addr
, const char *src
);
124 void tor_addr_copy(tor_addr_t
*dest
, const tor_addr_t
*src
);
125 void tor_addr_from_ipv4n(tor_addr_t
*dest
, uint32_t v4addr
);
126 /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
128 #define tor_addr_from_ipv4h(dest, v4addr) \
129 tor_addr_from_ipv4n((dest), htonl(v4addr))
130 void tor_addr_from_ipv6_bytes(tor_addr_t
*dest
, const char *bytes
);
131 void tor_addr_from_in6(tor_addr_t
*dest
, const struct in6_addr
*in6
);
132 int tor_addr_is_null(const tor_addr_t
*addr
);
133 int tor_addr_is_loopback(const tor_addr_t
*addr
);
136 int is_internal_IP(uint32_t ip
, int for_listening
) ATTR_PURE
;
137 int parse_addr_port(int severity
, const char *addrport
, char **address
,
138 uint32_t *addr
, uint16_t *port_out
);
139 int parse_port_range(const char *port
, uint16_t *port_min_out
,
140 uint16_t *port_max_out
);
141 int parse_addr_and_port_range(const char *s
, uint32_t *addr_out
,
142 maskbits_t
*maskbits_out
, uint16_t *port_min_out
,
143 uint16_t *port_max_out
);
144 int addr_mask_get_bits(uint32_t mask
);
145 int addr_mask_cmp_bits(uint32_t a1
, uint32_t a2
, maskbits_t bits
);
146 #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
147 int tor_inet_ntoa(const struct in_addr
*in
, char *buf
, size_t buf_len
);
148 char *tor_dup_ip(uint32_t addr
) ATTR_MALLOC
;
149 int get_interface_address(int severity
, uint32_t *addr
);