networkstatus.c 45 KB

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