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, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 const char routerlist_c_id
[] =
13 * maintain and access the global list of routerinfos for known
19 // #define DEBUG_ROUTERLIST
21 /****************************************************************************/
23 /* static function prototypes */
24 static routerstatus_t
*router_pick_directory_server_impl(
25 authority_type_t auth
, int flags
);
26 static routerstatus_t
*router_pick_trusteddirserver_impl(
27 authority_type_t auth
, int flags
);
28 static void mark_all_trusteddirservers_up(void);
29 static int router_nickname_matches(routerinfo_t
*router
, const char *nickname
);
30 static void trusted_dir_server_free(trusted_dir_server_t
*ds
);
31 static void launch_router_descriptor_downloads(smartlist_t
*downloadable
,
33 static void update_consensus_router_descriptor_downloads(time_t now
);
34 static int signed_desc_digest_is_recognized(signed_descriptor_t
*desc
);
35 static void update_router_have_minimum_dir_info(void);
36 static const char *signed_descriptor_get_body_impl(signed_descriptor_t
*desc
,
37 int with_annotations
);
38 static void list_pending_downloads(digestmap_t
*result
,
39 int purpose
, const char *prefix
);
41 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_
, digest_sd_map_t
, signed_descriptor_t
)
42 DECLARE_TYPED_DIGESTMAP_FNS(rimap_
, digest_ri_map_t
, routerinfo_t
)
43 DECLARE_TYPED_DIGESTMAP_FNS(eimap_
, digest_ei_map_t
, extrainfo_t
)
45 /****************************************************************************/
47 /** Global list of a trusted_dir_server_t object for each trusted directory
49 static smartlist_t
*trusted_dir_servers
= NULL
;
52 typedef struct cert_list_t
{
53 download_status_t dl_status
;
56 /** Map from v3 identity key digest to cert_list_t. */
57 static digestmap_t
*trusted_dir_certs
= NULL
;
58 /** True iff any key certificate in at least one member of
59 * <b>trusted_dir_certs</b> has changed since we last flushed the
60 * certificates to disk. */
61 static int trusted_dir_servers_certs_changed
= 0;
63 /** Global list of all of the routers that we know about. */
64 static routerlist_t
*routerlist
= NULL
;
66 /** List of strings for nicknames we've already warned about and that are
67 * still unknown / unavailable. */
68 static smartlist_t
*warned_nicknames
= NULL
;
70 /** The last time we tried to download any routerdesc, or 0 for "never". We
71 * use this to rate-limit download attempts when the number of routerdescs to
73 static time_t last_routerdesc_download_attempted
= 0;
75 /** Return the number of directory authorities whose type matches some bit set
78 get_n_authorities(authority_type_t type
)
81 if (!trusted_dir_servers
)
83 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ds
,
89 #define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
93 get_cert_list(const char *id_digest
)
96 if (!trusted_dir_certs
)
97 trusted_dir_certs
= digestmap_new();
98 cl
= digestmap_get(trusted_dir_certs
, id_digest
);
100 cl
= tor_malloc_zero(sizeof(cert_list_t
));
101 cl
->certs
= smartlist_create();
102 digestmap_set(trusted_dir_certs
, id_digest
, cl
);
107 /** Reload the cached v3 key certificates from the cached-certs file in
108 * the data directory. Return 0 on success, -1 on failure. */
110 trusted_dirs_reload_certs(void)
116 filename
= get_datadir_fname("cached-certs");
117 contents
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
121 r
= trusted_dirs_load_certs_from_string(contents
, 1);
126 /** Load a bunch of new key certificates from the string <b>contents</b>. If
127 * <b>from_store</b> is true, the certificates are from the cache, and we
128 * don't need to flush them to disk. If <b>from_store</b> is false, we need
129 * to flush any changed certificates to disk. Return 0 on success, -1 on
132 trusted_dirs_load_certs_from_string(const char *contents
, int from_store
)
134 trusted_dir_server_t
*ds
;
138 for (s
= contents
; *s
; s
= eos
) {
139 authority_cert_t
*cert
= authority_cert_parse_from_string(s
, &eos
);
143 ds
= trusteddirserver_get_by_v3_auth_digest(
144 cert
->cache_info
.identity_digest
);
147 if (drop_unknown
&& !ds
) {
148 log_info(LD_DIR
, "Found %s certificate whose key didn't match "
149 "any v3 authority we recognized; skipping.",
150 from_store
? "cached" : "downloaded");
151 authority_cert_free(cert
);
156 cl
= get_cert_list(cert
->cache_info
.identity_digest
);
158 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, c
,
160 if (!memcmp(c
->cache_info
.signed_descriptor_digest
,
161 cert
->cache_info
.signed_descriptor_digest
,
163 /* we already have this one. continue. */
164 log_info(LD_DIR
, "Skipping %s certificate for %s that we "
166 from_store
? "cached" : "downloaded", ds
->nickname
);
167 authority_cert_free(cert
);
177 log_info(LD_DIR
, "Adding %s certificate for directory authority %s with "
178 "signing key %s", from_store
? "cached" : "downloaded",
179 ds
->nickname
, hex_str(cert
->signing_key_digest
,DIGEST_LEN
));
181 log_info(LD_DIR
, "Adding %s certificate for unrecognized directory "
182 "authority with signing key %s",
183 from_store
? "cached" : "downloaded",
184 hex_str(cert
->signing_key_digest
,DIGEST_LEN
));
187 smartlist_add(cl
->certs
, cert
);
188 if (ds
&& cert
->cache_info
.published_on
> ds
->addr_current_at
) {
189 /* Check to see whether we should update our view of the authority's
191 if (cert
->addr
&& cert
->dir_port
&&
192 (ds
->addr
!= cert
->addr
||
193 ds
->dir_port
!= cert
->dir_port
)) {
194 char *a
= tor_dup_addr(cert
->addr
);
195 log_notice(LD_DIR
, "Updating address for directory authority %s "
196 "from %s:%d to %s:%d based on in certificate.",
197 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
200 ds
->addr
= cert
->addr
;
201 ds
->dir_port
= cert
->dir_port
;
203 ds
->addr_current_at
= cert
->cache_info
.published_on
;
207 trusted_dir_servers_certs_changed
= 1;
210 trusted_dirs_flush_certs_to_disk();
212 networkstatus_note_certs_arrived();
216 /** Save all v3 key certifiacates to the cached-certs file. */
218 trusted_dirs_flush_certs_to_disk(void)
223 if (!trusted_dir_servers_certs_changed
|| !trusted_dir_certs
)
226 chunks
= smartlist_create();
227 DIGESTMAP_FOREACH(trusted_dir_certs
, key
, cert_list_t
*, cl
) {
228 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
230 sized_chunk_t
*c
= tor_malloc(sizeof(sized_chunk_t
));
231 c
->bytes
= cert
->cache_info
.signed_descriptor_body
;
232 c
->len
= cert
->cache_info
.signed_descriptor_len
;
233 smartlist_add(chunks
, c
);
235 } DIGESTMAP_FOREACH_END
237 filename
= get_datadir_fname("cached-certs");
238 if (write_chunks_to_file(filename
, chunks
, 0)) {
239 log_warn(LD_FS
, "Error writing certificates to disk.");
242 SMARTLIST_FOREACH(chunks
, sized_chunk_t
*, c
, tor_free(c
));
243 smartlist_free(chunks
);
245 trusted_dir_servers_certs_changed
= 0;
248 /** Remove all v3 authority certificates that have been superseded for more
249 * than 48 hours. (If the most recent cert was published more than 48 hours
250 * ago, then we aren't going to get any consensuses signed with older
253 trusted_dirs_remove_old_certs(void)
255 #define OLD_CERT_LIFETIME (48*60*60)
256 if (!trusted_dir_certs
)
259 DIGESTMAP_FOREACH(trusted_dir_certs
, key
, cert_list_t
*, cl
) {
260 authority_cert_t
*newest
= NULL
;
261 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
262 if (!newest
|| (cert
->cache_info
.published_on
>
263 newest
->cache_info
.published_on
))
265 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
266 if (newest
&& (newest
->cache_info
.published_on
>
267 cert
->cache_info
.published_on
+ OLD_CERT_LIFETIME
)) {
268 SMARTLIST_DEL_CURRENT(cl
->certs
, cert
);
269 authority_cert_free(cert
);
270 trusted_dir_servers_certs_changed
= 1;
272 } DIGESTMAP_FOREACH_END
273 #undef OLD_CERT_LIFETIME
275 trusted_dirs_flush_certs_to_disk();
278 /** Return the newest v3 authority certificate whose v3 authority identity key
279 * has digest <b>id_digest</b>. Return NULL if no such authority is known,
280 * or it has no certificate. */
282 authority_cert_get_newest_by_id(const char *id_digest
)
285 authority_cert_t
*best
= NULL
;
286 if (!trusted_dir_certs
|| !(cl
= digestmap_get(trusted_dir_certs
, id_digest
)))
288 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
290 if (!best
|| cert
->cache_info
.published_on
> best
->cache_info
.published_on
)
296 /** Return the newest v3 authority certificate whose directory signing key has
297 * giest <sk_digest</b>. Return NULL if no such certificate is known.
300 authority_cert_get_by_sk_digest(const char *sk_digest
)
302 if (!trusted_dir_certs
)
305 DIGESTMAP_FOREACH(trusted_dir_certs
, key
, cert_list_t
*, cl
) {
306 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
308 if (!memcmp(cert
->signing_key_digest
, sk_digest
, DIGEST_LEN
))
311 } DIGESTMAP_FOREACH_END
315 /** Return the v3 authority certificate with signing key matching
316 * <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
317 * Return NULL if no such authority is known. */
319 authority_cert_get_by_digests(const char *id_digest
,
320 const char *sk_digest
)
323 if (!trusted_dir_certs
|| !(cl
= digestmap_get(trusted_dir_certs
, id_digest
)))
325 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
326 if (!memcmp(cert
->signing_key_digest
, sk_digest
, DIGEST_LEN
))
334 authority_cert_get_all(smartlist_t
*certs_out
)
336 if (!trusted_dir_certs
)
339 DIGESTMAP_FOREACH(trusted_dir_certs
, key
, cert_list_t
*, cl
) {
340 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, c
,
341 smartlist_add(certs_out
, c
));
342 } DIGESTMAP_FOREACH_END
347 authority_cert_dl_failed(const char *id_digest
, int status
)
350 if (!trusted_dir_certs
|| !(cl
= digestmap_get(trusted_dir_certs
, id_digest
)))
353 download_status_failed(&cl
->dl_status
, status
);
356 /** How many times will we try to fetch a certificate before giving up? */
357 #define MAX_CERT_DL_FAILURES 8
359 /** Try to download any v3 authority certificates that we may be missing. If
360 * <b>status</b> is provided, try to get all the ones that were used to sign
361 * <b>status</b>. Additionally, try to have a non-expired certificate for
362 * every V3 authority in trusted_dir_servers. Don't fetch certificates we
366 authority_certs_fetch_missing(networkstatus_vote_t
*status
, time_t now
)
368 digestmap_t
*pending
;
369 smartlist_t
*missing_digests
;
370 char *resource
= NULL
;
372 const int cache
= directory_caches_dir_info(get_options());
374 if (should_delay_dir_fetches(get_options()))
377 pending
= digestmap_new();
378 missing_digests
= smartlist_create();
380 list_pending_downloads(pending
, DIR_PURPOSE_FETCH_CERTIFICATE
, "fp/");
382 SMARTLIST_FOREACH(status
->voters
, networkstatus_voter_info_t
*, voter
,
384 if (tor_digest_is_zero(voter
->signing_key_digest
))
385 continue; /* This authority never signed this consensus, so don't
386 * go looking for a cert with key digest 0000000000. */
388 !trusteddirserver_get_by_v3_auth_digest(voter
->identity_digest
))
389 continue; /* We are not a cache, and we don't know this authority.*/
390 cl
= get_cert_list(voter
->identity_digest
);
391 if (authority_cert_get_by_digests(voter
->identity_digest
,
392 voter
->signing_key_digest
)) {
393 download_status_reset(&cl
->dl_status
);
396 if (download_status_is_ready(&cl
->dl_status
, now
,
397 MAX_CERT_DL_FAILURES
)) {
398 log_notice(LD_DIR
, "We're missing a certificate from authority "
399 "with signing key %s: launching request.",
400 hex_str(voter
->signing_key_digest
, DIGEST_LEN
));
401 smartlist_add(missing_digests
, voter
->identity_digest
);
405 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ds
,
408 if (!(ds
->type
& V3_AUTHORITY
))
410 if (smartlist_digest_isin(missing_digests
, ds
->v3_identity_digest
))
412 cl
= get_cert_list(ds
->v3_identity_digest
);
413 SMARTLIST_FOREACH(cl
->certs
, authority_cert_t
*, cert
,
415 if (!ftime_definitely_after(now
, cert
->expires
)) {
416 /* It's not expired, and we weren't looking for something to
417 * verify a consensus with. Call it done. */
418 download_status_reset(&cl
->dl_status
);
423 if (!found
&& download_status_is_ready(&cl
->dl_status
, now
,
424 MAX_CERT_DL_FAILURES
)) {
425 log_notice(LD_DIR
, "No current certificate known for authority %s; "
426 "launching request.", ds
->nickname
);
427 smartlist_add(missing_digests
, ds
->v3_identity_digest
);
431 if (!smartlist_len(missing_digests
)) {
434 smartlist_t
*fps
= smartlist_create();
435 smartlist_add(fps
, tor_strdup("fp/"));
436 SMARTLIST_FOREACH(missing_digests
, const char *, d
, {
438 if (digestmap_get(pending
, d
))
440 fp
= tor_malloc(HEX_DIGEST_LEN
+2);
441 base16_encode(fp
, HEX_DIGEST_LEN
+1, d
, DIGEST_LEN
);
442 fp
[HEX_DIGEST_LEN
] = '+';
443 fp
[HEX_DIGEST_LEN
+1] = '\0';
444 smartlist_add(fps
, fp
);
446 if (smartlist_len(fps
) == 1) {
447 /* we didn't add any: they were all pending */
448 SMARTLIST_FOREACH(fps
, char *, cp
, tor_free(cp
));
452 resource
= smartlist_join_strings(fps
, "", 0, NULL
);
453 resource
[strlen(resource
)-1] = '\0';
454 SMARTLIST_FOREACH(fps
, char *, cp
, tor_free(cp
));
457 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE
, 0,
462 smartlist_free(missing_digests
);
463 digestmap_free(pending
, NULL
);
466 /* Router descriptor storage.
468 * Routerdescs are stored in a big file, named "cached-descriptors". As new
469 * routerdescs arrive, we append them to a journal file named
470 * "cached-descriptors.new".
472 * From time to time, we replace "cached-descriptors" with a new file
473 * containing only the live, non-superseded descriptors, and clear
474 * cached-routers.new.
476 * On startup, we read both files.
479 /** Helper: return 1 iff the router log is so big we want to rebuild the
482 router_should_rebuild_store(desc_store_t
*store
)
484 if (store
->store_len
> (1<<16))
485 return (store
->journal_len
> store
->store_len
/ 2 ||
486 store
->bytes_dropped
> store
->store_len
/ 2);
488 return store
->journal_len
> (1<<15);
491 static INLINE desc_store_t
*
492 desc_get_store(routerlist_t
*rl
, signed_descriptor_t
*sd
)
494 if (sd
->is_extrainfo
)
495 return &rl
->extrainfo_store
;
497 return &rl
->desc_store
;
500 /** Add the signed_descriptor_t in <b>desc</b> to the router
501 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
502 * offset appropriately. */
504 signed_desc_append_to_journal(signed_descriptor_t
*desc
,
507 char *fname
= get_datadir_fname_suffix(store
->fname_base
, ".new");
508 const char *body
= signed_descriptor_get_body_impl(desc
,1);
509 size_t len
= desc
->signed_descriptor_len
+ desc
->annotations_len
;
511 tor_assert(len
== strlen(body
));
513 if (append_bytes_to_file(fname
, body
, len
, 1)) {
514 log_warn(LD_FS
, "Unable to store router descriptor");
518 desc
->saved_location
= SAVED_IN_JOURNAL
;
521 desc
->saved_offset
= store
->journal_len
;
522 store
->journal_len
+= len
;
527 /** Sorting helper: return <0, 0, or >0 depending on whether the
528 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
529 * the signed_descriptor_t* in *<b>b</b>. */
531 _compare_signed_descriptors_by_age(const void **_a
, const void **_b
)
533 const signed_descriptor_t
*r1
= *_a
, *r2
= *_b
;
534 return r1
->published_on
- r2
->published_on
;
537 /** If the journal is too long, or if <b>force</b> is true, then atomically
538 * replace the router store with the routers currently in our routerlist, and
539 * clear the journal. Return 0 on success, -1 on failure.
541 * If <b>extrainfo</b> is true, rebuild the extrainfo store; else rebuild the
542 * router descriptor store.
545 router_rebuild_store(int force
, desc_store_t
*store
)
547 smartlist_t
*chunk_list
= NULL
;
548 char *fname
= NULL
, *fname_tmp
= NULL
;
551 smartlist_t
*signed_descriptors
= NULL
;
553 size_t total_expected_len
= 0;
556 if (!force
&& !router_should_rebuild_store(store
))
561 if (store
->type
== EXTRAINFO_STORE
)
562 had_any
= !eimap_isempty(routerlist
->extra_info_map
);
564 had_any
= (smartlist_len(routerlist
->routers
)+
565 smartlist_len(routerlist
->old_routers
))>0;
567 /* Don't save deadweight. */
568 routerlist_remove_old_routers();
570 log_info(LD_DIR
, "Rebuilding %s cache", store
->description
);
572 fname
= get_datadir_fname(store
->fname_base
);
573 fname_tmp
= get_datadir_fname_suffix(store
->fname_base
, ".tmp");
575 chunk_list
= smartlist_create();
577 /* We sort the routers by age to enhance locality on disk. */
578 signed_descriptors
= smartlist_create();
579 if (store
->type
== EXTRAINFO_STORE
) {
581 for (iter
= eimap_iter_init(routerlist
->extra_info_map
);
582 !eimap_iter_done(iter
);
583 iter
= eimap_iter_next(routerlist
->extra_info_map
, iter
)) {
586 eimap_iter_get(iter
, &key
, &ei
);
587 smartlist_add(signed_descriptors
, &ei
->cache_info
);
590 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
591 smartlist_add(signed_descriptors
, sd
));
592 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
593 smartlist_add(signed_descriptors
, &ri
->cache_info
));
596 smartlist_sort(signed_descriptors
, _compare_signed_descriptors_by_age
);
598 /* Now, add the appropriate members to chunk_list */
599 SMARTLIST_FOREACH(signed_descriptors
, signed_descriptor_t
*, sd
,
602 const char *body
= signed_descriptor_get_body_impl(sd
, 1);
604 log_warn(LD_BUG
, "No descriptor available for router.");
607 if (sd
->do_not_cache
) {
611 c
= tor_malloc(sizeof(sized_chunk_t
));
613 c
->len
= sd
->signed_descriptor_len
+ sd
->annotations_len
;
614 total_expected_len
+= c
->len
;
615 smartlist_add(chunk_list
, c
);
618 if (write_chunks_to_file(fname_tmp
, chunk_list
, 1)<0) {
619 log_warn(LD_FS
, "Error writing router store to disk.");
623 /* Our mmap is now invalid. */
625 tor_munmap_file(store
->mmap
);
629 if (replace_file(fname_tmp
, fname
)<0) {
630 log_warn(LD_FS
, "Error replacing old router store.");
635 store
->mmap
= tor_mmap_file(fname
);
637 if (errno
== ERANGE
) {
639 if (total_expected_len
) {
640 log_warn(LD_FS
, "We wrote some bytes to a new descriptor file at '%s',"
641 " but when we went to mmap it, it was empty!", fname
);
642 } else if (had_any
) {
643 log_notice(LD_FS
, "We just removed every descriptor in '%s'. This is "
644 "okay if we're just starting up after a long time. "
645 "Otherwise, it's a bug.",
647 /* XXX020 should we reduce the severity of the above log
648 * message? I don't think we see it much in practice. -RD */
651 log_warn(LD_FS
, "Unable to mmap new descriptor file at '%s'.",fname
);
655 log_info(LD_DIR
, "Reconstructing pointers into cache");
658 SMARTLIST_FOREACH(signed_descriptors
, signed_descriptor_t
*, sd
,
660 if (sd
->do_not_cache
)
662 sd
->saved_location
= SAVED_IN_CACHE
;
664 tor_free(sd
->signed_descriptor_body
); // sets it to null
665 sd
->saved_offset
= offset
;
667 offset
+= sd
->signed_descriptor_len
+ sd
->annotations_len
;
668 signed_descriptor_get_body(sd
); /* reconstruct and assert */
672 fname
= get_datadir_fname_suffix(store
->fname_base
, ".new");
673 write_str_to_file(fname
, "", 1);
676 store
->store_len
= (size_t) offset
;
677 store
->journal_len
= 0;
678 store
->bytes_dropped
= 0;
680 if (signed_descriptors
)
681 smartlist_free(signed_descriptors
);
684 SMARTLIST_FOREACH(chunk_list
, sized_chunk_t
*, c
, tor_free(c
));
685 smartlist_free(chunk_list
);
690 /** Helper: Reload a cache file and its associated journal, setting metadata
691 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
692 * else reload the router descriptor store. */
694 router_reload_router_list_impl(desc_store_t
*store
)
696 char *fname
= NULL
, *altname
= NULL
, *contents
= NULL
;
698 int read_from_old_location
= 0;
699 int extrainfo
= (store
->type
== EXTRAINFO_STORE
);
700 time_t now
= time(NULL
);
701 store
->journal_len
= store
->store_len
= 0;
703 fname
= get_datadir_fname(store
->fname_base
);
704 if (store
->fname_alt_base
)
705 altname
= get_datadir_fname(store
->fname_alt_base
);
707 if (store
->mmap
) /* get rid of it first */
708 tor_munmap_file(store
->mmap
);
711 store
->mmap
= tor_mmap_file(fname
);
712 if (!store
->mmap
&& altname
&& file_status(altname
) == FN_FILE
) {
713 read_from_old_location
= 1;
714 log_notice(LD_DIR
, "Couldn't read %s; trying to load routers from old "
715 "location %s.", fname
, altname
);
716 if ((store
->mmap
= tor_mmap_file(altname
)))
717 read_from_old_location
= 1;
719 if (altname
&& !read_from_old_location
) {
720 remove_file_if_very_old(altname
, now
);
723 store
->store_len
= store
->mmap
->size
;
725 router_load_extrainfo_from_string(store
->mmap
->data
,
726 store
->mmap
->data
+store
->mmap
->size
,
727 SAVED_IN_CACHE
, NULL
, 0);
729 router_load_routers_from_string(store
->mmap
->data
,
730 store
->mmap
->data
+store
->mmap
->size
,
731 SAVED_IN_CACHE
, NULL
, 0, NULL
);
735 fname
= get_datadir_fname_suffix(store
->fname_base
, ".new");
736 if (file_status(fname
) == FN_FILE
)
737 contents
= read_file_to_str(fname
, RFTS_BIN
|RFTS_IGNORE_MISSING
, &st
);
738 if (read_from_old_location
) {
740 altname
= get_datadir_fname_suffix(store
->fname_alt_base
, ".new");
742 contents
= read_file_to_str(altname
, RFTS_BIN
|RFTS_IGNORE_MISSING
, &st
);
744 remove_file_if_very_old(altname
, now
);
748 router_load_extrainfo_from_string(contents
, NULL
,SAVED_IN_JOURNAL
,
751 router_load_routers_from_string(contents
, NULL
, SAVED_IN_JOURNAL
,
753 store
->journal_len
= (size_t) st
.st_size
;
760 if (store
->journal_len
|| read_from_old_location
) {
761 /* Always clear the journal on startup.*/
762 router_rebuild_store(1, store
);
763 } else if (!extrainfo
) {
764 /* Don't cache expired routers. (This is in an else because
765 * router_rebuild_store() also calls remove_old_routers().) */
766 routerlist_remove_old_routers();
772 /** Load all cached router descriptors and extra-info documents from the
773 * store. Return 0 on success and -1 on failure.
776 router_reload_router_list(void)
778 routerlist_t
*rl
= router_get_routerlist();
779 if (router_reload_router_list_impl(&rl
->desc_store
))
781 if (router_reload_router_list_impl(&rl
->extrainfo_store
))
786 /** Return a smartlist containing a list of trusted_dir_server_t * for all
787 * known trusted dirservers. Callers must not modify the list or its
791 router_get_trusted_dir_servers(void)
793 if (!trusted_dir_servers
)
794 trusted_dir_servers
= smartlist_create();
796 return trusted_dir_servers
;
799 /** Try to find a running dirserver that supports operations of <b>type</b>.
801 * If there are no running dirservers in our routerlist and the
802 * <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the authoritative ones
803 * as running again, and pick one.
805 * If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
806 * dirservers that we can't reach.
808 * If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
809 * (if we're a dirserver).
811 * Don't pick an authority if any non-authority is viable; try to avoid using
812 * servers that have returned 503 recently.
815 router_pick_directory_server(authority_type_t type
, int flags
)
817 routerstatus_t
*choice
;
818 if (get_options()->PreferTunneledDirConns
)
819 flags
|= _PDS_PREFER_TUNNELED_DIR_CONNS
;
824 choice
= router_pick_directory_server_impl(type
, flags
);
825 if (choice
|| !(flags
& PDS_RETRY_IF_NO_SERVERS
))
829 "No reachable router entries for dirservers. "
830 "Trying them all again.");
831 /* mark all authdirservers as up again */
832 mark_all_trusteddirservers_up();
834 choice
= router_pick_directory_server_impl(type
, flags
);
838 /* XXXX020 what's the point of *reloading* and trying again?? -NM */
839 log_info(LD_DIR
,"Still no %s router entries. Reloading and trying again.",
840 (flags
& PDS_IGNORE_FASCISTFIREWALL
) ? "known" : "reachable");
841 if (router_reload_router_list()) {
844 /* give it one last try */
845 choice
= router_pick_directory_server_impl(type
, flags
);
849 /** Return the trusted_dir_server_t for the directory authority whose identity
850 * key hashes to <b>digest</b>, or NULL if no such authority is known.
852 trusted_dir_server_t
*
853 router_get_trusteddirserver_by_digest(const char *digest
)
855 if (!trusted_dir_servers
)
858 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ds
,
860 if (!memcmp(ds
->digest
, digest
, DIGEST_LEN
))
867 /** Return the trusted_dir_server_t for the directory authority whose identity
868 * key hashes to <b>digest</b>, or NULL if no such authority is known.
870 trusted_dir_server_t
*
871 trusteddirserver_get_by_v3_auth_digest(const char *digest
)
873 if (!trusted_dir_servers
)
876 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ds
,
878 if (!memcmp(ds
->v3_identity_digest
, digest
, DIGEST_LEN
) &&
879 (ds
->type
& V3_AUTHORITY
))
886 /** Try to find a running trusted dirserver. Flags are as for
887 * router_pick_directory_server.
890 router_pick_trusteddirserver(authority_type_t type
, int flags
)
892 routerstatus_t
*choice
;
893 if (get_options()->PreferTunneledDirConns
)
894 flags
|= _PDS_PREFER_TUNNELED_DIR_CONNS
;
896 choice
= router_pick_trusteddirserver_impl(type
, flags
);
897 if (choice
|| !(flags
& PDS_RETRY_IF_NO_SERVERS
))
901 "No trusted dirservers are reachable. Trying them all again.");
902 mark_all_trusteddirservers_up();
903 return router_pick_trusteddirserver_impl(type
, flags
);
906 /** How long do we avoid using a directory server after it's given us a 503? */
907 #define DIR_503_TIMEOUT (60*60)
909 /** Pick a random running valid directory server/mirror from our
910 * routerlist. Arguments are as for router_pick_directory_server(), except
911 * that RETRY_IF_NO_SERVERS is ignored, and:
913 * If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers
914 * that we can use with BEGINDIR.
916 static routerstatus_t
*
917 router_pick_directory_server_impl(authority_type_t type
, int flags
)
919 routerstatus_t
*result
;
920 smartlist_t
*direct
, *tunnel
;
921 smartlist_t
*trusted_direct
, *trusted_tunnel
;
922 smartlist_t
*overloaded_direct
, *overloaded_tunnel
;
923 time_t now
= time(NULL
);
924 const networkstatus_vote_t
*consensus
= networkstatus_get_latest_consensus();
925 int requireother
= ! (flags
& PDS_ALLOW_SELF
);
926 int fascistfirewall
= ! (flags
& PDS_IGNORE_FASCISTFIREWALL
);
927 int prefer_tunnel
= (flags
& _PDS_PREFER_TUNNELED_DIR_CONNS
);
932 direct
= smartlist_create();
933 tunnel
= smartlist_create();
934 trusted_direct
= smartlist_create();
935 trusted_tunnel
= smartlist_create();
936 overloaded_direct
= smartlist_create();
937 overloaded_tunnel
= smartlist_create();
939 /* Find all the running dirservers we know about. */
940 SMARTLIST_FOREACH(consensus
->routerstatus_list
, routerstatus_t
*, status
,
943 int is_overloaded
= status
->last_dir_503_at
+ DIR_503_TIMEOUT
> now
;
944 if (!status
->is_running
|| !status
->dir_port
|| !status
->is_valid
)
946 if (status
->is_bad_directory
)
948 if (requireother
&& router_digest_is_me(status
->identity_digest
))
950 if (type
& V3_AUTHORITY
) {
951 if (!(status
->version_supports_v3_dir
||
952 router_digest_is_trusted_dir_type(status
->identity_digest
,
956 is_trusted
= router_digest_is_trusted_dir(status
->identity_digest
);
957 if ((type
& V2_AUTHORITY
) && !(status
->is_v2_dir
|| is_trusted
))
959 if ((type
& EXTRAINFO_CACHE
) &&
960 !router_supports_extrainfo(status
->identity_digest
, 0))
963 status
->version_supports_begindir
&&
965 fascist_firewall_allows_address_or(status
->addr
, status
->or_port
)))
966 smartlist_add(is_trusted
? trusted_tunnel
:
967 is_overloaded
? overloaded_tunnel
: tunnel
, status
);
968 else if (!fascistfirewall
||
969 fascist_firewall_allows_address_dir(status
->addr
,
971 smartlist_add(is_trusted
? trusted_direct
:
972 is_overloaded
? overloaded_direct
: direct
, status
);
975 if (smartlist_len(tunnel
)) {
976 result
= routerstatus_sl_choose_by_bandwidth(tunnel
);
977 } else if (smartlist_len(overloaded_tunnel
)) {
978 result
= routerstatus_sl_choose_by_bandwidth(overloaded_tunnel
);
979 } else if (smartlist_len(trusted_tunnel
)) {
980 /* FFFF We don't distinguish between trusteds and overloaded trusteds
981 * yet. Maybe one day we should. */
982 /* FFFF We also don't load balance over authorities yet. I think this
983 * is a feature, but it could easily be a bug. -RD */
984 result
= smartlist_choose(trusted_tunnel
);
985 } else if (smartlist_len(direct
)) {
986 result
= routerstatus_sl_choose_by_bandwidth(direct
);
987 } else if (smartlist_len(overloaded_direct
)) {
988 result
= routerstatus_sl_choose_by_bandwidth(overloaded_direct
);
990 result
= smartlist_choose(trusted_direct
);
992 smartlist_free(direct
);
993 smartlist_free(tunnel
);
994 smartlist_free(trusted_direct
);
995 smartlist_free(trusted_tunnel
);
996 smartlist_free(overloaded_direct
);
997 smartlist_free(overloaded_tunnel
);
1001 /** Choose randomly from among the trusted dirservers that are up. Flags
1002 * are as for router_pick_directory_server_impl().
1004 static routerstatus_t
*
1005 router_pick_trusteddirserver_impl(authority_type_t type
, int flags
)
1007 smartlist_t
*direct
, *tunnel
;
1008 smartlist_t
*overloaded_direct
, *overloaded_tunnel
;
1009 routerinfo_t
*me
= router_get_my_routerinfo();
1010 routerstatus_t
*result
;
1011 time_t now
= time(NULL
);
1012 int requireother
= ! (flags
& PDS_ALLOW_SELF
);
1013 int fascistfirewall
= ! (flags
& PDS_IGNORE_FASCISTFIREWALL
);
1014 int prefer_tunnel
= (flags
& _PDS_PREFER_TUNNELED_DIR_CONNS
);
1016 if (!trusted_dir_servers
)
1019 direct
= smartlist_create();
1020 tunnel
= smartlist_create();
1021 overloaded_direct
= smartlist_create();
1022 overloaded_tunnel
= smartlist_create();
1024 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, d
,
1027 d
->fake_status
.last_dir_503_at
+ DIR_503_TIMEOUT
> now
;
1028 if (!d
->is_running
) continue;
1029 if ((type
& d
->type
) == 0)
1031 if ((type
& EXTRAINFO_CACHE
) &&
1032 !router_supports_extrainfo(d
->digest
, 1))
1034 if (requireother
&& me
&& router_digest_is_me(d
->digest
))
1036 if (prefer_tunnel
&&
1038 (!fascistfirewall
||
1039 fascist_firewall_allows_address_or(d
->addr
, d
->or_port
)))
1040 smartlist_add(is_overloaded
? overloaded_tunnel
: tunnel
,
1042 else if (!fascistfirewall
||
1043 fascist_firewall_allows_address_dir(d
->addr
, d
->dir_port
))
1044 smartlist_add(is_overloaded
? overloaded_direct
: direct
,
1048 if (smartlist_len(tunnel
)) {
1049 result
= smartlist_choose(tunnel
);
1050 } else if (smartlist_len(overloaded_tunnel
)) {
1051 result
= smartlist_choose(overloaded_tunnel
);
1052 } else if (smartlist_len(direct
)) {
1053 result
= smartlist_choose(direct
);
1055 result
= smartlist_choose(overloaded_direct
);
1058 smartlist_free(direct
);
1059 smartlist_free(tunnel
);
1060 smartlist_free(overloaded_direct
);
1061 smartlist_free(overloaded_tunnel
);
1065 /** Go through and mark the authoritative dirservers as up. */
1067 mark_all_trusteddirservers_up(void)
1070 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1071 if (router_digest_is_trusted_dir(router
->cache_info
.identity_digest
) &&
1072 router
->dir_port
> 0) {
1073 router
->is_running
= 1;
1076 if (trusted_dir_servers
) {
1077 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, dir
,
1080 dir
->is_running
= 1;
1081 download_status_reset(&dir
->v2_ns_dl_status
);
1082 rs
= router_get_consensus_status_by_id(dir
->digest
);
1083 if (rs
&& !rs
->is_running
) {
1085 rs
->last_dir_503_at
= 0;
1086 control_event_networkstatus_changed_single(rs
);
1090 router_dir_info_changed();
1093 /** Reset all internal variables used to count failed downloads of network
1094 * status objects. */
1096 router_reset_status_download_failures(void)
1098 mark_all_trusteddirservers_up();
1101 /** Return true iff router1 and router2 have the same /16 network. */
1103 routers_in_same_network_family(routerinfo_t
*r1
, routerinfo_t
*r2
)
1105 return (r1
->addr
& 0xffff0000) == (r2
->addr
& 0xffff0000);
1108 /** Look through the routerlist and identify routers that
1109 * advertise the same /16 network address as <b>router</b>.
1110 * Add each of them to <b>sl</b>.
1113 routerlist_add_network_family(smartlist_t
*sl
, routerinfo_t
*router
)
1115 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, r
,
1117 if (router
!= r
&& routers_in_same_network_family(router
, r
))
1118 smartlist_add(sl
, r
);
1122 /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
1123 * This is used to make sure we don't pick siblings in a single path.
1126 routerlist_add_family(smartlist_t
*sl
, routerinfo_t
*router
)
1130 or_options_t
*options
= get_options();
1132 /* First, add any routers with similar network addresses. */
1133 if (options
->EnforceDistinctSubnets
)
1134 routerlist_add_network_family(sl
, router
);
1136 if (router
->declared_family
) {
1137 /* Add every r such that router declares familyness with r, and r
1138 * declares familyhood with router. */
1139 SMARTLIST_FOREACH(router
->declared_family
, const char *, n
,
1141 if (!(r
= router_get_by_nickname(n
, 0)))
1143 if (!r
->declared_family
)
1145 SMARTLIST_FOREACH(r
->declared_family
, const char *, n2
,
1147 if (router_nickname_matches(router
, n2
))
1148 smartlist_add(sl
, r
);
1153 /* If the user declared any families locally, honor those too. */
1154 for (cl
= options
->NodeFamilies
; cl
; cl
= cl
->next
) {
1155 if (router_nickname_is_in_list(router
, cl
->value
)) {
1156 add_nickname_list_to_smartlist(sl
, cl
->value
, 0);
1161 /** Return true iff r is named by some nickname in <b>lst</b>. */
1163 router_in_nickname_smartlist(smartlist_t
*lst
, routerinfo_t
*r
)
1166 SMARTLIST_FOREACH(lst
, const char *, name
,
1167 if (router_nickname_matches(r
, name
))
1172 /** Return true iff r1 and r2 are in the same family, but not the same
1175 routers_in_same_family(routerinfo_t
*r1
, routerinfo_t
*r2
)
1177 or_options_t
*options
= get_options();
1180 if (options
->EnforceDistinctSubnets
&& routers_in_same_network_family(r1
,r2
))
1183 if (router_in_nickname_smartlist(r1
->declared_family
, r2
) &&
1184 router_in_nickname_smartlist(r2
->declared_family
, r1
))
1187 for (cl
= options
->NodeFamilies
; cl
; cl
= cl
->next
) {
1188 if (router_nickname_is_in_list(r1
, cl
->value
) &&
1189 router_nickname_is_in_list(r2
, cl
->value
))
1195 /** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
1196 * see which nicknames in <b>list</b> name routers in our routerlist, and add
1197 * the routerinfos for those routers to <b>sl</b>. If <b>must_be_running</b>,
1198 * only include routers that we think are running.
1199 * Warn if any non-Named routers are specified by nickname.
1202 add_nickname_list_to_smartlist(smartlist_t
*sl
, const char *list
,
1203 int must_be_running
)
1205 routerinfo_t
*router
;
1206 smartlist_t
*nickname_list
;
1207 int have_dir_info
= router_have_minimum_dir_info();
1210 return; /* nothing to do */
1213 nickname_list
= smartlist_create();
1214 if (!warned_nicknames
)
1215 warned_nicknames
= smartlist_create();
1217 smartlist_split_string(nickname_list
, list
, ",",
1218 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1220 SMARTLIST_FOREACH(nickname_list
, const char *, nick
, {
1222 if (!is_legal_nickname_or_hexdigest(nick
)) {
1223 log_warn(LD_CONFIG
, "Nickname '%s' is misformed; skipping", nick
);
1226 router
= router_get_by_nickname(nick
, 1);
1227 warned
= smartlist_string_isin(warned_nicknames
, nick
);
1229 if (!must_be_running
|| router
->is_running
) {
1230 smartlist_add(sl
,router
);
1232 } else if (!router_get_consensus_status_by_nickname(nick
,1)) {
1234 log_fn(have_dir_info
? LOG_WARN
: LOG_INFO
, LD_CONFIG
,
1235 "Nickname list includes '%s' which isn't a known router.",nick
);
1236 smartlist_add(warned_nicknames
, tor_strdup(nick
));
1240 SMARTLIST_FOREACH(nickname_list
, char *, nick
, tor_free(nick
));
1241 smartlist_free(nickname_list
);
1244 /** Return 1 iff any member of the (possibly NULL) comma-separated list
1245 * <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
1249 router_nickname_is_in_list(routerinfo_t
*router
, const char *list
)
1251 smartlist_t
*nickname_list
;
1255 return 0; /* definitely not */
1258 nickname_list
= smartlist_create();
1259 smartlist_split_string(nickname_list
, list
, ",",
1260 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1261 SMARTLIST_FOREACH(nickname_list
, const char *, cp
,
1262 if (router_nickname_matches(router
, cp
)) {v
=1;break;});
1263 SMARTLIST_FOREACH(nickname_list
, char *, cp
, tor_free(cp
));
1264 smartlist_free(nickname_list
);
1268 /** Add every suitable router from our routerlist to <b>sl</b>, so that
1269 * we can pick a node for a circuit.
1272 router_add_running_routers_to_smartlist(smartlist_t
*sl
, int allow_invalid
,
1273 int need_uptime
, int need_capacity
,
1279 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1281 if (router
->is_running
&&
1282 router
->purpose
== ROUTER_PURPOSE_GENERAL
&&
1283 (router
->is_valid
|| allow_invalid
) &&
1284 !router_is_unreliable(router
, need_uptime
,
1285 need_capacity
, need_guard
)) {
1286 /* If it's running, and it's suitable according to the
1287 * other flags we had in mind */
1288 smartlist_add(sl
, router
);
1293 /** Look through the routerlist until we find a router that has my key.
1296 routerlist_find_my_routerinfo(void)
1301 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1303 if (router_is_me(router
))
1309 /** Find a router that's up, that has this IP address, and
1310 * that allows exit to this address:port, or return NULL if there
1314 router_find_exact_exit_enclave(const char *address
, uint16_t port
)
1319 if (!tor_inet_aton(address
, &in
))
1320 return NULL
; /* it's not an IP already */
1321 addr
= ntohl(in
.s_addr
);
1323 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1325 if (router
->is_running
&&
1326 router
->addr
== addr
&&
1327 compare_addr_to_addr_policy(addr
, port
, router
->exit_policy
) ==
1328 ADDR_POLICY_ACCEPTED
)
1334 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1335 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1336 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1338 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1341 router_is_unreliable(routerinfo_t
*router
, int need_uptime
,
1342 int need_capacity
, int need_guard
)
1344 if (need_uptime
&& !router
->is_stable
)
1346 if (need_capacity
&& !router
->is_fast
)
1348 if (need_guard
&& !router
->is_possible_guard
)
1353 /** Return the smaller of the router's configured BandwidthRate
1354 * and its advertised capacity. */
1356 router_get_advertised_bandwidth(routerinfo_t
*router
)
1358 if (router
->bandwidthcapacity
< router
->bandwidthrate
)
1359 return router
->bandwidthcapacity
;
1360 return router
->bandwidthrate
;
1363 /** Do not weight any declared bandwidth more than this much when picking
1364 * routers by bandwidth. */
1365 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
1367 /** Eventually, the number we return will come from the directory
1368 * consensus, so clients can dynamically update to better numbers.
1370 * But for now, or in case there is no consensus available, just return
1371 * a sufficient default. */
1373 get_max_believable_bandwidth(void)
1375 return DEFAULT_MAX_BELIEVABLE_BANDWIDTH
;
1378 /** Helper function:
1379 * choose a random element of smartlist <b>sl</b>, weighted by
1380 * the advertised bandwidth of each element.
1382 * If <b>statuses</b> is zero, then <b>sl</b> is a list of
1383 * routerinfo_t's. Otherwise it's a list of routerstatus_t's.
1385 * If <b>for_exit</b>, we're picking an exit node: consider all nodes'
1386 * bandwidth equally regardless of their Exit status, since there may be
1387 * some in the list because they exit to obscure ports. If not <b>for_exit</b>,
1388 * we're picking a non-exit node: weight exit-node's bandwidth less
1389 * depending on the smallness of the fraction of Exit-to-total bandwidth.
1391 * If <b>for_guard</b>, we're picking a guard node: consider all guard's
1392 * bandwidth equally. Otherwise, weight guards proportionally less.
1396 smartlist_choose_by_bandwidth(smartlist_t
*sl
, bandwidth_weight_rule_t rule
,
1400 routerinfo_t
*router
;
1401 routerstatus_t
*status
=NULL
;
1402 int32_t *bandwidths
;
1405 uint64_t total_nonexit_bw
= 0, total_exit_bw
= 0, total_bw
= 0;
1406 uint64_t total_nonguard_bw
= 0, total_guard_bw
= 0;
1407 uint64_t rand_bw
, tmp
;
1409 double guard_weight
;
1411 bitarray_t
*exit_bits
;
1412 bitarray_t
*guard_bits
;
1413 uint32_t max_believable_bw
= get_max_believable_bandwidth();
1415 /* Can't choose exit and guard at same time */
1416 tor_assert(rule
== NO_WEIGHTING
||
1417 rule
== WEIGHT_FOR_EXIT
||
1418 rule
== WEIGHT_FOR_GUARD
);
1420 /* First count the total bandwidth weight, and make a list
1421 * of each value. <0 means "unknown; no routerinfo." We use the
1422 * bits of negative values to remember whether the router was fast (-x)&1
1423 * and whether it was an exit (-x)&2 or guard (-x)&4. Yes, it's a hack. */
1424 bandwidths
= tor_malloc(sizeof(int32_t)*smartlist_len(sl
));
1425 exit_bits
= bitarray_init_zero(smartlist_len(sl
));
1426 guard_bits
= bitarray_init_zero(smartlist_len(sl
));
1428 /* Iterate over all the routerinfo_t or routerstatus_t, and */
1429 for (i
= 0; i
< (unsigned)smartlist_len(sl
); ++i
) {
1430 /* first, learn what bandwidth we think i has */
1433 uint32_t this_bw
= 0;
1435 /* need to extract router info */
1436 status
= smartlist_get(sl
, i
);
1437 router
= router_get_by_digest(status
->identity_digest
);
1438 is_exit
= status
->is_exit
;
1439 is_guard
= status
->is_possible_guard
;
1441 this_bw
= router_get_advertised_bandwidth(router
);
1442 } else { /* guess */
1444 flags
= status
->is_fast
? 1 : 0;
1445 flags
|= is_exit
? 2 : 0;
1446 flags
|= is_guard
? 4 : 0;
1449 router
= smartlist_get(sl
, i
);
1450 is_exit
= router
->is_exit
;
1451 is_guard
= router
->is_possible_guard
;
1452 this_bw
= router_get_advertised_bandwidth(router
);
1455 bitarray_set(exit_bits
, i
);
1457 bitarray_set(guard_bits
, i
);
1458 /* if they claim something huge, don't believe it */
1459 if (this_bw
> max_believable_bw
) {
1460 char fp
[HEX_DIGEST_LEN
+1];
1461 base16_encode(fp
, sizeof(fp
), statuses
?
1462 status
->identity_digest
:
1463 router
->cache_info
.identity_digest
,
1465 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
,
1466 "Bandwidth %d for router %s (%s) exceeds allowed max %d, capping",
1467 this_bw
, router
? router
->nickname
: "(null)",
1468 fp
, max_believable_bw
);
1469 this_bw
= max_believable_bw
;
1472 bandwidths
[i
] = (int32_t) this_bw
; // safe since MAX_BELIEVABLE<INT32_MAX
1474 total_guard_bw
+= this_bw
;
1476 total_nonguard_bw
+= this_bw
;
1478 total_exit_bw
+= this_bw
;
1480 total_nonexit_bw
+= this_bw
;
1483 bandwidths
[i
] = -flags
;
1487 /* Now, fill in the unknown values. */
1489 int32_t avg_fast
, avg_slow
;
1490 if (total_exit_bw
+total_nonexit_bw
) {
1491 /* if there's some bandwidth, there's at least one known router,
1492 * so no worries about div by 0 here */
1493 int n_known
= smartlist_len(sl
)-n_unknown
;
1494 avg_fast
= avg_slow
= (int32_t)
1495 ((total_exit_bw
+total_nonexit_bw
)/((uint64_t) n_known
));
1500 for (i
=0; i
<(unsigned)smartlist_len(sl
); ++i
) {
1501 int32_t bw
= bandwidths
[i
];
1504 is_exit
= ((-bw
)&2);
1505 is_guard
= ((-bw
)&4);
1506 bandwidths
[i
] = ((-bw
)&1) ? avg_fast
: avg_slow
;
1508 total_exit_bw
+= bandwidths
[i
];
1510 total_nonexit_bw
+= bandwidths
[i
];
1512 total_guard_bw
+= bandwidths
[i
];
1514 total_nonguard_bw
+= bandwidths
[i
];
1518 /* If there's no bandwidth at all, pick at random. */
1519 if (!(total_exit_bw
+total_nonexit_bw
)) {
1520 tor_free(bandwidths
);
1521 tor_free(exit_bits
);
1522 tor_free(guard_bits
);
1523 return smartlist_choose(sl
);
1526 /* Figure out how to weight exits and guards */
1528 double all_bw
= U64_TO_DBL(total_exit_bw
+total_nonexit_bw
);
1529 double exit_bw
= U64_TO_DBL(total_exit_bw
);
1530 double guard_bw
= U64_TO_DBL(total_guard_bw
);
1532 * For detailed derivation of this formula, see
1533 * http://archives.seul.org/or/dev/Jul-2007/msg00056.html
1535 if (rule
== WEIGHT_FOR_EXIT
)
1538 exit_weight
= 1.0 - all_bw
/(3.0*exit_bw
);
1540 if (rule
== WEIGHT_FOR_GUARD
)
1543 guard_weight
= 1.0 - all_bw
/(3.0*guard_bw
);
1545 if (exit_weight
<= 0.0)
1548 if (guard_weight
<= 0.0)
1552 for (i
=0; i
< (unsigned)smartlist_len(sl
); i
++) {
1553 is_exit
= bitarray_is_set(exit_bits
, i
);
1554 is_guard
= bitarray_is_set(guard_bits
, i
);
1555 if (is_exit
&& is_guard
)
1556 total_bw
+= ((uint64_t)(bandwidths
[i
] * exit_weight
* guard_weight
));
1558 total_bw
+= ((uint64_t)(bandwidths
[i
] * guard_weight
));
1560 total_bw
+= ((uint64_t)(bandwidths
[i
] * exit_weight
));
1562 total_bw
+= bandwidths
[i
];
1565 log_debug(LD_CIRC
, "Total weighted bw = "U64_FORMAT
1566 ", exit bw = "U64_FORMAT
1567 ", nonexit bw = "U64_FORMAT
", exit weight = %lf "
1569 ", guard bw = "U64_FORMAT
1570 ", nonguard bw = "U64_FORMAT
", guard weight = %lf "
1571 "(for guard == %d)",
1572 U64_PRINTF_ARG(total_bw
),
1573 U64_PRINTF_ARG(total_exit_bw
), U64_PRINTF_ARG(total_nonexit_bw
),
1574 exit_weight
, (int)(rule
== WEIGHT_FOR_EXIT
),
1575 U64_PRINTF_ARG(total_guard_bw
), U64_PRINTF_ARG(total_nonguard_bw
),
1576 guard_weight
, (int)(rule
== WEIGHT_FOR_GUARD
));
1578 /* Almost done: choose a random value from the bandwidth weights. */
1579 rand_bw
= crypto_rand_uint64(total_bw
);
1581 /* Last, count through sl until we get to the element we picked */
1583 for (i
=0; i
< (unsigned)smartlist_len(sl
); i
++) {
1584 is_exit
= bitarray_is_set(exit_bits
, i
);
1585 is_guard
= bitarray_is_set(guard_bits
, i
);
1587 /* Weights can be 0 if not counting guards/exits */
1588 if (is_exit
&& is_guard
)
1589 tmp
+= ((uint64_t)(bandwidths
[i
] * exit_weight
* guard_weight
));
1591 tmp
+= ((uint64_t)(bandwidths
[i
] * guard_weight
));
1593 tmp
+= ((uint64_t)(bandwidths
[i
] * exit_weight
));
1595 tmp
+= bandwidths
[i
];
1600 if (i
== (unsigned)smartlist_len(sl
)) {
1601 /* This was once possible due to round-off error, but shouldn't be able
1602 * to occur any longer. */
1603 tor_fragile_assert();
1605 log_warn(LD_BUG
, "Round-off error in computing bandwidth had an effect on "
1606 " which router we chose. Please tell the developers. "
1607 U64_FORMAT
" " U64_FORMAT
" " U64_FORMAT
, U64_PRINTF_ARG(tmp
),
1608 U64_PRINTF_ARG(rand_bw
), U64_PRINTF_ARG(total_bw
));
1610 tor_free(bandwidths
);
1611 tor_free(exit_bits
);
1612 tor_free(guard_bits
);
1613 return smartlist_get(sl
, i
);
1616 /** Choose a random element of router list <b>sl</b>, weighted by
1617 * the advertised bandwidth of each router.
1620 routerlist_sl_choose_by_bandwidth(smartlist_t
*sl
,
1621 bandwidth_weight_rule_t rule
)
1623 return smartlist_choose_by_bandwidth(sl
, rule
, 0);
1626 /** Choose a random element of status list <b>sl</b>, weighted by
1627 * the advertised bandwidth of each status.
1630 routerstatus_sl_choose_by_bandwidth(smartlist_t
*sl
)
1632 /* We are choosing neither exit nor guard here. Weight accordingly. */
1633 return smartlist_choose_by_bandwidth(sl
, NO_WEIGHTING
, 1);
1636 /** Return a random running router from the routerlist. If any node
1637 * named in <b>preferred</b> is available, pick one of those. Never
1638 * pick a node named in <b>excluded</b>, or whose routerinfo is in
1639 * <b>excludedsmartlist</b>, even if they are the only nodes
1640 * available. If <b>strict</b> is true, never pick any node besides
1641 * those in <b>preferred</b>.
1642 * If <b>need_uptime</b> is non-zero and any router has more than
1643 * a minimum uptime, return one of those.
1644 * If <b>need_capacity</b> is non-zero, weight your choice by the
1645 * advertised capacity of each router.
1646 * If ! <b>allow_invalid</b>, consider only Valid routers.
1647 * If <b>need_guard</b>, consider only Guard routers.
1648 * If <b>weight_for_exit</b>, we weight bandwidths as if picking an exit node,
1649 * otherwise we weight bandwidths for picking a relay node (that is, possibly
1650 * discounting exit nodes).
1653 router_choose_random_node(const char *preferred
,
1654 const char *excluded
,
1655 smartlist_t
*excludedsmartlist
,
1656 int need_uptime
, int need_capacity
,
1658 int allow_invalid
, int strict
,
1659 int weight_for_exit
)
1661 smartlist_t
*sl
, *excludednodes
;
1662 routerinfo_t
*choice
= NULL
;
1663 bandwidth_weight_rule_t rule
;
1665 tor_assert(!(weight_for_exit
&& need_guard
));
1666 rule
= weight_for_exit
? WEIGHT_FOR_EXIT
:
1667 (need_guard
? WEIGHT_FOR_GUARD
: NO_WEIGHTING
);
1669 excludednodes
= smartlist_create();
1670 add_nickname_list_to_smartlist(excludednodes
,excluded
,0);
1672 /* Try the preferred nodes first. Ignore need_uptime and need_capacity
1673 * and need_guard, since the user explicitly asked for these nodes. */
1675 sl
= smartlist_create();
1676 add_nickname_list_to_smartlist(sl
,preferred
,1);
1677 smartlist_subtract(sl
,excludednodes
);
1678 if (excludedsmartlist
)
1679 smartlist_subtract(sl
,excludedsmartlist
);
1680 choice
= smartlist_choose(sl
);
1683 if (!choice
&& !strict
) {
1684 /* Then give up on our preferred choices: any node
1685 * will do that has the required attributes. */
1686 sl
= smartlist_create();
1687 router_add_running_routers_to_smartlist(sl
, allow_invalid
,
1688 need_uptime
, need_capacity
,
1690 smartlist_subtract(sl
,excludednodes
);
1691 if (excludedsmartlist
)
1692 smartlist_subtract(sl
,excludedsmartlist
);
1694 if (need_capacity
|| need_guard
)
1695 choice
= routerlist_sl_choose_by_bandwidth(sl
, rule
);
1697 choice
= smartlist_choose(sl
);
1700 if (!choice
&& (need_uptime
|| need_capacity
|| need_guard
)) {
1701 /* try once more -- recurse but with fewer restrictions. */
1703 "We couldn't find any live%s%s%s routers; falling back "
1704 "to list of all routers.",
1705 need_capacity
?", fast":"",
1706 need_uptime
?", stable":"",
1707 need_guard
?", guard":"");
1708 choice
= router_choose_random_node(
1709 NULL
, excluded
, excludedsmartlist
,
1710 0, 0, 0, allow_invalid
, 0, weight_for_exit
);
1713 smartlist_free(excludednodes
);
1716 log_warn(LD_CIRC
, "All preferred nodes were down when trying to choose "
1717 "node, and the Strict[...]Nodes option is set. Failing.");
1720 "No available nodes when trying to choose node. Failing.");
1726 /** Return true iff the digest of <b>router</b>'s identity key,
1727 * encoded in hexadecimal, matches <b>hexdigest</b> (which is
1728 * optionally prefixed with a single dollar sign). Return false if
1729 * <b>hexdigest</b> is malformed, or it doesn't match. */
1731 router_hex_digest_matches(routerinfo_t
*router
, const char *hexdigest
)
1733 char digest
[DIGEST_LEN
];
1735 tor_assert(hexdigest
);
1736 if (hexdigest
[0] == '$')
1739 len
= strlen(hexdigest
);
1740 if (len
< HEX_DIGEST_LEN
)
1742 else if (len
> HEX_DIGEST_LEN
&&
1743 (hexdigest
[HEX_DIGEST_LEN
] == '=' ||
1744 hexdigest
[HEX_DIGEST_LEN
] == '~')) {
1745 if (strcasecmp(hexdigest
+HEX_DIGEST_LEN
+1, router
->nickname
))
1747 if (hexdigest
[HEX_DIGEST_LEN
] == '=' && !router
->is_named
)
1751 if (base16_decode(digest
, DIGEST_LEN
, hexdigest
, HEX_DIGEST_LEN
)<0)
1753 return (!memcmp(digest
, router
->cache_info
.identity_digest
, DIGEST_LEN
));
1756 /** Return true if <b>router</b>'s nickname matches <b>nickname</b>
1757 * (case-insensitive), or if <b>router's</b> identity key digest
1758 * matches a hexadecimal value stored in <b>nickname</b>. Return
1759 * false otherwise. */
1761 router_nickname_matches(routerinfo_t
*router
, const char *nickname
)
1763 if (nickname
[0]!='$' && !strcasecmp(router
->nickname
, nickname
))
1765 return router_hex_digest_matches(router
, nickname
);
1768 /** Return the router in our routerlist whose (case-insensitive)
1769 * nickname or (case-sensitive) hexadecimal key digest is
1770 * <b>nickname</b>. Return NULL if no such router is known.
1773 router_get_by_nickname(const char *nickname
, int warn_if_unnamed
)
1776 char digest
[DIGEST_LEN
];
1777 routerinfo_t
*best_match
=NULL
;
1779 const char *named_digest
= NULL
;
1781 tor_assert(nickname
);
1784 if (nickname
[0] == '$')
1785 return router_get_by_hexdigest(nickname
);
1786 if (!strcasecmp(nickname
, UNNAMED_ROUTER_NICKNAME
))
1788 if (server_mode(get_options()) &&
1789 !strcasecmp(nickname
, get_options()->Nickname
))
1790 return router_get_my_routerinfo();
1792 maybedigest
= (strlen(nickname
) >= HEX_DIGEST_LEN
) &&
1793 (base16_decode(digest
,DIGEST_LEN
,nickname
,HEX_DIGEST_LEN
) == 0);
1795 if ((named_digest
= networkstatus_get_router_digest_by_nickname(nickname
))) {
1796 return rimap_get(routerlist
->identity_map
, named_digest
);
1798 if (networkstatus_nickname_is_unnamed(nickname
))
1801 /* If we reach this point, there's no canonical value for the nickname. */
1803 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1805 if (!strcasecmp(router
->nickname
, nickname
)) {
1807 if (n_matches
<= 1 || router
->is_running
)
1808 best_match
= router
;
1809 } else if (maybedigest
&&
1810 !memcmp(digest
, router
->cache_info
.identity_digest
, DIGEST_LEN
)
1812 if (router_hex_digest_matches(router
, nickname
))
1815 best_match
= router
; // XXXX020 NM not exactly right.
1820 if (warn_if_unnamed
&& n_matches
> 1) {
1821 smartlist_t
*fps
= smartlist_create();
1822 int any_unwarned
= 0;
1823 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
1828 char fp
[HEX_DIGEST_LEN
+1];
1829 if (strcasecmp(router
->nickname
, nickname
))
1831 rs
= router_get_consensus_status_by_id(
1832 router
->cache_info
.identity_digest
);
1833 if (rs
&& !rs
->name_lookup_warned
) {
1834 rs
->name_lookup_warned
= 1;
1837 base16_encode(fp
, sizeof(fp
),
1838 router
->cache_info
.identity_digest
, DIGEST_LEN
);
1839 dlen
= 32 + HEX_DIGEST_LEN
+ strlen(router
->address
);
1840 desc
= tor_malloc(dlen
);
1841 tor_snprintf(desc
, dlen
, "\"$%s\" for the one at %s:%d",
1842 fp
, router
->address
, router
->or_port
);
1843 smartlist_add(fps
, desc
);
1846 char *alternatives
= smartlist_join_strings(fps
, "; ",0,NULL
);
1848 "There are multiple matches for the nickname \"%s\","
1849 " but none is listed as named by the directory authorities. "
1850 "Choosing one arbitrarily. If you meant one in particular, "
1851 "you should say %s.", nickname
, alternatives
);
1852 tor_free(alternatives
);
1854 SMARTLIST_FOREACH(fps
, char *, cp
, tor_free(cp
));
1855 smartlist_free(fps
);
1856 } else if (warn_if_unnamed
) {
1857 routerstatus_t
*rs
= router_get_consensus_status_by_id(
1858 best_match
->cache_info
.identity_digest
);
1859 if (rs
&& !rs
->name_lookup_warned
) {
1860 char fp
[HEX_DIGEST_LEN
+1];
1861 base16_encode(fp
, sizeof(fp
),
1862 best_match
->cache_info
.identity_digest
, DIGEST_LEN
);
1863 log_warn(LD_CONFIG
, "You specified a server \"%s\" by name, but this "
1864 "name is not registered, so it could be used by any server, "
1865 "not just the one you meant. "
1866 "To make sure you get the same server in the future, refer to "
1867 "it by key, as \"$%s\".", nickname
, fp
);
1868 rs
->name_lookup_warned
= 1;
1877 /** Try to find a routerinfo for <b>digest</b>. If we don't have one,
1878 * return 1. If we do, ask tor_version_as_new_as() for the answer.
1881 router_digest_version_as_new_as(const char *digest
, const char *cutoff
)
1883 routerinfo_t
*router
= router_get_by_digest(digest
);
1886 return tor_version_as_new_as(router
->platform
, cutoff
);
1889 /** Return true iff <b>digest</b> is the digest of the identity key of a
1890 * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
1891 * is zero, any authority is okay. */
1893 router_digest_is_trusted_dir_type(const char *digest
, authority_type_t type
)
1895 if (!trusted_dir_servers
)
1897 if (authdir_mode(get_options()) && router_digest_is_me(digest
))
1899 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ent
,
1900 if (!memcmp(digest
, ent
->digest
, DIGEST_LEN
)) {
1901 return (!type
) || ((type
& ent
->type
) != 0);
1906 /** Return true iff <b>addr</b> is the address of one of our trusted
1907 * directory authorities. */
1909 router_addr_is_trusted_dir(uint32_t addr
)
1911 if (!trusted_dir_servers
)
1913 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ent
,
1914 if (ent
->addr
== addr
)
1920 /** If hexdigest is correctly formed, base16_decode it into
1921 * digest, which must have DIGEST_LEN space in it.
1922 * Return 0 on success, -1 on failure.
1925 hexdigest_to_digest(const char *hexdigest
, char *digest
)
1927 if (hexdigest
[0]=='$')
1929 if (strlen(hexdigest
) < HEX_DIGEST_LEN
||
1930 base16_decode(digest
,DIGEST_LEN
,hexdigest
,HEX_DIGEST_LEN
) < 0)
1935 /** Return the router in our routerlist whose hexadecimal key digest
1936 * is <b>hexdigest</b>. Return NULL if no such router is known. */
1938 router_get_by_hexdigest(const char *hexdigest
)
1940 char digest
[DIGEST_LEN
];
1944 tor_assert(hexdigest
);
1947 if (hexdigest
[0]=='$')
1949 len
= strlen(hexdigest
);
1950 if (hexdigest_to_digest(hexdigest
, digest
) < 0)
1953 ri
= router_get_by_digest(digest
);
1955 if (ri
&& len
> HEX_DIGEST_LEN
) {
1956 if (hexdigest
[HEX_DIGEST_LEN
] == '=') {
1957 if (strcasecmp(ri
->nickname
, hexdigest
+HEX_DIGEST_LEN
+1) ||
1960 } else if (hexdigest
[HEX_DIGEST_LEN
] == '~') {
1961 if (strcasecmp(ri
->nickname
, hexdigest
+HEX_DIGEST_LEN
+1))
1971 /** Return the router in our routerlist whose 20-byte key digest
1972 * is <b>digest</b>. Return NULL if no such router is known. */
1974 router_get_by_digest(const char *digest
)
1978 if (!routerlist
) return NULL
;
1980 // routerlist_assert_ok(routerlist);
1982 return rimap_get(routerlist
->identity_map
, digest
);
1985 /** Return the router in our routerlist whose 20-byte descriptor
1986 * is <b>digest</b>. Return NULL if no such router is known. */
1987 signed_descriptor_t
*
1988 router_get_by_descriptor_digest(const char *digest
)
1992 if (!routerlist
) return NULL
;
1994 return sdmap_get(routerlist
->desc_digest_map
, digest
);
1997 /** Return the signed descriptor for the router in our routerlist whose
1998 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
2000 signed_descriptor_t
*
2001 router_get_by_extrainfo_digest(const char *digest
)
2005 if (!routerlist
) return NULL
;
2007 return sdmap_get(routerlist
->desc_by_eid_map
, digest
);
2010 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
2011 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
2012 * document is known. */
2013 signed_descriptor_t
*
2014 extrainfo_get_by_descriptor_digest(const char *digest
)
2018 if (!routerlist
) return NULL
;
2019 ei
= eimap_get(routerlist
->extra_info_map
, digest
);
2020 return ei
? &ei
->cache_info
: NULL
;
2023 /** Return a pointer to the signed textual representation of a descriptor.
2024 * The returned string is not guaranteed to be NUL-terminated: the string's
2025 * length will be in desc-\>signed_descriptor_len.
2027 * If <b>with_annotations</b> is set, the returned string will include
2029 * (if any) preceding the descriptor. This will increase the length of the
2030 * string by desc-\>annotations_len.
2032 * The caller must not free the string returned.
2035 signed_descriptor_get_body_impl(signed_descriptor_t
*desc
,
2036 int with_annotations
)
2038 const char *r
= NULL
;
2039 size_t len
= desc
->signed_descriptor_len
;
2040 off_t offset
= desc
->saved_offset
;
2041 if (with_annotations
)
2042 len
+= desc
->annotations_len
;
2044 offset
+= desc
->annotations_len
;
2046 tor_assert(len
> 32);
2047 if (desc
->saved_location
== SAVED_IN_CACHE
&& routerlist
) {
2048 desc_store_t
*store
= desc_get_store(router_get_routerlist(), desc
);
2049 if (store
&& store
->mmap
) {
2050 tor_assert(desc
->saved_offset
+ len
<= store
->mmap
->size
);
2051 r
= store
->mmap
->data
+ offset
;
2054 if (!r
) /* no mmap, or not in cache. */
2055 r
= desc
->signed_descriptor_body
+
2056 (with_annotations
? 0 : desc
->annotations_len
);
2059 if (!with_annotations
) {
2060 if (memcmp("router ", r
, 7) && memcmp("extra-info ", r
, 11)) {
2061 char *cp
= tor_strndup(r
, 64);
2062 log_err(LD_DIR
, "descriptor at %p begins with unexpected string %s",
2066 tor_assert(!memcmp("router ", r
, 7) || !memcmp("extra-info ", r
, 11));
2072 /** Return a pointer to the signed textual representation of a descriptor.
2073 * The returned string is not guaranteed to be NUL-terminated: the string's
2074 * length will be in desc-\>signed_descriptor_len.
2076 * The caller must not free the string returned.
2079 signed_descriptor_get_body(signed_descriptor_t
*desc
)
2081 return signed_descriptor_get_body_impl(desc
, 0);
2084 /** As signed_descriptor_get_body(), but points to the beginning of the
2085 * annotations section rather than the beginning of the descriptor. */
2087 signed_descriptor_get_annotations(signed_descriptor_t
*desc
)
2089 return signed_descriptor_get_body_impl(desc
, 1);
2092 /** Return the current list of all known routers. */
2094 router_get_routerlist(void)
2096 if (PREDICT_UNLIKELY(!routerlist
)) {
2097 routerlist
= tor_malloc_zero(sizeof(routerlist_t
));
2098 routerlist
->routers
= smartlist_create();
2099 routerlist
->old_routers
= smartlist_create();
2100 routerlist
->identity_map
= rimap_new();
2101 routerlist
->desc_digest_map
= sdmap_new();
2102 routerlist
->desc_by_eid_map
= sdmap_new();
2103 routerlist
->extra_info_map
= eimap_new();
2105 routerlist
->desc_store
.fname_base
= "cached-descriptors";
2106 routerlist
->desc_store
.fname_alt_base
= "cached-routers";
2107 routerlist
->extrainfo_store
.fname_base
= "cached-extrainfo";
2109 routerlist
->desc_store
.type
= ROUTER_STORE
;
2110 routerlist
->extrainfo_store
.type
= EXTRAINFO_STORE
;
2112 routerlist
->desc_store
.description
= "router descriptors";
2113 routerlist
->extrainfo_store
.description
= "extra-info documents";
2118 /** Free all storage held by <b>router</b>. */
2120 routerinfo_free(routerinfo_t
*router
)
2125 tor_free(router
->cache_info
.signed_descriptor_body
);
2126 tor_free(router
->address
);
2127 tor_free(router
->nickname
);
2128 tor_free(router
->platform
);
2129 tor_free(router
->contact_info
);
2130 if (router
->onion_pkey
)
2131 crypto_free_pk_env(router
->onion_pkey
);
2132 if (router
->identity_pkey
)
2133 crypto_free_pk_env(router
->identity_pkey
);
2134 if (router
->declared_family
) {
2135 SMARTLIST_FOREACH(router
->declared_family
, char *, s
, tor_free(s
));
2136 smartlist_free(router
->declared_family
);
2138 addr_policy_list_free(router
->exit_policy
);
2140 /* XXXX020 Remove once 414/417 is fixed. But I have a hunch... */
2141 memset(router
, 77, sizeof(routerinfo_t
));
2146 /** Release all storage held by <b>extrainfo</b> */
2148 extrainfo_free(extrainfo_t
*extrainfo
)
2152 tor_free(extrainfo
->cache_info
.signed_descriptor_body
);
2153 tor_free(extrainfo
->pending_sig
);
2155 /* XXXX020 remove this once more bugs go away. */
2156 memset(extrainfo
, 88, sizeof(extrainfo_t
)); /* debug bad memory usage */
2157 tor_free(extrainfo
);
2160 /** Release storage held by <b>sd</b>. */
2162 signed_descriptor_free(signed_descriptor_t
*sd
)
2164 tor_free(sd
->signed_descriptor_body
);
2166 /* XXXX020 remove this once more bugs go away. */
2167 memset(sd
, 99, sizeof(signed_descriptor_t
)); /* Debug bad mem usage */
2171 /** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
2173 static signed_descriptor_t
*
2174 signed_descriptor_from_routerinfo(routerinfo_t
*ri
)
2176 signed_descriptor_t
*sd
= tor_malloc_zero(sizeof(signed_descriptor_t
));
2177 memcpy(sd
, &(ri
->cache_info
), sizeof(signed_descriptor_t
));
2178 sd
->routerlist_index
= -1;
2179 ri
->cache_info
.signed_descriptor_body
= NULL
;
2180 routerinfo_free(ri
);
2184 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
2186 _extrainfo_free(void *e
)
2191 /** Free all storage held by a routerlist <b>rl</b>. */
2193 routerlist_free(routerlist_t
*rl
)
2196 rimap_free(rl
->identity_map
, NULL
);
2197 sdmap_free(rl
->desc_digest_map
, NULL
);
2198 sdmap_free(rl
->desc_by_eid_map
, NULL
);
2199 eimap_free(rl
->extra_info_map
, _extrainfo_free
);
2200 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, r
,
2201 routerinfo_free(r
));
2202 SMARTLIST_FOREACH(rl
->old_routers
, signed_descriptor_t
*, sd
,
2203 signed_descriptor_free(sd
));
2204 smartlist_free(rl
->routers
);
2205 smartlist_free(rl
->old_routers
);
2206 if (routerlist
->desc_store
.mmap
)
2207 tor_munmap_file(routerlist
->desc_store
.mmap
);
2208 if (routerlist
->extrainfo_store
.mmap
)
2209 tor_munmap_file(routerlist
->extrainfo_store
.mmap
);
2212 router_dir_info_changed();
2215 /** Log information about how much memory is being used for routerlist,
2216 * at log level <b>severity</b>. */
2218 dump_routerlist_mem_usage(int severity
)
2220 uint64_t livedescs
= 0;
2221 uint64_t olddescs
= 0;
2224 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, r
,
2225 livedescs
+= r
->cache_info
.signed_descriptor_len
);
2226 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
2227 olddescs
+= sd
->signed_descriptor_len
);
2229 log(severity
, LD_GENERAL
,
2230 "In %d live descriptors: "U64_FORMAT
" bytes. "
2231 "In %d old descriptors: "U64_FORMAT
" bytes.",
2232 smartlist_len(routerlist
->routers
), U64_PRINTF_ARG(livedescs
),
2233 smartlist_len(routerlist
->old_routers
), U64_PRINTF_ARG(olddescs
));
2237 _routerlist_find_elt(smartlist_t
*sl
, void *ri
, int idx
)
2241 SMARTLIST_FOREACH(sl
, routerinfo_t
*, r
,
2247 tor_assert(idx
< smartlist_len(sl
));
2248 tor_assert(smartlist_get(sl
, idx
) == ri
);
2253 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
2254 * as needed. There must be no previous member of <b>rl</b> with the same
2255 * identity digest as <b>ri</b>: If there is, call routerlist_replace
2259 routerlist_insert(routerlist_t
*rl
, routerinfo_t
*ri
)
2261 routerinfo_t
*ri_old
;
2263 /* XXXX020 remove this code once bug 417/404 is fixed. */
2264 routerinfo_t
*ri_generated
= router_get_my_routerinfo();
2265 tor_assert(ri_generated
!= ri
);
2267 tor_assert(ri
->cache_info
.routerlist_index
== -1);
2269 ri_old
= rimap_set(rl
->identity_map
, ri
->cache_info
.identity_digest
, ri
);
2270 tor_assert(!ri_old
);
2271 sdmap_set(rl
->desc_digest_map
, ri
->cache_info
.signed_descriptor_digest
,
2273 if (!tor_digest_is_zero(ri
->cache_info
.extra_info_digest
))
2274 sdmap_set(rl
->desc_by_eid_map
, ri
->cache_info
.extra_info_digest
,
2276 smartlist_add(rl
->routers
, ri
);
2277 ri
->cache_info
.routerlist_index
= smartlist_len(rl
->routers
) - 1;
2278 router_dir_info_changed();
2279 #ifdef DEBUG_ROUTERLIST
2280 routerlist_assert_ok(rl
);
2284 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
2285 * corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
2286 * we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
2288 extrainfo_insert(routerlist_t
*rl
, extrainfo_t
*ei
)
2291 routerinfo_t
*ri
= rimap_get(rl
->identity_map
,
2292 ei
->cache_info
.identity_digest
);
2293 signed_descriptor_t
*sd
=
2294 sdmap_get(rl
->desc_by_eid_map
, ei
->cache_info
.signed_descriptor_digest
);
2295 extrainfo_t
*ei_tmp
;
2298 /* XXXX020 remove this code once bug 417/404 is fixed. */
2299 extrainfo_t
*ei_generated
= router_get_my_extrainfo();
2300 tor_assert(ei_generated
!= ei
);
2304 /* This router is unknown; we can't even verify the signature. Give up.*/
2307 if (routerinfo_incompatible_with_extrainfo(ri
, ei
, sd
, NULL
)) {
2311 /* Okay, if we make it here, we definitely have a router corresponding to
2312 * this extrainfo. */
2314 ei_tmp
= eimap_set(rl
->extra_info_map
,
2315 ei
->cache_info
.signed_descriptor_digest
,
2319 rl
->extrainfo_store
.bytes_dropped
+=
2320 ei_tmp
->cache_info
.signed_descriptor_len
;
2321 extrainfo_free(ei_tmp
);
2328 #ifdef DEBUG_ROUTERLIST
2329 routerlist_assert_ok(rl
);
2334 #define should_cache_old_descriptors() \
2335 directory_caches_dir_info(get_options())
2337 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
2338 * a copy of router <b>ri</b> yet, add it to the list of old (not
2339 * recommended but still served) descriptors. Else free it. */
2341 routerlist_insert_old(routerlist_t
*rl
, routerinfo_t
*ri
)
2344 /* XXXX020 remove this code once bug 417/404 is fixed. */
2345 routerinfo_t
*ri_generated
= router_get_my_routerinfo();
2346 tor_assert(ri_generated
!= ri
);
2348 tor_assert(ri
->cache_info
.routerlist_index
== -1);
2350 if (should_cache_old_descriptors() &&
2351 ri
->purpose
== ROUTER_PURPOSE_GENERAL
&&
2352 !sdmap_get(rl
->desc_digest_map
,
2353 ri
->cache_info
.signed_descriptor_digest
)) {
2354 signed_descriptor_t
*sd
= signed_descriptor_from_routerinfo(ri
);
2355 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
2356 smartlist_add(rl
->old_routers
, sd
);
2357 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
2358 if (!tor_digest_is_zero(sd
->extra_info_digest
))
2359 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
2361 routerinfo_free(ri
);
2363 #ifdef DEBUG_ROUTERLIST
2364 routerlist_assert_ok(rl
);
2368 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
2369 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl->routers,
2370 * idx) == ri, we don't need to do a linear search over the list to decide
2371 * which to remove. We fill the gap in rl->routers with a later element in
2372 * the list, if any exists. <b>ri</b> is freed.
2374 * If <b>make_old</b> is true, instead of deleting the router, we try adding
2375 * it to rl->old_routers. */
2377 routerlist_remove(routerlist_t
*rl
, routerinfo_t
*ri
, int make_old
)
2379 routerinfo_t
*ri_tmp
;
2380 extrainfo_t
*ei_tmp
;
2381 int idx
= ri
->cache_info
.routerlist_index
;
2382 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->routers
));
2383 tor_assert(smartlist_get(rl
->routers
, idx
) == ri
);
2385 ri
->cache_info
.routerlist_index
= -1;
2386 smartlist_del(rl
->routers
, idx
);
2387 if (idx
< smartlist_len(rl
->routers
)) {
2388 routerinfo_t
*r
= smartlist_get(rl
->routers
, idx
);
2389 r
->cache_info
.routerlist_index
= idx
;
2392 ri_tmp
= rimap_remove(rl
->identity_map
, ri
->cache_info
.identity_digest
);
2393 router_dir_info_changed();
2394 tor_assert(ri_tmp
== ri
);
2396 if (make_old
&& should_cache_old_descriptors() &&
2397 ri
->purpose
== ROUTER_PURPOSE_GENERAL
) {
2398 signed_descriptor_t
*sd
;
2399 sd
= signed_descriptor_from_routerinfo(ri
);
2400 smartlist_add(rl
->old_routers
, sd
);
2401 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
2402 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
2403 if (!tor_digest_is_zero(sd
->extra_info_digest
))
2404 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
2406 signed_descriptor_t
*sd_tmp
;
2407 sd_tmp
= sdmap_remove(rl
->desc_digest_map
,
2408 ri
->cache_info
.signed_descriptor_digest
);
2409 tor_assert(sd_tmp
== &(ri
->cache_info
));
2410 rl
->desc_store
.bytes_dropped
+= ri
->cache_info
.signed_descriptor_len
;
2411 ei_tmp
= eimap_remove(rl
->extra_info_map
,
2412 ri
->cache_info
.extra_info_digest
);
2414 rl
->extrainfo_store
.bytes_dropped
+=
2415 ei_tmp
->cache_info
.signed_descriptor_len
;
2416 extrainfo_free(ei_tmp
);
2418 if (!tor_digest_is_zero(ri
->cache_info
.extra_info_digest
))
2419 sdmap_remove(rl
->desc_by_eid_map
, ri
->cache_info
.extra_info_digest
);
2420 routerinfo_free(ri
);
2422 #ifdef DEBUG_ROUTERLIST
2423 routerlist_assert_ok(rl
);
2427 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
2428 * adjust <b>rl</b> as appropriate. <b>idx</i> is -1, or the index of
2431 routerlist_remove_old(routerlist_t
*rl
, signed_descriptor_t
*sd
, int idx
)
2433 signed_descriptor_t
*sd_tmp
;
2434 extrainfo_t
*ei_tmp
;
2435 desc_store_t
*store
;
2437 idx
= sd
->routerlist_index
;
2439 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->old_routers
));
2440 /* XXX020 edmanm's bridge relay triggered the following assert while
2441 * running 0.2.0.12-alpha: */
2442 tor_assert(smartlist_get(rl
->old_routers
, idx
) == sd
);
2443 tor_assert(idx
== sd
->routerlist_index
);
2445 sd
->routerlist_index
= -1;
2446 smartlist_del(rl
->old_routers
, idx
);
2447 if (idx
< smartlist_len(rl
->old_routers
)) {
2448 signed_descriptor_t
*d
= smartlist_get(rl
->old_routers
, idx
);
2449 d
->routerlist_index
= idx
;
2451 sd_tmp
= sdmap_remove(rl
->desc_digest_map
,
2452 sd
->signed_descriptor_digest
);
2453 tor_assert(sd_tmp
== sd
);
2454 store
= desc_get_store(rl
, sd
);
2456 store
->bytes_dropped
+= sd
->signed_descriptor_len
;
2458 ei_tmp
= eimap_remove(rl
->extra_info_map
,
2459 sd
->extra_info_digest
);
2461 rl
->extrainfo_store
.bytes_dropped
+=
2462 ei_tmp
->cache_info
.signed_descriptor_len
;
2463 extrainfo_free(ei_tmp
);
2465 if (!tor_digest_is_zero(sd
->extra_info_digest
))
2466 sdmap_remove(rl
->desc_by_eid_map
, sd
->extra_info_digest
);
2468 signed_descriptor_free(sd
);
2469 #ifdef DEBUG_ROUTERLIST
2470 routerlist_assert_ok(rl
);
2474 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
2475 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
2476 * smartlist_get(rl->routers, idx) == ri, we don't need to do a linear
2477 * search over the list to decide which to remove. We put ri_new in the same
2478 * index as ri_old, if possible. ri is freed as appropriate.
2480 * If should_cache_descriptors() is true, instead of deleting the router,
2481 * we add it to rl->old_routers. */
2483 routerlist_replace(routerlist_t
*rl
, routerinfo_t
*ri_old
,
2484 routerinfo_t
*ri_new
)
2488 routerinfo_t
*ri_tmp
;
2489 extrainfo_t
*ei_tmp
;
2491 /* XXXX020 remove this code once bug 417/404 is fixed. */
2492 routerinfo_t
*ri_generated
= router_get_my_routerinfo();
2493 tor_assert(ri_generated
!= ri_new
);
2495 tor_assert(ri_old
!= ri_new
);
2496 tor_assert(ri_new
->cache_info
.routerlist_index
== -1);
2498 idx
= ri_old
->cache_info
.routerlist_index
;
2499 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->routers
));
2500 tor_assert(smartlist_get(rl
->routers
, idx
) == ri_old
);
2502 router_dir_info_changed();
2504 smartlist_set(rl
->routers
, idx
, ri_new
);
2505 ri_old
->cache_info
.routerlist_index
= -1;
2506 ri_new
->cache_info
.routerlist_index
= idx
;
2507 /* Check that ri_old is not in rl->routers anymore: */
2508 tor_assert( _routerlist_find_elt(rl
->routers
, ri_old
, -1) == -1 );
2510 log_warn(LD_BUG
, "Appending entry from routerlist_replace.");
2511 routerlist_insert(rl
, ri_new
);
2514 if (memcmp(ri_old
->cache_info
.identity_digest
,
2515 ri_new
->cache_info
.identity_digest
, DIGEST_LEN
)) {
2516 /* digests don't match; digestmap_set won't replace */
2517 rimap_remove(rl
->identity_map
, ri_old
->cache_info
.identity_digest
);
2519 ri_tmp
= rimap_set(rl
->identity_map
,
2520 ri_new
->cache_info
.identity_digest
, ri_new
);
2521 tor_assert(!ri_tmp
|| ri_tmp
== ri_old
);
2522 sdmap_set(rl
->desc_digest_map
,
2523 ri_new
->cache_info
.signed_descriptor_digest
,
2524 &(ri_new
->cache_info
));
2526 if (!tor_digest_is_zero(ri_new
->cache_info
.extra_info_digest
)) {
2527 sdmap_set(rl
->desc_by_eid_map
, ri_new
->cache_info
.extra_info_digest
,
2528 &ri_new
->cache_info
);
2531 if (should_cache_old_descriptors() &&
2532 ri_old
->purpose
== ROUTER_PURPOSE_GENERAL
) {
2533 signed_descriptor_t
*sd
= signed_descriptor_from_routerinfo(ri_old
);
2534 smartlist_add(rl
->old_routers
, sd
);
2535 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
2536 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
2537 if (!tor_digest_is_zero(sd
->extra_info_digest
))
2538 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
2540 if (memcmp(ri_old
->cache_info
.signed_descriptor_digest
,
2541 ri_new
->cache_info
.signed_descriptor_digest
,
2543 /* digests don't match; digestmap_set didn't replace */
2544 sdmap_remove(rl
->desc_digest_map
,
2545 ri_old
->cache_info
.signed_descriptor_digest
);
2548 ei_tmp
= eimap_remove(rl
->extra_info_map
,
2549 ri_old
->cache_info
.extra_info_digest
);
2551 rl
->extrainfo_store
.bytes_dropped
+=
2552 ei_tmp
->cache_info
.signed_descriptor_len
;
2553 extrainfo_free(ei_tmp
);
2555 if (!tor_digest_is_zero(ri_old
->cache_info
.extra_info_digest
)) {
2556 sdmap_remove(rl
->desc_by_eid_map
,
2557 ri_old
->cache_info
.extra_info_digest
);
2559 rl
->desc_store
.bytes_dropped
+= ri_old
->cache_info
.signed_descriptor_len
;
2560 routerinfo_free(ri_old
);
2562 #ifdef DEBUG_ROUTERLIST
2563 routerlist_assert_ok(rl
);
2567 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
2568 * it as a fresh routerinfo_t. */
2569 static routerinfo_t
*
2570 routerlist_reparse_old(routerlist_t
*rl
, signed_descriptor_t
*sd
)
2575 body
= signed_descriptor_get_annotations(sd
);
2577 ri
= router_parse_entry_from_string(body
,
2578 body
+sd
->signed_descriptor_len
+sd
->annotations_len
,
2582 memcpy(&ri
->cache_info
, sd
, sizeof(signed_descriptor_t
));
2583 sd
->signed_descriptor_body
= NULL
; /* Steal reference. */
2584 ri
->cache_info
.routerlist_index
= -1;
2586 routerlist_remove_old(rl
, sd
, -1);
2591 /** Free all memory held by the routerlist module. */
2593 routerlist_free_all(void)
2596 routerlist_free(routerlist
);
2598 if (warned_nicknames
) {
2599 SMARTLIST_FOREACH(warned_nicknames
, char *, cp
, tor_free(cp
));
2600 smartlist_free(warned_nicknames
);
2601 warned_nicknames
= NULL
;
2603 if (trusted_dir_servers
) {
2604 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ds
,
2605 trusted_dir_server_free(ds
));
2606 smartlist_free(trusted_dir_servers
);
2607 trusted_dir_servers
= NULL
;
2611 /** Forget that we have issued any router-related warnings, so that we'll
2612 * warn again if we see the same errors. */
2614 routerlist_reset_warnings(void)
2616 if (!warned_nicknames
)
2617 warned_nicknames
= smartlist_create();
2618 SMARTLIST_FOREACH(warned_nicknames
, char *, cp
, tor_free(cp
));
2619 smartlist_clear(warned_nicknames
); /* now the list is empty. */
2621 networkstatus_reset_warnings();
2624 /** Mark the router with ID <b>digest</b> as running or non-running
2625 * in our routerlist. */
2627 router_set_status(const char *digest
, int up
)
2629 routerinfo_t
*router
;
2630 routerstatus_t
*status
;
2633 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, d
,
2634 if (!memcmp(d
->digest
, digest
, DIGEST_LEN
))
2635 d
->is_running
= up
);
2637 router
= router_get_by_digest(digest
);
2639 log_debug(LD_DIR
,"Marking router '%s' as %s.",
2640 router
->nickname
, up
? "up" : "down");
2641 if (!up
&& router_is_me(router
) && !we_are_hibernating())
2642 log_warn(LD_NET
, "We just marked ourself as down. Are your external "
2643 "addresses reachable?");
2644 router
->is_running
= up
;
2646 status
= router_get_consensus_status_by_id(digest
);
2647 if (status
&& status
->is_running
!= up
) {
2648 status
->is_running
= up
;
2649 control_event_networkstatus_changed_single(status
);
2651 router_dir_info_changed();
2654 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
2655 * older entries (if any) with the same key. Note: Callers should not hold
2656 * their pointers to <b>router</b> if this function fails; <b>router</b>
2657 * will either be inserted into the routerlist or freed. Similarly, even
2658 * if this call succeeds, they should not hold their pointers to
2659 * <b>router</b> after subsequent calls with other routerinfo's -- they
2660 * might cause the original routerinfo to get freed.
2662 * Returns >= 0 if the router was added; less than 0 if it was not.
2664 * If we're returning non-zero, then assign to *<b>msg</b> a static string
2665 * describing the reason for not liking the routerinfo.
2667 * If the return value is less than -1, there was a problem with the
2668 * routerinfo. If the return value is equal to -1, then the routerinfo was
2669 * fine, but out-of-date. If the return value is equal to 1, the
2670 * routerinfo was accepted, but we should notify the generator of the
2671 * descriptor using the message *<b>msg</b>.
2673 * If <b>from_cache</b>, this descriptor came from our disk cache. If
2674 * <b>from_fetch</b>, we received it in response to a request we made.
2675 * (If both are false, that means it was uploaded to us as an auth dir
2676 * server or via the controller.)
2678 * This function should be called *after*
2679 * routers_update_status_from_consensus_networkstatus; subsequently, you
2680 * should call router_rebuild_store and routerlist_descriptors_added.
2683 router_add_to_routerlist(routerinfo_t
*router
, const char **msg
,
2684 int from_cache
, int from_fetch
)
2686 const char *id_digest
;
2687 int authdir
= authdir_mode_handles_descs(get_options(), router
->purpose
);
2688 int authdir_believes_valid
= 0;
2689 routerinfo_t
*old_router
;
2690 networkstatus_vote_t
*consensus
= networkstatus_get_latest_consensus();
2691 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
2692 int in_consensus
= 0;
2697 router_get_routerlist();
2699 id_digest
= router
->cache_info
.identity_digest
;
2701 /* Make sure that we haven't already got this exact descriptor. */
2702 if (sdmap_get(routerlist
->desc_digest_map
,
2703 router
->cache_info
.signed_descriptor_digest
)) {
2705 "Dropping descriptor that we already have for router '%s'",
2707 *msg
= "Router descriptor was not new.";
2708 routerinfo_free(router
);
2713 if (routerlist_is_overfull(routerlist
))
2714 routerlist_remove_old_routers();
2718 if (authdir_wants_to_reject_router(router
, msg
,
2719 !from_cache
&& !from_fetch
)) {
2721 routerinfo_free(router
);
2724 authdir_believes_valid
= router
->is_valid
;
2725 } else if (from_fetch
) {
2726 /* Only check the descriptor digest against the network statuses when
2727 * we are receiving in response to a fetch. */
2729 if (!signed_desc_digest_is_recognized(&router
->cache_info
) &&
2730 !routerinfo_is_a_configured_bridge(router
)) {
2731 /* We asked for it, so some networkstatus must have listed it when we
2732 * did. Save it if we're a cache in case somebody else asks for it. */
2734 "Received a no-longer-recognized descriptor for router '%s'",
2736 *msg
= "Router descriptor is not referenced by any network-status.";
2738 /* Only journal this desc if we'll be serving it. */
2739 if (!from_cache
&& should_cache_old_descriptors())
2740 signed_desc_append_to_journal(&router
->cache_info
,
2741 &routerlist
->desc_store
);
2742 routerlist_insert_old(routerlist
, router
);
2747 /* We no longer need a router with this descriptor digest. */
2748 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
2750 routerstatus_t
*rs
=
2751 networkstatus_v2_find_entry(ns
, router
->cache_info
.identity_digest
);
2752 if (rs
&& !memcmp(rs
->descriptor_digest
,
2753 router
->cache_info
.signed_descriptor_digest
,
2755 rs
->need_to_mirror
= 0;
2758 routerstatus_t
*rs
= networkstatus_vote_find_entry(consensus
,
2759 router
->cache_info
.identity_digest
);
2760 if (rs
&& !memcmp(rs
->descriptor_digest
,
2761 router
->cache_info
.signed_descriptor_digest
,
2764 rs
->need_to_mirror
= 0;
2768 if (router
->purpose
== ROUTER_PURPOSE_GENERAL
&&
2769 consensus
&& !in_consensus
&& !authdir
) {
2770 /* If it's a general router not listed in the consensus, then don't
2771 * consider replacing the latest router with it. */
2772 if (!from_cache
&& should_cache_old_descriptors())
2773 signed_desc_append_to_journal(&router
->cache_info
,
2774 &routerlist
->desc_store
);
2775 routerlist_insert_old(routerlist
, router
);
2776 *msg
= "Skipping router descriptor: not in consensus.";
2780 /* If we have a router with the same identity key, choose the newer one. */
2781 old_router
= rimap_get(routerlist
->identity_map
,
2782 router
->cache_info
.identity_digest
);
2784 if (!in_consensus
&& (router
->cache_info
.published_on
<=
2785 old_router
->cache_info
.published_on
)) {
2786 /* Same key, but old. This one is not listed in the consensus. */
2787 log_debug(LD_DIR
, "Skipping not-new descriptor for router '%s'",
2789 /* Only journal this desc if we'll be serving it. */
2790 if (!from_cache
&& should_cache_old_descriptors())
2791 signed_desc_append_to_journal(&router
->cache_info
,
2792 &routerlist
->desc_store
);
2793 routerlist_insert_old(routerlist
, router
);
2794 *msg
= "Router descriptor was not new.";
2797 /* Same key, and either new, or listed in the consensus. */
2798 log_debug(LD_DIR
, "Replacing entry for router '%s/%s' [%s]",
2799 router
->nickname
, old_router
->nickname
,
2800 hex_str(id_digest
,DIGEST_LEN
));
2801 if (router
->addr
== old_router
->addr
&&
2802 router
->or_port
== old_router
->or_port
) {
2803 /* these carry over when the address and orport are unchanged. */
2804 router
->last_reachable
= old_router
->last_reachable
;
2805 router
->testing_since
= old_router
->testing_since
;
2807 routerlist_replace(routerlist
, old_router
, router
);
2809 signed_desc_append_to_journal(&router
->cache_info
,
2810 &routerlist
->desc_store
);
2812 directory_set_dirty();
2813 *msg
= authdir_believes_valid
? "Valid server updated" :
2814 ("Invalid server updated. (This dirserver is marking your "
2815 "server as unapproved.)");
2820 /* We haven't seen a router with this identity before. Add it to the end of
2822 routerlist_insert(routerlist
, router
);
2824 signed_desc_append_to_journal(&router
->cache_info
,
2825 &routerlist
->desc_store
);
2826 directory_set_dirty();
2830 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
2831 * as for router_add_to_routerlist(). Return true iff we actually inserted
2835 router_add_extrainfo_to_routerlist(extrainfo_t
*ei
, const char **msg
,
2836 int from_cache
, int from_fetch
)
2840 if (msg
) *msg
= NULL
;
2842 inserted
= extrainfo_insert(router_get_routerlist(), ei
);
2844 if (inserted
&& !from_cache
)
2845 signed_desc_append_to_journal(&ei
->cache_info
,
2846 &routerlist
->extrainfo_store
);
2851 /** Sorting helper: return <0, 0, or >0 depending on whether the
2852 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
2853 * to, or later than that of *<b>b</b>. */
2855 _compare_old_routers_by_identity(const void **_a
, const void **_b
)
2858 const signed_descriptor_t
*r1
= *_a
, *r2
= *_b
;
2859 if ((i
= memcmp(r1
->identity_digest
, r2
->identity_digest
, DIGEST_LEN
)))
2861 return r1
->published_on
- r2
->published_on
;
2864 /** Internal type used to represent how long an old descriptor was valid,
2865 * where it appeared in the list of old descriptors, and whether it's extra
2866 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
2867 struct duration_idx_t
{
2873 /** Sorting helper: compare two duration_idx_t by their duration. */
2875 _compare_duration_idx(const void *_d1
, const void *_d2
)
2877 const struct duration_idx_t
*d1
= _d1
;
2878 const struct duration_idx_t
*d2
= _d2
;
2879 return d1
->duration
- d2
->duration
;
2882 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
2883 * must contain routerinfo_t with the same identity and with publication time
2884 * in ascending order. Remove members from this range until there are no more
2885 * than max_descriptors_per_router() remaining. Start by removing the oldest
2886 * members from before <b>cutoff</b>, then remove members which were current
2887 * for the lowest amount of time. The order of members of old_routers at
2888 * indices <b>lo</b> or higher may be changed.
2891 routerlist_remove_old_cached_routers_with_id(time_t now
,
2892 time_t cutoff
, int lo
, int hi
,
2893 digestmap_t
*retain
)
2896 unsigned n_extra
, n_rmv
= 0;
2897 struct duration_idx_t
*lifespans
;
2898 uint8_t *rmv
, *must_keep
;
2899 smartlist_t
*lst
= routerlist
->old_routers
;
2902 tor_assert(hi
< smartlist_len(lst
));
2903 tor_assert(lo
<= hi
);
2904 ident
= ((signed_descriptor_t
*)smartlist_get(lst
, lo
))->identity_digest
;
2905 for (i
= lo
+1; i
<= hi
; ++i
) {
2906 signed_descriptor_t
*r
= smartlist_get(lst
, i
);
2907 tor_assert(!memcmp(ident
, r
->identity_digest
, DIGEST_LEN
));
2911 /* Check whether we need to do anything at all. */
2913 int mdpr
= directory_caches_dir_info(get_options()) ? 5 : 2;
2919 lifespans
= tor_malloc_zero(sizeof(struct duration_idx_t
)*n
);
2920 rmv
= tor_malloc_zero(sizeof(uint8_t)*n
);
2921 must_keep
= tor_malloc_zero(sizeof(uint8_t)*n
);
2922 /* Set lifespans to contain the lifespan and index of each server. */
2923 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
2924 for (i
= lo
; i
<= hi
; ++i
) {
2925 signed_descriptor_t
*r
= smartlist_get(lst
, i
);
2926 signed_descriptor_t
*r_next
;
2927 lifespans
[i
-lo
].idx
= i
;
2928 if (r
->last_listed_as_valid_until
>= now
||
2929 (retain
&& digestmap_get(retain
, r
->signed_descriptor_digest
))) {
2930 must_keep
[i
-lo
] = 1;
2933 r_next
= smartlist_get(lst
, i
+1);
2934 tor_assert(r
->published_on
<= r_next
->published_on
);
2935 lifespans
[i
-lo
].duration
= (r_next
->published_on
- r
->published_on
);
2938 lifespans
[i
-lo
].duration
= INT_MAX
;
2940 if (!must_keep
[i
-lo
] && r
->published_on
< cutoff
&& n_rmv
< n_extra
) {
2942 lifespans
[i
-lo
].old
= 1;
2947 if (n_rmv
< n_extra
) {
2949 * We aren't removing enough servers for being old. Sort lifespans by
2950 * the duration of liveness, and remove the ones we're not already going to
2951 * remove based on how long they were alive.
2953 qsort(lifespans
, n
, sizeof(struct duration_idx_t
), _compare_duration_idx
);
2954 for (i
= 0; i
< n
&& n_rmv
< n_extra
; ++i
) {
2955 if (!must_keep
[lifespans
[i
].idx
-lo
] && !lifespans
[i
].old
) {
2956 rmv
[lifespans
[i
].idx
-lo
] = 1;
2965 routerlist_remove_old(routerlist
, smartlist_get(lst
, i
), i
);
2966 } while (--i
>= lo
);
2967 tor_free(must_keep
);
2969 tor_free(lifespans
);
2972 /** Deactivate any routers from the routerlist that are more than
2973 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
2974 * remove old routers from the list of cached routers if we have too many.
2977 routerlist_remove_old_routers(void)
2980 const char *cur_id
= NULL
;
2981 time_t now
= time(NULL
);
2983 routerinfo_t
*router
;
2984 signed_descriptor_t
*sd
;
2985 digestmap_t
*retain
;
2986 int caches
= directory_caches_dir_info(get_options());
2987 const networkstatus_vote_t
*consensus
= networkstatus_get_latest_consensus();
2988 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
2990 trusted_dirs_remove_old_certs();
2992 if (!routerlist
|| !consensus
)
2995 // routerlist_assert_ok(routerlist);
2997 retain
= digestmap_new();
2998 cutoff
= now
- OLD_ROUTER_DESC_MAX_AGE
;
2999 /* Build a list of all the descriptors that _anybody_ lists. */
3001 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
3003 /* XXXX The inner loop here gets pretty expensive, and actually shows up
3004 * on some profiles. It may be the reason digestmap_set shows up in
3005 * profiles too. If instead we kept a per-descriptor digest count of
3006 * how many networkstatuses recommended each descriptor, and changed
3007 * that only when the networkstatuses changed, that would be a speed
3008 * improvement, possibly 1-4% if it also removes digestmap_set from the
3009 * profile. Not worth it for 0.1.2.x, though. The new directory
3010 * system will obsolete this whole thing in 0.2.0.x. */
3011 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
3012 if (rs
->published_on
>= cutoff
)
3013 digestmap_set(retain
, rs
->descriptor_digest
, (void*)1));
3017 /* Retain anything listed in the consensus. */
3019 SMARTLIST_FOREACH(consensus
->routerstatus_list
, routerstatus_t
*, rs
,
3020 if (rs
->published_on
>= cutoff
)
3021 digestmap_set(retain
, rs
->descriptor_digest
, (void*)1));
3024 /* If we have a bunch of networkstatuses, we should consider pruning current
3025 * routers that are too old and that nobody recommends. (If we don't have
3026 * enough networkstatuses, then we should get more before we decide to kill
3029 smartlist_len(networkstatus_v2_list
) > get_n_v2_authorities() / 2) {
3030 cutoff
= now
- ROUTER_MAX_AGE
;
3031 /* Remove too-old unrecommended members of routerlist->routers. */
3032 for (i
= 0; i
< smartlist_len(routerlist
->routers
); ++i
) {
3033 router
= smartlist_get(routerlist
->routers
, i
);
3034 if (router
->cache_info
.published_on
<= cutoff
&&
3035 router
->cache_info
.last_listed_as_valid_until
< now
&&
3036 !digestmap_get(retain
,router
->cache_info
.signed_descriptor_digest
)) {
3037 /* Too old: remove it. (If we're a cache, just move it into
3040 "Forgetting obsolete (too old) routerinfo for router '%s'",
3042 routerlist_remove(routerlist
, router
, 1);
3048 //routerlist_assert_ok(routerlist);
3050 /* Remove far-too-old members of routerlist->old_routers. */
3051 cutoff
= now
- OLD_ROUTER_DESC_MAX_AGE
;
3052 for (i
= 0; i
< smartlist_len(routerlist
->old_routers
); ++i
) {
3053 sd
= smartlist_get(routerlist
->old_routers
, i
);
3054 if (sd
->published_on
<= cutoff
&&
3055 sd
->last_listed_as_valid_until
< now
&&
3056 !digestmap_get(retain
, sd
->signed_descriptor_digest
)) {
3057 /* Too old. Remove it. */
3058 routerlist_remove_old(routerlist
, sd
, i
--);
3062 //routerlist_assert_ok(routerlist);
3064 log_info(LD_DIR
, "We have %d live routers and %d old router descriptors. "
3065 "At most %d must be retained because of networkstatuses.",
3066 smartlist_len(routerlist
->routers
),
3067 smartlist_len(routerlist
->old_routers
),
3068 digestmap_size(retain
));
3070 /* Now we might have to look at routerlist->old_routers for extraneous
3071 * members. (We'd keep all the members if we could, but we need to save
3072 * space.) First, check whether we have too many router descriptors, total.
3073 * We're okay with having too many for some given router, so long as the
3074 * total number doesn't approach max_descriptors_per_router()*len(router).
3076 if (smartlist_len(routerlist
->old_routers
) <
3077 smartlist_len(routerlist
->routers
) * (caches
?4:2))
3080 smartlist_sort(routerlist
->old_routers
, _compare_old_routers_by_identity
);
3082 for (i
= 0; i
< smartlist_len(routerlist
->old_routers
); ++i
) {
3083 signed_descriptor_t
*r
= smartlist_get(routerlist
->old_routers
, i
);
3084 r
->routerlist_index
= i
;
3087 /* Iterate through the list from back to front, so when we remove descriptors
3088 * we don't mess up groups we haven't gotten to. */
3089 for (i
= smartlist_len(routerlist
->old_routers
)-1; i
>= 0; --i
) {
3090 signed_descriptor_t
*r
= smartlist_get(routerlist
->old_routers
, i
);
3092 cur_id
= r
->identity_digest
;
3095 if (memcmp(cur_id
, r
->identity_digest
, DIGEST_LEN
)) {
3096 routerlist_remove_old_cached_routers_with_id(now
,
3097 cutoff
, i
+1, hi
, retain
);
3098 cur_id
= r
->identity_digest
;
3103 routerlist_remove_old_cached_routers_with_id(now
, cutoff
, 0, hi
, retain
);
3104 //routerlist_assert_ok(routerlist);
3107 digestmap_free(retain
, NULL
);
3110 /** We just added a new set of descriptors. Take whatever extra steps
3113 routerlist_descriptors_added(smartlist_t
*sl
, int from_cache
)
3116 control_event_descriptors_changed(sl
);
3117 SMARTLIST_FOREACH(sl
, routerinfo_t
*, ri
,
3118 if (ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
3119 learned_bridge_descriptor(ri
, from_cache
);
3124 * Code to parse a single router descriptor and insert it into the
3125 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
3126 * descriptor was well-formed but could not be added; and 1 if the
3127 * descriptor was added.
3129 * If we don't add it and <b>msg</b> is not NULL, then assign to
3130 * *<b>msg</b> a static string describing the reason for refusing the
3133 * This is used only by the controller.
3136 router_load_single_router(const char *s
, uint8_t purpose
, int cache
,
3142 char annotation_buf
[ROUTER_ANNOTATION_BUF_LEN
];
3146 tor_snprintf(annotation_buf
, sizeof(annotation_buf
),
3147 "@source controller\n"
3148 "@purpose %s\n", router_purpose_to_string(purpose
));
3150 if (!(ri
= router_parse_entry_from_string(s
, NULL
, 1, 0, annotation_buf
))) {
3151 log_warn(LD_DIR
, "Error parsing router descriptor; dropping.");
3152 *msg
= "Couldn't parse router descriptor.";
3155 tor_assert(ri
->purpose
== purpose
);
3156 if (router_is_me(ri
)) {
3157 log_warn(LD_DIR
, "Router's identity key matches mine; dropping.");
3158 *msg
= "Router's identity key matches mine.";
3159 routerinfo_free(ri
);
3163 if (!cache
) /* obey the preference of the controller */
3164 ri
->cache_info
.do_not_cache
= 1;
3166 lst
= smartlist_create();
3167 smartlist_add(lst
, ri
);
3168 routers_update_status_from_consensus_networkstatus(lst
, 0);
3170 if ((r
=router_add_to_routerlist(ri
, msg
, 0, 0))<0) {
3171 /* we've already assigned to *msg now, and ri is already freed */
3174 log_warn(LD_DIR
, "Couldn't add router to list: %s Dropping.", *msg
);
3175 smartlist_free(lst
);
3178 routerlist_descriptors_added(lst
, 0);
3179 smartlist_free(lst
);
3180 log_debug(LD_DIR
, "Added router to list");
3185 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
3186 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
3187 * are in response to a query to the network: cache them by adding them to
3190 * If <b>requested_fingerprints</b> is provided, it must contain a list of
3191 * uppercased fingerprints. Do not update any router whose
3192 * fingerprint is not on the list; after updating a router, remove its
3193 * fingerprint from the list.
3195 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
3196 * are descriptor digests. Otherwise they are identity digests.
3199 router_load_routers_from_string(const char *s
, const char *eos
,
3200 saved_location_t saved_location
,
3201 smartlist_t
*requested_fingerprints
,
3202 int descriptor_digests
,
3203 const char *prepend_annotations
)
3205 smartlist_t
*routers
= smartlist_create(), *changed
= smartlist_create();
3206 char fp
[HEX_DIGEST_LEN
+1];
3208 int from_cache
= (saved_location
!= SAVED_NOWHERE
);
3209 int allow_annotations
= (saved_location
!= SAVED_NOWHERE
);
3210 int any_changed
= 0;
3212 router_parse_list_from_string(&s
, eos
, routers
, saved_location
, 0,
3213 allow_annotations
, prepend_annotations
);
3215 routers_update_status_from_consensus_networkstatus(routers
, !from_cache
);
3217 log_info(LD_DIR
, "%d elements to add", smartlist_len(routers
));
3219 SMARTLIST_FOREACH(routers
, routerinfo_t
*, ri
,
3221 if (requested_fingerprints
) {
3222 base16_encode(fp
, sizeof(fp
), descriptor_digests
?
3223 ri
->cache_info
.signed_descriptor_digest
:
3224 ri
->cache_info
.identity_digest
,
3226 if (smartlist_string_isin(requested_fingerprints
, fp
)) {
3227 smartlist_string_remove(requested_fingerprints
, fp
);
3230 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
3232 "We received a router descriptor with a fingerprint (%s) "
3233 "that we never requested. (We asked for: %s.) Dropping.",
3235 tor_free(requested
);
3236 routerinfo_free(ri
);
3241 if (router_add_to_routerlist(ri
, &msg
, from_cache
, !from_cache
) >= 0) {
3243 smartlist_add(changed
, ri
);
3244 routerlist_descriptors_added(changed
, from_cache
);
3245 smartlist_clear(changed
);
3249 routerlist_assert_ok(routerlist
);
3252 router_rebuild_store(0, &routerlist
->desc_store
);
3254 smartlist_free(routers
);
3255 smartlist_free(changed
);
3258 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
3259 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
3260 * router_load_routers_from_string(). */
3262 router_load_extrainfo_from_string(const char *s
, const char *eos
,
3263 saved_location_t saved_location
,
3264 smartlist_t
*requested_fingerprints
,
3265 int descriptor_digests
)
3267 smartlist_t
*extrainfo_list
= smartlist_create();
3269 int from_cache
= (saved_location
!= SAVED_NOWHERE
);
3271 router_parse_list_from_string(&s
, eos
, extrainfo_list
, saved_location
, 1, 0,
3274 log_info(LD_DIR
, "%d elements to add", smartlist_len(extrainfo_list
));
3276 SMARTLIST_FOREACH(extrainfo_list
, extrainfo_t
*, ei
, {
3278 router_add_extrainfo_to_routerlist(ei
, &msg
, from_cache
, !from_cache
);
3279 if (added
&& requested_fingerprints
) {
3280 char fp
[HEX_DIGEST_LEN
+1];
3281 base16_encode(fp
, sizeof(fp
), descriptor_digests
?
3282 ei
->cache_info
.signed_descriptor_digest
:
3283 ei
->cache_info
.identity_digest
,
3285 smartlist_string_remove(requested_fingerprints
, fp
);
3286 /* XXX020 We silently let people stuff us with extrainfos we
3287 * didn't ask for. Is this a problem? -RD */
3291 routerlist_assert_ok(routerlist
);
3292 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store
);
3294 smartlist_free(extrainfo_list
);
3297 /** Return true iff any networkstatus includes a descriptor whose digest
3298 * is that of <b>desc</b>. */
3300 signed_desc_digest_is_recognized(signed_descriptor_t
*desc
)
3303 networkstatus_vote_t
*consensus
= networkstatus_get_latest_consensus();
3304 int caches
= directory_caches_dir_info(get_options());
3305 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
3308 rs
= networkstatus_vote_find_entry(consensus
, desc
->identity_digest
);
3309 if (rs
&& !memcmp(rs
->descriptor_digest
,
3310 desc
->signed_descriptor_digest
, DIGEST_LEN
))
3313 if (caches
&& networkstatus_v2_list
) {
3314 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
3316 if (!(rs
= networkstatus_v2_find_entry(ns
, desc
->identity_digest
)))
3318 if (!memcmp(rs
->descriptor_digest
,
3319 desc
->signed_descriptor_digest
, DIGEST_LEN
))
3326 /** Clear all our timeouts for fetching v2 and v3 directory stuff, and then
3327 * give it all a try again. */
3329 routerlist_retry_directory_downloads(time_t now
)
3331 router_reset_status_download_failures();
3332 router_reset_descriptor_download_failures();
3333 update_networkstatus_downloads(now
);
3334 update_router_descriptor_downloads(now
);
3337 /** Return 1 if all running sufficiently-stable routers will reject
3338 * addr:port, return 0 if any might accept it. */
3340 router_exit_policy_all_routers_reject(uint32_t addr
, uint16_t port
,
3343 addr_policy_result_t r
;
3344 if (!routerlist
) return 1;
3346 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
3348 if (router
->is_running
&&
3349 !router_is_unreliable(router
, need_uptime
, 0, 0)) {
3350 r
= compare_addr_to_addr_policy(addr
, port
, router
->exit_policy
);
3351 if (r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
)
3352 return 0; /* this one could be ok. good enough. */
3355 return 1; /* all will reject. */
3358 /** Return true iff <b>router</b> does not permit exit streams.
3361 router_exit_policy_rejects_all(routerinfo_t
*router
)
3363 return compare_addr_to_addr_policy(0, 0, router
->exit_policy
)
3364 == ADDR_POLICY_REJECTED
;
3367 /** Add to the list of authorized directory servers one at
3368 * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
3369 * <b>address</b> is NULL, add ourself. Return 0 if success, -1 if
3370 * we couldn't add it. */
3371 trusted_dir_server_t
*
3372 add_trusted_dir_server(const char *nickname
, const char *address
,
3373 uint16_t dir_port
, uint16_t or_port
,
3374 const char *digest
, const char *v3_auth_digest
,
3375 authority_type_t type
)
3377 trusted_dir_server_t
*ent
;
3379 char *hostname
= NULL
;
3381 if (!trusted_dir_servers
)
3382 trusted_dir_servers
= smartlist_create();
3384 if (!address
) { /* The address is us; we should guess. */
3385 if (resolve_my_address(LOG_WARN
, get_options(), &a
, &hostname
) < 0) {
3387 "Couldn't find a suitable address when adding ourself as a "
3388 "trusted directory server.");
3392 if (tor_lookup_hostname(address
, &a
)) {
3394 "Unable to lookup address for directory server at '%s'",
3398 hostname
= tor_strdup(address
);
3401 ent
= tor_malloc_zero(sizeof(trusted_dir_server_t
));
3402 ent
->nickname
= nickname
? tor_strdup(nickname
) : NULL
;
3403 ent
->address
= hostname
;
3405 ent
->dir_port
= dir_port
;
3406 ent
->or_port
= or_port
;
3407 ent
->is_running
= 1;
3409 memcpy(ent
->digest
, digest
, DIGEST_LEN
);
3410 if (v3_auth_digest
&& (type
& V3_AUTHORITY
))
3411 memcpy(ent
->v3_identity_digest
, v3_auth_digest
, DIGEST_LEN
);
3413 dlen
= 64 + strlen(hostname
) + (nickname
?strlen(nickname
):0);
3414 ent
->description
= tor_malloc(dlen
);
3416 tor_snprintf(ent
->description
, dlen
, "directory server \"%s\" at %s:%d",
3417 nickname
, hostname
, (int)dir_port
);
3419 tor_snprintf(ent
->description
, dlen
, "directory server at %s:%d",
3420 hostname
, (int)dir_port
);
3422 ent
->fake_status
.addr
= ent
->addr
;
3423 memcpy(ent
->fake_status
.identity_digest
, digest
, DIGEST_LEN
);
3425 strlcpy(ent
->fake_status
.nickname
, nickname
,
3426 sizeof(ent
->fake_status
.nickname
));
3428 ent
->fake_status
.nickname
[0] = '\0';
3429 ent
->fake_status
.dir_port
= ent
->dir_port
;
3430 ent
->fake_status
.or_port
= ent
->or_port
;
3433 ent
->fake_status
.version_supports_begindir
= 1;
3435 smartlist_add(trusted_dir_servers
, ent
);
3436 router_dir_info_changed();
3440 /** Free storage held in <b>cert</b>. */
3442 authority_cert_free(authority_cert_t
*cert
)
3447 tor_free(cert
->cache_info
.signed_descriptor_body
);
3448 if (cert
->signing_key
)
3449 crypto_free_pk_env(cert
->signing_key
);
3450 if (cert
->identity_key
)
3451 crypto_free_pk_env(cert
->identity_key
);
3456 /** Free storage held in <b>ds</b>. */
3458 trusted_dir_server_free(trusted_dir_server_t
*ds
)
3460 tor_free(ds
->nickname
);
3461 tor_free(ds
->description
);
3462 tor_free(ds
->address
);
3466 /** Remove all members from the list of trusted dir servers. */
3468 clear_trusted_dir_servers(void)
3470 if (trusted_dir_servers
) {
3471 SMARTLIST_FOREACH(trusted_dir_servers
, trusted_dir_server_t
*, ent
,
3472 trusted_dir_server_free(ent
));
3473 smartlist_clear(trusted_dir_servers
);
3475 trusted_dir_servers
= smartlist_create();
3477 router_dir_info_changed();
3480 /** Return 1 if any trusted dir server supports v1 directories,
3483 any_trusted_dir_is_v1_authority(void)
3485 if (trusted_dir_servers
)
3486 return get_n_authorities(V1_AUTHORITY
) > 0;
3491 /** For every current directory connection whose purpose is <b>purpose</b>,
3492 * and where the resource being downloaded begins with <b>prefix</b>, split
3493 * rest of the resource into base16 fingerprints, decode them, and set the
3494 * corresponding elements of <b>result</b> to a nonzero value. */
3496 list_pending_downloads(digestmap_t
*result
,
3497 int purpose
, const char *prefix
)
3499 const size_t p_len
= strlen(prefix
);
3500 smartlist_t
*tmp
= smartlist_create();
3501 smartlist_t
*conns
= get_connection_array();
3505 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3507 if (conn
->type
== CONN_TYPE_DIR
&&
3508 conn
->purpose
== purpose
&&
3509 !conn
->marked_for_close
) {
3510 const char *resource
= TO_DIR_CONN(conn
)->requested_resource
;
3511 if (!strcmpstart(resource
, prefix
))
3512 dir_split_resource_into_fingerprints(resource
+ p_len
,
3516 SMARTLIST_FOREACH(tmp
, char *, d
,
3518 digestmap_set(result
, d
, (void*)1);
3521 smartlist_free(tmp
);
3524 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
3525 * true) we are currently downloading by descriptor digest, set result[d] to
3528 list_pending_descriptor_downloads(digestmap_t
*result
, int extrainfo
)
3531 extrainfo
? DIR_PURPOSE_FETCH_EXTRAINFO
: DIR_PURPOSE_FETCH_SERVERDESC
;
3532 list_pending_downloads(result
, purpose
, "d/");
3535 /** Launch downloads for all the descriptors whose digests are listed
3536 * as digests[i] for lo <= i < hi. (Lo and hi may be out of range.)
3537 * If <b>source</b> is given, download from <b>source</b>; otherwise,
3538 * download from an appropriate random directory server.
3541 initiate_descriptor_downloads(routerstatus_t
*source
,
3543 smartlist_t
*digests
,
3547 char *resource
, *cp
;
3553 if (hi
> smartlist_len(digests
))
3554 hi
= smartlist_len(digests
);
3556 r_len
= 8 + (HEX_DIGEST_LEN
+1)*n
;
3557 cp
= resource
= tor_malloc(r_len
);
3558 memcpy(cp
, "d/", 2);
3560 for (i
= lo
; i
< hi
; ++i
) {
3561 base16_encode(cp
, r_len
-(cp
-resource
),
3562 smartlist_get(digests
,i
), DIGEST_LEN
);
3563 cp
+= HEX_DIGEST_LEN
;
3566 memcpy(cp
-1, ".z", 3);
3569 /* We know which authority we want. */
3570 directory_initiate_command_routerstatus(source
, purpose
,
3571 ROUTER_PURPOSE_GENERAL
,
3572 0, /* not private */
3573 resource
, NULL
, 0, 0);
3575 directory_get_from_dirserver(purpose
, ROUTER_PURPOSE_GENERAL
, resource
, 1);
3580 /** Clients don't download any descriptor this recent, since it will probably
3581 * not have propagated to enough caches. */
3582 #define ESTIMATED_PROPAGATION_TIME (10*60)
3584 /** Return 0 if this routerstatus is obsolete, too new, isn't
3585 * running, or otherwise not a descriptor that we would make any
3586 * use of even if we had it. Else return 1. */
3588 client_would_use_router(routerstatus_t
*rs
, time_t now
, or_options_t
*options
)
3590 if (!rs
->is_running
&& !options
->FetchUselessDescriptors
) {
3591 /* If we had this router descriptor, we wouldn't even bother using it.
3592 * But, if we want to have a complete list, fetch it anyway. */
3595 if (rs
->published_on
+ ESTIMATED_PROPAGATION_TIME
> now
) {
3596 /* Most caches probably don't have this descriptor yet. */
3599 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
3600 /* We'd drop it immediately for being too old. */
3606 /** Max amount of hashes to download per request.
3607 * Since squid does not like URLs >= 4096 bytes we limit it to 96.
3608 * 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
3609 * 4058/41 (40 for the hash and 1 for the + that separates them) => 98
3610 * So use 96 because it's a nice number.
3612 #define MAX_DL_PER_REQUEST 96
3613 /** Don't split our requests so finely that we are requesting fewer than
3614 * this number per server. */
3615 #define MIN_DL_PER_REQUEST 4
3616 /** To prevent a single screwy cache from confusing us by selective reply,
3617 * try to split our requests into at least this this many requests. */
3618 #define MIN_REQUESTS 3
3619 /** If we want fewer than this many descriptors, wait until we
3620 * want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
3622 #define MAX_DL_TO_DELAY 16
3623 /** When directory clients have only a few servers to request, they batch
3624 * them until they have more, or until this amount of time has passed. */
3625 #define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
3627 /** Given a list of router descriptor digests in <b>downloadable</b>, decide
3628 * whether to delay fetching until we have more. If we don't want to delay,
3629 * launch one or more requests to the appropriate directory authorities. */
3631 launch_router_descriptor_downloads(smartlist_t
*downloadable
, time_t now
)
3633 int should_delay
= 0, n_downloadable
;
3634 or_options_t
*options
= get_options();
3636 n_downloadable
= smartlist_len(downloadable
);
3637 if (!directory_fetches_dir_info_early(options
)) {
3638 if (n_downloadable
>= MAX_DL_TO_DELAY
) {
3640 "There are enough downloadable routerdescs to launch requests.");
3643 should_delay
= (last_routerdesc_download_attempted
+
3644 MAX_CLIENT_INTERVAL_WITHOUT_REQUEST
) > now
;
3645 if (!should_delay
&& n_downloadable
) {
3646 if (last_routerdesc_download_attempted
) {
3648 "There are not many downloadable routerdescs, but we've "
3649 "been waiting long enough (%d seconds). Downloading.",
3650 (int)(now
-last_routerdesc_download_attempted
));
3653 "There are not many downloadable routerdescs, but we haven't "
3654 "tried downloading descriptors recently. Downloading.");
3659 /* XXX020 should we consider having even the dir mirrors delay
3660 * a little bit, so we don't load the authorities as much? -RD */
3662 if (! should_delay
&& n_downloadable
) {
3663 int i
, n_per_request
;
3664 const char *req_plural
= "", *rtr_plural
= "";
3665 n_per_request
= (n_downloadable
+MIN_REQUESTS
-1) / MIN_REQUESTS
;
3666 if (n_per_request
> MAX_DL_PER_REQUEST
)
3667 n_per_request
= MAX_DL_PER_REQUEST
;
3668 if (n_per_request
< MIN_DL_PER_REQUEST
)
3669 n_per_request
= MIN_DL_PER_REQUEST
;
3671 if (n_downloadable
> n_per_request
)
3672 req_plural
= rtr_plural
= "s";
3673 else if (n_downloadable
> 1)
3677 "Launching %d request%s for %d router%s, %d at a time",
3678 (n_downloadable
+n_per_request
-1)/n_per_request
,
3679 req_plural
, n_downloadable
, rtr_plural
, n_per_request
);
3680 smartlist_sort_digests(downloadable
);
3681 for (i
=0; i
< n_downloadable
; i
+= n_per_request
) {
3682 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_SERVERDESC
,
3683 downloadable
, i
, i
+n_per_request
);
3685 last_routerdesc_download_attempted
= now
;
3689 /** Launch downloads for router status as needed, using the strategy used by
3690 * authorities and caches: based on the v2 networkstatuses we have, download
3691 * every descriptor we don't have but would serve, from a random authority
3694 update_router_descriptor_cache_downloads_v2(time_t now
)
3696 smartlist_t
**downloadable
; /* For each authority, what can we dl from it? */
3697 smartlist_t
**download_from
; /* ... and, what will we dl from it? */
3698 digestmap_t
*map
; /* Which descs are in progress, or assigned? */
3701 or_options_t
*options
= get_options();
3702 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
3704 if (! directory_fetches_dir_info_early(options
)) {
3705 log_warn(LD_BUG
, "Called update_router_descriptor_cache_downloads_v2() "
3706 "on a non-dir-mirror?");
3709 if (!networkstatus_v2_list
|| !smartlist_len(networkstatus_v2_list
))
3712 map
= digestmap_new();
3713 n
= smartlist_len(networkstatus_v2_list
);
3715 downloadable
= tor_malloc_zero(sizeof(smartlist_t
*) * n
);
3716 download_from
= tor_malloc_zero(sizeof(smartlist_t
*) * n
);
3718 /* Set map[d]=1 for the digest of every descriptor that we are currently
3720 list_pending_descriptor_downloads(map
, 0);
3722 /* For the digest of every descriptor that we don't have, and that we aren't
3723 * downloading, add d to downloadable[i] if the i'th networkstatus knows
3724 * about that descriptor, and we haven't already failed to get that
3725 * descriptor from the corresponding authority.
3728 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
3730 trusted_dir_server_t
*ds
;
3732 dl
= downloadable
[ns_sl_idx
] = smartlist_create();
3733 download_from
[ns_sl_idx
] = smartlist_create();
3734 if (ns
->published_on
+ MAX_NETWORKSTATUS_AGE
+10*60 < now
) {
3735 /* Don't download if the networkstatus is almost ancient. */
3736 /* Actually, I suspect what's happening here is that we ask
3737 * for the descriptor when we have a given networkstatus,
3738 * and then we get a newer networkstatus, and then we receive
3739 * the descriptor. Having a networkstatus actually expire is
3740 * probably a rare event, and we'll probably be happiest if
3741 * we take this clause out. -RD */
3745 /* Don't try dirservers that we think are down -- we might have
3746 * just tried them and just marked them as down. */
3747 ds
= router_get_trusteddirserver_by_digest(ns
->identity_digest
);
3748 if (ds
&& !ds
->is_running
)
3751 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
* , rs
,
3753 if (!rs
->need_to_mirror
)
3755 if (router_get_by_descriptor_digest(rs
->descriptor_digest
)) {
3757 "We have a router descriptor, but need_to_mirror=1.");
3758 rs
->need_to_mirror
= 0;
3761 if (authdir_mode(options
) && dirserv_would_reject_router(rs
)) {
3762 rs
->need_to_mirror
= 0;
3765 if (digestmap_get(map
, rs
->descriptor_digest
)) {
3766 /* We're downloading it already. */
3769 /* We could download it from this guy. */
3770 smartlist_add(dl
, rs
->descriptor_digest
);
3776 /* At random, assign descriptors to authorities such that:
3777 * - if d is a member of some downloadable[x], d is a member of some
3778 * download_from[y]. (Everything we want to download, we try to download
3780 * - If d is a member of download_from[y], d is a member of downloadable[y].
3781 * (We only try to download descriptors from authorities who claim to have
3783 * - No d is a member of download_from[x] and download_from[y] s.t. x != y.
3784 * (We don't try to download anything from two authorities concurrently.)
3786 while (n_download
) {
3787 int which_ns
= crypto_rand_int(n
);
3788 smartlist_t
*dl
= downloadable
[which_ns
];
3791 if (!smartlist_len(dl
))
3793 idx
= crypto_rand_int(smartlist_len(dl
));
3794 d
= smartlist_get(dl
, idx
);
3795 if (! digestmap_get(map
, d
)) {
3796 smartlist_add(download_from
[which_ns
], d
);
3797 digestmap_set(map
, d
, (void*) 1);
3799 smartlist_del(dl
, idx
);
3803 /* Now, we can actually launch our requests. */
3804 for (i
=0; i
<n
; ++i
) {
3805 networkstatus_v2_t
*ns
= smartlist_get(networkstatus_v2_list
, i
);
3806 trusted_dir_server_t
*ds
=
3807 router_get_trusteddirserver_by_digest(ns
->identity_digest
);
3808 smartlist_t
*dl
= download_from
[i
];
3810 log_warn(LD_BUG
, "Networkstatus with no corresponding authority!");
3813 if (! smartlist_len(dl
))
3815 log_info(LD_DIR
, "Requesting %d descriptors from authority \"%s\"",
3816 smartlist_len(dl
), ds
->nickname
);
3817 for (j
=0; j
< smartlist_len(dl
); j
+= MAX_DL_PER_REQUEST
) {
3818 initiate_descriptor_downloads(&(ds
->fake_status
),
3819 DIR_PURPOSE_FETCH_SERVERDESC
, dl
, j
,
3820 j
+MAX_DL_PER_REQUEST
);
3824 for (i
=0; i
<n
; ++i
) {
3825 smartlist_free(download_from
[i
]);
3826 smartlist_free(downloadable
[i
]);
3828 tor_free(download_from
);
3829 tor_free(downloadable
);
3830 digestmap_free(map
,NULL
);
3833 /** For any descriptor that we want that's currently listed in the live
3834 * consensus, download it as appropriate. */
3836 update_consensus_router_descriptor_downloads(time_t now
)
3838 or_options_t
*options
= get_options();
3839 digestmap_t
*map
= NULL
;
3840 smartlist_t
*no_longer_old
= smartlist_create();
3841 smartlist_t
*downloadable
= smartlist_create();
3842 int authdir
= authdir_mode(options
);
3843 networkstatus_vote_t
*consensus
=
3844 networkstatus_get_reasonably_live_consensus(now
);
3845 int n_delayed
=0, n_have
=0, n_would_reject
=0, n_wouldnt_use
=0,
3846 n_inprogress
=0, n_in_oldrouters
=0;
3848 if (directory_too_idle_to_fetch_descriptors(options
, now
))
3853 map
= digestmap_new();
3854 list_pending_descriptor_downloads(map
, 0);
3855 SMARTLIST_FOREACH(consensus
->routerstatus_list
, routerstatus_t
*, rs
,
3857 signed_descriptor_t
*sd
;
3858 if ((sd
= router_get_by_descriptor_digest(rs
->descriptor_digest
))) {
3861 if (!(ri
= router_get_by_digest(rs
->identity_digest
)) ||
3862 memcmp(ri
->cache_info
.signed_descriptor_digest
,
3863 sd
->signed_descriptor_digest
, DIGEST_LEN
)) {
3864 /* We have a descriptor with this digest, but either there is no
3865 * entry in routerlist with the same ID (!ri), or there is one,
3866 * but the identity digest differs (memcmp).
3868 smartlist_add(no_longer_old
, sd
);
3869 ++n_in_oldrouters
; /* We have it in old_routers. */
3871 continue; /* We have it already. */
3873 if (digestmap_get(map
, rs
->descriptor_digest
)) {
3875 continue; /* We have an in-progress download. */
3877 if (!download_status_is_ready(&rs
->dl_status
, now
,
3878 MAX_ROUTERDESC_DOWNLOAD_FAILURES
)) {
3879 ++n_delayed
; /* Not ready for retry. */
3882 if (authdir
&& dirserv_would_reject_router(rs
)) {
3884 continue; /* We would throw it out immediately. */
3886 if (!directory_caches_dir_info(options
) &&
3887 !client_would_use_router(rs
, now
, options
)) {
3889 continue; /* We would never use it ourself. */
3891 smartlist_add(downloadable
, rs
->descriptor_digest
);
3894 if (!authdir_mode_handles_descs(options
, ROUTER_PURPOSE_GENERAL
)
3895 && smartlist_len(no_longer_old
)) {
3896 routerlist_t
*rl
= router_get_routerlist();
3897 log_info(LD_DIR
, "%d router descriptors listed in consensus are "
3898 "currently in old_routers; making them current.",
3899 smartlist_len(no_longer_old
));
3900 SMARTLIST_FOREACH(no_longer_old
, signed_descriptor_t
*, sd
, {
3903 routerinfo_t
*ri
= routerlist_reparse_old(rl
, sd
);
3905 log_warn(LD_BUG
, "Failed to re-parse a router.");
3908 r
= router_add_to_routerlist(ri
, &msg
, 1, 0);
3910 log_warn(LD_DIR
, "Couldn't add re-parsed router: %s",
3914 routerlist_assert_ok(rl
);
3918 "%d router descriptors downloadable. %d delayed; %d present "
3919 "(%d of those were in old_routers); %d would_reject; "
3920 "%d wouldnt_use, %d in progress.",
3921 smartlist_len(downloadable
), n_delayed
, n_have
, n_in_oldrouters
,
3922 n_would_reject
, n_wouldnt_use
, n_inprogress
);
3924 launch_router_descriptor_downloads(downloadable
, now
);
3926 digestmap_free(map
, NULL
);
3928 smartlist_free(downloadable
);
3929 smartlist_free(no_longer_old
);
3932 /** Launch downloads for router status as needed. */
3934 update_router_descriptor_downloads(time_t now
)
3936 or_options_t
*options
= get_options();
3937 if (should_delay_dir_fetches(options
))
3939 if (directory_fetches_dir_info_early(options
)) {
3940 update_router_descriptor_cache_downloads_v2(now
);
3942 update_consensus_router_descriptor_downloads(now
);
3945 /** Launch extrainfo downloads as needed. */
3947 update_extrainfo_downloads(time_t now
)
3949 or_options_t
*options
= get_options();
3951 smartlist_t
*wanted
;
3952 digestmap_t
*pending
;
3954 int n_no_ei
= 0, n_pending
= 0, n_have
= 0, n_delay
= 0;
3955 if (! options
->DownloadExtraInfo
)
3957 if (should_delay_dir_fetches(options
))
3959 if (!router_have_minimum_dir_info())
3962 pending
= digestmap_new();
3963 list_pending_descriptor_downloads(pending
, 1);
3964 rl
= router_get_routerlist();
3965 wanted
= smartlist_create();
3966 for (old_routers
= 0; old_routers
< 2; ++old_routers
) {
3967 smartlist_t
*lst
= old_routers
? rl
->old_routers
: rl
->routers
;
3968 for (i
= 0; i
< smartlist_len(lst
); ++i
) {
3969 signed_descriptor_t
*sd
;
3972 sd
= smartlist_get(lst
, i
);
3974 sd
= &((routerinfo_t
*)smartlist_get(lst
, i
))->cache_info
;
3975 if (sd
->is_extrainfo
)
3976 continue; /* This should never happen. */
3977 if (old_routers
&& !router_get_by_digest(sd
->identity_digest
))
3978 continue; /* Couldn't check the signature if we got it. */
3979 if (sd
->extrainfo_is_bogus
)
3981 d
= sd
->extra_info_digest
;
3982 if (tor_digest_is_zero(d
)) {
3986 if (eimap_get(rl
->extra_info_map
, d
)) {
3990 if (!download_status_is_ready(&sd
->ei_dl_status
, now
,
3991 MAX_ROUTERDESC_DOWNLOAD_FAILURES
)) {
3995 if (digestmap_get(pending
, d
)) {
3999 smartlist_add(wanted
, d
);
4002 digestmap_free(pending
, NULL
);
4004 log_info(LD_DIR
, "Extrainfo download status: %d router with no ei, %d "
4005 "with present ei, %d delaying, %d pending, %d downloadable.",
4006 n_no_ei
, n_have
, n_delay
, n_pending
, smartlist_len(wanted
));
4008 smartlist_shuffle(wanted
);
4009 for (i
= 0; i
< smartlist_len(wanted
); i
+= MAX_DL_PER_REQUEST
) {
4010 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_EXTRAINFO
,
4011 wanted
, i
, i
+ MAX_DL_PER_REQUEST
);
4014 smartlist_free(wanted
);
4017 /** True iff, the last time we checked whether we had enough directory info
4018 * to build circuits, the answer was "yes". */
4019 static int have_min_dir_info
= 0;
4020 /** True iff enough has changed since the last time we checked whether we had
4021 * enough directory info to build circuits that our old answer can no longer
4023 static int need_to_update_have_min_dir_info
= 1;
4024 /** String describing what we're missing before we have enough directory
4026 static char dir_info_status
[128] = "";
4028 /** Return true iff we have enough networkstatus and router information to
4029 * start building circuits. Right now, this means "more than half the
4030 * networkstatus documents, and at least 1/4 of expected routers." */
4031 //XXX should consider whether we have enough exiting nodes here.
4033 router_have_minimum_dir_info(void)
4035 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info
)) {
4036 update_router_have_minimum_dir_info();
4037 need_to_update_have_min_dir_info
= 0;
4039 return have_min_dir_info
;
4042 /** Called when our internal view of the directory has changed. This can be
4043 * when the authorities change, networkstatuses change, the list of routerdescs
4044 * changes, or number of running routers changes.
4047 router_dir_info_changed(void)
4049 need_to_update_have_min_dir_info
= 1;
4052 /** Return a string describing what we're missing before we have enough
4053 * directory info. */
4055 get_dir_info_status_string(void)
4057 return dir_info_status
;
4060 /** Change the value of have_min_dir_info, setting it true iff we have enough
4061 * network and router information to build circuits. Clear the value of
4062 * need_to_update_have_min_dir_info. */
4064 update_router_have_minimum_dir_info(void)
4066 int num_present
= 0, num_usable
=0;
4067 time_t now
= time(NULL
);
4069 or_options_t
*options
= get_options();
4070 const networkstatus_vote_t
*consensus
=
4071 networkstatus_get_reasonably_live_consensus(now
);
4074 if (!networkstatus_get_latest_consensus())
4075 strlcpy(dir_info_status
, "We have no network-status consensus.",
4076 sizeof(dir_info_status
));
4078 strlcpy(dir_info_status
, "We have no recent network-status consensus.",
4079 sizeof(dir_info_status
));
4084 if (should_delay_dir_fetches(get_options())) {
4085 log_notice(LD_DIR
, "no known bridge descriptors running yet; stalling");
4086 strlcpy(dir_info_status
, "No live bridge descriptors.",
4087 sizeof(dir_info_status
));
4092 SMARTLIST_FOREACH(consensus
->routerstatus_list
, routerstatus_t
*, rs
,
4094 if (client_would_use_router(rs
, now
, options
)) {
4096 if (router_get_by_digest(rs
->identity_digest
)) {
4102 if (num_present
< num_usable
/4) {
4103 tor_snprintf(dir_info_status
, sizeof(dir_info_status
),
4104 "We have only %d/%d usable descriptors.", num_present
, num_usable
);
4106 } else if (num_usable
< 2) {
4107 tor_snprintf(dir_info_status
, sizeof(dir_info_status
),
4108 "Only %d usable descriptor%s known!", num_usable
,
4109 num_usable
? "" : "s");
4116 if (res
&& !have_min_dir_info
) {
4117 log(LOG_NOTICE
, LD_DIR
,
4118 "We now have enough directory information to build circuits.");
4119 control_event_client_status(LOG_NOTICE
, "ENOUGH_DIR_INFO");
4121 if (!res
&& have_min_dir_info
) {
4122 log(LOG_NOTICE
, LD_DIR
,"Our directory information is no longer up-to-date "
4123 "enough to build circuits.%s",
4124 num_usable
> 2 ? "" : " (Not enough servers seem reachable -- "
4125 "is your network connection down?)");
4126 control_event_client_status(LOG_NOTICE
, "NOT_ENOUGH_DIR_INFO");
4128 have_min_dir_info
= res
;
4129 need_to_update_have_min_dir_info
= 0;
4132 /** Reset the descriptor download failure count on all routers, so that we
4133 * can retry any long-failed routers immediately.
4136 router_reset_descriptor_download_failures(void)
4138 networkstatus_reset_download_failures();
4139 last_routerdesc_download_attempted
= 0;
4142 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
4144 download_status_reset(&ri
->cache_info
.ei_dl_status
);
4146 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
4148 download_status_reset(&sd
->ei_dl_status
);
4152 /** Any changes in a router descriptor's publication time larger than this are
4153 * automatically non-cosmetic. */
4154 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
4156 /** We allow uptime to vary from how much it ought to be by this much. */
4157 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
4159 /** Return true iff the only differences between r1 and r2 are such that
4160 * would not cause a recent (post 0.1.1.6) dirserver to republish.
4163 router_differences_are_cosmetic(routerinfo_t
*r1
, routerinfo_t
*r2
)
4165 time_t r1pub
, r2pub
;
4166 int time_difference
;
4167 tor_assert(r1
&& r2
);
4169 /* r1 should be the one that was published first. */
4170 if (r1
->cache_info
.published_on
> r2
->cache_info
.published_on
) {
4171 routerinfo_t
*ri_tmp
= r2
;
4176 /* If any key fields differ, they're different. */
4177 if (strcasecmp(r1
->address
, r2
->address
) ||
4178 strcasecmp(r1
->nickname
, r2
->nickname
) ||
4179 r1
->or_port
!= r2
->or_port
||
4180 r1
->dir_port
!= r2
->dir_port
||
4181 r1
->purpose
!= r2
->purpose
||
4182 crypto_pk_cmp_keys(r1
->onion_pkey
, r2
->onion_pkey
) ||
4183 crypto_pk_cmp_keys(r1
->identity_pkey
, r2
->identity_pkey
) ||
4184 strcasecmp(r1
->platform
, r2
->platform
) ||
4185 (r1
->contact_info
&& !r2
->contact_info
) || /* contact_info is optional */
4186 (!r1
->contact_info
&& r2
->contact_info
) ||
4187 (r1
->contact_info
&& r2
->contact_info
&&
4188 strcasecmp(r1
->contact_info
, r2
->contact_info
)) ||
4189 r1
->is_hibernating
!= r2
->is_hibernating
||
4190 r1
->has_old_dnsworkers
!= r2
->has_old_dnsworkers
||
4191 cmp_addr_policies(r1
->exit_policy
, r2
->exit_policy
))
4193 if ((r1
->declared_family
== NULL
) != (r2
->declared_family
== NULL
))
4195 if (r1
->declared_family
&& r2
->declared_family
) {
4197 if (smartlist_len(r1
->declared_family
)!=smartlist_len(r2
->declared_family
))
4199 n
= smartlist_len(r1
->declared_family
);
4200 for (i
=0; i
< n
; ++i
) {
4201 if (strcasecmp(smartlist_get(r1
->declared_family
, i
),
4202 smartlist_get(r2
->declared_family
, i
)))
4207 /* Did bandwidth change a lot? */
4208 if ((r1
->bandwidthcapacity
< r2
->bandwidthcapacity
/2) ||
4209 (r2
->bandwidthcapacity
< r1
->bandwidthcapacity
/2))
4212 /* Did more than 12 hours pass? */
4213 if (r1
->cache_info
.published_on
+ ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
4214 < r2
->cache_info
.published_on
)
4217 /* Did uptime fail to increase by approximately the amount we would think,
4218 * give or take some slop? */
4219 r1pub
= r1
->cache_info
.published_on
;
4220 r2pub
= r2
->cache_info
.published_on
;
4221 time_difference
= abs(r2
->uptime
- (r1
->uptime
+ (r2pub
- r1pub
)));
4222 if (time_difference
> ROUTER_ALLOW_UPTIME_DRIFT
&&
4223 time_difference
> r1
->uptime
* .05 &&
4224 time_difference
> r2
->uptime
* .05)
4227 /* Otherwise, the difference is cosmetic. */
4231 /** Check whether <b>ri</b> (a.k.a. sd) is a router compatible with the
4232 * extrainfo document
4233 * <b>ei</b>. If no router is compatible with <b>ei</b>, <b>ei</b> should be
4234 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
4235 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
4236 * <b>msg</b> is present, set *<b>msg</b> to a description of the
4237 * incompatibility (if any).
4240 routerinfo_incompatible_with_extrainfo(routerinfo_t
*ri
, extrainfo_t
*ei
,
4241 signed_descriptor_t
*sd
,
4244 int digest_matches
, r
=1;
4248 sd
= &ri
->cache_info
;
4251 if (msg
) *msg
= "Extrainfo signature was bad, or signed with wrong key.";
4255 digest_matches
= !memcmp(ei
->cache_info
.signed_descriptor_digest
,
4256 sd
->extra_info_digest
, DIGEST_LEN
);
4258 /* The identity must match exactly to have been generated at the same time
4259 * by the same router. */
4260 if (memcmp(ri
->cache_info
.identity_digest
, ei
->cache_info
.identity_digest
,
4262 if (msg
) *msg
= "Extrainfo nickname or identity did not match routerinfo";
4263 goto err
; /* different servers */
4266 if (ei
->pending_sig
) {
4267 char signed_digest
[128];
4268 if (crypto_pk_public_checksig(ri
->identity_pkey
, signed_digest
,
4269 ei
->pending_sig
, ei
->pending_sig_len
) != DIGEST_LEN
||
4270 memcmp(signed_digest
, ei
->cache_info
.signed_descriptor_digest
,
4273 tor_free(ei
->pending_sig
);
4274 if (msg
) *msg
= "Extrainfo signature bad, or signed with wrong key";
4275 goto err
; /* Bad signature, or no match. */
4278 ei
->cache_info
.send_unencrypted
= ri
->cache_info
.send_unencrypted
;
4279 tor_free(ei
->pending_sig
);
4282 if (ei
->cache_info
.published_on
< sd
->published_on
) {
4283 if (msg
) *msg
= "Extrainfo published time did not match routerdesc";
4285 } else if (ei
->cache_info
.published_on
> sd
->published_on
) {
4286 if (msg
) *msg
= "Extrainfo published time did not match routerdesc";
4291 if (!digest_matches
) {
4292 if (msg
) *msg
= "Extrainfo digest did not match value from routerdesc";
4293 goto err
; /* Digest doesn't match declared value. */
4298 if (digest_matches
) {
4299 /* This signature was okay, and the digest was right: This is indeed the
4300 * corresponding extrainfo. But insanely, it doesn't match the routerinfo
4301 * that lists it. Don't try to fetch this one again. */
4302 sd
->extrainfo_is_bogus
= 1;
4308 /** Assert that the internal representation of <b>rl</b> is
4309 * self-consistent. */
4311 routerlist_assert_ok(routerlist_t
*rl
)
4313 digestmap_iter_t
*iter
; /* XXXX020 use the appropriate iter type. */
4315 signed_descriptor_t
*sd2
;
4318 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, r
,
4320 r2
= rimap_get(rl
->identity_map
, r
->cache_info
.identity_digest
);
4321 tor_assert(r
== r2
);
4322 sd2
= sdmap_get(rl
->desc_digest_map
,
4323 r
->cache_info
.signed_descriptor_digest
);
4324 tor_assert(&(r
->cache_info
) == sd2
);
4325 tor_assert(r
->cache_info
.routerlist_index
== r_sl_idx
);
4329 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
4330 * commenting this out is just a band-aid.
4332 * The problem is that, although well-behaved router descriptors
4333 * should never have the same value for their extra_info_digest, it's
4334 * possible for ill-behaved routers to claim whatever they like there.
4336 * The real answer is to trash desc_by_eid_map and instead have
4337 * something that indicates for a given extra-info digest we want,
4338 * what its download status is. We'll do that as a part of routerlist
4339 * refactoring once consensus directories are in. For now,
4340 * this rep violation is probably harmless: an adversary can make us
4341 * reset our retry count for an extrainfo, but that's not the end
4344 if (!tor_digest_is_zero(r
->cache_info
.extra_info_digest
)) {
4345 signed_descriptor_t
*sd3
=
4346 sdmap_get(rl
->desc_by_eid_map
, r
->cache_info
.extra_info_digest
);
4347 tor_assert(sd3
== &(r
->cache_info
));
4351 SMARTLIST_FOREACH(rl
->old_routers
, signed_descriptor_t
*, sd
,
4353 r2
= rimap_get(rl
->identity_map
, sd
->identity_digest
);
4354 tor_assert(sd
!= &(r2
->cache_info
));
4355 sd2
= sdmap_get(rl
->desc_digest_map
, sd
->signed_descriptor_digest
);
4356 tor_assert(sd
== sd2
);
4357 tor_assert(sd
->routerlist_index
== sd_sl_idx
);
4359 /* XXXX020 see above. */
4360 if (!tor_digest_is_zero(sd
->extra_info_digest
)) {
4361 signed_descriptor_t
*sd3
=
4362 sdmap_get(rl
->desc_by_eid_map
, sd
->extra_info_digest
);
4363 tor_assert(sd3
== sd
);
4368 iter
= digestmap_iter_init((digestmap_t
*)rl
->identity_map
);
4369 while (!digestmap_iter_done(iter
)) {
4373 digestmap_iter_get(iter
, &d
, &_r
);
4375 tor_assert(!memcmp(r
->cache_info
.identity_digest
, d
, DIGEST_LEN
));
4376 iter
= digestmap_iter_next((digestmap_t
*)rl
->identity_map
, iter
);
4378 iter
= digestmap_iter_init((digestmap_t
*)rl
->desc_digest_map
);
4379 while (!digestmap_iter_done(iter
)) {
4382 signed_descriptor_t
*sd
;
4383 digestmap_iter_get(iter
, &d
, &_sd
);
4385 tor_assert(!memcmp(sd
->signed_descriptor_digest
, d
, DIGEST_LEN
));
4386 iter
= digestmap_iter_next((digestmap_t
*)rl
->desc_digest_map
, iter
);
4388 iter
= digestmap_iter_init((digestmap_t
*)rl
->desc_by_eid_map
);
4389 while (!digestmap_iter_done(iter
)) {
4392 signed_descriptor_t
*sd
;
4393 digestmap_iter_get(iter
, &d
, &_sd
);
4395 tor_assert(!tor_digest_is_zero(d
));
4397 tor_assert(!memcmp(sd
->extra_info_digest
, d
, DIGEST_LEN
));
4398 iter
= digestmap_iter_next((digestmap_t
*)rl
->desc_by_eid_map
, iter
);
4400 iter
= digestmap_iter_init((digestmap_t
*)rl
->extra_info_map
);
4401 while (!digestmap_iter_done(iter
)) {
4405 signed_descriptor_t
*sd
;
4406 digestmap_iter_get(iter
, &d
, &_ei
);
4408 tor_assert(!memcmp(ei
->cache_info
.signed_descriptor_digest
,
4410 sd
= sdmap_get(rl
->desc_by_eid_map
,
4411 ei
->cache_info
.signed_descriptor_digest
);
4412 // tor_assert(sd); // XXXX020 see above
4414 tor_assert(!memcmp(ei
->cache_info
.signed_descriptor_digest
,
4415 sd
->extra_info_digest
, DIGEST_LEN
));
4417 iter
= digestmap_iter_next((digestmap_t
*)rl
->extra_info_map
, iter
);
4421 /** Allocate and return a new string representing the contact info
4422 * and platform string for <b>router</b>,
4423 * surrounded by quotes and using standard C escapes.
4425 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
4426 * thread. Also, each call invalidates the last-returned value, so don't
4427 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
4429 * If <b>router</b> is NULL, it just frees its internal memory and returns.
4432 esc_router_info(routerinfo_t
*router
)
4434 static char *info
=NULL
;
4435 char *esc_contact
, *esc_platform
;
4440 return NULL
; /* we're exiting; just free the memory we use */
4442 esc_contact
= esc_for_log(router
->contact_info
);
4443 esc_platform
= esc_for_log(router
->platform
);
4445 len
= strlen(esc_contact
)+strlen(esc_platform
)+32;
4446 info
= tor_malloc(len
);
4447 tor_snprintf(info
, len
, "Contact %s, Platform %s", esc_contact
,
4449 tor_free(esc_contact
);
4450 tor_free(esc_platform
);
4455 /** Helper for sorting: compare two routerinfos by their identity
4458 _compare_routerinfo_by_id_digest(const void **a
, const void **b
)
4460 routerinfo_t
*first
= *(routerinfo_t
**)a
, *second
= *(routerinfo_t
**)b
;
4461 return memcmp(first
->cache_info
.identity_digest
,
4462 second
->cache_info
.identity_digest
,
4466 /** Sort a list of routerinfo_t in ascending order of identity digest. */
4468 routers_sort_by_identity(smartlist_t
*routers
)
4470 smartlist_sort(routers
, _compare_routerinfo_by_id_digest
);
4473 /** Determine the routers that are responsible for <b>id</b> (binary) and
4474 * add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
4475 * Return -1 if we're returning an empty smartlist, else return 0.
4478 hid_serv_get_responsible_directories(smartlist_t
*responsible_dirs
,
4481 int start
, found
, n_added
= 0, i
;
4482 networkstatus_vote_t
*c
= networkstatus_get_latest_consensus();
4483 if (!c
|| !smartlist_len(c
->routerstatus_list
)) {
4484 log_warn(LD_REND
, "We don't have a consensus, so we can't perform v2 "
4485 "rendezvous operations.");
4489 start
= networkstatus_vote_find_entry_idx(c
, id
, &found
);
4490 if (start
== smartlist_len(c
->routerstatus_list
)) start
= 0;
4493 routerstatus_t
*r
= smartlist_get(c
->routerstatus_list
, i
);
4495 smartlist_add(responsible_dirs
, r
);
4496 if (++n_added
== REND_NUMBER_OF_CONSECUTIVE_REPLICAS
)
4499 if (++i
== smartlist_len(c
->routerstatus_list
))
4501 } while (i
!= start
);
4503 /* Even though we don't have the desired number of hidden service
4504 * directories, be happy if we got any. */
4505 return smartlist_len(responsible_dirs
) ? 0 : -1;
4508 /** Return true if this node is currently acting as hidden service
4509 * directory, false otherwise. */
4511 hid_serv_acting_as_directory(void)
4513 routerinfo_t
*me
= router_get_my_routerinfo();
4514 networkstatus_vote_t
*c
;
4518 if (!get_options()->HidServDirectoryV2
) {
4519 log_info(LD_REND
, "We are not acting as hidden service directory, "
4520 "because we have not been configured as such.");
4523 if (!(c
= networkstatus_get_latest_consensus())) {
4524 log_info(LD_REND
, "There's no consensus, so I can't tell if I'm a hidden "
4525 "service directory");
4528 rs
= networkstatus_vote_find_entry(c
, me
->cache_info
.identity_digest
);
4530 log_info(LD_REND
, "We're not listed in the consensus, so we're not "
4531 "being a hidden service directory.");
4534 if (!rs
->is_hs_dir
) {
4535 log_info(LD_REND
, "We're not listed as a hidden service directory in "
4536 "the consensus, so we won't be one.");
4542 /** Return true if this node is responsible for storing the descriptor ID
4543 * in <b>query</b> and false otherwise. */
4545 hid_serv_responsible_for_desc_id(const char *query
)
4548 routerstatus_t
*last_rs
;
4549 const char *my_id
, *last_id
;
4551 smartlist_t
*responsible
;
4552 if (!hid_serv_acting_as_directory())
4554 if (!(me
= router_get_my_routerinfo()))
4555 return 0; /* This is redundant, but let's be paranoid. */
4556 my_id
= me
->cache_info
.identity_digest
;
4557 responsible
= smartlist_create();
4558 if (hid_serv_get_responsible_directories(responsible
, query
) < 0) {
4559 smartlist_free(responsible
);
4562 last_rs
= smartlist_get(responsible
, smartlist_len(responsible
)-1);
4563 last_id
= last_rs
->identity_digest
;
4564 result
= rend_id_is_in_interval(my_id
, query
, last_id
);
4565 smartlist_free(responsible
);