nodelist.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. return node->rs->dir_port != 0;
  565. else if (node->ri)
  566. return node->ri->dir_port != 0;
  567. else
  568. return 0;
  569. }
  570. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  571. * is, a routerdescriptor or a microdescriptor. */
  572. int
  573. node_has_descriptor(const node_t *node)
  574. {
  575. return (node->ri ||
  576. (node->rs && node->md));
  577. }
  578. /** Return the router_purpose of <b>node</b>. */
  579. int
  580. node_get_purpose(const node_t *node)
  581. {
  582. if (node->ri)
  583. return node->ri->purpose;
  584. else
  585. return ROUTER_PURPOSE_GENERAL;
  586. }
  587. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  588. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  589. * <b>verbose_name_out</b> */
  590. void
  591. node_get_verbose_nickname(const node_t *node,
  592. char *verbose_name_out)
  593. {
  594. const char *nickname = node_get_nickname(node);
  595. int is_named = node_is_named(node);
  596. verbose_name_out[0] = '$';
  597. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  598. DIGEST_LEN);
  599. if (!nickname)
  600. return;
  601. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  602. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  603. }
  604. /** Compute the verbose ("extended") nickname of node with
  605. * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
  606. * character buffer at <b>verbose_name_out</b>
  607. *
  608. * If node_get_by_id() returns NULL, base 16 encoding of
  609. * <b>id_digest</b> is returned instead. */
  610. void
  611. node_get_verbose_nickname_by_id(const char *id_digest,
  612. char *verbose_name_out)
  613. {
  614. const node_t *node = node_get_by_id(id_digest);
  615. if (!node) {
  616. verbose_name_out[0] = '$';
  617. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
  618. } else {
  619. node_get_verbose_nickname(node, verbose_name_out);
  620. }
  621. }
  622. /** Return true iff it seems that <b>node</b> allows circuits to exit
  623. * through it directlry from the client. */
  624. int
  625. node_allows_single_hop_exits(const node_t *node)
  626. {
  627. if (node && node->ri)
  628. return node->ri->allow_single_hop_exits;
  629. else
  630. return 0;
  631. }
  632. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  633. * actually permit anything to exit, or we don't know its exit policy */
  634. int
  635. node_exit_policy_rejects_all(const node_t *node)
  636. {
  637. if (node->rejects_all)
  638. return 1;
  639. if (node->ri)
  640. return node->ri->policy_is_reject_star;
  641. else if (node->md)
  642. return node->md->exit_policy == NULL ||
  643. short_policy_is_reject_star(node->md->exit_policy);
  644. else
  645. return 1;
  646. }
  647. /** Return true iff the exit policy for <b>node</b> is such that we can treat
  648. * rejecting an address of type <b>family</b> unexpectedly as a sign of that
  649. * node's failure. */
  650. int
  651. node_exit_policy_is_exact(const node_t *node, sa_family_t family)
  652. {
  653. if (family == AF_UNSPEC) {
  654. return 1; /* Rejecting an address but not telling us what address
  655. * is a bad sign. */
  656. } else if (family == AF_INET) {
  657. return node->ri != NULL;
  658. } else if (family == AF_INET6) {
  659. return 0;
  660. }
  661. tor_fragile_assert();
  662. return 1;
  663. }
  664. /* Check if the "addr" and port_field fields from r are a valid non-listening
  665. * address/port. If so, set valid to true and add a newly allocated
  666. * tor_addr_port_t containing "addr" and port_field to sl.
  667. * "addr" is an IPv4 host-order address and port_field is a uint16_t.
  668. * r is typically a routerinfo_t or routerstatus_t.
  669. */
  670. #define SL_ADD_NEW_IPV4_AP(r, port_field, sl, valid) \
  671. STMT_BEGIN \
  672. if (tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
  673. valid = 1; \
  674. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
  675. tor_addr_from_ipv4h(&ap->addr, (r)->addr); \
  676. ap->port = (r)->port_field; \
  677. smartlist_add((sl), ap); \
  678. } \
  679. STMT_END
  680. /* Check if the "addr" and port_field fields from r are a valid non-listening
  681. * address/port. If so, set valid to true and add a newly allocated
  682. * tor_addr_port_t containing "addr" and port_field to sl.
  683. * "addr" is a tor_addr_t and port_field is a uint16_t.
  684. * r is typically a routerinfo_t or routerstatus_t.
  685. */
  686. #define SL_ADD_NEW_IPV6_AP(r, port_field, sl, valid) \
  687. STMT_BEGIN \
  688. if (tor_addr_port_is_valid(&(r)->ipv6_addr, (r)->port_field, 0)) { \
  689. valid = 1; \
  690. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
  691. tor_addr_copy(&ap->addr, &(r)->ipv6_addr); \
  692. ap->port = (r)->port_field; \
  693. smartlist_add((sl), ap); \
  694. } \
  695. STMT_END
  696. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  697. * addr + TCP port) for <b>node</b>. Caller must free all elements
  698. * using tor_free() and free the list using smartlist_free().
  699. *
  700. * XXX this is potentially a memory fragmentation hog -- if on
  701. * critical path consider the option of having the caller allocate the
  702. * memory
  703. */
  704. smartlist_t *
  705. node_get_all_orports(const node_t *node)
  706. {
  707. smartlist_t *sl = smartlist_new();
  708. int valid = 0;
  709. /* Find a valid IPv4 address and port */
  710. if (node->ri != NULL) {
  711. SL_ADD_NEW_IPV4_AP(node->ri, or_port, sl, valid);
  712. }
  713. /* If we didn't find a valid address/port in the ri, try the rs */
  714. if (!valid && node->rs != NULL) {
  715. SL_ADD_NEW_IPV4_AP(node->rs, or_port, sl, valid);
  716. }
  717. /* Find a valid IPv6 address and port */
  718. valid = 0;
  719. if (node->ri != NULL) {
  720. SL_ADD_NEW_IPV6_AP(node->ri, ipv6_orport, sl, valid);
  721. }
  722. if (!valid && node->rs != NULL) {
  723. SL_ADD_NEW_IPV6_AP(node->rs, ipv6_orport, sl, valid);
  724. }
  725. if (!valid && node->md != NULL) {
  726. SL_ADD_NEW_IPV6_AP(node->md, ipv6_orport, sl, valid);
  727. }
  728. return sl;
  729. }
  730. #undef SL_ADD_NEW_IPV4_AP
  731. #undef SL_ADD_NEW_IPV6_AP
  732. /** Wrapper around node_get_prim_orport for backward
  733. compatibility. */
  734. void
  735. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  736. {
  737. tor_addr_port_t ap;
  738. node_get_prim_orport(node, &ap);
  739. tor_addr_copy(addr_out, &ap.addr);
  740. }
  741. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  742. * seem to have one. */
  743. uint32_t
  744. node_get_prim_addr_ipv4h(const node_t *node)
  745. {
  746. /* Don't check the ORPort or DirPort, as this function isn't port-specific,
  747. * and the node might have a valid IPv4 address, yet have a zero
  748. * ORPort or DirPort.
  749. */
  750. if (node->ri && tor_addr_is_valid_ipv4h(node->ri->addr, 0)) {
  751. return node->ri->addr;
  752. } else if (node->rs && tor_addr_is_valid_ipv4h(node->rs->addr, 0)) {
  753. return node->rs->addr;
  754. }
  755. return 0;
  756. }
  757. /** Copy a string representation of an IP address for <b>node</b> into
  758. * the <b>len</b>-byte buffer at <b>buf</b>. */
  759. void
  760. node_get_address_string(const node_t *node, char *buf, size_t len)
  761. {
  762. if (node->ri) {
  763. strlcpy(buf, fmt_addr32(node->ri->addr), len);
  764. } else if (node->rs) {
  765. tor_addr_t addr;
  766. tor_addr_from_ipv4h(&addr, node->rs->addr);
  767. tor_addr_to_str(buf, &addr, len, 0);
  768. } else {
  769. buf[0] = '\0';
  770. }
  771. }
  772. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  773. * one. */
  774. long
  775. node_get_declared_uptime(const node_t *node)
  776. {
  777. if (node->ri)
  778. return node->ri->uptime;
  779. else
  780. return -1;
  781. }
  782. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  783. const char *
  784. node_get_platform(const node_t *node)
  785. {
  786. /* If we wanted, we could record the version in the routerstatus_t, since
  787. * the consensus lists it. We don't, though, so this function just won't
  788. * work with microdescriptors. */
  789. if (node->ri)
  790. return node->ri->platform;
  791. else
  792. return NULL;
  793. }
  794. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  795. time_t
  796. node_get_published_on(const node_t *node)
  797. {
  798. if (node->ri)
  799. return node->ri->cache_info.published_on;
  800. else
  801. return 0;
  802. }
  803. /** Return true iff <b>node</b> is one representing this router. */
  804. int
  805. node_is_me(const node_t *node)
  806. {
  807. return router_digest_is_me(node->identity);
  808. }
  809. /** Return <b>node</b> declared family (as a list of names), or NULL if
  810. * the node didn't declare a family. */
  811. const smartlist_t *
  812. node_get_declared_family(const node_t *node)
  813. {
  814. if (node->ri && node->ri->declared_family)
  815. return node->ri->declared_family;
  816. else if (node->md && node->md->family)
  817. return node->md->family;
  818. else
  819. return NULL;
  820. }
  821. /* Does this node have a valid IPv6 address?
  822. * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
  823. * checking specific ports. */
  824. int
  825. node_has_ipv6_addr(const node_t *node)
  826. {
  827. /* Don't check the ORPort or DirPort, as this function isn't port-specific,
  828. * and the node might have a valid IPv6 address, yet have a zero
  829. * ORPort or DirPort.
  830. */
  831. if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
  832. return 1;
  833. if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
  834. return 1;
  835. if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
  836. return 1;
  837. return 0;
  838. }
  839. /* Does this node have a valid IPv6 ORPort? */
  840. int
  841. node_has_ipv6_orport(const node_t *node)
  842. {
  843. tor_addr_port_t ipv6_orport;
  844. node_get_pref_ipv6_orport(node, &ipv6_orport);
  845. return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
  846. }
  847. /* Does this node have a valid IPv6 DirPort? */
  848. int
  849. node_has_ipv6_dirport(const node_t *node)
  850. {
  851. tor_addr_port_t ipv6_dirport;
  852. node_get_pref_ipv6_dirport(node, &ipv6_dirport);
  853. return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
  854. }
  855. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  856. * <b>node</b>, else 0.
  857. *
  858. * We prefer the IPv6 address if the router has an IPv6 address,
  859. * and we can use IPv6 addresses, and:
  860. * i) the node_t says that it prefers IPv6
  861. * or
  862. * ii) the router has no IPv4 OR address.
  863. * or
  864. * iii) our preference is for IPv6 addresses.
  865. * (This extra step is needed in case our preferences have changed since
  866. * node->ipv6_preferred was set at the time the consensus was loaded.)
  867. */
  868. int
  869. node_ipv6_or_preferred(const node_t *node)
  870. {
  871. const or_options_t *options = get_options();
  872. tor_addr_port_t ipv4_addr;
  873. node_assert_ok(node);
  874. if (!fascist_firewall_use_ipv6(options)) {
  875. return 0;
  876. } else if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)
  877. || fascist_firewall_prefer_ipv6_orport(get_options())) {
  878. return node_has_ipv6_orport(node);
  879. }
  880. return 0;
  881. }
  882. #define RETURN_IPV4_AP(r, port_field, ap_out) \
  883. STMT_BEGIN \
  884. if (r && tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
  885. tor_addr_from_ipv4h(&(ap_out)->addr, (r)->addr); \
  886. (ap_out)->port = (r)->port_field; \
  887. return 0; \
  888. } \
  889. STMT_END
  890. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  891. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  892. * port was copied, else return non-zero.*/
  893. int
  894. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  895. {
  896. node_assert_ok(node);
  897. tor_assert(ap_out);
  898. RETURN_IPV4_AP(node->ri, or_port, ap_out);
  899. RETURN_IPV4_AP(node->rs, or_port, ap_out);
  900. /* Microdescriptors only have an IPv6 address */
  901. return -1;
  902. }
  903. /** Copy the preferred OR port (IP address and TCP port) for
  904. * <b>node</b> into *<b>ap_out</b>. */
  905. void
  906. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  907. {
  908. tor_assert(ap_out);
  909. if (node_ipv6_or_preferred(node)) {
  910. node_get_pref_ipv6_orport(node, ap_out);
  911. } else {
  912. /* the primary ORPort is always on IPv4 */
  913. node_get_prim_orport(node, ap_out);
  914. }
  915. }
  916. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  917. * <b>node</b> into *<b>ap_out</b>. */
  918. void
  919. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  920. {
  921. node_assert_ok(node);
  922. tor_assert(ap_out);
  923. /* Prefer routerstatus over microdesc for consistency with the
  924. * fascist_firewall_* functions. Also check if the address or port are valid,
  925. * and try another alternative if they are not. */
  926. if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
  927. node->ri->ipv6_orport, 0)) {
  928. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  929. ap_out->port = node->ri->ipv6_orport;
  930. } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
  931. node->rs->ipv6_orport, 0)) {
  932. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  933. ap_out->port = node->rs->ipv6_orport;
  934. } else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
  935. node->md->ipv6_orport, 0)) {
  936. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  937. ap_out->port = node->md->ipv6_orport;
  938. } else {
  939. tor_addr_make_null(&ap_out->addr, AF_INET6);
  940. ap_out->port = 0;
  941. }
  942. }
  943. /** Return 1 if we prefer the IPv6 address and Dir TCP port of
  944. * <b>node</b>, else 0.
  945. *
  946. * We prefer the IPv6 address if the router has an IPv6 address,
  947. * and we can use IPv6 addresses, and:
  948. * i) the node_t says that it prefers IPv6
  949. * or
  950. * ii) the router has no IPv4 Dir address.
  951. * or
  952. * iii) our preference is for IPv6 addresses.
  953. * (This extra step is needed in case our preferences have changed since
  954. * node->ipv6_preferred was set at the time the consensus was loaded.)
  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. if (!fascist_firewall_use_ipv6(options)) {
  963. return 0;
  964. } else if (node->ipv6_preferred || node_get_prim_dirport(node, &ipv4_addr)
  965. || fascist_firewall_prefer_ipv6_dirport(get_options())) {
  966. return node_has_ipv6_dirport(node);
  967. }
  968. return 0;
  969. }
  970. /** Copy the primary (IPv4) Dir port (IP address and TCP port) for
  971. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  972. * port was copied, else return non-zero.*/
  973. int
  974. node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
  975. {
  976. node_assert_ok(node);
  977. tor_assert(ap_out);
  978. RETURN_IPV4_AP(node->ri, dir_port, ap_out);
  979. RETURN_IPV4_AP(node->rs, dir_port, ap_out);
  980. /* Microdescriptors only have an IPv6 address */
  981. return -1;
  982. }
  983. #undef RETURN_IPV4_AP
  984. /** Copy the preferred Dir port (IP address and TCP port) for
  985. * <b>node</b> into *<b>ap_out</b>. */
  986. void
  987. node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
  988. {
  989. tor_assert(ap_out);
  990. if (node_ipv6_dir_preferred(node)) {
  991. node_get_pref_ipv6_dirport(node, ap_out);
  992. } else {
  993. /* the primary DirPort is always on IPv4 */
  994. node_get_prim_dirport(node, ap_out);
  995. }
  996. }
  997. /** Copy the preferred IPv6 Dir port (IP address and TCP port) for
  998. * <b>node</b> into *<b>ap_out</b>. */
  999. void
  1000. node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
  1001. {
  1002. node_assert_ok(node);
  1003. tor_assert(ap_out);
  1004. /* Check if the address or port are valid, and try another alternative if
  1005. * they are not. Note that microdescriptors have no dir_port. */
  1006. /* Assume IPv4 and IPv6 dirports are the same */
  1007. if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
  1008. node->ri->dir_port, 0)) {
  1009. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  1010. ap_out->port = node->ri->dir_port;
  1011. } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
  1012. node->rs->dir_port, 0)) {
  1013. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  1014. ap_out->port = node->rs->dir_port;
  1015. } else {
  1016. tor_addr_make_null(&ap_out->addr, AF_INET6);
  1017. ap_out->port = 0;
  1018. }
  1019. }
  1020. /** Return true iff <b>node</b> has a curve25519 onion key. */
  1021. int
  1022. node_has_curve25519_onion_key(const node_t *node)
  1023. {
  1024. if (node->ri)
  1025. return node->ri->onion_curve25519_pkey != NULL;
  1026. else if (node->md)
  1027. return node->md->onion_curve25519_pkey != NULL;
  1028. else
  1029. return 0;
  1030. }
  1031. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  1032. * each router when the GeoIP database is reloaded, and on all new routers. */
  1033. void
  1034. node_set_country(node_t *node)
  1035. {
  1036. tor_addr_t addr = TOR_ADDR_NULL;
  1037. /* XXXXipv6 */
  1038. if (node->rs)
  1039. tor_addr_from_ipv4h(&addr, node->rs->addr);
  1040. else if (node->ri)
  1041. tor_addr_from_ipv4h(&addr, node->ri->addr);
  1042. node->country = geoip_get_country_by_addr(&addr);
  1043. }
  1044. /** Set the country code of all routers in the routerlist. */
  1045. void
  1046. nodelist_refresh_countries(void)
  1047. {
  1048. smartlist_t *nodes = nodelist_get_list();
  1049. SMARTLIST_FOREACH(nodes, node_t *, node,
  1050. node_set_country(node));
  1051. }
  1052. /** Return true iff router1 and router2 have similar enough network addresses
  1053. * that we should treat them as being in the same family */
  1054. static inline int
  1055. addrs_in_same_network_family(const tor_addr_t *a1,
  1056. const tor_addr_t *a2)
  1057. {
  1058. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  1059. }
  1060. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  1061. * (case-insensitive), or if <b>node's</b> identity key digest
  1062. * matches a hexadecimal value stored in <b>nickname</b>. Return
  1063. * false otherwise. */
  1064. static int
  1065. node_nickname_matches(const node_t *node, const char *nickname)
  1066. {
  1067. const char *n = node_get_nickname(node);
  1068. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  1069. return 1;
  1070. return hex_digest_nickname_matches(nickname,
  1071. node->identity,
  1072. n,
  1073. node_is_named(node));
  1074. }
  1075. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  1076. static inline int
  1077. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  1078. {
  1079. if (!lst) return 0;
  1080. SMARTLIST_FOREACH(lst, const char *, name, {
  1081. if (node_nickname_matches(node, name))
  1082. return 1;
  1083. });
  1084. return 0;
  1085. }
  1086. /** Return true iff r1 and r2 are in the same family, but not the same
  1087. * router. */
  1088. int
  1089. nodes_in_same_family(const node_t *node1, const node_t *node2)
  1090. {
  1091. const or_options_t *options = get_options();
  1092. /* Are they in the same family because of their addresses? */
  1093. if (options->EnforceDistinctSubnets) {
  1094. tor_addr_t a1, a2;
  1095. node_get_addr(node1, &a1);
  1096. node_get_addr(node2, &a2);
  1097. if (addrs_in_same_network_family(&a1, &a2))
  1098. return 1;
  1099. }
  1100. /* Are they in the same family because the agree they are? */
  1101. {
  1102. const smartlist_t *f1, *f2;
  1103. f1 = node_get_declared_family(node1);
  1104. f2 = node_get_declared_family(node2);
  1105. if (f1 && f2 &&
  1106. node_in_nickname_smartlist(f1, node2) &&
  1107. node_in_nickname_smartlist(f2, node1))
  1108. return 1;
  1109. }
  1110. /* Are they in the same option because the user says they are? */
  1111. if (options->NodeFamilySets) {
  1112. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  1113. if (routerset_contains_node(rs, node1) &&
  1114. routerset_contains_node(rs, node2))
  1115. return 1;
  1116. });
  1117. }
  1118. return 0;
  1119. }
  1120. /**
  1121. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  1122. * the smartlist <b>sl</b>.
  1123. *
  1124. * This is used to make sure we don't pick siblings in a single path, or
  1125. * pick more than one relay from a family for our entry guard list.
  1126. * Note that a node may be added to <b>sl</b> more than once if it is
  1127. * part of <b>node</b>'s family for more than one reason.
  1128. */
  1129. void
  1130. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  1131. {
  1132. const smartlist_t *all_nodes = nodelist_get_list();
  1133. const smartlist_t *declared_family;
  1134. const or_options_t *options = get_options();
  1135. tor_assert(node);
  1136. declared_family = node_get_declared_family(node);
  1137. /* Let's make sure that we have the node itself, if it's a real node. */
  1138. {
  1139. const node_t *real_node = node_get_by_id(node->identity);
  1140. if (real_node)
  1141. smartlist_add(sl, (node_t*)real_node);
  1142. }
  1143. /* First, add any nodes with similar network addresses. */
  1144. if (options->EnforceDistinctSubnets) {
  1145. tor_addr_t node_addr;
  1146. node_get_addr(node, &node_addr);
  1147. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  1148. tor_addr_t a;
  1149. node_get_addr(node2, &a);
  1150. if (addrs_in_same_network_family(&a, &node_addr))
  1151. smartlist_add(sl, (void*)node2);
  1152. } SMARTLIST_FOREACH_END(node2);
  1153. }
  1154. /* Now, add all nodes in the declared_family of this node, if they
  1155. * also declare this node to be in their family. */
  1156. if (declared_family) {
  1157. /* Add every r such that router declares familyness with node, and node
  1158. * declares familyhood with router. */
  1159. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  1160. const node_t *node2;
  1161. const smartlist_t *family2;
  1162. if (!(node2 = node_get_by_nickname(name, 0)))
  1163. continue;
  1164. if (!(family2 = node_get_declared_family(node2)))
  1165. continue;
  1166. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  1167. if (node_nickname_matches(node, name2)) {
  1168. smartlist_add(sl, (void*)node2);
  1169. break;
  1170. }
  1171. } SMARTLIST_FOREACH_END(name2);
  1172. } SMARTLIST_FOREACH_END(name);
  1173. }
  1174. /* If the user declared any families locally, honor those too. */
  1175. if (options->NodeFamilySets) {
  1176. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  1177. if (routerset_contains_node(rs, node)) {
  1178. routerset_get_all_nodes(sl, rs, NULL, 0);
  1179. }
  1180. });
  1181. }
  1182. }
  1183. /** Find a router that's up, that has this IP address, and
  1184. * that allows exit to this address:port, or return NULL if there
  1185. * isn't a good one.
  1186. * Don't exit enclave to excluded relays -- it wouldn't actually
  1187. * hurt anything, but this way there are fewer confused users.
  1188. */
  1189. const node_t *
  1190. router_find_exact_exit_enclave(const char *address, uint16_t port)
  1191. {/*XXXX MOVE*/
  1192. uint32_t addr;
  1193. struct in_addr in;
  1194. tor_addr_t a;
  1195. const or_options_t *options = get_options();
  1196. if (!tor_inet_aton(address, &in))
  1197. return NULL; /* it's not an IP already */
  1198. addr = ntohl(in.s_addr);
  1199. tor_addr_from_ipv4h(&a, addr);
  1200. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  1201. if (node_get_addr_ipv4h(node) == addr &&
  1202. node->is_running &&
  1203. compare_tor_addr_to_node_policy(&a, port, node) ==
  1204. ADDR_POLICY_ACCEPTED &&
  1205. !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
  1206. return node;
  1207. });
  1208. return NULL;
  1209. }
  1210. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  1211. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  1212. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  1213. * bandwidth.
  1214. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  1215. */
  1216. int
  1217. node_is_unreliable(const node_t *node, int need_uptime,
  1218. int need_capacity, int need_guard)
  1219. {
  1220. if (need_uptime && !node->is_stable)
  1221. return 1;
  1222. if (need_capacity && !node->is_fast)
  1223. return 1;
  1224. if (need_guard && !node->is_possible_guard)
  1225. return 1;
  1226. return 0;
  1227. }
  1228. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1229. * addr:port. Return 0 if any might accept it. */
  1230. int
  1231. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1232. int need_uptime)
  1233. {
  1234. addr_policy_result_t r;
  1235. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1236. if (node->is_running &&
  1237. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1238. r = compare_tor_addr_to_node_policy(addr, port, node);
  1239. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1240. return 0; /* this one could be ok. good enough. */
  1241. }
  1242. } SMARTLIST_FOREACH_END(node);
  1243. return 1; /* all will reject. */
  1244. }
  1245. /** Mark the router with ID <b>digest</b> as running or non-running
  1246. * in our routerlist. */
  1247. void
  1248. router_set_status(const char *digest, int up)
  1249. {
  1250. node_t *node;
  1251. tor_assert(digest);
  1252. SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
  1253. dir_server_t *, d,
  1254. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1255. d->is_running = up);
  1256. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1257. dir_server_t *, d,
  1258. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1259. d->is_running = up);
  1260. node = node_get_mutable_by_id(digest);
  1261. if (node) {
  1262. #if 0
  1263. log_debug(LD_DIR,"Marking router %s as %s.",
  1264. node_describe(node), up ? "up" : "down");
  1265. #endif
  1266. if (!up && node_is_me(node) && !net_is_disabled())
  1267. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1268. "addresses reachable?");
  1269. if (bool_neq(node->is_running, up))
  1270. router_dir_info_changed();
  1271. node->is_running = up;
  1272. }
  1273. }
  1274. /** True iff, the last time we checked whether we had enough directory info
  1275. * to build circuits, the answer was "yes". If there are no exits in the
  1276. * consensus, we act as if we have 100% of the exit directory info. */
  1277. static int have_min_dir_info = 0;
  1278. /** Does the consensus contain nodes that can exit? */
  1279. static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
  1280. /** True iff enough has changed since the last time we checked whether we had
  1281. * enough directory info to build circuits that our old answer can no longer
  1282. * be trusted. */
  1283. static int need_to_update_have_min_dir_info = 1;
  1284. /** String describing what we're missing before we have enough directory
  1285. * info. */
  1286. static char dir_info_status[512] = "";
  1287. /** Return true iff we have enough consensus information to
  1288. * start building circuits. Right now, this means "a consensus that's
  1289. * less than a day old, and at least 60% of router descriptors (configurable),
  1290. * weighted by bandwidth. Treat the exit fraction as 100% if there are
  1291. * no exits in the consensus."
  1292. * To obtain the final weighted bandwidth, we multiply the
  1293. * weighted bandwidth fraction for each position (guard, middle, exit). */
  1294. int
  1295. router_have_minimum_dir_info(void)
  1296. {
  1297. static int logged_delay=0;
  1298. const char *delay_fetches_msg = NULL;
  1299. if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
  1300. if (!logged_delay)
  1301. log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
  1302. logged_delay=1;
  1303. strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
  1304. return 0;
  1305. }
  1306. logged_delay = 0; /* reset it if we get this far */
  1307. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1308. update_router_have_minimum_dir_info();
  1309. }
  1310. return have_min_dir_info;
  1311. }
  1312. /** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
  1313. * in the consensus. We update this flag in compute_frac_paths_available if
  1314. * there is at least one relay that has an Exit flag in the consensus.
  1315. * Used to avoid building exit circuits when they will almost certainly fail.
  1316. * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
  1317. * (This situation typically occurs during bootstrap of a test network.)
  1318. * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
  1319. * reason to believe our last known value was invalid or has expired.
  1320. * If we're in a network with TestingDirAuthVoteExit set,
  1321. * this can cause router_have_consensus_path() to be set to
  1322. * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
  1323. */
  1324. consensus_path_type_t
  1325. router_have_consensus_path(void)
  1326. {
  1327. return have_consensus_path;
  1328. }
  1329. /** Called when our internal view of the directory has changed. This can be
  1330. * when the authorities change, networkstatuses change, the list of routerdescs
  1331. * changes, or number of running routers changes.
  1332. */
  1333. void
  1334. router_dir_info_changed(void)
  1335. {
  1336. need_to_update_have_min_dir_info = 1;
  1337. rend_hsdir_routers_changed();
  1338. }
  1339. /** Return a string describing what we're missing before we have enough
  1340. * directory info. */
  1341. const char *
  1342. get_dir_info_status_string(void)
  1343. {
  1344. return dir_info_status;
  1345. }
  1346. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1347. * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
  1348. * many of <em>those</em> we have descriptors for (store this in
  1349. * *<b>num_present</b>).
  1350. *
  1351. * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
  1352. * If <b>exit_only</b> is USABLE_DESCRIPTOR_EXIT_ONLY, only consider nodes
  1353. * with the Exit flag.
  1354. * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
  1355. * to it.
  1356. */
  1357. static void
  1358. count_usable_descriptors(int *num_present, int *num_usable,
  1359. smartlist_t *descs_out,
  1360. const networkstatus_t *consensus,
  1361. const or_options_t *options, time_t now,
  1362. routerset_t *in_set,
  1363. usable_descriptor_t exit_only)
  1364. {
  1365. const int md = (consensus->flavor == FLAV_MICRODESC);
  1366. *num_present = 0, *num_usable = 0;
  1367. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1368. {
  1369. const node_t *node = node_get_by_id(rs->identity_digest);
  1370. if (!node)
  1371. continue; /* This would be a bug: every entry in the consensus is
  1372. * supposed to have a node. */
  1373. if (exit_only == USABLE_DESCRIPTOR_EXIT_ONLY && ! rs->is_exit)
  1374. continue;
  1375. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1376. continue;
  1377. if (client_would_use_router(rs, now, options)) {
  1378. const char * const digest = rs->descriptor_digest;
  1379. int present;
  1380. ++*num_usable; /* the consensus says we want it. */
  1381. if (md)
  1382. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1383. else
  1384. present = NULL != router_get_by_descriptor_digest(digest);
  1385. if (present) {
  1386. /* we have the descriptor listed in the consensus. */
  1387. ++*num_present;
  1388. }
  1389. if (descs_out)
  1390. smartlist_add(descs_out, (node_t*)node);
  1391. }
  1392. }
  1393. SMARTLIST_FOREACH_END(rs);
  1394. log_debug(LD_DIR, "%d usable, %d present (%s%s).",
  1395. *num_usable, *num_present,
  1396. md ? "microdesc" : "desc",
  1397. exit_only == USABLE_DESCRIPTOR_EXIT_ONLY ? " exits" : "s");
  1398. }
  1399. /** Return an estimate of which fraction of usable paths through the Tor
  1400. * network we have available for use. Count how many routers seem like ones
  1401. * we'd use (store this in *<b>num_usable_out</b>), and how many of
  1402. * <em>those</em> we have descriptors for (store this in
  1403. * *<b>num_present_out</b>.)
  1404. *
  1405. * If **<b>status_out</b> is present, allocate a new string and print the
  1406. * available percentages of guard, middle, and exit nodes to it, noting
  1407. * whether there are exits in the consensus.
  1408. * If there are no guards in the consensus,
  1409. * we treat the exit fraction as 100%.
  1410. */
  1411. static double
  1412. compute_frac_paths_available(const networkstatus_t *consensus,
  1413. const or_options_t *options, time_t now,
  1414. int *num_present_out, int *num_usable_out,
  1415. char **status_out)
  1416. {
  1417. smartlist_t *guards = smartlist_new();
  1418. smartlist_t *mid = smartlist_new();
  1419. smartlist_t *exits = smartlist_new();
  1420. double f_guard, f_mid, f_exit;
  1421. double f_path = 0.0;
  1422. /* Used to determine whether there are any exits in the consensus */
  1423. int np = 0;
  1424. /* Used to determine whether there are any exits with descriptors */
  1425. int nu = 0;
  1426. const int authdir = authdir_mode_v3(options);
  1427. count_usable_descriptors(num_present_out, num_usable_out,
  1428. mid, consensus, options, now, NULL,
  1429. USABLE_DESCRIPTOR_ALL);
  1430. if (options->EntryNodes) {
  1431. count_usable_descriptors(&np, &nu, guards, consensus, options, now,
  1432. options->EntryNodes, USABLE_DESCRIPTOR_ALL);
  1433. } else {
  1434. SMARTLIST_FOREACH(mid, const node_t *, node, {
  1435. if (authdir) {
  1436. if (node->rs && node->rs->is_possible_guard)
  1437. smartlist_add(guards, (node_t*)node);
  1438. } else {
  1439. if (node->is_possible_guard)
  1440. smartlist_add(guards, (node_t*)node);
  1441. }
  1442. });
  1443. }
  1444. /* All nodes with exit flag
  1445. * If we're in a network with TestingDirAuthVoteExit set,
  1446. * this can cause false positives on have_consensus_path,
  1447. * incorrectly setting it to CONSENSUS_PATH_EXIT. This is
  1448. * an unavoidable feature of forcing authorities to declare
  1449. * certain nodes as exits.
  1450. */
  1451. count_usable_descriptors(&np, &nu, exits, consensus, options, now,
  1452. NULL, USABLE_DESCRIPTOR_EXIT_ONLY);
  1453. log_debug(LD_NET,
  1454. "%s: %d present, %d usable",
  1455. "exits",
  1456. np,
  1457. nu);
  1458. /* We need at least 1 exit present in the consensus to consider
  1459. * building exit paths */
  1460. /* Update our understanding of whether the consensus has exits */
  1461. consensus_path_type_t old_have_consensus_path = have_consensus_path;
  1462. have_consensus_path = ((nu > 0) ?
  1463. CONSENSUS_PATH_EXIT :
  1464. CONSENSUS_PATH_INTERNAL);
  1465. if (have_consensus_path == CONSENSUS_PATH_INTERNAL
  1466. && old_have_consensus_path != have_consensus_path) {
  1467. log_notice(LD_NET,
  1468. "The current consensus has no exit nodes. "
  1469. "Tor can only build internal paths, "
  1470. "such as paths to hidden services.");
  1471. /* However, exit nodes can reachability self-test using this consensus,
  1472. * join the network, and appear in a later consensus. This will allow
  1473. * the network to build exit paths, such as paths for world wide web
  1474. * browsing (as distinct from hidden service web browsing). */
  1475. }
  1476. f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD);
  1477. f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID);
  1478. f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT);
  1479. log_debug(LD_NET,
  1480. "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
  1481. f_guard,
  1482. f_mid,
  1483. f_exit);
  1484. smartlist_free(guards);
  1485. smartlist_free(mid);
  1486. smartlist_free(exits);
  1487. if (options->ExitNodes) {
  1488. double f_myexit, f_myexit_unflagged;
  1489. smartlist_t *myexits= smartlist_new();
  1490. smartlist_t *myexits_unflagged = smartlist_new();
  1491. /* All nodes with exit flag in ExitNodes option */
  1492. count_usable_descriptors(&np, &nu, myexits, consensus, options, now,
  1493. options->ExitNodes, USABLE_DESCRIPTOR_EXIT_ONLY);
  1494. log_debug(LD_NET,
  1495. "%s: %d present, %d usable",
  1496. "myexits",
  1497. np,
  1498. nu);
  1499. /* Now compute the nodes in the ExitNodes option where which we don't know
  1500. * what their exit policy is, or we know it permits something. */
  1501. count_usable_descriptors(&np, &nu, myexits_unflagged,
  1502. consensus, options, now,
  1503. options->ExitNodes, USABLE_DESCRIPTOR_ALL);
  1504. log_debug(LD_NET,
  1505. "%s: %d present, %d usable",
  1506. "myexits_unflagged (initial)",
  1507. np,
  1508. nu);
  1509. SMARTLIST_FOREACH_BEGIN(myexits_unflagged, const node_t *, node) {
  1510. if (node_has_descriptor(node) && node_exit_policy_rejects_all(node)) {
  1511. SMARTLIST_DEL_CURRENT(myexits_unflagged, node);
  1512. /* this node is not actually an exit */
  1513. np--;
  1514. /* this node is unusable as an exit */
  1515. nu--;
  1516. }
  1517. } SMARTLIST_FOREACH_END(node);
  1518. log_debug(LD_NET,
  1519. "%s: %d present, %d usable",
  1520. "myexits_unflagged (final)",
  1521. np,
  1522. nu);
  1523. f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT);
  1524. f_myexit_unflagged=
  1525. frac_nodes_with_descriptors(myexits_unflagged,WEIGHT_FOR_EXIT);
  1526. log_debug(LD_NET,
  1527. "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
  1528. f_exit,
  1529. f_myexit,
  1530. f_myexit_unflagged);
  1531. /* If our ExitNodes list has eliminated every possible Exit node, and there
  1532. * were some possible Exit nodes, then instead consider nodes that permit
  1533. * exiting to some ports. */
  1534. if (smartlist_len(myexits) == 0 &&
  1535. smartlist_len(myexits_unflagged)) {
  1536. f_myexit = f_myexit_unflagged;
  1537. }
  1538. smartlist_free(myexits);
  1539. smartlist_free(myexits_unflagged);
  1540. /* This is a tricky point here: we don't want to make it easy for a
  1541. * directory to trickle exits to us until it learns which exits we have
  1542. * configured, so require that we have a threshold both of total exits
  1543. * and usable exits. */
  1544. if (f_myexit < f_exit)
  1545. f_exit = f_myexit;
  1546. }
  1547. /* if the consensus has no exits, treat the exit fraction as 100% */
  1548. if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
  1549. f_exit = 1.0;
  1550. }
  1551. f_path = f_guard * f_mid * f_exit;
  1552. if (status_out)
  1553. tor_asprintf(status_out,
  1554. "%d%% of guards bw, "
  1555. "%d%% of midpoint bw, and "
  1556. "%d%% of exit bw%s = "
  1557. "%d%% of path bw",
  1558. (int)(f_guard*100),
  1559. (int)(f_mid*100),
  1560. (int)(f_exit*100),
  1561. (router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
  1562. "" :
  1563. " (no exits in consensus)"),
  1564. (int)(f_path*100));
  1565. return f_path;
  1566. }
  1567. /** We just fetched a new set of descriptors. Compute how far through
  1568. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1569. * the controller of our progress. */
  1570. int
  1571. count_loading_descriptors_progress(void)
  1572. {
  1573. int num_present = 0, num_usable=0;
  1574. time_t now = time(NULL);
  1575. const or_options_t *options = get_options();
  1576. const networkstatus_t *consensus =
  1577. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1578. double paths, fraction;
  1579. if (!consensus)
  1580. return 0; /* can't count descriptors if we have no list of them */
  1581. paths = compute_frac_paths_available(consensus, options, now,
  1582. &num_present, &num_usable,
  1583. NULL);
  1584. fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
  1585. if (fraction > 1.0)
  1586. return 0; /* it's not the number of descriptors holding us back */
  1587. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1588. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1589. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1590. }
  1591. /** Return the fraction of paths needed before we're willing to build
  1592. * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
  1593. static double
  1594. get_frac_paths_needed_for_circs(const or_options_t *options,
  1595. const networkstatus_t *ns)
  1596. {
  1597. #define DFLT_PCT_USABLE_NEEDED 60
  1598. if (options->PathsNeededToBuildCircuits >= 0.0) {
  1599. return options->PathsNeededToBuildCircuits;
  1600. } else {
  1601. return networkstatus_get_param(ns, "min_paths_for_circs_pct",
  1602. DFLT_PCT_USABLE_NEEDED,
  1603. 25, 95)/100.0;
  1604. }
  1605. }
  1606. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1607. * network and router information to build circuits. Clear the value of
  1608. * need_to_update_have_min_dir_info. */
  1609. static void
  1610. update_router_have_minimum_dir_info(void)
  1611. {
  1612. time_t now = time(NULL);
  1613. int res;
  1614. const or_options_t *options = get_options();
  1615. const networkstatus_t *consensus =
  1616. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1617. int using_md;
  1618. if (!consensus) {
  1619. if (!networkstatus_get_latest_consensus())
  1620. strlcpy(dir_info_status, "We have no usable consensus.",
  1621. sizeof(dir_info_status));
  1622. else
  1623. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1624. sizeof(dir_info_status));
  1625. res = 0;
  1626. goto done;
  1627. }
  1628. using_md = consensus->flavor == FLAV_MICRODESC;
  1629. /* Check fraction of available paths */
  1630. {
  1631. char *status = NULL;
  1632. int num_present=0, num_usable=0;
  1633. double paths = compute_frac_paths_available(consensus, options, now,
  1634. &num_present, &num_usable,
  1635. &status);
  1636. if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
  1637. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1638. "We need more %sdescriptors: we have %d/%d, and "
  1639. "can only build %d%% of likely paths. (We have %s.)",
  1640. using_md?"micro":"", num_present, num_usable,
  1641. (int)(paths*100), status);
  1642. tor_free(status);
  1643. res = 0;
  1644. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1645. goto done;
  1646. }
  1647. tor_free(status);
  1648. res = 1;
  1649. }
  1650. done:
  1651. /* If paths have just become available in this update. */
  1652. if (res && !have_min_dir_info) {
  1653. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1654. if (control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0) == 0) {
  1655. log_notice(LD_DIR,
  1656. "We now have enough directory information to build circuits.");
  1657. }
  1658. }
  1659. /* If paths have just become unavailable in this update. */
  1660. if (!res && have_min_dir_info) {
  1661. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1662. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1663. "Our directory information is no longer up-to-date "
  1664. "enough to build circuits: %s", dir_info_status);
  1665. /* a) make us log when we next complete a circuit, so we know when Tor
  1666. * is back up and usable, and b) disable some activities that Tor
  1667. * should only do while circuits are working, like reachability tests
  1668. * and fetching bridge descriptors only over circuits. */
  1669. note_that_we_maybe_cant_complete_circuits();
  1670. have_consensus_path = CONSENSUS_PATH_UNKNOWN;
  1671. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1672. }
  1673. have_min_dir_info = res;
  1674. need_to_update_have_min_dir_info = 0;
  1675. }