microdesc.c 21 KB

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