networkstatus.c 42 KB

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