nodelist.c 60 KB

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