microdesc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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_new();
  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. md->held_in_map = 1;
  221. smartlist_add(added, md);
  222. ++cache->n_seen;
  223. cache->total_len_seen += md->bodylen;
  224. } SMARTLIST_FOREACH_END(md);
  225. if (f)
  226. finish_writing_to_file(open_file); /*XXX Check me.*/
  227. {
  228. networkstatus_t *ns = networkstatus_get_latest_consensus();
  229. if (ns && ns->flavor == FLAV_MICRODESC)
  230. SMARTLIST_FOREACH(added, microdesc_t *, md, nodelist_add_microdesc(md));
  231. }
  232. if (smartlist_len(added))
  233. router_dir_info_changed();
  234. return added;
  235. }
  236. /** Remove every microdescriptor in <b>cache</b>. */
  237. void
  238. microdesc_cache_clear(microdesc_cache_t *cache)
  239. {
  240. microdesc_t **entry, **next;
  241. for (entry = HT_START(microdesc_map, &cache->map); entry; entry = next) {
  242. microdesc_t *md = *entry;
  243. next = HT_NEXT_RMV(microdesc_map, &cache->map, entry);
  244. md->held_in_map = 0;
  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. cache->bytes_dropped = 0;
  255. }
  256. /** Reload the contents of <b>cache</b> from disk. If it is empty, load it
  257. * for the first time. Return 0 on success, -1 on failure. */
  258. int
  259. microdesc_cache_reload(microdesc_cache_t *cache)
  260. {
  261. struct stat st;
  262. char *journal_content;
  263. smartlist_t *added;
  264. tor_mmap_t *mm;
  265. int total = 0;
  266. microdesc_cache_clear(cache);
  267. mm = cache->cache_content = tor_mmap_file(cache->cache_fname);
  268. if (mm) {
  269. added = microdescs_add_to_cache(cache, mm->data, mm->data+mm->size,
  270. SAVED_IN_CACHE, 0, -1, NULL);
  271. if (added) {
  272. total += smartlist_len(added);
  273. smartlist_free(added);
  274. }
  275. }
  276. journal_content = read_file_to_str(cache->journal_fname,
  277. RFTS_IGNORE_MISSING, &st);
  278. if (journal_content) {
  279. cache->journal_len = (size_t) st.st_size;
  280. added = microdescs_add_to_cache(cache, journal_content,
  281. journal_content+st.st_size,
  282. SAVED_IN_JOURNAL, 0, -1, NULL);
  283. if (added) {
  284. total += smartlist_len(added);
  285. smartlist_free(added);
  286. }
  287. tor_free(journal_content);
  288. }
  289. log_notice(LD_DIR, "Reloaded microdescriptor cache. Found %d descriptors.",
  290. total);
  291. microdesc_cache_rebuild(cache, 0 /* don't force */);
  292. return 0;
  293. }
  294. /** By default, we remove any microdescriptors that have gone at least this
  295. * long without appearing in a current consensus. */
  296. #define TOLERATE_MICRODESC_AGE (7*24*60*60)
  297. /** Remove all microdescriptors from <b>cache</b> that haven't been listed for
  298. * a long time. Does not rebuild the cache on disk. If <b>cutoff</b> is
  299. * positive, specifically remove microdescriptors that have been unlisted
  300. * since <b>cutoff</b>. If <b>force</b> is true, remove microdescriptors even
  301. * if we have no current live microdescriptor consensus.
  302. */
  303. void
  304. microdesc_cache_clean(microdesc_cache_t *cache, time_t cutoff, int force)
  305. {
  306. microdesc_t **mdp, *victim;
  307. int dropped=0, kept=0;
  308. size_t bytes_dropped = 0;
  309. time_t now = time(NULL);
  310. /* If we don't know a live consensus, don't believe last_listed values: we
  311. * might be starting up after being down for a while. */
  312. if (! force &&
  313. ! networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC))
  314. return;
  315. if (cutoff <= 0)
  316. cutoff = now - TOLERATE_MICRODESC_AGE;
  317. for (mdp = HT_START(microdesc_map, &cache->map); mdp != NULL; ) {
  318. if ((*mdp)->last_listed < cutoff) {
  319. ++dropped;
  320. victim = *mdp;
  321. mdp = HT_NEXT_RMV(microdesc_map, &cache->map, mdp);
  322. victim->held_in_map = 0;
  323. bytes_dropped += victim->bodylen;
  324. microdesc_free(victim);
  325. } else {
  326. ++kept;
  327. mdp = HT_NEXT(microdesc_map, &cache->map, mdp);
  328. }
  329. }
  330. if (dropped) {
  331. log_notice(LD_DIR, "Removed %d/%d microdescriptors as old.",
  332. dropped,dropped+kept);
  333. cache->bytes_dropped += bytes_dropped;
  334. }
  335. }
  336. static int
  337. should_rebuild_md_cache(microdesc_cache_t *cache)
  338. {
  339. const size_t old_len =
  340. cache->cache_content ? cache->cache_content->size : 0;
  341. const size_t journal_len = cache->journal_len;
  342. const size_t dropped = cache->bytes_dropped;
  343. if (journal_len < 16384)
  344. return 0; /* Don't bother, not enough has happened yet. */
  345. if (dropped > (journal_len + old_len) / 3)
  346. return 1; /* We could save 1/3 or more of the currently used space. */
  347. if (journal_len > old_len / 2)
  348. return 1; /* We should append to the regular file */
  349. return 0;
  350. }
  351. /** Regenerate the main cache file for <b>cache</b>, clear the journal file,
  352. * and update every microdesc_t in the cache with pointers to its new
  353. * location. If <b>force</b> is true, do this unconditionally. If
  354. * <b>force</b> is false, do it only if we expect to save space on disk. */
  355. int
  356. microdesc_cache_rebuild(microdesc_cache_t *cache, int force)
  357. {
  358. open_file_t *open_file;
  359. FILE *f;
  360. microdesc_t **mdp;
  361. smartlist_t *wrote;
  362. ssize_t size;
  363. off_t off = 0;
  364. int orig_size, new_size;
  365. if (cache == NULL) {
  366. cache = the_microdesc_cache;
  367. if (cache == NULL)
  368. return 0;
  369. }
  370. /* Remove dead descriptors */
  371. microdesc_cache_clean(cache, 0/*cutoff*/, 0/*force*/);
  372. if (!force && !should_rebuild_md_cache(cache))
  373. return 0;
  374. log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
  375. orig_size = (int)(cache->cache_content ? cache->cache_content->size : 0);
  376. orig_size += (int)cache->journal_len;
  377. f = start_writing_to_stdio_file(cache->cache_fname,
  378. OPEN_FLAGS_REPLACE|O_BINARY,
  379. 0600, &open_file);
  380. if (!f)
  381. return -1;
  382. wrote = smartlist_new();
  383. HT_FOREACH(mdp, microdesc_map, &cache->map) {
  384. microdesc_t *md = *mdp;
  385. size_t annotation_len;
  386. if (md->no_save)
  387. continue;
  388. size = dump_microdescriptor(f, md, &annotation_len);
  389. if (size < 0) {
  390. /* XXX handle errors from dump_microdescriptor() */
  391. /* log? return -1? die? coredump the universe? */
  392. continue;
  393. }
  394. tor_assert(((size_t)size) == annotation_len + md->bodylen);
  395. md->off = off + annotation_len;
  396. off += size;
  397. if (md->saved_location != SAVED_IN_CACHE) {
  398. tor_free(md->body);
  399. md->saved_location = SAVED_IN_CACHE;
  400. }
  401. smartlist_add(wrote, md);
  402. }
  403. if (cache->cache_content)
  404. tor_munmap_file(cache->cache_content);
  405. finish_writing_to_file(open_file); /*XXX Check me.*/
  406. cache->cache_content = tor_mmap_file(cache->cache_fname);
  407. if (!cache->cache_content && smartlist_len(wrote)) {
  408. log_err(LD_DIR, "Couldn't map file that we just wrote to %s!",
  409. cache->cache_fname);
  410. smartlist_free(wrote);
  411. return -1;
  412. }
  413. SMARTLIST_FOREACH_BEGIN(wrote, microdesc_t *, md) {
  414. tor_assert(md->saved_location == SAVED_IN_CACHE);
  415. md->body = (char*)cache->cache_content->data + md->off;
  416. if (PREDICT_UNLIKELY(
  417. md->bodylen < 9 || fast_memneq(md->body, "onion-key", 9) != 0)) {
  418. /* XXXX023 once bug 2022 is solved, we can kill this block and turn it
  419. * into just the tor_assert(!memcmp) */
  420. off_t avail = cache->cache_content->size - md->off;
  421. char *bad_str;
  422. tor_assert(avail >= 0);
  423. bad_str = tor_strndup(md->body, MIN(128, (size_t)avail));
  424. log_err(LD_BUG, "After rebuilding microdesc cache, offsets seem wrong. "
  425. " At offset %d, I expected to find a microdescriptor starting "
  426. " with \"onion-key\". Instead I got %s.",
  427. (int)md->off, escaped(bad_str));
  428. tor_free(bad_str);
  429. tor_assert(fast_memeq(md->body, "onion-key", 9));
  430. }
  431. } SMARTLIST_FOREACH_END(md);
  432. smartlist_free(wrote);
  433. write_str_to_file(cache->journal_fname, "", 1);
  434. cache->journal_len = 0;
  435. cache->bytes_dropped = 0;
  436. new_size = cache->cache_content ? (int)cache->cache_content->size : 0;
  437. log_info(LD_DIR, "Done rebuilding microdesc cache. "
  438. "Saved %d bytes; %d still used.",
  439. orig_size-new_size, new_size);
  440. return 0;
  441. }
  442. /** Make sure that the reference count of every microdescriptor in cache is
  443. * accurate. */
  444. void
  445. microdesc_check_counts(void)
  446. {
  447. microdesc_t **mdp;
  448. if (!the_microdesc_cache)
  449. return;
  450. HT_FOREACH(mdp, microdesc_map, &the_microdesc_cache->map) {
  451. microdesc_t *md = *mdp;
  452. unsigned int found=0;
  453. const smartlist_t *nodes = nodelist_get_list();
  454. SMARTLIST_FOREACH(nodes, node_t *, node, {
  455. if (node->md == md) {
  456. ++found;
  457. }
  458. });
  459. tor_assert(found == md->held_by_nodes);
  460. }
  461. }
  462. /** Deallocate a single microdescriptor. Note: the microdescriptor MUST have
  463. * previously been removed from the cache if it had ever been inserted. */
  464. void
  465. microdesc_free(microdesc_t *md)
  466. {
  467. if (!md)
  468. return;
  469. /* Make sure that the microdesc was really removed from the appropriate data
  470. structures. */
  471. if (md->held_in_map) {
  472. microdesc_cache_t *cache = get_microdesc_cache();
  473. microdesc_t *md2 = HT_FIND(microdesc_map, &cache->map, md);
  474. if (md2 == md) {
  475. log_warn(LD_BUG, "microdesc_free() called, but md was still in "
  476. "microdesc_map");
  477. HT_REMOVE(microdesc_map, &cache->map, md);
  478. } else {
  479. log_warn(LD_BUG, "microdesc_free() called with held_in_map set, but "
  480. "microdesc was not in the map.");
  481. }
  482. tor_fragile_assert();
  483. }
  484. if (md->held_by_nodes) {
  485. int found=0;
  486. const smartlist_t *nodes = nodelist_get_list();
  487. SMARTLIST_FOREACH(nodes, node_t *, node, {
  488. if (node->md == md) {
  489. ++found;
  490. node->md = NULL;
  491. }
  492. });
  493. if (found) {
  494. log_warn(LD_BUG, "microdesc_free() called, but md was still referenced "
  495. "%d node(s); held_by_nodes == %u", found, md->held_by_nodes);
  496. } else {
  497. log_warn(LD_BUG, "microdesc_free() called with held_by_nodes set to %u, "
  498. "but md was not referenced by any nodes", md->held_by_nodes);
  499. }
  500. tor_fragile_assert();
  501. }
  502. //tor_assert(md->held_in_map == 0);
  503. //tor_assert(md->held_by_nodes == 0);
  504. if (md->onion_pkey)
  505. crypto_pk_free(md->onion_pkey);
  506. if (md->body && md->saved_location != SAVED_IN_CACHE)
  507. tor_free(md->body);
  508. if (md->family) {
  509. SMARTLIST_FOREACH(md->family, char *, cp, tor_free(cp));
  510. smartlist_free(md->family);
  511. }
  512. short_policy_free(md->exit_policy);
  513. tor_free(md);
  514. }
  515. /** Free all storage held in the microdesc.c module. */
  516. void
  517. microdesc_free_all(void)
  518. {
  519. if (the_microdesc_cache) {
  520. microdesc_cache_clear(the_microdesc_cache);
  521. tor_free(the_microdesc_cache->cache_fname);
  522. tor_free(the_microdesc_cache->journal_fname);
  523. tor_free(the_microdesc_cache);
  524. }
  525. }
  526. /** If there is a microdescriptor in <b>cache</b> whose sha256 digest is
  527. * <b>d</b>, return it. Otherwise return NULL. */
  528. microdesc_t *
  529. microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
  530. {
  531. microdesc_t *md, search;
  532. if (!cache)
  533. cache = get_microdesc_cache();
  534. memcpy(search.digest, d, DIGEST256_LEN);
  535. md = HT_FIND(microdesc_map, &cache->map, &search);
  536. return md;
  537. }
  538. /** Return the mean size of decriptors added to <b>cache</b> since it was last
  539. * cleared. Used to estimate the size of large downloads. */
  540. size_t
  541. microdesc_average_size(microdesc_cache_t *cache)
  542. {
  543. if (!cache)
  544. cache = get_microdesc_cache();
  545. if (!cache->n_seen)
  546. return 512;
  547. return (size_t)(cache->total_len_seen / cache->n_seen);
  548. }
  549. /** Return a smartlist of all the sha256 digest of the microdescriptors that
  550. * are listed in <b>ns</b> but not present in <b>cache</b>. Returns pointers
  551. * to internals of <b>ns</b>; you should not free the members of the resulting
  552. * smartlist. Omit all microdescriptors whose digest appear in <b>skip</b>. */
  553. smartlist_t *
  554. microdesc_list_missing_digest256(networkstatus_t *ns, microdesc_cache_t *cache,
  555. int downloadable_only, digestmap_t *skip)
  556. {
  557. smartlist_t *result = smartlist_new();
  558. time_t now = time(NULL);
  559. tor_assert(ns->flavor == FLAV_MICRODESC);
  560. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  561. if (microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest))
  562. continue;
  563. if (downloadable_only &&
  564. !download_status_is_ready(&rs->dl_status, now,
  565. MAX_MICRODESC_DOWNLOAD_FAILURES))
  566. continue;
  567. if (skip && digestmap_get(skip, rs->descriptor_digest))
  568. continue;
  569. if (tor_mem_is_zero(rs->descriptor_digest, DIGEST256_LEN))
  570. continue; /* This indicates a bug somewhere XXXX023*/
  571. /* XXXX Also skip if we're a noncache and wouldn't use this router.
  572. * XXXX NM Microdesc
  573. */
  574. smartlist_add(result, rs->descriptor_digest);
  575. } SMARTLIST_FOREACH_END(rs);
  576. return result;
  577. }
  578. /** Launch download requests for mircodescriptors as appropriate.
  579. *
  580. * Specifically, we should launch download requests if we are configured to
  581. * download mirodescriptors, and there are some microdescriptors listed the
  582. * current microdesc consensus that we don't have, and either we never asked
  583. * for them, or we failed to download them but we're willing to retry.
  584. */
  585. void
  586. update_microdesc_downloads(time_t now)
  587. {
  588. const or_options_t *options = get_options();
  589. networkstatus_t *consensus;
  590. smartlist_t *missing;
  591. digestmap_t *pending;
  592. if (should_delay_dir_fetches(options))
  593. return;
  594. if (directory_too_idle_to_fetch_descriptors(options, now))
  595. return;
  596. consensus = networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  597. if (!consensus)
  598. return;
  599. if (!we_fetch_microdescriptors(options))
  600. return;
  601. pending = digestmap_new();
  602. list_pending_microdesc_downloads(pending);
  603. missing = microdesc_list_missing_digest256(consensus,
  604. get_microdesc_cache(),
  605. 1,
  606. pending);
  607. digestmap_free(pending, NULL);
  608. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC,
  609. missing, NULL, now);
  610. smartlist_free(missing);
  611. }
  612. /** For every microdescriptor listed in the current microdecriptor consensus,
  613. * update its last_listed field to be at least as recent as the publication
  614. * time of the current microdescriptor consensus.
  615. */
  616. void
  617. update_microdescs_from_networkstatus(time_t now)
  618. {
  619. microdesc_cache_t *cache = get_microdesc_cache();
  620. microdesc_t *md;
  621. networkstatus_t *ns =
  622. networkstatus_get_reasonably_live_consensus(now, FLAV_MICRODESC);
  623. if (! ns)
  624. return;
  625. tor_assert(ns->flavor == FLAV_MICRODESC);
  626. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  627. md = microdesc_cache_lookup_by_digest256(cache, rs->descriptor_digest);
  628. if (md && ns->valid_after > md->last_listed)
  629. md->last_listed = ns->valid_after;
  630. } SMARTLIST_FOREACH_END(rs);
  631. }
  632. /** Return true iff we should prefer to use microdescriptors rather than
  633. * routerdescs for building circuits. */
  634. int
  635. we_use_microdescriptors_for_circuits(const or_options_t *options)
  636. {
  637. int ret = options->UseMicrodescriptors;
  638. if (ret == -1) {
  639. /* UseMicrodescriptors is "auto"; we need to decide: */
  640. /* So we decide that we'll use microdescriptors iff we are not a server,
  641. * and we're not autofetching everything. */
  642. ret = !server_mode(options) && !options->FetchUselessDescriptors;
  643. }
  644. return ret;
  645. }
  646. /** Return true iff we should try to download microdescriptors at all. */
  647. int
  648. we_fetch_microdescriptors(const or_options_t *options)
  649. {
  650. if (directory_caches_dir_info(options))
  651. return 1;
  652. if (options->FetchUselessDescriptors)
  653. return 1;
  654. return we_use_microdescriptors_for_circuits(options);
  655. }
  656. /** Return true iff we should try to download router descriptors at all. */
  657. int
  658. we_fetch_router_descriptors(const or_options_t *options)
  659. {
  660. if (directory_caches_dir_info(options))
  661. return 1;
  662. if (options->FetchUselessDescriptors)
  663. return 1;
  664. return ! we_use_microdescriptors_for_circuits(options);
  665. }
  666. /** Return the consensus flavor we actually want to use to build circuits. */
  667. int
  668. usable_consensus_flavor(void)
  669. {
  670. if (we_use_microdescriptors_for_circuits(get_options())) {
  671. return FLAV_MICRODESC;
  672. } else {
  673. return FLAV_NS;
  674. }
  675. }