microdesc.c 12 KB

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