microdesc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 list of the added microdescriptors. */
  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 encode. 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. continue;
  200. }
  201. /* Okay, it's a new one. */
  202. if (f) {
  203. size_t annotation_len;
  204. size = dump_microdescriptor(f, md, &annotation_len);
  205. if (size < 0) {
  206. /* XXX handle errors from dump_microdescriptor() */
  207. /* log? return -1? die? coredump the universe? */
  208. continue;
  209. }
  210. md->saved_location = SAVED_IN_JOURNAL;
  211. cache->journal_len += size;
  212. } else {
  213. md->saved_location = where;
  214. }
  215. md->no_save = no_save;
  216. HT_INSERT(microdesc_map, &cache->map, md);
  217. smartlist_add(added, md);
  218. ++cache->n_seen;
  219. cache->total_len_seen += md->bodylen;
  220. } SMARTLIST_FOREACH_END(md);
  221. if (f)
  222. finish_writing_to_file(open_file); /*XXX Check me.*/
  223. {
  224. size_t old_content_len =
  225. cache->cache_content ? cache->cache_content->size : 0;
  226. if ((cache->journal_len > 16384 + old_content_len &&
  227. cache->journal_len > old_content_len / 2))
  228. microdesc_cache_rebuild(cache);
  229. }
  230. {
  231. networkstatus_t *ns = networkstatus_get_latest_consensus();
  232. if (ns && ns->flavor == FLAV_MICRODESC)
  233. SMARTLIST_FOREACH(added, microdesc_t *, md, nodelist_add_microdesc(md));
  234. }
  235. return added;
  236. }
  237. /** Remove every microdescriptor in <b>cache</b>. */
  238. void
  239. microdesc_cache_clear(microdesc_cache_t *cache)
  240. {
  241. microdesc_t **entry, **next;
  242. for (entry = HT_START(microdesc_map, &cache->map); entry; entry = next) {
  243. microdesc_t *md = *entry;
  244. next = HT_NEXT_RMV(microdesc_map, &cache->map, entry);
  245. microdesc_free(md);
  246. }
  247. HT_CLEAR(microdesc_map, &cache->map);
  248. if (cache->cache_content) {
  249. tor_munmap_file(cache->cache_content);
  250. cache->cache_content = NULL;
  251. }
  252. cache->total_len_seen = 0;
  253. cache->n_seen = 0;
  254. }
  255. /** Reload the contents of <b>cache</b> from disk. If it is empty, load it
  256. * for the first time. Return 0 on success, -1 on failure. */
  257. int
  258. microdesc_cache_reload(microdesc_cache_t *cache)
  259. {
  260. struct stat st;
  261. char *journal_content;
  262. smartlist_t *added;
  263. tor_mmap_t *mm;
  264. int total = 0;
  265. microdesc_cache_clear(cache);
  266. mm = cache->cache_content = tor_mmap_file(cache->cache_fname);
  267. if (mm) {
  268. added = microdescs_add_to_cache(cache, mm->data, mm->data+mm->size,
  269. SAVED_IN_CACHE, 0, -1, NULL);
  270. if (added) {
  271. total += smartlist_len(added);
  272. smartlist_free(added);
  273. }
  274. }
  275. journal_content = read_file_to_str(cache->journal_fname,
  276. RFTS_IGNORE_MISSING, &st);
  277. if (journal_content) {
  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. return 0;
  290. }
  291. /** By default, we remove any microdescriptors that have gone at least this
  292. * long without appearing in a current consensus. */
  293. #define TOLERATE_MICRODESC_AGE (7*24*60*60)
  294. /** Remove all microdescriptors from <b>cache</b> that haven't been listed for
  295. * a long time. Does not rebuild the cache on disk. If <b>cutoff</b> is
  296. * positive, specifically remove microdescriptors that have been unlisted
  297. * since <b>cutoff</b>. If <b>force</b> is true, remove microdescriptors even
  298. * if we have no current live microdescriptor consensus.
  299. */
  300. void
  301. microdesc_cache_clean(microdesc_cache_t *cache, time_t cutoff, int force)
  302. {
  303. microdesc_t **mdp, *victim;
  304. int dropped=0, kept=0;
  305. size_t bytes_dropped = 0;
  306. time_t now = time(NULL);
  307. /* If we don't know a live consensus, don't believe last_listed values: we
  308. * might be starting up after being down for a while. */
  309. if (! force &&
  310. ! networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC))
  311. return;
  312. if (cutoff <= 0)
  313. cutoff = now - TOLERATE_MICRODESC_AGE;
  314. for (mdp = HT_START(microdesc_map, &cache->map); mdp != NULL; ) {
  315. if ((*mdp)->last_listed < cutoff) {
  316. ++dropped;
  317. victim = *mdp;
  318. mdp = HT_NEXT_RMV(microdesc_map, &cache->map, mdp);
  319. bytes_dropped += victim->bodylen;
  320. microdesc_free(victim);
  321. } else {
  322. ++kept;
  323. mdp = HT_NEXT(microdesc_map, &cache->map, mdp);
  324. }
  325. }
  326. if (dropped) {
  327. log_notice(LD_DIR, "Removed %d/%d microdescriptors as old.",
  328. dropped,dropped+kept);
  329. cache->bytes_dropped += bytes_dropped;
  330. }
  331. }
  332. /** Regenerate the main cache file for <b>cache</b>, clear the journal file,
  333. * and update every microdesc_t in the cache with pointers to its new
  334. * location. */
  335. int
  336. microdesc_cache_rebuild(microdesc_cache_t *cache)
  337. {
  338. open_file_t *open_file;
  339. FILE *f;
  340. microdesc_t **mdp;
  341. smartlist_t *wrote;
  342. ssize_t size;
  343. off_t off = 0;
  344. int orig_size, new_size;
  345. log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
  346. /* Remove dead descriptors */
  347. microdesc_cache_clean(cache, 0/*cutoff*/, 0/*force*/);
  348. /* Calculate starting disk usage */
  349. orig_size = (int)(cache->cache_content ? cache->cache_content->size : 0);
  350. orig_size += (int)cache->journal_len;
  351. f = start_writing_to_stdio_file(cache->cache_fname,
  352. OPEN_FLAGS_REPLACE|O_BINARY,
  353. 0600, &open_file);
  354. if (!f)
  355. return -1;
  356. wrote = smartlist_create();
  357. HT_FOREACH(mdp, microdesc_map, &cache->map) {
  358. microdesc_t *md = *mdp;
  359. size_t annotation_len;
  360. if (md->no_save)
  361. continue;
  362. size = dump_microdescriptor(f, md, &annotation_len);
  363. if (size < 0) {
  364. /* XXX handle errors from dump_microdescriptor() */
  365. /* log? return -1? die? coredump the universe? */
  366. continue;
  367. }
  368. tor_assert(((size_t)size) == annotation_len + md->bodylen);
  369. md->off = off + annotation_len;
  370. off += size;
  371. if (md->saved_location != SAVED_IN_CACHE) {
  372. tor_free(md->body);
  373. md->saved_location = SAVED_IN_CACHE;
  374. }
  375. smartlist_add(wrote, md);
  376. }
  377. finish_writing_to_file(open_file); /*XXX Check me.*/
  378. if (cache->cache_content)
  379. tor_munmap_file(cache->cache_content);
  380. cache->cache_content = tor_mmap_file(cache->cache_fname);
  381. if (!cache->cache_content && smartlist_len(wrote)) {
  382. log_err(LD_DIR, "Couldn't map file that we just wrote to %s!",
  383. cache->cache_fname);
  384. smartlist_free(wrote);
  385. return -1;
  386. }
  387. SMARTLIST_FOREACH_BEGIN(wrote, microdesc_t *, md) {
  388. tor_assert(md->saved_location == SAVED_IN_CACHE);
  389. md->body = (char*)cache->cache_content->data + md->off;
  390. if (PREDICT_UNLIKELY(
  391. md->bodylen < 9 || memcmp(md->body, "onion-key", 9) != 0)) {
  392. /* XXXX023 once bug 2022 is solved, we can kill this block and turn it
  393. * into just the tor_assert(!memcmp) */
  394. off_t avail = cache->cache_content->size - md->off;
  395. char *bad_str;
  396. tor_assert(avail >= 0);
  397. bad_str = tor_strndup(md->body, MIN(128, (size_t)avail));
  398. log_err(LD_BUG, "After rebuilding microdesc cache, offsets seem wrong. "
  399. " At offset %d, I expected to find a microdescriptor starting "
  400. " with \"onion-key\". Instead I got %s.",
  401. (int)md->off, escaped(bad_str));
  402. tor_free(bad_str);
  403. tor_assert(!memcmp(md->body, "onion-key", 9));
  404. }
  405. } SMARTLIST_FOREACH_END(md);
  406. smartlist_free(wrote);
  407. write_str_to_file(cache->journal_fname, "", 1);
  408. cache->journal_len = 0;
  409. cache->bytes_dropped = 0;
  410. new_size = (int)cache->cache_content->size;
  411. log_info(LD_DIR, "Done rebuilding microdesc cache. "
  412. "Saved %d bytes; %d still used.",
  413. orig_size-new_size, new_size);
  414. return 0;
  415. }
  416. /** Deallocate a single microdescriptor. Note: the microdescriptor MUST have
  417. * previously been removed from the cache if it had ever been inserted. */
  418. void
  419. microdesc_free(microdesc_t *md)
  420. {
  421. if (!md)
  422. return;
  423. /* Must be removed from hash table! */
  424. if (md->onion_pkey)
  425. crypto_free_pk_env(md->onion_pkey);
  426. if (md->body && md->saved_location != SAVED_IN_CACHE)
  427. tor_free(md->body);
  428. if (md->family) {
  429. SMARTLIST_FOREACH(md->family, char *, cp, tor_free(cp));
  430. smartlist_free(md->family);
  431. }
  432. short_policy_free(md->exit_policy);
  433. tor_free(md);
  434. }
  435. /** Free all storage held in the microdesc.c module. */
  436. void
  437. microdesc_free_all(void)
  438. {
  439. if (the_microdesc_cache) {
  440. microdesc_cache_clear(the_microdesc_cache);
  441. tor_free(the_microdesc_cache->cache_fname);
  442. tor_free(the_microdesc_cache->journal_fname);
  443. tor_free(the_microdesc_cache);
  444. }
  445. }
  446. /** If there is a microdescriptor in <b>cache</b> whose sha256 digest is
  447. * <b>d</b>, return it. Otherwise return NULL. */
  448. microdesc_t *
  449. microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
  450. {
  451. microdesc_t *md, search;
  452. if (!cache)
  453. cache = get_microdesc_cache();
  454. memcpy(search.digest, d, DIGEST256_LEN);
  455. md = HT_FIND(microdesc_map, &cache->map, &search);
  456. return md;
  457. }
  458. /** Return the mean size of decriptors added to <b>cache</b> since it was last
  459. * cleared. Used to estimate the size of large downloads. */
  460. size_t
  461. microdesc_average_size(microdesc_cache_t *cache)
  462. {
  463. if (!cache)
  464. cache = get_microdesc_cache();
  465. if (!cache->n_seen)
  466. return 512;
  467. return (size_t)(cache->total_len_seen / cache->n_seen);
  468. }
  469. /** Return a smartlist of all the sha256 digest of the microdescriptors that
  470. * are listed in <b>ns</b> but not present in <b>cache</b>. Returns pointers
  471. * to internals of <b>ns</b>; you should not free the members of the resulting
  472. * smartlist. Omit all microdescriptors whose digest appear in <b>skip</b>. */
  473. smartlist_t *
  474. microdesc_list_missing_digest256(networkstatus_t *ns, microdesc_cache_t *cache,
  475. int downloadable_only, digestmap_t *skip)
  476. {
  477. smartlist_t *result = smartlist_create();
  478. time_t now = time(NULL);
  479. tor_assert(ns->flavor == FLAV_MICRODESC);
  480. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  481. if (microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest))
  482. continue;
  483. if (downloadable_only &&
  484. !download_status_is_ready(&rs->dl_status, now,
  485. MAX_MICRODESC_DOWNLOAD_FAILURES))
  486. continue;
  487. if (skip && digestmap_get(skip, rs->descriptor_digest))
  488. continue;
  489. /* XXXX Also skip if we're a noncache and wouldn't use this router.
  490. * XXXX NM Microdesc
  491. */
  492. smartlist_add(result, rs->descriptor_digest);
  493. } SMARTLIST_FOREACH_END(rs);
  494. return result;
  495. }
  496. /** Launch download requests for mircodescriptors as appropriate.
  497. *
  498. * Specifically, we should launch download requests if we are configured to
  499. * download mirodescriptors, and there are some microdescriptors listed the
  500. * current microdesc consensus that we don't have, and either we never asked
  501. * for them, or we failed to download them but we're willing to retry.
  502. */
  503. void
  504. update_microdesc_downloads(time_t now)
  505. {
  506. or_options_t *options = get_options();
  507. networkstatus_t *consensus;
  508. smartlist_t *missing;
  509. digestmap_t *pending;
  510. if (should_delay_dir_fetches(options))
  511. return;
  512. if (directory_too_idle_to_fetch_descriptors(options, now))
  513. return;
  514. consensus = networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  515. if (!consensus)
  516. return;
  517. if (!directory_caches_dir_info(options)) {
  518. /* Right now, only caches fetch microdescriptors.
  519. * XXXX NM Microdescs */
  520. return;
  521. }
  522. pending = digestmap_new();
  523. list_pending_microdesc_downloads(pending);
  524. missing = microdesc_list_missing_digest256(consensus,
  525. get_microdesc_cache(),
  526. 1,
  527. pending);
  528. digestmap_free(pending, NULL);
  529. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC,
  530. missing, NULL, now);
  531. smartlist_free(missing);
  532. }
  533. /** For every microdescriptor listed in the current microdecriptor consensus,
  534. * update its last_listed field to be at least as recent as the publication
  535. * time of the current microdescriptor consensus.
  536. */
  537. void
  538. update_microdescs_from_networkstatus(time_t now)
  539. {
  540. microdesc_cache_t *cache = get_microdesc_cache();
  541. microdesc_t *md;
  542. networkstatus_t *ns =
  543. networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  544. if (! ns)
  545. return;
  546. tor_assert(ns->flavor == FLAV_MICRODESC);
  547. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  548. md = microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest);
  549. if (md && ns->valid_after > md->last_listed)
  550. md->last_listed = ns->valid_after;
  551. } SMARTLIST_FOREACH_END(rs);
  552. }