networkstatus.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char networkstatus_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file Functions and structures for handling network status documents as a
  10. * client or cache.
  11. */
  12. #include "or.h"
  13. /* For tracking v2 networkstatus documents. Only caches do this now. */
  14. /** Map from descriptor digest of routers listed in the v2 networkstatus
  15. * documents to download_status_t* */
  16. static digestmap_t *v2_download_status_map = NULL;
  17. /** Global list of all of the current v2 network_status documents that we know
  18. * about. This list is kept sorted by published_on. */
  19. static smartlist_t *networkstatus_v2_list = NULL;
  20. /** True iff any member of networkstatus_v2_list has changed since the last
  21. * time we called download_status_map_update_from_v2_networkstatus() */
  22. static int networkstatus_v2_list_has_changed = 0;
  23. /** Map from lowercase nickname to digest of named server, if any. */
  24. static strmap_t *named_server_map = NULL;
  25. /** Most recently received and validated v3 consensus network status. */
  26. static networkstatus_vote_t *current_consensus = NULL;
  27. /** A v3 consensus networkstatus that we've received, but which we don't
  28. * have enough certificates to be happy about. */
  29. static networkstatus_vote_t *consensus_waiting_for_certs = NULL;
  30. static char *consensus_waiting_for_certs_body = NULL;
  31. /** The last time we tried to download a networkstatus, or 0 for "never". We
  32. * use this to rate-limit download attempts for directory caches (including
  33. * mirrors). Clients don't use this now. */
  34. static time_t last_networkstatus_download_attempted = 0;
  35. /** A time before which we shouldn't try to replace the current consensus:
  36. * this will be at some point after the next consensus becomes valid, but
  37. * before the current consensus becomes invalid. */
  38. static time_t time_to_download_next_consensus = 0;
  39. /** Download status for the current consensus networkstatus. */
  40. static download_status_t consensus_dl_status = { 0, 0};
  41. /** True iff we have logged a warning about this OR not being valid or
  42. * not being named. */
  43. static int have_warned_about_invalid_status = 0;
  44. /** True iff we have logged a warning about this OR's version being older than
  45. * listed by the authorities */
  46. static int have_warned_about_old_version = 0;
  47. /** True iff we have logged a warning about this OR's version being newer than
  48. * listed by the authorities */
  49. static int have_warned_about_new_version = 0;
  50. static void download_status_map_update_from_v2_networkstatus(void);
  51. static void routerstatus_list_update_named_server_map(void);
  52. /** Forget that we've warned about anything networkstatus-related, so we will
  53. * give fresh warnings if the same behavior happens again. */
  54. void
  55. networkstatus_reset_warnings(void)
  56. {
  57. if (current_consensus) {
  58. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  59. routerstatus_t *, rs,
  60. rs->name_lookup_warned = 0);
  61. }
  62. have_warned_about_invalid_status = 0;
  63. have_warned_about_old_version = 0;
  64. have_warned_about_new_version = 0;
  65. }
  66. /** Reset the descriptor download failure count on all networkstatus docs, so
  67. * that we can retry any long-failed documents immediately.
  68. */
  69. void
  70. networkstatus_reset_download_failures(void)
  71. {
  72. const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
  73. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  74. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  75. {
  76. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  77. rs->need_to_mirror = 1;
  78. }));;
  79. download_status_reset(&consensus_dl_status);
  80. if (v2_download_status_map) {
  81. digestmap_iter_t *iter;
  82. digestmap_t *map = v2_download_status_map;
  83. const char *key;
  84. void *val;
  85. download_status_t *dls;
  86. for (iter = digestmap_iter_init(map); !digestmap_iter_done(iter);
  87. iter = digestmap_iter_next(map, iter) ) {
  88. digestmap_iter_get(iter, &key, &val);
  89. dls = val;
  90. download_status_reset(dls);
  91. }
  92. }
  93. }
  94. /** Repopulate our list of network_status_t objects from the list cached on
  95. * disk. Return 0 on success, -1 on failure. */
  96. int
  97. router_reload_v2_networkstatus(void)
  98. {
  99. smartlist_t *entries;
  100. struct stat st;
  101. char *s;
  102. char *filename = get_datadir_fname("cached-status");
  103. if (!networkstatus_v2_list)
  104. networkstatus_v2_list = smartlist_create();
  105. entries = tor_listdir(filename);
  106. tor_free(filename);
  107. SMARTLIST_FOREACH(entries, const char *, fn, {
  108. char buf[DIGEST_LEN];
  109. if (strlen(fn) != HEX_DIGEST_LEN ||
  110. base16_decode(buf, sizeof(buf), fn, strlen(fn))) {
  111. log_info(LD_DIR,
  112. "Skipping cached-status file with unexpected name \"%s\"",fn);
  113. continue;
  114. }
  115. filename = get_datadir_fname2("cached-status", fn);
  116. s = read_file_to_str(filename, 0, &st);
  117. if (s) {
  118. if (router_set_networkstatus_v2(s, st.st_mtime, NS_FROM_CACHE,
  119. NULL)<0) {
  120. log_warn(LD_FS, "Couldn't load networkstatus from \"%s\"",filename);
  121. }
  122. tor_free(s);
  123. }
  124. tor_free(filename);
  125. });
  126. SMARTLIST_FOREACH(entries, char *, fn, tor_free(fn));
  127. smartlist_free(entries);
  128. networkstatus_v2_list_clean(time(NULL));
  129. routers_update_all_from_networkstatus(time(NULL));
  130. return 0;
  131. }
  132. /** Read the cached v3 consensus networkstatus from the disk. */
  133. int
  134. router_reload_consensus_networkstatus(void)
  135. {
  136. char *filename;
  137. char *s;
  138. /* XXXX020 Suppress warnings if cached consensus is bad. */
  139. filename = get_datadir_fname("cached-consensus");
  140. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  141. if (s) {
  142. if (networkstatus_set_current_consensus(s, 1, 0)) {
  143. log_warn(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  144. filename);
  145. }
  146. tor_free(s);
  147. }
  148. tor_free(filename);
  149. filename = get_datadir_fname("unverified-consensus");
  150. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  151. if (s) {
  152. if (networkstatus_set_current_consensus(s, 1, 1)) {
  153. log_warn(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  154. filename);
  155. }
  156. tor_free(s);
  157. }
  158. tor_free(filename);
  159. return 0;
  160. }
  161. /** Free all storage held by the routerstatus object <b>rs</b>. */
  162. void
  163. routerstatus_free(routerstatus_t *rs)
  164. {
  165. tor_free(rs);
  166. }
  167. /** Free all storage held by the networkstatus object <b>ns</b>. */
  168. void
  169. networkstatus_v2_free(networkstatus_v2_t *ns)
  170. {
  171. tor_free(ns->source_address);
  172. tor_free(ns->contact);
  173. if (ns->signing_key)
  174. crypto_free_pk_env(ns->signing_key);
  175. tor_free(ns->client_versions);
  176. tor_free(ns->server_versions);
  177. if (ns->entries) {
  178. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  179. routerstatus_free(rs));
  180. smartlist_free(ns->entries);
  181. }
  182. tor_free(ns);
  183. }
  184. /** Helper: return a newly allocated string containing the name of the filename
  185. * where we plan to cache the network status with the given identity digest. */
  186. char *
  187. networkstatus_get_cache_filename(const char *identity_digest)
  188. {
  189. char fp[HEX_DIGEST_LEN+1];
  190. base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
  191. return get_datadir_fname2("cached-status", fp);
  192. }
  193. /** Helper for smartlist_sort: Compare two networkstatus objects by
  194. * publication date. */
  195. static int
  196. _compare_networkstatus_v2_published_on(const void **_a, const void **_b)
  197. {
  198. const networkstatus_v2_t *a = *_a, *b = *_b;
  199. if (a->published_on < b->published_on)
  200. return -1;
  201. else if (a->published_on > b->published_on)
  202. return 1;
  203. else
  204. return 0;
  205. }
  206. /** Add the parsed neworkstatus in <b>ns</b> (with original document in
  207. * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
  208. * appropriate. */
  209. static int
  210. add_networkstatus_to_cache(const char *s,
  211. networkstatus_source_t source,
  212. networkstatus_v2_t *ns)
  213. {
  214. if (source != NS_FROM_CACHE) {
  215. char *fn = networkstatus_get_cache_filename(ns->identity_digest);
  216. if (write_str_to_file(fn, s, 0)<0) {
  217. log_notice(LD_FS, "Couldn't write cached network status to \"%s\"", fn);
  218. }
  219. tor_free(fn);
  220. }
  221. if (dirserver_mode(get_options()))
  222. dirserv_set_cached_networkstatus_v2(s,
  223. ns->identity_digest,
  224. ns->published_on);
  225. return 0;
  226. }
  227. /** How far in the future do we allow a network-status to get before removing
  228. * it? (seconds) */
  229. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  230. /** Given a string <b>s</b> containing a network status that we received at
  231. * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
  232. * store it, and put it into our cache as necessary.
  233. *
  234. * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
  235. * own networkstatus_t (if we're an authoritative directory server).
  236. *
  237. * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
  238. * cache.
  239. *
  240. * If <b>requested_fingerprints</b> is provided, it must contain a list of
  241. * uppercased identity fingerprints. Do not update any networkstatus whose
  242. * fingerprint is not on the list; after updating a networkstatus, remove its
  243. * fingerprint from the list.
  244. *
  245. * Return 0 on success, -1 on failure.
  246. *
  247. * Callers should make sure that routers_update_all_from_networkstatus() is
  248. * invoked after this function succeeds.
  249. */
  250. int
  251. router_set_networkstatus_v2(const char *s, time_t arrived_at,
  252. networkstatus_source_t source, smartlist_t *requested_fingerprints)
  253. {
  254. networkstatus_v2_t *ns;
  255. int i, found;
  256. time_t now;
  257. int skewed = 0;
  258. trusted_dir_server_t *trusted_dir = NULL;
  259. const char *source_desc = NULL;
  260. char fp[HEX_DIGEST_LEN+1];
  261. char published[ISO_TIME_LEN+1];
  262. if (!dirserver_mode(get_options()))
  263. return 0; /* Don't bother storing it. */
  264. ns = networkstatus_v2_parse_from_string(s);
  265. if (!ns) {
  266. log_warn(LD_DIR, "Couldn't parse network status.");
  267. return -1;
  268. }
  269. base16_encode(fp, HEX_DIGEST_LEN+1, ns->identity_digest, DIGEST_LEN);
  270. if (!(trusted_dir =
  271. router_get_trusteddirserver_by_digest(ns->identity_digest)) ||
  272. !(trusted_dir->type & V2_AUTHORITY)) {
  273. log_info(LD_DIR, "Network status was signed, but not by an authoritative "
  274. "directory we recognize.");
  275. if (!dirserver_mode(get_options())) {
  276. networkstatus_v2_free(ns);
  277. return 0;
  278. }
  279. source_desc = fp;
  280. } else {
  281. source_desc = trusted_dir->description;
  282. }
  283. now = time(NULL);
  284. if (arrived_at > now)
  285. arrived_at = now;
  286. ns->received_on = arrived_at;
  287. format_iso_time(published, ns->published_on);
  288. if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
  289. log_warn(LD_GENERAL, "Network status from %s was published in the future "
  290. "(%s GMT). Check your system clock! "
  291. "Not caching.",
  292. source_desc, published);
  293. control_event_general_status(LOG_WARN,
  294. "CLOCK_SKEW SOURCE=NETWORKSTATUS:%s:%d",
  295. ns->source_address, ns->source_dirport);
  296. skewed = 1;
  297. }
  298. if (!networkstatus_v2_list)
  299. networkstatus_v2_list = smartlist_create();
  300. if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
  301. router_digest_is_me(ns->identity_digest)) {
  302. /* Don't replace our own networkstatus when we get it from somebody else.*/
  303. networkstatus_v2_free(ns);
  304. return 0;
  305. }
  306. if (requested_fingerprints) {
  307. if (smartlist_string_isin(requested_fingerprints, fp)) {
  308. smartlist_string_remove(requested_fingerprints, fp);
  309. } else {
  310. if (source != NS_FROM_DIR_ALL) {
  311. char *requested =
  312. smartlist_join_strings(requested_fingerprints," ",0,NULL);
  313. log_warn(LD_DIR,
  314. "We received a network status with a fingerprint (%s) that we "
  315. "never requested. (We asked for: %s.) Dropping.",
  316. fp, requested);
  317. tor_free(requested);
  318. return 0;
  319. }
  320. }
  321. }
  322. if (!trusted_dir) {
  323. if (!skewed && dirserver_mode(get_options())) {
  324. /* We got a non-trusted networkstatus, and we're a directory cache.
  325. * This means that we asked an authority, and it told us about another
  326. * authority we didn't recognize. */
  327. log_info(LD_DIR,
  328. "We do not recognize authority (%s) but we are willing "
  329. "to cache it.", fp);
  330. add_networkstatus_to_cache(s, source, ns);
  331. networkstatus_v2_free(ns);
  332. }
  333. return 0;
  334. }
  335. found = 0;
  336. for (i=0; i < smartlist_len(networkstatus_v2_list); ++i) {
  337. networkstatus_v2_t *old_ns = smartlist_get(networkstatus_v2_list, i);
  338. if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
  339. if (!memcmp(old_ns->networkstatus_digest,
  340. ns->networkstatus_digest, DIGEST_LEN)) {
  341. /* Same one we had before. */
  342. networkstatus_v2_free(ns);
  343. tor_assert(trusted_dir);
  344. log_info(LD_DIR,
  345. "Not replacing network-status from %s (published %s); "
  346. "we already have it.",
  347. trusted_dir->description, published);
  348. if (old_ns->received_on < arrived_at) {
  349. if (source != NS_FROM_CACHE) {
  350. char *fn;
  351. fn = networkstatus_get_cache_filename(old_ns->identity_digest);
  352. /* We use mtime to tell when it arrived, so update that. */
  353. touch_file(fn);
  354. tor_free(fn);
  355. }
  356. old_ns->received_on = arrived_at;
  357. }
  358. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  359. return 0;
  360. } else if (old_ns->published_on >= ns->published_on) {
  361. char old_published[ISO_TIME_LEN+1];
  362. format_iso_time(old_published, old_ns->published_on);
  363. tor_assert(trusted_dir);
  364. log_info(LD_DIR,
  365. "Not replacing network-status from %s (published %s);"
  366. " we have a newer one (published %s) for this authority.",
  367. trusted_dir->description, published,
  368. old_published);
  369. networkstatus_v2_free(ns);
  370. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  371. return 0;
  372. } else {
  373. networkstatus_v2_free(old_ns);
  374. smartlist_set(networkstatus_v2_list, i, ns);
  375. found = 1;
  376. break;
  377. }
  378. }
  379. }
  380. if (source != NS_FROM_CACHE && trusted_dir) {
  381. download_status_reset(&trusted_dir->v2_ns_dl_status);
  382. }
  383. if (!found)
  384. smartlist_add(networkstatus_v2_list, ns);
  385. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  386. {
  387. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  388. rs->need_to_mirror = 1;
  389. });
  390. log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
  391. source == NS_FROM_CACHE?"cached from":
  392. ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
  393. "downloaded from":"generated for"),
  394. trusted_dir->description, published);
  395. networkstatus_v2_list_has_changed = 1;
  396. router_dir_info_changed();
  397. smartlist_sort(networkstatus_v2_list,
  398. _compare_networkstatus_v2_published_on);
  399. if (!skewed)
  400. add_networkstatus_to_cache(s, source, ns);
  401. return 0;
  402. }
  403. /** Remove all very-old network_status_t objects from memory and from the
  404. * disk cache. */
  405. void
  406. networkstatus_v2_list_clean(time_t now)
  407. {
  408. int i;
  409. if (!networkstatus_v2_list)
  410. return;
  411. for (i = 0; i < smartlist_len(networkstatus_v2_list); ++i) {
  412. networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
  413. char *fname = NULL;
  414. if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
  415. continue;
  416. /* Okay, this one is too old. Remove it from the list, and delete it
  417. * from the cache. */
  418. smartlist_del(networkstatus_v2_list, i--);
  419. fname = networkstatus_get_cache_filename(ns->identity_digest);
  420. if (file_status(fname) == FN_FILE) {
  421. log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
  422. unlink(fname);
  423. }
  424. tor_free(fname);
  425. if (dirserver_mode(get_options())) {
  426. dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
  427. }
  428. networkstatus_v2_free(ns);
  429. router_dir_info_changed();
  430. }
  431. /* And now go through the directory cache for any cached untrusted
  432. * networkstatuses and other network info. */
  433. dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
  434. dirserv_clear_old_v1_info(now);
  435. }
  436. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  437. * digest in the key to the identity digest of a routerstatus_t. */
  438. static int
  439. _compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  440. {
  441. const char *key = _key;
  442. const routerstatus_t *rs = *_member;
  443. return memcmp(key, rs->identity_digest, DIGEST_LEN);
  444. }
  445. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  446. * NULL if none was found. */
  447. routerstatus_t *
  448. networkstatus_v2_find_entry(networkstatus_v2_t *ns, const char *digest)
  449. {
  450. return smartlist_bsearch(ns->entries, digest,
  451. _compare_digest_to_routerstatus_entry);
  452. }
  453. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  454. * NULL if none was found. */
  455. routerstatus_t *
  456. networkstatus_vote_find_entry(networkstatus_vote_t *ns, const char *digest)
  457. {
  458. return smartlist_bsearch(ns->routerstatus_list, digest,
  459. _compare_digest_to_routerstatus_entry);
  460. }
  461. /** Return a list of the v2 networkstatus documents. */
  462. const smartlist_t *
  463. networkstatus_get_v2_list(void)
  464. {
  465. if (!networkstatus_v2_list)
  466. networkstatus_v2_list = smartlist_create();
  467. return networkstatus_v2_list;
  468. }
  469. /** Return the consensus view of the status of the router whose current
  470. * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
  471. * known. */
  472. routerstatus_t *
  473. router_get_consensus_status_by_descriptor_digest(const char *digest)
  474. {
  475. if (!current_consensus) return NULL;
  476. if (!current_consensus->desc_digest_map) {
  477. digestmap_t * m = current_consensus->desc_digest_map = digestmap_new();
  478. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  479. routerstatus_t *, rs,
  480. {
  481. digestmap_set(m, rs->descriptor_digest, rs);
  482. });
  483. }
  484. return digestmap_get(current_consensus->desc_digest_map, digest);
  485. }
  486. /** Given the digest of a router descriptor, return its current download
  487. * status, or NULL if the digest is unrecognized. */
  488. download_status_t *
  489. router_get_dl_status_by_descriptor_digest(const char *d)
  490. {
  491. routerstatus_t *rs;
  492. if ((rs = router_get_consensus_status_by_descriptor_digest(d)))
  493. return &rs->dl_status;
  494. if (v2_download_status_map)
  495. return digestmap_get(v2_download_status_map, d);
  496. return NULL;
  497. }
  498. /** Return the consensus view of the status of the router whose identity
  499. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  500. routerstatus_t *
  501. router_get_consensus_status_by_id(const char *digest)
  502. {
  503. if (!current_consensus)
  504. return NULL;
  505. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  506. _compare_digest_to_routerstatus_entry);
  507. }
  508. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  509. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  510. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  511. * nickname, but the Named flag isn't set for that router. */
  512. routerstatus_t *
  513. router_get_consensus_status_by_nickname(const char *nickname,
  514. int warn_if_unnamed)
  515. {
  516. char digest[DIGEST_LEN];
  517. routerstatus_t *best=NULL;
  518. smartlist_t *matches=NULL;
  519. const char *named_id=NULL;
  520. if (!current_consensus || !nickname)
  521. return NULL;
  522. if (nickname[0] == '$') {
  523. if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))<0)
  524. return NULL;
  525. return networkstatus_vote_find_entry(current_consensus, digest);
  526. } else if (strlen(nickname) == HEX_DIGEST_LEN &&
  527. (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))==0)) {
  528. return networkstatus_vote_find_entry(current_consensus, digest);
  529. }
  530. if (named_server_map)
  531. named_id = strmap_get_lc(named_server_map, nickname);
  532. if (named_id)
  533. return networkstatus_vote_find_entry(current_consensus, named_id);
  534. /*XXXX020 is this behavior really what we want? */
  535. matches = smartlist_create();
  536. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  537. routerstatus_t *, lrs,
  538. {
  539. if (!strcasecmp(lrs->nickname, nickname)) {
  540. if (lrs->is_named) {
  541. smartlist_free(matches);
  542. return lrs;
  543. } else {
  544. smartlist_add(matches, lrs);
  545. best = lrs;
  546. }
  547. }
  548. });
  549. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  550. int any_unwarned=0;
  551. SMARTLIST_FOREACH(matches, routerstatus_t *, lrs,
  552. {
  553. if (! lrs->name_lookup_warned) {
  554. lrs->name_lookup_warned=1;
  555. any_unwarned=1;
  556. }
  557. });
  558. if (any_unwarned) {
  559. log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
  560. " but none is listed as named by the directory authorites. "
  561. "Choosing one arbitrarily.", nickname);
  562. }
  563. } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
  564. char fp[HEX_DIGEST_LEN+1];
  565. base16_encode(fp, sizeof(fp),
  566. best->identity_digest, DIGEST_LEN);
  567. log_warn(LD_CONFIG,
  568. "When looking up a status, you specified a server \"%s\" by name, "
  569. "but the directory authorities do not have any key registered for "
  570. "this nickname -- so it could be used by any server, "
  571. "not just the one you meant. "
  572. "To make sure you get the same server in the future, refer to "
  573. "it by key, as \"$%s\".", nickname, fp);
  574. best->name_lookup_warned = 1;
  575. }
  576. smartlist_free(matches);
  577. return best;
  578. }
  579. /** Return the identity digest that's mapped to officially by
  580. * <b>nickname</b>. */
  581. const char *
  582. networkstatus_get_router_digest_by_nickname(const char *nickname)
  583. {
  584. if (!named_server_map)
  585. return NULL;
  586. return strmap_get_lc(named_server_map, nickname);
  587. }
  588. /** How frequently do directory authorities re-download fresh networkstatus
  589. * documents? */
  590. #define AUTHORITY_NS_CACHE_INTERVAL (5*60)
  591. /** How frequently do non-authority directory caches re-download fresh
  592. * networkstatus documents? */
  593. #define NONAUTHORITY_NS_CACHE_INTERVAL (15*60)
  594. /** We are a directory server, and so cache network_status documents.
  595. * Initiate downloads as needed to update them. For v2 authorities,
  596. * this means asking each trusted directory for its network-status.
  597. * For caches, this means asking a random v2 authority for all
  598. * network-statuses.
  599. */
  600. static void
  601. update_v2_networkstatus_cache_downloads(time_t now)
  602. {
  603. int authority = authdir_mode_v2(get_options());
  604. int interval =
  605. authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
  606. const smartlist_t *trusted_dir_servers = router_get_trusted_dir_servers();
  607. if (last_networkstatus_download_attempted + interval >= now)
  608. return;
  609. last_networkstatus_download_attempted = now;
  610. if (authority) {
  611. /* An authority launches a separate connection for everybody. */
  612. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
  613. {
  614. char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
  615. if (!(ds->type & V2_AUTHORITY))
  616. continue;
  617. if (router_digest_is_me(ds->digest))
  618. continue;
  619. if (connection_get_by_type_addr_port_purpose(
  620. CONN_TYPE_DIR, ds->addr, ds->dir_port,
  621. DIR_PURPOSE_FETCH_NETWORKSTATUS)) {
  622. /* XXX020 the above dir_port won't be accurate if we're
  623. * doing a tunneled conn. In that case it should be or_port.
  624. * How to guess from here? Maybe make the function less general
  625. * and have it know that it's looking for dir conns. -RD */
  626. /* We are already fetching this one. */
  627. continue;
  628. }
  629. strlcpy(resource, "fp/", sizeof(resource));
  630. base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
  631. strlcat(resource, ".z", sizeof(resource));
  632. directory_initiate_command_routerstatus(
  633. &ds->fake_status, DIR_PURPOSE_FETCH_NETWORKSTATUS,
  634. ROUTER_PURPOSE_GENERAL,
  635. 0, /* Not private */
  636. resource,
  637. NULL, 0 /* No payload. */);
  638. });
  639. } else {
  640. /* A non-authority cache launches one connection to a random authority. */
  641. /* (Check whether we're currently fetching network-status objects.) */
  642. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  643. DIR_PURPOSE_FETCH_NETWORKSTATUS))
  644. directory_get_from_dirserver(DIR_PURPOSE_FETCH_NETWORKSTATUS,
  645. ROUTER_PURPOSE_GENERAL, "all.z",1);
  646. }
  647. }
  648. /** If we want to download a fresh consensus, launch a new download as
  649. * appropriate. */
  650. static void
  651. update_consensus_networkstatus_downloads(time_t now)
  652. {
  653. or_options_t *options = get_options();
  654. if (!networkstatus_get_live_consensus(now))
  655. time_to_download_next_consensus = now; /* No live consensus? Get one now!*/
  656. if (time_to_download_next_consensus > now)
  657. return; /* Wait until the current consensus is older. */
  658. if (authdir_mode_v3(options))
  659. return; /* Authorities never fetch a consensus */
  660. /*XXXX020 magic number 8.*/
  661. if (!download_status_is_ready(&consensus_dl_status, now, 8))
  662. return; /* We failed downloading a consensus too recently. */
  663. if (connection_get_by_type_purpose(CONN_TYPE_DIR,
  664. DIR_PURPOSE_FETCH_CONSENSUS))
  665. return; /* There's an in-progress download.*/
  666. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  667. ROUTER_PURPOSE_GENERAL, NULL, 1);
  668. }
  669. /** Called when an attempt to download a consensus fails: note that the
  670. * failure occurred, and possibly retry. */
  671. void
  672. networkstatus_consensus_download_failed(int status_code)
  673. {
  674. download_status_failed(&consensus_dl_status, status_code);
  675. /* Retry immediately, if appropriate. */
  676. update_consensus_networkstatus_downloads(time(NULL));
  677. }
  678. /** Update the time at which we'll consider replacing the current
  679. * consensus. */
  680. static void
  681. update_consensus_networkstatus_fetch_time(time_t now)
  682. {
  683. or_options_t *options = get_options();
  684. /* XXXX020 call this when DirPort switches on or off. NMNM */
  685. networkstatus_vote_t *c = networkstatus_get_live_consensus(now);
  686. if (c) {
  687. long dl_interval;
  688. long interval = c->fresh_until - c->valid_after;
  689. time_t start;
  690. if (dirserver_mode(options)) {
  691. start = c->fresh_until + 120; /*XXXX020 make this a macro. */
  692. dl_interval = interval/2;
  693. } else {
  694. start = c->fresh_until + (interval*3)/4;
  695. /* XXXX020 too much magic. */
  696. dl_interval = (c->valid_until - start) * 7 / 8;
  697. }
  698. if (dl_interval < 1)
  699. dl_interval = 1;
  700. /* We must not try to replace c while it's still the most valid: */
  701. tor_assert(c->fresh_until < start);
  702. /* We must download the next one before c is invalid: */
  703. tor_assert(start+dl_interval < c->valid_until);
  704. time_to_download_next_consensus = start + crypto_rand_int(dl_interval);
  705. {
  706. char tbuf1[ISO_TIME_LEN+1];
  707. char tbuf2[ISO_TIME_LEN+1];
  708. char tbuf3[ISO_TIME_LEN+1];
  709. format_local_iso_time(tbuf1, c->fresh_until);
  710. format_local_iso_time(tbuf2, c->valid_until);
  711. format_local_iso_time(tbuf3, time_to_download_next_consensus);
  712. log_info(LD_DIR, "Live consensus %s the most recent until %s and will "
  713. "expire at %s; fetching the next one at %s.",
  714. (c->fresh_until > now) ? "will be" : "was",
  715. tbuf1, tbuf2, tbuf3);
  716. }
  717. } else {
  718. time_to_download_next_consensus = now;
  719. log_info(LD_DIR, "No live consensus; we should fetch one immediately.");
  720. }
  721. }
  722. /** Return 1 if there's a reason we shouldn't try any directory
  723. * fetches yet (e.g. we demand bridges and none are yet known).
  724. * Else return 0. */
  725. int
  726. should_delay_dir_fetches(or_options_t *options)
  727. {
  728. if (options->UseBridges && !any_bridge_descriptors_known()) {
  729. log_info(LD_DIR, "delaying dir fetches");
  730. return 1;
  731. }
  732. return 0;
  733. }
  734. /** Launch requests for networkstatus documents and authority certificates as
  735. * appropriate. */
  736. void
  737. update_networkstatus_downloads(time_t now)
  738. {
  739. or_options_t *options = get_options();
  740. if (should_delay_dir_fetches(options))
  741. return;
  742. if (dirserver_mode(options))
  743. update_v2_networkstatus_cache_downloads(now);
  744. update_consensus_networkstatus_downloads(now);
  745. if (consensus_waiting_for_certs)
  746. authority_certs_fetch_missing(consensus_waiting_for_certs, now);
  747. else
  748. authority_certs_fetch_missing(current_consensus, now);
  749. }
  750. /** Return the network status with a given identity digest. */
  751. networkstatus_v2_t *
  752. networkstatus_v2_get_by_digest(const char *digest)
  753. {
  754. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  755. {
  756. if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
  757. return ns;
  758. });
  759. return NULL;
  760. }
  761. /** Return the most recent consensus that we have downloaded, or NULL if we
  762. * don't have one. */
  763. networkstatus_vote_t *
  764. networkstatus_get_latest_consensus(void)
  765. {
  766. return current_consensus;
  767. }
  768. /** Return the most recent consensus that we have downloaded, or NULL if it is
  769. * no longer live. */
  770. networkstatus_vote_t *
  771. networkstatus_get_live_consensus(time_t now)
  772. {
  773. if (current_consensus &&
  774. current_consensus->valid_after <= now &&
  775. now <= current_consensus->valid_until)
  776. return current_consensus;
  777. else
  778. return NULL;
  779. }
  780. /** Copy all the ancillary information (like router download status and so on)
  781. * from <b>old_c</b> to <b>new_c</b>. */
  782. static void
  783. networkstatus_copy_old_consensus_info(networkstatus_vote_t *new_c,
  784. const networkstatus_vote_t *old_c)
  785. {
  786. int idx = 0;
  787. const routerstatus_t *rs_old;
  788. if (old_c == new_c)
  789. return;
  790. if (!smartlist_len(old_c->routerstatus_list))
  791. return;
  792. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  793. SMARTLIST_FOREACH(new_c->routerstatus_list, routerstatus_t *, rs_new,
  794. {
  795. int r;
  796. while ((r = memcmp(rs_old->identity_digest, rs_new->identity_digest,
  797. DIGEST_LEN))<0) {
  798. if (++idx == smartlist_len(old_c->routerstatus_list))
  799. goto done;
  800. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  801. }
  802. if (r>0)
  803. continue;
  804. tor_assert(r==0);
  805. /* Okay, so we're looking at the same identity. */
  806. rs_new->name_lookup_warned = rs_old->name_lookup_warned;
  807. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  808. if (!memcmp(rs_old->descriptor_digest, rs_new->descriptor_digest,
  809. DIGEST_LEN)) {
  810. /* And the same descriptor too! */
  811. rs_new->need_to_mirror = rs_old->need_to_mirror; /*XXXX020 NM ????? */
  812. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  813. }
  814. });
  815. done:
  816. return;
  817. }
  818. /** Try to replace the current cached v3 networkstatus with the one in
  819. * <b>consensus</b>. If we don't have enough certificates to validate it,
  820. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  821. *
  822. * Return 0 on success, -1 on failure. */
  823. int
  824. networkstatus_set_current_consensus(const char *consensus, int from_cache,
  825. int was_waiting_for_certs)
  826. {
  827. networkstatus_vote_t *c;
  828. int r, result=-1;
  829. time_t now = time(NULL);
  830. char *unverified_fname = NULL, *consensus_fname = NULL;
  831. /* Make sure it's parseable. */
  832. c = networkstatus_parse_vote_from_string(consensus, NULL, 0);
  833. if (!c) {
  834. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  835. goto done;
  836. }
  837. consensus_fname = get_datadir_fname("cached-consensus");
  838. unverified_fname = get_datadir_fname("unverified-consensus");
  839. /* Make sure it's signed enough. */
  840. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  841. if (r == -1 && !was_waiting_for_certs) {
  842. /* Okay, so it _might_ be signed enough if we get more certificates. */
  843. if (!was_waiting_for_certs)
  844. log_notice(LD_DIR, "Not enough certificates to check networkstatus "
  845. "consensus");
  846. if (!current_consensus ||
  847. c->valid_after > current_consensus->valid_after) {
  848. if (consensus_waiting_for_certs)
  849. networkstatus_vote_free(consensus_waiting_for_certs);
  850. tor_free(consensus_waiting_for_certs_body);
  851. consensus_waiting_for_certs = c;
  852. consensus_waiting_for_certs_body = tor_strdup(consensus);
  853. /*XXXX020 delay next update. NMNM */
  854. if (!from_cache) {
  855. write_str_to_file(unverified_fname, consensus, 0);
  856. }
  857. authority_certs_fetch_missing(c, now);
  858. } else {
  859. /* Even if we had enough signatures, we'd never use this as the
  860. * latest consensus. */
  861. if (was_waiting_for_certs && from_cache)
  862. unlink(unverified_fname);
  863. }
  864. download_status_reset(&consensus_dl_status); /*XXXX020 not quite right.*/
  865. result = 0;
  866. goto done;
  867. } else {
  868. /* This can never be signed enough Kill it. */
  869. if (!was_waiting_for_certs)
  870. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  871. "consensus");
  872. if (was_waiting_for_certs && from_cache)
  873. unlink(unverified_fname);
  874. networkstatus_vote_free(c);
  875. goto done;
  876. }
  877. }
  878. download_status_reset(&consensus_dl_status); /*XXXX020 not quite right.*/
  879. /* Are we missing any certificates at all? */
  880. if (r != 1)
  881. authority_certs_fetch_missing(c, now);
  882. if (current_consensus) {
  883. networkstatus_copy_old_consensus_info(c, current_consensus);
  884. networkstatus_vote_free(current_consensus);
  885. }
  886. if (consensus_waiting_for_certs &&
  887. consensus_waiting_for_certs->valid_after <= c->valid_after) {
  888. networkstatus_vote_free(consensus_waiting_for_certs);
  889. consensus_waiting_for_certs = NULL;
  890. if (consensus != consensus_waiting_for_certs_body)
  891. tor_free(consensus_waiting_for_certs_body);
  892. unlink(unverified_fname);
  893. }
  894. current_consensus = c;
  895. update_consensus_networkstatus_fetch_time(now);
  896. dirvote_recalculate_timing(get_options(), now);
  897. routerstatus_list_update_named_server_map();
  898. if (!from_cache) {
  899. write_str_to_file(consensus_fname, consensus, 0);
  900. }
  901. if (dirserver_mode(get_options()))
  902. dirserv_set_cached_networkstatus_v3(consensus, c->valid_after);
  903. router_dir_info_changed();
  904. result = 0;
  905. done:
  906. tor_free(consensus_fname);
  907. tor_free(unverified_fname);
  908. return result;
  909. }
  910. /** Called when we have gotten more certificates: see whether we can
  911. * now verify a pending consensus. */
  912. void
  913. networkstatus_note_certs_arrived(void)
  914. {
  915. if (consensus_waiting_for_certs) {
  916. if (networkstatus_check_consensus_signature(
  917. consensus_waiting_for_certs, 0)<0) {
  918. if (!networkstatus_set_current_consensus(
  919. consensus_waiting_for_certs_body, 0, 1)) {
  920. tor_free(consensus_waiting_for_certs_body);
  921. }
  922. }
  923. }
  924. }
  925. /** If the network-status list has changed since the last time we called this
  926. * function, update the status of every routerinfo from the network-status
  927. * list.
  928. */
  929. void
  930. routers_update_all_from_networkstatus(time_t now)
  931. {
  932. routerinfo_t *me;
  933. routerlist_t *rl = router_get_routerlist();
  934. networkstatus_vote_t *consensus = networkstatus_get_live_consensus(now);
  935. router_dir_info_changed(); /*XXXX020 really? */
  936. if (networkstatus_v2_list_has_changed)
  937. download_status_map_update_from_v2_networkstatus();
  938. if (!consensus)
  939. return;
  940. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  941. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  942. ri->routerlist_index = ri_sl_idx);
  943. entry_guards_compute_status();
  944. me = router_get_my_routerinfo();
  945. if (me && !have_warned_about_invalid_status) {
  946. routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
  947. me->cache_info.identity_digest);
  948. if (!rs) {
  949. log_info(LD_GENERAL, "The latest consensus does not list us."
  950. "Are you misconfigured?");
  951. have_warned_about_invalid_status = 1;
  952. } else if (!rs->is_named) {
  953. /*XXXX020 this isn't a correct warning. */
  954. log_info(LD_GENERAL, "The directory authorities do not recognize "
  955. "your nickname. Please consider sending your "
  956. "nickname and identity fingerprint to the tor-ops.");
  957. have_warned_about_invalid_status = 1;
  958. }
  959. }
  960. if (!have_warned_about_old_version) {
  961. int is_server = server_mode(get_options());
  962. version_status_t status;
  963. const char *recommended = is_server ?
  964. consensus->server_versions : consensus->client_versions;
  965. status = tor_version_is_obsolete(VERSION, recommended);
  966. if (status == VS_RECOMMENDED) {
  967. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  968. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  969. if (!have_warned_about_new_version) {
  970. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  971. "recommended version%s, according to the directory "
  972. "authorities. Recommended versions are: %s",
  973. VERSION,
  974. status == VS_NEW_IN_SERIES ? " in its series" : "",
  975. recommended);
  976. have_warned_about_new_version = 1;
  977. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  978. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  979. VERSION, "NEW", recommended);
  980. }
  981. } else {
  982. log_warn(LD_GENERAL, "Please upgrade! "
  983. "This version of Tor (%s) is %s, according to the directory "
  984. "authorities. Recommended versions are: %s",
  985. VERSION,
  986. status == VS_OLD ? "obsolete" : "not recommended",
  987. recommended);
  988. have_warned_about_old_version = 1;
  989. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  990. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  991. VERSION, status == VS_OLD ? "OLD" : "UNRECOMMENDED",
  992. recommended);
  993. }
  994. }
  995. }
  996. /** Update v2_download_status_map to contain an entry for every router
  997. * descriptor listed in the v2 networkstatuses. */
  998. static void
  999. download_status_map_update_from_v2_networkstatus(void)
  1000. {
  1001. digestmap_t *dl_status;
  1002. if (!networkstatus_v2_list)
  1003. return;
  1004. if (!v2_download_status_map)
  1005. v2_download_status_map = digestmap_new();
  1006. dl_status = digestmap_new();
  1007. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1008. {
  1009. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  1010. {
  1011. const char *d = rs->descriptor_digest;
  1012. download_status_t *s;
  1013. if (digestmap_get(dl_status, d))
  1014. continue;
  1015. if (!(s = digestmap_remove(v2_download_status_map, d))) {
  1016. s = tor_malloc_zero(sizeof(download_status_t));
  1017. }
  1018. digestmap_set(dl_status, d, s);
  1019. });
  1020. });
  1021. digestmap_free(v2_download_status_map, _tor_free);
  1022. v2_download_status_map = dl_status;
  1023. networkstatus_v2_list_has_changed = 0;
  1024. }
  1025. /** Update our view of the list of named servers from the most recently
  1026. * retrieved networkstatus consensus. */
  1027. static void
  1028. routerstatus_list_update_named_server_map(void)
  1029. {
  1030. if (!current_consensus)
  1031. return;
  1032. if (named_server_map)
  1033. strmap_free(named_server_map, _tor_free);
  1034. named_server_map = strmap_new();
  1035. SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
  1036. {
  1037. if (rs->is_named) {
  1038. strmap_set(named_server_map, rs->nickname,
  1039. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1040. }
  1041. });
  1042. }
  1043. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1044. * according to our current consensus networkstatus. May re-order
  1045. * <b>routers</b>. */
  1046. void
  1047. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1048. int reset_failures)
  1049. {
  1050. trusted_dir_server_t *ds;
  1051. routerstatus_t *rs;
  1052. or_options_t *options = get_options();
  1053. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  1054. int namingdir = authdir && options->NamingAuthoritativeDir;
  1055. networkstatus_vote_t *ns = current_consensus;
  1056. int idx;
  1057. if (!ns || !smartlist_len(ns->routerstatus_list))
  1058. return;
  1059. routers_sort_by_identity(routers);
  1060. /* Now routers and ns->routerstatus_list are both in ascending order
  1061. * of identity digest. */
  1062. idx = 0;
  1063. rs = smartlist_get(ns->routerstatus_list, idx);
  1064. SMARTLIST_FOREACH(routers, routerinfo_t *, router,
  1065. {
  1066. const char *digest = router->cache_info.identity_digest;
  1067. int r;
  1068. while ((r = memcmp(rs->identity_digest, digest, DIGEST_LEN))<0) {
  1069. if (++idx == smartlist_len(ns->routerstatus_list)) {
  1070. /* We're out of routerstatuses. Bail. */
  1071. goto done;
  1072. }
  1073. rs = smartlist_get(ns->routerstatus_list, idx);
  1074. }
  1075. if (r>0) {
  1076. /* We have no routerstatus for this router. Clear flags and skip it. */
  1077. if (!namingdir)
  1078. router->is_named = 0;
  1079. if (!authdir) {
  1080. if (router->purpose == ROUTER_PURPOSE_GENERAL) {
  1081. router->is_valid = router->is_running =
  1082. router->is_fast = router->is_stable =
  1083. router->is_possible_guard = router->is_exit =
  1084. router->is_bad_exit = 0;
  1085. }
  1086. }
  1087. continue;
  1088. }
  1089. tor_assert(r==0);
  1090. ds = router_get_trusteddirserver_by_digest(digest);
  1091. if (!namingdir) {
  1092. if (rs->is_named && !strcasecmp(router->nickname, rs->nickname))
  1093. router->is_named = 1;
  1094. else
  1095. router->is_named = 0;
  1096. }
  1097. if (!authdir) {
  1098. /* If we're not an authdir, believe others. */
  1099. router->is_valid = rs->is_valid;
  1100. router->is_running = rs->is_running;
  1101. router->is_fast = rs->is_fast;
  1102. router->is_stable = rs->is_stable;
  1103. router->is_possible_guard = rs->is_possible_guard;
  1104. router->is_exit = rs->is_exit;
  1105. router->is_bad_exit = rs->is_bad_exit;
  1106. }
  1107. if (router->is_running && ds) {
  1108. download_status_reset(&ds->v2_ns_dl_status);
  1109. }
  1110. if (reset_failures) {
  1111. download_status_reset(&rs->dl_status);
  1112. }
  1113. });
  1114. done:
  1115. router_dir_info_changed();
  1116. }
  1117. /** Generate networkstatus lines for a single routerstatus_t object, and
  1118. * return the result in a newly allocated string. Used only by controller
  1119. * interface (for now.) */
  1120. char *
  1121. networkstatus_getinfo_helper_single(routerstatus_t *rs)
  1122. {
  1123. char buf[256];
  1124. routerstatus_format_entry(buf, sizeof(buf), rs, NULL, 0);
  1125. return tor_strdup(buf);
  1126. }
  1127. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1128. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1129. * newly-allocated string containing networkstatus lines for the appropriate
  1130. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1131. int
  1132. getinfo_helper_networkstatus(control_connection_t *conn,
  1133. const char *question, char **answer)
  1134. {
  1135. routerstatus_t *status;
  1136. (void) conn;
  1137. if (!current_consensus) {
  1138. *answer = tor_strdup("");
  1139. return 0;
  1140. }
  1141. if (!strcmp(question, "ns/all")) {
  1142. smartlist_t *statuses = smartlist_create();
  1143. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1144. routerstatus_t *, rs,
  1145. {
  1146. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1147. });
  1148. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1149. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1150. smartlist_free(statuses);
  1151. return 0;
  1152. } else if (!strcmpstart(question, "ns/id/")) {
  1153. char d[DIGEST_LEN];
  1154. if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6)))
  1155. return -1;
  1156. status = router_get_consensus_status_by_id(d);
  1157. } else if (!strcmpstart(question, "ns/name/")) {
  1158. status = router_get_consensus_status_by_nickname(question+8, 0);
  1159. } else {
  1160. return -1;
  1161. }
  1162. if (status) {
  1163. *answer = networkstatus_getinfo_helper_single(status);
  1164. }
  1165. return 0;
  1166. }
  1167. /** Free all storage held locally in this module. */
  1168. void
  1169. networkstatus_free_all(void)
  1170. {
  1171. if (networkstatus_v2_list) {
  1172. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1173. networkstatus_v2_free(ns));
  1174. smartlist_free(networkstatus_v2_list);
  1175. networkstatus_v2_list = NULL;
  1176. }
  1177. if (current_consensus) {
  1178. networkstatus_vote_free(current_consensus);
  1179. current_consensus = NULL;
  1180. }
  1181. if (consensus_waiting_for_certs) {
  1182. networkstatus_vote_free(current_consensus);
  1183. current_consensus = NULL;
  1184. }
  1185. tor_free(consensus_waiting_for_certs_body);
  1186. if (named_server_map) {
  1187. strmap_free(named_server_map, _tor_free);
  1188. }
  1189. }