address.h File Reference

Headers for address.h. More...

#include "orconfig.h"
#include "torint.h"
#include "compat.h"

Go to the source code of this file.

Data Structures

struct  tor_addr_t

Defines

#define tor_addr_to_in6_addr8(x)   tor_addr_to_in6(x)->s6_addr
#define tor_addr_to_in6_addr16(x)   S6_ADDR16(*tor_addr_to_in6(x))
#define tor_addr_to_in6_addr32(x)   S6_ADDR32(*tor_addr_to_in6(x))
#define TOR_ADDR_BUF_LEN   48
#define tor_addr_eq(a, b)   (0==tor_addr_compare((a),(b),CMP_EXACT))
#define REVERSE_LOOKUP_NAME_BUF_LEN   73
#define tor_addr_from_ipv4h(dest, v4addr)   tor_addr_from_ipv4n((dest), htonl(v4addr))
#define tor_addr_from_in(dest, in)   tor_addr_from_ipv4n((dest), (in)->s_addr);
#define INET_NTOA_BUF_LEN   16

Typedefs

typedef uint8_t maskbits_t

Enumerations

enum  tor_addr_comparison_t { CMP_EXACT, CMP_SEMANTIC }

Functions

static INLINE struct in6_addr * tor_addr_to_in6 (const tor_addr_t *a)
static INLINE uint32_t tor_addr_to_ipv4n (const tor_addr_t *a)
static INLINE uint32_t tor_addr_to_ipv4h (const tor_addr_t *a)
static INLINE uint32_t tor_addr_to_mapped_ipv4h (const tor_addr_t *a)
static INLINE sa_family_t tor_addr_family (const tor_addr_t *a)
static INLINE struct in_addr * tor_addr_to_in (const tor_addr_t *a)
static INLINE int tor_addr_eq_ipv4h (const tor_addr_t *a, uint32_t u)
socklen_t tor_addr_to_sockaddr (const tor_addr_t *a, uint16_t port, struct sockaddr *sa_out, socklen_t len)
int tor_addr_from_sockaddr (tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
void tor_addr_make_unspec (tor_addr_t *a)
int tor_addr_lookup (const char *name, uint16_t family, tor_addr_t *addr_out)
char * tor_dup_addr (const tor_addr_t *addr) ATTR_MALLOC
const char * fmt_addr (const tor_addr_t *addr)
int get_interface_address6 (int severity, sa_family_t family, tor_addr_t *addr)
int tor_addr_compare (const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
int tor_addr_compare_masked (const tor_addr_t *addr1, const tor_addr_t *addr2, maskbits_t mask, tor_addr_comparison_t how)
unsigned int tor_addr_hash (const tor_addr_t *addr)
int tor_addr_is_v4 (const tor_addr_t *addr)
int tor_addr_is_internal (const tor_addr_t *ip, int for_listening) ATTR_PURE
int tor_addr_to_reverse_lookup_name (char *out, size_t outlen, const tor_addr_t *addr)
int tor_addr_parse_reverse_lookup_name (tor_addr_t *result, const char *address, int family, int accept_regular)
int tor_addr_port_parse (const char *s, tor_addr_t *addr_out, uint16_t *port_out)
int tor_addr_parse_mask_ports (const char *s, tor_addr_t *addr_out, maskbits_t *mask_out, uint16_t *port_min_out, uint16_t *port_max_out)
const char * tor_addr_to_str (char *dest, const tor_addr_t *addr, int len, int decorate)
int tor_addr_from_str (tor_addr_t *addr, const char *src)
void tor_addr_copy (tor_addr_t *dest, const tor_addr_t *src)
void tor_addr_from_ipv4n (tor_addr_t *dest, uint32_t v4addr)
void tor_addr_from_ipv6_bytes (tor_addr_t *dest, const char *bytes)
void tor_addr_from_in6 (tor_addr_t *dest, const struct in6_addr *in6)
int tor_addr_is_null (const tor_addr_t *addr)
int tor_addr_is_loopback (const tor_addr_t *addr)
int is_internal_IP (uint32_t ip, int for_listening) ATTR_PURE
int parse_addr_port (int severity, const char *addrport, char **address, uint32_t *addr, uint16_t *port_out)
int parse_port_range (const char *port, uint16_t *port_min_out, uint16_t *port_max_out)
int parse_addr_and_port_range (const char *s, uint32_t *addr_out, maskbits_t *maskbits_out, uint16_t *port_min_out, uint16_t *port_max_out)
int addr_mask_get_bits (uint32_t mask)
int addr_mask_cmp_bits (uint32_t a1, uint32_t a2, maskbits_t bits)
int tor_inet_ntoa (const struct in_addr *in, char *buf, size_t buf_len)
char * tor_dup_ip (uint32_t addr) ATTR_MALLOC
int get_interface_address (int severity, uint32_t *addr)


Detailed Description

Headers for address.h.


Define Documentation

#define INET_NTOA_BUF_LEN   16

#define REVERSE_LOOKUP_NAME_BUF_LEN   73

Longest length that can be required for a reverse lookup name.

Referenced by connection_ap_handshake_send_resolve(), and tor_addr_to_reverse_lookup_name().

#define tor_addr_eq ( a,
 )     (0==tor_addr_compare((a),(b),CMP_EXACT))

#define tor_addr_from_ipv4h ( dest,
v4addr   )     tor_addr_from_ipv4n((dest), htonl(v4addr))


