networkstatus.c 46 KB

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