nodelist.c 46 KB

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