Typedef Documentation

typedef uint8_t maskbits_t

The number of bits from an address to consider while doing a masked comparison.


Enumeration Type Documentation

Flag to specify how to do a comparison between addresses. In an "exact" comparison, addresses are equivalent only if they are in the same family with the same value. In a "semantic" comparison, IPv4 addresses match all IPv6 encodings of those addresses.


Function Documentation

int addr_mask_cmp_bits ( uint32_t  a1,
uint32_t  a2,
maskbits_t  bits 
)

Compare two addresses a1 and a2 for equality under a netmask of mbits bits. Return -1, 0, or 1.

XXXX_IP6 Temporary function to allow masks as bitcounts everywhere. This will be replaced with an IPv6-aware version as soon as 32-bit addresses are no longer passed around.

Referenced by address_is_in_virtual_range(), addressmap_get_virtual_address(), and parse_virtual_addr_network().

int addr_mask_get_bits ( uint32_t  mask  ) 

If mask is an address mask for a bit-prefix, return the number of bits. Otherwise, return -1.

Referenced by parse_addr_and_port_range(), and tor_addr_parse_mask_ports().

const char* fmt_addr ( const tor_addr_t addr  ) 

Return a string representing the address addr. This string is statically allocated, and must not be freed. Each call to fmt_addr invalidates the last result of the function. This function is not thread-safe.

References tor_addr_to_str().

Referenced by circuit_extend(), circuit_handle_first_hop(), connection_edge_finished_connecting(), connection_handle_listener_read(), connection_proxy_connect(), fetch_bridge_descriptors(), parse_bridge_line(), and rend_add_service().

int get_interface_address ( int  severity,
uint32_t *  addr 
)

Set *addr to the host-order IPv4 address (if any) of whatever interface connects to the Internet. This address should only be used in checking whether our address has changed. Return 0 on success, -1 on failure.

References get_interface_address6(), and tor_addr_to_ipv4h().

Referenced by client_check_address_changed(), and resolve_my_address().

int get_interface_address6 ( int  severity,
sa_family_t  family,
tor_addr_t addr 
)

Set *addr to the IP address (if any) of whatever interface connects to the Internet. This address should only be used in checking whether our address has changed. Return 0 on success, -1 on failure.

References LD_NET, tor_addr_from_sockaddr(), tor_assert, tor_close_socket(), and tor_open_socket().

Referenced by get_interface_address().

int is_internal_IP ( uint32_t  ip,
int  for_listening 
)

Return true iff ip (in host order) is an IP reserved to localhost, or reserved for local networks by RFC 1918.

References tor_addr_t::addr, tor_addr_t::family, tor_addr_t::in_addr, and tor_addr_is_internal().

Referenced by connection_ap_process_end_not_open(), connection_edge_process_relay_cell_not_open(), dirserv_router_has_valid_address(), http_set_address_origin(), options_validate(), resolve_my_address(), and router_new_address_suggestion().

int parse_addr_and_port_range ( const char *  s,
uint32_t *  addr_out,
maskbits_t maskbits_out,
uint16_t *  port_min_out,
uint16_t *  port_max_out 
)

Parse a string s in the format of (IP(/mask|/mask-bits)?|*)(:(*|port(-maxport))?)?, setting the various *out pointers as appropriate. Return 0 on success, -1 on failure.

References addr_mask_get_bits(), escaped(), LD_GENERAL, parse_port_range(), tor_assert, tor_free, and tor_inet_aton().

