Tor  0.4.7.0-alpha-dev
Macros | Typedefs | Enumerations | Functions | Variables
resolve_addr.c File Reference

Implement resolving address functions. More...

#include "app/config/config.h"
#include "app/config/resolve_addr.h"
#include "core/mainloop/mainloop.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/authmode.h"
#include "lib/encoding/confline.h"
#include "lib/net/gethostname.h"
#include "lib/net/resolve.h"

Go to the source code of this file.

Macros

#define RESOLVE_ADDR_PRIVATE
 
#define MAX_CONFIG_ADDRESS   2
 
#define IDX_NULL   0 /* Index to zeroed address object. */
 
#define IDX_IPV4   1 /* Index to AF_INET. */
 
#define IDX_IPV6   2 /* Index to AF_INET6. */
 
#define IDX_SIZE   3 /* How many indexes do we have. */
 
#define ERR_DEFAULT_DIRAUTH   -1 /* Using default authorities. */
 
#define ERR_ADDRESS_IS_INTERNAL   -2 /* IP is internal. */
 

Typedefs

typedef fn_address_ret_t(* fn_address_t) (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
 

Enumerations

enum  fn_address_ret_t { FN_RET_OK = 0 , FN_RET_BAIL = 1 , FN_RET_NEXT = 2 }
 

Functions

 CTASSERT (ARRAY_LENGTH(last_resolved_addrs)==IDX_SIZE)
 
 CTASSERT (ARRAY_LENGTH(last_suggested_addrs)==IDX_SIZE)
 
 CTASSERT (ARRAY_LENGTH(last_addrs_configured)==IDX_SIZE)
 
static int af_to_idx (const int family)
 
const char * resolved_addr_method_to_str (const resolved_addr_method_t method)
 
bool resolved_addr_is_configured (int family)
 
void resolved_addr_get_suggested (int family, tor_addr_t *addr_out)
 
void resolved_addr_set_suggested (const tor_addr_t *addr)
 
void resolved_addr_get_last (int family, tor_addr_t *addr_out)
 
void resolved_addr_reset_last (int family)
 
static int address_can_be_used (const tor_addr_t *addr, const or_options_t *options, int warn_severity, const bool explicit_ip)
 Return true iff the given IP address can be used as a valid external resolved address. More...
 
static fn_address_ret_t get_address_from_config (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
 Get IP address from the given config line and for a specific address family. More...
 
static fn_address_ret_t get_address_from_hostname (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
 Get IP address from the local hostname by calling gethostbyname() and doing a DNS resolution on the hostname. More...
 
static fn_address_ret_t get_address_from_interface (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
 Get IP address from a network interface. More...
 
static fn_address_ret_t get_address_from_orport (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
 Get IP address from the ORPort (if any). More...
 
void resolved_addr_set_last (const tor_addr_t *addr, const resolved_addr_method_t method_used, const char *hostname_used)
 Set the last resolved address cache using the given address. More...
 
bool find_my_address (const or_options_t *options, int family, int warn_severity, tor_addr_t *addr_out, resolved_addr_method_t *method_out, char **hostname_out)
 Attempt to find our IP address that can be used as our external reachable address. More...
 
bool is_local_to_resolve_addr (const tor_addr_t *addr)
 : Return true iff the given addr is judged to be local to our resolved address. More...
 

Variables

static tor_addr_t last_resolved_addrs []
 
static tor_addr_t last_suggested_addrs []
 
static bool last_addrs_configured [] = { false, false, false }
 
static const fn_address_t fn_address_table []
 
static const size_t fn_address_table_len
 
static const fn_address_t fn_address_table_auth []
 
static const size_t fn_address_table_auth_len
 

Detailed Description

Implement resolving address functions.

Definition in file resolve_addr.c.

Macro Definition Documentation

◆ ERR_DEFAULT_DIRAUTH

#define ERR_DEFAULT_DIRAUTH   -1 /* Using default authorities. */

Errors returned by address_can_be_used() in order for the caller to know why the address is denied or not.

Definition at line 166 of file resolve_addr.c.

◆ IDX_NULL

#define IDX_NULL   0 /* Index to zeroed address object. */

Ease our life. Arrays containing state per address family. These are to add semantic to the code so we know what is accessed.

Definition at line 28 of file resolve_addr.c.

◆ MAX_CONFIG_ADDRESS

#define MAX_CONFIG_ADDRESS   2

Maximum "Address" statement allowed in our configuration.

Definition at line 24 of file resolve_addr.c.

Typedef Documentation

◆ fn_address_t

typedef fn_address_ret_t(* fn_address_t) (const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)

Ease our lives. Typedef to the address discovery function signature.

Definition at line 627 of file resolve_addr.c.

Enumeration Type Documentation

◆ fn_address_ret_t

Function in our address function table return one of these code.

Definition at line 34 of file resolve_addr.c.

Function Documentation

◆ address_can_be_used()

static int address_can_be_used ( const tor_addr_t addr,
const or_options_t options,
int  warn_severity,
const bool  explicit_ip 
)
static

Return true iff the given IP address can be used as a valid external resolved address.

Two tests are done in this function: 1) If the address if NOT internal, it can be used. 2) If the address is internal and we have custom directory authorities configured then it can they be used. Important for testing networks.

Parameters
addrThe IP address to validate.
optionsGlobal configuration options.
warn_severityLog level that should be used on error.
explicit_ipWas the IP address explicitly given.
Returns
Return 0 if it can be used. Return error code ERR_* found at the top of the file.

Definition at line 186 of file resolve_addr.c.

◆ find_my_address()

bool find_my_address ( const or_options_t options,
int  family,
int  warn_severity,
tor_addr_t addr_out,
resolved_addr_method_t method_out,
char **  hostname_out 
)

Attempt to find our IP address that can be used as our external reachable address.

The following describe the algorithm to find an address. Each have specific conditions so read carefully.

On success, true is returned and depending on how the address was found, the out parameters can have different values.

On error, false is returned and out parameters are set to NULL.

  1. Look at the configuration Address option.

    If Address is a public address, True is returned and addr_out is set with it, the method_out is set to RESOLVED_ADDR_CONFIGURED and hostname_out is set to NULL.

    If Address is an internal address but NO custom authorities are used, an error is returned.

    If Address is a hostname, that is it can't be converted to an address, it is resolved. On success, addr_out is set with the address, method_out is set to RESOLVED_ADDR_RESOLVED and hostname_out is set to the resolved hostname. On failure to resolve, an error is returned.

    If no given Address, fallback to the network interface (see section 2).

  2. Look at the network interface.

    Attempt to find the first public usable address from the list of network interfaces returned by the OS.

    On failure, we attempt to look at the local hostname (3).

    On success, addr_out is set with it, method_out is set to RESOLVED_ADDR_INTERFACE and hostname_out is set to NULL.

  3. Look at the local hostname.

    If the local hostname resolves to a non internal address, addr_out is set with it, method_out is set to RESOLVED_ADDR_GETHOSTNAME and hostname_out is set to the resolved hostname.

    If a local hostname can NOT be found, an error is returned.

    If the local hostname resolves to an internal address, an error is returned.

    If the local hostname can NOT be resolved, an error is returned.

Parameters
optionsGlobal configuration options.
familyIP address family. Only AF_INET and AF_INET6 are supported.
warn_severityLogging level.
addr_outOUT: Set with the IP address found if any.
method_outOUT: (optional) Method denoting how the address wa found. This is described in the control-spec.txt as actions for "STATUS_SERVER".
hostname_outOUT: String containing the hostname if any was used. Only be set for RESOLVED and GETHOSTNAME methods. Else it is set to NULL.
Returns
True if the address was found for the given family. False if not or on errors.

Definition at line 727 of file resolve_addr.c.

◆ get_address_from_config()

static fn_address_ret_t get_address_from_config ( const or_options_t options,
int  warn_severity,
int  family,
resolved_addr_method_t method_out,
char **  hostname_out,
tor_addr_t addr_out 
)
static

Get IP address from the given config line and for a specific address family.

This can fail is more than two Address statement are found for the same address family. It also fails if no statement is found.

Parameters
optionsGlobal configuration options.
warn_severityLog level that should be used on error.
familyIP address family. Only AF_INET and AF_INET6 are supported.
method_outOUT: Method denoting how the address was found. This is described in the control-spec.txt as actions for "STATUS_SERVER".
hostname_outOUT: String containing the hostname gotten from the Address value if any.
addr_outOUT: Tor address of the address found in the cline or resolved from the cline.
Returns
Return 0 on success that is an address has been found or resolved successfully. Return error code ERR_* found at the top of the file.

Definition at line 253 of file resolve_addr.c.

◆ get_address_from_hostname()

static fn_address_ret_t get_address_from_hostname ( const or_options_t options,
int  warn_severity,
int  family,
resolved_addr_method_t method_out,
char **  hostname_out,
tor_addr_t addr_out 
)
static

Get IP address from the local hostname by calling gethostbyname() and doing a DNS resolution on the hostname.

Parameters
optionsGlobal configuration options.
warn_severityLog level that should be used on error.
familyIP address family. Only AF_INET and AF_INET6 are supported.
method_outOUT: Method denoting how the address was found. This is described in the control-spec.txt as actions for "STATUS_SERVER".
hostname_outOUT: String containing the local hostname.
addr_outOUT: Tor address resolved from the local hostname.
Returns
Return 0 on success that is an address has been found and resolved successfully. Return error code ERR_* found at the top of the file.

Definition at line 384 of file resolve_addr.c.

◆ get_address_from_interface()

static fn_address_ret_t get_address_from_interface ( const or_options_t options,
int  warn_severity,
int  family,
resolved_addr_method_t method_out,
char **  hostname_out,
tor_addr_t addr_out 
)
static

Get IP address from a network interface.

Parameters
optionsGlobal configuration options.
warn_severityLog level that should be used on error.
familyIP address family. Only AF_INET and AF_INET6 are supported.
method_outOUT: Always RESOLVED_ADDR_INTERFACE on success which is detailed in the control-spec.txt as actions for "STATUS_SERVER".
hostname_outOUT: String containing the local hostname. For this function, it is always set to NULL.
addr_outOUT: Tor address found attached to the interface.
Returns
Return 0 on success that is an address has been found. Return error code ERR_* found at the top of the file.

Definition at line 447 of file resolve_addr.c.

◆ get_address_from_orport()

static fn_address_ret_t get_address_from_orport ( const or_options_t options,
int  warn_severity,
int  family,
resolved_addr_method_t method_out,
char **  hostname_out,
tor_addr_t addr_out 
)
static

Get IP address from the ORPort (if any).

Parameters
optionsGlobal configuration options.
warn_severityLog level that should be used on error.
familyIP address family. Only AF_INET and AF_INET6 are supported.
method_outOUT: Always RESOLVED_ADDR_CONFIGURED_ORPORT on success which is detailed in the control-spec.txt as actions for "STATUS_SERVER".
hostname_outOUT: String containing the ORPort hostname if any.
addr_outOUT: Tor address found if any.
Returns
Return 0 on success that is an address has been found. Return error code ERR_* found at the top of the file.

Definition at line 498 of file resolve_addr.c.

◆ is_local_to_resolve_addr()

bool is_local_to_resolve_addr ( const tor_addr_t addr)

: Return true iff the given addr is judged to be local to our resolved address.

This function is used to tell whether another address is 'remote' enough that we can trust it when it tells us that we are reachable, or that we have a certain address.

The criterion to learn if the address is local are the following:

  1. Internal address.
  2. If EnforceDistinctSubnets is set then it is never local.
  3. Network mask is compared. IPv4: /24 and IPv6 /48. This is different from the path selection that looks at /16 and /32 because we only want to learn here if the address is considered to come from the Internet basically.
Parameters
addrThe address to test if local and also test against our resovled address.
Returns
True iff address is considered local or else False.

Definition at line 819 of file resolve_addr.c.

◆ resolved_addr_get_last()

void resolved_addr_get_last ( int  family,
tor_addr_t addr_out 
)

Copy the last resolved address of family into addr_out.

If not last resolved address existed, the addr_out is a null address (use tor_addr_is_null()).

Definition at line 150 of file resolve_addr.c.

Referenced by relay_find_addr_to_publish().

◆ resolved_addr_get_suggested()

void resolved_addr_get_suggested ( int  family,
tor_addr_t addr_out 
)

Copy the last suggested address of family into addr_out.

If no last suggested address exists, the addr_out is a null address (use tor_addr_is_null() to confirm).

Definition at line 119 of file resolve_addr.c.

◆ resolved_addr_is_configured()

bool resolved_addr_is_configured ( int  family)

Return true if the last address of family was configured or not. An address is considered configured if it was found in the Address or ORPort statement.

This applies to the address returned by the function resolved_addr_get_last() which is the cache of discovered addresses.

Definition at line 109 of file resolve_addr.c.

Referenced by reachability_warnings_callback().

◆ resolved_addr_method_to_str()

const char* resolved_addr_method_to_str ( const resolved_addr_method_t  method)

Return string representation of the given method.

Definition at line 81 of file resolve_addr.c.

◆ resolved_addr_reset_last()

void resolved_addr_reset_last ( int  family)

Reset the last resolved address of family.

This makes it null address.

Definition at line 159 of file resolve_addr.c.

◆ resolved_addr_set_last()

void resolved_addr_set_last ( const tor_addr_t addr,
const resolved_addr_method_t  method_used,
const char *  hostname_used 
)

Set the last resolved address cache using the given address.

A log notice is emitted if the given address has changed from before. Not emitted on first resolve.

Control port event "STATUS_SERVER" is emitted with the new information if it has changed.

Finally, tor is notified that the IP address has changed.

Parameters
addrIP address to update the cache with.
method_usedBy which method did we resolved it (for logging and control port).
hostname_usedWhich hostname was used. If none were used, it is NULL. (for logging and control port).

Have we done a first resolve. This is used to control logging.

Definition at line 561 of file resolve_addr.c.

◆ resolved_addr_set_suggested()

void resolved_addr_set_suggested ( const tor_addr_t addr)

Set the last suggested address into our cache. This is called when we get a new NETINFO cell from a trusted source.

Definition at line 127 of file resolve_addr.c.

Variable Documentation

◆ fn_address_table

const fn_address_t fn_address_table[]
static
Initial value:
=
{
}
static fn_address_ret_t get_address_from_hostname(const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
Get IP address from the local hostname by calling gethostbyname() and doing a DNS resolution on the h...
Definition: resolve_addr.c:384
static fn_address_ret_t get_address_from_config(const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
Get IP address from the given config line and for a specific address family.
Definition: resolve_addr.c:253
static fn_address_ret_t get_address_from_interface(const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
Get IP address from a network interface.
Definition: resolve_addr.c:447
static fn_address_ret_t get_address_from_orport(const or_options_t *options, int warn_severity, int family, resolved_addr_method_t *method_out, char **hostname_out, tor_addr_t *addr_out)
Get IP address from the ORPort (if any).
Definition: resolve_addr.c:498

Address discovery function table. The order matters as in the first one is executed first and so on.

Definition at line 635 of file resolve_addr.c.

◆ fn_address_table_auth

const fn_address_t fn_address_table_auth[]
static
Initial value:

Definition at line 652 of file resolve_addr.c.

◆ fn_address_table_auth_len

const size_t fn_address_table_auth_len
static
Initial value:
=
ARRAY_LENGTH(fn_address_table_auth)
#define ARRAY_LENGTH(x)

Length of address table as in how many functions.

Definition at line 659 of file resolve_addr.c.

◆ fn_address_table_len

const size_t fn_address_table_len
static
Initial value:
=
static const fn_address_t fn_address_table[]
Definition: resolve_addr.c:635

Length of address table as in how many functions.

Definition at line 644 of file resolve_addr.c.

◆ last_addrs_configured

bool last_addrs_configured[] = { false, false, false }
static

True iff the address was found to be configured that is from the configuration file either using Address or ORPort.

Definition at line 60 of file resolve_addr.c.

Referenced by resolved_addr_is_configured().

◆ last_resolved_addrs

tor_addr_t last_resolved_addrs[]
static
Initial value:
=
{ TOR_ADDR_NULL, TOR_ADDR_NULL, TOR_ADDR_NULL }

Last resolved addresses.

Definition at line 46 of file resolve_addr.c.

Referenced by is_local_to_resolve_addr(), resolved_addr_get_last(), and resolved_addr_reset_last().

◆ last_suggested_addrs

tor_addr_t last_suggested_addrs[]
static
Initial value:
=
{ TOR_ADDR_NULL, TOR_ADDR_NULL, TOR_ADDR_NULL }

Last suggested addresses.

These addresses come from a NETINFO cell from a trusted relay (currently only authorities). We only use those in last resort.

Definition at line 54 of file resolve_addr.c.

Referenced by resolved_addr_get_suggested().