nodelist.c 48 KB

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