start to refactor dirserver_mode()

svn:r12621
This commit is contained in:
Roger Dingledine 2007-12-01 04:58:53 +00:00
parent 6d49465b69
commit f8df8d791e
11 changed files with 79 additions and 38 deletions

View File

@ -1132,7 +1132,8 @@ options_act(or_options_t *old_options)
if (old_options) {
if (authdir_mode_v3(options) && !authdir_mode_v3(old_options))
dirvote_recalculate_timing(options, time(NULL));
if (!bool_eq(dirserver_mode(options), dirserver_mode(old_options))) {
if (!bool_eq(directory_caches_dir_info(options),
directory_caches_dir_info(old_options))) {
/* Make sure update_router_have_min_dir_info gets called. */
router_dir_info_changed();
/* We might need to download a new consensus status later or sooner than

View File

@ -2292,7 +2292,8 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
}
} else if (rh.command == RELAY_COMMAND_BEGIN_DIR) {
or_options_t *options = get_options();
if (!dirserver_mode(options) || circ->purpose != CIRCUIT_PURPOSE_OR) {
if (!directory_permits_begindir_requests(options) ||
circ->purpose != CIRCUIT_PURPOSE_OR) {
end_payload[0] = END_STREAM_REASON_NOTDIRECTORY;
relay_send_command_from_edge(rh.stream_id, circ, RELAY_COMMAND_END,
end_payload, 1, NULL);

View File

@ -1382,7 +1382,7 @@ getinfo_helper_dir(control_connection_t *control_conn,
tor_free(url);
smartlist_free(descs);
} else if (!strcmpstart(question, "dir/status/")) {
if (dirserver_mode(get_options())) {
if (directory_permits_controller_requests(get_options())) {
size_t len=0;
char *cp;
smartlist_t *status_list = smartlist_create();

View File

@ -282,7 +282,7 @@ directory_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
{
routerstatus_t *rs = NULL;
or_options_t *options = get_options();
int prefer_authority = server_mode(options) && dirserver_mode(options);
int prefer_authority = directory_fetches_from_authorities(options);
int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose);
authority_type_t type;
int flags = retry_if_no_servers ? PDS_RETRY_IF_NO_SERVERS : 0;
@ -2982,8 +2982,7 @@ dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
{
char digest[DIGEST_LEN];
time_t now = time(NULL);
or_options_t *options = get_options();
int server = server_mode(options) && dirserver_mode(options);
int server = directory_fetches_from_authorities(get_options());
if (!was_descriptor_digests) {
if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
tor_assert(!was_extrainfo); /* not supported yet */

View File

@ -1056,6 +1056,50 @@ dirserv_dump_directory_to_string(char **dir_out,
return -1;
}
/********************************************************************/
/* A set of functions to answer questions about how we'd like to behave
* as a directory cache/client. */
/** Return 1 if we want to keep descriptors, networkstatuses, etc around
* and serve them to others, or 0 otherwise.
* Also causes us to fetch new networkstatuses, descriptors, etc on the
* "mirror" schedule rather than the "client" schedule.
*/
int
directory_caches_dir_info(or_options_t *options)
{
return options->DirPort != 0;
}
/** Return 1 if we fetch our directory material directly from the
* authorities, rather than some other cache. */
int
directory_fetches_from_authorities(or_options_t *options)
{
return server_mode(options) && options->DirPort != 0;
}
/** Return 1 if we want to allow remote people to ask us directory
* requests via the "begin_dir" interface, which doesn't require
* having any separate port open. */
int
directory_permits_begindir_requests(or_options_t *options)
{
return options->DirPort != 0;
}
/** Return 1 if we want to allow controllers to ask us directory
* requests via the controller interface, which doesn't require
* having any separate port open. */
int
directory_permits_controller_requests(or_options_t *options)
{
return options->DirPort != 0;
}
/********************************************************************/
/* Used only by non-v1-auth dirservers: The v1 directory and
* runningrouters we'll serve when requested. */
static cached_dir_t *cached_directory = NULL;

View File

@ -950,7 +950,7 @@ run_scheduled_events(time_t now)
* (if we've passed our internal checks). */
if (time_to_fetch_directory < now) {
/* Only caches actually need to fetch directories now. */
if (dirserver_mode(options) && !authdir_mode_v1(options)) {
if (directory_caches_dir_info(options) && !authdir_mode_v1(options)) {
/* XXX020 actually, we should only do this if we want to advertise
* our dirport. not simply if we configured one. -RD */
if (any_trusted_dir_is_v1_authority() &&
@ -964,7 +964,8 @@ run_scheduled_events(time_t now)
}
/* Caches need to fetch running_routers; directory clients don't. */
if (dirserver_mode(options) && time_to_fetch_running_routers < now) {
if (directory_caches_dir_info(options) &&
time_to_fetch_running_routers < now) {
if (!authdir_mode_v1(options) && !should_delay_dir_fetches(options)) {
directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST,
ROUTER_PURPOSE_GENERAL, NULL, 1);

View File

@ -483,7 +483,7 @@ add_networkstatus_to_cache(const char *s,
tor_free(fn);
}
if (dirserver_mode(get_options()))
if (directory_caches_dir_info(get_options()))
dirserv_set_cached_networkstatus_v2(s,
ns->identity_digest,
ns->published_on);
@ -528,7 +528,7 @@ router_set_networkstatus_v2(const char *s, time_t arrived_at,
char fp[HEX_DIGEST_LEN+1];
char published[ISO_TIME_LEN+1];
if (!dirserver_mode(get_options()))
if (!directory_caches_dir_info(get_options()))
return 0; /* Don't bother storing it. */
ns = networkstatus_v2_parse_from_string(s);
@ -542,10 +542,6 @@ router_set_networkstatus_v2(const char *s, time_t arrived_at,
!(trusted_dir->type & V2_AUTHORITY)) {
log_info(LD_DIR, "Network status was signed, but not by an authoritative "
"directory we recognize.");
if (!dirserver_mode(get_options())) {
networkstatus_v2_free(ns);
return 0;
}
source_desc = fp;
} else {
source_desc = trusted_dir->description;
@ -600,7 +596,7 @@ router_set_networkstatus_v2(const char *s, time_t arrived_at,
}
if (!trusted_dir) {
if (!skewed && dirserver_mode(get_options())) {
if (!skewed) {
/* We got a non-trusted networkstatus, and we're a directory cache.
* This means that we asked an authority, and it told us about another
* authority we didn't recognize. */
@ -713,7 +709,7 @@ networkstatus_v2_list_clean(time_t now)
unlink(fname);
}
tor_free(fname);
if (dirserver_mode(get_options())) {
if (directory_caches_dir_info(get_options())) {
dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
}
networkstatus_v2_free(ns);
@ -1055,7 +1051,7 @@ update_consensus_networkstatus_fetch_time(time_t now)
long dl_interval;
long interval = c->fresh_until - c->valid_after;
time_t start;
if (dirserver_mode(options)) {
if (directory_caches_dir_info(options)) {
/* We want to cache the next one at some point after this one
* is no longer fresh... */
start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
@ -1114,7 +1110,7 @@ update_networkstatus_downloads(time_t now)
or_options_t *options = get_options();
if (should_delay_dir_fetches(options))
return;
if (dirserver_mode(options))
if (directory_caches_dir_info(options))
update_v2_networkstatus_cache_downloads(now);
update_consensus_networkstatus_downloads(now);
update_certificate_downloads(now);
@ -1351,7 +1347,7 @@ networkstatus_set_current_consensus(const char *consensus, int from_cache,
write_str_to_file(consensus_fname, consensus, 0);
}
if (dirserver_mode(get_options()))
if (directory_caches_dir_info(get_options()))
dirserv_set_cached_networkstatus_v3(consensus,
current_consensus->valid_after);

View File

@ -3001,6 +3001,12 @@ int list_server_status(smartlist_t *routers, char **router_status_out,
int for_controller);
int dirserv_dump_directory_to_string(char **dir_out,
crypto_pk_env_t *private_key);
int directory_caches_dir_info(or_options_t *options);
int directory_fetches_from_authorities(or_options_t *options);
int directory_permits_begindir_requests(or_options_t *options);
int directory_permits_controller_requests(or_options_t *options);
void directory_set_dirty(void);
cached_dir_t *dirserv_get_directory(void);
cached_dir_t *dirserv_get_runningrouters(void);
@ -3628,7 +3634,6 @@ int authdir_mode_tests_reachability(or_options_t *options);
int authdir_mode_bridge(or_options_t *options);
int authdir_mode_any_nonbridge(or_options_t *options);
int clique_mode(or_options_t *options);
int dirserver_mode(or_options_t *options);
int server_mode(or_options_t *options);
int advertised_server_mode(void);
int proxy_mode(or_options_t *options);

View File

@ -884,14 +884,6 @@ server_mode(or_options_t *options)
return (options->ORPort != 0 || options->ORListenAddress);
}
/** Return true iff we are trying to be a directory server */
int
dirserver_mode(or_options_t *options)
{
// if (options->ClientOnly) return 0;
return options->DirPort != 0;
}
/** Remember if we've advertised ourselves to the dirservers. */
static int server_is_advertised=0;

View File

@ -2262,7 +2262,8 @@ extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
return r;
}
#define should_cache_old_descriptors() dirserver_mode(get_options())
#define should_cache_old_descriptors() \
directory_caches_dir_info(get_options())
/** If we're a directory cache and routerlist <b>rl</b> doesn't have
* a copy of router <b>ri</b> yet, add it to the list of old (not
@ -2835,7 +2836,7 @@ routerlist_remove_old_cached_routers_with_id(time_t now,
/* Check whether we need to do anything at all. */
{
int mdpr = dirserver_mode(get_options()) ? 5 : 2;
int mdpr = directory_caches_dir_info(get_options()) ? 5 : 2;
if (n <= mdpr)
return;
n_extra = n - mdpr;
@ -2908,7 +2909,7 @@ routerlist_remove_old_routers(void)
routerinfo_t *router;
signed_descriptor_t *sd;
digestmap_t *retain;
int dirserv = dirserver_mode(get_options());
int dirserv = directory_caches_dir_info(get_options());
const networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
@ -2999,7 +3000,7 @@ routerlist_remove_old_routers(void)
* total number doesn't approach max_descriptors_per_router()*len(router).
*/
if (smartlist_len(routerlist->old_routers) <
smartlist_len(routerlist->routers) * (dirserver_mode(get_options())?4:2))
smartlist_len(routerlist->routers) * (dirserv?4:2))
goto done;
smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
@ -3223,7 +3224,7 @@ signed_desc_digest_is_recognized(signed_descriptor_t *desc)
{
routerstatus_t *rs;
networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
int dirserv = dirserver_mode(get_options());
int dirserv = directory_caches_dir_info(get_options());
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
if (consensus) {
@ -3559,7 +3560,7 @@ launch_router_descriptor_downloads(smartlist_t *downloadable, time_t now)
or_options_t *options = get_options();
n_downloadable = smartlist_len(downloadable);
if (!dirserver_mode(options)) {
if (!directory_caches_dir_info(options)) {
if (n_downloadable >= MAX_DL_TO_DELAY) {
log_debug(LD_DIR,
"There are enough downloadable routerdescs to launch requests.");
@ -3623,7 +3624,7 @@ update_router_descriptor_cache_downloads(time_t now)
or_options_t *options = get_options();
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
if (! dirserver_mode(options)) {
if (! directory_caches_dir_info(options)) {
log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads() "
"on a non-dir-mirror?");
}
@ -3762,7 +3763,7 @@ update_consensus_router_descriptor_downloads(time_t now)
smartlist_t *no_longer_old = smartlist_create();
smartlist_t *downloadable = smartlist_create();
int authdir = authdir_mode(options);
int dirserver = dirserver_mode(options);
int dirserver = directory_caches_dir_info(options);
networkstatus_vote_t *consensus =
networkstatus_get_reasonably_live_consensus(now);
int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
@ -3858,7 +3859,7 @@ update_router_descriptor_downloads(time_t now)
or_options_t *options = get_options();
if (should_delay_dir_fetches(options))
return;
if (dirserver_mode(options)) {
if (directory_caches_dir_info(options)) {
update_router_descriptor_cache_downloads(now);
}
update_consensus_router_descriptor_downloads(now);

View File

@ -686,7 +686,8 @@ router_parse_directory(const char *str)
/* Now that we know the signature is okay, and we have a
* publication time, cache the directory. */
if (dirserver_mode(get_options()) && !authdir_mode_v1(get_options()))
if (directory_caches_dir_info(get_options()) &&
!authdir_mode_v1(get_options()))
dirserv_set_cached_directory(str, published_on, 0);
r = 0;