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