microdesc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /* Copyright (c) 2009-2011, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "directory.h"
  6. #include "dirserv.h"
  7. #include "microdesc.h"
  8. #include "networkstatus.h"
  9. #include "nodelist.h"
  10. #include "policies.h"
  11. #include "router.h"
  12. #include "routerlist.h"
  13. #include "routerparse.h"
  14. /** A data structure to hold a bunch of cached microdescriptors. There are
  15. * two active files in the cache: a "cache file" that we mmap, and a "journal
  16. * file" that we append to. Periodically, we rebuild the cache file to hold
  17. * only the microdescriptors that we want to keep */
  18. struct microdesc_cache_t {
  19. /** Map from sha256-digest to microdesc_t for every microdesc_t in the
  20. * cache. */
  21. HT_HEAD(microdesc_map, microdesc_t) map;
  22. /** Name of the cache file. */
  23. char *cache_fname;
  24. /** Name of the journal file. */
  25. char *journal_fname;
  26. /** Mmap'd contents of the cache file, or NULL if there is none. */
  27. tor_mmap_t *cache_content;
  28. /** Number of bytes used in the journal file. */
  29. size_t journal_len;
  30. /** Number of bytes in descriptors removed as too old. */
  31. size_t bytes_dropped;
  32. /** Total bytes of microdescriptor bodies we have added to this cache */
  33. uint64_t total_len_seen;
  34. /** Total number of microdescriptors we have added to this cache */
  35. unsigned n_seen;
  36. };
  37. /** Helper: computes a hash of <b>md</b> to place it in a hash table. */
  38. static INLINE unsigned int
  39. _microdesc_hash(microdesc_t *md)
  40. {
  41. unsigned *d = (unsigned*)md->digest;
  42. #if SIZEOF_INT == 4
  43. return d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[6] ^ d[7];
  44. #else
  45. return d[0] ^ d[1] ^ d[2] ^ d[3];
  46. #endif
  47. }
  48. /** Helper: compares <b>a</b> and </b> for equality for hash-table purposes. */
  49. static INLINE int
  50. _microdesc_eq(microdesc_t *a, microdesc_t *b)
  51. {
  52. return tor_memeq(a->digest, b->digest, DIGEST256_LEN);
  53. }
  54. HT_PROTOTYPE(microdesc_map, microdesc_t, node,
  55. _microdesc_hash, _microdesc_eq);
  56. HT_GENERATE(microdesc_map, microdesc_t, node,
  57. _microdesc_hash, _microdesc_eq, 0.6,
  58. malloc, realloc, free);
  59. /** Write the body of <b>md</b> into <b>f</b>, with appropriate annotations.
  60. * On success, return the total number of bytes written, and set
  61. * *<b>annotation_len_out</b> to the number of bytes written as
  62. * annotations. */
  63. static ssize_t
  64. dump_microdescriptor(FILE *f, microdesc_t *md, size_t *annotation_len_out)
  65. {
  66. ssize_t r = 0;
  67. size_t written;
  68. /* XXXX drops unkown annotations. */
  69. if (md->last_listed) {
  70. char buf[ISO_TIME_LEN+1];
  71. char annotation[ISO_TIME_LEN+32];
  72. format_iso_time(buf, md->last_listed);
  73. tor_snprintf(annotation, sizeof(annotation), "@last-listed %s\n", buf);
  74. fputs(annotation, f);
  75. r += strlen(annotation);
  76. *annotation_len_out = r;
  77. } else {
  78. *annotation_len_out = 0;
  79. }
  80. md->off = (off_t) ftell(f);
  81. written = fwrite(md->body, 1, md->bodylen, f);
  82. if (written != md->bodylen) {
  83. log_warn(LD_DIR,
  84. "Couldn't dump microdescriptor (wrote %lu out of %lu): %s",
  85. (unsigned long)written, (unsigned long)md->bodylen,
  86. strerror(ferror(f)));
  87. return -1;
  88. }
  89. r += md->bodylen;
  90. return r;
  91. }
  92. /** Holds a pointer to the current microdesc_cache_t object, or NULL if no
  93. * such object has been allocated. */
  94. static microdesc_cache_t *the_microdesc_cache = NULL;
  95. /** Return a pointer to the microdescriptor cache, loading it if necessary. */
  96. microdesc_cache_t *
  97. get_microdesc_cache(void)
  98. {
  99. if (PREDICT_UNLIKELY(the_microdesc_cache==NULL)) {
  100. microdesc_cache_t *cache = tor_malloc_zero(sizeof(microdesc_cache_t));
  101. HT_INIT(microdesc_map, &cache->map);
  102. cache->cache_fname = get_datadir_fname("cached-microdescs");
  103. cache->journal_fname = get_datadir_fname("cached-microdescs.new");
  104. microdesc_cache_reload(cache);
  105. the_microdesc_cache = cache;
  106. }
  107. return the_microdesc_cache;
  108. }
  109. /* There are three sources of microdescriptors:
  110. 1) Generated by us while acting as a directory authority.
  111. 2) Loaded from the cache on disk.
  112. 3) Downloaded.
  113. */
  114. /** Decode the microdescriptors from the string starting at <b>s</b> and
  115. * ending at <b>eos</b>, and store them in <b>cache</b>. If <b>no-save</b>,
  116. * mark them as non-writable to disk. If <b>where</b> is SAVED_IN_CACHE,
  117. * leave their bodies as pointers to the mmap'd cache. If where is
  118. * <b>SAVED_NOWHERE</b>, do not allow annotations. If listed_at is positive,
  119. * set the last_listed field of every microdesc to listed_at. If
  120. * requested_digests is non-null, then it contains a list of digests we mean
  121. * to allow, so we should reject any non-requested microdesc with a different
  122. * digest, and alter the list to contain only the digests of those microdescs
  123. * we didn't find.
  124. * Return a newly allocated list of the added microdescriptors, or NULL */
  125. smartlist_t *
  126. microdescs_add_to_cache(microdesc_cache_t *cache,
  127. const char *s, const char *eos, saved_location_t where,
  128. int no_save, time_t listed_at,
  129. smartlist_t *requested_digests256)
  130. {
  131. smartlist_t *descriptors, *added;
  132. const int allow_annotations = (where != SAVED_NOWHERE);
  133. const int copy_body = (where != SAVED_IN_CACHE);
  134. descriptors = microdescs_parse_from_string(s, eos,
  135. allow_annotations,
  136. copy_body);
  137. if (listed_at > 0) {
  138. SMARTLIST_FOREACH(descriptors, microdesc_t *, md,
  139. md->last_listed = listed_at);
  140. }
  141. if (requested_digests256) {
  142. digestmap_t *requested; /* XXXX actuqlly we should just use a
  143. digest256map */
  144. requested = digestmap_new();
  145. SMARTLIST_FOREACH(requested_digests256, const char *, cp,
  146. digestmap_set(requested, cp, (void*)1));
  147. SMARTLIST_FOREACH_BEGIN(descriptors, microdesc_t *, md) {
  148. if (digestmap_get(requested, md->digest)) {
  149. digestmap_set(requested, md->digest, (void*)2);
  150. } else {
  151. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Received non-requested microcdesc");
  152. microdesc_free(md);
  153. SMARTLIST_DEL_CURRENT(descriptors, md);
  154. }
  155. } SMARTLIST_FOREACH_END(md);
  156. SMARTLIST_FOREACH_BEGIN(requested_digests256, char *, cp) {
  157. if (digestmap_get(requested, cp) == (void*)2) {
  158. tor_free(cp);
  159. SMARTLIST_DEL_CURRENT(requested_digests256, cp);
  160. }
  161. } SMARTLIST_FOREACH_END(cp);
  162. digestmap_free(requested, NULL);
  163. }
  164. added = microdescs_add_list_to_cache(cache, descriptors, where, no_save);
  165. smartlist_free(descriptors);
  166. return added;
  167. }
  168. /* As microdescs_add_to_cache, but takes a list of micrdescriptors instead of
  169. * a string to decode. Frees any members of <b>descriptors</b> that it does
  170. * not add. */
  171. smartlist_t *
  172. microdescs_add_list_to_cache(microdesc_cache_t *cache,
  173. smartlist_t *descriptors, saved_location_t where,
  174. int no_save)
  175. {
  176. smartlist_t *added;
  177. open_file_t *open_file = NULL;
  178. FILE *f = NULL;
  179. // int n_added = 0;
  180. ssize_t size = 0;
  181. if (where == SAVED_NOWHERE && !no_save) {
  182. f = start_writing_to_stdio_file(cache->journal_fname,
  183. OPEN_FLAGS_APPEND|O_BINARY,
  184. 0600, &open_file);
  185. if (!f) {
  186. log_warn(LD_DIR, "Couldn't append to journal in %s: %s",
  187. cache->journal_fname, strerror(errno));
  188. return NULL;
  189. }
  190. }
  191. added = smartlist_create();
  192. SMARTLIST_FOREACH_BEGIN(descriptors, microdesc_t *, md) {
  193. microdesc_t *md2;
  194. md2 = HT_FIND(microdesc_map, &cache->map, md);
  195. if (md2) {
  196. /* We already had this one. */
  197. if (md2->last_listed < md->last_listed)
  198. md2->last_listed = md->last_listed;
  199. microdesc_free(md);
  200. if (where != SAVED_NOWHERE)
  201. cache->bytes_dropped += size;
  202. continue;
  203. }
  204. /* Okay, it's a new one. */
  205. if (f) {
  206. size_t annotation_len;
  207. size = dump_microdescriptor(f, md, &annotation_len);
  208. if (size < 0) {
  209. /* XXX handle errors from dump_microdescriptor() */
  210. /* log? return -1? die? coredump the universe? */
  211. continue;
  212. }
  213. md->saved_location = SAVED_IN_JOURNAL;
  214. cache->journal_len += size;
  215. } else {
  216. md->saved_location = where;
  217. }
  218. md->no_save = no_save;
  219. HT_INSERT(microdesc_map, &cache->map, md);
  220. smartlist_add(added, md);
  221. ++cache->n_seen;
  222. cache->total_len_seen += md->bodylen;
  223. } SMARTLIST_FOREACH_END(md);
  224. if (f)
  225. finish_writing_to_file(open_file); /*XXX Check me.*/
  226. {
  227. networkstatus_t *ns = networkstatus_get_latest_consensus();
  228. if (ns && ns->flavor == FLAV_MICRODESC)
  229. SMARTLIST_FOREACH(added, microdesc_t *, md, nodelist_add_microdesc(md));
  230. }
  231. if (smartlist_len(added))
  232. router_dir_info_changed();
  233. return added;
  234. }
  235. /** Remove every microdescriptor in <b>cache</b>. */
  236. void
  237. microdesc_cache_clear(microdesc_cache_t *cache)
  238. {
  239. microdesc_t **entry, **next;
  240. for (entry = HT_START(microdesc_map, &cache->map); entry; entry = next) {
  241. microdesc_t *md = *entry;
  242. next = HT_NEXT_RMV(microdesc_map, &cache->map, entry);
  243. microdesc_free(md);
  244. }
  245. HT_CLEAR(microdesc_map, &cache->map);
  246. if (cache->cache_content) {
  247. tor_munmap_file(cache->cache_content);
  248. cache->cache_content = NULL;
  249. }
  250. cache->total_len_seen = 0;
  251. cache->n_seen = 0;
  252. cache->bytes_dropped = 0;
  253. }
  254. /** Reload the contents of <b>cache</b> from disk. If it is empty, load it
  255. * for the first time. Return 0 on success, -1 on failure. */
  256. int
  257. microdesc_cache_reload(microdesc_cache_t *cache)
  258. {
  259. struct stat st;
  260. char *journal_content;
  261. smartlist_t *added;
  262. tor_mmap_t *mm;
  263. int total = 0;
  264. microdesc_cache_clear(cache);
  265. mm = cache->cache_content = tor_mmap_file(cache->cache_fname);
  266. if (mm) {
  267. added = microdescs_add_to_cache(cache, mm->data, mm->data+mm->size,
  268. SAVED_IN_CACHE, 0, -1, NULL);
  269. if (added) {
  270. total += smartlist_len(added);
  271. smartlist_free(added);
  272. }
  273. }
  274. journal_content = read_file_to_str(cache->journal_fname,
  275. RFTS_IGNORE_MISSING, &st);
  276. if (journal_content) {
  277. cache->journal_len = (size_t) st.st_size;
  278. added = microdescs_add_to_cache(cache, journal_content,
  279. journal_content+st.st_size,
  280. SAVED_IN_JOURNAL, 0, -1, NULL);
  281. if (added) {
  282. total += smartlist_len(added);
  283. smartlist_free(added);
  284. }
  285. tor_free(journal_content);
  286. }
  287. log_notice(LD_DIR, "Reloaded microdescriptor cache. Found %d descriptors.",
  288. total);
  289. microdesc_cache_rebuild(cache, 0 /* don't force */);
  290. return 0;
  291. }
  292. /** By default, we remove any microdescriptors that have gone at least this
  293. * long without appearing in a current consensus. */
  294. #define TOLERATE_MICRODESC_AGE (7*24*60*60)
  295. /** Remove all microdescriptors from <b>cache</b> that haven't been listed for
  296. * a long time. Does not rebuild the cache on disk. If <b>cutoff</b> is
  297. * positive, specifically remove microdescriptors that have been unlisted
  298. * since <b>cutoff</b>. If <b>force</b> is true, remove microdescriptors even
  299. * if we have no current live microdescriptor consensus.
  300. */
  301. void
  302. microdesc_cache_clean(microdesc_cache_t *cache, time_t cutoff, int force)
  303. {
  304. microdesc_t **mdp, *victim;
  305. int dropped=0, kept=0;
  306. size_t bytes_dropped = 0;
  307. time_t now = time(NULL);
  308. /* If we don't know a live consensus, don't believe last_listed values: we
  309. * might be starting up after being down for a while. */
  310. if (! force &&
  311. ! networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC))
  312. return;
  313. if (cutoff <= 0)
  314. cutoff = now - TOLERATE_MICRODESC_AGE;
  315. for (mdp = HT_START(microdesc_map, &cache->map); mdp != NULL; ) {
  316. if ((*mdp)->last_listed < cutoff) {
  317. ++dropped;
  318. victim = *mdp;
  319. mdp = HT_NEXT_RMV(microdesc_map, &cache->map, mdp);
  320. bytes_dropped += victim->bodylen;
  321. microdesc_free(victim);
  322. } else {
  323. ++kept;
  324. mdp = HT_NEXT(microdesc_map, &cache->map, mdp);
  325. }
  326. }
  327. if (dropped) {
  328. log_notice(LD_DIR, "Removed %d/%d microdescriptors as old.",
  329. dropped,dropped+kept);
  330. cache->bytes_dropped += bytes_dropped;
  331. }
  332. }
  333. static int
  334. should_rebuild_md_cache(microdesc_cache_t *cache)
  335. {
  336. const size_t old_len =
  337. cache->cache_content ? cache->cache_content->size : 0;
  338. const size_t journal_len = cache->journal_len;
  339. const size_t dropped = cache->bytes_dropped;
  340. if (journal_len < 16384)
  341. return 0; /* Don't bother, not enough has happened yet. */
  342. if (dropped > (journal_len + old_len) / 3)
  343. return 1; /* We could save 1/3 or more of the currently used space. */
  344. if (journal_len > old_len / 2)
  345. return 1; /* We should append to the regular file */
  346. return 0;
  347. }
  348. /** Regenerate the main cache file for <b>cache</b>, clear the journal file,
  349. * and update every microdesc_t in the cache with pointers to its new
  350. * location. If <b>force</b> is true, do this unconditionally. If
  351. * <b>force</b> is false, do it only if we expect to save space on disk. */
  352. int
  353. microdesc_cache_rebuild(microdesc_cache_t *cache, int force)
  354. {
  355. open_file_t *open_file;
  356. FILE *f;
  357. microdesc_t **mdp;
  358. smartlist_t *wrote;
  359. ssize_t size;
  360. off_t off = 0;
  361. int orig_size, new_size;
  362. if (cache == NULL) {
  363. cache = the_microdesc_cache;
  364. if (cache == NULL)
  365. return 0;
  366. }
  367. /* Remove dead descriptors */
  368. microdesc_cache_clean(cache, 0/*cutoff*/, 0/*force*/);
  369. if (!force && !should_rebuild_md_cache(cache))
  370. return 0;
  371. log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
  372. orig_size = (int)(cache->cache_content ? cache->cache_content->size : 0);
  373. orig_size += (int)cache->journal_len;
  374. f = start_writing_to_stdio_file(cache->cache_fname,
  375. OPEN_FLAGS_REPLACE|O_BINARY,
  376. 0600, &open_file);
  377. if (!f)
  378. return -1;
  379. wrote = smartlist_create();
  380. HT_FOREACH(mdp, microdesc_map, &cache->map) {
  381. microdesc_t *md = *mdp;
  382. size_t annotation_len;
  383. if (md->no_save)
  384. continue;
  385. size = dump_microdescriptor(f, md, &annotation_len);
  386. if (size < 0) {
  387. /* XXX handle errors from dump_microdescriptor() */
  388. /* log? return -1? die? coredump the universe? */
  389. continue;
  390. }
  391. tor_assert(((size_t)size) == annotation_len + md->bodylen);
  392. md->off = off + annotation_len;
  393. off += size;
  394. if (md->saved_location != SAVED_IN_CACHE) {
  395. tor_free(md->body);
  396. md->saved_location = SAVED_IN_CACHE;
  397. }
  398. smartlist_add(wrote, md);
  399. }
  400. if (cache->cache_content)
  401. tor_munmap_file(cache->cache_content);
  402. finish_writing_to_file(open_file); /*XXX Check me.*/
  403. cache->cache_content = tor_mmap_file(cache->cache_fname);
  404. if (!cache->cache_content && smartlist_len(wrote)) {
  405. log_err(LD_DIR, "Couldn't map file that we just wrote to %s!",
  406. cache->cache_fname);
  407. smartlist_free(wrote);
  408. return -1;
  409. }
  410. SMARTLIST_FOREACH_BEGIN(wrote, microdesc_t *, md) {
  411. tor_assert(md->saved_location == SAVED_IN_CACHE);
  412. md->body = (char*)cache->cache_content->data + md->off;
  413. if (PREDICT_UNLIKELY(
  414. md->bodylen < 9 || fast_memneq(md->body, "onion-key", 9) != 0)) {
  415. /* XXXX023 once bug 2022 is solved, we can kill this block and turn it
  416. * into just the tor_assert(!memcmp) */
  417. off_t avail = cache->cache_content->size - md->off;
  418. char *bad_str;
  419. tor_assert(avail >= 0);
  420. bad_str = tor_strndup(md->body, MIN(128, (size_t)avail));
  421. log_err(LD_BUG, "After rebuilding microdesc cache, offsets seem wrong. "
  422. " At offset %d, I expected to find a microdescriptor starting "
  423. " with \"onion-key\". Instead I got %s.",
  424. (int)md->off, escaped(bad_str));
  425. tor_free(bad_str);
  426. tor_assert(fast_memeq(md->body, "onion-key", 9));
  427. }
  428. } SMARTLIST_FOREACH_END(md);
  429. smartlist_free(wrote);
  430. write_str_to_file(cache->journal_fname, "", 1);
  431. cache->journal_len = 0;
  432. cache->bytes_dropped = 0;
  433. new_size = cache->cache_content ? (int)cache->cache_content->size : 0;
  434. log_info(LD_DIR, "Done rebuilding microdesc cache. "
  435. "Saved %d bytes; %d still used.",
  436. orig_size-new_size, new_size);
  437. return 0;
  438. }
  439. /** Deallocate a single microdescriptor. Note: the microdescriptor MUST have
  440. * previously been removed from the cache if it had ever been inserted. */
  441. void
  442. microdesc_free(microdesc_t *md)
  443. {
  444. if (!md)
  445. return;
  446. /* Must be removed from hash table! */
  447. if (md->onion_pkey)
  448. crypto_free_pk_env(md->onion_pkey);
  449. if (md->body && md->saved_location != SAVED_IN_CACHE)
  450. tor_free(md->body);
  451. if (md->family) {
  452. SMARTLIST_FOREACH(md->family, char *, cp, tor_free(cp));
  453. smartlist_free(md->family);
  454. }
  455. short_policy_free(md->exit_policy);
  456. tor_free(md);
  457. }
  458. /** Free all storage held in the microdesc.c module. */
  459. void
  460. microdesc_free_all(void)
  461. {
  462. if (the_microdesc_cache) {
  463. microdesc_cache_clear(the_microdesc_cache);
  464. tor_free(the_microdesc_cache->cache_fname);
  465. tor_free(the_microdesc_cache->journal_fname);
  466. tor_free(the_microdesc_cache);
  467. }
  468. }
  469. /** If there is a microdescriptor in <b>cache</b> whose sha256 digest is
  470. * <b>d</b>, return it. Otherwise return NULL. */
  471. microdesc_t *
  472. microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
  473. {
  474. microdesc_t *md, search;
  475. if (!cache)
  476. cache = get_microdesc_cache();
  477. memcpy(search.digest, d, DIGEST256_LEN);
  478. md = HT_FIND(microdesc_map, &cache->map, &search);
  479. return md;
  480. }
  481. /** Return the mean size of decriptors added to <b>cache</b> since it was last
  482. * cleared. Used to estimate the size of large downloads. */
  483. size_t
  484. microdesc_average_size(microdesc_cache_t *cache)
  485. {
  486. if (!cache)
  487. cache = get_microdesc_cache();
  488. if (!cache->n_seen)
  489. return 512;
  490. return (size_t)(cache->total_len_seen / cache->n_seen);
  491. }
  492. /** Return a smartlist of all the sha256 digest of the microdescriptors that
  493. * are listed in <b>ns</b> but not present in <b>cache</b>. Returns pointers
  494. * to internals of <b>ns</b>; you should not free the members of the resulting
  495. * smartlist. Omit all microdescriptors whose digest appear in <b>skip</b>. */
  496. smartlist_t *
  497. microdesc_list_missing_digest256(networkstatus_t *ns, microdesc_cache_t *cache,
  498. int downloadable_only, digestmap_t *skip)
  499. {
  500. smartlist_t *result = smartlist_create();
  501. time_t now = time(NULL);
  502. tor_assert(ns->flavor == FLAV_MICRODESC);
  503. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  504. if (microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest))
  505. continue;
  506. if (downloadable_only &&
  507. !download_status_is_ready(&rs->dl_status, now,
  508. MAX_MICRODESC_DOWNLOAD_FAILURES))
  509. continue;
  510. if (skip && digestmap_get(skip, rs->descriptor_digest))
  511. continue;
  512. if (tor_mem_is_zero(rs->descriptor_digest, DIGEST256_LEN))
  513. continue; /* This indicates a bug somewhere XXXX023*/
  514. /* XXXX Also skip if we're a noncache and wouldn't use this router.
  515. * XXXX NM Microdesc
  516. */
  517. smartlist_add(result, rs->descriptor_digest);
  518. } SMARTLIST_FOREACH_END(rs);
  519. return result;
  520. }
  521. /** Launch download requests for mircodescriptors as appropriate.
  522. *
  523. * Specifically, we should launch download requests if we are configured to
  524. * download mirodescriptors, and there are some microdescriptors listed the
  525. * current microdesc consensus that we don't have, and either we never asked
  526. * for them, or we failed to download them but we're willing to retry.
  527. */
  528. void
  529. update_microdesc_downloads(time_t now)
  530. {
  531. const or_options_t *options = get_options();
  532. networkstatus_t *consensus;
  533. smartlist_t *missing;
  534. digestmap_t *pending;
  535. if (should_delay_dir_fetches(options))
  536. return;
  537. if (directory_too_idle_to_fetch_descriptors(options, now))
  538. return;
  539. consensus = networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  540. if (!consensus)
  541. return;
  542. if (!we_fetch_microdescriptors(options))
  543. return;
  544. pending = digestmap_new();
  545. list_pending_microdesc_downloads(pending);
  546. missing = microdesc_list_missing_digest256(consensus,
  547. get_microdesc_cache(),
  548. 1,
  549. pending);
  550. digestmap_free(pending, NULL);
  551. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC,
  552. missing, NULL, now);
  553. smartlist_free(missing);
  554. }
  555. /** For every microdescriptor listed in the current microdecriptor consensus,
  556. * update its last_listed field to be at least as recent as the publication
  557. * time of the current microdescriptor consensus.
  558. */
  559. void
  560. update_microdescs_from_networkstatus(time_t now)
  561. {
  562. microdesc_cache_t *cache = get_microdesc_cache();
  563. microdesc_t *md;
  564. networkstatus_t *ns =
  565. networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  566. if (! ns)
  567. return;
  568. tor_assert(ns->flavor == FLAV_MICRODESC);
  569. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  570. md = microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest);
  571. if (md && ns->valid_after > md->last_listed)
  572. md->last_listed = ns->valid_after;
  573. } SMARTLIST_FOREACH_END(rs);
  574. }
  575. /** Return true iff we should prefer to use microdescriptors rather than
  576. * routerdescs for building circuits. */
  577. int
  578. we_use_microdescriptors_for_circuits(const or_options_t *options)
  579. {
  580. int ret = options->UseMicrodescriptors;
  581. if (ret == -1) {
  582. /* UseMicrodescriptors is "auto"; we need to decide: */
  583. #if 0
  584. /* So we decide that we'll use microdescriptors iff we are not a server */
  585. ret = ! server_mode(options);
  586. #else
  587. /* We don't use microdescs for now: not enough caches are running
  588. * 0.2.3.1-alpha */
  589. ret = 0;
  590. #endif
  591. }
  592. return ret;
  593. }
  594. /** Return true iff we should try to download microdescriptors at all. */
  595. int
  596. we_fetch_microdescriptors(const or_options_t *options)
  597. {
  598. if (directory_caches_dir_info(options))
  599. return 1;
  600. return we_use_microdescriptors_for_circuits(options);
  601. }
  602. /** Return true iff we should try to download router descriptors at all. */
  603. int
  604. we_fetch_router_descriptors(const or_options_t *options)
  605. {
  606. if (directory_caches_dir_info(options))
  607. return 1;
  608. return ! we_use_microdescriptors_for_circuits(options);
  609. }
  610. /** Return the consensus flavor we actually want to use to build circuits. */
  611. int
  612. usable_consensus_flavor(void)
  613. {
  614. if (we_use_microdescriptors_for_circuits(get_options())) {
  615. return FLAV_MICRODESC;
  616. } else {
  617. return FLAV_NS;
  618. }
  619. }