nodelist.c 60 KB

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