Create rendservice.h
[tor/rransom.git] / src / or / routerlist.c
blob6beecac40650fe97acbbc12b94c4d5d3909b9f26
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-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file routerlist.c
9 * \brief Code to
10 * maintain and access the global list of routerinfos for known
11 * servers.
12 **/
14 #include "or.h"
15 #include "geoip.h"
16 #include "rendcommon.h"
17 #include "rendservice.h"
18 #include "router.h"
19 #include "routerlist.h"
21 // #define DEBUG_ROUTERLIST
23 /****************************************************************************/
25 /* static function prototypes */
26 static routerstatus_t *router_pick_directory_server_impl(
27 authority_type_t auth, int flags);
28 static routerstatus_t *router_pick_trusteddirserver_impl(
29 authority_type_t auth, int flags, int *n_busy_out);
30 static void mark_all_trusteddirservers_up(void);
31 static int router_nickname_matches(routerinfo_t *router, const char *nickname);
32 static void trusted_dir_server_free(trusted_dir_server_t *ds);
33 static void launch_router_descriptor_downloads(smartlist_t *downloadable,
34 routerstatus_t *source,
35 time_t now);
36 static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
37 static void update_router_have_minimum_dir_info(void);
38 static const char *signed_descriptor_get_body_impl(signed_descriptor_t *desc,
39 int with_annotations);
40 static void list_pending_downloads(digestmap_t *result,
41 int purpose, const char *prefix);
43 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
44 DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
45 DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
46 #define SDMAP_FOREACH(map, keyvar, valvar) \
47 DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
48 valvar)
49 #define RIMAP_FOREACH(map, keyvar, valvar) \
50 DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
51 #define EIMAP_FOREACH(map, keyvar, valvar) \
52 DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
54 /****************************************************************************/
56 /** Global list of a trusted_dir_server_t object for each trusted directory
57 * server. */
58 static smartlist_t *trusted_dir_servers = NULL;
60 /** List of for a given authority, and download status for latest certificate.
62 typedef struct cert_list_t {
63 download_status_t dl_status;
64 smartlist_t *certs;
65 } cert_list_t;
66 /** Map from v3 identity key digest to cert_list_t. */
67 static digestmap_t *trusted_dir_certs = NULL;
68 /** True iff any key certificate in at least one member of
69 * <b>trusted_dir_certs</b> has changed since we last flushed the
70 * certificates to disk. */
71 static int trusted_dir_servers_certs_changed = 0;
73 /** Global list of all of the routers that we know about. */
74 static routerlist_t *routerlist = NULL;
76 /** List of strings for nicknames we've already warned about and that are
77 * still unknown / unavailable. */
78 static smartlist_t *warned_nicknames = NULL;
80 /** The last time we tried to download any routerdesc, or 0 for "never". We
81 * use this to rate-limit download attempts when the number of routerdescs to
82 * download is low. */
83 static time_t last_routerdesc_download_attempted = 0;
85 /** When we last computed the weights to use for bandwidths on directory
86 * requests, what were the total weighted bandwidth, and our share of that
87 * bandwidth? Used to determine what fraction of directory requests we should
88 * expect to see. */
89 static uint64_t sl_last_total_weighted_bw = 0,
90 sl_last_weighted_bw_of_me = 0;
92 /** Return the number of directory authorities whose type matches some bit set
93 * in <b>type</b> */
94 int
95 get_n_authorities(authority_type_t type)
97 int n = 0;
98 if (!trusted_dir_servers)
99 return 0;
100 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
101 if (ds->type & type)
102 ++n);
103 return n;
106 #define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
108 /** Helper: Return the cert_list_t for an authority whose authority ID is
109 * <b>id_digest</b>, allocating a new list if necessary. */
110 static cert_list_t *
111 get_cert_list(const char *id_digest)
113 cert_list_t *cl;
114 if (!trusted_dir_certs)
115 trusted_dir_certs = digestmap_new();
116 cl = digestmap_get(trusted_dir_certs, id_digest);
117 if (!cl) {
118 cl = tor_malloc_zero(sizeof(cert_list_t));
119 cl->dl_status.schedule = DL_SCHED_CONSENSUS;
120 cl->certs = smartlist_create();
121 digestmap_set(trusted_dir_certs, id_digest, cl);
123 return cl;
126 /** Reload the cached v3 key certificates from the cached-certs file in
127 * the data directory. Return 0 on success, -1 on failure. */
129 trusted_dirs_reload_certs(void)
131 char *filename;
132 char *contents;
133 int r;
135 filename = get_datadir_fname("cached-certs");
136 contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
137 tor_free(filename);
138 if (!contents)
139 return 0;
140 r = trusted_dirs_load_certs_from_string(contents, 1, 1);
141 tor_free(contents);
142 return r;
145 /** Helper: return true iff we already have loaded the exact cert
146 * <b>cert</b>. */
147 static INLINE int
148 already_have_cert(authority_cert_t *cert)
150 cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);
152 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
154 if (!memcmp(c->cache_info.signed_descriptor_digest,
155 cert->cache_info.signed_descriptor_digest,
156 DIGEST_LEN))
157 return 1;
159 return 0;
162 /** Load a bunch of new key certificates from the string <b>contents</b>. If
163 * <b>from_store</b> is true, the certificates are from the cache, and we
164 * don't need to flush them to disk. If <b>flush</b> is true, we need
165 * to flush any changed certificates to disk now. Return 0 on success, -1
166 * if any certs fail to parse. */
168 trusted_dirs_load_certs_from_string(const char *contents, int from_store,
169 int flush)
171 trusted_dir_server_t *ds;
172 const char *s, *eos;
173 int failure_code = 0;
175 for (s = contents; *s; s = eos) {
176 authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
177 cert_list_t *cl;
178 if (!cert) {
179 failure_code = -1;
180 break;
182 ds = trusteddirserver_get_by_v3_auth_digest(
183 cert->cache_info.identity_digest);
184 log_debug(LD_DIR, "Parsed certificate for %s",
185 ds ? ds->nickname : "unknown authority");
187 if (already_have_cert(cert)) {
188 /* we already have this one. continue. */
189 log_info(LD_DIR, "Skipping %s certificate for %s that we "
190 "already have.",
191 from_store ? "cached" : "downloaded",
192 ds ? ds->nickname : "an old or new authority");
194 /* a duplicate on a download should be treated as a failure, since it
195 * probably means we wanted a different secret key or we are trying to
196 * replace an expired cert that has not in fact been updated. */
197 if (!from_store) {
198 log_warn(LD_DIR, "Got a certificate for %s, but we already have it. "
199 "Maybe they haven't updated it. Waiting for a while.",
200 ds ? ds->nickname : "an old or new authority");
201 authority_cert_dl_failed(cert->cache_info.identity_digest, 404);
204 authority_cert_free(cert);
205 continue;
208 if (ds) {
209 log_info(LD_DIR, "Adding %s certificate for directory authority %s with "
210 "signing key %s", from_store ? "cached" : "downloaded",
211 ds->nickname, hex_str(cert->signing_key_digest,DIGEST_LEN));
212 } else {
213 int adding = directory_caches_dir_info(get_options());
214 log_info(LD_DIR, "%s %s certificate for unrecognized directory "
215 "authority with signing key %s",
216 adding ? "Adding" : "Not adding",
217 from_store ? "cached" : "downloaded",
218 hex_str(cert->signing_key_digest,DIGEST_LEN));
219 if (!adding) {
220 authority_cert_free(cert);
221 continue;
225 cl = get_cert_list(cert->cache_info.identity_digest);
226 smartlist_add(cl->certs, cert);
227 if (ds && cert->cache_info.published_on > ds->addr_current_at) {
228 /* Check to see whether we should update our view of the authority's
229 * address. */
230 if (cert->addr && cert->dir_port &&
231 (ds->addr != cert->addr ||
232 ds->dir_port != cert->dir_port)) {
233 char *a = tor_dup_ip(cert->addr);
234 log_notice(LD_DIR, "Updating address for directory authority %s "
235 "from %s:%d to %s:%d based on certificate.",
236 ds->nickname, ds->address, (int)ds->dir_port,
237 a, cert->dir_port);
238 tor_free(a);
239 ds->addr = cert->addr;
240 ds->dir_port = cert->dir_port;
242 ds->addr_current_at = cert->cache_info.published_on;
245 if (!from_store)
246 trusted_dir_servers_certs_changed = 1;
249 if (flush)
250 trusted_dirs_flush_certs_to_disk();
252 /* call this even if failure_code is <0, since some certs might have
253 * succeeded. */
254 networkstatus_note_certs_arrived();
256 return failure_code;
259 /** Save all v3 key certificates to the cached-certs file. */
260 void
261 trusted_dirs_flush_certs_to_disk(void)
263 char *filename;
264 smartlist_t *chunks;
266 if (!trusted_dir_servers_certs_changed || !trusted_dir_certs)
267 return;
269 chunks = smartlist_create();
270 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
271 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
273 sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
274 c->bytes = cert->cache_info.signed_descriptor_body;
275 c->len = cert->cache_info.signed_descriptor_len;
276 smartlist_add(chunks, c);
278 } DIGESTMAP_FOREACH_END;
280 filename = get_datadir_fname("cached-certs");
281 if (write_chunks_to_file(filename, chunks, 0)) {
282 log_warn(LD_FS, "Error writing certificates to disk.");
284 tor_free(filename);
285 SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
286 smartlist_free(chunks);
288 trusted_dir_servers_certs_changed = 0;
291 /** Remove all v3 authority certificates that have been superseded for more
292 * than 48 hours. (If the most recent cert was published more than 48 hours
293 * ago, then we aren't going to get any consensuses signed with older
294 * keys.) */
295 static void
296 trusted_dirs_remove_old_certs(void)
298 time_t now = time(NULL);
299 #define DEAD_CERT_LIFETIME (2*24*60*60)
300 #define OLD_CERT_LIFETIME (7*24*60*60)
301 if (!trusted_dir_certs)
302 return;
304 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
305 authority_cert_t *newest = NULL;
306 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
307 if (!newest || (cert->cache_info.published_on >
308 newest->cache_info.published_on))
309 newest = cert);
310 if (newest) {
311 const time_t newest_published = newest->cache_info.published_on;
312 SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
313 int expired;
314 time_t cert_published;
315 if (newest == cert)
316 continue;
317 expired = ftime_definitely_after(now, cert->expires);
318 cert_published = cert->cache_info.published_on;
319 /* Store expired certs for 48 hours after a newer arrives;
321 if (expired ?
322 (newest_published + DEAD_CERT_LIFETIME < now) :
323 (cert_published + OLD_CERT_LIFETIME < newest_published)) {
324 SMARTLIST_DEL_CURRENT(cl->certs, cert);
325 authority_cert_free(cert);
326 trusted_dir_servers_certs_changed = 1;
328 } SMARTLIST_FOREACH_END(cert);
330 } DIGESTMAP_FOREACH_END;
331 #undef OLD_CERT_LIFETIME
333 trusted_dirs_flush_certs_to_disk();
336 /** Return the newest v3 authority certificate whose v3 authority identity key
337 * has digest <b>id_digest</b>. Return NULL if no such authority is known,
338 * or it has no certificate. */
339 authority_cert_t *
340 authority_cert_get_newest_by_id(const char *id_digest)
342 cert_list_t *cl;
343 authority_cert_t *best = NULL;
344 if (!trusted_dir_certs ||
345 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
346 return NULL;
348 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
350 if (!best || cert->cache_info.published_on > best->cache_info.published_on)
351 best = cert;
353 return best;
356 /** Return the newest v3 authority certificate whose directory signing key has
357 * digest <b>sk_digest</b>. Return NULL if no such certificate is known.
359 authority_cert_t *
360 authority_cert_get_by_sk_digest(const char *sk_digest)
362 authority_cert_t *c;
363 if (!trusted_dir_certs)
364 return NULL;
366 if ((c = get_my_v3_authority_cert()) &&
367 !memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
368 return c;
369 if ((c = get_my_v3_legacy_cert()) &&
370 !memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
371 return c;
373 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
374 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
376 if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
377 return cert;
379 } DIGESTMAP_FOREACH_END;
380 return NULL;
383 /** Return the v3 authority certificate with signing key matching
384 * <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
385 * Return NULL if no such authority is known. */
386 authority_cert_t *
387 authority_cert_get_by_digests(const char *id_digest,
388 const char *sk_digest)
390 cert_list_t *cl;
391 if (!trusted_dir_certs ||
392 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
393 return NULL;
394 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
395 if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
396 return cert; );
398 return NULL;
401 /** Add every known authority_cert_t to <b>certs_out</b>. */
402 void
403 authority_cert_get_all(smartlist_t *certs_out)
405 tor_assert(certs_out);
406 if (!trusted_dir_certs)
407 return;
409 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
410 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
411 smartlist_add(certs_out, c));
412 } DIGESTMAP_FOREACH_END;
415 /** Called when an attempt to download a certificate with the authority with
416 * ID <b>id_digest</b> fails with HTTP response code <b>status</b>: remember
417 * the failure, so we don't try again immediately. */
418 void
419 authority_cert_dl_failed(const char *id_digest, int status)
421 cert_list_t *cl;
422 if (!trusted_dir_certs ||
423 !(cl = digestmap_get(trusted_dir_certs, id_digest)))
424 return;
426 download_status_failed(&cl->dl_status, status);
429 /** How many times will we try to fetch a certificate before giving up? */
430 #define MAX_CERT_DL_FAILURES 8
432 /** Try to download any v3 authority certificates that we may be missing. If
433 * <b>status</b> is provided, try to get all the ones that were used to sign
434 * <b>status</b>. Additionally, try to have a non-expired certificate for
435 * every V3 authority in trusted_dir_servers. Don't fetch certificates we
436 * already have.
438 void
439 authority_certs_fetch_missing(networkstatus_t *status, time_t now)
441 digestmap_t *pending;
442 authority_cert_t *cert;
443 smartlist_t *missing_digests;
444 char *resource = NULL;
445 cert_list_t *cl;
446 const int cache = directory_caches_dir_info(get_options());
448 if (should_delay_dir_fetches(get_options()))
449 return;
451 pending = digestmap_new();
452 missing_digests = smartlist_create();
454 list_pending_downloads(pending, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
455 if (status) {
456 SMARTLIST_FOREACH_BEGIN(status->voters, networkstatus_voter_info_t *,
457 voter) {
458 if (!smartlist_len(voter->sigs))
459 continue; /* This authority never signed this consensus, so don't
460 * go looking for a cert with key digest 0000000000. */
461 if (!cache &&
462 !trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
463 continue; /* We are not a cache, and we don't know this authority.*/
464 cl = get_cert_list(voter->identity_digest);
465 SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
466 cert = authority_cert_get_by_digests(voter->identity_digest,
467 sig->signing_key_digest);
468 if (cert) {
469 if (now < cert->expires)
470 download_status_reset(&cl->dl_status);
471 continue;
473 if (download_status_is_ready(&cl->dl_status, now,
474 MAX_CERT_DL_FAILURES) &&
475 !digestmap_get(pending, voter->identity_digest)) {
476 log_notice(LD_DIR, "We're missing a certificate from authority "
477 "with signing key %s: launching request.",
478 hex_str(sig->signing_key_digest, DIGEST_LEN));
479 smartlist_add(missing_digests, sig->identity_digest);
481 } SMARTLIST_FOREACH_END(sig);
482 } SMARTLIST_FOREACH_END(voter);
484 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, ds) {
485 int found = 0;
486 if (!(ds->type & V3_AUTHORITY))
487 continue;
488 if (smartlist_digest_isin(missing_digests, ds->v3_identity_digest))
489 continue;
490 cl = get_cert_list(ds->v3_identity_digest);
491 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert, {
492 if (!ftime_definitely_after(now, cert->expires)) {
493 /* It's not expired, and we weren't looking for something to
494 * verify a consensus with. Call it done. */
495 download_status_reset(&cl->dl_status);
496 found = 1;
497 break;
500 if (!found &&
501 download_status_is_ready(&cl->dl_status, now,MAX_CERT_DL_FAILURES) &&
502 !digestmap_get(pending, ds->v3_identity_digest)) {
503 log_notice(LD_DIR, "No current certificate known for authority %s; "
504 "launching request.", ds->nickname);
505 smartlist_add(missing_digests, ds->v3_identity_digest);
507 } SMARTLIST_FOREACH_END(ds);
509 if (!smartlist_len(missing_digests)) {
510 goto done;
511 } else {
512 smartlist_t *fps = smartlist_create();
513 smartlist_add(fps, tor_strdup("fp/"));
514 SMARTLIST_FOREACH(missing_digests, const char *, d, {
515 char *fp;
516 if (digestmap_get(pending, d))
517 continue;
518 fp = tor_malloc(HEX_DIGEST_LEN+2);
519 base16_encode(fp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
520 fp[HEX_DIGEST_LEN] = '+';
521 fp[HEX_DIGEST_LEN+1] = '\0';
522 smartlist_add(fps, fp);
524 if (smartlist_len(fps) == 1) {
525 /* we didn't add any: they were all pending */
526 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
527 smartlist_free(fps);
528 goto done;
530 resource = smartlist_join_strings(fps, "", 0, NULL);
531 resource[strlen(resource)-1] = '\0';
532 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
533 smartlist_free(fps);
535 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
536 resource, PDS_RETRY_IF_NO_SERVERS);
538 done:
539 tor_free(resource);
540 smartlist_free(missing_digests);
541 digestmap_free(pending, NULL);
544 /* Router descriptor storage.
546 * Routerdescs are stored in a big file, named "cached-descriptors". As new
547 * routerdescs arrive, we append them to a journal file named
548 * "cached-descriptors.new".
550 * From time to time, we replace "cached-descriptors" with a new file
551 * containing only the live, non-superseded descriptors, and clear
552 * cached-routers.new.
554 * On startup, we read both files.
557 /** Helper: return 1 iff the router log is so big we want to rebuild the
558 * store. */
559 static int
560 router_should_rebuild_store(desc_store_t *store)
562 if (store->store_len > (1<<16))
563 return (store->journal_len > store->store_len / 2 ||
564 store->bytes_dropped > store->store_len / 2);
565 else
566 return store->journal_len > (1<<15);
569 /** Return the desc_store_t in <b>rl</b> that should be used to store
570 * <b>sd</b>. */
571 static INLINE desc_store_t *
572 desc_get_store(routerlist_t *rl, signed_descriptor_t *sd)
574 if (sd->is_extrainfo)
575 return &rl->extrainfo_store;
576 else
577 return &rl->desc_store;
580 /** Add the signed_descriptor_t in <b>desc</b> to the router
581 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
582 * offset appropriately. */
583 static int
584 signed_desc_append_to_journal(signed_descriptor_t *desc,
585 desc_store_t *store)
587 char *fname = get_datadir_fname_suffix(store->fname_base, ".new");
588 const char *body = signed_descriptor_get_body_impl(desc,1);
589 size_t len = desc->signed_descriptor_len + desc->annotations_len;
591 if (append_bytes_to_file(fname, body, len, 1)) {
592 log_warn(LD_FS, "Unable to store router descriptor");
593 tor_free(fname);
594 return -1;
596 desc->saved_location = SAVED_IN_JOURNAL;
597 tor_free(fname);
599 desc->saved_offset = store->journal_len;
600 store->journal_len += len;
602 return 0;
605 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
606 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
607 * the signed_descriptor_t* in *<b>b</b>. */
608 static int
609 _compare_signed_descriptors_by_age(const void **_a, const void **_b)
611 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
612 return (int)(r1->published_on - r2->published_on);
615 #define RRS_FORCE 1
616 #define RRS_DONT_REMOVE_OLD 2
618 /** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
619 * <b>flags</b>, then atomically replace the saved router store with the
620 * routers currently in our routerlist, and clear the journal. Unless
621 * RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
622 * rebuilding the store. Return 0 on success, -1 on failure.
624 static int
625 router_rebuild_store(int flags, desc_store_t *store)
627 smartlist_t *chunk_list = NULL;
628 char *fname = NULL, *fname_tmp = NULL;
629 int r = -1;
630 off_t offset = 0;
631 smartlist_t *signed_descriptors = NULL;
632 int nocache=0;
633 size_t total_expected_len = 0;
634 int had_any;
635 int force = flags & RRS_FORCE;
637 if (!force && !router_should_rebuild_store(store)) {
638 r = 0;
639 goto done;
641 if (!routerlist) {
642 r = 0;
643 goto done;
646 if (store->type == EXTRAINFO_STORE)
647 had_any = !eimap_isempty(routerlist->extra_info_map);
648 else
649 had_any = (smartlist_len(routerlist->routers)+
650 smartlist_len(routerlist->old_routers))>0;
652 /* Don't save deadweight. */
653 if (!(flags & RRS_DONT_REMOVE_OLD))
654 routerlist_remove_old_routers();
656 log_info(LD_DIR, "Rebuilding %s cache", store->description);
658 fname = get_datadir_fname(store->fname_base);
659 fname_tmp = get_datadir_fname_suffix(store->fname_base, ".tmp");
661 chunk_list = smartlist_create();
663 /* We sort the routers by age to enhance locality on disk. */
664 signed_descriptors = smartlist_create();
665 if (store->type == EXTRAINFO_STORE) {
666 eimap_iter_t *iter;
667 for (iter = eimap_iter_init(routerlist->extra_info_map);
668 !eimap_iter_done(iter);
669 iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
670 const char *key;
671 extrainfo_t *ei;
672 eimap_iter_get(iter, &key, &ei);
673 smartlist_add(signed_descriptors, &ei->cache_info);
675 } else {
676 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
677 smartlist_add(signed_descriptors, sd));
678 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
679 smartlist_add(signed_descriptors, &ri->cache_info));
682 smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);
684 /* Now, add the appropriate members to chunk_list */
685 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
687 sized_chunk_t *c;
688 const char *body = signed_descriptor_get_body_impl(sd, 1);
689 if (!body) {
690 log_warn(LD_BUG, "No descriptor available for router.");
691 goto done;
693 if (sd->do_not_cache) {
694 ++nocache;
695 continue;
697 c = tor_malloc(sizeof(sized_chunk_t));
698 c->bytes = body;
699 c->len = sd->signed_descriptor_len + sd->annotations_len;
700 total_expected_len += c->len;
701 smartlist_add(chunk_list, c);
704 if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
705 log_warn(LD_FS, "Error writing router store to disk.");
706 goto done;
709 /* Our mmap is now invalid. */
710 if (store->mmap) {
711 tor_munmap_file(store->mmap);
712 store->mmap = NULL;
715 if (replace_file(fname_tmp, fname)<0) {
716 log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
717 goto done;
720 errno = 0;
721 store->mmap = tor_mmap_file(fname);
722 if (! store->mmap) {
723 if (errno == ERANGE) {
724 /* empty store.*/
725 if (total_expected_len) {
726 log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
727 " but when we went to mmap it, it was empty!", fname);
728 } else if (had_any) {
729 log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
730 "okay if we're just starting up after a long time. "
731 "Otherwise, it's a bug.", fname);
733 } else {
734 log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
738 log_info(LD_DIR, "Reconstructing pointers into cache");
740 offset = 0;
741 SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
743 if (sd->do_not_cache)
744 continue;
745 sd->saved_location = SAVED_IN_CACHE;
746 if (store->mmap) {
747 tor_free(sd->signed_descriptor_body); // sets it to null
748 sd->saved_offset = offset;
750 offset += sd->signed_descriptor_len + sd->annotations_len;
751 signed_descriptor_get_body(sd); /* reconstruct and assert */
754 tor_free(fname);
755 fname = get_datadir_fname_suffix(store->fname_base, ".new");
756 write_str_to_file(fname, "", 1);
758 r = 0;
759 store->store_len = (size_t) offset;
760 store->journal_len = 0;
761 store->bytes_dropped = 0;
762 done:
763 smartlist_free(signed_descriptors);
764 tor_free(fname);
765 tor_free(fname_tmp);
766 if (chunk_list) {
767 SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
768 smartlist_free(chunk_list);
771 return r;
774 /** Helper: Reload a cache file and its associated journal, setting metadata
775 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
776 * else reload the router descriptor store. */
777 static int
778 router_reload_router_list_impl(desc_store_t *store)
780 char *fname = NULL, *altname = NULL, *contents = NULL;
781 struct stat st;
782 int read_from_old_location = 0;
783 int extrainfo = (store->type == EXTRAINFO_STORE);
784 time_t now = time(NULL);
785 store->journal_len = store->store_len = 0;
787 fname = get_datadir_fname(store->fname_base);
788 if (store->fname_alt_base)
789 altname = get_datadir_fname(store->fname_alt_base);
791 if (store->mmap) /* get rid of it first */
792 tor_munmap_file(store->mmap);
793 store->mmap = NULL;
795 store->mmap = tor_mmap_file(fname);
796 if (!store->mmap && altname && file_status(altname) == FN_FILE) {
797 read_from_old_location = 1;
798 log_notice(LD_DIR, "Couldn't read %s; trying to load routers from old "
799 "location %s.", fname, altname);
800 if ((store->mmap = tor_mmap_file(altname)))
801 read_from_old_location = 1;
803 if (altname && !read_from_old_location) {
804 remove_file_if_very_old(altname, now);
806 if (store->mmap) {
807 store->store_len = store->mmap->size;
808 if (extrainfo)
809 router_load_extrainfo_from_string(store->mmap->data,
810 store->mmap->data+store->mmap->size,
811 SAVED_IN_CACHE, NULL, 0);
812 else
813 router_load_routers_from_string(store->mmap->data,
814 store->mmap->data+store->mmap->size,
815 SAVED_IN_CACHE, NULL, 0, NULL);
818 tor_free(fname);
819 fname = get_datadir_fname_suffix(store->fname_base, ".new");
820 if (file_status(fname) == FN_FILE)
821 contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
822 if (read_from_old_location) {
823 tor_free(altname);
824 altname = get_datadir_fname_suffix(store->fname_alt_base, ".new");
825 if (!contents)
826 contents = read_file_to_str(altname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
827 else
828 remove_file_if_very_old(altname, now);
830 if (contents) {
831 if (extrainfo)
832 router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL,
833 NULL, 0);
834 else
835 router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL,
836 NULL, 0, NULL);
837 store->journal_len = (size_t) st.st_size;
838 tor_free(contents);
841 tor_free(fname);
842 tor_free(altname);
844 if (store->journal_len || read_from_old_location) {
845 /* Always clear the journal on startup.*/
846 router_rebuild_store(RRS_FORCE, store);
847 } else if (!extrainfo) {
848 /* Don't cache expired routers. (This is in an else because
849 * router_rebuild_store() also calls remove_old_routers().) */
850 routerlist_remove_old_routers();
853 return 0;
856 /** Load all cached router descriptors and extra-info documents from the
857 * store. Return 0 on success and -1 on failure.
860 router_reload_router_list(void)
862 routerlist_t *rl = router_get_routerlist();
863 if (router_reload_router_list_impl(&rl->desc_store))
864 return -1;
865 if (router_reload_router_list_impl(&rl->extrainfo_store))
866 return -1;
867 return 0;
870 /** Return a smartlist containing a list of trusted_dir_server_t * for all
871 * known trusted dirservers. Callers must not modify the list or its
872 * contents.
874 smartlist_t *
875 router_get_trusted_dir_servers(void)
877 if (!trusted_dir_servers)
878 trusted_dir_servers = smartlist_create();
880 return trusted_dir_servers;
883 /** Try to find a running dirserver that supports operations of <b>type</b>.
885 * If there are no running dirservers in our routerlist and the
886 * <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the authoritative ones
887 * as running again, and pick one.
889 * If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
890 * dirservers that we can't reach.
892 * If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
893 * (if we're a dirserver).
895 * Don't pick an authority if any non-authority is viable; try to avoid using
896 * servers that have returned 503 recently.
898 routerstatus_t *
899 router_pick_directory_server(authority_type_t type, int flags)
901 routerstatus_t *choice;
902 if (get_options()->PreferTunneledDirConns)
903 flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
905 if (!routerlist)
906 return NULL;
908 choice = router_pick_directory_server_impl(type, flags);
909 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
910 return choice;
912 log_info(LD_DIR,
913 "No reachable router entries for dirservers. "
914 "Trying them all again.");
915 /* mark all authdirservers as up again */
916 mark_all_trusteddirservers_up();
917 /* try again */
918 choice = router_pick_directory_server_impl(type, flags);
919 return choice;
922 /** Try to determine which fraction of v2 and v3 directory requests aimed at
923 * caches will be sent to us. Set *<b>v2_share_out</b> and
924 * *<b>v3_share_out</b> to the fractions of v2 and v3 protocol shares we
925 * expect to see, respectively. Return 0 on success, negative on failure. */
927 router_get_my_share_of_directory_requests(double *v2_share_out,
928 double *v3_share_out)
930 routerinfo_t *me = router_get_my_routerinfo();
931 routerstatus_t *rs;
932 const int pds_flags = PDS_ALLOW_SELF|PDS_IGNORE_FASCISTFIREWALL;
933 *v2_share_out = *v3_share_out = 0.0;
934 if (!me)
935 return -1;
936 rs = router_get_consensus_status_by_id(me->cache_info.identity_digest);
937 if (!rs)
938 return -1;
940 /* Calling for side effect */
941 /* XXXX This is a bit of a kludge */
942 if (rs->is_v2_dir) {
943 sl_last_total_weighted_bw = 0;
944 router_pick_directory_server(V2_AUTHORITY, pds_flags);
945 if (sl_last_total_weighted_bw != 0) {
946 *v2_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
947 U64_TO_DBL(sl_last_total_weighted_bw);
951 if (rs->version_supports_v3_dir) {
952 sl_last_total_weighted_bw = 0;
953 router_pick_directory_server(V3_AUTHORITY, pds_flags);
954 if (sl_last_total_weighted_bw != 0) {
955 *v3_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
956 U64_TO_DBL(sl_last_total_weighted_bw);
960 return 0;
963 /** Return the trusted_dir_server_t for the directory authority whose identity
964 * key hashes to <b>digest</b>, or NULL if no such authority is known.
966 trusted_dir_server_t *
967 router_get_trusteddirserver_by_digest(const char *digest)
969 if (!trusted_dir_servers)
970 return NULL;
972 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
974 if (!memcmp(ds->digest, digest, DIGEST_LEN))
975 return ds;
978 return NULL;
981 /** Return the trusted_dir_server_t for the directory authority whose
982 * v3 identity key hashes to <b>digest</b>, or NULL if no such authority
983 * is known.
985 trusted_dir_server_t *
986 trusteddirserver_get_by_v3_auth_digest(const char *digest)
988 if (!trusted_dir_servers)
989 return NULL;
991 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
993 if (!memcmp(ds->v3_identity_digest, digest, DIGEST_LEN) &&
994 (ds->type & V3_AUTHORITY))
995 return ds;
998 return NULL;
1001 /** Try to find a running trusted dirserver. Flags are as for
1002 * router_pick_directory_server.
1004 routerstatus_t *
1005 router_pick_trusteddirserver(authority_type_t type, int flags)
1007 routerstatus_t *choice;
1008 int busy = 0;
1009 if (get_options()->PreferTunneledDirConns)
1010 flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
1012 choice = router_pick_trusteddirserver_impl(type, flags, &busy);
1013 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
1014 return choice;
1015 if (busy) {
1016 /* If the reason that we got no server is that servers are "busy",
1017 * we must be excluding good servers because we already have serverdesc
1018 * fetches with them. Do not mark down servers up because of this. */
1019 tor_assert((flags & PDS_NO_EXISTING_SERVERDESC_FETCH));
1020 return NULL;
1023 log_info(LD_DIR,
1024 "No trusted dirservers are reachable. Trying them all again.");
1025 mark_all_trusteddirservers_up();
1026 return router_pick_trusteddirserver_impl(type, flags, NULL);
1029 /** How long do we avoid using a directory server after it's given us a 503? */
1030 #define DIR_503_TIMEOUT (60*60)
1032 /** Pick a random running valid directory server/mirror from our
1033 * routerlist. Arguments are as for router_pick_directory_server(), except
1034 * that RETRY_IF_NO_SERVERS is ignored, and:
1036 * If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers
1037 * that we can use with BEGINDIR.
1039 static routerstatus_t *
1040 router_pick_directory_server_impl(authority_type_t type, int flags)
1042 routerstatus_t *result;
1043 smartlist_t *direct, *tunnel;
1044 smartlist_t *trusted_direct, *trusted_tunnel;
1045 smartlist_t *overloaded_direct, *overloaded_tunnel;
1046 time_t now = time(NULL);
1047 const networkstatus_t *consensus = networkstatus_get_latest_consensus();
1048 int requireother = ! (flags & PDS_ALLOW_SELF);
1049 int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
1050 int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
1052 if (!consensus)
1053 return NULL;
1055 direct = smartlist_create();
1056 tunnel = smartlist_create();
1057 trusted_direct = smartlist_create();
1058 trusted_tunnel = smartlist_create();
1059 overloaded_direct = smartlist_create();
1060 overloaded_tunnel = smartlist_create();
1062 /* Find all the running dirservers we know about. */
1063 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *,
1064 status) {
1065 int is_trusted;
1066 int is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
1067 tor_addr_t addr;
1068 if (!status->is_running || !status->dir_port || !status->is_valid)
1069 continue;
1070 if (status->is_bad_directory)
1071 continue;
1072 if (requireother && router_digest_is_me(status->identity_digest))
1073 continue;
1074 if (type & V3_AUTHORITY) {
1075 if (!(status->version_supports_v3_dir ||
1076 router_digest_is_trusted_dir_type(status->identity_digest,
1077 V3_AUTHORITY)))
1078 continue;
1080 is_trusted = router_digest_is_trusted_dir(status->identity_digest);
1081 if ((type & V2_AUTHORITY) && !(status->is_v2_dir || is_trusted))
1082 continue;
1083 if ((type & EXTRAINFO_CACHE) &&
1084 !router_supports_extrainfo(status->identity_digest, 0))
1085 continue;
1087 /* XXXX IP6 proposal 118 */
1088 tor_addr_from_ipv4h(&addr, status->addr);
1090 if (prefer_tunnel &&
1091 status->version_supports_begindir &&
1092 (!fascistfirewall ||
1093 fascist_firewall_allows_address_or(&addr, status->or_port)))
1094 smartlist_add(is_trusted ? trusted_tunnel :
1095 is_overloaded ? overloaded_tunnel : tunnel, status);
1096 else if (!fascistfirewall ||
1097 fascist_firewall_allows_address_dir(&addr, status->dir_port))
1098 smartlist_add(is_trusted ? trusted_direct :
1099 is_overloaded ? overloaded_direct : direct, status);
1100 } SMARTLIST_FOREACH_END(status);
1102 if (smartlist_len(tunnel)) {
1103 result = routerstatus_sl_choose_by_bandwidth(tunnel, WEIGHT_FOR_DIR);
1104 } else if (smartlist_len(overloaded_tunnel)) {
1105 result = routerstatus_sl_choose_by_bandwidth(overloaded_tunnel,
1106 WEIGHT_FOR_DIR);
1107 } else if (smartlist_len(trusted_tunnel)) {
1108 /* FFFF We don't distinguish between trusteds and overloaded trusteds
1109 * yet. Maybe one day we should. */
1110 /* FFFF We also don't load balance over authorities yet. I think this
1111 * is a feature, but it could easily be a bug. -RD */
1112 result = smartlist_choose(trusted_tunnel);
1113 } else if (smartlist_len(direct)) {
1114 result = routerstatus_sl_choose_by_bandwidth(direct, WEIGHT_FOR_DIR);
1115 } else if (smartlist_len(overloaded_direct)) {
1116 result = routerstatus_sl_choose_by_bandwidth(overloaded_direct,
1117 WEIGHT_FOR_DIR);
1118 } else {
1119 result = smartlist_choose(trusted_direct);
1121 smartlist_free(direct);
1122 smartlist_free(tunnel);
1123 smartlist_free(trusted_direct);
1124 smartlist_free(trusted_tunnel);
1125 smartlist_free(overloaded_direct);
1126 smartlist_free(overloaded_tunnel);
1127 return result;
1130 /** Choose randomly from among the trusted dirservers that are up. Flags
1131 * are as for router_pick_directory_server_impl().
1133 static routerstatus_t *
1134 router_pick_trusteddirserver_impl(authority_type_t type, int flags,
1135 int *n_busy_out)
1137 smartlist_t *direct, *tunnel;
1138 smartlist_t *overloaded_direct, *overloaded_tunnel;
1139 routerinfo_t *me = router_get_my_routerinfo();
1140 routerstatus_t *result;
1141 time_t now = time(NULL);
1142 const int requireother = ! (flags & PDS_ALLOW_SELF);
1143 const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
1144 const int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
1145 const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
1146 int n_busy = 0;
1148 if (!trusted_dir_servers)
1149 return NULL;
1151 direct = smartlist_create();
1152 tunnel = smartlist_create();
1153 overloaded_direct = smartlist_create();
1154 overloaded_tunnel = smartlist_create();
1156 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, d)
1158 int is_overloaded =
1159 d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
1160 tor_addr_t addr;
1161 if (!d->is_running) continue;
1162 if ((type & d->type) == 0)
1163 continue;
1164 if ((type & EXTRAINFO_CACHE) &&
1165 !router_supports_extrainfo(d->digest, 1))
1166 continue;
1167 if (requireother && me && router_digest_is_me(d->digest))
1168 continue;
1170 /* XXXX IP6 proposal 118 */
1171 tor_addr_from_ipv4h(&addr, d->addr);
1173 if (no_serverdesc_fetching) {
1174 if (connection_get_by_type_addr_port_purpose(
1175 CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_SERVERDESC)
1176 || connection_get_by_type_addr_port_purpose(
1177 CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_EXTRAINFO)) {
1178 //log_debug(LD_DIR, "We have an existing connection to fetch "
1179 // "descriptor from %s; delaying",d->description);
1180 ++n_busy;
1181 continue;
1185 if (prefer_tunnel &&
1186 d->or_port &&
1187 (!fascistfirewall ||
1188 fascist_firewall_allows_address_or(&addr, d->or_port)))
1189 smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
1190 &d->fake_status);
1191 else if (!fascistfirewall ||
1192 fascist_firewall_allows_address_dir(&addr, d->dir_port))
1193 smartlist_add(is_overloaded ? overloaded_direct : direct,
1194 &d->fake_status);
1196 SMARTLIST_FOREACH_END(d);
1198 if (smartlist_len(tunnel)) {
1199 result = smartlist_choose(tunnel);
1200 } else if (smartlist_len(overloaded_tunnel)) {
1201 result = smartlist_choose(overloaded_tunnel);
1202 } else if (smartlist_len(direct)) {
1203 result = smartlist_choose(direct);
1204 } else {
1205 result = smartlist_choose(overloaded_direct);
1208 if (n_busy_out)
1209 *n_busy_out = n_busy;
1211 smartlist_free(direct);
1212 smartlist_free(tunnel);
1213 smartlist_free(overloaded_direct);
1214 smartlist_free(overloaded_tunnel);
1215 return result;
1218 /** Go through and mark the authoritative dirservers as up. */
1219 static void
1220 mark_all_trusteddirservers_up(void)
1222 if (routerlist) {
1223 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1224 if (router_digest_is_trusted_dir(router->cache_info.identity_digest) &&
1225 router->dir_port > 0) {
1226 router->is_running = 1;
1229 if (trusted_dir_servers) {
1230 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
1232 routerstatus_t *rs;
1233 dir->is_running = 1;
1234 download_status_reset(&dir->v2_ns_dl_status);
1235 rs = router_get_consensus_status_by_id(dir->digest);
1236 if (rs && !rs->is_running) {
1237 rs->is_running = 1;
1238 rs->last_dir_503_at = 0;
1239 control_event_networkstatus_changed_single(rs);
1243 router_dir_info_changed();
1246 /** Reset all internal variables used to count failed downloads of network
1247 * status objects. */
1248 void
1249 router_reset_status_download_failures(void)
1251 mark_all_trusteddirservers_up();
1254 /** Return true iff router1 and router2 have the same /16 network. */
1255 static INLINE int
1256 routers_in_same_network_family(routerinfo_t *r1, routerinfo_t *r2)
1258 return (r1->addr & 0xffff0000) == (r2->addr & 0xffff0000);
1261 /** Look through the routerlist and identify routers that
1262 * advertise the same /16 network address as <b>router</b>.
1263 * Add each of them to <b>sl</b>.
1265 static void
1266 routerlist_add_network_family(smartlist_t *sl, routerinfo_t *router)
1268 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
1270 if (router != r && routers_in_same_network_family(router, r))
1271 smartlist_add(sl, r);
1275 /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
1276 * This is used to make sure we don't pick siblings in a single path,
1277 * or pick more than one relay from a family for our entry guard list.
1279 void
1280 routerlist_add_family(smartlist_t *sl, routerinfo_t *router)
1282 routerinfo_t *r;
1283 config_line_t *cl;
1284 or_options_t *options = get_options();
1286 /* First, add any routers with similar network addresses. */
1287 if (options->EnforceDistinctSubnets)
1288 routerlist_add_network_family(sl, router);
1290 if (router->declared_family) {
1291 /* Add every r such that router declares familyness with r, and r
1292 * declares familyhood with router. */
1293 SMARTLIST_FOREACH(router->declared_family, const char *, n,
1295 if (!(r = router_get_by_nickname(n, 0)))
1296 continue;
1297 if (!r->declared_family)
1298 continue;
1299 SMARTLIST_FOREACH(r->declared_family, const char *, n2,
1301 if (router_nickname_matches(router, n2))
1302 smartlist_add(sl, r);
1307 /* If the user declared any families locally, honor those too. */
1308 for (cl = options->NodeFamilies; cl; cl = cl->next) {
1309 if (router_nickname_is_in_list(router, cl->value)) {
1310 add_nickname_list_to_smartlist(sl, cl->value, 0);
1315 /** Return true iff r is named by some nickname in <b>lst</b>. */
1316 static INLINE int
1317 router_in_nickname_smartlist(smartlist_t *lst, routerinfo_t *r)
1319 if (!lst) return 0;
1320 SMARTLIST_FOREACH(lst, const char *, name,
1321 if (router_nickname_matches(r, name))
1322 return 1;);
1323 return 0;
1326 /** Return true iff r1 and r2 are in the same family, but not the same
1327 * router. */
1329 routers_in_same_family(routerinfo_t *r1, routerinfo_t *r2)
1331 or_options_t *options = get_options();
1332 config_line_t *cl;
1334 if (options->EnforceDistinctSubnets && routers_in_same_network_family(r1,r2))
1335 return 1;
1337 if (router_in_nickname_smartlist(r1->declared_family, r2) &&
1338 router_in_nickname_smartlist(r2->declared_family, r1))
1339 return 1;
1341 for (cl = options->NodeFamilies; cl; cl = cl->next) {
1342 if (router_nickname_is_in_list(r1, cl->value) &&
1343 router_nickname_is_in_list(r2, cl->value))
1344 return 1;
1346 return 0;
1349 /** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
1350 * see which nicknames in <b>list</b> name routers in our routerlist, and add
1351 * the routerinfos for those routers to <b>sl</b>. If <b>must_be_running</b>,
1352 * only include routers that we think are running.
1353 * Warn if any non-Named routers are specified by nickname.
1355 void
1356 add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
1357 int must_be_running)
1359 routerinfo_t *router;
1360 smartlist_t *nickname_list;
1361 int have_dir_info = router_have_minimum_dir_info();
1363 if (!list)
1364 return; /* nothing to do */
1365 tor_assert(sl);
1367 nickname_list = smartlist_create();
1368 if (!warned_nicknames)
1369 warned_nicknames = smartlist_create();
1371 smartlist_split_string(nickname_list, list, ",",
1372 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1374 SMARTLIST_FOREACH(nickname_list, const char *, nick, {
1375 int warned;
1376 if (!is_legal_nickname_or_hexdigest(nick)) {
1377 log_warn(LD_CONFIG, "Nickname '%s' is misformed; skipping", nick);
1378 continue;
1380 router = router_get_by_nickname(nick, 1);
1381 warned = smartlist_string_isin(warned_nicknames, nick);
1382 if (router) {
1383 if (!must_be_running || router->is_running) {
1384 smartlist_add(sl,router);
1386 } else if (!router_get_consensus_status_by_nickname(nick,1)) {
1387 if (!warned) {
1388 log_fn(have_dir_info ? LOG_WARN : LOG_INFO, LD_CONFIG,
1389 "Nickname list includes '%s' which isn't a known router.",nick);
1390 smartlist_add(warned_nicknames, tor_strdup(nick));
1394 SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
1395 smartlist_free(nickname_list);
1398 /** Return 1 iff any member of the (possibly NULL) comma-separated list
1399 * <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
1400 * return 0.
1403 router_nickname_is_in_list(routerinfo_t *router, const char *list)
1405 smartlist_t *nickname_list;
1406 int v = 0;
1408 if (!list)
1409 return 0; /* definitely not */
1410 tor_assert(router);
1412 nickname_list = smartlist_create();
1413 smartlist_split_string(nickname_list, list, ",",
1414 SPLIT_SKIP_SPACE|SPLIT_STRIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1415 SMARTLIST_FOREACH(nickname_list, const char *, cp,
1416 if (router_nickname_matches(router, cp)) {v=1;break;});
1417 SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
1418 smartlist_free(nickname_list);
1419 return v;
1422 /** Add every suitable router from our routerlist to <b>sl</b>, so that
1423 * we can pick a node for a circuit.
1425 static void
1426 router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_invalid,
1427 int need_uptime, int need_capacity,
1428 int need_guard)
1430 if (!routerlist)
1431 return;
1433 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1435 if (router->is_running &&
1436 router->purpose == ROUTER_PURPOSE_GENERAL &&
1437 (router->is_valid || allow_invalid) &&
1438 !router_is_unreliable(router, need_uptime,
1439 need_capacity, need_guard)) {
1440 /* If it's running, and it's suitable according to the
1441 * other flags we had in mind */
1442 smartlist_add(sl, router);
1447 /** Look through the routerlist until we find a router that has my key.
1448 Return it. */
1449 routerinfo_t *
1450 routerlist_find_my_routerinfo(void)
1452 if (!routerlist)
1453 return NULL;
1455 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1457 if (router_is_me(router))
1458 return router;
1460 return NULL;
1463 /** Find a router that's up, that has this IP address, and
1464 * that allows exit to this address:port, or return NULL if there
1465 * isn't a good one.
1467 routerinfo_t *
1468 router_find_exact_exit_enclave(const char *address, uint16_t port)
1470 uint32_t addr;
1471 struct in_addr in;
1472 tor_addr_t a;
1474 if (!tor_inet_aton(address, &in))
1475 return NULL; /* it's not an IP already */
1476 addr = ntohl(in.s_addr);
1478 tor_addr_from_ipv4h(&a, addr);
1480 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
1482 if (router->addr == addr &&
1483 router->is_running &&
1484 compare_tor_addr_to_addr_policy(&a, port, router->exit_policy) ==
1485 ADDR_POLICY_ACCEPTED)
1486 return router;
1488 return NULL;
1491 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1492 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1493 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1494 * bandwidth.
1495 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1498 router_is_unreliable(routerinfo_t *router, int need_uptime,
1499 int need_capacity, int need_guard)
1501 if (need_uptime && !router->is_stable)
1502 return 1;
1503 if (need_capacity && !router->is_fast)
1504 return 1;
1505 if (need_guard && !router->is_possible_guard)
1506 return 1;
1507 return 0;
1510 /** Return the smaller of the router's configured BandwidthRate
1511 * and its advertised capacity. */
1512 uint32_t
1513 router_get_advertised_bandwidth(routerinfo_t *router)
1515 if (router->bandwidthcapacity < router->bandwidthrate)
1516 return router->bandwidthcapacity;
1517 return router->bandwidthrate;
1520 /** Do not weight any declared bandwidth more than this much when picking
1521 * routers by bandwidth. */
1522 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
1524 /** Return the smaller of the router's configured BandwidthRate
1525 * and its advertised capacity, capped by max-believe-bw. */
1526 uint32_t
1527 router_get_advertised_bandwidth_capped(routerinfo_t *router)
1529 uint32_t result = router->bandwidthcapacity;
1530 if (result > router->bandwidthrate)
1531 result = router->bandwidthrate;
1532 if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH)
1533 result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
1534 return result;
1537 /** Return bw*1000, unless bw*1000 would overflow, in which case return
1538 * INT32_MAX. */
1539 static INLINE int32_t
1540 kb_to_bytes(uint32_t bw)
1542 return (bw > (INT32_MAX/1000)) ? INT32_MAX : bw*1000;
1545 /** Helper function:
1546 * choose a random element of smartlist <b>sl</b>, weighted by
1547 * the advertised bandwidth of each element using the consensus
1548 * bandwidth weights.
1550 * If <b>statuses</b> is zero, then <b>sl</b> is a list of
1551 * routerinfo_t's. Otherwise it's a list of routerstatus_t's.
1553 * If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
1554 * nodes' bandwidth equally regardless of their Exit status, since there may
1555 * be some in the list because they exit to obscure ports. If
1556 * <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
1557 * exit-node's bandwidth less depending on the smallness of the fraction of
1558 * Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
1559 * guard node: consider all guard's bandwidth equally. Otherwise, weight
1560 * guards proportionally less.
1562 static void *
1563 smartlist_choose_by_bandwidth_weights(smartlist_t *sl,
1564 bandwidth_weight_rule_t rule,
1565 int statuses)
1567 int64_t weight_scale;
1568 int64_t rand_bw;
1569 double Wg = -1, Wm = -1, We = -1, Wd = -1;
1570 double Wgb = -1, Wmb = -1, Web = -1, Wdb = -1;
1571 double weighted_bw = 0;
1572 double *bandwidths;
1573 double tmp = 0;
1574 unsigned int i;
1576 /* Can't choose exit and guard at same time */
1577 tor_assert(rule == NO_WEIGHTING ||
1578 rule == WEIGHT_FOR_EXIT ||
1579 rule == WEIGHT_FOR_GUARD ||
1580 rule == WEIGHT_FOR_MID ||
1581 rule == WEIGHT_FOR_DIR);
1583 if (smartlist_len(sl) == 0) {
1584 log_info(LD_CIRC,
1585 "Empty routerlist passed in to consensus weight node "
1586 "selection for rule %s",
1587 bandwidth_weight_rule_to_string(rule));
1588 return NULL;
1591 weight_scale = networkstatus_get_param(NULL, "bwweightscale",
1592 BW_WEIGHT_SCALE);
1594 if (rule == WEIGHT_FOR_GUARD) {
1595 Wg = networkstatus_get_bw_weight(NULL, "Wgg", -1);
1596 Wm = networkstatus_get_bw_weight(NULL, "Wgm", -1); /* Bridges */
1597 We = 0;
1598 Wd = networkstatus_get_bw_weight(NULL, "Wgd", -1);
1600 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
1601 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
1602 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
1603 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
1604 } else if (rule == WEIGHT_FOR_MID) {
1605 Wg = networkstatus_get_bw_weight(NULL, "Wmg", -1);
1606 Wm = networkstatus_get_bw_weight(NULL, "Wmm", -1);
1607 We = networkstatus_get_bw_weight(NULL, "Wme", -1);
1608 Wd = networkstatus_get_bw_weight(NULL, "Wmd", -1);
1610 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
1611 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
1612 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
1613 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
1614 } else if (rule == WEIGHT_FOR_EXIT) {
1615 // Guards CAN be exits if they have weird exit policies
1616 // They are d then I guess...
1617 We = networkstatus_get_bw_weight(NULL, "Wee", -1);
1618 Wm = networkstatus_get_bw_weight(NULL, "Wem", -1); /* Odd exit policies */
1619 Wd = networkstatus_get_bw_weight(NULL, "Wed", -1);
1620 Wg = networkstatus_get_bw_weight(NULL, "Weg", -1); /* Odd exit policies */
1622 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
1623 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
1624 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
1625 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
1626 } else if (rule == WEIGHT_FOR_DIR) {
1627 We = networkstatus_get_bw_weight(NULL, "Wbe", -1);
1628 Wm = networkstatus_get_bw_weight(NULL, "Wbm", -1);
1629 Wd = networkstatus_get_bw_weight(NULL, "Wbd", -1);
1630 Wg = networkstatus_get_bw_weight(NULL, "Wbg", -1);
1632 Wgb = Wmb = Web = Wdb = weight_scale;
1633 } else if (rule == NO_WEIGHTING) {
1634 Wg = Wm = We = Wd = weight_scale;
1635 Wgb = Wmb = Web = Wdb = weight_scale;
1638 if (Wg < 0 || Wm < 0 || We < 0 || Wd < 0 || Wgb < 0 || Wmb < 0 || Wdb < 0
1639 || Web < 0) {
1640 log_debug(LD_CIRC,
1641 "Got negative bandwidth weights. Defaulting to old selection"
1642 " algorithm.");
1643 return NULL; // Use old algorithm.
1646 Wg /= weight_scale;
1647 Wm /= weight_scale;
1648 We /= weight_scale;
1649 Wd /= weight_scale;
1651 Wgb /= weight_scale;
1652 Wmb /= weight_scale;
1653 Web /= weight_scale;
1654 Wdb /= weight_scale;
1656 bandwidths = tor_malloc_zero(sizeof(double)*smartlist_len(sl));
1658 // Cycle through smartlist and total the bandwidth.
1659 for (i = 0; i < (unsigned)smartlist_len(sl); ++i) {
1660 int is_exit = 0, is_guard = 0, is_dir = 0, this_bw = 0, is_me = 0;
1661 double weight = 1;
1662 if (statuses) {
1663 routerstatus_t *status = smartlist_get(sl, i);
1664 is_exit = status->is_exit;
1665 is_guard = status->is_possible_guard;
1666 is_dir = (status->dir_port != 0);
1667 if (!status->has_bandwidth) {
1668 tor_free(bandwidths);
1669 /* This should never happen, unless all the authorites downgrade
1670 * to 0.2.0 or rogue routerstatuses get inserted into our consensus. */
1671 log_warn(LD_BUG,
1672 "Consensus is not listing bandwidths. Defaulting back to "
1673 "old router selection algorithm.");
1674 return NULL;
1676 this_bw = kb_to_bytes(status->bandwidth);
1677 if (router_digest_is_me(status->identity_digest))
1678 is_me = 1;
1679 } else {
1680 routerstatus_t *rs;
1681 routerinfo_t *router = smartlist_get(sl, i);
1682 rs = router_get_consensus_status_by_id(
1683 router->cache_info.identity_digest);
1684 is_exit = router->is_exit;
1685 is_guard = router->is_possible_guard;
1686 is_dir = (router->dir_port != 0);
1687 if (rs && rs->has_bandwidth) {
1688 this_bw = kb_to_bytes(rs->bandwidth);
1689 } else { /* bridge or other descriptor not in our consensus */
1690 this_bw = router_get_advertised_bandwidth_capped(router);
1692 if (router_digest_is_me(router->cache_info.identity_digest))
1693 is_me = 1;
1695 if (is_guard && is_exit) {
1696 weight = (is_dir ? Wdb*Wd : Wd);
1697 } else if (is_guard) {
1698 weight = (is_dir ? Wgb*Wg : Wg);
1699 } else if (is_exit) {
1700 weight = (is_dir ? Web*We : We);
1701 } else { // middle
1702 weight = (is_dir ? Wmb*Wm : Wm);
1705 bandwidths[i] = weight*this_bw;
1706 weighted_bw += weight*this_bw;
1707 if (is_me)
1708 sl_last_weighted_bw_of_me = weight*this_bw;
1711 /* XXXX022 this is a kludge to expose these values. */
1712 sl_last_total_weighted_bw = weighted_bw;
1714 log_debug(LD_CIRC, "Choosing node for rule %s based on weights "
1715 "Wg=%lf Wm=%lf We=%lf Wd=%lf with total bw %lf",
1716 bandwidth_weight_rule_to_string(rule),
1717 Wg, Wm, We, Wd, weighted_bw);
1719 /* If there is no bandwidth, choose at random */
1720 if (DBL_TO_U64(weighted_bw) == 0) {
1721 log_warn(LD_CIRC,
1722 "Weighted bandwidth is %lf in node selection for rule %s",
1723 weighted_bw, bandwidth_weight_rule_to_string(rule));
1724 tor_free(bandwidths);
1725 return smartlist_choose(sl);
1728 rand_bw = crypto_rand_uint64(DBL_TO_U64(weighted_bw));
1729 rand_bw++; /* crypto_rand_uint64() counts from 0, and we need to count
1730 * from 1 below. See bug 1203 for details. */
1732 /* Last, count through sl until we get to the element we picked */
1733 tmp = 0.0;
1734 for (i=0; i < (unsigned)smartlist_len(sl); i++) {
1735 tmp += bandwidths[i];
1736 if (tmp >= rand_bw)
1737 break;
1740 if (i == (unsigned)smartlist_len(sl)) {
1741 /* This was once possible due to round-off error, but shouldn't be able
1742 * to occur any longer. */
1743 tor_fragile_assert();
1744 --i;
1745 log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
1746 " which router we chose. Please tell the developers. "
1747 "%lf " U64_FORMAT " %lf", tmp, U64_PRINTF_ARG(rand_bw),
1748 weighted_bw);
1750 tor_free(bandwidths);
1751 return smartlist_get(sl, i);
1754 /** Helper function:
1755 * choose a random element of smartlist <b>sl</b>, weighted by
1756 * the advertised bandwidth of each element.
1758 * If <b>statuses</b> is zero, then <b>sl</b> is a list of
1759 * routerinfo_t's. Otherwise it's a list of routerstatus_t's.
1761 * If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
1762 * nodes' bandwidth equally regardless of their Exit status, since there may
1763 * be some in the list because they exit to obscure ports. If
1764 * <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
1765 * exit-node's bandwidth less depending on the smallness of the fraction of
1766 * Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
1767 * guard node: consider all guard's bandwidth equally. Otherwise, weight
1768 * guards proportionally less.
1770 static void *
1771 smartlist_choose_by_bandwidth(smartlist_t *sl, bandwidth_weight_rule_t rule,
1772 int statuses)
1774 unsigned int i;
1775 routerinfo_t *router;
1776 routerstatus_t *status=NULL;
1777 int32_t *bandwidths;
1778 int is_exit;
1779 int is_guard;
1780 uint64_t total_nonexit_bw = 0, total_exit_bw = 0, total_bw = 0;
1781 uint64_t total_nonguard_bw = 0, total_guard_bw = 0;
1782 uint64_t rand_bw, tmp;
1783 double exit_weight;
1784 double guard_weight;
1785 int n_unknown = 0;
1786 bitarray_t *exit_bits;
1787 bitarray_t *guard_bits;
1788 int me_idx = -1;
1790 // This function does not support WEIGHT_FOR_DIR
1791 // or WEIGHT_FOR_MID
1792 if (rule == WEIGHT_FOR_DIR || rule == WEIGHT_FOR_MID) {
1793 rule = NO_WEIGHTING;
1796 /* Can't choose exit and guard at same time */
1797 tor_assert(rule == NO_WEIGHTING ||
1798 rule == WEIGHT_FOR_EXIT ||
1799 rule == WEIGHT_FOR_GUARD);
1801 if (smartlist_len(sl) == 0) {
1802 log_info(LD_CIRC,
1803 "Empty routerlist passed in to old node selection for rule %s",
1804 bandwidth_weight_rule_to_string(rule));
1805 return NULL;
1808 /* First count the total bandwidth weight, and make a list
1809 * of each value. <0 means "unknown; no routerinfo." We use the
1810 * bits of negative values to remember whether the router was fast (-x)&1
1811 * and whether it was an exit (-x)&2 or guard (-x)&4. Yes, it's a hack. */
1812 bandwidths = tor_malloc(sizeof(int32_t)*smartlist_len(sl));
1813 exit_bits = bitarray_init_zero(smartlist_len(sl));
1814 guard_bits = bitarray_init_zero(smartlist_len(sl));
1816 /* Iterate over all the routerinfo_t or routerstatus_t, and */
1817 for (i = 0; i < (unsigned)smartlist_len(sl); ++i) {
1818 /* first, learn what bandwidth we think i has */
1819 int is_known = 1;
1820 int32_t flags = 0;
1821 uint32_t this_bw = 0;
1822 if (statuses) {
1823 status = smartlist_get(sl, i);
1824 if (router_digest_is_me(status->identity_digest))
1825 me_idx = i;
1826 router = router_get_by_digest(status->identity_digest);
1827 is_exit = status->is_exit;
1828 is_guard = status->is_possible_guard;
1829 if (status->has_bandwidth) {
1830 this_bw = kb_to_bytes(status->bandwidth);
1831 } else { /* guess */
1832 /* XXX022 once consensuses always list bandwidths, we can take
1833 * this guessing business out. -RD */
1834 is_known = 0;
1835 flags = status->is_fast ? 1 : 0;
1836 flags |= is_exit ? 2 : 0;
1837 flags |= is_guard ? 4 : 0;
1839 } else {
1840 routerstatus_t *rs;
1841 router = smartlist_get(sl, i);
1842 rs = router_get_consensus_status_by_id(
1843 router->cache_info.identity_digest);
1844 if (router_digest_is_me(router->cache_info.identity_digest))
1845 me_idx = i;
1846 is_exit = router->is_exit;
1847 is_guard = router->is_possible_guard;
1848 if (rs && rs->has_bandwidth) {
1849 this_bw = kb_to_bytes(rs->bandwidth);
1850 } else if (rs) { /* guess; don't trust the descriptor */
1851 /* XXX022 once consensuses always list bandwidths, we can take
1852 * this guessing business out. -RD */
1853 is_known = 0;
1854 flags = router->is_fast ? 1 : 0;
1855 flags |= is_exit ? 2 : 0;
1856 flags |= is_guard ? 4 : 0;
1857 } else /* bridge or other descriptor not in our consensus */
1858 this_bw = router_get_advertised_bandwidth_capped(router);
1860 if (is_exit)
1861 bitarray_set(exit_bits, i);
1862 if (is_guard)
1863 bitarray_set(guard_bits, i);
1864 if (is_known) {
1865 bandwidths[i] = (int32_t) this_bw; // safe since MAX_BELIEVABLE<INT32_MAX
1866 tor_assert(bandwidths[i] >= 0);
1867 if (is_guard)
1868 total_guard_bw += this_bw;
1869 else
1870 total_nonguard_bw += this_bw;
1871 if (is_exit)
1872 total_exit_bw += this_bw;
1873 else
1874 total_nonexit_bw += this_bw;
1875 } else {
1876 ++n_unknown;
1877 bandwidths[i] = -flags;
1881 /* Now, fill in the unknown values. */
1882 if (n_unknown) {
1883 int32_t avg_fast, avg_slow;
1884 if (total_exit_bw+total_nonexit_bw) {
1885 /* if there's some bandwidth, there's at least one known router,
1886 * so no worries about div by 0 here */
1887 int n_known = smartlist_len(sl)-n_unknown;
1888 avg_fast = avg_slow = (int32_t)
1889 ((total_exit_bw+total_nonexit_bw)/((uint64_t) n_known));
1890 } else {
1891 avg_fast = 40000;
1892 avg_slow = 20000;
1894 for (i=0; i<(unsigned)smartlist_len(sl); ++i) {
1895 int32_t bw = bandwidths[i];
1896 if (bw>=0)
1897 continue;
1898 is_exit = ((-bw)&2);
1899 is_guard = ((-bw)&4);
1900 bandwidths[i] = ((-bw)&1) ? avg_fast : avg_slow;
1901 if (is_exit)
1902 total_exit_bw += bandwidths[i];
1903 else
1904 total_nonexit_bw += bandwidths[i];
1905 if (is_guard)
1906 total_guard_bw += bandwidths[i];
1907 else
1908 total_nonguard_bw += bandwidths[i];
1912 /* If there's no bandwidth at all, pick at random. */
1913 if (!(total_exit_bw+total_nonexit_bw)) {
1914 tor_free(bandwidths);
1915 tor_free(exit_bits);
1916 tor_free(guard_bits);
1917 return smartlist_choose(sl);
1920 /* Figure out how to weight exits and guards */
1922 double all_bw = U64_TO_DBL(total_exit_bw+total_nonexit_bw);
1923 double exit_bw = U64_TO_DBL(total_exit_bw);
1924 double guard_bw = U64_TO_DBL(total_guard_bw);
1926 * For detailed derivation of this formula, see
1927 * http://archives.seul.org/or/dev/Jul-2007/msg00056.html
1929 if (rule == WEIGHT_FOR_EXIT || !total_exit_bw)
1930 exit_weight = 1.0;
1931 else
1932 exit_weight = 1.0 - all_bw/(3.0*exit_bw);
1934 if (rule == WEIGHT_FOR_GUARD || !total_guard_bw)
1935 guard_weight = 1.0;
1936 else
1937 guard_weight = 1.0 - all_bw/(3.0*guard_bw);
1939 if (exit_weight <= 0.0)
1940 exit_weight = 0.0;
1942 if (guard_weight <= 0.0)
1943 guard_weight = 0.0;
1945 total_bw = 0;
1946 sl_last_weighted_bw_of_me = 0;
1947 for (i=0; i < (unsigned)smartlist_len(sl); i++) {
1948 uint64_t bw;
1949 is_exit = bitarray_is_set(exit_bits, i);
1950 is_guard = bitarray_is_set(guard_bits, i);
1951 if (is_exit && is_guard)
1952 bw = ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
1953 else if (is_guard)
1954 bw = ((uint64_t)(bandwidths[i] * guard_weight));
1955 else if (is_exit)
1956 bw = ((uint64_t)(bandwidths[i] * exit_weight));
1957 else
1958 bw = bandwidths[i];
1959 total_bw += bw;
1960 if (i == (unsigned) me_idx)
1961 sl_last_weighted_bw_of_me = bw;
1965 /* XXXX022 this is a kludge to expose these values. */
1966 sl_last_total_weighted_bw = total_bw;
1968 log_debug(LD_CIRC, "Total weighted bw = "U64_FORMAT
1969 ", exit bw = "U64_FORMAT
1970 ", nonexit bw = "U64_FORMAT", exit weight = %lf "
1971 "(for exit == %d)"
1972 ", guard bw = "U64_FORMAT
1973 ", nonguard bw = "U64_FORMAT", guard weight = %lf "
1974 "(for guard == %d)",
1975 U64_PRINTF_ARG(total_bw),
1976 U64_PRINTF_ARG(total_exit_bw), U64_PRINTF_ARG(total_nonexit_bw),
1977 exit_weight, (int)(rule == WEIGHT_FOR_EXIT),
1978 U64_PRINTF_ARG(total_guard_bw), U64_PRINTF_ARG(total_nonguard_bw),
1979 guard_weight, (int)(rule == WEIGHT_FOR_GUARD));
1981 /* Almost done: choose a random value from the bandwidth weights. */
1982 rand_bw = crypto_rand_uint64(total_bw);
1983 rand_bw++; /* crypto_rand_uint64() counts from 0, and we need to count
1984 * from 1 below. See bug 1203 for details. */
1986 /* Last, count through sl until we get to the element we picked */
1987 tmp = 0;
1988 for (i=0; i < (unsigned)smartlist_len(sl); i++) {
1989 is_exit = bitarray_is_set(exit_bits, i);
1990 is_guard = bitarray_is_set(guard_bits, i);
1992 /* Weights can be 0 if not counting guards/exits */
1993 if (is_exit && is_guard)
1994 tmp += ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
1995 else if (is_guard)
1996 tmp += ((uint64_t)(bandwidths[i] * guard_weight));
1997 else if (is_exit)
1998 tmp += ((uint64_t)(bandwidths[i] * exit_weight));
1999 else
2000 tmp += bandwidths[i];
2002 if (tmp >= rand_bw)
2003 break;
2005 if (i == (unsigned)smartlist_len(sl)) {
2006 /* This was once possible due to round-off error, but shouldn't be able
2007 * to occur any longer. */
2008 tor_fragile_assert();
2009 --i;
2010 log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
2011 " which router we chose. Please tell the developers. "
2012 U64_FORMAT " " U64_FORMAT " " U64_FORMAT, U64_PRINTF_ARG(tmp),
2013 U64_PRINTF_ARG(rand_bw), U64_PRINTF_ARG(total_bw));
2015 tor_free(bandwidths);
2016 tor_free(exit_bits);
2017 tor_free(guard_bits);
2018 return smartlist_get(sl, i);
2021 /** Choose a random element of router list <b>sl</b>, weighted by
2022 * the advertised bandwidth of each router.
2024 routerinfo_t *
2025 routerlist_sl_choose_by_bandwidth(smartlist_t *sl,
2026 bandwidth_weight_rule_t rule)
2028 routerinfo_t *ret;
2029 if ((ret = smartlist_choose_by_bandwidth_weights(sl, rule, 0))) {
2030 return ret;
2031 } else {
2032 return smartlist_choose_by_bandwidth(sl, rule, 0);
2036 /** Choose a random element of status list <b>sl</b>, weighted by
2037 * the advertised bandwidth of each status.
2039 routerstatus_t *
2040 routerstatus_sl_choose_by_bandwidth(smartlist_t *sl,
2041 bandwidth_weight_rule_t rule)
2043 /* We are choosing neither exit nor guard here. Weight accordingly. */
2044 routerstatus_t *ret;
2045 if ((ret = smartlist_choose_by_bandwidth_weights(sl, rule, 1))) {
2046 return ret;
2047 } else {
2048 return smartlist_choose_by_bandwidth(sl, rule, 1);
2052 /** Return a random running router from the routerlist. Never
2053 * pick a node whose routerinfo is in
2054 * <b>excludedsmartlist</b>, or whose routerinfo matches <b>excludedset</b>,
2055 * even if they are the only nodes available.
2056 * If <b>CRN_NEED_UPTIME</b> is set in flags and any router has more than
2057 * a minimum uptime, return one of those.
2058 * If <b>CRN_NEED_CAPACITY</b> is set in flags, weight your choice by the
2059 * advertised capacity of each router.
2060 * If <b>CRN_ALLOW_INVALID</b> is not set in flags, consider only Valid
2061 * routers.
2062 * If <b>CRN_NEED_GUARD</b> is set in flags, consider only Guard routers.
2063 * If <b>CRN_WEIGHT_AS_EXIT</b> is set in flags, we weight bandwidths as if
2064 * picking an exit node, otherwise we weight bandwidths for picking a relay
2065 * node (that is, possibly discounting exit nodes).
2067 routerinfo_t *
2068 router_choose_random_node(smartlist_t *excludedsmartlist,
2069 routerset_t *excludedset,
2070 router_crn_flags_t flags)
2072 const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
2073 const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
2074 const int need_guard = (flags & CRN_NEED_GUARD) != 0;
2075 const int allow_invalid = (flags & CRN_ALLOW_INVALID) != 0;
2076 const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
2078 smartlist_t *sl=smartlist_create(),
2079 *excludednodes=smartlist_create();
2080 routerinfo_t *choice = NULL, *r;
2081 bandwidth_weight_rule_t rule;
2083 tor_assert(!(weight_for_exit && need_guard));
2084 rule = weight_for_exit ? WEIGHT_FOR_EXIT :
2085 (need_guard ? WEIGHT_FOR_GUARD : WEIGHT_FOR_MID);
2087 /* Exclude relays that allow single hop exit circuits, if the user
2088 * wants to (such relays might be risky) */
2089 if (get_options()->ExcludeSingleHopRelays) {
2090 routerlist_t *rl = router_get_routerlist();
2091 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
2092 if (r->allow_single_hop_exits) {
2093 smartlist_add(excludednodes, r);
2097 if ((r = routerlist_find_my_routerinfo())) {
2098 smartlist_add(excludednodes, r);
2099 routerlist_add_family(excludednodes, r);
2102 router_add_running_routers_to_smartlist(sl, allow_invalid,
2103 need_uptime, need_capacity,
2104 need_guard);
2105 smartlist_subtract(sl,excludednodes);
2106 if (excludedsmartlist)
2107 smartlist_subtract(sl,excludedsmartlist);
2108 if (excludedset)
2109 routerset_subtract_routers(sl,excludedset);
2111 // Always weight by bandwidth
2112 choice = routerlist_sl_choose_by_bandwidth(sl, rule);
2114 smartlist_free(sl);
2115 if (!choice && (need_uptime || need_capacity || need_guard)) {
2116 /* try once more -- recurse but with fewer restrictions. */
2117 log_info(LD_CIRC,
2118 "We couldn't find any live%s%s%s routers; falling back "
2119 "to list of all routers.",
2120 need_capacity?", fast":"",
2121 need_uptime?", stable":"",
2122 need_guard?", guard":"");
2123 flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD);
2124 choice = router_choose_random_node(
2125 excludedsmartlist, excludedset, flags);
2127 smartlist_free(excludednodes);
2128 if (!choice) {
2129 log_warn(LD_CIRC,
2130 "No available nodes when trying to choose node. Failing.");
2132 return choice;
2135 /** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
2136 * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
2137 * (which is optionally prefixed with a single dollar sign). Return false if
2138 * <b>hexdigest</b> is malformed, or it doesn't match. */
2139 static INLINE int
2140 hex_digest_matches(const char *hexdigest, const char *identity_digest,
2141 const char *nickname, int is_named)
2143 char digest[DIGEST_LEN];
2144 size_t len;
2145 tor_assert(hexdigest);
2146 if (hexdigest[0] == '$')
2147 ++hexdigest;
2149 len = strlen(hexdigest);
2150 if (len < HEX_DIGEST_LEN)
2151 return 0;
2152 else if (len > HEX_DIGEST_LEN &&
2153 (hexdigest[HEX_DIGEST_LEN] == '=' ||
2154 hexdigest[HEX_DIGEST_LEN] == '~')) {
2155 if (strcasecmp(hexdigest+HEX_DIGEST_LEN+1, nickname))
2156 return 0;
2157 if (hexdigest[HEX_DIGEST_LEN] == '=' && !is_named)
2158 return 0;
2161 if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
2162 return 0;
2163 return (!memcmp(digest, identity_digest, DIGEST_LEN));
2166 /** Return true iff the digest of <b>router</b>'s identity key,
2167 * encoded in hexadecimal, matches <b>hexdigest</b> (which is
2168 * optionally prefixed with a single dollar sign). Return false if
2169 * <b>hexdigest</b> is malformed, or it doesn't match. */
2170 static INLINE int
2171 router_hex_digest_matches(routerinfo_t *router, const char *hexdigest)
2173 return hex_digest_matches(hexdigest, router->cache_info.identity_digest,
2174 router->nickname, router->is_named);
2177 /** Return true if <b>router</b>'s nickname matches <b>nickname</b>
2178 * (case-insensitive), or if <b>router's</b> identity key digest
2179 * matches a hexadecimal value stored in <b>nickname</b>. Return
2180 * false otherwise. */
2181 static int
2182 router_nickname_matches(routerinfo_t *router, const char *nickname)
2184 if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
2185 return 1;
2186 return router_hex_digest_matches(router, nickname);
2189 /** Return the router in our routerlist whose (case-insensitive)
2190 * nickname or (case-sensitive) hexadecimal key digest is
2191 * <b>nickname</b>. Return NULL if no such router is known.
2193 routerinfo_t *
2194 router_get_by_nickname(const char *nickname, int warn_if_unnamed)
2196 int maybedigest;
2197 char digest[DIGEST_LEN];
2198 routerinfo_t *best_match=NULL;
2199 int n_matches = 0;
2200 const char *named_digest = NULL;
2202 tor_assert(nickname);
2203 if (!routerlist)
2204 return NULL;
2205 if (nickname[0] == '$')
2206 return router_get_by_hexdigest(nickname);
2207 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
2208 return NULL;
2209 if (server_mode(get_options()) &&
2210 !strcasecmp(nickname, get_options()->Nickname))
2211 return router_get_my_routerinfo();
2213 maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
2214 (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
2216 if ((named_digest = networkstatus_get_router_digest_by_nickname(nickname))) {
2217 return rimap_get(routerlist->identity_map, named_digest);
2219 if (networkstatus_nickname_is_unnamed(nickname))
2220 return NULL;
2222 /* If we reach this point, there's no canonical value for the nickname. */
2224 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
2226 if (!strcasecmp(router->nickname, nickname)) {
2227 ++n_matches;
2228 if (n_matches <= 1 || router->is_running)
2229 best_match = router;
2230 } else if (maybedigest &&
2231 !memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN)
2233 if (router_hex_digest_matches(router, nickname))
2234 return router;
2235 /* If we reach this point, we have a ID=name syntax that matches the
2236 * identity but not the name. That isn't an acceptable match. */
2240 if (best_match) {
2241 if (warn_if_unnamed && n_matches > 1) {
2242 smartlist_t *fps = smartlist_create();
2243 int any_unwarned = 0;
2244 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
2246 routerstatus_t *rs;
2247 char *desc;
2248 size_t dlen;
2249 char fp[HEX_DIGEST_LEN+1];
2250 if (strcasecmp(router->nickname, nickname))
2251 continue;
2252 rs = router_get_consensus_status_by_id(
2253 router->cache_info.identity_digest);
2254 if (rs && !rs->name_lookup_warned) {
2255 rs->name_lookup_warned = 1;
2256 any_unwarned = 1;
2258 base16_encode(fp, sizeof(fp),
2259 router->cache_info.identity_digest, DIGEST_LEN);
2260 dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
2261 desc = tor_malloc(dlen);
2262 tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
2263 fp, router->address, router->or_port);
2264 smartlist_add(fps, desc);
2266 if (any_unwarned) {
2267 char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
2268 log_warn(LD_CONFIG,
2269 "There are multiple matches for the nickname \"%s\","
2270 " but none is listed as named by the directory authorities. "
2271 "Choosing one arbitrarily. If you meant one in particular, "
2272 "you should say %s.", nickname, alternatives);
2273 tor_free(alternatives);
2275 SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
2276 smartlist_free(fps);
2277 } else if (warn_if_unnamed) {
2278 routerstatus_t *rs = router_get_consensus_status_by_id(
2279 best_match->cache_info.identity_digest);
2280 if (rs && !rs->name_lookup_warned) {
2281 char fp[HEX_DIGEST_LEN+1];
2282 base16_encode(fp, sizeof(fp),
2283 best_match->cache_info.identity_digest, DIGEST_LEN);
2284 log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
2285 "name is not registered, so it could be used by any server, "
2286 "not just the one you meant. "
2287 "To make sure you get the same server in the future, refer to "
2288 "it by key, as \"$%s\".", nickname, fp);
2289 rs->name_lookup_warned = 1;
2292 return best_match;
2295 return NULL;
2298 /** Try to find a routerinfo for <b>digest</b>. If we don't have one,
2299 * return 1. If we do, ask tor_version_as_new_as() for the answer.
2302 router_digest_version_as_new_as(const char *digest, const char *cutoff)
2304 routerinfo_t *router = router_get_by_digest(digest);
2305 if (!router)
2306 return 1;
2307 return tor_version_as_new_as(router->platform, cutoff);
2310 /** Return true iff <b>digest</b> is the digest of the identity key of a
2311 * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
2312 * is zero, any authority is okay. */
2314 router_digest_is_trusted_dir_type(const char *digest, authority_type_t type)
2316 if (!trusted_dir_servers)
2317 return 0;
2318 if (authdir_mode(get_options()) && router_digest_is_me(digest))
2319 return 1;
2320 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
2321 if (!memcmp(digest, ent->digest, DIGEST_LEN)) {
2322 return (!type) || ((type & ent->type) != 0);
2324 return 0;
2327 /** Return true iff <b>addr</b> is the address of one of our trusted
2328 * directory authorities. */
2330 router_addr_is_trusted_dir(uint32_t addr)
2332 if (!trusted_dir_servers)
2333 return 0;
2334 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
2335 if (ent->addr == addr)
2336 return 1;
2338 return 0;
2341 /** If hexdigest is correctly formed, base16_decode it into
2342 * digest, which must have DIGEST_LEN space in it.
2343 * Return 0 on success, -1 on failure.
2346 hexdigest_to_digest(const char *hexdigest, char *digest)
2348 if (hexdigest[0]=='$')
2349 ++hexdigest;
2350 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
2351 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
2352 return -1;
2353 return 0;
2356 /** Return the router in our routerlist whose hexadecimal key digest
2357 * is <b>hexdigest</b>. Return NULL if no such router is known. */
2358 routerinfo_t *
2359 router_get_by_hexdigest(const char *hexdigest)
2361 char digest[DIGEST_LEN];
2362 size_t len;
2363 routerinfo_t *ri;
2365 tor_assert(hexdigest);
2366 if (!routerlist)
2367 return NULL;
2368 if (hexdigest[0]=='$')
2369 ++hexdigest;
2370 len = strlen(hexdigest);
2371 if (hexdigest_to_digest(hexdigest, digest) < 0)
2372 return NULL;
2374 ri = router_get_by_digest(digest);
2376 if (ri && len > HEX_DIGEST_LEN) {
2377 if (hexdigest[HEX_DIGEST_LEN] == '=') {
2378 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1) ||
2379 !ri->is_named)
2380 return NULL;
2381 } else if (hexdigest[HEX_DIGEST_LEN] == '~') {
2382 if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1))
2383 return NULL;
2384 } else {
2385 return NULL;
2389 return ri;
2392 /** Return the router in our routerlist whose 20-byte key digest
2393 * is <b>digest</b>. Return NULL if no such router is known. */
2394 routerinfo_t *
2395 router_get_by_digest(const char *digest)
2397 tor_assert(digest);
2399 if (!routerlist) return NULL;
2401 // routerlist_assert_ok(routerlist);
2403 return rimap_get(routerlist->identity_map, digest);
2406 /** Return the router in our routerlist whose 20-byte descriptor
2407 * is <b>digest</b>. Return NULL if no such router is known. */
2408 signed_descriptor_t *
2409 router_get_by_descriptor_digest(const char *digest)
2411 tor_assert(digest);
2413 if (!routerlist) return NULL;
2415 return sdmap_get(routerlist->desc_digest_map, digest);
2418 /** Return the signed descriptor for the router in our routerlist whose
2419 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
2420 * is known. */
2421 signed_descriptor_t *
2422 router_get_by_extrainfo_digest(const char *digest)
2424 tor_assert(digest);
2426 if (!routerlist) return NULL;
2428 return sdmap_get(routerlist->desc_by_eid_map, digest);
2431 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
2432 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
2433 * document is known. */
2434 signed_descriptor_t *
2435 extrainfo_get_by_descriptor_digest(const char *digest)
2437 extrainfo_t *ei;
2438 tor_assert(digest);
2439 if (!routerlist) return NULL;
2440 ei = eimap_get(routerlist->extra_info_map, digest);
2441 return ei ? &ei->cache_info : NULL;
2444 /** Return a pointer to the signed textual representation of a descriptor.
2445 * The returned string is not guaranteed to be NUL-terminated: the string's
2446 * length will be in desc-\>signed_descriptor_len.
2448 * If <b>with_annotations</b> is set, the returned string will include
2449 * the annotations
2450 * (if any) preceding the descriptor. This will increase the length of the
2451 * string by desc-\>annotations_len.
2453 * The caller must not free the string returned.
2455 static const char *
2456 signed_descriptor_get_body_impl(signed_descriptor_t *desc,
2457 int with_annotations)
2459 const char *r = NULL;
2460 size_t len = desc->signed_descriptor_len;
2461 off_t offset = desc->saved_offset;
2462 if (with_annotations)
2463 len += desc->annotations_len;
2464 else
2465 offset += desc->annotations_len;
2467 tor_assert(len > 32);
2468 if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
2469 desc_store_t *store = desc_get_store(router_get_routerlist(), desc);
2470 if (store && store->mmap) {
2471 tor_assert(desc->saved_offset + len <= store->mmap->size);
2472 r = store->mmap->data + offset;
2473 } else if (store) {
2474 log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
2475 "mmaped in our cache. Is another process running in our data "
2476 "directory? Exiting.");
2477 exit(1);
2480 if (!r) /* no mmap, or not in cache. */
2481 r = desc->signed_descriptor_body +
2482 (with_annotations ? 0 : desc->annotations_len);
2484 tor_assert(r);
2485 if (!with_annotations) {
2486 if (memcmp("router ", r, 7) && memcmp("extra-info ", r, 11)) {
2487 char *cp = tor_strndup(r, 64);
2488 log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
2489 "Is another process running in our data directory? Exiting.",
2490 desc, escaped(cp));
2491 exit(1);
2495 return r;
2498 /** Return a pointer to the signed textual representation of a descriptor.
2499 * The returned string is not guaranteed to be NUL-terminated: the string's
2500 * length will be in desc-\>signed_descriptor_len.
2502 * The caller must not free the string returned.
2504 const char *
2505 signed_descriptor_get_body(signed_descriptor_t *desc)
2507 return signed_descriptor_get_body_impl(desc, 0);
2510 /** As signed_descriptor_get_body(), but points to the beginning of the
2511 * annotations section rather than the beginning of the descriptor. */
2512 const char *
2513 signed_descriptor_get_annotations(signed_descriptor_t *desc)
2515 return signed_descriptor_get_body_impl(desc, 1);
2518 /** Return the current list of all known routers. */
2519 routerlist_t *
2520 router_get_routerlist(void)
2522 if (PREDICT_UNLIKELY(!routerlist)) {
2523 routerlist = tor_malloc_zero(sizeof(routerlist_t));
2524 routerlist->routers = smartlist_create();
2525 routerlist->old_routers = smartlist_create();
2526 routerlist->identity_map = rimap_new();
2527 routerlist->desc_digest_map = sdmap_new();
2528 routerlist->desc_by_eid_map = sdmap_new();
2529 routerlist->extra_info_map = eimap_new();
2531 routerlist->desc_store.fname_base = "cached-descriptors";
2532 routerlist->desc_store.fname_alt_base = "cached-routers";
2533 routerlist->extrainfo_store.fname_base = "cached-extrainfo";
2535 routerlist->desc_store.type = ROUTER_STORE;
2536 routerlist->extrainfo_store.type = EXTRAINFO_STORE;
2538 routerlist->desc_store.description = "router descriptors";
2539 routerlist->extrainfo_store.description = "extra-info documents";
2541 return routerlist;
2544 /** Free all storage held by <b>router</b>. */
2545 void
2546 routerinfo_free(routerinfo_t *router)
2548 if (!router)
2549 return;
2551 tor_free(router->cache_info.signed_descriptor_body);
2552 tor_free(router->address);
2553 tor_free(router->nickname);
2554 tor_free(router->platform);
2555 tor_free(router->contact_info);
2556 if (router->onion_pkey)
2557 crypto_free_pk_env(router->onion_pkey);
2558 if (router->identity_pkey)
2559 crypto_free_pk_env(router->identity_pkey);
2560 if (router->declared_family) {
2561 SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
2562 smartlist_free(router->declared_family);
2564 addr_policy_list_free(router->exit_policy);
2566 /* XXXX Remove if this turns out to affect performance. */
2567 memset(router, 77, sizeof(routerinfo_t));
2569 tor_free(router);
2572 /** Release all storage held by <b>extrainfo</b> */
2573 void
2574 extrainfo_free(extrainfo_t *extrainfo)
2576 if (!extrainfo)
2577 return;
2578 tor_free(extrainfo->cache_info.signed_descriptor_body);
2579 tor_free(extrainfo->pending_sig);
2581 /* XXXX remove this if it turns out to slow us down. */
2582 memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
2583 tor_free(extrainfo);
2586 /** Release storage held by <b>sd</b>. */
2587 static void
2588 signed_descriptor_free(signed_descriptor_t *sd)
2590 if (!sd)
2591 return;
2593 tor_free(sd->signed_descriptor_body);
2595 /* XXXX remove this once more bugs go away. */
2596 memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
2597 tor_free(sd);
2600 /** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
2602 static signed_descriptor_t *
2603 signed_descriptor_from_routerinfo(routerinfo_t *ri)
2605 signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t));
2606 memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t));
2607 sd->routerlist_index = -1;
2608 ri->cache_info.signed_descriptor_body = NULL;
2609 routerinfo_free(ri);
2610 return sd;
2613 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
2614 static void
2615 _extrainfo_free(void *e)
2617 extrainfo_free(e);
2620 /** Free all storage held by a routerlist <b>rl</b>. */
2621 void
2622 routerlist_free(routerlist_t *rl)
2624 if (!rl)
2625 return;
2626 rimap_free(rl->identity_map, NULL);
2627 sdmap_free(rl->desc_digest_map, NULL);
2628 sdmap_free(rl->desc_by_eid_map, NULL);
2629 eimap_free(rl->extra_info_map, _extrainfo_free);
2630 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
2631 routerinfo_free(r));
2632 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
2633 signed_descriptor_free(sd));
2634 smartlist_free(rl->routers);
2635 smartlist_free(rl->old_routers);
2636 if (routerlist->desc_store.mmap)
2637 tor_munmap_file(routerlist->desc_store.mmap);
2638 if (routerlist->extrainfo_store.mmap)
2639 tor_munmap_file(routerlist->extrainfo_store.mmap);
2640 tor_free(rl);
2642 router_dir_info_changed();
2645 /** Log information about how much memory is being used for routerlist,
2646 * at log level <b>severity</b>. */
2647 void
2648 dump_routerlist_mem_usage(int severity)
2650 uint64_t livedescs = 0;
2651 uint64_t olddescs = 0;
2652 if (!routerlist)
2653 return;
2654 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
2655 livedescs += r->cache_info.signed_descriptor_len);
2656 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
2657 olddescs += sd->signed_descriptor_len);
2659 log(severity, LD_DIR,
2660 "In %d live descriptors: "U64_FORMAT" bytes. "
2661 "In %d old descriptors: "U64_FORMAT" bytes.",
2662 smartlist_len(routerlist->routers), U64_PRINTF_ARG(livedescs),
2663 smartlist_len(routerlist->old_routers), U64_PRINTF_ARG(olddescs));
2666 /** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
2667 * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
2668 * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
2669 * is not in <b>sl</b>. */
2670 static INLINE int
2671 _routerlist_find_elt(smartlist_t *sl, void *ri, int idx)
2673 if (idx < 0) {
2674 idx = -1;
2675 SMARTLIST_FOREACH(sl, routerinfo_t *, r,
2676 if (r == ri) {
2677 idx = r_sl_idx;
2678 break;
2680 } else {
2681 tor_assert(idx < smartlist_len(sl));
2682 tor_assert(smartlist_get(sl, idx) == ri);
2684 return idx;
2687 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
2688 * as needed. There must be no previous member of <b>rl</b> with the same
2689 * identity digest as <b>ri</b>: If there is, call routerlist_replace
2690 * instead.
2692 static void
2693 routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
2695 routerinfo_t *ri_old;
2697 /* XXXX Remove if this slows us down. */
2698 routerinfo_t *ri_generated = router_get_my_routerinfo();
2699 tor_assert(ri_generated != ri);
2701 tor_assert(ri->cache_info.routerlist_index == -1);
2703 ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
2704 tor_assert(!ri_old);
2705 sdmap_set(rl->desc_digest_map, ri->cache_info.signed_descriptor_digest,
2706 &(ri->cache_info));
2707 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
2708 sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
2709 &ri->cache_info);
2710 smartlist_add(rl->routers, ri);
2711 ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
2712 router_dir_info_changed();
2713 #ifdef DEBUG_ROUTERLIST
2714 routerlist_assert_ok(rl);
2715 #endif
2718 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
2719 * corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
2720 * we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
2721 static int
2722 extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
2724 int r = 0;
2725 routerinfo_t *ri = rimap_get(rl->identity_map,
2726 ei->cache_info.identity_digest);
2727 signed_descriptor_t *sd =
2728 sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
2729 extrainfo_t *ei_tmp;
2732 /* XXXX remove this code if it slows us down. */
2733 extrainfo_t *ei_generated = router_get_my_extrainfo();
2734 tor_assert(ei_generated != ei);
2737 if (!ri) {
2738 /* This router is unknown; we can't even verify the signature. Give up.*/
2739 goto done;
2741 if (routerinfo_incompatible_with_extrainfo(ri, ei, sd, NULL)) {
2742 goto done;
2745 /* Okay, if we make it here, we definitely have a router corresponding to
2746 * this extrainfo. */
2748 ei_tmp = eimap_set(rl->extra_info_map,
2749 ei->cache_info.signed_descriptor_digest,
2750 ei);
2751 r = 1;
2752 if (ei_tmp) {
2753 rl->extrainfo_store.bytes_dropped +=
2754 ei_tmp->cache_info.signed_descriptor_len;
2755 extrainfo_free(ei_tmp);
2758 done:
2759 if (r == 0)
2760 extrainfo_free(ei);
2762 #ifdef DEBUG_ROUTERLIST
2763 routerlist_assert_ok(rl);
2764 #endif
2765 return r;
2768 #define should_cache_old_descriptors() \
2769 directory_caches_dir_info(get_options())
2771 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
2772 * a copy of router <b>ri</b> yet, add it to the list of old (not
2773 * recommended but still served) descriptors. Else free it. */
2774 static void
2775 routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
2778 /* XXXX remove this code if it slows us down. */
2779 routerinfo_t *ri_generated = router_get_my_routerinfo();
2780 tor_assert(ri_generated != ri);
2782 tor_assert(ri->cache_info.routerlist_index == -1);
2784 if (should_cache_old_descriptors() &&
2785 ri->purpose == ROUTER_PURPOSE_GENERAL &&
2786 !sdmap_get(rl->desc_digest_map,
2787 ri->cache_info.signed_descriptor_digest)) {
2788 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri);
2789 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2790 smartlist_add(rl->old_routers, sd);
2791 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2792 if (!tor_digest_is_zero(sd->extra_info_digest))
2793 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2794 } else {
2795 routerinfo_free(ri);
2797 #ifdef DEBUG_ROUTERLIST
2798 routerlist_assert_ok(rl);
2799 #endif
2802 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
2803 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl-&gt;routers,
2804 * idx) == ri, we don't need to do a linear search over the list to decide
2805 * which to remove. We fill the gap in rl-&gt;routers with a later element in
2806 * the list, if any exists. <b>ri</b> is freed.
2808 * If <b>make_old</b> is true, instead of deleting the router, we try adding
2809 * it to rl-&gt;old_routers. */
2810 void
2811 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
2813 routerinfo_t *ri_tmp;
2814 extrainfo_t *ei_tmp;
2815 int idx = ri->cache_info.routerlist_index;
2816 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2817 tor_assert(smartlist_get(rl->routers, idx) == ri);
2819 /* make sure the rephist module knows that it's not running */
2820 rep_hist_note_router_unreachable(ri->cache_info.identity_digest, now);
2822 ri->cache_info.routerlist_index = -1;
2823 smartlist_del(rl->routers, idx);
2824 if (idx < smartlist_len(rl->routers)) {
2825 routerinfo_t *r = smartlist_get(rl->routers, idx);
2826 r->cache_info.routerlist_index = idx;
2829 ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
2830 router_dir_info_changed();
2831 tor_assert(ri_tmp == ri);
2833 if (make_old && should_cache_old_descriptors() &&
2834 ri->purpose == ROUTER_PURPOSE_GENERAL) {
2835 signed_descriptor_t *sd;
2836 sd = signed_descriptor_from_routerinfo(ri);
2837 smartlist_add(rl->old_routers, sd);
2838 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2839 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2840 if (!tor_digest_is_zero(sd->extra_info_digest))
2841 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2842 } else {
2843 signed_descriptor_t *sd_tmp;
2844 sd_tmp = sdmap_remove(rl->desc_digest_map,
2845 ri->cache_info.signed_descriptor_digest);
2846 tor_assert(sd_tmp == &(ri->cache_info));
2847 rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
2848 ei_tmp = eimap_remove(rl->extra_info_map,
2849 ri->cache_info.extra_info_digest);
2850 if (ei_tmp) {
2851 rl->extrainfo_store.bytes_dropped +=
2852 ei_tmp->cache_info.signed_descriptor_len;
2853 extrainfo_free(ei_tmp);
2855 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
2856 sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
2857 routerinfo_free(ri);
2859 #ifdef DEBUG_ROUTERLIST
2860 routerlist_assert_ok(rl);
2861 #endif
2864 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
2865 * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
2866 * <b>sd</b>. */
2867 static void
2868 routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
2870 signed_descriptor_t *sd_tmp;
2871 extrainfo_t *ei_tmp;
2872 desc_store_t *store;
2873 if (idx == -1) {
2874 idx = sd->routerlist_index;
2876 tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
2877 /* XXXX edmanm's bridge relay triggered the following assert while
2878 * running 0.2.0.12-alpha. If anybody triggers this again, see if we
2879 * can get a backtrace. */
2880 tor_assert(smartlist_get(rl->old_routers, idx) == sd);
2881 tor_assert(idx == sd->routerlist_index);
2883 sd->routerlist_index = -1;
2884 smartlist_del(rl->old_routers, idx);
2885 if (idx < smartlist_len(rl->old_routers)) {
2886 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
2887 d->routerlist_index = idx;
2889 sd_tmp = sdmap_remove(rl->desc_digest_map,
2890 sd->signed_descriptor_digest);
2891 tor_assert(sd_tmp == sd);
2892 store = desc_get_store(rl, sd);
2893 if (store)
2894 store->bytes_dropped += sd->signed_descriptor_len;
2896 ei_tmp = eimap_remove(rl->extra_info_map,
2897 sd->extra_info_digest);
2898 if (ei_tmp) {
2899 rl->extrainfo_store.bytes_dropped +=
2900 ei_tmp->cache_info.signed_descriptor_len;
2901 extrainfo_free(ei_tmp);
2903 if (!tor_digest_is_zero(sd->extra_info_digest))
2904 sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
2906 signed_descriptor_free(sd);
2907 #ifdef DEBUG_ROUTERLIST
2908 routerlist_assert_ok(rl);
2909 #endif
2912 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
2913 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
2914 * smartlist_get(rl-&gt;routers, idx) == ri, we don't need to do a linear
2915 * search over the list to decide which to remove. We put ri_new in the same
2916 * index as ri_old, if possible. ri is freed as appropriate.
2918 * If should_cache_descriptors() is true, instead of deleting the router,
2919 * we add it to rl-&gt;old_routers. */
2920 static void
2921 routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old,
2922 routerinfo_t *ri_new)
2924 int idx;
2926 routerinfo_t *ri_tmp;
2927 extrainfo_t *ei_tmp;
2929 /* XXXX Remove this if it turns out to slow us down. */
2930 routerinfo_t *ri_generated = router_get_my_routerinfo();
2931 tor_assert(ri_generated != ri_new);
2933 tor_assert(ri_old != ri_new);
2934 tor_assert(ri_new->cache_info.routerlist_index == -1);
2936 idx = ri_old->cache_info.routerlist_index;
2937 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
2938 tor_assert(smartlist_get(rl->routers, idx) == ri_old);
2940 router_dir_info_changed();
2941 if (idx >= 0) {
2942 smartlist_set(rl->routers, idx, ri_new);
2943 ri_old->cache_info.routerlist_index = -1;
2944 ri_new->cache_info.routerlist_index = idx;
2945 /* Check that ri_old is not in rl->routers anymore: */
2946 tor_assert( _routerlist_find_elt(rl->routers, ri_old, -1) == -1 );
2947 } else {
2948 log_warn(LD_BUG, "Appending entry from routerlist_replace.");
2949 routerlist_insert(rl, ri_new);
2950 return;
2952 if (memcmp(ri_old->cache_info.identity_digest,
2953 ri_new->cache_info.identity_digest, DIGEST_LEN)) {
2954 /* digests don't match; digestmap_set won't replace */
2955 rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
2957 ri_tmp = rimap_set(rl->identity_map,
2958 ri_new->cache_info.identity_digest, ri_new);
2959 tor_assert(!ri_tmp || ri_tmp == ri_old);
2960 sdmap_set(rl->desc_digest_map,
2961 ri_new->cache_info.signed_descriptor_digest,
2962 &(ri_new->cache_info));
2964 if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
2965 sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
2966 &ri_new->cache_info);
2969 if (should_cache_old_descriptors() &&
2970 ri_old->purpose == ROUTER_PURPOSE_GENERAL) {
2971 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
2972 smartlist_add(rl->old_routers, sd);
2973 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
2974 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
2975 if (!tor_digest_is_zero(sd->extra_info_digest))
2976 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
2977 } else {
2978 if (memcmp(ri_old->cache_info.signed_descriptor_digest,
2979 ri_new->cache_info.signed_descriptor_digest,
2980 DIGEST_LEN)) {
2981 /* digests don't match; digestmap_set didn't replace */
2982 sdmap_remove(rl->desc_digest_map,
2983 ri_old->cache_info.signed_descriptor_digest);
2986 ei_tmp = eimap_remove(rl->extra_info_map,
2987 ri_old->cache_info.extra_info_digest);
2988 if (ei_tmp) {
2989 rl->extrainfo_store.bytes_dropped +=
2990 ei_tmp->cache_info.signed_descriptor_len;
2991 extrainfo_free(ei_tmp);
2993 if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
2994 sdmap_remove(rl->desc_by_eid_map,
2995 ri_old->cache_info.extra_info_digest);
2997 rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
2998 routerinfo_free(ri_old);
3000 #ifdef DEBUG_ROUTERLIST
3001 routerlist_assert_ok(rl);
3002 #endif
3005 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
3006 * it as a fresh routerinfo_t. */
3007 static routerinfo_t *
3008 routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
3010 routerinfo_t *ri;
3011 const char *body;
3013 body = signed_descriptor_get_annotations(sd);
3015 ri = router_parse_entry_from_string(body,
3016 body+sd->signed_descriptor_len+sd->annotations_len,
3017 0, 1, NULL);
3018 if (!ri)
3019 return NULL;
3020 memcpy(&ri->cache_info, sd, sizeof(signed_descriptor_t));
3021 sd->signed_descriptor_body = NULL; /* Steal reference. */
3022 ri->cache_info.routerlist_index = -1;
3024 routerlist_remove_old(rl, sd, -1);
3026 return ri;
3029 /** Free all memory held by the routerlist module. */
3030 void
3031 routerlist_free_all(void)
3033 routerlist_free(routerlist);
3034 routerlist = NULL;
3035 if (warned_nicknames) {
3036 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
3037 smartlist_free(warned_nicknames);
3038 warned_nicknames = NULL;
3040 if (trusted_dir_servers) {
3041 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
3042 trusted_dir_server_free(ds));
3043 smartlist_free(trusted_dir_servers);
3044 trusted_dir_servers = NULL;
3046 if (trusted_dir_certs) {
3047 DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
3048 SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
3049 authority_cert_free(cert));
3050 smartlist_free(cl->certs);
3051 tor_free(cl);
3052 } DIGESTMAP_FOREACH_END;
3053 digestmap_free(trusted_dir_certs, NULL);
3054 trusted_dir_certs = NULL;
3058 /** Forget that we have issued any router-related warnings, so that we'll
3059 * warn again if we see the same errors. */
3060 void
3061 routerlist_reset_warnings(void)
3063 if (!warned_nicknames)
3064 warned_nicknames = smartlist_create();
3065 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
3066 smartlist_clear(warned_nicknames); /* now the list is empty. */
3068 networkstatus_reset_warnings();
3071 /** Mark the router with ID <b>digest</b> as running or non-running
3072 * in our routerlist. */
3073 void
3074 router_set_status(const char *digest, int up)
3076 routerinfo_t *router;
3077 routerstatus_t *status;
3078 tor_assert(digest);
3080 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
3081 if (!memcmp(d->digest, digest, DIGEST_LEN))
3082 d->is_running = up);
3084 router = router_get_by_digest(digest);
3085 if (router) {
3086 log_debug(LD_DIR,"Marking router '%s/%s' as %s.",
3087 router->nickname, router->address, up ? "up" : "down");
3088 if (!up && router_is_me(router) && !we_are_hibernating())
3089 log_warn(LD_NET, "We just marked ourself as down. Are your external "
3090 "addresses reachable?");
3091 router->is_running = up;
3093 status = router_get_consensus_status_by_id(digest);
3094 if (status && status->is_running != up) {
3095 status->is_running = up;
3096 control_event_networkstatus_changed_single(status);
3098 router_dir_info_changed();
3101 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
3102 * older entries (if any) with the same key. Note: Callers should not hold
3103 * their pointers to <b>router</b> if this function fails; <b>router</b>
3104 * will either be inserted into the routerlist or freed. Similarly, even
3105 * if this call succeeds, they should not hold their pointers to
3106 * <b>router</b> after subsequent calls with other routerinfo's -- they
3107 * might cause the original routerinfo to get freed.
3109 * Returns the status for the operation. Might set *<b>msg</b> if it wants
3110 * the poster of the router to know something.
3112 * If <b>from_cache</b>, this descriptor came from our disk cache. If
3113 * <b>from_fetch</b>, we received it in response to a request we made.
3114 * (If both are false, that means it was uploaded to us as an auth dir
3115 * server or via the controller.)
3117 * This function should be called *after*
3118 * routers_update_status_from_consensus_networkstatus; subsequently, you
3119 * should call router_rebuild_store and routerlist_descriptors_added.
3121 was_router_added_t
3122 router_add_to_routerlist(routerinfo_t *router, const char **msg,
3123 int from_cache, int from_fetch)
3125 const char *id_digest;
3126 int authdir = authdir_mode_handles_descs(get_options(), router->purpose);
3127 int authdir_believes_valid = 0;
3128 routerinfo_t *old_router;
3129 networkstatus_t *consensus = networkstatus_get_latest_consensus();
3130 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3131 int in_consensus = 0;
3133 tor_assert(msg);
3135 if (!routerlist)
3136 router_get_routerlist();
3138 id_digest = router->cache_info.identity_digest;
3140 /* Make sure that we haven't already got this exact descriptor. */
3141 if (sdmap_get(routerlist->desc_digest_map,
3142 router->cache_info.signed_descriptor_digest)) {
3143 log_info(LD_DIR,
3144 "Dropping descriptor that we already have for router '%s'",
3145 router->nickname);
3146 *msg = "Router descriptor was not new.";
3147 routerinfo_free(router);
3148 return ROUTER_WAS_NOT_NEW;
3151 if (authdir) {
3152 if (authdir_wants_to_reject_router(router, msg,
3153 !from_cache && !from_fetch)) {
3154 tor_assert(*msg);
3155 routerinfo_free(router);
3156 return ROUTER_AUTHDIR_REJECTS;
3158 authdir_believes_valid = router->is_valid;
3159 } else if (from_fetch) {
3160 /* Only check the descriptor digest against the network statuses when
3161 * we are receiving in response to a fetch. */
3163 if (!signed_desc_digest_is_recognized(&router->cache_info) &&
3164 !routerinfo_is_a_configured_bridge(router)) {
3165 /* We asked for it, so some networkstatus must have listed it when we
3166 * did. Save it if we're a cache in case somebody else asks for it. */
3167 log_info(LD_DIR,
3168 "Received a no-longer-recognized descriptor for router '%s'",
3169 router->nickname);
3170 *msg = "Router descriptor is not referenced by any network-status.";
3172 /* Only journal this desc if we'll be serving it. */
3173 if (!from_cache && should_cache_old_descriptors())
3174 signed_desc_append_to_journal(&router->cache_info,
3175 &routerlist->desc_store);
3176 routerlist_insert_old(routerlist, router);
3177 return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
3181 /* We no longer need a router with this descriptor digest. */
3182 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3184 routerstatus_t *rs =
3185 networkstatus_v2_find_entry(ns, router->cache_info.identity_digest);
3186 if (rs && !memcmp(rs->descriptor_digest,
3187 router->cache_info.signed_descriptor_digest,
3188 DIGEST_LEN))
3189 rs->need_to_mirror = 0;
3191 if (consensus) {
3192 routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
3193 router->cache_info.identity_digest);
3194 if (rs && !memcmp(rs->descriptor_digest,
3195 router->cache_info.signed_descriptor_digest,
3196 DIGEST_LEN)) {
3197 in_consensus = 1;
3198 rs->need_to_mirror = 0;
3202 if (router->purpose == ROUTER_PURPOSE_GENERAL &&
3203 consensus && !in_consensus && !authdir) {
3204 /* If it's a general router not listed in the consensus, then don't
3205 * consider replacing the latest router with it. */
3206 if (!from_cache && should_cache_old_descriptors())
3207 signed_desc_append_to_journal(&router->cache_info,
3208 &routerlist->desc_store);
3209 routerlist_insert_old(routerlist, router);
3210 *msg = "Skipping router descriptor: not in consensus.";
3211 return ROUTER_NOT_IN_CONSENSUS;
3214 /* If we have a router with the same identity key, choose the newer one. */
3215 old_router = rimap_get(routerlist->identity_map,
3216 router->cache_info.identity_digest);
3217 if (old_router) {
3218 if (!in_consensus && (router->cache_info.published_on <=
3219 old_router->cache_info.published_on)) {
3220 /* Same key, but old. This one is not listed in the consensus. */
3221 log_debug(LD_DIR, "Not-new descriptor for router '%s'",
3222 router->nickname);
3223 /* Only journal this desc if we'll be serving it. */
3224 if (!from_cache && should_cache_old_descriptors())
3225 signed_desc_append_to_journal(&router->cache_info,
3226 &routerlist->desc_store);
3227 routerlist_insert_old(routerlist, router);
3228 *msg = "Router descriptor was not new.";
3229 return ROUTER_WAS_NOT_NEW;
3230 } else {
3231 /* Same key, and either new, or listed in the consensus. */
3232 log_debug(LD_DIR, "Replacing entry for router '%s/%s' [%s]",
3233 router->nickname, old_router->nickname,
3234 hex_str(id_digest,DIGEST_LEN));
3235 if (router->addr == old_router->addr &&
3236 router->or_port == old_router->or_port) {
3237 /* these carry over when the address and orport are unchanged. */
3238 router->last_reachable = old_router->last_reachable;
3239 router->testing_since = old_router->testing_since;
3241 routerlist_replace(routerlist, old_router, router);
3242 if (!from_cache) {
3243 signed_desc_append_to_journal(&router->cache_info,
3244 &routerlist->desc_store);
3246 directory_set_dirty();
3247 *msg = authdir_believes_valid ? "Valid server updated" :
3248 ("Invalid server updated. (This dirserver is marking your "
3249 "server as unapproved.)");
3250 return ROUTER_ADDED_SUCCESSFULLY;
3254 if (!in_consensus && from_cache &&
3255 router->cache_info.published_on < time(NULL) - OLD_ROUTER_DESC_MAX_AGE) {
3256 *msg = "Router descriptor was really old.";
3257 routerinfo_free(router);
3258 return ROUTER_WAS_NOT_NEW;
3261 /* We haven't seen a router with this identity before. Add it to the end of
3262 * the list. */
3263 routerlist_insert(routerlist, router);
3264 if (!from_cache) {
3265 if (authdir) {
3266 /* launch an immediate reachability test, so we will have an opinion
3267 * soon in case we're generating a consensus soon */
3268 dirserv_single_reachability_test(time(NULL), router);
3270 signed_desc_append_to_journal(&router->cache_info,
3271 &routerlist->desc_store);
3273 directory_set_dirty();
3274 return ROUTER_ADDED_SUCCESSFULLY;
3277 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
3278 * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
3279 * we actually inserted it, ROUTER_BAD_EI otherwise.
3281 was_router_added_t
3282 router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg,
3283 int from_cache, int from_fetch)
3285 int inserted;
3286 (void)from_fetch;
3287 if (msg) *msg = NULL;
3288 /*XXXX022 Do something with msg */
3290 inserted = extrainfo_insert(router_get_routerlist(), ei);
3292 if (inserted && !from_cache)
3293 signed_desc_append_to_journal(&ei->cache_info,
3294 &routerlist->extrainfo_store);
3296 if (inserted)
3297 return ROUTER_ADDED_SUCCESSFULLY;
3298 else
3299 return ROUTER_BAD_EI;
3302 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
3303 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
3304 * to, or later than that of *<b>b</b>. */
3305 static int
3306 _compare_old_routers_by_identity(const void **_a, const void **_b)
3308 int i;
3309 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
3310 if ((i = memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
3311 return i;
3312 return (int)(r1->published_on - r2->published_on);
3315 /** Internal type used to represent how long an old descriptor was valid,
3316 * where it appeared in the list of old descriptors, and whether it's extra
3317 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
3318 struct duration_idx_t {
3319 int duration;
3320 int idx;
3321 int old;
3324 /** Sorting helper: compare two duration_idx_t by their duration. */
3325 static int
3326 _compare_duration_idx(const void *_d1, const void *_d2)
3328 const struct duration_idx_t *d1 = _d1;
3329 const struct duration_idx_t *d2 = _d2;
3330 return d1->duration - d2->duration;
3333 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
3334 * must contain routerinfo_t with the same identity and with publication time
3335 * in ascending order. Remove members from this range until there are no more
3336 * than max_descriptors_per_router() remaining. Start by removing the oldest
3337 * members from before <b>cutoff</b>, then remove members which were current
3338 * for the lowest amount of time. The order of members of old_routers at
3339 * indices <b>lo</b> or higher may be changed.
3341 static void
3342 routerlist_remove_old_cached_routers_with_id(time_t now,
3343 time_t cutoff, int lo, int hi,
3344 digestset_t *retain)
3346 int i, n = hi-lo+1;
3347 unsigned n_extra, n_rmv = 0;
3348 struct duration_idx_t *lifespans;
3349 uint8_t *rmv, *must_keep;
3350 smartlist_t *lst = routerlist->old_routers;
3351 #if 1
3352 const char *ident;
3353 tor_assert(hi < smartlist_len(lst));
3354 tor_assert(lo <= hi);
3355 ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
3356 for (i = lo+1; i <= hi; ++i) {
3357 signed_descriptor_t *r = smartlist_get(lst, i);
3358 tor_assert(!memcmp(ident, r->identity_digest, DIGEST_LEN));
3360 #endif
3361 /* Check whether we need to do anything at all. */
3363 int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
3364 if (n <= mdpr)
3365 return;
3366 n_extra = n - mdpr;
3369 lifespans = tor_malloc_zero(sizeof(struct duration_idx_t)*n);
3370 rmv = tor_malloc_zero(sizeof(uint8_t)*n);
3371 must_keep = tor_malloc_zero(sizeof(uint8_t)*n);
3372 /* Set lifespans to contain the lifespan and index of each server. */
3373 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
3374 for (i = lo; i <= hi; ++i) {
3375 signed_descriptor_t *r = smartlist_get(lst, i);
3376 signed_descriptor_t *r_next;
3377 lifespans[i-lo].idx = i;
3378 if (r->last_listed_as_valid_until >= now ||
3379 (retain && digestset_isin(retain, r->signed_descriptor_digest))) {
3380 must_keep[i-lo] = 1;
3382 if (i < hi) {
3383 r_next = smartlist_get(lst, i+1);
3384 tor_assert(r->published_on <= r_next->published_on);
3385 lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
3386 } else {
3387 r_next = NULL;
3388 lifespans[i-lo].duration = INT_MAX;
3390 if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
3391 ++n_rmv;
3392 lifespans[i-lo].old = 1;
3393 rmv[i-lo] = 1;
3397 if (n_rmv < n_extra) {
3399 * We aren't removing enough servers for being old. Sort lifespans by
3400 * the duration of liveness, and remove the ones we're not already going to
3401 * remove based on how long they were alive.
3403 qsort(lifespans, n, sizeof(struct duration_idx_t), _compare_duration_idx);
3404 for (i = 0; i < n && n_rmv < n_extra; ++i) {
3405 if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
3406 rmv[lifespans[i].idx-lo] = 1;
3407 ++n_rmv;
3412 i = hi;
3413 do {
3414 if (rmv[i-lo])
3415 routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
3416 } while (--i >= lo);
3417 tor_free(must_keep);
3418 tor_free(rmv);
3419 tor_free(lifespans);
3422 /** Deactivate any routers from the routerlist that are more than
3423 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
3424 * remove old routers from the list of cached routers if we have too many.
3426 void
3427 routerlist_remove_old_routers(void)
3429 int i, hi=-1;
3430 const char *cur_id = NULL;
3431 time_t now = time(NULL);
3432 time_t cutoff;
3433 routerinfo_t *router;
3434 signed_descriptor_t *sd;
3435 digestset_t *retain;
3436 int caches = directory_caches_dir_info(get_options());
3437 const networkstatus_t *consensus = networkstatus_get_latest_consensus();
3438 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3439 int have_enough_v2;
3441 trusted_dirs_remove_old_certs();
3443 if (!routerlist || !consensus)
3444 return;
3446 // routerlist_assert_ok(routerlist);
3448 /* We need to guess how many router descriptors we will wind up wanting to
3449 retain, so that we can be sure to allocate a large enough Bloom filter
3450 to hold the digest set. Overestimating is fine; underestimating is bad.
3453 /* We'll probably retain everything in the consensus. */
3454 int n_max_retain = smartlist_len(consensus->routerstatus_list);
3455 if (caches && networkstatus_v2_list) {
3456 /* If we care about v2 statuses, we'll retain at most as many as are
3457 listed any of the v2 statues. This will be at least the length of
3458 the largest v2 networkstatus, and in the worst case, this set will be
3459 equal to the sum of the lengths of all v2 consensuses. Take the
3460 worst case.
3462 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3463 n_max_retain += smartlist_len(ns->entries));
3465 retain = digestset_new(n_max_retain);
3468 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
3469 /* Build a list of all the descriptors that _anybody_ lists. */
3470 if (caches && networkstatus_v2_list) {
3471 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3473 /* XXXX The inner loop here gets pretty expensive, and actually shows up
3474 * on some profiles. It may be the reason digestmap_set shows up in
3475 * profiles too. If instead we kept a per-descriptor digest count of
3476 * how many networkstatuses recommended each descriptor, and changed
3477 * that only when the networkstatuses changed, that would be a speed
3478 * improvement, possibly 1-4% if it also removes digestmap_set from the
3479 * profile. Not worth it for 0.1.2.x, though. The new directory
3480 * system will obsolete this whole thing in 0.2.0.x. */
3481 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
3482 if (rs->published_on >= cutoff)
3483 digestset_add(retain, rs->descriptor_digest));
3487 /* Retain anything listed in the consensus. */
3488 if (consensus) {
3489 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
3490 if (rs->published_on >= cutoff)
3491 digestset_add(retain, rs->descriptor_digest));
3494 /* If we have a consensus, and nearly as many v2 networkstatuses as we want,
3495 * we should consider pruning current routers that are too old and that
3496 * nobody recommends. (If we don't have a consensus or enough v2
3497 * networkstatuses, then we should get more before we decide to kill
3498 * routers.) */
3499 /* we set this to true iff we don't care about v2 info, or we have enough. */
3500 have_enough_v2 = !caches ||
3501 (networkstatus_v2_list &&
3502 smartlist_len(networkstatus_v2_list) > get_n_v2_authorities() / 2);
3504 if (have_enough_v2 && consensus) {
3505 cutoff = now - ROUTER_MAX_AGE;
3506 /* Remove too-old unrecommended members of routerlist->routers. */
3507 for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
3508 router = smartlist_get(routerlist->routers, i);
3509 if (router->cache_info.published_on <= cutoff &&
3510 router->cache_info.last_listed_as_valid_until < now &&
3511 !digestset_isin(retain,
3512 router->cache_info.signed_descriptor_digest)) {
3513 /* Too old: remove it. (If we're a cache, just move it into
3514 * old_routers.) */
3515 log_info(LD_DIR,
3516 "Forgetting obsolete (too old) routerinfo for router '%s'",
3517 router->nickname);
3518 routerlist_remove(routerlist, router, 1, now);
3519 i--;
3524 //routerlist_assert_ok(routerlist);
3526 /* Remove far-too-old members of routerlist->old_routers. */
3527 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
3528 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
3529 sd = smartlist_get(routerlist->old_routers, i);
3530 if (sd->published_on <= cutoff &&
3531 sd->last_listed_as_valid_until < now &&
3532 !digestset_isin(retain, sd->signed_descriptor_digest)) {
3533 /* Too old. Remove it. */
3534 routerlist_remove_old(routerlist, sd, i--);
3538 //routerlist_assert_ok(routerlist);
3540 log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
3541 smartlist_len(routerlist->routers),
3542 smartlist_len(routerlist->old_routers));
3544 /* Now we might have to look at routerlist->old_routers for extraneous
3545 * members. (We'd keep all the members if we could, but we need to save
3546 * space.) First, check whether we have too many router descriptors, total.
3547 * We're okay with having too many for some given router, so long as the
3548 * total number doesn't approach max_descriptors_per_router()*len(router).
3550 if (smartlist_len(routerlist->old_routers) <
3551 smartlist_len(routerlist->routers))
3552 goto done;
3554 /* Sort by identity, then fix indices. */
3555 smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
3556 /* Fix indices. */
3557 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
3558 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
3559 r->routerlist_index = i;
3562 /* Iterate through the list from back to front, so when we remove descriptors
3563 * we don't mess up groups we haven't gotten to. */
3564 for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
3565 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
3566 if (!cur_id) {
3567 cur_id = r->identity_digest;
3568 hi = i;
3570 if (memcmp(cur_id, r->identity_digest, DIGEST_LEN)) {
3571 routerlist_remove_old_cached_routers_with_id(now,
3572 cutoff, i+1, hi, retain);
3573 cur_id = r->identity_digest;
3574 hi = i;
3577 if (hi>=0)
3578 routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
3579 //routerlist_assert_ok(routerlist);
3581 done:
3582 digestset_free(retain);
3583 router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
3584 router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
3587 /** We just added a new set of descriptors. Take whatever extra steps
3588 * we need. */
3589 static void
3590 routerlist_descriptors_added(smartlist_t *sl, int from_cache)
3592 tor_assert(sl);
3593 control_event_descriptors_changed(sl);
3594 SMARTLIST_FOREACH(sl, routerinfo_t *, ri,
3595 if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
3596 learned_bridge_descriptor(ri, from_cache);
3601 * Code to parse a single router descriptor and insert it into the
3602 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
3603 * descriptor was well-formed but could not be added; and 1 if the
3604 * descriptor was added.
3606 * If we don't add it and <b>msg</b> is not NULL, then assign to
3607 * *<b>msg</b> a static string describing the reason for refusing the
3608 * descriptor.
3610 * This is used only by the controller.
3613 router_load_single_router(const char *s, uint8_t purpose, int cache,
3614 const char **msg)
3616 routerinfo_t *ri;
3617 was_router_added_t r;
3618 smartlist_t *lst;
3619 char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
3620 tor_assert(msg);
3621 *msg = NULL;
3623 tor_snprintf(annotation_buf, sizeof(annotation_buf),
3624 "@source controller\n"
3625 "@purpose %s\n", router_purpose_to_string(purpose));
3627 if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0, annotation_buf))) {
3628 log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
3629 *msg = "Couldn't parse router descriptor.";
3630 return -1;
3632 tor_assert(ri->purpose == purpose);
3633 if (router_is_me(ri)) {
3634 log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
3635 *msg = "Router's identity key matches mine.";
3636 routerinfo_free(ri);
3637 return 0;
3640 if (!cache) /* obey the preference of the controller */
3641 ri->cache_info.do_not_cache = 1;
3643 lst = smartlist_create();
3644 smartlist_add(lst, ri);
3645 routers_update_status_from_consensus_networkstatus(lst, 0);
3647 r = router_add_to_routerlist(ri, msg, 0, 0);
3648 if (!WRA_WAS_ADDED(r)) {
3649 /* we've already assigned to *msg now, and ri is already freed */
3650 tor_assert(*msg);
3651 if (r == ROUTER_AUTHDIR_REJECTS)
3652 log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
3653 smartlist_free(lst);
3654 return 0;
3655 } else {
3656 routerlist_descriptors_added(lst, 0);
3657 smartlist_free(lst);
3658 log_debug(LD_DIR, "Added router to list");
3659 return 1;
3663 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
3664 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
3665 * are in response to a query to the network: cache them by adding them to
3666 * the journal.
3668 * Return the number of routers actually added.
3670 * If <b>requested_fingerprints</b> is provided, it must contain a list of
3671 * uppercased fingerprints. Do not update any router whose
3672 * fingerprint is not on the list; after updating a router, remove its
3673 * fingerprint from the list.
3675 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
3676 * are descriptor digests. Otherwise they are identity digests.
3679 router_load_routers_from_string(const char *s, const char *eos,
3680 saved_location_t saved_location,
3681 smartlist_t *requested_fingerprints,
3682 int descriptor_digests,
3683 const char *prepend_annotations)
3685 smartlist_t *routers = smartlist_create(), *changed = smartlist_create();
3686 char fp[HEX_DIGEST_LEN+1];
3687 const char *msg;
3688 int from_cache = (saved_location != SAVED_NOWHERE);
3689 int allow_annotations = (saved_location != SAVED_NOWHERE);
3690 int any_changed = 0;
3692 router_parse_list_from_string(&s, eos, routers, saved_location, 0,
3693 allow_annotations, prepend_annotations);
3695 routers_update_status_from_consensus_networkstatus(routers, !from_cache);
3697 log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
3699 SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
3700 was_router_added_t r;
3701 char d[DIGEST_LEN];
3702 if (requested_fingerprints) {
3703 base16_encode(fp, sizeof(fp), descriptor_digests ?
3704 ri->cache_info.signed_descriptor_digest :
3705 ri->cache_info.identity_digest,
3706 DIGEST_LEN);
3707 if (smartlist_string_isin(requested_fingerprints, fp)) {
3708 smartlist_string_remove(requested_fingerprints, fp);
3709 } else {
3710 char *requested =
3711 smartlist_join_strings(requested_fingerprints," ",0,NULL);
3712 log_warn(LD_DIR,
3713 "We received a router descriptor with a fingerprint (%s) "
3714 "that we never requested. (We asked for: %s.) Dropping.",
3715 fp, requested);
3716 tor_free(requested);
3717 routerinfo_free(ri);
3718 continue;
3722 memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
3723 r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
3724 if (WRA_WAS_ADDED(r)) {
3725 any_changed++;
3726 smartlist_add(changed, ri);
3727 routerlist_descriptors_added(changed, from_cache);
3728 smartlist_clear(changed);
3729 } else if (WRA_WAS_REJECTED(r)) {
3730 download_status_t *dl_status;
3731 dl_status = router_get_dl_status_by_descriptor_digest(d);
3732 if (dl_status) {
3733 log_info(LD_GENERAL, "Marking router %s as never downloadable",
3734 hex_str(d, DIGEST_LEN));
3735 download_status_mark_impossible(dl_status);
3738 } SMARTLIST_FOREACH_END(ri);
3740 routerlist_assert_ok(routerlist);
3742 if (any_changed)
3743 router_rebuild_store(0, &routerlist->desc_store);
3745 smartlist_free(routers);
3746 smartlist_free(changed);
3748 return any_changed;
3751 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
3752 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
3753 * router_load_routers_from_string(). */
3754 void
3755 router_load_extrainfo_from_string(const char *s, const char *eos,
3756 saved_location_t saved_location,
3757 smartlist_t *requested_fingerprints,
3758 int descriptor_digests)
3760 smartlist_t *extrainfo_list = smartlist_create();
3761 const char *msg;
3762 int from_cache = (saved_location != SAVED_NOWHERE);
3764 router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
3765 NULL);
3767 log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
3769 SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
3770 was_router_added_t added =
3771 router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
3772 if (WRA_WAS_ADDED(added) && requested_fingerprints) {
3773 char fp[HEX_DIGEST_LEN+1];
3774 base16_encode(fp, sizeof(fp), descriptor_digests ?
3775 ei->cache_info.signed_descriptor_digest :
3776 ei->cache_info.identity_digest,
3777 DIGEST_LEN);
3778 smartlist_string_remove(requested_fingerprints, fp);
3779 /* We silently let people stuff us with extrainfos we didn't ask for,
3780 * so long as we would have wanted them anyway. Since we always fetch
3781 * all the extrainfos we want, and we never actually act on them
3782 * inside Tor, this should be harmless. */
3786 routerlist_assert_ok(routerlist);
3787 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
3789 smartlist_free(extrainfo_list);
3792 /** Return true iff any networkstatus includes a descriptor whose digest
3793 * is that of <b>desc</b>. */
3794 static int
3795 signed_desc_digest_is_recognized(signed_descriptor_t *desc)
3797 routerstatus_t *rs;
3798 networkstatus_t *consensus = networkstatus_get_latest_consensus();
3799 int caches = directory_caches_dir_info(get_options());
3800 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
3802 if (consensus) {
3803 rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
3804 if (rs && !memcmp(rs->descriptor_digest,
3805 desc->signed_descriptor_digest, DIGEST_LEN))
3806 return 1;
3808 if (caches && networkstatus_v2_list) {
3809 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
3811 if (!(rs = networkstatus_v2_find_entry(ns, desc->identity_digest)))
3812 continue;
3813 if (!memcmp(rs->descriptor_digest,
3814 desc->signed_descriptor_digest, DIGEST_LEN))
3815 return 1;
3818 return 0;
3821 /** Clear all our timeouts for fetching v2 and v3 directory stuff, and then
3822 * give it all a try again. */
3823 void
3824 routerlist_retry_directory_downloads(time_t now)
3826 router_reset_status_download_failures();
3827 router_reset_descriptor_download_failures();
3828 update_networkstatus_downloads(now);
3829 update_router_descriptor_downloads(now);
3832 /** Return 1 if all running sufficiently-stable routers will reject
3833 * addr:port, return 0 if any might accept it. */
3835 router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
3836 int need_uptime)
3838 addr_policy_result_t r;
3839 if (!routerlist) return 1;
3841 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
3843 if (router->is_running &&
3844 !router_is_unreliable(router, need_uptime, 0, 0)) {
3845 r = compare_addr_to_addr_policy(addr, port, router->exit_policy);
3846 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
3847 return 0; /* this one could be ok. good enough. */
3850 return 1; /* all will reject. */
3853 /** Return true iff <b>router</b> does not permit exit streams.
3856 router_exit_policy_rejects_all(routerinfo_t *router)
3858 return router->policy_is_reject_star;
3861 /** Add to the list of authoritative directory servers one at
3862 * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
3863 * <b>address</b> is NULL, add ourself. Return the new trusted directory
3864 * server entry on success or NULL if we couldn't add it. */
3865 trusted_dir_server_t *
3866 add_trusted_dir_server(const char *nickname, const char *address,
3867 uint16_t dir_port, uint16_t or_port,
3868 const char *digest, const char *v3_auth_digest,
3869 authority_type_t type)
3871 trusted_dir_server_t *ent;
3872 uint32_t a;
3873 char *hostname = NULL;
3874 size_t dlen;
3875 if (!trusted_dir_servers)
3876 trusted_dir_servers = smartlist_create();
3878 if (!address) { /* The address is us; we should guess. */
3879 if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
3880 log_warn(LD_CONFIG,
3881 "Couldn't find a suitable address when adding ourself as a "
3882 "trusted directory server.");
3883 return NULL;
3885 } else {
3886 if (tor_lookup_hostname(address, &a)) {
3887 log_warn(LD_CONFIG,
3888 "Unable to lookup address for directory server at '%s'",
3889 address);
3890 return NULL;
3892 hostname = tor_strdup(address);
3895 ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
3896 ent->nickname = nickname ? tor_strdup(nickname) : NULL;
3897 ent->address = hostname;
3898 ent->addr = a;
3899 ent->dir_port = dir_port;
3900 ent->or_port = or_port;
3901 ent->is_running = 1;
3902 ent->type = type;
3903 memcpy(ent->digest, digest, DIGEST_LEN);
3904 if (v3_auth_digest && (type & V3_AUTHORITY))
3905 memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
3907 dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
3908 ent->description = tor_malloc(dlen);
3909 if (nickname)
3910 tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
3911 nickname, hostname, (int)dir_port);
3912 else
3913 tor_snprintf(ent->description, dlen, "directory server at %s:%d",
3914 hostname, (int)dir_port);
3916 ent->fake_status.addr = ent->addr;
3917 memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
3918 if (nickname)
3919 strlcpy(ent->fake_status.nickname, nickname,
3920 sizeof(ent->fake_status.nickname));
3921 else
3922 ent->fake_status.nickname[0] = '\0';
3923 ent->fake_status.dir_port = ent->dir_port;
3924 ent->fake_status.or_port = ent->or_port;
3926 if (ent->or_port)
3927 ent->fake_status.version_supports_begindir = 1;
3929 ent->fake_status.version_supports_conditional_consensus = 1;
3931 smartlist_add(trusted_dir_servers, ent);
3932 router_dir_info_changed();
3933 return ent;
3936 /** Free storage held in <b>cert</b>. */
3937 void
3938 authority_cert_free(authority_cert_t *cert)
3940 if (!cert)
3941 return;
3943 tor_free(cert->cache_info.signed_descriptor_body);
3944 crypto_free_pk_env(cert->signing_key);
3945 crypto_free_pk_env(cert->identity_key);
3947 tor_free(cert);
3950 /** Free storage held in <b>ds</b>. */
3951 static void
3952 trusted_dir_server_free(trusted_dir_server_t *ds)
3954 if (!ds)
3955 return;
3957 tor_free(ds->nickname);
3958 tor_free(ds->description);
3959 tor_free(ds->address);
3960 tor_free(ds);
3963 /** Remove all members from the list of trusted dir servers. */
3964 void
3965 clear_trusted_dir_servers(void)
3967 if (trusted_dir_servers) {
3968 SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
3969 trusted_dir_server_free(ent));
3970 smartlist_clear(trusted_dir_servers);
3971 } else {
3972 trusted_dir_servers = smartlist_create();
3974 router_dir_info_changed();
3977 /** Return 1 if any trusted dir server supports v1 directories,
3978 * else return 0. */
3980 any_trusted_dir_is_v1_authority(void)
3982 if (trusted_dir_servers)
3983 return get_n_authorities(V1_AUTHORITY) > 0;
3985 return 0;
3988 /** For every current directory connection whose purpose is <b>purpose</b>,
3989 * and where the resource being downloaded begins with <b>prefix</b>, split
3990 * rest of the resource into base16 fingerprints, decode them, and set the
3991 * corresponding elements of <b>result</b> to a nonzero value. */
3992 static void
3993 list_pending_downloads(digestmap_t *result,
3994 int purpose, const char *prefix)
3996 const size_t p_len = strlen(prefix);
3997 smartlist_t *tmp = smartlist_create();
3998 smartlist_t *conns = get_connection_array();
4000 tor_assert(result);
4002 SMARTLIST_FOREACH(conns, connection_t *, conn,
4004 if (conn->type == CONN_TYPE_DIR &&
4005 conn->purpose == purpose &&
4006 !conn->marked_for_close) {
4007 const char *resource = TO_DIR_CONN(conn)->requested_resource;
4008 if (!strcmpstart(resource, prefix))
4009 dir_split_resource_into_fingerprints(resource + p_len,
4010 tmp, NULL, DSR_HEX);
4013 SMARTLIST_FOREACH(tmp, char *, d,
4015 digestmap_set(result, d, (void*)1);
4016 tor_free(d);
4018 smartlist_free(tmp);
4021 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
4022 * true) we are currently downloading by descriptor digest, set result[d] to
4023 * (void*)1. */
4024 static void
4025 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
4027 int purpose =
4028 extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC;
4029 list_pending_downloads(result, purpose, "d/");
4032 /** Launch downloads for all the descriptors whose digests are listed
4033 * as digests[i] for lo <= i < hi. (Lo and hi may be out of range.)
4034 * If <b>source</b> is given, download from <b>source</b>; otherwise,
4035 * download from an appropriate random directory server.
4037 static void
4038 initiate_descriptor_downloads(routerstatus_t *source,
4039 int purpose,
4040 smartlist_t *digests,
4041 int lo, int hi, int pds_flags)
4043 int i, n = hi-lo;
4044 char *resource, *cp;
4045 size_t r_len;
4046 if (n <= 0)
4047 return;
4048 if (lo < 0)
4049 lo = 0;
4050 if (hi > smartlist_len(digests))
4051 hi = smartlist_len(digests);
4053 r_len = 8 + (HEX_DIGEST_LEN+1)*n;
4054 cp = resource = tor_malloc(r_len);
4055 memcpy(cp, "d/", 2);
4056 cp += 2;
4057 for (i = lo; i < hi; ++i) {
4058 base16_encode(cp, r_len-(cp-resource),
4059 smartlist_get(digests,i), DIGEST_LEN);
4060 cp += HEX_DIGEST_LEN;
4061 *cp++ = '+';
4063 memcpy(cp-1, ".z", 3);
4065 if (source) {
4066 /* We know which authority we want. */
4067 directory_initiate_command_routerstatus(source, purpose,
4068 ROUTER_PURPOSE_GENERAL,
4069 0, /* not private */
4070 resource, NULL, 0, 0);
4071 } else {
4072 directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource,
4073 pds_flags);
4075 tor_free(resource);
4078 /** Return 0 if this routerstatus is obsolete, too new, isn't
4079 * running, or otherwise not a descriptor that we would make any
4080 * use of even if we had it. Else return 1. */
4081 static INLINE int
4082 client_would_use_router(routerstatus_t *rs, time_t now, or_options_t *options)
4084 if (!rs->is_running && !options->FetchUselessDescriptors) {
4085 /* If we had this router descriptor, we wouldn't even bother using it.
4086 * But, if we want to have a complete list, fetch it anyway. */
4087 return 0;
4089 if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
4090 > now) {
4091 /* Most caches probably don't have this descriptor yet. */
4092 return 0;
4094 if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
4095 /* We'd drop it immediately for being too old. */
4096 return 0;
4098 return 1;
4101 /** Max amount of hashes to download per request.
4102 * Since squid does not like URLs >= 4096 bytes we limit it to 96.
4103 * 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
4104 * 4058/41 (40 for the hash and 1 for the + that separates them) => 98
4105 * So use 96 because it's a nice number.
4107 #define MAX_DL_PER_REQUEST 96
4108 /** Don't split our requests so finely that we are requesting fewer than
4109 * this number per server. */
4110 #define MIN_DL_PER_REQUEST 4
4111 /** To prevent a single screwy cache from confusing us by selective reply,
4112 * try to split our requests into at least this many requests. */
4113 #define MIN_REQUESTS 3
4114 /** If we want fewer than this many descriptors, wait until we
4115 * want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
4116 * passed. */
4117 #define MAX_DL_TO_DELAY 16
4118 /** When directory clients have only a few servers to request, they batch
4119 * them until they have more, or until this amount of time has passed. */
4120 #define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
4122 /** Given a list of router descriptor digests in <b>downloadable</b>, decide
4123 * whether to delay fetching until we have more. If we don't want to delay,
4124 * launch one or more requests to the appropriate directory authorities. */
4125 static void
4126 launch_router_descriptor_downloads(smartlist_t *downloadable,
4127 routerstatus_t *source, time_t now)
4129 int should_delay = 0, n_downloadable;
4130 or_options_t *options = get_options();
4132 n_downloadable = smartlist_len(downloadable);
4133 if (!directory_fetches_dir_info_early(options)) {
4134 if (n_downloadable >= MAX_DL_TO_DELAY) {
4135 log_debug(LD_DIR,
4136 "There are enough downloadable routerdescs to launch requests.");
4137 should_delay = 0;
4138 } else {
4139 should_delay = (last_routerdesc_download_attempted +
4140 MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
4141 if (!should_delay && n_downloadable) {
4142 if (last_routerdesc_download_attempted) {
4143 log_info(LD_DIR,
4144 "There are not many downloadable routerdescs, but we've "
4145 "been waiting long enough (%d seconds). Downloading.",
4146 (int)(now-last_routerdesc_download_attempted));
4147 } else {
4148 log_info(LD_DIR,
4149 "There are not many downloadable routerdescs, but we haven't "
4150 "tried downloading descriptors recently. Downloading.");
4155 /* XXX should we consider having even the dir mirrors delay
4156 * a little bit, so we don't load the authorities as much? -RD
4157 * I don't think so. If we do, clients that want those descriptors may
4158 * not actually find them if the caches haven't got them yet. -NM
4161 if (! should_delay && n_downloadable) {
4162 int i, n_per_request;
4163 const char *req_plural = "", *rtr_plural = "";
4164 int pds_flags = PDS_RETRY_IF_NO_SERVERS;
4165 if (! authdir_mode_any_nonhidserv(options)) {
4166 /* If we wind up going to the authorities, we want to only open one
4167 * connection to each authority at a time, so that we don't overload
4168 * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
4169 * regardless of whether we're a cache or not; it gets ignored if we're
4170 * not calling router_pick_trusteddirserver.
4172 * Setting this flag can make initiate_descriptor_downloads() ignore
4173 * requests. We need to make sure that we do in fact call
4174 * update_router_descriptor_downloads() later on, once the connections
4175 * have succeeded or failed.
4177 pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH;
4180 n_per_request = (n_downloadable+MIN_REQUESTS-1) / MIN_REQUESTS;
4181 if (n_per_request > MAX_DL_PER_REQUEST)
4182 n_per_request = MAX_DL_PER_REQUEST;
4183 if (n_per_request < MIN_DL_PER_REQUEST)
4184 n_per_request = MIN_DL_PER_REQUEST;
4186 if (n_downloadable > n_per_request)
4187 req_plural = rtr_plural = "s";
4188 else if (n_downloadable > 1)
4189 rtr_plural = "s";
4191 log_info(LD_DIR,
4192 "Launching %d request%s for %d router%s, %d at a time",
4193 (n_downloadable+n_per_request-1)/n_per_request,
4194 req_plural, n_downloadable, rtr_plural, n_per_request);
4195 smartlist_sort_digests(downloadable);
4196 for (i=0; i < n_downloadable; i += n_per_request) {
4197 initiate_descriptor_downloads(source, DIR_PURPOSE_FETCH_SERVERDESC,
4198 downloadable, i, i+n_per_request,
4199 pds_flags);
4201 last_routerdesc_download_attempted = now;
4205 /** Launch downloads for router status as needed, using the strategy used by
4206 * authorities and caches: based on the v2 networkstatuses we have, download
4207 * every descriptor we don't have but would serve, from a random authority
4208 * that lists it. */
4209 static void
4210 update_router_descriptor_cache_downloads_v2(time_t now)
4212 smartlist_t **downloadable; /* For each authority, what can we dl from it? */
4213 smartlist_t **download_from; /* ... and, what will we dl from it? */
4214 digestmap_t *map; /* Which descs are in progress, or assigned? */
4215 int i, j, n;
4216 int n_download;
4217 or_options_t *options = get_options();
4218 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
4220 if (! directory_fetches_dir_info_early(options)) {
4221 log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads_v2() "
4222 "on a non-dir-mirror?");
4225 if (!networkstatus_v2_list || !smartlist_len(networkstatus_v2_list))
4226 return;
4228 map = digestmap_new();
4229 n = smartlist_len(networkstatus_v2_list);
4231 downloadable = tor_malloc_zero(sizeof(smartlist_t*) * n);
4232 download_from = tor_malloc_zero(sizeof(smartlist_t*) * n);
4234 /* Set map[d]=1 for the digest of every descriptor that we are currently
4235 * downloading. */
4236 list_pending_descriptor_downloads(map, 0);
4238 /* For the digest of every descriptor that we don't have, and that we aren't
4239 * downloading, add d to downloadable[i] if the i'th networkstatus knows
4240 * about that descriptor, and we haven't already failed to get that
4241 * descriptor from the corresponding authority.
4243 n_download = 0;
4244 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
4246 trusted_dir_server_t *ds;
4247 smartlist_t *dl;
4248 dl = downloadable[ns_sl_idx] = smartlist_create();
4249 download_from[ns_sl_idx] = smartlist_create();
4250 if (ns->published_on + MAX_NETWORKSTATUS_AGE+10*60 < now) {
4251 /* Don't download if the networkstatus is almost ancient. */
4252 /* Actually, I suspect what's happening here is that we ask
4253 * for the descriptor when we have a given networkstatus,
4254 * and then we get a newer networkstatus, and then we receive
4255 * the descriptor. Having a networkstatus actually expire is
4256 * probably a rare event, and we'll probably be happiest if
4257 * we take this clause out. -RD */
4258 continue;
4261 /* Don't try dirservers that we think are down -- we might have
4262 * just tried them and just marked them as down. */
4263 ds = router_get_trusteddirserver_by_digest(ns->identity_digest);
4264 if (ds && !ds->is_running)
4265 continue;
4267 SMARTLIST_FOREACH(ns->entries, routerstatus_t * , rs,
4269 if (!rs->need_to_mirror)
4270 continue;
4271 if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
4272 log_warn(LD_BUG,
4273 "We have a router descriptor, but need_to_mirror=1.");
4274 rs->need_to_mirror = 0;
4275 continue;
4277 if (authdir_mode(options) && dirserv_would_reject_router(rs)) {
4278 rs->need_to_mirror = 0;
4279 continue;
4281 if (digestmap_get(map, rs->descriptor_digest)) {
4282 /* We're downloading it already. */
4283 continue;
4284 } else {
4285 /* We could download it from this guy. */
4286 smartlist_add(dl, rs->descriptor_digest);
4287 ++n_download;
4292 /* At random, assign descriptors to authorities such that:
4293 * - if d is a member of some downloadable[x], d is a member of some
4294 * download_from[y]. (Everything we want to download, we try to download
4295 * from somebody.)
4296 * - If d is a member of download_from[y], d is a member of downloadable[y].
4297 * (We only try to download descriptors from authorities who claim to have
4298 * them.)
4299 * - No d is a member of download_from[x] and download_from[y] s.t. x != y.
4300 * (We don't try to download anything from two authorities concurrently.)
4302 while (n_download) {
4303 int which_ns = crypto_rand_int(n);
4304 smartlist_t *dl = downloadable[which_ns];
4305 int idx;
4306 char *d;
4307 if (!smartlist_len(dl))
4308 continue;
4309 idx = crypto_rand_int(smartlist_len(dl));
4310 d = smartlist_get(dl, idx);
4311 if (! digestmap_get(map, d)) {
4312 smartlist_add(download_from[which_ns], d);
4313 digestmap_set(map, d, (void*) 1);
4315 smartlist_del(dl, idx);
4316 --n_download;
4319 /* Now, we can actually launch our requests. */
4320 for (i=0; i<n; ++i) {
4321 networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
4322 trusted_dir_server_t *ds =
4323 router_get_trusteddirserver_by_digest(ns->identity_digest);
4324 smartlist_t *dl = download_from[i];
4325 int pds_flags = PDS_RETRY_IF_NO_SERVERS;
4326 if (! authdir_mode_any_nonhidserv(options))
4327 pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH; /* XXXX ignored*/
4329 if (!ds) {
4330 log_info(LD_DIR, "Networkstatus with no corresponding authority!");
4331 continue;
4333 if (! smartlist_len(dl))
4334 continue;
4335 log_info(LD_DIR, "Requesting %d descriptors from authority \"%s\"",
4336 smartlist_len(dl), ds->nickname);
4337 for (j=0; j < smartlist_len(dl); j += MAX_DL_PER_REQUEST) {
4338 initiate_descriptor_downloads(&(ds->fake_status),
4339 DIR_PURPOSE_FETCH_SERVERDESC, dl, j,
4340 j+MAX_DL_PER_REQUEST, pds_flags);
4344 for (i=0; i<n; ++i) {
4345 smartlist_free(download_from[i]);
4346 smartlist_free(downloadable[i]);
4348 tor_free(download_from);
4349 tor_free(downloadable);
4350 digestmap_free(map,NULL);
4353 /** For any descriptor that we want that's currently listed in
4354 * <b>consensus</b>, download it as appropriate. */
4355 void
4356 update_consensus_router_descriptor_downloads(time_t now, int is_vote,
4357 networkstatus_t *consensus)
4359 or_options_t *options = get_options();
4360 digestmap_t *map = NULL;
4361 smartlist_t *no_longer_old = smartlist_create();
4362 smartlist_t *downloadable = smartlist_create();
4363 routerstatus_t *source = NULL;
4364 int authdir = authdir_mode(options);
4365 int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
4366 n_inprogress=0, n_in_oldrouters=0;
4368 if (directory_too_idle_to_fetch_descriptors(options, now))
4369 goto done;
4370 if (!consensus)
4371 goto done;
4373 if (is_vote) {
4374 /* where's it from, so we know whom to ask for descriptors */
4375 trusted_dir_server_t *ds;
4376 networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
4377 tor_assert(voter);
4378 ds = trusteddirserver_get_by_v3_auth_digest(voter->identity_digest);
4379 if (ds)
4380 source = &(ds->fake_status);
4381 else
4382 log_warn(LD_DIR, "couldn't lookup source from vote?");
4385 map = digestmap_new();
4386 list_pending_descriptor_downloads(map, 0);
4387 SMARTLIST_FOREACH(consensus->routerstatus_list, void *, rsp,
4389 routerstatus_t *rs =
4390 is_vote ? &(((vote_routerstatus_t *)rsp)->status) : rsp;
4391 signed_descriptor_t *sd;
4392 if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) {
4393 routerinfo_t *ri;
4394 ++n_have;
4395 if (!(ri = router_get_by_digest(rs->identity_digest)) ||
4396 memcmp(ri->cache_info.signed_descriptor_digest,
4397 sd->signed_descriptor_digest, DIGEST_LEN)) {
4398 /* We have a descriptor with this digest, but either there is no
4399 * entry in routerlist with the same ID (!ri), or there is one,
4400 * but the identity digest differs (memcmp).
4402 smartlist_add(no_longer_old, sd);
4403 ++n_in_oldrouters; /* We have it in old_routers. */
4405 continue; /* We have it already. */
4407 if (digestmap_get(map, rs->descriptor_digest)) {
4408 ++n_inprogress;
4409 continue; /* We have an in-progress download. */
4411 if (!download_status_is_ready(&rs->dl_status, now,
4412 MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
4413 ++n_delayed; /* Not ready for retry. */
4414 continue;
4416 if (authdir && dirserv_would_reject_router(rs)) {
4417 ++n_would_reject;
4418 continue; /* We would throw it out immediately. */
4420 if (!directory_caches_dir_info(options) &&
4421 !client_would_use_router(rs, now, options)) {
4422 ++n_wouldnt_use;
4423 continue; /* We would never use it ourself. */
4425 if (is_vote && source) {
4426 char time_bufnew[ISO_TIME_LEN+1];
4427 char time_bufold[ISO_TIME_LEN+1];
4428 routerinfo_t *oldrouter = router_get_by_digest(rs->identity_digest);
4429 format_iso_time(time_bufnew, rs->published_on);
4430 if (oldrouter)
4431 format_iso_time(time_bufold, oldrouter->cache_info.published_on);
4432 log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
4433 rs->nickname, time_bufnew,
4434 oldrouter ? time_bufold : "none",
4435 source->nickname, oldrouter ? "known" : "unknown");
4437 smartlist_add(downloadable, rs->descriptor_digest);
4440 if (!authdir_mode_handles_descs(options, ROUTER_PURPOSE_GENERAL)
4441 && smartlist_len(no_longer_old)) {
4442 routerlist_t *rl = router_get_routerlist();
4443 log_info(LD_DIR, "%d router descriptors listed in consensus are "
4444 "currently in old_routers; making them current.",
4445 smartlist_len(no_longer_old));
4446 SMARTLIST_FOREACH(no_longer_old, signed_descriptor_t *, sd, {
4447 const char *msg;
4448 was_router_added_t r;
4449 routerinfo_t *ri = routerlist_reparse_old(rl, sd);
4450 if (!ri) {
4451 log_warn(LD_BUG, "Failed to re-parse a router.");
4452 continue;
4454 r = router_add_to_routerlist(ri, &msg, 1, 0);
4455 if (WRA_WAS_OUTDATED(r)) {
4456 log_warn(LD_DIR, "Couldn't add re-parsed router: %s",
4457 msg?msg:"???");
4460 routerlist_assert_ok(rl);
4463 log_info(LD_DIR,
4464 "%d router descriptors downloadable. %d delayed; %d present "
4465 "(%d of those were in old_routers); %d would_reject; "
4466 "%d wouldnt_use; %d in progress.",
4467 smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
4468 n_would_reject, n_wouldnt_use, n_inprogress);
4470 launch_router_descriptor_downloads(downloadable, source, now);
4472 digestmap_free(map, NULL);
4473 done:
4474 smartlist_free(downloadable);
4475 smartlist_free(no_longer_old);
4478 /** How often should we launch a server/authority request to be sure of getting
4479 * a guess for our IP? */
4480 /*XXXX021 this info should come from netinfo cells or something, or we should
4481 * do this only when we aren't seeing incoming data. see bug 652. */
4482 #define DUMMY_DOWNLOAD_INTERVAL (20*60)
4484 /** Launch downloads for router status as needed. */
4485 void
4486 update_router_descriptor_downloads(time_t now)
4488 or_options_t *options = get_options();
4489 static time_t last_dummy_download = 0;
4490 if (should_delay_dir_fetches(options))
4491 return;
4492 if (directory_fetches_dir_info_early(options)) {
4493 update_router_descriptor_cache_downloads_v2(now);
4495 update_consensus_router_descriptor_downloads(now, 0,
4496 networkstatus_get_reasonably_live_consensus(now));
4498 /* XXXX021 we could be smarter here; see notes on bug 652. */
4499 /* If we're a server that doesn't have a configured address, we rely on
4500 * directory fetches to learn when our address changes. So if we haven't
4501 * tried to get any routerdescs in a long time, try a dummy fetch now. */
4502 if (!options->Address &&
4503 server_mode(options) &&
4504 last_routerdesc_download_attempted + DUMMY_DOWNLOAD_INTERVAL < now &&
4505 last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
4506 last_dummy_download = now;
4507 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
4508 ROUTER_PURPOSE_GENERAL, "authority.z",
4509 PDS_RETRY_IF_NO_SERVERS);
4513 /** Launch extrainfo downloads as needed. */
4514 void
4515 update_extrainfo_downloads(time_t now)
4517 or_options_t *options = get_options();
4518 routerlist_t *rl;
4519 smartlist_t *wanted;
4520 digestmap_t *pending;
4521 int old_routers, i;
4522 int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0;
4523 if (! options->DownloadExtraInfo)
4524 return;
4525 if (should_delay_dir_fetches(options))
4526 return;
4527 if (!router_have_minimum_dir_info())
4528 return;
4530 pending = digestmap_new();
4531 list_pending_descriptor_downloads(pending, 1);
4532 rl = router_get_routerlist();
4533 wanted = smartlist_create();
4534 for (old_routers = 0; old_routers < 2; ++old_routers) {
4535 smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
4536 for (i = 0; i < smartlist_len(lst); ++i) {
4537 signed_descriptor_t *sd;
4538 char *d;
4539 if (old_routers)
4540 sd = smartlist_get(lst, i);
4541 else
4542 sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
4543 if (sd->is_extrainfo)
4544 continue; /* This should never happen. */
4545 if (old_routers && !router_get_by_digest(sd->identity_digest))
4546 continue; /* Couldn't check the signature if we got it. */
4547 if (sd->extrainfo_is_bogus)
4548 continue;
4549 d = sd->extra_info_digest;
4550 if (tor_digest_is_zero(d)) {
4551 ++n_no_ei;
4552 continue;
4554 if (eimap_get(rl->extra_info_map, d)) {
4555 ++n_have;
4556 continue;
4558 if (!download_status_is_ready(&sd->ei_dl_status, now,
4559 MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
4560 ++n_delay;
4561 continue;
4563 if (digestmap_get(pending, d)) {
4564 ++n_pending;
4565 continue;
4567 smartlist_add(wanted, d);
4570 digestmap_free(pending, NULL);
4572 log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
4573 "with present ei, %d delaying, %d pending, %d downloadable.",
4574 n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted));
4576 smartlist_shuffle(wanted);
4577 for (i = 0; i < smartlist_len(wanted); i += MAX_DL_PER_REQUEST) {
4578 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
4579 wanted, i, i + MAX_DL_PER_REQUEST,
4580 PDS_RETRY_IF_NO_SERVERS|PDS_NO_EXISTING_SERVERDESC_FETCH);
4583 smartlist_free(wanted);
4586 /** True iff, the last time we checked whether we had enough directory info
4587 * to build circuits, the answer was "yes". */
4588 static int have_min_dir_info = 0;
4589 /** True iff enough has changed since the last time we checked whether we had
4590 * enough directory info to build circuits that our old answer can no longer
4591 * be trusted. */
4592 static int need_to_update_have_min_dir_info = 1;
4593 /** String describing what we're missing before we have enough directory
4594 * info. */
4595 static char dir_info_status[128] = "";
4597 /** Return true iff we have enough networkstatus and router information to
4598 * start building circuits. Right now, this means "more than half the
4599 * networkstatus documents, and at least 1/4 of expected routers." */
4600 //XXX should consider whether we have enough exiting nodes here.
4602 router_have_minimum_dir_info(void)
4604 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
4605 update_router_have_minimum_dir_info();
4606 need_to_update_have_min_dir_info = 0;
4608 return have_min_dir_info;
4611 /** Called when our internal view of the directory has changed. This can be
4612 * when the authorities change, networkstatuses change, the list of routerdescs
4613 * changes, or number of running routers changes.
4615 void
4616 router_dir_info_changed(void)
4618 need_to_update_have_min_dir_info = 1;
4619 rend_hsdir_routers_changed();
4622 /** Return a string describing what we're missing before we have enough
4623 * directory info. */
4624 const char *
4625 get_dir_info_status_string(void)
4627 return dir_info_status;
4630 /** Iterate over the servers listed in <b>consensus</b>, and count how many of
4631 * them seem like ones we'd use, and how many of <em>those</em> we have
4632 * descriptors for. Store the former in *<b>num_usable</b> and the latter in
4633 * *<b>num_present</b>. */
4634 static void
4635 count_usable_descriptors(int *num_present, int *num_usable,
4636 const networkstatus_t *consensus,
4637 or_options_t *options, time_t now)
4639 *num_present = 0, *num_usable=0;
4641 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
4643 if (client_would_use_router(rs, now, options)) {
4644 ++*num_usable; /* the consensus says we want it. */
4645 if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
4646 /* we have the descriptor listed in the consensus. */
4647 ++*num_present;
4652 log_debug(LD_DIR, "%d usable, %d present.", *num_usable, *num_present);
4655 /** We just fetched a new set of descriptors. Compute how far through
4656 * the "loading descriptors" bootstrapping phase we are, so we can inform
4657 * the controller of our progress. */
4659 count_loading_descriptors_progress(void)
4661 int num_present = 0, num_usable=0;
4662 time_t now = time(NULL);
4663 const networkstatus_t *consensus =
4664 networkstatus_get_reasonably_live_consensus(now);
4665 double fraction;
4667 if (!consensus)
4668 return 0; /* can't count descriptors if we have no list of them */
4670 count_usable_descriptors(&num_present, &num_usable,
4671 consensus, get_options(), now);
4673 if (num_usable == 0)
4674 return 0; /* don't div by 0 */
4675 fraction = num_present / (num_usable/4.);
4676 if (fraction > 1.0)
4677 return 0; /* it's not the number of descriptors holding us back */
4678 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
4679 (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
4680 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
4683 /** Change the value of have_min_dir_info, setting it true iff we have enough
4684 * network and router information to build circuits. Clear the value of
4685 * need_to_update_have_min_dir_info. */
4686 static void
4687 update_router_have_minimum_dir_info(void)
4689 int num_present = 0, num_usable=0;
4690 time_t now = time(NULL);
4691 int res;
4692 or_options_t *options = get_options();
4693 const networkstatus_t *consensus =
4694 networkstatus_get_reasonably_live_consensus(now);
4696 if (!consensus) {
4697 if (!networkstatus_get_latest_consensus())
4698 strlcpy(dir_info_status, "We have no network-status consensus.",
4699 sizeof(dir_info_status));
4700 else
4701 strlcpy(dir_info_status, "We have no recent network-status consensus.",
4702 sizeof(dir_info_status));
4703 res = 0;
4704 goto done;
4707 if (should_delay_dir_fetches(get_options())) {
4708 log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
4709 strlcpy(dir_info_status, "No live bridge descriptors.",
4710 sizeof(dir_info_status));
4711 res = 0;
4712 goto done;
4715 count_usable_descriptors(&num_present, &num_usable, consensus, options, now);
4717 if (num_present < num_usable/4) {
4718 tor_snprintf(dir_info_status, sizeof(dir_info_status),
4719 "We have only %d/%d usable descriptors.", num_present, num_usable);
4720 res = 0;
4721 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
4722 } else if (num_present < 2) {
4723 tor_snprintf(dir_info_status, sizeof(dir_info_status),
4724 "Only %d descriptor%s here and believed reachable!",
4725 num_present, num_present ? "" : "s");
4726 res = 0;
4727 } else {
4728 res = 1;
4731 done:
4732 if (res && !have_min_dir_info) {
4733 log(LOG_NOTICE, LD_DIR,
4734 "We now have enough directory information to build circuits.");
4735 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
4736 control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
4738 if (!res && have_min_dir_info) {
4739 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
4740 log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
4741 "Our directory information is no longer up-to-date "
4742 "enough to build circuits: %s", dir_info_status);
4743 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
4745 have_min_dir_info = res;
4746 need_to_update_have_min_dir_info = 0;
4749 /** Reset the descriptor download failure count on all routers, so that we
4750 * can retry any long-failed routers immediately.
4752 void
4753 router_reset_descriptor_download_failures(void)
4755 networkstatus_reset_download_failures();
4756 last_routerdesc_download_attempted = 0;
4757 if (!routerlist)
4758 return;
4759 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
4761 download_status_reset(&ri->cache_info.ei_dl_status);
4763 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
4765 download_status_reset(&sd->ei_dl_status);
4769 /** Any changes in a router descriptor's publication time larger than this are
4770 * automatically non-cosmetic. */
4771 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
4773 /** We allow uptime to vary from how much it ought to be by this much. */
4774 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
4776 /** Return true iff the only differences between r1 and r2 are such that
4777 * would not cause a recent (post 0.1.1.6) dirserver to republish.
4780 router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2)
4782 time_t r1pub, r2pub;
4783 long time_difference;
4784 tor_assert(r1 && r2);
4786 /* r1 should be the one that was published first. */
4787 if (r1->cache_info.published_on > r2->cache_info.published_on) {
4788 routerinfo_t *ri_tmp = r2;
4789 r2 = r1;
4790 r1 = ri_tmp;
4793 /* If any key fields differ, they're different. */
4794 if (strcasecmp(r1->address, r2->address) ||
4795 strcasecmp(r1->nickname, r2->nickname) ||
4796 r1->or_port != r2->or_port ||
4797 r1->dir_port != r2->dir_port ||
4798 r1->purpose != r2->purpose ||
4799 crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
4800 crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
4801 strcasecmp(r1->platform, r2->platform) ||
4802 (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
4803 (!r1->contact_info && r2->contact_info) ||
4804 (r1->contact_info && r2->contact_info &&
4805 strcasecmp(r1->contact_info, r2->contact_info)) ||
4806 r1->is_hibernating != r2->is_hibernating ||
4807 r1->has_old_dnsworkers != r2->has_old_dnsworkers ||
4808 cmp_addr_policies(r1->exit_policy, r2->exit_policy))
4809 return 0;
4810 if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
4811 return 0;
4812 if (r1->declared_family && r2->declared_family) {
4813 int i, n;
4814 if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
4815 return 0;
4816 n = smartlist_len(r1->declared_family);
4817 for (i=0; i < n; ++i) {
4818 if (strcasecmp(smartlist_get(r1->declared_family, i),
4819 smartlist_get(r2->declared_family, i)))
4820 return 0;
4824 /* Did bandwidth change a lot? */
4825 if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
4826 (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
4827 return 0;
4829 /* Did the bandwidthrate or bandwidthburst change? */
4830 if ((r1->bandwidthrate != r2->bandwidthrate) ||
4831 (r1->bandwidthburst != r2->bandwidthburst))
4832 return 0;
4834 /* Did more than 12 hours pass? */
4835 if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
4836 < r2->cache_info.published_on)
4837 return 0;
4839 /* Did uptime fail to increase by approximately the amount we would think,
4840 * give or take some slop? */
4841 r1pub = r1->cache_info.published_on;
4842 r2pub = r2->cache_info.published_on;
4843 time_difference = labs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
4844 if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
4845 time_difference > r1->uptime * .05 &&
4846 time_difference > r2->uptime * .05)
4847 return 0;
4849 /* Otherwise, the difference is cosmetic. */
4850 return 1;
4853 /** Check whether <b>ri</b> (a.k.a. sd) is a router compatible with the
4854 * extrainfo document
4855 * <b>ei</b>. If no router is compatible with <b>ei</b>, <b>ei</b> should be
4856 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
4857 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
4858 * <b>msg</b> is present, set *<b>msg</b> to a description of the
4859 * incompatibility (if any).
4862 routerinfo_incompatible_with_extrainfo(routerinfo_t *ri, extrainfo_t *ei,
4863 signed_descriptor_t *sd,
4864 const char **msg)
4866 int digest_matches, r=1;
4867 tor_assert(ri);
4868 tor_assert(ei);
4869 if (!sd)
4870 sd = &ri->cache_info;
4872 if (ei->bad_sig) {
4873 if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
4874 return 1;
4877 digest_matches = !memcmp(ei->cache_info.signed_descriptor_digest,
4878 sd->extra_info_digest, DIGEST_LEN);
4880 /* The identity must match exactly to have been generated at the same time
4881 * by the same router. */
4882 if (memcmp(ri->cache_info.identity_digest, ei->cache_info.identity_digest,
4883 DIGEST_LEN)) {
4884 if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
4885 goto err; /* different servers */
4888 if (ei->pending_sig) {
4889 char signed_digest[128];
4890 if (crypto_pk_public_checksig(ri->identity_pkey, signed_digest,
4891 ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
4892 memcmp(signed_digest, ei->cache_info.signed_descriptor_digest,
4893 DIGEST_LEN)) {
4894 ei->bad_sig = 1;
4895 tor_free(ei->pending_sig);
4896 if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
4897 goto err; /* Bad signature, or no match. */
4900 ei->cache_info.send_unencrypted = ri->cache_info.send_unencrypted;
4901 tor_free(ei->pending_sig);
4904 if (ei->cache_info.published_on < sd->published_on) {
4905 if (msg) *msg = "Extrainfo published time did not match routerdesc";
4906 goto err;
4907 } else if (ei->cache_info.published_on > sd->published_on) {
4908 if (msg) *msg = "Extrainfo published time did not match routerdesc";
4909 r = -1;
4910 goto err;
4913 if (!digest_matches) {
4914 if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
4915 goto err; /* Digest doesn't match declared value. */
4918 return 0;
4919 err:
4920 if (digest_matches) {
4921 /* This signature was okay, and the digest was right: This is indeed the
4922 * corresponding extrainfo. But insanely, it doesn't match the routerinfo
4923 * that lists it. Don't try to fetch this one again. */
4924 sd->extrainfo_is_bogus = 1;
4927 return r;
4930 /** Assert that the internal representation of <b>rl</b> is
4931 * self-consistent. */
4932 void
4933 routerlist_assert_ok(routerlist_t *rl)
4935 routerinfo_t *r2;
4936 signed_descriptor_t *sd2;
4937 if (!rl)
4938 return;
4939 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
4941 r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
4942 tor_assert(r == r2);
4943 sd2 = sdmap_get(rl->desc_digest_map,
4944 r->cache_info.signed_descriptor_digest);
4945 tor_assert(&(r->cache_info) == sd2);
4946 tor_assert(r->cache_info.routerlist_index == r_sl_idx);
4947 /* XXXX
4949 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
4950 * commenting this out is just a band-aid.
4952 * The problem is that, although well-behaved router descriptors
4953 * should never have the same value for their extra_info_digest, it's
4954 * possible for ill-behaved routers to claim whatever they like there.
4956 * The real answer is to trash desc_by_eid_map and instead have
4957 * something that indicates for a given extra-info digest we want,
4958 * what its download status is. We'll do that as a part of routerlist
4959 * refactoring once consensus directories are in. For now,
4960 * this rep violation is probably harmless: an adversary can make us
4961 * reset our retry count for an extrainfo, but that's not the end
4962 * of the world. Changing the representation in 0.2.0.x would just
4963 * destabilize the codebase.
4964 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
4965 signed_descriptor_t *sd3 =
4966 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
4967 tor_assert(sd3 == &(r->cache_info));
4971 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
4973 r2 = rimap_get(rl->identity_map, sd->identity_digest);
4974 tor_assert(sd != &(r2->cache_info));
4975 sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
4976 tor_assert(sd == sd2);
4977 tor_assert(sd->routerlist_index == sd_sl_idx);
4978 /* XXXX see above.
4979 if (!tor_digest_is_zero(sd->extra_info_digest)) {
4980 signed_descriptor_t *sd3 =
4981 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
4982 tor_assert(sd3 == sd);
4987 RIMAP_FOREACH(rl->identity_map, d, r) {
4988 tor_assert(!memcmp(r->cache_info.identity_digest, d, DIGEST_LEN));
4989 } DIGESTMAP_FOREACH_END;
4990 SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
4991 tor_assert(!memcmp(sd->signed_descriptor_digest, d, DIGEST_LEN));
4992 } DIGESTMAP_FOREACH_END;
4993 SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
4994 tor_assert(!tor_digest_is_zero(d));
4995 tor_assert(sd);
4996 tor_assert(!memcmp(sd->extra_info_digest, d, DIGEST_LEN));
4997 } DIGESTMAP_FOREACH_END;
4998 EIMAP_FOREACH(rl->extra_info_map, d, ei) {
4999 signed_descriptor_t *sd;
5000 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
5001 d, DIGEST_LEN));
5002 sd = sdmap_get(rl->desc_by_eid_map,
5003 ei->cache_info.signed_descriptor_digest);
5004 // tor_assert(sd); // XXXX see above
5005 if (sd) {
5006 tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
5007 sd->extra_info_digest, DIGEST_LEN));
5009 } DIGESTMAP_FOREACH_END;
5012 /** Allocate and return a new string representing the contact info
5013 * and platform string for <b>router</b>,
5014 * surrounded by quotes and using standard C escapes.
5016 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
5017 * thread. Also, each call invalidates the last-returned value, so don't
5018 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
5020 * If <b>router</b> is NULL, it just frees its internal memory and returns.
5022 const char *
5023 esc_router_info(routerinfo_t *router)
5025 static char *info=NULL;
5026 char *esc_contact, *esc_platform;
5027 size_t len;
5028 tor_free(info);
5030 if (!router)
5031 return NULL; /* we're exiting; just free the memory we use */
5033 esc_contact = esc_for_log(router->contact_info);
5034 esc_platform = esc_for_log(router->platform);
5036 len = strlen(esc_contact)+strlen(esc_platform)+32;
5037 info = tor_malloc(len);
5038 tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
5039 esc_platform);
5040 tor_free(esc_contact);
5041 tor_free(esc_platform);
5043 return info;
5046 /** Helper for sorting: compare two routerinfos by their identity
5047 * digest. */
5048 static int
5049 _compare_routerinfo_by_id_digest(const void **a, const void **b)
5051 routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
5052 return memcmp(first->cache_info.identity_digest,
5053 second->cache_info.identity_digest,
5054 DIGEST_LEN);
5057 /** Sort a list of routerinfo_t in ascending order of identity digest. */
5058 void
5059 routers_sort_by_identity(smartlist_t *routers)
5061 smartlist_sort(routers, _compare_routerinfo_by_id_digest);
5064 /** A routerset specifies constraints on a set of possible routerinfos, based
5065 * on their names, identities, or addresses. It is optimized for determining
5066 * whether a router is a member or not, in O(1+P) time, where P is the number
5067 * of address policy constraints. */
5068 struct routerset_t {
5069 /** A list of strings for the elements of the policy. Each string is either
5070 * a nickname, a hexadecimal identity fingerprint, or an address policy. A
5071 * router belongs to the set if its nickname OR its identity OR its address
5072 * matches an entry here. */
5073 smartlist_t *list;
5074 /** A map from lowercase nicknames of routers in the set to (void*)1 */
5075 strmap_t *names;
5076 /** A map from identity digests routers in the set to (void*)1 */
5077 digestmap_t *digests;
5078 /** An address policy for routers in the set. For implementation reasons,
5079 * a router belongs to the set if it is _rejected_ by this policy. */
5080 smartlist_t *policies;
5082 /** A human-readable description of what this routerset is for. Used in
5083 * log messages. */
5084 char *description;
5086 /** A list of the country codes in this set. */
5087 smartlist_t *country_names;
5088 /** Total number of countries we knew about when we built <b>countries</b>.*/
5089 int n_countries;
5090 /** Bit array mapping the return value of geoip_get_country() to 1 iff the
5091 * country is a member of this routerset. Note that we MUST call
5092 * routerset_refresh_countries() whenever the geoip country list is
5093 * reloaded. */
5094 bitarray_t *countries;
5097 /** Return a new empty routerset. */
5098 routerset_t *
5099 routerset_new(void)
5101 routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
5102 result->list = smartlist_create();
5103 result->names = strmap_new();
5104 result->digests = digestmap_new();
5105 result->policies = smartlist_create();
5106 result->country_names = smartlist_create();
5107 return result;
5110 /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
5111 * string holding the "cc" part. Else, return NULL. */
5112 static char *
5113 routerset_get_countryname(const char *c)
5115 char *country;
5117 if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
5118 return NULL;
5120 country = tor_strndup(c+1, 2);
5121 tor_strlower(country);
5122 return country;
5125 #if 0
5126 /** Add the GeoIP database's integer index (+1) of a valid two-character
5127 * country code to the routerset's <b>countries</b> bitarray. Return the
5128 * integer index if the country code is valid, -1 otherwise.*/
5129 static int
5130 routerset_add_country(const char *c)
5132 char country[3];
5133 country_t cc;
5135 /* XXXX: Country codes must be of the form \{[a-z\?]{2}\} but this accepts
5136 \{[.]{2}\}. Do we need to be strict? -RH */
5137 /* Nope; if the country code is bad, we'll get 0 when we look it up. */
5139 if (!geoip_is_loaded()) {
5140 log(LOG_WARN, LD_CONFIG, "GeoIP database not loaded: Cannot add country"
5141 "entry %s, ignoring.", c);
5142 return -1;
5145 memcpy(country, c+1, 2);
5146 country[2] = '\0';
5147 tor_strlower(country);
5149 if ((cc=geoip_get_country(country))==-1) {
5150 log(LOG_WARN, LD_CONFIG, "Country code '%s' is not valid, ignoring.",
5151 country);
5153 return cc;
5155 #endif
5157 /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
5158 * the GeoIP database is reloaded.
5160 void
5161 routerset_refresh_countries(routerset_t *target)
5163 int cc;
5164 bitarray_free(target->countries);
5166 if (!geoip_is_loaded()) {
5167 target->countries = NULL;
5168 target->n_countries = 0;
5169 return;
5171 target->n_countries = geoip_get_n_countries();
5172 target->countries = bitarray_init_zero(target->n_countries);
5173 SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
5174 cc = geoip_get_country(country);
5175 if (cc >= 0) {
5176 tor_assert(cc < target->n_countries);
5177 bitarray_set(target->countries, cc);
5178 } else {
5179 log(LOG_WARN, LD_CONFIG, "Country code '%s' is not recognized.",
5180 country);
5182 } SMARTLIST_FOREACH_END(country);
5185 /** Parse the string <b>s</b> to create a set of routerset entries, and add
5186 * them to <b>target</b>. In log messages, refer to the string as
5187 * <b>description</b>. Return 0 on success, -1 on failure.
5189 * Three kinds of elements are allowed in routersets: nicknames, IP address
5190 * patterns, and fingerprints. They may be surrounded by optional space, and
5191 * must be separated by commas.
5194 routerset_parse(routerset_t *target, const char *s, const char *description)
5196 int r = 0;
5197 int added_countries = 0;
5198 char *countryname;
5199 smartlist_t *list = smartlist_create();
5200 smartlist_split_string(list, s, ",",
5201 SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
5202 SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
5203 addr_policy_t *p;
5204 if (is_legal_hexdigest(nick)) {
5205 char d[DIGEST_LEN];
5206 if (*nick == '$')
5207 ++nick;
5208 log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
5209 base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
5210 digestmap_set(target->digests, d, (void*)1);
5211 } else if (is_legal_nickname(nick)) {
5212 log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
5213 strmap_set_lc(target->names, nick, (void*)1);
5214 } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
5215 log_debug(LD_CONFIG, "Adding country %s to %s", nick,
5216 description);
5217 smartlist_add(target->country_names, countryname);
5218 added_countries = 1;
5219 } else if ((strchr(nick,'.') || strchr(nick, '*')) &&
5220 (p = router_parse_addr_policy_item_from_string(
5221 nick, ADDR_POLICY_REJECT))) {
5222 log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
5223 smartlist_add(target->policies, p);
5224 } else {
5225 log_warn(LD_CONFIG, "Entry '%s' in %s is misformed.", nick,
5226 description);
5227 r = -1;
5228 tor_free(nick);
5229 SMARTLIST_DEL_CURRENT(list, nick);
5231 } SMARTLIST_FOREACH_END(nick);
5232 smartlist_add_all(target->list, list);
5233 smartlist_free(list);
5234 if (added_countries)
5235 routerset_refresh_countries(target);
5236 return r;
5239 /** Called when we change a node set, or when we reload the geoip list:
5240 * recompute all country info in all configuration node sets and in the
5241 * routerlist. */
5242 void
5243 refresh_all_country_info(void)
5245 or_options_t *options = get_options();
5247 if (options->EntryNodes)
5248 routerset_refresh_countries(options->EntryNodes);
5249 if (options->ExitNodes)
5250 routerset_refresh_countries(options->ExitNodes);
5251 if (options->ExcludeNodes)
5252 routerset_refresh_countries(options->ExcludeNodes);
5253 if (options->ExcludeExitNodes)
5254 routerset_refresh_countries(options->ExcludeExitNodes);
5255 if (options->_ExcludeExitNodesUnion)
5256 routerset_refresh_countries(options->_ExcludeExitNodesUnion);
5258 routerlist_refresh_countries();
5261 /** Add all members of the set <b>source</b> to <b>target</b>. */
5262 void
5263 routerset_union(routerset_t *target, const routerset_t *source)
5265 char *s;
5266 tor_assert(target);
5267 if (!source || !source->list)
5268 return;
5269 s = routerset_to_string(source);
5270 routerset_parse(target, s, "other routerset");
5271 tor_free(s);
5274 /** Return true iff <b>set</b> lists only nicknames and digests, and includes
5275 * no IP ranges or countries. */
5277 routerset_is_list(const routerset_t *set)
5279 return smartlist_len(set->country_names) == 0 &&
5280 smartlist_len(set->policies) == 0;
5283 /** Return true iff we need a GeoIP IP-to-country database to make sense of
5284 * <b>set</b>. */
5286 routerset_needs_geoip(const routerset_t *set)
5288 return set && smartlist_len(set->country_names);
5291 /** Return true iff there are no entries in <b>set</b>. */
5292 static int
5293 routerset_is_empty(const routerset_t *set)
5295 return !set || smartlist_len(set->list) == 0;
5298 /** Helper. Return true iff <b>set</b> contains a router based on the other
5299 * provided fields. Return higher values for more specific subentries: a
5300 * single router is more specific than an address range of routers, which is
5301 * more specific in turn than a country code.
5303 * (If country is -1, then we take the country
5304 * from addr.) */
5305 static int
5306 routerset_contains(const routerset_t *set, const tor_addr_t *addr,
5307 uint16_t orport,
5308 const char *nickname, const char *id_digest, int is_named,
5309 country_t country)
5311 if (!set || !set->list) return 0;
5312 (void) is_named; /* not supported */
5313 if (nickname && strmap_get_lc(set->names, nickname))
5314 return 4;
5315 if (id_digest && digestmap_get(set->digests, id_digest))
5316 return 4;
5317 if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
5318 == ADDR_POLICY_REJECTED)
5319 return 3;
5320 if (set->countries) {
5321 if (country < 0 && addr)
5322 country = geoip_get_country_by_ip(tor_addr_to_ipv4h(addr));
5324 if (country >= 0 && country < set->n_countries &&
5325 bitarray_is_set(set->countries, country))
5326 return 2;
5328 return 0;
5331 /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
5333 routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
5335 return routerset_contains(set,
5336 &ei->addr,
5337 ei->port,
5338 ei->nickname,
5339 ei->identity_digest,
5340 -1, /*is_named*/
5341 -1 /*country*/);
5344 /** Return true iff <b>ri</b> is in <b>set</b>. */
5346 routerset_contains_router(const routerset_t *set, routerinfo_t *ri)
5348 tor_addr_t addr;
5349 tor_addr_from_ipv4h(&addr, ri->addr);
5350 return routerset_contains(set,
5351 &addr,
5352 ri->or_port,
5353 ri->nickname,
5354 ri->cache_info.identity_digest,
5355 ri->is_named,
5356 ri->country);
5359 /** Return true iff <b>rs</b> is in <b>set</b>. */
5361 routerset_contains_routerstatus(const routerset_t *set, routerstatus_t *rs)
5363 tor_addr_t addr;
5364 tor_addr_from_ipv4h(&addr, rs->addr);
5365 return routerset_contains(set,
5366 &addr,
5367 rs->or_port,
5368 rs->nickname,
5369 rs->identity_digest,
5370 rs->is_named,
5371 -1);
5374 /** Add every known routerinfo_t that is a member of <b>routerset</b> to
5375 * <b>out</b>. If <b>running_only</b>, only add the running ones. */
5376 void
5377 routerset_get_all_routers(smartlist_t *out, const routerset_t *routerset,
5378 int running_only)
5380 tor_assert(out);
5381 if (!routerset || !routerset->list)
5382 return;
5383 if (!warned_nicknames)
5384 warned_nicknames = smartlist_create();
5385 if (routerset_is_list(routerset)) {
5387 /* No routers are specified by type; all are given by name or digest.
5388 * we can do a lookup in O(len(list)). */
5389 SMARTLIST_FOREACH(routerset->list, const char *, name, {
5390 routerinfo_t *router = router_get_by_nickname(name, 1);
5391 if (router) {
5392 if (!running_only || router->is_running)
5393 smartlist_add(out, router);
5396 } else {
5397 /* We need to iterate over the routerlist to get all the ones of the
5398 * right kind. */
5399 routerlist_t *rl = router_get_routerlist();
5400 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
5401 if (running_only && !router->is_running)
5402 continue;
5403 if (routerset_contains_router(routerset, router))
5404 smartlist_add(out, router);
5409 /** Add to <b>target</b> every routerinfo_t from <b>source</b> except:
5411 * 1) Don't add it if <b>include</b> is non-empty and the relay isn't in
5412 * <b>include</b>; and
5413 * 2) Don't add it if <b>exclude</b> is non-empty and the relay is
5414 * excluded in a more specific fashion by <b>exclude</b>.
5415 * 3) If <b>running_only</b>, don't add non-running routers.
5417 void
5418 routersets_get_disjunction(smartlist_t *target,
5419 const smartlist_t *source,
5420 const routerset_t *include,
5421 const routerset_t *exclude, int running_only)
5423 SMARTLIST_FOREACH(source, routerinfo_t *, router, {
5424 int include_result;
5425 if (running_only && !router->is_running)
5426 continue;
5427 if (!routerset_is_empty(include))
5428 include_result = routerset_contains_router(include, router);
5429 else
5430 include_result = 1;
5432 if (include_result) {
5433 int exclude_result = routerset_contains_router(exclude, router);
5434 if (include_result >= exclude_result)
5435 smartlist_add(target, router);
5440 /** Remove every routerinfo_t from <b>lst</b> that is in <b>routerset</b>. */
5441 void
5442 routerset_subtract_routers(smartlist_t *lst, const routerset_t *routerset)
5444 tor_assert(lst);
5445 if (!routerset)
5446 return;
5447 SMARTLIST_FOREACH(lst, routerinfo_t *, r, {
5448 if (routerset_contains_router(routerset, r)) {
5449 //log_debug(LD_DIR, "Subtracting %s",r->nickname);
5450 SMARTLIST_DEL_CURRENT(lst, r);
5455 /** Return a new string that when parsed by routerset_parse_string() will
5456 * yield <b>set</b>. */
5457 char *
5458 routerset_to_string(const routerset_t *set)
5460 if (!set || !set->list)
5461 return tor_strdup("");
5462 return smartlist_join_strings(set->list, ",", 0, NULL);
5465 /** Helper: return true iff old and new are both NULL, or both non-NULL
5466 * equal routersets. */
5468 routerset_equal(const routerset_t *old, const routerset_t *new)
5470 if (old == NULL && new == NULL)
5471 return 1;
5472 else if (old == NULL || new == NULL)
5473 return 0;
5475 if (smartlist_len(old->list) != smartlist_len(new->list))
5476 return 0;
5478 SMARTLIST_FOREACH(old->list, const char *, cp1, {
5479 const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
5480 if (strcmp(cp1, cp2))
5481 return 0;
5484 return 1;
5487 /** Free all storage held in <b>routerset</b>. */
5488 void
5489 routerset_free(routerset_t *routerset)
5491 if (!routerset)
5492 return;
5494 SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
5495 smartlist_free(routerset->list);
5496 SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
5497 addr_policy_free(p));
5498 smartlist_free(routerset->policies);
5499 SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
5500 smartlist_free(routerset->country_names);
5502 strmap_free(routerset->names, NULL);
5503 digestmap_free(routerset->digests, NULL);
5504 bitarray_free(routerset->countries);
5505 tor_free(routerset);
5508 /** Refresh the country code of <b>ri</b>. This function MUST be called on
5509 * each router when the GeoIP database is reloaded, and on all new routers. */
5510 void
5511 routerinfo_set_country(routerinfo_t *ri)
5513 ri->country = geoip_get_country_by_ip(ri->addr);
5516 /** Set the country code of all routers in the routerlist. */
5517 void
5518 routerlist_refresh_countries(void)
5520 routerlist_t *rl = router_get_routerlist();
5521 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
5522 routerinfo_set_country(ri));
5525 /** Determine the routers that are responsible for <b>id</b> (binary) and
5526 * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
5527 * Return -1 if we're returning an empty smartlist, else return 0.
5530 hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
5531 const char *id)
5533 int start, found, n_added = 0, i;
5534 networkstatus_t *c = networkstatus_get_latest_consensus();
5535 int use_begindir = get_options()->TunnelDirConns;
5536 if (!c || !smartlist_len(c->routerstatus_list)) {
5537 log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
5538 "rendezvous operations.");
5539 return -1;
5541 tor_assert(id);
5542 start = networkstatus_vote_find_entry_idx(c, id, &found);
5543 if (start == smartlist_len(c->routerstatus_list)) start = 0;
5544 i = start;
5545 do {
5546 routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
5547 if (r->is_hs_dir) {
5548 if (r->dir_port || use_begindir)
5549 smartlist_add(responsible_dirs, r);
5550 else
5551 log_info(LD_REND, "Not adding router '%s' to list of responsible "
5552 "hidden service directories, because we have no way of "
5553 "reaching it.", r->nickname);
5554 if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
5555 break;
5557 if (++i == smartlist_len(c->routerstatus_list))
5558 i = 0;
5559 } while (i != start);
5561 /* Even though we don't have the desired number of hidden service
5562 * directories, be happy if we got any. */
5563 return smartlist_len(responsible_dirs) ? 0 : -1;
5566 /** Return true if this node is currently acting as hidden service
5567 * directory, false otherwise. */
5569 hid_serv_acting_as_directory(void)
5571 routerinfo_t *me = router_get_my_routerinfo();
5572 networkstatus_t *c;
5573 routerstatus_t *rs;
5574 if (!me)
5575 return 0;
5576 if (!get_options()->HidServDirectoryV2) {
5577 log_info(LD_REND, "We are not acting as hidden service directory, "
5578 "because we have not been configured as such.");
5579 return 0;
5581 if (!(c = networkstatus_get_latest_consensus())) {
5582 log_info(LD_REND, "There's no consensus, so I can't tell if I'm a hidden "
5583 "service directory");
5584 return 0;
5586 rs = networkstatus_vote_find_entry(c, me->cache_info.identity_digest);
5587 if (!rs) {
5588 log_info(LD_REND, "We're not listed in the consensus, so we're not "
5589 "being a hidden service directory.");
5590 return 0;
5592 if (!rs->is_hs_dir) {
5593 log_info(LD_REND, "We're not listed as a hidden service directory in "
5594 "the consensus, so we won't be one.");
5595 return 0;
5597 return 1;
5600 /** Return true if this node is responsible for storing the descriptor ID
5601 * in <b>query</b> and false otherwise. */
5603 hid_serv_responsible_for_desc_id(const char *query)
5605 routerinfo_t *me;
5606 routerstatus_t *last_rs;
5607 const char *my_id, *last_id;
5608 int result;
5609 smartlist_t *responsible;
5610 if (!hid_serv_acting_as_directory())
5611 return 0;
5612 if (!(me = router_get_my_routerinfo()))
5613 return 0; /* This is redundant, but let's be paranoid. */
5614 my_id = me->cache_info.identity_digest;
5615 responsible = smartlist_create();
5616 if (hid_serv_get_responsible_directories(responsible, query) < 0) {
5617 smartlist_free(responsible);
5618 return 0;
5620 last_rs = smartlist_get(responsible, smartlist_len(responsible)-1);
5621 last_id = last_rs->identity_digest;
5622 result = rend_id_is_in_interval(my_id, query, last_id);
5623 smartlist_free(responsible);
5624 return result;