nodelist.c 60 KB

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