Tor
0.4.7.0-alpha-dev
|
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 |
Implement resolving address functions.
Definition in file resolve_addr.c.
#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.
#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.
#define MAX_CONFIG_ADDRESS 2 |
Maximum "Address" statement allowed in our configuration.
Definition at line 24 of file resolve_addr.c.
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.
enum fn_address_ret_t |
Function in our address function table return one of these code.
Definition at line 34 of file resolve_addr.c.
|
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.
addr | The IP address to validate. |
options | Global configuration options. |
warn_severity | Log level that should be used on error. |
explicit_ip | Was the IP address explicitly given. |
Definition at line 186 of file resolve_addr.c.
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.
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).
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.
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.
options | Global configuration options. |
family | IP address family. Only AF_INET and AF_INET6 are supported. |
warn_severity | Logging level. |
addr_out | OUT: Set with the IP address found if any. |
method_out | OUT: (optional) Method denoting how the address wa found. This is described in the control-spec.txt as actions for "STATUS_SERVER". |
hostname_out | OUT: String containing the hostname if any was used. Only be set for RESOLVED and GETHOSTNAME methods. Else it is set to NULL. |
Definition at line 727 of file resolve_addr.c.
|
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.
options | Global configuration options. |
warn_severity | Log level that should be used on error. |
family | IP address family. Only AF_INET and AF_INET6 are supported. |
method_out | OUT: Method denoting how the address was found. This is described in the control-spec.txt as actions for "STATUS_SERVER". |
hostname_out | OUT: String containing the hostname gotten from the Address value if any. |
addr_out | OUT: Tor address of the address found in the cline or resolved from the cline. |
Definition at line 253 of file resolve_addr.c.
|
static |
Get IP address from the local hostname by calling gethostbyname() and doing a DNS resolution on the hostname.
options | Global configuration options. |
warn_severity | Log level that should be used on error. |
family | IP address family. Only AF_INET and AF_INET6 are supported. |
method_out | OUT: Method denoting how the address was found. This is described in the control-spec.txt as actions for "STATUS_SERVER". |
hostname_out | OUT: String containing the local hostname. |
addr_out | OUT: Tor address resolved from the local hostname. |
Definition at line 384 of file resolve_addr.c.
|
static |
Get IP address from a network interface.
options | Global configuration options. |
warn_severity | Log level that should be used on error. |
family | IP address family. Only AF_INET and AF_INET6 are supported. |
method_out | OUT: Always RESOLVED_ADDR_INTERFACE on success which is detailed in the control-spec.txt as actions for "STATUS_SERVER". |
hostname_out | OUT: String containing the local hostname. For this function, it is always set to NULL. |
addr_out | OUT: Tor address found attached to the interface. |
Definition at line 447 of file resolve_addr.c.
|
static |
Get IP address from the ORPort (if any).
options | Global configuration options. |
warn_severity | Log level that should be used on error. |
family | IP address family. Only AF_INET and AF_INET6 are supported. |
method_out | OUT: Always RESOLVED_ADDR_CONFIGURED_ORPORT on success which is detailed in the control-spec.txt as actions for "STATUS_SERVER". |
hostname_out | OUT: String containing the ORPort hostname if any. |
addr_out | OUT: Tor address found if any. |
Definition at line 498 of file resolve_addr.c.
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:
addr | The address to test if local and also test against our resovled address. |
Definition at line 819 of file resolve_addr.c.
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().
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.
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().
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.
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.
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.
addr | IP address to update the cache with. |
method_used | By which method did we resolved it (for logging and control port). |
hostname_used | Which 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.
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.
|
static |
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.
|
static |
Definition at line 652 of file resolve_addr.c.
|
static |
Length of address table as in how many functions.
Definition at line 659 of file resolve_addr.c.
|
static |
Length of address table as in how many functions.
Definition at line 644 of file resolve_addr.c.
|
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().
|
static |
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().
|
static |
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().