Back to index

tor  0.2.3.18-rc
address.h
Go to the documentation of this file.
00001 /* Copyright (c) 2003-2004, Roger Dingledine
00002  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See LICENSE for licensing information */
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     uint32_t dummy_; /* This field is here so we have something to initialize
00030                       * with a reliable cross-platform type. */
00031     struct in_addr in_addr;
00032     struct in6_addr in6_addr;
00033   } addr;
00034 } tor_addr_t;
00035 
00037 typedef struct tor_addr_port_t
00038 {
00039   tor_addr_t addr;
00040   uint16_t port;
00041 } tor_addr_port_t;
00042 
00043 #define TOR_ADDR_NULL {AF_UNSPEC, {0}};
00044 
00045 static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
00046 static INLINE uint32_t tor_addr_to_ipv4n(const tor_addr_t *a);
00047 static INLINE uint32_t tor_addr_to_ipv4h(const tor_addr_t *a);
00048 static INLINE uint32_t tor_addr_to_mapped_ipv4h(const tor_addr_t *a);
00049 static INLINE sa_family_t tor_addr_family(const tor_addr_t *a);
00050 static INLINE const struct in_addr *tor_addr_to_in(const tor_addr_t *a);
00051 static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
00052 
00053 socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
00054                                struct sockaddr *sa_out, socklen_t len);
00055 int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
00056                            uint16_t *port_out);
00057 void tor_addr_make_unspec(tor_addr_t *a);
00058 char *tor_sockaddr_to_str(const struct sockaddr *sa);
00059 
00062 static INLINE const struct in6_addr *
00063 tor_addr_to_in6(const tor_addr_t *a)
00064 {
00065   return a->family == AF_INET6 ? &a->addr.in6_addr : NULL;
00066 }
00067 
00072 #define tor_addr_to_in6_addr8(x) tor_addr_to_in6(x)->s6_addr
00073 
00077 #define tor_addr_to_in6_addr16(x) S6_ADDR16(*tor_addr_to_in6(x))
00078 
00082 #define tor_addr_to_in6_addr32(x) S6_ADDR32(*tor_addr_to_in6(x))
00083 
00086 static INLINE uint32_t
00087 tor_addr_to_ipv4n(const tor_addr_t *a)
00088 {
00089   return a->family == AF_INET ? a->addr.in_addr.s_addr : 0;
00090 }
00093 static INLINE uint32_t
00094 tor_addr_to_ipv4h(const tor_addr_t *a)
00095 {
00096   return ntohl(tor_addr_to_ipv4n(a));
00097 }
00102 static INLINE uint32_t
00103 tor_addr_to_mapped_ipv4h(const tor_addr_t *a)
00104 {
00105   return a->family == AF_INET6 ? ntohl(tor_addr_to_in6_addr32(a)[3]) : 0;
00106 }
00109 static INLINE sa_family_t
00110 tor_addr_family(const tor_addr_t *a)
00111 {
00112   return a->family;
00113 }
00116 static INLINE const struct in_addr *
00117 tor_addr_to_in(const tor_addr_t *a)
00118 {
00119   return a->family == AF_INET ? &a->addr.in_addr : NULL;
00120 }
00123 static INLINE int
00124 tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
00125 {
00126   return a->family == AF_INET ? (tor_addr_to_ipv4h(a) == u) : 0;
00127 }
00128 
00136 #define TOR_ADDR_BUF_LEN 48
00137 
00138 int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
00139 char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
00140 
00143 #define fmt_addr(a) fmt_addr_impl((a), 0)
00144 
00146 #define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
00147 const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
00148 const char * fmt_addr32(uint32_t addr);
00149 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
00150 
00155 typedef enum {
00156   CMP_EXACT,
00157   CMP_SEMANTIC,
00158 } tor_addr_comparison_t;
00159 
00160 int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
00161                      tor_addr_comparison_t how);
00162 int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
00163                             maskbits_t mask, tor_addr_comparison_t how);
00166 #define tor_addr_eq(a,b) (0==tor_addr_compare((a),(b),CMP_EXACT))
00167 
00168 unsigned int tor_addr_hash(const tor_addr_t *addr);
00169 int tor_addr_is_v4(const tor_addr_t *addr);
00170 int tor_addr_is_internal(const tor_addr_t *ip, int for_listening);
00171 
00173 /* 32 nybbles, 32 dots, 8 characters of "ip6.arpa", 1 NUL: 73 characters. */
00174 #define REVERSE_LOOKUP_NAME_BUF_LEN 73
00175 int tor_addr_to_PTR_name(char *out, size_t outlen,
00176                                     const tor_addr_t *addr);
00177 int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
00178                                        int family, int accept_regular);
00179 
00180 int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
00181                         uint16_t *port_out);
00182 int tor_addr_parse_mask_ports(const char *s,
00183                               tor_addr_t *addr_out, maskbits_t *mask_out,
00184                               uint16_t *port_min_out, uint16_t *port_max_out);
00185 const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
00186                              int decorate);
00187 int tor_addr_parse(tor_addr_t *addr, const char *src);
00188 void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
00189 void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
00192 #define tor_addr_from_ipv4h(dest, v4addr)       \
00193   tor_addr_from_ipv4n((dest), htonl(v4addr))
00194 void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *bytes);
00196 #define tor_addr_from_in(dest, in) \
00197   tor_addr_from_ipv4n((dest), (in)->s_addr);
00198 void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6);
00199 int tor_addr_is_null(const tor_addr_t *addr);
00200 int tor_addr_is_loopback(const tor_addr_t *addr);
00201 
00202 int tor_addr_port_split(int severity, const char *addrport,
00203                         char **address_out, uint16_t *port_out);
00204 
00205 int tor_addr_hostname_is_local(const char *name);
00206 
00207 /* IPv4 helpers */
00208 int is_internal_IP(uint32_t ip, int for_listening);
00209 int addr_port_lookup(int severity, const char *addrport, char **address,
00210                     uint32_t *addr, uint16_t *port_out);
00211 int parse_port_range(const char *port, uint16_t *port_min_out,
00212                      uint16_t *port_max_out);
00213 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
00214                               maskbits_t *maskbits_out, uint16_t *port_min_out,
00215                               uint16_t *port_max_out);
00216 int addr_mask_get_bits(uint32_t mask);
00217 int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
00219 #define INET_NTOA_BUF_LEN 16
00220 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
00221 char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
00222 int get_interface_address(int severity, uint32_t *addr);
00223 
00224 #endif
00225