nodelist.c 60 KB

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