microdesc.c 12 KB

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