#include "orconfig.h"
#include "compat.h"
#include "util.h"
#include "address.h"
#include "log.h"
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/param.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
Defines | |
#define | MAX_ADDRESS_LENGTH (TOR_ADDR_BUF_LEN+2+(1+INET_NTOA_BUF_LEN)+12+1) |
#define | TRISTATE(a, b) (((a)<(b))?-1: (((a)==(b))?0:1)) |
Functions | |
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) |
int | tor_addr_is_internal (const tor_addr_t *addr, int for_listening) |
const char * | tor_addr_to_str (char *dest, const tor_addr_t *addr, int len, int decorate) |
int | tor_addr_parse_reverse_lookup_name (tor_addr_t *result, const char *address, int family, int accept_regular) |
int | tor_addr_to_reverse_lookup_name (char *out, size_t outlen, const tor_addr_t *addr) |
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) |
int | tor_addr_is_v4 (const tor_addr_t *addr) |
int | tor_addr_is_null (const tor_addr_t *addr) |
int | tor_addr_is_loopback (const tor_addr_t *addr) |
void | tor_addr_from_ipv4n (tor_addr_t *dest, uint32_t v4addr) |
void | tor_addr_from_ipv6_bytes (tor_addr_t *dest, const char *ipv6_bytes) |
void | tor_addr_from_in6 (tor_addr_t *dest, const struct in6_addr *in6) |
void | tor_addr_copy (tor_addr_t *dest, const tor_addr_t *src) |
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 mbits, tor_addr_comparison_t how) |
unsigned int | tor_addr_hash (const tor_addr_t *addr) |
char * | tor_dup_addr (const tor_addr_t *addr) |
const char * | fmt_addr (const tor_addr_t *addr) |
int | tor_addr_from_str (tor_addr_t *addr, const char *src) |
int | tor_addr_port_parse (const char *s, tor_addr_t *addr_out, uint16_t *port_out) |
int | get_interface_address6 (int severity, sa_family_t family, tor_addr_t *addr) |
int | is_internal_IP (uint32_t ip, int for_listening) |
int | parse_addr_port (int severity, const char *addrport, char **address, uint32_t *addr, uint16_t *port_out) |
int | addr_mask_get_bits (uint32_t mask) |
int | addr_mask_cmp_bits (uint32_t a1, uint32_t a2, maskbits_t bits) |
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 | tor_inet_ntoa (const struct in_addr *in, char *buf, size_t buf_len) |
char * | tor_dup_ip (uint32_t addr) |
int | get_interface_address (int severity, uint32_t *addr) |
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 | |||
) |
Copy a tor_addr_t from src to dest.
References tor_assert.
Referenced by bridge_add_from_config(), connection_exit_begin_conn(), connection_exit_connect_dir(), connection_handle_listener_read(), connection_or_connect(), connection_or_init_conn_from_address(), directory_initiate_command_rend(), dns_resolve_impl(), evdns_server_callback(), extend_info_alloc(), parse_port_config(), and tor_addr_port_parse().
void tor_addr_from_in6 | ( | tor_addr_t * | dest, | |
const struct in6_addr * | in6 | |||
) |
Set dest equal to the IPv6 address in the in6_addr in6.
References tor_addr_from_ipv6_bytes().
Referenced by connection_ap_get_original_destination(), tor_addr_from_sockaddr(), tor_addr_from_str(), tor_addr_lookup(), tor_addr_parse_mask_ports(), and tor_addr_parse_reverse_lookup_name().
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 | |||
) |
Set the tor_addr_t in a to contain the socket address contained in sa.
References tor_addr_from_in6(), tor_addr_from_ipv4n(), tor_addr_make_unspec(), and tor_assert.
Referenced by connection_ap_get_original_destination(), connection_handle_listener_read(), evdns_server_callback(), and get_interface_address6().
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 /
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().
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 | ) |
Return a newly allocated string with a representation of addr.
References tor_addr_to_str().
Referenced by connection_exit_begin_conn(), connection_handle_listener_read(), connection_or_init_conn_from_address(), evdns_server_callback(), launch_direct_bridge_descriptor_fetch(), and rend_encode_v2_intro_points().
char* tor_dup_ip | ( | uint32_t | addr | ) |
Given a host-order addr, call tor_inet_ntop() on it and return a strdup of the resulting address.
References tor_inet_ntop().
Referenced by directory_post_to_hs_dir(), dirserv_generate_networkstatus_vote_obj(), getinfo_helper_misc(), rep_hist_get_router_stability_doc(), router_rebuild_descriptor(), and trusted_dirs_load_certs_from_string().
int tor_inet_ntoa | ( | const struct in_addr * | in, | |
char * | buf, | |||
size_t | buf_len | |||
) |
Given an IPv4 in_addr struct *in (in network order, as usual), write it as a string into the buf_len-byte buffer in buf.
References tor_snprintf().
Referenced by addressmap_get_virtual_address(), client_dns_set_addressmap(), connection_edge_process_relay_cell_not_open(), directory_initiate_command_routerstatus_rend(), evdns_callback(), evdns_wildcard_check_callback(), fetch_from_buf_socks(), format_networkstatus_vote(), generate_v2_networkstatus_opinion(), log_addr_has_changed(), networkstatus_compute_consensus(), remap_event_helper(), resolve_my_address(), routerstatus_format_entry(), tell_controller_about_resolved_result(), and tor_inet_ntop().