nodelist.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "address.h"
  8. #include "config.h"
  9. #include "control.h"
  10. #include "dirserv.h"
  11. #include "geoip.h"
  12. #include "main.h"
  13. #include "microdesc.h"
  14. #include "networkstatus.h"
  15. #include "nodelist.h"
  16. #include "policies.h"
  17. #include "rendservice.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "routerset.h"
  21. #include <string.h>
  22. static void nodelist_drop_node(node_t *node, int remove_from_ht);
  23. static void node_free(node_t *node);
  24. static void update_router_have_minimum_dir_info(void);
  25. /** A nodelist_t holds a node_t object for every router we're "willing to use
  26. * for something". Specifically, it should hold a node_t for every node that
  27. * is currently in the routerlist, or currently in the consensus we're using.
  28. */
  29. typedef struct nodelist_t {
  30. /* A list of all the nodes. */
  31. smartlist_t *nodes;
  32. /* Hash table to map from node ID digest to node. */
  33. HT_HEAD(nodelist_map, node_t) nodes_by_id;
  34. } nodelist_t;
  35. static INLINE unsigned int
  36. node_id_hash(const node_t *node)
  37. {
  38. #if SIZEOF_INT == 4
  39. const uint32_t *p = (const uint32_t*)node->identity;
  40. return p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4];
  41. #elif SIZEOF_INT == 8
  42. const uint64_t *p = (const uint32_t*)node->identity;
  43. const uint32_t *p32 = (const uint32_t*)node->identity;
  44. return p[0] ^ p[1] ^ p32[4];
  45. #endif
  46. }
  47. static INLINE unsigned int
  48. node_id_eq(const node_t *node1, const node_t *node2)
  49. {
  50. return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
  51. }
  52. HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
  53. HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
  54. 0.6, malloc, realloc, free);
  55. /** The global nodelist. */
  56. static nodelist_t *the_nodelist=NULL;
  57. /** Create an empty nodelist if we haven't done so already. */
  58. static void
  59. init_nodelist(void)
  60. {
  61. if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
  62. the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
  63. HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
  64. the_nodelist->nodes = smartlist_new();
  65. }
  66. }
  67. /** As node_get_by_id, but returns a non-const pointer */
  68. node_t *
  69. node_get_mutable_by_id(const char *identity_digest)
  70. {
  71. node_t search, *node;
  72. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  73. return NULL;
  74. memcpy(&search.identity, identity_digest, DIGEST_LEN);
  75. node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
  76. return node;
  77. }
  78. /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
  79. * if no such node exists. */
  80. const node_t *
  81. node_get_by_id(const char *identity_digest)
  82. {
  83. return node_get_mutable_by_id(identity_digest);
  84. }
  85. /** Internal: return the node_t whose identity_digest is
  86. * <b>identity_digest</b>. If none exists, create a new one, add it to the
  87. * nodelist, and return it.
  88. *
  89. * Requires that the nodelist be initialized.
  90. */
  91. static node_t *
  92. node_get_or_create(const char *identity_digest)
  93. {
  94. node_t *node;
  95. if ((node = node_get_mutable_by_id(identity_digest)))
  96. return node;
  97. node = tor_malloc_zero(sizeof(node_t));
  98. memcpy(node->identity, identity_digest, DIGEST_LEN);
  99. HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
  100. smartlist_add(the_nodelist->nodes, node);
  101. node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
  102. node->country = -1;
  103. return node;
  104. }
  105. /** Called when a node's address changes. */
  106. static void
  107. node_addrs_changed(node_t *node)
  108. {
  109. node->last_reachable = node->last_reachable6 = 0;
  110. node->country = -1;
  111. }
  112. /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
  113. * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
  114. * to the previous routerinfo.
  115. */
  116. node_t *
  117. nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
  118. {
  119. node_t *node;
  120. const char *id_digest;
  121. int had_router = 0;
  122. tor_assert(ri);
  123. init_nodelist();
  124. id_digest = ri->cache_info.identity_digest;
  125. node = node_get_or_create(id_digest);
  126. if (node->ri) {
  127. if (!routers_have_same_or_addrs(node->ri, ri)) {
  128. node_addrs_changed(node);
  129. }
  130. had_router = 1;
  131. if (ri_old_out)
  132. *ri_old_out = node->ri;
  133. } else {
  134. if (ri_old_out)
  135. *ri_old_out = NULL;
  136. }
  137. node->ri = ri;
  138. if (node->country == -1)
  139. node_set_country(node);
  140. if (authdir_mode(get_options()) && !had_router) {
  141. const char *discard=NULL;
  142. uint32_t status = dirserv_router_get_status(ri, &discard);
  143. dirserv_set_node_flags_from_authoritative_status(node, status);
  144. }
  145. return node;
  146. }
  147. /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
  148. *
  149. * Called when a new microdesc has arrived and the usable consensus flavor
  150. * is "microdesc".
  151. **/
  152. node_t *
  153. nodelist_add_microdesc(microdesc_t *md)
  154. {
  155. networkstatus_t *ns =
  156. networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  157. const routerstatus_t *rs;
  158. node_t *node;
  159. if (ns == NULL)
  160. return NULL;
  161. init_nodelist();
  162. /* Microdescriptors don't carry an identity digest, so we need to figure
  163. * it out by looking up the routerstatus. */
  164. rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
  165. if (rs == NULL)
  166. return NULL;
  167. node = node_get_mutable_by_id(rs->identity_digest);
  168. if (node) {
  169. if (node->md)
  170. node->md->held_by_nodes--;
  171. node->md = md;
  172. md->held_by_nodes++;
  173. }
  174. return node;
  175. }
  176. /** Tell the nodelist that the current usable consensus is <b>ns</b>.
  177. * This makes the nodelist change all of the routerstatus entries for
  178. * the nodes, drop nodes that no longer have enough info to get used,
  179. * and grab microdescriptors into nodes as appropriate.
  180. */
  181. void
  182. nodelist_set_consensus(networkstatus_t *ns)
  183. {
  184. const or_options_t *options = get_options();
  185. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  186. int client = !server_mode(options);
  187. init_nodelist();
  188. if (ns->flavor == FLAV_MICRODESC)
  189. (void) get_microdesc_cache(); /* Make sure it exists first. */
  190. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  191. node->rs = NULL);
  192. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  193. node_t *node = node_get_or_create(rs->identity_digest);
  194. node->rs = rs;
  195. if (ns->flavor == FLAV_MICRODESC) {
  196. if (node->md == NULL ||
  197. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  198. if (node->md)
  199. node->md->held_by_nodes--;
  200. node->md = microdesc_cache_lookup_by_digest256(NULL,
  201. rs->descriptor_digest);
  202. if (node->md)
  203. node->md->held_by_nodes++;
  204. }
  205. }
  206. node_set_country(node);
  207. /* If we're not an authdir, believe others. */
  208. if (!authdir) {
  209. node->is_valid = rs->is_valid;
  210. node->is_running = rs->is_flagged_running;
  211. node->is_fast = rs->is_fast;
  212. node->is_stable = rs->is_stable;
  213. node->is_possible_guard = rs->is_possible_guard;
  214. node->is_exit = rs->is_exit;
  215. node->is_bad_directory = rs->is_bad_directory;
  216. node->is_bad_exit = rs->is_bad_exit;
  217. node->is_hs_dir = rs->is_hs_dir;
  218. node->ipv6_preferred = 0;
  219. if (client && options->ClientPreferIPv6ORPort == 1 &&
  220. (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
  221. (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
  222. node->ipv6_preferred = 1;
  223. }
  224. } SMARTLIST_FOREACH_END(rs);
  225. nodelist_purge();
  226. if (! authdir) {
  227. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  228. /* We have no routerstatus for this router. Clear flags so we can skip
  229. * it, maybe.*/
  230. if (!node->rs) {
  231. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  232. * would have removed it. */
  233. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  234. /* Clear all flags. */
  235. node->is_valid = node->is_running = node->is_hs_dir =
  236. node->is_fast = node->is_stable =
  237. node->is_possible_guard = node->is_exit =
  238. node->is_bad_exit = node->is_bad_directory =
  239. node->ipv6_preferred = 0;
  240. }
  241. }
  242. } SMARTLIST_FOREACH_END(node);
  243. }
  244. }
  245. /** Helper: return true iff a node has a usable amount of information*/
  246. static INLINE int
  247. node_is_usable(const node_t *node)
  248. {
  249. return (node->rs) || (node->ri);
  250. }
  251. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  252. * node with <b>identity_digest</b>. */
  253. void
  254. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  255. {
  256. node_t *node = node_get_mutable_by_id(identity_digest);
  257. if (node && node->md == md) {
  258. node->md = NULL;
  259. md->held_by_nodes--;
  260. }
  261. }
  262. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  263. void
  264. nodelist_remove_routerinfo(routerinfo_t *ri)
  265. {
  266. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  267. if (node && node->ri == ri) {
  268. node->ri = NULL;
  269. if (! node_is_usable(node)) {
  270. nodelist_drop_node(node, 1);
  271. node_free(node);
  272. }
  273. }
  274. }
  275. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  276. * with.) */
  277. static void
  278. nodelist_drop_node(node_t *node, int remove_from_ht)
  279. {
  280. node_t *tmp;
  281. int idx;
  282. if (remove_from_ht) {
  283. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  284. tor_assert(tmp == node);
  285. }
  286. idx = node->nodelist_idx;
  287. tor_assert(idx >= 0);
  288. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  289. smartlist_del(the_nodelist->nodes, idx);
  290. if (idx < smartlist_len(the_nodelist->nodes)) {
  291. tmp = smartlist_get(the_nodelist->nodes, idx);
  292. tmp->nodelist_idx = idx;
  293. }
  294. node->nodelist_idx = -1;
  295. }
  296. /** Release storage held by <b>node</b> */
  297. static void
  298. node_free(node_t *node)
  299. {
  300. if (!node)
  301. return;
  302. if (node->md)
  303. node->md->held_by_nodes--;
  304. tor_assert(node->nodelist_idx == -1);
  305. tor_free(node);
  306. }
  307. /** Remove all entries from the nodelist that don't have enough info to be
  308. * usable for anything. */
  309. void
  310. nodelist_purge(void)
  311. {
  312. node_t **iter;
  313. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  314. return;
  315. /* Remove the non-usable nodes. */
  316. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  317. node_t *node = *iter;
  318. if (node->md && !node->rs) {
  319. /* An md is only useful if there is an rs. */
  320. node->md->held_by_nodes--;
  321. node->md = NULL;
  322. }
  323. if (node_is_usable(node)) {
  324. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  325. } else {
  326. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  327. nodelist_drop_node(node, 0);
  328. node_free(node);
  329. }
  330. }
  331. nodelist_assert_ok();
  332. }
  333. /** Release all storage held by the nodelist. */
  334. void
  335. nodelist_free_all(void)
  336. {
  337. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  338. return;
  339. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  340. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  341. node->nodelist_idx = -1;
  342. node_free(node);
  343. } SMARTLIST_FOREACH_END(node);
  344. smartlist_free(the_nodelist->nodes);
  345. tor_free(the_nodelist);
  346. }
  347. /** Check that the nodelist is internally consistent, and consistent with
  348. * the directory info it's derived from.
  349. */
  350. void
  351. nodelist_assert_ok(void)
  352. {
  353. routerlist_t *rl = router_get_routerlist();
  354. networkstatus_t *ns = networkstatus_get_latest_consensus();
  355. digestmap_t *dm;
  356. if (!the_nodelist)
  357. return;
  358. dm = digestmap_new();
  359. /* every routerinfo in rl->routers should be in the nodelist. */
  360. if (rl) {
  361. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  362. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  363. tor_assert(node && node->ri == ri);
  364. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  365. node->identity, DIGEST_LEN));
  366. tor_assert(! digestmap_get(dm, node->identity));
  367. digestmap_set(dm, node->identity, (void*)node);
  368. } SMARTLIST_FOREACH_END(ri);
  369. }
  370. /* every routerstatus in ns should be in the nodelist */
  371. if (ns) {
  372. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  373. const node_t *node = node_get_by_id(rs->identity_digest);
  374. tor_assert(node && node->rs == rs);
  375. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  376. digestmap_set(dm, node->identity, (void*)node);
  377. if (ns->flavor == FLAV_MICRODESC) {
  378. /* If it's a microdesc consensus, every entry that has a
  379. * microdescriptor should be in the nodelist.
  380. */
  381. microdesc_t *md =
  382. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  383. tor_assert(md == node->md);
  384. if (md)
  385. tor_assert(md->held_by_nodes >= 1);
  386. }
  387. } SMARTLIST_FOREACH_END(rs);
  388. }
  389. /* The nodelist should have no other entries, and its entries should be
  390. * well-formed. */
  391. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  392. tor_assert(digestmap_get(dm, node->identity) != NULL);
  393. tor_assert(node_sl_idx == node->nodelist_idx);
  394. } SMARTLIST_FOREACH_END(node);
  395. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  396. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  397. digestmap_free(dm, NULL);
  398. }
  399. /** Return a list of a node_t * for every node we know about. The caller
  400. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  401. * you must, but you must not add or remove nodes.) */
  402. smartlist_t *
  403. nodelist_get_list(void)
  404. {
  405. init_nodelist();
  406. return the_nodelist->nodes;
  407. }
  408. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  409. * or $DIGEST~name, return the node with the matching identity digest and
  410. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  411. * is not well-formed. */
  412. const node_t *
  413. node_get_by_hex_id(const char *hex_id)
  414. {
  415. char digest_buf[DIGEST_LEN];
  416. char nn_buf[MAX_NICKNAME_LEN+1];
  417. char nn_char='\0';
  418. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  419. const node_t *node = node_get_by_id(digest_buf);
  420. if (!node)
  421. return NULL;
  422. if (nn_char) {
  423. const char *real_name = node_get_nickname(node);
  424. if (!real_name || strcasecmp(real_name, nn_buf))
  425. return NULL;
  426. if (nn_char == '=') {
  427. const char *named_id =
  428. networkstatus_get_router_digest_by_nickname(nn_buf);
  429. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  430. return NULL;
  431. }
  432. }
  433. return node;
  434. }
  435. return NULL;
  436. }
  437. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  438. * the corresponding node_t, or NULL if none exists. Warn the user if
  439. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  440. * nickname, but the Named flag isn't set for that router. */
  441. const node_t *
  442. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  443. {
  444. const node_t *node;
  445. if (!the_nodelist)
  446. return NULL;
  447. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  448. if ((node = node_get_by_hex_id(nickname)) != NULL)
  449. return node;
  450. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  451. return NULL;
  452. /* Okay, so if we get here, the nickname is just a nickname. Is there
  453. * a binding for it in the consensus? */
  454. {
  455. const char *named_id =
  456. networkstatus_get_router_digest_by_nickname(nickname);
  457. if (named_id)
  458. return node_get_by_id(named_id);
  459. }
  460. /* Is it marked as owned-by-someone-else? */
  461. if (networkstatus_nickname_is_unnamed(nickname)) {
  462. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  463. "router that holds it, but not one listed in the current "
  464. "consensus.", escaped(nickname));
  465. return NULL;
  466. }
  467. /* Okay, so the name is not canonical for anybody. */
  468. {
  469. smartlist_t *matches = smartlist_new();
  470. const node_t *choice = NULL;
  471. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  472. if (!strcasecmp(node_get_nickname(node), nickname))
  473. smartlist_add(matches, node);
  474. } SMARTLIST_FOREACH_END(node);
  475. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  476. int any_unwarned = 0;
  477. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  478. if (!node->name_lookup_warned) {
  479. node->name_lookup_warned = 1;
  480. any_unwarned = 1;
  481. }
  482. } SMARTLIST_FOREACH_END(node);
  483. if (any_unwarned) {
  484. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  485. "but none is listed as Named in the directory consensus. "
  486. "Choosing one arbitrarily.", nickname);
  487. }
  488. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  489. char fp[HEX_DIGEST_LEN+1];
  490. node_t *node = smartlist_get(matches, 0);
  491. if (node->name_lookup_warned) {
  492. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  493. log_warn(LD_CONFIG,
  494. "You specified a server \"%s\" by name, but the directory "
  495. "authorities do not have any key registered for this "
  496. "nickname -- so it could be used by any server, not just "
  497. "the one you meant. "
  498. "To make sure you get the same server in the future, refer "
  499. "to it by key, as \"$%s\".", nickname, fp);
  500. node->name_lookup_warned = 1;
  501. }
  502. }
  503. if (smartlist_len(matches))
  504. choice = smartlist_get(matches, 0);
  505. smartlist_free(matches);
  506. return choice;
  507. }
  508. }
  509. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  510. const char *
  511. node_get_nickname(const node_t *node)
  512. {
  513. tor_assert(node);
  514. if (node->rs)
  515. return node->rs->nickname;
  516. else if (node->ri)
  517. return node->ri->nickname;
  518. else
  519. return NULL;
  520. }
  521. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  522. * latest consensus. */
  523. int
  524. node_is_named(const node_t *node)
  525. {
  526. const char *named_id;
  527. const char *nickname = node_get_nickname(node);
  528. if (!nickname)
  529. return 0;
  530. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  531. if (!named_id)
  532. return 0;
  533. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  534. }
  535. /** Return true iff <b>node</b> appears to be a directory authority or
  536. * directory cache */
  537. int
  538. node_is_dir(const node_t *node)
  539. {
  540. if (node->rs)
  541. return node->rs->dir_port != 0;
  542. else if (node->ri)
  543. return node->ri->dir_port != 0;
  544. else
  545. return 0;
  546. }
  547. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  548. * is, a routerdescriptor or a microdescriptor. */
  549. int
  550. node_has_descriptor(const node_t *node)
  551. {
  552. return (node->ri ||
  553. (node->rs && node->md));
  554. }
  555. /** Return the router_purpose of <b>node</b>. */
  556. int
  557. node_get_purpose(const node_t *node)
  558. {
  559. if (node->ri)
  560. return node->ri->purpose;
  561. else
  562. return ROUTER_PURPOSE_GENERAL;
  563. }
  564. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  565. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  566. * <b>verbose_nickname_out</b> */
  567. void
  568. node_get_verbose_nickname(const node_t *node,
  569. char *verbose_name_out)
  570. {
  571. const char *nickname = node_get_nickname(node);
  572. int is_named = node_is_named(node);
  573. verbose_name_out[0] = '$';
  574. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  575. DIGEST_LEN);
  576. if (!nickname)
  577. return;
  578. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  579. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  580. }
  581. /** Return true iff it seems that <b>node</b> allows circuits to exit
  582. * through it directlry from the client. */
  583. int
  584. node_allows_single_hop_exits(const node_t *node)
  585. {
  586. if (node && node->ri)
  587. return node->ri->allow_single_hop_exits;
  588. else
  589. return 0;
  590. }
  591. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  592. * actually permit anything to exit, or we don't know its exit policy */
  593. int
  594. node_exit_policy_rejects_all(const node_t *node)
  595. {
  596. if (node->rejects_all)
  597. return 1;
  598. if (node->ri)
  599. return node->ri->policy_is_reject_star;
  600. else if (node->md)
  601. return node->md->exit_policy == NULL ||
  602. short_policy_is_reject_star(node->md->exit_policy);
  603. else
  604. return 1;
  605. }
  606. /** Return true iff the exit policy for <b>node</b> is such that we can treat
  607. * rejecting an address of type <b>family</b> unexpectedly as a sign of that
  608. * node's failure. */
  609. int
  610. node_exit_policy_is_exact(const node_t *node, sa_family_t family)
  611. {
  612. if (family == AF_UNSPEC) {
  613. return 1; /* Rejecting an address but not telling us what address
  614. * is a bad sign. */
  615. } else if (family == AF_INET) {
  616. return node->ri != NULL;
  617. } else if (family == AF_INET6) {
  618. return 0;
  619. }
  620. tor_fragile_assert();
  621. return 1;
  622. }
  623. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  624. * addr + TCP port) for <b>node</b>. Caller must free all elements
  625. * using tor_free() and free the list using smartlist_free().
  626. *
  627. * XXX this is potentially a memory fragmentation hog -- if on
  628. * critical path consider the option of having the caller allocate the
  629. * memory
  630. */
  631. smartlist_t *
  632. node_get_all_orports(const node_t *node)
  633. {
  634. smartlist_t *sl = smartlist_new();
  635. if (node->ri != NULL) {
  636. if (node->ri->addr != 0) {
  637. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  638. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  639. ap->port = node->ri->or_port;
  640. smartlist_add(sl, ap);
  641. }
  642. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  643. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  644. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  645. ap->port = node->ri->or_port;
  646. smartlist_add(sl, ap);
  647. }
  648. } else if (node->rs != NULL) {
  649. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  650. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  651. ap->port = node->rs->or_port;
  652. smartlist_add(sl, ap);
  653. }
  654. return sl;
  655. }
  656. /** Wrapper around node_get_prim_orport for backward
  657. compatibility. */
  658. void
  659. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  660. {
  661. tor_addr_port_t ap;
  662. node_get_prim_orport(node, &ap);
  663. tor_addr_copy(addr_out, &ap.addr);
  664. }
  665. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  666. * seem to have one. */
  667. uint32_t
  668. node_get_prim_addr_ipv4h(const node_t *node)
  669. {
  670. if (node->ri) {
  671. return node->ri->addr;
  672. } else if (node->rs) {
  673. return node->rs->addr;
  674. }
  675. return 0;
  676. }
  677. /** Copy a string representation of an IP address for <b>node</b> into
  678. * the <b>len</b>-byte buffer at <b>buf</b>. */
  679. void
  680. node_get_address_string(const node_t *node, char *buf, size_t len)
  681. {
  682. if (node->ri) {
  683. strlcpy(buf, node->ri->address, len);
  684. } else if (node->rs) {
  685. tor_addr_t addr;
  686. tor_addr_from_ipv4h(&addr, node->rs->addr);
  687. tor_addr_to_str(buf, &addr, len, 0);
  688. } else {
  689. buf[0] = '\0';
  690. }
  691. }
  692. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  693. * one. */
  694. long
  695. node_get_declared_uptime(const node_t *node)
  696. {
  697. if (node->ri)
  698. return node->ri->uptime;
  699. else
  700. return -1;
  701. }
  702. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  703. const char *
  704. node_get_platform(const node_t *node)
  705. {
  706. /* If we wanted, we could record the version in the routerstatus_t, since
  707. * the consensus lists it. We don't, though, so this function just won't
  708. * work with microdescriptors. */
  709. if (node->ri)
  710. return node->ri->platform;
  711. else
  712. return NULL;
  713. }
  714. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  715. time_t
  716. node_get_published_on(const node_t *node)
  717. {
  718. if (node->ri)
  719. return node->ri->cache_info.published_on;
  720. else
  721. return 0;
  722. }
  723. /** Return true iff <b>node</b> is one representing this router. */
  724. int
  725. node_is_me(const node_t *node)
  726. {
  727. return router_digest_is_me(node->identity);
  728. }
  729. /** Return <b>node</b> declared family (as a list of names), or NULL if
  730. * the node didn't declare a family. */
  731. const smartlist_t *
  732. node_get_declared_family(const node_t *node)
  733. {
  734. if (node->ri && node->ri->declared_family)
  735. return node->ri->declared_family;
  736. else if (node->md && node->md->family)
  737. return node->md->family;
  738. else
  739. return NULL;
  740. }
  741. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  742. * <b>node</b>, else 0.
  743. *
  744. * We prefer the IPv6 address if the router has an IPv6 address and
  745. * i) the node_t says that it prefers IPv6
  746. * or
  747. * ii) the router has no IPv4 address. */
  748. int
  749. node_ipv6_preferred(const node_t *node)
  750. {
  751. tor_addr_port_t ipv4_addr;
  752. node_assert_ok(node);
  753. if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  754. if (node->ri)
  755. return !tor_addr_is_null(&node->ri->ipv6_addr);
  756. if (node->md)
  757. return !tor_addr_is_null(&node->md->ipv6_addr);
  758. if (node->rs)
  759. return !tor_addr_is_null(&node->rs->ipv6_addr);
  760. }
  761. return 0;
  762. }
  763. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  764. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  765. * port was copied, else return non-zero.*/
  766. int
  767. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  768. {
  769. node_assert_ok(node);
  770. tor_assert(ap_out);
  771. if (node->ri) {
  772. if (node->ri->addr == 0 || node->ri->or_port == 0)
  773. return -1;
  774. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  775. ap_out->port = node->ri->or_port;
  776. return 0;
  777. }
  778. if (node->rs) {
  779. if (node->rs->addr == 0 || node->rs->or_port == 0)
  780. return -1;
  781. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  782. ap_out->port = node->rs->or_port;
  783. return 0;
  784. }
  785. return -1;
  786. }
  787. /** Copy the preferred OR port (IP address and TCP port) for
  788. * <b>node</b> into *<b>ap_out</b>. */
  789. void
  790. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  791. {
  792. const or_options_t *options = get_options();
  793. tor_assert(ap_out);
  794. /* Cheap implementation of config option ClientUseIPv6 -- simply
  795. don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
  796. client running with bridges. See #4455 for more on this subject.
  797. Note that this filter is too strict since we're hindering not
  798. only clients! Erring on the safe side shouldn't be a problem
  799. though. XXX move this check to where outgoing connections are
  800. made? -LN */
  801. if ((options->ClientUseIPv6 || options->UseBridges) &&
  802. node_ipv6_preferred(node)) {
  803. node_get_pref_ipv6_orport(node, ap_out);
  804. } else {
  805. node_get_prim_orport(node, ap_out);
  806. }
  807. }
  808. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  809. * <b>node</b> into *<b>ap_out</b>. */
  810. void
  811. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  812. {
  813. node_assert_ok(node);
  814. tor_assert(ap_out);
  815. /* We prefer the microdesc over a potential routerstatus here. They
  816. are not being synchronised atm so there might be a chance that
  817. they differ at some point, f.ex. when flipping
  818. UseMicrodescriptors? -LN */
  819. if (node->ri) {
  820. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  821. ap_out->port = node->ri->ipv6_orport;
  822. } else if (node->md) {
  823. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  824. ap_out->port = node->md->ipv6_orport;
  825. } else if (node->rs) {
  826. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  827. ap_out->port = node->rs->ipv6_orport;
  828. }
  829. }
  830. /** Return true iff <b>node</b> has a curve25519 onion key. */
  831. int
  832. node_has_curve25519_onion_key(const node_t *node)
  833. {
  834. if (node->ri)
  835. return node->ri->onion_curve25519_pkey != NULL;
  836. else if (node->md)
  837. return node->md->onion_curve25519_pkey != NULL;
  838. else
  839. return 0;
  840. }
  841. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  842. * each router when the GeoIP database is reloaded, and on all new routers. */
  843. void
  844. node_set_country(node_t *node)
  845. {
  846. tor_addr_t addr = TOR_ADDR_NULL;
  847. /* XXXXipv6 */
  848. if (node->rs)
  849. tor_addr_from_ipv4h(&addr, node->rs->addr);
  850. else if (node->ri)
  851. tor_addr_from_ipv4h(&addr, node->ri->addr);
  852. node->country = geoip_get_country_by_addr(&addr);
  853. }
  854. /** Set the country code of all routers in the routerlist. */
  855. void
  856. nodelist_refresh_countries(void)
  857. {
  858. smartlist_t *nodes = nodelist_get_list();
  859. SMARTLIST_FOREACH(nodes, node_t *, node,
  860. node_set_country(node));
  861. }
  862. /** Return true iff router1 and router2 have similar enough network addresses
  863. * that we should treat them as being in the same family */
  864. static INLINE int
  865. addrs_in_same_network_family(const tor_addr_t *a1,
  866. const tor_addr_t *a2)
  867. {
  868. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  869. }
  870. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  871. * (case-insensitive), or if <b>node's</b> identity key digest
  872. * matches a hexadecimal value stored in <b>nickname</b>. Return
  873. * false otherwise. */
  874. static int
  875. node_nickname_matches(const node_t *node, const char *nickname)
  876. {
  877. const char *n = node_get_nickname(node);
  878. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  879. return 1;
  880. return hex_digest_nickname_matches(nickname,
  881. node->identity,
  882. n,
  883. node_is_named(node));
  884. }
  885. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  886. static INLINE int
  887. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  888. {
  889. if (!lst) return 0;
  890. SMARTLIST_FOREACH(lst, const char *, name, {
  891. if (node_nickname_matches(node, name))
  892. return 1;
  893. });
  894. return 0;
  895. }
  896. /** Return true iff r1 and r2 are in the same family, but not the same
  897. * router. */
  898. int
  899. nodes_in_same_family(const node_t *node1, const node_t *node2)
  900. {
  901. const or_options_t *options = get_options();
  902. /* Are they in the same family because of their addresses? */
  903. if (options->EnforceDistinctSubnets) {
  904. tor_addr_t a1, a2;
  905. node_get_addr(node1, &a1);
  906. node_get_addr(node2, &a2);
  907. if (addrs_in_same_network_family(&a1, &a2))
  908. return 1;
  909. }
  910. /* Are they in the same family because the agree they are? */
  911. {
  912. const smartlist_t *f1, *f2;
  913. f1 = node_get_declared_family(node1);
  914. f2 = node_get_declared_family(node2);
  915. if (f1 && f2 &&
  916. node_in_nickname_smartlist(f1, node2) &&
  917. node_in_nickname_smartlist(f2, node1))
  918. return 1;
  919. }
  920. /* Are they in the same option because the user says they are? */
  921. if (options->NodeFamilySets) {
  922. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  923. if (routerset_contains_node(rs, node1) &&
  924. routerset_contains_node(rs, node2))
  925. return 1;
  926. });
  927. }
  928. return 0;
  929. }
  930. /**
  931. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  932. * the smartlist <b>sl</b>.
  933. *
  934. * This is used to make sure we don't pick siblings in a single path, or
  935. * pick more than one relay from a family for our entry guard list.
  936. * Note that a node may be added to <b>sl</b> more than once if it is
  937. * part of <b>node</b>'s family for more than one reason.
  938. */
  939. void
  940. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  941. {
  942. const smartlist_t *all_nodes = nodelist_get_list();
  943. const smartlist_t *declared_family;
  944. const or_options_t *options = get_options();
  945. tor_assert(node);
  946. declared_family = node_get_declared_family(node);
  947. /* Let's make sure that we have the node itself, if it's a real node. */
  948. {
  949. const node_t *real_node = node_get_by_id(node->identity);
  950. if (real_node)
  951. smartlist_add(sl, (node_t*)real_node);
  952. }
  953. /* First, add any nodes with similar network addresses. */
  954. if (options->EnforceDistinctSubnets) {
  955. tor_addr_t node_addr;
  956. node_get_addr(node, &node_addr);
  957. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  958. tor_addr_t a;
  959. node_get_addr(node2, &a);
  960. if (addrs_in_same_network_family(&a, &node_addr))
  961. smartlist_add(sl, (void*)node2);
  962. } SMARTLIST_FOREACH_END(node2);
  963. }
  964. /* Now, add all nodes in the declared_family of this node, if they
  965. * also declare this node to be in their family. */
  966. if (declared_family) {
  967. /* Add every r such that router declares familyness with node, and node
  968. * declares familyhood with router. */
  969. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  970. const node_t *node2;
  971. const smartlist_t *family2;
  972. if (!(node2 = node_get_by_nickname(name, 0)))
  973. continue;
  974. if (!(family2 = node_get_declared_family(node2)))
  975. continue;
  976. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  977. if (node_nickname_matches(node, name2)) {
  978. smartlist_add(sl, (void*)node2);
  979. break;
  980. }
  981. } SMARTLIST_FOREACH_END(name2);
  982. } SMARTLIST_FOREACH_END(name);
  983. }
  984. /* If the user declared any families locally, honor those too. */
  985. if (options->NodeFamilySets) {
  986. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  987. if (routerset_contains_node(rs, node)) {
  988. routerset_get_all_nodes(sl, rs, NULL, 0);
  989. }
  990. });
  991. }
  992. }
  993. /** Find a router that's up, that has this IP address, and
  994. * that allows exit to this address:port, or return NULL if there
  995. * isn't a good one.
  996. * Don't exit enclave to excluded relays -- it wouldn't actually
  997. * hurt anything, but this way there are fewer confused users.
  998. */
  999. const node_t *
  1000. router_find_exact_exit_enclave(const char *address, uint16_t port)
  1001. {/*XXXX MOVE*/
  1002. uint32_t addr;
  1003. struct in_addr in;
  1004. tor_addr_t a;
  1005. const or_options_t *options = get_options();
  1006. if (!tor_inet_aton(address, &in))
  1007. return NULL; /* it's not an IP already */
  1008. addr = ntohl(in.s_addr);
  1009. tor_addr_from_ipv4h(&a, addr);
  1010. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  1011. if (node_get_addr_ipv4h(node) == addr &&
  1012. node->is_running &&
  1013. compare_tor_addr_to_node_policy(&a, port, node) ==
  1014. ADDR_POLICY_ACCEPTED &&
  1015. !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
  1016. return node;
  1017. });
  1018. return NULL;
  1019. }
  1020. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  1021. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  1022. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  1023. * bandwidth.
  1024. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  1025. */
  1026. int
  1027. node_is_unreliable(const node_t *node, int need_uptime,
  1028. int need_capacity, int need_guard)
  1029. {
  1030. if (need_uptime && !node->is_stable)
  1031. return 1;
  1032. if (need_capacity && !node->is_fast)
  1033. return 1;
  1034. if (need_guard && !node->is_possible_guard)
  1035. return 1;
  1036. return 0;
  1037. }
  1038. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1039. * addr:port. Return 0 if any might accept it. */
  1040. int
  1041. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1042. int need_uptime)
  1043. {
  1044. addr_policy_result_t r;
  1045. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1046. if (node->is_running &&
  1047. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1048. r = compare_tor_addr_to_node_policy(addr, port, node);
  1049. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1050. return 0; /* this one could be ok. good enough. */
  1051. }
  1052. } SMARTLIST_FOREACH_END(node);
  1053. return 1; /* all will reject. */
  1054. }
  1055. /** Mark the router with ID <b>digest</b> as running or non-running
  1056. * in our routerlist. */
  1057. void
  1058. router_set_status(const char *digest, int up)
  1059. {
  1060. node_t *node;
  1061. tor_assert(digest);
  1062. SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
  1063. dir_server_t *, d,
  1064. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1065. d->is_running = up);
  1066. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1067. dir_server_t *, d,
  1068. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1069. d->is_running = up);
  1070. node = node_get_mutable_by_id(digest);
  1071. if (node) {
  1072. #if 0
  1073. log_debug(LD_DIR,"Marking router %s as %s.",
  1074. node_describe(node), up ? "up" : "down");
  1075. #endif
  1076. if (!up && node_is_me(node) && !net_is_disabled())
  1077. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1078. "addresses reachable?");
  1079. node->is_running = up;
  1080. }
  1081. router_dir_info_changed();
  1082. }
  1083. /** True iff, the last time we checked whether we had enough directory info
  1084. * to build circuits, the answer was "yes". */
  1085. static int have_min_dir_info = 0;
  1086. /** True iff enough has changed since the last time we checked whether we had
  1087. * enough directory info to build circuits that our old answer can no longer
  1088. * be trusted. */
  1089. static int need_to_update_have_min_dir_info = 1;
  1090. /** String describing what we're missing before we have enough directory
  1091. * info. */
  1092. static char dir_info_status[256] = "";
  1093. /** Return true iff we have enough networkstatus and router information to
  1094. * start building circuits. Right now, this means "more than half the
  1095. * networkstatus documents, and at least 1/4 of expected routers." */
  1096. //XXX should consider whether we have enough exiting nodes here.
  1097. int
  1098. router_have_minimum_dir_info(void)
  1099. {
  1100. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1101. update_router_have_minimum_dir_info();
  1102. need_to_update_have_min_dir_info = 0;
  1103. }
  1104. return have_min_dir_info;
  1105. }
  1106. /** Called when our internal view of the directory has changed. This can be
  1107. * when the authorities change, networkstatuses change, the list of routerdescs
  1108. * changes, or number of running routers changes.
  1109. */
  1110. void
  1111. router_dir_info_changed(void)
  1112. {
  1113. need_to_update_have_min_dir_info = 1;
  1114. rend_hsdir_routers_changed();
  1115. }
  1116. /** Return a string describing what we're missing before we have enough
  1117. * directory info. */
  1118. const char *
  1119. get_dir_info_status_string(void)
  1120. {
  1121. return dir_info_status;
  1122. }
  1123. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1124. * them seem like ones we'd use, and how many of <em>those</em> we have
  1125. * descriptors for. Store the former in *<b>num_usable</b> and the latter in
  1126. * *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
  1127. * routers in <b>in_set</b>. If <b>exit_only</b> is true, only consider nodes
  1128. * with the Exit flag. If *descs_out is present, add a node_t for each
  1129. * usable descriptor to it.
  1130. */
  1131. static void
  1132. count_usable_descriptors(int *num_present, int *num_usable,
  1133. smartlist_t *descs_out,
  1134. const networkstatus_t *consensus,
  1135. const or_options_t *options, time_t now,
  1136. routerset_t *in_set, int exit_only)
  1137. {
  1138. const int md = (consensus->flavor == FLAV_MICRODESC);
  1139. *num_present = 0, *num_usable=0;
  1140. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1141. {
  1142. const node_t *node = node_get_by_id(rs->identity_digest);
  1143. if (!node)
  1144. continue; /* This would be a bug: every entry in the consensus is
  1145. * supposed to have a node. */
  1146. if (exit_only && ! rs->is_exit)
  1147. continue;
  1148. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1149. continue;
  1150. if (client_would_use_router(rs, now, options)) {
  1151. const char * const digest = rs->descriptor_digest;
  1152. int present;
  1153. ++*num_usable; /* the consensus says we want it. */
  1154. if (md)
  1155. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1156. else
  1157. present = NULL != router_get_by_descriptor_digest(digest);
  1158. if (present) {
  1159. /* we have the descriptor listed in the consensus. */
  1160. ++*num_present;
  1161. }
  1162. if (descs_out)
  1163. smartlist_add(descs_out, (node_t*)node);
  1164. }
  1165. }
  1166. SMARTLIST_FOREACH_END(rs);
  1167. log_debug(LD_DIR, "%d usable, %d present (%s%s).",
  1168. *num_usable, *num_present,
  1169. md ? "microdesc" : "desc", exit_only ? " exits" : "s");
  1170. }
  1171. /** Return an extimate of which fraction of usable paths through the Tor
  1172. * network we have available for use. */
  1173. static double
  1174. compute_frac_paths_available(const networkstatus_t *consensus,
  1175. const or_options_t *options, time_t now,
  1176. int *num_present_out, int *num_usable_out,
  1177. char **status_out)
  1178. {
  1179. smartlist_t *guards = smartlist_new();
  1180. smartlist_t *mid = smartlist_new();
  1181. smartlist_t *exits = smartlist_new();
  1182. smartlist_t *myexits= smartlist_new();
  1183. double f_guard, f_mid, f_exit, f_myexit;
  1184. int np, nu; /* Ignored */
  1185. const int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  1186. count_usable_descriptors(num_present_out, num_usable_out,
  1187. mid, consensus, options, now, NULL, 0);
  1188. if (options->EntryNodes) {
  1189. count_usable_descriptors(&np, &nu, guards, consensus, options, now,
  1190. options->EntryNodes, 0);
  1191. } else {
  1192. SMARTLIST_FOREACH(mid, const node_t *, node, {
  1193. if (authdir) {
  1194. if (node->rs && node->rs->is_possible_guard)
  1195. smartlist_add(guards, (node_t*)node);
  1196. } else {
  1197. if (node->is_possible_guard)
  1198. smartlist_add(guards, (node_t*)node);
  1199. }
  1200. });
  1201. }
  1202. count_usable_descriptors(&np, &nu, exits, consensus, options, now,
  1203. NULL, 1);
  1204. count_usable_descriptors(&np, &nu, myexits, consensus, options, now,
  1205. options->ExitNodes, 1);
  1206. f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD);
  1207. f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID);
  1208. f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT);
  1209. f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT);
  1210. smartlist_free(guards);
  1211. smartlist_free(mid);
  1212. smartlist_free(exits);
  1213. smartlist_free(myexits);
  1214. /* This is a tricky point here: we don't want to make it easy for a
  1215. * directory to trickle exits to us until it learns which exits we have
  1216. * configured, so require that we have a threshold both of total exits
  1217. * and usable exits. */
  1218. if (f_myexit < f_exit)
  1219. f_exit = f_myexit;
  1220. tor_asprintf(status_out,
  1221. "%d%% of guards bw, "
  1222. "%d%% of midpoint bw, and "
  1223. "%d%% of exit bw",
  1224. (int)(f_guard*100),
  1225. (int)(f_mid*100),
  1226. (int)(f_exit*100));
  1227. return f_guard * f_mid * f_exit;
  1228. }
  1229. /** We just fetched a new set of descriptors. Compute how far through
  1230. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1231. * the controller of our progress. */
  1232. int
  1233. count_loading_descriptors_progress(void)
  1234. {
  1235. int num_present = 0, num_usable=0;
  1236. time_t now = time(NULL);
  1237. const networkstatus_t *consensus =
  1238. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1239. double fraction;
  1240. if (!consensus)
  1241. return 0; /* can't count descriptors if we have no list of them */
  1242. count_usable_descriptors(&num_present, &num_usable, NULL,
  1243. consensus, get_options(), now, NULL, 0);
  1244. if (num_usable == 0)
  1245. return 0; /* don't div by 0 */
  1246. fraction = num_present / (num_usable/4.);
  1247. if (fraction > 1.0)
  1248. return 0; /* it's not the number of descriptors holding us back */
  1249. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1250. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1251. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1252. }
  1253. /** Return the fraction of paths needed before we're willing to build
  1254. * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
  1255. static double
  1256. get_frac_paths_needed_for_circs(const or_options_t *options,
  1257. const networkstatus_t *ns)
  1258. {
  1259. #define DFLT_PCT_USABLE_NEEDED 60
  1260. if (options->PathsNeededToBuildCircuits >= 0.0) {
  1261. return options->PathsNeededToBuildCircuits;
  1262. } else {
  1263. return networkstatus_get_param(ns, "min_paths_for_circs_pct",
  1264. DFLT_PCT_USABLE_NEEDED,
  1265. 25, 95)/100.0;
  1266. }
  1267. }
  1268. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1269. * network and router information to build circuits. Clear the value of
  1270. * need_to_update_have_min_dir_info. */
  1271. static void
  1272. update_router_have_minimum_dir_info(void)
  1273. {
  1274. time_t now = time(NULL);
  1275. int res;
  1276. const or_options_t *options = get_options();
  1277. const networkstatus_t *consensus =
  1278. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1279. int using_md;
  1280. if (!consensus) {
  1281. if (!networkstatus_get_latest_consensus())
  1282. strlcpy(dir_info_status, "We have no usable consensus.",
  1283. sizeof(dir_info_status));
  1284. else
  1285. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1286. sizeof(dir_info_status));
  1287. res = 0;
  1288. goto done;
  1289. }
  1290. if (should_delay_dir_fetches(get_options())) {
  1291. log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
  1292. strlcpy(dir_info_status, "No live bridge descriptors.",
  1293. sizeof(dir_info_status));
  1294. res = 0;
  1295. goto done;
  1296. }
  1297. using_md = consensus->flavor == FLAV_MICRODESC;
  1298. {
  1299. char *status = NULL;
  1300. int num_present=0, num_usable=0;
  1301. double paths = compute_frac_paths_available(consensus, options, now,
  1302. &num_present, &num_usable,
  1303. &status);
  1304. if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
  1305. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1306. "We need more %sdescriptors: we have %d/%d, and "
  1307. "can only build %d%% of likely paths. (We have %s.)",
  1308. using_md?"micro":"", num_present, num_usable,
  1309. (int)(paths*100), status);
  1310. /* log_notice(LD_NET, "%s", dir_info_status); */
  1311. tor_free(status);
  1312. res = 0;
  1313. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1314. goto done;
  1315. }
  1316. tor_free(status);
  1317. res = 1;
  1318. }
  1319. done:
  1320. if (res && !have_min_dir_info) {
  1321. log_notice(LD_DIR,
  1322. "We now have enough directory information to build circuits.");
  1323. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1324. control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
  1325. }
  1326. if (!res && have_min_dir_info) {
  1327. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1328. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1329. "Our directory information is no longer up-to-date "
  1330. "enough to build circuits: %s", dir_info_status);
  1331. /* a) make us log when we next complete a circuit, so we know when Tor
  1332. * is back up and usable, and b) disable some activities that Tor
  1333. * should only do while circuits are working, like reachability tests
  1334. * and fetching bridge descriptors only over circuits. */
  1335. can_complete_circuit = 0;
  1336. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1337. }
  1338. have_min_dir_info = res;
  1339. need_to_update_have_min_dir_info = 0;
  1340. }