Tor  0.4.8.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"
67 #include "core/mainloop/mainloop.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"
72 #include "feature/client/bridges.h"
96 #include "feature/stats/rephist.h"
99 
110 
111 #include "lib/crypt_ops/digestset.h"
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 
124 DECLARE_TYPED_DIGESTMAP_FNS(sdmap, digest_sd_map_t, signed_descriptor_t)
125 DECLARE_TYPED_DIGESTMAP_FNS(rimap, digest_ri_map_t, routerinfo_t)
126 DECLARE_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 */
140 static 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. */
147 static routerlist_t *routerlist = NULL;
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. */
173 static 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>. */
185 static 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. */
197 static 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>. */
222 static int
223 compare_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  */
238 static 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. */
390 static 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  */
457 int
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  */
478 int
479 router_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  */
494 int
495 router_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. */
506 int
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  */
549 bool
550 router_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 
562  const or_options_t *options = get_options();
563  const bool check_reach =
564  !router_or_conn_should_skip_reachable_address_check(options, pref_addr);
565  const bool direct_bridge = direct_conn && options->UseBridges;
566 
567  if (!node->is_running || !node->is_valid)
568  return false;
569  if (need_desc && !node_has_preferred_descriptor(node, direct_conn))
570  return false;
571  if (node->ri) {
572  if (direct_bridge && node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
573  return false;
574  else if (node->ri->purpose != ROUTER_PURPOSE_GENERAL)
575  return false;
576  }
577  if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
578  return false;
579  /* Don't choose nodes if we are certain they can't do EXTEND2 cells */
580  if (node->rs && !routerstatus_version_supports_extend2_cells(node->rs, 1))
581  return false;
582  /* Don't choose nodes if we are certain they can't do ntor. */
583  if ((node->ri || node->md) && !node_has_curve25519_onion_key(node))
584  return false;
585  /* Exclude relays that allow single hop exit circuits. This is an
586  * obsolete option since 0.2.9.2-alpha and done by default in
587  * 0.3.1.0-alpha. */
589  return false;
590  /* Exclude relays that can not become a rendezvous for a hidden service
591  * version 3. */
592  if (rendezvous_v3 &&
594  return false;
595  /* Choose a node with an OR address that matches the firewall rules */
596  if (direct_conn && check_reach &&
598  FIREWALL_OR_CONNECTION,
599  pref_addr))
600  return false;
601  if (initiate_ipv6_extend && !node_supports_initiating_ipv6_extends(node))
602  return false;
603 
604  return true;
605 }
606 
607 /** Add every suitable node from our nodelist to <b>sl</b>, so that
608  * we can pick a node for a circuit based on <b>flags</b>.
609  *
610  * See router_can_choose_node() for details of <b>flags</b>.
611  */
612 void
614 {
616  if (!router_can_choose_node(node, flags))
617  continue;
618  smartlist_add(sl, (void *)node);
619  } SMARTLIST_FOREACH_END(node);
620 }
621 
622 /** Look through the routerlist until we find a router that has my key.
623  Return it. */
624 const routerinfo_t *
626 {
627  if (!routerlist)
628  return NULL;
629 
631  {
632  if (router_is_me(router))
633  return router;
634  });
635  return NULL;
636 }
637 
638 /** Return the smaller of the router's configured BandwidthRate
639  * and its advertised capacity. */
640 uint32_t
642 {
643  if (router->bandwidthcapacity < router->bandwidthrate)
644  return router->bandwidthcapacity;
645  return router->bandwidthrate;
646 }
647 
648 /** Do not weight any declared bandwidth more than this much when picking
649  * routers by bandwidth. */
650 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
651 
652 /** Return the smaller of the router's configured BandwidthRate
653  * and its advertised capacity, capped by max-believe-bw. */
654 uint32_t
656 {
657  uint32_t result = router->bandwidthcapacity;
658  if (result > router->bandwidthrate)
659  result = router->bandwidthrate;
662  return result;
663 }
664 
665 /** Helper: given an extended nickname in <b>hexdigest</b> try to decode it.
666  * Return 0 on success, -1 on failure. Store the result into the
667  * DIGEST_LEN-byte buffer at <b>digest_out</b>, the single character at
668  * <b>nickname_qualifier_char_out</b>, and the MAXNICKNAME_LEN+1-byte buffer
669  * at <b>nickname_out</b>.
670  *
671  * The recognized format is:
672  * HexName = Dollar? HexDigest NamePart?
673  * Dollar = '?'
674  * HexDigest = HexChar*20
675  * HexChar = 'a'..'f' | 'A'..'F' | '0'..'9'
676  * NamePart = QualChar Name
677  * QualChar = '=' | '~'
678  * Name = NameChar*(1..MAX_NICKNAME_LEN)
679  * NameChar = Any ASCII alphanumeric character
680  */
681 int
682 hex_digest_nickname_decode(const char *hexdigest,
683  char *digest_out,
684  char *nickname_qualifier_char_out,
685  char *nickname_out)
686 {
687  size_t len;
688 
689  tor_assert(hexdigest);
690  if (hexdigest[0] == '$')
691  ++hexdigest;
692 
693  len = strlen(hexdigest);
694  if (len < HEX_DIGEST_LEN) {
695  return -1;
696  } else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' ||
697  hexdigest[HEX_DIGEST_LEN] == '~') &&
698  len <= HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN) {
699  *nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN];
700  strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1);
701  } else if (len == HEX_DIGEST_LEN) {
702  ;
703  } else {
704  return -1;
705  }
706 
707  if (base16_decode(digest_out, DIGEST_LEN,
708  hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN)
709  return -1;
710  return 0;
711 }
712 
713 /** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
714  * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
715  * (which is optionally prefixed with a single dollar sign). Return false if
716  * <b>hexdigest</b> is malformed, or it doesn't match. */
717 int
718 hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest,
719  const char *nickname)
720 {
721  char digest[DIGEST_LEN];
722  char nn_char='\0';
723  char nn_buf[MAX_NICKNAME_LEN+1];
724 
725  if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1)
726  return 0;
727 
728  if (nn_char == '=') {
729  return 0;
730  }
731 
732  if (nn_char == '~') {
733  if (!nickname) // XXX This seems wrong. -NM
734  return 0;
735  if (strcasecmp(nn_buf, nickname))
736  return 0;
737  }
738 
739  return tor_memeq(digest, identity_digest, DIGEST_LEN);
740 }
741 
742 /** If hexdigest is correctly formed, base16_decode it into
743  * digest, which must have DIGEST_LEN space in it.
744  * Return 0 on success, -1 on failure.
745  */
746 int
747 hexdigest_to_digest(const char *hexdigest, char *digest)
748 {
749  if (hexdigest[0]=='$')
750  ++hexdigest;
751  if (strlen(hexdigest) < HEX_DIGEST_LEN ||
752  base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN)
753  return -1;
754  return 0;
755 }
756 
757 /** As router_get_by_id_digest,but return a pointer that you're allowed to
758  * modify */
759 routerinfo_t *
760 router_get_mutable_by_digest(const char *digest)
761 {
762  tor_assert(digest);
763 
764  if (!routerlist) return NULL;
765 
766  // routerlist_assert_ok(routerlist);
767 
768  return rimap_get(routerlist->identity_map, digest);
769 }
770 
771 /** Return the router in our routerlist whose 20-byte key digest
772  * is <b>digest</b>. Return NULL if no such router is known. */
773 const routerinfo_t *
774 router_get_by_id_digest(const char *digest)
775 {
776  return router_get_mutable_by_digest(digest);
777 }
778 
779 /** Return the router in our routerlist whose 20-byte descriptor
780  * is <b>digest</b>. Return NULL if no such router is known. */
783 {
784  tor_assert(digest);
785 
786  if (!routerlist) return NULL;
787 
788  return sdmap_get(routerlist->desc_digest_map, digest);
789 }
790 
791 /** Return the signed descriptor for the router in our routerlist whose
792  * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
793  * is known. */
795 router_get_by_extrainfo_digest,(const char *digest))
796 {
797  tor_assert(digest);
798 
799  if (!routerlist) return NULL;
800 
801  return sdmap_get(routerlist->desc_by_eid_map, digest);
802 }
803 
804 /** Return the signed descriptor for the extrainfo_t in our routerlist whose
805  * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
806  * document is known. */
809 {
810  extrainfo_t *ei;
811  tor_assert(digest);
812  if (!routerlist) return NULL;
813  ei = eimap_get(routerlist->extra_info_map, digest);
814  return ei ? &ei->cache_info : NULL;
815 }
816 
817 /** Return a pointer to the signed textual representation of a descriptor.
818  * The returned string is not guaranteed to be NUL-terminated: the string's
819  * length will be in desc->signed_descriptor_len.
820  *
821  * If <b>with_annotations</b> is set, the returned string will include
822  * the annotations
823  * (if any) preceding the descriptor. This will increase the length of the
824  * string by desc->annotations_len.
825  *
826  * The caller must not free the string returned.
827  */
828 static const char *
830  int with_annotations)
831 {
832  const char *r = NULL;
833  size_t len = desc->signed_descriptor_len;
834  off_t offset = desc->saved_offset;
835  if (with_annotations)
836  len += desc->annotations_len;
837  else
838  offset += desc->annotations_len;
839 
840  tor_assert(len > 32);
841  if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
843  if (store && store->mmap) {
844  tor_assert(desc->saved_offset + len <= store->mmap->size);
845  r = store->mmap->data + offset;
846  } else if (store) {
847  log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
848  "mmaped in our cache. Is another process running in our data "
849  "directory? Exiting.");
850  exit(1); // XXXX bad exit: should recover.
851  }
852  }
853  if (!r) /* no mmap, or not in cache. */
854  r = desc->signed_descriptor_body +
855  (with_annotations ? 0 : desc->annotations_len);
856 
857  tor_assert(r);
858  if (!with_annotations) {
859  if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) {
860  char *cp = tor_strndup(r, 64);
861  log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
862  "Is another process running in our data directory? Exiting.",
863  desc, escaped(cp));
864  exit(1); // XXXX bad exit: should recover.
865  }
866  }
867 
868  return r;
869 }
870 
871 /** Return a pointer to the signed textual representation of a descriptor.
872  * The returned string is not guaranteed to be NUL-terminated: the string's
873  * length will be in desc->signed_descriptor_len.
874  *
875  * The caller must not free the string returned.
876  */
877 const char *
879 {
880  return signed_descriptor_get_body_impl(desc, 0);
881 }
882 
883 /** As signed_descriptor_get_body(), but points to the beginning of the
884  * annotations section rather than the beginning of the descriptor. */
885 const char *
887 {
888  return signed_descriptor_get_body_impl(desc, 1);
889 }
890 
891 /** Return the current list of all known routers. */
892 routerlist_t *
894 {
895  if (PREDICT_UNLIKELY(!routerlist)) {
896  routerlist = tor_malloc_zero(sizeof(routerlist_t));
899  routerlist->identity_map = rimap_new();
900  routerlist->desc_digest_map = sdmap_new();
901  routerlist->desc_by_eid_map = sdmap_new();
902  routerlist->extra_info_map = eimap_new();
903 
904  routerlist->desc_store.fname_base = "cached-descriptors";
905  routerlist->extrainfo_store.fname_base = "cached-extrainfo";
906 
907  routerlist->desc_store.type = ROUTER_STORE;
908  routerlist->extrainfo_store.type = EXTRAINFO_STORE;
909 
910  routerlist->desc_store.description = "router descriptors";
911  routerlist->extrainfo_store.description = "extra-info documents";
912  }
913  return routerlist;
914 }
915 
916 /** Free all storage held by <b>router</b>. */
917 void
919 {
920  if (!router)
921  return;
922 
923  tor_free(router->cache_info.signed_descriptor_body);
924  tor_free(router->nickname);
925  tor_free(router->platform);
926  tor_free(router->protocol_list);
927  tor_free(router->contact_info);
928  if (router->onion_pkey)
929  tor_free(router->onion_pkey);
931  if (router->identity_pkey)
932  crypto_pk_free(router->identity_pkey);
933  tor_cert_free(router->cache_info.signing_key_cert);
934  if (router->declared_family) {
935  SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
936  smartlist_free(router->declared_family);
937  }
938  addr_policy_list_free(router->exit_policy);
939  short_policy_free(router->ipv6_exit_policy);
940 
941  memset(router, 77, sizeof(routerinfo_t));
942 
943  tor_free(router);
944 }
945 
946 /** Release all storage held by <b>extrainfo</b> */
947 void
949 {
950  if (!extrainfo)
951  return;
952  tor_cert_free(extrainfo->cache_info.signing_key_cert);
953  tor_free(extrainfo->cache_info.signed_descriptor_body);
954  tor_free(extrainfo->pending_sig);
955 
956  memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
957  tor_free(extrainfo);
958 }
959 
960 #define signed_descriptor_free(val) \
961  FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val))
962 
963 /** Release storage held by <b>sd</b>. */
964 static void
966 {
967  if (!sd)
968  return;
969 
971  tor_cert_free(sd->signing_key_cert);
972 
973  memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
974  tor_free(sd);
975 }
976 
977 /** Reset the given signed descriptor <b>sd</b> by freeing the allocated
978  * memory inside the object and by zeroing its content. */
979 static void
981 {
982  tor_assert(sd);
984  tor_cert_free(sd->signing_key_cert);
985  memset(sd, 0, sizeof(*sd));
986 }
987 
988 /** Copy src into dest, and steal all references inside src so that when
989  * we free src, we don't mess up dest. */
990 static void
992  signed_descriptor_t *src)
993 {
994  tor_assert(dest != src);
995  /* Cleanup destination object before overwriting it.*/
997  memcpy(dest, src, sizeof(signed_descriptor_t));
998  src->signed_descriptor_body = NULL;
999  src->signing_key_cert = NULL;
1000  dest->routerlist_index = -1;
1001 }
1002 
1003 /** Extract a signed_descriptor_t from a general routerinfo, and free the
1004  * routerinfo.
1005  */
1006 static signed_descriptor_t *
1008 {
1009  signed_descriptor_t *sd;
1011  sd = tor_malloc_zero(sizeof(signed_descriptor_t));
1012  signed_descriptor_move(sd, &ri->cache_info);
1013  routerinfo_free(ri);
1014  return sd;
1015 }
1016 
1017 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
1018 static void
1020 {
1021  extrainfo_free_(e);
1022 }
1023 
1024 /** Free all storage held by a routerlist <b>rl</b>. */
1025 void
1027 {
1028  if (!rl)
1029  return;
1030  rimap_free(rl->identity_map, NULL);
1031  sdmap_free(rl->desc_digest_map, NULL);
1032  sdmap_free(rl->desc_by_eid_map, NULL);
1033  eimap_free(rl->extra_info_map, extrainfo_free_void);
1035  routerinfo_free(r));
1037  signed_descriptor_free(sd));
1038  smartlist_free(rl->routers);
1039  smartlist_free(rl->old_routers);
1040  if (rl->desc_store.mmap) {
1041  int res = tor_munmap_file(rl->desc_store.mmap);
1042  if (res != 0) {
1043  log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap");
1044  }
1045  }
1046  if (rl->extrainfo_store.mmap) {
1047  int res = tor_munmap_file(rl->extrainfo_store.mmap);
1048  if (res != 0) {
1049  log_warn(LD_FS, "Failed to munmap routerlist->extrainfo_store.mmap");
1050  }
1051  }
1052  tor_free(rl);
1053 }
1054 
1055 /** Log information about how much memory is being used for routerlist,
1056  * at log level <b>severity</b>. */
1057 void
1059 {
1060  uint64_t livedescs = 0;
1061  uint64_t olddescs = 0;
1062  if (!routerlist)
1063  return;
1065  livedescs += r->cache_info.signed_descriptor_len);
1067  olddescs += sd->signed_descriptor_len);
1068 
1069  tor_log(severity, LD_DIR,
1070  "In %d live descriptors: %"PRIu64" bytes. "
1071  "In %d old descriptors: %"PRIu64" bytes.",
1072  smartlist_len(routerlist->routers), (livedescs),
1073  smartlist_len(routerlist->old_routers), (olddescs));
1074 }
1075 
1076 /** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
1077  * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
1078  * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
1079  * is not in <b>sl</b>. */
1080 static inline int
1081 routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
1082 {
1083  if (idx < 0) {
1084  idx = -1;
1086  if (r == ri) {
1087  idx = r_sl_idx;
1088  break;
1089  });
1090  } else {
1091  tor_assert(idx < smartlist_len(sl));
1092  tor_assert(smartlist_get(sl, idx) == ri);
1093  };
1094  return idx;
1095 }
1096 
1097 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
1098  * as needed. There must be no previous member of <b>rl</b> with the same
1099  * identity digest as <b>ri</b>: If there is, call routerlist_replace
1100  * instead.
1101  */
1102 static void
1104 {
1105  routerinfo_t *ri_old;
1106  signed_descriptor_t *sd_old;
1107  {
1108  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1109  tor_assert(ri_generated != ri);
1110  }
1111  tor_assert(ri->cache_info.routerlist_index == -1);
1112 
1113  ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
1114  tor_assert(!ri_old);
1115 
1116  sd_old = sdmap_set(rl->desc_digest_map,
1117  ri->cache_info.signed_descriptor_digest,
1118  &(ri->cache_info));
1119  if (sd_old) {
1120  int idx = sd_old->routerlist_index;
1121  sd_old->routerlist_index = -1;
1122  smartlist_del(rl->old_routers, idx);
1123  if (idx < smartlist_len(rl->old_routers)) {
1124  signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1125  d->routerlist_index = idx;
1126  }
1128  sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest);
1129  signed_descriptor_free(sd_old);
1130  }
1131 
1132  if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1133  sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
1134  &ri->cache_info);
1135  smartlist_add(rl->routers, ri);
1136  ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
1137  nodelist_set_routerinfo(ri, NULL);
1139 #ifdef DEBUG_ROUTERLIST
1141 #endif
1142 }
1143 
1144 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
1145  * corresponding router in rl->routers or rl->old_routers. Return the status
1146  * of inserting <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
1148 extrainfo_insert,(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible))
1149 {
1151  const char *compatibility_error_msg;
1152  routerinfo_t *ri = rimap_get(rl->identity_map,
1153  ei->cache_info.identity_digest);
1154  signed_descriptor_t *sd =
1155  sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
1156  extrainfo_t *ei_tmp;
1157  const int severity = warn_if_incompatible ? LOG_WARN : LOG_INFO;
1158 
1159  {
1160  extrainfo_t *ei_generated = router_get_my_extrainfo();
1161  tor_assert(ei_generated != ei);
1162  }
1163 
1164  if (!ri) {
1165  /* This router is unknown; we can't even verify the signature. Give up.*/
1166  r = ROUTER_NOT_IN_CONSENSUS;
1167  goto done;
1168  }
1169  if (! sd) {
1170  /* The extrainfo router doesn't have a known routerdesc to attach it to.
1171  * This just won't work. */;
1172  static ratelim_t no_sd_ratelim = RATELIM_INIT(1800);
1173  r = ROUTER_BAD_EI;
1174  /* This is a DEBUG because it can happen naturally, if we tried
1175  * to add an extrainfo for which we no longer have the
1176  * corresponding routerinfo.
1177  */
1178  log_fn_ratelim(&no_sd_ratelim, LOG_DEBUG, LD_DIR,
1179  "No entry found in extrainfo map.");
1180  goto done;
1181  }
1182  if (tor_memneq(ei->cache_info.signed_descriptor_digest,
1183  sd->extra_info_digest, DIGEST_LEN)) {
1184  static ratelim_t digest_mismatch_ratelim = RATELIM_INIT(1800);
1185  /* The sd we got from the map doesn't match the digest we used to look
1186  * it up. This makes no sense. */
1187  r = ROUTER_BAD_EI;
1188  log_fn_ratelim(&digest_mismatch_ratelim, severity, LD_BUG,
1189  "Mismatch in digest in extrainfo map.");
1190  goto done;
1191  }
1193  &compatibility_error_msg)) {
1194  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
1195  r = (ri->cache_info.extrainfo_is_bogus) ?
1196  ROUTER_BAD_EI : ROUTER_NOT_IN_CONSENSUS;
1197 
1198  base16_encode(d1, sizeof(d1), ri->cache_info.identity_digest, DIGEST_LEN);
1199  base16_encode(d2, sizeof(d2), ei->cache_info.identity_digest, DIGEST_LEN);
1200 
1201  log_fn(severity,LD_DIR,
1202  "router info incompatible with extra info (ri id: %s, ei id %s, "
1203  "reason: %s)", d1, d2, compatibility_error_msg);
1204 
1205  goto done;
1206  }
1207 
1208  /* Okay, if we make it here, we definitely have a router corresponding to
1209  * this extrainfo. */
1210 
1211  ei_tmp = eimap_set(rl->extra_info_map,
1212  ei->cache_info.signed_descriptor_digest,
1213  ei);
1214  r = ROUTER_ADDED_SUCCESSFULLY;
1215  if (ei_tmp) {
1217  ei_tmp->cache_info.signed_descriptor_len;
1218  extrainfo_free(ei_tmp);
1219  }
1220 
1221  done:
1222  if (r != ROUTER_ADDED_SUCCESSFULLY)
1223  extrainfo_free(ei);
1224 
1225 #ifdef DEBUG_ROUTERLIST
1227 #endif
1228  return r;
1229 }
1230 
1231 #define should_cache_old_descriptors() \
1232  directory_caches_dir_info(get_options())
1233 
1234 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
1235  * a copy of router <b>ri</b> yet, add it to the list of old (not
1236  * recommended but still served) descriptors. Else free it. */
1237 static void
1239 {
1240  {
1241  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1242  tor_assert(ri_generated != ri);
1243  }
1244  tor_assert(ri->cache_info.routerlist_index == -1);
1245 
1246  if (should_cache_old_descriptors() &&
1248  !sdmap_get(rl->desc_digest_map,
1249  ri->cache_info.signed_descriptor_digest)) {
1251  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1252  smartlist_add(rl->old_routers, sd);
1253  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1255  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1256  } else {
1257  routerinfo_free(ri);
1258  }
1259 #ifdef DEBUG_ROUTERLIST
1261 #endif
1262 }
1263 
1264 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
1265  * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl-&gt;routers,
1266  * idx) == ri, we don't need to do a linear search over the list to decide
1267  * which to remove. We fill the gap in rl-&gt;routers with a later element in
1268  * the list, if any exists. <b>ri</b> is freed.
1269  *
1270  * If <b>make_old</b> is true, instead of deleting the router, we try adding
1271  * it to rl-&gt;old_routers. */
1272 void
1273 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
1274 {
1275  routerinfo_t *ri_tmp;
1276  extrainfo_t *ei_tmp;
1277  int idx = ri->cache_info.routerlist_index;
1278  tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1279  tor_assert(smartlist_get(rl->routers, idx) == ri);
1280 
1282 
1283  /* make sure the rephist module knows that it's not running */
1285 
1286  ri->cache_info.routerlist_index = -1;
1287  smartlist_del(rl->routers, idx);
1288  if (idx < smartlist_len(rl->routers)) {
1289  routerinfo_t *r = smartlist_get(rl->routers, idx);
1290  r->cache_info.routerlist_index = idx;
1291  }
1292 
1293  ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
1295  tor_assert(ri_tmp == ri);
1296 
1297  if (make_old && should_cache_old_descriptors() &&
1299  signed_descriptor_t *sd;
1301  smartlist_add(rl->old_routers, sd);
1302  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1303  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1305  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1306  } else {
1307  signed_descriptor_t *sd_tmp;
1308  sd_tmp = sdmap_remove(rl->desc_digest_map,
1309  ri->cache_info.signed_descriptor_digest);
1310  tor_assert(sd_tmp == &(ri->cache_info));
1311  rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
1312  ei_tmp = eimap_remove(rl->extra_info_map,
1313  ri->cache_info.extra_info_digest);
1314  if (ei_tmp) {
1316  ei_tmp->cache_info.signed_descriptor_len;
1317  extrainfo_free(ei_tmp);
1318  }
1319  if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1320  sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
1321  routerinfo_free(ri);
1322  }
1323 #ifdef DEBUG_ROUTERLIST
1325 #endif
1326 }
1327 
1328 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>->old_routers, and
1329  * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
1330  * <b>sd</b>. */
1331 static void
1333 {
1334  signed_descriptor_t *sd_tmp;
1335  extrainfo_t *ei_tmp;
1336  desc_store_t *store;
1337  if (idx == -1) {
1338  idx = sd->routerlist_index;
1339  }
1340  tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
1341  /* XXXX edmanm's bridge relay triggered the following assert while
1342  * running 0.2.0.12-alpha. If anybody triggers this again, see if we
1343  * can get a backtrace. */
1344  tor_assert(smartlist_get(rl->old_routers, idx) == sd);
1345  tor_assert(idx == sd->routerlist_index);
1346 
1347  sd->routerlist_index = -1;
1348  smartlist_del(rl->old_routers, idx);
1349  if (idx < smartlist_len(rl->old_routers)) {
1350  signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1351  d->routerlist_index = idx;
1352  }
1353  sd_tmp = sdmap_remove(rl->desc_digest_map,
1355  tor_assert(sd_tmp == sd);
1356  store = desc_get_store(rl, sd);
1357  if (store)
1358  store->bytes_dropped += sd->signed_descriptor_len;
1359 
1360  ei_tmp = eimap_remove(rl->extra_info_map,
1361  sd->extra_info_digest);
1362  if (ei_tmp) {
1364  ei_tmp->cache_info.signed_descriptor_len;
1365  extrainfo_free(ei_tmp);
1366  }
1368  sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
1369 
1370  signed_descriptor_free(sd);
1371 #ifdef DEBUG_ROUTERLIST
1373 #endif
1374 }
1375 
1376 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
1377  * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
1378  * smartlist_get(rl-&gt;routers, idx) == ri, we don't need to do a linear
1379  * search over the list to decide which to remove. We put ri_new in the same
1380  * index as ri_old, if possible. ri is freed as appropriate.
1381  *
1382  * If should_cache_descriptors() is true, instead of deleting the router,
1383  * we add it to rl-&gt;old_routers. */
1384 static void
1386  routerinfo_t *ri_new)
1387 {
1388  int idx;
1389  int same_descriptors;
1390 
1391  routerinfo_t *ri_tmp;
1392  extrainfo_t *ei_tmp;
1393  {
1394  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1395  tor_assert(ri_generated != ri_new);
1396  }
1397  tor_assert(ri_old != ri_new);
1398  tor_assert(ri_new->cache_info.routerlist_index == -1);
1399 
1400  idx = ri_old->cache_info.routerlist_index;
1401  tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1402  tor_assert(smartlist_get(rl->routers, idx) == ri_old);
1403 
1404  {
1405  routerinfo_t *ri_old_tmp=NULL;
1406  nodelist_set_routerinfo(ri_new, &ri_old_tmp);
1407  tor_assert(ri_old == ri_old_tmp);
1408  }
1409 
1411  if (idx >= 0) {
1412  smartlist_set(rl->routers, idx, ri_new);
1413  ri_old->cache_info.routerlist_index = -1;
1414  ri_new->cache_info.routerlist_index = idx;
1415  /* Check that ri_old is not in rl->routers anymore: */
1416  tor_assert( routerlist_find_elt_(rl->routers, ri_old, -1) == -1 );
1417  } else {
1418  log_warn(LD_BUG, "Appending entry from routerlist_replace.");
1419  routerlist_insert(rl, ri_new);
1420  return;
1421  }
1422  if (tor_memneq(ri_old->cache_info.identity_digest,
1423  ri_new->cache_info.identity_digest, DIGEST_LEN)) {
1424  /* digests don't match; digestmap_set won't replace */
1425  rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
1426  }
1427  ri_tmp = rimap_set(rl->identity_map,
1428  ri_new->cache_info.identity_digest, ri_new);
1429  tor_assert(!ri_tmp || ri_tmp == ri_old);
1430  sdmap_set(rl->desc_digest_map,
1431  ri_new->cache_info.signed_descriptor_digest,
1432  &(ri_new->cache_info));
1433 
1434  if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
1435  sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
1436  &ri_new->cache_info);
1437  }
1438 
1439  same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest,
1440  ri_new->cache_info.signed_descriptor_digest,
1441  DIGEST_LEN);
1442 
1443  if (should_cache_old_descriptors() &&
1444  ri_old->purpose == ROUTER_PURPOSE_GENERAL &&
1445  !same_descriptors) {
1446  /* ri_old is going to become a signed_descriptor_t and go into
1447  * old_routers */
1449  smartlist_add(rl->old_routers, sd);
1450  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1451  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1453  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1454  } else {
1455  /* We're dropping ri_old. */
1456  if (!same_descriptors) {
1457  /* digests don't match; The sdmap_set above didn't replace */
1458  sdmap_remove(rl->desc_digest_map,
1459  ri_old->cache_info.signed_descriptor_digest);
1460 
1461  if (tor_memneq(ri_old->cache_info.extra_info_digest,
1462  ri_new->cache_info.extra_info_digest, DIGEST_LEN)) {
1463  ei_tmp = eimap_remove(rl->extra_info_map,
1464  ri_old->cache_info.extra_info_digest);
1465  if (ei_tmp) {
1467  ei_tmp->cache_info.signed_descriptor_len;
1468  extrainfo_free(ei_tmp);
1469  }
1470  }
1471 
1472  if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
1473  sdmap_remove(rl->desc_by_eid_map,
1474  ri_old->cache_info.extra_info_digest);
1475  }
1476  }
1477  rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
1478  routerinfo_free(ri_old);
1479  }
1480 #ifdef DEBUG_ROUTERLIST
1482 #endif
1483 }
1484 
1485 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
1486  * it as a fresh routerinfo_t. */
1487 static routerinfo_t *
1489 {
1490  routerinfo_t *ri;
1491  const char *body;
1492 
1494 
1497  0, 1, NULL, NULL);
1498  if (!ri)
1499  return NULL;
1500  signed_descriptor_move(&ri->cache_info, sd);
1501 
1502  routerlist_remove_old(rl, sd, -1);
1503 
1504  return ri;
1505 }
1506 
1507 /** Free all memory held by the routerlist module.
1508  * Note: Calling routerlist_free_all() should always be paired with
1509  * a call to nodelist_free_all(). These should only be called during
1510  * cleanup.
1511  */
1512 void
1514 {
1515  routerlist_t *rl = routerlist;
1516  routerlist = NULL; // Prevent internals of routerlist_free() from using
1517  // routerlist.
1518  routerlist_free(rl);
1519  dirlist_free_all();
1520  if (warned_nicknames) {
1521  SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
1522  smartlist_free(warned_nicknames);
1523  warned_nicknames = NULL;
1524  }
1525  authcert_free_all();
1526 }
1527 
1528 /** Forget that we have issued any router-related warnings, so that we'll
1529  * warn again if we see the same errors. */
1530 void
1532 {
1533  if (!warned_nicknames)
1535  SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
1536  smartlist_clear(warned_nicknames); /* now the list is empty. */
1537 
1539 }
1540 
1541 /** Return 1 if the signed descriptor of this router is older than
1542  * <b>seconds</b> seconds. Otherwise return 0. */
1543 MOCK_IMPL(int,
1544 router_descriptor_is_older_than,(const routerinfo_t *router, int seconds))
1545 {
1546  return router->cache_info.published_on < approx_time() - seconds;
1547 }
1548 
1549 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
1550  * older entries (if any) with the same key.
1551  *
1552  * Note: Callers should not hold their pointers to <b>router</b> if this
1553  * function fails; <b>router</b> will either be inserted into the routerlist or
1554  * freed. Similarly, even if this call succeeds, they should not hold their
1555  * pointers to <b>router</b> after subsequent calls with other routerinfo's --
1556  * they might cause the original routerinfo to get freed.
1557  *
1558  * Returns the status for the operation. Might set *<b>msg</b> if it wants
1559  * the poster of the router to know something.
1560  *
1561  * If <b>from_cache</b>, this descriptor came from our disk cache. If
1562  * <b>from_fetch</b>, we received it in response to a request we made.
1563  * (If both are false, that means it was uploaded to us as an auth dir
1564  * server or via the controller.)
1565  *
1566  * This function should be called *after*
1567  * routers_update_status_from_consensus_networkstatus; subsequently, you
1568  * should call router_rebuild_store and routerlist_descriptors_added.
1569  */
1571 router_add_to_routerlist(routerinfo_t *router, const char **msg,
1572  int from_cache, int from_fetch)
1573 {
1574  const char *id_digest;
1575  const or_options_t *options = get_options();
1576  int authdir = authdir_mode_handles_descs(options, router->purpose);
1577  int authdir_believes_valid = 0;
1578  routerinfo_t *old_router;
1579  networkstatus_t *consensus =
1581  int in_consensus = 0;
1582 
1583  tor_assert(msg);
1584 
1585  if (!routerlist)
1587 
1588  id_digest = router->cache_info.identity_digest;
1589 
1590  old_router = router_get_mutable_by_digest(id_digest);
1591 
1592  /* Make sure that it isn't expired. */
1593  if (router->cert_expiration_time < approx_time()) {
1594  routerinfo_free(router);
1595  *msg = "Some certs on this router are expired.";
1596  return ROUTER_CERTS_EXPIRED;
1597  }
1598 
1599  /* Make sure that we haven't already got this exact descriptor. */
1600  if (sdmap_get(routerlist->desc_digest_map,
1601  router->cache_info.signed_descriptor_digest)) {
1602  /* If we have this descriptor already and the new descriptor is a bridge
1603  * descriptor, replace it. If we had a bridge descriptor before and the
1604  * new one is not a bridge descriptor, don't replace it. */
1605 
1606  /* Only members of routerlist->identity_map can be bridges; we don't
1607  * put bridges in old_routers. */
1608  const int was_bridge = old_router &&
1609  old_router->purpose == ROUTER_PURPOSE_BRIDGE;
1610 
1611  if (routerinfo_is_a_configured_bridge(router) &&
1612  router->purpose == ROUTER_PURPOSE_BRIDGE &&
1613  !was_bridge) {
1614  log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
1615  "descriptor for router %s",
1616  router_describe(router));
1617  } else {
1618  if (router->purpose == ROUTER_PURPOSE_BRIDGE) {
1619  /* Even if we're not going to keep this descriptor, we need to
1620  * let the bridge descriptor fetch subsystem know that we
1621  * succeeded at getting it -- so we can adjust the retry schedule
1622  * to stop trying for a while. */
1623  learned_bridge_descriptor(router, from_cache, 0);
1624  }
1625  log_info(LD_DIR,
1626  "Dropping descriptor that we already have for router %s",
1627  router_describe(router));
1628  *msg = "Router descriptor was not new.";
1629  routerinfo_free(router);
1630  return ROUTER_IS_ALREADY_KNOWN;
1631  }
1632  }
1633 
1634  if (authdir) {
1635  if (authdir_wants_to_reject_router(router, msg,
1636  !from_cache && !from_fetch,
1637  &authdir_believes_valid)) {
1638  tor_assert(*msg);
1639  routerinfo_free(router);
1640  return ROUTER_AUTHDIR_REJECTS;
1641  }
1642  } else if (from_fetch) {
1643  /* Only check the descriptor digest against the network statuses when
1644  * we are receiving in response to a fetch. */
1645 
1646  if (!signed_desc_digest_is_recognized(&router->cache_info) &&
1648  /* We asked for it, so some networkstatus must have listed it when we
1649  * did. Save it if we're a cache in case somebody else asks for it. */
1650  log_info(LD_DIR,
1651  "Received a no-longer-recognized descriptor for router %s",
1652  router_describe(router));
1653  *msg = "Router descriptor is not referenced by any network-status.";
1654 
1655  /* Only journal this desc if we want to keep old descriptors */
1656  if (!from_cache && should_cache_old_descriptors())
1657  signed_desc_append_to_journal(&router->cache_info,
1660  return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
1661  }
1662  }
1663 
1664  /* We no longer need a router with this descriptor digest. */
1665  if (consensus) {
1667  consensus, id_digest);
1668  if (rs && tor_memeq(rs->descriptor_digest,
1669  router->cache_info.signed_descriptor_digest,
1670  DIGEST_LEN)) {
1671  in_consensus = 1;
1672  }
1673  }
1674 
1675  if (router->purpose == ROUTER_PURPOSE_GENERAL &&
1676  consensus && !in_consensus && !authdir) {
1677  /* If it's a general router not listed in the consensus, then don't
1678  * consider replacing the latest router with it. */
1679  if (!from_cache && should_cache_old_descriptors())
1680  signed_desc_append_to_journal(&router->cache_info,
1683  *msg = "Skipping router descriptor: not in consensus.";
1684  return ROUTER_NOT_IN_CONSENSUS;
1685  }
1686 
1687  /* If we're reading a bridge descriptor from our cache, and we don't
1688  * recognize it as one of our currently configured bridges, drop the
1689  * descriptor. Otherwise we could end up using it as one of our entry
1690  * guards even if it isn't in our Bridge config lines. */
1691  if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
1692  !authdir_mode_bridge(options) &&
1694  log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
1695  "no bridge configured at that address.",
1696  safe_str_client(router_describe(router)));
1697  *msg = "Router descriptor was not a configured bridge.";
1698  routerinfo_free(router);
1699  return ROUTER_WAS_NOT_WANTED;
1700  }
1701 
1702  /* If we have a router with the same identity key, choose the newer one. */
1703  if (old_router) {
1704  if (!in_consensus && (router->cache_info.published_on <=
1705  old_router->cache_info.published_on)) {
1706  /* Same key, but old. This one is not listed in the consensus. */
1707  log_debug(LD_DIR, "Not-new descriptor for router %s",
1708  router_describe(router));
1709  /* Only journal this desc if we'll be serving it. */
1710  if (!from_cache && should_cache_old_descriptors())
1711  signed_desc_append_to_journal(&router->cache_info,
1714  *msg = "Router descriptor was not new.";
1715  return ROUTER_IS_ALREADY_KNOWN;
1716  } else {
1717  /* Same key, and either new, or listed in the consensus. */
1718  log_debug(LD_DIR, "Replacing entry for router %s",
1719  router_describe(router));
1720  routerlist_replace(routerlist, old_router, router);
1721  if (!from_cache) {
1722  signed_desc_append_to_journal(&router->cache_info,
1724  }
1725  *msg = authdir_believes_valid ? "Valid server updated" :
1726  ("Invalid server updated. (This dirserver is marking your "
1727  "server as unapproved.)");
1728  return ROUTER_ADDED_SUCCESSFULLY;
1729  }
1730  }
1731 
1732  if (!in_consensus && from_cache &&
1734  *msg = "Router descriptor was really old.";
1735  routerinfo_free(router);
1736  return ROUTER_WAS_TOO_OLD;
1737  }
1738 
1739  /* We haven't seen a router with this identity before. Add it to the end of
1740  * the list. */
1741  routerlist_insert(routerlist, router);
1742  if (!from_cache) {
1743  signed_desc_append_to_journal(&router->cache_info,
1745  }
1746  return ROUTER_ADDED_SUCCESSFULLY;
1747 }
1748 
1749 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
1750  * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
1751  * we actually inserted it, ROUTER_BAD_EI otherwise.
1752  */
1755  int from_cache, int from_fetch)
1756 {
1757  was_router_added_t inserted;
1758  (void)from_fetch;
1759  if (msg) *msg = NULL;
1760  /*XXXX Do something with msg */
1761 
1762  inserted = extrainfo_insert(router_get_routerlist(), ei, !from_cache);
1763 
1764  if (WRA_WAS_ADDED(inserted) && !from_cache)
1765  signed_desc_append_to_journal(&ei->cache_info,
1767 
1768  return inserted;
1769 }
1770 
1771 /** Sorting helper: return &lt;0, 0, or &gt;0 depending on whether the
1772  * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
1773  * to, or later than that of *<b>b</b>. */
1774 static int
1775 compare_old_routers_by_identity_(const void **_a, const void **_b)
1776 {
1777  int i;
1778  const signed_descriptor_t *r1 = *_a, *r2 = *_b;
1779  if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
1780  return i;
1781  return (int)(r1->published_on - r2->published_on);
1782 }
1783 
1784 /** Internal type used to represent how long an old descriptor was valid,
1785  * where it appeared in the list of old descriptors, and whether it's extra
1786  * old. Used only by routerlist_remove_old_cached_routers_with_id(). */
1788  int duration;
1789  int idx;
1790  int old;
1791 };
1792 
1793 /** Sorting helper: compare two duration_idx_t by their duration. */
1794 static int
1795 compare_duration_idx_(const void *_d1, const void *_d2)
1796 {
1797  const struct duration_idx_t *d1 = _d1;
1798  const struct duration_idx_t *d2 = _d2;
1799  return d1->duration - d2->duration;
1800 }
1801 
1802 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
1803  * must contain routerinfo_t with the same identity and with publication time
1804  * in ascending order. Remove members from this range until there are no more
1805  * than max_descriptors_per_router() remaining. Start by removing the oldest
1806  * members from before <b>cutoff</b>, then remove members which were current
1807  * for the lowest amount of time. The order of members of old_routers at
1808  * indices <b>lo</b> or higher may be changed.
1809  */
1810 static void
1812  time_t cutoff, int lo, int hi,
1813  digestset_t *retain)
1814 {
1815  int i, n = hi-lo+1;
1816  unsigned n_extra, n_rmv = 0;
1817  struct duration_idx_t *lifespans;
1818  uint8_t *rmv, *must_keep;
1820 #if 1
1821  const char *ident;
1822  tor_assert(hi < smartlist_len(lst));
1823  tor_assert(lo <= hi);
1824  ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
1825  for (i = lo+1; i <= hi; ++i) {
1826  signed_descriptor_t *r = smartlist_get(lst, i);
1828  }
1829 #endif /* 1 */
1830  /* Check whether we need to do anything at all. */
1831  {
1832  int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
1833  if (n <= mdpr)
1834  return;
1835  n_extra = n - mdpr;
1836  }
1837 
1838  lifespans = tor_calloc(n, sizeof(struct duration_idx_t));
1839  rmv = tor_calloc(n, sizeof(uint8_t));
1840  must_keep = tor_calloc(n, sizeof(uint8_t));
1841  /* Set lifespans to contain the lifespan and index of each server. */
1842  /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
1843  for (i = lo; i <= hi; ++i) {
1844  signed_descriptor_t *r = smartlist_get(lst, i);
1845  signed_descriptor_t *r_next;
1846  lifespans[i-lo].idx = i;
1847  if (r->last_listed_as_valid_until >= now ||
1848  (retain && digestset_probably_contains(retain,
1849  r->signed_descriptor_digest))) {
1850  must_keep[i-lo] = 1;
1851  }
1852  if (i < hi) {
1853  r_next = smartlist_get(lst, i+1);
1854  tor_assert(r->published_on <= r_next->published_on);
1855  lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
1856  } else {
1857  r_next = NULL;
1858  lifespans[i-lo].duration = INT_MAX;
1859  }
1860  if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
1861  ++n_rmv;
1862  lifespans[i-lo].old = 1;
1863  rmv[i-lo] = 1;
1864  }
1865  }
1866 
1867  if (n_rmv < n_extra) {
1868  /**
1869  * We aren't removing enough servers for being old. Sort lifespans by
1870  * the duration of liveness, and remove the ones we're not already going to
1871  * remove based on how long they were alive.
1872  **/
1873  qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_);
1874  for (i = 0; i < n && n_rmv < n_extra; ++i) {
1875  if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
1876  rmv[lifespans[i].idx-lo] = 1;
1877  ++n_rmv;
1878  }
1879  }
1880  }
1881 
1882  i = hi;
1883  do {
1884  if (rmv[i-lo])
1885  routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
1886  } while (--i >= lo);
1887  tor_free(must_keep);
1888  tor_free(rmv);
1889  tor_free(lifespans);
1890 }
1891 
1892 /** Deactivate any routers from the routerlist that are more than
1893  * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
1894  * remove old routers from the list of cached routers if we have too many.
1895  */
1896 void
1898 {
1899  int i, hi=-1;
1900  const char *cur_id = NULL;
1901  time_t now = time(NULL);
1902  time_t cutoff;
1903  routerinfo_t *router;
1904  signed_descriptor_t *sd;
1905  digestset_t *retain;
1907 
1909 
1910  if (!routerlist || !consensus)
1911  return;
1912 
1913  // routerlist_assert_ok(routerlist);
1914 
1915  /* We need to guess how many router descriptors we will wind up wanting to
1916  retain, so that we can be sure to allocate a large enough Bloom filter
1917  to hold the digest set. Overestimating is fine; underestimating is bad.
1918  */
1919  {
1920  /* We'll probably retain everything in the consensus. */
1921  int n_max_retain = smartlist_len(consensus->routerstatus_list);
1922  retain = digestset_new(n_max_retain);
1923  }
1924 
1925  /* Retain anything listed in the consensus. */
1926  if (consensus) {
1928  digestset_add(retain, rs->descriptor_digest));
1929  }
1930 
1931  /* If we have a consensus, we should consider pruning current routers that
1932  * are too old and that nobody recommends. (If we don't have a consensus,
1933  * then we should get one before we decide to kill routers.) */
1934 
1935  if (consensus) {
1936  cutoff = now - ROUTER_MAX_AGE;
1937  /* Remove too-old unrecommended members of routerlist->routers. */
1938  for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
1939  router = smartlist_get(routerlist->routers, i);
1940  if (router->cache_info.published_on <= cutoff &&
1941  router->cache_info.last_listed_as_valid_until < now &&
1943  router->cache_info.signed_descriptor_digest)) {
1944  /* Too old: remove it. (If we're a cache, just move it into
1945  * old_routers.) */
1946  log_info(LD_DIR,
1947  "Forgetting obsolete (too old) routerinfo for router %s",
1948  router_describe(router));
1949  routerlist_remove(routerlist, router, 1, now);
1950  i--;
1951  }
1952  }
1953  }
1954 
1955  //routerlist_assert_ok(routerlist);
1956 
1957  /* Remove far-too-old members of routerlist->old_routers. */
1958  cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
1959  for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1960  sd = smartlist_get(routerlist->old_routers, i);
1961  if (sd->published_on <= cutoff &&
1962  sd->last_listed_as_valid_until < now &&
1964  /* Too old. Remove it. */
1966  }
1967  }
1968 
1969  //routerlist_assert_ok(routerlist);
1970 
1971  log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
1972  smartlist_len(routerlist->routers),
1973  smartlist_len(routerlist->old_routers));
1974 
1975  /* Now we might have to look at routerlist->old_routers for extraneous
1976  * members. (We'd keep all the members if we could, but we need to save
1977  * space.) First, check whether we have too many router descriptors, total.
1978  * We're okay with having too many for some given router, so long as the
1979  * total number doesn't approach max_descriptors_per_router()*len(router).
1980  */
1981  if (smartlist_len(routerlist->old_routers) <
1982  smartlist_len(routerlist->routers))
1983  goto done;
1984 
1985  /* Sort by identity, then fix indices. */
1987  /* Fix indices. */
1988  for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1989  signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
1990  r->routerlist_index = i;
1991  }
1992 
1993  /* Iterate through the list from back to front, so when we remove descriptors
1994  * we don't mess up groups we haven't gotten to. */
1995  for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
1996  signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
1997  if (!cur_id) {
1998  cur_id = r->identity_digest;
1999  hi = i;
2000  }
2001  if (tor_memneq(cur_id, r->identity_digest, DIGEST_LEN)) {
2003  cutoff, i+1, hi, retain);
2004  cur_id = r->identity_digest;
2005  hi = i;
2006  }
2007  }
2008  if (hi>=0)
2009  routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
2010  //routerlist_assert_ok(routerlist);
2011 
2012  done:
2013  digestset_free(retain);
2014  router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
2015  router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
2016 }
2017 
2018 /* Drop every bridge descriptor in our routerlist. Used by the external
2019  * 'bridgestrap' tool to discard bridge descriptors so that it can then
2020  * do a clean reachability test. */
2021 void
2022 routerlist_drop_bridge_descriptors(void)
2023 {
2024  routerinfo_t *router;
2025  int i;
2026 
2027  if (!routerlist)
2028  return;
2029 
2030  for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
2031  router = smartlist_get(routerlist->routers, i);
2032  if (router->purpose == ROUTER_PURPOSE_BRIDGE) {
2033  log_notice(LD_DIR,
2034  "Dropping existing bridge descriptor for %s",
2035  router_describe(router));
2036  routerlist_remove(routerlist, router, 0, time(NULL));
2037  i--;
2038  }
2039  }
2040 }
2041 
2042 /** We just added a new set of descriptors. Take whatever extra steps
2043  * we need. */
2044 void
2046 {
2047  // XXXX use pubsub mechanism here.
2048 
2049  tor_assert(sl);
2052  if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
2053  learned_bridge_descriptor(ri, from_cache, 1);
2054  if (ri->needs_retest_if_added) {
2055  ri->needs_retest_if_added = 0;
2057  }
2058  } SMARTLIST_FOREACH_END(ri);
2059 }
2060 
2061 /**
2062  * Code to parse a single router descriptor and insert it into the
2063  * routerlist. Return -1 if the descriptor was ill-formed; 0 if the
2064  * descriptor was well-formed but could not be added; and 1 if the
2065  * descriptor was added.
2066  *
2067  * If we don't add it and <b>msg</b> is not NULL, then assign to
2068  * *<b>msg</b> a static string describing the reason for refusing the
2069  * descriptor.
2070  *
2071  * This is used only by the controller.
2072  */
2073 int
2074 router_load_single_router(const char *s, uint8_t purpose, int cache,
2075  const char **msg)
2076 {
2077  routerinfo_t *ri;
2079  smartlist_t *lst;
2080  char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
2081  tor_assert(msg);
2082  *msg = NULL;
2083 
2084  tor_snprintf(annotation_buf, sizeof(annotation_buf),
2085  "@source controller\n"
2086  "@purpose %s\n", router_purpose_to_string(purpose));
2087 
2088  if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0,
2089  annotation_buf, NULL))) {
2090  log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
2091  *msg = "Couldn't parse router descriptor.";
2092  return -1;
2093  }
2094  tor_assert(ri->purpose == purpose);
2095  if (router_is_me(ri)) {
2096  log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
2097  *msg = "Router's identity key matches mine.";
2098  routerinfo_free(ri);
2099  return 0;
2100  }
2101 
2102  if (!cache) /* obey the preference of the controller */
2103  ri->cache_info.do_not_cache = 1;
2104 
2105  lst = smartlist_new();
2106  smartlist_add(lst, ri);
2108 
2109  r = router_add_to_routerlist(ri, msg, 0, 0);
2110  if (!WRA_WAS_ADDED(r)) {
2111  /* we've already assigned to *msg now, and ri is already freed */
2112  tor_assert(*msg);
2113  if (r == ROUTER_AUTHDIR_REJECTS)
2114  log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
2115  smartlist_free(lst);
2116  return 0;
2117  } else {
2119  smartlist_free(lst);
2120  log_debug(LD_DIR, "Added router to list");
2121  return 1;
2122  }
2123 }
2124 
2125 /** Given a string <b>s</b> containing some routerdescs, parse it and put the
2126  * routers into our directory. If saved_location is SAVED_NOWHERE, the routers
2127  * are in response to a query to the network: cache them by adding them to
2128  * the journal.
2129  *
2130  * Return the number of routers actually added.
2131  *
2132  * If <b>requested_fingerprints</b> is provided, it must contain a list of
2133  * uppercased fingerprints. Do not update any router whose
2134  * fingerprint is not on the list; after updating a router, remove its
2135  * fingerprint from the list.
2136  *
2137  * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
2138  * are descriptor digests. Otherwise they are identity digests.
2139  */
2140 int
2141 router_load_routers_from_string(const char *s, const char *eos,
2142  saved_location_t saved_location,
2143  smartlist_t *requested_fingerprints,
2144  int descriptor_digests,
2145  const char *prepend_annotations)
2146 {
2147  smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
2148  char fp[HEX_DIGEST_LEN+1];
2149  const char *msg;
2150  int from_cache = (saved_location != SAVED_NOWHERE);
2151  int allow_annotations = (saved_location != SAVED_NOWHERE);
2152  int any_changed = 0;
2153  smartlist_t *invalid_digests = smartlist_new();
2154 
2155  router_parse_list_from_string(&s, eos, routers, saved_location, 0,
2156  allow_annotations, prepend_annotations,
2157  invalid_digests);
2158 
2160 
2161  log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
2162 
2163  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
2165  char d[DIGEST_LEN];
2166  if (requested_fingerprints) {
2167  base16_encode(fp, sizeof(fp), descriptor_digests ?
2168  ri->cache_info.signed_descriptor_digest :
2169  ri->cache_info.identity_digest,
2170  DIGEST_LEN);
2171  if (smartlist_contains_string(requested_fingerprints, fp)) {
2172  smartlist_string_remove(requested_fingerprints, fp);
2173  } else {
2174  char *requested =
2175  smartlist_join_strings(requested_fingerprints," ",0,NULL);
2176  log_warn(LD_DIR,
2177  "We received a router descriptor with a fingerprint (%s) "
2178  "that we never requested. (We asked for: %s.) Dropping.",
2179  fp, requested);
2180  tor_free(requested);
2181  routerinfo_free(ri);
2182  continue;
2183  }
2184  }
2185 
2186  memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
2187  r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
2188  if (WRA_WAS_ADDED(r)) {
2189  any_changed++;
2190  smartlist_add(changed, ri);
2191  routerlist_descriptors_added(changed, from_cache);
2192  smartlist_clear(changed);
2193  } else if (WRA_NEVER_DOWNLOADABLE(r)) {
2194  download_status_t *dl_status;
2196  if (dl_status) {
2197  log_info(LD_GENERAL, "Marking router %s as never downloadable",
2198  hex_str(d, DIGEST_LEN));
2200  }
2201  }
2202  } SMARTLIST_FOREACH_END(ri);
2203 
2204  SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2205  /* This digest is never going to be parseable. */
2206  base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2207  if (requested_fingerprints && descriptor_digests) {
2208  if (! smartlist_contains_string(requested_fingerprints, fp)) {
2209  /* But we didn't ask for it, so we should assume shennanegans. */
2210  continue;
2211  }
2212  smartlist_string_remove(requested_fingerprints, fp);
2213  }
2214  download_status_t *dls;
2215  dls = router_get_dl_status_by_descriptor_digest((char*)bad_digest);
2216  if (dls) {
2217  log_info(LD_GENERAL, "Marking router with descriptor %s as unparseable, "
2218  "and therefore undownloadable", fp);
2220  }
2221  } SMARTLIST_FOREACH_END(bad_digest);
2222  SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2223  smartlist_free(invalid_digests);
2224 
2226 
2227  if (any_changed)
2229 
2230  smartlist_free(routers);
2231  smartlist_free(changed);
2232 
2233  return any_changed;
2234 }
2235 
2236 /** Parse one or more extrainfos from <b>s</b> (ending immediately before
2237  * <b>eos</b> if <b>eos</b> is present). Other arguments are as for
2238  * router_load_routers_from_string(). */
2239 void
2240 router_load_extrainfo_from_string(const char *s, const char *eos,
2241  saved_location_t saved_location,
2242  smartlist_t *requested_fingerprints,
2243  int descriptor_digests)
2244 {
2245  smartlist_t *extrainfo_list = smartlist_new();
2246  const char *msg;
2247  int from_cache = (saved_location != SAVED_NOWHERE);
2248  smartlist_t *invalid_digests = smartlist_new();
2249 
2250  router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
2251  NULL, invalid_digests);
2252 
2253  log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
2254 
2255  SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) {
2256  uint8_t d[DIGEST_LEN];
2257  memcpy(d, ei->cache_info.signed_descriptor_digest, DIGEST_LEN);
2258  was_router_added_t added =
2259  router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
2260  if (WRA_WAS_ADDED(added) && requested_fingerprints) {
2261  char fp[HEX_DIGEST_LEN+1];
2262  base16_encode(fp, sizeof(fp), descriptor_digests ?
2263  ei->cache_info.signed_descriptor_digest :
2264  ei->cache_info.identity_digest,
2265  DIGEST_LEN);
2266  smartlist_string_remove(requested_fingerprints, fp);
2267  /* We silently let relays stuff us with extrainfos we didn't ask for,
2268  * so long as we would have wanted them anyway. Since we always fetch
2269  * all the extrainfos we want, and we never actually act on them
2270  * inside Tor, this should be harmless. */
2271  } else if (WRA_NEVER_DOWNLOADABLE(added)) {
2273  if (sd) {
2274  log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2275  "unparseable, and therefore undownloadable",
2276  hex_str((char*)d,DIGEST_LEN));
2278  }
2279  }
2280  } SMARTLIST_FOREACH_END(ei);
2281 
2282  SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2283  /* This digest is never going to be parseable. */
2284  char fp[HEX_DIGEST_LEN+1];
2285  base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2286  if (requested_fingerprints) {
2287  if (! smartlist_contains_string(requested_fingerprints, fp)) {
2288  /* But we didn't ask for it, so we should assume shennanegans. */
2289  continue;
2290  }
2291  smartlist_string_remove(requested_fingerprints, fp);
2292  }
2293  signed_descriptor_t *sd =
2294  router_get_by_extrainfo_digest((char*)bad_digest);
2295  if (sd) {
2296  log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2297  "unparseable, and therefore undownloadable", fp);
2299  }
2300  } SMARTLIST_FOREACH_END(bad_digest);
2301  SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2302  smartlist_free(invalid_digests);
2303 
2305  router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
2306 
2307  smartlist_free(extrainfo_list);
2308 }
2309 
2310 /** Return true iff the latest ns-flavored consensus includes a descriptor
2311  * whose digest is that of <b>desc</b>. */
2312 static int
2314 {
2315  const routerstatus_t *rs;
2317  FLAV_NS);
2318 
2319  if (consensus) {
2320  rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
2321  if (rs && tor_memeq(rs->descriptor_digest,
2323  return 1;
2324  }
2325  return 0;
2326 }
2327 
2328 /** Update downloads for router descriptors and/or microdescriptors as
2329  * appropriate. */
2330 void
2332 {
2334  return;
2337 }
2338 
2339 /** Clear all our timeouts for fetching v3 directory stuff, and then
2340  * give it all a try again. */
2341 void
2343 {
2344  (void)now;
2345 
2346  log_debug(LD_GENERAL,
2347  "In routerlist_retry_directory_downloads()");
2348 
2352 }
2353 
2354 /** Return true iff <b>router</b> does not permit exit streams.
2355  */
2356 int
2358 {
2359  return router->policy_is_reject_star;
2360 }
2361 
2362 /** For every current directory connection whose purpose is <b>purpose</b>,
2363  * and where the resource being downloaded begins with <b>prefix</b>, split
2364  * rest of the resource into base16 fingerprints (or base64 fingerprints if
2365  * purpose==DIR_PURPOSE_FETCH_MICRODESC), decode them, and set the
2366  * corresponding elements of <b>result</b> to a nonzero value.
2367  */
2368 void
2369 list_pending_downloads(digestmap_t *result, digest256map_t *result256,
2370  int purpose, const char *prefix)
2371 {
2372  const size_t p_len = strlen(prefix);
2373  smartlist_t *tmp = smartlist_new();
2374  smartlist_t *conns = get_connection_array();
2375  int flags = DSR_HEX;
2376  if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
2377  flags = DSR_DIGEST256|DSR_BASE64;
2378 
2379  tor_assert(result || result256);
2380 
2381  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
2382  if (conn->type == CONN_TYPE_DIR &&
2383  conn->purpose == purpose &&
2384  !conn->marked_for_close) {
2385  const char *resource = TO_DIR_CONN(conn)->requested_resource;
2386  if (!strcmpstart(resource, prefix))
2387  dir_split_resource_into_fingerprints(resource + p_len,
2388  tmp, NULL, flags);
2389  }
2390  } SMARTLIST_FOREACH_END(conn);
2391 
2392  if (result) {
2393  SMARTLIST_FOREACH(tmp, char *, d,
2394  {
2395  digestmap_set(result, d, (void*)1);
2396  tor_free(d);
2397  });
2398  } else if (result256) {
2399  SMARTLIST_FOREACH(tmp, uint8_t *, d,
2400  {
2401  digest256map_set(result256, d, (void*)1);
2402  tor_free(d);
2403  });
2404  }
2405  smartlist_free(tmp);
2406 }
2407 
2408 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is
2409  * true) we are currently downloading by descriptor digest, set result[d] to
2410  * (void*)1. */
2411 static void
2412 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
2413 {
2414  int purpose =
2416  list_pending_downloads(result, NULL, purpose, "d/");
2417 }
2418 
2419 /** For every microdescriptor we are currently downloading by descriptor
2420  * digest, set result[d] to (void*)1.
2421  */
2422 void
2423 list_pending_microdesc_downloads(digest256map_t *result)
2424 {
2426 }
2427 
2428 /** Launch downloads for all the descriptors whose digests or digests256
2429  * are listed as digests[i] for lo <= i < hi. (Lo and hi may be out of
2430  * range.) If <b>source</b> is given, download from <b>source</b>;
2431  * otherwise, download from an appropriate random directory server.
2432  */
2433 MOCK_IMPL(STATIC void,
2435  int purpose, smartlist_t *digests,
2436  int lo, int hi, int pds_flags))
2437 {
2438  char *resource, *cp;
2439  int digest_len, enc_digest_len;
2440  const char *sep;
2441  int b64_256;
2442  smartlist_t *tmp;
2443 
2444  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2445  /* Microdescriptors are downloaded by "-"-separated base64-encoded
2446  * 256-bit digests. */
2447  digest_len = DIGEST256_LEN;
2448  enc_digest_len = BASE64_DIGEST256_LEN + 1;
2449  sep = "-";
2450  b64_256 = 1;
2451  } else {
2452  digest_len = DIGEST_LEN;
2453  enc_digest_len = HEX_DIGEST_LEN + 1;
2454  sep = "+";
2455  b64_256 = 0;
2456  }
2457 
2458  if (lo < 0)
2459  lo = 0;
2460  if (hi > smartlist_len(digests))
2461  hi = smartlist_len(digests);
2462 
2463  if (hi-lo <= 0)
2464  return;
2465 
2466  tmp = smartlist_new();
2467 
2468  for (; lo < hi; ++lo) {
2469  cp = tor_malloc(enc_digest_len);
2470  if (b64_256) {
2471  digest256_to_base64(cp, smartlist_get(digests, lo));
2472  } else {
2473  base16_encode(cp, enc_digest_len, smartlist_get(digests, lo),
2474  digest_len);
2475  }
2476  smartlist_add(tmp, cp);
2477  }
2478 
2479  cp = smartlist_join_strings(tmp, sep, 0, NULL);
2480  tor_asprintf(&resource, "d/%s.z", cp);
2481 
2482  SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1));
2483  smartlist_free(tmp);
2484  tor_free(cp);
2485 
2486  if (source) {
2487  /* We know which authority or directory mirror we want. */
2490  directory_request_set_resource(req, resource);
2492  directory_request_free(req);
2493  } else {
2495  pds_flags, DL_WANT_ANY_DIRSERVER);
2496  }
2497  tor_free(resource);
2498 }
2499 
2500 /** Return the max number of hashes to put in a URL for a given request.
2501  */
2502 static int
2503 max_dl_per_request(const or_options_t *options, int purpose)
2504 {
2505  /* Since squid does not like URLs >= 4096 bytes we limit it to 96.
2506  * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2507  * /tor/server/d/.z) == 4026
2508  * 4026/41 (40 for the hash and 1 for the + that separates them) => 98
2509  * So use 96 because it's a nice number.
2510  *
2511  * For microdescriptors, the calculation is
2512  * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2513  * /tor/micro/d/.z) == 4027
2514  * 4027/44 (43 for the hash and 1 for the - that separates them) => 91
2515  * So use 90 because it's a nice number.
2516  */
2517  int max = 96;
2518  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2519  max = 90;
2520  }
2521  /* If we're going to tunnel our connections, we can ask for a lot more
2522  * in a request. */
2523  if (dirclient_must_use_begindir(options)) {
2524  max = 500;
2525  }
2526  return max;
2527 }
2528 
2529 /** Don't split our requests so finely that we are requesting fewer than
2530  * this number per server. (Grouping more than this at once leads to
2531  * diminishing returns.) */
2532 #define MIN_DL_PER_REQUEST 32
2533 /** To prevent a single screwy cache from confusing us by selective reply,
2534  * try to split our requests into at least this many requests. */
2535 #define MIN_REQUESTS 3
2536 /** If we want fewer than this many descriptors, wait until we
2537  * want more, or until TestingClientMaxIntervalWithoutRequest has passed. */
2538 #define MAX_DL_TO_DELAY 16
2539 
2540 /** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of
2541  * router descriptor digests or microdescriptor digest256s in
2542  * <b>downloadable</b>, decide whether to delay fetching until we have more.
2543  * If we don't want to delay, launch one or more requests to the appropriate
2544  * directory authorities.
2545  */
2546 void
2548  smartlist_t *downloadable,
2549  const routerstatus_t *source, time_t now)
2550 {
2551  const or_options_t *options = get_options();
2552  const char *descname;
2553  const int fetch_microdesc = (purpose == DIR_PURPOSE_FETCH_MICRODESC);
2554  int n_downloadable = smartlist_len(downloadable);
2555 
2556  int i, n_per_request, max_dl_per_req;
2557  const char *req_plural = "", *rtr_plural = "";
2558  int pds_flags = PDS_RETRY_IF_NO_SERVERS;
2559 
2560  tor_assert(fetch_microdesc || purpose == DIR_PURPOSE_FETCH_SERVERDESC);
2561  descname = fetch_microdesc ? "microdesc" : "routerdesc";
2562 
2563  if (!n_downloadable)
2564  return;
2565 
2566  if (!dirclient_fetches_dir_info_early(options)) {
2567  if (n_downloadable >= MAX_DL_TO_DELAY) {
2568  log_debug(LD_DIR,
2569  "There are enough downloadable %ss to launch requests.",
2570  descname);
2571  } else if (! router_have_minimum_dir_info()) {
2572  log_debug(LD_DIR,
2573  "We are only missing %d %ss, but we'll fetch anyway, since "
2574  "we don't yet have enough directory info.",
2575  n_downloadable, descname);
2576  } else {
2577 
2578  /* should delay */
2581  return;
2582 
2584  log_info(LD_DIR,
2585  "There are not many downloadable %ss, but we've "
2586  "been waiting long enough (%d seconds). Downloading.",
2587  descname,
2589  } else {
2590  log_info(LD_DIR,
2591  "There are not many downloadable %ss, but we haven't "
2592  "tried downloading descriptors recently. Downloading.",
2593  descname);
2594  }
2595  }
2596  }
2597 
2598  if (!authdir_mode(options)) {
2599  /* If we wind up going to the authorities, we want to only open one
2600  * connection to each authority at a time, so that we don't overload
2601  * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
2602  * regardless of whether we're a cache or not.
2603  *
2604  * Setting this flag can make initiate_descriptor_downloads() ignore
2605  * requests. We need to make sure that we do in fact call
2606  * update_router_descriptor_downloads() later on, once the connections
2607  * have succeeded or failed.
2608  */
2609  pds_flags |= fetch_microdesc ?
2612  }
2613 
2614  n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
2615  max_dl_per_req = max_dl_per_request(options, purpose);
2616 
2617  if (n_per_request > max_dl_per_req)
2618  n_per_request = max_dl_per_req;
2619 
2620  if (n_per_request < MIN_DL_PER_REQUEST) {
2621  n_per_request = MIN(MIN_DL_PER_REQUEST, n_downloadable);
2622  }
2623 
2624  if (n_downloadable > n_per_request)
2625  req_plural = rtr_plural = "s";
2626  else if (n_downloadable > 1)
2627  rtr_plural = "s";
2628 
2629  log_info(LD_DIR,
2630  "Launching %d request%s for %d %s%s, %d at a time",
2631  CEIL_DIV(n_downloadable, n_per_request), req_plural,
2632  n_downloadable, descname, rtr_plural, n_per_request);
2633  smartlist_sort_digests(downloadable);
2634  for (i=0; i < n_downloadable; i += n_per_request) {
2635  initiate_descriptor_downloads(source, purpose,
2636  downloadable, i, i+n_per_request,
2637  pds_flags);
2638  }
2640 }
2641 
2642 /** For any descriptor that we want that's currently listed in
2643  * <b>consensus</b>, download it as appropriate. */
2644 void
2646  networkstatus_t *consensus)
2647 {
2648  const or_options_t *options = get_options();
2649  digestmap_t *map = NULL;
2650  smartlist_t *no_longer_old = smartlist_new();
2651  smartlist_t *downloadable = smartlist_new();
2652  const routerstatus_t *source = NULL;
2653  int authdir = authdir_mode(options);
2654  int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
2655  n_inprogress=0, n_in_oldrouters=0;
2656 
2657  if (dirclient_too_idle_to_fetch_descriptors(options, now))
2658  goto done;
2659  if (!consensus)
2660  goto done;
2661 
2662  if (is_vote) {
2663  /* where's it from, so we know whom to ask for descriptors */
2664  dir_server_t *ds;
2665  networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
2666  tor_assert(voter);
2668  if (ds) {
2670  if (!source) {
2671  /* prefer to use the address in the consensus, but fall back to
2672  * the hard-coded trusted_dir_server address if we don't have a
2673  * consensus or this digest isn't in our consensus. */
2674  source = &ds->fake_status;
2675  }
2676  } else {
2677  log_warn(LD_DIR, "couldn't lookup source from vote?");
2678  }
2679  }
2680 
2681  map = digestmap_new();
2683  SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) {
2684  routerstatus_t *rs;
2685  vote_routerstatus_t *vrs;
2686  if (is_vote) {
2687  rs = &(((vote_routerstatus_t *)rsp)->status);
2688  vrs = rsp;
2689  } else {
2690  rs = rsp;
2691  vrs = NULL;
2692  }
2693  signed_descriptor_t *sd;
2695  const routerinfo_t *ri;
2696  ++n_have;
2697  if (!(ri = router_get_by_id_digest(rs->identity_digest)) ||
2698  tor_memneq(ri->cache_info.signed_descriptor_digest,
2700  /* We have a descriptor with this digest, but either there is no
2701  * entry in routerlist with the same ID (!ri), or there is one,
2702  * but the identity digest differs (memneq).
2703  */
2704  smartlist_add(no_longer_old, sd);
2705  ++n_in_oldrouters; /* We have it in old_routers. */
2706  }
2707  continue; /* We have it already. */
2708  }
2709  if (digestmap_get(map, rs->descriptor_digest)) {
2710  ++n_inprogress;
2711  continue; /* We have an in-progress download. */
2712  }
2713  if (!download_status_is_ready(&rs->dl_status, now)) {
2714  ++n_delayed; /* Not ready for retry. */
2715  continue;
2716  }
2717  if (authdir && is_vote && dirserv_would_reject_router(rs, vrs)) {
2718  ++n_would_reject;
2719  continue; /* We would throw it out immediately. */
2720  }
2721  if (!we_want_to_fetch_flavor(options, consensus->flavor) &&
2722  !client_would_use_router(rs, now)) {
2723  ++n_wouldnt_use;
2724  continue; /* We would never use it ourself. */
2725  }
2726  if (is_vote && source) {
2727  char old_digest_buf[HEX_DIGEST_LEN+1];
2728  const char *old_digest = "none";
2729  const routerinfo_t *oldrouter;
2730  oldrouter = router_get_by_id_digest(rs->identity_digest);
2731  if (oldrouter) {
2732  base16_encode(old_digest_buf, sizeof(old_digest_buf),
2733  oldrouter->cache_info.signed_descriptor_digest,
2734  DIGEST_LEN);
2735  old_digest = old_digest_buf;
2736  }
2737  log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
2740  old_digest,
2741  source->nickname, oldrouter ? "known" : "unknown");
2742  }
2743  smartlist_add(downloadable, rs->descriptor_digest);
2744  } SMARTLIST_FOREACH_END(rsp);
2745 
2746  if (!authdir_mode_v3(options)
2747  && smartlist_len(no_longer_old)) {
2749  log_info(LD_DIR, "%d router descriptors listed in consensus are "
2750  "currently in old_routers; making them current.",
2751  smartlist_len(no_longer_old));
2752  SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) {
2753  const char *msg;
2755  time_t tmp_cert_expiration_time;
2756  routerinfo_t *ri = routerlist_reparse_old(rl, sd);
2757  if (!ri) {
2758  log_warn(LD_BUG, "Failed to re-parse a router.");
2759  continue;
2760  }
2761  /* need to remember for below, since add_to_routerlist may free. */
2762  tmp_cert_expiration_time = ri->cert_expiration_time;
2763 
2764  r = router_add_to_routerlist(ri, &msg, 1, 0);
2765  if (WRA_WAS_OUTDATED(r)) {
2766  log_warn(LD_DIR, "Couldn't add re-parsed router: %s. This isn't "
2767  "usually a big deal, but you should make sure that your "
2768  "clock and timezone are set correctly.",
2769  msg?msg:"???");
2770  if (r == ROUTER_CERTS_EXPIRED) {
2771  char time_cons[ISO_TIME_LEN+1];
2772  char time_cert_expires[ISO_TIME_LEN+1];
2773  format_iso_time(time_cons, consensus->valid_after);
2774  format_iso_time(time_cert_expires, tmp_cert_expiration_time);
2775  log_warn(LD_DIR, " (I'm looking at a consensus from %s; This "
2776  "router's certificates began expiring at %s.)",
2777  time_cons, time_cert_expires);
2778  }
2779  }
2780  } SMARTLIST_FOREACH_END(sd);
2782  }
2783 
2784  log_info(LD_DIR,
2785  "%d router descriptors downloadable. %d delayed; %d present "
2786  "(%d of those were in old_routers); %d would_reject; "
2787  "%d wouldnt_use; %d in progress.",
2788  smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
2789  n_would_reject, n_wouldnt_use, n_inprogress);
2790 
2792  downloadable, source, now);
2793 
2794  digestmap_free(map, NULL);
2795  done:
2796  smartlist_free(downloadable);
2797  smartlist_free(no_longer_old);
2798 }
2799 
2800 /** Launch downloads for router status as needed. */
2801 void
2803 {
2804  const or_options_t *options = get_options();
2805  if (should_delay_dir_fetches(options, NULL))
2806  return;
2807  if (!we_fetch_router_descriptors(options))
2808  return;
2809 
2812 }
2813 
2814 /** Launch extrainfo downloads as needed. */
2815 void
2817 {
2818  const or_options_t *options = get_options();
2819  routerlist_t *rl;
2820  smartlist_t *wanted;
2821  digestmap_t *pending;
2822  int old_routers, i, max_dl_per_req;
2823  int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0, n_bogus[2] = {0,0};
2824  if (! options->DownloadExtraInfo)
2825  return;
2826  if (should_delay_dir_fetches(options, NULL))
2827  return;
2829  return;
2830 
2831  pending = digestmap_new();
2833  rl = router_get_routerlist();
2834  wanted = smartlist_new();
2835  for (old_routers = 0; old_routers < 2; ++old_routers) {
2836  smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
2837  for (i = 0; i < smartlist_len(lst); ++i) {
2838  signed_descriptor_t *sd;
2839  char *d;
2840  if (old_routers)
2841  sd = smartlist_get(lst, i);
2842  else
2843  sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
2844  if (sd->is_extrainfo)
2845  continue; /* This should never happen. */
2846  if (old_routers && !router_get_by_id_digest(sd->identity_digest))
2847  continue; /* Couldn't check the signature if we got it. */
2848  if (sd->extrainfo_is_bogus)
2849  continue;
2850  d = sd->extra_info_digest;
2851  if (tor_digest_is_zero(d)) {
2852  ++n_no_ei;
2853  continue;
2854  }
2855  if (eimap_get(rl->extra_info_map, d)) {
2856  ++n_have;
2857  continue;
2858  }
2859  if (!download_status_is_ready(&sd->ei_dl_status, now)) {
2860  ++n_delay;
2861  continue;
2862  }
2863  if (digestmap_get(pending, d)) {
2864  ++n_pending;
2865  continue;
2866  }
2867 
2869  if (sd2 != sd) {
2870  if (sd2 != NULL) {
2871  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2872  char d3[HEX_DIGEST_LEN+1], d4[HEX_DIGEST_LEN+1];
2873  base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2874  base16_encode(d2, sizeof(d2), sd2->identity_digest, DIGEST_LEN);
2875  base16_encode(d3, sizeof(d3), d, DIGEST_LEN);
2876  base16_encode(d4, sizeof(d3), sd2->extra_info_digest, DIGEST_LEN);
2877 
2878  log_info(LD_DIR, "Found an entry in %s with mismatched "
2879  "router_get_by_extrainfo_digest() value. This has ID %s "
2880  "but the entry in the map has ID %s. This has EI digest "
2881  "%s and the entry in the map has EI digest %s.",
2882  old_routers?"old_routers":"routers",
2883  d1, d2, d3, d4);
2884  } else {
2885  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2886  base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2887  base16_encode(d2, sizeof(d2), d, DIGEST_LEN);
2888 
2889  log_info(LD_DIR, "Found an entry in %s with NULL "
2890  "router_get_by_extrainfo_digest() value. This has ID %s "
2891  "and EI digest %s.",
2892  old_routers?"old_routers":"routers",
2893  d1, d2);
2894  }
2895  ++n_bogus[old_routers];
2896  continue;
2897  }
2898  smartlist_add(wanted, d);
2899  }
2900  }
2901  digestmap_free(pending, NULL);
2902 
2903  log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
2904  "with present ei, %d delaying, %d pending, %d downloadable, %d "
2905  "bogus in routers, %d bogus in old_routers",
2906  n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted),
2907  n_bogus[0], n_bogus[1]);
2908 
2909  smartlist_shuffle(wanted);
2910 
2911  max_dl_per_req = max_dl_per_request(options, DIR_PURPOSE_FETCH_EXTRAINFO);
2912  for (i = 0; i < smartlist_len(wanted); i += max_dl_per_req) {
2914  wanted, i, i+max_dl_per_req,
2916  }
2917 
2918  smartlist_free(wanted);
2919 }
2920 
2921 /** Reset the consensus and extra-info download failure count on all routers.
2922  * When we get a new consensus,
2923  * routers_update_status_from_consensus_networkstatus() will reset the
2924  * download statuses on the descriptors in that consensus.
2925  */
2926 void
2928 {
2929  log_debug(LD_GENERAL,
2930  "In router_reset_descriptor_download_failures()");
2931 
2934  if (!routerlist)
2935  return;
2936  /* We want to download *all* extra-info descriptors, not just those in
2937  * the consensus we currently have (or are about to have) */
2939  {
2940  download_status_reset(&ri->cache_info.ei_dl_status);
2941  });
2943  {
2944  download_status_reset(&sd->ei_dl_status);
2945  });
2946 }
2947 
2948 /** Any changes in a router descriptor's publication time larger than this are
2949  * automatically non-cosmetic. */
2950 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
2951 
2952 /** We allow uptime to vary from how much it ought to be by this much. */
2953 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
2954 
2955 /** Return true iff the only differences between r1 and r2 are such that
2956  * would not cause a recent (post 0.1.1.6) dirserver to republish.
2957  */
2958 int
2960 {
2961  time_t r1pub, r2pub;
2962  time_t time_difference;
2963  tor_assert(r1 && r2);
2964 
2965  /* r1 should be the one that was published first. */
2966  if (r1->cache_info.published_on > r2->cache_info.published_on) {
2967  const routerinfo_t *ri_tmp = r2;
2968  r2 = r1;
2969  r1 = ri_tmp;
2970  }
2971 
2972  /* If any key fields differ, they're different. */
2973  if (!tor_addr_eq(&r1->ipv4_addr, &r2->ipv4_addr) ||
2974  strcasecmp(r1->nickname, r2->nickname) ||
2975  r1->ipv4_orport != r2->ipv4_orport ||
2976  !tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) ||
2977  r1->ipv6_orport != r2->ipv6_orport ||
2978  r1->ipv4_dirport != r2->ipv4_dirport ||
2979  r1->purpose != r2->purpose ||
2980  r1->onion_pkey_len != r2->onion_pkey_len ||
2981  !tor_memeq(r1->onion_pkey, r2->onion_pkey, r1->onion_pkey_len) ||
2983  strcasecmp(r1->platform, r2->platform) ||
2984  (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
2985  (!r1->contact_info && r2->contact_info) ||
2986  (r1->contact_info && r2->contact_info &&
2987  strcasecmp(r1->contact_info, r2->contact_info)) ||
2988  r1->is_hibernating != r2->is_hibernating ||
2992  return 0;
2993  if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
2994  return 0;
2995  if (r1->declared_family && r2->declared_family) {
2996  int i, n;
2997  if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
2998  return 0;
2999  n = smartlist_len(r1->declared_family);
3000  for (i=0; i < n; ++i) {
3001  if (strcasecmp(smartlist_get(r1->declared_family, i),
3002  smartlist_get(r2->declared_family, i)))
3003  return 0;
3004  }
3005  }
3006 
3007  /* Did bandwidth change a lot? */
3008  if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
3009  (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
3010  return 0;
3011 
3012  /* Did the bandwidthrate or bandwidthburst change? */
3013  if ((r1->bandwidthrate != r2->bandwidthrate) ||
3014  (r1->bandwidthburst != r2->bandwidthburst))
3015  return 0;
3016 
3017  /* Has enough time passed between the publication times? */
3019  < r2->cache_info.published_on)
3020  return 0;
3021 
3022  /* Did uptime fail to increase by approximately the amount we would think,
3023  * give or take some slop? */
3024  r1pub = r1->cache_info.published_on;
3025  r2pub = r2->cache_info.published_on;
3026  time_difference = r2->uptime - (r1->uptime + (r2pub - r1pub));
3027  if (time_difference < 0)
3028  time_difference = - time_difference;
3029  if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
3030  time_difference > r1->uptime * .05 &&
3031  time_difference > r2->uptime * .05)
3032  return 0;
3033 
3034  /* Otherwise, the difference is cosmetic. */
3035  return 1;
3036 }
3037 
3038 /** Check whether <b>sd</b> describes a router descriptor compatible with the
3039  * extrainfo document <b>ei</b>.
3040  *
3041  * <b>identity_pkey</b> (which must also be provided) is RSA1024 identity key
3042  * for the router. We use it to check the signature of the extrainfo document,
3043  * if it has not already been checked.
3044  *
3045  * If no router is compatible with <b>ei</b>, <b>ei</b> should be
3046  * dropped. Return 0 for "compatible", return 1 for "reject, and inform
3047  * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
3048  * <b>msg</b> is present, set *<b>msg</b> to a description of the
3049  * incompatibility (if any).
3050  *
3051  * Set the extrainfo_is_bogus field in <b>sd</b> if the digests matched
3052  * but the extrainfo was nonetheless incompatible.
3053  **/
3054 int
3056  extrainfo_t *ei,
3057  signed_descriptor_t *sd,
3058  const char **msg)
3059 {
3060  int digest_matches, digest256_matches, r=1;
3061  tor_assert(identity_pkey);
3062  tor_assert(sd);
3063  tor_assert(ei);
3064 
3065  if (ei->bad_sig) {
3066  if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
3067  return 1;
3068  }
3069 
3070  digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest,
3072  /* Set digest256_matches to 1 if the digest is correct, or if no
3073  * digest256 was in the ri. */
3074  digest256_matches = tor_memeq(ei->digest256,
3076  digest256_matches |=
3078 
3079  /* The identity must match exactly to have been generated at the same time
3080  * by the same router. */
3081  if (tor_memneq(sd->identity_digest,
3082  ei->cache_info.identity_digest,
3083  DIGEST_LEN)) {
3084  if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
3085  goto err; /* different servers */
3086  }
3087 
3089  ei->cache_info.signing_key_cert)) {
3090  if (msg) *msg = "Extrainfo signing key cert didn't match routerinfo";
3091  goto err; /* different servers */
3092  }
3093 
3094  if (ei->pending_sig) {
3095  char signed_digest[128];
3096  if (crypto_pk_public_checksig(identity_pkey,
3097  signed_digest, sizeof(signed_digest),
3098  ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
3099  tor_memneq(signed_digest, ei->cache_info.signed_descriptor_digest,
3100  DIGEST_LEN)) {
3101  ei->bad_sig = 1;
3102  tor_free(ei->pending_sig);
3103  if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
3104  goto err; /* Bad signature, or no match. */
3105  }
3106 
3107  ei->cache_info.send_unencrypted = sd->send_unencrypted;
3108  tor_free(ei->pending_sig);
3109  }
3110 
3111  if (ei->cache_info.published_on < sd->published_on) {
3112  if (msg) *msg = "Extrainfo published time did not match routerdesc";
3113  goto err;
3114  } else if (ei->cache_info.published_on > sd->published_on) {
3115  if (msg) *msg = "Extrainfo published time did not match routerdesc";
3116  r = -1;
3117  goto err;
3118  }
3119 
3120  if (!digest256_matches && !digest_matches) {
3121  if (msg) *msg = "Neither digest256 or digest matched "
3122  "digest from routerdesc";
3123  goto err;
3124  }
3125 
3126  if (!digest256_matches) {
3127  if (msg) *msg = "Extrainfo digest did not match digest256 from routerdesc";
3128  goto err; /* Digest doesn't match declared value. */
3129  }
3130 
3131  if (!digest_matches) {
3132  if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
3133  goto err; /* Digest doesn't match declared value. */
3134  }
3135 
3136  return 0;
3137  err:
3138  if (digest_matches) {
3139  /* This signature was okay, and the digest was right: This is indeed the
3140  * corresponding extrainfo. But insanely, it doesn't match the routerinfo
3141  * that lists it. Don't try to fetch this one again. */
3142  sd->extrainfo_is_bogus = 1;
3143  }
3144 
3145  return r;
3146 }
3147 
3148 /* Does ri have a valid ntor onion key?
3149  * Valid ntor onion keys exist and have at least one non-zero byte. */
3150 int
3151 routerinfo_has_curve25519_onion_key(const routerinfo_t *ri)
3152 {
3153  if (!ri) {
3154  return 0;
3155  }
3156 
3157  if (!ri->onion_curve25519_pkey) {
3158  return 0;
3159  }
3160 
3161  if (fast_mem_is_zero((const char*)ri->onion_curve25519_pkey->public_key,
3163  return 0;
3164  }
3165 
3166  return 1;
3167 }
3168 
3169 /* Is rs running a tor version known to support EXTEND2 cells?
3170  * If allow_unknown_versions is true, return true if we can't tell
3171  * (from a versions line or a protocols line) whether it supports extend2
3172  * cells.
3173  * Otherwise, return false if the version is unknown. */
3174 int
3175 routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
3176  int allow_unknown_versions)
3177 {
3178  if (!rs) {
3179  return allow_unknown_versions;
3180  }
3181 
3182  if (!rs->pv.protocols_known) {
3183  return allow_unknown_versions;
3184  }
3185 
3186  return rs->pv.supports_extend2_cells;
3187 }
3188 
3189 /** Assert that the internal representation of <b>rl</b> is
3190  * self-consistent. */
3191 void
3193 {
3194  routerinfo_t *r2;
3195  signed_descriptor_t *sd2;
3196  if (!rl)
3197  return;
3199  r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
3200  tor_assert(r == r2);
3201  sd2 = sdmap_get(rl->desc_digest_map,
3202  r->cache_info.signed_descriptor_digest);
3203  tor_assert(&(r->cache_info) == sd2);
3204  tor_assert(r->cache_info.routerlist_index == r_sl_idx);
3205  /* XXXX
3206  *
3207  * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
3208  * commenting this out is just a band-aid.
3209  *
3210  * The problem is that, although well-behaved router descriptors
3211  * should never have the same value for their extra_info_digest, it's
3212  * possible for ill-behaved routers to claim whatever they like there.
3213  *
3214  * The real answer is to trash desc_by_eid_map and instead have
3215  * something that indicates for a given extra-info digest we want,
3216  * what its download status is. We'll do that as a part of routerlist
3217  * refactoring once consensus directories are in. For now,
3218  * this rep violation is probably harmless: an adversary can make us
3219  * reset our retry count for an extrainfo, but that's not the end
3220  * of the world. Changing the representation in 0.2.0.x would just
3221  * destabilize the codebase.
3222  if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
3223  signed_descriptor_t *sd3 =
3224  sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
3225  tor_assert(sd3 == &(r->cache_info));
3226  }
3227  */
3228  } SMARTLIST_FOREACH_END(r);
3230  r2 = rimap_get(rl->identity_map, sd->identity_digest);
3231  tor_assert(!r2 || sd != &(r2->cache_info));
3232  sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
3233  tor_assert(sd == sd2);
3234  tor_assert(sd->routerlist_index == sd_sl_idx);
3235  /* XXXX see above.
3236  if (!tor_digest_is_zero(sd->extra_info_digest)) {
3237  signed_descriptor_t *sd3 =
3238  sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
3239  tor_assert(sd3 == sd);
3240  }
3241  */
3242  } SMARTLIST_FOREACH_END(sd);
3243 
3244  RIMAP_FOREACH(rl->identity_map, d, r) {
3245  tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN));
3247  SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
3248  tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN));
3250  SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
3252  tor_assert(sd);
3253  tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN));
3255  EIMAP_FOREACH(rl->extra_info_map, d, ei) {
3256  signed_descriptor_t *sd;
3257  tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3258  d, DIGEST_LEN));
3259  sd = sdmap_get(rl->desc_by_eid_map,
3260  ei->cache_info.signed_descriptor_digest);
3261  // tor_assert(sd); // XXXX see above
3262  if (sd) {
3263  tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3265  }
3267 }
3268 
3269 /** Allocate and return a new string representing the contact info
3270  * and platform string for <b>router</b>,
3271  * surrounded by quotes and using standard C escapes.
3272  *
3273  * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
3274  * thread. Also, each call invalidates the last-returned value, so don't
3275  * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
3276  *
3277  * If <b>router</b> is NULL, it just frees its internal memory and returns.
3278  */
3279 const char *
3281 {
3282  static char *info=NULL;
3283  char *esc_contact, *esc_platform;
3284  tor_free(info);
3285 
3286  if (!router)
3287  return NULL; /* we're exiting; just free the memory we use */
3288 
3289  esc_contact = esc_for_log(router->contact_info);
3290  esc_platform = esc_for_log(router->platform);
3291 
3292  tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
3293  tor_free(esc_contact);
3294  tor_free(esc_platform);
3295 
3296  return info;
3297 }
3298 
3299 /** Helper for sorting: compare two routerinfos by their identity
3300  * digest. */
3301 static int
3302 compare_routerinfo_by_id_digest_(const void **a, const void **b)
3303 {
3304  routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
3305  return fast_memcmp(first->cache_info.identity_digest,
3306  second->cache_info.identity_digest,
3307  DIGEST_LEN);
3308 }
3309 
3310 /** Sort a list of routerinfo_t in ascending order of identity digest. */
3311 void
3313 {
3315 }
3316 
3317 /** Called when we change a node set, or when we reload the geoip IPv4 list:
3318  * recompute all country info in all configuration node sets and in the
3319  * routerlist. */
3320 void
3322 {
3323  const or_options_t *options = get_options();
3324 
3325  if (options->EntryNodes)
3327  if (options->ExitNodes)
3329  if (options->MiddleNodes)
3331  if (options->ExcludeNodes)
3333  if (options->ExcludeExitNodes)
3335  if (options->ExcludeExitNodesUnion_)
3337 
3339 }
#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:322
void learned_bridge_descriptor(routerinfo_t *ri, int from_cache, int desc_is_new)
Definition: bridges.c:956
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:926
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
int digestset_probably_contains(const digestset_t *set, const char *digest)
Definition: digestset.c:54
digestset_t * digestset_new(int max_guess)
Definition: digestset.c:30
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:1041
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1143
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:945
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:1248
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
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:215
void router_reset_status_download_failures(void)
Definition: dirlist.c:151
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.
const char * escaped(const char *s)
Definition: escape.c:126
char * esc_for_log(const char *s)
Definition: escape.c:30
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:274
#define RFTS_IGNORE_MISSING
Definition: files.h:101
file_status_t file_status(const char *filename)
Definition: files.c:212
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:554
int replace_file(const char *from, const char *to)
Definition: files.c:117
#define RFTS_BIN
Definition: files.h:99
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:590
#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:1607
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.
routerstatus_t * networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
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)
int client_would_use_router(const routerstatus_t *rs, time_t now)
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
networkstatus_t * networkstatus_get_latest_consensus(void)
download_status_t * router_get_dl_status_by_descriptor_digest(const char *d)
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
const routerstatus_t * networkstatus_vote_find_entry(networkstatus_t *ns, 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:67
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:73
#define PDS_RETRY_IF_NO_SERVERS
Definition: node_select.h:54
Node information structure.
void nodelist_refresh_countries(void)
Definition: nodelist.c:2083
void router_dir_info_changed(void)
Definition: nodelist.c:2470
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:1500
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2335
bool node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1271
node_t * nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
Definition: nodelist.c:579
int node_allows_single_hop_exits(const node_t *node)
Definition: nodelist.c:1568
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:2009
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2427
Header file for nodelist.c.
Master header file for Tor-specific functionality.
saved_location_t
Definition: or.h:614
@ SAVED_IN_JOURNAL
Definition: or.h:628
@ SAVED_NOWHERE
Definition: or.h:617
@ SAVED_IN_CACHE
Definition: or.h:621
#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:671
#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:703
Header file for rephist.c.
extrainfo_t * router_get_my_extrainfo(void)
Definition: router.c:1854
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1801
int router_is_me(const routerinfo_t *router)
Definition: router.c:1768
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:1103
void routerinfo_free_(routerinfo_t *router)
Definition: routerlist.c:918
void routerlist_free_all(void)
Definition: routerlist.c:1513
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:1811
void list_pending_microdesc_downloads(digest256map_t *result)
Definition: routerlist.c:2423
void update_consensus_router_descriptor_downloads(time_t now, int is_vote, networkstatus_t *consensus)
Definition: routerlist.c:2645
int router_exit_policy_rejects_all(const routerinfo_t *router)
Definition: routerlist.c:2357
void routerlist_free_(routerlist_t *rl)
Definition: routerlist.c:1026
int hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest, const char *nickname)
Definition: routerlist.c:718
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH
Definition: routerlist.c:650
void launch_descriptor_downloads(int purpose, smartlist_t *downloadable, const routerstatus_t *source, time_t now)
Definition: routerlist.c:2547
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:2240
static void signed_descriptor_move(signed_descriptor_t *dest, signed_descriptor_t *src)
Definition: routerlist.c:991
static void signed_descriptor_reset(signed_descriptor_t *sd)
Definition: routerlist.c:980
static void routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
Definition: routerlist.c:1332
STATIC void initiate_descriptor_downloads(const routerstatus_t *source, int purpose, smartlist_t *digests, int lo, int hi, int pds_flags)
Definition: routerlist.c:2436
void dump_routerlist_mem_usage(int severity)
Definition: routerlist.c:1058
uint32_t router_get_advertised_bandwidth(const routerinfo_t *router)
Definition: routerlist.c:641
void routerlist_retry_directory_downloads(time_t now)
Definition: routerlist.c:2342
void router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags)
Definition: routerlist.c:613
static int routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
Definition: routerlist.c:1081
int hex_digest_nickname_decode(const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
Definition: routerlist.c:682
static void signed_descriptor_free_(signed_descriptor_t *sd)
Definition: routerlist.c:965
static void extrainfo_free_void(void *e)
Definition: routerlist.c:1019
void routerlist_assert_ok(const routerlist_t *rl)
Definition: routerlist.c:3192
static void list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
Definition: routerlist.c:2412
int router_descriptor_is_older_than(const routerinfo_t *router, int seconds)
Definition: routerlist.c:1544
int router_load_single_router(const char *s, uint8_t purpose, int cache, const char **msg)
Definition: routerlist.c:2074
static routerlist_t * routerlist
Definition: routerlist.c:147
static routerinfo_t * routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
Definition: routerlist.c:1488
routerinfo_t * router_get_mutable_by_digest(const char *digest)
Definition: routerlist.c:760
STATIC was_router_added_t extrainfo_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
Definition: routerlist.c:1148
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:2503
void refresh_all_country_info(void)
Definition: routerlist.c:3321
void update_router_descriptor_downloads(time_t now)
Definition: routerlist.c:2802
int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *identity_pkey, extrainfo_t *ei, signed_descriptor_t *sd, const char **msg)
Definition: routerlist.c:3055
void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
Definition: routerlist.c:1273
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:893
was_router_added_t router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1754
static void routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old, routerinfo_t *ri_new)
Definition: routerlist.c:1385
void extrainfo_free_(extrainfo_t *extrainfo)
Definition: routerlist.c:948
int hexdigest_to_digest(const char *hexdigest, char *digest)
Definition: routerlist.c:747
#define MAX_DL_TO_DELAY
Definition: routerlist.c:2538
void list_pending_downloads(digestmap_t *result, digest256map_t *result256, int purpose, const char *prefix)
Definition: routerlist.c:2369
int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:507
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:774
#define MIN_REQUESTS
Definition: routerlist.c:2535
const char * signed_descriptor_get_annotations(const signed_descriptor_t *desc)
Definition: routerlist.c:886
signed_descriptor_t * router_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:782
void routers_sort_by_identity(smartlist_t *routers)
Definition: routerlist.c:3312
was_router_added_t router_add_to_routerlist(routerinfo_t *router, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1571
signed_descriptor_t * extrainfo_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:808
void update_extrainfo_downloads(time_t now)
Definition: routerlist.c:2816
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:829
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:2959
static int compare_old_routers_by_identity_(const void **_a, const void **_b)
Definition: routerlist.c:1775
static int compare_duration_idx_(const void *_d1, const void *_d2)
Definition: routerlist.c:1795
static int router_rebuild_store(int flags, desc_store_t *store)
Definition: routerlist.c:239
#define MIN_DL_PER_REQUEST
Definition: routerlist.c:2532
#define ROUTER_ALLOW_UPTIME_DRIFT
Definition: routerlist.c:2953
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:2045
static int router_should_rebuild_store(desc_store_t *store)
Definition: routerlist.c:174
void routerlist_remove_old_routers(void)
Definition: routerlist.c:1897
void routerlist_reset_warnings(void)
Definition: routerlist.c:1531
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Definition: routerlist.c:655
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:2313
void router_reset_descriptor_download_failures(void)
Definition: routerlist.c:2927
static void routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
Definition: routerlist.c:1238
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:2141
signed_descriptor_t * router_get_by_extrainfo_digest(const char *digest)
Definition: routerlist.c:795
static int compare_routerinfo_by_id_digest_(const void **a, const void **b)
Definition: routerlist.c:3302
static int signed_desc_append_to_journal(signed_descriptor_t *desc, desc_store_t *store)
Definition: routerlist.c:198
const char * signed_descriptor_get_body(const signed_descriptor_t *desc)
Definition: routerlist.c:878
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
Definition: routerlist.c:2950
static desc_store_t * desc_get_store(routerlist_t *rl, const signed_descriptor_t *sd)
Definition: routerlist.c:186
const routerinfo_t * routerlist_find_my_routerinfo(void)
Definition: routerlist.c:625
const char * esc_router_info(const routerinfo_t *router)
Definition: routerlist.c:3280
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2331
static signed_descriptor_t * signed_descriptor_from_routerinfo(routerinfo_t *ri)
Definition: routerlist.c:1007
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
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
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:684
unsigned int protocols_known
Definition: or.h:680
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:102
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:215
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
Routerstatus (vote entry) structure.
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20