#include "core/or/or.h"
#include "app/config/config.h"
#include "app/config/resolve_addr.h"
#include "core/mainloop/mainloop.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/extendinfo.h"
#include "feature/control/control_events.h"
#include "feature/dircommon/dir_connection_st.h"
#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/node_select.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerstatus_st.h"
#include "feature/relay/relay_find_addr.h"
#include "feature/relay/router.h"
#include "feature/relay/routermode.h"
void
relay_address_new_suggestion(const tor_addr_t *suggested_addr,
const tor_addr_t *peer_addr,
const char *identity_digest)
{
const or_options_t *options = get_options();
tor_assert(suggested_addr);
tor_assert(peer_addr);
if (!server_mode(options)) {
return;
}
if (!router_addr_is_trusted_dir(peer_addr) ||
(identity_digest && !router_digest_is_trusted_dir(identity_digest))) {
return;
}
if (tor_addr_is_internal(suggested_addr, 0)) {
return;
}
if (tor_addr_eq(suggested_addr, peer_addr)) {
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
"A relay endpoint %s is telling us that their address is ours.",
fmt_addr(peer_addr));
return;
}
resolved_addr_set_suggested(suggested_addr);
}
MOCK_IMPL(bool,
relay_find_addr_to_publish, (const or_options_t *options, int family,
int flags, tor_addr_t *addr_out))
{
tor_assert(options);
tor_assert(addr_out);
tor_addr_make_unspec(addr_out);
if (family == AF_INET6 && options->AddressDisableIPv6) {
return false;
}
if (!routerconf_find_or_port(options, family)) {
return false;
}
resolved_addr_get_last(family, addr_out);
if (!tor_addr_is_null(addr_out)) {
goto found;
}
if (!(flags & RELAY_FIND_ADDR_CACHE_ONLY)) {
if (find_my_address(options, family, LOG_INFO, addr_out, NULL, NULL)) {
goto found;
}
static ratelim_t rlim = RATELIM_INIT(3600);
log_fn_ratelim(&rlim, LOG_NOTICE, LD_CONFIG,
"Unable to find %s address for ORPort %u. "
"You might want to specify %sOnly to it or set an "
"explicit address or set Address.",
fmt_af_family(family),
routerconf_find_or_port(options, family),
fmt_af_family(family));
}
resolved_addr_get_suggested(family, addr_out);
if (!tor_addr_is_null(addr_out)) {
goto found;
}
return false;
found:
return true;
}
bool
relay_has_address_set(int family)
{
tor_addr_t addr;
return relay_find_addr_to_publish(get_options(), family,
RELAY_FIND_ADDR_CACHE_ONLY, &addr);
}
#define DUMMY_DOWNLOAD_INTERVAL (20*60)
void
relay_addr_learn_from_dirauth(void)
{
static time_t last_dummy_circuit = 0;
const or_options_t *options = get_options();
time_t now = time(NULL);
bool have_addr;
tor_addr_t addr_out;
if (BUG(!server_mode(options))) {
return;
}
have_addr = relay_find_addr_to_publish(options, AF_INET,
RELAY_FIND_ADDR_CACHE_ONLY,
&addr_out);
if (!have_addr && last_dummy_circuit + DUMMY_DOWNLOAD_INTERVAL < now) {
last_dummy_circuit = now;
const routerstatus_t *rs = router_pick_trusteddirserver(V3_DIRINFO, 0);
if (BUG(!rs)) {
return;
}
const node_t *node = node_get_by_id(rs->identity_digest);
if (!node) {
log_info(LD_GENERAL, "Can't build a circuit to an authority. Unable to "
"learn for now our address from them.");
return;
}
extend_info_t *ei = extend_info_from_node(node, 1);
if (BUG(!ei)) {
return;
}
log_debug(LD_GENERAL, "Attempting dummy testing circuit to an authority "
"in order to learn our address.");
circuit_launch_by_extend_info(CIRCUIT_PURPOSE_TESTING, ei,
CIRCLAUNCH_IS_INTERNAL |
CIRCLAUNCH_ONEHOP_TUNNEL);
extend_info_free(ei);
}
}