Referenced by parse_virtual_addr_network().

int parse_addr_port ( int  severity,
const char *  addrport,
char **  address,
uint32_t *  addr,
uint16_t *  port_out 
)

Parse a string of the form "host[:port]" from addrport. If address is provided, set *address to a copy of the host portion of the string. If addr is provided, try to resolve the host portion of the string and store it into *addr (in host byte order). If port_out is provided, store the port number into *port_out, or 0 if no port is given. If port_out is NULL, then there must be no port number in addrport. Return 0 on success, -1 on failure.

References esc_for_log(), escaped(), LD_GENERAL, LD_NET, tor_assert, tor_free, tor_lookup_hostname(), and tor_parse_long().

Referenced by authority_cert_parse_from_string(), connection_exit_begin_conn(), create_inet_sockaddr(), options_validate(), parse_dir_server_line(), and retry_listeners().

int parse_port_range ( const char *  port,
uint16_t *  port_min_out,
uint16_t *  port_max_out 
)

Parse a string s in the format of (*|port(-maxport)?)?, setting the various *out pointers as appropriate. Return 0 on success, -1 on failure.

References escaped(), LD_GENERAL, tor_assert, and tor_parse_long().

Referenced by parse_addr_and_port_range(), router_parse_addr_policy_private(), and tor_addr_parse_mask_ports().

int tor_addr_compare ( const tor_addr_t addr1,
const tor_addr_t addr2,
tor_addr_comparison_t  how 
)

Given two addresses addr1 and addr2, return 0 if the two addresses are equivalent under the mask mbits, less than 0 if addr1 precedes addr2, and greater than 0 otherwise.

Different address families (IPv4 vs IPv6) are always considered unequal if how is CMP_EXACT; otherwise, IPv6-mapped IPv4 addresses are considered equivalent to their IPv4 equivalents.

References tor_addr_compare_masked().

Referenced by cmp_single_addr_policy(), connection_or_get_for_extend(), connection_or_group_set_badness(), and policy_summary_add_item().

int tor_addr_compare_masked ( const tor_addr_t addr1,
const tor_addr_t addr2,
maskbits_t  mbits,
tor_addr_comparison_t  how 
)

As tor_addr_compare(), but only looks at the first mask bits of the address.

Reduce over-specific masks (>128 for ipv6, >32 for ipv4) to 128 or 32.

The mask is interpreted relative to addr1, so that if a is ffff:1.2.3.4, and b is 3.4.5.6, tor_addr_compare_masked(a,b,100,CMP_SEMANTIC) is the same as -tor_addr_compare_masked(b,a,4,CMP_SEMANTIC).

We guarantee that the ordering from tor_addr_compare_masked is a total order on addresses, but not that it is any particular order, or that it will be the same from one version to the next.

References tor_addr_family(), tor_addr_is_v4(), tor_addr_to_ipv4h(), tor_assert, and tor_fragile_assert.

Referenced by addr_policy_covers(), addr_policy_intersects(), compare_known_tor_addr_to_addr_policy(), compare_known_tor_addr_to_addr_policy_noport(), and tor_addr_compare().

void tor_addr_copy ( tor_addr_t dest,
const tor_addr_t src 
)

static INLINE int tor_addr_eq_ipv4h ( const tor_addr_t a,
uint32_t  u 
) [static]

static INLINE sa_family_t tor_addr_family ( const tor_addr_t a  )  [static]

void tor_addr_from_in6 ( tor_addr_t dest,
const struct in6_addr *  in6 
)

void tor_addr_from_ipv4n ( tor_addr_t dest,
uint32_t  v4addr 
)

Set dest to equal the IPv4 address in v4addr (given in network order).

References tor_addr_t::addr, tor_addr_t::family, tor_addr_t::in_addr, and tor_assert.

Referenced by connection_ap_get_original_destination(), decode_address_from_payload(), fetch_from_buf_socks(), rend_service_introduce(), and tor_addr_from_sockaddr().

void tor_addr_from_ipv6_bytes ( tor_addr_t dest,
const char *  ipv6_bytes 
)

Set dest to equal the IPv6 address in the 16 bytes at ipv6_bytes.

References tor_addr_t::addr, tor_addr_t::family, tor_addr_t::in6_addr, and tor_assert.

Referenced by decode_address_from_payload(), fetch_from_buf_socks(), and tor_addr_from_in6().

