nodelist.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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-2013, 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. static void update_router_have_minimum_dir_info(void);
  25. static double get_frac_paths_needed_for_circs(const or_options_t *options,
  26. const networkstatus_t *ns);
  27. /** A nodelist_t holds a node_t object for every router we're "willing to use
  28. * for something". Specifically, it should hold a node_t for every node that
  29. * is currently in the routerlist, or currently in the consensus we're using.
  30. */
  31. typedef struct nodelist_t {
  32. /* A list of all the nodes. */
  33. smartlist_t *nodes;
  34. /* Hash table to map from node ID digest to node. */
  35. HT_HEAD(nodelist_map, node_t) nodes_by_id;
  36. } nodelist_t;
  37. static INLINE unsigned int
  38. node_id_hash(const node_t *node)
  39. {
  40. #if SIZEOF_INT == 4
  41. const uint32_t *p = (const uint32_t*)node->identity;
  42. return p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4];
  43. #elif SIZEOF_INT == 8
  44. const uint64_t *p = (const uint32_t*)node->identity;
  45. const uint32_t *p32 = (const uint32_t*)node->identity;
  46. return p[0] ^ p[1] ^ p32[4];
  47. #endif
  48. }
  49. static INLINE unsigned int
  50. node_id_eq(const node_t *node1, const node_t *node2)
  51. {
  52. return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
  53. }
  54. HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
  55. HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
  56. 0.6, malloc, realloc, free);
  57. /** The global nodelist. */
  58. static nodelist_t *the_nodelist=NULL;
  59. /** Create an empty nodelist if we haven't done so already. */
  60. static void
  61. init_nodelist(void)
  62. {
  63. if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
  64. the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
  65. HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
  66. the_nodelist->nodes = smartlist_new();
  67. }
  68. }
  69. /** As node_get_by_id, but returns a non-const pointer */
  70. node_t *
  71. node_get_mutable_by_id(const char *identity_digest)
  72. {
  73. node_t search, *node;
  74. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  75. return NULL;
  76. memcpy(&search.identity, identity_digest, DIGEST_LEN);
  77. node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
  78. return node;
  79. }
  80. /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
  81. * if no such node exists. */
  82. const node_t *
  83. node_get_by_id(const char *identity_digest)
  84. {
  85. return node_get_mutable_by_id(identity_digest);
  86. }
  87. /** Internal: return the node_t whose identity_digest is
  88. * <b>identity_digest</b>. If none exists, create a new one, add it to the
  89. * nodelist, and return it.
  90. *
  91. * Requires that the nodelist be initialized.
  92. */
  93. static node_t *
  94. node_get_or_create(const char *identity_digest)
  95. {
  96. node_t *node;
  97. if ((node = node_get_mutable_by_id(identity_digest)))
  98. return node;
  99. node = tor_malloc_zero(sizeof(node_t));
  100. memcpy(node->identity, identity_digest, DIGEST_LEN);
  101. HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
  102. smartlist_add(the_nodelist->nodes, node);
  103. node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
  104. node->country = -1;
  105. return node;
  106. }
  107. /** Called when a node's address changes. */
  108. static void
  109. node_addrs_changed(node_t *node)
  110. {
  111. node->last_reachable = node->last_reachable6 = 0;
  112. node->country = -1;
  113. }
  114. /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
  115. * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
  116. * to the previous routerinfo.
  117. */
  118. node_t *
  119. nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
  120. {
  121. node_t *node;
  122. const char *id_digest;
  123. int had_router = 0;
  124. tor_assert(ri);
  125. init_nodelist();
  126. id_digest = ri->cache_info.identity_digest;
  127. node = node_get_or_create(id_digest);
  128. if (node->ri) {
  129. if (!routers_have_same_or_addrs(node->ri, ri)) {
  130. node_addrs_changed(node);
  131. }
  132. had_router = 1;
  133. if (ri_old_out)
  134. *ri_old_out = node->ri;
  135. } else {
  136. if (ri_old_out)
  137. *ri_old_out = NULL;
  138. }
  139. node->ri = ri;
  140. if (node->country == -1)
  141. node_set_country(node);
  142. if (authdir_mode(get_options()) && !had_router) {
  143. const char *discard=NULL;
  144. uint32_t status = dirserv_router_get_status(ri, &discard);
  145. dirserv_set_node_flags_from_authoritative_status(node, status);
  146. }
  147. return node;
  148. }
  149. /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
  150. *
  151. * Called when a new microdesc has arrived and the usable consensus flavor
  152. * is "microdesc".
  153. **/
  154. node_t *
  155. nodelist_add_microdesc(microdesc_t *md)
  156. {
  157. networkstatus_t *ns =
  158. networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  159. const routerstatus_t *rs;
  160. node_t *node;
  161. if (ns == NULL)
  162. return NULL;
  163. init_nodelist();
  164. /* Microdescriptors don't carry an identity digest, so we need to figure
  165. * it out by looking up the routerstatus. */
  166. rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
  167. if (rs == NULL)
  168. return NULL;
  169. node = node_get_mutable_by_id(rs->identity_digest);
  170. if (node) {
  171. if (node->md)
  172. node->md->held_by_nodes--;
  173. node->md = md;
  174. md->held_by_nodes++;
  175. }
  176. return node;
  177. }
  178. /** Tell the nodelist that the current usable consensus is <b>ns</b>.
  179. * This makes the nodelist change all of the routerstatus entries for
  180. * the nodes, drop nodes that no longer have enough info to get used,
  181. * and grab microdescriptors into nodes as appropriate.
  182. */
  183. void
  184. nodelist_set_consensus(networkstatus_t *ns)
  185. {
  186. const or_options_t *options = get_options();
  187. int authdir = authdir_mode_v3(options);
  188. int client = !server_mode(options);
  189. init_nodelist();
  190. if (ns->flavor == FLAV_MICRODESC)
  191. (void) get_microdesc_cache(); /* Make sure it exists first. */
  192. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  193. node->rs = NULL);
  194. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  195. node_t *node = node_get_or_create(rs->identity_digest);
  196. node->rs = rs;
  197. if (ns->flavor == FLAV_MICRODESC) {
  198. if (node->md == NULL ||
  199. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  200. if (node->md)
  201. node->md->held_by_nodes--;
  202. node->md = microdesc_cache_lookup_by_digest256(NULL,
  203. rs->descriptor_digest);
  204. if (node->md)
  205. node->md->held_by_nodes++;
  206. }
  207. }
  208. node_set_country(node);
  209. /* If we're not an authdir, believe others. */
  210. if (!authdir) {
  211. node->is_valid = rs->is_valid;
  212. node->is_running = rs->is_flagged_running;
  213. node->is_fast = rs->is_fast;
  214. node->is_stable = rs->is_stable;
  215. node->is_possible_guard = rs->is_possible_guard;
  216. node->is_exit = rs->is_exit;
  217. node->is_bad_directory = rs->is_bad_directory;
  218. node->is_bad_exit = rs->is_bad_exit;
  219. node->is_hs_dir = rs->is_hs_dir;
  220. node->ipv6_preferred = 0;
  221. if (client && options->ClientPreferIPv6ORPort == 1 &&
  222. (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
  223. (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
  224. node->ipv6_preferred = 1;
  225. }
  226. } SMARTLIST_FOREACH_END(rs);
  227. nodelist_purge();
  228. if (! authdir) {
  229. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  230. /* We have no routerstatus for this router. Clear flags so we can skip
  231. * it, maybe.*/
  232. if (!node->rs) {
  233. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  234. * would have removed it. */
  235. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  236. /* Clear all flags. */
  237. node->is_valid = node->is_running = node->is_hs_dir =
  238. node->is_fast = node->is_stable =
  239. node->is_possible_guard = node->is_exit =
  240. node->is_bad_exit = node->is_bad_directory =
  241. node->ipv6_preferred = 0;
  242. }
  243. }
  244. } SMARTLIST_FOREACH_END(node);
  245. }
  246. }
  247. /** Helper: return true iff a node has a usable amount of information*/
  248. static INLINE int
  249. node_is_usable(const node_t *node)
  250. {
  251. return (node->rs) || (node->ri);
  252. }
  253. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  254. * node with <b>identity_digest</b>. */
  255. void
  256. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  257. {
  258. node_t *node = node_get_mutable_by_id(identity_digest);
  259. if (node && node->md == md) {
  260. node->md = NULL;
  261. md->held_by_nodes--;
  262. }
  263. }
  264. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  265. void
  266. nodelist_remove_routerinfo(routerinfo_t *ri)
  267. {
  268. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  269. if (node && node->ri == ri) {
  270. node->ri = NULL;
  271. if (! node_is_usable(node)) {
  272. nodelist_drop_node(node, 1);
  273. node_free(node);
  274. }
  275. }
  276. }
  277. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  278. * with.) */
  279. static void
  280. nodelist_drop_node(node_t *node, int remove_from_ht)
  281. {
  282. node_t *tmp;
  283. int idx;
  284. if (remove_from_ht) {
  285. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  286. tor_assert(tmp == node);
  287. }
  288. idx = node->nodelist_idx;
  289. tor_assert(idx >= 0);
  290. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  291. smartlist_del(the_nodelist->nodes, idx);
  292. if (idx < smartlist_len(the_nodelist->nodes)) {
  293. tmp = smartlist_get(the_nodelist->nodes, idx);
  294. tmp->nodelist_idx = idx;
  295. }
  296. node->nodelist_idx = -1;
  297. }
  298. /** Release storage held by <b>node</b> */
  299. static void
  300. node_free(node_t *node)
  301. {
  302. if (!node)
  303. return;
  304. if (node->md)
  305. node->md->held_by_nodes--;
  306. tor_assert(node->nodelist_idx == -1);
  307. tor_free(node);
  308. }
  309. /** Remove all entries from the nodelist that don't have enough info to be
  310. * usable for anything. */
  311. void
  312. nodelist_purge(void)
  313. {
  314. node_t **iter;
  315. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  316. return;
  317. /* Remove the non-usable nodes. */
  318. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  319. node_t *node = *iter;
  320. if (node->md && !node->rs) {
  321. /* An md is only useful if there is an rs. */
  322. node->md->held_by_nodes--;
  323. node->md = NULL;
  324. }
  325. if (node_is_usable(node)) {
  326. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  327. } else {
  328. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  329. nodelist_drop_node(node, 0);
  330. node_free(node);
  331. }
  332. }
  333. nodelist_assert_ok();
  334. }
  335. /** Release all storage held by the nodelist. */
  336. void
  337. nodelist_free_all(void)
  338. {
  339. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  340. return;
  341. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  342. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  343. node->nodelist_idx = -1;
  344. node_free(node);
  345. } SMARTLIST_FOREACH_END(node);
  346. smartlist_free(the_nodelist->nodes);
  347. tor_free(the_nodelist);
  348. }
  349. /** Check that the nodelist is internally consistent, and consistent with
  350. * the directory info it's derived from.
  351. */
  352. void
  353. nodelist_assert_ok(void)
  354. {
  355. routerlist_t *rl = router_get_routerlist();
  356. networkstatus_t *ns = networkstatus_get_latest_consensus();
  357. digestmap_t *dm;
  358. if (!the_nodelist)
  359. return;
  360. dm = digestmap_new();
  361. /* every routerinfo in rl->routers should be in the nodelist. */
  362. if (rl) {
  363. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  364. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  365. tor_assert(node && node->ri == ri);
  366. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  367. node->identity, DIGEST_LEN));
  368. tor_assert(! digestmap_get(dm, node->identity));
  369. digestmap_set(dm, node->identity, (void*)node);
  370. } SMARTLIST_FOREACH_END(ri);
  371. }
  372. /* every routerstatus in ns should be in the nodelist */
  373. if (ns) {
  374. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  375. const node_t *node = node_get_by_id(rs->identity_digest);
  376. tor_assert(node && node->rs == rs);
  377. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  378. digestmap_set(dm, node->identity, (void*)node);
  379. if (ns->flavor == FLAV_MICRODESC) {
  380. /* If it's a microdesc consensus, every entry that has a
  381. * microdescriptor should be in the nodelist.
  382. */
  383. microdesc_t *md =
  384. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  385. tor_assert(md == node->md);
  386. if (md)
  387. tor_assert(md->held_by_nodes >= 1);
  388. }
  389. } SMARTLIST_FOREACH_END(rs);
  390. }
  391. /* The nodelist should have no other entries, and its entries should be
  392. * well-formed. */
  393. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  394. tor_assert(digestmap_get(dm, node->identity) != NULL);
  395. tor_assert(node_sl_idx == node->nodelist_idx);
  396. } SMARTLIST_FOREACH_END(node);
  397. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  398. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  399. digestmap_free(dm, NULL);
  400. }
  401. /** Return a list of a node_t * for every node we know about. The caller
  402. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  403. * you must, but you must not add or remove nodes.) */
  404. smartlist_t *
  405. nodelist_get_list(void)
  406. {
  407. init_nodelist();
  408. return the_nodelist->nodes;
  409. }
  410. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  411. * or $DIGEST~name, return the node with the matching identity digest and
  412. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  413. * is not well-formed. */
  414. const node_t *
  415. node_get_by_hex_id(const char *hex_id)
  416. {
  417. char digest_buf[DIGEST_LEN];
  418. char nn_buf[MAX_NICKNAME_LEN+1];
  419. char nn_char='\0';
  420. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  421. const node_t *node = node_get_by_id(digest_buf);
  422. if (!node)
  423. return NULL;
  424. if (nn_char) {
  425. const char *real_name = node_get_nickname(node);
  426. if (!real_name || strcasecmp(real_name, nn_buf))
  427. return NULL;
  428. if (nn_char == '=') {
  429. const char *named_id =
  430. networkstatus_get_router_digest_by_nickname(nn_buf);
  431. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  432. return NULL;
  433. }
  434. }
  435. return node;
  436. }
  437. return NULL;
  438. }
  439. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  440. * the corresponding node_t, or NULL if none exists. Warn the user if
  441. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  442. * nickname, but the Named flag isn't set for that router. */
  443. const node_t *
  444. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  445. {
  446. const node_t *node;
  447. if (!the_nodelist)
  448. return NULL;
  449. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  450. if ((node = node_get_by_hex_id(nickname)) != NULL)
  451. return node;
  452. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  453. return NULL;
  454. /* Okay, so if we get here, the nickname is just a nickname. Is there
  455. * a binding for it in the consensus? */
  456. {
  457. const char *named_id =
  458. networkstatus_get_router_digest_by_nickname(nickname);
  459. if (named_id)
  460. return node_get_by_id(named_id);
  461. }
  462. /* Is it marked as owned-by-someone-else? */
  463. if (networkstatus_nickname_is_unnamed(nickname)) {
  464. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  465. "router that holds it, but not one listed in the current "
  466. "consensus.", escaped(nickname));
  467. return NULL;
  468. }
  469. /* Okay, so the name is not canonical for anybody. */
  470. {
  471. smartlist_t *matches = smartlist_new();
  472. const node_t *choice = NULL;
  473. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  474. if (!strcasecmp(node_get_nickname(node), nickname))
  475. smartlist_add(matches, node);
  476. } SMARTLIST_FOREACH_END(node);
  477. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  478. int any_unwarned = 0;
  479. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  480. if (!node->name_lookup_warned) {
  481. node->name_lookup_warned = 1;
  482. any_unwarned = 1;
  483. }
  484. } SMARTLIST_FOREACH_END(node);
  485. if (any_unwarned) {
  486. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  487. "but none is listed as Named in the directory consensus. "
  488. "Choosing one arbitrarily.", nickname);
  489. }
  490. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  491. char fp[HEX_DIGEST_LEN+1];
  492. node_t *node = smartlist_get(matches, 0);
  493. if (node->name_lookup_warned) {
  494. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  495. log_warn(LD_CONFIG,
  496. "You specified a server \"%s\" by name, but the directory "
  497. "authorities do not have any key registered for this "
  498. "nickname -- so it could be used by any server, not just "
  499. "the one you meant. "
  500. "To make sure you get the same server in the future, refer "
  501. "to it by key, as \"$%s\".", nickname, fp);
  502. node->name_lookup_warned = 1;
  503. }
  504. }
  505. if (smartlist_len(matches))
  506. choice = smartlist_get(matches, 0);
  507. smartlist_free(matches);
  508. return choice;
  509. }
  510. }
  511. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  512. const char *
  513. node_get_nickname(const node_t *node)
  514. {
  515. tor_assert(node);
  516. if (node->rs)
  517. return node->rs->nickname;
  518. else if (node->ri)
  519. return node->ri->nickname;
  520. else
  521. return NULL;
  522. }
  523. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  524. * latest consensus. */
  525. int
  526. node_is_named(const node_t *node)
  527. {
  528. const char *named_id;
  529. const char *nickname = node_get_nickname(node);
  530. if (!nickname)
  531. return 0;
  532. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  533. if (!named_id)
  534. return 0;
  535. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  536. }
  537. /** Return true iff <b>node</b> appears to be a directory authority or
  538. * directory cache */
  539. int
  540. node_is_dir(const node_t *node)
  541. {
  542. if (node->rs)
  543. return node->rs->dir_port != 0;
  544. else if (node->ri)
  545. return node->ri->dir_port != 0;
  546. else
  547. return 0;
  548. }
  549. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  550. * is, a routerdescriptor or a microdescriptor. */
  551. int
  552. node_has_descriptor(const node_t *node)
  553. {
  554. return (node->ri ||
  555. (node->rs && node->md));
  556. }
  557. /** Return the router_purpose of <b>node</b>. */
  558. int
  559. node_get_purpose(const node_t *node)
  560. {
  561. if (node->ri)
  562. return node->ri->purpose;
  563. else
  564. return ROUTER_PURPOSE_GENERAL;
  565. }
  566. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  567. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  568. * <b>verbose_name_out</b> */
  569. void
  570. node_get_verbose_nickname(const node_t *node,
  571. char *verbose_name_out)
  572. {
  573. const char *nickname = node_get_nickname(node);
  574. int is_named = node_is_named(node);
  575. verbose_name_out[0] = '$';
  576. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  577. DIGEST_LEN);
  578. if (!nickname)
  579. return;
  580. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  581. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  582. }
  583. /** Compute the verbose ("extended") nickname of node with
  584. * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
  585. * character buffer at <b>verbose_name_out</b>
  586. *
  587. * If node_get_by_id() returns NULL, base 16 encoding of
  588. * <b>id_digest</b> is returned instead. */
  589. void
  590. node_get_verbose_nickname_by_id(const char *id_digest,
  591. char *verbose_name_out)
  592. {
  593. const node_t *node = node_get_by_id(id_digest);
  594. if (!node) {
  595. verbose_name_out[0] = '$';
  596. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
  597. } else {
  598. node_get_verbose_nickname(node, verbose_name_out);
  599. }
  600. }
  601. /** Return true iff it seems that <b>node</b> allows circuits to exit
  602. * through it directlry from the client. */
  603. int
  604. node_allows_single_hop_exits(const node_t *node)
  605. {
  606. if (node && node->ri)
  607. return node->ri->allow_single_hop_exits;
  608. else
  609. return 0;
  610. }
  611. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  612. * actually permit anything to exit, or we don't know its exit policy */
  613. int
  614. node_exit_policy_rejects_all(const node_t *node)
  615. {
  616. if (node->rejects_all)
  617. return 1;
  618. if (node->ri)
  619. return node->ri->policy_is_reject_star;
  620. else if (node->md)
  621. return node->md->exit_policy == NULL ||
  622. short_policy_is_reject_star(node->md->exit_policy);
  623. else
  624. return 1;
  625. }
  626. /** Return true iff the exit policy for <b>node</b> is such that we can treat
  627. * rejecting an address of type <b>family</b> unexpectedly as a sign of that
  628. * node's failure. */
  629. int
  630. node_exit_policy_is_exact(const node_t *node, sa_family_t family)
  631. {
  632. if (family == AF_UNSPEC) {
  633. return 1; /* Rejecting an address but not telling us what address
  634. * is a bad sign. */
  635. } else if (family == AF_INET) {
  636. return node->ri != NULL;
  637. } else if (family == AF_INET6) {
  638. return 0;
  639. }
  640. tor_fragile_assert();
  641. return 1;
  642. }
  643. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  644. * addr + TCP port) for <b>node</b>. Caller must free all elements
  645. * using tor_free() and free the list using smartlist_free().
  646. *
  647. * XXX this is potentially a memory fragmentation hog -- if on
  648. * critical path consider the option of having the caller allocate the
  649. * memory
  650. */
  651. smartlist_t *
  652. node_get_all_orports(const node_t *node)
  653. {
  654. smartlist_t *sl = smartlist_new();
  655. if (node->ri != NULL) {
  656. if (node->ri->addr != 0) {
  657. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  658. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  659. ap->port = node->ri->or_port;
  660. smartlist_add(sl, ap);
  661. }
  662. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  663. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  664. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  665. ap->port = node->ri->or_port;
  666. smartlist_add(sl, ap);
  667. }
  668. } else if (node->rs != NULL) {
  669. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  670. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  671. ap->port = node->rs->or_port;
  672. smartlist_add(sl, ap);
  673. }
  674. return sl;
  675. }
  676. /** Wrapper around node_get_prim_orport for backward
  677. compatibility. */
  678. void
  679. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  680. {
  681. tor_addr_port_t ap;
  682. node_get_prim_orport(node, &ap);
  683. tor_addr_copy(addr_out, &ap.addr);
  684. }
  685. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  686. * seem to have one. */
  687. uint32_t
  688. node_get_prim_addr_ipv4h(const node_t *node)
  689. {
  690. if (node->ri) {
  691. return node->ri->addr;
  692. } else if (node->rs) {
  693. return node->rs->addr;
  694. }
  695. return 0;
  696. }
  697. /** Copy a string representation of an IP address for <b>node</b> into
  698. * the <b>len</b>-byte buffer at <b>buf</b>. */
  699. void
  700. node_get_address_string(const node_t *node, char *buf, size_t len)
  701. {
  702. if (node->ri) {
  703. strlcpy(buf, node->ri->address, len);
  704. } else if (node->rs) {
  705. tor_addr_t addr;
  706. tor_addr_from_ipv4h(&addr, node->rs->addr);
  707. tor_addr_to_str(buf, &addr, len, 0);
  708. } else {
  709. buf[0] = '\0';
  710. }
  711. }
  712. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  713. * one. */
  714. long
  715. node_get_declared_uptime(const node_t *node)
  716. {
  717. if (node->ri)
  718. return node->ri->uptime;
  719. else
  720. return -1;
  721. }
  722. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  723. const char *
  724. node_get_platform(const node_t *node)
  725. {
  726. /* If we wanted, we could record the version in the routerstatus_t, since
  727. * the consensus lists it. We don't, though, so this function just won't
  728. * work with microdescriptors. */
  729. if (node->ri)
  730. return node->ri->platform;
  731. else
  732. return NULL;
  733. }
  734. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  735. time_t
  736. node_get_published_on(const node_t *node)
  737. {
  738. if (node->ri)
  739. return node->ri->cache_info.published_on;
  740. else
  741. return 0;
  742. }
  743. /** Return true iff <b>node</b> is one representing this router. */
  744. int
  745. node_is_me(const node_t *node)
  746. {
  747. return router_digest_is_me(node->identity);
  748. }
  749. /** Return <b>node</b> declared family (as a list of names), or NULL if
  750. * the node didn't declare a family. */
  751. const smartlist_t *
  752. node_get_declared_family(const node_t *node)
  753. {
  754. if (node->ri && node->ri->declared_family)
  755. return node->ri->declared_family;
  756. else if (node->md && node->md->family)
  757. return node->md->family;
  758. else
  759. return NULL;
  760. }
  761. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  762. * <b>node</b>, else 0.
  763. *
  764. * We prefer the IPv6 address if the router has an IPv6 address and
  765. * i) the node_t says that it prefers IPv6
  766. * or
  767. * ii) the router has no IPv4 address. */
  768. int
  769. node_ipv6_preferred(const node_t *node)
  770. {
  771. tor_addr_port_t ipv4_addr;
  772. node_assert_ok(node);
  773. if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  774. if (node->ri)
  775. return !tor_addr_is_null(&node->ri->ipv6_addr);
  776. if (node->md)
  777. return !tor_addr_is_null(&node->md->ipv6_addr);
  778. if (node->rs)
  779. return !tor_addr_is_null(&node->rs->ipv6_addr);
  780. }
  781. return 0;
  782. }
  783. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  784. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  785. * port was copied, else return non-zero.*/
  786. int
  787. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  788. {
  789. node_assert_ok(node);
  790. tor_assert(ap_out);
  791. if (node->ri) {
  792. if (node->ri->addr == 0 || node->ri->or_port == 0)
  793. return -1;
  794. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  795. ap_out->port = node->ri->or_port;
  796. return 0;
  797. }
  798. if (node->rs) {
  799. if (node->rs->addr == 0 || node->rs->or_port == 0)
  800. return -1;
  801. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  802. ap_out->port = node->rs->or_port;
  803. return 0;
  804. }
  805. return -1;
  806. }
  807. /** Copy the preferred OR port (IP address and TCP port) for
  808. * <b>node</b> into *<b>ap_out</b>. */
  809. void
  810. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  811. {
  812. const or_options_t *options = get_options();
  813. tor_assert(ap_out);
  814. /* Cheap implementation of config option ClientUseIPv6 -- simply
  815. don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
  816. client running with bridges. See #4455 for more on this subject.
  817. Note that this filter is too strict since we're hindering not
  818. only clients! Erring on the safe side shouldn't be a problem
  819. though. XXX move this check to where outgoing connections are
  820. made? -LN */
  821. if ((options->ClientUseIPv6 || options->UseBridges) &&
  822. node_ipv6_preferred(node)) {
  823. node_get_pref_ipv6_orport(node, ap_out);
  824. } else {
  825. node_get_prim_orport(node, ap_out);
  826. }
  827. }
  828. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  829. * <b>node</b> into *<b>ap_out</b>. */
  830. void
  831. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  832. {
  833. node_assert_ok(node);
  834. tor_assert(ap_out);
  835. /* We prefer the microdesc over a potential routerstatus here. They
  836. are not being synchronised atm so there might be a chance that
  837. they differ at some point, f.ex. when flipping
  838. UseMicrodescriptors? -LN */
  839. if (node->ri) {
  840. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  841. ap_out->port = node->ri->ipv6_orport;
  842. } else if (node->md) {
  843. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  844. ap_out->port = node->md->ipv6_orport;
  845. } else if (node->rs) {
  846. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  847. ap_out->port = node->rs->ipv6_orport;
  848. }
  849. }
  850. /** Return true iff <b>node</b> has a curve25519 onion key. */
  851. int
  852. node_has_curve25519_onion_key(const node_t *node)
  853. {
  854. if (node->ri)
  855. return node->ri->onion_curve25519_pkey != NULL;
  856. else if (node->md)
  857. return node->md->onion_curve25519_pkey != NULL;
  858. else
  859. return 0;
  860. }
  861. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  862. * each router when the GeoIP database is reloaded, and on all new routers. */
  863. void
  864. node_set_country(node_t *node)
  865. {
  866. tor_addr_t addr = TOR_ADDR_NULL;
  867. /* XXXXipv6 */
  868. if (node->rs)
  869. tor_addr_from_ipv4h(&addr, node->rs->addr);
  870. else if (node->ri)
  871. tor_addr_from_ipv4h(&addr, node->ri->addr);
  872. node->country = geoip_get_country_by_addr(&addr);
  873. }
  874. /** Set the country code of all routers in the routerlist. */
  875. void
  876. nodelist_refresh_countries(void)
  877. {
  878. smartlist_t *nodes = nodelist_get_list();
  879. SMARTLIST_FOREACH(nodes, node_t *, node,
  880. node_set_country(node));
  881. }
  882. /** Return true iff router1 and router2 have similar enough network addresses
  883. * that we should treat them as being in the same family */
  884. static INLINE int
  885. addrs_in_same_network_family(const tor_addr_t *a1,
  886. const tor_addr_t *a2)
  887. {
  888. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  889. }
  890. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  891. * (case-insensitive), or if <b>node's</b> identity key digest
  892. * matches a hexadecimal value stored in <b>nickname</b>. Return
  893. * false otherwise. */
  894. static int
  895. node_nickname_matches(const node_t *node, const char *nickname)
  896. {
  897. const char *n = node_get_nickname(node);
  898. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  899. return 1;
  900. return hex_digest_nickname_matches(nickname,
  901. node->identity,
  902. n,
  903. node_is_named(node));
  904. }
  905. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  906. static INLINE int
  907. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  908. {
  909. if (!lst) return 0;
  910. SMARTLIST_FOREACH(lst, const char *, name, {
  911. if (node_nickname_matches(node, name))
  912. return 1;
  913. });
  914. return 0;
  915. }
  916. /** Return true iff r1 and r2 are in the same family, but not the same
  917. * router. */
  918. int
  919. nodes_in_same_family(const node_t *node1, const node_t *node2)
  920. {
  921. const or_options_t *options = get_options();
  922. /* Are they in the same family because of their addresses? */
  923. if (options->EnforceDistinctSubnets) {
  924. tor_addr_t a1, a2;
  925. node_get_addr(node1, &a1);
  926. node_get_addr(node2, &a2);
  927. if (addrs_in_same_network_family(&a1, &a2))
  928. return 1;
  929. }
  930. /* Are they in the same family because the agree they are? */
  931. {
  932. const smartlist_t *f1, *f2;
  933. f1 = node_get_declared_family(node1);
  934. f2 = node_get_declared_family(node2);
  935. if (f1 && f2 &&
  936. node_in_nickname_smartlist(f1, node2) &&
  937. node_in_nickname_smartlist(f2, node1))
  938. return 1;
  939. }
  940. /* Are they in the same option because the user says they are? */
  941. if (options->NodeFamilySets) {
  942. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  943. if (routerset_contains_node(rs, node1) &&
  944. routerset_contains_node(rs, node2))
  945. return 1;
  946. });
  947. }
  948. return 0;
  949. }
  950. /**
  951. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  952. * the smartlist <b>sl</b>.
  953. *
  954. * This is used to make sure we don't pick siblings in a single path, or
  955. * pick more than one relay from a family for our entry guard list.
  956. * Note that a node may be added to <b>sl</b> more than once if it is
  957. * part of <b>node</b>'s family for more than one reason.
  958. */
  959. void
  960. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  961. {
  962. const smartlist_t *all_nodes = nodelist_get_list();
  963. const smartlist_t *declared_family;
  964. const or_options_t *options = get_options();
  965. tor_assert(node);
  966. declared_family = node_get_declared_family(node);
  967. /* Let's make sure that we have the node itself, if it's a real node. */
  968. {
  969. const node_t *real_node = node_get_by_id(node->identity);
  970. if (real_node)
  971. smartlist_add(sl, (node_t*)real_node);
  972. }
  973. /* First, add any nodes with similar network addresses. */
  974. if (options->EnforceDistinctSubnets) {
  975. tor_addr_t node_addr;
  976. node_get_addr(node, &node_addr);
  977. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  978. tor_addr_t a;
  979. node_get_addr(node2, &a);
  980. if (addrs_in_same_network_family(&a, &node_addr))
  981. smartlist_add(sl, (void*)node2);
  982. } SMARTLIST_FOREACH_END(node2);
  983. }
  984. /* Now, add all nodes in the declared_family of this node, if they
  985. * also declare this node to be in their family. */
  986. if (declared_family) {
  987. /* Add every r such that router declares familyness with node, and node
  988. * declares familyhood with router. */
  989. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  990. const node_t *node2;
  991. const smartlist_t *family2;
  992. if (!(node2 = node_get_by_nickname(name, 0)))
  993. continue;
  994. if (!(family2 = node_get_declared_family(node2)))
  995. continue;
  996. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  997. if (node_nickname_matches(node, name2)) {
  998. smartlist_add(sl, (void*)node2);
  999. break;
  1000. }
  1001. } SMARTLIST_FOREACH_END(name2);
  1002. } SMARTLIST_FOREACH_END(name);
  1003. }
  1004. /* If the user declared any families locally, honor those too. */
  1005. if (options->NodeFamilySets) {
  1006. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  1007. if (routerset_contains_node(rs, node)) {
  1008. routerset_get_all_nodes(sl, rs, NULL, 0);
  1009. }
  1010. });
  1011. }
  1012. }
  1013. /** Find a router that's up, that has this IP address, and
  1014. * that allows exit to this address:port, or return NULL if there
  1015. * isn't a good one.
  1016. * Don't exit enclave to excluded relays -- it wouldn't actually
  1017. * hurt anything, but this way there are fewer confused users.
  1018. */
  1019. const node_t *
  1020. router_find_exact_exit_enclave(const char *address, uint16_t port)
  1021. {/*XXXX MOVE*/
  1022. uint32_t addr;
  1023. struct in_addr in;
  1024. tor_addr_t a;
  1025. const or_options_t *options = get_options();
  1026. if (!tor_inet_aton(address, &in))
  1027. return NULL; /* it's not an IP already */
  1028. addr = ntohl(in.s_addr);
  1029. tor_addr_from_ipv4h(&a, addr);
  1030. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  1031. if (node_get_addr_ipv4h(node) == addr &&
  1032. node->is_running &&
  1033. compare_tor_addr_to_node_policy(&a, port, node) ==
  1034. ADDR_POLICY_ACCEPTED &&
  1035. !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
  1036. return node;
  1037. });
  1038. return NULL;
  1039. }
  1040. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  1041. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  1042. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  1043. * bandwidth.
  1044. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  1045. */
  1046. int
  1047. node_is_unreliable(const node_t *node, int need_uptime,
  1048. int need_capacity, int need_guard)
  1049. {
  1050. if (need_uptime && !node->is_stable)
  1051. return 1;
  1052. if (need_capacity && !node->is_fast)
  1053. return 1;
  1054. if (need_guard && !node->is_possible_guard)
  1055. return 1;
  1056. return 0;
  1057. }
  1058. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1059. * addr:port. Return 0 if any might accept it. */
  1060. int
  1061. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1062. int need_uptime)
  1063. {
  1064. addr_policy_result_t r;
  1065. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1066. if (node->is_running &&
  1067. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1068. r = compare_tor_addr_to_node_policy(addr, port, node);
  1069. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1070. return 0; /* this one could be ok. good enough. */
  1071. }
  1072. } SMARTLIST_FOREACH_END(node);
  1073. return 1; /* all will reject. */
  1074. }
  1075. /** Mark the router with ID <b>digest</b> as running or non-running
  1076. * in our routerlist. */
  1077. void
  1078. router_set_status(const char *digest, int up)
  1079. {
  1080. node_t *node;
  1081. tor_assert(digest);
  1082. SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
  1083. dir_server_t *, d,
  1084. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1085. d->is_running = up);
  1086. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1087. dir_server_t *, d,
  1088. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1089. d->is_running = up);
  1090. node = node_get_mutable_by_id(digest);
  1091. if (node) {
  1092. #if 0
  1093. log_debug(LD_DIR,"Marking router %s as %s.",
  1094. node_describe(node), up ? "up" : "down");
  1095. #endif
  1096. if (!up && node_is_me(node) && !net_is_disabled())
  1097. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1098. "addresses reachable?");
  1099. node->is_running = up;
  1100. }
  1101. router_dir_info_changed();
  1102. }
  1103. /** True iff, the last time we checked whether we had enough directory info
  1104. * to build circuits, the answer was "yes". */
  1105. static int have_min_dir_info = 0;
  1106. /** True iff enough has changed since the last time we checked whether we had
  1107. * enough directory info to build circuits that our old answer can no longer
  1108. * be trusted. */
  1109. static int need_to_update_have_min_dir_info = 1;
  1110. /** String describing what we're missing before we have enough directory
  1111. * info. */
  1112. static char dir_info_status[256] = "";
  1113. /** Return true iff we have enough networkstatus and router information to
  1114. * start building circuits. Right now, this means "more than half the
  1115. * networkstatus documents, and at least 1/4 of expected routers." */
  1116. //XXX should consider whether we have enough exiting nodes here.
  1117. int
  1118. router_have_minimum_dir_info(void)
  1119. {
  1120. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1121. update_router_have_minimum_dir_info();
  1122. need_to_update_have_min_dir_info = 0;
  1123. }
  1124. return have_min_dir_info;
  1125. }
  1126. /** Called when our internal view of the directory has changed. This can be
  1127. * when the authorities change, networkstatuses change, the list of routerdescs
  1128. * changes, or number of running routers changes.
  1129. */
  1130. void
  1131. router_dir_info_changed(void)
  1132. {
  1133. need_to_update_have_min_dir_info = 1;
  1134. rend_hsdir_routers_changed();
  1135. }
  1136. /** Return a string describing what we're missing before we have enough
  1137. * directory info. */
  1138. const char *
  1139. get_dir_info_status_string(void)
  1140. {
  1141. return dir_info_status;
  1142. }
  1143. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1144. * them seem like ones we'd use, and how many of <em>those</em> we have
  1145. * descriptors for. Store the former in *<b>num_usable</b> and the latter in
  1146. * *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
  1147. * routers in <b>in_set</b>. If <b>exit_only</b> is true, only consider nodes
  1148. * with the Exit flag. If *descs_out is present, add a node_t for each
  1149. * usable descriptor to it.
  1150. */
  1151. static void
  1152. count_usable_descriptors(int *num_present, int *num_usable,
  1153. smartlist_t *descs_out,
  1154. const networkstatus_t *consensus,
  1155. const or_options_t *options, time_t now,
  1156. routerset_t *in_set, int exit_only)
  1157. {
  1158. const int md = (consensus->flavor == FLAV_MICRODESC);
  1159. *num_present = 0, *num_usable=0;
  1160. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1161. {
  1162. const node_t *node = node_get_by_id(rs->identity_digest);
  1163. if (!node)
  1164. continue; /* This would be a bug: every entry in the consensus is
  1165. * supposed to have a node. */
  1166. if (exit_only && ! rs->is_exit)
  1167. continue;
  1168. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1169. continue;
  1170. if (client_would_use_router(rs, now, options)) {
  1171. const char * const digest = rs->descriptor_digest;
  1172. int present;
  1173. ++*num_usable; /* the consensus says we want it. */
  1174. if (md)
  1175. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1176. else
  1177. present = NULL != router_get_by_descriptor_digest(digest);
  1178. if (present) {
  1179. /* we have the descriptor listed in the consensus. */
  1180. ++*num_present;
  1181. }
  1182. if (descs_out)
  1183. smartlist_add(descs_out, (node_t*)node);
  1184. }
  1185. }
  1186. SMARTLIST_FOREACH_END(rs);
  1187. log_debug(LD_DIR, "%d usable, %d present (%s%s).",
  1188. *num_usable, *num_present,
  1189. md ? "microdesc" : "desc", exit_only ? " exits" : "s");
  1190. }
  1191. /** Return an estimate of which fraction of usable paths through the Tor
  1192. * network we have available for use. */
  1193. static double
  1194. compute_frac_paths_available(const networkstatus_t *consensus,
  1195. const or_options_t *options, time_t now,
  1196. int *num_present_out, int *num_usable_out,
  1197. char **status_out)
  1198. {
  1199. smartlist_t *guards = smartlist_new();
  1200. smartlist_t *mid = smartlist_new();
  1201. smartlist_t *exits = smartlist_new();
  1202. smartlist_t *myexits= smartlist_new();
  1203. double f_guard, f_mid, f_exit, f_myexit;
  1204. int np, nu; /* Ignored */
  1205. const int authdir = authdir_mode_v3(options);
  1206. count_usable_descriptors(num_present_out, num_usable_out,
  1207. mid, consensus, options, now, NULL, 0);
  1208. if (options->EntryNodes) {
  1209. count_usable_descriptors(&np, &nu, guards, consensus, options, now,
  1210. options->EntryNodes, 0);
  1211. } else {
  1212. SMARTLIST_FOREACH(mid, const node_t *, node, {
  1213. if (authdir) {
  1214. if (node->rs && node->rs->is_possible_guard)
  1215. smartlist_add(guards, (node_t*)node);
  1216. } else {
  1217. if (node->is_possible_guard)
  1218. smartlist_add(guards, (node_t*)node);
  1219. }
  1220. });
  1221. }
  1222. count_usable_descriptors(&np, &nu, exits, consensus, options, now,
  1223. NULL, 1);
  1224. count_usable_descriptors(&np, &nu, myexits, consensus, options, now,
  1225. options->ExitNodes, 1);
  1226. f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD);
  1227. f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID);
  1228. f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT);
  1229. f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT);
  1230. smartlist_free(guards);
  1231. smartlist_free(mid);
  1232. smartlist_free(exits);
  1233. smartlist_free(myexits);
  1234. /* This is a tricky point here: we don't want to make it easy for a
  1235. * directory to trickle exits to us until it learns which exits we have
  1236. * configured, so require that we have a threshold both of total exits
  1237. * and usable exits. */
  1238. if (f_myexit < f_exit)
  1239. f_exit = f_myexit;
  1240. if (status_out)
  1241. tor_asprintf(status_out,
  1242. "%d%% of guards bw, "
  1243. "%d%% of midpoint bw, and "
  1244. "%d%% of exit bw",
  1245. (int)(f_guard*100),
  1246. (int)(f_mid*100),
  1247. (int)(f_exit*100));
  1248. return f_guard * f_mid * f_exit;
  1249. }
  1250. /** We just fetched a new set of descriptors. Compute how far through
  1251. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1252. * the controller of our progress. */
  1253. int
  1254. count_loading_descriptors_progress(void)
  1255. {
  1256. int num_present = 0, num_usable=0;
  1257. time_t now = time(NULL);
  1258. const or_options_t *options = get_options();
  1259. const networkstatus_t *consensus =
  1260. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1261. double paths, fraction;
  1262. if (!consensus)
  1263. return 0; /* can't count descriptors if we have no list of them */
  1264. paths = compute_frac_paths_available(consensus, options, now,
  1265. &num_present, &num_usable,
  1266. NULL);
  1267. fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
  1268. if (fraction > 1.0)
  1269. return 0; /* it's not the number of descriptors holding us back */
  1270. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1271. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1272. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1273. }
  1274. /** Return the fraction of paths needed before we're willing to build
  1275. * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
  1276. static double
  1277. get_frac_paths_needed_for_circs(const or_options_t *options,
  1278. const networkstatus_t *ns)
  1279. {
  1280. #define DFLT_PCT_USABLE_NEEDED 60
  1281. if (options->PathsNeededToBuildCircuits >= 0.0) {
  1282. return options->PathsNeededToBuildCircuits;
  1283. } else {
  1284. return networkstatus_get_param(ns, "min_paths_for_circs_pct",
  1285. DFLT_PCT_USABLE_NEEDED,
  1286. 25, 95)/100.0;
  1287. }
  1288. }
  1289. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1290. * network and router information to build circuits. Clear the value of
  1291. * need_to_update_have_min_dir_info. */
  1292. static void
  1293. update_router_have_minimum_dir_info(void)
  1294. {
  1295. time_t now = time(NULL);
  1296. int res;
  1297. const or_options_t *options = get_options();
  1298. const networkstatus_t *consensus =
  1299. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1300. int using_md;
  1301. if (!consensus) {
  1302. if (!networkstatus_get_latest_consensus())
  1303. strlcpy(dir_info_status, "We have no usable consensus.",
  1304. sizeof(dir_info_status));
  1305. else
  1306. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1307. sizeof(dir_info_status));
  1308. res = 0;
  1309. goto done;
  1310. }
  1311. if (should_delay_dir_fetches(get_options())) {
  1312. log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
  1313. strlcpy(dir_info_status, "No live bridge descriptors.",
  1314. sizeof(dir_info_status));
  1315. res = 0;
  1316. goto done;
  1317. }
  1318. using_md = consensus->flavor == FLAV_MICRODESC;
  1319. {
  1320. char *status = NULL;
  1321. int num_present=0, num_usable=0;
  1322. double paths = compute_frac_paths_available(consensus, options, now,
  1323. &num_present, &num_usable,
  1324. &status);
  1325. if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
  1326. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1327. "We need more %sdescriptors: we have %d/%d, and "
  1328. "can only build %d%% of likely paths. (We have %s.)",
  1329. using_md?"micro":"", num_present, num_usable,
  1330. (int)(paths*100), status);
  1331. /* log_notice(LD_NET, "%s", dir_info_status); */
  1332. tor_free(status);
  1333. res = 0;
  1334. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1335. goto done;
  1336. }
  1337. tor_free(status);
  1338. res = 1;
  1339. }
  1340. done:
  1341. if (res && !have_min_dir_info) {
  1342. log_notice(LD_DIR,
  1343. "We now have enough directory information to build circuits.");
  1344. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1345. control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
  1346. }
  1347. if (!res && have_min_dir_info) {
  1348. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1349. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1350. "Our directory information is no longer up-to-date "
  1351. "enough to build circuits: %s", dir_info_status);
  1352. /* a) make us log when we next complete a circuit, so we know when Tor
  1353. * is back up and usable, and b) disable some activities that Tor
  1354. * should only do while circuits are working, like reachability tests
  1355. * and fetching bridge descriptors only over circuits. */
  1356. can_complete_circuit = 0;
  1357. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1358. }
  1359. have_min_dir_info = res;
  1360. need_to_update_have_min_dir_info = 0;
  1361. }