From 91bafc476ef1e5a6e51c61d4a2b1c206af628ffc Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 6 Jan 2005 20:11:52 +0000 Subject: [PATCH] Fix a fun bug: do not rewrite a cached directory back to the cache; otherwise we will think it is recent and not fetch a newer one. svn:r3319 --- src/or/directory.c | 4 ++-- src/or/dirserv.c | 3 ++- src/or/or.h | 8 +++++--- src/or/routerlist.c | 13 +++++++++---- src/or/routerparse.c | 11 +++++++---- src/or/test.c | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/or/directory.c b/src/or/directory.c index 66b09eb0d8..d07f04532e 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -614,7 +614,7 @@ connection_dir_client_reached_eof(connection_t *conn) tor_free(body); tor_free(headers); return -1; } - if (router_load_routerlist_from_directory(body, NULL, 1) < 0) { + if (router_load_routerlist_from_directory(body, NULL, 1, 0) < 0) { log_fn(LOG_WARN,"I failed to parse the directory I fetched from %s:%d. Ignoring.", conn->address, conn->port); } else { log_fn(LOG_INFO,"updated routers."); @@ -633,7 +633,7 @@ connection_dir_client_reached_eof(connection_t *conn) tor_free(body); tor_free(headers); return -1; } - if (!(rrs = router_parse_runningrouters(body))) { + if (!(rrs = router_parse_runningrouters(body, 1))) { log_fn(LOG_WARN, "Can't parse runningrouters list (server '%s')", conn->address); tor_free(body); tor_free(headers); return -1; diff --git a/src/or/dirserv.c b/src/or/dirserv.c index 7462e1ee7c..2f04cc6a6e 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -820,7 +820,8 @@ static int dirserv_regenerate_directory(void) * necessary, but safe is better than sorry. */ new_directory = tor_strdup(the_directory); /* use a new copy of the dir, since get_dir_from_string scribbles on it */ - if (router_load_routerlist_from_directory(new_directory, get_identity_key(), 1)) { + if (router_load_routerlist_from_directory(new_directory, + get_identity_key(), 1, 0)) { log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying."); tor_cleanup(); exit(0); diff --git a/src/or/or.h b/src/or/or.h index 7e842f1b15..f09f9ec1fb 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -1611,7 +1611,7 @@ routerinfo_t *routerinfo_copy(const routerinfo_t *router); void router_mark_as_down(const char *digest); void routerlist_remove_old_routers(int age); int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey, - int check_version); + int dir_is_recent, int dir_is_cached); int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port, addr_policy_t *policy); #define ADDR_POLICY_ACCEPTED 0 @@ -1696,8 +1696,10 @@ int router_parse_list_from_string(const char **s, int router_parse_routerlist_from_directory(const char *s, routerlist_t **dest, crypto_pk_env_t *pkey, - int check_version); -running_routers_t *router_parse_runningrouters(const char *str); + int check_version, + int write_to_cache); +running_routers_t *router_parse_runningrouters(const char *str, + int write_to_cache); routerinfo_t *router_parse_entry_from_string(const char *s, const char *end); int router_add_exit_policy_from_string(routerinfo_t *router, const char *s); addr_policy_t *router_parse_addr_policy_from_string(const char *s); diff --git a/src/or/routerlist.c b/src/or/routerlist.c index cef59634b0..c25eeaab03 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -60,7 +60,7 @@ int router_reload_router_list(void) stat(filename, &st); /* if s is true, stat probably worked */ log_fn(LOG_INFO, "Loading cached directory from %s", filename); is_recent = st.st_mtime > time(NULL) - 60*15; - if (router_load_routerlist_from_directory(s, NULL, is_recent) < 0) { + if (router_load_routerlist_from_directory(s, NULL, is_recent, 1) < 0) { log_fn(LOG_WARN, "Cached directory at '%s' was unparseable; ignoring.", filename); } if (routerlist && @@ -846,14 +846,19 @@ routerlist_remove_old_routers(int age) /** Add to the current routerlist each router stored in the * signed directory s. If pkey is provided, check the signature against - * pkey; else check against the pkey of the signing directory server. */ + * pkey; else check against the pkey of the signing directory server. + * + * DOCDOC dir_is_recent/cached + */ int router_load_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey, - int dir_is_recent) + int dir_is_recent, + int dir_is_cached) { routerlist_t *new_list = NULL; if (router_parse_routerlist_from_directory(s, &new_list, pkey, - dir_is_recent)) { + dir_is_recent, + !dir_is_cached)) { log_fn(LOG_WARN, "Couldn't parse directory."); return -1; } diff --git a/src/or/routerparse.c b/src/or/routerparse.c index b194ae007b..b4bc715c25 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -291,12 +291,15 @@ int check_software_version_against_directory(const char *directory, /** Parse a directory from str and, when done, store the * resulting routerlist in *dest, freeing the old value if necessary. * If pkey is provided, we check the directory signature with pkey. + * + * DOCDOC check_version, write_to_cache. */ int /* Should be static; exposed for unit tests */ router_parse_routerlist_from_directory(const char *str, routerlist_t **dest, crypto_pk_env_t *pkey, - int check_version) + int check_version, + int write_to_cache) { directory_token_t *tok; char digest[DIGEST_LEN]; @@ -394,7 +397,7 @@ router_parse_routerlist_from_directory(const char *str, /* Now that we know the signature is okay, and we have a * publication time, cache the directory. */ - if (!get_options()->AuthoritativeDir) + if (!get_options()->AuthoritativeDir && write_to_cache) dirserv_set_cached_directory(str, published_on, 0); if (!(tok = find_first_by_keyword(tokens, K_RECOMMENDED_SOFTWARE))) { @@ -480,7 +483,7 @@ router_parse_routerlist_from_directory(const char *str, /* DOCDOC */ running_routers_t * -router_parse_runningrouters(const char *str) +router_parse_runningrouters(const char *str, int write_to_cache) { char digest[DIGEST_LEN]; running_routers_t *new_list = NULL; @@ -520,7 +523,7 @@ router_parse_runningrouters(const char *str) /* Now that we know the signature is okay, and we have a * publication time, cache the list. */ - if (!get_options()->AuthoritativeDir) + if (!get_options()->AuthoritativeDir && write_to_cache) dirserv_set_cached_directory(str, published_on, 1); if (!(tok = find_first_by_keyword(tokens, K_ROUTER_STATUS))) { diff --git a/src/or/test.c b/src/or/test.c index 01e1cc2dd3..3a5d079faf 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -1161,7 +1161,7 @@ test_dir_format(void) test_eq(dirserv_add_descriptor((const char**)&cp), 1); get_options()->Nickname = tor_strdup("DirServer"); test_assert(!dirserv_dump_directory_to_string(&cp,pk3)); - test_assert(!router_parse_routerlist_from_directory(cp, &dir1, pk3, 1)); + test_assert(!router_parse_routerlist_from_directory(cp, &dir1, pk3, 1, 0)); test_eq(2, smartlist_len(dir1->routers)); dirserv_free_fingerprint_list(); tor_free(cp);