networkstatus.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. const char networkstatus_c_id[] =
  8. "$Id$";
  9. /**
  10. * \file Functions and structures for handling network status documents as a
  11. * client or cache.
  12. */
  13. #include "or.h"
  14. /* For tracking v2 networkstatus documents. Only caches do this now. */
  15. /** Map from descriptor digest of routers listed in the v2 networkstatus
  16. * documents to download_status_t* */
  17. static digestmap_t *v2_download_status_map = NULL;
  18. /** Global list of all of the current v2 network_status documents that we know
  19. * about. This list is kept sorted by published_on. */
  20. static smartlist_t *networkstatus_v2_list = NULL;
  21. /** True iff any member of networkstatus_v2_list has changed since the last
  22. * time we called download_status_map_update_from_v2_networkstatus() */
  23. static int networkstatus_v2_list_has_changed = 0;
  24. /** Map from lowercase nickname to identity digest of named server, if any. */
  25. static strmap_t *named_server_map = NULL;
  26. /** Map from lowercase nickname to (void*)1 for all names that are listed
  27. * as unnamed for some server in the consensus. */
  28. static strmap_t *unnamed_server_map = NULL;
  29. /** Most recently received and validated v3 consensus network status. */
  30. static networkstatus_vote_t *current_consensus = NULL;
  31. /** A v3 consensus networkstatus that we've received, but which we don't
  32. * have enough certificates to be happy about. */
  33. static networkstatus_vote_t *consensus_waiting_for_certs = NULL;
  34. static char *consensus_waiting_for_certs_body = NULL;
  35. static time_t consensus_waiting_for_certs_set_at = 0;
  36. static int consensus_waiting_for_certs_dl_failed = 0;
  37. /** The last time we tried to download a networkstatus, or 0 for "never". We
  38. * use this to rate-limit download attempts for directory caches (including
  39. * mirrors). Clients don't use this now. */
  40. static time_t last_networkstatus_download_attempted = 0;
  41. /** A time before which we shouldn't try to replace the current consensus:
  42. * this will be at some point after the next consensus becomes valid, but
  43. * before the current consensus becomes invalid. */
  44. static time_t time_to_download_next_consensus = 0;
  45. /** Download status for the current consensus networkstatus. */
  46. static download_status_t consensus_dl_status = { 0, 0, DL_SCHED_CONSENSUS };
  47. /** True iff we have logged a warning about this OR not being valid or
  48. * not being named. */
  49. static int have_warned_about_invalid_status = 0;
  50. /** True iff we have logged a warning about this OR's version being older than
  51. * listed by the authorities. */
  52. static int have_warned_about_old_version = 0;
  53. /** True iff we have logged a warning about this OR's version being newer than
  54. * listed by the authorities. */
  55. static int have_warned_about_new_version = 0;
  56. static void download_status_map_update_from_v2_networkstatus(void);
  57. static void routerstatus_list_update_named_server_map(void);
  58. /** Forget that we've warned about anything networkstatus-related, so we will
  59. * give fresh warnings if the same behavior happens again. */
  60. void
  61. networkstatus_reset_warnings(void)
  62. {
  63. if (current_consensus) {
  64. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  65. routerstatus_t *, rs,
  66. rs->name_lookup_warned = 0);
  67. }
  68. have_warned_about_invalid_status = 0;
  69. have_warned_about_old_version = 0;
  70. have_warned_about_new_version = 0;
  71. }
  72. /** Reset the descriptor download failure count on all networkstatus docs, so
  73. * that we can retry any long-failed documents immediately.
  74. */
  75. void
  76. networkstatus_reset_download_failures(void)
  77. {
  78. const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
  79. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  80. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  81. {
  82. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  83. rs->need_to_mirror = 1;
  84. }));;
  85. download_status_reset(&consensus_dl_status);
  86. if (v2_download_status_map) {
  87. digestmap_iter_t *iter;
  88. digestmap_t *map = v2_download_status_map;
  89. const char *key;
  90. void *val;
  91. download_status_t *dls;
  92. for (iter = digestmap_iter_init(map); !digestmap_iter_done(iter);
  93. iter = digestmap_iter_next(map, iter) ) {
  94. digestmap_iter_get(iter, &key, &val);
  95. dls = val;
  96. download_status_reset(dls);
  97. }
  98. }
  99. }
  100. /** Repopulate our list of network_status_t objects from the list cached on
  101. * disk. Return 0 on success, -1 on failure. */
  102. int
  103. router_reload_v2_networkstatus(void)
  104. {
  105. smartlist_t *entries;
  106. struct stat st;
  107. char *s;
  108. char *filename = get_datadir_fname("cached-status");
  109. if (!networkstatus_v2_list)
  110. networkstatus_v2_list = smartlist_create();
  111. entries = tor_listdir(filename);
  112. tor_free(filename);
  113. SMARTLIST_FOREACH(entries, const char *, fn, {
  114. char buf[DIGEST_LEN];
  115. if (strlen(fn) != HEX_DIGEST_LEN ||
  116. base16_decode(buf, sizeof(buf), fn, strlen(fn))) {
  117. log_info(LD_DIR,
  118. "Skipping cached-status file with unexpected name \"%s\"",fn);
  119. continue;
  120. }
  121. filename = get_datadir_fname2("cached-status", fn);
  122. s = read_file_to_str(filename, 0, &st);
  123. if (s) {
  124. if (router_set_networkstatus_v2(s, st.st_mtime, NS_FROM_CACHE,
  125. NULL)<0) {
  126. log_warn(LD_FS, "Couldn't load networkstatus from \"%s\"",filename);
  127. }
  128. tor_free(s);
  129. }
  130. tor_free(filename);
  131. });
  132. SMARTLIST_FOREACH(entries, char *, fn, tor_free(fn));
  133. smartlist_free(entries);
  134. networkstatus_v2_list_clean(time(NULL));
  135. routers_update_all_from_networkstatus(time(NULL), 2);
  136. return 0;
  137. }
  138. /** Read the cached v3 consensus networkstatus from the disk. */
  139. int
  140. router_reload_consensus_networkstatus(void)
  141. {
  142. char *filename;
  143. char *s;
  144. struct stat st;
  145. or_options_t *options = get_options();
  146. /* XXXX020 Suppress warnings if cached consensus is bad. */
  147. filename = get_datadir_fname("cached-consensus");
  148. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  149. if (s) {
  150. if (networkstatus_set_current_consensus(s, 1, 0)) {
  151. log_warn(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  152. filename);
  153. }
  154. tor_free(s);
  155. }
  156. tor_free(filename);
  157. filename = get_datadir_fname("unverified-consensus");
  158. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  159. if (s) {
  160. if (networkstatus_set_current_consensus(s, 1, 1)) {
  161. log_info(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  162. filename);
  163. }
  164. tor_free(s);
  165. }
  166. tor_free(filename);
  167. if (!current_consensus ||
  168. (stat(options->FallbackNetworkstatusFile, &st)==0 &&
  169. st.st_mtime > current_consensus->valid_after)) {
  170. s = read_file_to_str(options->FallbackNetworkstatusFile,
  171. RFTS_IGNORE_MISSING, NULL);
  172. if (s) {
  173. if (networkstatus_set_current_consensus(s, 1, 1)) {
  174. log_info(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
  175. options->FallbackNetworkstatusFile);
  176. } else {
  177. log_notice(LD_FS, "Loaded fallback consensus networkstaus from \"%s\"",
  178. options->FallbackNetworkstatusFile);
  179. }
  180. tor_free(s);
  181. }
  182. }
  183. routers_update_all_from_networkstatus(time(NULL), 3);
  184. return 0;
  185. }
  186. /** Free all storage held by the routerstatus object <b>rs</b>. */
  187. void
  188. routerstatus_free(routerstatus_t *rs)
  189. {
  190. tor_free(rs);
  191. }
  192. /** Free all storage held by the networkstatus object <b>ns</b>. */
  193. void
  194. networkstatus_v2_free(networkstatus_v2_t *ns)
  195. {
  196. tor_free(ns->source_address);
  197. tor_free(ns->contact);
  198. if (ns->signing_key)
  199. crypto_free_pk_env(ns->signing_key);
  200. tor_free(ns->client_versions);
  201. tor_free(ns->server_versions);
  202. if (ns->entries) {
  203. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  204. routerstatus_free(rs));
  205. smartlist_free(ns->entries);
  206. }
  207. tor_free(ns);
  208. }
  209. /** Clear all storage held in <b>ns</b>. */
  210. void
  211. networkstatus_vote_free(networkstatus_vote_t *ns)
  212. {
  213. if (!ns)
  214. return;
  215. tor_free(ns->client_versions);
  216. tor_free(ns->server_versions);
  217. if (ns->known_flags) {
  218. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  219. smartlist_free(ns->known_flags);
  220. }
  221. if (ns->voters) {
  222. SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
  223. {
  224. tor_free(voter->nickname);
  225. tor_free(voter->address);
  226. tor_free(voter->contact);
  227. tor_free(voter->signature);
  228. tor_free(voter);
  229. });
  230. smartlist_free(ns->voters);
  231. }
  232. if (ns->cert)
  233. authority_cert_free(ns->cert);
  234. if (ns->routerstatus_list) {
  235. if (ns->is_vote) {
  236. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  237. {
  238. tor_free(rs->version);
  239. tor_free(rs);
  240. });
  241. } else {
  242. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  243. tor_free(rs));
  244. }
  245. smartlist_free(ns->routerstatus_list);
  246. }
  247. if (ns->desc_digest_map)
  248. digestmap_free(ns->desc_digest_map, NULL);
  249. memset(ns, 11, sizeof(*ns));
  250. tor_free(ns);
  251. }
  252. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  253. * is <b>identity</b>, or NULL if no such voter is associated with
  254. * <b>vote</b>. */
  255. networkstatus_voter_info_t *
  256. networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
  257. const char *identity)
  258. {
  259. if (!vote || !vote->voters)
  260. return NULL;
  261. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  262. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  263. return voter);
  264. return NULL;
  265. }
  266. /** Check whether the signature on <b>voter</b> is correctly signed by
  267. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  268. * signing key; otherwise set the good_signature or bad_signature flag on
  269. * <b>voter</b>, and return 0. */
  270. /* (private; exposed for testing.) */
  271. int
  272. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  273. networkstatus_voter_info_t *voter,
  274. authority_cert_t *cert)
  275. {
  276. char d[DIGEST_LEN];
  277. char *signed_digest;
  278. size_t signed_digest_len;
  279. if (crypto_pk_get_digest(cert->signing_key, d)<0)
  280. return -1;
  281. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN))
  282. return -1;
  283. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  284. signed_digest = tor_malloc(signed_digest_len);
  285. if (crypto_pk_public_checksig(cert->signing_key,
  286. signed_digest,
  287. voter->signature,
  288. voter->signature_len) != DIGEST_LEN ||
  289. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  290. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  291. voter->bad_signature = 1;
  292. } else {
  293. voter->good_signature = 1;
  294. }
  295. tor_free(signed_digest);
  296. return 0;
  297. }
  298. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  299. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  300. * signature from every recognized authority on it, 0 if there are
  301. * enough good signatures from recognized authorities on it, -1 if we might
  302. * get enough good signatures by fetching missing certificates, and -2
  303. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  304. * about every problem; if warn is at least 1, warn only if we can't get
  305. * enough signatures; if warn is negative, log nothing at all. */
  306. int
  307. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus,
  308. int warn)
  309. {
  310. int n_good = 0;
  311. int n_missing_key = 0;
  312. int n_bad = 0;
  313. int n_unknown = 0;
  314. int n_no_signature = 0;
  315. int n_v3_authorities = get_n_authorities(V3_AUTHORITY);
  316. int n_required = n_v3_authorities/2 + 1;
  317. smartlist_t *need_certs_from = smartlist_create();
  318. smartlist_t *unrecognized = smartlist_create();
  319. smartlist_t *missing_authorities = smartlist_create();
  320. int severity;
  321. tor_assert(! consensus->is_vote);
  322. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  323. {
  324. if (!voter->good_signature && !voter->bad_signature && voter->signature) {
  325. /* we can try to check the signature. */
  326. authority_cert_t *cert =
  327. authority_cert_get_by_digests(voter->identity_digest,
  328. voter->signing_key_digest);
  329. if (! cert) {
  330. if (!trusteddirserver_get_by_v3_auth_digest(voter->identity_digest)) {
  331. smartlist_add(unrecognized, voter);
  332. ++n_unknown;
  333. } else {
  334. smartlist_add(need_certs_from, voter);
  335. ++n_missing_key;
  336. }
  337. continue;
  338. }
  339. if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
  340. smartlist_add(need_certs_from, voter);
  341. ++n_missing_key;
  342. continue;
  343. }
  344. }
  345. if (voter->good_signature)
  346. ++n_good;
  347. else if (voter->bad_signature)
  348. ++n_bad;
  349. else
  350. ++n_no_signature;
  351. });
  352. /* Now see whether we're missing any voters entirely. */
  353. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  354. trusted_dir_server_t *, ds,
  355. {
  356. if ((ds->type & V3_AUTHORITY) &&
  357. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  358. smartlist_add(missing_authorities, ds);
  359. });
  360. if (warn > 1 || (warn >= 0 && n_good < n_required))
  361. severity = LOG_WARN;
  362. else
  363. severity = LOG_INFO;
  364. if (warn >= 0) {
  365. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  366. {
  367. log(severity, LD_DIR, "Consensus includes unrecognized authority '%s' "
  368. "at %s:%d (contact %s; identity %s)",
  369. voter->nickname, voter->address, (int)voter->dir_port,
  370. voter->contact?voter->contact:"n/a",
  371. hex_str(voter->identity_digest, DIGEST_LEN));
  372. });
  373. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  374. {
  375. log_info(LD_DIR, "Looks like we need to download a new certificate "
  376. "from authority '%s' at %s:%d (contact %s; identity %s)",
  377. voter->nickname, voter->address, (int)voter->dir_port,
  378. voter->contact?voter->contact:"n/a",
  379. hex_str(voter->identity_digest, DIGEST_LEN));
  380. });
  381. SMARTLIST_FOREACH(missing_authorities, trusted_dir_server_t *, ds,
  382. {
  383. log(severity, LD_DIR, "Consensus does not include configured "
  384. "authority '%s' at %s:%d (identity %s)",
  385. ds->nickname, ds->address, (int)ds->dir_port,
  386. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  387. });
  388. log(severity, LD_DIR,
  389. "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
  390. "%d required", n_unknown, n_missing_key, n_good, n_bad,
  391. n_no_signature, n_required);
  392. }
  393. smartlist_free(unrecognized);
  394. smartlist_free(need_certs_from);
  395. smartlist_free(missing_authorities);
  396. if (n_good == n_v3_authorities)
  397. return 1;
  398. else if (n_good >= n_required)
  399. return 0;
  400. else if (n_good + n_missing_key >= n_required)
  401. return -1;
  402. else
  403. return -2;
  404. }
  405. /** Helper: return a newly allocated string containing the name of the filename
  406. * where we plan to cache the network status with the given identity digest. */
  407. char *
  408. networkstatus_get_cache_filename(const char *identity_digest)
  409. {
  410. char fp[HEX_DIGEST_LEN+1];
  411. base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
  412. return get_datadir_fname2("cached-status", fp);
  413. }
  414. /** Helper for smartlist_sort: Compare two networkstatus objects by
  415. * publication date. */
  416. static int
  417. _compare_networkstatus_v2_published_on(const void **_a, const void **_b)
  418. {
  419. const networkstatus_v2_t *a = *_a, *b = *_b;
  420. if (a->published_on < b->published_on)
  421. return -1;
  422. else if (a->published_on > b->published_on)
  423. return 1;
  424. else
  425. return 0;
  426. }
  427. /** Add the parsed neworkstatus in <b>ns</b> (with original document in
  428. * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
  429. * appropriate. */
  430. static int
  431. add_networkstatus_to_cache(const char *s,
  432. networkstatus_source_t source,
  433. networkstatus_v2_t *ns)
  434. {
  435. if (source != NS_FROM_CACHE) {
  436. char *fn = networkstatus_get_cache_filename(ns->identity_digest);
  437. if (write_str_to_file(fn, s, 0)<0) {
  438. log_notice(LD_FS, "Couldn't write cached network status to \"%s\"", fn);
  439. }
  440. tor_free(fn);
  441. }
  442. if (directory_caches_dir_info(get_options()))
  443. dirserv_set_cached_networkstatus_v2(s,
  444. ns->identity_digest,
  445. ns->published_on);
  446. return 0;
  447. }
  448. /** How far in the future do we allow a network-status to get before removing
  449. * it? (seconds) */
  450. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  451. /** Given a string <b>s</b> containing a network status that we received at
  452. * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
  453. * store it, and put it into our cache as necessary.
  454. *
  455. * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
  456. * own networkstatus_t (if we're an authoritative directory server).
  457. *
  458. * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
  459. * cache.
  460. *
  461. * If <b>requested_fingerprints</b> is provided, it must contain a list of
  462. * uppercased identity fingerprints. Do not update any networkstatus whose
  463. * fingerprint is not on the list; after updating a networkstatus, remove its
  464. * fingerprint from the list.
  465. *
  466. * Return 0 on success, -1 on failure.
  467. *
  468. * Callers should make sure that routers_update_all_from_networkstatus() is
  469. * invoked after this function succeeds.
  470. */
  471. int
  472. router_set_networkstatus_v2(const char *s, time_t arrived_at,
  473. networkstatus_source_t source, smartlist_t *requested_fingerprints)
  474. {
  475. networkstatus_v2_t *ns;
  476. int i, found;
  477. time_t now;
  478. int skewed = 0;
  479. trusted_dir_server_t *trusted_dir = NULL;
  480. const char *source_desc = NULL;
  481. char fp[HEX_DIGEST_LEN+1];
  482. char published[ISO_TIME_LEN+1];
  483. if (!directory_caches_dir_info(get_options()))
  484. return 0; /* Don't bother storing it. */
  485. ns = networkstatus_v2_parse_from_string(s);
  486. if (!ns) {
  487. log_warn(LD_DIR, "Couldn't parse network status.");
  488. return -1;
  489. }
  490. base16_encode(fp, HEX_DIGEST_LEN+1, ns->identity_digest, DIGEST_LEN);
  491. if (!(trusted_dir =
  492. router_get_trusteddirserver_by_digest(ns->identity_digest)) ||
  493. !(trusted_dir->type & V2_AUTHORITY)) {
  494. log_info(LD_DIR, "Network status was signed, but not by an authoritative "
  495. "directory we recognize.");
  496. source_desc = fp;
  497. } else {
  498. source_desc = trusted_dir->description;
  499. }
  500. now = time(NULL);
  501. if (arrived_at > now)
  502. arrived_at = now;
  503. ns->received_on = arrived_at;
  504. format_iso_time(published, ns->published_on);
  505. if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
  506. char dbuf[64];
  507. long delta = now - ns->published_on;
  508. format_time_interval(dbuf, sizeof(dbuf), delta);
  509. log_warn(LD_GENERAL, "Network status from %s was published %s in the "
  510. "future (%s GMT). Check your time and date settings! "
  511. "Not caching.",
  512. source_desc, dbuf, published);
  513. control_event_general_status(LOG_WARN,
  514. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
  515. delta, ns->source_address, ns->source_dirport);
  516. skewed = 1;
  517. }
  518. if (!networkstatus_v2_list)
  519. networkstatus_v2_list = smartlist_create();
  520. if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
  521. router_digest_is_me(ns->identity_digest)) {
  522. /* Don't replace our own networkstatus when we get it from somebody else.*/
  523. networkstatus_v2_free(ns);
  524. return 0;
  525. }
  526. if (requested_fingerprints) {
  527. if (smartlist_string_isin(requested_fingerprints, fp)) {
  528. smartlist_string_remove(requested_fingerprints, fp);
  529. } else {
  530. if (source != NS_FROM_DIR_ALL) {
  531. char *requested =
  532. smartlist_join_strings(requested_fingerprints," ",0,NULL);
  533. log_warn(LD_DIR,
  534. "We received a network status with a fingerprint (%s) that we "
  535. "never requested. (We asked for: %s.) Dropping.",
  536. fp, requested);
  537. tor_free(requested);
  538. return 0;
  539. }
  540. }
  541. }
  542. if (!trusted_dir) {
  543. if (!skewed) {
  544. /* We got a non-trusted networkstatus, and we're a directory cache.
  545. * This means that we asked an authority, and it told us about another
  546. * authority we didn't recognize. */
  547. log_info(LD_DIR,
  548. "We do not recognize authority (%s) but we are willing "
  549. "to cache it.", fp);
  550. add_networkstatus_to_cache(s, source, ns);
  551. networkstatus_v2_free(ns);
  552. }
  553. return 0;
  554. }
  555. found = 0;
  556. for (i=0; i < smartlist_len(networkstatus_v2_list); ++i) {
  557. networkstatus_v2_t *old_ns = smartlist_get(networkstatus_v2_list, i);
  558. if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
  559. if (!memcmp(old_ns->networkstatus_digest,
  560. ns->networkstatus_digest, DIGEST_LEN)) {
  561. /* Same one we had before. */
  562. networkstatus_v2_free(ns);
  563. tor_assert(trusted_dir);
  564. log_info(LD_DIR,
  565. "Not replacing network-status from %s (published %s); "
  566. "we already have it.",
  567. trusted_dir->description, published);
  568. if (old_ns->received_on < arrived_at) {
  569. if (source != NS_FROM_CACHE) {
  570. char *fn;
  571. fn = networkstatus_get_cache_filename(old_ns->identity_digest);
  572. /* We use mtime to tell when it arrived, so update that. */
  573. touch_file(fn);
  574. tor_free(fn);
  575. }
  576. old_ns->received_on = arrived_at;
  577. }
  578. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  579. return 0;
  580. } else if (old_ns->published_on >= ns->published_on) {
  581. char old_published[ISO_TIME_LEN+1];
  582. format_iso_time(old_published, old_ns->published_on);
  583. tor_assert(trusted_dir);
  584. log_info(LD_DIR,
  585. "Not replacing network-status from %s (published %s);"
  586. " we have a newer one (published %s) for this authority.",
  587. trusted_dir->description, published,
  588. old_published);
  589. networkstatus_v2_free(ns);
  590. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  591. return 0;
  592. } else {
  593. networkstatus_v2_free(old_ns);
  594. smartlist_set(networkstatus_v2_list, i, ns);
  595. found = 1;
  596. break;
  597. }
  598. }
  599. }
  600. if (source != NS_FROM_CACHE && trusted_dir) {
  601. download_status_reset(&trusted_dir->v2_ns_dl_status);
  602. }
  603. if (!found)
  604. smartlist_add(networkstatus_v2_list, ns);
  605. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  606. {
  607. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  608. rs->need_to_mirror = 1;
  609. });
  610. log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
  611. source == NS_FROM_CACHE?"cached from":
  612. ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
  613. "downloaded from":"generated for"),
  614. trusted_dir->description, published);
  615. networkstatus_v2_list_has_changed = 1;
  616. router_dir_info_changed();
  617. smartlist_sort(networkstatus_v2_list,
  618. _compare_networkstatus_v2_published_on);
  619. if (!skewed)
  620. add_networkstatus_to_cache(s, source, ns);
  621. return 0;
  622. }
  623. /** Remove all very-old network_status_t objects from memory and from the
  624. * disk cache. */
  625. void
  626. networkstatus_v2_list_clean(time_t now)
  627. {
  628. int i;
  629. if (!networkstatus_v2_list)
  630. return;
  631. for (i = 0; i < smartlist_len(networkstatus_v2_list); ++i) {
  632. networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
  633. char *fname = NULL;
  634. if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
  635. continue;
  636. /* Okay, this one is too old. Remove it from the list, and delete it
  637. * from the cache. */
  638. smartlist_del(networkstatus_v2_list, i--);
  639. fname = networkstatus_get_cache_filename(ns->identity_digest);
  640. if (file_status(fname) == FN_FILE) {
  641. log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
  642. unlink(fname);
  643. }
  644. tor_free(fname);
  645. if (directory_caches_dir_info(get_options())) {
  646. dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
  647. }
  648. networkstatus_v2_free(ns);
  649. router_dir_info_changed();
  650. }
  651. /* And now go through the directory cache for any cached untrusted
  652. * networkstatuses and other network info. */
  653. dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
  654. dirserv_clear_old_v1_info(now);
  655. }
  656. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  657. * digest in the key to the identity digest of a routerstatus_t. */
  658. static int
  659. _compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  660. {
  661. const char *key = _key;
  662. const routerstatus_t *rs = *_member;
  663. return memcmp(key, rs->identity_digest, DIGEST_LEN);
  664. }
  665. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  666. * NULL if none was found. */
  667. routerstatus_t *
  668. networkstatus_v2_find_entry(networkstatus_v2_t *ns, const char *digest)
  669. {
  670. return smartlist_bsearch(ns->entries, digest,
  671. _compare_digest_to_routerstatus_entry);
  672. }
  673. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  674. * NULL if none was found. */
  675. routerstatus_t *
  676. networkstatus_vote_find_entry(networkstatus_vote_t *ns, const char *digest)
  677. {
  678. return smartlist_bsearch(ns->routerstatus_list, digest,
  679. _compare_digest_to_routerstatus_entry);
  680. }
  681. /*XXXX020 make this static once functions are moved into this file. */
  682. /** DOCDOC */
  683. int
  684. networkstatus_vote_find_entry_idx(networkstatus_vote_t *ns,
  685. const char *digest, int *found_out)
  686. {
  687. return smartlist_bsearch_idx(ns->routerstatus_list, digest,
  688. _compare_digest_to_routerstatus_entry,
  689. found_out);
  690. }
  691. /** Return a list of the v2 networkstatus documents. */
  692. const smartlist_t *
  693. networkstatus_get_v2_list(void)
  694. {
  695. if (!networkstatus_v2_list)
  696. networkstatus_v2_list = smartlist_create();
  697. return networkstatus_v2_list;
  698. }
  699. /** Return the consensus view of the status of the router whose current
  700. * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
  701. * known. */
  702. routerstatus_t *
  703. router_get_consensus_status_by_descriptor_digest(const char *digest)
  704. {
  705. if (!current_consensus) return NULL;
  706. if (!current_consensus->desc_digest_map) {
  707. digestmap_t * m = current_consensus->desc_digest_map = digestmap_new();
  708. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  709. routerstatus_t *, rs,
  710. {
  711. digestmap_set(m, rs->descriptor_digest, rs);
  712. });
  713. }
  714. return digestmap_get(current_consensus->desc_digest_map, digest);
  715. }
  716. /** Given the digest of a router descriptor, return its current download
  717. * status, or NULL if the digest is unrecognized. */
  718. download_status_t *
  719. router_get_dl_status_by_descriptor_digest(const char *d)
  720. {
  721. routerstatus_t *rs;
  722. if ((rs = router_get_consensus_status_by_descriptor_digest(d)))
  723. return &rs->dl_status;
  724. if (v2_download_status_map)
  725. return digestmap_get(v2_download_status_map, d);
  726. return NULL;
  727. }
  728. /** Return the consensus view of the status of the router whose identity
  729. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  730. routerstatus_t *
  731. router_get_consensus_status_by_id(const char *digest)
  732. {
  733. if (!current_consensus)
  734. return NULL;
  735. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  736. _compare_digest_to_routerstatus_entry);
  737. }
  738. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  739. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  740. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  741. * nickname, but the Named flag isn't set for that router. */
  742. routerstatus_t *
  743. router_get_consensus_status_by_nickname(const char *nickname,
  744. int warn_if_unnamed)
  745. {
  746. char digest[DIGEST_LEN];
  747. routerstatus_t *best=NULL;
  748. smartlist_t *matches=NULL;
  749. const char *named_id=NULL;
  750. if (!current_consensus || !nickname)
  751. return NULL;
  752. if (nickname[0] == '$') {
  753. if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname+1))<0)
  754. return NULL;
  755. return networkstatus_vote_find_entry(current_consensus, digest);
  756. } else if (strlen(nickname) == HEX_DIGEST_LEN &&
  757. (base16_decode(digest, DIGEST_LEN, nickname, strlen(nickname))==0)) {
  758. return networkstatus_vote_find_entry(current_consensus, digest);
  759. }
  760. if (named_server_map)
  761. named_id = strmap_get_lc(named_server_map, nickname);
  762. if (named_id)
  763. return networkstatus_vote_find_entry(current_consensus, named_id);
  764. if (unnamed_server_map &&
  765. strmap_get_lc(unnamed_server_map, nickname))
  766. return NULL; /* XXXX020 should we warn? */
  767. /*XXXX020 is this behavior really what we want? */
  768. matches = smartlist_create();
  769. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  770. routerstatus_t *, lrs,
  771. {
  772. if (!strcasecmp(lrs->nickname, nickname)) {
  773. if (lrs->is_named) {
  774. tor_fragile_assert() /* This should never happen. */
  775. smartlist_free(matches);
  776. return lrs;
  777. } else {
  778. if (lrs->is_unnamed) {
  779. tor_fragile_assert(); /* nor should this. */
  780. smartlist_clear(matches);
  781. best=NULL;
  782. break;
  783. }
  784. smartlist_add(matches, lrs);
  785. best = lrs;
  786. }
  787. }
  788. });
  789. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  790. int any_unwarned=0;
  791. SMARTLIST_FOREACH(matches, routerstatus_t *, lrs,
  792. {
  793. if (! lrs->name_lookup_warned) {
  794. lrs->name_lookup_warned=1;
  795. any_unwarned=1;
  796. }
  797. });
  798. if (any_unwarned) {
  799. log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
  800. " but none is listed as named by the directory authorites. "
  801. "Choosing one arbitrarily.", nickname);
  802. }
  803. } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
  804. char fp[HEX_DIGEST_LEN+1];
  805. base16_encode(fp, sizeof(fp),
  806. best->identity_digest, DIGEST_LEN);
  807. log_warn(LD_CONFIG,
  808. "When looking up a status, you specified a server \"%s\" by name, "
  809. "but the directory authorities do not have any key registered for "
  810. "this nickname -- so it could be used by any server, "
  811. "not just the one you meant. "
  812. "To make sure you get the same server in the future, refer to "
  813. "it by key, as \"$%s\".", nickname, fp);
  814. best->name_lookup_warned = 1;
  815. }
  816. smartlist_free(matches);
  817. return best;
  818. }
  819. /** Return the identity digest that's mapped to officially by
  820. * <b>nickname</b>. */
  821. const char *
  822. networkstatus_get_router_digest_by_nickname(const char *nickname)
  823. {
  824. if (!named_server_map)
  825. return NULL;
  826. return strmap_get_lc(named_server_map, nickname);
  827. }
  828. /** DOCDOC */
  829. int
  830. networkstatus_nickname_is_unnamed(const char *nickname)
  831. {
  832. if (!unnamed_server_map)
  833. return 0;
  834. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  835. }
  836. /** How frequently do directory authorities re-download fresh networkstatus
  837. * documents? */
  838. #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
  839. /** How frequently do non-authority directory caches re-download fresh
  840. * networkstatus documents? */
  841. #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
  842. /** We are a directory server, and so cache network_status documents.
  843. * Initiate downloads as needed to update them. For v2 authorities,
  844. * this means asking each trusted directory for its network-status.
  845. * For caches, this means asking a random v2 authority for all
  846. * network-statuses.
  847. */
  848. static void
  849. update_v2_networkstatus_cache_downloads(time_t now)
  850. {
  851. int authority = authdir_mode_v2(get_options());
  852. int interval =
  853. authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
  854. const smartlist_t *trusted_dir_servers = router_get_trusted_dir_servers();
  855. if (last_networkstatus_download_attempted + interval >= now)
  856. return;
  857. last_networkstatus_download_attempted = now;
  858. if (authority) {
  859. /* An authority launches a separate connection for everybody. */
  860. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
  861. {
  862. char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
  863. if (!(ds->type & V2_AUTHORITY))
  864. continue;
  865. if (router_digest_is_me(ds->digest))
  866. continue;
  867. if (connection_get_by_type_addr_port_purpose(
  868. CONN_TYPE_DIR, ds->addr, ds->dir_port,
  869. DIR_PURPOSE_FETCH_NETWORKSTATUS)) {
  870. /* XXX020 the above dir_port won't be accurate if we're
  871. * doing a tunneled conn. In that case it should be or_port.
  872. * How to guess from here? Maybe make the function less general
  873. * and have it know that it's looking for dir conns. -RD */
  874. /* We are already fetching this one. */
  875. continue;
  876. }
  877. strlcpy(resource, "fp/", sizeof(resource));
  878. base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
  879. strlcat(resource, ".z", sizeof(resource));
  880. directory_initiate_command_routerstatus(
  881. &ds->fake_status, DIR_PURPOSE_FETCH_NETWORKSTATUS,
  882. ROUTER_PURPOSE_GENERAL,
  883. 0, /* Not private */
  884. resource,
  885. NULL, 0 /* No payload. */,
  886. 0 /* No I-M-S. */);
  887. });
  888. } else {
  889. /* A non-authority cache launches one connection to a random authority. */
  890. /* (Check whether we're currently fetching network-status objects.) */
  891. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  892. DIR_PURPOSE_FETCH_NETWORKSTATUS))
  893. directory_get_from_dirserver(DIR_PURPOSE_FETCH_NETWORKSTATUS,
  894. ROUTER_PURPOSE_GENERAL, "all.z",1);
  895. }
  896. }
  897. /**DOCDOC*/
  898. #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
  899. /**DOCDOC*/
  900. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  901. /** If we want to download a fresh consensus, launch a new download as
  902. * appropriate. */
  903. static void
  904. update_consensus_networkstatus_downloads(time_t now)
  905. {
  906. or_options_t *options = get_options();
  907. if (!networkstatus_get_live_consensus(now))
  908. time_to_download_next_consensus = now; /* No live consensus? Get one now!*/
  909. if (time_to_download_next_consensus > now)
  910. return; /* Wait until the current consensus is older. */
  911. if (authdir_mode_v3(options))
  912. return; /* Authorities never fetch a consensus */
  913. if (!download_status_is_ready(&consensus_dl_status, now,
  914. CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES))
  915. return; /* We failed downloading a consensus too recently. */
  916. if (connection_get_by_type_purpose(CONN_TYPE_DIR,
  917. DIR_PURPOSE_FETCH_CONSENSUS))
  918. return; /* There's an in-progress download.*/
  919. if (consensus_waiting_for_certs) {
  920. if (consensus_waiting_for_certs_set_at + DELAY_WHILE_FETCHING_CERTS > now)
  921. return; /* We're still getting certs for this one. */
  922. else {
  923. if (!consensus_waiting_for_certs_dl_failed) {
  924. download_status_failed(&consensus_dl_status, 0);
  925. consensus_waiting_for_certs_dl_failed=1;
  926. }
  927. }
  928. }
  929. log_info(LD_DIR, "Launching networkstatus consensus download.");
  930. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  931. ROUTER_PURPOSE_GENERAL, NULL, 1);
  932. }
  933. /** Called when an attempt to download a consensus fails: note that the
  934. * failure occurred, and possibly retry. */
  935. void
  936. networkstatus_consensus_download_failed(int status_code)
  937. {
  938. download_status_failed(&consensus_dl_status, status_code);
  939. /* Retry immediately, if appropriate. */
  940. update_consensus_networkstatus_downloads(time(NULL));
  941. }
  942. /**DOCDOC*/
  943. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  944. /** Update the time at which we'll consider replacing the current
  945. * consensus. */
  946. void
  947. update_consensus_networkstatus_fetch_time(time_t now)
  948. {
  949. or_options_t *options = get_options();
  950. networkstatus_vote_t *c = networkstatus_get_live_consensus(now);
  951. if (c) {
  952. long dl_interval;
  953. long interval = c->fresh_until - c->valid_after;
  954. time_t start;
  955. if (directory_fetches_dir_info_like_mirror(options)) {
  956. /* We want to cache the next one at some point after this one
  957. * is no longer fresh... */
  958. start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  959. /* But only in the first half-interval after that. */
  960. dl_interval = interval/2;
  961. } else {
  962. /* We're an ordinary client or a bridge. Give all the caches enough
  963. * time to download the consensus. */
  964. start = c->fresh_until + (interval*3)/4;
  965. /* But download the next one well before this one is expired. */
  966. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  967. /* If we're a bridge user, make use of the numbers we just computed
  968. * to choose the rest of the interval *after* them. */
  969. if (directory_fetches_dir_info_like_bridge_user(options)) {
  970. /* Give all the *clients* enough time to download the consensus. */
  971. start = start + dl_interval + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  972. /* But try to get it before ours actually expires. */
  973. dl_interval = (c->valid_until - start) -
  974. CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  975. }
  976. }
  977. if (dl_interval < 1)
  978. dl_interval = 1;
  979. /* We must not try to replace c while it's still the most valid: */
  980. tor_assert(c->fresh_until < start);
  981. /* We must download the next one before c is invalid: */
  982. tor_assert(start+dl_interval < c->valid_until);
  983. time_to_download_next_consensus = start + crypto_rand_int(dl_interval);
  984. {
  985. char tbuf1[ISO_TIME_LEN+1];
  986. char tbuf2[ISO_TIME_LEN+1];
  987. char tbuf3[ISO_TIME_LEN+1];
  988. format_local_iso_time(tbuf1, c->fresh_until);
  989. format_local_iso_time(tbuf2, c->valid_until);
  990. format_local_iso_time(tbuf3, time_to_download_next_consensus);
  991. log_info(LD_DIR, "Live consensus %s the most recent until %s and will "
  992. "expire at %s; fetching the next one at %s.",
  993. (c->fresh_until > now) ? "will be" : "was",
  994. tbuf1, tbuf2, tbuf3);
  995. }
  996. } else {
  997. time_to_download_next_consensus = now;
  998. log_info(LD_DIR, "No live consensus; we should fetch one immediately.");
  999. }
  1000. }
  1001. /** Return 1 if there's a reason we shouldn't try any directory
  1002. * fetches yet (e.g. we demand bridges and none are yet known).
  1003. * Else return 0. */
  1004. int
  1005. should_delay_dir_fetches(or_options_t *options)
  1006. {
  1007. if (options->UseBridges && !any_bridge_descriptors_known()) {
  1008. log_info(LD_DIR, "delaying dir fetches");
  1009. return 1;
  1010. }
  1011. return 0;
  1012. }
  1013. /** Launch requests for networkstatus documents and authority certificates as
  1014. * appropriate. */
  1015. void
  1016. update_networkstatus_downloads(time_t now)
  1017. {
  1018. or_options_t *options = get_options();
  1019. if (should_delay_dir_fetches(options))
  1020. return;
  1021. if (directory_fetches_dir_info_like_mirror(options))
  1022. update_v2_networkstatus_cache_downloads(now);
  1023. update_consensus_networkstatus_downloads(now);
  1024. update_certificate_downloads(now);
  1025. }
  1026. /**DOCDOC */
  1027. void
  1028. update_certificate_downloads(time_t now)
  1029. {
  1030. if (consensus_waiting_for_certs)
  1031. authority_certs_fetch_missing(consensus_waiting_for_certs, now);
  1032. else
  1033. authority_certs_fetch_missing(current_consensus, now);
  1034. }
  1035. /** Return the network status with a given identity digest. */
  1036. networkstatus_v2_t *
  1037. networkstatus_v2_get_by_digest(const char *digest)
  1038. {
  1039. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1040. {
  1041. if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
  1042. return ns;
  1043. });
  1044. return NULL;
  1045. }
  1046. /** Return the most recent consensus that we have downloaded, or NULL if we
  1047. * don't have one. */
  1048. networkstatus_vote_t *
  1049. networkstatus_get_latest_consensus(void)
  1050. {
  1051. return current_consensus;
  1052. }
  1053. /** Return the most recent consensus that we have downloaded, or NULL if it is
  1054. * no longer live. */
  1055. networkstatus_vote_t *
  1056. networkstatus_get_live_consensus(time_t now)
  1057. {
  1058. if (current_consensus &&
  1059. current_consensus->valid_after <= now &&
  1060. now <= current_consensus->valid_until)
  1061. return current_consensus;
  1062. else
  1063. return NULL;
  1064. }
  1065. /* XXXX020 remove this in favor of get_live_consensus. But actually,
  1066. * leave something like it for bridge users, who need to not totally
  1067. * lose if they spend a while fetching a new consensus. */
  1068. networkstatus_vote_t *
  1069. networkstatus_get_reasonably_live_consensus(time_t now)
  1070. {
  1071. #define REASONABLY_LIVE_TIME (24*60*60)
  1072. if (current_consensus &&
  1073. current_consensus->valid_after <= now &&
  1074. now <= current_consensus->valid_until+REASONABLY_LIVE_TIME)
  1075. return current_consensus;
  1076. else
  1077. return NULL;
  1078. }
  1079. /** Copy all the ancillary information (like router download status and so on)
  1080. * from <b>old_c</b> to <b>new_c</b>. */
  1081. static void
  1082. networkstatus_copy_old_consensus_info(networkstatus_vote_t *new_c,
  1083. const networkstatus_vote_t *old_c)
  1084. {
  1085. int idx = 0;
  1086. const routerstatus_t *rs_old;
  1087. if (old_c == new_c)
  1088. return;
  1089. if (!smartlist_len(old_c->routerstatus_list))
  1090. return;
  1091. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  1092. SMARTLIST_FOREACH(new_c->routerstatus_list, routerstatus_t *, rs_new,
  1093. {
  1094. int r;
  1095. while ((r = memcmp(rs_old->identity_digest, rs_new->identity_digest,
  1096. DIGEST_LEN))<0) {
  1097. if (++idx == smartlist_len(old_c->routerstatus_list))
  1098. goto done;
  1099. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  1100. }
  1101. if (r>0)
  1102. continue;
  1103. tor_assert(r==0);
  1104. /* Okay, so we're looking at the same identity. */
  1105. rs_new->name_lookup_warned = rs_old->name_lookup_warned;
  1106. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  1107. if (!memcmp(rs_old->descriptor_digest, rs_new->descriptor_digest,
  1108. DIGEST_LEN)) {
  1109. /* And the same descriptor too! */
  1110. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  1111. }
  1112. });
  1113. done:
  1114. return;
  1115. }
  1116. /** Try to replace the current cached v3 networkstatus with the one in
  1117. * <b>consensus</b>. If we don't have enough certificates to validate it,
  1118. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  1119. *
  1120. * Return 0 on success, <0 on failure. On failure, caller should increment
  1121. * the failure count as appropriate.
  1122. *
  1123. * We return -1 for mild failures that don't need to be reported to the
  1124. * user, and -2 for more serious problems.
  1125. */
  1126. int
  1127. networkstatus_set_current_consensus(const char *consensus, int from_cache,
  1128. int was_waiting_for_certs)
  1129. {
  1130. networkstatus_vote_t *c;
  1131. int r, result = -1;
  1132. time_t now = time(NULL);
  1133. char *unverified_fname = NULL, *consensus_fname = NULL;
  1134. /* Make sure it's parseable. */
  1135. c = networkstatus_parse_vote_from_string(consensus, NULL, 0);
  1136. if (!c) {
  1137. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1138. result = -2;
  1139. goto done;
  1140. }
  1141. if (current_consensus &&
  1142. !memcmp(c->networkstatus_digest, current_consensus->networkstatus_digest,
  1143. DIGEST_LEN)) {
  1144. /* We already have this one. That's a failure. */
  1145. log_info(LD_DIR, "Got a consensus we already have");
  1146. goto done;
  1147. }
  1148. if (current_consensus && c->valid_after <= current_consensus->valid_after) {
  1149. /* We have a newer one. There's no point in accepting this one,
  1150. * even if it's great. */
  1151. log_info(LD_DIR, "Got a consensus at least as old as the one we have");
  1152. goto done;
  1153. }
  1154. consensus_fname = get_datadir_fname("cached-consensus");
  1155. unverified_fname = get_datadir_fname("unverified-consensus");
  1156. /* Make sure it's signed enough. */
  1157. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1158. if (r == -1 && !was_waiting_for_certs) {
  1159. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1160. if (!was_waiting_for_certs) {
  1161. /* XXX020 eventually downgrade this log severity, or make it so
  1162. * users know why they're being told. */
  1163. log_notice(LD_DIR, "Not enough certificates to check networkstatus "
  1164. "consensus");
  1165. }
  1166. if (!current_consensus ||
  1167. c->valid_after > current_consensus->valid_after) {
  1168. if (consensus_waiting_for_certs)
  1169. networkstatus_vote_free(consensus_waiting_for_certs);
  1170. tor_free(consensus_waiting_for_certs_body);
  1171. consensus_waiting_for_certs = c;
  1172. c = NULL; /* Prevent free. */
  1173. consensus_waiting_for_certs_body = tor_strdup(consensus);
  1174. consensus_waiting_for_certs_set_at = now;
  1175. consensus_waiting_for_certs_dl_failed = 0;
  1176. if (!from_cache) {
  1177. write_str_to_file(unverified_fname, consensus, 0);
  1178. }
  1179. authority_certs_fetch_missing(c, now);
  1180. /* This case is not a success or a failure until we get the certs
  1181. * or fail to get the certs. */
  1182. result = 0;
  1183. } else {
  1184. /* Even if we had enough signatures, we'd never use this as the
  1185. * latest consensus. */
  1186. if (was_waiting_for_certs && from_cache)
  1187. unlink(unverified_fname);
  1188. }
  1189. goto done;
  1190. } else {
  1191. /* This can never be signed enough: Kill it. */
  1192. if (!was_waiting_for_certs) {
  1193. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1194. "consensus");
  1195. result = -2;
  1196. }
  1197. if (was_waiting_for_certs && (r < -1) && from_cache)
  1198. unlink(unverified_fname);
  1199. goto done;
  1200. }
  1201. }
  1202. /* Are we missing any certificates at all? */
  1203. if (r != 1)
  1204. authority_certs_fetch_missing(c, now);
  1205. if (current_consensus) {
  1206. networkstatus_copy_old_consensus_info(c, current_consensus);
  1207. networkstatus_vote_free(current_consensus);
  1208. }
  1209. if (consensus_waiting_for_certs &&
  1210. consensus_waiting_for_certs->valid_after <= c->valid_after) {
  1211. networkstatus_vote_free(consensus_waiting_for_certs);
  1212. consensus_waiting_for_certs = NULL;
  1213. if (consensus != consensus_waiting_for_certs_body)
  1214. tor_free(consensus_waiting_for_certs_body);
  1215. else
  1216. consensus_waiting_for_certs_body = NULL;
  1217. consensus_waiting_for_certs_set_at = 0;
  1218. consensus_waiting_for_certs_dl_failed = 0;
  1219. unlink(unverified_fname);
  1220. }
  1221. /* Reset the failure count only if this consensus is actually valid. */
  1222. if (c->valid_after <= now && now <= c->valid_until) {
  1223. download_status_reset(&consensus_dl_status);
  1224. } else {
  1225. if (!from_cache)
  1226. download_status_failed(&consensus_dl_status, 0);
  1227. }
  1228. current_consensus = c;
  1229. c = NULL; /* Prevent free. */
  1230. update_consensus_networkstatus_fetch_time(now);
  1231. dirvote_recalculate_timing(get_options(), now);
  1232. routerstatus_list_update_named_server_map();
  1233. if (!from_cache) {
  1234. write_str_to_file(consensus_fname, consensus, 0);
  1235. }
  1236. if (directory_caches_dir_info(get_options()))
  1237. dirserv_set_cached_networkstatus_v3(consensus,
  1238. current_consensus->valid_after);
  1239. if (ftime_definitely_before(now, current_consensus->valid_after)) {
  1240. char tbuf[ISO_TIME_LEN+1];
  1241. char dbuf[64];
  1242. long delta = now - current_consensus->valid_after;
  1243. format_iso_time(tbuf, current_consensus->valid_after);
  1244. format_time_interval(dbuf, sizeof(dbuf), delta);
  1245. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1246. "consensus network status document (%s GMT). Tor needs an "
  1247. "accurate clock to work correctly. Please check your time and "
  1248. "date settings!", dbuf, tbuf);
  1249. control_event_general_status(LOG_WARN,
  1250. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1251. }
  1252. router_dir_info_changed();
  1253. result = 0;
  1254. done:
  1255. if (c)
  1256. networkstatus_vote_free(c);
  1257. tor_free(consensus_fname);
  1258. tor_free(unverified_fname);
  1259. return result;
  1260. }
  1261. /** Called when we have gotten more certificates: see whether we can
  1262. * now verify a pending consensus. */
  1263. void
  1264. networkstatus_note_certs_arrived(void)
  1265. {
  1266. if (consensus_waiting_for_certs) {
  1267. if (networkstatus_check_consensus_signature(
  1268. consensus_waiting_for_certs, 0)>=0) {
  1269. if (!networkstatus_set_current_consensus(
  1270. consensus_waiting_for_certs_body, 0, 1)) {
  1271. tor_free(consensus_waiting_for_certs_body);
  1272. }
  1273. }
  1274. }
  1275. }
  1276. /** If the network-status list has changed since the last time we called this
  1277. * function, update the status of every routerinfo from the network-status
  1278. * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
  1279. * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
  1280. */
  1281. void
  1282. routers_update_all_from_networkstatus(time_t now, int dir_version)
  1283. {
  1284. routerinfo_t *me;
  1285. routerlist_t *rl = router_get_routerlist();
  1286. networkstatus_vote_t *consensus = networkstatus_get_live_consensus(now);
  1287. if (networkstatus_v2_list_has_changed)
  1288. download_status_map_update_from_v2_networkstatus();
  1289. if (!consensus || dir_version < 3) /* nothing more we should do */
  1290. return;
  1291. /* More routers may be up or down now: we need to recalc whether there's
  1292. * enough directory info. */
  1293. router_dir_info_changed();
  1294. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1295. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1296. ri->cache_info.routerlist_index = ri_sl_idx);
  1297. if (rl->old_routers)
  1298. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1299. me = router_get_my_routerinfo();
  1300. if (me && !have_warned_about_invalid_status) {
  1301. routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
  1302. me->cache_info.identity_digest);
  1303. if (!rs) {
  1304. log_info(LD_GENERAL, "The latest consensus does not list us."
  1305. "Are you misconfigured?");
  1306. have_warned_about_invalid_status = 1;
  1307. } else if (rs->is_unnamed) {
  1308. /* XXXX020 this isn't a useful warning. */
  1309. log_info(LD_GENERAL, "The directory have assigned the nickname "
  1310. "you're using to a different identity.");
  1311. have_warned_about_invalid_status = 1;
  1312. } else if (!rs->is_named) {
  1313. /*XXXX020 this isn't a correct warning. */
  1314. log_info(LD_GENERAL, "The directory authorities do not recognize "
  1315. "your nickname. Please consider sending your "
  1316. "nickname and identity fingerprint to the tor-ops.");
  1317. have_warned_about_invalid_status = 1;
  1318. }
  1319. }
  1320. if (!have_warned_about_old_version) {
  1321. int is_server = server_mode(get_options());
  1322. version_status_t status;
  1323. const char *recommended = is_server ?
  1324. consensus->server_versions : consensus->client_versions;
  1325. status = tor_version_is_obsolete(VERSION, recommended);
  1326. if (status == VS_RECOMMENDED) {
  1327. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1328. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1329. if (!have_warned_about_new_version) {
  1330. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1331. "recommended version%s, according to the directory "
  1332. "authorities. Recommended versions are: %s",
  1333. VERSION,
  1334. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1335. recommended);
  1336. have_warned_about_new_version = 1;
  1337. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1338. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1339. VERSION, "NEW", recommended);
  1340. }
  1341. } else {
  1342. log_warn(LD_GENERAL, "Please upgrade! "
  1343. "This version of Tor (%s) is %s, according to the directory "
  1344. "authorities. Recommended versions are: %s",
  1345. VERSION,
  1346. status == VS_OLD ? "obsolete" : "not recommended",
  1347. recommended);
  1348. have_warned_about_old_version = 1;
  1349. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1350. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1351. VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
  1352. recommended);
  1353. }
  1354. }
  1355. }
  1356. /** Update v2_download_status_map to contain an entry for every router
  1357. * descriptor listed in the v2 networkstatuses. */
  1358. static void
  1359. download_status_map_update_from_v2_networkstatus(void)
  1360. {
  1361. digestmap_t *dl_status;
  1362. if (!networkstatus_v2_list)
  1363. return;
  1364. if (!v2_download_status_map)
  1365. v2_download_status_map = digestmap_new();
  1366. dl_status = digestmap_new();
  1367. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1368. {
  1369. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  1370. {
  1371. const char *d = rs->descriptor_digest;
  1372. download_status_t *s;
  1373. if (digestmap_get(dl_status, d))
  1374. continue;
  1375. if (!(s = digestmap_remove(v2_download_status_map, d))) {
  1376. s = tor_malloc_zero(sizeof(download_status_t));
  1377. }
  1378. digestmap_set(dl_status, d, s);
  1379. });
  1380. });
  1381. digestmap_free(v2_download_status_map, _tor_free);
  1382. v2_download_status_map = dl_status;
  1383. networkstatus_v2_list_has_changed = 0;
  1384. }
  1385. /** Update our view of the list of named servers from the most recently
  1386. * retrieved networkstatus consensus. */
  1387. static void
  1388. routerstatus_list_update_named_server_map(void)
  1389. {
  1390. if (!current_consensus)
  1391. return;
  1392. if (named_server_map)
  1393. strmap_free(named_server_map, _tor_free);
  1394. named_server_map = strmap_new();
  1395. if (unnamed_server_map)
  1396. strmap_free(unnamed_server_map, NULL);
  1397. unnamed_server_map = strmap_new();
  1398. SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
  1399. {
  1400. if (rs->is_named) {
  1401. strmap_set_lc(named_server_map, rs->nickname,
  1402. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1403. }
  1404. if (rs->is_unnamed) {
  1405. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1406. }
  1407. });
  1408. }
  1409. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1410. * according to our current consensus networkstatus. May re-order
  1411. * <b>routers</b>. */
  1412. void
  1413. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1414. int reset_failures)
  1415. {
  1416. trusted_dir_server_t *ds;
  1417. routerstatus_t *rs;
  1418. or_options_t *options = get_options();
  1419. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  1420. int namingdir = authdir && options->NamingAuthoritativeDir;
  1421. networkstatus_vote_t *ns = current_consensus;
  1422. int idx;
  1423. if (!ns || !smartlist_len(ns->routerstatus_list))
  1424. return;
  1425. routers_sort_by_identity(routers);
  1426. /* Now routers and ns->routerstatus_list are both in ascending order
  1427. * of identity digest. */
  1428. idx = 0;
  1429. rs = smartlist_get(ns->routerstatus_list, idx);
  1430. SMARTLIST_FOREACH(routers, routerinfo_t *, router,
  1431. {
  1432. const char *digest = router->cache_info.identity_digest;
  1433. int r;
  1434. while ((r = memcmp(rs->identity_digest, digest, DIGEST_LEN))<0) {
  1435. if (++idx == smartlist_len(ns->routerstatus_list)) {
  1436. /* We're out of routerstatuses. Bail. */
  1437. goto done;
  1438. }
  1439. rs = smartlist_get(ns->routerstatus_list, idx);
  1440. }
  1441. if (r>0) {
  1442. /* We have no routerstatus for this router. Clear flags and skip it. */
  1443. if (!namingdir)
  1444. router->is_named = 0;
  1445. if (!authdir) {
  1446. if (router->purpose == ROUTER_PURPOSE_GENERAL)
  1447. router_clear_status_flags(router);
  1448. }
  1449. continue;
  1450. }
  1451. tor_assert(r==0);
  1452. ds = router_get_trusteddirserver_by_digest(digest);
  1453. if (!namingdir) {
  1454. if (rs->is_named && !strcasecmp(router->nickname, rs->nickname))
  1455. router->is_named = 1;
  1456. else
  1457. router->is_named = 0;
  1458. }
  1459. if (!memcmp(router->cache_info.signed_descriptor_digest,
  1460. rs->descriptor_digest, DIGEST_LEN)) {
  1461. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1462. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1463. }
  1464. if (!authdir) {
  1465. /* If we're not an authdir, believe others. */
  1466. router->is_valid = rs->is_valid;
  1467. router->is_running = rs->is_running;
  1468. router->is_fast = rs->is_fast;
  1469. router->is_stable = rs->is_stable;
  1470. router->is_possible_guard = rs->is_possible_guard;
  1471. router->is_exit = rs->is_exit;
  1472. router->is_bad_directory = rs->is_bad_directory;
  1473. router->is_bad_exit = rs->is_bad_exit;
  1474. router->is_hs_dir = rs->is_hs_dir;
  1475. }
  1476. if (router->is_running && ds) {
  1477. download_status_reset(&ds->v2_ns_dl_status);
  1478. }
  1479. if (reset_failures) {
  1480. download_status_reset(&rs->dl_status);
  1481. }
  1482. });
  1483. done:
  1484. router_dir_info_changed();
  1485. }
  1486. /**DOCDOC*/
  1487. void
  1488. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1489. {
  1490. networkstatus_vote_t *ns = current_consensus;
  1491. if (!ns)
  1492. return;
  1493. if (!ns->desc_digest_map) {
  1494. char dummy[DIGEST_LEN];
  1495. /* instantiates the digest map. */
  1496. memset(dummy, 0, sizeof(dummy));
  1497. router_get_consensus_status_by_descriptor_digest(dummy);
  1498. }
  1499. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1500. {
  1501. routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1502. d->signed_descriptor_digest);
  1503. if (rs) {
  1504. if (ns->valid_until > d->last_listed_as_valid_until)
  1505. d->last_listed_as_valid_until = ns->valid_until;
  1506. }
  1507. });
  1508. }
  1509. /** Generate networkstatus lines for a single routerstatus_t object, and
  1510. * return the result in a newly allocated string. Used only by controller
  1511. * interface (for now.) */
  1512. char *
  1513. networkstatus_getinfo_helper_single(routerstatus_t *rs)
  1514. {
  1515. char buf[256];
  1516. routerstatus_format_entry(buf, sizeof(buf), rs, NULL, 0);
  1517. return tor_strdup(buf);
  1518. }
  1519. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1520. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1521. * newly-allocated string containing networkstatus lines for the appropriate
  1522. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1523. int
  1524. getinfo_helper_networkstatus(control_connection_t *conn,
  1525. const char *question, char **answer)
  1526. {
  1527. routerstatus_t *status;
  1528. (void) conn;
  1529. if (!current_consensus) {
  1530. *answer = tor_strdup("");
  1531. return 0;
  1532. }
  1533. if (!strcmp(question, "ns/all")) {
  1534. smartlist_t *statuses = smartlist_create();
  1535. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1536. routerstatus_t *, rs,
  1537. {
  1538. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1539. });
  1540. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1541. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1542. smartlist_free(statuses);
  1543. return 0;
  1544. } else if (!strcmpstart(question, "ns/id/")) {
  1545. char d[DIGEST_LEN];
  1546. if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6)))
  1547. return -1;
  1548. status = router_get_consensus_status_by_id(d);
  1549. } else if (!strcmpstart(question, "ns/name/")) {
  1550. status = router_get_consensus_status_by_nickname(question+8, 0);
  1551. } else {
  1552. return -1;
  1553. }
  1554. if (status) {
  1555. *answer = networkstatus_getinfo_helper_single(status);
  1556. }
  1557. return 0;
  1558. }
  1559. /** Free all storage held locally in this module. */
  1560. void
  1561. networkstatus_free_all(void)
  1562. {
  1563. if (networkstatus_v2_list) {
  1564. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1565. networkstatus_v2_free(ns));
  1566. smartlist_free(networkstatus_v2_list);
  1567. networkstatus_v2_list = NULL;
  1568. }
  1569. if (v2_download_status_map) {
  1570. digestmap_free(v2_download_status_map, _tor_free);
  1571. v2_download_status_map = NULL;
  1572. }
  1573. if (current_consensus) {
  1574. networkstatus_vote_free(current_consensus);
  1575. current_consensus = NULL;
  1576. }
  1577. if (consensus_waiting_for_certs) {
  1578. networkstatus_vote_free(consensus_waiting_for_certs);
  1579. consensus_waiting_for_certs = NULL;
  1580. }
  1581. tor_free(consensus_waiting_for_certs_body);
  1582. if (named_server_map) {
  1583. strmap_free(named_server_map, _tor_free);
  1584. }
  1585. if (unnamed_server_map) {
  1586. strmap_free(unnamed_server_map, NULL);
  1587. }
  1588. }