networkstatus.c 59 KB

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