Tor 0.4.9.0-alpha-dev
routerlist.c
Go to the documentation of this file.
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-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file routerlist.c
9 * \brief Code to
10 * maintain and access the global list of routerinfos for known
11 * servers.
12 *
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.
19 *
20 * Most users will not use router descriptors for most relays. Instead,
21 * they use the information in microdescriptors and in the consensus
22 * networkstatus.
23 *
24 * Right now, routerinfo_t objects are used in these ways:
25 * <ul>
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
34 * else wants them.
35 * </ul>
36 *
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.)
42 *
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.)
48 *
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.
53 *
54 * <br>
55 *
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.)
60 **/
61
62#define ROUTERLIST_PRIVATE
63#include "core/or/or.h"
64
65#include "app/config/config.h"
68#include "core/or/circuitlist.h"
69#include "core/or/circuituse.h"
70#include "core/or/extendinfo.h"
71#include "core/or/policies.h"
99
110
112
113#ifdef HAVE_SYS_STAT_H
114#include <sys/stat.h>
115#endif
116
117// #define DEBUG_ROUTERLIST
118
119/****************************************************************************/
120
121/* Typed wrappers for different digestmap types; used to avoid type
122 * confusion. */
123
124DECLARE_TYPED_DIGESTMAP_FNS(sdmap, digest_sd_map_t, signed_descriptor_t)
125DECLARE_TYPED_DIGESTMAP_FNS(rimap, digest_ri_map_t, routerinfo_t)
126DECLARE_TYPED_DIGESTMAP_FNS(eimap, digest_ei_map_t, extrainfo_t)
127#define SDMAP_FOREACH(map, keyvar, valvar) \
128 DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
129 valvar)
130#define RIMAP_FOREACH(map, keyvar, valvar) \
131 DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
132#define EIMAP_FOREACH(map, keyvar, valvar) \
133 DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
134#define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn))
135#define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn))
136#define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn))
137
138/* static function prototypes */
140static const char *signed_descriptor_get_body_impl(
141 const signed_descriptor_t *desc,
142 int with_annotations);
143
144/****************************************************************************/
145
146/** Global list of all of the routers that we know about. */
148
149/** List of strings for nicknames we've already warned about and that are
150 * still unknown / unavailable. */
152
153/** The last time we tried to download any routerdesc, or 0 for "never". We
154 * use this to rate-limit download attempts when the number of routerdescs to
155 * download is low. */
157
158/* Router descriptor storage.
159 *
160 * Routerdescs are stored in a big file, named "cached-descriptors". As new
161 * routerdescs arrive, we append them to a journal file named
162 * "cached-descriptors.new".
163 *
164 * From time to time, we replace "cached-descriptors" with a new file
165 * containing only the live, non-superseded descriptors, and clear
166 * cached-descriptors.new.
167 *
168 * On startup, we read both files.
169 */
170
171/** Helper: return 1 iff the router log is so big we want to rebuild the
172 * store. */
173static int
175{
176 if (store->store_len > (1<<16))
177 return (store->journal_len > store->store_len / 2 ||
178 store->bytes_dropped > store->store_len / 2);
179 else
180 return store->journal_len > (1<<15);
181}
182
183/** Return the desc_store_t in <b>rl</b> that should be used to store
184 * <b>sd</b>. */
185static inline desc_store_t *
187{
188 if (sd->is_extrainfo)
189 return &rl->extrainfo_store;
190 else
191 return &rl->desc_store;
192}
193
194/** Add the signed_descriptor_t in <b>desc</b> to the router
195 * journal; change its saved_location to SAVED_IN_JOURNAL and set its
196 * offset appropriately. */
197static int
199 desc_store_t *store)
200{
201 char *fname = get_cachedir_fname_suffix(store->fname_base, ".new");
202 const char *body = signed_descriptor_get_body_impl(desc,1);
203 size_t len = desc->signed_descriptor_len + desc->annotations_len;
204
205 if (append_bytes_to_file(fname, body, len, 1)) {
206 log_warn(LD_FS, "Unable to store router descriptor");
207 tor_free(fname);
208 return -1;
209 }
211 tor_free(fname);
212
213 desc->saved_offset = store->journal_len;
214 store->journal_len += len;
215
216 return 0;
217}
218
219/** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
220 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
221 * the signed_descriptor_t* in *<b>b</b>. */
222static int
223compare_signed_descriptors_by_age_(const void **_a, const void **_b)
224{
225 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
226 return (int)(r1->published_on - r2->published_on);
227}
228
229#define RRS_FORCE 1
230#define RRS_DONT_REMOVE_OLD 2
231
232/** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
233 * <b>flags</b>, then atomically replace the saved router store with the
234 * routers currently in our routerlist, and clear the journal. Unless
235 * RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
236 * rebuilding the store. Return 0 on success, -1 on failure.
237 */
238static int
240{
241 smartlist_t *chunk_list = NULL;
242 char *fname = NULL, *fname_tmp = NULL;
243 int r = -1;
244 off_t offset = 0;
245 smartlist_t *signed_descriptors = NULL;
246 size_t total_expected_len = 0;
247 int had_any;
248 int force = flags & RRS_FORCE;
249
250 if (!force && !router_should_rebuild_store(store)) {
251 r = 0;
252 goto done;
253 }
254 if (!routerlist) {
255 r = 0;
256 goto done;
257 }
258
259 if (store->type == EXTRAINFO_STORE)
260 had_any = !eimap_isempty(routerlist->extra_info_map);
261 else
262 had_any = (smartlist_len(routerlist->routers)+
263 smartlist_len(routerlist->old_routers))>0;
264
265 /* Don't save deadweight. */
266 if (!(flags & RRS_DONT_REMOVE_OLD))
268
269 log_info(LD_DIR, "Rebuilding %s cache", store->description);
270
271 fname = get_cachedir_fname(store->fname_base);
272 fname_tmp = get_cachedir_fname_suffix(store->fname_base, ".tmp");
273
274 chunk_list = smartlist_new();
275
276 /* We sort the routers by age to enhance locality on disk. */
277 signed_descriptors = smartlist_new();
278 if (store->type == EXTRAINFO_STORE) {
279 eimap_iter_t *iter;
280 for (iter = eimap_iter_init(routerlist->extra_info_map);
281 !eimap_iter_done(iter);
282 iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
283 const char *key;
284 extrainfo_t *ei;
285 eimap_iter_get(iter, &key, &ei);
286 smartlist_add(signed_descriptors, &ei->cache_info);
287 }
288 } else {
290 smartlist_add(signed_descriptors, sd));
292 smartlist_add(signed_descriptors, &ri->cache_info));
293 }
294
296
297 /* Now, add the appropriate members to chunk_list */
298 SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
299 sized_chunk_t *c;
300 const char *body = signed_descriptor_get_body_impl(sd, 1);
301 if (!body) {
302 log_warn(LD_BUG, "No descriptor available for router.");
303 goto done;
304 }
305 if (sd->do_not_cache) {
306 continue;
307 }
308 c = tor_malloc(sizeof(sized_chunk_t));
309 c->bytes = body;
310 c->len = sd->signed_descriptor_len + sd->annotations_len;
311 total_expected_len += c->len;
312 smartlist_add(chunk_list, c);
313 } SMARTLIST_FOREACH_END(sd);
314
315 if (write_chunks_to_file(fname_tmp, chunk_list, 1, 1)<0) {
316 log_warn(LD_FS, "Error writing router store to disk.");
317 goto done;
318 }
319
320 /* Our mmap is now invalid. */
321 if (store->mmap) {
322 int res = tor_munmap_file(store->mmap);
323 store->mmap = NULL;
324 if (res != 0) {
325 log_warn(LD_FS, "Unable to munmap route store in %s", fname);
326 }
327 }
328
329 if (replace_file(fname_tmp, fname)<0) {
330 log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
331 goto done;
332 }
333
334 errno = 0;
335 store->mmap = tor_mmap_file(fname);
336 if (! store->mmap) {
337 if (errno == ERANGE) {
338 /* empty store.*/
339 if (total_expected_len) {
340 log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
341 " but when we went to mmap it, it was empty!", fname);
342 } else if (had_any) {
343 log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
344 "okay if we're just starting up after a long time. "
345 "Otherwise, it's a bug.", fname);
346 }
347 } else {
348 log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
349 }
350 }
351
352 log_info(LD_DIR, "Reconstructing pointers into cache");
353
354 offset = 0;
355 SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
356 if (sd->do_not_cache)
357 continue;
358 sd->saved_location = SAVED_IN_CACHE;
359 if (store->mmap) {
360 tor_free(sd->signed_descriptor_body); // sets it to null
361 sd->saved_offset = offset;
362 }
363 offset += sd->signed_descriptor_len + sd->annotations_len;
364 signed_descriptor_get_body(sd); /* reconstruct and assert */
365 } SMARTLIST_FOREACH_END(sd);
366
367 tor_free(fname);
368 fname = get_cachedir_fname_suffix(store->fname_base, ".new");
369 write_str_to_file(fname, "", 1);
370
371 r = 0;
372 store->store_len = (size_t) offset;
373 store->journal_len = 0;
374 store->bytes_dropped = 0;
375 done:
376 smartlist_free(signed_descriptors);
377 tor_free(fname);
378 tor_free(fname_tmp);
379 if (chunk_list) {
380 SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
381 smartlist_free(chunk_list);
382 }
383
384 return r;
385}
386
387/** Helper: Reload a cache file and its associated journal, setting metadata
388 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
389 * else reload the router descriptor store. */
390static int
392{
393 char *fname = NULL, *contents = NULL;
394 struct stat st;
395 int extrainfo = (store->type == EXTRAINFO_STORE);
396 store->journal_len = store->store_len = 0;
397
398 fname = get_cachedir_fname(store->fname_base);
399
400 if (store->mmap) {
401 /* get rid of it first */
402 int res = tor_munmap_file(store->mmap);
403 store->mmap = NULL;
404 if (res != 0) {
405 log_warn(LD_FS, "Failed to munmap %s", fname);
406 tor_free(fname);
407 return -1;
408 }
409 }
410
411 store->mmap = tor_mmap_file(fname);
412 if (store->mmap) {
413 store->store_len = store->mmap->size;
414 if (extrainfo)
416 store->mmap->data+store->mmap->size,
417 SAVED_IN_CACHE, NULL, 0);
418 else
420 store->mmap->data+store->mmap->size,
421 SAVED_IN_CACHE, NULL, 0, NULL);
422 }
423
424 tor_free(fname);
425 fname = get_cachedir_fname_suffix(store->fname_base, ".new");
426 /* don't load empty files - we wouldn't get any data, even if we tried */
427 if (file_status(fname) == FN_FILE)
428 contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
429 if (contents) {
430 if (extrainfo)
432 NULL, 0);
433 else
435 NULL, 0, NULL);
436 store->journal_len = (size_t) st.st_size;
437 tor_free(contents);
438 }
439
440 tor_free(fname);
441
442 if (store->journal_len) {
443 /* Always clear the journal on startup.*/
444 router_rebuild_store(RRS_FORCE, store);
445 } else if (!extrainfo) {
446 /* Don't cache expired routers. (This is in an else because
447 * router_rebuild_store() also calls remove_old_routers().) */
449 }
450
451 return 0;
452}
453
454/** Load all cached router descriptors and extra-info documents from the
455 * store. Return 0 on success and -1 on failure.
456 */
457int
459{
462 return -1;
464 return -1;
465 return 0;
466}
467
468/* When selecting a router for a direct connection, can OR address/port
469 * preference and reachability checks be skipped?
470 *
471 * Servers never check ReachableAddresses or ClientPreferIPv6. Returns
472 * true for servers.
473 *
474 * Otherwise, if <b>try_ip_pref</b> is true, returns false. Used to make
475 * clients check ClientPreferIPv6, even if ReachableAddresses is not set.
476 * Finally, return true if ReachableAddresses is set.
477 */
478int
479router_or_conn_should_skip_reachable_address_check(
480 const or_options_t *options,
481 int try_ip_pref)
482{
483 /* Servers always have and prefer IPv4.
484 * And if clients are checking against the firewall for reachability only,
485 * but there's no firewall, don't bother checking */
486 return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_or());
487}
488
489/* When selecting a router for a direct connection, can Dir address/port
490 * and reachability checks be skipped?
491 *
492 * This function is obsolete, because clients only use ORPorts.
493 */
494int
495router_dir_conn_should_skip_reachable_address_check(
496 const or_options_t *options,
497 int try_ip_pref)
498{
499 /* Servers always have and prefer IPv4.
500 * And if clients are checking against the firewall for reachability only,
501 * but there's no firewall, don't bother checking */
502 return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_dir());
503}
504
505/** Return true iff r1 and r2 have the same address and OR port. */
506int
508{
509 return tor_addr_eq(&r1->ipv4_addr, &r2->ipv4_addr) &&
510 r1->ipv4_orport == r2->ipv4_orport &&
511 tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) &&
512 r1->ipv6_orport == r2->ipv6_orport;
513}
514
515/* Returns true if <b>node</b> can be chosen based on <b>flags</b>.
516 *
517 * The following conditions are applied to all nodes:
518 * - is running;
519 * - is valid;
520 * - supports EXTEND2 cells;
521 * - has an ntor circuit crypto key; and
522 * - does not allow single-hop exits.
523 *
524 * If the node has a routerinfo, we're checking for a direct connection, and
525 * we're using bridges, the following condition is applied:
526 * - has a bridge-purpose routerinfo;
527 * and for all other nodes:
528 * - has a general-purpose routerinfo (or no routerinfo).
529 *
530 * Nodes that don't have a routerinfo must be general-purpose nodes, because
531 * routerstatuses and microdescriptors only come via consensuses.
532 *
533 * The <b>flags</b> check that <b>node</b>:
534 * - <b>CRN_NEED_UPTIME</b>: has more than a minimum uptime;
535 * - <b>CRN_NEED_CAPACITY</b>: has more than a minimum capacity;
536 * - <b>CRN_NEED_GUARD</b>: is a Guard;
537 * - <b>CRN_NEED_DESC</b>: has a routerinfo or microdescriptor -- that is,
538 * enough info to be used to build a circuit;
539 * - <b>CRN_DIRECT_CONN</b>: is suitable for direct connections. Checks
540 * for the relevant descriptors. Checks the address
541 * against ReachableAddresses, ClientUseIPv4 0, and
542 * reachable_addr_use_ipv6() == 0);
543 * - <b>CRN_PREF_ADDR</b>: if we are connecting directly to the node, it has
544 * an address that is preferred by the
545 * ClientPreferIPv6ORPort setting;
546 * - <b>CRN_RENDEZVOUS_V3</b>: can become a v3 onion service rendezvous point;
547 * - <b>CRN_INITIATE_IPV6_EXTEND</b>: can initiate IPv6 extends.
548 */
549bool
550router_can_choose_node(const node_t *node, int flags)
551{
552 /* The full set of flags used for node selection. */
553 const bool need_uptime = (flags & CRN_NEED_UPTIME) != 0;
554 const bool need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
555 const bool need_guard = (flags & CRN_NEED_GUARD) != 0;
556 const bool need_desc = (flags & CRN_NEED_DESC) != 0;
557 const bool pref_addr = (flags & CRN_PREF_ADDR) != 0;
558 const bool direct_conn = (flags & CRN_DIRECT_CONN) != 0;
559 const bool rendezvous_v3 = (flags & CRN_RENDEZVOUS_V3) != 0;
560 const bool initiate_ipv6_extend = (flags & CRN_INITIATE_IPV6_EXTEND) != 0;
561 const bool need_conflux = (flags & CRN_CONFLUX) != 0;
562
563 const or_options_t *options = get_options();
564 const bool check_reach =
565 !router_or_conn_should_skip_reachable_address_check(options, pref_addr);
566 const bool direct_bridge = direct_conn && options->UseBridges;
567
568 if (!node->is_running || !node->is_valid)
569 return false;
570 if (need_desc && !node_has_preferred_descriptor(node, direct_conn))
571 return false;
572 if (node->ri) {
573 if (direct_bridge && node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
574 return false;
575 else if (node->ri->purpose != ROUTER_PURPOSE_GENERAL)
576 return false;
577 }
578 if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
579 return false;
580 /* Don't choose nodes if we are certain they can't do EXTEND2 cells */
581 if (node->rs && !routerstatus_version_supports_extend2_cells(node->rs, 1))
582 return false;
583 /* Don't choose nodes if we are certain they can't do ntor. */
584 if ((node->ri || node->md) && !node_has_curve25519_onion_key(node))
585 return false;
586 /* Exclude relays that allow single hop exit circuits. This is an
587 * obsolete option since 0.2.9.2-alpha and done by default in
588 * 0.3.1.0-alpha. */
590 return false;
591 /* Exclude relays that can not become a rendezvous for a hidden service
592 * version 3. */
593 if (rendezvous_v3 &&
595 return false;
596 /* Exclude relay that don't do conflux if requested. */
597 if (need_conflux && !node_supports_conflux(node)) {
598 return false;
599 }
600 /* Choose a node with an OR address that matches the firewall rules */
601 if (direct_conn && check_reach &&
603 FIREWALL_OR_CONNECTION,
604 pref_addr))
605 return false;
606 if (initiate_ipv6_extend && !node_supports_initiating_ipv6_extends(node))
607 return false;
608
609 return true;
610}
611
612/** Add every suitable node from our nodelist to <b>sl</b>, so that
613 * we can pick a node for a circuit based on <b>flags</b>.
614 *
615 * See router_can_choose_node() for details of <b>flags</b>.
616 */
617void
619{
621 if (!router_can_choose_node(node, flags))
622 continue;
623 smartlist_add(sl, (void *)node);
624 } SMARTLIST_FOREACH_END(node);
625}
626
627/** Look through the routerlist until we find a router that has my key.
628 Return it. */
629const routerinfo_t *
631{
632 if (!routerlist)
633 return NULL;
634
636 {
637 if (router_is_me(router))
638 return router;
639 });
640 return NULL;
641}
642
643/** Return the smaller of the router's configured BandwidthRate
644 * and its advertised capacity. */
645uint32_t
647{
648 if (router->bandwidthcapacity < router->bandwidthrate)
649 return router->bandwidthcapacity;
650 return router->bandwidthrate;
651}
652
653/** Do not weight any declared bandwidth more than this much when picking
654 * routers by bandwidth. */
655#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
656
657/** Return the smaller of the router's configured BandwidthRate
658 * and its advertised capacity, capped by max-believe-bw. */
659uint32_t
661{
662 uint32_t result = router->bandwidthcapacity;
663 if (result > router->bandwidthrate)
664 result = router->bandwidthrate;
667 return result;
668}
669
670/** Helper: given an extended nickname in <b>hexdigest</b> try to decode it.
671 * Return 0 on success, -1 on failure. Store the result into the
672 * DIGEST_LEN-byte buffer at <b>digest_out</b>, the single character at
673 * <b>nickname_qualifier_char_out</b>, and the MAXNICKNAME_LEN+1-byte buffer
674 * at <b>nickname_out</b>.
675 *
676 * The recognized format is:
677 * HexName = Dollar? HexDigest NamePart?
678 * Dollar = '?'
679 * HexDigest = HexChar*20
680 * HexChar = 'a'..'f' | 'A'..'F' | '0'..'9'
681 * NamePart = QualChar Name
682 * QualChar = '=' | '~'
683 * Name = NameChar*(1..MAX_NICKNAME_LEN)
684 * NameChar = Any ASCII alphanumeric character
685 */
686int
687hex_digest_nickname_decode(const char *hexdigest,
688 char *digest_out,
689 char *nickname_qualifier_char_out,
690 char *nickname_out)
691{
692 size_t len;
693
694 tor_assert(hexdigest);
695 if (hexdigest[0] == '$')
696 ++hexdigest;
697
698 len = strlen(hexdigest);
699 if (len < HEX_DIGEST_LEN) {
700 return -1;
701 } else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' ||
702 hexdigest[HEX_DIGEST_LEN] == '~') &&
704 *nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN];
705 strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1);
706 } else if (len == HEX_DIGEST_LEN) {
707 ;
708 } else {
709 return -1;
710 }
711
712 if (base16_decode(digest_out, DIGEST_LEN,
713 hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN)
714 return -1;
715 return 0;
716}
717
718/** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
719 * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
720 * (which is optionally prefixed with a single dollar sign). Return false if
721 * <b>hexdigest</b> is malformed, or it doesn't match. */
722int
723hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest,
724 const char *nickname)
725{
726 char digest[DIGEST_LEN];
727 char nn_char='\0';
728 char nn_buf[MAX_NICKNAME_LEN+1];
729
730 if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1)
731 return 0;
732
733 if (nn_char == '=') {
734 return 0;
735 }
736
737 if (nn_char == '~') {
738 if (!nickname) // XXX This seems wrong. -NM
739 return 0;
740 if (strcasecmp(nn_buf, nickname))
741 return 0;
742 }
743
744 return tor_memeq(digest, identity_digest, DIGEST_LEN);
745}
746
747/** If hexdigest is correctly formed, base16_decode it into
748 * digest, which must have DIGEST_LEN space in it.
749 * Return 0 on success, -1 on failure.
750 */
751int
752hexdigest_to_digest(const char *hexdigest, char *digest)
753{
754 if (hexdigest[0]=='$')
755 ++hexdigest;
756 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
758 return -1;
759 return 0;
760}
761
762/** As router_get_by_id_digest,but return a pointer that you're allowed to
763 * modify */
766{
767 tor_assert(digest);
768
769 if (!routerlist) return NULL;
770
771 // routerlist_assert_ok(routerlist);
772
773 return rimap_get(routerlist->identity_map, digest);
774}
775
776/** Return the router in our routerlist whose 20-byte key digest
777 * is <b>digest</b>. Return NULL if no such router is known. */
778const routerinfo_t *
779router_get_by_id_digest(const char *digest)
780{
781 return router_get_mutable_by_digest(digest);
782}
783
784/** Return the router in our routerlist whose 20-byte descriptor
785 * is <b>digest</b>. Return NULL if no such router is known. */
788{
789 tor_assert(digest);
790
791 if (!routerlist) return NULL;
792
793 return sdmap_get(routerlist->desc_digest_map, digest);
794}
795
796/** Return the signed descriptor for the router in our routerlist whose
797 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
798 * is known. */
801{
802 tor_assert(digest);
803
804 if (!routerlist) return NULL;
805
806 return sdmap_get(routerlist->desc_by_eid_map, digest);
807}
808
809/** Return the signed descriptor for the extrainfo_t in our routerlist whose
810 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
811 * document is known. */
814{
815 extrainfo_t *ei;
816 tor_assert(digest);
817 if (!routerlist) return NULL;
818 ei = eimap_get(routerlist->extra_info_map, digest);
819 return ei ? &ei->cache_info : NULL;
820}
821
822/** Return a pointer to the signed textual representation of a descriptor.
823 * The returned string is not guaranteed to be NUL-terminated: the string's
824 * length will be in desc->signed_descriptor_len.
825 *
826 * If <b>with_annotations</b> is set, the returned string will include
827 * the annotations
828 * (if any) preceding the descriptor. This will increase the length of the
829 * string by desc->annotations_len.
830 *
831 * The caller must not free the string returned.
832 */
833static const char *
835 int with_annotations)
836{
837 const char *r = NULL;
838 size_t len = desc->signed_descriptor_len;
839 off_t offset = desc->saved_offset;
840 if (with_annotations)
841 len += desc->annotations_len;
842 else
843 offset += desc->annotations_len;
844
845 tor_assert(len > 32);
846 if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
848 if (store && store->mmap) {
849 tor_assert(desc->saved_offset + len <= store->mmap->size);
850 r = store->mmap->data + offset;
851 } else if (store) {
852 log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
853 "mmaped in our cache. Is another process running in our data "
854 "directory? Exiting.");
855 exit(1); // XXXX bad exit: should recover.
856 }
857 }
858 if (!r) /* no mmap, or not in cache. */
859 r = desc->signed_descriptor_body +
860 (with_annotations ? 0 : desc->annotations_len);
861
862 tor_assert(r);
863 if (!with_annotations) {
864 if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) {
865 char *cp = tor_strndup(r, 64);
866 log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
867 "Is another process running in our data directory? Exiting.",
868 desc, escaped(cp));
869 exit(1); // XXXX bad exit: should recover.
870 }
871 }
872
873 return r;
874}
875
876/** Return a pointer to the signed textual representation of a descriptor.
877 * The returned string is not guaranteed to be NUL-terminated: the string's
878 * length will be in desc->signed_descriptor_len.
879 *
880 * The caller must not free the string returned.
881 */
882const char *
884{
885 return signed_descriptor_get_body_impl(desc, 0);
886}
887
888/** As signed_descriptor_get_body(), but points to the beginning of the
889 * annotations section rather than the beginning of the descriptor. */
890const char *
892{
893 return signed_descriptor_get_body_impl(desc, 1);
894}
895
896/** Return the current list of all known routers. */
899{
900 if (PREDICT_UNLIKELY(!routerlist)) {
901 routerlist = tor_malloc_zero(sizeof(routerlist_t));
904 routerlist->identity_map = rimap_new();
905 routerlist->desc_digest_map = sdmap_new();
906 routerlist->desc_by_eid_map = sdmap_new();
907 routerlist->extra_info_map = eimap_new();
908
909 routerlist->desc_store.fname_base = "cached-descriptors";
910 routerlist->extrainfo_store.fname_base = "cached-extrainfo";
911
912 routerlist->desc_store.type = ROUTER_STORE;
913 routerlist->extrainfo_store.type = EXTRAINFO_STORE;
914
915 routerlist->desc_store.description = "router descriptors";
916 routerlist->extrainfo_store.description = "extra-info documents";
917 }
918 return routerlist;
919}
920
921/** Free all storage held by <b>router</b>. */
922void
924{
925 if (!router)
926 return;
927
928 tor_free(router->cache_info.signed_descriptor_body);
929 tor_free(router->nickname);
930 tor_free(router->platform);
931 tor_free(router->protocol_list);
932 tor_free(router->contact_info);
933 if (router->onion_pkey)
934 tor_free(router->onion_pkey);
936 if (router->identity_pkey)
937 crypto_pk_free(router->identity_pkey);
938 tor_cert_free(router->cache_info.signing_key_cert);
939 if (router->declared_family) {
940 SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
941 smartlist_free(router->declared_family);
942 }
943 addr_policy_list_free(router->exit_policy);
944 short_policy_free(router->ipv6_exit_policy);
945
946 memset(router, 77, sizeof(routerinfo_t));
947
948 tor_free(router);
949}
950
951/** Release all storage held by <b>extrainfo</b> */
952void
954{
955 if (!extrainfo)
956 return;
957 tor_cert_free(extrainfo->cache_info.signing_key_cert);
958 tor_free(extrainfo->cache_info.signed_descriptor_body);
959 tor_free(extrainfo->pending_sig);
960
961 memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
962 tor_free(extrainfo);
963}
964
965#define signed_descriptor_free(val) \
966 FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val))
967
968/** Release storage held by <b>sd</b>. */
969static void
971{
972 if (!sd)
973 return;
974
976 tor_cert_free(sd->signing_key_cert);
977
978 memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
979 tor_free(sd);
980}
981
982/** Reset the given signed descriptor <b>sd</b> by freeing the allocated
983 * memory inside the object and by zeroing its content. */
984static void
986{
987 tor_assert(sd);
989 tor_cert_free(sd->signing_key_cert);
990 memset(sd, 0, sizeof(*sd));
991}
992
993/** Copy src into dest, and steal all references inside src so that when
994 * we free src, we don't mess up dest. */
995static void
998{
999 tor_assert(dest != src);
1000 /* Cleanup destination object before overwriting it.*/
1002 memcpy(dest, src, sizeof(signed_descriptor_t));
1003 src->signed_descriptor_body = NULL;
1004 src->signing_key_cert = NULL;
1005 dest->routerlist_index = -1;
1006}
1007
1008/** Extract a signed_descriptor_t from a general routerinfo, and free the
1009 * routerinfo.
1010 */
1011static signed_descriptor_t *
1013{
1016 sd = tor_malloc_zero(sizeof(signed_descriptor_t));
1017 signed_descriptor_move(sd, &ri->cache_info);
1018 routerinfo_free(ri);
1019 return sd;
1020}
1021
1022/** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
1023static void
1025{
1026 extrainfo_free_(e);
1027}
1028
1029/** Free all storage held by a routerlist <b>rl</b>. */
1030void
1032{
1033 if (!rl)
1034 return;
1035 rimap_free(rl->identity_map, NULL);
1036 sdmap_free(rl->desc_digest_map, NULL);
1037 sdmap_free(rl->desc_by_eid_map, NULL);
1038 eimap_free(rl->extra_info_map, extrainfo_free_void);
1040 routerinfo_free(r));
1042 signed_descriptor_free(sd));
1043 smartlist_free(rl->routers);
1044 smartlist_free(rl->old_routers);
1045 if (rl->desc_store.mmap) {
1046 int res = tor_munmap_file(rl->desc_store.mmap);
1047 if (res != 0) {
1048 log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap");
1049 }
1050 }
1051 if (rl->extrainfo_store.mmap) {
1052 int res = tor_munmap_file(rl->extrainfo_store.mmap);
1053 if (res != 0) {
1054 log_warn(LD_FS, "Failed to munmap routerlist->extrainfo_store.mmap");
1055 }
1056 }
1057 tor_free(rl);
1058}
1059
1060/** Log information about how much memory is being used for routerlist,
1061 * at log level <b>severity</b>. */
1062void
1064{
1065 uint64_t livedescs = 0;
1066 uint64_t olddescs = 0;
1067 if (!routerlist)
1068 return;
1070 livedescs += r->cache_info.signed_descriptor_len);
1072 olddescs += sd->signed_descriptor_len);
1073
1074 tor_log(severity, LD_DIR,
1075 "In %d live descriptors: %"PRIu64" bytes. "
1076 "In %d old descriptors: %"PRIu64" bytes.",
1077 smartlist_len(routerlist->routers), (livedescs),
1078 smartlist_len(routerlist->old_routers), (olddescs));
1079}
1080
1081/** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
1082 * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
1083 * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
1084 * is not in <b>sl</b>. */
1085static inline int
1086routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
1087{
1088 if (idx < 0) {
1089 idx = -1;
1091 if (r == ri) {
1092 idx = r_sl_idx;
1093 break;
1094 });
1095 } else {
1096 tor_assert(idx < smartlist_len(sl));
1097 tor_assert(smartlist_get(sl, idx) == ri);
1098 };
1099 return idx;
1100}
1101
1102/** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
1103 * as needed. There must be no previous member of <b>rl</b> with the same
1104 * identity digest as <b>ri</b>: If there is, call routerlist_replace
1105 * instead.
1106 */
1107static void
1109{
1110 routerinfo_t *ri_old;
1111 signed_descriptor_t *sd_old;
1112 {
1113 const routerinfo_t *ri_generated = router_get_my_routerinfo();
1114 tor_assert(ri_generated != ri);
1115 }
1116 tor_assert(ri->cache_info.routerlist_index == -1);
1117
1118 ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
1119 tor_assert(!ri_old);
1120
1121 sd_old = sdmap_set(rl->desc_digest_map,
1122 ri->cache_info.signed_descriptor_digest,
1123 &(ri->cache_info));
1124 if (sd_old) {
1125 int idx = sd_old->routerlist_index;
1126 sd_old->routerlist_index = -1;
1127 smartlist_del(rl->old_routers, idx);
1128 if (idx < smartlist_len(rl->old_routers)) {
1129 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1130 d->routerlist_index = idx;
1131 }
1133 sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest);
1134 signed_descriptor_free(sd_old);
1135 }
1136
1137 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1138 sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
1139 &ri->cache_info);
1140 smartlist_add(rl->routers, ri);
1141 ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
1142 nodelist_set_routerinfo(ri, NULL);
1144#ifdef DEBUG_ROUTERLIST
1146#endif
1147}
1148
1149/** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
1150 * corresponding router in rl->routers or rl->old_routers. Return the status
1151 * of inserting <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
1153extrainfo_insert,(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible))
1154{
1156 const char *compatibility_error_msg;
1157 routerinfo_t *ri = rimap_get(rl->identity_map,
1158 ei->cache_info.identity_digest);
1160 sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
1161 extrainfo_t *ei_tmp;
1162 const int severity = warn_if_incompatible ? LOG_WARN : LOG_INFO;
1163
1164 {
1165 extrainfo_t *ei_generated = router_get_my_extrainfo();
1166 tor_assert(ei_generated != ei);
1167 }
1168
1169 if (!ri) {
1170 /* This router is unknown; we can't even verify the signature. Give up.*/
1171 r = ROUTER_NOT_IN_CONSENSUS;
1172 goto done;
1173 }
1174 if (! sd) {
1175 /* The extrainfo router doesn't have a known routerdesc to attach it to.
1176 * This just won't work. */;
1177 static ratelim_t no_sd_ratelim = RATELIM_INIT(1800);
1178 r = ROUTER_BAD_EI;
1179 /* This is a DEBUG because it can happen naturally, if we tried
1180 * to add an extrainfo for which we no longer have the
1181 * corresponding routerinfo.
1182 */
1183 log_fn_ratelim(&no_sd_ratelim, LOG_DEBUG, LD_DIR,
1184 "No entry found in extrainfo map.");
1185 goto done;
1186 }
1187 if (tor_memneq(ei->cache_info.signed_descriptor_digest,
1189 static ratelim_t digest_mismatch_ratelim = RATELIM_INIT(1800);
1190 /* The sd we got from the map doesn't match the digest we used to look
1191 * it up. This makes no sense. */
1192 r = ROUTER_BAD_EI;
1193 log_fn_ratelim(&digest_mismatch_ratelim, severity, LD_BUG,
1194 "Mismatch in digest in extrainfo map.");
1195 goto done;
1196 }
1198 &compatibility_error_msg)) {
1199 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
1200 r = (ri->cache_info.extrainfo_is_bogus) ?
1201 ROUTER_BAD_EI : ROUTER_NOT_IN_CONSENSUS;
1202
1203 base16_encode(d1, sizeof(d1), ri->cache_info.identity_digest, DIGEST_LEN);
1204 base16_encode(d2, sizeof(d2), ei->cache_info.identity_digest, DIGEST_LEN);
1205
1206 log_fn(severity,LD_DIR,
1207 "router info incompatible with extra info (ri id: %s, ei id %s, "
1208 "reason: %s)", d1, d2, compatibility_error_msg);
1209
1210 goto done;
1211 }
1212
1213 /* Okay, if we make it here, we definitely have a router corresponding to
1214 * this extrainfo. */
1215
1216 ei_tmp = eimap_set(rl->extra_info_map,
1217 ei->cache_info.signed_descriptor_digest,
1218 ei);
1219 r = ROUTER_ADDED_SUCCESSFULLY;
1220 if (ei_tmp) {
1222 ei_tmp->cache_info.signed_descriptor_len;
1223 extrainfo_free(ei_tmp);
1224 }
1225
1226 done:
1227 if (r != ROUTER_ADDED_SUCCESSFULLY)
1228 extrainfo_free(ei);
1229
1230#ifdef DEBUG_ROUTERLIST
1232#endif
1233 return r;
1234}
1235
1236#define should_cache_old_descriptors() \
1237 directory_caches_dir_info(get_options())
1238
1239/** If we're a directory cache and routerlist <b>rl</b> doesn't have
1240 * a copy of router <b>ri</b> yet, add it to the list of old (not
1241 * recommended but still served) descriptors. Else free it. */
1242static void
1244{
1245 {
1246 const routerinfo_t *ri_generated = router_get_my_routerinfo();
1247 tor_assert(ri_generated != ri);
1248 }
1249 tor_assert(ri->cache_info.routerlist_index == -1);
1250
1251 if (should_cache_old_descriptors() &&
1253 !sdmap_get(rl->desc_digest_map,
1254 ri->cache_info.signed_descriptor_digest)) {
1256 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1257 smartlist_add(rl->old_routers, sd);
1258 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1260 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1261 } else {
1262 routerinfo_free(ri);
1263 }
1264#ifdef DEBUG_ROUTERLIST
1266#endif
1267}
1268
1269/** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
1270 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl-&gt;routers,
1271 * idx) == ri, we don't need to do a linear search over the list to decide
1272 * which to remove. We fill the gap in rl-&gt;routers with a later element in
1273 * the list, if any exists. <b>ri</b> is freed.
1274 *
1275 * If <b>make_old</b> is true, instead of deleting the router, we try adding
1276 * it to rl-&gt;old_routers. */
1277void
1278routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
1279{
1280 routerinfo_t *ri_tmp;
1281 extrainfo_t *ei_tmp;
1282 int idx = ri->cache_info.routerlist_index;
1283 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1284 tor_assert(smartlist_get(rl->routers, idx) == ri);
1285
1287
1288 /* make sure the rephist module knows that it's not running */
1290
1291 ri->cache_info.routerlist_index = -1;
1292 smartlist_del(rl->routers, idx);
1293 if (idx < smartlist_len(rl->routers)) {
1294 routerinfo_t *r = smartlist_get(rl->routers, idx);
1295 r->cache_info.routerlist_index = idx;
1296 }
1297
1298 ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
1300 tor_assert(ri_tmp == ri);
1301
1302 if (make_old && should_cache_old_descriptors() &&
1306 smartlist_add(rl->old_routers, sd);
1307 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1308 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1310 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1311 } else {
1312 signed_descriptor_t *sd_tmp;
1313 sd_tmp = sdmap_remove(rl->desc_digest_map,
1314 ri->cache_info.signed_descriptor_digest);
1315 tor_assert(sd_tmp == &(ri->cache_info));
1316 rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
1317 ei_tmp = eimap_remove(rl->extra_info_map,
1318 ri->cache_info.extra_info_digest);
1319 if (ei_tmp) {
1321 ei_tmp->cache_info.signed_descriptor_len;
1322 extrainfo_free(ei_tmp);
1323 }
1324 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1325 sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
1326 routerinfo_free(ri);
1327 }
1328#ifdef DEBUG_ROUTERLIST
1330#endif
1331}
1332
1333/** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>->old_routers, and
1334 * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
1335 * <b>sd</b>. */
1336static void
1338{
1339 signed_descriptor_t *sd_tmp;
1340 extrainfo_t *ei_tmp;
1341 desc_store_t *store;
1342 if (idx == -1) {
1343 idx = sd->routerlist_index;
1344 }
1345 tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
1346 /* XXXX edmanm's bridge relay triggered the following assert while
1347 * running 0.2.0.12-alpha. If anybody triggers this again, see if we
1348 * can get a backtrace. */
1349 tor_assert(smartlist_get(rl->old_routers, idx) == sd);
1350 tor_assert(idx == sd->routerlist_index);
1351
1352 sd->routerlist_index = -1;
1353 smartlist_del(rl->old_routers, idx);
1354 if (idx < smartlist_len(rl->old_routers)) {
1355 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1356 d->routerlist_index = idx;
1357 }
1358 sd_tmp = sdmap_remove(rl->desc_digest_map,
1360 tor_assert(sd_tmp == sd);
1361 store = desc_get_store(rl, sd);
1362 if (store)
1364
1365 ei_tmp = eimap_remove(rl->extra_info_map,
1366 sd->extra_info_digest);
1367 if (ei_tmp) {
1369 ei_tmp->cache_info.signed_descriptor_len;
1370 extrainfo_free(ei_tmp);
1371 }
1373 sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
1374
1375 signed_descriptor_free(sd);
1376#ifdef DEBUG_ROUTERLIST
1378#endif
1379}
1380
1381/** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
1382 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
1383 * smartlist_get(rl-&gt;routers, idx) == ri, we don't need to do a linear
1384 * search over the list to decide which to remove. We put ri_new in the same
1385 * index as ri_old, if possible. ri is freed as appropriate.
1386 *
1387 * If should_cache_descriptors() is true, instead of deleting the router,
1388 * we add it to rl-&gt;old_routers. */
1389static void
1391 routerinfo_t *ri_new)
1392{
1393 int idx;
1394 int same_descriptors;
1395
1396 routerinfo_t *ri_tmp;
1397 extrainfo_t *ei_tmp;
1398 {
1399 const routerinfo_t *ri_generated = router_get_my_routerinfo();
1400 tor_assert(ri_generated != ri_new);
1401 }
1402 tor_assert(ri_old != ri_new);
1403 tor_assert(ri_new->cache_info.routerlist_index == -1);
1404
1405 idx = ri_old->cache_info.routerlist_index;
1406 tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1407 tor_assert(smartlist_get(rl->routers, idx) == ri_old);
1408
1409 {
1410 routerinfo_t *ri_old_tmp=NULL;
1411 nodelist_set_routerinfo(ri_new, &ri_old_tmp);
1412 tor_assert(ri_old == ri_old_tmp);
1413 }
1414
1416 if (idx >= 0) {
1417 smartlist_set(rl->routers, idx, ri_new);
1418 ri_old->cache_info.routerlist_index = -1;
1419 ri_new->cache_info.routerlist_index = idx;
1420 /* Check that ri_old is not in rl->routers anymore: */
1421 tor_assert( routerlist_find_elt_(rl->routers, ri_old, -1) == -1 );
1422 } else {
1423 log_warn(LD_BUG, "Appending entry from routerlist_replace.");
1424 routerlist_insert(rl, ri_new);
1425 return;
1426 }
1427 if (tor_memneq(ri_old->cache_info.identity_digest,
1428 ri_new->cache_info.identity_digest, DIGEST_LEN)) {
1429 /* digests don't match; digestmap_set won't replace */
1430 rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
1431 }
1432 ri_tmp = rimap_set(rl->identity_map,
1433 ri_new->cache_info.identity_digest, ri_new);
1434 tor_assert(!ri_tmp || ri_tmp == ri_old);
1435 sdmap_set(rl->desc_digest_map,
1436 ri_new->cache_info.signed_descriptor_digest,
1437 &(ri_new->cache_info));
1438
1439 if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
1440 sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
1441 &ri_new->cache_info);
1442 }
1443
1444 same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest,
1445 ri_new->cache_info.signed_descriptor_digest,
1446 DIGEST_LEN);
1447
1448 if (should_cache_old_descriptors() &&
1449 ri_old->purpose == ROUTER_PURPOSE_GENERAL &&
1450 !same_descriptors) {
1451 /* ri_old is going to become a signed_descriptor_t and go into
1452 * old_routers */
1454 smartlist_add(rl->old_routers, sd);
1455 sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1456 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1458 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1459 } else {
1460 /* We're dropping ri_old. */
1461 if (!same_descriptors) {
1462 /* digests don't match; The sdmap_set above didn't replace */
1463 sdmap_remove(rl->desc_digest_map,
1464 ri_old->cache_info.signed_descriptor_digest);
1465
1466 if (tor_memneq(ri_old->cache_info.extra_info_digest,
1467 ri_new->cache_info.extra_info_digest, DIGEST_LEN)) {
1468 ei_tmp = eimap_remove(rl->extra_info_map,
1469 ri_old->cache_info.extra_info_digest);
1470 if (ei_tmp) {
1472 ei_tmp->cache_info.signed_descriptor_len;
1473 extrainfo_free(ei_tmp);
1474 }
1475 }
1476
1477 if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
1478 sdmap_remove(rl->desc_by_eid_map,
1479 ri_old->cache_info.extra_info_digest);
1480 }
1481 }
1482 rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
1483 routerinfo_free(ri_old);
1484 }
1485#ifdef DEBUG_ROUTERLIST
1487#endif
1488}
1489
1490/** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
1491 * it as a fresh routerinfo_t. */
1492static routerinfo_t *
1494{
1495 routerinfo_t *ri;
1496 const char *body;
1497
1499
1502 0, 1, NULL, NULL);
1503 if (!ri)
1504 return NULL;
1505 signed_descriptor_move(&ri->cache_info, sd);
1506
1507 routerlist_remove_old(rl, sd, -1);
1508
1509 return ri;
1510}
1511
1512/** Free all memory held by the routerlist module.
1513 * Note: Calling routerlist_free_all() should always be paired with
1514 * a call to nodelist_free_all(). These should only be called during
1515 * cleanup.
1516 */
1517void
1519{
1521 routerlist = NULL; // Prevent internals of routerlist_free() from using
1522 // routerlist.
1523 routerlist_free(rl);
1524 dirlist_free_all();
1525 if (warned_nicknames) {
1527 smartlist_free(warned_nicknames);
1528 warned_nicknames = NULL;
1529 }
1530 authcert_free_all();
1531}
1532
1533/** Forget that we have issued any router-related warnings, so that we'll
1534 * warn again if we see the same errors. */
1535void
1537{
1538 if (!warned_nicknames)
1541 smartlist_clear(warned_nicknames); /* now the list is empty. */
1542
1544}
1545
1546/** Return 1 if the signed descriptor of this router is older than
1547 * <b>seconds</b> seconds. Otherwise return 0. */
1548MOCK_IMPL(int,
1550{
1551 return router->cache_info.published_on < approx_time() - seconds;
1552}
1553
1554/** Add <b>router</b> to the routerlist, if we don't already have it. Replace
1555 * older entries (if any) with the same key.
1556 *
1557 * Note: Callers should not hold their pointers to <b>router</b> if this
1558 * function fails; <b>router</b> will either be inserted into the routerlist or
1559 * freed. Similarly, even if this call succeeds, they should not hold their
1560 * pointers to <b>router</b> after subsequent calls with other routerinfo's --
1561 * they might cause the original routerinfo to get freed.
1562 *
1563 * Returns the status for the operation. Might set *<b>msg</b> if it wants
1564 * the poster of the router to know something.
1565 *
1566 * If <b>from_cache</b>, this descriptor came from our disk cache. If
1567 * <b>from_fetch</b>, we received it in response to a request we made.
1568 * (If both are false, that means it was uploaded to us as an auth dir
1569 * server or via the controller.)
1570 *
1571 * This function should be called *after*
1572 * routers_update_status_from_consensus_networkstatus; subsequently, you
1573 * should call router_rebuild_store and routerlist_descriptors_added.
1574 */
1576router_add_to_routerlist(routerinfo_t *router, const char **msg,
1577 int from_cache, int from_fetch)
1578{
1579 const char *id_digest;
1580 const or_options_t *options = get_options();
1581 int authdir = authdir_mode_handles_descs(options, router->purpose);
1582 int authdir_believes_valid = 0;
1583 routerinfo_t *old_router;
1584 networkstatus_t *consensus =
1586 int in_consensus = 0;
1587
1588 tor_assert(msg);
1589
1590 if (!routerlist)
1592
1593 id_digest = router->cache_info.identity_digest;
1594
1595 old_router = router_get_mutable_by_digest(id_digest);
1596
1597 /* Make sure that it isn't expired. */
1598 if (router->cert_expiration_time < approx_time()) {
1599 routerinfo_free(router);
1600 *msg = "Some certs on this router are expired.";
1601 return ROUTER_CERTS_EXPIRED;
1602 }
1603
1604 /* Make sure that we haven't already got this exact descriptor. */
1605 if (sdmap_get(routerlist->desc_digest_map,
1606 router->cache_info.signed_descriptor_digest)) {
1607 /* If we have this descriptor already and the new descriptor is a bridge
1608 * descriptor, replace it. If we had a bridge descriptor before and the
1609 * new one is not a bridge descriptor, don't replace it. */
1610
1611 /* Only members of routerlist->identity_map can be bridges; we don't
1612 * put bridges in old_routers. */
1613 const int was_bridge = old_router &&
1614 old_router->purpose == ROUTER_PURPOSE_BRIDGE;
1615
1617 router->purpose == ROUTER_PURPOSE_BRIDGE &&
1618 !was_bridge) {
1619 log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
1620 "descriptor for router %s",
1621 router_describe(router));
1622 } else {
1623 if (router->purpose == ROUTER_PURPOSE_BRIDGE) {
1624 /* Even if we're not going to keep this descriptor, we need to
1625 * let the bridge descriptor fetch subsystem know that we
1626 * succeeded at getting it -- so we can adjust the retry schedule
1627 * to stop trying for a while. */
1628 learned_bridge_descriptor(router, from_cache, 0);
1629 }
1630 log_info(LD_DIR,
1631 "Dropping descriptor that we already have for router %s",
1632 router_describe(router));
1633 *msg = "Router descriptor was not new.";
1634 routerinfo_free(router);
1635 return ROUTER_IS_ALREADY_KNOWN;
1636 }
1637 }
1638
1639 if (authdir) {
1640 if (authdir_wants_to_reject_router(router, msg,
1641 !from_cache && !from_fetch,
1642 &authdir_believes_valid)) {
1643 tor_assert(*msg);
1644 routerinfo_free(router);
1645 return ROUTER_AUTHDIR_REJECTS;
1646 }
1647 } else if (from_fetch) {
1648 /* Only check the descriptor digest against the network statuses when
1649 * we are receiving in response to a fetch. */
1650
1651 if (!signed_desc_digest_is_recognized(&router->cache_info) &&
1653 /* We asked for it, so some networkstatus must have listed it when we
1654 * did. Save it if we're a cache in case somebody else asks for it. */
1655 log_info(LD_DIR,
1656 "Received a no-longer-recognized descriptor for router %s",
1657 router_describe(router));
1658 *msg = "Router descriptor is not referenced by any network-status.";
1659
1660 /* Only journal this desc if we want to keep old descriptors */
1661 if (!from_cache && should_cache_old_descriptors())
1662 signed_desc_append_to_journal(&router->cache_info,
1665 return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
1666 }
1667 }
1668
1669 /* We no longer need a router with this descriptor digest. */
1670 if (consensus) {
1672 consensus, id_digest);
1673 if (rs && tor_memeq(rs->descriptor_digest,
1674 router->cache_info.signed_descriptor_digest,
1675 DIGEST_LEN)) {
1676 in_consensus = 1;
1677 }
1678 }
1679
1680 if (router->purpose == ROUTER_PURPOSE_GENERAL &&
1681 consensus && !in_consensus && !authdir) {
1682 /* If it's a general router not listed in the consensus, then don't
1683 * consider replacing the latest router with it. */
1684 if (!from_cache && should_cache_old_descriptors())
1685 signed_desc_append_to_journal(&router->cache_info,
1688 *msg = "Skipping router descriptor: not in consensus.";
1689 return ROUTER_NOT_IN_CONSENSUS;
1690 }
1691
1692 /* If we're reading a bridge descriptor from our cache, and we don't
1693 * recognize it as one of our currently configured bridges, drop the
1694 * descriptor. Otherwise we could end up using it as one of our entry
1695 * guards even if it isn't in our Bridge config lines. */
1696 if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
1697 !authdir_mode_bridge(options) &&
1699 log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
1700 "no bridge configured at that address.",
1701 safe_str_client(router_describe(router)));
1702 *msg = "Router descriptor was not a configured bridge.";
1703 routerinfo_free(router);
1704 return ROUTER_WAS_NOT_WANTED;
1705 }
1706
1707 /* If we have a router with the same identity key, choose the newer one. */
1708 if (old_router) {
1709 if (!in_consensus && (router->cache_info.published_on <=
1710 old_router->cache_info.published_on)) {
1711 /* Same key, but old. This one is not listed in the consensus. */
1712 log_debug(LD_DIR, "Not-new descriptor for router %s",
1713 router_describe(router));
1714 /* Only journal this desc if we'll be serving it. */
1715 if (!from_cache && should_cache_old_descriptors())
1716 signed_desc_append_to_journal(&router->cache_info,
1719 *msg = "Router descriptor was not new.";
1720 return ROUTER_IS_ALREADY_KNOWN;
1721 } else {
1722 /* Same key, and either new, or listed in the consensus. */
1723 log_debug(LD_DIR, "Replacing entry for router %s",
1724 router_describe(router));
1725 routerlist_replace(routerlist, old_router, router);
1726 if (!from_cache) {
1727 signed_desc_append_to_journal(&router->cache_info,
1729 }
1730 *msg = authdir_believes_valid ? "Valid server updated" :
1731 ("Invalid server updated. (This dirserver is marking your "
1732 "server as unapproved.)");
1733 return ROUTER_ADDED_SUCCESSFULLY;
1734 }
1735 }
1736
1737 if (!in_consensus && from_cache &&
1739 *msg = "Router descriptor was really old.";
1740 routerinfo_free(router);
1741 return ROUTER_WAS_TOO_OLD;
1742 }
1743
1744 /* We haven't seen a router with this identity before. Add it to the end of
1745 * the list. */
1747 if (!from_cache) {
1748 signed_desc_append_to_journal(&router->cache_info,
1750 }
1751 return ROUTER_ADDED_SUCCESSFULLY;
1752}
1753
1754/** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
1755 * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
1756 * we actually inserted it, ROUTER_BAD_EI otherwise.
1757 */
1760 int from_cache, int from_fetch)
1761{
1762 was_router_added_t inserted;
1763 (void)from_fetch;
1764 if (msg) *msg = NULL;
1765 /*XXXX Do something with msg */
1766
1767 inserted = extrainfo_insert(router_get_routerlist(), ei, !from_cache);
1768
1769 if (WRA_WAS_ADDED(inserted) && !from_cache)
1770 signed_desc_append_to_journal(&ei->cache_info,
1772
1773 return inserted;
1774}
1775
1776/** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
1777 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
1778 * to, or later than that of *<b>b</b>. */
1779static int
1780compare_old_routers_by_identity_(const void **_a, const void **_b)
1781{
1782 int i;
1783 const signed_descriptor_t *r1 = *_a, *r2 = *_b;
1784 if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
1785 return i;
1786 return (int)(r1->published_on - r2->published_on);
1787}
1788
1789/** Internal type used to represent how long an old descriptor was valid,
1790 * where it appeared in the list of old descriptors, and whether it's extra
1791 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
1793 int duration;
1794 int idx;
1795 int old;
1796};
1797
1798/** Sorting helper: compare two duration_idx_t by their duration. */
1799static int
1800compare_duration_idx_(const void *_d1, const void *_d2)
1801{
1802 const struct duration_idx_t *d1 = _d1;
1803 const struct duration_idx_t *d2 = _d2;
1804 return d1->duration - d2->duration;
1805}
1806
1807/** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
1808 * must contain routerinfo_t with the same identity and with publication time
1809 * in ascending order. Remove members from this range until there are no more
1810 * than max_descriptors_per_router() remaining. Start by removing the oldest
1811 * members from before <b>cutoff</b>, then remove members which were current
1812 * for the lowest amount of time. The order of members of old_routers at
1813 * indices <b>lo</b> or higher may be changed.
1814 */
1815static void
1817 time_t cutoff, int lo, int hi,
1818 digestset_t *retain)
1819{
1820 int i, n = hi-lo+1;
1821 unsigned n_extra, n_rmv = 0;
1822 struct duration_idx_t *lifespans;
1823 uint8_t *rmv, *must_keep;
1825#if 1
1826 const char *ident;
1827 tor_assert(hi < smartlist_len(lst));
1828 tor_assert(lo <= hi);
1829 ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
1830 for (i = lo+1; i <= hi; ++i) {
1831 signed_descriptor_t *r = smartlist_get(lst, i);
1833 }
1834#endif /* 1 */
1835 /* Check whether we need to do anything at all. */
1836 {
1837 int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
1838 if (n <= mdpr)
1839 return;
1840 n_extra = n - mdpr;
1841 }
1842
1843 lifespans = tor_calloc(n, sizeof(struct duration_idx_t));
1844 rmv = tor_calloc(n, sizeof(uint8_t));
1845 must_keep = tor_calloc(n, sizeof(uint8_t));
1846 /* Set lifespans to contain the lifespan and index of each server. */
1847 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
1848 for (i = lo; i <= hi; ++i) {
1849 signed_descriptor_t *r = smartlist_get(lst, i);
1850 signed_descriptor_t *r_next;
1851 lifespans[i-lo].idx = i;
1852 if (r->last_listed_as_valid_until >= now ||
1853 (retain && digestset_probably_contains(retain,
1855 must_keep[i-lo] = 1;
1856 }
1857 if (i < hi) {
1858 r_next = smartlist_get(lst, i+1);
1859 tor_assert(r->published_on <= r_next->published_on);
1860 lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
1861 } else {
1862 r_next = NULL;
1863 lifespans[i-lo].duration = INT_MAX;
1864 }
1865 if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
1866 ++n_rmv;
1867 lifespans[i-lo].old = 1;
1868 rmv[i-lo] = 1;
1869 }
1870 }
1871
1872 if (n_rmv < n_extra) {
1873 /**
1874 * We aren't removing enough servers for being old. Sort lifespans by
1875 * the duration of liveness, and remove the ones we're not already going to
1876 * remove based on how long they were alive.
1877 **/
1878 qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_);
1879 for (i = 0; i < n && n_rmv < n_extra; ++i) {
1880 if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
1881 rmv[lifespans[i].idx-lo] = 1;
1882 ++n_rmv;
1883 }
1884 }
1885 }
1886
1887 i = hi;
1888 do {
1889 if (rmv[i-lo])
1890 routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
1891 } while (--i >= lo);
1892 tor_free(must_keep);
1893 tor_free(rmv);
1894 tor_free(lifespans);
1895}
1896
1897/** Deactivate any routers from the routerlist that are more than
1898 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
1899 * remove old routers from the list of cached routers if we have too many.
1900 */
1901void
1903{
1904 int i, hi=-1;
1905 const char *cur_id = NULL;
1906 time_t now = time(NULL);
1907 time_t cutoff;
1908 routerinfo_t *router;
1910 digestset_t *retain;
1912
1914
1915 if (!routerlist || !consensus)
1916 return;
1917
1918 // routerlist_assert_ok(routerlist);
1919
1920 /* We need to guess how many router descriptors we will wind up wanting to
1921 retain, so that we can be sure to allocate a large enough Bloom filter
1922 to hold the digest set. Overestimating is fine; underestimating is bad.
1923 */
1924 {
1925 /* We'll probably retain everything in the consensus. */
1926 int n_max_retain = smartlist_len(consensus->routerstatus_list);
1927 retain = digestset_new(n_max_retain);
1928 }
1929
1930 /* Retain anything listed in the consensus. */
1931 if (consensus) {
1933 digestset_add(retain, rs->descriptor_digest));
1934 }
1935
1936 /* If we have a consensus, we should consider pruning current routers that
1937 * are too old and that nobody recommends. (If we don't have a consensus,
1938 * then we should get one before we decide to kill routers.) */
1939
1940 if (consensus) {
1941 cutoff = now - ROUTER_MAX_AGE;
1942 /* Remove too-old unrecommended members of routerlist->routers. */
1943 for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
1944 router = smartlist_get(routerlist->routers, i);
1945 if (router->cache_info.published_on <= cutoff &&
1946 router->cache_info.last_listed_as_valid_until < now &&
1948 router->cache_info.signed_descriptor_digest)) {
1949 /* Too old: remove it. (If we're a cache, just move it into
1950 * old_routers.) */
1951 log_info(LD_DIR,
1952 "Forgetting obsolete (too old) routerinfo for router %s",
1953 router_describe(router));
1954 routerlist_remove(routerlist, router, 1, now);
1955 i--;
1956 }
1957 }
1958 }
1959
1960 //routerlist_assert_ok(routerlist);
1961
1962 /* Remove far-too-old members of routerlist->old_routers. */
1963 cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
1964 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1965 sd = smartlist_get(routerlist->old_routers, i);
1966 if (sd->published_on <= cutoff &&
1967 sd->last_listed_as_valid_until < now &&
1969 /* Too old. Remove it. */
1971 }
1972 }
1973
1974 //routerlist_assert_ok(routerlist);
1975
1976 log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
1977 smartlist_len(routerlist->routers),
1978 smartlist_len(routerlist->old_routers));
1979
1980 /* Now we might have to look at routerlist->old_routers for extraneous
1981 * members. (We'd keep all the members if we could, but we need to save
1982 * space.) First, check whether we have too many router descriptors, total.
1983 * We're okay with having too many for some given router, so long as the
1984 * total number doesn't approach max_descriptors_per_router()*len(router).
1985 */
1986 if (smartlist_len(routerlist->old_routers) <
1987 smartlist_len(routerlist->routers))
1988 goto done;
1989
1990 /* Sort by identity, then fix indices. */
1992 /* Fix indices. */
1993 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1994 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
1995 r->routerlist_index = i;
1996 }
1997
1998 /* Iterate through the list from back to front, so when we remove descriptors
1999 * we don't mess up groups we haven't gotten to. */
2000 for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
2001 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
2002 if (!cur_id) {
2003 cur_id = r->identity_digest;
2004 hi = i;
2005 }
2006 if (tor_memneq(cur_id, r->identity_digest, DIGEST_LEN)) {
2008 cutoff, i+1, hi, retain);
2009 cur_id = r->identity_digest;
2010 hi = i;
2011 }
2012 }
2013 if (hi>=0)
2014 routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
2015 //routerlist_assert_ok(routerlist);
2016
2017 done:
2018 digestset_free(retain);
2019 router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
2020 router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
2021}
2022
2023/* Drop every bridge descriptor in our routerlist. Used by the external
2024 * 'bridgestrap' tool to discard bridge descriptors so that it can then
2025 * do a clean reachability test. */
2026void
2027routerlist_drop_bridge_descriptors(void)
2028{
2029 routerinfo_t *router;
2030 int i;
2031
2032 if (!routerlist)
2033 return;
2034
2035 for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
2036 router = smartlist_get(routerlist->routers, i);
2037 if (router->purpose == ROUTER_PURPOSE_BRIDGE) {
2038 log_notice(LD_DIR,
2039 "Dropping existing bridge descriptor for %s",
2040 router_describe(router));
2041 routerlist_remove(routerlist, router, 0, time(NULL));
2042 i--;
2043 }
2044 }
2045}
2046
2047/** We just added a new set of descriptors. Take whatever extra steps
2048 * we need. */
2049void
2051{
2052 // XXXX use pubsub mechanism here.
2053
2054 tor_assert(sl);
2057 if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
2058 learned_bridge_descriptor(ri, from_cache, 1);
2059 if (ri->needs_retest_if_added) {
2060 ri->needs_retest_if_added = 0;
2062 }
2063 } SMARTLIST_FOREACH_END(ri);
2064}
2065
2066/**
2067 * Code to parse a single router descriptor and insert it into the
2068 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
2069 * descriptor was well-formed but could not be added; and 1 if the
2070 * descriptor was added.
2071 *
2072 * If we don't add it and <b>msg</b> is not NULL, then assign to
2073 * *<b>msg</b> a static string describing the reason for refusing the
2074 * descriptor.
2075 *
2076 * This is used only by the controller.
2077 */
2078int
2079router_load_single_router(const char *s, uint8_t purpose, int cache,
2080 const char **msg)
2081{
2082 routerinfo_t *ri;
2084 smartlist_t *lst;
2085 char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
2086 tor_assert(msg);
2087 *msg = NULL;
2088
2089 tor_snprintf(annotation_buf, sizeof(annotation_buf),
2090 "@source controller\n"
2091 "@purpose %s\n", router_purpose_to_string(purpose));
2092
2093 if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0,
2094 annotation_buf, NULL))) {
2095 log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
2096 *msg = "Couldn't parse router descriptor.";
2097 return -1;
2098 }
2099 tor_assert(ri->purpose == purpose);
2100 if (router_is_me(ri)) {
2101 log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
2102 *msg = "Router's identity key matches mine.";
2103 routerinfo_free(ri);
2104 return 0;
2105 }
2106
2107 if (!cache) /* obey the preference of the controller */
2108 ri->cache_info.do_not_cache = 1;
2109
2110 lst = smartlist_new();
2111 smartlist_add(lst, ri);
2113
2114 r = router_add_to_routerlist(ri, msg, 0, 0);
2115 if (!WRA_WAS_ADDED(r)) {
2116 /* we've already assigned to *msg now, and ri is already freed */
2117 tor_assert(*msg);
2118 if (r == ROUTER_AUTHDIR_REJECTS)
2119 log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
2120 smartlist_free(lst);
2121 return 0;
2122 } else {
2124 smartlist_free(lst);
2125 log_debug(LD_DIR, "Added router to list");
2126 return 1;
2127 }
2128}
2129
2130/** Given a string <b>s</b> containing some routerdescs, parse it and put the
2131 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
2132 * are in response to a query to the network: cache them by adding them to
2133 * the journal.
2134 *
2135 * Return the number of routers actually added.
2136 *
2137 * If <b>requested_fingerprints</b> is provided, it must contain a list of
2138 * uppercased fingerprints. Do not update any router whose
2139 * fingerprint is not on the list; after updating a router, remove its
2140 * fingerprint from the list.
2141 *
2142 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
2143 * are descriptor digests. Otherwise they are identity digests.
2144 */
2145int
2146router_load_routers_from_string(const char *s, const char *eos,
2147 saved_location_t saved_location,
2148 smartlist_t *requested_fingerprints,
2149 int descriptor_digests,
2150 const char *prepend_annotations)
2151{
2152 smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
2153 char fp[HEX_DIGEST_LEN+1];
2154 const char *msg;
2155 int from_cache = (saved_location != SAVED_NOWHERE);
2156 int allow_annotations = (saved_location != SAVED_NOWHERE);
2157 int any_changed = 0;
2158 smartlist_t *invalid_digests = smartlist_new();
2159
2160 router_parse_list_from_string(&s, eos, routers, saved_location, 0,
2161 allow_annotations, prepend_annotations,
2162 invalid_digests);
2163
2165
2166 log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
2167
2168 SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
2170 char d[DIGEST_LEN];
2171 if (requested_fingerprints) {
2172 base16_encode(fp, sizeof(fp), descriptor_digests ?
2173 ri->cache_info.signed_descriptor_digest :
2174 ri->cache_info.identity_digest,
2175 DIGEST_LEN);
2176 if (smartlist_contains_string(requested_fingerprints, fp)) {
2177 smartlist_string_remove(requested_fingerprints, fp);
2178 } else {
2179 char *requested =
2180 smartlist_join_strings(requested_fingerprints," ",0,NULL);
2181 log_warn(LD_DIR,
2182 "We received a router descriptor with a fingerprint (%s) "
2183 "that we never requested. (We asked for: %s.) Dropping.",
2184 fp, requested);
2185 tor_free(requested);
2186 routerinfo_free(ri);
2187 continue;
2188 }
2189 }
2190
2191 memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
2192 r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
2193 if (WRA_WAS_ADDED(r)) {
2194 any_changed++;
2195 smartlist_add(changed, ri);
2196 routerlist_descriptors_added(changed, from_cache);
2197 smartlist_clear(changed);
2198 } else if (WRA_NEVER_DOWNLOADABLE(r)) {
2199 download_status_t *dl_status;
2201 if (dl_status) {
2202 log_info(LD_GENERAL, "Marking router %s as never downloadable",
2203 hex_str(d, DIGEST_LEN));
2205 }
2206 }
2207 } SMARTLIST_FOREACH_END(ri);
2208
2209 SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2210 /* This digest is never going to be parseable. */
2211 base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2212 if (requested_fingerprints && descriptor_digests) {
2213 if (! smartlist_contains_string(requested_fingerprints, fp)) {
2214 /* But we didn't ask for it, so we should assume shennanegans. */
2215 continue;
2216 }
2217 smartlist_string_remove(requested_fingerprints, fp);
2218 }
2219 download_status_t *dls;
2220 dls = router_get_dl_status_by_descriptor_digest((char*)bad_digest);
2221 if (dls) {
2222 log_info(LD_GENERAL, "Marking router with descriptor %s as unparseable, "
2223 "and therefore undownloadable", fp);
2225 }
2226 } SMARTLIST_FOREACH_END(bad_digest);
2227 SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2228 smartlist_free(invalid_digests);
2229
2231
2232 if (any_changed)
2234
2235 smartlist_free(routers);
2236 smartlist_free(changed);
2237
2238 return any_changed;
2239}
2240
2241/** Parse one or more extrainfos from <b>s</b> (ending immediately before
2242 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
2243 * router_load_routers_from_string(). */
2244void
2245router_load_extrainfo_from_string(const char *s, const char *eos,
2246 saved_location_t saved_location,
2247 smartlist_t *requested_fingerprints,
2248 int descriptor_digests)
2249{
2250 smartlist_t *extrainfo_list = smartlist_new();
2251 const char *msg;
2252 int from_cache = (saved_location != SAVED_NOWHERE);
2253 smartlist_t *invalid_digests = smartlist_new();
2254
2255 router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
2256 NULL, invalid_digests);
2257
2258 log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
2259
2260 SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) {
2261 uint8_t d[DIGEST_LEN];
2262 memcpy(d, ei->cache_info.signed_descriptor_digest, DIGEST_LEN);
2263 was_router_added_t added =
2264 router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
2265 if (WRA_WAS_ADDED(added) && requested_fingerprints) {
2266 char fp[HEX_DIGEST_LEN+1];
2267 base16_encode(fp, sizeof(fp), descriptor_digests ?
2268 ei->cache_info.signed_descriptor_digest :
2269 ei->cache_info.identity_digest,
2270 DIGEST_LEN);
2271 smartlist_string_remove(requested_fingerprints, fp);
2272 /* We silently let relays stuff us with extrainfos we didn't ask for,
2273 * so long as we would have wanted them anyway. Since we always fetch
2274 * all the extrainfos we want, and we never actually act on them
2275 * inside Tor, this should be harmless. */
2276 } else if (WRA_NEVER_DOWNLOADABLE(added)) {
2278 if (sd) {
2279 log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2280 "unparseable, and therefore undownloadable",
2281 hex_str((char*)d,DIGEST_LEN));
2283 }
2284 }
2285 } SMARTLIST_FOREACH_END(ei);
2286
2287 SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2288 /* This digest is never going to be parseable. */
2289 char fp[HEX_DIGEST_LEN+1];
2290 base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2291 if (requested_fingerprints) {
2292 if (! smartlist_contains_string(requested_fingerprints, fp)) {
2293 /* But we didn't ask for it, so we should assume shennanegans. */
2294 continue;
2295 }
2296 smartlist_string_remove(requested_fingerprints, fp);
2297 }
2299 router_get_by_extrainfo_digest((char*)bad_digest);
2300 if (sd) {
2301 log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2302 "unparseable, and therefore undownloadable", fp);
2304 }
2305 } SMARTLIST_FOREACH_END(bad_digest);
2306 SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2307 smartlist_free(invalid_digests);
2308
2310 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
2311
2312 smartlist_free(extrainfo_list);
2313}
2314
2315/** Return true iff the latest ns-flavored consensus includes a descriptor
2316 * whose digest is that of <b>desc</b>. */
2317static int
2319{
2320 const routerstatus_t *rs;
2322 FLAV_NS);
2323
2324 if (consensus) {
2325 rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
2326 if (rs && tor_memeq(rs->descriptor_digest,
2328 return 1;
2329 }
2330 return 0;
2331}
2332
2333/** Update downloads for router descriptors and/or microdescriptors as
2334 * appropriate. */
2335void
2337{
2339 return;
2342}
2343
2344/** Clear all our timeouts for fetching v3 directory stuff, and then
2345 * give it all a try again. */
2346void
2348{
2349 (void)now;
2350
2351 log_debug(LD_GENERAL,
2352 "In routerlist_retry_directory_downloads()");
2353
2357}
2358
2359/** Return true iff <b>router</b> does not permit exit streams.
2360 */
2361int
2363{
2364 return router->policy_is_reject_star;
2365}
2366
2367/** For every current directory connection whose purpose is <b>purpose</b>,
2368 * and where the resource being downloaded begins with <b>prefix</b>, split
2369 * rest of the resource into base16 fingerprints (or base64 fingerprints if
2370 * purpose==DIR_PURPOSE_FETCH_MICRODESC), decode them, and set the
2371 * corresponding elements of <b>result</b> to a nonzero value.
2372 */
2373void
2374list_pending_downloads(digestmap_t *result, digest256map_t *result256,
2375 int purpose, const char *prefix)
2376{
2377 const size_t p_len = strlen(prefix);
2378 smartlist_t *tmp = smartlist_new();
2380 int flags = DSR_HEX;
2381 if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
2382 flags = DSR_DIGEST256|DSR_BASE64;
2383
2384 tor_assert(result || result256);
2385
2386 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
2387 if (conn->type == CONN_TYPE_DIR &&
2388 conn->purpose == purpose &&
2389 !conn->marked_for_close) {
2390 const char *resource = TO_DIR_CONN(conn)->requested_resource;
2391 if (!strcmpstart(resource, prefix))
2392 dir_split_resource_into_fingerprints(resource + p_len,
2393 tmp, NULL, flags);
2394 }
2395 } SMARTLIST_FOREACH_END(conn);
2396
2397 if (result) {
2398 SMARTLIST_FOREACH(tmp, char *, d,
2399 {
2400 digestmap_set(result, d, (void*)1);
2401 tor_free(d);
2402 });
2403 } else if (result256) {
2404 SMARTLIST_FOREACH(tmp, uint8_t *, d,
2405 {
2406 digest256map_set(result256, d, (void*)1);
2407 tor_free(d);
2408 });
2409 }
2410 smartlist_free(tmp);
2411}
2412
2413/** For every router descriptor (or extra-info document if <b>extrainfo</b> is
2414 * true) we are currently downloading by descriptor digest, set result[d] to
2415 * (void*)1. */
2416static void
2417list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
2418{
2419 int purpose =
2421 list_pending_downloads(result, NULL, purpose, "d/");
2422}
2423
2424/** For every microdescriptor we are currently downloading by descriptor
2425 * digest, set result[d] to (void*)1.
2426 */
2427void
2429{
2431}
2432
2433/** Launch downloads for all the descriptors whose digests or digests256
2434 * are listed as digests[i] for lo <= i < hi. (Lo and hi may be out of
2435 * range.) If <b>source</b> is given, download from <b>source</b>;
2436 * otherwise, download from an appropriate random directory server.
2437 */
2438MOCK_IMPL(STATIC void,
2440 int purpose, smartlist_t *digests,
2441 int lo, int hi, int pds_flags))
2442{
2443 char *resource, *cp;
2444 int digest_len, enc_digest_len;
2445 const char *sep;
2446 int b64_256;
2447 smartlist_t *tmp;
2448
2449 if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2450 /* Microdescriptors are downloaded by "-"-separated base64-encoded
2451 * 256-bit digests. */
2452 digest_len = DIGEST256_LEN;
2453 enc_digest_len = BASE64_DIGEST256_LEN + 1;
2454 sep = "-";
2455 b64_256 = 1;
2456 } else {
2457 digest_len = DIGEST_LEN;
2458 enc_digest_len = HEX_DIGEST_LEN + 1;
2459 sep = "+";
2460 b64_256 = 0;
2461 }
2462
2463 if (lo < 0)
2464 lo = 0;
2465 if (hi > smartlist_len(digests))
2466 hi = smartlist_len(digests);
2467
2468 if (hi-lo <= 0)
2469 return;
2470
2471 tmp = smartlist_new();
2472
2473 for (; lo < hi; ++lo) {
2474 cp = tor_malloc(enc_digest_len);
2475 if (b64_256) {
2476 digest256_to_base64(cp, smartlist_get(digests, lo));
2477 } else {
2478 base16_encode(cp, enc_digest_len, smartlist_get(digests, lo),
2479 digest_len);
2480 }
2481 smartlist_add(tmp, cp);
2482 }
2483
2484 cp = smartlist_join_strings(tmp, sep, 0, NULL);
2485 tor_asprintf(&resource, "d/%s.z", cp);
2486
2487 SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1));
2488 smartlist_free(tmp);
2489 tor_free(cp);
2490
2491 if (source) {
2492 /* We know which authority or directory mirror we want. */
2495 directory_request_set_resource(req, resource);
2497 directory_request_free(req);
2498 } else {
2500 pds_flags, DL_WANT_ANY_DIRSERVER);
2501 }
2502 tor_free(resource);
2503}
2504
2505/** Return the max number of hashes to put in a URL for a given request.
2506 */
2507static int
2508max_dl_per_request(const or_options_t *options, int purpose)
2509{
2510 /* Since squid does not like URLs >= 4096 bytes we limit it to 96.
2511 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2512 * /tor/server/d/.z) == 4026
2513 * 4026/41 (40 for the hash and 1 for the + that separates them) => 98
2514 * So use 96 because it's a nice number.
2515 *
2516 * For microdescriptors, the calculation is
2517 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2518 * /tor/micro/d/.z) == 4027
2519 * 4027/44 (43 for the hash and 1 for the - that separates them) => 91
2520 * So use 90 because it's a nice number.
2521 */
2522 int max = 96;
2523 if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2524 max = 90;
2525 }
2526 /* If we're going to tunnel our connections, we can ask for a lot more
2527 * in a request. */
2528 if (dirclient_must_use_begindir(options)) {
2529 max = 500;
2530 }
2531 return max;
2532}
2533
2534/** Don't split our requests so finely that we are requesting fewer than
2535 * this number per server. (Grouping more than this at once leads to
2536 * diminishing returns.) */
2537#define MIN_DL_PER_REQUEST 32
2538/** To prevent a single screwy cache from confusing us by selective reply,
2539 * try to split our requests into at least this many requests. */
2540#define MIN_REQUESTS 3
2541/** If we want fewer than this many descriptors, wait until we
2542 * want more, or until TestingClientMaxIntervalWithoutRequest has passed. */
2543#define MAX_DL_TO_DELAY 16
2544
2545/** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of
2546 * router descriptor digests or microdescriptor digest256s in
2547 * <b>downloadable</b>, decide whether to delay fetching until we have more.
2548 * If we don't want to delay, launch one or more requests to the appropriate
2549 * directory authorities.
2550 */
2551void
2553 smartlist_t *downloadable,
2554 const routerstatus_t *source, time_t now)
2555{
2556 const or_options_t *options = get_options();
2557 const char *descname;
2558 const int fetch_microdesc = (purpose == DIR_PURPOSE_FETCH_MICRODESC);
2559 int n_downloadable = smartlist_len(downloadable);
2560
2561 int i, n_per_request, max_dl_per_req;
2562 const char *req_plural = "", *rtr_plural = "";
2563 int pds_flags = PDS_RETRY_IF_NO_SERVERS;
2564
2565 tor_assert(fetch_microdesc || purpose == DIR_PURPOSE_FETCH_SERVERDESC);
2566 descname = fetch_microdesc ? "microdesc" : "routerdesc";
2567
2568 if (!n_downloadable)
2569 return;
2570
2571 if (!dirclient_fetches_dir_info_early(options)) {
2572 if (n_downloadable >= MAX_DL_TO_DELAY) {
2573 log_debug(LD_DIR,
2574 "There are enough downloadable %ss to launch requests.",
2575 descname);
2576 } else if (! router_have_minimum_dir_info()) {
2577 log_debug(LD_DIR,
2578 "We are only missing %d %ss, but we'll fetch anyway, since "
2579 "we don't yet have enough directory info.",
2580 n_downloadable, descname);
2581 } else {
2582
2583 /* should delay */
2586 return;
2587
2589 log_info(LD_DIR,
2590 "There are not many downloadable %ss, but we've "
2591 "been waiting long enough (%d seconds). Downloading.",
2592 descname,
2594 } else {
2595 log_info(LD_DIR,
2596 "There are not many downloadable %ss, but we haven't "
2597 "tried downloading descriptors recently. Downloading.",
2598 descname);
2599 }
2600 }
2601 }
2602
2603 if (!authdir_mode(options)) {
2604 /* If we wind up going to the authorities, we want to only open one
2605 * connection to each authority at a time, so that we don't overload
2606 * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
2607 * regardless of whether we're a cache or not.
2608 *
2609 * Setting this flag can make initiate_descriptor_downloads() ignore
2610 * requests. We need to make sure that we do in fact call
2611 * update_router_descriptor_downloads() later on, once the connections
2612 * have succeeded or failed.
2613 */
2614 pds_flags |= fetch_microdesc ?
2617 }
2618
2619 n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
2620 max_dl_per_req = max_dl_per_request(options, purpose);
2621
2622 if (n_per_request > max_dl_per_req)
2623 n_per_request = max_dl_per_req;
2624
2625 if (n_per_request < MIN_DL_PER_REQUEST) {
2626 n_per_request = MIN(MIN_DL_PER_REQUEST, n_downloadable);
2627 }
2628
2629 if (n_downloadable > n_per_request)
2630 req_plural = rtr_plural = "s";
2631 else if (n_downloadable > 1)
2632 rtr_plural = "s";
2633
2634 log_info(LD_DIR,
2635 "Launching %d request%s for %d %s%s, %d at a time",
2636 CEIL_DIV(n_downloadable, n_per_request), req_plural,
2637 n_downloadable, descname, rtr_plural, n_per_request);
2638 smartlist_sort_digests(downloadable);
2639 for (i=0; i < n_downloadable; i += n_per_request) {
2640 initiate_descriptor_downloads(source, purpose,
2641 downloadable, i, i+n_per_request,
2642 pds_flags);
2643 }
2645}
2646
2647/** For any descriptor that we want that's currently listed in
2648 * <b>consensus</b>, download it as appropriate. */
2649void
2651 networkstatus_t *consensus)
2652{
2653 const or_options_t *options = get_options();
2654 digestmap_t *map = NULL;
2655 smartlist_t *no_longer_old = smartlist_new();
2656 smartlist_t *downloadable = smartlist_new();
2657 const routerstatus_t *source = NULL;
2658 int authdir = authdir_mode(options);
2659 int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
2660 n_inprogress=0, n_in_oldrouters=0;
2661
2663 goto done;
2664 if (!consensus)
2665 goto done;
2666
2667 if (is_vote) {
2668 /* where's it from, so we know whom to ask for descriptors */
2669 dir_server_t *ds;
2670 networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
2671 tor_assert(voter);
2673 if (ds) {
2675 if (!source) {
2676 /* prefer to use the address in the consensus, but fall back to
2677 * the hard-coded trusted_dir_server address if we don't have a
2678 * consensus or this digest isn't in our consensus. */
2679 source = &ds->fake_status;
2680 }
2681 } else {
2682 log_warn(LD_DIR, "couldn't lookup source from vote?");
2683 }
2684 }
2685
2686 map = digestmap_new();
2688 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) {
2689 routerstatus_t *rs;
2691 if (is_vote) {
2692 rs = &(((vote_routerstatus_t *)rsp)->status);
2693 vrs = rsp;
2694 } else {
2695 rs = rsp;
2696 vrs = NULL;
2697 }
2700 const routerinfo_t *ri;
2701 ++n_have;
2702 if (!(ri = router_get_by_id_digest(rs->identity_digest)) ||
2703 tor_memneq(ri->cache_info.signed_descriptor_digest,
2705 /* We have a descriptor with this digest, but either there is no
2706 * entry in routerlist with the same ID (!ri), or there is one,
2707 * but the identity digest differs (memneq).
2708 */
2709 smartlist_add(no_longer_old, sd);
2710 ++n_in_oldrouters; /* We have it in old_routers. */
2711 }
2712 continue; /* We have it already. */
2713 }
2714 if (digestmap_get(map, rs->descriptor_digest)) {
2715 ++n_inprogress;
2716 continue; /* We have an in-progress download. */
2717 }
2718 if (!download_status_is_ready(&rs->dl_status, now)) {
2719 ++n_delayed; /* Not ready for retry. */
2720 continue;
2721 }
2722 if (authdir && is_vote && dirserv_would_reject_router(rs, vrs)) {
2723 ++n_would_reject;
2724 continue; /* We would throw it out immediately. */
2725 }
2726 if (!we_want_to_fetch_flavor(options, consensus->flavor) &&
2727 !client_would_use_router(rs, now)) {
2728 ++n_wouldnt_use;
2729 continue; /* We would never use it ourself. */
2730 }
2731 if (is_vote && source) {
2732 char old_digest_buf[HEX_DIGEST_LEN+1];
2733 const char *old_digest = "none";
2734 const routerinfo_t *oldrouter;
2736 if (oldrouter) {
2737 base16_encode(old_digest_buf, sizeof(old_digest_buf),
2738 oldrouter->cache_info.signed_descriptor_digest,
2739 DIGEST_LEN);
2740 old_digest = old_digest_buf;
2741 }
2742 log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
2745 old_digest,
2746 source->nickname, oldrouter ? "known" : "unknown");
2747 }
2748 smartlist_add(downloadable, rs->descriptor_digest);
2749 } SMARTLIST_FOREACH_END(rsp);
2750
2751 if (!authdir_mode_v3(options)
2752 && smartlist_len(no_longer_old)) {
2754 log_info(LD_DIR, "%d router descriptors listed in consensus are "
2755 "currently in old_routers; making them current.",
2756 smartlist_len(no_longer_old));
2757 SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) {
2758 const char *msg;
2760 time_t tmp_cert_expiration_time;
2762 if (!ri) {
2763 log_warn(LD_BUG, "Failed to re-parse a router.");
2764 continue;
2765 }
2766 /* need to remember for below, since add_to_routerlist may free. */
2767 tmp_cert_expiration_time = ri->cert_expiration_time;
2768
2769 r = router_add_to_routerlist(ri, &msg, 1, 0);
2770 if (WRA_WAS_OUTDATED(r)) {
2771 log_warn(LD_DIR, "Couldn't add re-parsed router: %s. This isn't "
2772 "usually a big deal, but you should make sure that your "
2773 "clock and timezone are set correctly.",
2774 msg?msg:"???");
2775 if (r == ROUTER_CERTS_EXPIRED) {
2776 char time_cons[ISO_TIME_LEN+1];
2777 char time_cert_expires[ISO_TIME_LEN+1];
2778 format_iso_time(time_cons, consensus->valid_after);
2779 format_iso_time(time_cert_expires, tmp_cert_expiration_time);
2780 log_warn(LD_DIR, " (I'm looking at a consensus from %s; This "
2781 "router's certificates began expiring at %s.)",
2782 time_cons, time_cert_expires);
2783 }
2784 }
2785 } SMARTLIST_FOREACH_END(sd);
2787 }
2788
2789 log_info(LD_DIR,
2790 "%d router descriptors downloadable. %d delayed; %d present "
2791 "(%d of those were in old_routers); %d would_reject; "
2792 "%d wouldnt_use; %d in progress.",
2793 smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
2794 n_would_reject, n_wouldnt_use, n_inprogress);
2795
2797 downloadable, source, now);
2798
2799 digestmap_free(map, NULL);
2800 done:
2801 smartlist_free(downloadable);
2802 smartlist_free(no_longer_old);
2803}
2804
2805/** Launch downloads for router status as needed. */
2806void
2808{
2809 const or_options_t *options = get_options();
2810 if (should_delay_dir_fetches(options, NULL))
2811 return;
2812 if (!we_fetch_router_descriptors(options))
2813 return;
2814
2817}
2818
2819/** Launch extrainfo downloads as needed. */
2820void
2822{
2823 const or_options_t *options = get_options();
2824 routerlist_t *rl;
2825 smartlist_t *wanted;
2826 digestmap_t *pending;
2827 int old_routers, i, max_dl_per_req;
2828 int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0, n_bogus[2] = {0,0};
2829 if (! options->DownloadExtraInfo)
2830 return;
2831 if (should_delay_dir_fetches(options, NULL))
2832 return;
2834 return;
2835
2836 pending = digestmap_new();
2838 rl = router_get_routerlist();
2839 wanted = smartlist_new();
2840 for (old_routers = 0; old_routers < 2; ++old_routers) {
2841 smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
2842 for (i = 0; i < smartlist_len(lst); ++i) {
2844 char *d;
2845 if (old_routers)
2846 sd = smartlist_get(lst, i);
2847 else
2848 sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
2849 if (sd->is_extrainfo)
2850 continue; /* This should never happen. */
2851 if (old_routers && !router_get_by_id_digest(sd->identity_digest))
2852 continue; /* Couldn't check the signature if we got it. */
2853 if (sd->extrainfo_is_bogus)
2854 continue;
2855 d = sd->extra_info_digest;
2856 if (tor_digest_is_zero(d)) {
2857 ++n_no_ei;
2858 continue;
2859 }
2860 if (eimap_get(rl->extra_info_map, d)) {
2861 ++n_have;
2862 continue;
2863 }
2864 if (!download_status_is_ready(&sd->ei_dl_status, now)) {
2865 ++n_delay;
2866 continue;
2867 }
2868 if (digestmap_get(pending, d)) {
2869 ++n_pending;
2870 continue;
2871 }
2872
2874 if (sd2 != sd) {
2875 if (sd2 != NULL) {
2876 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2877 char d3[HEX_DIGEST_LEN+1], d4[HEX_DIGEST_LEN+1];
2878 base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2879 base16_encode(d2, sizeof(d2), sd2->identity_digest, DIGEST_LEN);
2880 base16_encode(d3, sizeof(d3), d, DIGEST_LEN);
2881 base16_encode(d4, sizeof(d3), sd2->extra_info_digest, DIGEST_LEN);
2882
2883 log_info(LD_DIR, "Found an entry in %s with mismatched "
2884 "router_get_by_extrainfo_digest() value. This has ID %s "
2885 "but the entry in the map has ID %s. This has EI digest "
2886 "%s and the entry in the map has EI digest %s.",
2887 old_routers?"old_routers":"routers",
2888 d1, d2, d3, d4);
2889 } else {
2890 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2891 base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2892 base16_encode(d2, sizeof(d2), d, DIGEST_LEN);
2893
2894 log_info(LD_DIR, "Found an entry in %s with NULL "
2895 "router_get_by_extrainfo_digest() value. This has ID %s "
2896 "and EI digest %s.",
2897 old_routers?"old_routers":"routers",
2898 d1, d2);
2899 }
2900 ++n_bogus[old_routers];
2901 continue;
2902 }
2903 smartlist_add(wanted, d);
2904 }
2905 }
2906 digestmap_free(pending, NULL);
2907
2908 log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
2909 "with present ei, %d delaying, %d pending, %d downloadable, %d "
2910 "bogus in routers, %d bogus in old_routers",
2911 n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted),
2912 n_bogus[0], n_bogus[1]);
2913
2914 smartlist_shuffle(wanted);
2915
2916 max_dl_per_req = max_dl_per_request(options, DIR_PURPOSE_FETCH_EXTRAINFO);
2917 for (i = 0; i < smartlist_len(wanted); i += max_dl_per_req) {
2919 wanted, i, i+max_dl_per_req,
2921 }
2922
2923 smartlist_free(wanted);
2924}
2925
2926/** Reset the consensus and extra-info download failure count on all routers.
2927 * When we get a new consensus,
2928 * routers_update_status_from_consensus_networkstatus() will reset the
2929 * download statuses on the descriptors in that consensus.
2930 */
2931void
2933{
2934 log_debug(LD_GENERAL,
2935 "In router_reset_descriptor_download_failures()");
2936
2939 if (!routerlist)
2940 return;
2941 /* We want to download *all* extra-info descriptors, not just those in
2942 * the consensus we currently have (or are about to have) */
2944 {
2945 download_status_reset(&ri->cache_info.ei_dl_status);
2946 });
2948 {
2949 download_status_reset(&sd->ei_dl_status);
2950 });
2951}
2952
2953/** Any changes in a router descriptor's publication time larger than this are
2954 * automatically non-cosmetic. */
2955#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
2956
2957/** We allow uptime to vary from how much it ought to be by this much. */
2958#define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
2959
2960/** Return true iff the only differences between r1 and r2 are such that
2961 * would not cause a recent (post 0.1.1.6) dirserver to republish.
2962 */
2963int
2965{
2966 time_t r1pub, r2pub;
2967 time_t time_difference;
2968 tor_assert(r1 && r2);
2969
2970 /* r1 should be the one that was published first. */
2971 if (r1->cache_info.published_on > r2->cache_info.published_on) {
2972 const routerinfo_t *ri_tmp = r2;
2973 r2 = r1;
2974 r1 = ri_tmp;
2975 }
2976
2977 /* If any key fields differ, they're different. */
2978 if (!tor_addr_eq(&r1->ipv4_addr, &r2->ipv4_addr) ||
2979 strcasecmp(r1->nickname, r2->nickname) ||
2980 r1->ipv4_orport != r2->ipv4_orport ||
2981 !tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) ||
2982 r1->ipv6_orport != r2->ipv6_orport ||
2983 r1->ipv4_dirport != r2->ipv4_dirport ||
2984 r1->purpose != r2->purpose ||
2985 r1->onion_pkey_len != r2->onion_pkey_len ||
2988 strcasecmp(r1->platform, r2->platform) ||
2989 (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
2990 (!r1->contact_info && r2->contact_info) ||
2991 (r1->contact_info && r2->contact_info &&
2992 strcasecmp(r1->contact_info, r2->contact_info)) ||
2993 r1->is_hibernating != r2->is_hibernating ||
2997 return 0;
2998 if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
2999 return 0;
3000 if (r1->declared_family && r2->declared_family) {
3001 int i, n;
3002 if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
3003 return 0;
3004 n = smartlist_len(r1->declared_family);
3005 for (i=0; i < n; ++i) {
3006 if (strcasecmp(smartlist_get(r1->declared_family, i),
3007 smartlist_get(r2->declared_family, i)))
3008 return 0;
3009 }
3010 }
3011
3012 /* Did bandwidth change a lot? */
3013 if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
3015 return 0;
3016
3017 /* Did the bandwidthrate or bandwidthburst change? */
3018 if ((r1->bandwidthrate != r2->bandwidthrate) ||
3019 (r1->bandwidthburst != r2->bandwidthburst))
3020 return 0;
3021
3022 /* Has enough time passed between the publication times? */
3024 < r2->cache_info.published_on)
3025 return 0;
3026
3027 /* Did uptime fail to increase by approximately the amount we would think,
3028 * give or take some slop? */
3029 r1pub = r1->cache_info.published_on;
3030 r2pub = r2->cache_info.published_on;
3031 time_difference = r2->uptime - (r1->uptime + (r2pub - r1pub));
3032 if (time_difference < 0)
3033 time_difference = - time_difference;
3034 if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
3035 time_difference > r1->uptime * .05 &&
3036 time_difference > r2->uptime * .05)
3037 return 0;
3038
3039 /* Otherwise, the difference is cosmetic. */
3040 return 1;
3041}
3042
3043/** Check whether <b>sd</b> describes a router descriptor compatible with the
3044 * extrainfo document <b>ei</b>.
3045 *
3046 * <b>identity_pkey</b> (which must also be provided) is RSA1024 identity key
3047 * for the router. We use it to check the signature of the extrainfo document,
3048 * if it has not already been checked.
3049 *
3050 * If no router is compatible with <b>ei</b>, <b>ei</b> should be
3051 * dropped. Return 0 for "compatible", return 1 for "reject, and inform
3052 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
3053 * <b>msg</b> is present, set *<b>msg</b> to a description of the
3054 * incompatibility (if any).
3055 *
3056 * Set the extrainfo_is_bogus field in <b>sd</b> if the digests matched
3057 * but the extrainfo was nonetheless incompatible.
3058 **/
3059int
3061 extrainfo_t *ei,
3063 const char **msg)
3064{
3065 int digest_matches, digest256_matches, r=1;
3066 tor_assert(identity_pkey);
3067 tor_assert(sd);
3068 tor_assert(ei);
3069
3070 if (ei->bad_sig) {
3071 if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
3072 return 1;
3073 }
3074
3075 digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest,
3077 /* Set digest256_matches to 1 if the digest is correct, or if no
3078 * digest256 was in the ri. */
3079 digest256_matches = tor_memeq(ei->digest256,
3081 digest256_matches |=
3083
3084 /* The identity must match exactly to have been generated at the same time
3085 * by the same router. */
3087 ei->cache_info.identity_digest,
3088 DIGEST_LEN)) {
3089 if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
3090 goto err; /* different servers */
3091 }
3092
3094 ei->cache_info.signing_key_cert)) {
3095 if (msg) *msg = "Extrainfo signing key cert didn't match routerinfo";
3096 goto err; /* different servers */
3097 }
3098
3099 if (ei->pending_sig) {
3100 char signed_digest[128];
3101 if (crypto_pk_public_checksig(identity_pkey,
3102 signed_digest, sizeof(signed_digest),
3103 ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
3104 tor_memneq(signed_digest, ei->cache_info.signed_descriptor_digest,
3105 DIGEST_LEN)) {
3106 ei->bad_sig = 1;
3107 tor_free(ei->pending_sig);
3108 if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
3109 goto err; /* Bad signature, or no match. */
3110 }
3111
3112 ei->cache_info.send_unencrypted = sd->send_unencrypted;
3113 tor_free(ei->pending_sig);
3114 }
3115
3116 if (ei->cache_info.published_on < sd->published_on) {
3117 if (msg) *msg = "Extrainfo published time did not match routerdesc";
3118 goto err;
3119 } else if (ei->cache_info.published_on > sd->published_on) {
3120 if (msg) *msg = "Extrainfo published time did not match routerdesc";
3121 r = -1;
3122 goto err;
3123 }
3124
3125 if (!digest256_matches && !digest_matches) {
3126 if (msg) *msg = "Neither digest256 or digest matched "
3127 "digest from routerdesc";
3128 goto err;
3129 }
3130
3131 if (!digest256_matches) {
3132 if (msg) *msg = "Extrainfo digest did not match digest256 from routerdesc";
3133 goto err; /* Digest doesn't match declared value. */
3134 }
3135
3136 if (!digest_matches) {
3137 if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
3138 goto err; /* Digest doesn't match declared value. */
3139 }
3140
3141 return 0;
3142 err:
3143 if (digest_matches) {
3144 /* This signature was okay, and the digest was right: This is indeed the
3145 * corresponding extrainfo. But insanely, it doesn't match the routerinfo
3146 * that lists it. Don't try to fetch this one again. */
3147 sd->extrainfo_is_bogus = 1;
3148 }
3149
3150 return r;
3151}
3152
3153/* Does ri have a valid ntor onion key?
3154 * Valid ntor onion keys exist and have at least one non-zero byte. */
3155int
3156routerinfo_has_curve25519_onion_key(const routerinfo_t *ri)
3157{
3158 if (!ri) {
3159 return 0;
3160 }
3161
3162 if (!ri->onion_curve25519_pkey) {
3163 return 0;
3164 }
3165
3166 if (fast_mem_is_zero((const char*)ri->onion_curve25519_pkey->public_key,
3168 return 0;
3169 }
3170
3171 return 1;
3172}
3173
3174/* Is rs running a tor version known to support EXTEND2 cells?
3175 * If allow_unknown_versions is true, return true if we can't tell
3176 * (from a versions line or a protocols line) whether it supports extend2
3177 * cells.
3178 * Otherwise, return false if the version is unknown. */
3179int
3180routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
3181 int allow_unknown_versions)
3182{
3183 if (!rs) {
3184 return allow_unknown_versions;
3185 }
3186
3187 if (!rs->pv.protocols_known) {
3188 return allow_unknown_versions;
3189 }
3190
3191 return rs->pv.supports_extend2_cells;
3192}
3193
3194/** Assert that the internal representation of <b>rl</b> is
3195 * self-consistent. */
3196void
3198{
3199 routerinfo_t *r2;
3201 if (!rl)
3202 return;
3204 r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
3205 tor_assert(r == r2);
3206 sd2 = sdmap_get(rl->desc_digest_map,
3207 r->cache_info.signed_descriptor_digest);
3208 tor_assert(&(r->cache_info) == sd2);
3209 tor_assert(r->cache_info.routerlist_index == r_sl_idx);
3210 /* XXXX
3211 *
3212 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
3213 * commenting this out is just a band-aid.
3214 *
3215 * The problem is that, although well-behaved router descriptors
3216 * should never have the same value for their extra_info_digest, it's
3217 * possible for ill-behaved routers to claim whatever they like there.
3218 *
3219 * The real answer is to trash desc_by_eid_map and instead have
3220 * something that indicates for a given extra-info digest we want,
3221 * what its download status is. We'll do that as a part of routerlist
3222 * refactoring once consensus directories are in. For now,
3223 * this rep violation is probably harmless: an adversary can make us
3224 * reset our retry count for an extrainfo, but that's not the end
3225 * of the world. Changing the representation in 0.2.0.x would just
3226 * destabilize the codebase.
3227 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
3228 signed_descriptor_t *sd3 =
3229 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
3230 tor_assert(sd3 == &(r->cache_info));
3231 }
3232 */
3233 } SMARTLIST_FOREACH_END(r);
3235 r2 = rimap_get(rl->identity_map, sd->identity_digest);
3236 tor_assert(!r2 || sd != &(r2->cache_info));
3237 sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
3238 tor_assert(sd == sd2);
3239 tor_assert(sd->routerlist_index == sd_sl_idx);
3240 /* XXXX see above.
3241 if (!tor_digest_is_zero(sd->extra_info_digest)) {
3242 signed_descriptor_t *sd3 =
3243 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
3244 tor_assert(sd3 == sd);
3245 }
3246 */
3247 } SMARTLIST_FOREACH_END(sd);
3248
3249 RIMAP_FOREACH(rl->identity_map, d, r) {
3250 tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN));
3252 SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
3253 tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN));
3255 SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
3257 tor_assert(sd);
3258 tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN));
3260 EIMAP_FOREACH(rl->extra_info_map, d, ei) {
3262 tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3263 d, DIGEST_LEN));
3264 sd = sdmap_get(rl->desc_by_eid_map,
3265 ei->cache_info.signed_descriptor_digest);
3266 // tor_assert(sd); // XXXX see above
3267 if (sd) {
3268 tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3270 }
3272}
3273
3274/** Allocate and return a new string representing the contact info
3275 * and platform string for <b>router</b>,
3276 * surrounded by quotes and using standard C escapes.
3277 *
3278 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
3279 * thread. Also, each call invalidates the last-returned value, so don't
3280 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
3281 *
3282 * If <b>router</b> is NULL, it just frees its internal memory and returns.
3283 */
3284const char *
3286{
3287 static char *info=NULL;
3288 char *esc_contact, *esc_platform;
3289 tor_free(info);
3290
3291 if (!router)
3292 return NULL; /* we're exiting; just free the memory we use */
3293
3294 esc_contact = esc_for_log(router->contact_info);
3295 esc_platform = esc_for_log(router->platform);
3296
3297 tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
3298 tor_free(esc_contact);
3299 tor_free(esc_platform);
3300
3301 return info;
3302}
3303
3304/** Helper for sorting: compare two routerinfos by their identity
3305 * digest. */
3306static int
3307compare_routerinfo_by_id_digest_(const void **a, const void **b)
3308{
3309 routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
3310 return fast_memcmp(first->cache_info.identity_digest,
3311 second->cache_info.identity_digest,
3312 DIGEST_LEN);
3313}
3314
3315/** Sort a list of routerinfo_t in ascending order of identity digest. */
3316void
3318{
3320}
3321
3322/** Called when we change a node set, or when we reload the geoip IPv4 list:
3323 * recompute all country info in all configuration node sets and in the
3324 * routerlist. */
3325void
3327{
3328 const or_options_t *options = get_options();
3329
3330 if (options->EntryNodes)
3332 if (options->ExitNodes)
3334 if (options->MiddleNodes)
3336 if (options->ExcludeNodes)
3338 if (options->ExcludeExitNodes)
3340 if (options->ExcludeExitNodesUnion_)
3342
3344}
#define tor_addr_eq(a, b)
Definition: address.h:280
time_t approx_time(void)
Definition: approx_time.c:32
void trusted_dirs_remove_old_certs(void)
Definition: authcert.c:548
Header file for authcert.c.
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
int authdir_mode_handles_descs(const or_options_t *options, int purpose)
Definition: authmode.c:43
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:76
Header file for directory authority mode.
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
Definition: bridges.c:357
void learned_bridge_descriptor(routerinfo_t *ri, int from_cache, int desc_is_new)
Definition: bridges.c:991
Header file for circuitbuild.c.
Header file for circuitlist.c.
Header file for circuituse.c.
const or_options_t * get_options(void)
Definition: config.c:944
Header file for config.c.
Header file for connection.c.
#define CONN_TYPE_DIR
Definition: connection.h:55
int control_event_descriptors_changed(smartlist_t *routers)
Header file for control_events.c.
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
void digest256_to_base64(char *d64, const char *digest)
Header for crypto_format.c.
void smartlist_shuffle(smartlist_t *sl)
Definition: crypto_rand.c:606
Common functions for using (pseudo-)random number generators.
int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b)
Definition: crypto_rsa.c:71
int crypto_pk_public_checksig(const crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
const char * router_describe(const routerinfo_t *ri)
Definition: describe.c:137
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:203
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
#define fast_memcmp(a, b, c)
Definition: di_ops.h:28
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define DIGEST256_LEN
Definition: digest_sizes.h:23
void digestset_add(digestset_t *set, const char *digest)
Definition: digestset.c:44
digestset_t * digestset_new(int max_guess)
Definition: digestset.c:30
int digestset_probably_contains(const digestset_t *set, const char *digest)
Definition: digestset.c:54
Types to handle sets of digests, based on bloom filters.
Client/server directory connection structure.
Trusted/fallback directory server structure.
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1046
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1148
void directory_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose, const char *resource, int pds_flags, download_want_authority_t want_authority)
Definition: dirclient.c:453
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1253
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:950
Header file for dirclient.c.
struct directory_request_t directory_request_t
Definition: dirclient.h:52
int dirclient_fetches_dir_info_early(const or_options_t *options)
int dirclient_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
Header for feature/dirclient/dirclient_modes.c.
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:88
int dir_split_resource_into_fingerprints(const char *resource, smartlist_t *fp_out, int *compressed_out, int flags)
Definition: directory.c:640
Header file for directory.c.
#define DIR_PURPOSE_FETCH_EXTRAINFO
Definition: directory.h:39
#define DIR_PURPOSE_FETCH_MICRODESC
Definition: directory.h:65
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:36
void router_reset_status_download_failures(void)
Definition: dirlist.c:151
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:215
Header file for dirlist.c.
int directory_caches_dir_info(const or_options_t *options)
Definition: dirserv.c:94
Header file for dirserv.c.
int download_status_is_ready(download_status_t *dls, time_t now)
Definition: dlstatus.c:380
void download_status_mark_impossible(download_status_t *dl)
Definition: dlstatus.c:392
Header file for dlstatus.c.
Authority signature structure.
char * esc_for_log(const char *s)
Definition: escape.c:30
const char * escaped(const char *s)
Definition: escape.c:126
Header for core/or/extendinfo.c.
A relay's extra-info structure.
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:275
#define RFTS_IGNORE_MISSING
Definition: files.h:101
file_status_t file_status(const char *filename)
Definition: files.c:213
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:555
int replace_file(const char *from, const char *to)
Definition: files.c:118
#define RFTS_BIN
Definition: files.h:99
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:591
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LOG_DEBUG
Definition: log.h:42
#define LD_FS
Definition: log.h:70
#define LD_BUG
Definition: log.h:86
#define LD_GENERAL
Definition: log.h:62
#define LD_DIR
Definition: log.h:88
#define LOG_WARN
Definition: log.h:53
#define LOG_INFO
Definition: log.h:45
void reschedule_directory_downloads(void)
Definition: mainloop.c:1617
smartlist_t * get_connection_array(void)
Definition: mainloop.c:443
Header file for mainloop.c.
#define tor_free(p)
Definition: malloc.h:56
#define DIGESTMAP_FOREACH_END
Definition: map.h:168
int we_fetch_router_descriptors(const or_options_t *options)
Definition: microdesc.c:1075
void update_microdesc_downloads(time_t now)
Definition: microdesc.c:993
Header file for microdesc.c.
networkstatus_t * networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
int we_want_to_fetch_flavor(const or_options_t *options, int flavor)
download_status_t * router_get_dl_status_by_descriptor_digest(const char *d)
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
int client_would_use_router(const routerstatus_t *rs, time_t now)
networkstatus_t * networkstatus_get_latest_consensus(void)
const routerstatus_t * networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
routerstatus_t * networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
void networkstatus_reset_download_failures(void)
void networkstatus_reset_warnings(void)
void routers_update_status_from_consensus_networkstatus(smartlist_t *routers, int reset_failures)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
Single consensus voter structure.
Header file for node_select.c.
#define PDS_NO_EXISTING_SERVERDESC_FETCH
Definition: node_select.h:69
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:75
#define PDS_RETRY_IF_NO_SERVERS
Definition: node_select.h:56
Node information structure.
void nodelist_refresh_countries(void)
Definition: nodelist.c:2092
void router_dir_info_changed(void)
Definition: nodelist.c:2479
const smartlist_t * nodelist_get_list(void)
Definition: nodelist.c:1047
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1509
node_t * nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
Definition: nodelist.c:579
bool node_supports_conflux(const node_t *node)
Definition: nodelist.c:1346
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2344
bool node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1271
int node_allows_single_hop_exits(const node_t *node)
Definition: nodelist.c:1577
void nodelist_remove_routerinfo(routerinfo_t *ri)
Definition: nodelist.c:823
bool node_supports_initiating_ipv6_extends(const node_t *node)
Definition: nodelist.c:1303
int node_has_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2018
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2436
Header file for nodelist.c.
Master header file for Tor-specific functionality.
saved_location_t
Definition: or.h:623
@ SAVED_IN_JOURNAL
Definition: or.h:637
@ SAVED_NOWHERE
Definition: or.h:626
@ SAVED_IN_CACHE
Definition: or.h:630
#define MAX_NICKNAME_LEN
Definition: or.h:112
#define OLD_ROUTER_DESC_MAX_AGE
Definition: or.h:163
#define ROUTER_ANNOTATION_BUF_LEN
Definition: or.h:680
#define ROUTER_MAX_AGE
Definition: or.h:158
int addr_policies_eq(const smartlist_t *a, const smartlist_t *b)
Definition: policies.c:1324
int firewall_is_fascist_or(void)
Definition: policies.c:359
int reachable_addr_allows_node(const node_t *node, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:693
int firewall_is_fascist_dir(void)
Definition: policies.c:370
Header file for policies.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
int dirserv_would_reject_router(const routerstatus_t *rs, const vote_routerstatus_t *vrs)
int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg, int complain, int *valid_out)
Header file for process_descs.c.
void dirserv_single_reachability_test(time_t now, routerinfo_t *router)
Definition: reachability.c:134
Header file for reachability.c.
Header file for relay_find_addr.c.
void rep_hist_note_router_unreachable(const char *id, time_t when)
Definition: rephist.c:706
Header file for rephist.c.
extrainfo_t * router_get_my_extrainfo(void)
Definition: router.c:1859
int router_is_me(const routerinfo_t *router)
Definition: router.c:1773
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1806
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:98
Header file for routerinfo.c.
Router descriptor structure.
#define ROUTER_PURPOSE_GENERAL
Definition: routerinfo_st.h:98
#define ROUTER_PURPOSE_BRIDGE
static void routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
Definition: routerlist.c:1108
static desc_store_t * desc_get_store(routerlist_t *rl, const signed_descriptor_t *sd)
Definition: routerlist.c:186
void routerinfo_free_(routerinfo_t *router)
Definition: routerlist.c:923
void routerlist_free_all(void)
Definition: routerlist.c:1518
const char * esc_router_info(const routerinfo_t *router)
Definition: routerlist.c:3285
static void routerlist_remove_old_cached_routers_with_id(time_t now, time_t cutoff, int lo, int hi, digestset_t *retain)
Definition: routerlist.c:1816
const char * signed_descriptor_get_body(const signed_descriptor_t *desc)
Definition: routerlist.c:883
void list_pending_microdesc_downloads(digest256map_t *result)
Definition: routerlist.c:2428
void update_consensus_router_descriptor_downloads(time_t now, int is_vote, networkstatus_t *consensus)
Definition: routerlist.c:2650
int router_exit_policy_rejects_all(const routerinfo_t *router)
Definition: routerlist.c:2362
void routerlist_free_(routerlist_t *rl)
Definition: routerlist.c:1031
int hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest, const char *nickname)
Definition: routerlist.c:723
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH
Definition: routerlist.c:655
void launch_descriptor_downloads(int purpose, smartlist_t *downloadable, const routerstatus_t *source, time_t now)
Definition: routerlist.c:2552
void router_load_extrainfo_from_string(const char *s, const char *eos, saved_location_t saved_location, smartlist_t *requested_fingerprints, int descriptor_digests)
Definition: routerlist.c:2245
static void signed_descriptor_move(signed_descriptor_t *dest, signed_descriptor_t *src)
Definition: routerlist.c:996
static void signed_descriptor_reset(signed_descriptor_t *sd)
Definition: routerlist.c:985
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:898
signed_descriptor_t * router_get_by_extrainfo_digest(const char *digest)
Definition: routerlist.c:800
signed_descriptor_t * router_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:787
static void routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
Definition: routerlist.c:1337
STATIC void initiate_descriptor_downloads(const routerstatus_t *source, int purpose, smartlist_t *digests, int lo, int hi, int pds_flags)
Definition: routerlist.c:2441
void dump_routerlist_mem_usage(int severity)
Definition: routerlist.c:1063
uint32_t router_get_advertised_bandwidth(const routerinfo_t *router)
Definition: routerlist.c:646
void routerlist_retry_directory_downloads(time_t now)
Definition: routerlist.c:2347
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:779
void router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags)
Definition: routerlist.c:618
static int routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
Definition: routerlist.c:1086
int hex_digest_nickname_decode(const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
Definition: routerlist.c:687
const char * signed_descriptor_get_annotations(const signed_descriptor_t *desc)
Definition: routerlist.c:891
static void signed_descriptor_free_(signed_descriptor_t *sd)
Definition: routerlist.c:970
static void extrainfo_free_void(void *e)
Definition: routerlist.c:1024
void routerlist_assert_ok(const routerlist_t *rl)
Definition: routerlist.c:3197
static void list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
Definition: routerlist.c:2417
int router_descriptor_is_older_than(const routerinfo_t *router, int seconds)
Definition: routerlist.c:1549
int router_load_single_router(const char *s, uint8_t purpose, int cache, const char **msg)
Definition: routerlist.c:2079
static routerlist_t * routerlist
Definition: routerlist.c:147
STATIC was_router_added_t extrainfo_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
Definition: routerlist.c:1153
int router_reload_router_list(void)
Definition: routerlist.c:458
static int max_dl_per_request(const or_options_t *options, int purpose)
Definition: routerlist.c:2508
void refresh_all_country_info(void)
Definition: routerlist.c:3326
void update_router_descriptor_downloads(time_t now)
Definition: routerlist.c:2807
int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *identity_pkey, extrainfo_t *ei, signed_descriptor_t *sd, const char **msg)
Definition: routerlist.c:3060
void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
Definition: routerlist.c:1278
was_router_added_t router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1759
static void routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old, routerinfo_t *ri_new)
Definition: routerlist.c:1390
void extrainfo_free_(extrainfo_t *extrainfo)
Definition: routerlist.c:953
int hexdigest_to_digest(const char *hexdigest, char *digest)
Definition: routerlist.c:752
#define MAX_DL_TO_DELAY
Definition: routerlist.c:2543
void list_pending_downloads(digestmap_t *result, digest256map_t *result256, int purpose, const char *prefix)
Definition: routerlist.c:2374
int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:507
#define MIN_REQUESTS
Definition: routerlist.c:2540
void routers_sort_by_identity(smartlist_t *routers)
Definition: routerlist.c:3317
was_router_added_t router_add_to_routerlist(routerinfo_t *router, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1576
void update_extrainfo_downloads(time_t now)
Definition: routerlist.c:2821
signed_descriptor_t * extrainfo_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:813
static time_t last_descriptor_download_attempted
Definition: routerlist.c:156
static const char * signed_descriptor_get_body_impl(const signed_descriptor_t *desc, int with_annotations)
Definition: routerlist.c:834
static smartlist_t * warned_nicknames
Definition: routerlist.c:151
int router_differences_are_cosmetic(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:2964
static int compare_old_routers_by_identity_(const void **_a, const void **_b)
Definition: routerlist.c:1780
static int compare_duration_idx_(const void *_d1, const void *_d2)
Definition: routerlist.c:1800
static int router_rebuild_store(int flags, desc_store_t *store)
Definition: routerlist.c:239
#define MIN_DL_PER_REQUEST
Definition: routerlist.c:2537
#define ROUTER_ALLOW_UPTIME_DRIFT
Definition: routerlist.c:2958
static int router_reload_router_list_impl(desc_store_t *store)
Definition: routerlist.c:391
void routerlist_descriptors_added(smartlist_t *sl, int from_cache)
Definition: routerlist.c:2050
static int router_should_rebuild_store(desc_store_t *store)
Definition: routerlist.c:174
void routerlist_remove_old_routers(void)
Definition: routerlist.c:1902
void routerlist_reset_warnings(void)
Definition: routerlist.c:1536
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Definition: routerlist.c:660
static int compare_signed_descriptors_by_age_(const void **_a, const void **_b)
Definition: routerlist.c:223
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc)
Definition: routerlist.c:2318
void router_reset_descriptor_download_failures(void)
Definition: routerlist.c:2932
static void routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
Definition: routerlist.c:1243
int router_load_routers_from_string(const char *s, const char *eos, saved_location_t saved_location, smartlist_t *requested_fingerprints, int descriptor_digests, const char *prepend_annotations)
Definition: routerlist.c:2146
static int compare_routerinfo_by_id_digest_(const void **a, const void **b)
Definition: routerlist.c:3307
routerinfo_t * router_get_mutable_by_digest(const char *digest)
Definition: routerlist.c:765
static routerinfo_t * routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
Definition: routerlist.c:1493
static int signed_desc_append_to_journal(signed_descriptor_t *desc, desc_store_t *store)
Definition: routerlist.c:198
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
Definition: routerlist.c:2955
const routerinfo_t * routerlist_find_my_routerinfo(void)
Definition: routerlist.c:630
static signed_descriptor_t * signed_descriptor_from_routerinfo(routerinfo_t *ri)
Definition: routerlist.c:1012
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2336
Header file for routerlist.c.
static int WRA_WAS_ADDED(was_router_added_t s)
Definition: routerlist.h:106
static int WRA_WAS_OUTDATED(was_router_added_t s)
Definition: routerlist.h:116
was_router_added_t
Definition: routerlist.h:17
static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
Definition: routerlist.h:132
Router descriptor list structure.
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
int router_parse_list_from_string(const char **s, const char *eos, smartlist_t *dest, saved_location_t saved_location, int want_extrainfo, int allow_annotations, const char *prepend_annotations, smartlist_t *invalid_digests_out)
Definition: routerparse.c:249
routerinfo_t * router_parse_entry_from_string(const char *s, const char *end, int cache_copy, int allow_annotations, const char *prepend_annotations, int *can_dl_again_out)
Definition: routerparse.c:394
Header file for routerparse.c.
void routerset_refresh_countries(routerset_t *target)
Definition: routerset.c:82
Header file for routerset.c.
void smartlist_sort_digests(smartlist_t *sl)
Definition: smartlist.c:824
void smartlist_string_remove(smartlist_t *sl, const char *element)
Definition: smartlist.c:74
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
void smartlist_del(smartlist_t *sl, int idx)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
store_type_t type
Definition: desc_store_st.h:33
const char * description
Definition: desc_store_st.h:29
const char * fname_base
Definition: desc_store_st.h:27
tor_mmap_t * mmap
Definition: desc_store_st.h:31
size_t journal_len
Definition: desc_store_st.h:36
size_t store_len
Definition: desc_store_st.h:38
size_t bytes_dropped
Definition: desc_store_st.h:41
routerstatus_t fake_status
Definition: dir_server_st.h:57
char digest[DIGEST_LEN]
Definition: dir_server_st.h:35
char * pending_sig
Definition: extrainfo_st.h:29
unsigned int bad_sig
Definition: extrainfo_st.h:26
uint8_t digest256[DIGEST256_LEN]
Definition: extrainfo_st.h:21
size_t pending_sig_len
Definition: extrainfo_st.h:31
smartlist_t * voters
smartlist_t * routerstatus_list
consensus_flavor_t flavor
Definition: node_st.h:34
unsigned int is_running
Definition: node_st.h:63
unsigned int is_valid
Definition: node_st.h:65
struct routerset_t * ExcludeExitNodes
struct routerset_t * ExcludeExitNodesUnion_
int TestingClientMaxIntervalWithoutRequest
struct routerset_t * EntryNodes
struct routerset_t * ExcludeNodes
struct routerset_t * ExitNodes
struct routerset_t * MiddleNodes
unsigned int supports_extend2_cells
Definition: or.h:693
unsigned int protocols_known
Definition: or.h:689
char * onion_pkey
Definition: routerinfo_st.h:37
char * platform
Definition: routerinfo_st.h:48
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:30
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:25
smartlist_t * exit_policy
Definition: routerinfo_st.h:59
size_t onion_pkey_len
Definition: routerinfo_st.h:39
smartlist_t * declared_family
Definition: routerinfo_st.h:65
uint32_t bandwidthrate
Definition: routerinfo_st.h:54
crypto_pk_t * identity_pkey
Definition: routerinfo_st.h:41
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:43
unsigned int is_hibernating
Definition: routerinfo_st.h:68
unsigned int policy_is_reject_star
Definition: routerinfo_st.h:77
char * protocol_list
Definition: routerinfo_st.h:50
uint8_t purpose
unsigned int supports_tunnelled_dir_requests
Definition: routerinfo_st.h:86
char * contact_info
Definition: routerinfo_st.h:67
uint32_t bandwidthcapacity
Definition: routerinfo_st.h:58
time_t cert_expiration_time
Definition: routerinfo_st.h:46
uint32_t bandwidthburst
Definition: routerinfo_st.h:56
char * nickname
Definition: routerinfo_st.h:22
struct short_policy_t * ipv6_exit_policy
Definition: routerinfo_st.h:63
struct digest_sd_map_t * desc_digest_map
Definition: routerlist_st.h:23
smartlist_t * routers
Definition: routerlist_st.h:32
desc_store_t desc_store
Definition: routerlist_st.h:39
desc_store_t extrainfo_store
Definition: routerlist_st.h:41
smartlist_t * old_routers
Definition: routerlist_st.h:35
struct digest_ei_map_t * extra_info_map
Definition: routerlist_st.h:26
struct digest_ri_map_t * identity_map
Definition: routerlist_st.h:20
struct digest_sd_map_t * desc_by_eid_map
Definition: routerlist_st.h:30
protover_summary_flags_t pv
char descriptor_digest[DIGEST256_LEN]
char identity_digest[DIGEST_LEN]
char nickname[MAX_NICKNAME_LEN+1]
char signed_descriptor_digest[DIGEST_LEN]
char extra_info_digest[DIGEST_LEN]
char identity_digest[DIGEST_LEN]
download_status_t ei_dl_status
struct tor_cert_st * signing_key_cert
char extra_info_digest256[DIGEST256_LEN]
saved_location_t saved_location
size_t size
Definition: mmap.h:27
const char * data
Definition: mmap.h:26
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:326
int tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
Definition: torcert.c:315
Header for torcert.c.
#define tor_assert(expr)
Definition: util_bug.h:103
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:217
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:76
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:98
Routerstatus (vote entry) structure.
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20