tor  0.4.1.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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
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"
88 #include "feature/nodelist/routerset.h"
89 #include "feature/nodelist/torcert.h"
91 #include "feature/stats/rephist.h"
94 
95 #include "feature/dircommon/dir_connection_st.h"
96 #include "feature/dirclient/dir_server_st.h"
97 #include "feature/nodelist/document_signature_st.h"
98 #include "feature/nodelist/extrainfo_st.h"
99 #include "feature/nodelist/networkstatus_st.h"
100 #include "feature/nodelist/networkstatus_voter_info_st.h"
101 #include "feature/nodelist/node_st.h"
102 #include "feature/nodelist/routerinfo_st.h"
103 #include "feature/nodelist/routerlist_st.h"
104 #include "feature/nodelist/vote_routerstatus_st.h"
105 
106 #include "lib/crypt_ops/digestset.h"
107 
108 #ifdef HAVE_SYS_STAT_H
109 #include <sys/stat.h>
110 #endif
111 
112 // #define DEBUG_ROUTERLIST
113 
114 /****************************************************************************/
115 
116 /* Typed wrappers for different digestmap types; used to avoid type
117  * confusion. */
118 
119 DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
120 DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
121 DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
122 #define SDMAP_FOREACH(map, keyvar, valvar) \
123  DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
124  valvar)
125 #define RIMAP_FOREACH(map, keyvar, valvar) \
126  DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
127 #define EIMAP_FOREACH(map, keyvar, valvar) \
128  DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
129 #define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn))
130 #define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn))
131 #define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn))
132 
133 /* static function prototypes */
135 static const char *signed_descriptor_get_body_impl(
136  const signed_descriptor_t *desc,
137  int with_annotations);
138 static void launch_dummy_descriptor_download_as_needed(time_t now,
139  const or_options_t *options);
140 
141 /****************************************************************************/
142 
144 static routerlist_t *routerlist = NULL;
145 
149 
154 
155 /* Router descriptor storage.
156  *
157  * Routerdescs are stored in a big file, named "cached-descriptors". As new
158  * routerdescs arrive, we append them to a journal file named
159  * "cached-descriptors.new".
160  *
161  * From time to time, we replace "cached-descriptors" with a new file
162  * containing only the live, non-superseded descriptors, and clear
163  * cached-descriptors.new.
164  *
165  * On startup, we read both files.
166  */
167 
170 static int
172 {
173  if (store->store_len > (1<<16))
174  return (store->journal_len > store->store_len / 2 ||
175  store->bytes_dropped > store->store_len / 2);
176  else
177  return store->journal_len > (1<<15);
178 }
179 
182 static inline desc_store_t *
184 {
185  if (sd->is_extrainfo)
186  return &rl->extrainfo_store;
187  else
188  return &rl->desc_store;
189 }
190 
194 static int
196  desc_store_t *store)
197 {
198  char *fname = get_cachedir_fname_suffix(store->fname_base, ".new");
199  const char *body = signed_descriptor_get_body_impl(desc,1);
200  size_t len = desc->signed_descriptor_len + desc->annotations_len;
201 
202  if (append_bytes_to_file(fname, body, len, 1)) {
203  log_warn(LD_FS, "Unable to store router descriptor");
204  tor_free(fname);
205  return -1;
206  }
208  tor_free(fname);
209 
210  desc->saved_offset = store->journal_len;
211  store->journal_len += len;
212 
213  return 0;
214 }
215 
219 static int
220 compare_signed_descriptors_by_age_(const void **_a, const void **_b)
221 {
222  const signed_descriptor_t *r1 = *_a, *r2 = *_b;
223  return (int)(r1->published_on - r2->published_on);
224 }
225 
226 #define RRS_FORCE 1
227 #define RRS_DONT_REMOVE_OLD 2
228 
235 static int
237 {
238  smartlist_t *chunk_list = NULL;
239  char *fname = NULL, *fname_tmp = NULL;
240  int r = -1;
241  off_t offset = 0;
242  smartlist_t *signed_descriptors = NULL;
243  int nocache=0;
244  size_t total_expected_len = 0;
245  int had_any;
246  int force = flags & RRS_FORCE;
247 
248  if (!force && !router_should_rebuild_store(store)) {
249  r = 0;
250  goto done;
251  }
252  if (!routerlist) {
253  r = 0;
254  goto done;
255  }
256 
257  if (store->type == EXTRAINFO_STORE)
258  had_any = !eimap_isempty(routerlist->extra_info_map);
259  else
260  had_any = (smartlist_len(routerlist->routers)+
261  smartlist_len(routerlist->old_routers))>0;
262 
263  /* Don't save deadweight. */
264  if (!(flags & RRS_DONT_REMOVE_OLD))
266 
267  log_info(LD_DIR, "Rebuilding %s cache", store->description);
268 
269  fname = get_cachedir_fname(store->fname_base);
270  fname_tmp = get_cachedir_fname_suffix(store->fname_base, ".tmp");
271 
272  chunk_list = smartlist_new();
273 
274  /* We sort the routers by age to enhance locality on disk. */
275  signed_descriptors = smartlist_new();
276  if (store->type == EXTRAINFO_STORE) {
277  eimap_iter_t *iter;
278  for (iter = eimap_iter_init(routerlist->extra_info_map);
279  !eimap_iter_done(iter);
280  iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
281  const char *key;
282  extrainfo_t *ei;
283  eimap_iter_get(iter, &key, &ei);
284  smartlist_add(signed_descriptors, &ei->cache_info);
285  }
286  } else {
288  smartlist_add(signed_descriptors, sd));
290  smartlist_add(signed_descriptors, &ri->cache_info));
291  }
292 
294 
295  /* Now, add the appropriate members to chunk_list */
296  SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
297  sized_chunk_t *c;
298  const char *body = signed_descriptor_get_body_impl(sd, 1);
299  if (!body) {
300  log_warn(LD_BUG, "No descriptor available for router.");
301  goto done;
302  }
303  if (sd->do_not_cache) {
304  ++nocache;
305  continue;
306  }
307  c = tor_malloc(sizeof(sized_chunk_t));
308  c->bytes = body;
309  c->len = sd->signed_descriptor_len + sd->annotations_len;
310  total_expected_len += c->len;
311  smartlist_add(chunk_list, c);
312  } SMARTLIST_FOREACH_END(sd);
313 
314  if (write_chunks_to_file(fname_tmp, chunk_list, 1, 1)<0) {
315  log_warn(LD_FS, "Error writing router store to disk.");
316  goto done;
317  }
318 
319  /* Our mmap is now invalid. */
320  if (store->mmap) {
321  int res = tor_munmap_file(store->mmap);
322  store->mmap = NULL;
323  if (res != 0) {
324  log_warn(LD_FS, "Unable to munmap route store in %s", fname);
325  }
326  }
327 
328  if (replace_file(fname_tmp, fname)<0) {
329  log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
330  goto done;
331  }
332 
333  errno = 0;
334  store->mmap = tor_mmap_file(fname);
335  if (! store->mmap) {
336  if (errno == ERANGE) {
337  /* empty store.*/
338  if (total_expected_len) {
339  log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
340  " but when we went to mmap it, it was empty!", fname);
341  } else if (had_any) {
342  log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
343  "okay if we're just starting up after a long time. "
344  "Otherwise, it's a bug.", fname);
345  }
346  } else {
347  log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
348  }
349  }
350 
351  log_info(LD_DIR, "Reconstructing pointers into cache");
352 
353  offset = 0;
354  SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
355  if (sd->do_not_cache)
356  continue;
357  sd->saved_location = SAVED_IN_CACHE;
358  if (store->mmap) {
359  tor_free(sd->signed_descriptor_body); // sets it to null
360  sd->saved_offset = offset;
361  }
362  offset += sd->signed_descriptor_len + sd->annotations_len;
363  signed_descriptor_get_body(sd); /* reconstruct and assert */
364  } SMARTLIST_FOREACH_END(sd);
365 
366  tor_free(fname);
367  fname = get_cachedir_fname_suffix(store->fname_base, ".new");
368  write_str_to_file(fname, "", 1);
369 
370  r = 0;
371  store->store_len = (size_t) offset;
372  store->journal_len = 0;
373  store->bytes_dropped = 0;
374  done:
375  smartlist_free(signed_descriptors);
376  tor_free(fname);
377  tor_free(fname_tmp);
378  if (chunk_list) {
379  SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
380  smartlist_free(chunk_list);
381  }
382 
383  return r;
384 }
385 
389 static int
391 {
392  char *fname = NULL, *contents = NULL;
393  struct stat st;
394  int extrainfo = (store->type == EXTRAINFO_STORE);
395  store->journal_len = store->store_len = 0;
396 
397  fname = get_cachedir_fname(store->fname_base);
398 
399  if (store->mmap) {
400  /* get rid of it first */
401  int res = tor_munmap_file(store->mmap);
402  store->mmap = NULL;
403  if (res != 0) {
404  log_warn(LD_FS, "Failed to munmap %s", fname);
405  tor_free(fname);
406  return -1;
407  }
408  }
409 
410  store->mmap = tor_mmap_file(fname);
411  if (store->mmap) {
412  store->store_len = store->mmap->size;
413  if (extrainfo)
415  store->mmap->data+store->mmap->size,
416  SAVED_IN_CACHE, NULL, 0);
417  else
419  store->mmap->data+store->mmap->size,
420  SAVED_IN_CACHE, NULL, 0, NULL);
421  }
422 
423  tor_free(fname);
424  fname = get_cachedir_fname_suffix(store->fname_base, ".new");
425  /* don't load empty files - we wouldn't get any data, even if we tried */
426  if (file_status(fname) == FN_FILE)
427  contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
428  if (contents) {
429  if (extrainfo)
431  NULL, 0);
432  else
434  NULL, 0, NULL);
435  store->journal_len = (size_t) st.st_size;
436  tor_free(contents);
437  }
438 
439  tor_free(fname);
440 
441  if (store->journal_len) {
442  /* Always clear the journal on startup.*/
443  router_rebuild_store(RRS_FORCE, store);
444  } else if (!extrainfo) {
445  /* Don't cache expired routers. (This is in an else because
446  * router_rebuild_store() also calls remove_old_routers().) */
448  }
449 
450  return 0;
451 }
452 
456 int
458 {
461  return -1;
463  return -1;
464  return 0;
465 }
466 
467 /* When iterating through the routerlist, can OR address/port preference
468  * and reachability checks be skipped?
469  */
470 int
471 router_skip_or_reachability(const or_options_t *options, int try_ip_pref)
472 {
473  /* Servers always have and prefer IPv4.
474  * And if clients are checking against the firewall for reachability only,
475  * but there's no firewall, don't bother checking */
476  return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_or());
477 }
478 
479 /* When iterating through the routerlist, can Dir address/port preference
480  * and reachability checks be skipped?
481  */
482 int
483 router_skip_dir_reachability(const or_options_t *options, int try_ip_pref)
484 {
485  /* Servers always have and prefer IPv4.
486  * And if clients are checking against the firewall for reachability only,
487  * but there's no firewall, don't bother checking */
488  return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_dir());
489 }
490 
492 int
494 {
495  return r1->addr == r2->addr && r1->or_port == r2->or_port &&
496  tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) &&
497  r1->ipv6_orport == r2->ipv6_orport;
498 }
499 
503 void
505  int need_capacity, int need_guard,
506  int need_desc, int pref_addr,
507  int direct_conn)
508 {
509  const int check_reach = !router_skip_or_reachability(get_options(),
510  pref_addr);
511  /* XXXX MOVE */
512  SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
513  if (!node->is_running || !node->is_valid)
514  continue;
515  if (need_desc && !node_has_preferred_descriptor(node, direct_conn))
516  continue;
517  if (node->ri && node->ri->purpose != ROUTER_PURPOSE_GENERAL)
518  continue;
519  if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
520  continue;
521  /* Don't choose nodes if we are certain they can't do EXTEND2 cells */
522  if (node->rs && !routerstatus_version_supports_extend2_cells(node->rs, 1))
523  continue;
524  /* Don't choose nodes if we are certain they can't do ntor. */
525  if ((node->ri || node->md) && !node_has_curve25519_onion_key(node))
526  continue;
527  /* Choose a node with an OR address that matches the firewall rules */
528  if (direct_conn && check_reach &&
530  FIREWALL_OR_CONNECTION,
531  pref_addr))
532  continue;
533 
534  smartlist_add(sl, (void *)node);
535  } SMARTLIST_FOREACH_END(node);
536 }
537 
540 const routerinfo_t *
542 {
543  if (!routerlist)
544  return NULL;
545 
547  {
548  if (router_is_me(router))
549  return router;
550  });
551  return NULL;
552 }
553 
556 uint32_t
558 {
559  if (router->bandwidthcapacity < router->bandwidthrate)
560  return router->bandwidthcapacity;
561  return router->bandwidthrate;
562 }
563 
566 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
567 
570 uint32_t
572 {
573  uint32_t result = router->bandwidthcapacity;
574  if (result > router->bandwidthrate)
575  result = router->bandwidthrate;
578  return result;
579 }
580 
597 int
598 hex_digest_nickname_decode(const char *hexdigest,
599  char *digest_out,
600  char *nickname_qualifier_char_out,
601  char *nickname_out)
602 {
603  size_t len;
604 
605  tor_assert(hexdigest);
606  if (hexdigest[0] == '$')
607  ++hexdigest;
608 
609  len = strlen(hexdigest);
610  if (len < HEX_DIGEST_LEN) {
611  return -1;
612  } else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' ||
613  hexdigest[HEX_DIGEST_LEN] == '~') &&
614  len <= HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN) {
615  *nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN];
616  strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1);
617  } else if (len == HEX_DIGEST_LEN) {
618  ;
619  } else {
620  return -1;
621  }
622 
623  if (base16_decode(digest_out, DIGEST_LEN,
624  hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN)
625  return -1;
626  return 0;
627 }
628 
633 int
634 hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest,
635  const char *nickname)
636 {
637  char digest[DIGEST_LEN];
638  char nn_char='\0';
639  char nn_buf[MAX_NICKNAME_LEN+1];
640 
641  if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1)
642  return 0;
643 
644  if (nn_char == '=') {
645  return 0;
646  }
647 
648  if (nn_char == '~') {
649  if (!nickname) // XXX This seems wrong. -NM
650  return 0;
651  if (strcasecmp(nn_buf, nickname))
652  return 0;
653  }
654 
655  return tor_memeq(digest, identity_digest, DIGEST_LEN);
656 }
657 
662 int
663 hexdigest_to_digest(const char *hexdigest, char *digest)
664 {
665  if (hexdigest[0]=='$')
666  ++hexdigest;
667  if (strlen(hexdigest) < HEX_DIGEST_LEN ||
668  base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN)
669  return -1;
670  return 0;
671 }
672 
675 routerinfo_t *
676 router_get_mutable_by_digest(const char *digest)
677 {
678  tor_assert(digest);
679 
680  if (!routerlist) return NULL;
681 
682  // routerlist_assert_ok(routerlist);
683 
684  return rimap_get(routerlist->identity_map, digest);
685 }
686 
689 const routerinfo_t *
690 router_get_by_id_digest(const char *digest)
691 {
692  return router_get_mutable_by_digest(digest);
693 }
694 
699 {
700  tor_assert(digest);
701 
702  if (!routerlist) return NULL;
703 
704  return sdmap_get(routerlist->desc_digest_map, digest);
705 }
706 
711 router_get_by_extrainfo_digest,(const char *digest))
712 {
713  tor_assert(digest);
714 
715  if (!routerlist) return NULL;
716 
717  return sdmap_get(routerlist->desc_by_eid_map, digest);
718 }
719 
724 extrainfo_get_by_descriptor_digest,(const char *digest))
725 {
726  extrainfo_t *ei;
727  tor_assert(digest);
728  if (!routerlist) return NULL;
729  ei = eimap_get(routerlist->extra_info_map, digest);
730  return ei ? &ei->cache_info : NULL;
731 }
732 
744 static const char *
746  int with_annotations)
747 {
748  const char *r = NULL;
749  size_t len = desc->signed_descriptor_len;
750  off_t offset = desc->saved_offset;
751  if (with_annotations)
752  len += desc->annotations_len;
753  else
754  offset += desc->annotations_len;
755 
756  tor_assert(len > 32);
757  if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
759  if (store && store->mmap) {
760  tor_assert(desc->saved_offset + len <= store->mmap->size);
761  r = store->mmap->data + offset;
762  } else if (store) {
763  log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
764  "mmaped in our cache. Is another process running in our data "
765  "directory? Exiting.");
766  exit(1); // XXXX bad exit: should recover.
767  }
768  }
769  if (!r) /* no mmap, or not in cache. */
770  r = desc->signed_descriptor_body +
771  (with_annotations ? 0 : desc->annotations_len);
772 
773  tor_assert(r);
774  if (!with_annotations) {
775  if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) {
776  char *cp = tor_strndup(r, 64);
777  log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
778  "Is another process running in our data directory? Exiting.",
779  desc, escaped(cp));
780  exit(1); // XXXX bad exit: should recover.
781  }
782  }
783 
784  return r;
785 }
786 
793 const char *
795 {
796  return signed_descriptor_get_body_impl(desc, 0);
797 }
798 
801 const char *
803 {
804  return signed_descriptor_get_body_impl(desc, 1);
805 }
806 
808 routerlist_t *
810 {
811  if (PREDICT_UNLIKELY(!routerlist)) {
812  routerlist = tor_malloc_zero(sizeof(routerlist_t));
813  routerlist->routers = smartlist_new();
814  routerlist->old_routers = smartlist_new();
815  routerlist->identity_map = rimap_new();
816  routerlist->desc_digest_map = sdmap_new();
817  routerlist->desc_by_eid_map = sdmap_new();
818  routerlist->extra_info_map = eimap_new();
819 
820  routerlist->desc_store.fname_base = "cached-descriptors";
821  routerlist->extrainfo_store.fname_base = "cached-extrainfo";
822 
823  routerlist->desc_store.type = ROUTER_STORE;
824  routerlist->extrainfo_store.type = EXTRAINFO_STORE;
825 
826  routerlist->desc_store.description = "router descriptors";
827  routerlist->extrainfo_store.description = "extra-info documents";
828  }
829  return routerlist;
830 }
831 
833 void
835 {
836  if (!router)
837  return;
838 
839  tor_free(router->cache_info.signed_descriptor_body);
840  tor_free(router->nickname);
841  tor_free(router->platform);
842  tor_free(router->protocol_list);
843  tor_free(router->contact_info);
844  if (router->onion_pkey)
845  tor_free(router->onion_pkey);
847  if (router->identity_pkey)
848  crypto_pk_free(router->identity_pkey);
849  tor_cert_free(router->cache_info.signing_key_cert);
850  if (router->declared_family) {
851  SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
852  smartlist_free(router->declared_family);
853  }
854  addr_policy_list_free(router->exit_policy);
855  short_policy_free(router->ipv6_exit_policy);
856 
857  memset(router, 77, sizeof(routerinfo_t));
858 
859  tor_free(router);
860 }
861 
863 void
865 {
866  if (!extrainfo)
867  return;
868  tor_cert_free(extrainfo->cache_info.signing_key_cert);
869  tor_free(extrainfo->cache_info.signed_descriptor_body);
870  tor_free(extrainfo->pending_sig);
871 
872  memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
873  tor_free(extrainfo);
874 }
875 
876 #define signed_descriptor_free(val) \
877  FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val))
878 
880 static void
882 {
883  if (!sd)
884  return;
885 
887  tor_cert_free(sd->signing_key_cert);
888 
889  memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
890  tor_free(sd);
891 }
892 
895 static void
897 {
898  tor_assert(sd);
900  tor_cert_free(sd->signing_key_cert);
901  memset(sd, 0, sizeof(*sd));
902 }
903 
906 static void
908  signed_descriptor_t *src)
909 {
910  tor_assert(dest != src);
911  /* Cleanup destination object before overwriting it.*/
913  memcpy(dest, src, sizeof(signed_descriptor_t));
914  src->signed_descriptor_body = NULL;
915  src->signing_key_cert = NULL;
916  dest->routerlist_index = -1;
917 }
918 
922 static signed_descriptor_t *
924 {
926  tor_assert(ri->purpose == ROUTER_PURPOSE_GENERAL);
927  sd = tor_malloc_zero(sizeof(signed_descriptor_t));
928  signed_descriptor_move(sd, &ri->cache_info);
929  routerinfo_free(ri);
930  return sd;
931 }
932 
934 static void
936 {
937  extrainfo_free_(e);
938 }
939 
941 void
943 {
944  if (!rl)
945  return;
946  rimap_free(rl->identity_map, NULL);
947  sdmap_free(rl->desc_digest_map, NULL);
948  sdmap_free(rl->desc_by_eid_map, NULL);
949  eimap_free(rl->extra_info_map, extrainfo_free_void);
951  routerinfo_free(r));
953  signed_descriptor_free(sd));
954  smartlist_free(rl->routers);
955  smartlist_free(rl->old_routers);
956  if (rl->desc_store.mmap) {
957  int res = tor_munmap_file(routerlist->desc_store.mmap);
958  if (res != 0) {
959  log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap");
960  }
961  }
962  if (rl->extrainfo_store.mmap) {
963  int res = tor_munmap_file(routerlist->extrainfo_store.mmap);
964  if (res != 0) {
965  log_warn(LD_FS, "Failed to munmap routerlist->extrainfo_store.mmap");
966  }
967  }
968  tor_free(rl);
969 
971 }
972 
975 void
977 {
978  uint64_t livedescs = 0;
979  uint64_t olddescs = 0;
980  if (!routerlist)
981  return;
983  livedescs += r->cache_info.signed_descriptor_len);
985  olddescs += sd->signed_descriptor_len);
986 
987  tor_log(severity, LD_DIR,
988  "In %d live descriptors: %"PRIu64" bytes. "
989  "In %d old descriptors: %"PRIu64" bytes.",
990  smartlist_len(routerlist->routers), (livedescs),
991  smartlist_len(routerlist->old_routers), (olddescs));
992 }
993 
998 static inline int
999 routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
1000 {
1001  if (idx < 0) {
1002  idx = -1;
1004  if (r == ri) {
1005  idx = r_sl_idx;
1006  break;
1007  });
1008  } else {
1009  tor_assert(idx < smartlist_len(sl));
1010  tor_assert(smartlist_get(sl, idx) == ri);
1011  };
1012  return idx;
1013 }
1014 
1020 static void
1022 {
1023  routerinfo_t *ri_old;
1024  signed_descriptor_t *sd_old;
1025  {
1026  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1027  tor_assert(ri_generated != ri);
1028  }
1029  tor_assert(ri->cache_info.routerlist_index == -1);
1030 
1031  ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
1032  tor_assert(!ri_old);
1033 
1034  sd_old = sdmap_set(rl->desc_digest_map,
1035  ri->cache_info.signed_descriptor_digest,
1036  &(ri->cache_info));
1037  if (sd_old) {
1038  int idx = sd_old->routerlist_index;
1039  sd_old->routerlist_index = -1;
1040  smartlist_del(rl->old_routers, idx);
1041  if (idx < smartlist_len(rl->old_routers)) {
1042  signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1043  d->routerlist_index = idx;
1044  }
1046  sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest);
1047  signed_descriptor_free(sd_old);
1048  }
1049 
1050  if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1051  sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
1052  &ri->cache_info);
1053  smartlist_add(rl->routers, ri);
1054  ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
1055  nodelist_set_routerinfo(ri, NULL);
1057 #ifdef DEBUG_ROUTERLIST
1059 #endif
1060 }
1061 
1066 extrainfo_insert,(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible))
1067 {
1069  const char *compatibility_error_msg;
1070  routerinfo_t *ri = rimap_get(rl->identity_map,
1071  ei->cache_info.identity_digest);
1072  signed_descriptor_t *sd =
1073  sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
1074  extrainfo_t *ei_tmp;
1075  const int severity = warn_if_incompatible ? LOG_WARN : LOG_INFO;
1076 
1077  {
1078  extrainfo_t *ei_generated = router_get_my_extrainfo();
1079  tor_assert(ei_generated != ei);
1080  }
1081 
1082  if (!ri) {
1083  /* This router is unknown; we can't even verify the signature. Give up.*/
1084  r = ROUTER_NOT_IN_CONSENSUS;
1085  goto done;
1086  }
1087  if (! sd) {
1088  /* The extrainfo router doesn't have a known routerdesc to attach it to.
1089  * This just won't work. */;
1090  static ratelim_t no_sd_ratelim = RATELIM_INIT(1800);
1091  r = ROUTER_BAD_EI;
1092  log_fn_ratelim(&no_sd_ratelim, severity, LD_BUG,
1093  "No entry found in extrainfo map.");
1094  goto done;
1095  }
1096  if (tor_memneq(ei->cache_info.signed_descriptor_digest,
1097  sd->extra_info_digest, DIGEST_LEN)) {
1098  static ratelim_t digest_mismatch_ratelim = RATELIM_INIT(1800);
1099  /* The sd we got from the map doesn't match the digest we used to look
1100  * it up. This makes no sense. */
1101  r = ROUTER_BAD_EI;
1102  log_fn_ratelim(&digest_mismatch_ratelim, severity, LD_BUG,
1103  "Mismatch in digest in extrainfo map.");
1104  goto done;
1105  }
1107  &compatibility_error_msg)) {
1108  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
1109  r = (ri->cache_info.extrainfo_is_bogus) ?
1110  ROUTER_BAD_EI : ROUTER_NOT_IN_CONSENSUS;
1111 
1112  base16_encode(d1, sizeof(d1), ri->cache_info.identity_digest, DIGEST_LEN);
1113  base16_encode(d2, sizeof(d2), ei->cache_info.identity_digest, DIGEST_LEN);
1114 
1115  log_fn(severity,LD_DIR,
1116  "router info incompatible with extra info (ri id: %s, ei id %s, "
1117  "reason: %s)", d1, d2, compatibility_error_msg);
1118 
1119  goto done;
1120  }
1121 
1122  /* Okay, if we make it here, we definitely have a router corresponding to
1123  * this extrainfo. */
1124 
1125  ei_tmp = eimap_set(rl->extra_info_map,
1126  ei->cache_info.signed_descriptor_digest,
1127  ei);
1128  r = ROUTER_ADDED_SUCCESSFULLY;
1129  if (ei_tmp) {
1131  ei_tmp->cache_info.signed_descriptor_len;
1132  extrainfo_free(ei_tmp);
1133  }
1134 
1135  done:
1136  if (r != ROUTER_ADDED_SUCCESSFULLY)
1137  extrainfo_free(ei);
1138 
1139 #ifdef DEBUG_ROUTERLIST
1141 #endif
1142  return r;
1143 }
1144 
1145 #define should_cache_old_descriptors() \
1146  directory_caches_dir_info(get_options())
1147 
1151 static void
1153 {
1154  {
1155  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1156  tor_assert(ri_generated != ri);
1157  }
1158  tor_assert(ri->cache_info.routerlist_index == -1);
1159 
1160  if (should_cache_old_descriptors() &&
1161  ri->purpose == ROUTER_PURPOSE_GENERAL &&
1162  !sdmap_get(rl->desc_digest_map,
1163  ri->cache_info.signed_descriptor_digest)) {
1165  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1166  smartlist_add(rl->old_routers, sd);
1167  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1169  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1170  } else {
1171  routerinfo_free(ri);
1172  }
1173 #ifdef DEBUG_ROUTERLIST
1175 #endif
1176 }
1177 
1186 void
1187 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
1188 {
1189  routerinfo_t *ri_tmp;
1190  extrainfo_t *ei_tmp;
1191  int idx = ri->cache_info.routerlist_index;
1192  tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1193  tor_assert(smartlist_get(rl->routers, idx) == ri);
1194 
1196 
1197  /* make sure the rephist module knows that it's not running */
1199 
1200  ri->cache_info.routerlist_index = -1;
1201  smartlist_del(rl->routers, idx);
1202  if (idx < smartlist_len(rl->routers)) {
1203  routerinfo_t *r = smartlist_get(rl->routers, idx);
1204  r->cache_info.routerlist_index = idx;
1205  }
1206 
1207  ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
1209  tor_assert(ri_tmp == ri);
1210 
1211  if (make_old && should_cache_old_descriptors() &&
1212  ri->purpose == ROUTER_PURPOSE_GENERAL) {
1213  signed_descriptor_t *sd;
1215  smartlist_add(rl->old_routers, sd);
1216  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1217  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1219  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1220  } else {
1221  signed_descriptor_t *sd_tmp;
1222  sd_tmp = sdmap_remove(rl->desc_digest_map,
1223  ri->cache_info.signed_descriptor_digest);
1224  tor_assert(sd_tmp == &(ri->cache_info));
1225  rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
1226  ei_tmp = eimap_remove(rl->extra_info_map,
1227  ri->cache_info.extra_info_digest);
1228  if (ei_tmp) {
1230  ei_tmp->cache_info.signed_descriptor_len;
1231  extrainfo_free(ei_tmp);
1232  }
1233  if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
1234  sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
1235  routerinfo_free(ri);
1236  }
1237 #ifdef DEBUG_ROUTERLIST
1239 #endif
1240 }
1241 
1245 static void
1247 {
1248  signed_descriptor_t *sd_tmp;
1249  extrainfo_t *ei_tmp;
1250  desc_store_t *store;
1251  if (idx == -1) {
1252  idx = sd->routerlist_index;
1253  }
1254  tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
1255  /* XXXX edmanm's bridge relay triggered the following assert while
1256  * running 0.2.0.12-alpha. If anybody triggers this again, see if we
1257  * can get a backtrace. */
1258  tor_assert(smartlist_get(rl->old_routers, idx) == sd);
1259  tor_assert(idx == sd->routerlist_index);
1260 
1261  sd->routerlist_index = -1;
1262  smartlist_del(rl->old_routers, idx);
1263  if (idx < smartlist_len(rl->old_routers)) {
1264  signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
1265  d->routerlist_index = idx;
1266  }
1267  sd_tmp = sdmap_remove(rl->desc_digest_map,
1269  tor_assert(sd_tmp == sd);
1270  store = desc_get_store(rl, sd);
1271  if (store)
1272  store->bytes_dropped += sd->signed_descriptor_len;
1273 
1274  ei_tmp = eimap_remove(rl->extra_info_map,
1275  sd->extra_info_digest);
1276  if (ei_tmp) {
1278  ei_tmp->cache_info.signed_descriptor_len;
1279  extrainfo_free(ei_tmp);
1280  }
1282  sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
1283 
1284  signed_descriptor_free(sd);
1285 #ifdef DEBUG_ROUTERLIST
1287 #endif
1288 }
1289 
1298 static void
1300  routerinfo_t *ri_new)
1301 {
1302  int idx;
1303  int same_descriptors;
1304 
1305  routerinfo_t *ri_tmp;
1306  extrainfo_t *ei_tmp;
1307  {
1308  const routerinfo_t *ri_generated = router_get_my_routerinfo();
1309  tor_assert(ri_generated != ri_new);
1310  }
1311  tor_assert(ri_old != ri_new);
1312  tor_assert(ri_new->cache_info.routerlist_index == -1);
1313 
1314  idx = ri_old->cache_info.routerlist_index;
1315  tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
1316  tor_assert(smartlist_get(rl->routers, idx) == ri_old);
1317 
1318  {
1319  routerinfo_t *ri_old_tmp=NULL;
1320  nodelist_set_routerinfo(ri_new, &ri_old_tmp);
1321  tor_assert(ri_old == ri_old_tmp);
1322  }
1323 
1325  if (idx >= 0) {
1326  smartlist_set(rl->routers, idx, ri_new);
1327  ri_old->cache_info.routerlist_index = -1;
1328  ri_new->cache_info.routerlist_index = idx;
1329  /* Check that ri_old is not in rl->routers anymore: */
1330  tor_assert( routerlist_find_elt_(rl->routers, ri_old, -1) == -1 );
1331  } else {
1332  log_warn(LD_BUG, "Appending entry from routerlist_replace.");
1333  routerlist_insert(rl, ri_new);
1334  return;
1335  }
1336  if (tor_memneq(ri_old->cache_info.identity_digest,
1337  ri_new->cache_info.identity_digest, DIGEST_LEN)) {
1338  /* digests don't match; digestmap_set won't replace */
1339  rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
1340  }
1341  ri_tmp = rimap_set(rl->identity_map,
1342  ri_new->cache_info.identity_digest, ri_new);
1343  tor_assert(!ri_tmp || ri_tmp == ri_old);
1344  sdmap_set(rl->desc_digest_map,
1345  ri_new->cache_info.signed_descriptor_digest,
1346  &(ri_new->cache_info));
1347 
1348  if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
1349  sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
1350  &ri_new->cache_info);
1351  }
1352 
1353  same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest,
1354  ri_new->cache_info.signed_descriptor_digest,
1355  DIGEST_LEN);
1356 
1357  if (should_cache_old_descriptors() &&
1358  ri_old->purpose == ROUTER_PURPOSE_GENERAL &&
1359  !same_descriptors) {
1360  /* ri_old is going to become a signed_descriptor_t and go into
1361  * old_routers */
1363  smartlist_add(rl->old_routers, sd);
1364  sd->routerlist_index = smartlist_len(rl->old_routers)-1;
1365  sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
1367  sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
1368  } else {
1369  /* We're dropping ri_old. */
1370  if (!same_descriptors) {
1371  /* digests don't match; The sdmap_set above didn't replace */
1372  sdmap_remove(rl->desc_digest_map,
1373  ri_old->cache_info.signed_descriptor_digest);
1374 
1375  if (tor_memneq(ri_old->cache_info.extra_info_digest,
1376  ri_new->cache_info.extra_info_digest, DIGEST_LEN)) {
1377  ei_tmp = eimap_remove(rl->extra_info_map,
1378  ri_old->cache_info.extra_info_digest);
1379  if (ei_tmp) {
1381  ei_tmp->cache_info.signed_descriptor_len;
1382  extrainfo_free(ei_tmp);
1383  }
1384  }
1385 
1386  if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
1387  sdmap_remove(rl->desc_by_eid_map,
1388  ri_old->cache_info.extra_info_digest);
1389  }
1390  }
1391  rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
1392  routerinfo_free(ri_old);
1393  }
1394 #ifdef DEBUG_ROUTERLIST
1396 #endif
1397 }
1398 
1401 static routerinfo_t *
1403 {
1404  routerinfo_t *ri;
1405  const char *body;
1406 
1408 
1411  0, 1, NULL, NULL);
1412  if (!ri)
1413  return NULL;
1414  signed_descriptor_move(&ri->cache_info, sd);
1415 
1416  routerlist_remove_old(rl, sd, -1);
1417 
1418  return ri;
1419 }
1420 
1426 void
1428 {
1429  routerlist_free(routerlist);
1430  routerlist = NULL;
1431  dirlist_free_all();
1432  if (warned_nicknames) {
1433  SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
1434  smartlist_free(warned_nicknames);
1435  warned_nicknames = NULL;
1436  }
1437  authcert_free_all();
1438 }
1439 
1442 void
1444 {
1445  if (!warned_nicknames)
1446  warned_nicknames = smartlist_new();
1447  SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
1448  smartlist_clear(warned_nicknames); /* now the list is empty. */
1449 
1451 }
1452 
1456 router_descriptor_is_older_than,(const routerinfo_t *router, int seconds))
1457 {
1458  return router->cache_info.published_on < approx_time() - seconds;
1459 }
1460 
1482 router_add_to_routerlist(routerinfo_t *router, const char **msg,
1483  int from_cache, int from_fetch)
1484 {
1485  const char *id_digest;
1486  const or_options_t *options = get_options();
1487  int authdir = authdir_mode_handles_descs(options, router->purpose);
1488  int authdir_believes_valid = 0;
1489  routerinfo_t *old_router;
1490  networkstatus_t *consensus =
1491  networkstatus_get_latest_consensus_by_flavor(FLAV_NS);
1492  int in_consensus = 0;
1493 
1494  tor_assert(msg);
1495 
1496  if (!routerlist)
1498 
1499  id_digest = router->cache_info.identity_digest;
1500 
1501  old_router = router_get_mutable_by_digest(id_digest);
1502 
1503  /* Make sure that it isn't expired. */
1504  if (router->cert_expiration_time < approx_time()) {
1505  routerinfo_free(router);
1506  *msg = "Some certs on this router are expired.";
1507  return ROUTER_CERTS_EXPIRED;
1508  }
1509 
1510  /* Make sure that we haven't already got this exact descriptor. */
1511  if (sdmap_get(routerlist->desc_digest_map,
1512  router->cache_info.signed_descriptor_digest)) {
1513  /* If we have this descriptor already and the new descriptor is a bridge
1514  * descriptor, replace it. If we had a bridge descriptor before and the
1515  * new one is not a bridge descriptor, don't replace it. */
1516 
1517  /* Only members of routerlist->identity_map can be bridges; we don't
1518  * put bridges in old_routers. */
1519  const int was_bridge = old_router &&
1520  old_router->purpose == ROUTER_PURPOSE_BRIDGE;
1521 
1522  if (routerinfo_is_a_configured_bridge(router) &&
1523  router->purpose == ROUTER_PURPOSE_BRIDGE &&
1524  !was_bridge) {
1525  log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
1526  "descriptor for router %s",
1527  router_describe(router));
1528  } else {
1529  log_info(LD_DIR,
1530  "Dropping descriptor that we already have for router %s",
1531  router_describe(router));
1532  *msg = "Router descriptor was not new.";
1533  routerinfo_free(router);
1534  return ROUTER_IS_ALREADY_KNOWN;
1535  }
1536  }
1537 
1538  if (authdir) {
1539  if (authdir_wants_to_reject_router(router, msg,
1540  !from_cache && !from_fetch,
1541  &authdir_believes_valid)) {
1542  tor_assert(*msg);
1543  routerinfo_free(router);
1544  return ROUTER_AUTHDIR_REJECTS;
1545  }
1546  } else if (from_fetch) {
1547  /* Only check the descriptor digest against the network statuses when
1548  * we are receiving in response to a fetch. */
1549 
1550  if (!signed_desc_digest_is_recognized(&router->cache_info) &&
1552  /* We asked for it, so some networkstatus must have listed it when we
1553  * did. Save it if we're a cache in case somebody else asks for it. */
1554  log_info(LD_DIR,
1555  "Received a no-longer-recognized descriptor for router %s",
1556  router_describe(router));
1557  *msg = "Router descriptor is not referenced by any network-status.";
1558 
1559  /* Only journal this desc if we want to keep old descriptors */
1560  if (!from_cache && should_cache_old_descriptors())
1561  signed_desc_append_to_journal(&router->cache_info,
1564  return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
1565  }
1566  }
1567 
1568  /* We no longer need a router with this descriptor digest. */
1569  if (consensus) {
1571  consensus, id_digest);
1572  if (rs && tor_memeq(rs->descriptor_digest,
1573  router->cache_info.signed_descriptor_digest,
1574  DIGEST_LEN)) {
1575  in_consensus = 1;
1576  }
1577  }
1578 
1579  if (router->purpose == ROUTER_PURPOSE_GENERAL &&
1580  consensus && !in_consensus && !authdir) {
1581  /* If it's a general router not listed in the consensus, then don't
1582  * consider replacing the latest router with it. */
1583  if (!from_cache && should_cache_old_descriptors())
1584  signed_desc_append_to_journal(&router->cache_info,
1587  *msg = "Skipping router descriptor: not in consensus.";
1588  return ROUTER_NOT_IN_CONSENSUS;
1589  }
1590 
1591  /* If we're reading a bridge descriptor from our cache, and we don't
1592  * recognize it as one of our currently configured bridges, drop the
1593  * descriptor. Otherwise we could end up using it as one of our entry
1594  * guards even if it isn't in our Bridge config lines. */
1595  if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
1596  !authdir_mode_bridge(options) &&
1598  log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
1599  "no bridge configured at that address.",
1600  safe_str_client(router_describe(router)));
1601  *msg = "Router descriptor was not a configured bridge.";
1602  routerinfo_free(router);
1603  return ROUTER_WAS_NOT_WANTED;
1604  }
1605 
1606  /* If we have a router with the same identity key, choose the newer one. */
1607  if (old_router) {
1608  if (!in_consensus && (router->cache_info.published_on <=
1609  old_router->cache_info.published_on)) {
1610  /* Same key, but old. This one is not listed in the consensus. */
1611  log_debug(LD_DIR, "Not-new descriptor for router %s",
1612  router_describe(router));
1613  /* Only journal this desc if we'll be serving it. */
1614  if (!from_cache && should_cache_old_descriptors())
1615  signed_desc_append_to_journal(&router->cache_info,
1618  *msg = "Router descriptor was not new.";
1619  return ROUTER_IS_ALREADY_KNOWN;
1620  } else {
1621  /* Same key, and either new, or listed in the consensus. */
1622  log_debug(LD_DIR, "Replacing entry for router %s",
1623  router_describe(router));
1624  routerlist_replace(routerlist, old_router, router);
1625  if (!from_cache) {
1626  signed_desc_append_to_journal(&router->cache_info,
1628  }
1629  *msg = authdir_believes_valid ? "Valid server updated" :
1630  ("Invalid server updated. (This dirserver is marking your "
1631  "server as unapproved.)");
1632  return ROUTER_ADDED_SUCCESSFULLY;
1633  }
1634  }
1635 
1636  if (!in_consensus && from_cache &&
1637  router_descriptor_is_older_than(router, OLD_ROUTER_DESC_MAX_AGE)) {
1638  *msg = "Router descriptor was really old.";
1639  routerinfo_free(router);
1640  return ROUTER_WAS_TOO_OLD;
1641  }
1642 
1643  /* We haven't seen a router with this identity before. Add it to the end of
1644  * the list. */
1645  routerlist_insert(routerlist, router);
1646  if (!from_cache) {
1647  signed_desc_append_to_journal(&router->cache_info,
1649  }
1650  return ROUTER_ADDED_SUCCESSFULLY;
1651 }
1652 
1659  int from_cache, int from_fetch)
1660 {
1661  was_router_added_t inserted;
1662  (void)from_fetch;
1663  if (msg) *msg = NULL;
1664  /*XXXX Do something with msg */
1665 
1666  inserted = extrainfo_insert(router_get_routerlist(), ei, !from_cache);
1667 
1668  if (WRA_WAS_ADDED(inserted) && !from_cache)
1669  signed_desc_append_to_journal(&ei->cache_info,
1671 
1672  return inserted;
1673 }
1674 
1678 static int
1679 compare_old_routers_by_identity_(const void **_a, const void **_b)
1680 {
1681  int i;
1682  const signed_descriptor_t *r1 = *_a, *r2 = *_b;
1683  if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
1684  return i;
1685  return (int)(r1->published_on - r2->published_on);
1686 }
1687 
1692  int duration;
1693  int idx;
1694  int old;
1695 };
1696 
1698 static int
1699 compare_duration_idx_(const void *_d1, const void *_d2)
1700 {
1701  const struct duration_idx_t *d1 = _d1;
1702  const struct duration_idx_t *d2 = _d2;
1703  return d1->duration - d2->duration;
1704 }
1705 
1714 static void
1716  time_t cutoff, int lo, int hi,
1717  digestset_t *retain)
1718 {
1719  int i, n = hi-lo+1;
1720  unsigned n_extra, n_rmv = 0;
1721  struct duration_idx_t *lifespans;
1722  uint8_t *rmv, *must_keep;
1724 #if 1
1725  const char *ident;
1726  tor_assert(hi < smartlist_len(lst));
1727  tor_assert(lo <= hi);
1728  ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
1729  for (i = lo+1; i <= hi; ++i) {
1730  signed_descriptor_t *r = smartlist_get(lst, i);
1732  }
1733 #endif /* 1 */
1734  /* Check whether we need to do anything at all. */
1735  {
1736  int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
1737  if (n <= mdpr)
1738  return;
1739  n_extra = n - mdpr;
1740  }
1741 
1742  lifespans = tor_calloc(n, sizeof(struct duration_idx_t));
1743  rmv = tor_calloc(n, sizeof(uint8_t));
1744  must_keep = tor_calloc(n, sizeof(uint8_t));
1745  /* Set lifespans to contain the lifespan and index of each server. */
1746  /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
1747  for (i = lo; i <= hi; ++i) {
1748  signed_descriptor_t *r = smartlist_get(lst, i);
1749  signed_descriptor_t *r_next;
1750  lifespans[i-lo].idx = i;
1751  if (r->last_listed_as_valid_until >= now ||
1752  (retain && digestset_probably_contains(retain,
1753  r->signed_descriptor_digest))) {
1754  must_keep[i-lo] = 1;
1755  }
1756  if (i < hi) {
1757  r_next = smartlist_get(lst, i+1);
1758  tor_assert(r->published_on <= r_next->published_on);
1759  lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
1760  } else {
1761  r_next = NULL;
1762  lifespans[i-lo].duration = INT_MAX;
1763  }
1764  if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
1765  ++n_rmv;
1766  lifespans[i-lo].old = 1;
1767  rmv[i-lo] = 1;
1768  }
1769  }
1770 
1771  if (n_rmv < n_extra) {
1777  qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_);
1778  for (i = 0; i < n && n_rmv < n_extra; ++i) {
1779  if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
1780  rmv[lifespans[i].idx-lo] = 1;
1781  ++n_rmv;
1782  }
1783  }
1784  }
1785 
1786  i = hi;
1787  do {
1788  if (rmv[i-lo])
1789  routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
1790  } while (--i >= lo);
1791  tor_free(must_keep);
1792  tor_free(rmv);
1793  tor_free(lifespans);
1794 }
1795 
1800 void
1802 {
1803  int i, hi=-1;
1804  const char *cur_id = NULL;
1805  time_t now = time(NULL);
1806  time_t cutoff;
1807  routerinfo_t *router;
1808  signed_descriptor_t *sd;
1809  digestset_t *retain;
1810  const networkstatus_t *consensus = networkstatus_get_latest_consensus();
1811 
1813 
1814  if (!routerlist || !consensus)
1815  return;
1816 
1817  // routerlist_assert_ok(routerlist);
1818 
1819  /* We need to guess how many router descriptors we will wind up wanting to
1820  retain, so that we can be sure to allocate a large enough Bloom filter
1821  to hold the digest set. Overestimating is fine; underestimating is bad.
1822  */
1823  {
1824  /* We'll probably retain everything in the consensus. */
1825  int n_max_retain = smartlist_len(consensus->routerstatus_list);
1826  retain = digestset_new(n_max_retain);
1827  }
1828 
1829  cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
1830  /* Retain anything listed in the consensus. */
1831  if (consensus) {
1833  if (rs->published_on >= cutoff)
1834  digestset_add(retain, rs->descriptor_digest));
1835  }
1836 
1837  /* If we have a consensus, we should consider pruning current routers that
1838  * are too old and that nobody recommends. (If we don't have a consensus,
1839  * then we should get one before we decide to kill routers.) */
1840 
1841  if (consensus) {
1842  cutoff = now - ROUTER_MAX_AGE;
1843  /* Remove too-old unrecommended members of routerlist->routers. */
1844  for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
1845  router = smartlist_get(routerlist->routers, i);
1846  if (router->cache_info.published_on <= cutoff &&
1847  router->cache_info.last_listed_as_valid_until < now &&
1849  router->cache_info.signed_descriptor_digest)) {
1850  /* Too old: remove it. (If we're a cache, just move it into
1851  * old_routers.) */
1852  log_info(LD_DIR,
1853  "Forgetting obsolete (too old) routerinfo for router %s",
1854  router_describe(router));
1855  routerlist_remove(routerlist, router, 1, now);
1856  i--;
1857  }
1858  }
1859  }
1860 
1861  //routerlist_assert_ok(routerlist);
1862 
1863  /* Remove far-too-old members of routerlist->old_routers. */
1864  cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
1865  for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1866  sd = smartlist_get(routerlist->old_routers, i);
1867  if (sd->published_on <= cutoff &&
1868  sd->last_listed_as_valid_until < now &&
1870  /* Too old. Remove it. */
1872  }
1873  }
1874 
1875  //routerlist_assert_ok(routerlist);
1876 
1877  log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
1878  smartlist_len(routerlist->routers),
1879  smartlist_len(routerlist->old_routers));
1880 
1881  /* Now we might have to look at routerlist->old_routers for extraneous
1882  * members. (We'd keep all the members if we could, but we need to save
1883  * space.) First, check whether we have too many router descriptors, total.
1884  * We're okay with having too many for some given router, so long as the
1885  * total number doesn't approach max_descriptors_per_router()*len(router).
1886  */
1887  if (smartlist_len(routerlist->old_routers) <
1888  smartlist_len(routerlist->routers))
1889  goto done;
1890 
1891  /* Sort by identity, then fix indices. */
1893  /* Fix indices. */
1894  for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
1895  signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
1896  r->routerlist_index = i;
1897  }
1898 
1899  /* Iterate through the list from back to front, so when we remove descriptors
1900  * we don't mess up groups we haven't gotten to. */
1901  for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
1902  signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
1903  if (!cur_id) {
1904  cur_id = r->identity_digest;
1905  hi = i;
1906  }
1907  if (tor_memneq(cur_id, r->identity_digest, DIGEST_LEN)) {
1909  cutoff, i+1, hi, retain);
1910  cur_id = r->identity_digest;
1911  hi = i;
1912  }
1913  }
1914  if (hi>=0)
1915  routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
1916  //routerlist_assert_ok(routerlist);
1917 
1918  done:
1919  digestset_free(retain);
1920  router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
1921  router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
1922 }
1923 
1926 void
1928 {
1929  tor_assert(sl);
1932  if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
1933  learned_bridge_descriptor(ri, from_cache);
1934  if (ri->needs_retest_if_added) {
1935  ri->needs_retest_if_added = 0;
1937  }
1938  } SMARTLIST_FOREACH_END(ri);
1939 }
1940 
1953 int
1954 router_load_single_router(const char *s, uint8_t purpose, int cache,
1955  const char **msg)
1956 {
1957  routerinfo_t *ri;
1959  smartlist_t *lst;
1960  char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
1961  tor_assert(msg);
1962  *msg = NULL;
1963 
1964  tor_snprintf(annotation_buf, sizeof(annotation_buf),
1965  "@source controller\n"
1966  "@purpose %s\n", router_purpose_to_string(purpose));
1967 
1968  if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0,
1969  annotation_buf, NULL))) {
1970  log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
1971  *msg = "Couldn't parse router descriptor.";
1972  return -1;
1973  }
1974  tor_assert(ri->purpose == purpose);
1975  if (router_is_me(ri)) {
1976  log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
1977  *msg = "Router's identity key matches mine.";
1978  routerinfo_free(ri);
1979  return 0;
1980  }
1981 
1982  if (!cache) /* obey the preference of the controller */
1983  ri->cache_info.do_not_cache = 1;
1984 
1985  lst = smartlist_new();
1986  smartlist_add(lst, ri);
1988 
1989  r = router_add_to_routerlist(ri, msg, 0, 0);
1990  if (!WRA_WAS_ADDED(r)) {
1991  /* we've already assigned to *msg now, and ri is already freed */
1992  tor_assert(*msg);
1993  if (r == ROUTER_AUTHDIR_REJECTS)
1994  log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
1995  smartlist_free(lst);
1996  return 0;
1997  } else {
1999  smartlist_free(lst);
2000  log_debug(LD_DIR, "Added router to list");
2001  return 1;
2002  }
2003 }
2004 
2020 int
2021 router_load_routers_from_string(const char *s, const char *eos,
2022  saved_location_t saved_location,
2023  smartlist_t *requested_fingerprints,
2024  int descriptor_digests,
2025  const char *prepend_annotations)
2026 {
2027  smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
2028  char fp[HEX_DIGEST_LEN+1];
2029  const char *msg;
2030  int from_cache = (saved_location != SAVED_NOWHERE);
2031  int allow_annotations = (saved_location != SAVED_NOWHERE);
2032  int any_changed = 0;
2033  smartlist_t *invalid_digests = smartlist_new();
2034 
2035  router_parse_list_from_string(&s, eos, routers, saved_location, 0,
2036  allow_annotations, prepend_annotations,
2037  invalid_digests);
2038 
2040 
2041  log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
2042 
2043  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
2045  char d[DIGEST_LEN];
2046  if (requested_fingerprints) {
2047  base16_encode(fp, sizeof(fp), descriptor_digests ?
2048  ri->cache_info.signed_descriptor_digest :
2049  ri->cache_info.identity_digest,
2050  DIGEST_LEN);
2051  if (smartlist_contains_string(requested_fingerprints, fp)) {
2052  smartlist_string_remove(requested_fingerprints, fp);
2053  } else {
2054  char *requested =
2055  smartlist_join_strings(requested_fingerprints," ",0,NULL);
2056  log_warn(LD_DIR,
2057  "We received a router descriptor with a fingerprint (%s) "
2058  "that we never requested. (We asked for: %s.) Dropping.",
2059  fp, requested);
2060  tor_free(requested);
2061  routerinfo_free(ri);
2062  continue;
2063  }
2064  }
2065 
2066  memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
2067  r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
2068  if (WRA_WAS_ADDED(r)) {
2069  any_changed++;
2070  smartlist_add(changed, ri);
2071  routerlist_descriptors_added(changed, from_cache);
2072  smartlist_clear(changed);
2073  } else if (WRA_NEVER_DOWNLOADABLE(r)) {
2074  download_status_t *dl_status;
2075  dl_status = router_get_dl_status_by_descriptor_digest(d);
2076  if (dl_status) {
2077  log_info(LD_GENERAL, "Marking router %s as never downloadable",
2078  hex_str(d, DIGEST_LEN));
2080  }
2081  }
2082  } SMARTLIST_FOREACH_END(ri);
2083 
2084  SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2085  /* This digest is never going to be parseable. */
2086  base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2087  if (requested_fingerprints && descriptor_digests) {
2088  if (! smartlist_contains_string(requested_fingerprints, fp)) {
2089  /* But we didn't ask for it, so we should assume shennanegans. */
2090  continue;
2091  }
2092  smartlist_string_remove(requested_fingerprints, fp);
2093  }
2094  download_status_t *dls;
2095  dls = router_get_dl_status_by_descriptor_digest((char*)bad_digest);
2096  if (dls) {
2097  log_info(LD_GENERAL, "Marking router with descriptor %s as unparseable, "
2098  "and therefore undownloadable", fp);
2100  }
2101  } SMARTLIST_FOREACH_END(bad_digest);
2102  SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2103  smartlist_free(invalid_digests);
2104 
2106 
2107  if (any_changed)
2109 
2110  smartlist_free(routers);
2111  smartlist_free(changed);
2112 
2113  return any_changed;
2114 }
2115 
2119 void
2120 router_load_extrainfo_from_string(const char *s, const char *eos,
2121  saved_location_t saved_location,
2122  smartlist_t *requested_fingerprints,
2123  int descriptor_digests)
2124 {
2125  smartlist_t *extrainfo_list = smartlist_new();
2126  const char *msg;
2127  int from_cache = (saved_location != SAVED_NOWHERE);
2128  smartlist_t *invalid_digests = smartlist_new();
2129 
2130  router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
2131  NULL, invalid_digests);
2132 
2133  log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
2134 
2135  SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) {
2136  uint8_t d[DIGEST_LEN];
2137  memcpy(d, ei->cache_info.signed_descriptor_digest, DIGEST_LEN);
2138  was_router_added_t added =
2139  router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
2140  if (WRA_WAS_ADDED(added) && requested_fingerprints) {
2141  char fp[HEX_DIGEST_LEN+1];
2142  base16_encode(fp, sizeof(fp), descriptor_digests ?
2143  ei->cache_info.signed_descriptor_digest :
2144  ei->cache_info.identity_digest,
2145  DIGEST_LEN);
2146  smartlist_string_remove(requested_fingerprints, fp);
2147  /* We silently let relays stuff us with extrainfos we didn't ask for,
2148  * so long as we would have wanted them anyway. Since we always fetch
2149  * all the extrainfos we want, and we never actually act on them
2150  * inside Tor, this should be harmless. */
2151  } else if (WRA_NEVER_DOWNLOADABLE(added)) {
2152  signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d);
2153  if (sd) {
2154  log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2155  "unparseable, and therefore undownloadable",
2156  hex_str((char*)d,DIGEST_LEN));
2158  }
2159  }
2160  } SMARTLIST_FOREACH_END(ei);
2161 
2162  SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
2163  /* This digest is never going to be parseable. */
2164  char fp[HEX_DIGEST_LEN+1];
2165  base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
2166  if (requested_fingerprints) {
2167  if (! smartlist_contains_string(requested_fingerprints, fp)) {
2168  /* But we didn't ask for it, so we should assume shennanegans. */
2169  continue;
2170  }
2171  smartlist_string_remove(requested_fingerprints, fp);
2172  }
2173  signed_descriptor_t *sd =
2174  router_get_by_extrainfo_digest((char*)bad_digest);
2175  if (sd) {
2176  log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
2177  "unparseable, and therefore undownloadable", fp);
2179  }
2180  } SMARTLIST_FOREACH_END(bad_digest);
2181  SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
2182  smartlist_free(invalid_digests);
2183 
2185  router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
2186 
2187  smartlist_free(extrainfo_list);
2188 }
2189 
2192 static int
2194 {
2195  const routerstatus_t *rs;
2196  networkstatus_t *consensus = networkstatus_get_latest_consensus_by_flavor(
2197  FLAV_NS);
2198 
2199  if (consensus) {
2200  rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
2201  if (rs && tor_memeq(rs->descriptor_digest,
2203  return 1;
2204  }
2205  return 0;
2206 }
2207 
2210 void
2212 {
2213  if (should_delay_dir_fetches(get_options(), NULL))
2214  return;
2217  launch_dummy_descriptor_download_as_needed(now, get_options());
2218 }
2219 
2222 void
2224 {
2225  (void)now;
2226 
2227  log_debug(LD_GENERAL,
2228  "In routerlist_retry_directory_downloads()");
2229 
2233 }
2234 
2237 int
2239 {
2240  return router->policy_is_reject_star;
2241 }
2242 
2249 void
2250 list_pending_downloads(digestmap_t *result, digest256map_t *result256,
2251  int purpose, const char *prefix)
2252 {
2253  const size_t p_len = strlen(prefix);
2254  smartlist_t *tmp = smartlist_new();
2255  smartlist_t *conns = get_connection_array();
2256  int flags = DSR_HEX;
2257  if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
2258  flags = DSR_DIGEST256|DSR_BASE64;
2259 
2260  tor_assert(result || result256);
2261 
2262  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
2263  if (conn->type == CONN_TYPE_DIR &&
2264  conn->purpose == purpose &&
2265  !conn->marked_for_close) {
2266  const char *resource = TO_DIR_CONN(conn)->requested_resource;
2267  if (!strcmpstart(resource, prefix))
2268  dir_split_resource_into_fingerprints(resource + p_len,
2269  tmp, NULL, flags);
2270  }
2271  } SMARTLIST_FOREACH_END(conn);
2272 
2273  if (result) {
2274  SMARTLIST_FOREACH(tmp, char *, d,
2275  {
2276  digestmap_set(result, d, (void*)1);
2277  tor_free(d);
2278  });
2279  } else if (result256) {
2280  SMARTLIST_FOREACH(tmp, uint8_t *, d,
2281  {
2282  digest256map_set(result256, d, (void*)1);
2283  tor_free(d);
2284  });
2285  }
2286  smartlist_free(tmp);
2287 }
2288 
2292 static void
2293 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
2294 {
2295  int purpose =
2297  list_pending_downloads(result, NULL, purpose, "d/");
2298 }
2299 
2303 void
2304 list_pending_microdesc_downloads(digest256map_t *result)
2305 {
2307 }
2308 
2314 MOCK_IMPL(STATIC void,
2315 initiate_descriptor_downloads,(const routerstatus_t *source,
2316  int purpose, smartlist_t *digests,
2317  int lo, int hi, int pds_flags))
2318 {
2319  char *resource, *cp;
2320  int digest_len, enc_digest_len;
2321  const char *sep;
2322  int b64_256;
2323  smartlist_t *tmp;
2324 
2325  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2326  /* Microdescriptors are downloaded by "-"-separated base64-encoded
2327  * 256-bit digests. */
2328  digest_len = DIGEST256_LEN;
2329  enc_digest_len = BASE64_DIGEST256_LEN + 1;
2330  sep = "-";
2331  b64_256 = 1;
2332  } else {
2333  digest_len = DIGEST_LEN;
2334  enc_digest_len = HEX_DIGEST_LEN + 1;
2335  sep = "+";
2336  b64_256 = 0;
2337  }
2338 
2339  if (lo < 0)
2340  lo = 0;
2341  if (hi > smartlist_len(digests))
2342  hi = smartlist_len(digests);
2343 
2344  if (hi-lo <= 0)
2345  return;
2346 
2347  tmp = smartlist_new();
2348 
2349  for (; lo < hi; ++lo) {
2350  cp = tor_malloc(enc_digest_len);
2351  if (b64_256) {
2352  digest256_to_base64(cp, smartlist_get(digests, lo));
2353  } else {
2354  base16_encode(cp, enc_digest_len, smartlist_get(digests, lo),
2355  digest_len);
2356  }
2357  smartlist_add(tmp, cp);
2358  }
2359 
2360  cp = smartlist_join_strings(tmp, sep, 0, NULL);
2361  tor_asprintf(&resource, "d/%s.z", cp);
2362 
2363  SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1));
2364  smartlist_free(tmp);
2365  tor_free(cp);
2366 
2367  if (source) {
2368  /* We know which authority or directory mirror we want. */
2371  directory_request_set_resource(req, resource);
2372  directory_initiate_request(req);
2373  directory_request_free(req);
2374  } else {
2375  directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource,
2376  pds_flags, DL_WANT_ANY_DIRSERVER);
2377  }
2378  tor_free(resource);
2379 }
2380 
2383 static int
2384 max_dl_per_request(const or_options_t *options, int purpose)
2385 {
2386  /* Since squid does not like URLs >= 4096 bytes we limit it to 96.
2387  * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2388  * /tor/server/d/.z) == 4026
2389  * 4026/41 (40 for the hash and 1 for the + that separates them) => 98
2390  * So use 96 because it's a nice number.
2391  *
2392  * For microdescriptors, the calculation is
2393  * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
2394  * /tor/micro/d/.z) == 4027
2395  * 4027/44 (43 for the hash and 1 for the - that separates them) => 91
2396  * So use 90 because it's a nice number.
2397  */
2398  int max = 96;
2399  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
2400  max = 90;
2401  }
2402  /* If we're going to tunnel our connections, we can ask for a lot more
2403  * in a request. */
2404  if (directory_must_use_begindir(options)) {
2405  max = 500;
2406  }
2407  return max;
2408 }
2409 
2413 #define MIN_DL_PER_REQUEST 32
2414 
2416 #define MIN_REQUESTS 3
2417 
2419 #define MAX_DL_TO_DELAY 16
2420 
2427 void
2429  smartlist_t *downloadable,
2430  const routerstatus_t *source, time_t now)
2431 {
2432  const or_options_t *options = get_options();
2433  const char *descname;
2434  const int fetch_microdesc = (purpose == DIR_PURPOSE_FETCH_MICRODESC);
2435  int n_downloadable = smartlist_len(downloadable);
2436 
2437  int i, n_per_request, max_dl_per_req;
2438  const char *req_plural = "", *rtr_plural = "";
2439  int pds_flags = PDS_RETRY_IF_NO_SERVERS;
2440 
2441  tor_assert(fetch_microdesc || purpose == DIR_PURPOSE_FETCH_SERVERDESC);
2442  descname = fetch_microdesc ? "microdesc" : "routerdesc";
2443 
2444  if (!n_downloadable)
2445  return;
2446 
2447  if (!directory_fetches_dir_info_early(options)) {
2448  if (n_downloadable >= MAX_DL_TO_DELAY) {
2449  log_debug(LD_DIR,
2450  "There are enough downloadable %ss to launch requests.",
2451  descname);
2452  } else if (! router_have_minimum_dir_info()) {
2453  log_debug(LD_DIR,
2454  "We are only missing %d %ss, but we'll fetch anyway, since "
2455  "we don't yet have enough directory info.",
2456  n_downloadable, descname);
2457  } else {
2458 
2459  /* should delay */
2462  return;
2463 
2465  log_info(LD_DIR,
2466  "There are not many downloadable %ss, but we've "
2467  "been waiting long enough (%d seconds). Downloading.",
2468  descname,
2470  } else {
2471  log_info(LD_DIR,
2472  "There are not many downloadable %ss, but we haven't "
2473  "tried downloading descriptors recently. Downloading.",
2474  descname);
2475  }
2476  }
2477  }
2478 
2479  if (!authdir_mode(options)) {
2480  /* If we wind up going to the authorities, we want to only open one
2481  * connection to each authority at a time, so that we don't overload
2482  * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
2483  * regardless of whether we're a cache or not.
2484  *
2485  * Setting this flag can make initiate_descriptor_downloads() ignore
2486  * requests. We need to make sure that we do in fact call
2487  * update_router_descriptor_downloads() later on, once the connections
2488  * have succeeded or failed.
2489  */
2490  pds_flags |= fetch_microdesc ?
2493  }
2494 
2495  n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
2496  max_dl_per_req = max_dl_per_request(options, purpose);
2497 
2498  if (n_per_request > max_dl_per_req)
2499  n_per_request = max_dl_per_req;
2500 
2501  if (n_per_request < MIN_DL_PER_REQUEST) {
2502  n_per_request = MIN(MIN_DL_PER_REQUEST, n_downloadable);
2503  }
2504 
2505  if (n_downloadable > n_per_request)
2506  req_plural = rtr_plural = "s";
2507  else if (n_downloadable > 1)
2508  rtr_plural = "s";
2509 
2510  log_info(LD_DIR,
2511  "Launching %d request%s for %d %s%s, %d at a time",
2512  CEIL_DIV(n_downloadable, n_per_request), req_plural,
2513  n_downloadable, descname, rtr_plural, n_per_request);
2514  smartlist_sort_digests(downloadable);
2515  for (i=0; i < n_downloadable; i += n_per_request) {
2516  initiate_descriptor_downloads(source, purpose,
2517  downloadable, i, i+n_per_request,
2518  pds_flags);
2519  }
2521 }
2522 
2525 void
2527  networkstatus_t *consensus)
2528 {
2529  const or_options_t *options = get_options();
2530  digestmap_t *map = NULL;
2531  smartlist_t *no_longer_old = smartlist_new();
2532  smartlist_t *downloadable = smartlist_new();
2533  routerstatus_t *source = NULL;
2534  int authdir = authdir_mode(options);
2535  int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
2536  n_inprogress=0, n_in_oldrouters=0;
2537 
2538  if (directory_too_idle_to_fetch_descriptors(options, now))
2539  goto done;
2540  if (!consensus)
2541  goto done;
2542 
2543  if (is_vote) {
2544  /* where's it from, so we know whom to ask for descriptors */
2545  dir_server_t *ds;
2546  networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
2547  tor_assert(voter);
2548  ds = trusteddirserver_get_by_v3_auth_digest(voter->identity_digest);
2549  if (ds)
2550  source = &(ds->fake_status);
2551  else
2552  log_warn(LD_DIR, "couldn't lookup source from vote?");
2553  }
2554 
2555  map = digestmap_new();
2557  SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) {
2558  routerstatus_t *rs =
2559  is_vote ? &(((vote_routerstatus_t *)rsp)->status) : rsp;
2560  signed_descriptor_t *sd;
2562  const routerinfo_t *ri;
2563  ++n_have;
2564  if (!(ri = router_get_by_id_digest(rs->identity_digest)) ||
2565  tor_memneq(ri->cache_info.signed_descriptor_digest,
2566  sd->signed_descriptor_digest, DIGEST_LEN)) {
2567  /* We have a descriptor with this digest, but either there is no
2568  * entry in routerlist with the same ID (!ri), or there is one,
2569  * but the identity digest differs (memneq).
2570  */
2571  smartlist_add(no_longer_old, sd);
2572  ++n_in_oldrouters; /* We have it in old_routers. */
2573  }
2574  continue; /* We have it already. */
2575  }
2576  if (digestmap_get(map, rs->descriptor_digest)) {
2577  ++n_inprogress;
2578  continue; /* We have an in-progress download. */
2579  }
2580  if (!download_status_is_ready(&rs->dl_status, now)) {
2581  ++n_delayed; /* Not ready for retry. */
2582  continue;
2583  }
2584  if (authdir && dirserv_would_reject_router(rs)) {
2585  ++n_would_reject;
2586  continue; /* We would throw it out immediately. */
2587  }
2588  if (!we_want_to_fetch_flavor(options, consensus->flavor) &&
2589  !client_would_use_router(rs, now)) {
2590  ++n_wouldnt_use;
2591  continue; /* We would never use it ourself. */
2592  }
2593  if (is_vote && source) {
2594  char time_bufnew[ISO_TIME_LEN+1];
2595  char time_bufold[ISO_TIME_LEN+1];
2596  const routerinfo_t *oldrouter;
2597  oldrouter = router_get_by_id_digest(rs->identity_digest);
2598  format_iso_time(time_bufnew, rs->published_on);
2599  if (oldrouter)
2600  format_iso_time(time_bufold, oldrouter->cache_info.published_on);
2601  log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
2603  time_bufnew,
2604  oldrouter ? time_bufold : "none",
2605  source->nickname, oldrouter ? "known" : "unknown");
2606  }
2607  smartlist_add(downloadable, rs->descriptor_digest);
2608  } SMARTLIST_FOREACH_END(rsp);
2609 
2610  if (!authdir_mode_v3(options)
2611  && smartlist_len(no_longer_old)) {
2613  log_info(LD_DIR, "%d router descriptors listed in consensus are "
2614  "currently in old_routers; making them current.",
2615  smartlist_len(no_longer_old));
2616  SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) {
2617  const char *msg;
2619  time_t tmp_cert_expiration_time;
2620  routerinfo_t *ri = routerlist_reparse_old(rl, sd);
2621  if (!ri) {
2622  log_warn(LD_BUG, "Failed to re-parse a router.");
2623  continue;
2624  }
2625  /* need to remember for below, since add_to_routerlist may free. */
2626  tmp_cert_expiration_time = ri->cert_expiration_time;
2627 
2628  r = router_add_to_routerlist(ri, &msg, 1, 0);
2629  if (WRA_WAS_OUTDATED(r)) {
2630  log_warn(LD_DIR, "Couldn't add re-parsed router: %s. This isn't "
2631  "usually a big deal, but you should make sure that your "
2632  "clock and timezone are set correctly.",
2633  msg?msg:"???");
2634  if (r == ROUTER_CERTS_EXPIRED) {
2635  char time_cons[ISO_TIME_LEN+1];
2636  char time_cert_expires[ISO_TIME_LEN+1];
2637  format_iso_time(time_cons, consensus->valid_after);
2638  format_iso_time(time_cert_expires, tmp_cert_expiration_time);
2639  log_warn(LD_DIR, " (I'm looking at a consensus from %s; This "
2640  "router's certificates began expiring at %s.)",
2641  time_cons, time_cert_expires);
2642  }
2643  }
2644  } SMARTLIST_FOREACH_END(sd);
2646  }
2647 
2648  log_info(LD_DIR,
2649  "%d router descriptors downloadable. %d delayed; %d present "
2650  "(%d of those were in old_routers); %d would_reject; "
2651  "%d wouldnt_use; %d in progress.",
2652  smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
2653  n_would_reject, n_wouldnt_use, n_inprogress);
2654 
2656  downloadable, source, now);
2657 
2658  digestmap_free(map, NULL);
2659  done:
2660  smartlist_free(downloadable);
2661  smartlist_free(no_longer_old);
2662 }
2663 
2666 /*XXXX+ this info should come from netinfo cells or something, or we should
2667  * do this only when we aren't seeing incoming data. see bug 652. */
2668 #define DUMMY_DOWNLOAD_INTERVAL (20*60)
2669 
2672 static void
2674  const or_options_t *options)
2675 {
2676  static time_t last_dummy_download = 0;
2677  /* XXXX+ we could be smarter here; see notes on bug 652. */
2678  /* If we're a server that doesn't have a configured address, we rely on
2679  * directory fetches to learn when our address changes. So if we haven't
2680  * tried to get any routerdescs in a long time, try a dummy fetch now. */
2681  if (!options->Address &&
2682  server_mode(options) &&
2684  last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
2685  last_dummy_download = now;
2686  /* XX/teor - do we want an authority here, because they are less likely
2687  * to give us the wrong address? (See #17782)
2688  * I'm leaving the previous behaviour intact, because I don't like
2689  * the idea of some relays contacting an authority every 20 minutes. */
2690  directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
2691  ROUTER_PURPOSE_GENERAL, "authority.z",
2693  DL_WANT_ANY_DIRSERVER);
2694  }
2695 }
2696 
2698 void
2700 {
2701  const or_options_t *options = get_options();
2702  if (should_delay_dir_fetches(options, NULL))
2703  return;
2704  if (!we_fetch_router_descriptors(options))
2705  return;
2706 
2708  networkstatus_get_reasonably_live_consensus(now, FLAV_NS));
2709 }
2710 
2712 void
2714 {
2715  const or_options_t *options = get_options();
2716  routerlist_t *rl;
2717  smartlist_t *wanted;
2718  digestmap_t *pending;
2719  int old_routers, i, max_dl_per_req;
2720  int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0, n_bogus[2] = {0,0};
2721  if (! options->DownloadExtraInfo)
2722  return;
2723  if (should_delay_dir_fetches(options, NULL))
2724  return;
2725  if (!router_have_minimum_dir_info())
2726  return;
2727 
2728  pending = digestmap_new();
2730  rl = router_get_routerlist();
2731  wanted = smartlist_new();
2732  for (old_routers = 0; old_routers < 2; ++old_routers) {
2733  smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
2734  for (i = 0; i < smartlist_len(lst); ++i) {
2735  signed_descriptor_t *sd;
2736  char *d;
2737  if (old_routers)
2738  sd = smartlist_get(lst, i);
2739  else
2740  sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
2741  if (sd->is_extrainfo)
2742  continue; /* This should never happen. */
2743  if (old_routers && !router_get_by_id_digest(sd->identity_digest))
2744  continue; /* Couldn't check the signature if we got it. */
2745  if (sd->extrainfo_is_bogus)
2746  continue;
2747  d = sd->extra_info_digest;
2748  if (tor_digest_is_zero(d)) {
2749  ++n_no_ei;
2750  continue;
2751  }
2752  if (eimap_get(rl->extra_info_map, d)) {
2753  ++n_have;
2754  continue;
2755  }
2756  if (!download_status_is_ready(&sd->ei_dl_status, now)) {
2757  ++n_delay;
2758  continue;
2759  }
2760  if (digestmap_get(pending, d)) {
2761  ++n_pending;
2762  continue;
2763  }
2764 
2765  const signed_descriptor_t *sd2 = router_get_by_extrainfo_digest(d);
2766  if (sd2 != sd) {
2767  if (sd2 != NULL) {
2768  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2769  char d3[HEX_DIGEST_LEN+1], d4[HEX_DIGEST_LEN+1];
2770  base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2771  base16_encode(d2, sizeof(d2), sd2->identity_digest, DIGEST_LEN);
2772  base16_encode(d3, sizeof(d3), d, DIGEST_LEN);
2773  base16_encode(d4, sizeof(d3), sd2->extra_info_digest, DIGEST_LEN);
2774 
2775  log_info(LD_DIR, "Found an entry in %s with mismatched "
2776  "router_get_by_extrainfo_digest() value. This has ID %s "
2777  "but the entry in the map has ID %s. This has EI digest "
2778  "%s and the entry in the map has EI digest %s.",
2779  old_routers?"old_routers":"routers",
2780  d1, d2, d3, d4);
2781  } else {
2782  char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
2783  base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
2784  base16_encode(d2, sizeof(d2), d, DIGEST_LEN);
2785 
2786  log_info(LD_DIR, "Found an entry in %s with NULL "
2787  "router_get_by_extrainfo_digest() value. This has ID %s "
2788  "and EI digest %s.",
2789  old_routers?"old_routers":"routers",
2790  d1, d2);
2791  }
2792  ++n_bogus[old_routers];
2793  continue;
2794  }
2795  smartlist_add(wanted, d);
2796  }
2797  }
2798  digestmap_free(pending, NULL);
2799 
2800  log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
2801  "with present ei, %d delaying, %d pending, %d downloadable, %d "
2802  "bogus in routers, %d bogus in old_routers",
2803  n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted),
2804  n_bogus[0], n_bogus[1]);
2805 
2806  smartlist_shuffle(wanted);
2807 
2808  max_dl_per_req = max_dl_per_request(options, DIR_PURPOSE_FETCH_EXTRAINFO);
2809  for (i = 0; i < smartlist_len(wanted); i += max_dl_per_req) {
2810  initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
2811  wanted, i, i+max_dl_per_req,
2813  }
2814 
2815  smartlist_free(wanted);
2816 }
2817 
2823 void
2825 {
2826  log_debug(LD_GENERAL,
2827  "In router_reset_descriptor_download_failures()");
2828 
2831  if (!routerlist)
2832  return;
2833  /* We want to download *all* extra-info descriptors, not just those in
2834  * the consensus we currently have (or are about to have) */
2836  {
2837  download_status_reset(&ri->cache_info.ei_dl_status);
2838  });
2840  {
2841  download_status_reset(&sd->ei_dl_status);
2842  });
2843 }
2844 
2847 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
2848 
2850 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
2851 
2855 int
2857 {
2858  time_t r1pub, r2pub;
2859  long time_difference;
2860  tor_assert(r1 && r2);
2861 
2862  /* r1 should be the one that was published first. */
2863  if (r1->cache_info.published_on > r2->cache_info.published_on) {
2864  const routerinfo_t *ri_tmp = r2;
2865  r2 = r1;
2866  r1 = ri_tmp;
2867  }
2868 
2869  /* If any key fields differ, they're different. */
2870  if (r1->addr != r2->addr ||
2871  strcasecmp(r1->nickname, r2->nickname) ||
2872  r1->or_port != r2->or_port ||
2873  !tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) ||
2874  r1->ipv6_orport != r2->ipv6_orport ||
2875  r1->dir_port != r2->dir_port ||
2876  r1->purpose != r2->purpose ||
2877  r1->onion_pkey_len != r2->onion_pkey_len ||
2878  !tor_memeq(r1->onion_pkey, r2->onion_pkey, r1->onion_pkey_len) ||
2880  strcasecmp(r1->platform, r2->platform) ||
2881  (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
2882  (!r1->contact_info && r2->contact_info) ||
2883  (r1->contact_info && r2->contact_info &&
2884  strcasecmp(r1->contact_info, r2->contact_info)) ||
2885  r1->is_hibernating != r2->is_hibernating ||
2889  return 0;
2890  if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
2891  return 0;
2892  if (r1->declared_family && r2->declared_family) {
2893  int i, n;
2894  if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
2895  return 0;
2896  n = smartlist_len(r1->declared_family);
2897  for (i=0; i < n; ++i) {
2898  if (strcasecmp(smartlist_get(r1->declared_family, i),
2899  smartlist_get(r2->declared_family, i)))
2900  return 0;
2901  }
2902  }
2903 
2904  /* Did bandwidth change a lot? */
2905  if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
2906  (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
2907  return 0;
2908 
2909  /* Did the bandwidthrate or bandwidthburst change? */
2910  if ((r1->bandwidthrate != r2->bandwidthrate) ||
2911  (r1->bandwidthburst != r2->bandwidthburst))
2912  return 0;
2913 
2914  /* Did more than 12 hours pass? */
2916  < r2->cache_info.published_on)
2917  return 0;
2918 
2919  /* Did uptime fail to increase by approximately the amount we would think,
2920  * give or take some slop? */
2921  r1pub = r1->cache_info.published_on;
2922  r2pub = r2->cache_info.published_on;
2923  time_difference = labs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
2924  if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
2925  time_difference > r1->uptime * .05 &&
2926  time_difference > r2->uptime * .05)
2927  return 0;
2928 
2929  /* Otherwise, the difference is cosmetic. */
2930  return 1;
2931 }
2932 
2949 int
2951  extrainfo_t *ei,
2952  signed_descriptor_t *sd,
2953  const char **msg)
2954 {
2955  int digest_matches, digest256_matches, r=1;
2956  tor_assert(identity_pkey);
2957  tor_assert(sd);
2958  tor_assert(ei);
2959 
2960  if (ei->bad_sig) {
2961  if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
2962  return 1;
2963  }
2964 
2965  digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest,
2967  /* Set digest256_matches to 1 if the digest is correct, or if no
2968  * digest256 was in the ri. */
2969  digest256_matches = tor_memeq(ei->digest256,
2971  digest256_matches |=
2973 
2974  /* The identity must match exactly to have been generated at the same time
2975  * by the same router. */
2976  if (tor_memneq(sd->identity_digest,
2977  ei->cache_info.identity_digest,
2978  DIGEST_LEN)) {
2979  if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
2980  goto err; /* different servers */
2981  }
2982 
2984  ei->cache_info.signing_key_cert)) {
2985  if (msg) *msg = "Extrainfo signing key cert didn't match routerinfo";
2986  goto err; /* different servers */
2987  }
2988 
2989  if (ei->pending_sig) {
2990  char signed_digest[128];
2991  if (crypto_pk_public_checksig(identity_pkey,
2992  signed_digest, sizeof(signed_digest),
2993  ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
2994  tor_memneq(signed_digest, ei->cache_info.signed_descriptor_digest,
2995  DIGEST_LEN)) {
2996  ei->bad_sig = 1;
2997  tor_free(ei->pending_sig);
2998  if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
2999  goto err; /* Bad signature, or no match. */
3000  }
3001 
3002  ei->cache_info.send_unencrypted = sd->send_unencrypted;
3003  tor_free(ei->pending_sig);
3004  }
3005 
3006  if (ei->cache_info.published_on < sd->published_on) {
3007  if (msg) *msg = "Extrainfo published time did not match routerdesc";
3008  goto err;
3009  } else if (ei->cache_info.published_on > sd->published_on) {
3010  if (msg) *msg = "Extrainfo published time did not match routerdesc";
3011  r = -1;
3012  goto err;
3013  }
3014 
3015  if (!digest256_matches && !digest_matches) {
3016  if (msg) *msg = "Neither digest256 or digest matched "
3017  "digest from routerdesc";
3018  goto err;
3019  }
3020 
3021  if (!digest256_matches) {
3022  if (msg) *msg = "Extrainfo digest did not match digest256 from routerdesc";
3023  goto err; /* Digest doesn't match declared value. */
3024  }
3025 
3026  if (!digest_matches) {
3027  if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
3028  goto err; /* Digest doesn't match declared value. */
3029  }
3030 
3031  return 0;
3032  err:
3033  if (digest_matches) {
3034  /* This signature was okay, and the digest was right: This is indeed the
3035  * corresponding extrainfo. But insanely, it doesn't match the routerinfo
3036  * that lists it. Don't try to fetch this one again. */
3037  sd->extrainfo_is_bogus = 1;
3038  }
3039 
3040  return r;
3041 }
3042 
3043 /* Does ri have a valid ntor onion key?
3044  * Valid ntor onion keys exist and have at least one non-zero byte. */
3045 int
3046 routerinfo_has_curve25519_onion_key(const routerinfo_t *ri)
3047 {
3048  if (!ri) {
3049  return 0;
3050  }
3051 
3052  if (!ri->onion_curve25519_pkey) {
3053  return 0;
3054  }
3055 
3056  if (tor_mem_is_zero((const char*)ri->onion_curve25519_pkey->public_key,
3058  return 0;
3059  }
3060 
3061  return 1;
3062 }
3063 
3064 /* Is rs running a tor version known to support EXTEND2 cells?
3065  * If allow_unknown_versions is true, return true if we can't tell
3066  * (from a versions line or a protocols line) whether it supports extend2
3067  * cells.
3068  * Otherwise, return false if the version is unknown. */
3069 int
3070 routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
3071  int allow_unknown_versions)
3072 {
3073  if (!rs) {
3074  return allow_unknown_versions;
3075  }
3076 
3077  if (!rs->pv.protocols_known) {
3078  return allow_unknown_versions;
3079  }
3080 
3081  return rs->pv.supports_extend2_cells;
3082 }
3083 
3086 void
3088 {
3089  routerinfo_t *r2;
3090  signed_descriptor_t *sd2;
3091  if (!rl)
3092  return;
3094  r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
3095  tor_assert(r == r2);
3096  sd2 = sdmap_get(rl->desc_digest_map,
3097  r->cache_info.signed_descriptor_digest);
3098  tor_assert(&(r->cache_info) == sd2);
3099  tor_assert(r->cache_info.routerlist_index == r_sl_idx);
3100  /* XXXX
3101  *
3102  * Hoo boy. We need to fix this one, and the fix is a bit tricky, so
3103  * commenting this out is just a band-aid.
3104  *
3105  * The problem is that, although well-behaved router descriptors
3106  * should never have the same value for their extra_info_digest, it's
3107  * possible for ill-behaved routers to claim whatever they like there.
3108  *
3109  * The real answer is to trash desc_by_eid_map and instead have
3110  * something that indicates for a given extra-info digest we want,
3111  * what its download status is. We'll do that as a part of routerlist
3112  * refactoring once consensus directories are in. For now,
3113  * this rep violation is probably harmless: an adversary can make us
3114  * reset our retry count for an extrainfo, but that's not the end
3115  * of the world. Changing the representation in 0.2.0.x would just
3116  * destabilize the codebase.
3117  if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
3118  signed_descriptor_t *sd3 =
3119  sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
3120  tor_assert(sd3 == &(r->cache_info));
3121  }
3122  */
3123  } SMARTLIST_FOREACH_END(r);
3125  r2 = rimap_get(rl->identity_map, sd->identity_digest);
3126  tor_assert(!r2 || sd != &(r2->cache_info));
3127  sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
3128  tor_assert(sd == sd2);
3129  tor_assert(sd->routerlist_index == sd_sl_idx);
3130  /* XXXX see above.
3131  if (!tor_digest_is_zero(sd->extra_info_digest)) {
3132  signed_descriptor_t *sd3 =
3133  sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
3134  tor_assert(sd3 == sd);
3135  }
3136  */
3137  } SMARTLIST_FOREACH_END(sd);
3138 
3139  RIMAP_FOREACH(rl->identity_map, d, r) {
3140  tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN));
3142  SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
3143  tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN));
3145  SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
3147  tor_assert(sd);
3148  tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN));
3150  EIMAP_FOREACH(rl->extra_info_map, d, ei) {
3151  signed_descriptor_t *sd;
3152  tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3153  d, DIGEST_LEN));
3154  sd = sdmap_get(rl->desc_by_eid_map,
3155  ei->cache_info.signed_descriptor_digest);
3156  // tor_assert(sd); // XXXX see above
3157  if (sd) {
3158  tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
3160  }
3162 }
3163 
3174 const char *
3176 {
3177  static char *info=NULL;
3178  char *esc_contact, *esc_platform;
3179  tor_free(info);
3180 
3181  if (!router)
3182  return NULL; /* we're exiting; just free the memory we use */
3183 
3184  esc_contact = esc_for_log(router->contact_info);
3185  esc_platform = esc_for_log(router->platform);
3186 
3187  tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
3188  tor_free(esc_contact);
3189  tor_free(esc_platform);
3190 
3191  return info;
3192 }
3193 
3196 static int
3197 compare_routerinfo_by_id_digest_(const void **a, const void **b)
3198 {
3199  routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
3200  return fast_memcmp(first->cache_info.identity_digest,
3201  second->cache_info.identity_digest,
3202  DIGEST_LEN);
3203 }
3204 
3206 void
3208 {
3210 }
3211 
3215 void
3217 {
3218  const or_options_t *options = get_options();
3219 
3220  if (options->EntryNodes)
3222  if (options->ExitNodes)
3224  if (options->MiddleNodes)
3226  if (options->ExcludeNodes)
3228  if (options->ExcludeExitNodes)
3230  if (options->ExcludeExitNodesUnion_)
3232 
3234 }
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:40
static void signed_descriptor_free_(signed_descriptor_t *sd)
Definition: routerlist.c:881
Header file for dirserv.c.
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:54
int directory_fetches_dir_info_early(const or_options_t *options)
Definition: dirserv.c:105
void router_reset_descriptor_download_failures(void)
Definition: routerlist.c:2824
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *rs)
Definition: dirclient.c:1160
int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b)
Definition: crypto_rsa.c:71
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Definition: routerlist.c:571
extrainfo_t * router_get_my_extrainfo(void)
Definition: router.c:1692
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2211
Header file for dirclient.c.
smartlist_t * old_routers
Definition: routerlist_st.h:30
void smartlist_string_remove(smartlist_t *sl, const char *element)
Definition: smartlist.c:74
struct digest_ri_map_t * identity_map
Definition: routerlist_st.h:15
char * contact_info
Definition: routerinfo_st.h:64
size_t store_len
Definition: desc_store_st.h:33
Common functions for using (pseudo-)random number generators.
Definition: node_st.h:28
unsigned int policy_is_reject_star
Definition: routerinfo_st.h:74
saved_location_t saved_location
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
MOCK_IMPL(signed_descriptor_t *, router_get_by_extrainfo_digest,(const char *digest))
Definition: routerlist.c:710
int TestingClientMaxIntervalWithoutRequest
int router_is_me(const routerinfo_t *router)
Definition: router.c:1621
int router_differences_are_cosmetic(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:2856
was_router_added_t
Definition: routerlist.h:17
int dirserv_would_reject_router(const routerstatus_t *rs)
static smartlist_t * warned_nicknames
Definition: routerlist.c:148
static int router_rebuild_store(int flags, desc_store_t *store)
Definition: routerlist.c:236
size_t journal_len
Definition: desc_store_st.h:31
static int compare_duration_idx_(const void *_d1, const void *_d2)
Definition: routerlist.c:1699
char extra_info_digest[DIGEST_LEN]
static void launch_dummy_descriptor_download_as_needed(time_t now, const or_options_t *options)
Definition: routerlist.c:2673
const char * signed_descriptor_get_annotations(const signed_descriptor_t *desc)
Definition: routerlist.c:802
protover_summary_flags_t pv
Header file for node_select.c.
routerset_t * EntryNodes
Definition: or_options_st.h:78
void router_dir_info_changed(void)
Definition: nodelist.c:2330
void dump_routerlist_mem_usage(int severity)
Definition: routerlist.c:976
Header file for connection.c.
const char * fname_base
Definition: desc_store_st.h:22
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define MAX_DL_TO_DELAY
Definition: routerlist.c:2419
#define LD_GENERAL
Definition: log.h:59
int replace_file(const char *from, const char *to)
Definition: files.c:117
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
#define PDS_NO_EXISTING_SERVERDESC_FETCH
Definition: node_select.h:61
char identity_digest[DIGEST_LEN]
void learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
Definition: bridges.c:937
#define LOG_INFO
Definition: log.h:42
void update_router_descriptor_downloads(time_t now)
Definition: routerlist.c:2699
Header file for describe.c.
Header file for nodelist.c.
int firewall_is_fascist_dir(void)
Definition: policies.c:364
int we_want_to_fetch_flavor(const or_options_t *options, int flavor)
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:632
int addr_policies_eq(const smartlist_t *a, const smartlist_t *b)
Definition: policies.c:1319
Header file for directory.c.
int firewall_is_fascist_or(void)
Definition: policies.c:353
void smartlist_add(smartlist_t *sl, void *element)
static int WRA_WAS_ADDED(was_router_added_t s)
Definition: routerlist.h:101
const char * signed_descriptor_get_body(const signed_descriptor_t *desc)
Definition: routerlist.c:794
void digestset_add(digestset_t *set, const char *digest)
Definition: digestset.c:44
#define MAX_NICKNAME_LEN
Definition: or.h:113
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:67
struct directory_request_t directory_request_t
Definition: dirclient.h:52
void list_pending_microdesc_downloads(digest256map_t *result)
Definition: routerlist.c:2304
static int routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
Definition: routerlist.c:999
#define MIN_REQUESTS
Definition: routerlist.c:2416
Header file for config.c.
struct digest_sd_map_t * desc_digest_map
Definition: routerlist_st.h:18
Header file for authcert.c.
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc)
Definition: routerlist.c:2193
tor_mmap_t * mmap
Definition: desc_store_st.h:26
void refresh_all_country_info(void)
Definition: routerlist.c:3216
int routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
Definition: bridges.c:286
#define OLD_ROUTER_DESC_MAX_AGE
Definition: or.h:164
Header file for microdesc.c.
routerinfo_t * router_get_mutable_by_digest(const char *digest)
Definition: routerlist.c:676
char extra_info_digest256[DIGEST256_LEN]
void routers_sort_by_identity(smartlist_t *routers)
Definition: routerlist.c:3207
desc_store_t extrainfo_store
Definition: routerlist_st.h:36
int we_fetch_router_descriptors(const or_options_t *options)
Definition: microdesc.c:1045
char signed_descriptor_digest[DIGEST_LEN]
Header file for reachability.c.
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:209
unsigned int supports_tunnelled_dir_requests
Definition: routerinfo_st.h:83
int directory_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
Definition: dirserv.c:169
static void routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old, routerinfo_t *ri_new)
Definition: routerlist.c:1299
#define tor_free(p)
Definition: malloc.h:52
static routerlist_t * routerlist
Definition: routerlist.c:144
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1340
uint32_t bandwidthburst
Definition: routerinfo_st.h:53
static int router_should_rebuild_store(desc_store_t *store)
Definition: routerlist.c:171
struct tor_cert_st * signing_key_cert
Header file for mainloop.c.
size_t size
Definition: mmap.h:26
static int signed_desc_append_to_journal(signed_descriptor_t *desc, desc_store_t *store)
Definition: routerlist.c:195
int control_event_descriptors_changed(smartlist_t *routers)
int download_status_is_ready(download_status_t *dls, time_t now)
Definition: dlstatus.c:376
void routerset_refresh_countries(routerset_t *target)
Definition: routerset.c:77
smartlist_t * declared_family
Definition: routerinfo_st.h:62
int node_has_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:1872
static void extrainfo_free_void(void *e)
Definition: routerlist.c:935
time_t cert_expiration_time
Definition: routerinfo_st.h:43
void update_microdesc_downloads(time_t now)
Definition: microdesc.c:963
void smartlist_shuffle(smartlist_t *sl)
Definition: crypto_rand.c:601
routerset_t * ExcludeExitNodes
Definition: or_options_st.h:88
saved_location_t
Definition: or.h:746
#define DIR_PURPOSE_FETCH_EXTRAINFO
Definition: directory.h:41
void routerlist_assert_ok(const routerlist_t *rl)
Definition: routerlist.c:3087
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2195
Header file for directory authority mode.
int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:493
void trusted_dirs_remove_old_certs(void)
Definition: authcert.c:550
unsigned int protocols_known
Definition: or.h:812
int router_exit_policy_rejects_all(const routerinfo_t *router)
Definition: routerlist.c:2238
static void routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
Definition: routerlist.c:1021
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Header file for policies.c.
uint32_t bandwidthrate
Definition: routerinfo_st.h:51
const char * description
Definition: desc_store_st.h:24
void routerinfo_free_(routerinfo_t *router)
Definition: routerlist.c:834
void update_consensus_router_descriptor_downloads(time_t now, int is_vote, networkstatus_t *consensus)
Definition: routerlist.c:2526
static routerinfo_t * routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
Definition: routerlist.c:1402
desc_store_t desc_store
Definition: routerlist_st.h:34
void extrainfo_free_(extrainfo_t *extrainfo)
Definition: routerlist.c:864
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:38
tor_assert(buffer)
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:946
routerset_t * ExitNodes
Definition: or_options_st.h:72
void routerlist_reset_warnings(void)
Definition: routerlist.c:1443
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:245
Header for crypto_format.c.
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:538
Header file for routermode.c.
static void signed_descriptor_move(signed_descriptor_t *dest, signed_descriptor_t *src)
Definition: routerlist.c:907
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
static const char * signed_descriptor_get_body_impl(const signed_descriptor_t *desc, int with_annotations)
Definition: routerlist.c:745
routerset_t * ExcludeNodes
Definition: or_options_st.h:84
int authdir_mode_handles_descs(const or_options_t *options, int purpose)
Definition: authmode.c:34
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:77
void download_status_mark_impossible(download_status_t *dl)
Definition: dlstatus.c:388
smartlist_t * exit_policy
Definition: routerinfo_st.h:56
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:690
#define DIGEST_LEN
Definition: digest_sizes.h:20
Header file for circuitbuild.c.
static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
Definition: routerlist.h:127
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:390
Master header file for Tor-specific functionality.
routerstatus_t fake_status
Definition: dir_server_st.h:46
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
const char * data
Definition: mmap.h:25
size_t onion_pkey_len
Definition: routerinfo_st.h:36
int hexdigest_to_digest(const char *hexdigest, char *digest)
Definition: routerlist.c:663
void networkstatus_reset_warnings(void)
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
Definition: routerlist.c:2847
void routerlist_descriptors_added(smartlist_t *sl, int from_cache)
Definition: routerlist.c:1927
Header file for rephist.c.
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:1715
void networkstatus_reset_download_failures(void)
#define LOG_WARN
Definition: log.h:50
static void signed_descriptor_reset(signed_descriptor_t *sd)
Definition: routerlist.c:896
static void list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
Definition: routerlist.c:2293
#define MIN_DL_PER_REQUEST
Definition: routerlist.c:2413
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:134
#define PDS_RETRY_IF_NO_SERVERS
Definition: node_select.h:48
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:271
char nickname[MAX_NICKNAME_LEN+1]
unsigned int bad_sig
Definition: extrainfo_st.h:21
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
signed_descriptor_t * router_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:698
void routers_update_status_from_consensus_networkstatus(smartlist_t *routers, int reset_failures)
void smartlist_del(smartlist_t *sl, int idx)
static signed_descriptor_t * signed_descriptor_from_routerinfo(routerinfo_t *ri)
Definition: routerlist.c:923
#define RFTS_IGNORE_MISSING
Definition: files.h:97
unsigned int supports_extend2_cells
Definition: or.h:816
Header file for process_descs.c.
#define ROUTER_ALLOW_UPTIME_DRIFT
Definition: routerlist.c:2850
Types to handle sets of digests, based on bloom filters.
void routerlist_remove_old_routers(void)
Definition: routerlist.c:1801
char * onion_pkey
Definition: routerinfo_st.h:34
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
void digest256_to_base64(char *d64, const char *digest)
struct digest_sd_map_t * desc_by_eid_map
Definition: routerlist_st.h:25
smartlist_t * voters
#define DIGESTMAP_FOREACH_END
Definition: map.h:167
#define LD_FS
Definition: log.h:67
smartlist_t * routerstatus_list
char descriptor_digest[DIGEST256_LEN]
const char * esc_router_info(const routerinfo_t *router)
Definition: routerlist.c:3175
const routerinfo_t * routerlist_find_my_routerinfo(void)
Definition: routerlist.c:541
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
struct short_policy_t * ipv6_exit_policy
Definition: routerinfo_st.h:60
#define LD_DIR
Definition: log.h:85
void router_reset_status_download_failures(void)
Definition: dirlist.c:103
consensus_flavor_t flavor
node_t * nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
Definition: nodelist.c:493
int router_load_single_router(const char *s, uint8_t purpose, int cache, const char **msg)
Definition: routerlist.c:1954
void launch_descriptor_downloads(int purpose, smartlist_t *downloadable, const routerstatus_t *source, time_t now)
Definition: routerlist.c:2428
int hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest, const char *nickname)
Definition: routerlist.c:634
store_type_t type
Definition: desc_store_st.h:28
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
char identity_digest[DIGEST_LEN]
int digestset_probably_contains(const digestset_t *set, const char *digest)
Definition: digestset.c:54
static time_t last_descriptor_download_attempted
Definition: routerlist.c:153
void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime, int need_capacity, int need_guard, int need_desc, int pref_addr, int direct_conn)
Definition: routerlist.c:504
routerset_t * ExcludeExitNodesUnion_
Definition: or_options_st.h:93
int hex_digest_nickname_decode(const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
Definition: routerlist.c:598
void rep_hist_note_router_unreachable(const char *id, time_t when)
Definition: rephist.c:314
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg, int complain, int *valid_out)
void list_pending_downloads(digestmap_t *result, digest256map_t *result256, int purpose, const char *prefix)
Definition: routerlist.c:2250
#define RFTS_BIN
Definition: files.h:95
#define SMARTLIST_FOREACH(sl, type, var, cmd)
char * protocol_list
Definition: routerinfo_st.h:47
const char * escaped(const char *s)
Definition: escape.c:126
static int compare_signed_descriptors_by_age_(const void **_a, const void **_b)
Definition: routerlist.c:220
char * nickname
Definition: routerinfo_st.h:17
void routerlist_retry_directory_downloads(time_t now)
Definition: routerlist.c:2223
struct digest_ei_map_t * extra_info_map
Definition: routerlist_st.h:21
void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
Definition: routerlist.c:1187
static void routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
Definition: routerlist.c:1152
Header file for dlstatus.c.
int fascist_firewall_allows_node(const node_t *node, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:732
uint16_t dir_port
Definition: routerinfo_st.h:21
#define log_fn(severity, domain, args,...)
Definition: log.h:266
void dirserv_single_reachability_test(time_t now, routerinfo_t *router)
Definition: reachability.c:126
int dir_split_resource_into_fingerprints(const char *resource, smartlist_t *fp_out, int *compressed_out, int flags)
Definition: directory.c:544
routerstatus_t * networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
void smartlist_sort_digests(smartlist_t *sl)
Definition: smartlist.c:824
#define DIR_PURPOSE_FETCH_MICRODESC
Definition: directory.h:70
void update_extrainfo_downloads(time_t now)
Definition: routerlist.c:2713
#define tor_addr_eq(a, b)
Definition: address.h:244
size_t bytes_dropped
Definition: desc_store_st.h:36
#define fast_memcmp(a, b, c)
Definition: di_ops.h:26
download_status_t ei_dl_status
time_t approx_time(void)
Definition: approx_time.c:32
static int WRA_WAS_OUTDATED(was_router_added_t s)
Definition: routerlist.h:111
uint32_t bandwidthcapacity
Definition: routerinfo_st.h:55
int tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
Definition: torcert.c:315
void routerlist_free_all(void)
Definition: routerlist.c:1427
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
routerset_t * MiddleNodes
Definition: or_options_st.h:75
Header file for dirlist.c.
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:67
int router_reload_router_list(void)
Definition: routerlist.c:457
uint32_t addr
Definition: routerinfo_st.h:19
void reschedule_directory_downloads(void)
Definition: mainloop.c:1711
static int compare_old_routers_by_identity_(const void **_a, const void **_b)
Definition: routerlist.c:1679
int directory_caches_dir_info(const or_options_t *options)
Definition: dirserv.c:143
was_router_added_t router_add_to_routerlist(routerinfo_t *router, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1482
void nodelist_remove_routerinfo(routerinfo_t *ri)
Definition: nodelist.c:727
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
char * platform
Definition: routerinfo_st.h:45
#define CONN_TYPE_DIR
Definition: connection.h:35
#define ROUTER_ANNOTATION_BUF_LEN
Definition: or.h:803
uint8_t purpose
static int compare_routerinfo_by_id_digest_(const void **a, const void **b)
Definition: routerlist.c:3197
char * esc_for_log(const char *s)
Definition: escape.c:30
unsigned int is_hibernating
Definition: routerinfo_st.h:65
char * Address
Definition: or_options_st.h:69
digestset_t * digestset_new(int max_guess)
Definition: digestset.c:30
static desc_store_t * desc_get_store(routerlist_t *rl, const signed_descriptor_t *sd)
Definition: routerlist.c:183
uint8_t digest256[DIGEST256_LEN]
Definition: extrainfo_st.h:16
static void routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
Definition: routerlist.c:1246
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:27
void nodelist_refresh_countries(void)
Definition: nodelist.c:1940
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1043
char * pending_sig
Definition: extrainfo_st.h:24
int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *identity_pkey, extrainfo_t *ei, signed_descriptor_t *sd, const char **msg)
Definition: routerlist.c:2950
Header file for routerinfo.c.
#define DUMMY_DOWNLOAD_INTERVAL
Definition: routerlist.c:2668
int client_would_use_router(const routerstatus_t *rs, time_t now)
Header file for control_events.c.
void smartlist_clear(smartlist_t *sl)
void routerlist_free_(routerlist_t *rl)
Definition: routerlist.c:942
#define ROUTER_MAX_AGE
Definition: or.h:159
uint32_t router_get_advertised_bandwidth(const routerinfo_t *router)
Definition: routerlist.c:557
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
static int router_reload_router_list_impl(desc_store_t *store)
Definition: routerlist.c:390
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH
Definition: routerlist.c:566
const char * router_describe(const routerinfo_t *ri)
Definition: describe.c:81
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:809
was_router_added_t router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg, int from_cache, int from_fetch)
Definition: routerlist.c:1658
crypto_pk_t * identity_pkey
Definition: routerinfo_st.h:38
file_status_t file_status(const char *filename)
Definition: files.c:212
Header file for networkstatus.c.
size_t pending_sig_len
Definition: extrainfo_st.h:26
#define LD_BUG
Definition: log.h:83
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20
static int max_dl_per_request(const or_options_t *options, int purpose)
Definition: routerlist.c:2384
smartlist_t * routers
Definition: routerlist_st.h:27
Header file for routerlist.c.
Header file for routerparse.c.
uint16_t or_port
Definition: routerinfo_st.h:20
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:2021
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:2120