Patch from Sebiastian for bug 888: mark a descriptor as "Impossible" if we reject...
[tor/rransom.git] / src / or / routerlist.c
blobf91af473f2d71ab86b6bf719075278a3a7fe87f2
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 /* $Id$ */
7 const char routerlist_c_id[] =
8 "$Id$";
10 /**
11 * \file routerlist.c
12 * \brief Code to
13 * maintain and access the global list of routerinfos for known
14 * servers.
15 **/
17 #include "or.h"
19 // #define DEBUG_ROUTERLIST
21 /****************************************************************************/
23 /* static function prototypes */
24 static routerstatus_t *router_pick_directory_server_impl(
25 authority_type_t auth, int flags);
26 static routerstatus_t *router_pick_trusteddirserver_impl(
27 authority_type_t auth, int flags, int *n_busy_out);
28 static void mark_all_trusteddirservers_up(void);
29 static int router_nickname_matches(routerinfo_t *router, const char *nickname);
30 static void trusted_dir_server_free(trusted_dir_server_t *ds);
31 static void launch_router_descriptor_downloads(smartlist_t *downloadable,
32 time_t now);
33 static void update_consensus_router_descriptor_downloads(time_t now);
34 static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
35 static void update_router_have_minimum_dir_info(void);
36 static const char *signed_descriptor_get_body_impl(signed_descriptor_t *desc,
37 int with_annotations);
38 static void list_pending_downloads(digestmap_t *result,
39 int purpose, const char *prefix);
41 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
42 DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
43 DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
44 #define SDMAP_FOREACH(map, keyvar, valvar) \
45 DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
46 valvar)
47 #define RIMAP_FOREACH(map, keyvar, valvar) \
48 DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
49 #define EIMAP_FOREACH(map, keyvar, valvar) \
50 DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
52 /****************************************************************************/
54 /** Global list of a trusted_dir_server_t object for each trusted directory
55 * server. */
56 static smartlist_t *trusted_dir_servers = NULL;
58 /** List of for a given authority, and download status for latest certificate.
60 typedef struct cert_list_t {
61 download_status_t dl_status;
62 smartlist_t *certs;
63 } cert_list_t;
64 /** Map from v3 identity key digest to cert_list_t. */
65 static digestmap_t *trusted_dir_certs = NULL;
66 /** True iff any key certificate in at least one member of
67 * <b>trusted_dir_certs</b> has changed since we last flushed the
68 * certificates to disk. */
69 static int trusted_dir_servers_certs_changed = 0;
71 /** Global list of all of the routers that we know about. */
72 static routerlist_t *routerlist = NULL;
74 /** List of strings for nicknames we've already warned about and that are
75 * still unknown / unavailable. */
76 static smartlist_t *warned_nicknames = NULL;
78 /** The last time we tried to download any routerdesc, or 0 for "never". We
79 * use this to rate-limit download attempts when the number of routerdescs to
80 * download is low. */
81 static time_t last_routerdesc_download_attempted = 0;
83 /* DOCDOC This is a massive massive kludge XXXX */
84 static uint64_t sl_last_total_weighted_bw = 0;
85 static uint64_t sl_last_weighted_bw_of_me = 0;
87 /** Return the number of directory authorities whose type matches some bit set
88 * in <b>type</b> */
89 int
90 get_n_authorities(authority_type_t type)
92 int n = 0;
93 if (!trusted_dir_servers)
94 return 0;
95 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
96 if (ds->type & type)
97 ++n);
98 return n;
101 #define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
103 /** Helper: Return the cert_list_t for an authority whose authority ID is
104 * <b>id_digest</b>, allocating a new list if necessary. */
105 static cert_list_t *
106 get_cert_list(const char *id_digest)
108 cert_list_t *cl;
109 if (!trusted_dir_certs)
110 trusted_dir_certs = digestmap_new();
111 cl = digestmap_get(trusted_dir_certs, id_digest);
112 if (!cl) {
113 cl = tor_malloc_zero(sizeof(cert_list_t));
114 cl->dl_status.schedule = DL_SCHED_CONSENSUS;
115 cl->certs = smartlist_create();
116 digestmap_set(trusted_dir_certs, id_digest, cl);
118 return cl;
121 /** Reload the cached v3 key certificates from the cached-certs file in
122 * the data directory. Return 0 on success, -1 on failure. */
124 trusted_dirs_reload_certs(void)
126 char *filename;
127 char *contents;
128 int r;
130 filename = get_datadir_fname("cached-certs");
131 contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
132 tor_free(filename);
133 if (!contents)
134 return 0;
135 r = trusted_dirs_load_certs_from_string(contents, 1, 1);
136 tor_free(contents);
137 return r;
140 /** Helper: return true iff we already have loaded the exact cert
141 * <b>cert</b>. */
142 static INLINE int
143 already_have_cert(authority_cert_t *cert)
145 cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);
147 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
149 if (!memcmp(c->cache_info.signed_descriptor_digest,
150 cert->cache_info.signed_descriptor_digest,
151 DIGEST_LEN))
152 return 1;
154 return 0;
157 /** Load a bunch of new key certificates from the string <b>contents</b>. If
158 * <b>from_store</b> is true, the certificates are from the cache, and we
159 * don't need to flush them to disk. If <b>from_store</b> is false, we need
160 * to flush any changed certificates to disk. Return 0 on success, -1 on
161 * failure. */
163 trusted_dirs_load_certs_from_string(const char *contents, int from_store,
164 int flush)
166 trusted_dir_server_t *ds;
167 const char *s, *eos;
169 for (s = contents; *s; s = eos) {
170 authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
171 cert_list_t *cl;
172 if (!cert)
173 break;
174 ds = trusteddirserver_get_by_v3_auth_digest(
175 cert->cache_info.identity_digest);
176 log_debug(LD_DIR, "Parsed certificate for %s",
177 ds ? ds->nickname : "unknown authority");
179 if (already_have_cert(cert)) {
180 /* we already have this one. continue. */
181 log_info(LD_DIR, "Skipping %s certificate for %s that we "
182 "already have.",
183 from_store ? "cached" : "downloaded",
184 ds ? ds->nickname : "??");
186 /* a duplicate on a download should be treated as a failure, since it
187 * probably means we wanted a different secret key or we are trying to
188 * replace an expired cert that has not in fact been updated. */
189 if (!from_store) {
190 log_warn(LD_DIR, "Got a certificate for %s that we already have. "
191 "Maybe they haven't updated it. Waiting for a while.",
192 ds ? ds->nickname : "??");
193 authority_cert_dl_failed(cert->cache_info.identity_digest, 404);
196 authority_cert_free(cert);
197 continue;
200 if (ds) {
201 log_info(LD_DIR, "Adding %s certificate for directory authority %s with "
202 "signing key %s", from_store ? "cached" : "downloaded",
203 ds->nickname, hex_str(cert->signing_key_digest,DIGEST_LEN));
204 } else {
205 log_info(LD_DIR, "Adding %s certificate for unrecognized directory "
206 "authority with signing key %s",
207 from_store ? "cached" : "downloaded",
208 hex_str(cert->signing_key_digest,DIGEST_LEN));
211 cl = get_cert_list(cert->cache_info.identity_digest);
212 smartlist_add(cl->certs, cert);
213 if (ds && cert->cache_info.published_on > ds->addr_current_at) {
214 /* Check to see whether we should update our view of the authority's
215 * address. */
216 if (cert->addr && cert->dir_port &&
217 (ds->addr != cert->addr ||
218 ds->dir_port != cert->dir_port)) {
219 char *a = tor_dup_ip(cert->addr);
220 log_notice(LD_DIR, "Updating address for directory authority %s "
221 "from %s:%d to %s:%d based on in certificate.",
222 ds->nickname, ds->address, (int)ds->dir_port,
223 a, cert->dir_port);
224 tor_free(a);
225 ds->addr = cert->addr;
226 ds->dir_port = cert->dir_port;
228 ds->addr_current_at = cert->cache_info.published_on;
231 if (!from_store)
232 trusted_dir_servers_certs_changed = 1;
235 if (flush)
236 trusted_dirs_flush_certs_to_disk();
238 networkstatus_note_certs_arrived();
239 return 0;
242 /** Save all v3 key certificates to the cached-certs file. */
243 void
244 trusted_dirs_flush_certs_to_disk(void)
246 char *filename;
247 smartlist_t *chunks;
249 if (!trusted_dir_servers_certs_changed || !trusted_dir_certs)
250 return;
252 chunks = smartlist_create();
253 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
254 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
256 sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
257 c->bytes = cert->cache_info.signed_descriptor_body;
258 c->len = cert->cache_info.signed_descriptor_len;
259 smartlist_add(chunks, c);
261 } DIGESTMAP_FOREACH_END;
263 filename = get_datadir_fname("cached-certs");
264 if (write_chunks_to_file(filename, chunks, 0)) {
265 log_warn(LD_FS, "Error writing certificates to disk.");
267 tor_free(filename);
268 SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
269 smartlist_free(chunks);
271 trusted_dir_servers_certs_changed = 0;
274 /** Remove all v3 authority certificates that have been superseded for more
275 * than 48 hours. (If the most recent cert was published more than 48 hours
276 * ago, then we aren't going to get any consensuses signed with older
277 * keys.) */
278 static void
279 trusted_dirs_remove_old_certs(void)
281 time_t now = time(NULL);
282 #define DEAD_CERT_LIFETIME (2*24*60*60)
283 #define OLD_CERT_LIFETIME (7*24*60*60)
284 if (!trusted_dir_certs)
285 return;
287 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
288 authority_cert_t *newest = NULL;
289 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
290 if (!newest || (cert->cache_info.published_on >
291 newest->cache_info.published_on))
292 newest = cert);
293 if (newest) {
294 const time_t newest_published = newest->cache_info.published_on;
295 SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
296 int expired;
297 time_t cert_published;
298 if (newest == cert)
299 continue;
300 expired = ftime_definitely_after(now, cert->expires);
301 cert_published = cert->cache_info.published_on;
302 /* Store expired certs for 48 hours after a newer arrives;
304 if (expired ?
305 (newest_published + DEAD_CERT_LIFETIME < now) :
306 (cert_published + OLD_CERT_LIFETIME < newest_published)) {
307 SMARTLIST_DEL_CURRENT(cl->certs, cert);
308 authority_cert_free(cert);
309 trusted_dir_servers_certs_changed = 1;
311 } SMARTLIST_FOREACH_END(cert);
313 } DIGESTMAP_FOREACH_END;
314 #undef OLD_CERT_LIFETIME
316 trusted_dirs_flush_certs_to_disk();
319 /** Return the newest v3 authority certificate whose v3 authority identity key
320 * has digest <b>id_digest</b>. Return NULL if no such authority is known,
321 * or it has no certificate. */
322 authority_cert_t *
323 authority_cert_get_newest_by_id(const char *id_digest)
325 cert_list_t *cl;
326 authority_cert_t *best = NULL;
327 if (!trusted_dir_certs ||
328 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
329 return NULL;
331 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
333 if (!best || cert->cache_info.published_on > best->cache_info.published_on)
334 best = cert;
336 return best;
339 /** Return the newest v3 authority certificate whose directory signing key has
340 * digest <b>sk_digest</b>. Return NULL if no such certificate is known.
342 authority_cert_t *
343 authority_cert_get_by_sk_digest(const char *sk_digest)
345 authority_cert_t *c;
346 if (!trusted_dir_certs)
347 return NULL;
349 if ((c = get_my_v3_authority_cert()) &&
350 !memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
351 return c;
352 if ((c = get_my_v3_legacy_cert()) &&
353 !memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
354 return c;
356 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
357 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
359 if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
360 return cert;
362 } DIGESTMAP_FOREACH_END;
363 return NULL;
366 /** Return the v3 authority certificate with signing key matching
367 * <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
368 * Return NULL if no such authority is known. */
369 authority_cert_t *
370 authority_cert_get_by_digests(const char *id_digest,
371 const char *sk_digest)
373 cert_list_t *cl;
374 if (!trusted_dir_certs ||
375 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
376 return NULL;
377 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
378 if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
379 return cert; );
381 return NULL;
384 /** Add every known authority_cert_t to <b>certs_out</b>. */
385 void
386 authority_cert_get_all(smartlist_t *certs_out)
388 tor_assert(certs_out);
389 if (!trusted_dir_certs)
390 return;
392 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
393 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
394 smartlist_add(certs_out, c));
395 } DIGESTMAP_FOREACH_END;
398 /** Called when an attempt to download a certificate with the authority with
399 * ID <b>id_digest</b> fails with HTTP response code <b>status</b>: remember
400 * the failure, so we don't try again immediately. */
401 void
402 authority_cert_dl_failed(const char *id_digest, int status)
404 cert_list_t *cl;
405 if (!trusted_dir_certs ||
406 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
407 return;
409 download_status_failed(&cl->dl_status, status);
412 /** How many times will we try to fetch a certificate before giving up? */
413 #define MAX_CERT_DL_FAILURES 8
415 /** Try to download any v3 authority certificates that we may be missing. If
416 * <b>status</b> is provided, try to get all the ones that were used to sign
417 * <b>status</b>. Additionally, try to have a non-expired certificate for
418 * every V3 authority in trusted_dir_servers. Don't fetch certificates we
419 * already have.
421 void
422 authority_certs_fetch_missing(networkstatus_t *status, time_t now)
424 digestmap_t *pending;
425 authority_cert_t *cert;
426 smartlist_t *missing_digests;
427 char *resource = NULL;
428 cert_list_t *cl;
429 const int cache = directory_caches_dir_info(get_options());
431 if (should_delay_dir_fetches(get_options()))
432 return;
434 pending = digestmap_new();
435 missing_digests = smartlist_create();
437 list_pending_downloads(pending, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
438 if (status) {
439 SMARTLIST_FOREACH(status->voters, networkstatus_voter_info_t *, voter,
441 if (tor_digest_is_zero(voter->signing_key_digest))
442 continue; /* This authority never signed this consensus, so don't
443 * go looking for a cert with key digest 0000000000. */
444 if (!cache &&
445 !trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
446 continue; /* We are not a cache, and we don't know this authority.*/
447 cl = get_cert_list(voter->identity_digest);
448 cert = authority_cert_get_by_digests(voter->identity_digest,
449 voter->signing_key_digest);
450 if (cert) {
451 if (now < cert->expires)
452 download_status_reset(&cl->dl_status);
453 continue;
455 if (download_status_is_ready(&cl->dl_status, now,
456 MAX_CERT_DL_FAILURES) &&
457 !digestmap_get(pending, voter->identity_digest)) {
458 log_notice(LD_DIR, "We're missing a certificate from authority "
459 "with signing key %s: launching request.",
460 hex_str(voter->signing_key_digest, DIGEST_LEN));
461 smartlist_add(missing_digests, voter->identity_digest);
465 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
467 int found = 0;
468 if (!(ds->type & V3_AUTHORITY))
469 continue;
470 if (smartlist_digest_isin(missing_digests, ds->v3_identity_digest))
471 continue;
472 cl = get_cert_list(ds->v3_identity_digest);
473 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
475 if (!ftime_definitely_after(now, cert->expires)) {
476 /* It's not expired, and we weren't looking for something to
477 * verify a consensus with. Call it done. */
478 download_status_reset(&cl->dl_status);
479 found = 1;
480 break;
483 if (!found &&
484 download_status_is_ready(&cl->dl_status, now,MAX_CERT_DL_FAILURES) &&
485 !digestmap_get(pending, ds->v3_identity_digest)) {
486 log_notice(LD_DIR, "No current certificate known for authority %s; "
487 "launching request.", ds->nickname);
488 smartlist_add(missing_digests, ds->v3_identity_digest);
492 if (!smartlist_len(missing_digests)) {
493 goto done;
494 } else {
495 smartlist_t *fps = smartlist_create();
496 smartlist_add(fps, tor_strdup("fp/"));
497 SMARTLIST_FOREACH(missing_digests, const char *, d, {
498 char *fp;
499 if (digestmap_get(pending, d))
500 continue;
501 fp = tor_malloc(HEX_DIGEST_LEN+2);
502 base16_encode(fp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
503 fp[HEX_DIGEST_LEN] = '+';
504 fp[HEX_DIGEST_LEN+1] = '\0';
505 smartlist_add(fps, fp);
507 if (smartlist_len(fps) == 1) {
508 /* we didn't add any: they were all pending */
509 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
510 smartlist_free(fps);
511 goto done;
513 resource = smartlist_join_strings(fps, "", 0, NULL);
514 resource[strlen(resource)-1] = '\0';
515 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
516 smartlist_free(fps);
518 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
519 resource, PDS_RETRY_IF_NO_SERVERS);
521 done:
522 tor_free(resource);
523 smartlist_free(missing_digests);
524 digestmap_free(pending, NULL);
527 /* Router descriptor storage.
529 * Routerdescs are stored in a big file, named "cached-descriptors". As new
530 * routerdescs arrive, we append them to a journal file named
531 * "cached-descriptors.new".
533 * From time to time, we replace "cached-descriptors" with a new file
534 * containing only the live, non-superseded descriptors, and clear
535 * cached-routers.new.
537 * On startup, we read both files.
540 /** Helper: return 1 iff the router log is so big we want to rebuild the
541 * store. */
542 static int
543 router_should_rebuild_store(desc_store_t *store)
545 if (store->store_len > (1<<16))
546 return (store->journal_len > store->store_len / 2 ||
547 store->bytes_dropped > store->store_len / 2);
548 else
549 return store->journal_len > (1<<15);
552 /** Return the desc_store_t in <b>rl</b> that should be used to store
553 * <b>sd</b>. */
554 static INLINE desc_store_t *
555 desc_get_store(routerlist_t *rl, signed_descriptor_t *sd)
557 if (sd->is_extrainfo)
558 return &rl->extrainfo_store;
559 else
560 return &rl->desc_store;
563 /** Add the signed_descriptor_t in <b>desc</b> to the router
564 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
565 * offset appropriately. */
566 static int
567 signed_desc_append_to_journal(signed_descriptor_t *desc,
568 desc_store_t *store)
570 char *fname = get_datadir_fname_suffix(store->fname_base, ".new");
571 const char *body = signed_descriptor_get_body_impl(desc,1);
572 size_t len = desc->signed_descriptor_len + desc->annotations_len;
574 tor_assert(len == strlen(body));
576 if (append_bytes_to_file(fname, body, len, 1)) {
577 log_warn(LD_FS, "Unable to store router descriptor");
578 tor_free(fname);
579 return -1;
581 desc->saved_location = SAVED_IN_JOURNAL;
582 tor_free(fname);
584 desc->saved_offset = store->journal_len;
585 store->journal_len += len;
587 return 0;
590 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
591 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
592 * the signed_descriptor_t* in *<b>b</b>. */
593 static int
594 _compare_signed_descriptors_by_age(const void **_a, const void **_b)
596 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
597 return (int)(r1->published_on - r2->published_on);
600 #define RRS_FORCE 1
601 #define RRS_DONT_REMOVE_OLD 2
603 /** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
604 * <b>flags</b>, then atomically replace the saved router store with the
605 * routers currently in our routerlist, and clear the journal. Unless
606 * RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
607 * rebuilding the store. Return 0 on success, -1 on failure.
609 static int
610 router_rebuild_store(int flags, desc_store_t *store)
612 smartlist_t *chunk_list = NULL;
613 char *fname = NULL, *fname_tmp = NULL;
614 int r = -1;
615 off_t offset = 0;
616 smartlist_t *signed_descriptors = NULL;
617 int nocache=0;
618 size_t total_expected_len = 0;
619 int had_any;
620 int force = flags & RRS_FORCE;
622 if (!force && !router_should_rebuild_store(store))
623 return 0;
624 if (!routerlist)
625 return 0;
627 if (store->type == EXTRAINFO_STORE)
628 had_any = !eimap_isempty(routerlist->extra_info_map);
629 else
630 had_any = (smartlist_len(routerlist->routers)+
631 smartlist_len(routerlist->old_routers))>0;
633 /* Don't save deadweight. */
634 if (!(flags & RRS_DONT_REMOVE_OLD))
635 routerlist_remove_old_routers();
637 log_info(LD_DIR, "Rebuilding %s cache", store->description);
639 fname = get_datadir_fname(store->fname_base);
640 fname_tmp = get_datadir_fname_suffix(store->fname_base, ".tmp");
642 chunk_list = smartlist_create();
644 /* We sort the routers by age to enhance locality on disk. */
645 signed_descriptors = smartlist_create();
646 if (store->type == EXTRAINFO_STORE) {
647 eimap_iter_t *iter;
648 for (iter = eimap_iter_init(routerlist->extra_info_map);
649 !eimap_iter_done(iter);
650 iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
651 const char *key;
652 extrainfo_t *ei;
653 eimap_iter_get(iter, &key, &ei);
654 smartlist_add(signed_descriptors, &ei->cache_info);
656 } else {
657 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
658 smartlist_add(signed_descriptors, sd));
659 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
660 smartlist_add(signed_descriptors, &ri->cache_info));
663 smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);
665 /* Now, add the appropriate members to chunk_list */
666 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
668 sized_chunk_t *c;
669 const char *body = signed_descriptor_get_body_impl(sd, 1);
670 if (!body) {
671 log_warn(LD_BUG, "No descriptor available for router.");
672 goto done;
674 if (sd->do_not_cache) {
675 ++nocache;
676 continue;
678 c = tor_malloc(sizeof(sized_chunk_t));
679 c->bytes = body;
680 c->len = sd->signed_descriptor_len + sd->annotations_len;
681 total_expected_len += c->len;
682 smartlist_add(chunk_list, c);
685 if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
686 log_warn(LD_FS, "Error writing router store to disk.");
687 goto done;
690 /* Our mmap is now invalid. */
691 if (store->mmap) {
692 tor_munmap_file(store->mmap);
693 store->mmap = NULL;
696 if (replace_file(fname_tmp, fname)<0) {
697 log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
698 goto done;
701 errno = 0;
702 store->mmap = tor_mmap_file(fname);
703 if (! store->mmap) {
704 if (errno == ERANGE) {
705 /* empty store.*/
706 if (total_expected_len) {
707 log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
708 " but when we went to mmap it, it was empty!", fname);
709 } else if (had_any) {
710 log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
711 "okay if we're just starting up after a long time. "
712 "Otherwise, it's a bug.", fname);
714 } else {
715 log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
719 log_info(LD_DIR, "Reconstructing pointers into cache");
721 offset = 0;
722 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
724 if (sd->do_not_cache)
725 continue;
726 sd->saved_location = SAVED_IN_CACHE;
727 if (store->mmap) {
728 tor_free(sd->signed_descriptor_body); // sets it to null
729 sd->saved_offset = offset;
731 offset += sd->signed_descriptor_len + sd->annotations_len;
732 signed_descriptor_get_body(sd); /* reconstruct and assert */
735 tor_free(fname);
736 fname = get_datadir_fname_suffix(store->fname_base, ".new");
737 write_str_to_file(fname, "", 1);
739 r = 0;
740 store->store_len = (size_t) offset;
741 store->journal_len = 0;
742 store->bytes_dropped = 0;
743 done:
744 if (signed_descriptors)
745 smartlist_free(signed_descriptors);
746 tor_free(fname);
747 tor_free(fname_tmp);
748 SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
749 smartlist_free(chunk_list);
751 return r;
754 /** Helper: Reload a cache file and its associated journal, setting metadata
755 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
756 * else reload the router descriptor store. */
757 static int
758 router_reload_router_list_impl(desc_store_t *store)
760 char *fname = NULL, *altname = NULL, *contents = NULL;
761 struct stat st;
762 int read_from_old_location = 0;
763 int extrainfo = (store->type == EXTRAINFO_STORE);
764 time_t now = time(NULL);
765 store->journal_len = store->store_len = 0;
767 fname = get_datadir_fname(store->fname_base);
768 if (store->fname_alt_base)
769 altname = get_datadir_fname(store->fname_alt_base);
771 if (store->mmap) /* get rid of it first */
772 tor_munmap_file(store->mmap);
773 store->mmap = NULL;
775 store->mmap = tor_mmap_file(fname);
776 if (!store->mmap && altname && file_status(altname) == FN_FILE) {
777 read_from_old_location = 1;
778 log_notice(LD_DIR, "Couldn't read %s; trying to load routers from old "
779 "location %s.", fname, altname);
780 if ((store->mmap = tor_mmap_file(altname)))
781 read_from_old_location = 1;
783 if (altname && !read_from_old_location) {
784 remove_file_if_very_old(altname, now);
786 if (store->mmap) {
787 store->store_len = store->mmap->size;
788 if (extrainfo)
789 router_load_extrainfo_from_string(store->mmap->data,
790 store->mmap->data+store->mmap->size,
791 SAVED_IN_CACHE, NULL, 0);
792 else
793 router_load_routers_from_string(store->mmap->data,
794 store->mmap->data+store->mmap->size,
795 SAVED_IN_CACHE, NULL, 0, NULL);
798 tor_free(fname);
799 fname = get_datadir_fname_suffix(store->fname_base, ".new");
800 if (file_status(fname) == FN_FILE)
801 contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
802 if (read_from_old_location) {
803 tor_free(altname);
804 altname = get_datadir_fname_suffix(store->fname_alt_base, ".new");
805 if (!contents)
806 contents = read_file_to_str(altname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
807 else
808 remove_file_if_very_old(altname, now);
810 if (contents) {
811 if (extrainfo)
812 router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL,
813 NULL, 0);
814 else
815 router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL,
816 NULL, 0, NULL);
817 store->journal_len = (size_t) st.st_size;
818 tor_free(contents);
821 tor_free(fname);
822 tor_free(altname);
824 if (store->journal_len || read_from_old_location) {
825 /* Always clear the journal on startup.*/
826 router_rebuild_store(RRS_FORCE, store);
827 } else if (!extrainfo) {
828 /* Don't cache expired routers. (This is in an else because
829 * router_rebuild_store() also calls remove_old_routers().) */
830 routerlist_remove_old_routers();
833 return 0;
836 /** Load all cached router descriptors and extra-info documents from the
837 * store. Return 0 on success and -1 on failure.
840 router_reload_router_list(void)
842 routerlist_t *rl = router_get_routerlist();
843 if (router_reload_router_list_impl(&rl->desc_store))
844 return -1;
845 if (router_reload_router_list_impl(&rl->extrainfo_store))
846 return -1;
847 return 0;
850 /** Return a smartlist containing a list of trusted_dir_server_t * for all
851 * known trusted dirservers. Callers must not modify the list or its
852 * contents.
854 smartlist_t *
855 router_get_trusted_dir_servers(void)
857 if (!trusted_dir_servers)
858 trusted_dir_servers = smartlist_create();
860 return trusted_dir_servers;
863 /** Try to find a running dirserver that supports operations of <b>type</b>.
865 * If there are no running dirservers in our routerlist and the
866 * <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the authoritative ones
867 * as running again, and pick one.
869 * If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
870 * dirservers that we can't reach.
872 * If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
873 * (if we're a dirserver).
875 * Don't pick an authority if any non-authority is viable; try to avoid using
876 * servers that have returned 503 recently.
878 routerstatus_t *
879 router_pick_directory_server(authority_type_t type, int flags)
881 routerstatus_t *choice;
882 if (get_options()->PreferTunneledDirConns)
883 flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
885 if (!routerlist)
886 return NULL;
888 choice = router_pick_directory_server_impl(type, flags);
889 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
890 return choice;
892 log_info(LD_DIR,
893 "No reachable router entries for dirservers. "
894 "Trying them all again.");
895 /* mark all authdirservers as up again */
896 mark_all_trusteddirservers_up();
897 /* try again */
898 choice = router_pick_directory_server_impl(type, flags);
899 return choice;
902 /** DOCDOC */
904 router_get_my_share_of_directory_requests(double *v2_share_out,
905 double *v3_share_out)
907 routerinfo_t *me = router_get_my_routerinfo();
908 routerstatus_t *rs;
909 const int pds_flags = PDS_ALLOW_SELF|PDS_IGNORE_FASCISTFIREWALL;
910 *v2_share_out = *v3_share_out = 0.0;
911 if (!me)
912 return -1;
913 rs = router_get_consensus_status_by_id(me->cache_info.identity_digest);
914 if (!rs)
915 return -1;
917 /* Calling for side effect */
918 if (rs->is_v2_dir) {
919 sl_last_total_weighted_bw = 0;
920 router_pick_directory_server(V2_AUTHORITY, pds_flags);
921 if (sl_last_total_weighted_bw != 0) {
922 *v2_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
923 U64_TO_DBL(sl_last_total_weighted_bw);
927 if (rs->version_supports_v3_dir) {
928 sl_last_total_weighted_bw = 0;
929 router_pick_directory_server(V3_AUTHORITY, pds_flags);
930 if (sl_last_total_weighted_bw != 0) {
931 *v3_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
932 U64_TO_DBL(sl_last_total_weighted_bw);
936 return 0;
939 /** Return the trusted_dir_server_t for the directory authority whose identity
940 * key hashes to <b>digest</b>, or NULL if no such authority is known.
942 trusted_dir_server_t *
943 router_get_trusteddirserver_by_digest(const char *digest)
945 if (!trusted_dir_servers)
946 return NULL;
948 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
950 if (!memcmp(ds->digest, digest, DIGEST_LEN))
951 return ds;
954 return NULL;
957 /** Return the trusted_dir_server_t for the directory authority whose identity
958 * key hashes to <b>digest</b>, or NULL if no such authority is known.
960 trusted_dir_server_t *
961 trusteddirserver_get_by_v3_auth_digest(const char *digest)
963 if (!trusted_dir_servers)
964 return NULL;
966 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
968 if (!memcmp(ds->v3_identity_digest, digest, DIGEST_LEN) &&
969 (ds->type & V3_AUTHORITY))
970 return ds;
973 return NULL;
976 /** Try to find a running trusted dirserver. Flags are as for
977 * router_pick_directory_server.
979 routerstatus_t *
980 router_pick_trusteddirserver(authority_type_t type, int flags)
982 routerstatus_t *choice;
983 int busy = 0;
984 if (get_options()->PreferTunneledDirConns)
985 flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
987 choice = router_pick_trusteddirserver_impl(type, flags, &busy);
988 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
989 return choice;
990 if (busy) {
991 /* If the reason that we got no server is that servers are "busy",
992 * we must be excluding good servers because we already have serverdesc
993 * fetches with them. Do not mark down servers up because of this. */
994 tor_assert((flags & PDS_NO_EXISTING_SERVERDESC_FETCH));
995 return NULL;
998 log_info(LD_DIR,
999 "No trusted dirservers are reachable. Trying them all again.");
1000 mark_all_trusteddirservers_up();
1001 return router_pick_trusteddirserver_impl(type, flags, NULL);
1004 /** How long do we avoid using a directory server after it's given us a 503? */
1005 #define DIR_503_TIMEOUT (60*60)
1007 /** Pick a random running valid directory server/mirror from our
1008 * routerlist. Arguments are as for router_pick_directory_server(), except
1009 * that RETRY_IF_NO_SERVERS is ignored, and:
1011 * If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers
1012 * that we can use with BEGINDIR.
1014 static routerstatus_t *
1015 router_pick_directory_server_impl(authority_type_t type, int flags)
1017 routerstatus_t *result;
1018 smartlist_t *direct, *tunnel;
1019 smartlist_t *trusted_direct, *trusted_tunnel;
1020 smartlist_t *overloaded_direct, *overloaded_tunnel;
1021 time_t now = time(NULL);
1022 const networkstatus_t *consensus = networkstatus_get_latest_consensus();
1023 int requireother = ! (flags & PDS_ALLOW_SELF);
1024 int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
1025 int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
1027 if (!consensus)
1028 return NULL;
1030 direct = smartlist_create();
1031 tunnel = smartlist_create();
1032 trusted_direct = smartlist_create();
1033 trusted_tunnel = smartlist_create();
1034 overloaded_direct = smartlist_create();
1035 overloaded_tunnel = smartlist_create();
1037 /* Find all the running dirservers we know about. */
1038 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *,
1039 status) {
1040 int is_trusted;
1041 int is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
1042 tor_addr_t addr;
1043 if (!status->is_running || !status->dir_port || !status->is_valid)
1044 continue;
1045 if (status->is_bad_directory)
1046 continue;
1047 if (requireother && router_digest_is_me(status->identity_digest))
1048 continue;
1049 if (type & V3_AUTHORITY) {
1050 if (!(status->version_supports_v3_dir ||
1051 router_digest_is_trusted_dir_type(status->identity_digest,
1052 V3_AUTHORITY)))
1053 continue;
1055 is_trusted = router_digest_is_trusted_dir(status->identity_digest);
1056 if ((type & V2_AUTHORITY) && !(status->is_v2_dir || is_trusted))
1057 continue;
1058 if ((type & EXTRAINFO_CACHE) &&
1059 !router_supports_extrainfo(status->identity_digest, 0))
1060 continue;
1062 /* XXXX IP6 proposal 118 */
1063 tor_addr_from_ipv4h(&addr, status->addr);
1065 if (prefer_tunnel &&
1066 status->version_supports_begindir &&
1067 (!fascistfirewall ||
1068 fascist_firewall_allows_address_or(&addr, status->or_port)))
1069 smartlist_add(is_trusted ? trusted_tunnel :
1070 is_overloaded ? overloaded_tunnel : tunnel, status);
1071 else if (!fascistfirewall ||
1072 fascist_firewall_allows_address_dir(&addr, status->dir_port))
1073 smartlist_add(is_trusted ? trusted_direct :
1074 is_overloaded ? overloaded_direct : direct, status);
1075 } SMARTLIST_FOREACH_END(status);
1077 if (smartlist_len(tunnel)) {
1078 result = routerstatus_sl_choose_by_bandwidth(tunnel);
1079 } else if (smartlist_len(overloaded_tunnel)) {
1080 result = routerstatus_sl_choose_by_bandwidth(overloaded_tunnel);
1081 } else if (smartlist_len(trusted_tunnel)) {
1082 /* FFFF We don't distinguish between trusteds and overloaded trusteds
1083 * yet. Maybe one day we should. */
1084 /* FFFF We also don't load balance over authorities yet. I think this
1085 * is a feature, but it could easily be a bug. -RD */
1086 result = smartlist_choose(trusted_tunnel);
1087 } else if (smartlist_len(direct)) {
1088 result = routerstatus_sl_choose_by_bandwidth(direct);
1089 } else if (smartlist_len(overloaded_direct)) {
1090 result = routerstatus_sl_choose_by_bandwidth(overloaded_direct);
1091 } else {
1092 result = smartlist_choose(trusted_direct);
1094 smartlist_free(direct);
1095 smartlist_free(tunnel);
1096 smartlist_free(trusted_direct);
1097 smartlist_free(trusted_tunnel);
1098 smartlist_free(overloaded_direct);
1099 smartlist_free(overloaded_tunnel);
1100 return result;
1103 /** Choose randomly from among the trusted dirservers that are up. Flags
1104 * are as for router_pick_directory_server_impl().
1106 static routerstatus_t *
1107 router_pick_trusteddirserver_impl(authority_type_t type, int flags,
1108 int *n_busy_out)
1110 smartlist_t *direct, *tunnel;
1111 smartlist_t *overloaded_direct, *overloaded_tunnel;
1112 routerinfo_t *me = router_get_my_routerinfo();
1113 routerstatus_t *result;
1114 time_t now = time(NULL);
1115 const int requireother = ! (flags & PDS_ALLOW_SELF);
1116 const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
1117 const int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
1118 const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
1119 int n_busy = 0;
1121 if (!trusted_dir_servers)
1122 return NULL;
1124 direct = smartlist_create();
1125 tunnel = smartlist_create();
1126 overloaded_direct = smartlist_create();
1127 overloaded_tunnel = smartlist_create();
1129 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, d)
1131 int is_overloaded =
1132 d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
1133 tor_addr_t addr;
1134 if (!d->is_running) continue;
1135 if ((type & d->type) == 0)
1136 continue;
1137 if ((type & EXTRAINFO_CACHE) &&
1138 !router_supports_extrainfo(d->digest, 1))
1139 continue;
1140 if (requireother && me && router_digest_is_me(d->digest))
1141 continue;
1143 /* XXXX IP6 proposal 118 */
1144 tor_addr_from_ipv4h(&addr, d->addr);
1146 if (no_serverdesc_fetching) {
1147 if (connection_get_by_type_addr_port_purpose(
1148 CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_SERVERDESC)
1149 || connection_get_by_type_addr_port_purpose(
1150 CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_EXTRAINFO)) {
1151 //log_debug(LD_DIR, "We have an existing connection to fetch "
1152 // "descriptor from %s; delaying",d->description);
1153 ++n_busy;
1154 continue;
1158 if (prefer_tunnel &&
1159 d->or_port &&
1160 (!fascistfirewall ||
1161 fascist_firewall_allows_address_or(&addr, d->or_port)))
1162 smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
1163 &d->fake_status);
1164 else if (!fascistfirewall ||
1165 fascist_firewall_allows_address_dir(&addr, d->dir_port))
1166 smartlist_add(is_overloaded ? overloaded_direct : direct,
1167 &d->fake_status);
1169 SMARTLIST_FOREACH_END(d);
1171 if (smartlist_len(tunnel)) {
1172 result = smartlist_choose(tunnel);
1173 } else if (smartlist_len(overloaded_tunnel)) {
1174 result = smartlist_choose(overloaded_tunnel);
1175 } else if (smartlist_len(direct)) {
1176 result = smartlist_choose(direct);
1177 } else {
1178 result = smartlist_choose(overloaded_direct);
1181 if (n_busy_out)
1182 *n_busy_out = n_busy;
1184 smartlist_free(direct);
1185 smartlist_free(tunnel);
1186 smartlist_free(overloaded_direct);
1187 smartlist_free(overloaded_tunnel);
1188 return result;
1191 /** Go through and mark the authoritative dirservers as up. */
1192 static void
1193 mark_all_trusteddirservers_up(void)
1195 if (routerlist) {
1196 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1197 if (router_digest_is_trusted_dir(router->cache_info.identity_digest) &&
1198 router->dir_port > 0) {
1199 router->is_running = 1;
1202 if (trusted_dir_servers) {
1203 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
1205 routerstatus_t *rs;
1206 dir->is_running = 1;
1207 download_status_reset(&dir->v2_ns_dl_status);
1208 rs = router_get_consensus_status_by_id(dir->digest);
1209 if (rs && !rs->is_running) {
1210 rs->is_running = 1;
1211 rs->last_dir_503_at = 0;
1212 control_event_networkstatus_changed_single(rs);
1216 router_dir_info_changed();
1219 /** Reset all internal variables used to count failed downloads of network
1220 * status objects. */
1221 void
1222 router_reset_status_download_failures(void)
1224 mark_all_trusteddirservers_up();
1227 /** Return true iff router1 and router2 have the same /16 network. */
1228 static INLINE int
1229 routers_in_same_network_family(routerinfo_t *r1, routerinfo_t *r2)
1231 return (r1->addr & 0xffff0000) == (r2->addr & 0xffff0000);
1234 /** Look through the routerlist and identify routers that
1235 * advertise the same /16 network address as <b>router</b>.
1236 * Add each of them to <b>sl</b>.
1238 static void
1239 routerlist_add_network_family(smartlist_t *sl, routerinfo_t *router)
1241 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
1243 if (router != r && routers_in_same_network_family(router, r))
1244 smartlist_add(sl, r);
1248 /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
1249 * This is used to make sure we don't pick siblings in a single path,
1250 * or pick more than one relay from a family for our entry guard list.
1252 void
1253 routerlist_add_family(smartlist_t *sl, routerinfo_t *router)
1255 routerinfo_t *r;
1256 config_line_t *cl;
1257 or_options_t *options = get_options();
1259 /* First, add any routers with similar network addresses. */
1260 if (options->EnforceDistinctSubnets)
1261 routerlist_add_network_family(sl, router);
1263 if (router->declared_family) {
1264 /* Add every r such that router declares familyness with r, and r
1265 * declares familyhood with router. */
1266 SMARTLIST_FOREACH(router->declared_family, const char *, n,
1268 if (!(r = router_get_by_nickname(n, 0)))
1269 continue;
1270 if (!r->declared_family)
1271 continue;
1272 SMARTLIST_FOREACH(r->declared_family, const char *, n2,
1274 if (router_nickname_matches(router, n2))
1275 smartlist_add(sl, r);
1280 /* If the user declared any families locally, honor those too. */
1281 for (cl = options->NodeFamilies; cl; cl = cl->next) {
1282 if (router_nickname_is_in_list(router, cl->value)) {
1283 add_nickname_list_to_smartlist(sl, cl->value, 0);
1288 /** Return true iff r is named by some nickname in <b>lst</b>. */
1289 static INLINE int
1290 router_in_nickname_smartlist(smartlist_t *lst, routerinfo_t *r)
1292 if (!lst) return 0;
1293 SMARTLIST_FOREACH(lst, const char *, name,
1294 if (router_nickname_matches(r, name))
1295 return 1;);
1296 return 0;
1299 /** Return true iff r1 and r2 are in the same family, but not the same
1300 * router. */
1302 routers_in_same_family(routerinfo_t *r1, routerinfo_t *r2)
1304 or_options_t *options = get_options();
1305 config_line_t *cl;
1307 if (options->EnforceDistinctSubnets && routers_in_same_network_family(r1,r2))
1308 return 1;
1310 if (router_in_nickname_smartlist(r1->declared_family, r2) &&
1311 router_in_nickname_smartlist(r2->declared_family, r1))
1312 return 1;
1314 for (cl = options->NodeFamilies; cl; cl = cl->next) {
1315 if (router_nickname_is_in_list(r1, cl->value) &&
1316 router_nickname_is_in_list(r2, cl->value))
1317 return 1;
1319 return 0;
1322 /** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
1323 * see which nicknames in <b>list</b> name routers in our routerlist, and add
1324 * the routerinfos for those routers to <b>sl</b>. If <b>must_be_running</b>,
1325 * only include routers that we think are running.
1326 * Warn if any non-Named routers are specified by nickname.
1328 void
1329 add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
1330 int must_be_running)
1332 routerinfo_t *router;
1333 smartlist_t *nickname_list;
1334 int have_dir_info = router_have_minimum_dir_info();
1336 if (!list)
1337 return; /* nothing to do */
1338 tor_assert(sl);
1340 nickname_list = smartlist_create();
1341 if (!warned_nicknames)
1342 warned_nicknames = smartlist_create();
1344 smartlist_split_string(nickname_list, list, ",",
1345 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1347 SMARTLIST_FOREACH(nickname_list, const char *, nick, {
1348 int warned;
1349 if (!is_legal_nickname_or_hexdigest(nick)) {
1350 log_warn(LD_CONFIG, "Nickname '%s' is misformed; skipping", nick);
1351 continue;
1353 router = router_get_by_nickname(nick, 1);
1354 warned = smartlist_string_isin(warned_nicknames, nick);
1355 if (router) {
1356 if (!must_be_running || router->is_running) {
1357 smartlist_add(sl,router);
1359 } else if (!router_get_consensus_status_by_nickname(nick,1)) {
1360 if (!warned) {
1361 log_fn(have_dir_info ? LOG_WARN : LOG_INFO, LD_CONFIG,
1362 "Nickname list includes '%s' which isn't a known router.",nick);
1363 smartlist_add(warned_nicknames, tor_strdup(nick));
1367 SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
1368 smartlist_free(nickname_list);
1371 /** Return 1 iff any member of the (possibly NULL) comma-separated list
1372 * <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
1373 * return 0.
1376 router_nickname_is_in_list(routerinfo_t *router, const char *list)
1378 smartlist_t *nickname_list;
1379 int v = 0;
1381 if (!list)
1382 return 0; /* definitely not */
1383 tor_assert(router);
1385 nickname_list = smartlist_create();
1386 smartlist_split_string(nickname_list, list, ",",
1387 SPLIT_SKIP_SPACE|SPLIT_STRIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1388 SMARTLIST_FOREACH(nickname_list, const char *, cp,
1389 if (router_nickname_matches(router, cp)) {v=1;break;});
1390 SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
1391 smartlist_free(nickname_list);
1392 return v;
1395 /** Add every suitable router from our routerlist to <b>sl</b>, so that
1396 * we can pick a node for a circuit.
1398 static void
1399 router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_invalid,
1400 int need_uptime, int need_capacity,
1401 int need_guard)
1403 if (!routerlist)
1404 return;
1406 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1408 if (router->is_running &&
1409 router->purpose == ROUTER_PURPOSE_GENERAL &&
1410 (router->is_valid || allow_invalid) &&
1411 !router_is_unreliable(router, need_uptime,
1412 need_capacity, need_guard)) {
1413 /* If it's running, and it's suitable according to the
1414 * other flags we had in mind */
1415 smartlist_add(sl, router);
1420 /** Look through the routerlist until we find a router that has my key.
1421 Return it. */
1422 routerinfo_t *
1423 routerlist_find_my_routerinfo(void)
1425 if (!routerlist)
1426 return NULL;
1428 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1430 if (router_is_me(router))
1431 return router;
1433 return NULL;
1436 /** Find a router that's up, that has this IP address, and
1437 * that allows exit to this address:port, or return NULL if there
1438 * isn't a good one.
1440 routerinfo_t *
1441 router_find_exact_exit_enclave(const char *address, uint16_t port)
1443 uint32_t addr;
1444 struct in_addr in;
1446 if (!tor_inet_aton(address, &in))
1447 return NULL; /* it's not an IP already */
1448 addr = ntohl(in.s_addr);
1450 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1452 if (router->is_running &&
1453 router->addr == addr &&
1454 compare_addr_to_addr_policy(addr, port, router->exit_policy) ==
1455 ADDR_POLICY_ACCEPTED)
1456 return router;
1458 return NULL;
1461 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1462 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1463 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1464 * bandwidth.
1465 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1468 router_is_unreliable(routerinfo_t *router, int need_uptime,
1469 int need_capacity, int need_guard)
1471 if (need_uptime && !router->is_stable)
1472 return 1;
1473 if (need_capacity && !router->is_fast)
1474 return 1;
1475 if (need_guard && !router->is_possible_guard)
1476 return 1;
1477 return 0;
1480 /** Return the smaller of the router's configured BandwidthRate
1481 * and its advertised capacity. */
1482 uint32_t
1483 router_get_advertised_bandwidth(routerinfo_t *router)
1485 if (router->bandwidthcapacity < router->bandwidthrate)
1486 return router->bandwidthcapacity;
1487 return router->bandwidthrate;
1490 /** Do not weight any declared bandwidth more than this much when picking
1491 * routers by bandwidth. */
1492 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
1494 /** Return the smaller of the router's configured BandwidthRate
1495 * and its advertised capacity, capped by max-believe-bw. */
1496 uint32_t
1497 router_get_advertised_bandwidth_capped(routerinfo_t *router)
1499 uint32_t result = router->bandwidthcapacity;
1500 if (result > router->bandwidthrate)
1501 result = router->bandwidthrate;
1502 if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH)
1503 result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
1504 return result;
1507 /** Eventually, the number we return will come from the directory
1508 * consensus, so clients can dynamically update to better numbers.
1510 * But for now, or in case there is no consensus available, just return
1511 * a sufficient default. */
1512 static uint32_t
1513 get_max_believable_bandwidth(void)
1515 return DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
1518 /** Helper function:
1519 * choose a random element of smartlist <b>sl</b>, weighted by
1520 * the advertised bandwidth of each element.
1522 * If <b>statuses</b> is zero, then <b>sl</b> is a list of
1523 * routerinfo_t's. Otherwise it's a list of routerstatus_t's.
1525 * If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
1526 * nodes' bandwidth equally regardless of their Exit status, since there may
1527 * be some in the list because they exit to obscure ports. If
1528 * <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
1529 * exit-node's bandwidth less depending on the smallness of the fraction of
1530 * Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
1531 * guard node: consider all guard's bandwidth equally. Otherwise, weight
1532 * guards proportionally less.
1534 static void *
1535 smartlist_choose_by_bandwidth(smartlist_t *sl, bandwidth_weight_rule_t rule,
1536 int statuses)
1538 unsigned int i;
1539 routerinfo_t *router;
1540 routerstatus_t *status=NULL;
1541 int32_t *bandwidths;
1542 int is_exit;
1543 int is_guard;
1544 uint64_t total_nonexit_bw = 0, total_exit_bw = 0, total_bw = 0;
1545 uint64_t total_nonguard_bw = 0, total_guard_bw = 0;
1546 uint64_t rand_bw, tmp;
1547 double exit_weight;
1548 double guard_weight;
1549 int n_unknown = 0;
1550 bitarray_t *exit_bits;
1551 bitarray_t *guard_bits;
1552 uint32_t max_believable_bw = get_max_believable_bandwidth();
1553 int me_idx = -1;
1555 /* Can't choose exit and guard at same time */
1556 tor_assert(rule == NO_WEIGHTING ||
1557 rule == WEIGHT_FOR_EXIT ||
1558 rule == WEIGHT_FOR_GUARD);
1560 /* First count the total bandwidth weight, and make a list
1561 * of each value. <0 means "unknown; no routerinfo." We use the
1562 * bits of negative values to remember whether the router was fast (-x)&1
1563 * and whether it was an exit (-x)&2 or guard (-x)&4. Yes, it's a hack. */
1564 bandwidths = tor_malloc(sizeof(int32_t)*smartlist_len(sl));
1565 exit_bits = bitarray_init_zero(smartlist_len(sl));
1566 guard_bits = bitarray_init_zero(smartlist_len(sl));
1568 /* Iterate over all the routerinfo_t or routerstatus_t, and */
1569 for (i = 0; i < (unsigned)smartlist_len(sl); ++i) {
1570 /* first, learn what bandwidth we think i has */
1571 int is_known = 1;
1572 int32_t flags = 0;
1573 uint32_t this_bw = 0;
1574 if (statuses) {
1575 /* need to extract router info */
1576 status = smartlist_get(sl, i);
1577 if (router_digest_is_me(status->identity_digest))
1578 me_idx = i;
1579 router = router_get_by_digest(status->identity_digest);
1580 is_exit = status->is_exit;
1581 is_guard = status->is_possible_guard;
1582 if (router) {
1583 this_bw = router_get_advertised_bandwidth(router);
1584 } else { /* guess */
1585 is_known = 0;
1586 flags = status->is_fast ? 1 : 0;
1587 flags |= is_exit ? 2 : 0;
1588 flags |= is_guard ? 4 : 0;
1590 } else {
1591 router = smartlist_get(sl, i);
1592 if (router_digest_is_me(router->cache_info.identity_digest))
1593 me_idx = i;
1594 is_exit = router->is_exit;
1595 is_guard = router->is_possible_guard;
1596 this_bw = router_get_advertised_bandwidth(router);
1598 if (is_exit)
1599 bitarray_set(exit_bits, i);
1600 if (is_guard)
1601 bitarray_set(guard_bits, i);
1602 /* if they claim something huge, don't believe it */
1603 if (this_bw > max_believable_bw) {
1604 char fp[HEX_DIGEST_LEN+1];
1605 base16_encode(fp, sizeof(fp), statuses ?
1606 status->identity_digest :
1607 router->cache_info.identity_digest,
1608 DIGEST_LEN);
1609 log_fn(LOG_PROTOCOL_WARN, LD_DIR,
1610 "Bandwidth %d for router %s (%s) exceeds allowed max %d, capping",
1611 this_bw, router ? router->nickname : "(null)",
1612 fp, max_believable_bw);
1613 this_bw = max_believable_bw;
1615 if (is_known) {
1616 bandwidths[i] = (int32_t) this_bw; // safe since MAX_BELIEVABLE<INT32_MAX
1617 if (is_guard)
1618 total_guard_bw += this_bw;
1619 else
1620 total_nonguard_bw += this_bw;
1621 if (is_exit)
1622 total_exit_bw += this_bw;
1623 else
1624 total_nonexit_bw += this_bw;
1625 } else {
1626 ++n_unknown;
1627 bandwidths[i] = -flags;
1631 /* Now, fill in the unknown values. */
1632 if (n_unknown) {
1633 int32_t avg_fast, avg_slow;
1634 if (total_exit_bw+total_nonexit_bw) {
1635 /* if there's some bandwidth, there's at least one known router,
1636 * so no worries about div by 0 here */
1637 int n_known = smartlist_len(sl)-n_unknown;
1638 avg_fast = avg_slow = (int32_t)
1639 ((total_exit_bw+total_nonexit_bw)/((uint64_t) n_known));
1640 } else {
1641 avg_fast = 40000;
1642 avg_slow = 20000;
1644 for (i=0; i<(unsigned)smartlist_len(sl); ++i) {
1645 int32_t bw = bandwidths[i];
1646 if (bw>=0)
1647 continue;
1648 is_exit = ((-bw)&2);
1649 is_guard = ((-bw)&4);
1650 bandwidths[i] = ((-bw)&1) ? avg_fast : avg_slow;
1651 if (is_exit)
1652 total_exit_bw += bandwidths[i];
1653 else
1654 total_nonexit_bw += bandwidths[i];
1655 if (is_guard)
1656 total_guard_bw += bandwidths[i];
1657 else
1658 total_nonguard_bw += bandwidths[i];
1662 /* If there's no bandwidth at all, pick at random. */
1663 if (!(total_exit_bw+total_nonexit_bw)) {
1664 tor_free(bandwidths);
1665 tor_free(exit_bits);
1666 tor_free(guard_bits);
1667 return smartlist_choose(sl);
1670 /* Figure out how to weight exits and guards */
1672 double all_bw = U64_TO_DBL(total_exit_bw+total_nonexit_bw);
1673 double exit_bw = U64_TO_DBL(total_exit_bw);
1674 double guard_bw = U64_TO_DBL(total_guard_bw);
1676 * For detailed derivation of this formula, see
1677 * http://archives.seul.org/or/dev/Jul-2007/msg00056.html
1679 if (rule == WEIGHT_FOR_EXIT)
1680 exit_weight = 1.0;
1681 else
1682 exit_weight = 1.0 - all_bw/(3.0*exit_bw);
1684 if (rule == WEIGHT_FOR_GUARD)
1685 guard_weight = 1.0;
1686 else
1687 guard_weight = 1.0 - all_bw/(3.0*guard_bw);
1689 if (exit_weight <= 0.0)
1690 exit_weight = 0.0;
1692 if (guard_weight <= 0.0)
1693 guard_weight = 0.0;
1695 total_bw = 0;
1696 sl_last_weighted_bw_of_me = 0;
1697 for (i=0; i < (unsigned)smartlist_len(sl); i++) {
1698 uint64_t bw;
1699 is_exit = bitarray_is_set(exit_bits, i);
1700 is_guard = bitarray_is_set(guard_bits, i);
1701 if (is_exit && is_guard)
1702 bw = ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
1703 else if (is_guard)
1704 bw = ((uint64_t)(bandwidths[i] * guard_weight));
1705 else if (is_exit)
1706 bw = ((uint64_t)(bandwidths[i] * exit_weight));
1707 else
1708 bw = bandwidths[i];
1709 total_bw += bw;
1710 if (i == (unsigned) me_idx)
1711 sl_last_weighted_bw_of_me = bw;
1715 /* XXXX021 this is a kludge to expose these values. */
1716 sl_last_total_weighted_bw = total_bw;
1718 log_debug(LD_CIRC, "Total weighted bw = "U64_FORMAT
1719 ", exit bw = "U64_FORMAT
1720 ", nonexit bw = "U64_FORMAT", exit weight = %lf "
1721 "(for exit == %d)"
1722 ", guard bw = "U64_FORMAT
1723 ", nonguard bw = "U64_FORMAT", guard weight = %lf "
1724 "(for guard == %d)",
1725 U64_PRINTF_ARG(total_bw),
1726 U64_PRINTF_ARG(total_exit_bw), U64_PRINTF_ARG(total_nonexit_bw),
1727 exit_weight, (int)(rule == WEIGHT_FOR_EXIT),
1728 U64_PRINTF_ARG(total_guard_bw), U64_PRINTF_ARG(total_nonguard_bw),
1729 guard_weight, (int)(rule == WEIGHT_FOR_GUARD));
1731 /* Almost done: choose a random value from the bandwidth weights. */
1732 rand_bw = crypto_rand_uint64(total_bw);
1734 /* Last, count through sl until we get to the element we picked */
1735 tmp = 0;
1736 for (i=0; i < (unsigned)smartlist_len(sl); i++) {
1737 is_exit = bitarray_is_set(exit_bits, i);
1738 is_guard = bitarray_is_set(guard_bits, i);
1740 /* Weights can be 0 if not counting guards/exits */
1741 if (is_exit && is_guard)
1742 tmp += ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
1743 else if (is_guard)
1744 tmp += ((uint64_t)(bandwidths[i] * guard_weight));
1745 else if (is_exit)
1746 tmp += ((uint64_t)(bandwidths[i] * exit_weight));
1747 else
1748 tmp += bandwidths[i];
1750 if (tmp >= rand_bw)
1751 break;
1753 if (i == (unsigned)smartlist_len(sl)) {
1754 /* This was once possible due to round-off error, but shouldn't be able
1755 * to occur any longer. */
1756 tor_fragile_assert();
1757 --i;
1758 log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
1759 " which router we chose. Please tell the developers. "
1760 U64_FORMAT " " U64_FORMAT " " U64_FORMAT, U64_PRINTF_ARG(tmp),
1761 U64_PRINTF_ARG(rand_bw), U64_PRINTF_ARG(total_bw));
1763 tor_free(bandwidths);
1764 tor_free(exit_bits);
1765 tor_free(guard_bits);
1766 return smartlist_get(sl, i);
1769 /** Choose a random element of router list <b>sl</b>, weighted by
1770 * the advertised bandwidth of each router.
1772 routerinfo_t *
1773 routerlist_sl_choose_by_bandwidth(smartlist_t *sl,
1774 bandwidth_weight_rule_t rule)
1776 return smartlist_choose_by_bandwidth(sl, rule, 0);
1779 /** Choose a random element of status list <b>sl</b>, weighted by
1780 * the advertised bandwidth of each status.
1782 routerstatus_t *
1783 routerstatus_sl_choose_by_bandwidth(smartlist_t *sl)
1785 /* We are choosing neither exit nor guard here. Weight accordingly. */
1786 return smartlist_choose_by_bandwidth(sl, NO_WEIGHTING, 1);
1789 /** Return a random running router from the routerlist. If any node
1790 * named in <b>preferred</b> is available, pick one of those. Never
1791 * pick a node whose routerinfo is in
1792 * <b>excludedsmartlist</b>, or whose routerinfo matches <b>excludedset</b>,
1793 * even if they are the only nodes
1794 * available. If <b>CRN_STRICT_PREFERRED</b> is set in flags, never pick
1795 * any node besides those in <b>preferred</b>.
1796 * If <b>CRN_NEED_UPTIME</b> is set in flags and any router has more than
1797 * a minimum uptime, return one of those.
1798 * If <b>CRN_NEED_CAPACITY</b> is set in flags, weight your choice by the
1799 * advertised capacity of each router.
1800 * If <b>CRN_ALLOW_INVALID</b> is not set in flags, consider only Valid
1801 * routers.
1802 * If <b>CRN_NEED_GUARD</b> is set in flags, consider only Guard routers.
1803 * If <b>CRN_WEIGHT_AS_EXIT</b> is set in flags, we weight bandwidths as if
1804 * picking an exit node, otherwise we weight bandwidths for picking a relay
1805 * node (that is, possibly discounting exit nodes).
1807 routerinfo_t *
1808 router_choose_random_node(const char *preferred,
1809 smartlist_t *excludedsmartlist,
1810 routerset_t *excludedset,
1811 router_crn_flags_t flags)
1813 const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
1814 const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
1815 const int need_guard = (flags & CRN_NEED_GUARD) != 0;
1816 const int allow_invalid = (flags & CRN_ALLOW_INVALID) != 0;
1817 const int strict = (flags & CRN_STRICT_PREFERRED) != 0;
1818 const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
1820 smartlist_t *sl, *excludednodes;
1821 routerinfo_t *choice = NULL, *r;
1822 bandwidth_weight_rule_t rule;
1824 tor_assert(!(weight_for_exit && need_guard));
1825 rule = weight_for_exit ? WEIGHT_FOR_EXIT :
1826 (need_guard ? WEIGHT_FOR_GUARD : NO_WEIGHTING);
1828 excludednodes = smartlist_create();
1830 /* Exclude relays that allow single hop exit circuits, if the user
1831 * wants to (such relays might be risky) */
1832 if (get_options()->ExcludeSingleHopRelays) {
1833 routerlist_t *rl = router_get_routerlist();
1834 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
1835 if (r->allow_single_hop_exits) {
1836 smartlist_add(excludednodes, r);
1840 if ((r = routerlist_find_my_routerinfo())) {
1841 smartlist_add(excludednodes, r);
1842 routerlist_add_family(excludednodes, r);
1845 /* Try the preferred nodes first. Ignore need_uptime and need_capacity
1846 * and need_guard, since the user explicitly asked for these nodes. */
1847 if (preferred) {
1848 sl = smartlist_create();
1849 add_nickname_list_to_smartlist(sl,preferred,1);
1850 smartlist_subtract(sl,excludednodes);
1851 if (excludedsmartlist)
1852 smartlist_subtract(sl,excludedsmartlist);
1853 if (excludedset)
1854 routerset_subtract_routers(sl,excludedset);
1855 choice = smartlist_choose(sl);
1856 smartlist_free(sl);
1858 if (!choice && !strict) {
1859 /* Then give up on our preferred choices: any node
1860 * will do that has the required attributes. */
1861 sl = smartlist_create();
1862 router_add_running_routers_to_smartlist(sl, allow_invalid,
1863 need_uptime, need_capacity,
1864 need_guard);
1865 smartlist_subtract(sl,excludednodes);
1866 if (excludedsmartlist)
1867 smartlist_subtract(sl,excludedsmartlist);
1868 if (excludedset)
1869 routerset_subtract_routers(sl,excludedset);
1871 if (need_capacity || need_guard)
1872 choice = routerlist_sl_choose_by_bandwidth(sl, rule);
1873 else
1874 choice = smartlist_choose(sl);
1876 smartlist_free(sl);
1877 if (!choice && (need_uptime || need_capacity || need_guard)) {
1878 /* try once more -- recurse but with fewer restrictions. */
1879 log_info(LD_CIRC,
1880 "We couldn't find any live%s%s%s routers; falling back "
1881 "to list of all routers.",
1882 need_capacity?", fast":"",
1883 need_uptime?", stable":"",
1884 need_guard?", guard":"");
1885 flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD);
1886 choice = router_choose_random_node(
1887 NULL, excludedsmartlist, excludedset, flags);
1890 smartlist_free(excludednodes);
1891 if (!choice) {
1892 if (strict) {
1893 log_warn(LD_CIRC, "All preferred nodes were down when trying to choose "
1894 "node, and the Strict[...]Nodes option is set. Failing.");
1895 } else {
1896 log_warn(LD_CIRC,
1897 "No available nodes when trying to choose node. Failing.");
1900 return choice;
1903 /** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
1904 * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
1905 * (which is optionally prefixed with a single dollar sign). Return false if
1906 * <b>hexdigest</b> is malformed, or it doesn't match. */
1907 static INLINE int
1908 hex_digest_matches(const char *hexdigest, const char *identity_digest,
1909 const char *nickname, int is_named)
1911 char digest[DIGEST_LEN];
1912 size_t len;
1913 tor_assert(hexdigest);
1914 if (hexdigest[0] == '$')
1915 ++hexdigest;
1917 len = strlen(hexdigest);
1918 if (len < HEX_DIGEST_LEN)
1919 return 0;
1920 else if (len > HEX_DIGEST_LEN &&
1921 (hexdigest[HEX_DIGEST_LEN] == '=' ||
1922 hexdigest[HEX_DIGEST_LEN] == '~')) {
1923 if (strcasecmp(hexdigest+HEX_DIGEST_LEN+1, nickname))
1924 return 0;
1925 if (hexdigest[HEX_DIGEST_LEN] == '=' && !is_named)
1926 return 0;
1929 if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
1930 return 0;
1931 return (!memcmp(digest, identity_digest, DIGEST_LEN));
1934 /** Return true iff the digest of <b>router</b>'s identity key,
1935 * encoded in hexadecimal, matches <b>hexdigest</b> (which is
1936 * optionally prefixed with a single dollar sign). Return false if
1937 * <b>hexdigest</b> is malformed, or it doesn't match. */
1938 static INLINE int
1939 router_hex_digest_matches(routerinfo_t *router, const char *hexdigest)
1941 return hex_digest_matches(hexdigest, router->cache_info.identity_digest,
1942 router->nickname, router->is_named);
1945 /** Return true if <b>router</b>'s nickname matches <b>nickname</b>
1946 * (case-insensitive), or if <b>router's</b> identity key digest
1947 * matches a hexadecimal value stored in <b>nickname</b>. Return
1948 * false otherwise. */
1949 static int
1950 router_nickname_matches(routerinfo_t *router, const char *nickname)
1952 if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
1953 return 1;
1954 return router_hex_digest_matches(router, nickname);
1957 /** Return the router in our routerlist whose (case-insensitive)
1958 * nickname or (case-sensitive) hexadecimal key digest is
1959 * <b>nickname</b>. Return NULL if no such router is known.
1961 routerinfo_t *
1962 router_get_by_nickname(const char *nickname, int warn_if_unnamed)
1964 int maybedigest;
1965 char digest[DIGEST_LEN];
1966 routerinfo_t *best_match=NULL;
1967 int n_matches = 0;
1968 const char *named_digest = NULL;
1970 tor_assert(nickname);
1971 if (!routerlist)
1972 return NULL;
1973 if (nickname[0] == '$')
1974 return router_get_by_hexdigest(nickname);
1975 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
1976 return NULL;
1977 if (server_mode(get_options()) &&
1978 !strcasecmp(nickname, get_options()->Nickname))
1979 return router_get_my_routerinfo();
1981 maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
1982 (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
1984 if ((named_digest = networkstatus_get_router_digest_by_nickname(nickname))) {
1985 return rimap_get(routerlist->identity_map, named_digest);
1987 if (networkstatus_nickname_is_unnamed(nickname))
1988 return NULL;
1990 /* If we reach this point, there's no canonical value for the nickname. */
1992 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1994 if (!strcasecmp(router->nickname, nickname)) {
1995 ++n_matches;
1996 if (n_matches <= 1 || router->is_running)
1997 best_match = router;
1998 } else if (maybedigest &&
1999 !memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN)
2001 if (router_hex_digest_matches(router, nickname))
2002 return router;
2003 /* If we reach this point, we have a ID=name syntax that matches the
2004 * identity but not the name. That isn't an acceptable match. */
2008 if (best_match) {
2009 if (warn_if_unnamed && n_matches > 1) {
2010 smartlist_t *fps = smartlist_create();
2011 int any_unwarned = 0;
2012 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
2014 routerstatus_t *rs;
2015 char *desc;
2016 size_t dlen;
2017 char fp[HEX_DIGEST_LEN+1];
2018 if (strcasecmp(router->nickname, nickname))
2019 continue;
2020 rs = router_get_consensus_status_by_id(
2021 router->cache_info.identity_digest);
2022 if (rs && !rs->name_lookup_warned) {
2023 rs->name_lookup_warned = 1;
2024 any_unwarned = 1;
2026 base16_encode(fp, sizeof(fp),
2027 router->cache_info.identity_digest, DIGEST_LEN);
2028 dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
2029 desc = tor_malloc(dlen);
2030 tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
2031 fp, router->address, router->or_port);
2032 smartlist_add(fps, desc);
2034 if (any_unwarned) {
2035 char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
2036 log_warn(LD_CONFIG,
2037 "There are multiple matches for the nickname \"%s\","
2038 " but none is listed as named by the directory authorities. "
2039 "Choosing one arbitrarily. If you meant one in particular, "
2040 "you should say %s.", nickname, alternatives);
2041 tor_free(alternatives);
2043 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
2044 smartlist_free(fps);
2045 } else if (warn_if_unnamed) {
2046 routerstatus_t *rs = router_get_consensus_status_by_id(
2047 best_match->cache_info.identity_digest);
2048 if (rs && !rs->name_lookup_warned) {
2049 char fp[HEX_DIGEST_LEN+1];
2050 base16_encode(fp, sizeof(fp),
2051 best_match->cache_info.identity_digest, DIGEST_LEN);
2052 log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
2053 "name is not registered, so it could be used by any server, "
2054 "not just the one you meant. "
2055 "To make sure you get the same server in the future, refer to "
2056 "it by key, as \"$%s\".", nickname, fp);
2057 rs->name_lookup_warned = 1;
2060 return best_match;
2063 return NULL;
2066 /** Try to find a routerinfo for <b>digest</b>. If we don't have one,
2067 * return 1. If we do, ask tor_version_as_new_as() for the answer.
2070 router_digest_version_as_new_as(const char *digest, const char *cutoff)
2072 routerinfo_t *router = router_get_by_digest(digest);
2073 if (!router)
2074 return 1;
2075 return tor_version_as_new_as(router->platform, cutoff);
2078 /** Return true iff <b>digest</b> is the digest of the identity key of a
2079 * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
2080 * is zero, any authority is okay. */
2082 router_digest_is_trusted_dir_type(const char *digest, authority_type_t type)
2084 if (!trusted_dir_servers)
2085 return 0;
2086 if (authdir_mode(get_options()) && router_digest_is_me(digest))
2087 return 1;
2088 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
2089 if (!memcmp(digest, ent->digest, DIGEST_LEN)) {
2090 return (!type) || ((type & ent->type) != 0);
2092 return 0;
2095 /** Return true iff <b>addr</b> is the address of one of our trusted
2096 * directory authorities. */
2098 router_addr_is_trusted_dir(uint32_t addr)
2100 if (!trusted_dir_servers)
2101 return 0;
2102 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
2103 if (ent->addr == addr)
2104 return 1;
2106 return 0;
2109 /** If hexdigest is correctly formed, base16_decode it into
2110 * digest, which must have DIGEST_LEN space in it.
2111 * Return 0 on success, -1 on failure.
2114 hexdigest_to_digest(const char *hexdigest, char *digest)
2116 if (hexdigest[0]=='$')
2117 ++hexdigest;
2118 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
2119 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
2120 return -1;
2121 return 0;
2124 /** Return the router in our routerlist whose hexadecimal key digest
2125 * is <b>hexdigest</b>. Return NULL if no such router is known. */
2126 routerinfo_t *
2127 router_get_by_hexdigest(const char *hexdigest)
2129 char digest[DIGEST_LEN];
2130 size_t len;
2131 routerinfo_t *ri;
2133 tor_assert(hexdigest);
2134 if (!routerlist)
2135 return NULL;
2136 if (hexdigest[0]=='$')
2137 ++hexdigest;
2138 len = strlen(hexdigest);
2139 if (hexdigest_to_digest(hexdigest, digest) < 0)
2140 return NULL;
2142 ri = router_get_by_digest(digest);
2144 if (ri && len > HEX_DIGEST_LEN) {
2145 if (hexdigest[HEX_DIGEST_LEN] == '=') {
2146 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1) ||
2147 !ri->is_named)
2148 return NULL;
2149 } else if (hexdigest[HEX_DIGEST_LEN] == '~') {
2150 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1))
2151 return NULL;
2152 } else {
2153 return NULL;
2157 return ri;
2160 /** Return the router in our routerlist whose 20-byte key digest
2161 * is <b>digest</b>. Return NULL if no such router is known. */
2162 routerinfo_t *
2163 router_get_by_digest(const char *digest)
2165 tor_assert(digest);
2167 if (!routerlist) return NULL;
2169 // routerlist_assert_ok(routerlist);
2171 return rimap_get(routerlist->identity_map, digest);
2174 /** Return the router in our routerlist whose 20-byte descriptor
2175 * is <b>digest</b>. Return NULL if no such router is known. */
2176 signed_descriptor_t *
2177 router_get_by_descriptor_digest(const char *digest)
2179 tor_assert(digest);
2181 if (!routerlist) return NULL;
2183 return sdmap_get(routerlist->desc_digest_map, digest);
2186 /** Return the signed descriptor for the router in our routerlist whose
2187 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
2188 * is known. */
2189 signed_descriptor_t *
2190 router_get_by_extrainfo_digest(const char *digest)
2192 tor_assert(digest);
2194 if (!routerlist) return NULL;
2196 return sdmap_get(routerlist->desc_by_eid_map, digest);
2199 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
2200 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
2201 * document is known. */
2202 signed_descriptor_t *
2203 extrainfo_get_by_descriptor_digest(const char *digest)
2205 extrainfo_t *ei;
2206 tor_assert(digest);
2207 if (!routerlist) return NULL;
2208 ei = eimap_get(routerlist->extra_info_map, digest);
2209 return ei ? &ei->cache_info : NULL;
2212 /** Return a pointer to the signed textual representation of a descriptor.
2213 * The returned string is not guaranteed to be NUL-terminated: the string's
2214 * length will be in desc-\>signed_descriptor_len.
2216 * If <b>with_annotations</b> is set, the returned string will include
2217 * the annotations
2218 * (if any) preceding the descriptor. This will increase the length of the
2219 * string by desc-\>annotations_len.
2221 * The caller must not free the string returned.
2223 static const char *
2224 signed_descriptor_get_body_impl(signed_descriptor_t *desc,
2225 int with_annotations)
2227 const char *r = NULL;
2228 size_t len = desc->signed_descriptor_len;
2229 off_t offset = desc->saved_offset;
2230 if (with_annotations)
2231 len += desc->annotations_len;
2232 else
2233 offset += desc->annotations_len;
2235 tor_assert(len > 32);
2236 if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
2237 desc_store_t *store = desc_get_store(router_get_routerlist(), desc);
2238 if (store && store->mmap) {
2239 tor_assert(desc->saved_offset + len <= store->mmap->size);
2240 r = store->mmap->data + offset;
2241 } else if (store) {
2242 log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
2243 "mmaped in our cache. Is another process running in our data "
2244 "directory? Exiting.");
2245 exit(1);
2248 if (!r) /* no mmap, or not in cache. */
2249 r = desc->signed_descriptor_body +
2250 (with_annotations ? 0 : desc->annotations_len);
2252 tor_assert(r);
2253 if (!with_annotations) {
2254 if (memcmp("router ", r, 7) && memcmp("extra-info ", r, 11)) {
2255 char *cp = tor_strndup(r, 64);
2256 log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
2257 "Is another process running in our data directory? Exiting.",
2258 desc, escaped(cp));
2259 exit(1);
2263 return r;
2266 /** Return a pointer to the signed textual representation of a descriptor.
2267 * The returned string is not guaranteed to be NUL-terminated: the string's
2268 * length will be in desc-\>signed_descriptor_len.
2270 * The caller must not free the string returned.
2272 const char *
2273 signed_descriptor_get_body(signed_descriptor_t *desc)
2275 return signed_descriptor_get_body_impl(desc, 0);
2278 /** As signed_descriptor_get_body(), but points to the beginning of the
2279 * annotations section rather than the beginning of the descriptor. */
2280 const char *
2281 signed_descriptor_get_annotations(signed_descriptor_t *desc)
2283 return signed_descriptor_get_body_impl(desc, 1);
2286 /** Return the current list of all known routers. */
2287 routerlist_t *
2288 router_get_routerlist(void)
2290 if (PREDICT_UNLIKELY(!routerlist)) {
2291 routerlist = tor_malloc_zero(sizeof(routerlist_t));
2292 routerlist->routers = smartlist_create();
2293 routerlist->old_routers = smartlist_create();
2294 routerlist->identity_map = rimap_new();
2295 routerlist->desc_digest_map = sdmap_new();
2296 routerlist->desc_by_eid_map = sdmap_new();
2297 routerlist->extra_info_map = eimap_new();
2299 routerlist->desc_store.fname_base = "cached-descriptors";
2300 routerlist->desc_store.fname_alt_base = "cached-routers";
2301 routerlist->extrainfo_store.fname_base = "cached-extrainfo";
2303 routerlist->desc_store.type = ROUTER_STORE;
2304 routerlist->extrainfo_store.type = EXTRAINFO_STORE;
2306 routerlist->desc_store.description = "router descriptors";
2307 routerlist->extrainfo_store.description = "extra-info documents";
2309 return routerlist;
2312 /** Free all storage held by <b>router</b>. */
2313 void
2314 routerinfo_free(routerinfo_t *router)
2316 if (!router)
2317 return;
2319 tor_free(router->cache_info.signed_descriptor_body);
2320 tor_free(router->address);
2321 tor_free(router->nickname);
2322 tor_free(router->platform);
2323 tor_free(router->contact_info);
2324 if (router->onion_pkey)
2325 crypto_free_pk_env(router->onion_pkey);
2326 if (router->identity_pkey)
2327 crypto_free_pk_env(router->identity_pkey);
2328 if (router->declared_family) {
2329 SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
2330 smartlist_free(router->declared_family);
2332 addr_policy_list_free(router->exit_policy);
2334 /* XXXX Remove if this turns out to affect performance. */
2335 memset(router, 77, sizeof(routerinfo_t));
2337 tor_free(router);
2340 /** Release all storage held by <b>extrainfo</b> */
2341 void
2342 extrainfo_free(extrainfo_t *extrainfo)
2344 if (!extrainfo)
2345 return;
2346 tor_free(extrainfo->cache_info.signed_descriptor_body);
2347 tor_free(extrainfo->pending_sig);
2349 /* XXXX remove this if it turns out to slow us down. */
2350 memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
2351 tor_free(extrainfo);
2354 /** Release storage held by <b>sd</b>. */
2355 static void
2356 signed_descriptor_free(signed_descriptor_t *sd)
2358 tor_free(sd->signed_descriptor_body);
2360 /* XXXX remove this once more bugs go away. */
2361 memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
2362 tor_free(sd);
2365 /** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
2367 static signed_descriptor_t *
2368 signed_descriptor_from_routerinfo(routerinfo_t *ri)
2370 signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t));
2371 memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t));
2372 sd->routerlist_index = -1;
2373 ri->cache_info.signed_descriptor_body = NULL;
2374 routerinfo_free(ri);
2375 return sd;
2378 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
2379 static void
2380 _extrainfo_free(void *e)
2382 extrainfo_free(e);
2385 /** Free all storage held by a routerlist <b>rl</b>. */
2386 void
2387 routerlist_free(routerlist_t *rl)
2389 tor_assert(rl);
2390 rimap_free(rl->identity_map, NULL);
2391 sdmap_free(rl->desc_digest_map, NULL);
2392 sdmap_free(rl->desc_by_eid_map, NULL);
2393 eimap_free(rl->extra_info_map, _extrainfo_free);
2394 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
2395 routerinfo_free(r));
2396 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
2397 signed_descriptor_free(sd));
2398 smartlist_free(rl->routers);
2399 smartlist_free(rl->old_routers);
2400 if (routerlist->desc_store.mmap)
2401 tor_munmap_file(routerlist->desc_store.mmap);
2402 if (routerlist->extrainfo_store.mmap)
2403 tor_munmap_file(routerlist->extrainfo_store.mmap);
2404 tor_free(rl);
2406 router_dir_info_changed();
2409 /** Log information about how much memory is being used for routerlist,
2410 * at log level <b>severity</b>. */
2411 void
2412 dump_routerlist_mem_usage(int severity)
2414 uint64_t livedescs = 0;
2415 uint64_t olddescs = 0;
2416 if (!routerlist)
2417 return;
2418 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
2419 livedescs += r->cache_info.signed_descriptor_len);
2420 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
2421 olddescs += sd->signed_descriptor_len);
2423 log(severity, LD_DIR,
2424 "In %d live descriptors: "U64_FORMAT" bytes. "
2425 "In %d old descriptors: "U64_FORMAT" bytes.",
2426 smartlist_len(routerlist->routers), U64_PRINTF_ARG(livedescs),
2427 smartlist_len(routerlist->old_routers), U64_PRINTF_ARG(olddescs));
2429 #if 0
2431 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
2432 networkstatus_t *consensus = networkstatus_get_latest_consensus();
2433 log(severity, LD_DIR, "Now let's look through old_descriptors!");
2434 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd, {
2435 int in_v2 = 0;
2436 int in_v3 = 0;
2437 char published[ISO_TIME_LEN+1];
2438 char last_valid_until[ISO_TIME_LEN+1];
2439 char last_served_at[ISO_TIME_LEN+1];
2440 char id[HEX_DIGEST_LEN+1];
2441 routerstatus_t *rs;
2442 format_iso_time(published, sd->published_on);
2443 format_iso_time(last_valid_until, sd->last_listed_as_valid_until);
2444 format_iso_time(last_served_at, sd->last_served_at);
2445 base16_encode(id, sizeof(id), sd->identity_digest, DIGEST_LEN);
2446 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
2448 rs = networkstatus_v2_find_entry(ns, sd->identity_digest);
2449 if (rs && !memcmp(rs->descriptor_digest,
2450 sd->signed_descriptor_digest, DIGEST_LEN)) {
2451 in_v2 = 1; break;
2454 if (consensus) {
2455 rs = networkstatus_vote_find_entry(consensus, sd->identity_digest);
2456 if (rs && !memcmp(rs->descriptor_digest,
2457 sd->signed_descriptor_digest, DIGEST_LEN))
2458 in_v3 = 1;
2460 log(severity, LD_DIR,
2461 "Old descriptor for %s (published %s) %sin v2 ns, %sin v3 "
2462 "consensus. Last valid until %s; last served at %s.",
2463 id, published, in_v2 ? "" : "not ", in_v3 ? "" : "not ",
2464 last_valid_until, last_served_at);
2467 #endif
2470 /** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
2471 * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
2472 * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
2473 * is not in <b>sl</b>. */
2474 static INLINE int
2475 _routerlist_find_elt(smartlist_t *sl, void *ri, int idx)
2477 if (idx < 0) {
2478 idx = -1;
2479 SMARTLIST_FOREACH(sl, routerinfo_t *, r,
2480 if (r == ri) {
2481 idx = r_sl_idx;
2482 break;
2484 } else {
2485 tor_assert(idx < smartlist_len(sl));
2486 tor_assert(smartlist_get(sl, idx) == ri);
2488 return idx;
2491 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
2492 * as needed. There must be no previous member of <b>rl</b> with the same
2493 * identity digest as <b>ri</b>: If there is, call routerlist_replace
2494 * instead.
2496 static void
2497 routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
2499 routerinfo_t *ri_old;
2501 /* XXXX Remove if this slows us down. */
2502 routerinfo_t *ri_generated = router_get_my_routerinfo();
2503 tor_assert(ri_generated != ri);
2505 tor_assert(ri->cache_info.routerlist_index == -1);
2507 ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
2508 tor_assert(!ri_old);
2509 sdmap_set(rl->desc_digest_map, ri->cache_info.signed_descriptor_digest,
2510 &(ri->cache_info));
2511 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
2512 sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
2513 &ri->cache_info);
2514 smartlist_add(rl->routers, ri);
2515 ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
2516 router_dir_info_changed();
2517 #ifdef DEBUG_ROUTERLIST
2518 routerlist_assert_ok(rl);
2519 #endif
2522 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
2523 * corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
2524 * we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
2525 static int
2526 extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
2528 int r = 0;
2529 routerinfo_t *ri = rimap_get(rl->identity_map,
2530 ei->cache_info.identity_digest);
2531 signed_descriptor_t *sd =
2532 sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
2533 extrainfo_t *ei_tmp;
2536 /* XXXX remove this code if it slows us down. */
2537 extrainfo_t *ei_generated = router_get_my_extrainfo();
2538 tor_assert(ei_generated != ei);
2541 if (!ri) {
2542 /* This router is unknown; we can't even verify the signature. Give up.*/
2543 goto done;
2545 if (routerinfo_incompatible_with_extrainfo(ri, ei, sd, NULL)) {
2546 goto done;
2549 /* Okay, if we make it here, we definitely have a router corresponding to
2550 * this extrainfo. */
2552 ei_tmp = eimap_set(rl->extra_info_map,
2553 ei->cache_info.signed_descriptor_digest,
2554 ei);
2555 r = 1;
2556 if (ei_tmp) {
2557 rl->extrainfo_store.bytes_dropped +=
2558 ei_tmp->cache_info.signed_descriptor_len;
2559 extrainfo_free(ei_tmp);
2562 done:
2563 if (r == 0)
2564 extrainfo_free(ei);
2566 #ifdef DEBUG_ROUTERLIST
2567 routerlist_assert_ok(rl);
2568 #endif
2569 return r;
2572 #define should_cache_old_descriptors() \
2573 directory_caches_dir_info(get_options())
2575 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
2576 * a copy of router <b>ri</b> yet, add it to the list of old (not
2577 * recommended but still served) descriptors. Else free it. */
2578 static void
2579 routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
2582 /* XXXX remove this code if it slows us down. */
2583 routerinfo_t *ri_generated = router_get_my_routerinfo();
2584 tor_assert(ri_generated != ri);
2586 tor_assert(ri->cache_info.routerlist_index == -1);
2588 if (should_cache_old_descriptors() &&
2589 ri->purpose == ROUTER_PURPOSE_GENERAL &&
2590 !sdmap_get(rl->desc_digest_map,
2591 ri->cache_info.signed_descriptor_digest)) {
2592 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri);
2593 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2594 smartlist_add(rl->old_routers, sd);
2595 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2596 if (!tor_digest_is_zero(sd->extra_info_digest))
2597 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2598 } else {
2599 routerinfo_free(ri);
2601 #ifdef DEBUG_ROUTERLIST
2602 routerlist_assert_ok(rl);
2603 #endif
2606 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
2607 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl-&gt;routers,
2608 * idx) == ri, we don't need to do a linear search over the list to decide
2609 * which to remove. We fill the gap in rl-&gt;routers with a later element in
2610 * the list, if any exists. <b>ri</b> is freed.
2612 * If <b>make_old</b> is true, instead of deleting the router, we try adding
2613 * it to rl-&gt;old_routers. */
2614 void
2615 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old)
2617 routerinfo_t *ri_tmp;
2618 extrainfo_t *ei_tmp;
2619 int idx = ri->cache_info.routerlist_index;
2620 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2621 tor_assert(smartlist_get(rl->routers, idx) == ri);
2623 ri->cache_info.routerlist_index = -1;
2624 smartlist_del(rl->routers, idx);
2625 if (idx < smartlist_len(rl->routers)) {
2626 routerinfo_t *r = smartlist_get(rl->routers, idx);
2627 r->cache_info.routerlist_index = idx;
2630 ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
2631 router_dir_info_changed();
2632 tor_assert(ri_tmp == ri);
2634 if (make_old && should_cache_old_descriptors() &&
2635 ri->purpose == ROUTER_PURPOSE_GENERAL) {
2636 signed_descriptor_t *sd;
2637 sd = signed_descriptor_from_routerinfo(ri);
2638 smartlist_add(rl->old_routers, sd);
2639 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2640 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2641 if (!tor_digest_is_zero(sd->extra_info_digest))
2642 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2643 } else {
2644 signed_descriptor_t *sd_tmp;
2645 sd_tmp = sdmap_remove(rl->desc_digest_map,
2646 ri->cache_info.signed_descriptor_digest);
2647 tor_assert(sd_tmp == &(ri->cache_info));
2648 rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
2649 ei_tmp = eimap_remove(rl->extra_info_map,
2650 ri->cache_info.extra_info_digest);
2651 if (ei_tmp) {
2652 rl->extrainfo_store.bytes_dropped +=
2653 ei_tmp->cache_info.signed_descriptor_len;
2654 extrainfo_free(ei_tmp);
2656 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
2657 sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
2658 routerinfo_free(ri);
2660 #ifdef DEBUG_ROUTERLIST
2661 routerlist_assert_ok(rl);
2662 #endif
2665 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
2666 * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
2667 * <b>sd</b>. */
2668 static void
2669 routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
2671 signed_descriptor_t *sd_tmp;
2672 extrainfo_t *ei_tmp;
2673 desc_store_t *store;
2674 if (idx == -1) {
2675 idx = sd->routerlist_index;
2677 tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
2678 /* XXXX edmanm's bridge relay triggered the following assert while
2679 * running 0.2.0.12-alpha. If anybody triggers this again, see if we
2680 * can get a backtrace. */
2681 tor_assert(smartlist_get(rl->old_routers, idx) == sd);
2682 tor_assert(idx == sd->routerlist_index);
2684 sd->routerlist_index = -1;
2685 smartlist_del(rl->old_routers, idx);
2686 if (idx < smartlist_len(rl->old_routers)) {
2687 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
2688 d->routerlist_index = idx;
2690 sd_tmp = sdmap_remove(rl->desc_digest_map,
2691 sd->signed_descriptor_digest);
2692 tor_assert(sd_tmp == sd);
2693 store = desc_get_store(rl, sd);
2694 if (store)
2695 store->bytes_dropped += sd->signed_descriptor_len;
2697 ei_tmp = eimap_remove(rl->extra_info_map,
2698 sd->extra_info_digest);
2699 if (ei_tmp) {
2700 rl->extrainfo_store.bytes_dropped +=
2701 ei_tmp->cache_info.signed_descriptor_len;
2702 extrainfo_free(ei_tmp);
2704 if (!tor_digest_is_zero(sd->extra_info_digest))
2705 sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
2707 signed_descriptor_free(sd);
2708 #ifdef DEBUG_ROUTERLIST
2709 routerlist_assert_ok(rl);
2710 #endif
2713 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
2714 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
2715 * smartlist_get(rl-&gt;routers, idx) == ri, we don't need to do a linear
2716 * search over the list to decide which to remove. We put ri_new in the same
2717 * index as ri_old, if possible. ri is freed as appropriate.
2719 * If should_cache_descriptors() is true, instead of deleting the router,
2720 * we add it to rl-&gt;old_routers. */
2721 static void
2722 routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old,
2723 routerinfo_t *ri_new)
2725 int idx;
2727 routerinfo_t *ri_tmp;
2728 extrainfo_t *ei_tmp;
2730 /* XXXX Remove this if it turns out to slow us down. */
2731 routerinfo_t *ri_generated = router_get_my_routerinfo();
2732 tor_assert(ri_generated != ri_new);
2734 tor_assert(ri_old != ri_new);
2735 tor_assert(ri_new->cache_info.routerlist_index == -1);
2737 idx = ri_old->cache_info.routerlist_index;
2738 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2739 tor_assert(smartlist_get(rl->routers, idx) == ri_old);
2741 router_dir_info_changed();
2742 if (idx >= 0) {
2743 smartlist_set(rl->routers, idx, ri_new);
2744 ri_old->cache_info.routerlist_index = -1;
2745 ri_new->cache_info.routerlist_index = idx;
2746 /* Check that ri_old is not in rl->routers anymore: */
2747 tor_assert( _routerlist_find_elt(rl->routers, ri_old, -1) == -1 );
2748 } else {
2749 log_warn(LD_BUG, "Appending entry from routerlist_replace.");
2750 routerlist_insert(rl, ri_new);
2751 return;
2753 if (memcmp(ri_old->cache_info.identity_digest,
2754 ri_new->cache_info.identity_digest, DIGEST_LEN)) {
2755 /* digests don't match; digestmap_set won't replace */
2756 rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
2758 ri_tmp = rimap_set(rl->identity_map,
2759 ri_new->cache_info.identity_digest, ri_new);
2760 tor_assert(!ri_tmp || ri_tmp == ri_old);
2761 sdmap_set(rl->desc_digest_map,
2762 ri_new->cache_info.signed_descriptor_digest,
2763 &(ri_new->cache_info));
2765 if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
2766 sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
2767 &ri_new->cache_info);
2770 if (should_cache_old_descriptors() &&
2771 ri_old->purpose == ROUTER_PURPOSE_GENERAL) {
2772 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
2773 smartlist_add(rl->old_routers, sd);
2774 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2775 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2776 if (!tor_digest_is_zero(sd->extra_info_digest))
2777 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2778 } else {
2779 if (memcmp(ri_old->cache_info.signed_descriptor_digest,
2780 ri_new->cache_info.signed_descriptor_digest,
2781 DIGEST_LEN)) {
2782 /* digests don't match; digestmap_set didn't replace */
2783 sdmap_remove(rl->desc_digest_map,
2784 ri_old->cache_info.signed_descriptor_digest);
2787 ei_tmp = eimap_remove(rl->extra_info_map,
2788 ri_old->cache_info.extra_info_digest);
2789 if (ei_tmp) {
2790 rl->extrainfo_store.bytes_dropped +=
2791 ei_tmp->cache_info.signed_descriptor_len;
2792 extrainfo_free(ei_tmp);
2794 if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
2795 sdmap_remove(rl->desc_by_eid_map,
2796 ri_old->cache_info.extra_info_digest);
2798 rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
2799 routerinfo_free(ri_old);
2801 #ifdef DEBUG_ROUTERLIST
2802 routerlist_assert_ok(rl);
2803 #endif
2806 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
2807 * it as a fresh routerinfo_t. */
2808 static routerinfo_t *
2809 routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
2811 routerinfo_t *ri;
2812 const char *body;
2814 body = signed_descriptor_get_annotations(sd);
2816 ri = router_parse_entry_from_string(body,
2817 body+sd->signed_descriptor_len+sd->annotations_len,
2818 0, 1, NULL);
2819 if (!ri)
2820 return NULL;
2821 memcpy(&ri->cache_info, sd, sizeof(signed_descriptor_t));
2822 sd->signed_descriptor_body = NULL; /* Steal reference. */
2823 ri->cache_info.routerlist_index = -1;
2825 routerlist_remove_old(rl, sd, -1);
2827 return ri;
2830 /** Free all memory held by the routerlist module. */
2831 void
2832 routerlist_free_all(void)
2834 if (routerlist)
2835 routerlist_free(routerlist);
2836 routerlist = NULL;
2837 if (warned_nicknames) {
2838 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
2839 smartlist_free(warned_nicknames);
2840 warned_nicknames = NULL;
2842 if (trusted_dir_servers) {
2843 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
2844 trusted_dir_server_free(ds));
2845 smartlist_free(trusted_dir_servers);
2846 trusted_dir_servers = NULL;
2848 if (trusted_dir_certs) {
2849 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
2850 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
2851 authority_cert_free(cert));
2852 smartlist_free(cl->certs);
2853 tor_free(cl);
2854 } DIGESTMAP_FOREACH_END;
2855 digestmap_free(trusted_dir_certs, NULL);
2856 trusted_dir_certs = NULL;
2860 /** Forget that we have issued any router-related warnings, so that we'll
2861 * warn again if we see the same errors. */
2862 void
2863 routerlist_reset_warnings(void)
2865 if (!warned_nicknames)
2866 warned_nicknames = smartlist_create();
2867 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
2868 smartlist_clear(warned_nicknames); /* now the list is empty. */
2870 networkstatus_reset_warnings();
2873 /** Mark the router with ID <b>digest</b> as running or non-running
2874 * in our routerlist. */
2875 void
2876 router_set_status(const char *digest, int up)
2878 routerinfo_t *router;
2879 routerstatus_t *status;
2880 tor_assert(digest);
2882 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
2883 if (!memcmp(d->digest, digest, DIGEST_LEN))
2884 d->is_running = up);
2886 router = router_get_by_digest(digest);
2887 if (router) {
2888 log_debug(LD_DIR,"Marking router '%s/%s' as %s.",
2889 router->nickname, router->address, up ? "up" : "down");
2890 if (!up && router_is_me(router) && !we_are_hibernating())
2891 log_warn(LD_NET, "We just marked ourself as down. Are your external "
2892 "addresses reachable?");
2893 router->is_running = up;
2895 status = router_get_consensus_status_by_id(digest);
2896 if (status && status->is_running != up) {
2897 status->is_running = up;
2898 control_event_networkstatus_changed_single(status);
2900 router_dir_info_changed();
2903 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
2904 * older entries (if any) with the same key. Note: Callers should not hold
2905 * their pointers to <b>router</b> if this function fails; <b>router</b>
2906 * will either be inserted into the routerlist or freed. Similarly, even
2907 * if this call succeeds, they should not hold their pointers to
2908 * <b>router</b> after subsequent calls with other routerinfo's -- they
2909 * might cause the original routerinfo to get freed.
2911 * Returns the status for the operation.
2913 * If <b>from_cache</b>, this descriptor came from our disk cache. If
2914 * <b>from_fetch</b>, we received it in response to a request we made.
2915 * (If both are false, that means it was uploaded to us as an auth dir
2916 * server or via the controller.)
2918 * This function should be called *after*
2919 * routers_update_status_from_consensus_networkstatus; subsequently, you
2920 * should call router_rebuild_store and routerlist_descriptors_added.
2922 was_router_added_t
2923 router_add_to_routerlist(routerinfo_t *router, const char **msg,
2924 int from_cache, int from_fetch)
2926 const char *id_digest;
2927 int authdir = authdir_mode_handles_descs(get_options(), router->purpose);
2928 int authdir_believes_valid = 0;
2929 routerinfo_t *old_router;
2930 networkstatus_t *consensus = networkstatus_get_latest_consensus();
2931 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
2932 int in_consensus = 0;
2934 tor_assert(msg);
2936 if (!routerlist)
2937 router_get_routerlist();
2939 id_digest = router->cache_info.identity_digest;
2941 /* Make sure that we haven't already got this exact descriptor. */
2942 if (sdmap_get(routerlist->desc_digest_map,
2943 router->cache_info.signed_descriptor_digest)) {
2944 log_info(LD_DIR,
2945 "Dropping descriptor that we already have for router '%s'",
2946 router->nickname);
2947 *msg = "Router descriptor was not new.";
2948 routerinfo_free(router);
2949 return ROUTER_WAS_NOT_NEW;
2952 if (authdir) {
2953 if (authdir_wants_to_reject_router(router, msg,
2954 !from_cache && !from_fetch)) {
2955 tor_assert(*msg);
2956 routerinfo_free(router);
2957 return ROUTER_AUTHDIR_REJECTS;
2959 authdir_believes_valid = router->is_valid;
2960 } else if (from_fetch) {
2961 /* Only check the descriptor digest against the network statuses when
2962 * we are receiving in response to a fetch. */
2964 if (!signed_desc_digest_is_recognized(&router->cache_info) &&
2965 !routerinfo_is_a_configured_bridge(router)) {
2966 /* We asked for it, so some networkstatus must have listed it when we
2967 * did. Save it if we're a cache in case somebody else asks for it. */
2968 log_info(LD_DIR,
2969 "Received a no-longer-recognized descriptor for router '%s'",
2970 router->nickname);
2971 *msg = "Router descriptor is not referenced by any network-status.";
2973 /* Only journal this desc if we'll be serving it. */
2974 if (!from_cache && should_cache_old_descriptors())
2975 signed_desc_append_to_journal(&router->cache_info,
2976 &routerlist->desc_store);
2977 routerlist_insert_old(routerlist, router);
2978 return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
2982 /* We no longer need a router with this descriptor digest. */
2983 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
2985 routerstatus_t *rs =
2986 networkstatus_v2_find_entry(ns, router->cache_info.identity_digest);
2987 if (rs && !memcmp(rs->descriptor_digest,
2988 router->cache_info.signed_descriptor_digest,
2989 DIGEST_LEN))
2990 rs->need_to_mirror = 0;
2992 if (consensus) {
2993 routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
2994 router->cache_info.identity_digest);
2995 if (rs && !memcmp(rs->descriptor_digest,
2996 router->cache_info.signed_descriptor_digest,
2997 DIGEST_LEN)) {
2998 in_consensus = 1;
2999 rs->need_to_mirror = 0;
3003 if (router->purpose == ROUTER_PURPOSE_GENERAL &&
3004 consensus && !in_consensus && !authdir) {
3005 /* If it's a general router not listed in the consensus, then don't
3006 * consider replacing the latest router with it. */
3007 if (!from_cache && should_cache_old_descriptors())
3008 signed_desc_append_to_journal(&router->cache_info,
3009 &routerlist->desc_store);
3010 routerlist_insert_old(routerlist, router);
3011 *msg = "Skipping router descriptor: not in consensus.";
3012 return ROUTER_NOT_IN_CONSENSUS;
3015 /* If we have a router with the same identity key, choose the newer one. */
3016 old_router = rimap_get(routerlist->identity_map,
3017 router->cache_info.identity_digest);
3018 if (old_router) {
3019 if (!in_consensus && (router->cache_info.published_on <=
3020 old_router->cache_info.published_on)) {
3021 /* Same key, but old. This one is not listed in the consensus. */
3022 log_debug(LD_DIR, "Skipping not-new descriptor for router '%s'",
3023 router->nickname);
3024 /* Only journal this desc if we'll be serving it. */
3025 if (!from_cache && should_cache_old_descriptors())
3026 signed_desc_append_to_journal(&router->cache_info,
3027 &routerlist->desc_store);
3028 routerlist_insert_old(routerlist, router);
3029 *msg = "Router descriptor was not new.";
3030 return ROUTER_WAS_NOT_NEW;
3031 } else {
3032 /* Same key, and either new, or listed in the consensus. */
3033 log_debug(LD_DIR, "Replacing entry for router '%s/%s' [%s]",
3034 router->nickname, old_router->nickname,
3035 hex_str(id_digest,DIGEST_LEN));
3036 if (router->addr == old_router->addr &&
3037 router->or_port == old_router->or_port) {
3038 /* these carry over when the address and orport are unchanged. */
3039 router->last_reachable = old_router->last_reachable;
3040 router->testing_since = old_router->testing_since;
3042 routerlist_replace(routerlist, old_router, router);
3043 if (!from_cache) {
3044 signed_desc_append_to_journal(&router->cache_info,
3045 &routerlist->desc_store);
3047 directory_set_dirty();
3048 *msg = authdir_believes_valid ? "Valid server updated" :
3049 ("Invalid server updated. (This dirserver is marking your "
3050 "server as unapproved.)");
3051 return ROUTER_ADDED_SUCCESSFULLY;
3055 /* We haven't seen a router with this identity before. Add it to the end of
3056 * the list. */
3057 routerlist_insert(routerlist, router);
3058 if (!from_cache)
3059 signed_desc_append_to_journal(&router->cache_info,
3060 &routerlist->desc_store);
3061 directory_set_dirty();
3062 return ROUTER_ADDED_SUCCESSFULLY;
3065 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
3066 * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
3067 * we actually inserted it, ROUTER_BAD_EI otherwise.
3069 was_router_added_t
3070 router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg,
3071 int from_cache, int from_fetch)
3073 int inserted;
3074 (void)from_fetch;
3075 if (msg) *msg = NULL;
3076 /*XXXX021 Do something with msg */
3078 inserted = extrainfo_insert(router_get_routerlist(), ei);
3080 if (inserted && !from_cache)
3081 signed_desc_append_to_journal(&ei->cache_info,
3082 &routerlist->extrainfo_store);
3084 if (inserted)
3085 return ROUTER_ADDED_SUCCESSFULLY;
3086 else
3087 return ROUTER_BAD_EI;
3090 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
3091 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
3092 * to, or later than that of *<b>b</b>. */
3093 static int
3094 _compare_old_routers_by_identity(const void **_a, const void **_b)
3096 int i;
3097 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
3098 if ((i = memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
3099 return i;
3100 return (int)(r1->published_on - r2->published_on);
3103 /** Internal type used to represent how long an old descriptor was valid,
3104 * where it appeared in the list of old descriptors, and whether it's extra
3105 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
3106 struct duration_idx_t {
3107 int duration;
3108 int idx;
3109 int old;
3112 /** Sorting helper: compare two duration_idx_t by their duration. */
3113 static int
3114 _compare_duration_idx(const void *_d1, const void *_d2)
3116 const struct duration_idx_t *d1 = _d1;
3117 const struct duration_idx_t *d2 = _d2;
3118 return d1->duration - d2->duration;
3121 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
3122 * must contain routerinfo_t with the same identity and with publication time
3123 * in ascending order. Remove members from this range until there are no more
3124 * than max_descriptors_per_router() remaining. Start by removing the oldest
3125 * members from before <b>cutoff</b>, then remove members which were current
3126 * for the lowest amount of time. The order of members of old_routers at
3127 * indices <b>lo</b> or higher may be changed.
3129 static void
3130 routerlist_remove_old_cached_routers_with_id(time_t now,
3131 time_t cutoff, int lo, int hi,
3132 digestset_t *retain)
3134 int i, n = hi-lo+1;
3135 unsigned n_extra, n_rmv = 0;
3136 struct duration_idx_t *lifespans;
3137 uint8_t *rmv, *must_keep;
3138 smartlist_t *lst = routerlist->old_routers;
3139 #if 1
3140 const char *ident;
3141 tor_assert(hi < smartlist_len(lst));
3142 tor_assert(lo <= hi);
3143 ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
3144 for (i = lo+1; i <= hi; ++i) {
3145 signed_descriptor_t *r = smartlist_get(lst, i);
3146 tor_assert(!memcmp(ident, r->identity_digest, DIGEST_LEN));
3148 #endif
3149 /* Check whether we need to do anything at all. */
3151 int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
3152 if (n <= mdpr)
3153 return;
3154 n_extra = n - mdpr;
3157 lifespans = tor_malloc_zero(sizeof(struct duration_idx_t)*n);
3158 rmv = tor_malloc_zero(sizeof(uint8_t)*n);
3159 must_keep = tor_malloc_zero(sizeof(uint8_t)*n);
3160 /* Set lifespans to contain the lifespan and index of each server. */
3161 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
3162 for (i = lo; i <= hi; ++i) {
3163 signed_descriptor_t *r = smartlist_get(lst, i);
3164 signed_descriptor_t *r_next;
3165 lifespans[i-lo].idx = i;
3166 if (r->last_listed_as_valid_until >= now ||
3167 (retain && digestset_isin(retain, r->signed_descriptor_digest))) {
3168 must_keep[i-lo] = 1;
3170 if (i < hi) {
3171 r_next = smartlist_get(lst, i+1);
3172 tor_assert(r->published_on <= r_next->published_on);
3173 lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
3174 } else {
3175 r_next = NULL;
3176 lifespans[i-lo].duration = INT_MAX;
3178 if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
3179 ++n_rmv;
3180 lifespans[i-lo].old = 1;
3181 rmv[i-lo] = 1;
3185 if (n_rmv < n_extra) {
3187 * We aren't removing enough servers for being old. Sort lifespans by
3188 * the duration of liveness, and remove the ones we're not already going to
3189 * remove based on how long they were alive.
3191 qsort(lifespans, n, sizeof(struct duration_idx_t), _compare_duration_idx);
3192 for (i = 0; i < n && n_rmv < n_extra; ++i) {
3193 if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
3194 rmv[lifespans[i].idx-lo] = 1;
3195 ++n_rmv;
3200 i = hi;
3201 do {
3202 if (rmv[i-lo])
3203 routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
3204 } while (--i >= lo);
3205 tor_free(must_keep);
3206 tor_free(rmv);
3207 tor_free(lifespans);
3210 /** Deactivate any routers from the routerlist that are more than
3211 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
3212 * remove old routers from the list of cached routers if we have too many.
3214 void
3215 routerlist_remove_old_routers(void)
3217 int i, hi=-1;
3218 const char *cur_id = NULL;
3219 time_t now = time(NULL);
3220 time_t cutoff;
3221 routerinfo_t *router;
3222 signed_descriptor_t *sd;
3223 digestset_t *retain;
3224 int caches = directory_caches_dir_info(get_options());
3225 const networkstatus_t *consensus = networkstatus_get_latest_consensus();
3226 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3227 int n_expected_retain = 0;
3229 trusted_dirs_remove_old_certs();
3231 if (!routerlist || !consensus)
3232 return;
3234 // routerlist_assert_ok(routerlist);
3236 n_expected_retain = smartlist_len(consensus->routerstatus_list);
3237 if (caches &&
3238 networkstatus_v2_list && smartlist_len(networkstatus_v2_list)) {
3239 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3240 n_expected_retain += smartlist_len(ns->entries));
3241 /* DOCDOC XXX021 too much magic. */
3242 n_expected_retain /= (smartlist_len(networkstatus_v2_list)/2+1);
3244 //log_notice(LD_DIR,"n_expected_retain=%d",n_expected_retain);
3246 retain = digestset_new(n_expected_retain);
3248 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
3249 /* Build a list of all the descriptors that _anybody_ lists. */
3250 if (caches) {
3251 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3253 /* XXXX The inner loop here gets pretty expensive, and actually shows up
3254 * on some profiles. It may be the reason digestmap_set shows up in
3255 * profiles too. If instead we kept a per-descriptor digest count of
3256 * how many networkstatuses recommended each descriptor, and changed
3257 * that only when the networkstatuses changed, that would be a speed
3258 * improvement, possibly 1-4% if it also removes digestmap_set from the
3259 * profile. Not worth it for 0.1.2.x, though. The new directory
3260 * system will obsolete this whole thing in 0.2.0.x. */
3261 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
3262 if (rs->published_on >= cutoff)
3263 digestset_add(retain, rs->descriptor_digest));
3267 /* Retain anything listed in the consensus. */
3268 if (consensus) {
3269 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
3270 if (rs->published_on >= cutoff)
3271 digestset_add(retain, rs->descriptor_digest));
3274 /* If we have nearly as many networkstatuses as we want, we should consider
3275 * pruning current routers that are too old and that nobody recommends. (If
3276 * we don't have enough networkstatuses, then we should get more before we
3277 * decide to kill routers.) */
3278 /* XXX021 we don't check if we have a v3 consensus, do we? should we? -RD */
3279 if (!caches ||
3280 smartlist_len(networkstatus_v2_list) > get_n_v2_authorities() / 2) {
3281 cutoff = now - ROUTER_MAX_AGE;
3282 /* Remove too-old unrecommended members of routerlist->routers. */
3283 for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
3284 router = smartlist_get(routerlist->routers, i);
3285 if (router->cache_info.published_on <= cutoff &&
3286 router->cache_info.last_listed_as_valid_until < now &&
3287 !digestset_isin(retain,
3288 router->cache_info.signed_descriptor_digest)) {
3289 /* Too old: remove it. (If we're a cache, just move it into
3290 * old_routers.) */
3291 log_info(LD_DIR,
3292 "Forgetting obsolete (too old) routerinfo for router '%s'",
3293 router->nickname);
3294 routerlist_remove(routerlist, router, 1);
3295 i--;
3300 //routerlist_assert_ok(routerlist);
3302 /* Remove far-too-old members of routerlist->old_routers. */
3303 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
3304 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
3305 sd = smartlist_get(routerlist->old_routers, i);
3306 if (sd->published_on <= cutoff &&
3307 sd->last_listed_as_valid_until < now &&
3308 !digestset_isin(retain, sd->signed_descriptor_digest)) {
3309 /* Too old. Remove it. */
3310 routerlist_remove_old(routerlist, sd, i--);
3314 //routerlist_assert_ok(routerlist);
3316 log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
3317 smartlist_len(routerlist->routers),
3318 smartlist_len(routerlist->old_routers));
3320 /* Now we might have to look at routerlist->old_routers for extraneous
3321 * members. (We'd keep all the members if we could, but we need to save
3322 * space.) First, check whether we have too many router descriptors, total.
3323 * We're okay with having too many for some given router, so long as the
3324 * total number doesn't approach max_descriptors_per_router()*len(router).
3326 if (smartlist_len(routerlist->old_routers) <
3327 smartlist_len(routerlist->routers))
3328 goto done;
3330 /* Sort by identity, then fix indices. */
3331 smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
3332 /* Fix indices. */
3333 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
3334 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
3335 r->routerlist_index = i;
3338 /* Iterate through the list from back to front, so when we remove descriptors
3339 * we don't mess up groups we haven't gotten to. */
3340 for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
3341 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
3342 if (!cur_id) {
3343 cur_id = r->identity_digest;
3344 hi = i;
3346 if (memcmp(cur_id, r->identity_digest, DIGEST_LEN)) {
3347 routerlist_remove_old_cached_routers_with_id(now,
3348 cutoff, i+1, hi, retain);
3349 cur_id = r->identity_digest;
3350 hi = i;
3353 if (hi>=0)
3354 routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
3355 //routerlist_assert_ok(routerlist);
3357 done:
3358 digestset_free(retain);
3359 router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
3360 router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
3363 /** We just added a new set of descriptors. Take whatever extra steps
3364 * we need. */
3365 static void
3366 routerlist_descriptors_added(smartlist_t *sl, int from_cache)
3368 tor_assert(sl);
3369 control_event_descriptors_changed(sl);
3370 SMARTLIST_FOREACH(sl, routerinfo_t *, ri,
3371 if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
3372 learned_bridge_descriptor(ri, from_cache);
3377 * Code to parse a single router descriptor and insert it into the
3378 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
3379 * descriptor was well-formed but could not be added; and 1 if the
3380 * descriptor was added.
3382 * If we don't add it and <b>msg</b> is not NULL, then assign to
3383 * *<b>msg</b> a static string describing the reason for refusing the
3384 * descriptor.
3386 * This is used only by the controller.
3389 router_load_single_router(const char *s, uint8_t purpose, int cache,
3390 const char **msg)
3392 routerinfo_t *ri;
3393 was_router_added_t r;
3394 smartlist_t *lst;
3395 char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
3396 tor_assert(msg);
3397 *msg = NULL;
3399 tor_snprintf(annotation_buf, sizeof(annotation_buf),
3400 "@source controller\n"
3401 "@purpose %s\n", router_purpose_to_string(purpose));
3403 if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0, annotation_buf))) {
3404 log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
3405 *msg = "Couldn't parse router descriptor.";
3406 return -1;
3408 tor_assert(ri->purpose == purpose);
3409 if (router_is_me(ri)) {
3410 log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
3411 *msg = "Router's identity key matches mine.";
3412 routerinfo_free(ri);
3413 return 0;
3416 if (!cache) /* obey the preference of the controller */
3417 ri->cache_info.do_not_cache = 1;
3419 lst = smartlist_create();
3420 smartlist_add(lst, ri);
3421 routers_update_status_from_consensus_networkstatus(lst, 0);
3423 r = router_add_to_routerlist(ri, msg, 0, 0);
3424 if (!WRA_WAS_ADDED(r)) {
3425 /* we've already assigned to *msg now, and ri is already freed */
3426 tor_assert(*msg);
3427 if (r == ROUTER_AUTHDIR_REJECTS)
3428 log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
3429 smartlist_free(lst);
3430 return 0;
3431 } else {
3432 routerlist_descriptors_added(lst, 0);
3433 smartlist_free(lst);
3434 log_debug(LD_DIR, "Added router to list");
3435 return 1;
3439 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
3440 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
3441 * are in response to a query to the network: cache them by adding them to
3442 * the journal.
3444 * Return the number of routers actually added.
3446 * If <b>requested_fingerprints</b> is provided, it must contain a list of
3447 * uppercased fingerprints. Do not update any router whose
3448 * fingerprint is not on the list; after updating a router, remove its
3449 * fingerprint from the list.
3451 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
3452 * are descriptor digests. Otherwise they are identity digests.
3455 router_load_routers_from_string(const char *s, const char *eos,
3456 saved_location_t saved_location,
3457 smartlist_t *requested_fingerprints,
3458 int descriptor_digests,
3459 const char *prepend_annotations)
3461 smartlist_t *routers = smartlist_create(), *changed = smartlist_create();
3462 char fp[HEX_DIGEST_LEN+1];
3463 const char *msg;
3464 int from_cache = (saved_location != SAVED_NOWHERE);
3465 int allow_annotations = (saved_location != SAVED_NOWHERE);
3466 int any_changed = 0;
3468 router_parse_list_from_string(&s, eos, routers, saved_location, 0,
3469 allow_annotations, prepend_annotations);
3471 routers_update_status_from_consensus_networkstatus(routers, !from_cache);
3473 log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
3475 SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
3476 was_router_added_t r;
3477 if (requested_fingerprints) {
3478 base16_encode(fp, sizeof(fp), descriptor_digests ?
3479 ri->cache_info.signed_descriptor_digest :
3480 ri->cache_info.identity_digest,
3481 DIGEST_LEN);
3482 if (smartlist_string_isin(requested_fingerprints, fp)) {
3483 smartlist_string_remove(requested_fingerprints, fp);
3484 } else {
3485 char *requested =
3486 smartlist_join_strings(requested_fingerprints," ",0,NULL);
3487 log_warn(LD_DIR,
3488 "We received a router descriptor with a fingerprint (%s) "
3489 "that we never requested. (We asked for: %s.) Dropping.",
3490 fp, requested);
3491 tor_free(requested);
3492 routerinfo_free(ri);
3493 continue;
3497 r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
3498 if (WRA_WAS_ADDED(r)) {
3499 any_changed++;
3500 smartlist_add(changed, ri);
3501 routerlist_descriptors_added(changed, from_cache);
3502 smartlist_clear(changed);
3503 } else if (WRA_WAS_REJECTED(r)) {
3504 download_status_t *dl_status;
3505 dl_status = router_get_dl_status_by_descriptor_digest(
3506 ri->cache_info.signed_descriptor_digest);
3507 if (dl_status) {
3508 log_info(LD_GENERAL, "Marking router %s as never downloadable",
3509 ri->cache_info.signed_descriptor_digest);
3510 download_status_mark_impossible(dl_status);
3513 } SMARTLIST_FOREACH_END(ri);
3515 routerlist_assert_ok(routerlist);
3517 if (any_changed)
3518 router_rebuild_store(0, &routerlist->desc_store);
3520 smartlist_free(routers);
3521 smartlist_free(changed);
3523 return any_changed;
3526 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
3527 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
3528 * router_load_routers_from_string(). */
3529 void
3530 router_load_extrainfo_from_string(const char *s, const char *eos,
3531 saved_location_t saved_location,
3532 smartlist_t *requested_fingerprints,
3533 int descriptor_digests)
3535 smartlist_t *extrainfo_list = smartlist_create();
3536 const char *msg;
3537 int from_cache = (saved_location != SAVED_NOWHERE);
3539 router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
3540 NULL);
3542 log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
3544 SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
3545 was_router_added_t added =
3546 router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
3547 if (WRA_WAS_ADDED(added) && requested_fingerprints) {
3548 char fp[HEX_DIGEST_LEN+1];
3549 base16_encode(fp, sizeof(fp), descriptor_digests ?
3550 ei->cache_info.signed_descriptor_digest :
3551 ei->cache_info.identity_digest,
3552 DIGEST_LEN);
3553 smartlist_string_remove(requested_fingerprints, fp);
3554 /* We silently let people stuff us with extrainfos we didn't ask for,
3555 * so long as we would have wanted them anyway. Since we always fetch
3556 * all the extrainfos we want, and we never actually act on them
3557 * inside Tor, this should be harmless. */
3561 routerlist_assert_ok(routerlist);
3562 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
3564 smartlist_free(extrainfo_list);
3567 /** Return true iff any networkstatus includes a descriptor whose digest
3568 * is that of <b>desc</b>. */
3569 static int
3570 signed_desc_digest_is_recognized(signed_descriptor_t *desc)
3572 routerstatus_t *rs;
3573 networkstatus_t *consensus = networkstatus_get_latest_consensus();
3574 int caches = directory_caches_dir_info(get_options());
3575 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3577 if (consensus) {
3578 rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
3579 if (rs && !memcmp(rs->descriptor_digest,
3580 desc->signed_descriptor_digest, DIGEST_LEN))
3581 return 1;
3583 if (caches && networkstatus_v2_list) {
3584 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3586 if (!(rs = networkstatus_v2_find_entry(ns, desc->identity_digest)))
3587 continue;
3588 if (!memcmp(rs->descriptor_digest,
3589 desc->signed_descriptor_digest, DIGEST_LEN))
3590 return 1;
3593 return 0;
3596 /** Clear all our timeouts for fetching v2 and v3 directory stuff, and then
3597 * give it all a try again. */
3598 void
3599 routerlist_retry_directory_downloads(time_t now)
3601 router_reset_status_download_failures();
3602 router_reset_descriptor_download_failures();
3603 update_networkstatus_downloads(now);
3604 update_router_descriptor_downloads(now);
3607 /** Return 1 if all running sufficiently-stable routers will reject
3608 * addr:port, return 0 if any might accept it. */
3610 router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
3611 int need_uptime)
3613 addr_policy_result_t r;
3614 if (!routerlist) return 1;
3616 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
3618 if (router->is_running &&
3619 !router_is_unreliable(router, need_uptime, 0, 0)) {
3620 r = compare_addr_to_addr_policy(addr, port, router->exit_policy);
3621 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
3622 return 0; /* this one could be ok. good enough. */
3625 return 1; /* all will reject. */
3628 /** Return true iff <b>router</b> does not permit exit streams.
3631 router_exit_policy_rejects_all(routerinfo_t *router)
3633 return compare_addr_to_addr_policy(0, 0, router->exit_policy)
3634 == ADDR_POLICY_REJECTED;
3637 /** Add to the list of authorized directory servers one at
3638 * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
3639 * <b>address</b> is NULL, add ourself. Return 0 if success, -1 if
3640 * we couldn't add it. */
3641 trusted_dir_server_t *
3642 add_trusted_dir_server(const char *nickname, const char *address,
3643 uint16_t dir_port, uint16_t or_port,
3644 const char *digest, const char *v3_auth_digest,
3645 authority_type_t type)
3647 trusted_dir_server_t *ent;
3648 uint32_t a;
3649 char *hostname = NULL;
3650 size_t dlen;
3651 if (!trusted_dir_servers)
3652 trusted_dir_servers = smartlist_create();
3654 if (!address) { /* The address is us; we should guess. */
3655 if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
3656 log_warn(LD_CONFIG,
3657 "Couldn't find a suitable address when adding ourself as a "
3658 "trusted directory server.");
3659 return NULL;
3661 } else {
3662 if (tor_lookup_hostname(address, &a)) {
3663 log_warn(LD_CONFIG,
3664 "Unable to lookup address for directory server at '%s'",
3665 address);
3666 return NULL;
3668 hostname = tor_strdup(address);
3671 ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
3672 ent->nickname = nickname ? tor_strdup(nickname) : NULL;
3673 ent->address = hostname;
3674 ent->addr = a;
3675 ent->dir_port = dir_port;
3676 ent->or_port = or_port;
3677 ent->is_running = 1;
3678 ent->type = type;
3679 memcpy(ent->digest, digest, DIGEST_LEN);
3680 if (v3_auth_digest && (type & V3_AUTHORITY))
3681 memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
3683 dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
3684 ent->description = tor_malloc(dlen);
3685 if (nickname)
3686 tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
3687 nickname, hostname, (int)dir_port);
3688 else
3689 tor_snprintf(ent->description, dlen, "directory server at %s:%d",
3690 hostname, (int)dir_port);
3692 ent->fake_status.addr = ent->addr;
3693 memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
3694 if (nickname)
3695 strlcpy(ent->fake_status.nickname, nickname,
3696 sizeof(ent->fake_status.nickname));
3697 else
3698 ent->fake_status.nickname[0] = '\0';
3699 ent->fake_status.dir_port = ent->dir_port;
3700 ent->fake_status.or_port = ent->or_port;
3702 if (ent->or_port)
3703 ent->fake_status.version_supports_begindir = 1;
3704 /* XX021 - wait until authorities are upgraded */
3705 #if 0
3706 ent->fake_status.version_supports_conditional_consensus = 1;
3707 #else
3708 ent->fake_status.version_supports_conditional_consensus = 0;
3709 #endif
3711 smartlist_add(trusted_dir_servers, ent);
3712 router_dir_info_changed();
3713 return ent;
3716 /** Free storage held in <b>cert</b>. */
3717 void
3718 authority_cert_free(authority_cert_t *cert)
3720 if (!cert)
3721 return;
3723 tor_free(cert->cache_info.signed_descriptor_body);
3724 if (cert->signing_key)
3725 crypto_free_pk_env(cert->signing_key);
3726 if (cert->identity_key)
3727 crypto_free_pk_env(cert->identity_key);
3729 tor_free(cert);
3732 /** Free storage held in <b>ds</b>. */
3733 static void
3734 trusted_dir_server_free(trusted_dir_server_t *ds)
3736 tor_free(ds->nickname);
3737 tor_free(ds->description);
3738 tor_free(ds->address);
3739 tor_free(ds);
3742 /** Remove all members from the list of trusted dir servers. */
3743 void
3744 clear_trusted_dir_servers(void)
3746 if (trusted_dir_servers) {
3747 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
3748 trusted_dir_server_free(ent));
3749 smartlist_clear(trusted_dir_servers);
3750 } else {
3751 trusted_dir_servers = smartlist_create();
3753 router_dir_info_changed();
3756 /** Return 1 if any trusted dir server supports v1 directories,
3757 * else return 0. */
3759 any_trusted_dir_is_v1_authority(void)
3761 if (trusted_dir_servers)
3762 return get_n_authorities(V1_AUTHORITY) > 0;
3764 return 0;
3767 /** For every current directory connection whose purpose is <b>purpose</b>,
3768 * and where the resource being downloaded begins with <b>prefix</b>, split
3769 * rest of the resource into base16 fingerprints, decode them, and set the
3770 * corresponding elements of <b>result</b> to a nonzero value. */
3771 static void
3772 list_pending_downloads(digestmap_t *result,
3773 int purpose, const char *prefix)
3775 const size_t p_len = strlen(prefix);
3776 smartlist_t *tmp = smartlist_create();
3777 smartlist_t *conns = get_connection_array();
3779 tor_assert(result);
3781 SMARTLIST_FOREACH(conns, connection_t *, conn,
3783 if (conn->type == CONN_TYPE_DIR &&
3784 conn->purpose == purpose &&
3785 !conn->marked_for_close) {
3786 const char *resource = TO_DIR_CONN(conn)->requested_resource;
3787 if (!strcmpstart(resource, prefix))
3788 dir_split_resource_into_fingerprints(resource + p_len,
3789 tmp, NULL, 1, 0);
3792 SMARTLIST_FOREACH(tmp, char *, d,
3794 digestmap_set(result, d, (void*)1);
3795 tor_free(d);
3797 smartlist_free(tmp);
3800 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
3801 * true) we are currently downloading by descriptor digest, set result[d] to
3802 * (void*)1. */
3803 static void
3804 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
3806 int purpose =
3807 extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC;
3808 list_pending_downloads(result, purpose, "d/");
3811 /** Launch downloads for all the descriptors whose digests are listed
3812 * as digests[i] for lo <= i < hi. (Lo and hi may be out of range.)
3813 * If <b>source</b> is given, download from <b>source</b>; otherwise,
3814 * download from an appropriate random directory server.
3816 static void
3817 initiate_descriptor_downloads(routerstatus_t *source,
3818 int purpose,
3819 smartlist_t *digests,
3820 int lo, int hi, int pds_flags)
3822 int i, n = hi-lo;
3823 char *resource, *cp;
3824 size_t r_len;
3825 if (n <= 0)
3826 return;
3827 if (lo < 0)
3828 lo = 0;
3829 if (hi > smartlist_len(digests))
3830 hi = smartlist_len(digests);
3832 r_len = 8 + (HEX_DIGEST_LEN+1)*n;
3833 cp = resource = tor_malloc(r_len);
3834 memcpy(cp, "d/", 2);
3835 cp += 2;
3836 for (i = lo; i < hi; ++i) {
3837 base16_encode(cp, r_len-(cp-resource),
3838 smartlist_get(digests,i), DIGEST_LEN);
3839 cp += HEX_DIGEST_LEN;
3840 *cp++ = '+';
3842 memcpy(cp-1, ".z", 3);
3844 if (source) {
3845 /* We know which authority we want. */
3846 directory_initiate_command_routerstatus(source, purpose,
3847 ROUTER_PURPOSE_GENERAL,
3848 0, /* not private */
3849 resource, NULL, 0, 0);
3850 } else {
3851 directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource,
3852 pds_flags);
3854 tor_free(resource);
3857 /** Return 0 if this routerstatus is obsolete, too new, isn't
3858 * running, or otherwise not a descriptor that we would make any
3859 * use of even if we had it. Else return 1. */
3860 static INLINE int
3861 client_would_use_router(routerstatus_t *rs, time_t now, or_options_t *options)
3863 if (!rs->is_running && !options->FetchUselessDescriptors) {
3864 /* If we had this router descriptor, we wouldn't even bother using it.
3865 * But, if we want to have a complete list, fetch it anyway. */
3866 return 0;
3868 if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
3869 > now) {
3870 /* Most caches probably don't have this descriptor yet. */
3871 return 0;
3873 if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
3874 /* We'd drop it immediately for being too old. */
3875 return 0;
3877 return 1;
3880 /** Max amount of hashes to download per request.
3881 * Since squid does not like URLs >= 4096 bytes we limit it to 96.
3882 * 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
3883 * 4058/41 (40 for the hash and 1 for the + that separates them) => 98
3884 * So use 96 because it's a nice number.
3886 #define MAX_DL_PER_REQUEST 96
3887 /** Don't split our requests so finely that we are requesting fewer than
3888 * this number per server. */
3889 #define MIN_DL_PER_REQUEST 4
3890 /** To prevent a single screwy cache from confusing us by selective reply,
3891 * try to split our requests into at least this this many requests. */
3892 #define MIN_REQUESTS 3
3893 /** If we want fewer than this many descriptors, wait until we
3894 * want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
3895 * passed. */
3896 #define MAX_DL_TO_DELAY 16
3897 /** When directory clients have only a few servers to request, they batch
3898 * them until they have more, or until this amount of time has passed. */
3899 #define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
3901 /** Given a list of router descriptor digests in <b>downloadable</b>, decide
3902 * whether to delay fetching until we have more. If we don't want to delay,
3903 * launch one or more requests to the appropriate directory authorities. */
3904 static void
3905 launch_router_descriptor_downloads(smartlist_t *downloadable, time_t now)
3907 int should_delay = 0, n_downloadable;
3908 or_options_t *options = get_options();
3910 n_downloadable = smartlist_len(downloadable);
3911 if (!directory_fetches_dir_info_early(options)) {
3912 if (n_downloadable >= MAX_DL_TO_DELAY) {
3913 log_debug(LD_DIR,
3914 "There are enough downloadable routerdescs to launch requests.");
3915 should_delay = 0;
3916 } else {
3917 should_delay = (last_routerdesc_download_attempted +
3918 MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
3919 if (!should_delay && n_downloadable) {
3920 if (last_routerdesc_download_attempted) {
3921 log_info(LD_DIR,
3922 "There are not many downloadable routerdescs, but we've "
3923 "been waiting long enough (%d seconds). Downloading.",
3924 (int)(now-last_routerdesc_download_attempted));
3925 } else {
3926 log_info(LD_DIR,
3927 "There are not many downloadable routerdescs, but we haven't "
3928 "tried downloading descriptors recently. Downloading.");
3933 /* XXX should we consider having even the dir mirrors delay
3934 * a little bit, so we don't load the authorities as much? -RD
3935 * I don't think so. If we do, clients that want those descriptors may
3936 * not actually find them if the caches haven't got them yet. -NM
3939 if (! should_delay && n_downloadable) {
3940 int i, n_per_request;
3941 const char *req_plural = "", *rtr_plural = "";
3942 int pds_flags = PDS_RETRY_IF_NO_SERVERS;
3943 if (! authdir_mode_any_nonhidserv(options)) {
3944 /* If we wind up going to the authorities, we want to only open one
3945 * connection to each authority at a time, so that we don't overload
3946 * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
3947 * regardless of whether we're a cache or not; it gets ignored if we're
3948 * not calling router_pick_trusteddirserver.
3950 * Setting this flag can make initiate_descriptor_downloads() ignore
3951 * requests. We need to make sure that we do in fact call
3952 * update_router_descriptor_downloads() later on, once the connections
3953 * have succeeded or failed.
3955 pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH;
3958 n_per_request = (n_downloadable+MIN_REQUESTS-1) / MIN_REQUESTS;
3959 if (n_per_request > MAX_DL_PER_REQUEST)
3960 n_per_request = MAX_DL_PER_REQUEST;
3961 if (n_per_request < MIN_DL_PER_REQUEST)
3962 n_per_request = MIN_DL_PER_REQUEST;
3964 if (n_downloadable > n_per_request)
3965 req_plural = rtr_plural = "s";
3966 else if (n_downloadable > 1)
3967 rtr_plural = "s";
3969 log_info(LD_DIR,
3970 "Launching %d request%s for %d router%s, %d at a time",
3971 (n_downloadable+n_per_request-1)/n_per_request,
3972 req_plural, n_downloadable, rtr_plural, n_per_request);
3973 smartlist_sort_digests(downloadable);
3974 for (i=0; i < n_downloadable; i += n_per_request) {
3975 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_SERVERDESC,
3976 downloadable, i, i+n_per_request,
3977 pds_flags);
3979 last_routerdesc_download_attempted = now;
3983 /** Launch downloads for router status as needed, using the strategy used by
3984 * authorities and caches: based on the v2 networkstatuses we have, download
3985 * every descriptor we don't have but would serve, from a random authority
3986 * that lists it. */
3987 static void
3988 update_router_descriptor_cache_downloads_v2(time_t now)
3990 smartlist_t **downloadable; /* For each authority, what can we dl from it? */
3991 smartlist_t **download_from; /* ... and, what will we dl from it? */
3992 digestmap_t *map; /* Which descs are in progress, or assigned? */
3993 int i, j, n;
3994 int n_download;
3995 or_options_t *options = get_options();
3996 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3998 if (! directory_fetches_dir_info_early(options)) {
3999 log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads_v2() "
4000 "on a non-dir-mirror?");
4003 if (!networkstatus_v2_list || !smartlist_len(networkstatus_v2_list))
4004 return;
4006 map = digestmap_new();
4007 n = smartlist_len(networkstatus_v2_list);
4009 downloadable = tor_malloc_zero(sizeof(smartlist_t*) * n);
4010 download_from = tor_malloc_zero(sizeof(smartlist_t*) * n);
4012 /* Set map[d]=1 for the digest of every descriptor that we are currently
4013 * downloading. */
4014 list_pending_descriptor_downloads(map, 0);
4016 /* For the digest of every descriptor that we don't have, and that we aren't
4017 * downloading, add d to downloadable[i] if the i'th networkstatus knows
4018 * about that descriptor, and we haven't already failed to get that
4019 * descriptor from the corresponding authority.
4021 n_download = 0;
4022 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
4024 trusted_dir_server_t *ds;
4025 smartlist_t *dl;
4026 dl = downloadable[ns_sl_idx] = smartlist_create();
4027 download_from[ns_sl_idx] = smartlist_create();
4028 if (ns->published_on + MAX_NETWORKSTATUS_AGE+10*60 < now) {
4029 /* Don't download if the networkstatus is almost ancient. */
4030 /* Actually, I suspect what's happening here is that we ask
4031 * for the descriptor when we have a given networkstatus,
4032 * and then we get a newer networkstatus, and then we receive
4033 * the descriptor. Having a networkstatus actually expire is
4034 * probably a rare event, and we'll probably be happiest if
4035 * we take this clause out. -RD */
4036 continue;
4039 /* Don't try dirservers that we think are down -- we might have
4040 * just tried them and just marked them as down. */
4041 ds = router_get_trusteddirserver_by_digest(ns->identity_digest);
4042 if (ds && !ds->is_running)
4043 continue;
4045 SMARTLIST_FOREACH(ns->entries, routerstatus_t * , rs,
4047 if (!rs->need_to_mirror)
4048 continue;
4049 if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
4050 log_warn(LD_BUG,
4051 "We have a router descriptor, but need_to_mirror=1.");
4052 rs->need_to_mirror = 0;
4053 continue;
4055 if (authdir_mode(options) && dirserv_would_reject_router(rs)) {
4056 rs->need_to_mirror = 0;
4057 continue;
4059 if (digestmap_get(map, rs->descriptor_digest)) {
4060 /* We're downloading it already. */
4061 continue;
4062 } else {
4063 /* We could download it from this guy. */
4064 smartlist_add(dl, rs->descriptor_digest);
4065 ++n_download;
4070 /* At random, assign descriptors to authorities such that:
4071 * - if d is a member of some downloadable[x], d is a member of some
4072 * download_from[y]. (Everything we want to download, we try to download
4073 * from somebody.)
4074 * - If d is a member of download_from[y], d is a member of downloadable[y].
4075 * (We only try to download descriptors from authorities who claim to have
4076 * them.)
4077 * - No d is a member of download_from[x] and download_from[y] s.t. x != y.
4078 * (We don't try to download anything from two authorities concurrently.)
4080 while (n_download) {
4081 int which_ns = crypto_rand_int(n);
4082 smartlist_t *dl = downloadable[which_ns];
4083 int idx;
4084 char *d;
4085 if (!smartlist_len(dl))
4086 continue;
4087 idx = crypto_rand_int(smartlist_len(dl));
4088 d = smartlist_get(dl, idx);
4089 if (! digestmap_get(map, d)) {
4090 smartlist_add(download_from[which_ns], d);
4091 digestmap_set(map, d, (void*) 1);
4093 smartlist_del(dl, idx);
4094 --n_download;
4097 /* Now, we can actually launch our requests. */
4098 for (i=0; i<n; ++i) {
4099 networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
4100 trusted_dir_server_t *ds =
4101 router_get_trusteddirserver_by_digest(ns->identity_digest);
4102 smartlist_t *dl = download_from[i];
4103 int pds_flags = PDS_RETRY_IF_NO_SERVERS;
4104 if (! authdir_mode_any_nonhidserv(options))
4105 pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH; /* XXXX ignored*/
4107 if (!ds) {
4108 log_warn(LD_BUG, "Networkstatus with no corresponding authority!");
4109 continue;
4111 if (! smartlist_len(dl))
4112 continue;
4113 log_info(LD_DIR, "Requesting %d descriptors from authority \"%s\"",
4114 smartlist_len(dl), ds->nickname);
4115 for (j=0; j < smartlist_len(dl); j += MAX_DL_PER_REQUEST) {
4116 initiate_descriptor_downloads(&(ds->fake_status),
4117 DIR_PURPOSE_FETCH_SERVERDESC, dl, j,
4118 j+MAX_DL_PER_REQUEST, pds_flags);
4122 for (i=0; i<n; ++i) {
4123 smartlist_free(download_from[i]);
4124 smartlist_free(downloadable[i]);
4126 tor_free(download_from);
4127 tor_free(downloadable);
4128 digestmap_free(map,NULL);
4131 /** For any descriptor that we want that's currently listed in the live
4132 * consensus, download it as appropriate. */
4133 static void
4134 update_consensus_router_descriptor_downloads(time_t now)
4136 or_options_t *options = get_options();
4137 digestmap_t *map = NULL;
4138 smartlist_t *no_longer_old = smartlist_create();
4139 smartlist_t *downloadable = smartlist_create();
4140 int authdir = authdir_mode(options);
4141 networkstatus_t *consensus =
4142 networkstatus_get_reasonably_live_consensus(now);
4143 int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
4144 n_inprogress=0, n_in_oldrouters=0;
4146 if (directory_too_idle_to_fetch_descriptors(options, now))
4147 goto done;
4148 if (!consensus)
4149 goto done;
4151 map = digestmap_new();
4152 list_pending_descriptor_downloads(map, 0);
4153 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
4155 signed_descriptor_t *sd;
4156 if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) {
4157 routerinfo_t *ri;
4158 ++n_have;
4159 if (!(ri = router_get_by_digest(rs->identity_digest)) ||
4160 memcmp(ri->cache_info.signed_descriptor_digest,
4161 sd->signed_descriptor_digest, DIGEST_LEN)) {
4162 /* We have a descriptor with this digest, but either there is no
4163 * entry in routerlist with the same ID (!ri), or there is one,
4164 * but the identity digest differs (memcmp).
4166 smartlist_add(no_longer_old, sd);
4167 ++n_in_oldrouters; /* We have it in old_routers. */
4169 continue; /* We have it already. */
4171 if (digestmap_get(map, rs->descriptor_digest)) {
4172 ++n_inprogress;
4173 continue; /* We have an in-progress download. */
4175 if (!download_status_is_ready(&rs->dl_status, now,
4176 MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
4177 ++n_delayed; /* Not ready for retry. */
4178 continue;
4180 if (authdir && dirserv_would_reject_router(rs)) {
4181 ++n_would_reject;
4182 continue; /* We would throw it out immediately. */
4184 if (!directory_caches_dir_info(options) &&
4185 !client_would_use_router(rs, now, options)) {
4186 ++n_wouldnt_use;
4187 continue; /* We would never use it ourself. */
4189 smartlist_add(downloadable, rs->descriptor_digest);
4192 if (!authdir_mode_handles_descs(options, ROUTER_PURPOSE_GENERAL)
4193 && smartlist_len(no_longer_old)) {
4194 routerlist_t *rl = router_get_routerlist();
4195 log_info(LD_DIR, "%d router descriptors listed in consensus are "
4196 "currently in old_routers; making them current.",
4197 smartlist_len(no_longer_old));
4198 SMARTLIST_FOREACH(no_longer_old, signed_descriptor_t *, sd, {
4199 const char *msg;
4200 was_router_added_t r;
4201 routerinfo_t *ri = routerlist_reparse_old(rl, sd);
4202 if (!ri) {
4203 log_warn(LD_BUG, "Failed to re-parse a router.");
4204 continue;
4206 r = router_add_to_routerlist(ri, &msg, 1, 0);
4207 if (WRA_WAS_OUTDATED(r)) {
4208 log_warn(LD_DIR, "Couldn't add re-parsed router: %s",
4209 msg?msg:"???");
4212 routerlist_assert_ok(rl);
4215 log_info(LD_DIR,
4216 "%d router descriptors downloadable. %d delayed; %d present "
4217 "(%d of those were in old_routers); %d would_reject; "
4218 "%d wouldnt_use; %d in progress.",
4219 smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
4220 n_would_reject, n_wouldnt_use, n_inprogress);
4222 launch_router_descriptor_downloads(downloadable, now);
4224 digestmap_free(map, NULL);
4225 done:
4226 smartlist_free(downloadable);
4227 smartlist_free(no_longer_old);
4230 /** How often should we launch a server/authority request to be sure of getting
4231 * a guess for our IP? */
4232 /*XXXX021 this info should come from netinfo cells or something, or we should
4233 * do this only when we aren't seeing incoming data. see bug 652. */
4234 #define DUMMY_DOWNLOAD_INTERVAL (20*60)
4236 /** Launch downloads for router status as needed. */
4237 void
4238 update_router_descriptor_downloads(time_t now)
4240 or_options_t *options = get_options();
4241 static time_t last_dummy_download = 0;
4242 if (should_delay_dir_fetches(options))
4243 return;
4244 if (directory_fetches_dir_info_early(options)) {
4245 update_router_descriptor_cache_downloads_v2(now);
4247 update_consensus_router_descriptor_downloads(now);
4249 /* XXXX021 we could be smarter here; see notes on bug 652. */
4250 /* If we're a server that doesn't have a configured address, we rely on
4251 * directory fetches to learn when our address changes. So if we haven't
4252 * tried to get any routerdescs in a long time, try a dummy fetch now. */
4253 if (!options->Address &&
4254 server_mode(options) &&
4255 last_routerdesc_download_attempted + DUMMY_DOWNLOAD_INTERVAL < now &&
4256 last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
4257 last_dummy_download = now;
4258 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
4259 ROUTER_PURPOSE_GENERAL, "authority.z",
4260 PDS_RETRY_IF_NO_SERVERS);
4264 /** Launch extrainfo downloads as needed. */
4265 void
4266 update_extrainfo_downloads(time_t now)
4268 or_options_t *options = get_options();
4269 routerlist_t *rl;
4270 smartlist_t *wanted;
4271 digestmap_t *pending;
4272 int old_routers, i;
4273 int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0;
4274 if (! options->DownloadExtraInfo)
4275 return;
4276 if (should_delay_dir_fetches(options))
4277 return;
4278 if (!router_have_minimum_dir_info())
4279 return;
4281 pending = digestmap_new();
4282 list_pending_descriptor_downloads(pending, 1);
4283 rl = router_get_routerlist();
4284 wanted = smartlist_create();
4285 for (old_routers = 0; old_routers < 2; ++old_routers) {
4286 smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
4287 for (i = 0; i < smartlist_len(lst); ++i) {
4288 signed_descriptor_t *sd;
4289 char *d;
4290 if (old_routers)
4291 sd = smartlist_get(lst, i);
4292 else
4293 sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
4294 if (sd->is_extrainfo)
4295 continue; /* This should never happen. */
4296 if (old_routers && !router_get_by_digest(sd->identity_digest))
4297 continue; /* Couldn't check the signature if we got it. */
4298 if (sd->extrainfo_is_bogus)
4299 continue;
4300 d = sd->extra_info_digest;
4301 if (tor_digest_is_zero(d)) {
4302 ++n_no_ei;
4303 continue;
4305 if (eimap_get(rl->extra_info_map, d)) {
4306 ++n_have;
4307 continue;
4309 if (!download_status_is_ready(&sd->ei_dl_status, now,
4310 MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
4311 ++n_delay;
4312 continue;
4314 if (digestmap_get(pending, d)) {
4315 ++n_pending;
4316 continue;
4318 smartlist_add(wanted, d);
4321 digestmap_free(pending, NULL);
4323 log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
4324 "with present ei, %d delaying, %d pending, %d downloadable.",
4325 n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted));
4327 smartlist_shuffle(wanted);
4328 for (i = 0; i < smartlist_len(wanted); i += MAX_DL_PER_REQUEST) {
4329 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
4330 wanted, i, i + MAX_DL_PER_REQUEST,
4331 PDS_RETRY_IF_NO_SERVERS|PDS_NO_EXISTING_SERVERDESC_FETCH);
4334 smartlist_free(wanted);
4337 /** True iff, the last time we checked whether we had enough directory info
4338 * to build circuits, the answer was "yes". */
4339 static int have_min_dir_info = 0;
4340 /** True iff enough has changed since the last time we checked whether we had
4341 * enough directory info to build circuits that our old answer can no longer
4342 * be trusted. */
4343 static int need_to_update_have_min_dir_info = 1;
4344 /** String describing what we're missing before we have enough directory
4345 * info. */
4346 static char dir_info_status[128] = "";
4348 /** Return true iff we have enough networkstatus and router information to
4349 * start building circuits. Right now, this means "more than half the
4350 * networkstatus documents, and at least 1/4 of expected routers." */
4351 //XXX should consider whether we have enough exiting nodes here.
4353 router_have_minimum_dir_info(void)
4355 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
4356 update_router_have_minimum_dir_info();
4357 need_to_update_have_min_dir_info = 0;
4359 return have_min_dir_info;
4362 /** Called when our internal view of the directory has changed. This can be
4363 * when the authorities change, networkstatuses change, the list of routerdescs
4364 * changes, or number of running routers changes.
4366 void
4367 router_dir_info_changed(void)
4369 need_to_update_have_min_dir_info = 1;
4370 rend_hsdir_routers_changed();
4373 /** Return a string describing what we're missing before we have enough
4374 * directory info. */
4375 const char *
4376 get_dir_info_status_string(void)
4378 return dir_info_status;
4381 /* DOCDOC count_usable_descriptors */
4382 static void
4383 count_usable_descriptors(int *num_present, int *num_usable,
4384 const networkstatus_t *consensus,
4385 or_options_t *options, time_t now)
4387 *num_present = 0, *num_usable=0;
4389 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
4391 if (client_would_use_router(rs, now, options)) {
4392 ++*num_usable; /* the consensus says we want it. */
4393 if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
4394 /* we have the descriptor listed in the consensus. */
4395 ++*num_present;
4400 log_debug(LD_DIR, "%d usable, %d present.", *num_usable, *num_present);
4403 /** We just fetched a new set of descriptors. Compute how far through
4404 * the "loading descriptors" bootstrapping phase we are, so we can inform
4405 * the controller of our progress. */
4407 count_loading_descriptors_progress(void)
4409 int num_present = 0, num_usable=0;
4410 time_t now = time(NULL);
4411 const networkstatus_t *consensus =
4412 networkstatus_get_reasonably_live_consensus(now);
4413 double fraction;
4415 if (!consensus)
4416 return 0; /* can't count descriptors if we have no list of them */
4418 count_usable_descriptors(&num_present, &num_usable,
4419 consensus, get_options(), now);
4421 if (num_usable == 0)
4422 return 0; /* don't div by 0 */
4423 fraction = num_present / (num_usable/4.);
4424 if (fraction > 1.0)
4425 return 0; /* it's not the number of descriptors holding us back */
4426 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
4427 (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
4428 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
4431 /** Change the value of have_min_dir_info, setting it true iff we have enough
4432 * network and router information to build circuits. Clear the value of
4433 * need_to_update_have_min_dir_info. */
4434 static void
4435 update_router_have_minimum_dir_info(void)
4437 int num_present = 0, num_usable=0;
4438 time_t now = time(NULL);
4439 int res;
4440 or_options_t *options = get_options();
4441 const networkstatus_t *consensus =
4442 networkstatus_get_reasonably_live_consensus(now);
4444 if (!consensus) {
4445 if (!networkstatus_get_latest_consensus())
4446 strlcpy(dir_info_status, "We have no network-status consensus.",
4447 sizeof(dir_info_status));
4448 else
4449 strlcpy(dir_info_status, "We have no recent network-status consensus.",
4450 sizeof(dir_info_status));
4451 res = 0;
4452 goto done;
4455 if (should_delay_dir_fetches(get_options())) {
4456 log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
4457 strlcpy(dir_info_status, "No live bridge descriptors.",
4458 sizeof(dir_info_status));
4459 res = 0;
4460 goto done;
4463 count_usable_descriptors(&num_present, &num_usable, consensus, options, now);
4465 if (num_present < num_usable/4) {
4466 tor_snprintf(dir_info_status, sizeof(dir_info_status),
4467 "We have only %d/%d usable descriptors.", num_present, num_usable);
4468 res = 0;
4469 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
4470 } else if (num_present < 2) {
4471 tor_snprintf(dir_info_status, sizeof(dir_info_status),
4472 "Only %d descriptor%s here and believed reachable!",
4473 num_present, num_present ? "" : "s");
4474 res = 0;
4475 } else {
4476 res = 1;
4479 done:
4480 if (res && !have_min_dir_info) {
4481 log(LOG_NOTICE, LD_DIR,
4482 "We now have enough directory information to build circuits.");
4483 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
4484 control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
4486 if (!res && have_min_dir_info) {
4487 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
4488 log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
4489 "Our directory information is no longer up-to-date "
4490 "enough to build circuits: %s", dir_info_status);
4491 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
4493 have_min_dir_info = res;
4494 need_to_update_have_min_dir_info = 0;
4497 /** Reset the descriptor download failure count on all routers, so that we
4498 * can retry any long-failed routers immediately.
4500 void
4501 router_reset_descriptor_download_failures(void)
4503 networkstatus_reset_download_failures();
4504 last_routerdesc_download_attempted = 0;
4505 if (!routerlist)
4506 return;
4507 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
4509 download_status_reset(&ri->cache_info.ei_dl_status);
4511 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
4513 download_status_reset(&sd->ei_dl_status);
4517 /** Any changes in a router descriptor's publication time larger than this are
4518 * automatically non-cosmetic. */
4519 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
4521 /** We allow uptime to vary from how much it ought to be by this much. */
4522 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
4524 /** Return true iff the only differences between r1 and r2 are such that
4525 * would not cause a recent (post 0.1.1.6) dirserver to republish.
4528 router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2)
4530 time_t r1pub, r2pub;
4531 long time_difference;
4532 tor_assert(r1 && r2);
4534 /* r1 should be the one that was published first. */
4535 if (r1->cache_info.published_on > r2->cache_info.published_on) {
4536 routerinfo_t *ri_tmp = r2;
4537 r2 = r1;
4538 r1 = ri_tmp;
4541 /* If any key fields differ, they're different. */
4542 if (strcasecmp(r1->address, r2->address) ||
4543 strcasecmp(r1->nickname, r2->nickname) ||
4544 r1->or_port != r2->or_port ||
4545 r1->dir_port != r2->dir_port ||
4546 r1->purpose != r2->purpose ||
4547 crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
4548 crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
4549 strcasecmp(r1->platform, r2->platform) ||
4550 (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
4551 (!r1->contact_info && r2->contact_info) ||
4552 (r1->contact_info && r2->contact_info &&
4553 strcasecmp(r1->contact_info, r2->contact_info)) ||
4554 r1->is_hibernating != r2->is_hibernating ||
4555 r1->has_old_dnsworkers != r2->has_old_dnsworkers ||
4556 cmp_addr_policies(r1->exit_policy, r2->exit_policy))
4557 return 0;
4558 if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
4559 return 0;
4560 if (r1->declared_family && r2->declared_family) {
4561 int i, n;
4562 if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
4563 return 0;
4564 n = smartlist_len(r1->declared_family);
4565 for (i=0; i < n; ++i) {
4566 if (strcasecmp(smartlist_get(r1->declared_family, i),
4567 smartlist_get(r2->declared_family, i)))
4568 return 0;
4572 /* Did bandwidth change a lot? */
4573 if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
4574 (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
4575 return 0;
4577 /* Did more than 12 hours pass? */
4578 if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
4579 < r2->cache_info.published_on)
4580 return 0;
4582 /* Did uptime fail to increase by approximately the amount we would think,
4583 * give or take some slop? */
4584 r1pub = r1->cache_info.published_on;
4585 r2pub = r2->cache_info.published_on;
4586 time_difference = labs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
4587 if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
4588 time_difference > r1->uptime * .05 &&
4589 time_difference > r2->uptime * .05)
4590 return 0;
4592 /* Otherwise, the difference is cosmetic. */
4593 return 1;
4596 /** Check whether <b>ri</b> (a.k.a. sd) is a router compatible with the
4597 * extrainfo document
4598 * <b>ei</b>. If no router is compatible with <b>ei</b>, <b>ei</b> should be
4599 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
4600 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
4601 * <b>msg</b> is present, set *<b>msg</b> to a description of the
4602 * incompatibility (if any).
4605 routerinfo_incompatible_with_extrainfo(routerinfo_t *ri, extrainfo_t *ei,
4606 signed_descriptor_t *sd,
4607 const char **msg)
4609 int digest_matches, r=1;
4610 tor_assert(ri);
4611 tor_assert(ei);
4612 if (!sd)
4613 sd = &ri->cache_info;
4615 if (ei->bad_sig) {
4616 if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
4617 return 1;
4620 digest_matches = !memcmp(ei->cache_info.signed_descriptor_digest,
4621 sd->extra_info_digest, DIGEST_LEN);
4623 /* The identity must match exactly to have been generated at the same time
4624 * by the same router. */
4625 if (memcmp(ri->cache_info.identity_digest, ei->cache_info.identity_digest,
4626 DIGEST_LEN)) {
4627 if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
4628 goto err; /* different servers */
4631 if (ei->pending_sig) {
4632 char signed_digest[128];
4633 if (crypto_pk_public_checksig(ri->identity_pkey, signed_digest,
4634 ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
4635 memcmp(signed_digest, ei->cache_info.signed_descriptor_digest,
4636 DIGEST_LEN)) {
4637 ei->bad_sig = 1;
4638 tor_free(ei->pending_sig);
4639 if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
4640 goto err; /* Bad signature, or no match. */
4643 ei->cache_info.send_unencrypted = ri->cache_info.send_unencrypted;
4644 tor_free(ei->pending_sig);
4647 if (ei->cache_info.published_on < sd->published_on) {
4648 if (msg) *msg = "Extrainfo published time did not match routerdesc";
4649 goto err;
4650 } else if (ei->cache_info.published_on > sd->published_on) {
4651 if (msg) *msg = "Extrainfo published time did not match routerdesc";
4652 r = -1;
4653 goto err;
4656 if (!digest_matches) {
4657 if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
4658 goto err; /* Digest doesn't match declared value. */
4661 return 0;
4662 err:
4663 if (digest_matches) {
4664 /* This signature was okay, and the digest was right: This is indeed the
4665 * corresponding extrainfo. But insanely, it doesn't match the routerinfo
4666 * that lists it. Don't try to fetch this one again. */
4667 sd->extrainfo_is_bogus = 1;
4670 return r;
4673 /** Assert that the internal representation of <b>rl</b> is
4674 * self-consistent. */
4675 void
4676 routerlist_assert_ok(routerlist_t *rl)
4678 routerinfo_t *r2;
4679 signed_descriptor_t *sd2;
4680 if (!rl)
4681 return;
4682 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
4684 r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
4685 tor_assert(r == r2);
4686 sd2 = sdmap_get(rl->desc_digest_map,
4687 r->cache_info.signed_descriptor_digest);
4688 tor_assert(&(r->cache_info) == sd2);
4689 tor_assert(r->cache_info.routerlist_index == r_sl_idx);
4690 /* XXXX
4692 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
4693 * commenting this out is just a band-aid.
4695 * The problem is that, although well-behaved router descriptors
4696 * should never have the same value for their extra_info_digest, it's
4697 * possible for ill-behaved routers to claim whatever they like there.
4699 * The real answer is to trash desc_by_eid_map and instead have
4700 * something that indicates for a given extra-info digest we want,
4701 * what its download status is. We'll do that as a part of routerlist
4702 * refactoring once consensus directories are in. For now,
4703 * this rep violation is probably harmless: an adversary can make us
4704 * reset our retry count for an extrainfo, but that's not the end
4705 * of the world. Changing the representation in 0.2.0.x would just
4706 * destabilize the codebase.
4707 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
4708 signed_descriptor_t *sd3 =
4709 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
4710 tor_assert(sd3 == &(r->cache_info));
4714 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
4716 r2 = rimap_get(rl->identity_map, sd->identity_digest);
4717 tor_assert(sd != &(r2->cache_info));
4718 sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
4719 tor_assert(sd == sd2);
4720 tor_assert(sd->routerlist_index == sd_sl_idx);
4721 /* XXXX see above.
4722 if (!tor_digest_is_zero(sd->extra_info_digest)) {
4723 signed_descriptor_t *sd3 =
4724 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
4725 tor_assert(sd3 == sd);
4730 RIMAP_FOREACH(rl->identity_map, d, r) {
4731 tor_assert(!memcmp(r->cache_info.identity_digest, d, DIGEST_LEN));
4732 } DIGESTMAP_FOREACH_END;
4733 SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
4734 tor_assert(!memcmp(sd->signed_descriptor_digest, d, DIGEST_LEN));
4735 } DIGESTMAP_FOREACH_END;
4736 SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
4737 tor_assert(!tor_digest_is_zero(d));
4738 tor_assert(sd);
4739 tor_assert(!memcmp(sd->extra_info_digest, d, DIGEST_LEN));
4740 } DIGESTMAP_FOREACH_END;
4741 EIMAP_FOREACH(rl->extra_info_map, d, ei) {
4742 signed_descriptor_t *sd;
4743 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
4744 d, DIGEST_LEN));
4745 sd = sdmap_get(rl->desc_by_eid_map,
4746 ei->cache_info.signed_descriptor_digest);
4747 // tor_assert(sd); // XXXX see above
4748 if (sd) {
4749 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
4750 sd->extra_info_digest, DIGEST_LEN));
4752 } DIGESTMAP_FOREACH_END;
4755 /** Allocate and return a new string representing the contact info
4756 * and platform string for <b>router</b>,
4757 * surrounded by quotes and using standard C escapes.
4759 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
4760 * thread. Also, each call invalidates the last-returned value, so don't
4761 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
4763 * If <b>router</b> is NULL, it just frees its internal memory and returns.
4765 const char *
4766 esc_router_info(routerinfo_t *router)
4768 static char *info=NULL;
4769 char *esc_contact, *esc_platform;
4770 size_t len;
4771 if (info)
4772 tor_free(info);
4773 if (!router)
4774 return NULL; /* we're exiting; just free the memory we use */
4776 esc_contact = esc_for_log(router->contact_info);
4777 esc_platform = esc_for_log(router->platform);
4779 len = strlen(esc_contact)+strlen(esc_platform)+32;
4780 info = tor_malloc(len);
4781 tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
4782 esc_platform);
4783 tor_free(esc_contact);
4784 tor_free(esc_platform);
4786 return info;
4789 /** Helper for sorting: compare two routerinfos by their identity
4790 * digest. */
4791 static int
4792 _compare_routerinfo_by_id_digest(const void **a, const void **b)
4794 routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
4795 return memcmp(first->cache_info.identity_digest,
4796 second->cache_info.identity_digest,
4797 DIGEST_LEN);
4800 /** Sort a list of routerinfo_t in ascending order of identity digest. */
4801 void
4802 routers_sort_by_identity(smartlist_t *routers)
4804 smartlist_sort(routers, _compare_routerinfo_by_id_digest);
4807 /** A routerset specifies constraints on a set of possible routerinfos, based
4808 * on their names, identities, or addresses. It is optimized for determining
4809 * whether a router is a member or not, in O(1+P) time, where P is the number
4810 * of address policy constraints. */
4811 struct routerset_t {
4812 /** A list of strings for the elements of the policy. Each string is either
4813 * a nickname, a hexadecimal identity fingerprint, or an address policy. A
4814 * router belongs to the set if its nickname OR its identity OR its address
4815 * matches an entry here. */
4816 smartlist_t *list;
4817 /** A map from lowercase nicknames of routers in the set to (void*)1 */
4818 strmap_t *names;
4819 /** A map from identity digests routers in the set to (void*)1 */
4820 digestmap_t *digests;
4821 /** An address policy for routers in the set. For implementation reasons,
4822 * a router belongs to the set if it is _rejected_ by this policy. */
4823 smartlist_t *policies;
4825 /** A human-readable description of what this routerset is for. Used in
4826 * log messages. */
4827 char *description;
4829 /** A list of the country codes in this set. */
4830 smartlist_t *country_names;
4831 /** Total number of countries we knew about when we built <b>countries</b>.*/
4832 int n_countries;
4833 /** Bit array mapping the return value of geoip_get_country() to 1 iff the
4834 * country is a member of this routerset. Note that we MUST call
4835 * routerset_refresh_countries() whenever the geoip country list is
4836 * reloaded. */
4837 bitarray_t *countries;
4840 /** Return a new empty routerset. */
4841 routerset_t *
4842 routerset_new(void)
4844 routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
4845 result->list = smartlist_create();
4846 result->names = strmap_new();
4847 result->digests = digestmap_new();
4848 result->policies = smartlist_create();
4849 result->country_names = smartlist_create();
4850 return result;
4853 /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
4854 * string holding the "cc" part. Else, return NULL. */
4855 static char *
4856 routerset_get_countryname(const char *c)
4858 char *country;
4860 if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
4861 return NULL;
4863 country = tor_strndup(c+1, 2);
4864 tor_strlower(country);
4865 return country;
4868 #if 0
4869 /** Add the GeoIP database's integer index (+1) of a valid two-character
4870 * country code to the routerset's <b>countries</b> bitarray. Return the
4871 * integer index if the country code is valid, -1 otherwise.*/
4872 static int
4873 routerset_add_country(const char *c)
4875 char country[3];
4876 country_t cc;
4878 /* XXXX: Country codes must be of the form \{[a-z\?]{2}\} but this accepts
4879 \{[.]{2}\}. Do we need to be strict? -RH */
4880 /* Nope; if the country code is bad, we'll get 0 when we look it up. */
4882 if (!geoip_is_loaded()) {
4883 log(LOG_WARN, LD_CONFIG, "GeoIP database not loaded: Cannot add country"
4884 "entry %s, ignoring.", c);
4885 return -1;
4888 memcpy(country, c+1, 2);
4889 country[2] = '\0';
4890 tor_strlower(country);
4892 if ((cc=geoip_get_country(country))==-1) {
4893 log(LOG_WARN, LD_CONFIG, "Country code '%s' is not valid, ignoring.",
4894 country);
4896 return cc;
4898 #endif
4900 /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
4901 * the GeoIP database is reloaded.
4903 void
4904 routerset_refresh_countries(routerset_t *target)
4906 int cc;
4907 if (target->countries) {
4908 bitarray_free(target->countries);
4910 if (!geoip_is_loaded()) {
4911 target->countries = NULL;
4912 target->n_countries = 0;
4913 return;
4915 target->n_countries = geoip_get_n_countries();
4916 target->countries = bitarray_init_zero(target->n_countries);
4917 SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
4918 cc = geoip_get_country(country);
4919 if (cc >= 0) {
4920 tor_assert(cc < target->n_countries);
4921 bitarray_set(target->countries, cc);
4922 } else {
4923 log(LOG_WARN, LD_CONFIG, "Country code '%s' is not recognized.",
4924 country);
4926 } SMARTLIST_FOREACH_END(country);
4929 /** Parse the string <b>s</b> to create a set of routerset entries, and add
4930 * them to <b>target</b>. In log messages, refer to the string as
4931 * <b>description</b>. Return 0 on success, -1 on failure.
4933 * Three kinds of elements are allowed in routersets: nicknames, IP address
4934 * patterns, and fingerprints. They may be surrounded by optional space, and
4935 * mst be separated by commas.
4938 routerset_parse(routerset_t *target, const char *s, const char *description)
4940 int r = 0;
4941 int added_countries = 0;
4942 char *countryname;
4943 smartlist_t *list = smartlist_create();
4944 smartlist_split_string(list, s, ",",
4945 SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
4946 SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
4947 addr_policy_t *p;
4948 if (is_legal_hexdigest(nick)) {
4949 char d[DIGEST_LEN];
4950 if (*nick == '$')
4951 ++nick;
4952 log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
4953 base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
4954 digestmap_set(target->digests, d, (void*)1);
4955 } else if (is_legal_nickname(nick)) {
4956 log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
4957 strmap_set_lc(target->names, nick, (void*)1);
4958 } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
4959 log_debug(LD_CONFIG, "Adding country %s to %s", nick,
4960 description);
4961 smartlist_add(target->country_names, countryname);
4962 added_countries = 1;
4963 } else if ((strchr(nick,'.') || strchr(nick, '*')) &&
4964 (p = router_parse_addr_policy_item_from_string(
4965 nick, ADDR_POLICY_REJECT))) {
4966 log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
4967 smartlist_add(target->policies, p);
4968 } else {
4969 log_warn(LD_CONFIG, "Entry '%s' in %s is misformed.", nick,
4970 description);
4971 r = -1;
4972 tor_free(nick);
4973 SMARTLIST_DEL_CURRENT(list, nick);
4975 } SMARTLIST_FOREACH_END(nick);
4976 smartlist_add_all(target->list, list);
4977 smartlist_free(list);
4978 if (added_countries)
4979 routerset_refresh_countries(target);
4980 return r;
4983 /** Add all members of the set <b>source</b> to <b>target</b>. */
4984 void
4985 routerset_union(routerset_t *target, const routerset_t *source)
4987 char *s;
4988 tor_assert(target);
4989 if (!source || !source->list)
4990 return;
4991 s = routerset_to_string(source);
4992 routerset_parse(target, s, "other routerset");
4993 tor_free(s);
4996 /** Return true iff <b>set</b> lists only nicknames and digests, and includes
4997 * no IP ranges or countries. */
4999 routerset_is_list(const routerset_t *set)
5001 return smartlist_len(set->country_names) == 0 &&
5002 smartlist_len(set->policies) == 0;
5005 /** Return true iff we need a GeoIP IP-to-country database to make sense of
5006 * <b>set</b>. */
5008 routerset_needs_geoip(const routerset_t *set)
5010 return set && smartlist_len(set->country_names);
5013 /** Return true iff there are no entries in <b>set</b>. */
5014 static int
5015 routerset_is_empty(const routerset_t *set)
5017 return !set || smartlist_len(set->list) == 0;
5020 /** Helper. Return true iff <b>set</b> contains a router based on the other
5021 * provided fields. Return higher values for more specific subentries: a
5022 * single router is more specific than an address range of routers, which is
5023 * more specific in turn than a country code.
5025 * (If country is -1, then we take the country
5026 * from addr.) */
5027 static int
5028 routerset_contains(const routerset_t *set, const tor_addr_t *addr,
5029 uint16_t orport,
5030 const char *nickname, const char *id_digest, int is_named,
5031 country_t country)
5033 if (!set || !set->list) return 0;
5034 (void) is_named; /* not supported */
5035 if (nickname && strmap_get_lc(set->names, nickname))
5036 return 4;
5037 if (id_digest && digestmap_get(set->digests, id_digest))
5038 return 4;
5039 if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
5040 == ADDR_POLICY_REJECTED)
5041 return 3;
5042 if (set->countries) {
5043 if (country < 0 && addr)
5044 country = geoip_get_country_by_ip(tor_addr_to_ipv4h(addr));
5046 if (country >= 0 && country < set->n_countries &&
5047 bitarray_is_set(set->countries, country))
5048 return 2;
5050 return 0;
5053 /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
5055 routerset_contains_extendinfo(const routerset_t *set, extend_info_t *ei)
5057 return routerset_contains(set,
5058 &ei->addr,
5059 ei->port,
5060 ei->nickname,
5061 ei->identity_digest,
5062 -1, /*is_named*/
5063 -1 /*country*/);
5066 /** Return true iff <b>ri</b> is in <b>set</b>. */
5068 routerset_contains_router(const routerset_t *set, routerinfo_t *ri)
5070 tor_addr_t addr;
5071 tor_addr_from_ipv4h(&addr, ri->addr);
5072 return routerset_contains(set,
5073 &addr,
5074 ri->or_port,
5075 ri->nickname,
5076 ri->cache_info.identity_digest,
5077 ri->is_named,
5078 ri->country);
5081 /** Return true iff <b>rs</b> is in <b>set</b>. */
5083 routerset_contains_routerstatus(const routerset_t *set, routerstatus_t *rs)
5085 tor_addr_t addr;
5086 tor_addr_from_ipv4h(&addr, rs->addr);
5087 return routerset_contains(set,
5088 &addr,
5089 rs->or_port,
5090 rs->nickname,
5091 rs->identity_digest,
5092 rs->is_named,
5093 -1);
5096 /** Add every known routerinfo_t that is a member of <b>routerset</b> to
5097 * <b>out</b>. If <b>running_only</b>, only add the running ones. */
5098 void
5099 routerset_get_all_routers(smartlist_t *out, const routerset_t *routerset,
5100 int running_only)
5102 tor_assert(out);
5103 if (!routerset || !routerset->list)
5104 return;
5105 if (!warned_nicknames)
5106 warned_nicknames = smartlist_create();
5107 if (routerset_is_list(routerset)) {
5109 /* No routers are specified by type; all are given by name or digest.
5110 * we can do a lookup in O(len(list)). */
5111 SMARTLIST_FOREACH(routerset->list, const char *, name, {
5112 routerinfo_t *router = router_get_by_nickname(name, 1);
5113 if (router) {
5114 if (!running_only || router->is_running)
5115 smartlist_add(out, router);
5118 } else {
5119 /* We need to iterate over the routerlist to get all the ones of the
5120 * right kind. */
5121 routerlist_t *rl = router_get_routerlist();
5122 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
5123 if (running_only && !router->is_running)
5124 continue;
5125 if (routerset_contains_router(routerset, router))
5126 smartlist_add(out, router);
5131 /** Add to <b>target</b> every routerinfo_t from <b>source</b> that is in
5132 * <b>include</b>, but not excluded in a more specific fashion by
5133 * <b>exclude</b>. If <b>running_only</b>, only include running routers.
5135 void
5136 routersets_get_disjunction(smartlist_t *target,
5137 const smartlist_t *source,
5138 const routerset_t *include,
5139 const routerset_t *exclude, int running_only)
5141 SMARTLIST_FOREACH(source, routerinfo_t *, router, {
5142 int include_result;
5143 if (running_only && !router->is_running)
5144 continue;
5145 if (!routerset_is_empty(include))
5146 include_result = routerset_contains_router(include, router);
5147 else
5148 include_result = 1;
5150 if (include_result) {
5151 int exclude_result = routerset_contains_router(exclude, router);
5152 if (include_result >= exclude_result)
5153 smartlist_add(target, router);
5158 /** Remove every routerinfo_t from <b>lst</b> that is in <b>routerset</b>. */
5159 void
5160 routerset_subtract_routers(smartlist_t *lst, const routerset_t *routerset)
5162 tor_assert(lst);
5163 if (!routerset)
5164 return;
5165 SMARTLIST_FOREACH(lst, routerinfo_t *, r, {
5166 if (routerset_contains_router(routerset, r)) {
5167 //log_debug(LD_DIR, "Subtracting %s",r->nickname);
5168 SMARTLIST_DEL_CURRENT(lst, r);
5173 /** Return a new string that when parsed by routerset_parse_string() will
5174 * yield <b>set</b>. */
5175 char *
5176 routerset_to_string(const routerset_t *set)
5178 if (!set || !set->list)
5179 return tor_strdup("");
5180 return smartlist_join_strings(set->list, ",", 0, NULL);
5183 /** Helper: return true iff old and new are both NULL, or both non-NULL
5184 * equal routersets. */
5186 routerset_equal(const routerset_t *old, const routerset_t *new)
5188 if (old == NULL && new == NULL)
5189 return 1;
5190 else if (old == NULL || new == NULL)
5191 return 0;
5193 if (smartlist_len(old->list) != smartlist_len(new->list))
5194 return 0;
5196 SMARTLIST_FOREACH(old->list, const char *, cp1, {
5197 const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
5198 if (strcmp(cp1, cp2))
5199 return 0;
5202 return 1;
5204 #if 0
5205 /* XXXX: This won't work if the names/digests are identical but in a
5206 different order. Checking for exact equality would be heavy going,
5207 is it worth it? -RH*/
5208 /* This code is totally bogus; sizeof doesn't work even remotely like this
5209 * code seems to think. Let's revert to a string-based comparison for
5210 * now. -NM*/
5211 if (sizeof(old->names) != sizeof(new->names))
5212 return 0;
5214 if (memcmp(old->names,new->names,sizeof(new->names)))
5215 return 0;
5216 if (sizeof(old->digests) != sizeof(new->digests))
5217 return 0;
5218 if (memcmp(old->digests,new->digests,sizeof(new->digests)))
5219 return 0;
5220 if (sizeof(old->countries) != sizeof(new->countries))
5221 return 0;
5222 if (memcmp(old->countries,new->countries,sizeof(new->countries)))
5223 return 0;
5224 return 1;
5225 #endif
5228 /** Free all storage held in <b>routerset</b>. */
5229 void
5230 routerset_free(routerset_t *routerset)
5232 SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
5233 smartlist_free(routerset->list);
5234 SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
5235 addr_policy_free(p));
5236 smartlist_free(routerset->policies);
5237 SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
5238 smartlist_free(routerset->country_names);
5240 strmap_free(routerset->names, NULL);
5241 digestmap_free(routerset->digests, NULL);
5242 if (routerset->countries)
5243 bitarray_free(routerset->countries);
5244 tor_free(routerset);
5247 /** Refresh the country code of <b>ri</b>. This function MUST be called on
5248 * each router when the GeoIP database is reloaded, and on all new routers. */
5249 void
5250 routerinfo_set_country(routerinfo_t *ri)
5252 ri->country = geoip_get_country_by_ip(ri->addr);
5255 /** Set the country code of all routers in the routerlist. */
5256 void
5257 routerlist_refresh_countries(void)
5259 routerlist_t *rl = router_get_routerlist();
5260 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
5261 routerinfo_set_country(ri));
5264 /** Determine the routers that are responsible for <b>id</b> (binary) and
5265 * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
5266 * Return -1 if we're returning an empty smartlist, else return 0.
5269 hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
5270 const char *id)
5272 int start, found, n_added = 0, i;
5273 networkstatus_t *c = networkstatus_get_latest_consensus();
5274 int use_begindir = get_options()->TunnelDirConns;
5275 if (!c || !smartlist_len(c->routerstatus_list)) {
5276 log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
5277 "rendezvous operations.");
5278 return -1;
5280 tor_assert(id);
5281 start = networkstatus_vote_find_entry_idx(c, id, &found);
5282 if (start == smartlist_len(c->routerstatus_list)) start = 0;
5283 i = start;
5284 do {
5285 routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
5286 if (r->is_hs_dir) {
5287 if (r->dir_port || use_begindir)
5288 smartlist_add(responsible_dirs, r);
5289 else
5290 log_info(LD_REND, "Not adding router '%s' to list of responsible "
5291 "hidden service directories, because we have no way of "
5292 "reaching it.", r->nickname);
5293 if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
5294 break;
5296 if (++i == smartlist_len(c->routerstatus_list))
5297 i = 0;
5298 } while (i != start);
5300 /* Even though we don't have the desired number of hidden service
5301 * directories, be happy if we got any. */
5302 return smartlist_len(responsible_dirs) ? 0 : -1;
5305 /** Return true if this node is currently acting as hidden service
5306 * directory, false otherwise. */
5308 hid_serv_acting_as_directory(void)
5310 routerinfo_t *me = router_get_my_routerinfo();
5311 networkstatus_t *c;
5312 routerstatus_t *rs;
5313 if (!me)
5314 return 0;
5315 if (!get_options()->HidServDirectoryV2) {
5316 log_info(LD_REND, "We are not acting as hidden service directory, "
5317 "because we have not been configured as such.");
5318 return 0;
5320 if (!(c = networkstatus_get_latest_consensus())) {
5321 log_info(LD_REND, "There's no consensus, so I can't tell if I'm a hidden "
5322 "service directory");
5323 return 0;
5325 rs = networkstatus_vote_find_entry(c, me->cache_info.identity_digest);
5326 if (!rs) {
5327 log_info(LD_REND, "We're not listed in the consensus, so we're not "
5328 "being a hidden service directory.");
5329 return 0;
5331 if (!rs->is_hs_dir) {
5332 log_info(LD_REND, "We're not listed as a hidden service directory in "
5333 "the consensus, so we won't be one.");
5334 return 0;
5336 return 1;
5339 /** Return true if this node is responsible for storing the descriptor ID
5340 * in <b>query</b> and false otherwise. */
5342 hid_serv_responsible_for_desc_id(const char *query)
5344 routerinfo_t *me;
5345 routerstatus_t *last_rs;
5346 const char *my_id, *last_id;
5347 int result;
5348 smartlist_t *responsible;
5349 if (!hid_serv_acting_as_directory())
5350 return 0;
5351 if (!(me = router_get_my_routerinfo()))
5352 return 0; /* This is redundant, but let's be paranoid. */
5353 my_id = me->cache_info.identity_digest;
5354 responsible = smartlist_create();
5355 if (hid_serv_get_responsible_directories(responsible, query) < 0) {
5356 smartlist_free(responsible);
5357 return 0;
5359 last_rs = smartlist_get(responsible, smartlist_len(responsible)-1);
5360 last_id = last_rs->identity_digest;
5361 result = rend_id_is_in_interval(my_id, query, last_id);
5362 smartlist_free(responsible);
5363 return result;