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