int tor_addr_from_sockaddr ( tor_addr_t a,
const struct sockaddr *  sa,
uint16_t *  port_out 
)

int tor_addr_from_str ( tor_addr_t addr,
const char *  src 
)

Convert the string in src to a tor_addr_t addr. The string may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by square brackets.

Return an address family on success, or -1 if an invalid address string is provided.

References tor_addr_from_in6(), tor_assert, tor_free, and tor_inet_pton().

Referenced by circuit_get_open_circ_or_launch(), circuit_is_acceptable(), configure_nameservers(), connection_ap_fail_onehop(), dns_resolve_impl(), rend_parse_introduction_points(), and tor_addr_parse_reverse_lookup_name().

unsigned int tor_addr_hash ( const tor_addr_t addr  ) 

Return a hash code based on the address addr

References tor_addr_family(), tor_addr_to_ipv4h(), and tor_fragile_assert.

Referenced by policy_hash().

int tor_addr_is_internal ( const tor_addr_t addr,
int  for_listening 
)

Return true iff ip is an IP reserved to localhost or local networks in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is also treated as internal for now.)

References LD_BUG, tor_addr_family(), tor_addr_is_v4(), and tor_addr_to_ipv4h().

Referenced by connection_is_rate_limited(), dns_resolve_impl(), exit_policy_is_general_exit_helper(), is_internal_IP(), and is_local_addr().

int tor_addr_is_loopback ( const tor_addr_t addr  ) 

Return true iff addr is a loopback address

References tor_addr_family(), tor_addr_to_ipv4h(), tor_assert, and tor_fragile_assert.

Referenced by connection_connect().

int tor_addr_is_null ( const tor_addr_t addr  ) 

Determine whether an address addr is null, either all zeroes or belonging to family AF_UNSPEC.

References LD_BUG, tor_addr_family(), tor_addr_to_ipv4n(), and tor_assert.

Referenced by compare_tor_addr_to_addr_policy(), connection_ap_expire_beginning(), connection_ap_handshake_attach_circuit(), connection_exit_begin_conn(), and router_compare_to_my_exit_policy().

int tor_addr_is_v4 ( const tor_addr_t addr  ) 

