9 #define RESOLVE_ADDR_PRIVATE
24 #define MAX_CONFIG_ADDRESS 2
47 { TOR_ADDR_NULL, TOR_ADDR_NULL, TOR_ADDR_NULL };
55 { TOR_ADDR_NULL, TOR_ADDR_NULL, TOR_ADDR_NULL };
64 af_to_idx(
const int family)
84 case RESOLVED_ADDR_NONE:
86 case RESOLVED_ADDR_CONFIGURED:
88 case RESOLVED_ADDR_CONFIGURED_ORPORT:
89 return "CONFIGURED_ORPORT";
90 case RESOLVED_ADDR_GETHOSTNAME:
92 case RESOLVED_ADDR_INTERFACE:
94 case RESOLVED_ADDR_RESOLVED:
139 log_notice(
LD_CONFIG,
"External address seen and suggested by a "
140 "directory authority: %s",
fmt_addr(addr));
166 #define ERR_DEFAULT_DIRAUTH -1
167 #define ERR_ADDRESS_IS_INTERNAL -2
187 int warn_severity,
const bool explicit_ip)
192 if (!tor_addr_is_internal(addr, 0)) {
210 if (using_default_dir_authorities(options)) {
212 "Address '%s' is a private IP address. Tor relays that use "
213 "the default DirAuthorities must have public IP addresses.",
222 "Address %s was resolved and thus not explicitly "
223 "set. Even if DirAuthorities are custom, this is "
225 return ERR_ADDRESS_IS_INTERNAL;
258 bool explicit_ip =
false, resolve_failure =
false;
259 int num_valid_addr = 0;
267 *hostname_out = NULL;
268 *method_out = RESOLVED_ADDR_NONE;
270 log_debug(
LD_CONFIG,
"Attempting to get address from configuration");
273 log_info(
LD_CONFIG,
"No Address option found in configuration.");
286 *method_out = RESOLVED_ADDR_CONFIGURED;
290 }
else if (af != -1) {
300 *method_out = RESOLVED_ADDR_RESOLVED;
304 *hostname_out = tor_strdup(cfg->value);
310 resolve_failure =
true;
312 "Could not resolve local Address '%s'. Failing.", cfg->value);
317 if (!num_valid_addr) {
318 if (resolve_failure) {
324 "No Address option found for family %s in configuration.",
333 "Found %d Address statement of address family %s. "
334 "Only one is allowed.", num_valid_addr,
fmt_af_family(family));
346 if (ret == ERR_ADDRESS_IS_INTERNAL) {
347 static bool logged_once =
false;
349 log_warn(
LD_CONFIG,
"Address set with an internal address. Tor will "
350 "not work unless custom directory authorities "
351 "are defined (AlternateDirAuthority). It is also "
352 "possible to use an internal address if "
353 "PublishServerDescriptor is set to 0 and "
354 "AssumeReachable(IPv6) to 1.");
363 log_info(
LD_CONFIG,
"Address found in configuration: %s",
395 *hostname_out = NULL;
396 *method_out = RESOLVED_ADDR_NONE;
398 log_debug(
LD_CONFIG,
"Attempting to get address from local hostname");
401 log_fn(warn_severity,
LD_NET,
"Error obtaining local hostname");
407 "Could not resolve local hostname '%s'. Failing.", hostname);
416 }
else if (ret == ERR_ADDRESS_IS_INTERNAL) {
422 *method_out = RESOLVED_ADDR_GETHOSTNAME;
423 *hostname_out = tor_strdup(hostname);
426 log_info(
LD_CONFIG,
"Address found from local hostname: %s",
458 *method_out = RESOLVED_ADDR_NONE;
459 *hostname_out = NULL;
461 log_debug(
LD_CONFIG,
"Attempting to get address from network interface");
465 "Could not get local interface IP address.");
476 *method_out = RESOLVED_ADDR_INTERFACE;
510 *method_out = RESOLVED_ADDR_NONE;
511 *hostname_out = NULL;
513 log_debug(
LD_CONFIG,
"Attempting to get address from ORPort");
515 if (!options->ORPort_set) {
516 log_info(
LD_CONFIG,
"No ORPort found in configuration.");
522 addr = get_orport_addr(family);
536 *method_out = RESOLVED_ADDR_CONFIGURED_ORPORT;
563 const char *hostname_used)
566 static bool have_resolved_once[] = {
false,
false,
false };
569 bool *done_one_resolve;
570 bool have_hostname =
false;
576 have_hostname = (hostname_used != NULL);
585 done_one_resolve = &have_resolved_once[idx];
594 if (*done_one_resolve) {
598 "Your IP address seems to have changed to %s "
599 "(METHOD=%s%s%s). Updating.",
602 have_hostname ?
" HOSTNAME=" :
"",
603 have_hostname ? hostname_used :
"");
609 "EXTERNAL_ADDRESS ADDRESS=%s METHOD=%s%s%s",
612 have_hostname ?
" HOSTNAME=" :
"",
613 have_hostname ? hostname_used :
"");
616 *done_one_resolve =
true;
620 if (method_used == RESOLVED_ADDR_CONFIGURED ||
621 method_used == RESOLVED_ADDR_CONFIGURED_ORPORT) {
629 const or_options_t *options,
int warn_severity,
int family,
732 char *hostname_used = NULL;
742 if (method_out) *method_out = RESOLVED_ADDR_NONE;
743 if (hostname_out) *hostname_out = NULL;
754 table = fn_address_table_auth;
763 for (
size_t idx = 0; idx < table_len; idx++) {
765 &method_used, &hostname_used, &my_addr);
766 if (ret == FN_RET_BAIL) {
768 }
else if (ret == FN_RET_OK) {
785 *method_out = method_used;
788 *hostname_out = hostname_used;
826 if (tor_addr_is_internal(addr, 0)) {
859 #ifdef TOR_UNIT_TESTS
862 resolve_addr_reset_suggested(
int family)
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
void tor_addr_make_unspec(tor_addr_t *a)
const char * fmt_af_family(sa_family_t family)
int tor_addr_parse(tor_addr_t *addr, const char *src)
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2, maskbits_t mbits, tor_addr_comparison_t how)
int tor_addr_is_null(const tor_addr_t *addr)
int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr)
static sa_family_t tor_addr_family(const tor_addr_t *a)
#define tor_addr_eq(a, b)
int authdir_mode(const or_options_t *options)
Header file for directory authority mode.
const or_options_t * get_options(void)
Header file for config.c.
int control_event_server_status(int severity, const char *format,...)
Header file for control_events.c.
int tor_gethostname(char *name, size_t namelen)
Header for gethostname.c.
#define log_fn(severity, domain, args,...)
void ip_address_changed(int on_client_conn)
Header file for mainloop.c.
int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
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...
static const fn_address_t fn_address_table[]
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.
const char * resolved_addr_method_to_str(const resolved_addr_method_t method)
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.
void resolved_addr_set_suggested(const tor_addr_t *addr)
static tor_addr_t last_resolved_addrs[]
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)
#define ERR_DEFAULT_DIRAUTH
void resolved_addr_reset_last(int family)
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.
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.
static const size_t fn_address_table_auth_len
static const size_t fn_address_table_len
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.
#define MAX_CONFIG_ADDRESS
void resolved_addr_get_suggested(int family, tor_addr_t *addr_out)
bool resolved_addr_is_configured(int family)
static tor_addr_t last_suggested_addrs[]
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).
static bool last_addrs_configured[]
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.
void resolved_addr_get_last(int family, tor_addr_t *addr_out)
Header file for resolve_addr.c.
dirinfo_type_t PublishServerDescriptor_
struct config_line_t * Address
#define MOCK_IMPL(rv, funcname, arglist)
#define tor_assert_nonfatal_unreached()