microdesc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (c) 2009, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. /** A data structure to hold a bunch of cached microdescriptors. There are
  5. * two active files in the cache: a "cache file" that we mmap, and a "journal
  6. * file" that we append to. Periodically, we rebuild the cache file to hold
  7. * only the microdescriptors that we want to keep */
  8. struct microdesc_cache_t {
  9. /** Map from sha256-digest to microdesc_t for every microdesc_t in the
  10. * cache. */
  11. HT_HEAD(microdesc_map, microdesc_t) map;
  12. /** Name of the cache file. */
  13. char *cache_fname;
  14. /** Name of the journal file. */
  15. char *journal_fname;
  16. /** Mmap'd contents of the cache file, or NULL if there is none. */
  17. tor_mmap_t *cache_content;
  18. /** Number of bytes used in the journal file. */
  19. size_t journal_len;
  20. /** Total bytes of microdescriptor bodies we have added to this cache */
  21. uint64_t total_len_seen;
  22. /** Total number of microdescriptors we have added to this cache */
  23. unsigned n_seen;
  24. };
  25. /** Helper: computes a hash of <b>md</b> to place it in a hash table. */
  26. static INLINE unsigned int
  27. _microdesc_hash(microdesc_t *md)
  28. {
  29. unsigned *d = (unsigned*)md->digest;
  30. #if SIZEOF_INT == 4
  31. return d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[6] ^ d[7];
  32. #else
  33. return d[0] ^ d[1] ^ d[2] ^ d[3];
  34. #endif
  35. }
  36. /** Helper: compares <b>a</b> and </b> for equality for hash-table purposes. */
  37. static INLINE int
  38. _microdesc_eq(microdesc_t *a, microdesc_t *b)
  39. {
  40. return !memcmp(a->digest, b->digest, DIGEST256_LEN);
  41. }
  42. HT_PROTOTYPE(microdesc_map, microdesc_t, node,
  43. _microdesc_hash, _microdesc_eq);
  44. HT_GENERATE(microdesc_map, microdesc_t, node,
  45. _microdesc_hash, _microdesc_eq, 0.6,
  46. _tor_malloc, _tor_realloc, _tor_free);
  47. /** Write the body of <b>md</b> into <b>f</b>, with appropriate annotations.
  48. * On success, return the total number of bytes written, and set
  49. * *<b>annotation_len_out</b> to the number of bytes written as
  50. * annotations. */
  51. static ssize_t
  52. dump_microdescriptor(FILE *f, microdesc_t *md, size_t *annotation_len_out)
  53. {
  54. ssize_t r = 0;
  55. size_t written;
  56. /* XXXX drops unkown annotations. */
  57. if (md->last_listed) {
  58. char buf[ISO_TIME_LEN+1];
  59. char annotation[ISO_TIME_LEN+32];
  60. format_iso_time(buf, md->last_listed);
  61. tor_snprintf(annotation, sizeof(annotation), "@last-listed %s\n", buf);
  62. fputs(annotation, f);
  63. r += strlen(annotation);
  64. *annotation_len_out = r;
  65. } else {
  66. *annotation_len_out = 0;
  67. }
  68. md->off = (off_t) ftell(f);
  69. written = fwrite(md->body, 1, md->bodylen, f);
  70. if (written != md->bodylen) {
  71. log_warn(LD_DIR,
  72. "Couldn't dump microdescriptor (wrote %lu out of %lu): %s",
  73. (unsigned long)written, (unsigned long)md->bodylen,
  74. strerror(ferror(f)));
  75. return -1;
  76. }
  77. r += md->bodylen;
  78. return r;
  79. }
  80. /** Holds a pointer to the current microdesc_cache_t object, or NULL if no
  81. * such object has been allocated. */
  82. static microdesc_cache_t *the_microdesc_cache = NULL;
  83. /** Return a pointer to the microdescriptor cache, loading it if necessary. */
  84. microdesc_cache_t *
  85. get_microdesc_cache(void)
  86. {
  87. if (PREDICT_UNLIKELY(the_microdesc_cache==NULL)) {
  88. microdesc_cache_t *cache = tor_malloc_zero(sizeof(microdesc_cache_t));
  89. HT_INIT(microdesc_map, &cache->map);
  90. cache->cache_fname = get_datadir_fname("cached-microdescs");
  91. cache->journal_fname = get_datadir_fname("cached-microdescs.new");
  92. microdesc_cache_reload(cache);
  93. the_microdesc_cache = cache;
  94. }
  95. return the_microdesc_cache;
  96. }
  97. /* There are three sources of microdescriptors:
  98. 1) Generated by us while acting as a directory authority.
  99. 2) Loaded from the cache on disk.
  100. 3) Downloaded.
  101. */
  102. /** Decode the microdescriptors from the string starting at <b>s</b> and
  103. * ending at <b>eos</b>, and store them in <b>cache</b>. If <b>no-save</b>,
  104. * mark them as non-writable to disk. If <b>where</b> is SAVED_IN_CACHE,
  105. * leave their bodies as pointers to the mmap'd cache. If where is
  106. * <b>SAVED_NOWHERE</b>, do not allow annotations. Return a list of the added
  107. * microdescriptors. */
  108. smartlist_t *
  109. microdescs_add_to_cache(microdesc_cache_t *cache,
  110. const char *s, const char *eos, saved_location_t where,
  111. int no_save)
  112. {
  113. /*XXXX need an argument that sets last_listed as appropriate. */
  114. smartlist_t *descriptors, *added;
  115. const int allow_annotations = (where != SAVED_NOWHERE);
  116. const int copy_body = (where != SAVED_IN_CACHE);
  117. descriptors = microdescs_parse_from_string(s, eos,
  118. allow_annotations,
  119. copy_body);
  120. added = microdescs_add_list_to_cache(cache, descriptors, where, no_save);
  121. smartlist_free(descriptors);
  122. return added;
  123. }
  124. /* As microdescs_add_to_cache, but takes a list of micrdescriptors instead of
  125. * a string to encode. Frees any members of <b>descriptors</b> that it does
  126. * not add. */
  127. smartlist_t *
  128. microdescs_add_list_to_cache(microdesc_cache_t *cache,
  129. smartlist_t *descriptors, saved_location_t where,
  130. int no_save)
  131. {
  132. smartlist_t *added;
  133. open_file_t *open_file = NULL;
  134. FILE *f = NULL;
  135. // int n_added = 0;
  136. ssize_t size = 0;
  137. if (where == SAVED_NOWHERE && !no_save) {
  138. f = start_writing_to_stdio_file(cache->journal_fname,
  139. OPEN_FLAGS_APPEND|O_BINARY,
  140. 0600, &open_file);
  141. if (!f) {
  142. log_warn(LD_DIR, "Couldn't append to journal in %s: %s",
  143. cache->journal_fname, strerror(errno));
  144. return NULL;
  145. }
  146. }
  147. added = smartlist_create();
  148. SMARTLIST_FOREACH_BEGIN(descriptors, microdesc_t *, md) {
  149. microdesc_t *md2;
  150. md2 = HT_FIND(microdesc_map, &cache->map, md);
  151. if (md2) {
  152. /* We already had this one. */
  153. if (md2->last_listed < md->last_listed)
  154. md2->last_listed = md->last_listed;
  155. microdesc_free(md);
  156. continue;
  157. }
  158. /* Okay, it's a new one. */
  159. if (f) {
  160. size_t annotation_len;
  161. size = dump_microdescriptor(f, md, &annotation_len);
  162. if (size < 0) {
  163. /* XXX handle errors from dump_microdescriptor() */
  164. /* log? return -1? die? coredump the universe? */
  165. continue;
  166. }
  167. md->saved_location = SAVED_IN_JOURNAL;
  168. cache->journal_len += size;
  169. } else {
  170. md->saved_location = where;
  171. }
  172. md->no_save = no_save;
  173. HT_INSERT(microdesc_map, &cache->map, md);
  174. smartlist_add(added, md);
  175. ++cache->n_seen;
  176. cache->total_len_seen += md->bodylen;
  177. } SMARTLIST_FOREACH_END(md);
  178. if (f)
  179. finish_writing_to_file(open_file); /*XXX Check me.*/
  180. {
  181. size_t old_content_len =
  182. cache->cache_content ? cache->cache_content->size : 0;
  183. if (cache->journal_len > 16384 + old_content_len &&
  184. cache->journal_len > old_content_len * 2) {
  185. microdesc_cache_rebuild(cache);
  186. }
  187. }
  188. return added;
  189. }
  190. /** Remove every microdescriptor in <b>cache</b>. */
  191. void
  192. microdesc_cache_clear(microdesc_cache_t *cache)
  193. {
  194. microdesc_t **entry, **next;
  195. for (entry = HT_START(microdesc_map, &cache->map); entry; entry = next) {
  196. microdesc_t *md = *entry;
  197. next = HT_NEXT_RMV(microdesc_map, &cache->map, entry);
  198. microdesc_free(md);
  199. }
  200. HT_CLEAR(microdesc_map, &cache->map);
  201. if (cache->cache_content) {
  202. tor_munmap_file(cache->cache_content);
  203. cache->cache_content = NULL;
  204. }
  205. cache->total_len_seen = 0;
  206. cache->n_seen = 0;
  207. }
  208. /** Reload the contents of <b>cache</b> from disk. If it is empty, load it
  209. * for the first time. Return 0 on success, -1 on failure. */
  210. int
  211. microdesc_cache_reload(microdesc_cache_t *cache)
  212. {
  213. struct stat st;
  214. char *journal_content;
  215. smartlist_t *added;
  216. tor_mmap_t *mm;
  217. int total = 0;
  218. microdesc_cache_clear(cache);
  219. mm = cache->cache_content = tor_mmap_file(cache->cache_fname);
  220. if (mm) {
  221. added = microdescs_add_to_cache(cache, mm->data, mm->data+mm->size,
  222. SAVED_IN_CACHE, 0);
  223. if (added) {
  224. total += smartlist_len(added);
  225. smartlist_free(added);
  226. }
  227. }
  228. journal_content = read_file_to_str(cache->journal_fname,
  229. RFTS_IGNORE_MISSING, &st);
  230. if (journal_content) {
  231. added = microdescs_add_to_cache(cache, journal_content,
  232. journal_content+st.st_size,
  233. SAVED_IN_JOURNAL, 0);
  234. if (added) {
  235. total += smartlist_len(added);
  236. smartlist_free(added);
  237. }
  238. tor_free(journal_content);
  239. }
  240. log_notice(LD_DIR, "Reloaded microdescriptor cache. Found %d descriptors.",
  241. total);
  242. return 0;
  243. }
  244. /** Regenerate the main cache file for <b>cache</b>, clear the journal file,
  245. * and update every microdesc_t in the cache with pointers to its new
  246. * location. */
  247. int
  248. microdesc_cache_rebuild(microdesc_cache_t *cache)
  249. {
  250. open_file_t *open_file;
  251. FILE *f;
  252. microdesc_t **mdp;
  253. smartlist_t *wrote;
  254. ssize_t size;
  255. off_t off = 0;
  256. int orig_size, new_size;
  257. log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
  258. orig_size = (int)(cache->cache_content ? cache->cache_content->size : 0);
  259. orig_size += (int)cache->journal_len;
  260. f = start_writing_to_stdio_file(cache->cache_fname,
  261. OPEN_FLAGS_REPLACE|O_BINARY,
  262. 0600, &open_file);
  263. if (!f)
  264. return -1;
  265. wrote = smartlist_create();
  266. HT_FOREACH(mdp, microdesc_map, &cache->map) {
  267. microdesc_t *md = *mdp;
  268. size_t annotation_len;
  269. if (md->no_save)
  270. continue;
  271. size = dump_microdescriptor(f, md, &annotation_len);
  272. if (size < 0) {
  273. /* XXX handle errors from dump_microdescriptor() */
  274. /* log? return -1? die? coredump the universe? */
  275. continue;
  276. }
  277. md->off = off + annotation_len;
  278. off += size;
  279. if (md->saved_location != SAVED_IN_CACHE) {
  280. tor_free(md->body);
  281. md->saved_location = SAVED_IN_CACHE;
  282. }
  283. smartlist_add(wrote, md);
  284. }
  285. finish_writing_to_file(open_file); /*XXX Check me.*/
  286. if (cache->cache_content)
  287. tor_munmap_file(cache->cache_content);
  288. cache->cache_content = tor_mmap_file(cache->cache_fname);
  289. if (!cache->cache_content && smartlist_len(wrote)) {
  290. log_err(LD_DIR, "Couldn't map file that we just wrote to %s!",
  291. cache->cache_fname);
  292. smartlist_free(wrote);
  293. return -1;
  294. }
  295. SMARTLIST_FOREACH_BEGIN(wrote, microdesc_t *, md) {
  296. tor_assert(md->saved_location == SAVED_IN_CACHE);
  297. md->body = (char*)cache->cache_content->data + md->off;
  298. tor_assert(!memcmp(md->body, "onion-key", 9));
  299. } SMARTLIST_FOREACH_END(md);
  300. smartlist_free(wrote);
  301. write_str_to_file(cache->journal_fname, "", 1);
  302. cache->journal_len = 0;
  303. new_size = (int)cache->cache_content->size;
  304. log_info(LD_DIR, "Done rebuilding microdesc cache. "
  305. "Saved %d bytes; %d still used.",
  306. orig_size-new_size, new_size);
  307. return 0;
  308. }
  309. /** Deallocate a single microdescriptor. Note: the microdescriptor MUST have
  310. * previously been removed from the cache if it had ever been inserted. */
  311. void
  312. microdesc_free(microdesc_t *md)
  313. {
  314. if (!md)
  315. return;
  316. /* Must be removed from hash table! */
  317. if (md->onion_pkey)
  318. crypto_free_pk_env(md->onion_pkey);
  319. if (md->body && md->saved_location != SAVED_IN_CACHE)
  320. tor_free(md->body);
  321. if (md->family) {
  322. SMARTLIST_FOREACH(md->family, char *, cp, tor_free(cp));
  323. smartlist_free(md->family);
  324. }
  325. tor_free(md->exitsummary);
  326. tor_free(md);
  327. }
  328. /** Free all storage held in the microdesc.c module. */
  329. void
  330. microdesc_free_all(void)
  331. {
  332. if (the_microdesc_cache) {
  333. microdesc_cache_clear(the_microdesc_cache);
  334. tor_free(the_microdesc_cache->cache_fname);
  335. tor_free(the_microdesc_cache->journal_fname);
  336. tor_free(the_microdesc_cache);
  337. }
  338. }
  339. /** If there is a microdescriptor in <b>cache</b> whose sha256 digest is
  340. * <b>d</b>, return it. Otherwise return NULL. */
  341. microdesc_t *
  342. microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
  343. {
  344. microdesc_t *md, search;
  345. if (!cache)
  346. cache = get_microdesc_cache();
  347. memcpy(search.digest, d, DIGEST256_LEN);
  348. md = HT_FIND(microdesc_map, &cache->map, &search);
  349. return md;
  350. }
  351. /** Return the mean size of decriptors added to <b>cache</b> since it was last
  352. * cleared. Used to estimate the size of large downloads. */
  353. size_t
  354. microdesc_average_size(microdesc_cache_t *cache)
  355. {
  356. if (!cache)
  357. cache = get_microdesc_cache();
  358. if (!cache->n_seen)
  359. return 512;
  360. return (size_t)(cache->total_len_seen / cache->n_seen);
  361. }