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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
10 * maintain and access the global list of routerinfos for known
13 * A "routerinfo_t" object represents a single self-signed router
14 * descriptor, as generated by a Tor relay in order to tell the rest of
15 * the world about its keys, address, and capabilities. An
16 * "extrainfo_t" object represents an adjunct "extra-info" object,
17 * certified by a corresponding router descriptor, reporting more
18 * information about the relay that nearly all users will not need.
20 * Most users will not use router descriptors for most relays. Instead,
21 * they use the information in microdescriptors and in the consensus
24 * Right now, routerinfo_t objects are used in these ways:
26 * <li>By clients, in order to learn about bridge keys and capabilities.
27 * (Bridges aren't listed in the consensus networkstatus, so they
28 * can't have microdescriptors.)
29 * <li>By relays, since relays want more information about other relays
30 * than they can learn from microdescriptors. (TODO: Is this still true?)
31 * <li>By authorities, which receive them and use them to generate the
32 * consensus and the microdescriptors.
33 * <li>By all directory caches, which download them in case somebody
37 * Routerinfos are mostly created by parsing them from a string, in
38 * routerparse.c. We store them to disk on receiving them, and
39 * periodically discard the ones we don't need. On restarting, we
40 * re-read them from disk. (This also applies to extrainfo documents, if
41 * we are configured to fetch them.)
43 * In order to keep our list of routerinfos up-to-date, we periodically
44 * check whether there are any listed in the latest consensus (or in the
45 * votes from other authorities, if we are an authority) that we don't
46 * have. (This also applies to extrainfo documents, if we are
47 * configured to fetch them.)
49 * Almost nothing in Tor should use a routerinfo_t to refer directly to
50 * a relay; instead, almost everything should use node_t (implemented in
51 * nodelist.c), which provides a common interface to routerinfo_t,
52 * routerstatus_t, and microdescriptor_t.
56 * This module also has some of the functions used for choosing random
57 * nodes according to different rules and weights. Historically, they
58 * were all in this module. Now, they are spread across this module,
59 * nodelist.c, and networkstatus.c. (TODO: Fix that.)
62 #define ROUTERLIST_PRIVATE
63 #include "core/or/or.h"
65 #include "app/config/config.h"
66 #include "core/mainloop/connection.h"
67 #include "core/mainloop/mainloop.h"
68 #include "core/or/policies.h"
69 #include "feature/client/bridges.h"
70 #include "feature/control/control.h"
71 #include "feature/dirauth/authmode.h"
72 #include "feature/dirauth/process_descs.h"
73 #include "feature/dirauth/reachability.h"
74 #include "feature/dircache/dirserv.h"
75 #include "feature/dirclient/dirclient.h"
76 #include "feature/dirclient/dlstatus.h"
77 #include "feature/dircommon/directory.h"
78 #include "feature/nodelist/authcert.h"
79 #include "feature/nodelist/describe.h"
80 #include "feature/nodelist/dirlist.h"
81 #include "feature/nodelist/microdesc.h"
82 #include "feature/nodelist/networkstatus.h"
83 #include "feature/nodelist/node_select.h"
84 #include "feature/nodelist/nodelist.h"
85 #include "feature/nodelist/routerinfo.h"
86 #include "feature/nodelist/routerlist.h"
87 #include "feature/dirparse/routerparse.h"
88 #include "feature/nodelist/routerset.h"
89 #include "feature/nodelist/torcert.h"
90 #include "feature/relay/routermode.h"
91 #include "feature/stats/rephist.h"
92 #include "lib/crypt_ops/crypto_format.h"
93 #include "lib/crypt_ops/crypto_rand.h"
95 #include "feature/dircommon/dir_connection_st.h"
96 #include "feature/dirclient/dir_server_st.h"
97 #include "feature/nodelist/document_signature_st.h"
98 #include "feature/nodelist/extrainfo_st.h"
99 #include "feature/nodelist/networkstatus_st.h"
100 #include "feature/nodelist/networkstatus_voter_info_st.h"
101 #include "feature/nodelist/node_st.h"
102 #include "feature/nodelist/routerinfo_st.h"
103 #include "feature/nodelist/routerlist_st.h"
104 #include "feature/nodelist/vote_routerstatus_st.h"
106 #include "lib/crypt_ops/digestset.h"
108 #ifdef HAVE_SYS_STAT_H
109 #include <sys/stat.h>
112 // #define DEBUG_ROUTERLIST
114 /****************************************************************************/
116 /* Typed wrappers for different digestmap types; used to avoid type
119 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_
, digest_sd_map_t
, signed_descriptor_t
)
120 DECLARE_TYPED_DIGESTMAP_FNS(rimap_
, digest_ri_map_t
, routerinfo_t
)
121 DECLARE_TYPED_DIGESTMAP_FNS(eimap_
, digest_ei_map_t
, extrainfo_t
)
122 #define SDMAP_FOREACH(map, keyvar, valvar) \
123 DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
125 #define RIMAP_FOREACH(map, keyvar, valvar) \
126 DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
127 #define EIMAP_FOREACH(map, keyvar, valvar) \
128 DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
129 #define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn))
130 #define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn))
131 #define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn))
133 /* static function prototypes */
134 static int signed_desc_digest_is_recognized(signed_descriptor_t
*desc
);
135 static const char *signed_descriptor_get_body_impl(
136 const signed_descriptor_t
*desc
,
137 int with_annotations
);
138 static void launch_dummy_descriptor_download_as_needed(time_t now
,
139 const or_options_t
*options
);
141 /****************************************************************************/
143 /** Global list of all of the routers that we know about. */
144 static routerlist_t
*routerlist
= NULL
;
146 /** List of strings for nicknames we've already warned about and that are
147 * still unknown / unavailable. */
148 static smartlist_t
*warned_nicknames
= NULL
;
150 /** The last time we tried to download any routerdesc, or 0 for "never". We
151 * use this to rate-limit download attempts when the number of routerdescs to
152 * download is low. */
153 static time_t last_descriptor_download_attempted
= 0;
155 /* Router descriptor storage.
157 * Routerdescs are stored in a big file, named "cached-descriptors". As new
158 * routerdescs arrive, we append them to a journal file named
159 * "cached-descriptors.new".
161 * From time to time, we replace "cached-descriptors" with a new file
162 * containing only the live, non-superseded descriptors, and clear
163 * cached-descriptors.new.
165 * On startup, we read both files.
168 /** Helper: return 1 iff the router log is so big we want to rebuild the
171 router_should_rebuild_store(desc_store_t
*store
)
173 if (store
->store_len
> (1<<16))
174 return (store
->journal_len
> store
->store_len
/ 2 ||
175 store
->bytes_dropped
> store
->store_len
/ 2);
177 return store
->journal_len
> (1<<15);
180 /** Return the desc_store_t in <b>rl</b> that should be used to store
182 static inline desc_store_t
*
183 desc_get_store(routerlist_t
*rl
, const signed_descriptor_t
*sd
)
185 if (sd
->is_extrainfo
)
186 return &rl
->extrainfo_store
;
188 return &rl
->desc_store
;
191 /** Add the signed_descriptor_t in <b>desc</b> to the router
192 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
193 * offset appropriately. */
195 signed_desc_append_to_journal(signed_descriptor_t
*desc
,
198 char *fname
= get_cachedir_fname_suffix(store
->fname_base
, ".new");
199 const char *body
= signed_descriptor_get_body_impl(desc
,1);
200 size_t len
= desc
->signed_descriptor_len
+ desc
->annotations_len
;
202 if (append_bytes_to_file(fname
, body
, len
, 1)) {
203 log_warn(LD_FS
, "Unable to store router descriptor");
207 desc
->saved_location
= SAVED_IN_JOURNAL
;
210 desc
->saved_offset
= store
->journal_len
;
211 store
->journal_len
+= len
;
216 /** Sorting helper: return <0, 0, or >0 depending on whether the
217 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
218 * the signed_descriptor_t* in *<b>b</b>. */
220 compare_signed_descriptors_by_age_(const void **_a
, const void **_b
)
222 const signed_descriptor_t
*r1
= *_a
, *r2
= *_b
;
223 return (int)(r1
->published_on
- r2
->published_on
);
227 #define RRS_DONT_REMOVE_OLD 2
229 /** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
230 * <b>flags</b>, then atomically replace the saved router store with the
231 * routers currently in our routerlist, and clear the journal. Unless
232 * RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
233 * rebuilding the store. Return 0 on success, -1 on failure.
236 router_rebuild_store(int flags
, desc_store_t
*store
)
238 smartlist_t
*chunk_list
= NULL
;
239 char *fname
= NULL
, *fname_tmp
= NULL
;
242 smartlist_t
*signed_descriptors
= NULL
;
244 size_t total_expected_len
= 0;
246 int force
= flags
& RRS_FORCE
;
248 if (!force
&& !router_should_rebuild_store(store
)) {
257 if (store
->type
== EXTRAINFO_STORE
)
258 had_any
= !eimap_isempty(routerlist
->extra_info_map
);
260 had_any
= (smartlist_len(routerlist
->routers
)+
261 smartlist_len(routerlist
->old_routers
))>0;
263 /* Don't save deadweight. */
264 if (!(flags
& RRS_DONT_REMOVE_OLD
))
265 routerlist_remove_old_routers();
267 log_info(LD_DIR
, "Rebuilding %s cache", store
->description
);
269 fname
= get_cachedir_fname(store
->fname_base
);
270 fname_tmp
= get_cachedir_fname_suffix(store
->fname_base
, ".tmp");
272 chunk_list
= smartlist_new();
274 /* We sort the routers by age to enhance locality on disk. */
275 signed_descriptors
= smartlist_new();
276 if (store
->type
== EXTRAINFO_STORE
) {
278 for (iter
= eimap_iter_init(routerlist
->extra_info_map
);
279 !eimap_iter_done(iter
);
280 iter
= eimap_iter_next(routerlist
->extra_info_map
, iter
)) {
283 eimap_iter_get(iter
, &key
, &ei
);
284 smartlist_add(signed_descriptors
, &ei
->cache_info
);
287 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
288 smartlist_add(signed_descriptors
, sd
));
289 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
290 smartlist_add(signed_descriptors
, &ri
->cache_info
));
293 smartlist_sort(signed_descriptors
, compare_signed_descriptors_by_age_
);
295 /* Now, add the appropriate members to chunk_list */
296 SMARTLIST_FOREACH_BEGIN(signed_descriptors
, signed_descriptor_t
*, sd
) {
298 const char *body
= signed_descriptor_get_body_impl(sd
, 1);
300 log_warn(LD_BUG
, "No descriptor available for router.");
303 if (sd
->do_not_cache
) {
307 c
= tor_malloc(sizeof(sized_chunk_t
));
309 c
->len
= sd
->signed_descriptor_len
+ sd
->annotations_len
;
310 total_expected_len
+= c
->len
;
311 smartlist_add(chunk_list
, c
);
312 } SMARTLIST_FOREACH_END(sd
);
314 if (write_chunks_to_file(fname_tmp
, chunk_list
, 1, 1)<0) {
315 log_warn(LD_FS
, "Error writing router store to disk.");
319 /* Our mmap is now invalid. */
321 int res
= tor_munmap_file(store
->mmap
);
324 log_warn(LD_FS
, "Unable to munmap route store in %s", fname
);
328 if (replace_file(fname_tmp
, fname
)<0) {
329 log_warn(LD_FS
, "Error replacing old router store: %s", strerror(errno
));
334 store
->mmap
= tor_mmap_file(fname
);
336 if (errno
== ERANGE
) {
338 if (total_expected_len
) {
339 log_warn(LD_FS
, "We wrote some bytes to a new descriptor file at '%s',"
340 " but when we went to mmap it, it was empty!", fname
);
341 } else if (had_any
) {
342 log_info(LD_FS
, "We just removed every descriptor in '%s'. This is "
343 "okay if we're just starting up after a long time. "
344 "Otherwise, it's a bug.", fname
);
347 log_warn(LD_FS
, "Unable to mmap new descriptor file at '%s'.",fname
);
351 log_info(LD_DIR
, "Reconstructing pointers into cache");
354 SMARTLIST_FOREACH_BEGIN(signed_descriptors
, signed_descriptor_t
*, sd
) {
355 if (sd
->do_not_cache
)
357 sd
->saved_location
= SAVED_IN_CACHE
;
359 tor_free(sd
->signed_descriptor_body
); // sets it to null
360 sd
->saved_offset
= offset
;
362 offset
+= sd
->signed_descriptor_len
+ sd
->annotations_len
;
363 signed_descriptor_get_body(sd
); /* reconstruct and assert */
364 } SMARTLIST_FOREACH_END(sd
);
367 fname
= get_cachedir_fname_suffix(store
->fname_base
, ".new");
368 write_str_to_file(fname
, "", 1);
371 store
->store_len
= (size_t) offset
;
372 store
->journal_len
= 0;
373 store
->bytes_dropped
= 0;
375 smartlist_free(signed_descriptors
);
379 SMARTLIST_FOREACH(chunk_list
, sized_chunk_t
*, c
, tor_free(c
));
380 smartlist_free(chunk_list
);
386 /** Helper: Reload a cache file and its associated journal, setting metadata
387 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
388 * else reload the router descriptor store. */
390 router_reload_router_list_impl(desc_store_t
*store
)
392 char *fname
= NULL
, *contents
= NULL
;
394 int extrainfo
= (store
->type
== EXTRAINFO_STORE
);
395 store
->journal_len
= store
->store_len
= 0;
397 fname
= get_cachedir_fname(store
->fname_base
);
400 /* get rid of it first */
401 int res
= tor_munmap_file(store
->mmap
);
404 log_warn(LD_FS
, "Failed to munmap %s", fname
);
410 store
->mmap
= tor_mmap_file(fname
);
412 store
->store_len
= store
->mmap
->size
;
414 router_load_extrainfo_from_string(store
->mmap
->data
,
415 store
->mmap
->data
+store
->mmap
->size
,
416 SAVED_IN_CACHE
, NULL
, 0);
418 router_load_routers_from_string(store
->mmap
->data
,
419 store
->mmap
->data
+store
->mmap
->size
,
420 SAVED_IN_CACHE
, NULL
, 0, NULL
);
424 fname
= get_cachedir_fname_suffix(store
->fname_base
, ".new");
425 /* don't load empty files - we wouldn't get any data, even if we tried */
426 if (file_status(fname
) == FN_FILE
)
427 contents
= read_file_to_str(fname
, RFTS_BIN
|RFTS_IGNORE_MISSING
, &st
);
430 router_load_extrainfo_from_string(contents
, NULL
,SAVED_IN_JOURNAL
,
433 router_load_routers_from_string(contents
, NULL
, SAVED_IN_JOURNAL
,
435 store
->journal_len
= (size_t) st
.st_size
;
441 if (store
->journal_len
) {
442 /* Always clear the journal on startup.*/
443 router_rebuild_store(RRS_FORCE
, store
);
444 } else if (!extrainfo
) {
445 /* Don't cache expired routers. (This is in an else because
446 * router_rebuild_store() also calls remove_old_routers().) */
447 routerlist_remove_old_routers();
453 /** Load all cached router descriptors and extra-info documents from the
454 * store. Return 0 on success and -1 on failure.
457 router_reload_router_list(void)
459 routerlist_t
*rl
= router_get_routerlist();
460 if (router_reload_router_list_impl(&rl
->desc_store
))
462 if (router_reload_router_list_impl(&rl
->extrainfo_store
))
467 /* When iterating through the routerlist, can OR address/port preference
468 * and reachability checks be skipped?
471 router_skip_or_reachability(const or_options_t
*options
, int try_ip_pref
)
473 /* Servers always have and prefer IPv4.
474 * And if clients are checking against the firewall for reachability only,
475 * but there's no firewall, don't bother checking */
476 return server_mode(options
) || (!try_ip_pref
&& !firewall_is_fascist_or());
479 /* When iterating through the routerlist, can Dir address/port preference
480 * and reachability checks be skipped?
483 router_skip_dir_reachability(const or_options_t
*options
, int try_ip_pref
)
485 /* Servers always have and prefer IPv4.
486 * And if clients are checking against the firewall for reachability only,
487 * but there's no firewall, don't bother checking */
488 return server_mode(options
) || (!try_ip_pref
&& !firewall_is_fascist_dir());
491 /** Return true iff r1 and r2 have the same address and OR port. */
493 routers_have_same_or_addrs(const routerinfo_t
*r1
, const routerinfo_t
*r2
)
495 return r1
->addr
== r2
->addr
&& r1
->or_port
== r2
->or_port
&&
496 tor_addr_eq(&r1
->ipv6_addr
, &r2
->ipv6_addr
) &&
497 r1
->ipv6_orport
== r2
->ipv6_orport
;
500 /** Add every suitable node from our nodelist to <b>sl</b>, so that
501 * we can pick a node for a circuit.
504 router_add_running_nodes_to_smartlist(smartlist_t
*sl
, int need_uptime
,
505 int need_capacity
, int need_guard
,
506 int need_desc
, int pref_addr
,
509 const int check_reach
= !router_skip_or_reachability(get_options(),
512 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t
*, node
) {
513 if (!node
->is_running
|| !node
->is_valid
)
515 if (need_desc
&& !node_has_preferred_descriptor(node
, direct_conn
))
517 if (node
->ri
&& node
->ri
->purpose
!= ROUTER_PURPOSE_GENERAL
)
519 if (node_is_unreliable(node
, need_uptime
, need_capacity
, need_guard
))
521 /* Don't choose nodes if we are certain they can't do EXTEND2 cells */
522 if (node
->rs
&& !routerstatus_version_supports_extend2_cells(node
->rs
, 1))
524 /* Don't choose nodes if we are certain they can't do ntor. */
525 if ((node
->ri
|| node
->md
) && !node_has_curve25519_onion_key(node
))
527 /* Choose a node with an OR address that matches the firewall rules */
528 if (direct_conn
&& check_reach
&&
529 !fascist_firewall_allows_node(node
,
530 FIREWALL_OR_CONNECTION
,
534 smartlist_add(sl
, (void *)node
);
535 } SMARTLIST_FOREACH_END(node
);
538 /** Look through the routerlist until we find a router that has my key.
541 routerlist_find_my_routerinfo(void)
546 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, router
,
548 if (router_is_me(router
))
554 /** Return the smaller of the router's configured BandwidthRate
555 * and its advertised capacity. */
557 router_get_advertised_bandwidth(const routerinfo_t
*router
)
559 if (router
->bandwidthcapacity
< router
->bandwidthrate
)
560 return router
->bandwidthcapacity
;
561 return router
->bandwidthrate
;
564 /** Do not weight any declared bandwidth more than this much when picking
565 * routers by bandwidth. */
566 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
568 /** Return the smaller of the router's configured BandwidthRate
569 * and its advertised capacity, capped by max-believe-bw. */
571 router_get_advertised_bandwidth_capped(const routerinfo_t
*router
)
573 uint32_t result
= router
->bandwidthcapacity
;
574 if (result
> router
->bandwidthrate
)
575 result
= router
->bandwidthrate
;
576 if (result
> DEFAULT_MAX_BELIEVABLE_BANDWIDTH
)
577 result
= DEFAULT_MAX_BELIEVABLE_BANDWIDTH
;
581 /** Helper: given an extended nickname in <b>hexdigest</b> try to decode it.
582 * Return 0 on success, -1 on failure. Store the result into the
583 * DIGEST_LEN-byte buffer at <b>digest_out</b>, the single character at
584 * <b>nickname_qualifier_char_out</b>, and the MAXNICKNAME_LEN+1-byte buffer
585 * at <b>nickname_out</b>.
587 * The recognized format is:
588 * HexName = Dollar? HexDigest NamePart?
590 * HexDigest = HexChar*20
591 * HexChar = 'a'..'f' | 'A'..'F' | '0'..'9'
592 * NamePart = QualChar Name
593 * QualChar = '=' | '~'
594 * Name = NameChar*(1..MAX_NICKNAME_LEN)
595 * NameChar = Any ASCII alphanumeric character
598 hex_digest_nickname_decode(const char *hexdigest
,
600 char *nickname_qualifier_char_out
,
605 tor_assert(hexdigest
);
606 if (hexdigest
[0] == '$')
609 len
= strlen(hexdigest
);
610 if (len
< HEX_DIGEST_LEN
) {
612 } else if (len
> HEX_DIGEST_LEN
&& (hexdigest
[HEX_DIGEST_LEN
] == '=' ||
613 hexdigest
[HEX_DIGEST_LEN
] == '~') &&
614 len
<= HEX_DIGEST_LEN
+1+MAX_NICKNAME_LEN
) {
615 *nickname_qualifier_char_out
= hexdigest
[HEX_DIGEST_LEN
];
616 strlcpy(nickname_out
, hexdigest
+HEX_DIGEST_LEN
+1 , MAX_NICKNAME_LEN
+1);
617 } else if (len
== HEX_DIGEST_LEN
) {
623 if (base16_decode(digest_out
, DIGEST_LEN
,
624 hexdigest
, HEX_DIGEST_LEN
) != DIGEST_LEN
)
629 /** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
630 * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
631 * (which is optionally prefixed with a single dollar sign). Return false if
632 * <b>hexdigest</b> is malformed, or it doesn't match. */
634 hex_digest_nickname_matches(const char *hexdigest
, const char *identity_digest
,
635 const char *nickname
)
637 char digest
[DIGEST_LEN
];
639 char nn_buf
[MAX_NICKNAME_LEN
+1];
641 if (hex_digest_nickname_decode(hexdigest
, digest
, &nn_char
, nn_buf
) == -1)
644 if (nn_char
== '=') {
648 if (nn_char
== '~') {
649 if (!nickname
) // XXX This seems wrong. -NM
651 if (strcasecmp(nn_buf
, nickname
))
655 return tor_memeq(digest
, identity_digest
, DIGEST_LEN
);
658 /** If hexdigest is correctly formed, base16_decode it into
659 * digest, which must have DIGEST_LEN space in it.
660 * Return 0 on success, -1 on failure.
663 hexdigest_to_digest(const char *hexdigest
, char *digest
)
665 if (hexdigest
[0]=='$')
667 if (strlen(hexdigest
) < HEX_DIGEST_LEN
||
668 base16_decode(digest
,DIGEST_LEN
,hexdigest
,HEX_DIGEST_LEN
) != DIGEST_LEN
)
673 /** As router_get_by_id_digest,but return a pointer that you're allowed to
676 router_get_mutable_by_digest(const char *digest
)
680 if (!routerlist
) return NULL
;
682 // routerlist_assert_ok(routerlist);
684 return rimap_get(routerlist
->identity_map
, digest
);
687 /** Return the router in our routerlist whose 20-byte key digest
688 * is <b>digest</b>. Return NULL if no such router is known. */
690 router_get_by_id_digest(const char *digest
)
692 return router_get_mutable_by_digest(digest
);
695 /** Return the router in our routerlist whose 20-byte descriptor
696 * is <b>digest</b>. Return NULL if no such router is known. */
697 signed_descriptor_t
*
698 router_get_by_descriptor_digest(const char *digest
)
702 if (!routerlist
) return NULL
;
704 return sdmap_get(routerlist
->desc_digest_map
, digest
);
707 /** Return the signed descriptor for the router in our routerlist whose
708 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
710 MOCK_IMPL(signed_descriptor_t
*,
711 router_get_by_extrainfo_digest
,(const char *digest
))
715 if (!routerlist
) return NULL
;
717 return sdmap_get(routerlist
->desc_by_eid_map
, digest
);
720 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
721 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
722 * document is known. */
723 MOCK_IMPL(signed_descriptor_t
*,
724 extrainfo_get_by_descriptor_digest
,(const char *digest
))
728 if (!routerlist
) return NULL
;
729 ei
= eimap_get(routerlist
->extra_info_map
, digest
);
730 return ei
? &ei
->cache_info
: NULL
;
733 /** Return a pointer to the signed textual representation of a descriptor.
734 * The returned string is not guaranteed to be NUL-terminated: the string's
735 * length will be in desc-\>signed_descriptor_len.
737 * If <b>with_annotations</b> is set, the returned string will include
739 * (if any) preceding the descriptor. This will increase the length of the
740 * string by desc-\>annotations_len.
742 * The caller must not free the string returned.
745 signed_descriptor_get_body_impl(const signed_descriptor_t
*desc
,
746 int with_annotations
)
748 const char *r
= NULL
;
749 size_t len
= desc
->signed_descriptor_len
;
750 off_t offset
= desc
->saved_offset
;
751 if (with_annotations
)
752 len
+= desc
->annotations_len
;
754 offset
+= desc
->annotations_len
;
756 tor_assert(len
> 32);
757 if (desc
->saved_location
== SAVED_IN_CACHE
&& routerlist
) {
758 desc_store_t
*store
= desc_get_store(router_get_routerlist(), desc
);
759 if (store
&& store
->mmap
) {
760 tor_assert(desc
->saved_offset
+ len
<= store
->mmap
->size
);
761 r
= store
->mmap
->data
+ offset
;
763 log_err(LD_DIR
, "We couldn't read a descriptor that is supposedly "
764 "mmaped in our cache. Is another process running in our data "
765 "directory? Exiting.");
766 exit(1); // XXXX bad exit: should recover.
769 if (!r
) /* no mmap, or not in cache. */
770 r
= desc
->signed_descriptor_body
+
771 (with_annotations
? 0 : desc
->annotations_len
);
774 if (!with_annotations
) {
775 if (fast_memcmp("router ", r
, 7) && fast_memcmp("extra-info ", r
, 11)) {
776 char *cp
= tor_strndup(r
, 64);
777 log_err(LD_DIR
, "descriptor at %p begins with unexpected string %s. "
778 "Is another process running in our data directory? Exiting.",
780 exit(1); // XXXX bad exit: should recover.
787 /** Return a pointer to the signed textual representation of a descriptor.
788 * The returned string is not guaranteed to be NUL-terminated: the string's
789 * length will be in desc-\>signed_descriptor_len.
791 * The caller must not free the string returned.
794 signed_descriptor_get_body(const signed_descriptor_t
*desc
)
796 return signed_descriptor_get_body_impl(desc
, 0);
799 /** As signed_descriptor_get_body(), but points to the beginning of the
800 * annotations section rather than the beginning of the descriptor. */
802 signed_descriptor_get_annotations(const signed_descriptor_t
*desc
)
804 return signed_descriptor_get_body_impl(desc
, 1);
807 /** Return the current list of all known routers. */
809 router_get_routerlist(void)
811 if (PREDICT_UNLIKELY(!routerlist
)) {
812 routerlist
= tor_malloc_zero(sizeof(routerlist_t
));
813 routerlist
->routers
= smartlist_new();
814 routerlist
->old_routers
= smartlist_new();
815 routerlist
->identity_map
= rimap_new();
816 routerlist
->desc_digest_map
= sdmap_new();
817 routerlist
->desc_by_eid_map
= sdmap_new();
818 routerlist
->extra_info_map
= eimap_new();
820 routerlist
->desc_store
.fname_base
= "cached-descriptors";
821 routerlist
->extrainfo_store
.fname_base
= "cached-extrainfo";
823 routerlist
->desc_store
.type
= ROUTER_STORE
;
824 routerlist
->extrainfo_store
.type
= EXTRAINFO_STORE
;
826 routerlist
->desc_store
.description
= "router descriptors";
827 routerlist
->extrainfo_store
.description
= "extra-info documents";
832 /** Free all storage held by <b>router</b>. */
834 routerinfo_free_(routerinfo_t
*router
)
839 tor_free(router
->cache_info
.signed_descriptor_body
);
840 tor_free(router
->nickname
);
841 tor_free(router
->platform
);
842 tor_free(router
->protocol_list
);
843 tor_free(router
->contact_info
);
844 if (router
->onion_pkey
)
845 tor_free(router
->onion_pkey
);
846 tor_free(router
->onion_curve25519_pkey
);
847 if (router
->identity_pkey
)
848 crypto_pk_free(router
->identity_pkey
);
849 tor_cert_free(router
->cache_info
.signing_key_cert
);
850 if (router
->declared_family
) {
851 SMARTLIST_FOREACH(router
->declared_family
, char *, s
, tor_free(s
));
852 smartlist_free(router
->declared_family
);
854 addr_policy_list_free(router
->exit_policy
);
855 short_policy_free(router
->ipv6_exit_policy
);
857 memset(router
, 77, sizeof(routerinfo_t
));
862 /** Release all storage held by <b>extrainfo</b> */
864 extrainfo_free_(extrainfo_t
*extrainfo
)
868 tor_cert_free(extrainfo
->cache_info
.signing_key_cert
);
869 tor_free(extrainfo
->cache_info
.signed_descriptor_body
);
870 tor_free(extrainfo
->pending_sig
);
872 memset(extrainfo
, 88, sizeof(extrainfo_t
)); /* debug bad memory usage */
876 #define signed_descriptor_free(val) \
877 FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val))
879 /** Release storage held by <b>sd</b>. */
881 signed_descriptor_free_(signed_descriptor_t
*sd
)
886 tor_free(sd
->signed_descriptor_body
);
887 tor_cert_free(sd
->signing_key_cert
);
889 memset(sd
, 99, sizeof(signed_descriptor_t
)); /* Debug bad mem usage */
893 /** Reset the given signed descriptor <b>sd</b> by freeing the allocated
894 * memory inside the object and by zeroing its content. */
896 signed_descriptor_reset(signed_descriptor_t
*sd
)
899 tor_free(sd
->signed_descriptor_body
);
900 tor_cert_free(sd
->signing_key_cert
);
901 memset(sd
, 0, sizeof(*sd
));
904 /** Copy src into dest, and steal all references inside src so that when
905 * we free src, we don't mess up dest. */
907 signed_descriptor_move(signed_descriptor_t
*dest
,
908 signed_descriptor_t
*src
)
910 tor_assert(dest
!= src
);
911 /* Cleanup destination object before overwriting it.*/
912 signed_descriptor_reset(dest
);
913 memcpy(dest
, src
, sizeof(signed_descriptor_t
));
914 src
->signed_descriptor_body
= NULL
;
915 src
->signing_key_cert
= NULL
;
916 dest
->routerlist_index
= -1;
919 /** Extract a signed_descriptor_t from a general routerinfo, and free the
922 static signed_descriptor_t
*
923 signed_descriptor_from_routerinfo(routerinfo_t
*ri
)
925 signed_descriptor_t
*sd
;
926 tor_assert(ri
->purpose
== ROUTER_PURPOSE_GENERAL
);
927 sd
= tor_malloc_zero(sizeof(signed_descriptor_t
));
928 signed_descriptor_move(sd
, &ri
->cache_info
);
933 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
935 extrainfo_free_void(void *e
)
940 /** Free all storage held by a routerlist <b>rl</b>. */
942 routerlist_free_(routerlist_t
*rl
)
946 rimap_free(rl
->identity_map
, NULL
);
947 sdmap_free(rl
->desc_digest_map
, NULL
);
948 sdmap_free(rl
->desc_by_eid_map
, NULL
);
949 eimap_free(rl
->extra_info_map
, extrainfo_free_void
);
950 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, r
,
952 SMARTLIST_FOREACH(rl
->old_routers
, signed_descriptor_t
*, sd
,
953 signed_descriptor_free(sd
));
954 smartlist_free(rl
->routers
);
955 smartlist_free(rl
->old_routers
);
956 if (rl
->desc_store
.mmap
) {
957 int res
= tor_munmap_file(routerlist
->desc_store
.mmap
);
959 log_warn(LD_FS
, "Failed to munmap routerlist->desc_store.mmap");
962 if (rl
->extrainfo_store
.mmap
) {
963 int res
= tor_munmap_file(routerlist
->extrainfo_store
.mmap
);
965 log_warn(LD_FS
, "Failed to munmap routerlist->extrainfo_store.mmap");
970 router_dir_info_changed();
973 /** Log information about how much memory is being used for routerlist,
974 * at log level <b>severity</b>. */
976 dump_routerlist_mem_usage(int severity
)
978 uint64_t livedescs
= 0;
979 uint64_t olddescs
= 0;
982 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, r
,
983 livedescs
+= r
->cache_info
.signed_descriptor_len
);
984 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
985 olddescs
+= sd
->signed_descriptor_len
);
987 tor_log(severity
, LD_DIR
,
988 "In %d live descriptors: %"PRIu64
" bytes. "
989 "In %d old descriptors: %"PRIu64
" bytes.",
990 smartlist_len(routerlist
->routers
), (livedescs
),
991 smartlist_len(routerlist
->old_routers
), (olddescs
));
994 /** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
995 * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
996 * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
997 * is not in <b>sl</b>. */
999 routerlist_find_elt_(smartlist_t
*sl
, void *ri
, int idx
)
1003 SMARTLIST_FOREACH(sl
, routerinfo_t
*, r
,
1009 tor_assert(idx
< smartlist_len(sl
));
1010 tor_assert(smartlist_get(sl
, idx
) == ri
);
1015 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
1016 * as needed. There must be no previous member of <b>rl</b> with the same
1017 * identity digest as <b>ri</b>: If there is, call routerlist_replace
1021 routerlist_insert(routerlist_t
*rl
, routerinfo_t
*ri
)
1023 routerinfo_t
*ri_old
;
1024 signed_descriptor_t
*sd_old
;
1026 const routerinfo_t
*ri_generated
= router_get_my_routerinfo();
1027 tor_assert(ri_generated
!= ri
);
1029 tor_assert(ri
->cache_info
.routerlist_index
== -1);
1031 ri_old
= rimap_set(rl
->identity_map
, ri
->cache_info
.identity_digest
, ri
);
1032 tor_assert(!ri_old
);
1034 sd_old
= sdmap_set(rl
->desc_digest_map
,
1035 ri
->cache_info
.signed_descriptor_digest
,
1038 int idx
= sd_old
->routerlist_index
;
1039 sd_old
->routerlist_index
= -1;
1040 smartlist_del(rl
->old_routers
, idx
);
1041 if (idx
< smartlist_len(rl
->old_routers
)) {
1042 signed_descriptor_t
*d
= smartlist_get(rl
->old_routers
, idx
);
1043 d
->routerlist_index
= idx
;
1045 rl
->desc_store
.bytes_dropped
+= sd_old
->signed_descriptor_len
;
1046 sdmap_remove(rl
->desc_by_eid_map
, sd_old
->extra_info_digest
);
1047 signed_descriptor_free(sd_old
);
1050 if (!tor_digest_is_zero(ri
->cache_info
.extra_info_digest
))
1051 sdmap_set(rl
->desc_by_eid_map
, ri
->cache_info
.extra_info_digest
,
1053 smartlist_add(rl
->routers
, ri
);
1054 ri
->cache_info
.routerlist_index
= smartlist_len(rl
->routers
) - 1;
1055 nodelist_set_routerinfo(ri
, NULL
);
1056 router_dir_info_changed();
1057 #ifdef DEBUG_ROUTERLIST
1058 routerlist_assert_ok(rl
);
1062 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
1063 * corresponding router in rl-\>routers or rl-\>old_routers. Return the status
1064 * of inserting <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
1065 MOCK_IMPL(STATIC was_router_added_t
,
1066 extrainfo_insert
,(routerlist_t
*rl
, extrainfo_t
*ei
, int warn_if_incompatible
))
1068 was_router_added_t r
;
1069 const char *compatibility_error_msg
;
1070 routerinfo_t
*ri
= rimap_get(rl
->identity_map
,
1071 ei
->cache_info
.identity_digest
);
1072 signed_descriptor_t
*sd
=
1073 sdmap_get(rl
->desc_by_eid_map
, ei
->cache_info
.signed_descriptor_digest
);
1074 extrainfo_t
*ei_tmp
;
1075 const int severity
= warn_if_incompatible
? LOG_WARN
: LOG_INFO
;
1078 extrainfo_t
*ei_generated
= router_get_my_extrainfo();
1079 tor_assert(ei_generated
!= ei
);
1083 /* This router is unknown; we can't even verify the signature. Give up.*/
1084 r
= ROUTER_NOT_IN_CONSENSUS
;
1088 /* The extrainfo router doesn't have a known routerdesc to attach it to.
1089 * This just won't work. */;
1090 static ratelim_t no_sd_ratelim
= RATELIM_INIT(1800);
1092 log_fn_ratelim(&no_sd_ratelim
, severity
, LD_BUG
,
1093 "No entry found in extrainfo map.");
1096 if (tor_memneq(ei
->cache_info
.signed_descriptor_digest
,
1097 sd
->extra_info_digest
, DIGEST_LEN
)) {
1098 static ratelim_t digest_mismatch_ratelim
= RATELIM_INIT(1800);
1099 /* The sd we got from the map doesn't match the digest we used to look
1100 * it up. This makes no sense. */
1102 log_fn_ratelim(&digest_mismatch_ratelim
, severity
, LD_BUG
,
1103 "Mismatch in digest in extrainfo map.");
1106 if (routerinfo_incompatible_with_extrainfo(ri
->identity_pkey
, ei
, sd
,
1107 &compatibility_error_msg
)) {
1108 char d1
[HEX_DIGEST_LEN
+1], d2
[HEX_DIGEST_LEN
+1];
1109 r
= (ri
->cache_info
.extrainfo_is_bogus
) ?
1110 ROUTER_BAD_EI
: ROUTER_NOT_IN_CONSENSUS
;
1112 base16_encode(d1
, sizeof(d1
), ri
->cache_info
.identity_digest
, DIGEST_LEN
);
1113 base16_encode(d2
, sizeof(d2
), ei
->cache_info
.identity_digest
, DIGEST_LEN
);
1115 log_fn(severity
,LD_DIR
,
1116 "router info incompatible with extra info (ri id: %s, ei id %s, "
1117 "reason: %s)", d1
, d2
, compatibility_error_msg
);
1122 /* Okay, if we make it here, we definitely have a router corresponding to
1123 * this extrainfo. */
1125 ei_tmp
= eimap_set(rl
->extra_info_map
,
1126 ei
->cache_info
.signed_descriptor_digest
,
1128 r
= ROUTER_ADDED_SUCCESSFULLY
;
1130 rl
->extrainfo_store
.bytes_dropped
+=
1131 ei_tmp
->cache_info
.signed_descriptor_len
;
1132 extrainfo_free(ei_tmp
);
1136 if (r
!= ROUTER_ADDED_SUCCESSFULLY
)
1139 #ifdef DEBUG_ROUTERLIST
1140 routerlist_assert_ok(rl
);
1145 #define should_cache_old_descriptors() \
1146 directory_caches_dir_info(get_options())
1148 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
1149 * a copy of router <b>ri</b> yet, add it to the list of old (not
1150 * recommended but still served) descriptors. Else free it. */
1152 routerlist_insert_old(routerlist_t
*rl
, routerinfo_t
*ri
)
1155 const routerinfo_t
*ri_generated
= router_get_my_routerinfo();
1156 tor_assert(ri_generated
!= ri
);
1158 tor_assert(ri
->cache_info
.routerlist_index
== -1);
1160 if (should_cache_old_descriptors() &&
1161 ri
->purpose
== ROUTER_PURPOSE_GENERAL
&&
1162 !sdmap_get(rl
->desc_digest_map
,
1163 ri
->cache_info
.signed_descriptor_digest
)) {
1164 signed_descriptor_t
*sd
= signed_descriptor_from_routerinfo(ri
);
1165 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
1166 smartlist_add(rl
->old_routers
, sd
);
1167 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
1168 if (!tor_digest_is_zero(sd
->extra_info_digest
))
1169 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
1171 routerinfo_free(ri
);
1173 #ifdef DEBUG_ROUTERLIST
1174 routerlist_assert_ok(rl
);
1178 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
1179 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl->routers,
1180 * idx) == ri, we don't need to do a linear search over the list to decide
1181 * which to remove. We fill the gap in rl->routers with a later element in
1182 * the list, if any exists. <b>ri</b> is freed.
1184 * If <b>make_old</b> is true, instead of deleting the router, we try adding
1185 * it to rl->old_routers. */
1187 routerlist_remove(routerlist_t
*rl
, routerinfo_t
*ri
, int make_old
, time_t now
)
1189 routerinfo_t
*ri_tmp
;
1190 extrainfo_t
*ei_tmp
;
1191 int idx
= ri
->cache_info
.routerlist_index
;
1192 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->routers
));
1193 tor_assert(smartlist_get(rl
->routers
, idx
) == ri
);
1195 nodelist_remove_routerinfo(ri
);
1197 /* make sure the rephist module knows that it's not running */
1198 rep_hist_note_router_unreachable(ri
->cache_info
.identity_digest
, now
);
1200 ri
->cache_info
.routerlist_index
= -1;
1201 smartlist_del(rl
->routers
, idx
);
1202 if (idx
< smartlist_len(rl
->routers
)) {
1203 routerinfo_t
*r
= smartlist_get(rl
->routers
, idx
);
1204 r
->cache_info
.routerlist_index
= idx
;
1207 ri_tmp
= rimap_remove(rl
->identity_map
, ri
->cache_info
.identity_digest
);
1208 router_dir_info_changed();
1209 tor_assert(ri_tmp
== ri
);
1211 if (make_old
&& should_cache_old_descriptors() &&
1212 ri
->purpose
== ROUTER_PURPOSE_GENERAL
) {
1213 signed_descriptor_t
*sd
;
1214 sd
= signed_descriptor_from_routerinfo(ri
);
1215 smartlist_add(rl
->old_routers
, sd
);
1216 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
1217 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
1218 if (!tor_digest_is_zero(sd
->extra_info_digest
))
1219 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
1221 signed_descriptor_t
*sd_tmp
;
1222 sd_tmp
= sdmap_remove(rl
->desc_digest_map
,
1223 ri
->cache_info
.signed_descriptor_digest
);
1224 tor_assert(sd_tmp
== &(ri
->cache_info
));
1225 rl
->desc_store
.bytes_dropped
+= ri
->cache_info
.signed_descriptor_len
;
1226 ei_tmp
= eimap_remove(rl
->extra_info_map
,
1227 ri
->cache_info
.extra_info_digest
);
1229 rl
->extrainfo_store
.bytes_dropped
+=
1230 ei_tmp
->cache_info
.signed_descriptor_len
;
1231 extrainfo_free(ei_tmp
);
1233 if (!tor_digest_is_zero(ri
->cache_info
.extra_info_digest
))
1234 sdmap_remove(rl
->desc_by_eid_map
, ri
->cache_info
.extra_info_digest
);
1235 routerinfo_free(ri
);
1237 #ifdef DEBUG_ROUTERLIST
1238 routerlist_assert_ok(rl
);
1242 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
1243 * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
1246 routerlist_remove_old(routerlist_t
*rl
, signed_descriptor_t
*sd
, int idx
)
1248 signed_descriptor_t
*sd_tmp
;
1249 extrainfo_t
*ei_tmp
;
1250 desc_store_t
*store
;
1252 idx
= sd
->routerlist_index
;
1254 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->old_routers
));
1255 /* XXXX edmanm's bridge relay triggered the following assert while
1256 * running 0.2.0.12-alpha. If anybody triggers this again, see if we
1257 * can get a backtrace. */
1258 tor_assert(smartlist_get(rl
->old_routers
, idx
) == sd
);
1259 tor_assert(idx
== sd
->routerlist_index
);
1261 sd
->routerlist_index
= -1;
1262 smartlist_del(rl
->old_routers
, idx
);
1263 if (idx
< smartlist_len(rl
->old_routers
)) {
1264 signed_descriptor_t
*d
= smartlist_get(rl
->old_routers
, idx
);
1265 d
->routerlist_index
= idx
;
1267 sd_tmp
= sdmap_remove(rl
->desc_digest_map
,
1268 sd
->signed_descriptor_digest
);
1269 tor_assert(sd_tmp
== sd
);
1270 store
= desc_get_store(rl
, sd
);
1272 store
->bytes_dropped
+= sd
->signed_descriptor_len
;
1274 ei_tmp
= eimap_remove(rl
->extra_info_map
,
1275 sd
->extra_info_digest
);
1277 rl
->extrainfo_store
.bytes_dropped
+=
1278 ei_tmp
->cache_info
.signed_descriptor_len
;
1279 extrainfo_free(ei_tmp
);
1281 if (!tor_digest_is_zero(sd
->extra_info_digest
))
1282 sdmap_remove(rl
->desc_by_eid_map
, sd
->extra_info_digest
);
1284 signed_descriptor_free(sd
);
1285 #ifdef DEBUG_ROUTERLIST
1286 routerlist_assert_ok(rl
);
1290 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
1291 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
1292 * smartlist_get(rl->routers, idx) == ri, we don't need to do a linear
1293 * search over the list to decide which to remove. We put ri_new in the same
1294 * index as ri_old, if possible. ri is freed as appropriate.
1296 * If should_cache_descriptors() is true, instead of deleting the router,
1297 * we add it to rl->old_routers. */
1299 routerlist_replace(routerlist_t
*rl
, routerinfo_t
*ri_old
,
1300 routerinfo_t
*ri_new
)
1303 int same_descriptors
;
1305 routerinfo_t
*ri_tmp
;
1306 extrainfo_t
*ei_tmp
;
1308 const routerinfo_t
*ri_generated
= router_get_my_routerinfo();
1309 tor_assert(ri_generated
!= ri_new
);
1311 tor_assert(ri_old
!= ri_new
);
1312 tor_assert(ri_new
->cache_info
.routerlist_index
== -1);
1314 idx
= ri_old
->cache_info
.routerlist_index
;
1315 tor_assert(0 <= idx
&& idx
< smartlist_len(rl
->routers
));
1316 tor_assert(smartlist_get(rl
->routers
, idx
) == ri_old
);
1319 routerinfo_t
*ri_old_tmp
=NULL
;
1320 nodelist_set_routerinfo(ri_new
, &ri_old_tmp
);
1321 tor_assert(ri_old
== ri_old_tmp
);
1324 router_dir_info_changed();
1326 smartlist_set(rl
->routers
, idx
, ri_new
);
1327 ri_old
->cache_info
.routerlist_index
= -1;
1328 ri_new
->cache_info
.routerlist_index
= idx
;
1329 /* Check that ri_old is not in rl->routers anymore: */
1330 tor_assert( routerlist_find_elt_(rl
->routers
, ri_old
, -1) == -1 );
1332 log_warn(LD_BUG
, "Appending entry from routerlist_replace.");
1333 routerlist_insert(rl
, ri_new
);
1336 if (tor_memneq(ri_old
->cache_info
.identity_digest
,
1337 ri_new
->cache_info
.identity_digest
, DIGEST_LEN
)) {
1338 /* digests don't match; digestmap_set won't replace */
1339 rimap_remove(rl
->identity_map
, ri_old
->cache_info
.identity_digest
);
1341 ri_tmp
= rimap_set(rl
->identity_map
,
1342 ri_new
->cache_info
.identity_digest
, ri_new
);
1343 tor_assert(!ri_tmp
|| ri_tmp
== ri_old
);
1344 sdmap_set(rl
->desc_digest_map
,
1345 ri_new
->cache_info
.signed_descriptor_digest
,
1346 &(ri_new
->cache_info
));
1348 if (!tor_digest_is_zero(ri_new
->cache_info
.extra_info_digest
)) {
1349 sdmap_set(rl
->desc_by_eid_map
, ri_new
->cache_info
.extra_info_digest
,
1350 &ri_new
->cache_info
);
1353 same_descriptors
= tor_memeq(ri_old
->cache_info
.signed_descriptor_digest
,
1354 ri_new
->cache_info
.signed_descriptor_digest
,
1357 if (should_cache_old_descriptors() &&
1358 ri_old
->purpose
== ROUTER_PURPOSE_GENERAL
&&
1359 !same_descriptors
) {
1360 /* ri_old is going to become a signed_descriptor_t and go into
1362 signed_descriptor_t
*sd
= signed_descriptor_from_routerinfo(ri_old
);
1363 smartlist_add(rl
->old_routers
, sd
);
1364 sd
->routerlist_index
= smartlist_len(rl
->old_routers
)-1;
1365 sdmap_set(rl
->desc_digest_map
, sd
->signed_descriptor_digest
, sd
);
1366 if (!tor_digest_is_zero(sd
->extra_info_digest
))
1367 sdmap_set(rl
->desc_by_eid_map
, sd
->extra_info_digest
, sd
);
1369 /* We're dropping ri_old. */
1370 if (!same_descriptors
) {
1371 /* digests don't match; The sdmap_set above didn't replace */
1372 sdmap_remove(rl
->desc_digest_map
,
1373 ri_old
->cache_info
.signed_descriptor_digest
);
1375 if (tor_memneq(ri_old
->cache_info
.extra_info_digest
,
1376 ri_new
->cache_info
.extra_info_digest
, DIGEST_LEN
)) {
1377 ei_tmp
= eimap_remove(rl
->extra_info_map
,
1378 ri_old
->cache_info
.extra_info_digest
);
1380 rl
->extrainfo_store
.bytes_dropped
+=
1381 ei_tmp
->cache_info
.signed_descriptor_len
;
1382 extrainfo_free(ei_tmp
);
1386 if (!tor_digest_is_zero(ri_old
->cache_info
.extra_info_digest
)) {
1387 sdmap_remove(rl
->desc_by_eid_map
,
1388 ri_old
->cache_info
.extra_info_digest
);
1391 rl
->desc_store
.bytes_dropped
+= ri_old
->cache_info
.signed_descriptor_len
;
1392 routerinfo_free(ri_old
);
1394 #ifdef DEBUG_ROUTERLIST
1395 routerlist_assert_ok(rl
);
1399 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
1400 * it as a fresh routerinfo_t. */
1401 static routerinfo_t
*
1402 routerlist_reparse_old(routerlist_t
*rl
, signed_descriptor_t
*sd
)
1407 body
= signed_descriptor_get_annotations(sd
);
1409 ri
= router_parse_entry_from_string(body
,
1410 body
+sd
->signed_descriptor_len
+sd
->annotations_len
,
1414 signed_descriptor_move(&ri
->cache_info
, sd
);
1416 routerlist_remove_old(rl
, sd
, -1);
1421 /** Free all memory held by the routerlist module.
1422 * Note: Calling routerlist_free_all() should always be paired with
1423 * a call to nodelist_free_all(). These should only be called during
1427 routerlist_free_all(void)
1429 routerlist_free(routerlist
);
1432 if (warned_nicknames
) {
1433 SMARTLIST_FOREACH(warned_nicknames
, char *, cp
, tor_free(cp
));
1434 smartlist_free(warned_nicknames
);
1435 warned_nicknames
= NULL
;
1437 authcert_free_all();
1440 /** Forget that we have issued any router-related warnings, so that we'll
1441 * warn again if we see the same errors. */
1443 routerlist_reset_warnings(void)
1445 if (!warned_nicknames
)
1446 warned_nicknames
= smartlist_new();
1447 SMARTLIST_FOREACH(warned_nicknames
, char *, cp
, tor_free(cp
));
1448 smartlist_clear(warned_nicknames
); /* now the list is empty. */
1450 networkstatus_reset_warnings();
1453 /** Return 1 if the signed descriptor of this router is older than
1454 * <b>seconds</b> seconds. Otherwise return 0. */
1456 router_descriptor_is_older_than
,(const routerinfo_t
*router
, int seconds
))
1458 return router
->cache_info
.published_on
< approx_time() - seconds
;
1461 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
1462 * older entries (if any) with the same key. Note: Callers should not hold
1463 * their pointers to <b>router</b> if this function fails; <b>router</b>
1464 * will either be inserted into the routerlist or freed. Similarly, even
1465 * if this call succeeds, they should not hold their pointers to
1466 * <b>router</b> after subsequent calls with other routerinfo's -- they
1467 * might cause the original routerinfo to get freed.
1469 * Returns the status for the operation. Might set *<b>msg</b> if it wants
1470 * the poster of the router to know something.
1472 * If <b>from_cache</b>, this descriptor came from our disk cache. If
1473 * <b>from_fetch</b>, we received it in response to a request we made.
1474 * (If both are false, that means it was uploaded to us as an auth dir
1475 * server or via the controller.)
1477 * This function should be called *after*
1478 * routers_update_status_from_consensus_networkstatus; subsequently, you
1479 * should call router_rebuild_store and routerlist_descriptors_added.
1482 router_add_to_routerlist(routerinfo_t
*router
, const char **msg
,
1483 int from_cache
, int from_fetch
)
1485 const char *id_digest
;
1486 const or_options_t
*options
= get_options();
1487 int authdir
= authdir_mode_handles_descs(options
, router
->purpose
);
1488 int authdir_believes_valid
= 0;
1489 routerinfo_t
*old_router
;
1490 networkstatus_t
*consensus
=
1491 networkstatus_get_latest_consensus_by_flavor(FLAV_NS
);
1492 int in_consensus
= 0;
1497 router_get_routerlist();
1499 id_digest
= router
->cache_info
.identity_digest
;
1501 old_router
= router_get_mutable_by_digest(id_digest
);
1503 /* Make sure that it isn't expired. */
1504 if (router
->cert_expiration_time
< approx_time()) {
1505 routerinfo_free(router
);
1506 *msg
= "Some certs on this router are expired.";
1507 return ROUTER_CERTS_EXPIRED
;
1510 /* Make sure that we haven't already got this exact descriptor. */
1511 if (sdmap_get(routerlist
->desc_digest_map
,
1512 router
->cache_info
.signed_descriptor_digest
)) {
1513 /* If we have this descriptor already and the new descriptor is a bridge
1514 * descriptor, replace it. If we had a bridge descriptor before and the
1515 * new one is not a bridge descriptor, don't replace it. */
1517 /* Only members of routerlist->identity_map can be bridges; we don't
1518 * put bridges in old_routers. */
1519 const int was_bridge
= old_router
&&
1520 old_router
->purpose
== ROUTER_PURPOSE_BRIDGE
;
1522 if (routerinfo_is_a_configured_bridge(router
) &&
1523 router
->purpose
== ROUTER_PURPOSE_BRIDGE
&&
1525 log_info(LD_DIR
, "Replacing non-bridge descriptor with bridge "
1526 "descriptor for router %s",
1527 router_describe(router
));
1530 "Dropping descriptor that we already have for router %s",
1531 router_describe(router
));
1532 *msg
= "Router descriptor was not new.";
1533 routerinfo_free(router
);
1534 return ROUTER_IS_ALREADY_KNOWN
;
1539 if (authdir_wants_to_reject_router(router
, msg
,
1540 !from_cache
&& !from_fetch
,
1541 &authdir_believes_valid
)) {
1543 routerinfo_free(router
);
1544 return ROUTER_AUTHDIR_REJECTS
;
1546 } else if (from_fetch
) {
1547 /* Only check the descriptor digest against the network statuses when
1548 * we are receiving in response to a fetch. */
1550 if (!signed_desc_digest_is_recognized(&router
->cache_info
) &&
1551 !routerinfo_is_a_configured_bridge(router
)) {
1552 /* We asked for it, so some networkstatus must have listed it when we
1553 * did. Save it if we're a cache in case somebody else asks for it. */
1555 "Received a no-longer-recognized descriptor for router %s",
1556 router_describe(router
));
1557 *msg
= "Router descriptor is not referenced by any network-status.";
1559 /* Only journal this desc if we want to keep old descriptors */
1560 if (!from_cache
&& should_cache_old_descriptors())
1561 signed_desc_append_to_journal(&router
->cache_info
,
1562 &routerlist
->desc_store
);
1563 routerlist_insert_old(routerlist
, router
);
1564 return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS
;
1568 /* We no longer need a router with this descriptor digest. */
1570 routerstatus_t
*rs
= networkstatus_vote_find_mutable_entry(
1571 consensus
, id_digest
);
1572 if (rs
&& tor_memeq(rs
->descriptor_digest
,
1573 router
->cache_info
.signed_descriptor_digest
,
1579 if (router
->purpose
== ROUTER_PURPOSE_GENERAL
&&
1580 consensus
&& !in_consensus
&& !authdir
) {
1581 /* If it's a general router not listed in the consensus, then don't
1582 * consider replacing the latest router with it. */
1583 if (!from_cache
&& should_cache_old_descriptors())
1584 signed_desc_append_to_journal(&router
->cache_info
,
1585 &routerlist
->desc_store
);
1586 routerlist_insert_old(routerlist
, router
);
1587 *msg
= "Skipping router descriptor: not in consensus.";
1588 return ROUTER_NOT_IN_CONSENSUS
;
1591 /* If we're reading a bridge descriptor from our cache, and we don't
1592 * recognize it as one of our currently configured bridges, drop the
1593 * descriptor. Otherwise we could end up using it as one of our entry
1594 * guards even if it isn't in our Bridge config lines. */
1595 if (router
->purpose
== ROUTER_PURPOSE_BRIDGE
&& from_cache
&&
1596 !authdir_mode_bridge(options
) &&
1597 !routerinfo_is_a_configured_bridge(router
)) {
1598 log_info(LD_DIR
, "Dropping bridge descriptor for %s because we have "
1599 "no bridge configured at that address.",
1600 safe_str_client(router_describe(router
)));
1601 *msg
= "Router descriptor was not a configured bridge.";
1602 routerinfo_free(router
);
1603 return ROUTER_WAS_NOT_WANTED
;
1606 /* If we have a router with the same identity key, choose the newer one. */
1608 if (!in_consensus
&& (router
->cache_info
.published_on
<=
1609 old_router
->cache_info
.published_on
)) {
1610 /* Same key, but old. This one is not listed in the consensus. */
1611 log_debug(LD_DIR
, "Not-new descriptor for router %s",
1612 router_describe(router
));
1613 /* Only journal this desc if we'll be serving it. */
1614 if (!from_cache
&& should_cache_old_descriptors())
1615 signed_desc_append_to_journal(&router
->cache_info
,
1616 &routerlist
->desc_store
);
1617 routerlist_insert_old(routerlist
, router
);
1618 *msg
= "Router descriptor was not new.";
1619 return ROUTER_IS_ALREADY_KNOWN
;
1621 /* Same key, and either new, or listed in the consensus. */
1622 log_debug(LD_DIR
, "Replacing entry for router %s",
1623 router_describe(router
));
1624 routerlist_replace(routerlist
, old_router
, router
);
1626 signed_desc_append_to_journal(&router
->cache_info
,
1627 &routerlist
->desc_store
);
1629 *msg
= authdir_believes_valid
? "Valid server updated" :
1630 ("Invalid server updated. (This dirserver is marking your "
1631 "server as unapproved.)");
1632 return ROUTER_ADDED_SUCCESSFULLY
;
1636 if (!in_consensus
&& from_cache
&&
1637 router_descriptor_is_older_than(router
, OLD_ROUTER_DESC_MAX_AGE
)) {
1638 *msg
= "Router descriptor was really old.";
1639 routerinfo_free(router
);
1640 return ROUTER_WAS_TOO_OLD
;
1643 /* We haven't seen a router with this identity before. Add it to the end of
1645 routerlist_insert(routerlist
, router
);
1647 signed_desc_append_to_journal(&router
->cache_info
,
1648 &routerlist
->desc_store
);
1650 return ROUTER_ADDED_SUCCESSFULLY
;
1653 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
1654 * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
1655 * we actually inserted it, ROUTER_BAD_EI otherwise.
1658 router_add_extrainfo_to_routerlist(extrainfo_t
*ei
, const char **msg
,
1659 int from_cache
, int from_fetch
)
1661 was_router_added_t inserted
;
1663 if (msg
) *msg
= NULL
;
1664 /*XXXX Do something with msg */
1666 inserted
= extrainfo_insert(router_get_routerlist(), ei
, !from_cache
);
1668 if (WRA_WAS_ADDED(inserted
) && !from_cache
)
1669 signed_desc_append_to_journal(&ei
->cache_info
,
1670 &routerlist
->extrainfo_store
);
1675 /** Sorting helper: return <0, 0, or >0 depending on whether the
1676 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
1677 * to, or later than that of *<b>b</b>. */
1679 compare_old_routers_by_identity_(const void **_a
, const void **_b
)
1682 const signed_descriptor_t
*r1
= *_a
, *r2
= *_b
;
1683 if ((i
= fast_memcmp(r1
->identity_digest
, r2
->identity_digest
, DIGEST_LEN
)))
1685 return (int)(r1
->published_on
- r2
->published_on
);
1688 /** Internal type used to represent how long an old descriptor was valid,
1689 * where it appeared in the list of old descriptors, and whether it's extra
1690 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
1691 struct duration_idx_t
{
1697 /** Sorting helper: compare two duration_idx_t by their duration. */
1699 compare_duration_idx_(const void *_d1
, const void *_d2
)
1701 const struct duration_idx_t
*d1
= _d1
;
1702 const struct duration_idx_t
*d2
= _d2
;
1703 return d1
->duration
- d2
->duration
;
1706 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
1707 * must contain routerinfo_t with the same identity and with publication time
1708 * in ascending order. Remove members from this range until there are no more
1709 * than max_descriptors_per_router() remaining. Start by removing the oldest
1710 * members from before <b>cutoff</b>, then remove members which were current
1711 * for the lowest amount of time. The order of members of old_routers at
1712 * indices <b>lo</b> or higher may be changed.
1715 routerlist_remove_old_cached_routers_with_id(time_t now
,
1716 time_t cutoff
, int lo
, int hi
,
1717 digestset_t
*retain
)
1720 unsigned n_extra
, n_rmv
= 0;
1721 struct duration_idx_t
*lifespans
;
1722 uint8_t *rmv
, *must_keep
;
1723 smartlist_t
*lst
= routerlist
->old_routers
;
1726 tor_assert(hi
< smartlist_len(lst
));
1727 tor_assert(lo
<= hi
);
1728 ident
= ((signed_descriptor_t
*)smartlist_get(lst
, lo
))->identity_digest
;
1729 for (i
= lo
+1; i
<= hi
; ++i
) {
1730 signed_descriptor_t
*r
= smartlist_get(lst
, i
);
1731 tor_assert(tor_memeq(ident
, r
->identity_digest
, DIGEST_LEN
));
1734 /* Check whether we need to do anything at all. */
1736 int mdpr
= directory_caches_dir_info(get_options()) ? 2 : 1;
1742 lifespans
= tor_calloc(n
, sizeof(struct duration_idx_t
));
1743 rmv
= tor_calloc(n
, sizeof(uint8_t));
1744 must_keep
= tor_calloc(n
, sizeof(uint8_t));
1745 /* Set lifespans to contain the lifespan and index of each server. */
1746 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
1747 for (i
= lo
; i
<= hi
; ++i
) {
1748 signed_descriptor_t
*r
= smartlist_get(lst
, i
);
1749 signed_descriptor_t
*r_next
;
1750 lifespans
[i
-lo
].idx
= i
;
1751 if (r
->last_listed_as_valid_until
>= now
||
1752 (retain
&& digestset_probably_contains(retain
,
1753 r
->signed_descriptor_digest
))) {
1754 must_keep
[i
-lo
] = 1;
1757 r_next
= smartlist_get(lst
, i
+1);
1758 tor_assert(r
->published_on
<= r_next
->published_on
);
1759 lifespans
[i
-lo
].duration
= (int)(r_next
->published_on
- r
->published_on
);
1762 lifespans
[i
-lo
].duration
= INT_MAX
;
1764 if (!must_keep
[i
-lo
] && r
->published_on
< cutoff
&& n_rmv
< n_extra
) {
1766 lifespans
[i
-lo
].old
= 1;
1771 if (n_rmv
< n_extra
) {
1773 * We aren't removing enough servers for being old. Sort lifespans by
1774 * the duration of liveness, and remove the ones we're not already going to
1775 * remove based on how long they were alive.
1777 qsort(lifespans
, n
, sizeof(struct duration_idx_t
), compare_duration_idx_
);
1778 for (i
= 0; i
< n
&& n_rmv
< n_extra
; ++i
) {
1779 if (!must_keep
[lifespans
[i
].idx
-lo
] && !lifespans
[i
].old
) {
1780 rmv
[lifespans
[i
].idx
-lo
] = 1;
1789 routerlist_remove_old(routerlist
, smartlist_get(lst
, i
), i
);
1790 } while (--i
>= lo
);
1791 tor_free(must_keep
);
1793 tor_free(lifespans
);
1796 /** Deactivate any routers from the routerlist that are more than
1797 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
1798 * remove old routers from the list of cached routers if we have too many.
1801 routerlist_remove_old_routers(void)
1804 const char *cur_id
= NULL
;
1805 time_t now
= time(NULL
);
1807 routerinfo_t
*router
;
1808 signed_descriptor_t
*sd
;
1809 digestset_t
*retain
;
1810 const networkstatus_t
*consensus
= networkstatus_get_latest_consensus();
1812 trusted_dirs_remove_old_certs();
1814 if (!routerlist
|| !consensus
)
1817 // routerlist_assert_ok(routerlist);
1819 /* We need to guess how many router descriptors we will wind up wanting to
1820 retain, so that we can be sure to allocate a large enough Bloom filter
1821 to hold the digest set. Overestimating is fine; underestimating is bad.
1824 /* We'll probably retain everything in the consensus. */
1825 int n_max_retain
= smartlist_len(consensus
->routerstatus_list
);
1826 retain
= digestset_new(n_max_retain
);
1829 cutoff
= now
- OLD_ROUTER_DESC_MAX_AGE
;
1830 /* Retain anything listed in the consensus. */
1832 SMARTLIST_FOREACH(consensus
->routerstatus_list
, routerstatus_t
*, rs
,
1833 if (rs
->published_on
>= cutoff
)
1834 digestset_add(retain
, rs
->descriptor_digest
));
1837 /* If we have a consensus, we should consider pruning current routers that
1838 * are too old and that nobody recommends. (If we don't have a consensus,
1839 * then we should get one before we decide to kill routers.) */
1842 cutoff
= now
- ROUTER_MAX_AGE
;
1843 /* Remove too-old unrecommended members of routerlist->routers. */
1844 for (i
= 0; i
< smartlist_len(routerlist
->routers
); ++i
) {
1845 router
= smartlist_get(routerlist
->routers
, i
);
1846 if (router
->cache_info
.published_on
<= cutoff
&&
1847 router
->cache_info
.last_listed_as_valid_until
< now
&&
1848 !digestset_probably_contains(retain
,
1849 router
->cache_info
.signed_descriptor_digest
)) {
1850 /* Too old: remove it. (If we're a cache, just move it into
1853 "Forgetting obsolete (too old) routerinfo for router %s",
1854 router_describe(router
));
1855 routerlist_remove(routerlist
, router
, 1, now
);
1861 //routerlist_assert_ok(routerlist);
1863 /* Remove far-too-old members of routerlist->old_routers. */
1864 cutoff
= now
- OLD_ROUTER_DESC_MAX_AGE
;
1865 for (i
= 0; i
< smartlist_len(routerlist
->old_routers
); ++i
) {
1866 sd
= smartlist_get(routerlist
->old_routers
, i
);
1867 if (sd
->published_on
<= cutoff
&&
1868 sd
->last_listed_as_valid_until
< now
&&
1869 !digestset_probably_contains(retain
, sd
->signed_descriptor_digest
)) {
1870 /* Too old. Remove it. */
1871 routerlist_remove_old(routerlist
, sd
, i
--);
1875 //routerlist_assert_ok(routerlist);
1877 log_info(LD_DIR
, "We have %d live routers and %d old router descriptors.",
1878 smartlist_len(routerlist
->routers
),
1879 smartlist_len(routerlist
->old_routers
));
1881 /* Now we might have to look at routerlist->old_routers for extraneous
1882 * members. (We'd keep all the members if we could, but we need to save
1883 * space.) First, check whether we have too many router descriptors, total.
1884 * We're okay with having too many for some given router, so long as the
1885 * total number doesn't approach max_descriptors_per_router()*len(router).
1887 if (smartlist_len(routerlist
->old_routers
) <
1888 smartlist_len(routerlist
->routers
))
1891 /* Sort by identity, then fix indices. */
1892 smartlist_sort(routerlist
->old_routers
, compare_old_routers_by_identity_
);
1894 for (i
= 0; i
< smartlist_len(routerlist
->old_routers
); ++i
) {
1895 signed_descriptor_t
*r
= smartlist_get(routerlist
->old_routers
, i
);
1896 r
->routerlist_index
= i
;
1899 /* Iterate through the list from back to front, so when we remove descriptors
1900 * we don't mess up groups we haven't gotten to. */
1901 for (i
= smartlist_len(routerlist
->old_routers
)-1; i
>= 0; --i
) {
1902 signed_descriptor_t
*r
= smartlist_get(routerlist
->old_routers
, i
);
1904 cur_id
= r
->identity_digest
;
1907 if (tor_memneq(cur_id
, r
->identity_digest
, DIGEST_LEN
)) {
1908 routerlist_remove_old_cached_routers_with_id(now
,
1909 cutoff
, i
+1, hi
, retain
);
1910 cur_id
= r
->identity_digest
;
1915 routerlist_remove_old_cached_routers_with_id(now
, cutoff
, 0, hi
, retain
);
1916 //routerlist_assert_ok(routerlist);
1919 digestset_free(retain
);
1920 router_rebuild_store(RRS_DONT_REMOVE_OLD
, &routerlist
->desc_store
);
1921 router_rebuild_store(RRS_DONT_REMOVE_OLD
,&routerlist
->extrainfo_store
);
1924 /** We just added a new set of descriptors. Take whatever extra steps
1927 routerlist_descriptors_added(smartlist_t
*sl
, int from_cache
)
1930 control_event_descriptors_changed(sl
);
1931 SMARTLIST_FOREACH_BEGIN(sl
, routerinfo_t
*, ri
) {
1932 if (ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
1933 learned_bridge_descriptor(ri
, from_cache
);
1934 if (ri
->needs_retest_if_added
) {
1935 ri
->needs_retest_if_added
= 0;
1936 dirserv_single_reachability_test(approx_time(), ri
);
1938 } SMARTLIST_FOREACH_END(ri
);
1942 * Code to parse a single router descriptor and insert it into the
1943 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
1944 * descriptor was well-formed but could not be added; and 1 if the
1945 * descriptor was added.
1947 * If we don't add it and <b>msg</b> is not NULL, then assign to
1948 * *<b>msg</b> a static string describing the reason for refusing the
1951 * This is used only by the controller.
1954 router_load_single_router(const char *s
, uint8_t purpose
, int cache
,
1958 was_router_added_t r
;
1960 char annotation_buf
[ROUTER_ANNOTATION_BUF_LEN
];
1964 tor_snprintf(annotation_buf
, sizeof(annotation_buf
),
1965 "@source controller\n"
1966 "@purpose %s\n", router_purpose_to_string(purpose
));
1968 if (!(ri
= router_parse_entry_from_string(s
, NULL
, 1, 0,
1969 annotation_buf
, NULL
))) {
1970 log_warn(LD_DIR
, "Error parsing router descriptor; dropping.");
1971 *msg
= "Couldn't parse router descriptor.";
1974 tor_assert(ri
->purpose
== purpose
);
1975 if (router_is_me(ri
)) {
1976 log_warn(LD_DIR
, "Router's identity key matches mine; dropping.");
1977 *msg
= "Router's identity key matches mine.";
1978 routerinfo_free(ri
);
1982 if (!cache
) /* obey the preference of the controller */
1983 ri
->cache_info
.do_not_cache
= 1;
1985 lst
= smartlist_new();
1986 smartlist_add(lst
, ri
);
1987 routers_update_status_from_consensus_networkstatus(lst
, 0);
1989 r
= router_add_to_routerlist(ri
, msg
, 0, 0);
1990 if (!WRA_WAS_ADDED(r
)) {
1991 /* we've already assigned to *msg now, and ri is already freed */
1993 if (r
== ROUTER_AUTHDIR_REJECTS
)
1994 log_warn(LD_DIR
, "Couldn't add router to list: %s Dropping.", *msg
);
1995 smartlist_free(lst
);
1998 routerlist_descriptors_added(lst
, 0);
1999 smartlist_free(lst
);
2000 log_debug(LD_DIR
, "Added router to list");
2005 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
2006 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
2007 * are in response to a query to the network: cache them by adding them to
2010 * Return the number of routers actually added.
2012 * If <b>requested_fingerprints</b> is provided, it must contain a list of
2013 * uppercased fingerprints. Do not update any router whose
2014 * fingerprint is not on the list; after updating a router, remove its
2015 * fingerprint from the list.
2017 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
2018 * are descriptor digests. Otherwise they are identity digests.
2021 router_load_routers_from_string(const char *s
, const char *eos
,
2022 saved_location_t saved_location
,
2023 smartlist_t
*requested_fingerprints
,
2024 int descriptor_digests
,
2025 const char *prepend_annotations
)
2027 smartlist_t
*routers
= smartlist_new(), *changed
= smartlist_new();
2028 char fp
[HEX_DIGEST_LEN
+1];
2030 int from_cache
= (saved_location
!= SAVED_NOWHERE
);
2031 int allow_annotations
= (saved_location
!= SAVED_NOWHERE
);
2032 int any_changed
= 0;
2033 smartlist_t
*invalid_digests
= smartlist_new();
2035 router_parse_list_from_string(&s
, eos
, routers
, saved_location
, 0,
2036 allow_annotations
, prepend_annotations
,
2039 routers_update_status_from_consensus_networkstatus(routers
, !from_cache
);
2041 log_info(LD_DIR
, "%d elements to add", smartlist_len(routers
));
2043 SMARTLIST_FOREACH_BEGIN(routers
, routerinfo_t
*, ri
) {
2044 was_router_added_t r
;
2046 if (requested_fingerprints
) {
2047 base16_encode(fp
, sizeof(fp
), descriptor_digests
?
2048 ri
->cache_info
.signed_descriptor_digest
:
2049 ri
->cache_info
.identity_digest
,
2051 if (smartlist_contains_string(requested_fingerprints
, fp
)) {
2052 smartlist_string_remove(requested_fingerprints
, fp
);
2055 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
2057 "We received a router descriptor with a fingerprint (%s) "
2058 "that we never requested. (We asked for: %s.) Dropping.",
2060 tor_free(requested
);
2061 routerinfo_free(ri
);
2066 memcpy(d
, ri
->cache_info
.signed_descriptor_digest
, DIGEST_LEN
);
2067 r
= router_add_to_routerlist(ri
, &msg
, from_cache
, !from_cache
);
2068 if (WRA_WAS_ADDED(r
)) {
2070 smartlist_add(changed
, ri
);
2071 routerlist_descriptors_added(changed
, from_cache
);
2072 smartlist_clear(changed
);
2073 } else if (WRA_NEVER_DOWNLOADABLE(r
)) {
2074 download_status_t
*dl_status
;
2075 dl_status
= router_get_dl_status_by_descriptor_digest(d
);
2077 log_info(LD_GENERAL
, "Marking router %s as never downloadable",
2078 hex_str(d
, DIGEST_LEN
));
2079 download_status_mark_impossible(dl_status
);
2082 } SMARTLIST_FOREACH_END(ri
);
2084 SMARTLIST_FOREACH_BEGIN(invalid_digests
, const uint8_t *, bad_digest
) {
2085 /* This digest is never going to be parseable. */
2086 base16_encode(fp
, sizeof(fp
), (char*)bad_digest
, DIGEST_LEN
);
2087 if (requested_fingerprints
&& descriptor_digests
) {
2088 if (! smartlist_contains_string(requested_fingerprints
, fp
)) {
2089 /* But we didn't ask for it, so we should assume shennanegans. */
2092 smartlist_string_remove(requested_fingerprints
, fp
);
2094 download_status_t
*dls
;
2095 dls
= router_get_dl_status_by_descriptor_digest((char*)bad_digest
);
2097 log_info(LD_GENERAL
, "Marking router with descriptor %s as unparseable, "
2098 "and therefore undownloadable", fp
);
2099 download_status_mark_impossible(dls
);
2101 } SMARTLIST_FOREACH_END(bad_digest
);
2102 SMARTLIST_FOREACH(invalid_digests
, uint8_t *, d
, tor_free(d
));
2103 smartlist_free(invalid_digests
);
2105 routerlist_assert_ok(routerlist
);
2108 router_rebuild_store(0, &routerlist
->desc_store
);
2110 smartlist_free(routers
);
2111 smartlist_free(changed
);
2116 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
2117 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
2118 * router_load_routers_from_string(). */
2120 router_load_extrainfo_from_string(const char *s
, const char *eos
,
2121 saved_location_t saved_location
,
2122 smartlist_t
*requested_fingerprints
,
2123 int descriptor_digests
)
2125 smartlist_t
*extrainfo_list
= smartlist_new();
2127 int from_cache
= (saved_location
!= SAVED_NOWHERE
);
2128 smartlist_t
*invalid_digests
= smartlist_new();
2130 router_parse_list_from_string(&s
, eos
, extrainfo_list
, saved_location
, 1, 0,
2131 NULL
, invalid_digests
);
2133 log_info(LD_DIR
, "%d elements to add", smartlist_len(extrainfo_list
));
2135 SMARTLIST_FOREACH_BEGIN(extrainfo_list
, extrainfo_t
*, ei
) {
2136 uint8_t d
[DIGEST_LEN
];
2137 memcpy(d
, ei
->cache_info
.signed_descriptor_digest
, DIGEST_LEN
);
2138 was_router_added_t added
=
2139 router_add_extrainfo_to_routerlist(ei
, &msg
, from_cache
, !from_cache
);
2140 if (WRA_WAS_ADDED(added
) && requested_fingerprints
) {
2141 char fp
[HEX_DIGEST_LEN
+1];
2142 base16_encode(fp
, sizeof(fp
), descriptor_digests
?
2143 ei
->cache_info
.signed_descriptor_digest
:
2144 ei
->cache_info
.identity_digest
,
2146 smartlist_string_remove(requested_fingerprints
, fp
);
2147 /* We silently let relays stuff us with extrainfos we didn't ask for,
2148 * so long as we would have wanted them anyway. Since we always fetch
2149 * all the extrainfos we want, and we never actually act on them
2150 * inside Tor, this should be harmless. */
2151 } else if (WRA_NEVER_DOWNLOADABLE(added
)) {
2152 signed_descriptor_t
*sd
= router_get_by_extrainfo_digest((char*)d
);
2154 log_info(LD_GENERAL
, "Marking extrainfo with descriptor %s as "
2155 "unparseable, and therefore undownloadable",
2156 hex_str((char*)d
,DIGEST_LEN
));
2157 download_status_mark_impossible(&sd
->ei_dl_status
);
2160 } SMARTLIST_FOREACH_END(ei
);
2162 SMARTLIST_FOREACH_BEGIN(invalid_digests
, const uint8_t *, bad_digest
) {
2163 /* This digest is never going to be parseable. */
2164 char fp
[HEX_DIGEST_LEN
+1];
2165 base16_encode(fp
, sizeof(fp
), (char*)bad_digest
, DIGEST_LEN
);
2166 if (requested_fingerprints
) {
2167 if (! smartlist_contains_string(requested_fingerprints
, fp
)) {
2168 /* But we didn't ask for it, so we should assume shennanegans. */
2171 smartlist_string_remove(requested_fingerprints
, fp
);
2173 signed_descriptor_t
*sd
=
2174 router_get_by_extrainfo_digest((char*)bad_digest
);
2176 log_info(LD_GENERAL
, "Marking extrainfo with descriptor %s as "
2177 "unparseable, and therefore undownloadable", fp
);
2178 download_status_mark_impossible(&sd
->ei_dl_status
);
2180 } SMARTLIST_FOREACH_END(bad_digest
);
2181 SMARTLIST_FOREACH(invalid_digests
, uint8_t *, d
, tor_free(d
));
2182 smartlist_free(invalid_digests
);
2184 routerlist_assert_ok(routerlist
);
2185 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store
);
2187 smartlist_free(extrainfo_list
);
2190 /** Return true iff the latest ns-flavored consensus includes a descriptor
2191 * whose digest is that of <b>desc</b>. */
2193 signed_desc_digest_is_recognized(signed_descriptor_t
*desc
)
2195 const routerstatus_t
*rs
;
2196 networkstatus_t
*consensus
= networkstatus_get_latest_consensus_by_flavor(
2200 rs
= networkstatus_vote_find_entry(consensus
, desc
->identity_digest
);
2201 if (rs
&& tor_memeq(rs
->descriptor_digest
,
2202 desc
->signed_descriptor_digest
, DIGEST_LEN
))
2208 /** Update downloads for router descriptors and/or microdescriptors as
2211 update_all_descriptor_downloads(time_t now
)
2213 if (should_delay_dir_fetches(get_options(), NULL
))
2215 update_router_descriptor_downloads(now
);
2216 update_microdesc_downloads(now
);
2217 launch_dummy_descriptor_download_as_needed(now
, get_options());
2220 /** Clear all our timeouts for fetching v3 directory stuff, and then
2221 * give it all a try again. */
2223 routerlist_retry_directory_downloads(time_t now
)
2227 log_debug(LD_GENERAL
,
2228 "In routerlist_retry_directory_downloads()");
2230 router_reset_status_download_failures();
2231 router_reset_descriptor_download_failures();
2232 reschedule_directory_downloads();
2235 /** Return true iff <b>router</b> does not permit exit streams.
2238 router_exit_policy_rejects_all(const routerinfo_t
*router
)
2240 return router
->policy_is_reject_star
;
2243 /** For every current directory connection whose purpose is <b>purpose</b>,
2244 * and where the resource being downloaded begins with <b>prefix</b>, split
2245 * rest of the resource into base16 fingerprints (or base64 fingerprints if
2246 * purpose==DIR_PURPOSE_FETCH_MICRODESC), decode them, and set the
2247 * corresponding elements of <b>result</b> to a nonzero value.
2250 list_pending_downloads(digestmap_t
*result
, digest256map_t
*result256
,
2251 int purpose
, const char *prefix
)
2253 const size_t p_len
= strlen(prefix
);
2254 smartlist_t
*tmp
= smartlist_new();
2255 smartlist_t
*conns
= get_connection_array();
2256 int flags
= DSR_HEX
;
2257 if (purpose
== DIR_PURPOSE_FETCH_MICRODESC
)
2258 flags
= DSR_DIGEST256
|DSR_BASE64
;
2260 tor_assert(result
|| result256
);
2262 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
) {
2263 if (conn
->type
== CONN_TYPE_DIR
&&
2264 conn
->purpose
== purpose
&&
2265 !conn
->marked_for_close
) {
2266 const char *resource
= TO_DIR_CONN(conn
)->requested_resource
;
2267 if (!strcmpstart(resource
, prefix
))
2268 dir_split_resource_into_fingerprints(resource
+ p_len
,
2271 } SMARTLIST_FOREACH_END(conn
);
2274 SMARTLIST_FOREACH(tmp
, char *, d
,
2276 digestmap_set(result
, d
, (void*)1);
2279 } else if (result256
) {
2280 SMARTLIST_FOREACH(tmp
, uint8_t *, d
,
2282 digest256map_set(result256
, d
, (void*)1);
2286 smartlist_free(tmp
);
2289 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
2290 * true) we are currently downloading by descriptor digest, set result[d] to
2293 list_pending_descriptor_downloads(digestmap_t
*result
, int extrainfo
)
2296 extrainfo
? DIR_PURPOSE_FETCH_EXTRAINFO
: DIR_PURPOSE_FETCH_SERVERDESC
;
2297 list_pending_downloads(result
, NULL
, purpose
, "d/");
2300 /** For every microdescriptor we are currently downloading by descriptor
2301 * digest, set result[d] to (void*)1.
2304 list_pending_microdesc_downloads(digest256map_t
*result
)
2306 list_pending_downloads(NULL
, result
, DIR_PURPOSE_FETCH_MICRODESC
, "d/");
2309 /** Launch downloads for all the descriptors whose digests or digests256
2310 * are listed as digests[i] for lo <= i < hi. (Lo and hi may be out of
2311 * range.) If <b>source</b> is given, download from <b>source</b>;
2312 * otherwise, download from an appropriate random directory server.
2314 MOCK_IMPL(STATIC
void,
2315 initiate_descriptor_downloads
,(const routerstatus_t
*source
,
2316 int purpose
, smartlist_t
*digests
,
2317 int lo
, int hi
, int pds_flags
))
2319 char *resource
, *cp
;
2320 int digest_len
, enc_digest_len
;
2325 if (purpose
== DIR_PURPOSE_FETCH_MICRODESC
) {
2326 /* Microdescriptors are downloaded by "-"-separated base64-encoded
2327 * 256-bit digests. */
2328 digest_len
= DIGEST256_LEN
;
2329 enc_digest_len
= BASE64_DIGEST256_LEN
+ 1;
2333 digest_len
= DIGEST_LEN
;
2334 enc_digest_len
= HEX_DIGEST_LEN
+ 1;
2341 if (hi
> smartlist_len(digests
))
2342 hi
= smartlist_len(digests
);
2347 tmp
= smartlist_new();
2349 for (; lo
< hi
; ++lo
) {
2350 cp
= tor_malloc(enc_digest_len
);
2352 digest256_to_base64(cp
, smartlist_get(digests
, lo
));
2354 base16_encode(cp
, enc_digest_len
, smartlist_get(digests
, lo
),
2357 smartlist_add(tmp
, cp
);
2360 cp
= smartlist_join_strings(tmp
, sep
, 0, NULL
);
2361 tor_asprintf(&resource
, "d/%s.z", cp
);
2363 SMARTLIST_FOREACH(tmp
, char *, cp1
, tor_free(cp1
));
2364 smartlist_free(tmp
);
2368 /* We know which authority or directory mirror we want. */
2369 directory_request_t
*req
= directory_request_new(purpose
);
2370 directory_request_set_routerstatus(req
, source
);
2371 directory_request_set_resource(req
, resource
);
2372 directory_initiate_request(req
);
2373 directory_request_free(req
);
2375 directory_get_from_dirserver(purpose
, ROUTER_PURPOSE_GENERAL
, resource
,
2376 pds_flags
, DL_WANT_ANY_DIRSERVER
);
2381 /** Return the max number of hashes to put in a URL for a given request.
2384 max_dl_per_request(const or_options_t
*options
, int purpose
)
2386 /* Since squid does not like URLs >= 4096 bytes we limit it to 96.
2387 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2388 * /tor/server/d/.z) == 4026
2389 * 4026/41 (40 for the hash and 1 for the + that separates them) => 98
2390 * So use 96 because it's a nice number.
2392 * For microdescriptors, the calculation is
2393 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2394 * /tor/micro/d/.z) == 4027
2395 * 4027/44 (43 for the hash and 1 for the - that separates them) => 91
2396 * So use 90 because it's a nice number.
2399 if (purpose
== DIR_PURPOSE_FETCH_MICRODESC
) {
2402 /* If we're going to tunnel our connections, we can ask for a lot more
2404 if (directory_must_use_begindir(options
)) {
2410 /** Don't split our requests so finely that we are requesting fewer than
2411 * this number per server. (Grouping more than this at once leads to
2412 * diminishing returns.) */
2413 #define MIN_DL_PER_REQUEST 32
2414 /** To prevent a single screwy cache from confusing us by selective reply,
2415 * try to split our requests into at least this many requests. */
2416 #define MIN_REQUESTS 3
2417 /** If we want fewer than this many descriptors, wait until we
2418 * want more, or until TestingClientMaxIntervalWithoutRequest has passed. */
2419 #define MAX_DL_TO_DELAY 16
2421 /** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of
2422 * router descriptor digests or microdescriptor digest256s in
2423 * <b>downloadable</b>, decide whether to delay fetching until we have more.
2424 * If we don't want to delay, launch one or more requests to the appropriate
2425 * directory authorities.
2428 launch_descriptor_downloads(int purpose
,
2429 smartlist_t
*downloadable
,
2430 const routerstatus_t
*source
, time_t now
)
2432 const or_options_t
*options
= get_options();
2433 const char *descname
;
2434 const int fetch_microdesc
= (purpose
== DIR_PURPOSE_FETCH_MICRODESC
);
2435 int n_downloadable
= smartlist_len(downloadable
);
2437 int i
, n_per_request
, max_dl_per_req
;
2438 const char *req_plural
= "", *rtr_plural
= "";
2439 int pds_flags
= PDS_RETRY_IF_NO_SERVERS
;
2441 tor_assert(fetch_microdesc
|| purpose
== DIR_PURPOSE_FETCH_SERVERDESC
);
2442 descname
= fetch_microdesc
? "microdesc" : "routerdesc";
2444 if (!n_downloadable
)
2447 if (!directory_fetches_dir_info_early(options
)) {
2448 if (n_downloadable
>= MAX_DL_TO_DELAY
) {
2450 "There are enough downloadable %ss to launch requests.",
2452 } else if (! router_have_minimum_dir_info()) {
2454 "We are only missing %d %ss, but we'll fetch anyway, since "
2455 "we don't yet have enough directory info.",
2456 n_downloadable
, descname
);
2460 if ((last_descriptor_download_attempted
+
2461 options
->TestingClientMaxIntervalWithoutRequest
) > now
)
2464 if (last_descriptor_download_attempted
) {
2466 "There are not many downloadable %ss, but we've "
2467 "been waiting long enough (%d seconds). Downloading.",
2469 (int)(now
-last_descriptor_download_attempted
));
2472 "There are not many downloadable %ss, but we haven't "
2473 "tried downloading descriptors recently. Downloading.",
2479 if (!authdir_mode(options
)) {
2480 /* If we wind up going to the authorities, we want to only open one
2481 * connection to each authority at a time, so that we don't overload
2482 * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
2483 * regardless of whether we're a cache or not.
2485 * Setting this flag can make initiate_descriptor_downloads() ignore
2486 * requests. We need to make sure that we do in fact call
2487 * update_router_descriptor_downloads() later on, once the connections
2488 * have succeeded or failed.
2490 pds_flags
|= fetch_microdesc
?
2491 PDS_NO_EXISTING_MICRODESC_FETCH
:
2492 PDS_NO_EXISTING_SERVERDESC_FETCH
;
2495 n_per_request
= CEIL_DIV(n_downloadable
, MIN_REQUESTS
);
2496 max_dl_per_req
= max_dl_per_request(options
, purpose
);
2498 if (n_per_request
> max_dl_per_req
)
2499 n_per_request
= max_dl_per_req
;
2501 if (n_per_request
< MIN_DL_PER_REQUEST
) {
2502 n_per_request
= MIN(MIN_DL_PER_REQUEST
, n_downloadable
);
2505 if (n_downloadable
> n_per_request
)
2506 req_plural
= rtr_plural
= "s";
2507 else if (n_downloadable
> 1)
2511 "Launching %d request%s for %d %s%s, %d at a time",
2512 CEIL_DIV(n_downloadable
, n_per_request
), req_plural
,
2513 n_downloadable
, descname
, rtr_plural
, n_per_request
);
2514 smartlist_sort_digests(downloadable
);
2515 for (i
=0; i
< n_downloadable
; i
+= n_per_request
) {
2516 initiate_descriptor_downloads(source
, purpose
,
2517 downloadable
, i
, i
+n_per_request
,
2520 last_descriptor_download_attempted
= now
;
2523 /** For any descriptor that we want that's currently listed in
2524 * <b>consensus</b>, download it as appropriate. */
2526 update_consensus_router_descriptor_downloads(time_t now
, int is_vote
,
2527 networkstatus_t
*consensus
)
2529 const or_options_t
*options
= get_options();
2530 digestmap_t
*map
= NULL
;
2531 smartlist_t
*no_longer_old
= smartlist_new();
2532 smartlist_t
*downloadable
= smartlist_new();
2533 routerstatus_t
*source
= NULL
;
2534 int authdir
= authdir_mode(options
);
2535 int n_delayed
=0, n_have
=0, n_would_reject
=0, n_wouldnt_use
=0,
2536 n_inprogress
=0, n_in_oldrouters
=0;
2538 if (directory_too_idle_to_fetch_descriptors(options
, now
))
2544 /* where's it from, so we know whom to ask for descriptors */
2546 networkstatus_voter_info_t
*voter
= smartlist_get(consensus
->voters
, 0);
2548 ds
= trusteddirserver_get_by_v3_auth_digest(voter
->identity_digest
);
2550 source
= &(ds
->fake_status
);
2552 log_warn(LD_DIR
, "couldn't lookup source from vote?");
2555 map
= digestmap_new();
2556 list_pending_descriptor_downloads(map
, 0);
2557 SMARTLIST_FOREACH_BEGIN(consensus
->routerstatus_list
, void *, rsp
) {
2558 routerstatus_t
*rs
=
2559 is_vote
? &(((vote_routerstatus_t
*)rsp
)->status
) : rsp
;
2560 signed_descriptor_t
*sd
;
2561 if ((sd
= router_get_by_descriptor_digest(rs
->descriptor_digest
))) {
2562 const routerinfo_t
*ri
;
2564 if (!(ri
= router_get_by_id_digest(rs
->identity_digest
)) ||
2565 tor_memneq(ri
->cache_info
.signed_descriptor_digest
,
2566 sd
->signed_descriptor_digest
, DIGEST_LEN
)) {
2567 /* We have a descriptor with this digest, but either there is no
2568 * entry in routerlist with the same ID (!ri), or there is one,
2569 * but the identity digest differs (memneq).
2571 smartlist_add(no_longer_old
, sd
);
2572 ++n_in_oldrouters
; /* We have it in old_routers. */
2574 continue; /* We have it already. */
2576 if (digestmap_get(map
, rs
->descriptor_digest
)) {
2578 continue; /* We have an in-progress download. */
2580 if (!download_status_is_ready(&rs
->dl_status
, now
)) {
2581 ++n_delayed
; /* Not ready for retry. */
2584 if (authdir
&& dirserv_would_reject_router(rs
)) {
2586 continue; /* We would throw it out immediately. */
2588 if (!we_want_to_fetch_flavor(options
, consensus
->flavor
) &&
2589 !client_would_use_router(rs
, now
)) {
2591 continue; /* We would never use it ourself. */
2593 if (is_vote
&& source
) {
2594 char time_bufnew
[ISO_TIME_LEN
+1];
2595 char time_bufold
[ISO_TIME_LEN
+1];
2596 const routerinfo_t
*oldrouter
;
2597 oldrouter
= router_get_by_id_digest(rs
->identity_digest
);
2598 format_iso_time(time_bufnew
, rs
->published_on
);
2600 format_iso_time(time_bufold
, oldrouter
->cache_info
.published_on
);
2601 log_info(LD_DIR
, "Learned about %s (%s vs %s) from %s's vote (%s)",
2602 routerstatus_describe(rs
),
2604 oldrouter
? time_bufold
: "none",
2605 source
->nickname
, oldrouter
? "known" : "unknown");
2607 smartlist_add(downloadable
, rs
->descriptor_digest
);
2608 } SMARTLIST_FOREACH_END(rsp
);
2610 if (!authdir_mode_v3(options
)
2611 && smartlist_len(no_longer_old
)) {
2612 routerlist_t
*rl
= router_get_routerlist();
2613 log_info(LD_DIR
, "%d router descriptors listed in consensus are "
2614 "currently in old_routers; making them current.",
2615 smartlist_len(no_longer_old
));
2616 SMARTLIST_FOREACH_BEGIN(no_longer_old
, signed_descriptor_t
*, sd
) {
2618 was_router_added_t r
;
2619 time_t tmp_cert_expiration_time
;
2620 routerinfo_t
*ri
= routerlist_reparse_old(rl
, sd
);
2622 log_warn(LD_BUG
, "Failed to re-parse a router.");
2625 /* need to remember for below, since add_to_routerlist may free. */
2626 tmp_cert_expiration_time
= ri
->cert_expiration_time
;
2628 r
= router_add_to_routerlist(ri
, &msg
, 1, 0);
2629 if (WRA_WAS_OUTDATED(r
)) {
2630 log_warn(LD_DIR
, "Couldn't add re-parsed router: %s. This isn't "
2631 "usually a big deal, but you should make sure that your "
2632 "clock and timezone are set correctly.",
2634 if (r
== ROUTER_CERTS_EXPIRED
) {
2635 char time_cons
[ISO_TIME_LEN
+1];
2636 char time_cert_expires
[ISO_TIME_LEN
+1];
2637 format_iso_time(time_cons
, consensus
->valid_after
);
2638 format_iso_time(time_cert_expires
, tmp_cert_expiration_time
);
2639 log_warn(LD_DIR
, " (I'm looking at a consensus from %s; This "
2640 "router's certificates began expiring at %s.)",
2641 time_cons
, time_cert_expires
);
2644 } SMARTLIST_FOREACH_END(sd
);
2645 routerlist_assert_ok(rl
);
2649 "%d router descriptors downloadable. %d delayed; %d present "
2650 "(%d of those were in old_routers); %d would_reject; "
2651 "%d wouldnt_use; %d in progress.",
2652 smartlist_len(downloadable
), n_delayed
, n_have
, n_in_oldrouters
,
2653 n_would_reject
, n_wouldnt_use
, n_inprogress
);
2655 launch_descriptor_downloads(DIR_PURPOSE_FETCH_SERVERDESC
,
2656 downloadable
, source
, now
);
2658 digestmap_free(map
, NULL
);
2660 smartlist_free(downloadable
);
2661 smartlist_free(no_longer_old
);
2664 /** How often should we launch a server/authority request to be sure of getting
2665 * a guess for our IP? */
2666 /*XXXX+ this info should come from netinfo cells or something, or we should
2667 * do this only when we aren't seeing incoming data. see bug 652. */
2668 #define DUMMY_DOWNLOAD_INTERVAL (20*60)
2670 /** As needed, launch a dummy router descriptor fetch to see if our
2671 * address has changed. */
2673 launch_dummy_descriptor_download_as_needed(time_t now
,
2674 const or_options_t
*options
)
2676 static time_t last_dummy_download
= 0;
2677 /* XXXX+ we could be smarter here; see notes on bug 652. */
2678 /* If we're a server that doesn't have a configured address, we rely on
2679 * directory fetches to learn when our address changes. So if we haven't
2680 * tried to get any routerdescs in a long time, try a dummy fetch now. */
2681 if (!options
->Address
&&
2682 server_mode(options
) &&
2683 last_descriptor_download_attempted
+ DUMMY_DOWNLOAD_INTERVAL
< now
&&
2684 last_dummy_download
+ DUMMY_DOWNLOAD_INTERVAL
< now
) {
2685 last_dummy_download
= now
;
2686 /* XX/teor - do we want an authority here, because they are less likely
2687 * to give us the wrong address? (See #17782)
2688 * I'm leaving the previous behaviour intact, because I don't like
2689 * the idea of some relays contacting an authority every 20 minutes. */
2690 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC
,
2691 ROUTER_PURPOSE_GENERAL
, "authority.z",
2692 PDS_RETRY_IF_NO_SERVERS
,
2693 DL_WANT_ANY_DIRSERVER
);
2697 /** Launch downloads for router status as needed. */
2699 update_router_descriptor_downloads(time_t now
)
2701 const or_options_t
*options
= get_options();
2702 if (should_delay_dir_fetches(options
, NULL
))
2704 if (!we_fetch_router_descriptors(options
))
2707 update_consensus_router_descriptor_downloads(now
, 0,
2708 networkstatus_get_reasonably_live_consensus(now
, FLAV_NS
));
2711 /** Launch extrainfo downloads as needed. */
2713 update_extrainfo_downloads(time_t now
)
2715 const or_options_t
*options
= get_options();
2717 smartlist_t
*wanted
;
2718 digestmap_t
*pending
;
2719 int old_routers
, i
, max_dl_per_req
;
2720 int n_no_ei
= 0, n_pending
= 0, n_have
= 0, n_delay
= 0, n_bogus
[2] = {0,0};
2721 if (! options
->DownloadExtraInfo
)
2723 if (should_delay_dir_fetches(options
, NULL
))
2725 if (!router_have_minimum_dir_info())
2728 pending
= digestmap_new();
2729 list_pending_descriptor_downloads(pending
, 1);
2730 rl
= router_get_routerlist();
2731 wanted
= smartlist_new();
2732 for (old_routers
= 0; old_routers
< 2; ++old_routers
) {
2733 smartlist_t
*lst
= old_routers
? rl
->old_routers
: rl
->routers
;
2734 for (i
= 0; i
< smartlist_len(lst
); ++i
) {
2735 signed_descriptor_t
*sd
;
2738 sd
= smartlist_get(lst
, i
);
2740 sd
= &((routerinfo_t
*)smartlist_get(lst
, i
))->cache_info
;
2741 if (sd
->is_extrainfo
)
2742 continue; /* This should never happen. */
2743 if (old_routers
&& !router_get_by_id_digest(sd
->identity_digest
))
2744 continue; /* Couldn't check the signature if we got it. */
2745 if (sd
->extrainfo_is_bogus
)
2747 d
= sd
->extra_info_digest
;
2748 if (tor_digest_is_zero(d
)) {
2752 if (eimap_get(rl
->extra_info_map
, d
)) {
2756 if (!download_status_is_ready(&sd
->ei_dl_status
, now
)) {
2760 if (digestmap_get(pending
, d
)) {
2765 const signed_descriptor_t
*sd2
= router_get_by_extrainfo_digest(d
);
2768 char d1
[HEX_DIGEST_LEN
+1], d2
[HEX_DIGEST_LEN
+1];
2769 char d3
[HEX_DIGEST_LEN
+1], d4
[HEX_DIGEST_LEN
+1];
2770 base16_encode(d1
, sizeof(d1
), sd
->identity_digest
, DIGEST_LEN
);
2771 base16_encode(d2
, sizeof(d2
), sd2
->identity_digest
, DIGEST_LEN
);
2772 base16_encode(d3
, sizeof(d3
), d
, DIGEST_LEN
);
2773 base16_encode(d4
, sizeof(d3
), sd2
->extra_info_digest
, DIGEST_LEN
);
2775 log_info(LD_DIR
, "Found an entry in %s with mismatched "
2776 "router_get_by_extrainfo_digest() value. This has ID %s "
2777 "but the entry in the map has ID %s. This has EI digest "
2778 "%s and the entry in the map has EI digest %s.",
2779 old_routers
?"old_routers":"routers",
2782 char d1
[HEX_DIGEST_LEN
+1], d2
[HEX_DIGEST_LEN
+1];
2783 base16_encode(d1
, sizeof(d1
), sd
->identity_digest
, DIGEST_LEN
);
2784 base16_encode(d2
, sizeof(d2
), d
, DIGEST_LEN
);
2786 log_info(LD_DIR
, "Found an entry in %s with NULL "
2787 "router_get_by_extrainfo_digest() value. This has ID %s "
2788 "and EI digest %s.",
2789 old_routers
?"old_routers":"routers",
2792 ++n_bogus
[old_routers
];
2795 smartlist_add(wanted
, d
);
2798 digestmap_free(pending
, NULL
);
2800 log_info(LD_DIR
, "Extrainfo download status: %d router with no ei, %d "
2801 "with present ei, %d delaying, %d pending, %d downloadable, %d "
2802 "bogus in routers, %d bogus in old_routers",
2803 n_no_ei
, n_have
, n_delay
, n_pending
, smartlist_len(wanted
),
2804 n_bogus
[0], n_bogus
[1]);
2806 smartlist_shuffle(wanted
);
2808 max_dl_per_req
= max_dl_per_request(options
, DIR_PURPOSE_FETCH_EXTRAINFO
);
2809 for (i
= 0; i
< smartlist_len(wanted
); i
+= max_dl_per_req
) {
2810 initiate_descriptor_downloads(NULL
, DIR_PURPOSE_FETCH_EXTRAINFO
,
2811 wanted
, i
, i
+max_dl_per_req
,
2812 PDS_RETRY_IF_NO_SERVERS
|PDS_NO_EXISTING_SERVERDESC_FETCH
);
2815 smartlist_free(wanted
);
2818 /** Reset the consensus and extra-info download failure count on all routers.
2819 * When we get a new consensus,
2820 * routers_update_status_from_consensus_networkstatus() will reset the
2821 * download statuses on the descriptors in that consensus.
2824 router_reset_descriptor_download_failures(void)
2826 log_debug(LD_GENERAL
,
2827 "In router_reset_descriptor_download_failures()");
2829 networkstatus_reset_download_failures();
2830 last_descriptor_download_attempted
= 0;
2833 /* We want to download *all* extra-info descriptors, not just those in
2834 * the consensus we currently have (or are about to have) */
2835 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
2837 download_status_reset(&ri
->cache_info
.ei_dl_status
);
2839 SMARTLIST_FOREACH(routerlist
->old_routers
, signed_descriptor_t
*, sd
,
2841 download_status_reset(&sd
->ei_dl_status
);
2845 /** Any changes in a router descriptor's publication time larger than this are
2846 * automatically non-cosmetic. */
2847 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
2849 /** We allow uptime to vary from how much it ought to be by this much. */
2850 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
2852 /** Return true iff the only differences between r1 and r2 are such that
2853 * would not cause a recent (post 0.1.1.6) dirserver to republish.
2856 router_differences_are_cosmetic(const routerinfo_t
*r1
, const routerinfo_t
*r2
)
2858 time_t r1pub
, r2pub
;
2859 long time_difference
;
2860 tor_assert(r1
&& r2
);
2862 /* r1 should be the one that was published first. */
2863 if (r1
->cache_info
.published_on
> r2
->cache_info
.published_on
) {
2864 const routerinfo_t
*ri_tmp
= r2
;
2869 /* If any key fields differ, they're different. */
2870 if (r1
->addr
!= r2
->addr
||
2871 strcasecmp(r1
->nickname
, r2
->nickname
) ||
2872 r1
->or_port
!= r2
->or_port
||
2873 !tor_addr_eq(&r1
->ipv6_addr
, &r2
->ipv6_addr
) ||
2874 r1
->ipv6_orport
!= r2
->ipv6_orport
||
2875 r1
->dir_port
!= r2
->dir_port
||
2876 r1
->purpose
!= r2
->purpose
||
2877 r1
->onion_pkey_len
!= r2
->onion_pkey_len
||
2878 !tor_memeq(r1
->onion_pkey
, r2
->onion_pkey
, r1
->onion_pkey_len
) ||
2879 !crypto_pk_eq_keys(r1
->identity_pkey
, r2
->identity_pkey
) ||
2880 strcasecmp(r1
->platform
, r2
->platform
) ||
2881 (r1
->contact_info
&& !r2
->contact_info
) || /* contact_info is optional */
2882 (!r1
->contact_info
&& r2
->contact_info
) ||
2883 (r1
->contact_info
&& r2
->contact_info
&&
2884 strcasecmp(r1
->contact_info
, r2
->contact_info
)) ||
2885 r1
->is_hibernating
!= r2
->is_hibernating
||
2886 ! addr_policies_eq(r1
->exit_policy
, r2
->exit_policy
) ||
2887 (r1
->supports_tunnelled_dir_requests
!=
2888 r2
->supports_tunnelled_dir_requests
))
2890 if ((r1
->declared_family
== NULL
) != (r2
->declared_family
== NULL
))
2892 if (r1
->declared_family
&& r2
->declared_family
) {
2894 if (smartlist_len(r1
->declared_family
)!=smartlist_len(r2
->declared_family
))
2896 n
= smartlist_len(r1
->declared_family
);
2897 for (i
=0; i
< n
; ++i
) {
2898 if (strcasecmp(smartlist_get(r1
->declared_family
, i
),
2899 smartlist_get(r2
->declared_family
, i
)))
2904 /* Did bandwidth change a lot? */
2905 if ((r1
->bandwidthcapacity
< r2
->bandwidthcapacity
/2) ||
2906 (r2
->bandwidthcapacity
< r1
->bandwidthcapacity
/2))
2909 /* Did the bandwidthrate or bandwidthburst change? */
2910 if ((r1
->bandwidthrate
!= r2
->bandwidthrate
) ||
2911 (r1
->bandwidthburst
!= r2
->bandwidthburst
))
2914 /* Did more than 12 hours pass? */
2915 if (r1
->cache_info
.published_on
+ ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
2916 < r2
->cache_info
.published_on
)
2919 /* Did uptime fail to increase by approximately the amount we would think,
2920 * give or take some slop? */
2921 r1pub
= r1
->cache_info
.published_on
;
2922 r2pub
= r2
->cache_info
.published_on
;
2923 time_difference
= labs(r2
->uptime
- (r1
->uptime
+ (r2pub
- r1pub
)));
2924 if (time_difference
> ROUTER_ALLOW_UPTIME_DRIFT
&&
2925 time_difference
> r1
->uptime
* .05 &&
2926 time_difference
> r2
->uptime
* .05)
2929 /* Otherwise, the difference is cosmetic. */
2933 /** Check whether <b>sd</b> describes a router descriptor compatible with the
2934 * extrainfo document <b>ei</b>.
2936 * <b>identity_pkey</b> (which must also be provided) is RSA1024 identity key
2937 * for the router. We use it to check the signature of the extrainfo document,
2938 * if it has not already been checked.
2940 * If no router is compatible with <b>ei</b>, <b>ei</b> should be
2941 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
2942 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
2943 * <b>msg</b> is present, set *<b>msg</b> to a description of the
2944 * incompatibility (if any).
2946 * Set the extrainfo_is_bogus field in <b>sd</b> if the digests matched
2947 * but the extrainfo was nonetheless incompatible.
2950 routerinfo_incompatible_with_extrainfo(const crypto_pk_t
*identity_pkey
,
2952 signed_descriptor_t
*sd
,
2955 int digest_matches
, digest256_matches
, r
=1;
2956 tor_assert(identity_pkey
);
2961 if (msg
) *msg
= "Extrainfo signature was bad, or signed with wrong key.";
2965 digest_matches
= tor_memeq(ei
->cache_info
.signed_descriptor_digest
,
2966 sd
->extra_info_digest
, DIGEST_LEN
);
2967 /* Set digest256_matches to 1 if the digest is correct, or if no
2968 * digest256 was in the ri. */
2969 digest256_matches
= tor_memeq(ei
->digest256
,
2970 sd
->extra_info_digest256
, DIGEST256_LEN
);
2971 digest256_matches
|=
2972 tor_mem_is_zero(sd
->extra_info_digest256
, DIGEST256_LEN
);
2974 /* The identity must match exactly to have been generated at the same time
2975 * by the same router. */
2976 if (tor_memneq(sd
->identity_digest
,
2977 ei
->cache_info
.identity_digest
,
2979 if (msg
) *msg
= "Extrainfo nickname or identity did not match routerinfo";
2980 goto err
; /* different servers */
2983 if (! tor_cert_opt_eq(sd
->signing_key_cert
,
2984 ei
->cache_info
.signing_key_cert
)) {
2985 if (msg
) *msg
= "Extrainfo signing key cert didn't match routerinfo";
2986 goto err
; /* different servers */
2989 if (ei
->pending_sig
) {
2990 char signed_digest
[128];
2991 if (crypto_pk_public_checksig(identity_pkey
,
2992 signed_digest
, sizeof(signed_digest
),
2993 ei
->pending_sig
, ei
->pending_sig_len
) != DIGEST_LEN
||
2994 tor_memneq(signed_digest
, ei
->cache_info
.signed_descriptor_digest
,
2997 tor_free(ei
->pending_sig
);
2998 if (msg
) *msg
= "Extrainfo signature bad, or signed with wrong key";
2999 goto err
; /* Bad signature, or no match. */
3002 ei
->cache_info
.send_unencrypted
= sd
->send_unencrypted
;
3003 tor_free(ei
->pending_sig
);
3006 if (ei
->cache_info
.published_on
< sd
->published_on
) {
3007 if (msg
) *msg
= "Extrainfo published time did not match routerdesc";
3009 } else if (ei
->cache_info
.published_on
> sd
->published_on
) {
3010 if (msg
) *msg
= "Extrainfo published time did not match routerdesc";
3015 if (!digest256_matches
&& !digest_matches
) {
3016 if (msg
) *msg
= "Neither digest256 or digest matched "
3017 "digest from routerdesc";
3021 if (!digest256_matches
) {
3022 if (msg
) *msg
= "Extrainfo digest did not match digest256 from routerdesc";
3023 goto err
; /* Digest doesn't match declared value. */
3026 if (!digest_matches
) {
3027 if (msg
) *msg
= "Extrainfo digest did not match value from routerdesc";
3028 goto err
; /* Digest doesn't match declared value. */
3033 if (digest_matches
) {
3034 /* This signature was okay, and the digest was right: This is indeed the
3035 * corresponding extrainfo. But insanely, it doesn't match the routerinfo
3036 * that lists it. Don't try to fetch this one again. */
3037 sd
->extrainfo_is_bogus
= 1;
3043 /* Does ri have a valid ntor onion key?
3044 * Valid ntor onion keys exist and have at least one non-zero byte. */
3046 routerinfo_has_curve25519_onion_key(const routerinfo_t
*ri
)
3052 if (!ri
->onion_curve25519_pkey
) {
3056 if (tor_mem_is_zero((const char*)ri
->onion_curve25519_pkey
->public_key
,
3057 CURVE25519_PUBKEY_LEN
)) {
3064 /* Is rs running a tor version known to support EXTEND2 cells?
3065 * If allow_unknown_versions is true, return true if we can't tell
3066 * (from a versions line or a protocols line) whether it supports extend2
3068 * Otherwise, return false if the version is unknown. */
3070 routerstatus_version_supports_extend2_cells(const routerstatus_t
*rs
,
3071 int allow_unknown_versions
)
3074 return allow_unknown_versions
;
3077 if (!rs
->pv
.protocols_known
) {
3078 return allow_unknown_versions
;
3081 return rs
->pv
.supports_extend2_cells
;
3084 /** Assert that the internal representation of <b>rl</b> is
3085 * self-consistent. */
3087 routerlist_assert_ok(const routerlist_t
*rl
)
3090 signed_descriptor_t
*sd2
;
3093 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, r
) {
3094 r2
= rimap_get(rl
->identity_map
, r
->cache_info
.identity_digest
);
3095 tor_assert(r
== r2
);
3096 sd2
= sdmap_get(rl
->desc_digest_map
,
3097 r
->cache_info
.signed_descriptor_digest
);
3098 tor_assert(&(r
->cache_info
) == sd2
);
3099 tor_assert(r
->cache_info
.routerlist_index
== r_sl_idx
);
3102 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
3103 * commenting this out is just a band-aid.
3105 * The problem is that, although well-behaved router descriptors
3106 * should never have the same value for their extra_info_digest, it's
3107 * possible for ill-behaved routers to claim whatever they like there.
3109 * The real answer is to trash desc_by_eid_map and instead have
3110 * something that indicates for a given extra-info digest we want,
3111 * what its download status is. We'll do that as a part of routerlist
3112 * refactoring once consensus directories are in. For now,
3113 * this rep violation is probably harmless: an adversary can make us
3114 * reset our retry count for an extrainfo, but that's not the end
3115 * of the world. Changing the representation in 0.2.0.x would just
3116 * destabilize the codebase.
3117 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
3118 signed_descriptor_t *sd3 =
3119 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
3120 tor_assert(sd3 == &(r->cache_info));
3123 } SMARTLIST_FOREACH_END(r
);
3124 SMARTLIST_FOREACH_BEGIN(rl
->old_routers
, signed_descriptor_t
*, sd
) {
3125 r2
= rimap_get(rl
->identity_map
, sd
->identity_digest
);
3126 tor_assert(!r2
|| sd
!= &(r2
->cache_info
));
3127 sd2
= sdmap_get(rl
->desc_digest_map
, sd
->signed_descriptor_digest
);
3128 tor_assert(sd
== sd2
);
3129 tor_assert(sd
->routerlist_index
== sd_sl_idx
);
3131 if (!tor_digest_is_zero(sd->extra_info_digest)) {
3132 signed_descriptor_t *sd3 =
3133 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
3134 tor_assert(sd3 == sd);
3137 } SMARTLIST_FOREACH_END(sd
);
3139 RIMAP_FOREACH(rl
->identity_map
, d
, r
) {
3140 tor_assert(tor_memeq(r
->cache_info
.identity_digest
, d
, DIGEST_LEN
));
3141 } DIGESTMAP_FOREACH_END
;
3142 SDMAP_FOREACH(rl
->desc_digest_map
, d
, sd
) {
3143 tor_assert(tor_memeq(sd
->signed_descriptor_digest
, d
, DIGEST_LEN
));
3144 } DIGESTMAP_FOREACH_END
;
3145 SDMAP_FOREACH(rl
->desc_by_eid_map
, d
, sd
) {
3146 tor_assert(!tor_digest_is_zero(d
));
3148 tor_assert(tor_memeq(sd
->extra_info_digest
, d
, DIGEST_LEN
));
3149 } DIGESTMAP_FOREACH_END
;
3150 EIMAP_FOREACH(rl
->extra_info_map
, d
, ei
) {
3151 signed_descriptor_t
*sd
;
3152 tor_assert(tor_memeq(ei
->cache_info
.signed_descriptor_digest
,
3154 sd
= sdmap_get(rl
->desc_by_eid_map
,
3155 ei
->cache_info
.signed_descriptor_digest
);
3156 // tor_assert(sd); // XXXX see above
3158 tor_assert(tor_memeq(ei
->cache_info
.signed_descriptor_digest
,
3159 sd
->extra_info_digest
, DIGEST_LEN
));
3161 } DIGESTMAP_FOREACH_END
;
3164 /** Allocate and return a new string representing the contact info
3165 * and platform string for <b>router</b>,
3166 * surrounded by quotes and using standard C escapes.
3168 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
3169 * thread. Also, each call invalidates the last-returned value, so don't
3170 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
3172 * If <b>router</b> is NULL, it just frees its internal memory and returns.
3175 esc_router_info(const routerinfo_t
*router
)
3177 static char *info
=NULL
;
3178 char *esc_contact
, *esc_platform
;
3182 return NULL
; /* we're exiting; just free the memory we use */
3184 esc_contact
= esc_for_log(router
->contact_info
);
3185 esc_platform
= esc_for_log(router
->platform
);
3187 tor_asprintf(&info
, "Contact %s, Platform %s", esc_contact
, esc_platform
);
3188 tor_free(esc_contact
);
3189 tor_free(esc_platform
);
3194 /** Helper for sorting: compare two routerinfos by their identity
3197 compare_routerinfo_by_id_digest_(const void **a
, const void **b
)
3199 routerinfo_t
*first
= *(routerinfo_t
**)a
, *second
= *(routerinfo_t
**)b
;
3200 return fast_memcmp(first
->cache_info
.identity_digest
,
3201 second
->cache_info
.identity_digest
,
3205 /** Sort a list of routerinfo_t in ascending order of identity digest. */
3207 routers_sort_by_identity(smartlist_t
*routers
)
3209 smartlist_sort(routers
, compare_routerinfo_by_id_digest_
);
3212 /** Called when we change a node set, or when we reload the geoip IPv4 list:
3213 * recompute all country info in all configuration node sets and in the
3216 refresh_all_country_info(void)
3218 const or_options_t
*options
= get_options();
3220 if (options
->EntryNodes
)
3221 routerset_refresh_countries(options
->EntryNodes
);
3222 if (options
->ExitNodes
)
3223 routerset_refresh_countries(options
->ExitNodes
);
3224 if (options
->MiddleNodes
)
3225 routerset_refresh_countries(options
->MiddleNodes
);
3226 if (options
->ExcludeNodes
)
3227 routerset_refresh_countries(options
->ExcludeNodes
);
3228 if (options
->ExcludeExitNodes
)
3229 routerset_refresh_countries(options
->ExcludeExitNodes
);
3230 if (options
->ExcludeExitNodesUnion_
)
3231 routerset_refresh_countries(options
->ExcludeExitNodesUnion_
);
3233 nodelist_refresh_countries();