free a small static string on exit.
[tor.git] / src / or / routerlist.c
blobe362cfe0d12137464171a8ca69d8b93f850a554c
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char routerlist_c_id[] =
7 "$Id$";
9 /**
10 * \file routerlist.c
11 * \brief Code to
12 * maintain and access the global list of routerinfos for known
13 * servers.
14 **/
16 #include "or.h"
18 // #define DEBUG_ROUTERLIST
20 /****************************************************************************/
22 /* static function prototypes */
23 static routerstatus_t *router_pick_directory_server_impl(int requireother,
24 int fascistfirewall,
25 int prefer_tunnel,
26 authority_type_t auth);
27 static routerstatus_t *router_pick_trusteddirserver_impl(
28 authority_type_t type, int requireother,
29 int fascistfirewall, int prefer_tunnel);
30 static void mark_all_trusteddirservers_up(void);
31 static int router_nickname_matches(routerinfo_t *router, const char *nickname);
32 static void routerstatus_list_update_from_networkstatus(time_t now);
33 static void local_routerstatus_free(local_routerstatus_t *rs);
34 static void trusted_dir_server_free(trusted_dir_server_t *ds);
35 static void update_networkstatus_cache_downloads(time_t now);
36 static void update_networkstatus_client_downloads(time_t now);
37 static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
38 static int have_tried_downloading_all_statuses(int n_failures);
39 static routerstatus_t *networkstatus_find_entry(networkstatus_t *ns,
40 const char *digest);
41 static local_routerstatus_t *router_get_combined_status_by_nickname(
42 const char *nickname,
43 int warn_if_unnamed);
44 static void update_router_have_minimum_dir_info(void);
45 static void router_dir_info_changed(void);
47 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
48 DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
49 DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
51 /****************************************************************************/
53 /** Global list of a trusted_dir_server_t object for each trusted directory
54 * server. */
55 static smartlist_t *trusted_dir_servers = NULL;
56 /** True iff the key certificate in at least one member of
57 * <b>trusted_dir_server_t</b> has changed since we last flushed the
58 * certificates to disk. */
59 static int trusted_dir_servers_certs_changed = 0;
61 /** Global list of all of the routers that we know about. */
62 static routerlist_t *routerlist = NULL;
64 /** Global list of all of the current network_status documents that we know
65 * about. This list is kept sorted by published_on. */
66 static smartlist_t *networkstatus_list = NULL;
68 /** DOCDOC */
69 static networkstatus_vote_t *current_consensus = NULL;
71 /** Global list of local_routerstatus_t for each router, known or unknown.
72 * Kept sorted by digest. */
73 static smartlist_t *routerstatus_list = NULL;
74 /** Map from descriptor digest to a member of routerstatus_list: used to
75 * update download status when a download fails. */
76 static digestmap_t *routerstatus_by_desc_digest_map = NULL;
78 /** Map from lowercase nickname to digest of named server, if any. */
79 static strmap_t *named_server_map = NULL;
81 /** True iff any member of networkstatus_list has changed since the last time
82 * we called routerstatus_list_update_from_networkstatus(). */
83 static int networkstatus_list_has_changed = 0;
85 /** True iff any element of routerstatus_list has changed since the last
86 * time we called routers_update_all_from_networkstatus().*/
87 static int routerstatus_list_has_changed = 0;
89 /** List of strings for nicknames we've already warned about and that are
90 * still unknown / unavailable. */
91 static smartlist_t *warned_nicknames = NULL;
93 /** List of strings for nicknames or fingerprints we've already warned about
94 * and that are still conflicted. */
95 static smartlist_t *warned_conflicts = NULL;
97 /** The last time we tried to download any routerdesc, or 0 for "never". We
98 * use this to rate-limit download attempts when the number of routerdescs to
99 * download is low. */
100 static time_t last_routerdesc_download_attempted = 0;
102 /** The last time we tried to download a networkstatus, or 0 for "never". We
103 * use this to rate-limit download attempts for directory caches (including
104 * mirrors). Clients don't use this now. */
105 static time_t last_networkstatus_download_attempted = 0;
107 /** True iff we have logged a warning about this OR not being valid or
108 * not being named. */
109 static int have_warned_about_invalid_status = 0;
110 /** True iff we have logged a warning about this OR's version being older than
111 * listed by the authorities */
112 static int have_warned_about_old_version = 0;
113 /** True iff we have logged a warning about this OR's version being newer than
114 * listed by the authorities */
115 static int have_warned_about_new_version = 0;
117 /** Return the number of directory authorities whose type matches some bit set
118 * in <b>type</b> */
120 get_n_authorities(authority_type_t type)
122 int n = 0;
123 if (!trusted_dir_servers)
124 return 0;
125 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
126 if (ds->type & type)
127 ++n);
128 return n;
131 #define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
133 /** Repopulate our list of network_status_t objects from the list cached on
134 * disk. Return 0 on success, -1 on failure. */
136 router_reload_networkstatus(void)
138 char filename[512];
139 smartlist_t *entries;
140 struct stat st;
141 char *s;
142 tor_assert(get_options()->DataDirectory);
143 if (!networkstatus_list)
144 networkstatus_list = smartlist_create();
146 routerlist_check_bug_417();
148 tor_snprintf(filename,sizeof(filename),"%s"PATH_SEPARATOR"cached-status",
149 get_options()->DataDirectory);
150 entries = tor_listdir(filename);
151 SMARTLIST_FOREACH(entries, const char *, fn, {
152 char buf[DIGEST_LEN];
153 if (strlen(fn) != HEX_DIGEST_LEN ||
154 base16_decode(buf, sizeof(buf), fn, strlen(fn))) {
155 log_info(LD_DIR,
156 "Skipping cached-status file with unexpected name \"%s\"",fn);
157 continue;
159 tor_snprintf(filename,sizeof(filename),
160 "%s"PATH_SEPARATOR"cached-status"PATH_SEPARATOR"%s",
161 get_options()->DataDirectory, fn);
162 s = read_file_to_str(filename, 0, &st);
163 if (s) {
164 if (router_set_networkstatus(s, st.st_mtime, NS_FROM_CACHE, NULL)<0) {
165 log_warn(LD_FS, "Couldn't load networkstatus from \"%s\"",filename);
167 tor_free(s);
170 SMARTLIST_FOREACH(entries, char *, fn, tor_free(fn));
171 smartlist_free(entries);
172 networkstatus_list_clean(time(NULL));
173 routers_update_all_from_networkstatus(time(NULL));
174 routerlist_check_bug_417();
175 return 0;
178 /** Reload the cached v3 key certificates from the cached-certs file in
179 * the data directory. Return 0 on success, -1 on failure. */
181 trusted_dirs_reload_certs(void)
183 char filename[512];
184 char *contents;
185 int r;
187 tor_snprintf(filename,sizeof(filename),"%s"PATH_SEPARATOR"cached-certs",
188 get_options()->DataDirectory);
189 contents = read_file_to_str(filename, 0, NULL);
190 if (!contents)
191 return -1;
192 r = trusted_dirs_load_certs_from_string(contents, 1);
193 tor_free(contents);
194 return r;
197 /** Load a bunch of new key certificates from the string <b>contents</b>. If
198 * <b>from_store</b> is true, the certificates are from the cache, and we
199 * don't need to flush them to disk. If <b>from_store</b> is false, we need
200 * to flush any changed certificates to disk. Return 0 on success, -1 on
201 * failure. */
203 trusted_dirs_load_certs_from_string(const char *contents, int from_store)
205 trusted_dir_server_t *ds;
206 const char *s, *eos;
208 for (s = contents; *s; s = eos) {
209 authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
210 if (!cert)
211 break;
212 ds = trusteddirserver_get_by_v3_auth_digest(
213 cert->cache_info.identity_digest);
214 if (!ds) {
215 log_info(LD_DIR, "Found cached certificate whose key didn't match "
216 "any v3 authority we recognized; skipping.");
217 authority_cert_free(cert);
218 continue;
221 if (ds->v3_cert) {
222 if (ds->v3_cert->expires < cert->expires) {
223 authority_cert_free(ds->v3_cert);
224 ds->v3_cert = NULL; /* redundant, but let's be safe. */
225 } else {
226 /* This also covers the case where the certificate is the same
227 * as the one we have. */
228 authority_cert_free(cert);
229 continue;
233 cert->cache_info.signed_descriptor_body = tor_strndup(s, eos-s);
234 cert->cache_info.signed_descriptor_len = eos-s;
235 ds->v3_cert = cert;
236 if (!from_store)
237 trusted_dir_servers_certs_changed = 1;
239 return 0;
242 /** Save all v3 key certifiacates to the cached-certs file. */
243 void
244 trusted_dirs_flush_certs_to_disk(void)
246 char filename[512];
247 smartlist_t *chunks = smartlist_create();
249 tor_snprintf(filename,sizeof(filename),"%s"PATH_SEPARATOR"cached-certs",
250 get_options()->DataDirectory);
251 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
253 if (ds->v3_cert) {
254 sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
255 c->bytes = ds->v3_cert->cache_info.signed_descriptor_body;
256 c->len = ds->v3_cert->cache_info.signed_descriptor_len;
257 smartlist_add(chunks, c);
260 if (write_chunks_to_file(filename, chunks, 0)) {
261 log_warn(LD_FS, "Error writing certificates to disk.");
263 SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
264 smartlist_free(chunks);
266 trusted_dir_servers_certs_changed = 0;
269 /* Router descriptor storage.
271 * Routerdescs are stored in a big file, named "cached-routers". As new
272 * routerdescs arrive, we append them to a journal file named
273 * "cached-routers.new".
275 * From time to time, we replace "cached-routers" with a new file containing
276 * only the live, non-superseded descriptors, and clear cached-routers.new.
278 * On startup, we read both files.
281 /** Information about disk space usage in a cached-routers or cached-extrainfo
282 * file and its associcated journal. */
283 typedef struct store_stats_t {
284 /** The size of the router log, in bytes. */
285 size_t journal_len;
286 /** The size of the router store, in bytes. */
287 size_t store_len;
288 /** Total bytes dropped since last rebuild: this is space currently
289 * used in the cache and the journal that could be freed by a rebuild. */
290 size_t bytes_dropped;
291 } store_stats_t;
293 /** Disk usage for cached-routers and cached-routers.new */
294 static store_stats_t router_store_stats = { 0, 0, 0 };
295 /** Disk usage for cached-extrainfo and cached-extrainfo.new */
296 static store_stats_t extrainfo_store_stats = { 0, 0, 0 };
298 /** Helper: return 1 iff the router log is so big we want to rebuild the
299 * store. */
300 static int
301 router_should_rebuild_store(store_stats_t *stats)
303 if (stats->store_len > (1<<16))
304 return (stats->journal_len > stats->store_len / 2 ||
305 stats->bytes_dropped > stats->store_len / 2);
306 else
307 return stats->journal_len > (1<<15);
310 /** Add the signed_descriptor_t in <b>desc</b> to the router
311 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
312 * offset appropriately.
314 * If <b>purpose</b> isn't ROUTER_PURPOSE_GENERAL or
315 * EXTRAINFO_PURPOSE_GENERAL, just do nothing. */
316 static int
317 signed_desc_append_to_journal(signed_descriptor_t *desc, int purpose)
319 or_options_t *options = get_options();
320 size_t fname_len = strlen(options->DataDirectory)+32;
321 char *fname;
322 const char *body = signed_descriptor_get_body(desc);
323 size_t len = desc->signed_descriptor_len;
324 const char *fname_base = purpose == ROUTER_PURPOSE_GENERAL ?
325 "cached-routers" : "cached-extrainfo";
326 store_stats_t *stats;
328 routerlist_check_bug_417();
330 if (purpose == ROUTER_PURPOSE_GENERAL) {
331 stats = &router_store_stats;
332 } else if (purpose == EXTRAINFO_PURPOSE_GENERAL) {
333 stats = &extrainfo_store_stats;
334 } else {
335 /* we shouldn't cache it. be happy and return. */
336 return 0;
339 fname = tor_malloc(fname_len);
340 tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"%s.new",
341 options->DataDirectory, fname_base);
343 tor_assert(len == strlen(body));
345 if (append_bytes_to_file(fname, body, len, 1)) {
346 log_warn(LD_FS, "Unable to store router descriptor");
347 tor_free(fname);
348 return -1;
350 desc->saved_location = SAVED_IN_JOURNAL;
351 tor_free(fname);
353 desc->saved_offset = stats->journal_len;
354 stats->journal_len += len;
356 routerlist_check_bug_417();
357 return 0;
360 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
361 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
362 * the signed_descriptor_t* in *<b>b</b>. */
363 static int
364 _compare_signed_descriptors_by_age(const void **_a, const void **_b)
366 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
367 return r1->published_on - r2->published_on;
370 /** If the journal is too long, or if <b>force</b> is true, then atomically
371 * replace the router store with the routers currently in our routerlist, and
372 * clear the journal. Return 0 on success, -1 on failure.
374 * If <b>extrainfo</b> is true, rebuild the extrainfo store; else rebuild the
375 * router descriptor store.
377 static int
378 router_rebuild_store(int force, int extrainfo)
380 or_options_t *options;
381 size_t fname_len;
382 smartlist_t *chunk_list = NULL;
383 char *fname = NULL, *fname_tmp = NULL;
384 int r = -1;
385 off_t offset = 0;
386 smartlist_t *signed_descriptors = NULL;
387 store_stats_t *stats =
388 extrainfo ? &extrainfo_store_stats : &router_store_stats;
389 const char *fname_base =
390 extrainfo ? "cached-extrainfo" : "cached-routers";
391 tor_mmap_t **mmap_ptr;
393 if (!force && !router_should_rebuild_store(stats))
394 return 0;
395 if (!routerlist)
396 return 0;
398 mmap_ptr =
399 extrainfo ? &routerlist->mmap_extrainfo : &routerlist->mmap_descriptors;
401 routerlist_check_bug_417();
402 routerlist_assert_ok(routerlist);
404 /* Don't save deadweight. */
405 routerlist_remove_old_routers();
407 log_info(LD_DIR, "Rebuilding %s cache",
408 extrainfo ? "extra-info" : "router descriptor");
410 options = get_options();
411 fname_len = strlen(options->DataDirectory)+32;
412 fname = tor_malloc(fname_len);
413 fname_tmp = tor_malloc(fname_len);
414 tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"%s",
415 options->DataDirectory, fname_base);
416 tor_snprintf(fname_tmp, fname_len, "%s"PATH_SEPARATOR"%s.tmp",
417 options->DataDirectory, fname_base);
419 chunk_list = smartlist_create();
421 /* We sort the routers by age to enhance locality on disk. */
422 signed_descriptors = smartlist_create();
423 if (extrainfo) {
424 eimap_iter_t *iter;
425 for (iter = eimap_iter_init(routerlist->extra_info_map);
426 !eimap_iter_done(iter);
427 iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
428 const char *key;
429 extrainfo_t *ei;
430 eimap_iter_get(iter, &key, &ei);
431 smartlist_add(signed_descriptors, &ei->cache_info);
433 } else {
434 smartlist_add_all(signed_descriptors, routerlist->old_routers);
435 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
436 smartlist_add(signed_descriptors, &ri->cache_info));
439 smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);
441 /* Now, add the appropriate members to chunk_list */
442 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
444 sized_chunk_t *c;
445 const char *body = signed_descriptor_get_body(sd);
446 if (!body) {
447 log_warn(LD_BUG, "No descriptor available for router.");
448 goto done;
450 if (sd->do_not_cache)
451 continue;
452 c = tor_malloc(sizeof(sized_chunk_t));
453 c->bytes = body;
454 c->len = sd->signed_descriptor_len;
455 smartlist_add(chunk_list, c);
458 if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
459 log_warn(LD_FS, "Error writing router store to disk.");
460 goto done;
463 /* Our mmap is now invalid. */
464 if (*mmap_ptr) {
465 tor_munmap_file(*mmap_ptr);
466 *mmap_ptr = NULL;
469 if (replace_file(fname_tmp, fname)<0) {
470 log_warn(LD_FS, "Error replacing old router store.");
471 goto done;
474 *mmap_ptr = tor_mmap_file(fname);
475 if (! *mmap_ptr)
476 log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
478 log_info(LD_DIR, "Reconstructing pointers into cache");
480 offset = 0;
481 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
483 if (sd->do_not_cache)
484 continue;
485 sd->saved_location = SAVED_IN_CACHE;
486 if (*mmap_ptr) {
487 tor_free(sd->signed_descriptor_body); // sets it to null
488 sd->saved_offset = offset;
490 offset += sd->signed_descriptor_len;
491 signed_descriptor_get_body(sd); /* reconstruct and assert */
494 tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"%s.new",
495 options->DataDirectory, fname_base);
497 write_str_to_file(fname, "", 1);
499 r = 0;
500 stats->store_len = (size_t) offset;
501 stats->journal_len = 0;
502 stats->bytes_dropped = 0;
503 done:
504 if (signed_descriptors)
505 smartlist_free(signed_descriptors);
506 tor_free(fname);
507 tor_free(fname_tmp);
508 SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
509 smartlist_free(chunk_list);
511 routerlist_check_bug_417();
512 return r;
515 /** Helper: Reload a cache file and its associated journal, setting metadata
516 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
517 * else reload the router descriptor store. */
518 static int
519 router_reload_router_list_impl(int extrainfo)
521 or_options_t *options = get_options();
522 size_t fname_len = strlen(options->DataDirectory)+32;
523 char *fname = tor_malloc(fname_len), *contents = NULL;
524 store_stats_t *stats =
525 extrainfo ? &extrainfo_store_stats : &router_store_stats;
526 const char *fname_base =
527 extrainfo ? "cached-extrainfo" : "cached-routers";
528 tor_mmap_t **mmap_ptr;
529 struct stat st;
531 routerlist_check_bug_417();
533 if (!routerlist)
534 router_get_routerlist(); /* mallocs and inits it in place */
536 mmap_ptr =
537 extrainfo ? &routerlist->mmap_extrainfo : &routerlist->mmap_descriptors;
539 stats->journal_len = stats->store_len = 0;
541 tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"%s",
542 options->DataDirectory, fname_base);
544 if (*mmap_ptr) /* get rid of it first */
545 tor_munmap_file(*mmap_ptr);
546 *mmap_ptr = NULL;
548 *mmap_ptr = tor_mmap_file(fname);
549 if (*mmap_ptr) {
550 stats->store_len = (*mmap_ptr)->size;
551 if (extrainfo)
552 router_load_extrainfo_from_string((*mmap_ptr)->data,
553 (*mmap_ptr)->data+(*mmap_ptr)->size,
554 SAVED_IN_CACHE, NULL);
555 else
556 router_load_routers_from_string((*mmap_ptr)->data,
557 (*mmap_ptr)->data+(*mmap_ptr)->size,
558 SAVED_IN_CACHE, NULL,
559 ROUTER_PURPOSE_GENERAL);
562 tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"%s.new",
563 options->DataDirectory, fname_base);
564 if (file_status(fname) == FN_FILE)
565 contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
566 if (contents) {
567 if (extrainfo)
568 router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL, NULL);
569 else
570 router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL, NULL,
571 ROUTER_PURPOSE_GENERAL);
572 stats->journal_len = (size_t) st.st_size;
573 tor_free(contents);
576 tor_free(fname);
578 if (stats->journal_len) {
579 /* Always clear the journal on startup.*/
580 router_rebuild_store(1, extrainfo);
581 } else if (!extrainfo) {
582 /* Don't cache expired routers. (This is in an else because
583 * router_rebuild_store() also calls remove_old_routers().) */
584 routerlist_remove_old_routers();
587 routerlist_check_bug_417();
589 return 0;
592 /** Load all cached router descriptors and extra-info documents from the
593 * store. Return 0 on success and -1 on failure.
596 router_reload_router_list(void)
598 if (router_reload_router_list_impl(0))
599 return 1;
600 if (router_reload_router_list_impl(1))
601 return 1;
602 return 0;
605 /** Return a smartlist containing a list of trusted_dir_server_t * for all
606 * known trusted dirservers. Callers must not modify the list or its
607 * contents.
609 smartlist_t *
610 router_get_trusted_dir_servers(void)
612 if (!trusted_dir_servers)
613 trusted_dir_servers = smartlist_create();
615 return trusted_dir_servers;
618 /** Try to find a running dirserver. If there are no running dirservers
619 * in our routerlist and <b>retry_if_no_servers</b> is non-zero,
620 * set all the authoritative ones as running again, and pick one;
621 * if there are then no dirservers at all in our routerlist,
622 * reload the routerlist and try one last time. If for_runningrouters is
623 * true, then only pick a dirserver that can answer runningrouters queries
624 * (that is, a trusted dirserver, or one running 0.0.9rc5-cvs or later).
625 * Don't pick an authority if any non-authority is viable.
626 * Other args are as in router_pick_directory_server_impl().
628 routerstatus_t *
629 router_pick_directory_server(int requireother,
630 int fascistfirewall,
631 authority_type_t type,
632 int retry_if_no_servers)
634 routerstatus_t *choice;
635 int prefer_tunnel = get_options()->PreferTunneledDirConns;
637 if (!routerlist)
638 return NULL;
640 choice = router_pick_directory_server_impl(requireother, fascistfirewall,
641 prefer_tunnel, type);
642 if (choice || !retry_if_no_servers)
643 return choice;
645 log_info(LD_DIR,
646 "No reachable router entries for dirservers. "
647 "Trying them all again.");
648 /* mark all authdirservers as up again */
649 mark_all_trusteddirservers_up();
650 /* try again */
651 choice = router_pick_directory_server_impl(requireother, fascistfirewall,
652 prefer_tunnel, type);
653 if (choice)
654 return choice;
656 log_info(LD_DIR,"Still no %s router entries. Reloading and trying again.",
657 fascistfirewall ? "reachable" : "known");
658 if (router_reload_router_list()) {
659 return NULL;
661 /* give it one last try */
662 choice = router_pick_directory_server_impl(requireother, fascistfirewall,
663 prefer_tunnel, type);
664 return choice;
667 /** Return the trusted_dir_server_t for the directory authority whose identity
668 * key hashes to <b>digest</b>, or NULL if no such authority is known.
670 trusted_dir_server_t *
671 router_get_trusteddirserver_by_digest(const char *digest)
673 if (!trusted_dir_servers)
674 return NULL;
676 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
678 if (!memcmp(ds->digest, digest, DIGEST_LEN))
679 return ds;
682 return NULL;
685 /** Return the trusted_dir_server_t for the directory authority whose identity
686 * key hashes to <b>digest</b>, or NULL if no such authority is known.
688 trusted_dir_server_t *
689 trusteddirserver_get_by_v3_auth_digest(const char *digest)
691 if (!trusted_dir_servers)
692 return NULL;
694 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
696 if (!memcmp(ds->v3_identity_digest, digest, DIGEST_LEN))
697 return ds;
700 return NULL;
703 /** Try to find a running trusted dirserver. If there are no running
704 * trusted dirservers and <b>retry_if_no_servers</b> is non-zero,
705 * set them all as running again, and try again.
706 * <b>type> specifies the type of authoritative dir we require.
707 * Other args are as in router_pick_trusteddirserver_impl().
709 routerstatus_t *
710 router_pick_trusteddirserver(authority_type_t type,
711 int requireother,
712 int fascistfirewall,
713 int retry_if_no_servers)
715 routerstatus_t *choice;
716 int prefer_tunnel = get_options()->PreferTunneledDirConns;
718 choice = router_pick_trusteddirserver_impl(type, requireother,
719 fascistfirewall, prefer_tunnel);
720 if (choice || !retry_if_no_servers)
721 return choice;
723 log_info(LD_DIR,
724 "No trusted dirservers are reachable. Trying them all again.");
725 mark_all_trusteddirservers_up();
726 return router_pick_trusteddirserver_impl(type, requireother,
727 fascistfirewall, prefer_tunnel);
730 /** How long do we avoid using a directory server after it's given us a 503? */
731 #define DIR_503_TIMEOUT (60*60)
733 /** Pick a random running valid directory server/mirror from our
734 * routerlist.
736 * If <b>fascistfirewall</b>, make sure the router we pick is allowed
737 * by our firewall options.
738 * If <b>requireother</b>, it cannot be us. If <b>for_v2_directory</b>,
739 * choose a directory server new enough to support the v2 directory
740 * functionality.
741 * If <b>prefer_tunnel</b>, choose a directory server that is reachable
742 * and supports BEGIN_DIR cells, if possible.
744 * Don't pick an authority if any non-authorities are viable. Try to
745 * avoid using servers that are overloaded (have returned 503 recently).
747 static routerstatus_t *
748 router_pick_directory_server_impl(int requireother, int fascistfirewall,
749 int prefer_tunnel, authority_type_t type)
751 routerstatus_t *result;
752 smartlist_t *direct, *tunnel;
753 smartlist_t *trusted_direct, *trusted_tunnel;
754 smartlist_t *overloaded_direct, *overloaded_tunnel;
755 time_t now = time(NULL);
757 if (!routerstatus_list)
758 return NULL;
760 direct = smartlist_create();
761 tunnel = smartlist_create();
762 trusted_direct = smartlist_create();
763 trusted_tunnel = smartlist_create();
764 overloaded_direct = smartlist_create();
765 overloaded_tunnel = smartlist_create();
767 /* Find all the running dirservers we know about. */
768 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, _local_status,
770 routerstatus_t *status = &(_local_status->status);
771 int is_trusted;
772 int is_overloaded = _local_status->last_dir_503_at + DIR_503_TIMEOUT > now;
773 if (!status->is_running || !status->dir_port || !status->is_valid)
774 continue;
775 if (status->is_bad_directory)
776 continue;
777 if (requireother && router_digest_is_me(status->identity_digest))
778 continue;
779 is_trusted = router_digest_is_trusted_dir(status->identity_digest);
780 if ((type & V2_AUTHORITY) && !(status->is_v2_dir || is_trusted))
781 continue;
782 if ((type & EXTRAINFO_CACHE) &&
783 !router_supports_extrainfo(status->identity_digest, 0))
784 continue;
785 if (prefer_tunnel &&
786 status->version_supports_begindir &&
787 (!fascistfirewall ||
788 fascist_firewall_allows_address_or(status->addr, status->or_port)))
789 smartlist_add(is_trusted ? trusted_tunnel :
790 is_overloaded ? overloaded_tunnel : tunnel, status);
791 else if (!fascistfirewall ||
792 fascist_firewall_allows_address_dir(status->addr,
793 status->dir_port))
794 smartlist_add(is_trusted ? trusted_direct :
795 is_overloaded ? overloaded_direct : direct, status);
798 if (smartlist_len(tunnel)) {
799 result = routerstatus_sl_choose_by_bandwidth(tunnel);
800 } else if (smartlist_len(overloaded_tunnel)) {
801 result = routerstatus_sl_choose_by_bandwidth(overloaded_tunnel);
802 } else if (smartlist_len(trusted_tunnel)) {
803 /* FFFF We don't distinguish between trusteds and overloaded trusteds
804 * yet. Maybe one day we should. */
805 /* FFFF We also don't load balance over authorities yet. I think this
806 * is a feature, but it could easily be a bug. -RD */
807 result = smartlist_choose(trusted_tunnel);
808 } else if (smartlist_len(direct)) {
809 result = routerstatus_sl_choose_by_bandwidth(direct);
810 } else if (smartlist_len(overloaded_direct)) {
811 result = routerstatus_sl_choose_by_bandwidth(overloaded_direct);
812 } else {
813 result = smartlist_choose(trusted_direct);
815 smartlist_free(direct);
816 smartlist_free(tunnel);
817 smartlist_free(trusted_direct);
818 smartlist_free(trusted_tunnel);
819 smartlist_free(overloaded_direct);
820 smartlist_free(overloaded_tunnel);
821 return result;
824 /** Choose randomly from among the trusted dirservers that are up. If
825 * <b>fascistfirewall</b>, make sure the port we pick is allowed by our
826 * firewall options. If <b>requireother</b>, it cannot be us.
827 * <b>type> specifies the type of authoritative dir we require.
829 static routerstatus_t *
830 router_pick_trusteddirserver_impl(authority_type_t type,
831 int requireother, int fascistfirewall,
832 int prefer_tunnel)
834 smartlist_t *direct, *tunnel;
835 smartlist_t *overloaded_direct, *overloaded_tunnel;
836 routerinfo_t *me = router_get_my_routerinfo();
837 routerstatus_t *result;
838 time_t now = time(NULL);
840 direct = smartlist_create();
841 tunnel = smartlist_create();
842 overloaded_direct = smartlist_create();
843 overloaded_tunnel = smartlist_create();
845 if (!trusted_dir_servers)
846 return NULL;
848 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
850 int is_overloaded =
851 d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
852 if (!d->is_running) continue;
853 if ((type & d->type) == 0)
854 continue;
855 if ((type & EXTRAINFO_CACHE) &&
856 !router_supports_extrainfo(d->digest, 1))
857 continue;
858 if (requireother && me && router_digest_is_me(d->digest))
859 continue;
860 if (prefer_tunnel &&
861 d->or_port &&
862 (!fascistfirewall ||
863 fascist_firewall_allows_address_or(d->addr, d->or_port)))
864 smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
865 &d->fake_status.status);
866 else if (!fascistfirewall ||
867 fascist_firewall_allows_address_dir(d->addr, d->dir_port))
868 smartlist_add(is_overloaded ? overloaded_direct : direct,
869 &d->fake_status.status);
872 if (smartlist_len(tunnel)) {
873 result = smartlist_choose(tunnel);
874 } else if (smartlist_len(overloaded_tunnel)) {
875 result = smartlist_choose(overloaded_tunnel);
876 } else if (smartlist_len(direct)) {
877 result = smartlist_choose(direct);
878 } else {
879 result = smartlist_choose(overloaded_direct);
882 smartlist_free(direct);
883 smartlist_free(tunnel);
884 smartlist_free(overloaded_direct);
885 smartlist_free(overloaded_tunnel);
886 return result;
889 /** Go through and mark the authoritative dirservers as up. */
890 static void
891 mark_all_trusteddirservers_up(void)
893 if (routerlist) {
894 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
895 if (router_digest_is_trusted_dir(router->cache_info.identity_digest) &&
896 router->dir_port > 0) {
897 router->is_running = 1;
900 if (trusted_dir_servers) {
901 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
903 local_routerstatus_t *rs;
904 dir->is_running = 1;
905 dir->n_networkstatus_failures = 0;
906 dir->fake_status.last_dir_503_at = 0;
907 rs = router_get_combined_status_by_digest(dir->digest);
908 if (rs && !rs->status.is_running) {
909 rs->status.is_running = 1;
910 rs->last_dir_503_at = 0;
911 control_event_networkstatus_changed_single(rs);
915 last_networkstatus_download_attempted = 0;
916 router_dir_info_changed();
919 /** Reset all internal variables used to count failed downloads of network
920 * status objects. */
921 void
922 router_reset_status_download_failures(void)
924 mark_all_trusteddirservers_up();
927 /** Look through the routerlist and identify routers that
928 * advertise the same /16 network address as <b>router</b>.
929 * Add each of them to <b>sl</b>.
931 static void
932 routerlist_add_network_family(smartlist_t *sl, routerinfo_t *router)
934 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
936 if (router != r &&
937 (router->addr & 0xffff0000) == (r->addr & 0xffff0000))
938 smartlist_add(sl, r);
942 /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
943 * This is used to make sure we don't pick siblings in a single path.
945 void
946 routerlist_add_family(smartlist_t *sl, routerinfo_t *router)
948 routerinfo_t *r;
949 config_line_t *cl;
950 or_options_t *options = get_options();
952 /* First, add any routers with similar network addresses. */
953 if (options->EnforceDistinctSubnets)
954 routerlist_add_network_family(sl, router);
956 if (router->declared_family) {
957 /* Add every r such that router declares familyness with r, and r
958 * declares familyhood with router. */
959 SMARTLIST_FOREACH(router->declared_family, const char *, n,
961 if (!(r = router_get_by_nickname(n, 0)))
962 continue;
963 if (!r->declared_family)
964 continue;
965 SMARTLIST_FOREACH(r->declared_family, const char *, n2,
967 if (router_nickname_matches(router, n2))
968 smartlist_add(sl, r);
973 /* If the user declared any families locally, honor those too. */
974 for (cl = get_options()->NodeFamilies; cl; cl = cl->next) {
975 if (router_nickname_is_in_list(router, cl->value)) {
976 add_nickname_list_to_smartlist(sl, cl->value, 0);
981 /** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
982 * see which nicknames in <b>list</b> name routers in our routerlist, and add
983 * the routerinfos for those routers to <b>sl</b>. If <b>must_be_running</b>,
984 * only include routers that we think are running.
985 * Warn if any non-Named routers are specified by nickname.
987 void
988 add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
989 int must_be_running)
991 routerinfo_t *router;
992 smartlist_t *nickname_list;
993 int have_dir_info = router_have_minimum_dir_info();
995 if (!list)
996 return; /* nothing to do */
997 tor_assert(sl);
999 nickname_list = smartlist_create();
1000 if (!warned_nicknames)
1001 warned_nicknames = smartlist_create();
1003 smartlist_split_string(nickname_list, list, ",",
1004 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1006 SMARTLIST_FOREACH(nickname_list, const char *, nick, {
1007 int warned;
1008 if (!is_legal_nickname_or_hexdigest(nick)) {
1009 log_warn(LD_CONFIG, "Nickname '%s' is misformed; skipping", nick);
1010 continue;
1012 router = router_get_by_nickname(nick, 1);
1013 warned = smartlist_string_isin(warned_nicknames, nick);
1014 if (router) {
1015 if (!must_be_running || router->is_running) {
1016 smartlist_add(sl,router);
1018 } else if (!router_get_combined_status_by_nickname(nick,1)) {
1019 if (!warned) {
1020 log_fn(have_dir_info ? LOG_WARN : LOG_INFO, LD_CONFIG,
1021 "Nickname list includes '%s' which isn't a known router.",nick);
1022 smartlist_add(warned_nicknames, tor_strdup(nick));
1026 SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
1027 smartlist_free(nickname_list);
1030 /** Return 1 iff any member of the (possibly NULL) comma-separated list
1031 * <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
1032 * return 0.
1035 router_nickname_is_in_list(routerinfo_t *router, const char *list)
1037 smartlist_t *nickname_list;
1038 int v = 0;
1040 if (!list)
1041 return 0; /* definitely not */
1042 tor_assert(router);
1044 nickname_list = smartlist_create();
1045 smartlist_split_string(nickname_list, list, ",",
1046 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1047 SMARTLIST_FOREACH(nickname_list, const char *, cp,
1048 if (router_nickname_matches(router, cp)) {v=1;break;});
1049 SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
1050 smartlist_free(nickname_list);
1051 return v;
1054 /** Add every suitable router from our routerlist to <b>sl</b>, so that
1055 * we can pick a node for a circuit.
1057 static void
1058 router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_invalid,
1059 int need_uptime, int need_capacity,
1060 int need_guard)
1062 if (!routerlist)
1063 return;
1065 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1067 if (router->is_running &&
1068 router->purpose == ROUTER_PURPOSE_GENERAL &&
1069 (router->is_valid || allow_invalid) &&
1070 !router_is_unreliable(router, need_uptime,
1071 need_capacity, need_guard)) {
1072 /* If it's running, and it's suitable according to the
1073 * other flags we had in mind */
1074 smartlist_add(sl, router);
1079 /** Look through the routerlist until we find a router that has my key.
1080 Return it. */
1081 routerinfo_t *
1082 routerlist_find_my_routerinfo(void)
1084 if (!routerlist)
1085 return NULL;
1087 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1089 if (router_is_me(router))
1090 return router;
1092 return NULL;
1095 /** Find a router that's up, that has this IP address, and
1096 * that allows exit to this address:port, or return NULL if there
1097 * isn't a good one.
1099 routerinfo_t *
1100 router_find_exact_exit_enclave(const char *address, uint16_t port)
1102 uint32_t addr;
1103 struct in_addr in;
1105 if (!tor_inet_aton(address, &in))
1106 return NULL; /* it's not an IP already */
1107 addr = ntohl(in.s_addr);
1109 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1111 if (router->is_running &&
1112 router->addr == addr &&
1113 compare_addr_to_addr_policy(addr, port, router->exit_policy) ==
1114 ADDR_POLICY_ACCEPTED)
1115 return router;
1117 return NULL;
1120 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1121 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1122 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1123 * bandwidth.
1124 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1127 router_is_unreliable(routerinfo_t *router, int need_uptime,
1128 int need_capacity, int need_guard)
1130 if (need_uptime && !router->is_stable)
1131 return 1;
1132 if (need_capacity && !router->is_fast)
1133 return 1;
1134 if (need_guard && !router->is_possible_guard)
1135 return 1;
1136 return 0;
1139 /** Return the smaller of the router's configured BandwidthRate
1140 * and its advertised capacity. */
1141 uint32_t
1142 router_get_advertised_bandwidth(routerinfo_t *router)
1144 if (router->bandwidthcapacity < router->bandwidthrate)
1145 return router->bandwidthcapacity;
1146 return router->bandwidthrate;
1149 /** Do not weight any declared bandwidth more than this much when picking
1150 * routers by bandwidth. */
1151 #define MAX_BELIEVABLE_BANDWIDTH 1500000 /* 1.5 MB/sec */
1153 /** Helper function:
1154 * choose a random element of smartlist <b>sl</b>, weighted by
1155 * the advertised bandwidth of each element.
1157 * If <b>statuses</b> is zero, then <b>sl</b> is a list of
1158 * routerinfo_t's. Otherwise it's a list of routerstatus_t's.
1160 * If <b>for_exit</b>, we're picking an exit node: consider all nodes'
1161 * bandwidth equally regardless of their Exit status. If not <b>for_exit</b>,
1162 * we're picking a non-exit node: weight exit-node's bandwidth less
1163 * depending on the smallness of the fraction of Exit-to-total bandwidth.
1165 static void *
1166 smartlist_choose_by_bandwidth(smartlist_t *sl, int for_exit, int statuses)
1168 int i;
1169 routerinfo_t *router;
1170 routerstatus_t *status;
1171 int32_t *bandwidths;
1172 int is_exit;
1173 uint64_t total_nonexit_bw = 0, total_exit_bw = 0, total_bw = 0;
1174 uint64_t rand_bw, tmp;
1175 double exit_weight;
1176 int n_unknown = 0;
1178 /* First count the total bandwidth weight, and make a list
1179 * of each value. <0 means "unknown; no routerinfo." We use the
1180 * bits of negative values to remember whether the router was fast (-x)&1
1181 * and whether it was an exit (-x)&2. Yes, it's a hack. */
1182 bandwidths = tor_malloc(sizeof(int32_t)*smartlist_len(sl));
1184 /* Iterate over all the routerinfo_t or routerstatus_t, and */
1185 for (i = 0; i < smartlist_len(sl); ++i) {
1186 /* first, learn what bandwidth we think i has */
1187 int is_known = 1;
1188 int32_t flags = 0;
1189 uint32_t this_bw = 0;
1190 if (statuses) {
1191 /* need to extract router info */
1192 status = smartlist_get(sl, i);
1193 router = router_get_by_digest(status->identity_digest);
1194 is_exit = status->is_exit;
1195 if (router) {
1196 this_bw = router_get_advertised_bandwidth(router);
1197 } else { /* guess */
1198 is_known = 0;
1199 flags = status->is_fast ? 1 : 0;
1200 flags |= is_exit ? 2 : 0;
1202 } else {
1203 router = smartlist_get(sl, i);
1204 is_exit = router->is_exit;
1205 this_bw = router_get_advertised_bandwidth(router);
1207 /* if they claim something huge, don't believe it */
1208 if (this_bw > MAX_BELIEVABLE_BANDWIDTH)
1209 this_bw = MAX_BELIEVABLE_BANDWIDTH;
1210 if (is_known) {
1211 bandwidths[i] = (int32_t) this_bw; // safe since MAX_BELIEVABLE<INT32_MAX
1212 if (is_exit)
1213 total_exit_bw += this_bw;
1214 else
1215 total_nonexit_bw += this_bw;
1216 } else {
1217 ++n_unknown;
1218 bandwidths[i] = -flags;
1222 /* Now, fill in the unknown values. */
1223 if (n_unknown) {
1224 int32_t avg_fast, avg_slow;
1225 if (total_exit_bw+total_nonexit_bw) {
1226 /* if there's some bandwidth, there's at least one known router,
1227 * so no worries about div by 0 here */
1228 int n_known = smartlist_len(sl)-n_unknown;
1229 avg_fast = avg_slow = (int32_t)
1230 ((total_exit_bw+total_nonexit_bw)/((uint64_t) n_known));
1231 } else {
1232 avg_fast = 40000;
1233 avg_slow = 20000;
1235 for (i=0; i<smartlist_len(sl); ++i) {
1236 int32_t bw = bandwidths[i];
1237 if (bw>=0)
1238 continue;
1239 is_exit = ((-bw)&2);
1240 bandwidths[i] = ((-bw)&1) ? avg_fast : avg_slow;
1241 if (is_exit)
1242 total_exit_bw += bandwidths[i];
1243 else
1244 total_nonexit_bw += bandwidths[i];
1248 /* If there's no bandwidth at all, pick at random. */
1249 if (!(total_exit_bw+total_nonexit_bw)) {
1250 tor_free(bandwidths);
1251 return smartlist_choose(sl);
1254 /* Figure out how to weight exits. */
1255 if (for_exit) {
1256 /* If we're choosing an exit node, exit bandwidth counts fully. */
1257 exit_weight = 1.0;
1258 total_bw = total_exit_bw + total_nonexit_bw;
1259 } else if (total_exit_bw < total_nonexit_bw / 2) {
1260 /* If we're choosing a relay and exits are greatly outnumbered, ignore
1261 * them. */
1262 exit_weight = 0.0;
1263 total_bw = total_nonexit_bw;
1264 } else {
1265 /* If we're choosing a relay and exits aren't outnumbered use the formula
1266 * from path-spec. */
1267 uint64_t leftover = (total_exit_bw - total_nonexit_bw / 2);
1268 exit_weight = U64_TO_DBL(leftover) /
1269 U64_TO_DBL(leftover + total_nonexit_bw);
1270 total_bw = total_nonexit_bw +
1271 DBL_TO_U64(exit_weight * U64_TO_DBL(total_exit_bw));
1274 log_debug(LD_CIRC, "Total bw = "U64_FORMAT", total exit bw = "U64_FORMAT
1275 ", total nonexit bw = "U64_FORMAT", exit weight = %lf "
1276 "(for exit == %d)",
1277 U64_PRINTF_ARG(total_bw), U64_PRINTF_ARG(total_exit_bw),
1278 U64_PRINTF_ARG(total_nonexit_bw), exit_weight, for_exit);
1281 /* Almost done: choose a random value from the bandwidth weights. */
1282 rand_bw = crypto_rand_uint64(total_bw);
1284 /* Last, count through sl until we get to the element we picked */
1285 tmp = 0;
1286 for (i=0; i < smartlist_len(sl); i++) {
1287 if (statuses) {
1288 status = smartlist_get(sl, i);
1289 is_exit = status->is_exit;
1290 } else {
1291 router = smartlist_get(sl, i);
1292 is_exit = router->is_exit;
1294 if (is_exit)
1295 tmp += ((uint64_t)(bandwidths[i] * exit_weight));
1296 else
1297 tmp += bandwidths[i];
1298 if (tmp >= rand_bw)
1299 break;
1301 if (i == smartlist_len(sl)) {
1302 /* This is possible due to round-off error. */
1303 --i;
1304 log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
1305 " which router we chose. Please tell the developers.");
1307 tor_free(bandwidths);
1308 return smartlist_get(sl, i);
1311 /** Choose a random element of router list <b>sl</b>, weighted by
1312 * the advertised bandwidth of each router.
1314 routerinfo_t *
1315 routerlist_sl_choose_by_bandwidth(smartlist_t *sl, int for_exit)
1317 return smartlist_choose_by_bandwidth(sl, for_exit, 0);
1320 /** Choose a random element of status list <b>sl</b>, weighted by
1321 * the advertised bandwidth of each status.
1323 routerstatus_t *
1324 routerstatus_sl_choose_by_bandwidth(smartlist_t *sl)
1326 return smartlist_choose_by_bandwidth(sl, 1, 1);
1329 /** Return a random running router from the routerlist. If any node
1330 * named in <b>preferred</b> is available, pick one of those. Never
1331 * pick a node named in <b>excluded</b>, or whose routerinfo is in
1332 * <b>excludedsmartlist</b>, even if they are the only nodes
1333 * available. If <b>strict</b> is true, never pick any node besides
1334 * those in <b>preferred</b>.
1335 * If <b>need_uptime</b> is non-zero and any router has more than
1336 * a minimum uptime, return one of those.
1337 * If <b>need_capacity</b> is non-zero, weight your choice by the
1338 * advertised capacity of each router.
1339 * If ! <b>allow_invalid</b>, consider only Valid routers.
1340 * If <b>need_guard</b>, consider only Guard routers.
1341 * If <b>weight_for_exit</b>, we weight bandwidths as if picking an exit node,
1342 * otherwise we weight bandwidths for picking a relay node (that is, possibly
1343 * discounting exit nodes).
1345 routerinfo_t *
1346 router_choose_random_node(const char *preferred,
1347 const char *excluded,
1348 smartlist_t *excludedsmartlist,
1349 int need_uptime, int need_capacity,
1350 int need_guard,
1351 int allow_invalid, int strict,
1352 int weight_for_exit)
1354 smartlist_t *sl, *excludednodes;
1355 routerinfo_t *choice = NULL;
1357 excludednodes = smartlist_create();
1358 add_nickname_list_to_smartlist(excludednodes,excluded,0);
1360 /* Try the preferred nodes first. Ignore need_uptime and need_capacity
1361 * and need_guard, since the user explicitly asked for these nodes. */
1362 if (preferred) {
1363 sl = smartlist_create();
1364 add_nickname_list_to_smartlist(sl,preferred,1);
1365 smartlist_subtract(sl,excludednodes);
1366 if (excludedsmartlist)
1367 smartlist_subtract(sl,excludedsmartlist);
1368 choice = smartlist_choose(sl);
1369 smartlist_free(sl);
1371 if (!choice && !strict) {
1372 /* Then give up on our preferred choices: any node
1373 * will do that has the required attributes. */
1374 sl = smartlist_create();
1375 router_add_running_routers_to_smartlist(sl, allow_invalid,
1376 need_uptime, need_capacity,
1377 need_guard);
1378 smartlist_subtract(sl,excludednodes);
1379 if (excludedsmartlist)
1380 smartlist_subtract(sl,excludedsmartlist);
1382 if (need_capacity || need_guard)
1383 choice = routerlist_sl_choose_by_bandwidth(sl, weight_for_exit);
1384 else
1385 choice = smartlist_choose(sl);
1387 smartlist_free(sl);
1388 if (!choice && (need_uptime || need_capacity || need_guard)) {
1389 /* try once more -- recurse but with fewer restrictions. */
1390 log_info(LD_CIRC,
1391 "We couldn't find any live%s%s%s routers; falling back "
1392 "to list of all routers.",
1393 need_capacity?", fast":"",
1394 need_uptime?", stable":"",
1395 need_guard?", guard":"");
1396 choice = router_choose_random_node(
1397 NULL, excluded, excludedsmartlist,
1398 0, 0, 0, allow_invalid, 0, weight_for_exit);
1401 smartlist_free(excludednodes);
1402 if (!choice) {
1403 if (strict) {
1404 log_warn(LD_CIRC, "All preferred nodes were down when trying to choose "
1405 "node, and the Strict[...]Nodes option is set. Failing.");
1406 } else {
1407 log_warn(LD_CIRC,
1408 "No available nodes when trying to choose node. Failing.");
1411 return choice;
1414 /** Return true iff the digest of <b>router</b>'s identity key,
1415 * encoded in hexadecimal, matches <b>hexdigest</b> (which is
1416 * optionally prefixed with a single dollar sign). Return false if
1417 * <b>hexdigest</b> is malformed, or it doesn't match. */
1418 static INLINE int
1419 router_hex_digest_matches(routerinfo_t *router, const char *hexdigest)
1421 char digest[DIGEST_LEN];
1422 size_t len;
1423 tor_assert(hexdigest);
1424 if (hexdigest[0] == '$')
1425 ++hexdigest;
1427 len = strlen(hexdigest);
1428 if (len < HEX_DIGEST_LEN)
1429 return 0;
1430 else if (len > HEX_DIGEST_LEN &&
1431 (hexdigest[HEX_DIGEST_LEN] == '=' ||
1432 hexdigest[HEX_DIGEST_LEN] == '~')) {
1433 if (strcasecmp(hexdigest+HEX_DIGEST_LEN+1, router->nickname))
1434 return 0;
1435 if (hexdigest[HEX_DIGEST_LEN] == '=' && !router->is_named)
1436 return 0;
1439 if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
1440 return 0;
1441 return (!memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN));
1444 /** Return true if <b>router</b>'s nickname matches <b>nickname</b>
1445 * (case-insensitive), or if <b>router's</b> identity key digest
1446 * matches a hexadecimal value stored in <b>nickname</b>. Return
1447 * false otherwise. */
1448 static int
1449 router_nickname_matches(routerinfo_t *router, const char *nickname)
1451 if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
1452 return 1;
1453 return router_hex_digest_matches(router, nickname);
1456 /** Return the router in our routerlist whose (case-insensitive)
1457 * nickname or (case-sensitive) hexadecimal key digest is
1458 * <b>nickname</b>. Return NULL if no such router is known.
1460 routerinfo_t *
1461 router_get_by_nickname(const char *nickname, int warn_if_unnamed)
1463 int maybedigest;
1464 char digest[DIGEST_LEN];
1465 routerinfo_t *best_match=NULL;
1466 int n_matches = 0;
1467 char *named_digest = NULL;
1469 tor_assert(nickname);
1470 if (!routerlist)
1471 return NULL;
1472 if (nickname[0] == '$')
1473 return router_get_by_hexdigest(nickname);
1474 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
1475 return NULL;
1476 if (server_mode(get_options()) &&
1477 !strcasecmp(nickname, get_options()->Nickname))
1478 return router_get_my_routerinfo();
1480 maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
1481 (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
1483 if (named_server_map &&
1484 (named_digest = strmap_get_lc(named_server_map, nickname))) {
1485 return rimap_get(routerlist->identity_map, named_digest);
1488 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1490 if (!strcasecmp(router->nickname, nickname)) {
1491 ++n_matches;
1492 if (n_matches <= 1 || router->is_running)
1493 best_match = router;
1494 } else if (maybedigest &&
1495 !memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN)
1497 if (router_hex_digest_matches(router, nickname))
1498 return router;
1499 else
1500 best_match = router; // XXXX020 NM not exactly right.
1504 if (best_match) {
1505 if (warn_if_unnamed && n_matches > 1) {
1506 smartlist_t *fps = smartlist_create();
1507 int any_unwarned = 0;
1508 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1510 local_routerstatus_t *rs;
1511 char *desc;
1512 size_t dlen;
1513 char fp[HEX_DIGEST_LEN+1];
1514 if (strcasecmp(router->nickname, nickname))
1515 continue;
1516 rs = router_get_combined_status_by_digest(
1517 router->cache_info.identity_digest);
1518 if (rs && !rs->name_lookup_warned) {
1519 rs->name_lookup_warned = 1;
1520 any_unwarned = 1;
1522 base16_encode(fp, sizeof(fp),
1523 router->cache_info.identity_digest, DIGEST_LEN);
1524 dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
1525 desc = tor_malloc(dlen);
1526 tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
1527 fp, router->address, router->or_port);
1528 smartlist_add(fps, desc);
1530 if (any_unwarned) {
1531 char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
1532 log_warn(LD_CONFIG,
1533 "There are multiple matches for the nickname \"%s\","
1534 " but none is listed as named by the directory authorities. "
1535 "Choosing one arbitrarily. If you meant one in particular, "
1536 "you should say %s.", nickname, alternatives);
1537 tor_free(alternatives);
1539 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
1540 smartlist_free(fps);
1541 } else if (warn_if_unnamed) {
1542 local_routerstatus_t *rs = router_get_combined_status_by_digest(
1543 best_match->cache_info.identity_digest);
1544 if (rs && !rs->name_lookup_warned) {
1545 char fp[HEX_DIGEST_LEN+1];
1546 base16_encode(fp, sizeof(fp),
1547 best_match->cache_info.identity_digest, DIGEST_LEN);
1548 log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
1549 "name is not registered, so it could be used by any server, "
1550 "not just the one you meant. "
1551 "To make sure you get the same server in the future, refer to "
1552 "it by key, as \"$%s\".", nickname, fp);
1553 rs->name_lookup_warned = 1;
1556 return best_match;
1559 return NULL;
1562 /** Try to find a routerinfo for <b>digest</b>. If we don't have one,
1563 * return 1. If we do, ask tor_version_as_new_as() for the answer.
1566 router_digest_version_as_new_as(const char *digest, const char *cutoff)
1568 routerinfo_t *router = router_get_by_digest(digest);
1569 if (!router)
1570 return 1;
1571 return tor_version_as_new_as(router->platform, cutoff);
1574 /** Return true iff <b>digest</b> is the digest of the identity key of
1575 * a trusted directory. */
1577 router_digest_is_trusted_dir(const char *digest)
1579 if (!trusted_dir_servers)
1580 return 0;
1581 if (authdir_mode(get_options()) && router_digest_is_me(digest))
1582 return 1;
1583 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
1584 if (!memcmp(digest, ent->digest, DIGEST_LEN)) return 1);
1585 return 0;
1588 /** If hexdigest is correctly formed, base16_decode it into
1589 * digest, which must have DIGEST_LEN space in it.
1590 * Return 0 on success, -1 on failure.
1593 hexdigest_to_digest(const char *hexdigest, char *digest)
1595 if (hexdigest[0]=='$')
1596 ++hexdigest;
1597 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
1598 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
1599 return -1;
1600 return 0;
1603 /** Return the router in our routerlist whose hexadecimal key digest
1604 * is <b>hexdigest</b>. Return NULL if no such router is known. */
1605 routerinfo_t *
1606 router_get_by_hexdigest(const char *hexdigest)
1608 char digest[DIGEST_LEN];
1609 size_t len;
1610 routerinfo_t *ri;
1612 tor_assert(hexdigest);
1613 if (!routerlist)
1614 return NULL;
1615 if (hexdigest[0]=='$')
1616 ++hexdigest;
1617 len = strlen(hexdigest);
1618 if (hexdigest_to_digest(hexdigest, digest) < 0)
1619 return NULL;
1621 ri = router_get_by_digest(digest);
1623 if (len > HEX_DIGEST_LEN) {
1624 if (hexdigest[HEX_DIGEST_LEN] == '=') {
1625 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1) ||
1626 !ri->is_named)
1627 return NULL;
1628 } else if (hexdigest[HEX_DIGEST_LEN] == '~') {
1629 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1))
1630 return NULL;
1631 } else {
1632 return NULL;
1636 return ri;
1639 /** Return the router in our routerlist whose 20-byte key digest
1640 * is <b>digest</b>. Return NULL if no such router is known. */
1641 routerinfo_t *
1642 router_get_by_digest(const char *digest)
1644 tor_assert(digest);
1646 if (!routerlist) return NULL;
1648 // routerlist_assert_ok(routerlist);
1650 return rimap_get(routerlist->identity_map, digest);
1653 /** Return the router in our routerlist whose 20-byte descriptor
1654 * is <b>digest</b>. Return NULL if no such router is known. */
1655 signed_descriptor_t *
1656 router_get_by_descriptor_digest(const char *digest)
1658 tor_assert(digest);
1660 if (!routerlist) return NULL;
1662 return sdmap_get(routerlist->desc_digest_map, digest);
1665 /** Return the signed descriptor for the router in our routerlist whose
1666 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
1667 * is known. */
1668 signed_descriptor_t *
1669 router_get_by_extrainfo_digest(const char *digest)
1671 tor_assert(digest);
1673 if (!routerlist) return NULL;
1675 return sdmap_get(routerlist->desc_by_eid_map, digest);
1678 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
1679 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
1680 * document is known. */
1681 signed_descriptor_t *
1682 extrainfo_get_by_descriptor_digest(const char *digest)
1684 extrainfo_t *ei;
1685 tor_assert(digest);
1686 if (!routerlist) return NULL;
1687 ei = eimap_get(routerlist->extra_info_map, digest);
1688 return ei ? &ei->cache_info : NULL;
1691 /** Return a pointer to the signed textual representation of a descriptor.
1692 * The returned string is not guaranteed to be NUL-terminated: the string's
1693 * length will be in desc-\>signed_descriptor_len. */
1694 const char *
1695 signed_descriptor_get_body(signed_descriptor_t *desc)
1697 const char *r = NULL;
1698 size_t len = desc->signed_descriptor_len;
1699 tor_mmap_t *mmap;
1700 tor_assert(len > 32);
1701 if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
1702 if (desc->is_extrainfo)
1703 mmap = routerlist->mmap_extrainfo;
1704 else
1705 mmap = routerlist->mmap_descriptors;
1706 if (mmap) {
1707 tor_assert(desc->saved_offset + len <= mmap->size);
1708 r = mmap->data + desc->saved_offset;
1711 if (!r) /* no mmap, or not in cache. */
1712 r = desc->signed_descriptor_body;
1714 tor_assert(r);
1715 if (memcmp("router ", r, 7) && memcmp("extra-info ", r, 11)) {
1716 log_err(LD_DIR, "descriptor at %p begins with unexpected string %s",
1717 desc, tor_strndup(r, 64));
1719 tor_assert(!memcmp("router ", r, 7) || !memcmp("extra-info ", r, 11));
1721 return r;
1724 /** Return the current list of all known routers. */
1725 routerlist_t *
1726 router_get_routerlist(void)
1728 if (!routerlist) {
1729 routerlist = tor_malloc_zero(sizeof(routerlist_t));
1730 routerlist->routers = smartlist_create();
1731 routerlist->old_routers = smartlist_create();
1732 routerlist->identity_map = rimap_new();
1733 routerlist->desc_digest_map = sdmap_new();
1734 routerlist->desc_by_eid_map = sdmap_new();
1735 routerlist->extra_info_map = eimap_new();
1737 return routerlist;
1740 /** Free all storage held by <b>router</b>. */
1741 void
1742 routerinfo_free(routerinfo_t *router)
1744 if (!router)
1745 return;
1747 tor_free(router->cache_info.signed_descriptor_body);
1748 tor_free(router->address);
1749 tor_free(router->nickname);
1750 tor_free(router->platform);
1751 tor_free(router->contact_info);
1752 if (router->onion_pkey)
1753 crypto_free_pk_env(router->onion_pkey);
1754 if (router->identity_pkey)
1755 crypto_free_pk_env(router->identity_pkey);
1756 if (router->declared_family) {
1757 SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
1758 smartlist_free(router->declared_family);
1760 addr_policy_free(router->exit_policy);
1762 /* Remove once 414/417 is fixed. But I have a hunch... */
1763 memset(router, 77, sizeof(routerinfo_t));
1765 tor_free(router);
1768 /** Release all storage held by <b>extrainfo</b> */
1769 void
1770 extrainfo_free(extrainfo_t *extrainfo)
1772 if (!extrainfo)
1773 return;
1774 tor_free(extrainfo->cache_info.signed_descriptor_body);
1775 tor_free(extrainfo->pending_sig);
1777 /* XXXX020 remove this once more bugs go away. */
1778 memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
1779 tor_free(extrainfo);
1782 /** Release storage held by <b>sd</b>. */
1783 static void
1784 signed_descriptor_free(signed_descriptor_t *sd)
1786 tor_free(sd->signed_descriptor_body);
1788 /* XXXX020 remove this once more bugs go away. */
1789 memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
1790 tor_free(sd);
1793 /** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
1795 static signed_descriptor_t *
1796 signed_descriptor_from_routerinfo(routerinfo_t *ri)
1798 signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t));
1799 memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t));
1800 ri->cache_info.signed_descriptor_body = NULL;
1801 routerinfo_free(ri);
1802 return sd;
1805 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
1806 static void
1807 _extrainfo_free(void *e)
1809 extrainfo_free(e);
1812 /** Free all storage held by a routerlist <b>rl</b>. */
1813 void
1814 routerlist_free(routerlist_t *rl)
1816 tor_assert(rl);
1817 rimap_free(rl->identity_map, NULL);
1818 sdmap_free(rl->desc_digest_map, NULL);
1819 sdmap_free(rl->desc_by_eid_map, NULL);
1820 eimap_free(rl->extra_info_map, _extrainfo_free);
1821 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
1822 routerinfo_free(r));
1823 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
1824 signed_descriptor_free(sd));
1825 smartlist_free(rl->routers);
1826 smartlist_free(rl->old_routers);
1827 if (routerlist->mmap_descriptors)
1828 tor_munmap_file(routerlist->mmap_descriptors);
1829 tor_free(rl);
1831 router_dir_info_changed();
1834 void
1835 dump_routerlist_mem_usage(int severity)
1837 uint64_t livedescs = 0;
1838 uint64_t olddescs = 0;
1839 if (!routerlist)
1840 return;
1841 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
1842 livedescs += r->cache_info.signed_descriptor_len);
1843 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
1844 olddescs += sd->signed_descriptor_len);
1846 log(severity, LD_GENERAL,
1847 "In %d live descriptors: "U64_FORMAT" bytes. "
1848 "In %d old descriptors: "U64_FORMAT" bytes.",
1849 smartlist_len(routerlist->routers), U64_PRINTF_ARG(livedescs),
1850 smartlist_len(routerlist->old_routers), U64_PRINTF_ARG(olddescs));
1853 /** Return the greatest number of routerdescs we'll hold for any given router.
1855 static int
1856 max_descriptors_per_router(void)
1858 int n_authorities = get_n_v2_authorities();
1859 return (n_authorities < 5) ? 5 : n_authorities;
1862 /** Return non-zero if we have a lot of extra descriptors in our
1863 * routerlist, and should get rid of some of them. Else return 0.
1865 * We should be careful to not return true too eagerly, since we
1866 * could churn. By using "+1" below, we make sure this function
1867 * only returns true at most every smartlist_len(rl-\>routers)
1868 * new descriptors.
1870 static INLINE int
1871 routerlist_is_overfull(routerlist_t *rl)
1873 return smartlist_len(rl->old_routers) >
1874 smartlist_len(rl->routers)*(max_descriptors_per_router()+1);
1877 static INLINE int
1878 _routerlist_find_elt(smartlist_t *sl, void *ri, int idx)
1880 if (idx < 0) {
1881 idx = -1;
1882 SMARTLIST_FOREACH(sl, routerinfo_t *, r,
1883 if (r == ri) {
1884 idx = r_sl_idx;
1885 break;
1887 } else {
1888 tor_assert(idx < smartlist_len(sl));
1889 tor_assert(smartlist_get(sl, idx) == ri);
1891 return idx;
1894 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
1895 * as needed. There must be no previous member of <b>rl</b> with the same
1896 * identity digest as <b>ri</b>: If there is, call routerlist_replace
1897 * instead.
1899 static void
1900 routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
1902 routerinfo_t *ri_old;
1903 routerlist_check_bug_417();
1905 /* XXXX020 remove this code once bug 417/404 is fixed. */
1906 routerinfo_t *ri_generated = router_get_my_routerinfo();
1907 tor_assert(ri_generated != ri);
1909 tor_assert(ri->routerlist_index == -1);
1911 ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
1912 tor_assert(!ri_old);
1913 sdmap_set(rl->desc_digest_map, ri->cache_info.signed_descriptor_digest,
1914 &(ri->cache_info));
1915 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1916 sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
1917 &ri->cache_info);
1918 smartlist_add(rl->routers, ri);
1919 ri->routerlist_index = smartlist_len(rl->routers) - 1;
1920 router_dir_info_changed();
1921 routerlist_check_bug_417();
1922 #ifdef DEBUG_ROUTERLIST
1923 routerlist_assert_ok(rl);
1924 #endif
1927 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
1928 * corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
1929 * we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
1930 static int
1931 extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
1933 int r = 0;
1934 routerinfo_t *ri = rimap_get(rl->identity_map,
1935 ei->cache_info.identity_digest);
1936 signed_descriptor_t *sd;
1937 extrainfo_t *ei_tmp;
1938 routerlist_check_bug_417();
1941 /* XXXX020 remove this code once bug 417/404 is fixed. */
1942 extrainfo_t *ei_generated = router_get_my_extrainfo();
1943 tor_assert(ei_generated != ei);
1946 if (!ri) {
1947 /* This router is unknown; we can't even verify the signature. Give up.*/
1948 goto done;
1950 if (routerinfo_incompatible_with_extrainfo(ri, ei, NULL)) {
1951 if (ei->bad_sig) /* If the signature didn't check, it's just wrong. */
1952 goto done;
1953 sd = sdmap_get(rl->desc_by_eid_map,
1954 ei->cache_info.signed_descriptor_digest);
1955 if (!sd ||
1956 memcmp(sd->identity_digest, ei->cache_info.identity_digest,
1957 DIGEST_LEN) ||
1958 sd->published_on != ei->cache_info.published_on)
1959 goto done;
1962 /* Okay, if we make it here, we definitely have a router corresponding to
1963 * this extrainfo. */
1965 ei_tmp = eimap_set(rl->extra_info_map,
1966 ei->cache_info.signed_descriptor_digest,
1967 ei);
1968 r = 1;
1969 if (ei_tmp) {
1970 extrainfo_store_stats.bytes_dropped +=
1971 ei_tmp->cache_info.signed_descriptor_len;
1972 extrainfo_free(ei_tmp);
1975 done:
1976 if (r == 0)
1977 extrainfo_free(ei);
1979 routerlist_check_bug_417();
1980 #ifdef DEBUG_ROUTERLIST
1981 routerlist_assert_ok(rl);
1982 #endif
1983 return r;
1986 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
1987 * a copy of router <b>ri</b> yet, add it to the list of old (not
1988 * recommended but still served) descriptors. Else free it. */
1989 static void
1990 routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
1992 routerlist_check_bug_417();
1994 /* XXXX020 remove this code once bug 417/404 is fixed. */
1995 routerinfo_t *ri_generated = router_get_my_routerinfo();
1996 tor_assert(ri_generated != ri);
1998 tor_assert(ri->routerlist_index == -1);
2000 if (get_options()->DirPort &&
2001 ri->purpose == ROUTER_PURPOSE_GENERAL &&
2002 !sdmap_get(rl->desc_digest_map,
2003 ri->cache_info.signed_descriptor_digest)) {
2004 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri);
2005 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2006 smartlist_add(rl->old_routers, sd);
2007 if (!tor_digest_is_zero(sd->extra_info_digest))
2008 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2009 } else {
2010 routerinfo_free(ri);
2012 routerlist_check_bug_417();
2013 #ifdef DEBUG_ROUTERLIST
2014 routerlist_assert_ok(rl);
2015 #endif
2018 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
2019 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl-&gt;routers,
2020 * idx) == ri, we don't need to do a linear search over the list to decide
2021 * which to remove. We fill the gap in rl-&gt;routers with a later element in
2022 * the list, if any exists. <b>ri</b> is freed.
2024 * If <b>make_old</b> is true, instead of deleting the router, we try adding
2025 * it to rl-&gt;old_routers. */
2026 void
2027 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old)
2029 routerinfo_t *ri_tmp;
2030 extrainfo_t *ei_tmp;
2031 int idx = ri->routerlist_index;
2032 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2033 tor_assert(smartlist_get(rl->routers, idx) == ri);
2035 routerlist_check_bug_417();
2036 ri->routerlist_index = -1;
2037 smartlist_del(rl->routers, idx);
2038 if (idx < smartlist_len(rl->routers)) {
2039 routerinfo_t *r = smartlist_get(rl->routers, idx);
2040 r->routerlist_index = idx;
2043 ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
2044 router_dir_info_changed();
2045 tor_assert(ri_tmp == ri);
2047 if (make_old && get_options()->DirPort &&
2048 ri->purpose == ROUTER_PURPOSE_GENERAL) {
2049 signed_descriptor_t *sd;
2050 sd = signed_descriptor_from_routerinfo(ri);
2051 smartlist_add(rl->old_routers, sd);
2052 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2053 if (!tor_digest_is_zero(sd->extra_info_digest))
2054 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2055 } else {
2056 signed_descriptor_t *sd_tmp;
2057 sd_tmp = sdmap_remove(rl->desc_digest_map,
2058 ri->cache_info.signed_descriptor_digest);
2059 tor_assert(sd_tmp == &(ri->cache_info));
2060 router_store_stats.bytes_dropped += ri->cache_info.signed_descriptor_len;
2061 ei_tmp = eimap_remove(rl->extra_info_map,
2062 ri->cache_info.extra_info_digest);
2063 if (ei_tmp) {
2064 extrainfo_store_stats.bytes_dropped +=
2065 ei_tmp->cache_info.signed_descriptor_len;
2066 extrainfo_free(ei_tmp);
2068 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
2069 sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
2070 routerinfo_free(ri);
2072 routerlist_check_bug_417();
2073 #ifdef DEBUG_ROUTERLIST
2074 routerlist_assert_ok(rl);
2075 #endif
2078 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
2079 * adjust <b>rl</b> as appropriate. <b>idx</i> is -1, or the index of
2080 * <b>sd</b>. */
2081 static void
2082 routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
2084 signed_descriptor_t *sd_tmp;
2085 extrainfo_t *ei_tmp;
2086 routerlist_check_bug_417();
2087 tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
2088 tor_assert(smartlist_get(rl->old_routers, idx) == sd);
2090 smartlist_del(rl->old_routers, idx);
2091 sd_tmp = sdmap_remove(rl->desc_digest_map,
2092 sd->signed_descriptor_digest);
2093 tor_assert(sd_tmp == sd);
2094 router_store_stats.bytes_dropped += sd->signed_descriptor_len;
2096 ei_tmp = eimap_remove(rl->extra_info_map,
2097 sd->extra_info_digest);
2098 if (ei_tmp) {
2099 extrainfo_store_stats.bytes_dropped +=
2100 ei_tmp->cache_info.signed_descriptor_len;
2101 extrainfo_free(ei_tmp);
2103 if (!tor_digest_is_zero(sd->extra_info_digest))
2104 sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
2106 signed_descriptor_free(sd);
2107 routerlist_check_bug_417();
2108 #ifdef DEBUG_ROUTERLIST
2109 routerlist_assert_ok(rl);
2110 #endif
2113 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
2114 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
2115 * smartlist_get(rl-&gt;routers, idx) == ri, we don't need to do a linear
2116 * search over the list to decide which to remove. We put ri_new in the same
2117 * index as ri_old, if possible. ri is freed as appropriate.
2119 * If <b>make_old</b> is true, instead of deleting the router, we try adding
2120 * it to rl-&gt;old_routers. */
2121 static void
2122 routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old,
2123 routerinfo_t *ri_new)
2125 int idx;
2127 routerinfo_t *ri_tmp;
2128 extrainfo_t *ei_tmp;
2129 routerlist_check_bug_417();
2131 /* XXXX020 remove this code once bug 417/404 is fixed. */
2132 routerinfo_t *ri_generated = router_get_my_routerinfo();
2133 tor_assert(ri_generated != ri_new);
2135 tor_assert(ri_old != ri_new);
2136 tor_assert(ri_new->routerlist_index == -1);
2138 idx = ri_old->routerlist_index;
2139 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2140 tor_assert(smartlist_get(rl->routers, idx) == ri_old);
2142 router_dir_info_changed();
2143 if (idx >= 0) {
2144 smartlist_set(rl->routers, idx, ri_new);
2145 ri_old->routerlist_index = -1;
2146 ri_new->routerlist_index = idx;
2147 /* Check that ri_old is not in rl->routers anymore: */
2148 tor_assert( _routerlist_find_elt(rl->routers, ri_old, -1) == -1 );
2149 } else {
2150 log_warn(LD_BUG, "Appending entry from routerlist_replace.");
2151 routerlist_insert(rl, ri_new);
2152 return;
2154 if (memcmp(ri_old->cache_info.identity_digest,
2155 ri_new->cache_info.identity_digest, DIGEST_LEN)) {
2156 /* digests don't match; digestmap_set won't replace */
2157 rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
2159 ri_tmp = rimap_set(rl->identity_map,
2160 ri_new->cache_info.identity_digest, ri_new);
2161 tor_assert(!ri_tmp || ri_tmp == ri_old);
2162 sdmap_set(rl->desc_digest_map,
2163 ri_new->cache_info.signed_descriptor_digest,
2164 &(ri_new->cache_info));
2166 if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
2167 sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
2168 &ri_new->cache_info);
2171 if (get_options()->DirPort &&
2172 ri_old->purpose == ROUTER_PURPOSE_GENERAL) {
2173 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
2174 smartlist_add(rl->old_routers, sd);
2175 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2176 if (!tor_digest_is_zero(sd->extra_info_digest))
2177 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2178 } else {
2179 if (memcmp(ri_old->cache_info.signed_descriptor_digest,
2180 ri_new->cache_info.signed_descriptor_digest,
2181 DIGEST_LEN)) {
2182 /* digests don't match; digestmap_set didn't replace */
2183 sdmap_remove(rl->desc_digest_map,
2184 ri_old->cache_info.signed_descriptor_digest);
2187 ei_tmp = eimap_remove(rl->extra_info_map,
2188 ri_old->cache_info.extra_info_digest);
2189 if (ei_tmp) {
2190 extrainfo_store_stats.bytes_dropped +=
2191 ei_tmp->cache_info.signed_descriptor_len;
2192 extrainfo_free(ei_tmp);
2194 if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
2195 sdmap_remove(rl->desc_by_eid_map,
2196 ri_old->cache_info.extra_info_digest);
2198 router_store_stats.bytes_dropped +=
2199 ri_old->cache_info.signed_descriptor_len;
2200 routerinfo_free(ri_old);
2202 routerlist_check_bug_417();
2203 #ifdef DEBUG_ROUTERLIST
2204 routerlist_assert_ok(rl);
2205 #endif
2208 /** Free all memory held by the routerlist module. */
2209 void
2210 routerlist_free_all(void)
2212 if (routerlist)
2213 routerlist_free(routerlist);
2214 routerlist = NULL;
2215 if (warned_nicknames) {
2216 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
2217 smartlist_free(warned_nicknames);
2218 warned_nicknames = NULL;
2220 if (warned_conflicts) {
2221 SMARTLIST_FOREACH(warned_conflicts, char *, cp, tor_free(cp));
2222 smartlist_free(warned_conflicts);
2223 warned_conflicts = NULL;
2225 if (trusted_dir_servers) {
2226 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
2227 trusted_dir_server_free(ds));
2228 smartlist_free(trusted_dir_servers);
2229 trusted_dir_servers = NULL;
2231 if (networkstatus_list) {
2232 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
2233 networkstatus_free(ns));
2234 smartlist_free(networkstatus_list);
2235 networkstatus_list = NULL;
2237 if (routerstatus_list) {
2238 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
2239 local_routerstatus_free(rs));
2240 smartlist_free(routerstatus_list);
2241 routerstatus_list = NULL;
2243 if (routerstatus_by_desc_digest_map) {
2244 digestmap_free(routerstatus_by_desc_digest_map, NULL);
2245 routerstatus_by_desc_digest_map = NULL;
2247 if (named_server_map) {
2248 strmap_free(named_server_map, _tor_free);
2252 /** Free all storage held by the routerstatus object <b>rs</b>. */
2253 void
2254 routerstatus_free(routerstatus_t *rs)
2256 tor_free(rs);
2259 /** Free all storage held by the local_routerstatus object <b>rs</b>. */
2260 static void
2261 local_routerstatus_free(local_routerstatus_t *rs)
2263 tor_free(rs);
2266 /** Free all storage held by the networkstatus object <b>ns</b>. */
2267 void
2268 networkstatus_free(networkstatus_t *ns)
2270 tor_free(ns->source_address);
2271 tor_free(ns->contact);
2272 if (ns->signing_key)
2273 crypto_free_pk_env(ns->signing_key);
2274 tor_free(ns->client_versions);
2275 tor_free(ns->server_versions);
2276 if (ns->entries) {
2277 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
2278 routerstatus_free(rs));
2279 smartlist_free(ns->entries);
2281 tor_free(ns);
2284 /** Forget that we have issued any router-related warnings, so that we'll
2285 * warn again if we see the same errors. */
2286 void
2287 routerlist_reset_warnings(void)
2289 if (!warned_nicknames)
2290 warned_nicknames = smartlist_create();
2291 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
2292 smartlist_clear(warned_nicknames); /* now the list is empty. */
2294 if (!warned_conflicts)
2295 warned_conflicts = smartlist_create();
2296 SMARTLIST_FOREACH(warned_conflicts, char *, cp, tor_free(cp));
2297 smartlist_clear(warned_conflicts); /* now the list is empty. */
2299 if (!routerstatus_list)
2300 routerstatus_list = smartlist_create();
2301 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
2302 rs->name_lookup_warned = 0);
2304 have_warned_about_invalid_status = 0;
2305 have_warned_about_old_version = 0;
2306 have_warned_about_new_version = 0;
2309 /** Mark the router with ID <b>digest</b> as running or non-running
2310 * in our routerlist. */
2311 void
2312 router_set_status(const char *digest, int up)
2314 routerinfo_t *router;
2315 local_routerstatus_t *status;
2316 tor_assert(digest);
2318 routerlist_check_bug_417();
2319 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
2320 if (!memcmp(d->digest, digest, DIGEST_LEN))
2321 d->is_running = up);
2323 router = router_get_by_digest(digest);
2324 if (router) {
2325 log_debug(LD_DIR,"Marking router '%s' as %s.",
2326 router->nickname, up ? "up" : "down");
2327 if (!up && router_is_me(router) && !we_are_hibernating())
2328 log_warn(LD_NET, "We just marked ourself as down. Are your external "
2329 "addresses reachable?");
2330 router->is_running = up;
2332 status = router_get_combined_status_by_digest(digest);
2333 if (status && status->status.is_running != up) {
2334 status->status.is_running = up;
2335 control_event_networkstatus_changed_single(status);
2337 router_dir_info_changed();
2338 routerlist_check_bug_417();
2341 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
2342 * older entries (if any) with the same key. Note: Callers should not hold
2343 * their pointers to <b>router</b> if this function fails; <b>router</b>
2344 * will either be inserted into the routerlist or freed. Similarly, even
2345 * if this call succeeds, they should not hold their pointers to
2346 * <b>router</b> after subsequent calls with other routerinfo's -- they
2347 * might cause the original routerinfo to get freed.
2349 * Returns >= 0 if the router was added; less than 0 if it was not.
2351 * If we're returning non-zero, then assign to *<b>msg</b> a static string
2352 * describing the reason for not liking the routerinfo.
2354 * If the return value is less than -1, there was a problem with the
2355 * routerinfo. If the return value is equal to -1, then the routerinfo was
2356 * fine, but out-of-date. If the return value is equal to 1, the
2357 * routerinfo was accepted, but we should notify the generator of the
2358 * descriptor using the message *<b>msg</b>.
2360 * If <b>from_cache</b>, this descriptor came from our disk cache. If
2361 * <b>from_fetch</b>, we received it in response to a request we made.
2362 * (If both are false, that means it was uploaded to us as an auth dir
2363 * server or via the controller.)
2365 * This function should be called *after*
2366 * routers_update_status_from_networkstatus; subsequently, you should call
2367 * router_rebuild_store and routerlist_descriptors_added.
2370 router_add_to_routerlist(routerinfo_t *router, const char **msg,
2371 int from_cache, int from_fetch)
2373 const char *id_digest;
2374 int authdir = authdir_mode(get_options());
2375 int authdir_believes_valid = 0;
2376 routerinfo_t *old_router;
2377 /* router_have_minimum_dir_info() has side effects, so do it before we
2378 * start the real work */
2379 int authdir_may_warn_about_unreachable_server =
2380 authdir && !from_cache && !from_fetch &&
2381 router_have_minimum_dir_info();
2383 routerlist_check_bug_417();
2384 tor_assert(msg);
2386 if (!routerlist)
2387 router_get_routerlist();
2388 if (!networkstatus_list)
2389 networkstatus_list = smartlist_create();
2391 id_digest = router->cache_info.identity_digest;
2393 /* Make sure that we haven't already got this exact descriptor. */
2394 if (sdmap_get(routerlist->desc_digest_map,
2395 router->cache_info.signed_descriptor_digest)) {
2396 log_info(LD_DIR,
2397 "Dropping descriptor that we already have for router '%s'",
2398 router->nickname);
2399 *msg = "Router descriptor was not new.";
2400 routerinfo_free(router);
2401 return -1;
2404 if (routerlist_is_overfull(routerlist))
2405 routerlist_remove_old_routers();
2407 if (authdir) {
2408 if (authdir_wants_to_reject_router(router, msg,
2409 !from_cache && !from_fetch)) {
2410 tor_assert(*msg);
2411 routerinfo_free(router);
2412 return -2;
2414 authdir_believes_valid = router->is_valid;
2415 } else if (from_fetch) {
2416 /* Only check the descriptor digest against the network statuses when
2417 * we are receiving in response to a fetch. */
2419 if (!signed_desc_digest_is_recognized(&router->cache_info) &&
2420 !routerinfo_is_a_configured_bridge(router)) {
2421 /* We asked for it, so some networkstatus must have listed it when we
2422 * did. Save it if we're a cache in case somebody else asks for it. */
2423 log_info(LD_DIR,
2424 "Received a no-longer-recognized descriptor for router '%s'",
2425 router->nickname);
2426 *msg = "Router descriptor is not referenced by any network-status.";
2428 /* Only journal this desc if we'll be serving it. */
2429 if (!from_cache && get_options()->DirPort)
2430 signed_desc_append_to_journal(&router->cache_info, router->purpose);
2431 routerlist_insert_old(routerlist, router);
2432 return -1;
2436 /* We no longer need a router with this descriptor digest. */
2437 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
2439 routerstatus_t *rs =
2440 networkstatus_find_entry(ns, router->cache_info.identity_digest);
2441 if (rs && !memcmp(rs->descriptor_digest,
2442 router->cache_info.signed_descriptor_digest,
2443 DIGEST_LEN))
2444 rs->need_to_mirror = 0;
2447 /* If we have a router with the same identity key, choose the newer one. */
2448 old_router = rimap_get(routerlist->identity_map,
2449 router->cache_info.identity_digest);
2450 if (old_router) {
2451 if (router->cache_info.published_on <=
2452 old_router->cache_info.published_on) {
2453 /* Same key, but old */
2454 log_debug(LD_DIR, "Skipping not-new descriptor for router '%s'",
2455 router->nickname);
2456 /* Only journal this desc if we'll be serving it. */
2457 if (!from_cache && get_options()->DirPort)
2458 signed_desc_append_to_journal(&router->cache_info, router->purpose);
2459 routerlist_insert_old(routerlist, router);
2460 *msg = "Router descriptor was not new.";
2461 return -1;
2462 } else {
2463 /* Same key, new. */
2464 int unreachable = 0;
2465 log_debug(LD_DIR, "Replacing entry for router '%s/%s' [%s]",
2466 router->nickname, old_router->nickname,
2467 hex_str(id_digest,DIGEST_LEN));
2468 if (router->addr == old_router->addr &&
2469 router->or_port == old_router->or_port) {
2470 /* these carry over when the address and orport are unchanged.*/
2471 router->last_reachable = old_router->last_reachable;
2472 router->testing_since = old_router->testing_since;
2473 router->num_unreachable_notifications =
2474 old_router->num_unreachable_notifications;
2476 if (authdir_may_warn_about_unreachable_server &&
2477 dirserv_thinks_router_is_blatantly_unreachable(router, time(NULL))) {
2478 if (router->num_unreachable_notifications >= 3) {
2479 unreachable = 1;
2480 log_notice(LD_DIR, "Notifying server '%s' that it's unreachable. "
2481 "(ContactInfo '%s', platform '%s').",
2482 router->nickname,
2483 router->contact_info ? router->contact_info : "",
2484 router->platform ? router->platform : "");
2485 } else {
2486 log_info(LD_DIR,"'%s' may be unreachable -- the %d previous "
2487 "descriptors were thought to be unreachable.",
2488 router->nickname, router->num_unreachable_notifications);
2489 router->num_unreachable_notifications++;
2492 routerlist_replace(routerlist, old_router, router);
2493 if (!from_cache) {
2494 signed_desc_append_to_journal(&router->cache_info, router->purpose);
2496 directory_set_dirty();
2497 *msg = unreachable ? "Dirserver believes your ORPort is unreachable" :
2498 authdir_believes_valid ? "Valid server updated" :
2499 ("Invalid server updated. (This dirserver is marking your "
2500 "server as unapproved.)");
2501 return unreachable ? 1 : 0;
2505 /* We haven't seen a router with this identity before. Add it to the end of
2506 * the list. */
2507 routerlist_insert(routerlist, router);
2508 if (!from_cache)
2509 signed_desc_append_to_journal(&router->cache_info, router->purpose);
2510 directory_set_dirty();
2511 return 0;
2514 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
2515 * as for router_add_to_routerlist(). */
2516 void
2517 router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg,
2518 int from_cache, int from_fetch)
2520 int inserted;
2521 (void)from_fetch;
2522 if (msg) *msg = NULL;
2524 inserted = extrainfo_insert(router_get_routerlist(), ei);
2526 if (inserted && !from_cache)
2527 signed_desc_append_to_journal(&ei->cache_info, EXTRAINFO_PURPOSE_GENERAL);
2530 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
2531 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
2532 * to, or later than that of *<b>b</b>. */
2533 static int
2534 _compare_old_routers_by_identity(const void **_a, const void **_b)
2536 int i;
2537 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
2538 if ((i = memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
2539 return i;
2540 return r1->published_on - r2->published_on;
2543 /** Internal type used to represent how long an old descriptor was valid,
2544 * where it appeared in the list of old descriptors, and whether it's extra
2545 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
2546 struct duration_idx_t {
2547 int duration;
2548 int idx;
2549 int old;
2552 /** Sorting helper: compare two duration_idx_t by their duration. */
2553 static int
2554 _compare_duration_idx(const void *_d1, const void *_d2)
2556 const struct duration_idx_t *d1 = _d1;
2557 const struct duration_idx_t *d2 = _d2;
2558 return d1->duration - d2->duration;
2561 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
2562 * must contain routerinfo_t with the same identity and with publication time
2563 * in ascending order. Remove members from this range until there are no more
2564 * than max_descriptors_per_router() remaining. Start by removing the oldest
2565 * members from before <b>cutoff</b>, then remove members which were current
2566 * for the lowest amount of time. The order of members of old_routers at
2567 * indices <b>lo</b> or higher may be changed.
2569 static void
2570 routerlist_remove_old_cached_routers_with_id(time_t cutoff, int lo, int hi,
2571 digestmap_t *retain)
2573 int i, n = hi-lo+1;
2574 unsigned n_extra, n_rmv = 0;
2575 struct duration_idx_t *lifespans;
2576 uint8_t *rmv, *must_keep;
2577 smartlist_t *lst = routerlist->old_routers;
2578 #if 1
2579 const char *ident;
2580 tor_assert(hi < smartlist_len(lst));
2581 tor_assert(lo <= hi);
2582 ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
2583 for (i = lo+1; i <= hi; ++i) {
2584 signed_descriptor_t *r = smartlist_get(lst, i);
2585 tor_assert(!memcmp(ident, r->identity_digest, DIGEST_LEN));
2587 #endif
2589 /* Check whether we need to do anything at all. */
2591 int mdpr = max_descriptors_per_router();
2592 if (n <= mdpr)
2593 return;
2594 n_extra = n - mdpr;
2597 lifespans = tor_malloc_zero(sizeof(struct duration_idx_t)*n);
2598 rmv = tor_malloc_zero(sizeof(uint8_t)*n);
2599 must_keep = tor_malloc_zero(sizeof(uint8_t)*n);
2600 /* Set lifespans to contain the lifespan and index of each server. */
2601 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
2602 for (i = lo; i <= hi; ++i) {
2603 signed_descriptor_t *r = smartlist_get(lst, i);
2604 signed_descriptor_t *r_next;
2605 lifespans[i-lo].idx = i;
2606 if (retain && digestmap_get(retain, r->signed_descriptor_digest)) {
2607 must_keep[i-lo] = 1;
2609 if (i < hi) {
2610 r_next = smartlist_get(lst, i+1);
2611 tor_assert(r->published_on <= r_next->published_on);
2612 lifespans[i-lo].duration = (r_next->published_on - r->published_on);
2613 } else {
2614 r_next = NULL;
2615 lifespans[i-lo].duration = INT_MAX;
2617 if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
2618 ++n_rmv;
2619 lifespans[i-lo].old = 1;
2620 rmv[i-lo] = 1;
2624 if (n_rmv < n_extra) {
2626 * We aren't removing enough servers for being old. Sort lifespans by
2627 * the duration of liveness, and remove the ones we're not already going to
2628 * remove based on how long they were alive.
2630 qsort(lifespans, n, sizeof(struct duration_idx_t), _compare_duration_idx);
2631 for (i = 0; i < n && n_rmv < n_extra; ++i) {
2632 if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
2633 rmv[lifespans[i].idx-lo] = 1;
2634 ++n_rmv;
2639 i = hi;
2640 do {
2641 if (rmv[i-lo])
2642 routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
2643 } while (--i >= lo);
2644 tor_free(must_keep);
2645 tor_free(rmv);
2646 tor_free(lifespans);
2649 /** Deactivate any routers from the routerlist that are more than
2650 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
2651 * remove old routers from the list of cached routers if we have too many.
2653 void
2654 routerlist_remove_old_routers(void)
2656 int i, hi=-1;
2657 const char *cur_id = NULL;
2658 time_t now = time(NULL);
2659 time_t cutoff;
2660 routerinfo_t *router;
2661 signed_descriptor_t *sd;
2662 digestmap_t *retain;
2663 if (!routerlist || !networkstatus_list)
2664 return;
2666 routerlist_assert_ok(routerlist);
2668 retain = digestmap_new();
2669 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
2670 /* Build a list of all the descriptors that _anybody_ recommends. */
2671 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
2673 /* XXXX The inner loop here gets pretty expensive, and actually shows up
2674 * on some profiles. It may be the reason digestmap_set shows up in
2675 * profiles too. If instead we kept a per-descriptor digest count of
2676 * how many networkstatuses recommended each descriptor, and changed
2677 * that only when the networkstatuses changed, that would be a speed
2678 * improvement, possibly 1-4% if it also removes digestmap_set from the
2679 * profile. Not worth it for 0.1.2.x, though. The new directory
2680 * system will obsolete this whole thing in 0.2.0.x. */
2681 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
2682 if (rs->published_on >= cutoff)
2683 digestmap_set(retain, rs->descriptor_digest, (void*)1));
2686 /* If we have a bunch of networkstatuses, we should consider pruning current
2687 * routers that are too old and that nobody recommends. (If we don't have
2688 * enough networkstatuses, then we should get more before we decide to kill
2689 * routers.) */
2690 if (smartlist_len(networkstatus_list) > get_n_v2_authorities() / 2) {
2691 cutoff = now - ROUTER_MAX_AGE;
2692 /* Remove too-old unrecommended members of routerlist->routers. */
2693 for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
2694 router = smartlist_get(routerlist->routers, i);
2695 if (router->cache_info.published_on <= cutoff &&
2696 !digestmap_get(retain,router->cache_info.signed_descriptor_digest)) {
2697 /* Too old: remove it. (If we're a cache, just move it into
2698 * old_routers.) */
2699 log_info(LD_DIR,
2700 "Forgetting obsolete (too old) routerinfo for router '%s'",
2701 router->nickname);
2702 routerlist_remove(routerlist, router, 1);
2703 i--;
2708 routerlist_assert_ok(routerlist);
2710 /* Remove far-too-old members of routerlist->old_routers. */
2711 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
2712 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
2713 sd = smartlist_get(routerlist->old_routers, i);
2714 if (sd->published_on <= cutoff &&
2715 !digestmap_get(retain, sd->signed_descriptor_digest)) {
2716 /* Too old. Remove it. */
2717 routerlist_remove_old(routerlist, sd, i--);
2721 routerlist_assert_ok(routerlist);
2723 /* Now we might have to look at routerlist->old_routers for extraneous
2724 * members. (We'd keep all the members if we could, but we need to save
2725 * space.) First, check whether we have too many router descriptors, total.
2726 * We're okay with having too many for some given router, so long as the
2727 * total number doesn't approach max_descriptors_per_router()*len(router).
2729 if (smartlist_len(routerlist->old_routers) <
2730 smartlist_len(routerlist->routers) * (max_descriptors_per_router() - 1))
2731 goto done;
2733 smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
2735 /* Iterate through the list from back to front, so when we remove descriptors
2736 * we don't mess up groups we haven't gotten to. */
2737 for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
2738 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
2739 if (!cur_id) {
2740 cur_id = r->identity_digest;
2741 hi = i;
2743 if (memcmp(cur_id, r->identity_digest, DIGEST_LEN)) {
2744 routerlist_remove_old_cached_routers_with_id(cutoff, i+1, hi, retain);
2745 cur_id = r->identity_digest;
2746 hi = i;
2749 if (hi>=0)
2750 routerlist_remove_old_cached_routers_with_id(cutoff, 0, hi, retain);
2751 routerlist_assert_ok(routerlist);
2753 done:
2754 digestmap_free(retain, NULL);
2757 /** We just added a new descriptor that isn't of purpose
2758 * ROUTER_PURPOSE_GENERAL. Take whatever extra steps we need. */
2759 static void
2760 routerlist_descriptors_added(smartlist_t *sl)
2762 tor_assert(sl);
2763 control_event_descriptors_changed(sl);
2764 SMARTLIST_FOREACH(sl, routerinfo_t *, ri,
2765 if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
2766 learned_bridge_descriptor(ri);
2771 * Code to parse a single router descriptor and insert it into the
2772 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
2773 * descriptor was well-formed but could not be added; and 1 if the
2774 * descriptor was added.
2776 * If we don't add it and <b>msg</b> is not NULL, then assign to
2777 * *<b>msg</b> a static string describing the reason for refusing the
2778 * descriptor.
2780 * This is used only by the controller.
2783 router_load_single_router(const char *s, uint8_t purpose, const char **msg)
2785 routerinfo_t *ri;
2786 int r;
2787 smartlist_t *lst;
2788 tor_assert(msg);
2789 *msg = NULL;
2791 routerlist_check_bug_417();
2792 if (!(ri = router_parse_entry_from_string(s, NULL, 1))) {
2793 log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
2794 *msg = "Couldn't parse router descriptor.";
2795 return -1;
2797 ri->purpose = purpose;
2798 if (ri->purpose != ROUTER_PURPOSE_GENERAL)
2799 ri->cache_info.do_not_cache = 1;
2800 if (router_is_me(ri)) {
2801 log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
2802 *msg = "Router's identity key matches mine.";
2803 routerinfo_free(ri);
2804 return 0;
2807 lst = smartlist_create();
2808 smartlist_add(lst, ri);
2809 routers_update_status_from_networkstatus(lst, 0);
2811 if ((r=router_add_to_routerlist(ri, msg, 0, 0))<0) {
2812 /* we've already assigned to *msg now, and ri is already freed */
2813 tor_assert(*msg);
2814 if (r < -1)
2815 log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
2816 smartlist_free(lst);
2817 return 0;
2818 } else {
2819 routerlist_descriptors_added(lst);
2820 smartlist_free(lst);
2821 log_debug(LD_DIR, "Added router to list");
2822 return 1;
2826 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
2827 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
2828 * are in response to a query to the network: cache them by adding them to
2829 * the journal.
2831 * If <b>requested_fingerprints</b> is provided, it must contain a list of
2832 * uppercased identity fingerprints. Do not update any router whose
2833 * fingerprint is not on the list; after updating a router, remove its
2834 * fingerprint from the list.
2836 void
2837 router_load_routers_from_string(const char *s, const char *eos,
2838 saved_location_t saved_location,
2839 smartlist_t *requested_fingerprints,
2840 uint8_t purpose)
2842 smartlist_t *routers = smartlist_create(), *changed = smartlist_create();
2843 char fp[HEX_DIGEST_LEN+1];
2844 const char *msg;
2845 int from_cache = (saved_location != SAVED_NOWHERE);
2847 router_parse_list_from_string(&s, eos, routers, saved_location, 0);
2849 routers_update_status_from_networkstatus(routers, !from_cache);
2851 log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
2853 SMARTLIST_FOREACH(routers, routerinfo_t *, ri,
2855 base16_encode(fp, sizeof(fp), ri->cache_info.signed_descriptor_digest,
2856 DIGEST_LEN);
2857 if (requested_fingerprints) {
2858 if (smartlist_string_isin(requested_fingerprints, fp)) {
2859 smartlist_string_remove(requested_fingerprints, fp);
2860 } else {
2861 char *requested =
2862 smartlist_join_strings(requested_fingerprints," ",0,NULL);
2863 log_warn(LD_DIR,
2864 "We received a router descriptor with a fingerprint (%s) "
2865 "that we never requested. (We asked for: %s.) Dropping.",
2866 fp, requested);
2867 tor_free(requested);
2868 routerinfo_free(ri);
2869 continue;
2873 ri->purpose = purpose;
2874 if (purpose != ROUTER_PURPOSE_GENERAL)
2875 ri->cache_info.do_not_cache = 1;
2877 if (router_add_to_routerlist(ri, &msg, from_cache, !from_cache) >= 0) {
2878 smartlist_add(changed, ri);
2879 routerlist_descriptors_added(changed);
2880 smartlist_clear(changed);
2884 routerlist_assert_ok(routerlist);
2885 router_rebuild_store(0, 0);
2887 smartlist_free(routers);
2888 smartlist_free(changed);
2891 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
2892 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
2893 * router_load_routers_from_string(). */
2894 void
2895 router_load_extrainfo_from_string(const char *s, const char *eos,
2896 saved_location_t saved_location,
2897 smartlist_t *requested_fingerprints)
2899 smartlist_t *extrainfo_list = smartlist_create();
2900 const char *msg;
2901 int from_cache = (saved_location != SAVED_NOWHERE);
2903 router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1);
2905 log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
2907 SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
2908 if (requested_fingerprints) {
2909 char fp[HEX_DIGEST_LEN+1];
2910 base16_encode(fp, sizeof(fp), ei->cache_info.signed_descriptor_digest,
2911 DIGEST_LEN);
2912 smartlist_string_remove(requested_fingerprints, fp);
2914 router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
2917 routerlist_assert_ok(routerlist);
2918 router_rebuild_store(0, 1);
2920 smartlist_free(extrainfo_list);
2923 /** Helper: return a newly allocated string containing the name of the filename
2924 * where we plan to cache the network status with the given identity digest. */
2925 char *
2926 networkstatus_get_cache_filename(const char *identity_digest)
2928 const char *datadir = get_options()->DataDirectory;
2929 size_t len = strlen(datadir)+64;
2930 char fp[HEX_DIGEST_LEN+1];
2931 char *fn = tor_malloc(len+1);
2932 base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
2933 tor_snprintf(fn, len, "%s"PATH_SEPARATOR"cached-status"PATH_SEPARATOR"%s",
2934 datadir,fp);
2935 return fn;
2938 /** Helper for smartlist_sort: Compare two networkstatus objects by
2939 * publication date. */
2940 static int
2941 _compare_networkstatus_published_on(const void **_a, const void **_b)
2943 const networkstatus_t *a = *_a, *b = *_b;
2944 if (a->published_on < b->published_on)
2945 return -1;
2946 else if (a->published_on > b->published_on)
2947 return 1;
2948 else
2949 return 0;
2952 /** Add the parsed neworkstatus in <b>ns</b> (with original document in
2953 * <b>s</b> to the disk cache (and the in-memory directory server cache) as
2954 * appropriate. */
2955 static int
2956 add_networkstatus_to_cache(const char *s,
2957 networkstatus_source_t source,
2958 networkstatus_t *ns)
2960 if (source != NS_FROM_CACHE) {
2961 char *fn = networkstatus_get_cache_filename(ns->identity_digest);
2962 if (write_str_to_file(fn, s, 0)<0) {
2963 log_notice(LD_FS, "Couldn't write cached network status to \"%s\"", fn);
2965 tor_free(fn);
2968 if (get_options()->DirPort)
2969 dirserv_set_cached_networkstatus_v2(s,
2970 ns->identity_digest,
2971 ns->published_on);
2973 return 0;
2976 /** How far in the future do we allow a network-status to get before removing
2977 * it? (seconds) */
2978 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
2980 /** Given a string <b>s</b> containing a network status that we received at
2981 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
2982 * store it, and put it into our cache as necessary.
2984 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
2985 * own networkstatus_t (if we're an authoritative directory server).
2987 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
2988 * cache.
2990 * If <b>requested_fingerprints</b> is provided, it must contain a list of
2991 * uppercased identity fingerprints. Do not update any networkstatus whose
2992 * fingerprint is not on the list; after updating a networkstatus, remove its
2993 * fingerprint from the list.
2995 * Return 0 on success, -1 on failure.
2997 * Callers should make sure that routers_update_all_from_networkstatus() is
2998 * invoked after this function succeeds.
3001 router_set_networkstatus(const char *s, time_t arrived_at,
3002 networkstatus_source_t source, smartlist_t *requested_fingerprints)
3004 networkstatus_t *ns;
3005 int i, found;
3006 time_t now;
3007 int skewed = 0;
3008 trusted_dir_server_t *trusted_dir = NULL;
3009 const char *source_desc = NULL;
3010 char fp[HEX_DIGEST_LEN+1];
3011 char published[ISO_TIME_LEN+1];
3013 ns = networkstatus_parse_from_string(s);
3014 if (!ns) {
3015 log_warn(LD_DIR, "Couldn't parse network status.");
3016 return -1;
3018 base16_encode(fp, HEX_DIGEST_LEN+1, ns->identity_digest, DIGEST_LEN);
3019 if (!(trusted_dir =
3020 router_get_trusteddirserver_by_digest(ns->identity_digest)) ||
3021 !(trusted_dir->type & V2_AUTHORITY)) {
3022 log_info(LD_DIR, "Network status was signed, but not by an authoritative "
3023 "directory we recognize.");
3024 if (!get_options()->DirPort) {
3025 networkstatus_free(ns);
3026 return 0;
3028 source_desc = fp;
3029 } else {
3030 source_desc = trusted_dir->description;
3032 now = time(NULL);
3033 if (arrived_at > now)
3034 arrived_at = now;
3036 ns->received_on = arrived_at;
3038 format_iso_time(published, ns->published_on);
3040 if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
3041 log_warn(LD_GENERAL, "Network status from %s was published in the future "
3042 "(%s GMT). Somebody is skewed here: check your clock. "
3043 "Not caching.",
3044 source_desc, published);
3045 control_event_general_status(LOG_WARN,
3046 "CLOCK_SKEW SOURCE=NETWORKSTATUS:%s:%d",
3047 ns->source_address, ns->source_dirport);
3048 skewed = 1;
3051 if (!networkstatus_list)
3052 networkstatus_list = smartlist_create();
3054 if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
3055 router_digest_is_me(ns->identity_digest)) {
3056 /* Don't replace our own networkstatus when we get it from somebody else.*/
3057 networkstatus_free(ns);
3058 return 0;
3061 if (requested_fingerprints) {
3062 if (smartlist_string_isin(requested_fingerprints, fp)) {
3063 smartlist_string_remove(requested_fingerprints, fp);
3064 } else {
3065 if (source != NS_FROM_DIR_ALL) {
3066 char *requested =
3067 smartlist_join_strings(requested_fingerprints," ",0,NULL);
3068 log_warn(LD_DIR,
3069 "We received a network status with a fingerprint (%s) that we "
3070 "never requested. (We asked for: %s.) Dropping.",
3071 fp, requested);
3072 tor_free(requested);
3073 return 0;
3078 if (!trusted_dir) {
3079 if (!skewed && get_options()->DirPort) {
3080 /* We got a non-trusted networkstatus, and we're a directory cache.
3081 * This means that we asked an authority, and it told us about another
3082 * authority we didn't recognize. */
3083 log_info(LD_DIR,
3084 "We do not recognize authority (%s) but we are willing "
3085 "to cache it.", fp);
3086 add_networkstatus_to_cache(s, source, ns);
3087 networkstatus_free(ns);
3089 return 0;
3092 found = 0;
3093 for (i=0; i < smartlist_len(networkstatus_list); ++i) {
3094 networkstatus_t *old_ns = smartlist_get(networkstatus_list, i);
3096 if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
3097 if (!memcmp(old_ns->networkstatus_digest,
3098 ns->networkstatus_digest, DIGEST_LEN)) {
3099 /* Same one we had before. */
3100 networkstatus_free(ns);
3101 tor_assert(trusted_dir);
3102 log_info(LD_DIR,
3103 "Not replacing network-status from %s (published %s); "
3104 "we already have it.",
3105 trusted_dir->description, published);
3106 if (old_ns->received_on < arrived_at) {
3107 if (source != NS_FROM_CACHE) {
3108 char *fn;
3109 fn = networkstatus_get_cache_filename(old_ns->identity_digest);
3110 /* We use mtime to tell when it arrived, so update that. */
3111 touch_file(fn);
3112 tor_free(fn);
3114 old_ns->received_on = arrived_at;
3116 ++trusted_dir->n_networkstatus_failures;
3117 return 0;
3118 } else if (old_ns->published_on >= ns->published_on) {
3119 char old_published[ISO_TIME_LEN+1];
3120 format_iso_time(old_published, old_ns->published_on);
3121 tor_assert(trusted_dir);
3122 log_info(LD_DIR,
3123 "Not replacing network-status from %s (published %s);"
3124 " we have a newer one (published %s) for this authority.",
3125 trusted_dir->description, published,
3126 old_published);
3127 networkstatus_free(ns);
3128 ++trusted_dir->n_networkstatus_failures;
3129 return 0;
3130 } else {
3131 networkstatus_free(old_ns);
3132 smartlist_set(networkstatus_list, i, ns);
3133 found = 1;
3134 break;
3139 if (source != NS_FROM_CACHE && trusted_dir)
3140 trusted_dir->n_networkstatus_failures = 0;
3142 if (!found)
3143 smartlist_add(networkstatus_list, ns);
3145 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
3147 if (!router_get_by_descriptor_digest(rs->descriptor_digest))
3148 rs->need_to_mirror = 1;
3151 log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
3152 source == NS_FROM_CACHE?"cached from":
3153 ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
3154 "downloaded from":"generated for"),
3155 trusted_dir->description, published);
3156 networkstatus_list_has_changed = 1;
3157 router_dir_info_changed();
3159 smartlist_sort(networkstatus_list, _compare_networkstatus_published_on);
3161 if (!skewed)
3162 add_networkstatus_to_cache(s, source, ns);
3164 networkstatus_list_update_recent(now);
3166 return 0;
3169 /** How old do we allow a network-status to get before removing it
3170 * completely? */
3171 #define MAX_NETWORKSTATUS_AGE (10*24*60*60)
3172 /** Remove all very-old network_status_t objects from memory and from the
3173 * disk cache. */
3174 void
3175 networkstatus_list_clean(time_t now)
3177 int i;
3178 if (!networkstatus_list)
3179 return;
3181 for (i = 0; i < smartlist_len(networkstatus_list); ++i) {
3182 networkstatus_t *ns = smartlist_get(networkstatus_list, i);
3183 char *fname = NULL;
3184 if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
3185 continue;
3186 /* Okay, this one is too old. Remove it from the list, and delete it
3187 * from the cache. */
3188 smartlist_del(networkstatus_list, i--);
3189 fname = networkstatus_get_cache_filename(ns->identity_digest);
3190 if (file_status(fname) == FN_FILE) {
3191 log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
3192 unlink(fname);
3194 tor_free(fname);
3195 if (get_options()->DirPort) {
3196 dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
3198 networkstatus_free(ns);
3199 router_dir_info_changed();
3202 /* And now go through the directory cache for any cached untrusted
3203 * networkstatuses and other network info. */
3204 dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
3205 dirserv_clear_old_v1_info(now);
3208 /** Helper for bsearching a list of routerstatus_t pointers.*/
3209 static int
3210 _compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
3212 const char *key = _key;
3213 const routerstatus_t *rs = *_member;
3214 return memcmp(key, rs->identity_digest, DIGEST_LEN);
3217 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
3218 * NULL if none was found. */
3219 static routerstatus_t *
3220 networkstatus_find_entry(networkstatus_t *ns, const char *digest)
3222 return smartlist_bsearch(ns->entries, digest,
3223 _compare_digest_to_routerstatus_entry);
3226 /** Return the consensus view of the status of the router whose identity
3227 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
3228 local_routerstatus_t *
3229 router_get_combined_status_by_digest(const char *digest)
3231 if (!routerstatus_list)
3232 return NULL;
3233 return smartlist_bsearch(routerstatus_list, digest,
3234 _compare_digest_to_routerstatus_entry);
3237 /** Return the consensus view of the status of the router whose current
3238 * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
3239 * known. */
3240 local_routerstatus_t *
3241 router_get_combined_status_by_descriptor_digest(const char *digest)
3243 if (!routerstatus_by_desc_digest_map)
3244 return NULL;
3245 return digestmap_get(routerstatus_by_desc_digest_map, digest);
3248 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
3249 * the corresponding local_routerstatus_t, or NULL if none exists. Warn the
3250 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
3251 * nickname, but the Named flag isn't set for that router. */
3252 static local_routerstatus_t *
3253 router_get_combined_status_by_nickname(const char *nickname,
3254 int warn_if_unnamed)
3256 char digest[DIGEST_LEN];
3257 local_routerstatus_t *best=NULL;
3258 smartlist_t *matches=NULL;
3260 if (!routerstatus_list || !nickname)
3261 return NULL;
3263 if (nickname[0] == '$') {
3264 if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))<0)
3265 return NULL;
3266 return router_get_combined_status_by_digest(digest);
3267 } else if (strlen(nickname) == HEX_DIGEST_LEN &&
3268 (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))==0)) {
3269 return router_get_combined_status_by_digest(digest);
3272 matches = smartlist_create();
3273 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, lrs,
3275 if (!strcasecmp(lrs->status.nickname, nickname)) {
3276 if (lrs->status.is_named) {
3277 smartlist_free(matches);
3278 return lrs;
3279 } else {
3280 smartlist_add(matches, lrs);
3281 best = lrs;
3286 if (smartlist_len(matches)>1 && warn_if_unnamed) {
3287 int any_unwarned=0;
3288 SMARTLIST_FOREACH(matches, local_routerstatus_t *, lrs,
3290 if (! lrs->name_lookup_warned) {
3291 lrs->name_lookup_warned=1;
3292 any_unwarned=1;
3295 if (any_unwarned) {
3296 log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
3297 " but none is listed as named by the directory authorites. "
3298 "Choosing one arbitrarily.", nickname);
3300 } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
3301 char fp[HEX_DIGEST_LEN+1];
3302 base16_encode(fp, sizeof(fp),
3303 best->status.identity_digest, DIGEST_LEN);
3304 log_warn(LD_CONFIG,
3305 "When looking up a status, you specified a server \"%s\" by name, "
3306 "but the directory authorities do not have any key registered for "
3307 "this nickname -- so it could be used by any server, "
3308 "not just the one you meant. "
3309 "To make sure you get the same server in the future, refer to "
3310 "it by key, as \"$%s\".", nickname, fp);
3311 best->name_lookup_warned = 1;
3313 smartlist_free(matches);
3314 return best;
3317 #if 0
3318 /** Find a routerstatus_t that corresponds to <b>hexdigest</b>, if
3319 * any. Prefer ones that belong to authorities. */
3320 routerstatus_t *
3321 routerstatus_get_by_hexdigest(const char *hexdigest)
3323 char digest[DIGEST_LEN];
3324 local_routerstatus_t *rs;
3325 trusted_dir_server_t *ds;
3327 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
3328 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
3329 return NULL;
3330 if ((ds = router_get_trusteddirserver_by_digest(digest)))
3331 return &(ds->fake_status.status);
3332 if ((rs = router_get_combined_status_by_digest(digest)))
3333 return &(rs->status);
3334 return NULL;
3336 #endif
3338 /** Return true iff any networkstatus includes a descriptor whose digest
3339 * is that of <b>desc</b>. */
3340 static int
3341 signed_desc_digest_is_recognized(signed_descriptor_t *desc)
3343 routerstatus_t *rs;
3344 if (!networkstatus_list)
3345 return 0;
3347 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
3349 if (!(rs = networkstatus_find_entry(ns, desc->identity_digest)))
3350 continue;
3351 if (!memcmp(rs->descriptor_digest,
3352 desc->signed_descriptor_digest, DIGEST_LEN))
3353 return 1;
3355 return 0;
3358 /** How frequently do directory authorities re-download fresh networkstatus
3359 * documents? */
3360 #define AUTHORITY_NS_CACHE_INTERVAL (5*60)
3362 /** How frequently do non-authority directory caches re-download fresh
3363 * networkstatus documents? */
3364 #define NONAUTHORITY_NS_CACHE_INTERVAL (15*60)
3366 /** We are a directory server, and so cache network_status documents.
3367 * Initiate downloads as needed to update them. For v2 authorities,
3368 * this means asking each trusted directory for its network-status.
3369 * For caches, this means asking a random v2 authority for all
3370 * network-statuses.
3372 static void
3373 update_networkstatus_cache_downloads(time_t now)
3375 int authority = authdir_mode_v2(get_options());
3376 int interval =
3377 authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
3379 if (last_networkstatus_download_attempted + interval >= now)
3380 return;
3381 if (!trusted_dir_servers)
3382 return;
3384 last_networkstatus_download_attempted = now;
3386 if (authority) {
3387 /* An authority launches a separate connection for everybody. */
3388 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
3390 char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
3391 if (!(ds->type & V2_AUTHORITY))
3392 continue;
3393 if (router_digest_is_me(ds->digest))
3394 continue;
3395 if (connection_get_by_type_addr_port_purpose(
3396 CONN_TYPE_DIR, ds->addr, ds->dir_port,
3397 DIR_PURPOSE_FETCH_NETWORKSTATUS)) {
3398 /* XXX020 the above dir_port won't be accurate if we're
3399 * doing a tunneled conn. In that case it should be or_port.
3400 * How to guess from here? Maybe make the function less general
3401 * and have it know that it's looking for dir conns. -RD */
3402 /* We are already fetching this one. */
3403 continue;
3405 strlcpy(resource, "fp/", sizeof(resource));
3406 base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
3407 strlcat(resource, ".z", sizeof(resource));
3408 directory_initiate_command_routerstatus(
3409 &ds->fake_status.status, DIR_PURPOSE_FETCH_NETWORKSTATUS,
3410 ROUTER_PURPOSE_GENERAL,
3411 0, /* Not private */
3412 resource,
3413 NULL, 0 /* No payload. */);
3415 } else {
3416 /* A non-authority cache launches one connection to a random authority. */
3417 /* (Check whether we're currently fetching network-status objects.) */
3418 if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
3419 DIR_PURPOSE_FETCH_NETWORKSTATUS))
3420 directory_get_from_dirserver(DIR_PURPOSE_FETCH_NETWORKSTATUS,
3421 ROUTER_PURPOSE_GENERAL, "all.z",1);
3425 /** How long (in seconds) does a client wait after getting a network status
3426 * before downloading the next in sequence? */
3427 #define NETWORKSTATUS_CLIENT_DL_INTERVAL (30*60)
3428 /** How many times do we allow a networkstatus download to fail before we
3429 * assume that the authority isn't publishing? */
3430 #define NETWORKSTATUS_N_ALLOWABLE_FAILURES 3
3431 /** We are not a directory cache or authority. Update our network-status list
3432 * by launching a new directory fetch for enough network-status documents "as
3433 * necessary". See function comments for implementation details.
3435 static void
3436 update_networkstatus_client_downloads(time_t now)
3438 int n_live = 0, n_dirservers, n_running_dirservers, needed = 0;
3439 int fetch_latest = 0;
3440 int most_recent_idx = -1;
3441 trusted_dir_server_t *most_recent = NULL;
3442 time_t most_recent_received = 0;
3443 char *resource, *cp;
3444 size_t resource_len;
3445 smartlist_t *missing;
3447 if (connection_get_by_type_purpose(CONN_TYPE_DIR,
3448 DIR_PURPOSE_FETCH_NETWORKSTATUS))
3449 return;
3451 /* This is a little tricky. We want to download enough network-status
3452 * objects so that we have all of them under
3453 * NETWORKSTATUS_MAX_AGE publication time. We want to download a new
3454 * *one* if the most recent one's publication time is under
3455 * NETWORKSTATUS_CLIENT_DL_INTERVAL.
3457 if (!get_n_v2_authorities())
3458 return;
3459 n_dirservers = n_running_dirservers = 0;
3460 missing = smartlist_create();
3461 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
3463 networkstatus_t *ns = networkstatus_get_by_digest(ds->digest);
3464 if (!(ds->type & V2_AUTHORITY))
3465 continue;
3466 ++n_dirservers;
3467 if (ds->n_networkstatus_failures > NETWORKSTATUS_N_ALLOWABLE_FAILURES)
3468 continue;
3469 ++n_running_dirservers;
3470 if (ns && ns->published_on > now-NETWORKSTATUS_MAX_AGE)
3471 ++n_live;
3472 else
3473 smartlist_add(missing, ds->digest);
3474 if (ns && (!most_recent || ns->received_on > most_recent_received)) {
3475 most_recent_idx = ds_sl_idx; /* magic variable from FOREACH */
3476 most_recent = ds;
3477 most_recent_received = ns->received_on;
3481 /* Also, download at least 1 every NETWORKSTATUS_CLIENT_DL_INTERVAL. */
3482 if (!smartlist_len(missing) &&
3483 most_recent_received < now-NETWORKSTATUS_CLIENT_DL_INTERVAL) {
3484 log_info(LD_DIR, "Our most recent network-status document (from %s) "
3485 "is %d seconds old; downloading another.",
3486 most_recent?most_recent->description:"nobody",
3487 (int)(now-most_recent_received));
3488 fetch_latest = 1;
3489 needed = 1;
3490 } else if (smartlist_len(missing)) {
3491 log_info(LD_DIR, "For %d/%d running directory servers, we have %d live"
3492 " network-status documents. Downloading %d.",
3493 n_running_dirservers, n_dirservers, n_live,
3494 smartlist_len(missing));
3495 needed = smartlist_len(missing);
3496 } else {
3497 smartlist_free(missing);
3498 return;
3501 /* If no networkstatus was found, choose a dirserver at random as "most
3502 * recent". */
3503 if (most_recent_idx<0)
3504 most_recent_idx = crypto_rand_int(smartlist_len(trusted_dir_servers));
3506 if (fetch_latest) {
3507 int i;
3508 int n_failed = 0;
3509 for (i = most_recent_idx + 1; 1; ++i) {
3510 trusted_dir_server_t *ds;
3511 if (i >= smartlist_len(trusted_dir_servers))
3512 i = 0;
3513 ds = smartlist_get(trusted_dir_servers, i);
3514 if (!(ds->type & V2_AUTHORITY))
3515 continue;
3516 if (n_failed >= n_dirservers) {
3517 log_info(LD_DIR, "All authorities have failed. Not trying any.");
3518 smartlist_free(missing);
3519 return;
3521 if (ds->n_networkstatus_failures > NETWORKSTATUS_N_ALLOWABLE_FAILURES) {
3522 ++n_failed;
3523 continue;
3525 smartlist_add(missing, ds->digest);
3526 break;
3530 /* Build a request string for all the resources we want. */
3531 resource_len = smartlist_len(missing) * (HEX_DIGEST_LEN+1) + 6;
3532 resource = tor_malloc(resource_len);
3533 memcpy(resource, "fp/", 3);
3534 cp = resource+3;
3535 smartlist_sort_digests(missing);
3536 needed = smartlist_len(missing);
3537 SMARTLIST_FOREACH(missing, const char *, d,
3539 base16_encode(cp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
3540 cp += HEX_DIGEST_LEN;
3541 --needed;
3542 if (needed)
3543 *cp++ = '+';
3545 memcpy(cp, ".z", 3);
3546 directory_get_from_dirserver(DIR_PURPOSE_FETCH_NETWORKSTATUS,
3547 ROUTER_PURPOSE_GENERAL, resource, 1);
3548 tor_free(resource);
3549 smartlist_free(missing);
3552 /** Return 1 if there's a reason we shouldn't try any directory
3553 * fetches yet (e.g. we demand bridges and none are yet known).
3554 * Else return 0. */
3556 should_delay_dir_fetches(or_options_t *options)
3558 if (options->UseBridges && !any_bridge_descriptors_known()) {
3559 log_notice(LD_DIR, "delaying dir fetches");
3560 return 1;
3562 return 0;
3565 /** Launch requests for networkstatus documents as appropriate. */
3566 void
3567 update_networkstatus_downloads(time_t now)
3569 or_options_t *options = get_options();
3570 if (should_delay_dir_fetches(options))
3571 return;
3572 if (options->DirPort)
3573 update_networkstatus_cache_downloads(now);
3574 else
3575 update_networkstatus_client_downloads(now);
3578 /** Clear all our timeouts for fetching v2 directory stuff, and then
3579 * give it all a try again. */
3580 void
3581 routerlist_retry_directory_downloads(time_t now)
3583 router_reset_status_download_failures();
3584 router_reset_descriptor_download_failures();
3585 update_networkstatus_downloads(now);
3586 update_router_descriptor_downloads(now);
3589 /** Return 1 if all running sufficiently-stable routers will reject
3590 * addr:port, return 0 if any might accept it. */
3592 router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
3593 int need_uptime)
3595 addr_policy_result_t r;
3596 if (!routerlist) return 1;
3598 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
3600 if (router->is_running &&
3601 !router_is_unreliable(router, need_uptime, 0, 0)) {
3602 r = compare_addr_to_addr_policy(addr, port, router->exit_policy);
3603 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
3604 return 0; /* this one could be ok. good enough. */
3607 return 1; /* all will reject. */
3610 /** Return true iff <b>router</b> does not permit exit streams.
3613 router_exit_policy_rejects_all(routerinfo_t *router)
3615 return compare_addr_to_addr_policy(0, 0, router->exit_policy)
3616 == ADDR_POLICY_REJECTED;
3619 /** Add to the list of authorized directory servers one at
3620 * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
3621 * <b>address</b> is NULL, add ourself. */
3622 void
3623 add_trusted_dir_server(const char *nickname, const char *address,
3624 uint16_t dir_port, uint16_t or_port,
3625 const char *digest, authority_type_t type)
3627 trusted_dir_server_t *ent;
3628 uint32_t a;
3629 char *hostname = NULL;
3630 size_t dlen;
3631 if (!trusted_dir_servers)
3632 trusted_dir_servers = smartlist_create();
3634 if (!address) { /* The address is us; we should guess. */
3635 if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
3636 log_warn(LD_CONFIG,
3637 "Couldn't find a suitable address when adding ourself as a "
3638 "trusted directory server.");
3639 return;
3641 } else {
3642 if (tor_lookup_hostname(address, &a)) {
3643 log_warn(LD_CONFIG,
3644 "Unable to lookup address for directory server at '%s'",
3645 address);
3646 return;
3648 hostname = tor_strdup(address);
3651 ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
3652 ent->nickname = nickname ? tor_strdup(nickname) : NULL;
3653 ent->address = hostname;
3654 ent->addr = a;
3655 ent->dir_port = dir_port;
3656 ent->or_port = or_port;
3657 ent->is_running = 1;
3658 ent->type = type;
3659 memcpy(ent->digest, digest, DIGEST_LEN);
3661 dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
3662 ent->description = tor_malloc(dlen);
3663 if (nickname)
3664 tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
3665 nickname, hostname, (int)dir_port);
3666 else
3667 tor_snprintf(ent->description, dlen, "directory server at %s:%d",
3668 hostname, (int)dir_port);
3670 ent->fake_status.status.addr = ent->addr;
3671 memcpy(ent->fake_status.status.identity_digest, digest, DIGEST_LEN);
3672 if (nickname)
3673 strlcpy(ent->fake_status.status.nickname, nickname,
3674 sizeof(ent->fake_status.status.nickname));
3675 else
3676 ent->fake_status.status.nickname[0] = '\0';
3677 ent->fake_status.status.dir_port = ent->dir_port;
3678 ent->fake_status.status.or_port = ent->or_port;
3680 if (ent->or_port)
3681 ent->fake_status.status.version_supports_begindir = 1;
3683 smartlist_add(trusted_dir_servers, ent);
3684 router_dir_info_changed();
3687 /** Free storage held in <b>ds</b>. */
3688 static void
3689 trusted_dir_server_free(trusted_dir_server_t *ds)
3691 if (ds->v3_cert)
3692 authority_cert_free(ds->v3_cert);
3693 tor_free(ds->nickname);
3694 tor_free(ds->description);
3695 tor_free(ds->address);
3696 tor_free(ds);
3699 /** Remove all members from the list of trusted dir servers. */
3700 void
3701 clear_trusted_dir_servers(void)
3703 if (trusted_dir_servers) {
3704 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
3705 trusted_dir_server_free(ent));
3706 smartlist_clear(trusted_dir_servers);
3707 } else {
3708 trusted_dir_servers = smartlist_create();
3710 router_dir_info_changed();
3713 /** Return 1 if any trusted dir server supports v1 directories,
3714 * else return 0. */
3716 any_trusted_dir_is_v1_authority(void)
3718 if (trusted_dir_servers)
3719 return get_n_authorities(V1_AUTHORITY) > 0;
3721 return 0;
3724 /** Return the network status with a given identity digest. */
3725 networkstatus_t *
3726 networkstatus_get_by_digest(const char *digest)
3728 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
3730 if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
3731 return ns;
3733 return NULL;
3736 /** DOCDOC */
3737 networkstatus_vote_t *
3738 networkstatus_get_latest_consensus(void)
3740 return current_consensus;
3743 /** DOCDOC */
3744 networkstatus_vote_t *
3745 networkstatus_get_live_consensus(time_t now)
3747 /* XXXX020 check for liveness */
3748 (void)now;
3749 return current_consensus;
3752 /** We believe networkstatuses more recent than this when they tell us that
3753 * our server is broken, invalid, obsolete, etc. */
3754 #define SELF_OPINION_INTERVAL (90*60)
3756 /** Return a newly allocated string naming the versions of Tor recommended by
3757 * more than half the versioning networkstatuses. */
3758 char *
3759 compute_recommended_versions(time_t now, int client,
3760 const char *my_version,
3761 combined_version_status_t *status_out)
3763 int n_seen;
3764 char *current;
3765 smartlist_t *combined, *recommended;
3766 int n_versioning, n_recommending;
3767 char *result;
3768 /** holds the compromise status taken among all non-recommending
3769 * authorities */
3770 version_status_t consensus = VS_RECOMMENDED;
3771 (void) now; /* right now, we consider *all* statuses, regardless of age. */
3773 tor_assert(my_version);
3774 tor_assert(status_out);
3776 memset(status_out, 0, sizeof(combined_version_status_t));
3778 if (!networkstatus_list)
3779 return tor_strdup("<none>");
3781 combined = smartlist_create();
3782 n_versioning = n_recommending = 0;
3783 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
3785 const char *vers;
3786 smartlist_t *versions;
3787 version_status_t status;
3788 if (! ns->recommends_versions)
3789 continue;
3790 n_versioning++;
3791 vers = client ? ns->client_versions : ns->server_versions;
3792 if (!vers)
3793 continue;
3794 versions = smartlist_create();
3795 smartlist_split_string(versions, vers, ",",
3796 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3797 sort_version_list(versions, 1);
3798 smartlist_add_all(combined, versions);
3799 smartlist_free(versions);
3801 /* now, check _our_ version */
3802 status = tor_version_is_obsolete(my_version, vers);
3803 if (status == VS_RECOMMENDED)
3804 n_recommending++;
3805 consensus = version_status_join(status, consensus);
3808 sort_version_list(combined, 0);
3810 current = NULL;
3811 n_seen = 0;
3812 recommended = smartlist_create();
3813 SMARTLIST_FOREACH(combined, char *, cp,
3815 if (current && !strcmp(cp, current)) {
3816 ++n_seen;
3817 } else {
3818 if (current)
3819 log_info(LD_DIR,"version %s is recommended by %d authorities",
3820 current, n_seen);
3821 if (n_seen > n_versioning/2 && current) {
3822 smartlist_add(recommended, current);
3824 n_seen = 1;
3825 current = cp;
3828 if (current)
3829 log_info(LD_DIR,"version %s is recommended by %d authorities",
3830 current, n_seen);
3831 if (n_seen > n_versioning/2 && current)
3832 smartlist_add(recommended, current);
3834 result = smartlist_join_strings(recommended, ", ", 0, NULL);
3836 SMARTLIST_FOREACH(combined, char *, cp, tor_free(cp));
3837 smartlist_free(combined);
3838 smartlist_free(recommended);
3840 status_out->n_versioning = n_versioning;
3841 if (n_recommending > n_versioning/2) {
3842 status_out->consensus = VS_RECOMMENDED;
3843 status_out->n_concurring = n_recommending;
3844 } else {
3845 status_out->consensus = consensus;
3846 status_out->n_concurring = n_versioning - n_recommending;
3849 return result;
3852 /** How many times do we have to fail at getting a networkstatus we can't find
3853 * before we're willing to believe it's okay to set up router statuses? */
3854 #define N_NS_ATTEMPTS_TO_SET_ROUTERS 4
3855 /** How many times do we have to fail at getting a networkstatus we can't find
3856 * before we're willing to believe it's okay to check our version? */
3857 #define N_NS_ATTEMPTS_TO_CHECK_VERSION 4
3859 /** If the network-status list has changed since the last time we called this
3860 * function, update the status of every routerinfo from the network-status
3861 * list.
3863 void
3864 routers_update_all_from_networkstatus(time_t now)
3866 routerinfo_t *me;
3867 if (!routerlist || !networkstatus_list ||
3868 (!networkstatus_list_has_changed && !routerstatus_list_has_changed))
3869 return;
3871 router_dir_info_changed();
3873 if (networkstatus_list_has_changed)
3874 routerstatus_list_update_from_networkstatus(now);
3876 routers_update_status_from_networkstatus(routerlist->routers, 0);
3878 me = router_get_my_routerinfo();
3879 if (me && !have_warned_about_invalid_status &&
3880 have_tried_downloading_all_statuses(N_NS_ATTEMPTS_TO_SET_ROUTERS)) {
3881 int n_recent = 0, n_listing = 0, n_valid = 0, n_named = 0, n_naming = 0;
3882 routerstatus_t *rs;
3883 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
3885 if (ns->received_on + SELF_OPINION_INTERVAL < now)
3886 continue;
3887 ++n_recent;
3888 if (ns->binds_names)
3889 ++n_naming;
3890 if (!(rs = networkstatus_find_entry(ns, me->cache_info.identity_digest)))
3891 continue;
3892 ++n_listing;
3893 if (rs->is_valid)
3894 ++n_valid;
3895 if (rs->is_named)
3896 ++n_named;
3899 if (n_listing) {
3900 if (n_valid <= n_listing/2) {
3901 log_info(LD_GENERAL,
3902 "%d/%d recent statements from directory authorities list us "
3903 "as unapproved. Are you misconfigured?",
3904 n_listing-n_valid, n_listing);
3905 have_warned_about_invalid_status = 1;
3906 } else if (n_naming && !n_named) {
3907 log_info(LD_GENERAL, "0/%d name-binding directory authorities "
3908 "recognize your nickname. Please consider sending your "
3909 "nickname and identity fingerprint to the tor-ops.",
3910 n_naming);
3911 have_warned_about_invalid_status = 1;
3916 entry_guards_compute_status();
3918 if (!have_warned_about_old_version &&
3919 have_tried_downloading_all_statuses(N_NS_ATTEMPTS_TO_CHECK_VERSION)) {
3920 combined_version_status_t st;
3921 int is_server = server_mode(get_options());
3922 char *recommended;
3924 recommended = compute_recommended_versions(now, !is_server, VERSION, &st);
3926 if (st.n_versioning) {
3927 if (st.consensus == VS_RECOMMENDED) {
3928 log_info(LD_GENERAL, "%d/%d statements from version-listing "
3929 "directory authorities say my version is ok.",
3930 st.n_concurring, st.n_versioning);
3931 } else if (st.consensus == VS_NEW || st.consensus == VS_NEW_IN_SERIES) {
3932 if (!have_warned_about_new_version) {
3933 log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
3934 "recommended version%s, according to %d/%d version-listing "
3935 "network statuses. Versions recommended by more than %d "
3936 "authorit%s are: %s",
3937 VERSION,
3938 st.consensus == VS_NEW_IN_SERIES ? " in its series" : "",
3939 st.n_concurring, st.n_versioning, st.n_versioning/2,
3940 st.n_versioning/2 > 1 ? "ies" : "y", recommended);
3941 have_warned_about_new_version = 1;
3942 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
3943 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
3944 VERSION, "NEW", recommended);
3946 } else {
3947 log_warn(LD_GENERAL, "Please upgrade! "
3948 "This version of Tor (%s) is %s, according to %d/%d version-"
3949 "listing network statuses. Versions recommended by "
3950 "at least %d authorit%s are: %s",
3951 VERSION,
3952 st.consensus == VS_OLD ? "obsolete" : "not recommended",
3953 st.n_concurring, st.n_versioning, st.n_versioning/2,
3954 st.n_versioning/2 > 1 ? "ies" : "y", recommended);
3955 have_warned_about_old_version = 1;
3956 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
3957 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
3958 VERSION, st.consensus == VS_OLD ? "OLD" : "UNRECOMMENDED",
3959 recommended);
3962 tor_free(recommended);
3965 routerstatus_list_has_changed = 0;
3968 /** Allow any network-status newer than this to influence our view of who's
3969 * running. */
3970 #define DEFAULT_RUNNING_INTERVAL (60*60)
3971 /** If possible, always allow at least this many network-statuses to influence
3972 * our view of who's running. */
3973 #define MIN_TO_INFLUENCE_RUNNING 3
3975 /** Change the is_recent field of each member of networkstatus_list so that
3976 * all members more recent than DEFAULT_RUNNING_INTERVAL are recent, and
3977 * at least the MIN_TO_INFLUENCE_RUNNING most recent members are recent, and no
3978 * others are recent. Set networkstatus_list_has_changed if anything happened.
3980 void
3981 networkstatus_list_update_recent(time_t now)
3983 int n_statuses, n_recent, changed, i;
3984 char published[ISO_TIME_LEN+1];
3986 if (!networkstatus_list)
3987 return;
3989 n_statuses = smartlist_len(networkstatus_list);
3990 n_recent = 0;
3991 changed = 0;
3992 for (i=n_statuses-1; i >= 0; --i) {
3993 networkstatus_t *ns = smartlist_get(networkstatus_list, i);
3994 trusted_dir_server_t *ds =
3995 router_get_trusteddirserver_by_digest(ns->identity_digest);
3996 const char *src = ds?ds->description:ns->source_address;
3997 if (n_recent < MIN_TO_INFLUENCE_RUNNING ||
3998 ns->published_on + DEFAULT_RUNNING_INTERVAL > now) {
3999 if (!ns->is_recent) {
4000 format_iso_time(published, ns->published_on);
4001 log_info(LD_DIR,
4002 "Networkstatus from %s (published %s) is now \"recent\"",
4003 src, published);
4004 changed = 1;
4006 ns->is_recent = 1;
4007 ++n_recent;
4008 } else {
4009 if (ns->is_recent) {
4010 format_iso_time(published, ns->published_on);
4011 log_info(LD_DIR,
4012 "Networkstatus from %s (published %s) is "
4013 "no longer \"recent\"",
4014 src, published);
4015 changed = 1;
4016 ns->is_recent = 0;
4020 if (changed) {
4021 networkstatus_list_has_changed = 1;
4022 router_dir_info_changed();
4026 /** Helper for routerstatus_list_update_from_networkstatus: remember how many
4027 * authorities recommend a given descriptor digest. */
4028 typedef struct {
4029 routerstatus_t *rs;
4030 int count;
4031 } desc_digest_count_t;
4033 /** Update our view of router status (as stored in routerstatus_list) from the
4034 * current set of network status documents (as stored in networkstatus_list).
4035 * Do nothing unless the network status list has changed since the last time
4036 * this function was called.
4038 static void
4039 routerstatus_list_update_from_networkstatus(time_t now)
4041 or_options_t *options = get_options();
4042 int n_trusted, n_statuses, n_recent = 0, n_naming = 0;
4043 int n_listing_bad_exits = 0, n_listing_bad_directories = 0;
4044 int i, j, warned;
4045 int *index, *size;
4046 networkstatus_t **networkstatus;
4047 smartlist_t *result, *changed_list;
4048 strmap_t *name_map;
4049 char conflict[DIGEST_LEN]; /* Sentinel value */
4050 desc_digest_count_t *digest_counts = NULL;
4052 /* compute which network statuses will have a vote now */
4053 networkstatus_list_update_recent(now);
4054 router_dir_info_changed();
4056 if (!networkstatus_list_has_changed)
4057 return;
4058 if (!networkstatus_list)
4059 networkstatus_list = smartlist_create();
4060 if (!routerstatus_list)
4061 routerstatus_list = smartlist_create();
4062 if (!trusted_dir_servers)
4063 trusted_dir_servers = smartlist_create();
4064 if (!warned_conflicts)
4065 warned_conflicts = smartlist_create();
4067 n_statuses = smartlist_len(networkstatus_list);
4068 n_trusted = get_n_v2_authorities();
4070 if (n_statuses <= n_trusted/2) {
4071 /* Not enough statuses to adjust status. */
4072 log_info(LD_DIR,
4073 "Not enough statuses to update router status list. (%d/%d)",
4074 n_statuses, n_trusted);
4075 return;
4078 log_info(LD_DIR, "Rebuilding router status list.");
4080 index = tor_malloc(sizeof(int)*n_statuses);
4081 size = tor_malloc(sizeof(int)*n_statuses);
4082 networkstatus = tor_malloc(sizeof(networkstatus_t *)*n_statuses);
4083 for (i = 0; i < n_statuses; ++i) {
4084 index[i] = 0;
4085 networkstatus[i] = smartlist_get(networkstatus_list, i);
4086 size[i] = smartlist_len(networkstatus[i]->entries);
4087 if (networkstatus[i]->binds_names)
4088 ++n_naming;
4089 if (networkstatus[i]->is_recent)
4090 ++n_recent;
4091 if (networkstatus[i]->lists_bad_exits)
4092 ++n_listing_bad_exits;
4093 if (networkstatus[i]->lists_bad_directories)
4094 ++n_listing_bad_directories;
4097 /** Iterate over all entries in all networkstatuses, and build
4098 * name_map as a map from lc nickname to identity digest. If there
4099 * is a conflict on that nickname, map the lc nickname to conflict.
4101 name_map = strmap_new();
4102 /* Clear the global map... */
4103 if (named_server_map)
4104 strmap_free(named_server_map, _tor_free);
4105 named_server_map = strmap_new();
4106 memset(conflict, 0xff, sizeof(conflict));
4107 for (i = 0; i < n_statuses; ++i) {
4108 if (!networkstatus[i]->binds_names)
4109 continue;
4110 SMARTLIST_FOREACH(networkstatus[i]->entries, routerstatus_t *, rs,
4112 const char *other_digest;
4113 if (!rs->is_named)
4114 continue;
4115 other_digest = strmap_get_lc(name_map, rs->nickname);
4116 warned = smartlist_string_isin(warned_conflicts, rs->nickname);
4117 if (!other_digest) {
4118 strmap_set_lc(name_map, rs->nickname, rs->identity_digest);
4119 strmap_set_lc(named_server_map, rs->nickname,
4120 tor_memdup(rs->identity_digest, DIGEST_LEN));
4121 if (warned)
4122 smartlist_string_remove(warned_conflicts, rs->nickname);
4123 } else if (memcmp(other_digest, rs->identity_digest, DIGEST_LEN) &&
4124 other_digest != conflict) {
4125 if (!warned) {
4126 char *d;
4127 int should_warn = options->DirPort && authdir_mode(options);
4128 char fp1[HEX_DIGEST_LEN+1];
4129 char fp2[HEX_DIGEST_LEN+1];
4130 base16_encode(fp1, sizeof(fp1), other_digest, DIGEST_LEN);
4131 base16_encode(fp2, sizeof(fp2), rs->identity_digest, DIGEST_LEN);
4132 log_fn(should_warn ? LOG_WARN : LOG_INFO, LD_DIR,
4133 "Naming authorities disagree about which key goes with %s. "
4134 "($%s vs $%s)",
4135 rs->nickname, fp1, fp2);
4136 strmap_set_lc(name_map, rs->nickname, conflict);
4137 d = strmap_remove_lc(named_server_map, rs->nickname);
4138 tor_free(d);
4139 smartlist_add(warned_conflicts, tor_strdup(rs->nickname));
4141 } else {
4142 if (warned)
4143 smartlist_string_remove(warned_conflicts, rs->nickname);
4148 result = smartlist_create();
4149 changed_list = smartlist_create();
4150 digest_counts = tor_malloc_zero(sizeof(desc_digest_count_t)*n_statuses);
4152 /* Iterate through all of the sorted routerstatus lists in lockstep.
4153 * Invariants:
4154 * - For 0 <= i < n_statuses: index[i] is an index into
4155 * networkstatus[i]->entries, which has size[i] elements.
4156 * - For i1, i2, j such that 0 <= i1 < n_statuses, 0 <= i2 < n_statues, 0 <=
4157 * j < index[i1]: networkstatus[i1]->entries[j]->identity_digest <
4158 * networkstatus[i2]->entries[index[i2]]->identity_digest.
4160 * (That is, the indices are always advanced past lower digest before
4161 * higher.)
4163 while (1) {
4164 int n_running=0, n_named=0, n_valid=0, n_listing=0;
4165 int n_v2_dir=0, n_fast=0, n_stable=0, n_exit=0, n_guard=0, n_bad_exit=0;
4166 int n_bad_directory=0;
4167 int n_version_known=0, n_supports_begindir=0;
4168 int n_supports_extrainfo_upload=0;
4169 int n_desc_digests=0, highest_count=0;
4170 const char *the_name = NULL;
4171 local_routerstatus_t *rs_out, *rs_old;
4172 routerstatus_t *rs, *most_recent;
4173 networkstatus_t *ns;
4174 const char *lowest = NULL;
4176 /* Find out which of the digests appears first. */
4177 for (i = 0; i < n_statuses; ++i) {
4178 if (index[i] < size[i]) {
4179 rs = smartlist_get(networkstatus[i]->entries, index[i]);
4180 if (!lowest || memcmp(rs->identity_digest, lowest, DIGEST_LEN)<0)
4181 lowest = rs->identity_digest;
4184 if (!lowest) {
4185 /* We're out of routers. Great! */
4186 break;
4188 /* Okay. The routers at networkstatus[i]->entries[index[i]] whose digests
4189 * match "lowest" are next in order. Iterate over them, incrementing those
4190 * index[i] as we go. */
4191 for (i = 0; i < n_statuses; ++i) {
4192 if (index[i] >= size[i])
4193 continue;
4194 ns = networkstatus[i];
4195 rs = smartlist_get(ns->entries, index[i]);
4196 if (memcmp(rs->identity_digest, lowest, DIGEST_LEN))
4197 continue;
4198 /* At this point, we know that we're looking at a routersatus with
4199 * identity "lowest".
4201 ++index[i];
4202 ++n_listing;
4203 /* Should we name this router? Only if all the names from naming
4204 * authorities match. */
4205 if (rs->is_named && ns->binds_names) {
4206 if (!the_name)
4207 the_name = rs->nickname;
4208 if (!strcasecmp(rs->nickname, the_name)) {
4209 ++n_named;
4210 } else if (strcmp(the_name,"**mismatch**")) {
4211 char hd[HEX_DIGEST_LEN+1];
4212 base16_encode(hd, HEX_DIGEST_LEN+1, rs->identity_digest, DIGEST_LEN);
4213 if (! smartlist_string_isin(warned_conflicts, hd)) {
4214 log_warn(LD_DIR,
4215 "Naming authorities disagree about nicknames for $%s "
4216 "(\"%s\" vs \"%s\")",
4217 hd, the_name, rs->nickname);
4218 smartlist_add(warned_conflicts, tor_strdup(hd));
4220 the_name = "**mismatch**";
4223 /* Keep a running count of how often which descriptor digests
4224 * appear. */
4225 for (j = 0; j < n_desc_digests; ++j) {
4226 if (!memcmp(rs->descriptor_digest,
4227 digest_counts[j].rs->descriptor_digest, DIGEST_LEN)) {
4228 if (++digest_counts[j].count > highest_count)
4229 highest_count = digest_counts[j].count;
4230 goto found;
4233 digest_counts[n_desc_digests].rs = rs;
4234 digest_counts[n_desc_digests].count = 1;
4235 if (!highest_count)
4236 highest_count = 1;
4237 ++n_desc_digests;
4238 found:
4239 /* Now tally up the easily-tallied flags. */
4240 if (rs->is_valid)
4241 ++n_valid;
4242 if (rs->is_running && ns->is_recent)
4243 ++n_running;
4244 if (rs->is_exit)
4245 ++n_exit;
4246 if (rs->is_fast)
4247 ++n_fast;
4248 if (rs->is_possible_guard)
4249 ++n_guard;
4250 if (rs->is_stable)
4251 ++n_stable;
4252 if (rs->is_v2_dir)
4253 ++n_v2_dir;
4254 if (rs->is_bad_exit)
4255 ++n_bad_exit;
4256 if (rs->is_bad_directory)
4257 ++n_bad_directory;
4258 if (rs->version_known)
4259 ++n_version_known;
4260 if (rs->version_supports_begindir)
4261 ++n_supports_begindir;
4262 if (rs->version_supports_extrainfo_upload)
4263 ++n_supports_extrainfo_upload;
4265 /* Go over the descriptor digests and figure out which descriptor we
4266 * want. */
4267 most_recent = NULL;
4268 for (i = 0; i < n_desc_digests; ++i) {
4269 /* If any digest appears twice or more, ignore those that don't.*/
4270 if (highest_count >= 2 && digest_counts[i].count < 2)
4271 continue;
4272 if (!most_recent ||
4273 digest_counts[i].rs->published_on > most_recent->published_on)
4274 most_recent = digest_counts[i].rs;
4276 rs_out = tor_malloc_zero(sizeof(local_routerstatus_t));
4277 memcpy(&rs_out->status, most_recent, sizeof(routerstatus_t));
4278 /* Copy status info about this router, if we had any before. */
4279 if ((rs_old = router_get_combined_status_by_digest(lowest))) {
4280 if (!memcmp(rs_out->status.descriptor_digest,
4281 most_recent->descriptor_digest, DIGEST_LEN)) {
4282 rs_out->dl_status.n_download_failures =
4283 rs_old->dl_status.n_download_failures;
4284 rs_out->dl_status.next_attempt_at = rs_old->dl_status.next_attempt_at;
4286 rs_out->name_lookup_warned = rs_old->name_lookup_warned;
4287 rs_out->last_dir_503_at = rs_old->last_dir_503_at;
4289 smartlist_add(result, rs_out);
4290 log_debug(LD_DIR, "Router '%s' is listed by %d/%d directories, "
4291 "named by %d/%d, validated by %d/%d, and %d/%d recent "
4292 "directories think it's running.",
4293 rs_out->status.nickname,
4294 n_listing, n_statuses, n_named, n_naming, n_valid, n_statuses,
4295 n_running, n_recent);
4296 rs_out->status.is_named = 0;
4297 if (the_name && strcmp(the_name, "**mismatch**") && n_named > 0) {
4298 const char *d = strmap_get_lc(name_map, the_name);
4299 if (d && d != conflict)
4300 rs_out->status.is_named = 1;
4301 if (smartlist_string_isin(warned_conflicts, rs_out->status.nickname))
4302 smartlist_string_remove(warned_conflicts, rs_out->status.nickname);
4304 if (rs_out->status.is_named)
4305 strlcpy(rs_out->status.nickname, the_name,
4306 sizeof(rs_out->status.nickname));
4307 rs_out->status.is_valid = n_valid > n_statuses/2;
4308 rs_out->status.is_running = n_running > n_recent/2;
4309 rs_out->status.is_exit = n_exit > n_statuses/2;
4310 rs_out->status.is_fast = n_fast > n_statuses/2;
4311 rs_out->status.is_possible_guard = n_guard > n_statuses/2;
4312 rs_out->status.is_stable = n_stable > n_statuses/2;
4313 rs_out->status.is_v2_dir = n_v2_dir > n_statuses/2;
4314 rs_out->status.is_bad_exit = n_bad_exit > n_listing_bad_exits/2;
4315 rs_out->status.is_bad_directory =
4316 n_bad_directory > n_listing_bad_directories/2;
4317 rs_out->status.version_known = n_version_known > 0;
4318 rs_out->status.version_supports_begindir =
4319 n_supports_begindir > n_version_known/2;
4320 rs_out->status.version_supports_extrainfo_upload =
4321 n_supports_extrainfo_upload > n_version_known/2;
4322 if (!rs_old || memcmp(rs_old, rs_out, sizeof(local_routerstatus_t)))
4323 smartlist_add(changed_list, rs_out);
4325 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
4326 local_routerstatus_free(rs));
4328 smartlist_free(routerstatus_list);
4329 routerstatus_list = result;
4331 if (routerstatus_by_desc_digest_map)
4332 digestmap_free(routerstatus_by_desc_digest_map, NULL);
4333 routerstatus_by_desc_digest_map = digestmap_new();
4334 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
4335 digestmap_set(routerstatus_by_desc_digest_map,
4336 rs->status.descriptor_digest,
4337 rs));
4339 tor_free(networkstatus);
4340 tor_free(index);
4341 tor_free(size);
4342 tor_free(digest_counts);
4343 strmap_free(name_map, NULL);
4345 networkstatus_list_has_changed = 0;
4346 routerstatus_list_has_changed = 1;
4348 control_event_networkstatus_changed(changed_list);
4349 smartlist_free(changed_list);
4352 /** Given a list <b>routers</b> of routerinfo_t *, update each routers's
4353 * is_named, is_valid, and is_running fields according to our current
4354 * networkstatus_t documents. */
4355 void
4356 routers_update_status_from_networkstatus(smartlist_t *routers,
4357 int reset_failures)
4359 trusted_dir_server_t *ds;
4360 local_routerstatus_t *rs;
4361 or_options_t *options = get_options();
4362 int authdir = authdir_mode_v2(options);
4363 int namingdir = authdir && options->NamingAuthoritativeDir;
4365 if (!routerstatus_list)
4366 return;
4368 SMARTLIST_FOREACH(routers, routerinfo_t *, router,
4370 const char *digest = router->cache_info.identity_digest;
4371 rs = router_get_combined_status_by_digest(digest);
4372 ds = router_get_trusteddirserver_by_digest(digest);
4374 if (!rs)
4375 continue;
4377 if (!namingdir)
4378 router->is_named = rs->status.is_named;
4380 if (!authdir) {
4381 /* If we're not an authdir, believe others. */
4382 router->is_valid = rs->status.is_valid;
4383 router->is_running = rs->status.is_running;
4384 router->is_fast = rs->status.is_fast;
4385 router->is_stable = rs->status.is_stable;
4386 router->is_possible_guard = rs->status.is_possible_guard;
4387 router->is_exit = rs->status.is_exit;
4388 router->is_bad_exit = rs->status.is_bad_exit;
4390 if (router->is_running && ds) {
4391 ds->n_networkstatus_failures = 0;
4393 if (reset_failures) {
4394 rs->dl_status.n_download_failures = 0;
4395 rs->dl_status.next_attempt_at = 0;
4398 router_dir_info_changed();
4401 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
4402 * true) we are currently downloading by descriptor digest, set result[d] to
4403 * (void*)1. */
4404 static void
4405 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
4407 const char *prefix = "d/";
4408 size_t p_len = strlen(prefix);
4409 smartlist_t *tmp = smartlist_create();
4410 int purpose =
4411 extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC;
4412 smartlist_t *conns = get_connection_array();
4414 tor_assert(result);
4416 SMARTLIST_FOREACH(conns, connection_t *, conn,
4418 if (conn->type == CONN_TYPE_DIR &&
4419 conn->purpose == purpose &&
4420 !conn->marked_for_close) {
4421 const char *resource = TO_DIR_CONN(conn)->requested_resource;
4422 if (!strcmpstart(resource, prefix))
4423 dir_split_resource_into_fingerprints(resource + p_len,
4424 tmp, NULL, 1, 0);
4427 SMARTLIST_FOREACH(tmp, char *, d,
4429 digestmap_set(result, d, (void*)1);
4430 tor_free(d);
4432 smartlist_free(tmp);
4435 /** Launch downloads for all the descriptors whose digests are listed
4436 * as digests[i] for lo <= i < hi. (Lo and hi may be out of range.)
4437 * If <b>source</b> is given, download from <b>source</b>; otherwise,
4438 * download from an appropriate random directory server.
4440 static void
4441 initiate_descriptor_downloads(routerstatus_t *source,
4442 int purpose,
4443 smartlist_t *digests,
4444 int lo, int hi)
4446 int i, n = hi-lo;
4447 char *resource, *cp;
4448 size_t r_len;
4449 if (n <= 0)
4450 return;
4451 if (lo < 0)
4452 lo = 0;
4453 if (hi > smartlist_len(digests))
4454 hi = smartlist_len(digests);
4456 r_len = 8 + (HEX_DIGEST_LEN+1)*n;
4457 cp = resource = tor_malloc(r_len);
4458 memcpy(cp, "d/", 2);
4459 cp += 2;
4460 for (i = lo; i < hi; ++i) {
4461 base16_encode(cp, r_len-(cp-resource),
4462 smartlist_get(digests,i), DIGEST_LEN);
4463 cp += HEX_DIGEST_LEN;
4464 *cp++ = '+';
4466 memcpy(cp-1, ".z", 3);
4468 if (source) {
4469 /* We know which authority we want. */
4470 directory_initiate_command_routerstatus(source, purpose,
4471 ROUTER_PURPOSE_GENERAL,
4472 0, /* not private */
4473 resource, NULL, 0);
4474 } else {
4475 directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource, 1);
4477 tor_free(resource);
4480 /** Clients don't download any descriptor this recent, since it will probably
4481 * not have propagated to enough caches. */
4482 #define ESTIMATED_PROPAGATION_TIME (10*60)
4484 /** Return 0 if this routerstatus is obsolete, too new, isn't
4485 * running, or otherwise not a descriptor that we would make any
4486 * use of even if we had it. Else return 1. */
4487 static INLINE int
4488 client_would_use_router(routerstatus_t *rs, time_t now, or_options_t *options)
4490 if (!rs->is_running && !options->FetchUselessDescriptors) {
4491 /* If we had this router descriptor, we wouldn't even bother using it.
4492 * But, if we want to have a complete list, fetch it anyway. */
4493 return 0;
4495 if (rs->published_on + ESTIMATED_PROPAGATION_TIME > now) {
4496 /* Most caches probably don't have this descriptor yet. */
4497 return 0;
4499 return 1;
4502 /** Return new list of ID fingerprints for routers that we (as a client) would
4503 * like to download.
4505 static smartlist_t *
4506 router_list_client_downloadable(void)
4508 int n_downloadable = 0;
4509 smartlist_t *downloadable = smartlist_create();
4510 digestmap_t *downloading;
4511 time_t now = time(NULL);
4512 /* these are just used for logging */
4513 int n_not_ready = 0, n_in_progress = 0, n_uptodate = 0, n_wouldnt_use = 0;
4514 or_options_t *options = get_options();
4516 if (!routerstatus_list)
4517 return downloadable;
4519 downloading = digestmap_new();
4520 list_pending_descriptor_downloads(downloading, 0);
4522 routerstatus_list_update_from_networkstatus(now);
4523 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
4525 routerinfo_t *ri;
4526 if (router_get_by_descriptor_digest(rs->status.descriptor_digest)) {
4527 /* We have the 'best' descriptor for this router. */
4528 ++n_uptodate;
4529 } else if (!client_would_use_router(&rs->status, now, options)) {
4530 /* We wouldn't want this descriptor even if we got it. */
4531 ++n_wouldnt_use;
4532 } else if (digestmap_get(downloading, rs->status.descriptor_digest)) {
4533 /* We're downloading this one now. */
4534 ++n_in_progress;
4535 } else if ((ri = router_get_by_digest(rs->status.identity_digest)) &&
4536 ri->cache_info.published_on > rs->status.published_on) {
4537 /* Oddly, we have a descriptor more recent than the 'best' one, but it
4538 was once best. So that's okay. */
4539 ++n_uptodate;
4540 } else if (rs->dl_status.next_attempt_at > now) {
4541 /* We failed too recently to try again. */
4542 ++n_not_ready;
4543 } else {
4544 /* Okay, time to try it. */
4545 smartlist_add(downloadable, rs->status.descriptor_digest);
4546 ++n_downloadable;
4550 #if 0
4551 log_info(LD_DIR,
4552 "%d router descriptors are downloadable. "
4553 "%d are in progress. %d are up-to-date. "
4554 "%d are non-useful. %d failed too recently to retry.",
4555 n_downloadable, n_in_progress, n_uptodate,
4556 n_wouldnt_use, n_not_ready);
4557 #endif
4559 digestmap_free(downloading, NULL);
4560 return downloadable;
4563 /** Initiate new router downloads as needed, using the strategy for
4564 * non-directory-servers.
4566 * We don't launch any downloads if there are fewer than MAX_DL_TO_DELAY
4567 * descriptors to get and less than MAX_CLIENT_INTERVAL_WITHOUT_REQUEST
4568 * seconds have passed.
4570 * Otherwise, we ask for all descriptors that we think are different from what
4571 * we have, and that we don't currently have an in-progress download attempt
4572 * for. */
4573 static void
4574 update_router_descriptor_client_downloads(time_t now)
4576 /** Max amount of hashes to download per request.
4577 * Since squid does not like URLs >= 4096 bytes we limit it to 96.
4578 * 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
4579 * 4058/41 (40 for the hash and 1 for the + that separates them) => 98
4580 * So use 96 because it's a nice number.
4582 #define MAX_DL_PER_REQUEST 96
4583 /** Don't split our requests so finely that we are requesting fewer than
4584 * this number per server. */
4585 #define MIN_DL_PER_REQUEST 4
4586 /** To prevent a single screwy cache from confusing us by selective reply,
4587 * try to split our requests into at least this this many requests. */
4588 #define MIN_REQUESTS 3
4589 /** If we want fewer than this many descriptors, wait until we
4590 * want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
4591 * passed. */
4592 #define MAX_DL_TO_DELAY 16
4593 /** When directory clients have only a few servers to request, they batch
4594 * them until they have more, or until this amount of time has passed. */
4595 #define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
4596 smartlist_t *downloadable = NULL;
4597 int should_delay, n_downloadable;
4598 or_options_t *options = get_options();
4600 if (options->DirPort) {
4601 log_warn(LD_BUG,
4602 "Called router_descriptor_client_downloads() on a dir mirror?");
4605 if (rep_hist_circbuilding_dormant(now)) {
4606 // log_info(LD_CIRC, "Skipping descriptor downloads: we haven't needed "
4607 // "any circuits lately.");
4608 return;
4611 if (networkstatus_list &&
4612 smartlist_len(networkstatus_list) <= get_n_v2_authorities()/2) {
4613 log_info(LD_DIR,
4614 "Not enough networkstatus documents to launch requests.");
4615 return;
4618 downloadable = router_list_client_downloadable();
4619 n_downloadable = smartlist_len(downloadable);
4620 if (n_downloadable >= MAX_DL_TO_DELAY) {
4621 log_debug(LD_DIR,
4622 "There are enough downloadable routerdescs to launch requests.");
4623 should_delay = 0;
4624 } else if (n_downloadable == 0) {
4625 // log_debug(LD_DIR, "No routerdescs need to be downloaded.");
4626 should_delay = 1;
4627 } else {
4628 should_delay = (last_routerdesc_download_attempted +
4629 MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
4630 if (!should_delay) {
4631 if (last_routerdesc_download_attempted) {
4632 log_info(LD_DIR,
4633 "There are not many downloadable routerdescs, but we've "
4634 "been waiting long enough (%d seconds). Downloading.",
4635 (int)(now-last_routerdesc_download_attempted));
4636 } else {
4637 log_info(LD_DIR,
4638 "There are not many downloadable routerdescs, but we haven't "
4639 "tried downloading descriptors recently. Downloading.");
4644 if (! should_delay) {
4645 int i, n_per_request;
4646 const char *req_plural = "", *rtr_plural = "";
4647 n_per_request = (n_downloadable+MIN_REQUESTS-1) / MIN_REQUESTS;
4648 if (n_per_request > MAX_DL_PER_REQUEST)
4649 n_per_request = MAX_DL_PER_REQUEST;
4650 if (n_per_request < MIN_DL_PER_REQUEST)
4651 n_per_request = MIN_DL_PER_REQUEST;
4653 if (n_downloadable > n_per_request)
4654 req_plural = rtr_plural = "s";
4655 else if (n_downloadable > 1)
4656 rtr_plural = "s";
4658 log_info(LD_DIR,
4659 "Launching %d request%s for %d router%s, %d at a time",
4660 (n_downloadable+n_per_request-1)/n_per_request,
4661 req_plural, n_downloadable, rtr_plural, n_per_request);
4662 smartlist_sort_digests(downloadable);
4663 for (i=0; i < n_downloadable; i += n_per_request) {
4664 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_SERVERDESC,
4665 downloadable, i, i+n_per_request);
4667 last_routerdesc_download_attempted = now;
4669 smartlist_free(downloadable);
4672 /** Launch downloads for router status as needed, using the strategy used by
4673 * authorities and caches: download every descriptor we don't have but would
4674 * serve, from a random authority that lists it. */
4675 static void
4676 update_router_descriptor_cache_downloads(time_t now)
4678 smartlist_t **downloadable; /* For each authority, what can we dl from it? */
4679 smartlist_t **download_from; /* ... and, what will we dl from it? */
4680 digestmap_t *map; /* Which descs are in progress, or assigned? */
4681 int i, j, n;
4682 int n_download;
4683 or_options_t *options = get_options();
4685 if (!options->DirPort) {
4686 log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads() "
4687 "on a non-dir-mirror?");
4690 if (!networkstatus_list || !smartlist_len(networkstatus_list))
4691 return;
4693 map = digestmap_new();
4694 n = smartlist_len(networkstatus_list);
4696 downloadable = tor_malloc_zero(sizeof(smartlist_t*) * n);
4697 download_from = tor_malloc_zero(sizeof(smartlist_t*) * n);
4699 /* Set map[d]=1 for the digest of every descriptor that we are currently
4700 * downloading. */
4701 list_pending_descriptor_downloads(map, 0);
4703 /* For the digest of every descriptor that we don't have, and that we aren't
4704 * downloading, add d to downloadable[i] if the i'th networkstatus knows
4705 * about that descriptor, and we haven't already failed to get that
4706 * descriptor from the corresponding authority.
4708 n_download = 0;
4709 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
4711 trusted_dir_server_t *ds;
4712 smartlist_t *dl;
4713 dl = downloadable[ns_sl_idx] = smartlist_create();
4714 download_from[ns_sl_idx] = smartlist_create();
4715 if (ns->published_on + MAX_NETWORKSTATUS_AGE+10*60 < now) {
4716 /* Don't download if the networkstatus is almost ancient. */
4717 /* Actually, I suspect what's happening here is that we ask
4718 * for the descriptor when we have a given networkstatus,
4719 * and then we get a newer networkstatus, and then we receive
4720 * the descriptor. Having a networkstatus actually expire is
4721 * probably a rare event, and we'll probably be happiest if
4722 * we take this clause out. -RD */
4723 continue;
4726 /* Don't try dirservers that we think are down -- we might have
4727 * just tried them and just marked them as down. */
4728 ds = router_get_trusteddirserver_by_digest(ns->identity_digest);
4729 if (ds && !ds->is_running)
4730 continue;
4732 SMARTLIST_FOREACH(ns->entries, routerstatus_t * , rs,
4734 if (!rs->need_to_mirror)
4735 continue;
4736 if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
4737 log_warn(LD_BUG,
4738 "We have a router descriptor, but need_to_mirror=1.");
4739 rs->need_to_mirror = 0;
4740 continue;
4742 if (authdir_mode(options) && dirserv_would_reject_router(rs)) {
4743 rs->need_to_mirror = 0;
4744 continue;
4746 if (digestmap_get(map, rs->descriptor_digest)) {
4747 /* We're downloading it already. */
4748 continue;
4749 } else {
4750 /* We could download it from this guy. */
4751 smartlist_add(dl, rs->descriptor_digest);
4752 ++n_download;
4757 /* At random, assign descriptors to authorities such that:
4758 * - if d is a member of some downloadable[x], d is a member of some
4759 * download_from[y]. (Everything we want to download, we try to download
4760 * from somebody.)
4761 * - If d is a member of download_from[y], d is a member of downloadable[y].
4762 * (We only try to download descriptors from authorities who claim to have
4763 * them.)
4764 * - No d is a member of download_from[x] and download_from[y] s.t. x != y.
4765 * (We don't try to download anything from two authorities concurrently.)
4767 while (n_download) {
4768 int which_ns = crypto_rand_int(n);
4769 smartlist_t *dl = downloadable[which_ns];
4770 int idx;
4771 char *d;
4772 if (!smartlist_len(dl))
4773 continue;
4774 idx = crypto_rand_int(smartlist_len(dl));
4775 d = smartlist_get(dl, idx);
4776 if (! digestmap_get(map, d)) {
4777 smartlist_add(download_from[which_ns], d);
4778 digestmap_set(map, d, (void*) 1);
4780 smartlist_del(dl, idx);
4781 --n_download;
4784 /* Now, we can actually launch our requests. */
4785 for (i=0; i<n; ++i) {
4786 networkstatus_t *ns = smartlist_get(networkstatus_list, i);
4787 trusted_dir_server_t *ds =
4788 router_get_trusteddirserver_by_digest(ns->identity_digest);
4789 smartlist_t *dl = download_from[i];
4790 if (!ds) {
4791 log_warn(LD_BUG, "Networkstatus with no corresponding authority!");
4792 continue;
4794 if (! smartlist_len(dl))
4795 continue;
4796 log_info(LD_DIR, "Requesting %d descriptors from authority \"%s\"",
4797 smartlist_len(dl), ds->nickname);
4798 for (j=0; j < smartlist_len(dl); j += MAX_DL_PER_REQUEST) {
4799 initiate_descriptor_downloads(&(ds->fake_status.status),
4800 DIR_PURPOSE_FETCH_SERVERDESC, dl, j,
4801 j+MAX_DL_PER_REQUEST);
4805 for (i=0; i<n; ++i) {
4806 smartlist_free(download_from[i]);
4807 smartlist_free(downloadable[i]);
4809 tor_free(download_from);
4810 tor_free(downloadable);
4811 digestmap_free(map,NULL);
4814 /** Launch downloads for router status as needed. */
4815 void
4816 update_router_descriptor_downloads(time_t now)
4818 or_options_t *options = get_options();
4819 if (should_delay_dir_fetches(options))
4820 return;
4821 if (options->DirPort) {
4822 update_router_descriptor_cache_downloads(now);
4823 } else {
4824 update_router_descriptor_client_downloads(now);
4828 /** Return true iff <b>sd</b> is the descriptor for a router descriptor that
4829 * has an extrainfo that we don't currently have, are not currently
4830 * downloading, and have not recently tried to download. */
4831 static INLINE int
4832 should_download_extrainfo(signed_descriptor_t *sd,
4833 const routerlist_t *rl,
4834 const digestmap_t *pending,
4835 time_t now)
4837 const char *d = sd->extra_info_digest;
4838 return (!sd->is_extrainfo &&
4839 !tor_digest_is_zero(d) &&
4840 sd->ei_dl_status.next_attempt_at <= now &&
4841 !eimap_get(rl->extra_info_map, d) &&
4842 !digestmap_get(pending, d));
4845 /** Launch extrainfo downloads as needed. */
4846 void
4847 update_extrainfo_downloads(time_t now)
4849 or_options_t *options = get_options();
4850 routerlist_t *rl;
4851 smartlist_t *wanted;
4852 digestmap_t *pending;
4853 int i;
4854 if (! options->DownloadExtraInfo)
4855 return;
4856 if (should_delay_dir_fetches(options))
4857 return;
4859 pending = digestmap_new();
4860 list_pending_descriptor_downloads(pending, 1);
4861 rl = router_get_routerlist();
4862 wanted = smartlist_create();
4863 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
4864 if (should_download_extrainfo(&ri->cache_info, rl, pending, now)) {
4865 smartlist_add(wanted, ri->cache_info.extra_info_digest);
4868 if (options->DirPort) {
4869 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd, {
4870 if (should_download_extrainfo(sd, rl, pending, now)) {
4871 smartlist_add(wanted, sd->extra_info_digest);
4875 digestmap_free(pending, NULL);
4877 smartlist_shuffle(wanted);
4878 for (i = 0; i < smartlist_len(wanted); i += MAX_DL_PER_REQUEST) {
4879 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
4880 wanted, i, i + MAX_DL_PER_REQUEST);
4883 smartlist_free(wanted);
4886 /** Return the number of routerstatus_t in <b>entries</b> that we'd actually
4887 * use. */
4888 static int
4889 routerstatus_count_usable_entries(smartlist_t *entries)
4891 int count = 0;
4892 time_t now = time(NULL);
4893 or_options_t *options = get_options();
4894 SMARTLIST_FOREACH(entries, routerstatus_t *, rs,
4895 if (client_would_use_router(rs, now, options)) count++);
4896 return count;
4899 /** True iff, the last time we checked whether we had enough directory info
4900 * to build circuits, the answer was "yes". */
4901 static int have_min_dir_info = 0;
4902 /** True iff enough has changed since the last time we checked whether we had
4903 * enough directory info to build circuits that our old answer can no longer
4904 * be trusted. */
4905 static int need_to_update_have_min_dir_info = 1;
4907 /** Return true iff we have enough networkstatus and router information to
4908 * start building circuits. Right now, this means "more than half the
4909 * networkstatus documents, and at least 1/4 of expected routers." */
4910 //XXX should consider whether we have enough exiting nodes here.
4912 router_have_minimum_dir_info(void)
4914 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
4915 update_router_have_minimum_dir_info();
4916 need_to_update_have_min_dir_info = 0;
4918 return have_min_dir_info;
4921 /** Called when our internal view of the directory has changed. This can be
4922 * when the authorities change, networkstatuses change, the list of routerdescs
4923 * changes, or number of running routers changes.
4925 static void
4926 router_dir_info_changed(void)
4928 need_to_update_have_min_dir_info = 1;
4931 /** Change the value of have_min_dir_info, setting it true iff we have enough
4932 * network and router information to build circuits. Clear the value of
4933 * need_to_update_have_min_dir_info. */
4934 static void
4935 update_router_have_minimum_dir_info(void)
4937 int tot = 0, num_running = 0;
4938 int n_ns, n_authorities, res, avg;
4939 time_t now = time(NULL);
4940 if (!networkstatus_list || !routerlist) {
4941 res = 0;
4942 goto done;
4944 routerlist_remove_old_routers();
4945 networkstatus_list_clean(now);
4947 if (should_delay_dir_fetches(get_options())) {
4948 log_notice(LD_DIR, "no bridge descs known yet");
4949 res = 0;
4950 goto done;
4953 n_authorities = get_n_v2_authorities();
4954 n_ns = smartlist_len(networkstatus_list);
4955 if (n_ns<=n_authorities/2) {
4956 log_info(LD_DIR,
4957 "We have %d of %d network statuses, and we want "
4958 "more than %d.", n_ns, n_authorities, n_authorities/2);
4959 res = 0;
4960 goto done;
4962 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
4963 tot += routerstatus_count_usable_entries(ns->entries));
4964 avg = tot / n_ns;
4965 if (!routerstatus_list)
4966 routerstatus_list = smartlist_create();
4967 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
4969 if (rs->status.is_running)
4970 num_running++;
4972 res = smartlist_len(routerlist->routers) >= (avg/4) && num_running > 2;
4973 done:
4974 if (res && !have_min_dir_info) {
4975 log(LOG_NOTICE, LD_DIR,
4976 "We now have enough directory information to build circuits.");
4977 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
4979 if (!res && have_min_dir_info) {
4980 log(LOG_NOTICE, LD_DIR,"Our directory information is no longer up-to-date "
4981 "enough to build circuits.%s",
4982 num_running > 2 ? "" : " (Not enough servers seem reachable -- "
4983 "is your network connection down?)");
4984 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
4986 have_min_dir_info = res;
4989 /** Return true iff we have downloaded, or attempted to download at least
4990 * n_failures times, a network status for each authority. */
4991 static int
4992 have_tried_downloading_all_statuses(int n_failures)
4994 if (!trusted_dir_servers)
4995 return 0;
4997 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
4999 if (!(ds->type & V2_AUTHORITY))
5000 continue;
5001 /* If we don't have the status, and we haven't failed to get the status,
5002 * we haven't tried to get the status. */
5003 if (!networkstatus_get_by_digest(ds->digest) &&
5004 ds->n_networkstatus_failures <= n_failures)
5005 return 0;
5008 return 1;
5011 /** Reset the descriptor download failure count on all routers, so that we
5012 * can retry any long-failed routers immediately.
5014 void
5015 router_reset_descriptor_download_failures(void)
5017 if (!routerstatus_list)
5018 return;
5019 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, rs,
5021 rs->dl_status.n_download_failures = 0;
5022 rs->dl_status.next_attempt_at = 0;
5024 tor_assert(networkstatus_list);
5025 SMARTLIST_FOREACH(networkstatus_list, networkstatus_t *, ns,
5026 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
5028 if (!router_get_by_descriptor_digest(rs->descriptor_digest))
5029 rs->need_to_mirror = 1;
5030 }));
5031 last_routerdesc_download_attempted = 0;
5032 if (!routerlist)
5033 return;
5034 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
5036 ri->cache_info.ei_dl_status.n_download_failures = 0;
5037 ri->cache_info.ei_dl_status.next_attempt_at = 0;
5039 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
5041 sd->ei_dl_status.n_download_failures = 0;
5042 sd->ei_dl_status.next_attempt_at = 0;
5047 /** Any changes in a router descriptor's publication time larger than this are
5048 * automatically non-cosmetic. */
5049 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
5051 /** We allow uptime to vary from how much it ought to be by this much. */
5052 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
5054 /** Return true iff the only differences between r1 and r2 are such that
5055 * would not cause a recent (post 0.1.1.6) dirserver to republish.
5058 router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2)
5060 time_t r1pub, r2pub;
5061 int time_difference;
5062 tor_assert(r1 && r2);
5064 /* r1 should be the one that was published first. */
5065 if (r1->cache_info.published_on > r2->cache_info.published_on) {
5066 routerinfo_t *ri_tmp = r2;
5067 r2 = r1;
5068 r1 = ri_tmp;
5071 /* If any key fields differ, they're different. */
5072 if (strcasecmp(r1->address, r2->address) ||
5073 strcasecmp(r1->nickname, r2->nickname) ||
5074 r1->or_port != r2->or_port ||
5075 r1->dir_port != r2->dir_port ||
5076 crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
5077 crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
5078 strcasecmp(r1->platform, r2->platform) ||
5079 (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
5080 (!r1->contact_info && r2->contact_info) ||
5081 (r1->contact_info && r2->contact_info &&
5082 strcasecmp(r1->contact_info, r2->contact_info)) ||
5083 r1->is_hibernating != r2->is_hibernating ||
5084 r1->has_old_dnsworkers != r2->has_old_dnsworkers ||
5085 cmp_addr_policies(r1->exit_policy, r2->exit_policy))
5086 return 0;
5087 if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
5088 return 0;
5089 if (r1->declared_family && r2->declared_family) {
5090 int i, n;
5091 if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
5092 return 0;
5093 n = smartlist_len(r1->declared_family);
5094 for (i=0; i < n; ++i) {
5095 if (strcasecmp(smartlist_get(r1->declared_family, i),
5096 smartlist_get(r2->declared_family, i)))
5097 return 0;
5101 /* Did bandwidth change a lot? */
5102 if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
5103 (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
5104 return 0;
5106 /* Did more than 12 hours pass? */
5107 if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
5108 < r2->cache_info.published_on)
5109 return 0;
5111 /* Did uptime fail to increase by approximately the amount we would think,
5112 * give or take some slop? */
5113 r1pub = r1->cache_info.published_on;
5114 r2pub = r2->cache_info.published_on;
5115 time_difference = abs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
5116 if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
5117 time_difference > r1->uptime * .05 &&
5118 time_difference > r2->uptime * .05)
5119 return 0;
5121 /* Otherwise, the difference is cosmetic. */
5122 return 1;
5125 /** Check whether <b>ri</b> is a router compatible with the extrainfo document
5126 * <b>ei</b>. If no router is compatible with <b>ei</b>, <b>ei</b> should be
5127 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
5128 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
5129 * <b>msg</b> is present, set *<b>msg</b> to a description of the
5130 * incompatibility (if any). */
5132 routerinfo_incompatible_with_extrainfo(routerinfo_t *ri, extrainfo_t *ei,
5133 const char **msg)
5135 tor_assert(ri);
5136 tor_assert(ei);
5138 if (ei->bad_sig) {
5139 if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
5140 return 1;
5143 /* The nickname must match exactly to have been generated at the same time
5144 * by the same router. */
5145 if (strcmp(ri->nickname, ei->nickname) ||
5146 memcmp(ri->cache_info.identity_digest, ei->cache_info.identity_digest,
5147 DIGEST_LEN)) {
5148 if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
5149 return 1; /* different servers */
5152 if (ei->pending_sig) {
5153 char signed_digest[128];
5154 if (crypto_pk_public_checksig(ri->identity_pkey, signed_digest,
5155 ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
5156 memcmp(signed_digest, ei->cache_info.signed_descriptor_digest,
5157 DIGEST_LEN)) {
5158 ei->bad_sig = 1;
5159 tor_free(ei->pending_sig);
5160 if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
5161 return 1; /* Bad signature, or no match. */
5164 tor_free(ei->pending_sig);
5167 if (ei->cache_info.published_on < ri->cache_info.published_on) {
5168 if (msg) *msg = "Extrainfo published time did not match routerdesc";
5169 return 1;
5170 } else if (ei->cache_info.published_on > ri->cache_info.published_on) {
5171 if (msg) *msg = "Extrainfo published time did not match routerdesc";
5172 return -1;
5175 if (memcmp(ei->cache_info.signed_descriptor_digest,
5176 ri->cache_info.extra_info_digest, DIGEST_LEN)) {
5177 if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
5178 return 1; /* Digest doesn't match declared value. */
5181 return 0;
5184 /** Generate networkstatus lines for a single routerstatus_t object, and
5185 * return the result in a newly allocated string. Used only by controller
5186 * interface (for now.) */
5187 char *
5188 networkstatus_getinfo_helper_single(routerstatus_t *rs)
5190 char buf[256];
5191 routerstatus_format_entry(buf, sizeof(buf), rs, NULL, 0);
5192 return tor_strdup(buf);
5195 /** If <b>question</b> is a string beginning with "ns/" in a format the
5196 * control interface expects for a GETINFO question, set *<b>answer</b> to a
5197 * newly-allocated string containing networkstatus lines for the appropriate
5198 * ORs. Return 0 on success, -1 on unrecognized question format. */
5200 getinfo_helper_networkstatus(control_connection_t *conn,
5201 const char *question, char **answer)
5203 local_routerstatus_t *status;
5204 (void) conn;
5206 if (!routerstatus_list) {
5207 *answer = tor_strdup("");
5208 return 0;
5211 if (!strcmp(question, "ns/all")) {
5212 smartlist_t *statuses = smartlist_create();
5213 SMARTLIST_FOREACH(routerstatus_list, local_routerstatus_t *, lrs,
5215 routerstatus_t *rs = &(lrs->status);
5216 smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
5218 *answer = smartlist_join_strings(statuses, "", 0, NULL);
5219 SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
5220 smartlist_free(statuses);
5221 return 0;
5222 } else if (!strcmpstart(question, "ns/id/")) {
5223 char d[DIGEST_LEN];
5225 if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6)))
5226 return -1;
5227 status = router_get_combined_status_by_digest(d);
5228 } else if (!strcmpstart(question, "ns/name/")) {
5229 status = router_get_combined_status_by_nickname(question+8, 0);
5230 } else {
5231 return -1;
5234 if (status) {
5235 *answer = networkstatus_getinfo_helper_single(&status->status);
5237 return 0;
5240 /** Assert that the internal representation of <b>rl</b> is
5241 * self-consistent. */
5242 void
5243 routerlist_assert_ok(routerlist_t *rl)
5245 digestmap_iter_t *iter; /* XXXX020 use the appropriate iter type. */
5246 routerinfo_t *r2;
5247 signed_descriptor_t *sd2;
5248 if (!rl)
5249 return;
5250 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
5252 r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
5253 tor_assert(r == r2);
5254 sd2 = sdmap_get(rl->desc_digest_map,
5255 r->cache_info.signed_descriptor_digest);
5256 tor_assert(&(r->cache_info) == sd2);
5257 tor_assert(r->routerlist_index == r_sl_idx);
5258 #if 0
5259 /* XXXX020.
5261 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
5262 * commenting this out is just a band-aid.
5264 * The problem is that, although well-behaved router descriptors
5265 * should never have the same value for their extra_info_digest, it's
5266 * possible for ill-behaved routers to claim whatever they like there.
5268 * The real answer is to trash desc_by_eid_map and instead have
5269 * something that indicates for a given extra-info digest we want,
5270 * what its download status is. We'll do that as a part of routerlist
5271 * refactoring once consensus directories are in. For now,
5272 * this rep violation is probably harmless: an adversary can make us
5273 * reset our retry count for an extrainfo, but that's not the end
5274 * of the world.
5276 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
5277 signed_descriptor_t *sd3 =
5278 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
5279 tor_assert(sd3 == &(r->cache_info));
5281 #endif
5283 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
5285 r2 = rimap_get(rl->identity_map, sd->identity_digest);
5286 tor_assert(sd != &(r2->cache_info));
5287 sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
5288 tor_assert(sd == sd2);
5289 #if 0
5290 /* XXXX020 see above. */
5291 if (!tor_digest_is_zero(sd->extra_info_digest)) {
5292 signed_descriptor_t *sd3 =
5293 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
5294 tor_assert(sd3 == sd);
5296 #endif
5299 iter = digestmap_iter_init((digestmap_t*)rl->identity_map);
5300 while (!digestmap_iter_done(iter)) {
5301 const char *d;
5302 void *_r;
5303 routerinfo_t *r;
5304 digestmap_iter_get(iter, &d, &_r);
5305 r = _r;
5306 tor_assert(!memcmp(r->cache_info.identity_digest, d, DIGEST_LEN));
5307 iter = digestmap_iter_next((digestmap_t*)rl->identity_map, iter);
5309 iter = digestmap_iter_init((digestmap_t*)rl->desc_digest_map);
5310 while (!digestmap_iter_done(iter)) {
5311 const char *d;
5312 void *_sd;
5313 signed_descriptor_t *sd;
5314 digestmap_iter_get(iter, &d, &_sd);
5315 sd = _sd;
5316 tor_assert(!memcmp(sd->signed_descriptor_digest, d, DIGEST_LEN));
5317 iter = digestmap_iter_next((digestmap_t*)rl->desc_digest_map, iter);
5319 iter = digestmap_iter_init((digestmap_t*)rl->desc_by_eid_map);
5320 while (!digestmap_iter_done(iter)) {
5321 const char *d;
5322 void *_sd;
5323 signed_descriptor_t *sd;
5324 digestmap_iter_get(iter, &d, &_sd);
5325 sd = _sd;
5326 tor_assert(!tor_digest_is_zero(d));
5327 tor_assert(sd);
5328 tor_assert(!memcmp(sd->extra_info_digest, d, DIGEST_LEN));
5329 iter = digestmap_iter_next((digestmap_t*)rl->desc_by_eid_map, iter);
5331 iter = digestmap_iter_init((digestmap_t*)rl->extra_info_map);
5332 while (!digestmap_iter_done(iter)) {
5333 const char *d;
5334 void *_ei;
5335 extrainfo_t *ei;
5336 signed_descriptor_t *sd;
5337 digestmap_iter_get(iter, &d, &_ei);
5338 ei = _ei;
5339 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
5340 d, DIGEST_LEN));
5341 sd = sdmap_get(rl->desc_by_eid_map,
5342 ei->cache_info.signed_descriptor_digest);
5343 // tor_assert(sd); // XXXX020 see above
5344 if (sd) {
5345 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
5346 sd->extra_info_digest, DIGEST_LEN));
5348 iter = digestmap_iter_next((digestmap_t*)rl->extra_info_map, iter);
5352 /** Debugging function. With any luck, we can remove this soon. Fail with an
5353 * assertion if the symptoms for bug 417/404 seem to be present. */
5354 void
5355 routerlist_check_bug_417(void)
5357 /* XXXX020 remove this function once bug 417/404 is fixed. */
5358 routerlist_t *rl = router_get_routerlist();
5359 routerinfo_t *ri_generated, *r2, *r;
5360 int idx = -1;
5362 ri_generated = router_get_my_routerinfo();
5363 if (!ri_generated)
5364 return;
5366 r = rimap_get(rl->identity_map,
5367 ri_generated->cache_info.identity_digest);
5368 if (r) {
5369 idx = r->routerlist_index;
5370 tor_assert(idx >= 0);
5371 tor_assert(idx < smartlist_len(rl->routers));
5372 r2 = smartlist_get(rl->routers, idx);
5373 tor_assert(r == r2);
5374 } else {
5375 #if 0
5376 /* Too slow; the bug seems to be in the former case anyway. */
5377 SMARTLIST_FROEACH(rl->routers, routerinfo_t *, ri,
5379 tor_assert(!router_is_me(ri));
5381 #endif
5384 tor_assert(ri_generated->routerlist_index == -1);
5387 /** Allocate and return a new string representing the contact info
5388 * and platform string for <b>router</b>,
5389 * surrounded by quotes and using standard C escapes.
5391 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
5392 * thread. Also, each call invalidates the last-returned value, so don't
5393 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
5395 * If <b>router</b> is NULL, it just frees its internal memory and returns.
5397 const char *
5398 esc_router_info(routerinfo_t *router)
5400 static char *info=NULL;
5401 char *esc_contact, *esc_platform;
5402 size_t len;
5403 if (info)
5404 tor_free(info);
5405 if (!router)
5406 return NULL; /* we're exiting; just free the memory we use */
5408 esc_contact = esc_for_log(router->contact_info);
5409 esc_platform = esc_for_log(router->platform);
5411 len = strlen(esc_contact)+strlen(esc_platform)+32;
5412 info = tor_malloc(len);
5413 tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
5414 esc_platform);
5415 tor_free(esc_contact);
5416 tor_free(esc_platform);
5418 return info;