networkstatus.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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));
  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));
  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 (dirserver_mode(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 (!dirserver_mode(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. if (!dirserver_mode(get_options())) {
  496. networkstatus_v2_free(ns);
  497. return 0;
  498. }
  499. source_desc = fp;
  500. } else {
  501. source_desc = trusted_dir->description;
  502. }
  503. now = time(NULL);
  504. if (arrived_at > now)
  505. arrived_at = now;
  506. ns->received_on = arrived_at;
  507. format_iso_time(published, ns->published_on);
  508. if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
  509. char dbuf[64];
  510. long delta = now - ns->published_on;
  511. format_time_interval(dbuf, sizeof(dbuf), delta);
  512. log_warn(LD_GENERAL, "Network status from %s was published %s in the "
  513. "future (%s GMT). Check your time and date settings! "
  514. "Not caching.",
  515. source_desc, dbuf, published);
  516. control_event_general_status(LOG_WARN,
  517. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
  518. delta, ns->source_address, ns->source_dirport);
  519. skewed = 1;
  520. }
  521. if (!networkstatus_v2_list)
  522. networkstatus_v2_list = smartlist_create();
  523. if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
  524. router_digest_is_me(ns->identity_digest)) {
  525. /* Don't replace our own networkstatus when we get it from somebody else.*/
  526. networkstatus_v2_free(ns);
  527. return 0;
  528. }
  529. if (requested_fingerprints) {
  530. if (smartlist_string_isin(requested_fingerprints, fp)) {
  531. smartlist_string_remove(requested_fingerprints, fp);
  532. } else {
  533. if (source != NS_FROM_DIR_ALL) {
  534. char *requested =
  535. smartlist_join_strings(requested_fingerprints," ",0,NULL);
  536. log_warn(LD_DIR,
  537. "We received a network status with a fingerprint (%s) that we "
  538. "never requested. (We asked for: %s.) Dropping.",
  539. fp, requested);
  540. tor_free(requested);
  541. return 0;
  542. }
  543. }
  544. }
  545. if (!trusted_dir) {
  546. if (!skewed && dirserver_mode(get_options())) {
  547. /* We got a non-trusted networkstatus, and we're a directory cache.
  548. * This means that we asked an authority, and it told us about another
  549. * authority we didn't recognize. */
  550. log_info(LD_DIR,
  551. "We do not recognize authority (%s) but we are willing "
  552. "to cache it.", fp);
  553. add_networkstatus_to_cache(s, source, ns);
  554. networkstatus_v2_free(ns);
  555. }
  556. return 0;
  557. }
  558. found = 0;
  559. for (i=0; i < smartlist_len(networkstatus_v2_list); ++i) {
  560. networkstatus_v2_t *old_ns = smartlist_get(networkstatus_v2_list, i);
  561. if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
  562. if (!memcmp(old_ns->networkstatus_digest,
  563. ns->networkstatus_digest, DIGEST_LEN)) {
  564. /* Same one we had before. */
  565. networkstatus_v2_free(ns);
  566. tor_assert(trusted_dir);
  567. log_info(LD_DIR,
  568. "Not replacing network-status from %s (published %s); "
  569. "we already have it.",
  570. trusted_dir->description, published);
  571. if (old_ns->received_on < arrived_at) {
  572. if (source != NS_FROM_CACHE) {
  573. char *fn;
  574. fn = networkstatus_get_cache_filename(old_ns->identity_digest);
  575. /* We use mtime to tell when it arrived, so update that. */
  576. touch_file(fn);
  577. tor_free(fn);
  578. }
  579. old_ns->received_on = arrived_at;
  580. }
  581. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  582. return 0;
  583. } else if (old_ns->published_on >= ns->published_on) {
  584. char old_published[ISO_TIME_LEN+1];
  585. format_iso_time(old_published, old_ns->published_on);
  586. tor_assert(trusted_dir);
  587. log_info(LD_DIR,
  588. "Not replacing network-status from %s (published %s);"
  589. " we have a newer one (published %s) for this authority.",
  590. trusted_dir->description, published,
  591. old_published);
  592. networkstatus_v2_free(ns);
  593. download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
  594. return 0;
  595. } else {
  596. networkstatus_v2_free(old_ns);
  597. smartlist_set(networkstatus_v2_list, i, ns);
  598. found = 1;
  599. break;
  600. }
  601. }
  602. }
  603. if (source != NS_FROM_CACHE && trusted_dir) {
  604. download_status_reset(&trusted_dir->v2_ns_dl_status);
  605. }
  606. if (!found)
  607. smartlist_add(networkstatus_v2_list, ns);
  608. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  609. {
  610. if (!router_get_by_descriptor_digest(rs->descriptor_digest))
  611. rs->need_to_mirror = 1;
  612. });
  613. log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
  614. source == NS_FROM_CACHE?"cached from":
  615. ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
  616. "downloaded from":"generated for"),
  617. trusted_dir->description, published);
  618. networkstatus_v2_list_has_changed = 1;
  619. router_dir_info_changed();
  620. smartlist_sort(networkstatus_v2_list,
  621. _compare_networkstatus_v2_published_on);
  622. if (!skewed)
  623. add_networkstatus_to_cache(s, source, ns);
  624. return 0;
  625. }
  626. /** Remove all very-old network_status_t objects from memory and from the
  627. * disk cache. */
  628. void
  629. networkstatus_v2_list_clean(time_t now)
  630. {
  631. int i;
  632. if (!networkstatus_v2_list)
  633. return;
  634. for (i = 0; i < smartlist_len(networkstatus_v2_list); ++i) {
  635. networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
  636. char *fname = NULL;
  637. if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
  638. continue;
  639. /* Okay, this one is too old. Remove it from the list, and delete it
  640. * from the cache. */
  641. smartlist_del(networkstatus_v2_list, i--);
  642. fname = networkstatus_get_cache_filename(ns->identity_digest);
  643. if (file_status(fname) == FN_FILE) {
  644. log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
  645. unlink(fname);
  646. }
  647. tor_free(fname);
  648. if (dirserver_mode(get_options())) {
  649. dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
  650. }
  651. networkstatus_v2_free(ns);
  652. router_dir_info_changed();
  653. }
  654. /* And now go through the directory cache for any cached untrusted
  655. * networkstatuses and other network info. */
  656. dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
  657. dirserv_clear_old_v1_info(now);
  658. }
  659. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  660. * digest in the key to the identity digest of a routerstatus_t. */
  661. static int
  662. _compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  663. {
  664. const char *key = _key;
  665. const routerstatus_t *rs = *_member;
  666. return memcmp(key, rs->identity_digest, DIGEST_LEN);
  667. }
  668. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  669. * NULL if none was found. */
  670. routerstatus_t *
  671. networkstatus_v2_find_entry(networkstatus_v2_t *ns, const char *digest)
  672. {
  673. return smartlist_bsearch(ns->entries, digest,
  674. _compare_digest_to_routerstatus_entry);
  675. }
  676. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  677. * NULL if none was found. */
  678. routerstatus_t *
  679. networkstatus_vote_find_entry(networkstatus_vote_t *ns, const char *digest)
  680. {
  681. return smartlist_bsearch(ns->routerstatus_list, digest,
  682. _compare_digest_to_routerstatus_entry);
  683. }
  684. /** Return a list of the v2 networkstatus documents. */
  685. const smartlist_t *
  686. networkstatus_get_v2_list(void)
  687. {
  688. if (!networkstatus_v2_list)
  689. networkstatus_v2_list = smartlist_create();
  690. return networkstatus_v2_list;
  691. }
  692. /** Return the consensus view of the status of the router whose current
  693. * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
  694. * known. */
  695. routerstatus_t *
  696. router_get_consensus_status_by_descriptor_digest(const char *digest)
  697. {
  698. if (!current_consensus) return NULL;
  699. if (!current_consensus->desc_digest_map) {
  700. digestmap_t * m = current_consensus->desc_digest_map = digestmap_new();
  701. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  702. routerstatus_t *, rs,
  703. {
  704. digestmap_set(m, rs->descriptor_digest, rs);
  705. });
  706. }
  707. return digestmap_get(current_consensus->desc_digest_map, digest);
  708. }
  709. /** Given the digest of a router descriptor, return its current download
  710. * status, or NULL if the digest is unrecognized. */
  711. download_status_t *
  712. router_get_dl_status_by_descriptor_digest(const char *d)
  713. {
  714. routerstatus_t *rs;
  715. if ((rs = router_get_consensus_status_by_descriptor_digest(d)))
  716. return &rs->dl_status;
  717. if (v2_download_status_map)
  718. return digestmap_get(v2_download_status_map, d);
  719. return NULL;
  720. }
  721. /** Return the consensus view of the status of the router whose identity
  722. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  723. routerstatus_t *
  724. router_get_consensus_status_by_id(const char *digest)
  725. {
  726. if (!current_consensus)
  727. return NULL;
  728. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  729. _compare_digest_to_routerstatus_entry);
  730. }
  731. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  732. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  733. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  734. * nickname, but the Named flag isn't set for that router. */
  735. routerstatus_t *
  736. router_get_consensus_status_by_nickname(const char *nickname,
  737. int warn_if_unnamed)
  738. {
  739. char digest[DIGEST_LEN];
  740. routerstatus_t *best=NULL;
  741. smartlist_t *matches=NULL;
  742. const char *named_id=NULL;
  743. if (!current_consensus || !nickname)
  744. return NULL;
  745. if (nickname[0] == '$') {
  746. if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))<0)
  747. return NULL;
  748. return networkstatus_vote_find_entry(current_consensus, digest);
  749. } else if (strlen(nickname) == HEX_DIGEST_LEN &&
  750. (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname))==0)) {
  751. return networkstatus_vote_find_entry(current_consensus, digest);
  752. }
  753. if (named_server_map)
  754. named_id = strmap_get_lc(named_server_map, nickname);
  755. if (named_id)
  756. return networkstatus_vote_find_entry(current_consensus, named_id);
  757. if (unnamed_server_map &&
  758. strmap_get_lc(unnamed_server_map, nickname))
  759. return NULL; /* XXXX020 should we warn? */
  760. /*XXXX020 is this behavior really what we want? */
  761. matches = smartlist_create();
  762. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  763. routerstatus_t *, lrs,
  764. {
  765. if (!strcasecmp(lrs->nickname, nickname)) {
  766. if (lrs->is_named) {
  767. tor_fragile_assert() /* This should never happen. */
  768. smartlist_free(matches);
  769. return lrs;
  770. } else {
  771. if (lrs->is_unnamed) {
  772. tor_fragile_assert(); /* nor should this. */
  773. smartlist_clear(matches);
  774. best=NULL;
  775. break;
  776. }
  777. smartlist_add(matches, lrs);
  778. best = lrs;
  779. }
  780. }
  781. });
  782. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  783. int any_unwarned=0;
  784. SMARTLIST_FOREACH(matches, routerstatus_t *, lrs,
  785. {
  786. if (! lrs->name_lookup_warned) {
  787. lrs->name_lookup_warned=1;
  788. any_unwarned=1;
  789. }
  790. });
  791. if (any_unwarned) {
  792. log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
  793. " but none is listed as named by the directory authorites. "
  794. "Choosing one arbitrarily.", nickname);
  795. }
  796. } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
  797. char fp[HEX_DIGEST_LEN+1];
  798. base16_encode(fp, sizeof(fp),
  799. best->identity_digest, DIGEST_LEN);
  800. log_warn(LD_CONFIG,
  801. "When looking up a status, you specified a server \"%s\" by name, "
  802. "but the directory authorities do not have any key registered for "
  803. "this nickname -- so it could be used by any server, "
  804. "not just the one you meant. "
  805. "To make sure you get the same server in the future, refer to "
  806. "it by key, as \"$%s\".", nickname, fp);
  807. best->name_lookup_warned = 1;
  808. }
  809. smartlist_free(matches);
  810. return best;
  811. }
  812. /** Return the identity digest that's mapped to officially by
  813. * <b>nickname</b>. */
  814. const char *
  815. networkstatus_get_router_digest_by_nickname(const char *nickname)
  816. {
  817. if (!named_server_map)
  818. return NULL;
  819. return strmap_get_lc(named_server_map, nickname);
  820. }
  821. /** DOCDOC */
  822. int
  823. networkstatus_nickname_is_unnamed(const char *nickname)
  824. {
  825. if (!unnamed_server_map)
  826. return 0;
  827. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  828. }
  829. /** How frequently do directory authorities re-download fresh networkstatus
  830. * documents? */
  831. #define AUTHORITY_NS_CACHE_INTERVAL (5*60)
  832. /** How frequently do non-authority directory caches re-download fresh
  833. * networkstatus documents? */
  834. #define NONAUTHORITY_NS_CACHE_INTERVAL (15*60)
  835. /** We are a directory server, and so cache network_status documents.
  836. * Initiate downloads as needed to update them. For v2 authorities,
  837. * this means asking each trusted directory for its network-status.
  838. * For caches, this means asking a random v2 authority for all
  839. * network-statuses.
  840. */
  841. static void
  842. update_v2_networkstatus_cache_downloads(time_t now)
  843. {
  844. int authority = authdir_mode_v2(get_options());
  845. int interval =
  846. authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
  847. const smartlist_t *trusted_dir_servers = router_get_trusted_dir_servers();
  848. if (last_networkstatus_download_attempted + interval >= now)
  849. return;
  850. last_networkstatus_download_attempted = now;
  851. if (authority) {
  852. /* An authority launches a separate connection for everybody. */
  853. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
  854. {
  855. char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
  856. if (!(ds->type & V2_AUTHORITY))
  857. continue;
  858. if (router_digest_is_me(ds->digest))
  859. continue;
  860. if (connection_get_by_type_addr_port_purpose(
  861. CONN_TYPE_DIR, ds->addr, ds->dir_port,
  862. DIR_PURPOSE_FETCH_NETWORKSTATUS)) {
  863. /* XXX020 the above dir_port won't be accurate if we're
  864. * doing a tunneled conn. In that case it should be or_port.
  865. * How to guess from here? Maybe make the function less general
  866. * and have it know that it's looking for dir conns. -RD */
  867. /* We are already fetching this one. */
  868. continue;
  869. }
  870. strlcpy(resource, "fp/", sizeof(resource));
  871. base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
  872. strlcat(resource, ".z", sizeof(resource));
  873. directory_initiate_command_routerstatus(
  874. &ds->fake_status, DIR_PURPOSE_FETCH_NETWORKSTATUS,
  875. ROUTER_PURPOSE_GENERAL,
  876. 0, /* Not private */
  877. resource,
  878. NULL, 0 /* No payload. */,
  879. 0 /* No I-M-S. */);
  880. });
  881. } else {
  882. /* A non-authority cache launches one connection to a random authority. */
  883. /* (Check whether we're currently fetching network-status objects.) */
  884. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  885. DIR_PURPOSE_FETCH_NETWORKSTATUS))
  886. directory_get_from_dirserver(DIR_PURPOSE_FETCH_NETWORKSTATUS,
  887. ROUTER_PURPOSE_GENERAL, "all.z",1);
  888. }
  889. }
  890. /**DOCDOC*/
  891. #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
  892. /**DOCDOC*/
  893. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  894. /** If we want to download a fresh consensus, launch a new download as
  895. * appropriate. */
  896. static void
  897. update_consensus_networkstatus_downloads(time_t now)
  898. {
  899. or_options_t *options = get_options();
  900. if (!networkstatus_get_live_consensus(now))
  901. time_to_download_next_consensus = now; /* No live consensus? Get one now!*/
  902. if (time_to_download_next_consensus > now)
  903. return; /* Wait until the current consensus is older. */
  904. if (authdir_mode_v3(options))
  905. return; /* Authorities never fetch a consensus */
  906. if (!download_status_is_ready(&consensus_dl_status, now,
  907. CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES))
  908. return; /* We failed downloading a consensus too recently. */
  909. if (connection_get_by_type_purpose(CONN_TYPE_DIR,
  910. DIR_PURPOSE_FETCH_CONSENSUS))
  911. return; /* There's an in-progress download.*/
  912. if (consensus_waiting_for_certs) {
  913. if (consensus_waiting_for_certs_set_at + DELAY_WHILE_FETCHING_CERTS > now)
  914. return; /* We're still getting certs for this one. */
  915. else {
  916. if (!consensus_waiting_for_certs_dl_failed) {
  917. download_status_failed(&consensus_dl_status, 0);
  918. consensus_waiting_for_certs_dl_failed=1;
  919. }
  920. }
  921. }
  922. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  923. ROUTER_PURPOSE_GENERAL, NULL, 1);
  924. }
  925. /** Called when an attempt to download a consensus fails: note that the
  926. * failure occurred, and possibly retry. */
  927. void
  928. networkstatus_consensus_download_failed(int status_code)
  929. {
  930. download_status_failed(&consensus_dl_status, status_code);
  931. /* Retry immediately, if appropriate. */
  932. update_consensus_networkstatus_downloads(time(NULL));
  933. }
  934. /**DOCDOC*/
  935. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  936. /** Update the time at which we'll consider replacing the current
  937. * consensus. */
  938. void
  939. update_consensus_networkstatus_fetch_time(time_t now)
  940. {
  941. or_options_t *options = get_options();
  942. networkstatus_vote_t *c = networkstatus_get_live_consensus(now);
  943. if (c) {
  944. long dl_interval;
  945. long interval = c->fresh_until - c->valid_after;
  946. time_t start;
  947. if (dirserver_mode(options)) {
  948. /* We want to cache the next one at some point after this one
  949. * is no longer fresh... */
  950. start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  951. /* But only in the first half-interval after that. */
  952. dl_interval = interval/2;
  953. } else {
  954. /* Give all the caches enough time to download the consensus.*/
  955. start = c->fresh_until + (interval*3)/4;
  956. /* But download the next one before this one is expired. */
  957. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  958. }
  959. if (dl_interval < 1)
  960. dl_interval = 1;
  961. /* We must not try to replace c while it's still the most valid: */
  962. tor_assert(c->fresh_until < start);
  963. /* We must download the next one before c is invalid: */
  964. tor_assert(start+dl_interval < c->valid_until);
  965. time_to_download_next_consensus = start + crypto_rand_int(dl_interval);
  966. {
  967. char tbuf1[ISO_TIME_LEN+1];
  968. char tbuf2[ISO_TIME_LEN+1];
  969. char tbuf3[ISO_TIME_LEN+1];
  970. format_local_iso_time(tbuf1, c->fresh_until);
  971. format_local_iso_time(tbuf2, c->valid_until);
  972. format_local_iso_time(tbuf3, time_to_download_next_consensus);
  973. log_info(LD_DIR, "Live consensus %s the most recent until %s and will "
  974. "expire at %s; fetching the next one at %s.",
  975. (c->fresh_until > now) ? "will be" : "was",
  976. tbuf1, tbuf2, tbuf3);
  977. }
  978. } else {
  979. time_to_download_next_consensus = now;
  980. log_info(LD_DIR, "No live consensus; we should fetch one immediately.");
  981. }
  982. }
  983. /** Return 1 if there's a reason we shouldn't try any directory
  984. * fetches yet (e.g. we demand bridges and none are yet known).
  985. * Else return 0. */
  986. int
  987. should_delay_dir_fetches(or_options_t *options)
  988. {
  989. if (options->UseBridges && !any_bridge_descriptors_known()) {
  990. log_info(LD_DIR, "delaying dir fetches");
  991. return 1;
  992. }
  993. return 0;
  994. }
  995. /** Launch requests for networkstatus documents and authority certificates as
  996. * appropriate. */
  997. void
  998. update_networkstatus_downloads(time_t now)
  999. {
  1000. or_options_t *options = get_options();
  1001. if (should_delay_dir_fetches(options))
  1002. return;
  1003. if (dirserver_mode(options))
  1004. update_v2_networkstatus_cache_downloads(now);
  1005. update_consensus_networkstatus_downloads(now);
  1006. update_certificate_downloads(now);
  1007. }
  1008. /**DOCDOC */
  1009. void
  1010. update_certificate_downloads(time_t now)
  1011. {
  1012. if (consensus_waiting_for_certs)
  1013. authority_certs_fetch_missing(consensus_waiting_for_certs, now);
  1014. else
  1015. authority_certs_fetch_missing(current_consensus, now);
  1016. }
  1017. /** Return the network status with a given identity digest. */
  1018. networkstatus_v2_t *
  1019. networkstatus_v2_get_by_digest(const char *digest)
  1020. {
  1021. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1022. {
  1023. if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
  1024. return ns;
  1025. });
  1026. return NULL;
  1027. }
  1028. /** Return the most recent consensus that we have downloaded, or NULL if we
  1029. * don't have one. */
  1030. networkstatus_vote_t *
  1031. networkstatus_get_latest_consensus(void)
  1032. {
  1033. return current_consensus;
  1034. }
  1035. /** Return the most recent consensus that we have downloaded, or NULL if it is
  1036. * no longer live. */
  1037. networkstatus_vote_t *
  1038. networkstatus_get_live_consensus(time_t now)
  1039. {
  1040. if (current_consensus &&
  1041. current_consensus->valid_after <= now &&
  1042. now <= current_consensus->valid_until)
  1043. return current_consensus;
  1044. else
  1045. return NULL;
  1046. }
  1047. /* XXXX020 remove this in favor of get_live_consensus. */
  1048. networkstatus_vote_t *
  1049. networkstatus_get_reasonably_live_consensus(time_t now)
  1050. {
  1051. #define REASONABLY_LIVE_TIME (24*60*60)
  1052. if (current_consensus &&
  1053. current_consensus->valid_after <= now &&
  1054. now <= current_consensus->valid_until+REASONABLY_LIVE_TIME)
  1055. return current_consensus;
  1056. else
  1057. return NULL;
  1058. }
  1059. /** Copy all the ancillary information (like router download status and so on)
  1060. * from <b>old_c</b> to <b>new_c</b>. */
  1061. static void
  1062. networkstatus_copy_old_consensus_info(networkstatus_vote_t *new_c,
  1063. const networkstatus_vote_t *old_c)
  1064. {
  1065. int idx = 0;
  1066. const routerstatus_t *rs_old;
  1067. if (old_c == new_c)
  1068. return;
  1069. if (!smartlist_len(old_c->routerstatus_list))
  1070. return;
  1071. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  1072. SMARTLIST_FOREACH(new_c->routerstatus_list, routerstatus_t *, rs_new,
  1073. {
  1074. int r;
  1075. while ((r = memcmp(rs_old->identity_digest, rs_new->identity_digest,
  1076. DIGEST_LEN))<0) {
  1077. if (++idx == smartlist_len(old_c->routerstatus_list))
  1078. goto done;
  1079. rs_old = smartlist_get(old_c->routerstatus_list, idx);
  1080. }
  1081. if (r>0)
  1082. continue;
  1083. tor_assert(r==0);
  1084. /* Okay, so we're looking at the same identity. */
  1085. rs_new->name_lookup_warned = rs_old->name_lookup_warned;
  1086. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  1087. if (!memcmp(rs_old->descriptor_digest, rs_new->descriptor_digest,
  1088. DIGEST_LEN)) {
  1089. /* And the same descriptor too! */
  1090. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  1091. }
  1092. });
  1093. done:
  1094. return;
  1095. }
  1096. /** Try to replace the current cached v3 networkstatus with the one in
  1097. * <b>consensus</b>. If we don't have enough certificates to validate it,
  1098. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  1099. *
  1100. * Return 0 on success, -1 on failure. On -1, caller should increment
  1101. * the failure count as appropriate.
  1102. */
  1103. int
  1104. networkstatus_set_current_consensus(const char *consensus, int from_cache,
  1105. int was_waiting_for_certs)
  1106. {
  1107. networkstatus_vote_t *c;
  1108. int r, result=-1;
  1109. time_t now = time(NULL);
  1110. char *unverified_fname = NULL, *consensus_fname = NULL;
  1111. /* Make sure it's parseable. */
  1112. c = networkstatus_parse_vote_from_string(consensus, NULL, 0);
  1113. if (!c) {
  1114. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1115. goto done;
  1116. }
  1117. if (current_consensus &&
  1118. !memcmp(c->networkstatus_digest, current_consensus->networkstatus_digest,
  1119. DIGEST_LEN)) {
  1120. /* We already have this one. That's a failure. */
  1121. log_info(LD_DIR, "Got a consensus we already have");
  1122. goto done;
  1123. }
  1124. if (current_consensus && c->valid_after <= current_consensus->valid_after) {
  1125. /* We have a newer one. There's no point in accepting this one,
  1126. * even if it's great. */
  1127. log_info(LD_DIR, "Got a consensus at least as old as the one we have");
  1128. goto done;
  1129. }
  1130. consensus_fname = get_datadir_fname("cached-consensus");
  1131. unverified_fname = get_datadir_fname("unverified-consensus");
  1132. /* Make sure it's signed enough. */
  1133. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1134. if (r == -1 && !was_waiting_for_certs) {
  1135. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1136. if (!was_waiting_for_certs)
  1137. log_notice(LD_DIR, "Not enough certificates to check networkstatus "
  1138. "consensus");
  1139. if (!current_consensus ||
  1140. c->valid_after > current_consensus->valid_after) {
  1141. if (consensus_waiting_for_certs)
  1142. networkstatus_vote_free(consensus_waiting_for_certs);
  1143. tor_free(consensus_waiting_for_certs_body);
  1144. consensus_waiting_for_certs = c;
  1145. consensus_waiting_for_certs_body = tor_strdup(consensus);
  1146. consensus_waiting_for_certs_set_at = now;
  1147. consensus_waiting_for_certs_dl_failed = 0;
  1148. if (!from_cache) {
  1149. write_str_to_file(unverified_fname, consensus, 0);
  1150. }
  1151. authority_certs_fetch_missing(c, now);
  1152. /* This case is not a success or a failure until we get the certs
  1153. * or fail to get the certs. */
  1154. result = 0;
  1155. } else {
  1156. /* Even if we had enough signatures, we'd never use this as the
  1157. * latest consensus. */
  1158. if (was_waiting_for_certs && from_cache)
  1159. unlink(unverified_fname);
  1160. }
  1161. goto done;
  1162. } else {
  1163. /* This can never be signed enough: Kill it. */
  1164. if (!was_waiting_for_certs)
  1165. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1166. "consensus");
  1167. if (was_waiting_for_certs && (r < -1) && from_cache)
  1168. unlink(unverified_fname);
  1169. goto done;
  1170. }
  1171. }
  1172. /* Are we missing any certificates at all? */
  1173. if (r != 1)
  1174. authority_certs_fetch_missing(c, now);
  1175. if (current_consensus) {
  1176. networkstatus_copy_old_consensus_info(c, current_consensus);
  1177. networkstatus_vote_free(current_consensus);
  1178. }
  1179. if (consensus_waiting_for_certs &&
  1180. consensus_waiting_for_certs->valid_after <= c->valid_after) {
  1181. networkstatus_vote_free(consensus_waiting_for_certs);
  1182. consensus_waiting_for_certs = NULL;
  1183. if (consensus != consensus_waiting_for_certs_body)
  1184. tor_free(consensus_waiting_for_certs_body);
  1185. consensus_waiting_for_certs_set_at = 0;
  1186. consensus_waiting_for_certs_dl_failed = 0;
  1187. unlink(unverified_fname);
  1188. }
  1189. /* Reset the failure count only if this consensus is actually valid. */
  1190. if (c->valid_after <= now && now <= c->valid_until) {
  1191. download_status_reset(&consensus_dl_status);
  1192. } else {
  1193. if (!from_cache)
  1194. download_status_failed(&consensus_dl_status, 0);
  1195. }
  1196. current_consensus = c;
  1197. c = NULL; /* Prevent free. */
  1198. update_consensus_networkstatus_fetch_time(now);
  1199. dirvote_recalculate_timing(get_options(), now);
  1200. routerstatus_list_update_named_server_map();
  1201. if (!from_cache) {
  1202. write_str_to_file(consensus_fname, consensus, 0);
  1203. }
  1204. if (dirserver_mode(get_options()))
  1205. dirserv_set_cached_networkstatus_v3(consensus,
  1206. current_consensus->valid_after);
  1207. if (ftime_definitely_before(now, current_consensus->valid_after)) {
  1208. char tbuf[ISO_TIME_LEN+1];
  1209. char dbuf[64];
  1210. long delta = now - current_consensus->valid_after;
  1211. format_iso_time(tbuf, current_consensus->valid_after);
  1212. format_time_interval(dbuf, sizeof(dbuf), delta);
  1213. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1214. "consensus network status document (%s GMT). Tor needs an "
  1215. "accurate clock to work correctly. Please check your time and "
  1216. "date settings!", dbuf, tbuf);
  1217. control_event_general_status(LOG_WARN,
  1218. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1219. }
  1220. router_dir_info_changed();
  1221. result = 0;
  1222. done:
  1223. if (c)
  1224. networkstatus_vote_free(c);
  1225. tor_free(consensus_fname);
  1226. tor_free(unverified_fname);
  1227. return result;
  1228. }
  1229. /** Called when we have gotten more certificates: see whether we can
  1230. * now verify a pending consensus. */
  1231. void
  1232. networkstatus_note_certs_arrived(void)
  1233. {
  1234. if (consensus_waiting_for_certs) {
  1235. if (networkstatus_check_consensus_signature(
  1236. consensus_waiting_for_certs, 0)<0) {
  1237. if (!networkstatus_set_current_consensus(
  1238. consensus_waiting_for_certs_body, 0, 1)) {
  1239. tor_free(consensus_waiting_for_certs_body);
  1240. }
  1241. }
  1242. }
  1243. }
  1244. /** If the network-status list has changed since the last time we called this
  1245. * function, update the status of every routerinfo from the network-status
  1246. * list.
  1247. */
  1248. void
  1249. routers_update_all_from_networkstatus(time_t now)
  1250. {
  1251. routerinfo_t *me;
  1252. routerlist_t *rl = router_get_routerlist();
  1253. networkstatus_vote_t *consensus = networkstatus_get_live_consensus(now);
  1254. if (networkstatus_v2_list_has_changed)
  1255. download_status_map_update_from_v2_networkstatus();
  1256. if (!consensus)
  1257. return;
  1258. /* More routers may be up or down now: we need to recalc whether there's
  1259. * enough directory info. */
  1260. router_dir_info_changed();
  1261. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1262. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1263. ri->routerlist_index = ri_sl_idx);
  1264. if (rl->old_routers)
  1265. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1266. entry_guards_compute_status();
  1267. me = router_get_my_routerinfo();
  1268. if (me && !have_warned_about_invalid_status) {
  1269. routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
  1270. me->cache_info.identity_digest);
  1271. if (!rs) {
  1272. log_info(LD_GENERAL, "The latest consensus does not list us."
  1273. "Are you misconfigured?");
  1274. have_warned_about_invalid_status = 1;
  1275. } else if (rs->is_unnamed) {
  1276. /* XXXX020 this isn't a useful warning. */
  1277. log_info(LD_GENERAL, "The directory have assigned the nickname "
  1278. "you're using to a different identity.");
  1279. have_warned_about_invalid_status = 1;
  1280. } else if (!rs->is_named) {
  1281. /*XXXX020 this isn't a correct warning. */
  1282. log_info(LD_GENERAL, "The directory authorities do not recognize "
  1283. "your nickname. Please consider sending your "
  1284. "nickname and identity fingerprint to the tor-ops.");
  1285. have_warned_about_invalid_status = 1;
  1286. }
  1287. }
  1288. if (!have_warned_about_old_version) {
  1289. int is_server = server_mode(get_options());
  1290. version_status_t status;
  1291. const char *recommended = is_server ?
  1292. consensus->server_versions : consensus->client_versions;
  1293. status = tor_version_is_obsolete(VERSION, recommended);
  1294. if (status == VS_RECOMMENDED) {
  1295. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1296. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1297. if (!have_warned_about_new_version) {
  1298. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1299. "recommended version%s, according to the directory "
  1300. "authorities. Recommended versions are: %s",
  1301. VERSION,
  1302. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1303. recommended);
  1304. have_warned_about_new_version = 1;
  1305. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1306. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1307. VERSION, "NEW", recommended);
  1308. }
  1309. } else {
  1310. log_warn(LD_GENERAL, "Please upgrade! "
  1311. "This version of Tor (%s) is %s, according to the directory "
  1312. "authorities. Recommended versions are: %s",
  1313. VERSION,
  1314. status == VS_OLD ? "obsolete" : "not recommended",
  1315. recommended);
  1316. have_warned_about_old_version = 1;
  1317. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1318. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1319. VERSION, status == VS_OLD ? "OLD" : "UNRECOMMENDED",
  1320. recommended);
  1321. }
  1322. }
  1323. }
  1324. /** Update v2_download_status_map to contain an entry for every router
  1325. * descriptor listed in the v2 networkstatuses. */
  1326. static void
  1327. download_status_map_update_from_v2_networkstatus(void)
  1328. {
  1329. digestmap_t *dl_status;
  1330. if (!networkstatus_v2_list)
  1331. return;
  1332. if (!v2_download_status_map)
  1333. v2_download_status_map = digestmap_new();
  1334. dl_status = digestmap_new();
  1335. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1336. {
  1337. SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
  1338. {
  1339. const char *d = rs->descriptor_digest;
  1340. download_status_t *s;
  1341. if (digestmap_get(dl_status, d))
  1342. continue;
  1343. if (!(s = digestmap_remove(v2_download_status_map, d))) {
  1344. s = tor_malloc_zero(sizeof(download_status_t));
  1345. }
  1346. digestmap_set(dl_status, d, s);
  1347. });
  1348. });
  1349. digestmap_free(v2_download_status_map, _tor_free);
  1350. v2_download_status_map = dl_status;
  1351. networkstatus_v2_list_has_changed = 0;
  1352. }
  1353. /** Update our view of the list of named servers from the most recently
  1354. * retrieved networkstatus consensus. */
  1355. static void
  1356. routerstatus_list_update_named_server_map(void)
  1357. {
  1358. if (!current_consensus)
  1359. return;
  1360. if (named_server_map)
  1361. strmap_free(named_server_map, _tor_free);
  1362. named_server_map = strmap_new();
  1363. if (unnamed_server_map)
  1364. strmap_free(unnamed_server_map, NULL);
  1365. unnamed_server_map = strmap_new();
  1366. SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
  1367. {
  1368. if (rs->is_named) {
  1369. strmap_set_lc(named_server_map, rs->nickname,
  1370. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1371. }
  1372. if (rs->is_unnamed) {
  1373. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1374. }
  1375. });
  1376. }
  1377. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1378. * according to our current consensus networkstatus. May re-order
  1379. * <b>routers</b>. */
  1380. void
  1381. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1382. int reset_failures)
  1383. {
  1384. trusted_dir_server_t *ds;
  1385. routerstatus_t *rs;
  1386. or_options_t *options = get_options();
  1387. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  1388. int namingdir = authdir && options->NamingAuthoritativeDir;
  1389. networkstatus_vote_t *ns = current_consensus;
  1390. int idx;
  1391. if (!ns || !smartlist_len(ns->routerstatus_list))
  1392. return;
  1393. routers_sort_by_identity(routers);
  1394. /* Now routers and ns->routerstatus_list are both in ascending order
  1395. * of identity digest. */
  1396. idx = 0;
  1397. rs = smartlist_get(ns->routerstatus_list, idx);
  1398. SMARTLIST_FOREACH(routers, routerinfo_t *, router,
  1399. {
  1400. const char *digest = router->cache_info.identity_digest;
  1401. int r;
  1402. while ((r = memcmp(rs->identity_digest, digest, DIGEST_LEN))<0) {
  1403. if (++idx == smartlist_len(ns->routerstatus_list)) {
  1404. /* We're out of routerstatuses. Bail. */
  1405. goto done;
  1406. }
  1407. rs = smartlist_get(ns->routerstatus_list, idx);
  1408. }
  1409. if (r>0) {
  1410. /* We have no routerstatus for this router. Clear flags and skip it. */
  1411. if (!namingdir)
  1412. router->is_named = 0;
  1413. if (!authdir) {
  1414. if (router->purpose == ROUTER_PURPOSE_GENERAL) {
  1415. router->is_valid = router->is_running =
  1416. router->is_fast = router->is_stable =
  1417. router->is_possible_guard = router->is_exit =
  1418. router->is_bad_exit = 0;
  1419. }
  1420. }
  1421. continue;
  1422. }
  1423. tor_assert(r==0);
  1424. ds = router_get_trusteddirserver_by_digest(digest);
  1425. if (!namingdir) {
  1426. if (rs->is_named && !strcasecmp(router->nickname, rs->nickname))
  1427. router->is_named = 1;
  1428. else
  1429. router->is_named = 0;
  1430. }
  1431. if (!memcmp(router->cache_info.signed_descriptor_digest,
  1432. rs->descriptor_digest, DIGEST_LEN)) {
  1433. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1434. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1435. }
  1436. if (!authdir) {
  1437. /* If we're not an authdir, believe others. */
  1438. router->is_valid = rs->is_valid;
  1439. router->is_running = rs->is_running;
  1440. router->is_fast = rs->is_fast;
  1441. router->is_stable = rs->is_stable;
  1442. router->is_possible_guard = rs->is_possible_guard;
  1443. router->is_exit = rs->is_exit;
  1444. router->is_bad_exit = rs->is_bad_exit;
  1445. router->is_hs_dir = rs->is_hs_dir;
  1446. }
  1447. if (router->is_running && ds) {
  1448. download_status_reset(&ds->v2_ns_dl_status);
  1449. }
  1450. if (reset_failures) {
  1451. download_status_reset(&rs->dl_status);
  1452. }
  1453. });
  1454. done:
  1455. router_dir_info_changed();
  1456. }
  1457. /**DOCDOC*/
  1458. void
  1459. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1460. {
  1461. networkstatus_vote_t *ns = current_consensus;
  1462. if (!ns)
  1463. return;
  1464. if (!ns->desc_digest_map) {
  1465. char dummy[DIGEST_LEN];
  1466. /* instantiates the digest map. */
  1467. memset(dummy, 0, sizeof(dummy));
  1468. router_get_consensus_status_by_descriptor_digest(dummy);
  1469. }
  1470. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1471. {
  1472. routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1473. d->signed_descriptor_digest);
  1474. if (rs) {
  1475. if (ns->valid_until > d->last_listed_as_valid_until)
  1476. d->last_listed_as_valid_until = ns->valid_until;
  1477. }
  1478. });
  1479. }
  1480. /** Generate networkstatus lines for a single routerstatus_t object, and
  1481. * return the result in a newly allocated string. Used only by controller
  1482. * interface (for now.) */
  1483. char *
  1484. networkstatus_getinfo_helper_single(routerstatus_t *rs)
  1485. {
  1486. char buf[256];
  1487. routerstatus_format_entry(buf, sizeof(buf), rs, NULL, 0);
  1488. return tor_strdup(buf);
  1489. }
  1490. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1491. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1492. * newly-allocated string containing networkstatus lines for the appropriate
  1493. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1494. int
  1495. getinfo_helper_networkstatus(control_connection_t *conn,
  1496. const char *question, char **answer)
  1497. {
  1498. routerstatus_t *status;
  1499. (void) conn;
  1500. if (!current_consensus) {
  1501. *answer = tor_strdup("");
  1502. return 0;
  1503. }
  1504. if (!strcmp(question, "ns/all")) {
  1505. smartlist_t *statuses = smartlist_create();
  1506. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1507. routerstatus_t *, rs,
  1508. {
  1509. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1510. });
  1511. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1512. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1513. smartlist_free(statuses);
  1514. return 0;
  1515. } else if (!strcmpstart(question, "ns/id/")) {
  1516. char d[DIGEST_LEN];
  1517. if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6)))
  1518. return -1;
  1519. status = router_get_consensus_status_by_id(d);
  1520. } else if (!strcmpstart(question, "ns/name/")) {
  1521. status = router_get_consensus_status_by_nickname(question+8, 0);
  1522. } else {
  1523. return -1;
  1524. }
  1525. if (status) {
  1526. *answer = networkstatus_getinfo_helper_single(status);
  1527. }
  1528. return 0;
  1529. }
  1530. /** Free all storage held locally in this module. */
  1531. void
  1532. networkstatus_free_all(void)
  1533. {
  1534. if (networkstatus_v2_list) {
  1535. SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  1536. networkstatus_v2_free(ns));
  1537. smartlist_free(networkstatus_v2_list);
  1538. networkstatus_v2_list = NULL;
  1539. }
  1540. if (v2_download_status_map) {
  1541. digestmap_free(v2_download_status_map, _tor_free);
  1542. v2_download_status_map = NULL;
  1543. }
  1544. if (current_consensus) {
  1545. networkstatus_vote_free(current_consensus);
  1546. current_consensus = NULL;
  1547. }
  1548. if (consensus_waiting_for_certs) {
  1549. networkstatus_vote_free(current_consensus);
  1550. current_consensus = NULL;
  1551. }
  1552. tor_free(consensus_waiting_for_certs_body);
  1553. if (named_server_map) {
  1554. strmap_free(named_server_map, _tor_free);
  1555. }
  1556. if (unnamed_server_map) {
  1557. strmap_free(unnamed_server_map, NULL);
  1558. }
  1559. }