nodelist.c 54 KB

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