microdesc.c 25 KB

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