Move in-addr.arpa parsing and generation into address.c, and simplify the code that...
[tor/rransom.git] / src / common / address.h
blobfa2ac1b5bc930f598ce8a59a6a2c634af5642d26
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$ */
7 /**
8 * \file address.h
9 * \brief Headers for address.h
10 **/
12 #ifndef __ADDRESS_H
13 #define __ADDRESS_H
14 #define ADDRESS_H_ID "$Id$"
16 #include "orconfig.h"
17 #include "torint.h"
18 #include "compat.h"
20 typedef uint8_t maskbits_t;
21 struct in_addr;
22 /** Holds an IPv4 or IPv6 address. (Uses less memory than struct
23 * sockaddr_storage.) */
24 typedef struct tor_addr_t
26 sa_family_t family;
27 union {
28 struct in_addr in_addr;
29 struct in6_addr in6_addr;
30 } addr;
31 } tor_addr_t;
33 /* DOCDOC*/
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,
44 uint16_t *port_out);
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)
75 return a->family;
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;
82 static INLINE int
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 void tor_addr_assign(tor_addr_t *dest, const tor_addr_t *src);
94 const char *fmt_addr(const tor_addr_t *addr);
95 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
97 /** Flag to specify how to do a comparison between addresses. In an "exact"
98 * comparison, addresses are equivalent only if they are in the same family
99 * with the same value. In a "semantic" comparison, IPv4 addresses match all
100 * IPv6 encodings of those addresses. */
101 typedef enum {
102 CMP_EXACT,
103 CMP_SEMANTIC,
104 } tor_addr_comparison_t;
106 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
107 tor_addr_comparison_t how);
108 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
109 maskbits_t mask, tor_addr_comparison_t how);
110 /** Return true iff a and b are the same address. The comparison is done
111 * "exactly". */
112 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
114 unsigned int tor_addr_hash(const tor_addr_t *addr);
115 int tor_addr_is_v4(const tor_addr_t *addr);
116 int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
118 /** Longest length that can be required for a reverse lookup name. */
119 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
120 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
121 int tor_addr_to_reverse_lookup_name(char *out, size_t outlen,
122 const tor_addr_t *addr);
123 int tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
124 int family, int accept_regular);
126 int tor_addr_port_parse(const char *s, tor_addr_t *addr_out,
127 uint16_t *port_out);
128 int tor_addr_parse_mask_ports(const char *s,
129 tor_addr_t *addr_out, maskbits_t *mask_out,
130 uint16_t *port_min_out, uint16_t *port_max_out);
131 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len,
132 int decorate);
133 int tor_addr_from_str(tor_addr_t *addr, const char *src);
134 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
135 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
136 /** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
137 * order. */
138 #define tor_addr_from_ipv4h(dest, v4addr) \
139 tor_addr_from_ipv4n((dest), htonl(v4addr))
140 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
141 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
142 int tor_addr_is_null(const tor_addr_t *addr);
143 int tor_addr_is_loopback(const tor_addr_t *addr);
145 /* IPv4 helpers */
146 int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
147 int parse_addr_port(int severity, const char *addrport, char **address,
148 uint32_t *addr, uint16_t *port_out);
149 int parse_port_range(const char *port, uint16_t *port_min_out,
150 uint16_t *port_max_out);
151 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
152 maskbits_t *maskbits_out, uint16_t *port_min_out,
153 uint16_t *port_max_out);
154 int addr_mask_get_bits(uint32_t mask);
155 int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
156 #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
157 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
158 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
159 int get_interface_address(int severity, uint32_t *addr);
161 #endif