Determine whether an address is IPv4, either native or IPv4-mapped IPv6. Note that this is about representation only, as any decent stack will reject IPv4-mapped addresses received on the wire (and won't use them on the wire either).

References tor_addr_family(), and tor_assert.

Referenced by tor_addr_compare_masked(), tor_addr_is_internal(), and tor_addr_parse_mask_ports().

int tor_addr_lookup ( const char *  name,
uint16_t  family,
tor_addr_t addr 
)

Similar behavior to Unix gethostbyname: resolve name, and set *addr to the proper IP address and family. The family argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a preferred family, though another one may be returned if only one family is implemented for this address.

Return 0 on success, -1 on failure; 1 on transient failure.

References tor_addr_from_in6(), tor_assert, and tor_inet_pton().

Referenced by tor_addr_port_parse(), and tor_lookup_hostname().

void tor_addr_make_unspec ( tor_addr_t a  ) 

Set address a to the unspecified address. This address belongs to no family.

References tor_addr_t::family.

Referenced by connection_ap_make_link(), connection_handle_listener_read(), decode_address_from_payload(), and tor_addr_from_sockaddr().

int tor_addr_parse_mask_ports ( const char *  s,
tor_addr_t addr_out,
maskbits_t maskbits_out,
uint16_t *  port_min_out,
uint16_t *  port_max_out 
)

Parse a string s containing an IPv4/IPv6 address, and possibly a mask and port or port range. Store the parsed address in addr_out, a mask (if any) in mask_out, and port(s) (if any) in port_min_out and port_max_out.

The syntax is: Address OptMask OptPortRange Address ::= IPv4Address / "[" IPv6Address "]" / "*" OptMask ::= "/" Integer / OptPortRange ::= ":*" / ":" Integer / ":" Integer "-" Integer /

  • If mask, minport, or maxport are NULL, we do not want these options to be set; treat them as an error if present.
  • If the string has no mask, the mask is set to /32 (IPv4) or /128 (IPv6).
  • If the string has one port, it is placed in both min and max port variables.
  • If the string has no port(s), port_(min|max)_out are set to 1 and 65535.

Return an address family on success, or -1 if an invalid address string is provided.

References addr_mask_get_bits(), escaped(), LD_GENERAL, parse_port_range(), tor_addr_family(), tor_addr_from_in6(), tor_addr_from_ipv4h, tor_addr_is_v4(), tor_assert, tor_free, and tor_inet_pton().

Referenced by policy_expand_private(), policy_summary_add_item(), and router_parse_addr_policy().

int tor_addr_parse_reverse_lookup_name ( tor_addr_t result,
const char *  address,
int  family,
int  accept_regular 
)

Parse an .in-addr.arpa or .ip6.arpa address from address. Return 0 if this is not an .in-addr.arpa address or an .ip6.arpa address. Return -1 if this is an ill-formed .in-addr.arpa address or an .ip6.arpa address. Also return -1 if family is not AF_UNSPEC, and the parsed address family does not match family. On success, return 1, and store the result, if any, into result, if provided.

If accept_regular is set and the address is in neither recognized reverse lookup hostname format, try parsing the address as a regular IPv4 or IPv6 address too.

References hex_decode_digit(), INET_NTOA_BUF_LEN, strcasecmpend(), tor_addr_from_in6(), tor_addr_from_str(), and tor_inet_aton().

Referenced by connection_ap_handshake_send_resolve(), dns_resolve_impl(), and launch_resolve().

int tor_addr_port_parse ( const char *  s,
tor_addr_t addr_out,
uint16_t *  port_out 
)

Parse an address or address-port combination from s, resolve the address as needed, and put the result in addr_out and (optionally) port_out. Return 0 on success, negative on failure.

References eat_whitespace(), tor_addr_copy(), tor_addr_lookup(), tor_assert, tor_free, and tor_parse_long().

Referenced by parse_bridge_line(), and parse_port_config().

static INLINE struct in_addr * tor_addr_to_in ( const tor_addr_t a  )  [static, read]

Return an in_addr* equivalent to a, or NULL if a is not an IPv4 address.

References tor_addr_t::addr, tor_addr_t::family, and tor_addr_t::in_addr.

Referenced by launch_resolve().

static INLINE struct in6_addr * tor_addr_to_in6 ( const tor_addr_t a  )  [static, read]

Return an in6_addr* equivalent to a, or NULL if a is not an IPv6 address.

References tor_addr_t::addr, tor_addr_t::family, and tor_addr_t::in6_addr.

Referenced by connection_ap_get_original_destination(), connection_send_socks5_connect(), launch_resolve(), and tor_addr_to_sockaddr().

static INLINE uint32_t tor_addr_to_ipv4h ( const tor_addr_t a  )  [static]

static INLINE uint32_t tor_addr_to_ipv4n ( const tor_addr_t a  )  [static]

int tor_addr_to_reverse_lookup_name ( char *  out,
size_t  outlen,
const tor_addr_t addr 
)

Convert addr to an in-addr.arpa name or a .ip6.arpa name, and store the result in the outlen-byte buffer at out. Return 0 on success, -1 on failure.

References tor_addr_t::family, REVERSE_LOOKUP_NAME_BUF_LEN, tor_addr_to_ipv4h(), and tor_snprintf().

Referenced by connection_ap_handshake_send_resolve().

socklen_t tor_addr_to_sockaddr ( const tor_addr_t a,
uint16_t  port,
struct sockaddr *  sa_out,
socklen_t  len 
)

Convert the tor_addr_t in a, with port in port, into a socklen object in *sa_out of object size len. If not enough room is free, or on error, return -1. Else return the length of the sockaddr.

References tor_addr_family(), tor_addr_to_in6(), and tor_addr_to_ipv4n().

Referenced by configure_nameservers(), and connection_connect().

const char* tor_addr_to_str ( char *  dest,
const tor_addr_t addr,
int  len,
int  decorate 
)

Convert a tor_addr_t addr into a string, and store it in dest of size len. Returns a pointer to dest on success, or NULL on failure. If decorate, surround IPv6 addresses with brackets.

References tor_addr_t::addr, tor_addr_t::in6_addr, tor_addr_t::in_addr, tor_addr_family(), tor_assert, and tor_inet_ntop().

Referenced by connection_ap_get_original_destination(), fetch_from_buf_socks(), fmt_addr(), policy_write_item(), and tor_dup_addr().

char* tor_dup_addr ( const tor_addr_t addr  ) 

char* tor_dup_ip ( uint32_t  addr  ) 

int tor_inet_ntoa ( const struct in_addr *  in,
char *  buf,
size_t  buf_len 
)


Generated on Tue May 25 00:30:32 2010 for tor by  doxygen 1.5.6