From 2dd566d5d589ca8e758e28d1ffb961b7d4975571 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 12 Jan 2006 18:04:17 +0000 Subject: [PATCH] Indirect access to the signed_descriptor field to make it easier to keep them lazily on disk. svn:r5827 --- src/or/control.c | 19 +++++++---- src/or/directory.c | 11 ++++--- src/or/dirserv.c | 4 ++- src/or/or.h | 3 +- src/or/router.c | 15 +++++---- src/or/routerlist.c | 78 +++++++++++++------------------------------- src/or/routerparse.c | 2 +- 7 files changed, 56 insertions(+), 76 deletions(-) diff --git a/src/or/control.c b/src/or/control.c index 6e3cd42c67..72822dbf20 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -1279,20 +1279,27 @@ handle_getinfo_helper(const char *question, char **answer) *answer = list_getinfo_options(); } else if (!strcmpstart(question, "desc/id/")) { routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/")); - if (ri && ri->cache_info.signed_descriptor) - *answer = tor_strdup(ri->cache_info.signed_descriptor); + if (ri) { + const char *body = signed_descriptor_get_body(&ri->cache_info); + if (body) + *answer = tor_strdup(body); + } } else if (!strcmpstart(question, "desc/name/")) { routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"),1); - if (ri && ri->cache_info.signed_descriptor) - *answer = tor_strdup(ri->cache_info.signed_descriptor); + if (ri) { + const char *body = signed_descriptor_get_body(&ri->cache_info); + if (body) + *answer = tor_strdup(body); + } } else if (!strcmp(question, "desc/all-recent")) { routerlist_t *routerlist = router_get_routerlist(); smartlist_t *sl = smartlist_create(); if (routerlist && routerlist->routers) { SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri, { - if (ri && ri->cache_info.signed_descriptor) - smartlist_add(sl, tor_strdup(ri->cache_info.signed_descriptor)); + const char *body = signed_descriptor_get_body(&ri->cache_info); + if (body) + smartlist_add(sl, tor_strdup(body)); }); } *answer = smartlist_join_strings(sl, "", 0, NULL); diff --git a/src/or/directory.c b/src/or/directory.c index 59a87ef731..4e278d182c 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -1516,8 +1516,8 @@ directory_handle_command_get(connection_t *conn, char *headers, char *cp = inp; SMARTLIST_FOREACH(descs, signed_descriptor_t *, ri, { - memcpy(cp, ri->signed_descriptor, - ri->signed_descriptor_len); + const char *body = signed_descriptor_get_body(ri); + memcpy(cp, body, ri->signed_descriptor_len); cp += ri->signed_descriptor_len; *cp++ = '\n'; }); @@ -1550,9 +1550,10 @@ directory_handle_command_get(connection_t *conn, char *headers, note_request(request_type, len); connection_write_to_buf(tmp, strlen(tmp), conn); SMARTLIST_FOREACH(descs, signed_descriptor_t *, ri, - connection_write_to_buf(ri->signed_descriptor, - ri->signed_descriptor_len, - conn)); + { + const char *body = signed_descriptor_get_body(ri); + connection_write_to_buf(body, ri->signed_descriptor_len, conn); + }); } } smartlist_free(descs); diff --git a/src/or/dirserv.c b/src/or/dirserv.c index 1ba051585c..7226488775 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -871,9 +871,11 @@ dirserv_dump_directory_to_string(char **dir_out, SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, { size_t len = ri->cache_info.signed_descriptor_len; + const char *body; if (cp+len+1 >= buf+buf_len) goto truncated; - memcpy(cp, ri->cache_info.signed_descriptor, len); + body = signed_descriptor_get_body(&ri->cache_info); + memcpy(cp, body, len); cp += len; *cp++ = '\n'; /* add an extra newline in case somebody was depending on * it. */ diff --git a/src/or/or.h b/src/or/or.h index 5914459e98..a4a3a0f48b 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -762,7 +762,7 @@ typedef struct cached_dir_t { /** Information need to cache an onion router's descriptor. */ typedef struct signed_descriptor_t { - char *signed_descriptor; + char *signed_descriptor_body; size_t signed_descriptor_len; char signed_descriptor_digest[DIGEST_LEN]; char identity_digest[DIGEST_LEN]; @@ -2277,6 +2277,7 @@ routerinfo_t *router_get_by_nickname(const char *nickname, routerinfo_t *router_get_by_hexdigest(const char *hexdigest); routerinfo_t *router_get_by_digest(const char *digest); signed_descriptor_t *router_get_by_descriptor_digest(const char *digest); +const char *signed_descriptor_get_body(signed_descriptor_t *desc); int router_digest_version_as_new_as(const char *digest, const char *cutoff); int router_digest_is_trusted_dir(const char *digest); routerlist_t *router_get_routerlist(void); diff --git a/src/or/router.c b/src/or/router.c index 916b18a119..d83de02221 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -761,13 +761,14 @@ router_get_my_routerinfo(void) const char * router_get_my_descriptor(void) { + const char *body; if (!desc_routerinfo) { if (router_rebuild_descriptor(1)) return NULL; } - debug(LD_GENERAL,"my desc is '%s'", - desc_routerinfo->cache_info.signed_descriptor); - return desc_routerinfo->cache_info.signed_descriptor; + body = signed_descriptor_get_body(&desc_routerinfo->cache_info); + debug(LD_GENERAL,"my desc is '%s'", body); + return body; } /*DOCDOC*/ @@ -865,16 +866,16 @@ router_rebuild_descriptor(int force) }); smartlist_free(family); } - ri->cache_info.signed_descriptor = tor_malloc(8192); - if (router_dump_router_to_string(ri->cache_info.signed_descriptor, 8192, + ri->cache_info.signed_descriptor_body = tor_malloc(8192); + if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192, ri, get_identity_key())<0) { warn(LD_BUG, "Couldn't allocate string for descriptor."); return -1; } ri->cache_info.signed_descriptor_len = - strlen(ri->cache_info.signed_descriptor); + strlen(ri->cache_info.signed_descriptor_body); crypto_digest(ri->cache_info.signed_descriptor_digest, - ri->cache_info.signed_descriptor, + ri->cache_info.signed_descriptor_body, ri->cache_info.signed_descriptor_len); if (desc_routerinfo) diff --git a/src/or/routerlist.c b/src/or/routerlist.c index d9d636e707..27d9ab8669 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -159,19 +159,20 @@ router_should_rebuild_store(void) /** Add the len-type router descriptor in s to the router * journal. */ static int -router_append_to_journal(const char *s, size_t len) +router_append_to_journal(signed_descriptor_t *desc) { or_options_t *options = get_options(); size_t fname_len = strlen(options->DataDirectory)+32; - char *fname = tor_malloc(len); + char *fname = tor_malloc(fname_len); + const char *body = signed_descriptor_get_body(desc); + size_t len = desc->signed_descriptor_len; tor_snprintf(fname, fname_len, "%s/cached-routers.new", options->DataDirectory); - if (!len) - len = strlen(s); + tor_assert(len == strlen(body)); - if (append_bytes_to_file(fname, s, len, 0)) { + if (append_bytes_to_file(fname, body, len, 0)) { warn(LD_FS, "Unable to store router descriptor"); tor_free(fname); return -1; @@ -218,12 +219,13 @@ router_rebuild_store(int force) signed_descriptor_t *sd = (i==0) ? ((signed_descriptor_t*)ptr): &((routerinfo_t*)ptr)->cache_info; sized_chunk_t *c; - if (!sd->signed_descriptor) { - warn(LD_BUG, "Bug! No descriptor stored for router."); + const char *body = signed_descriptor_get_body(sd); + if (!body) { + warn(LD_BUG, "Bug! No descriptor available for router."); goto done; } c = tor_malloc(sizeof(sized_chunk_t)); - c->bytes = sd->signed_descriptor; + c->bytes = body; c->len = sd->signed_descriptor_len; smartlist_add(chunk_list, c); }); @@ -1062,6 +1064,12 @@ router_get_by_descriptor_digest(const char *digest) return digestmap_get(routerlist->desc_digest_map, digest); } +const char * +signed_descriptor_get_body(signed_descriptor_t *desc) +{ + return desc->signed_descriptor_body; +} + /** Return the current list of all known routers. */ routerlist_t * router_get_routerlist(void) @@ -1083,7 +1091,7 @@ routerinfo_free(routerinfo_t *router) if (!router) return; - tor_free(router->cache_info.signed_descriptor); + tor_free(router->cache_info.signed_descriptor_body); tor_free(router->address); tor_free(router->nickname); tor_free(router->platform); @@ -1104,7 +1112,7 @@ routerinfo_free(routerinfo_t *router) static void signed_descriptor_free(signed_descriptor_t *sd) { - tor_free(sd->signed_descriptor); + tor_free(sd->signed_descriptor_body); tor_free(sd); } @@ -1115,50 +1123,11 @@ signed_descriptor_from_routerinfo(routerinfo_t *ri) { signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t)); memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t)); - ri->cache_info.signed_descriptor = NULL; + ri->cache_info.signed_descriptor_body = NULL; routerinfo_free(ri); return sd; } -#if 0 -/** Allocate a fresh copy of router */ -/* XXX Nobody uses this. Remove it? */ -routerinfo_t * -routerinfo_copy(const routerinfo_t *router) -{ - routerinfo_t *r; - addr_policy_t **e, *tmp; - - r = tor_malloc(sizeof(routerinfo_t)); - memcpy(r, router, sizeof(routerinfo_t)); - - r->address = tor_strdup(r->address); - r->nickname = tor_strdup(r->nickname); - r->platform = tor_strdup(r->platform); - if (r->cache_info.signed_descriptor) - r->cache_info.signed_descriptor = - tor_strdup(r->cache_info.signed_descriptor); - if (r->onion_pkey) - r->onion_pkey = crypto_pk_dup_key(r->onion_pkey); - if (r->identity_pkey) - r->identity_pkey = crypto_pk_dup_key(r->identity_pkey); - e = &r->exit_policy; - while (*e) { - tmp = tor_malloc(sizeof(addr_policy_t)); - memcpy(tmp,*e,sizeof(addr_policy_t)); - *e = tmp; - (*e)->string = tor_strdup((*e)->string); - e = & ((*e)->next); - } - if (r->declared_family) { - r->declared_family = smartlist_create(); - SMARTLIST_FOREACH(router->declared_family, const char *, s, - smartlist_add(r->declared_family, tor_strdup(s))); - } - return r; -} -#endif - /** Free all storage held by a routerlist rl */ void routerlist_free(routerlist_t *rl) @@ -1588,9 +1557,9 @@ router_add_to_routerlist(routerinfo_t *router, const char **msg, } } routerlist_replace(routerlist, old_router, router, i, 1); - if (!from_cache) - router_append_to_journal(router->cache_info.signed_descriptor, - router->cache_info.signed_descriptor_len); + if (!from_cache) { + router_append_to_journal(&router->cache_info); + } directory_set_dirty(); *msg = unreachable ? "Dirserver believes your ORPort is unreachable" : authdir_verified ? "Verified server updated" : @@ -1633,8 +1602,7 @@ router_add_to_routerlist(routerinfo_t *router, const char **msg, * the list. */ routerlist_insert(routerlist, router); if (!from_cache) - router_append_to_journal(router->cache_info.signed_descriptor, - router->cache_info.signed_descriptor_len); + router_append_to_journal(&router->cache_info); directory_set_dirty(); return 0; } diff --git a/src/or/routerparse.c b/src/or/routerparse.c index 01b2396888..979ad22b08 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -745,7 +745,7 @@ router_parse_entry_from_string(const char *s, const char *end) } router = tor_malloc_zero(sizeof(routerinfo_t)); - router->cache_info.signed_descriptor = tor_strndup(s, end-s); + router->cache_info.signed_descriptor_body = tor_strndup(s, end-s); router->cache_info.signed_descriptor_len = end-s; memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);