microdesc.c 20 KB

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