From f8df8d791e4a58ab65d8903a0522b4cfa55cc163 Mon Sep 17 00:00:00 2001 From: Roger Dingledine Date: Sat, 1 Dec 2007 04:58:53 +0000 Subject: [PATCH] start to refactor dirserver_mode() svn:r12621 --- src/or/config.c | 3 ++- src/or/connection_edge.c | 3 ++- src/or/control.c | 2 +- src/or/directory.c | 5 ++--- src/or/dirserv.c | 44 ++++++++++++++++++++++++++++++++++++++++ src/or/main.c | 5 +++-- src/or/networkstatus.c | 18 +++++++--------- src/or/or.h | 7 ++++++- src/or/router.c | 8 -------- src/or/routerlist.c | 19 +++++++++-------- src/or/routerparse.c | 3 ++- 11 files changed, 79 insertions(+), 38 deletions(-) diff --git a/src/or/config.c b/src/or/config.c index 91f7862c44..a119644b98 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -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 diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 0ee2e16885..dd218f5dd4 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -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); diff --git a/src/or/control.c b/src/or/control.c index 76b53e8ba7..5d0eda1a24 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -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(); diff --git a/src/or/directory.c b/src/or/directory.c index b0dd6a5a75..802cfedd0a 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -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 */ diff --git a/src/or/dirserv.c b/src/or/dirserv.c index bc76d27212..3b4c8e2382 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -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; diff --git a/src/or/main.c b/src/or/main.c index 9d88b76217..7582192f5f 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -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); diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c index 4fd72dff20..46b8e43ddf 100644 --- a/src/or/networkstatus.c +++ b/src/or/networkstatus.c @@ -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); diff --git a/src/or/or.h b/src/or/or.h index f8c8e1eb71..261c582d71 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -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); diff --git a/src/or/router.c b/src/or/router.c index 2cf762f650..e9cc3901e7 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -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; diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 8635674a67..7239a455c4 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -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 rl doesn't have * a copy of router ri 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); diff --git a/src/or/routerparse.c b/src/or/routerparse.c index 84d0c24e4d..28e44849fb 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -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;