nodelist.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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-2012, 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. /** Release storage held by <b>node</b> */
  297. static void
  298. node_free(node_t *node)
  299. {
  300. if (!node)
  301. return;
  302. if (node->md)
  303. node->md->held_by_nodes--;
  304. tor_assert(node->nodelist_idx == -1);
  305. tor_free(node);
  306. }
  307. /** Remove all entries from the nodelist that don't have enough info to be
  308. * usable for anything. */
  309. void
  310. nodelist_purge(void)
  311. {
  312. node_t **iter;
  313. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  314. return;
  315. /* Remove the non-usable nodes. */
  316. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  317. node_t *node = *iter;
  318. if (node->md && !node->rs) {
  319. /* An md is only useful if there is an rs. */
  320. node->md->held_by_nodes--;
  321. node->md = NULL;
  322. }
  323. if (node_is_usable(node)) {
  324. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  325. } else {
  326. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  327. nodelist_drop_node(node, 0);
  328. node_free(node);
  329. }
  330. }
  331. nodelist_assert_ok();
  332. }
  333. /** Release all storage held by the nodelist. */
  334. void
  335. nodelist_free_all(void)
  336. {
  337. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  338. return;
  339. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  340. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  341. node->nodelist_idx = -1;
  342. node_free(node);
  343. } SMARTLIST_FOREACH_END(node);
  344. smartlist_free(the_nodelist->nodes);
  345. tor_free(the_nodelist);
  346. }
  347. /** Check that the nodelist is internally consistent, and consistent with
  348. * the directory info it's derived from.
  349. */
  350. void
  351. nodelist_assert_ok(void)
  352. {
  353. routerlist_t *rl = router_get_routerlist();
  354. networkstatus_t *ns = networkstatus_get_latest_consensus();
  355. digestmap_t *dm;
  356. if (!the_nodelist)
  357. return;
  358. dm = digestmap_new();
  359. /* every routerinfo in rl->routers should be in the nodelist. */
  360. if (rl) {
  361. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  362. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  363. tor_assert(node && node->ri == ri);
  364. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  365. node->identity, DIGEST_LEN));
  366. tor_assert(! digestmap_get(dm, node->identity));
  367. digestmap_set(dm, node->identity, (void*)node);
  368. } SMARTLIST_FOREACH_END(ri);
  369. }
  370. /* every routerstatus in ns should be in the nodelist */
  371. if (ns) {
  372. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  373. const node_t *node = node_get_by_id(rs->identity_digest);
  374. tor_assert(node && node->rs == rs);
  375. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  376. digestmap_set(dm, node->identity, (void*)node);
  377. if (ns->flavor == FLAV_MICRODESC) {
  378. /* If it's a microdesc consensus, every entry that has a
  379. * microdescriptor should be in the nodelist.
  380. */
  381. microdesc_t *md =
  382. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  383. tor_assert(md == node->md);
  384. if (md)
  385. tor_assert(md->held_by_nodes >= 1);
  386. }
  387. } SMARTLIST_FOREACH_END(rs);
  388. }
  389. /* The nodelist should have no other entries, and its entries should be
  390. * well-formed. */
  391. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  392. tor_assert(digestmap_get(dm, node->identity) != NULL);
  393. tor_assert(node_sl_idx == node->nodelist_idx);
  394. } SMARTLIST_FOREACH_END(node);
  395. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  396. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  397. digestmap_free(dm, NULL);
  398. }
  399. /** Return a list of a node_t * for every node we know about. The caller
  400. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  401. * you must, but you must not add or remove nodes.) */
  402. smartlist_t *
  403. nodelist_get_list(void)
  404. {
  405. init_nodelist();
  406. return the_nodelist->nodes;
  407. }
  408. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  409. * or $DIGEST~name, return the node with the matching identity digest and
  410. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  411. * is not well-formed. */
  412. const node_t *
  413. node_get_by_hex_id(const char *hex_id)
  414. {
  415. char digest_buf[DIGEST_LEN];
  416. char nn_buf[MAX_NICKNAME_LEN+1];
  417. char nn_char='\0';
  418. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  419. const node_t *node = node_get_by_id(digest_buf);
  420. if (!node)
  421. return NULL;
  422. if (nn_char) {
  423. const char *real_name = node_get_nickname(node);
  424. if (!real_name || strcasecmp(real_name, nn_buf))
  425. return NULL;
  426. if (nn_char == '=') {
  427. const char *named_id =
  428. networkstatus_get_router_digest_by_nickname(nn_buf);
  429. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  430. return NULL;
  431. }
  432. }
  433. return node;
  434. }
  435. return NULL;
  436. }
  437. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  438. * the corresponding node_t, or NULL if none exists. Warn the user if
  439. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  440. * nickname, but the Named flag isn't set for that router. */
  441. const node_t *
  442. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  443. {
  444. const node_t *node;
  445. if (!the_nodelist)
  446. return NULL;
  447. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  448. if ((node = node_get_by_hex_id(nickname)) != NULL)
  449. return node;
  450. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  451. return NULL;
  452. /* Okay, so if we get here, the nickname is just a nickname. Is there
  453. * a binding for it in the consensus? */
  454. {
  455. const char *named_id =
  456. networkstatus_get_router_digest_by_nickname(nickname);
  457. if (named_id)
  458. return node_get_by_id(named_id);
  459. }
  460. /* Is it marked as owned-by-someone-else? */
  461. if (networkstatus_nickname_is_unnamed(nickname)) {
  462. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  463. "router that holds it, but not one listed in the current "
  464. "consensus.", escaped(nickname));
  465. return NULL;
  466. }
  467. /* Okay, so the name is not canonical for anybody. */
  468. {
  469. smartlist_t *matches = smartlist_new();
  470. const node_t *choice = NULL;
  471. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  472. if (!strcasecmp(node_get_nickname(node), nickname))
  473. smartlist_add(matches, node);
  474. } SMARTLIST_FOREACH_END(node);
  475. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  476. int any_unwarned = 0;
  477. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  478. if (!node->name_lookup_warned) {
  479. node->name_lookup_warned = 1;
  480. any_unwarned = 1;
  481. }
  482. } SMARTLIST_FOREACH_END(node);
  483. if (any_unwarned) {
  484. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  485. "but none is listed as Named in the directory consensus. "
  486. "Choosing one arbitrarily.", nickname);
  487. }
  488. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  489. char fp[HEX_DIGEST_LEN+1];
  490. node_t *node = smartlist_get(matches, 0);
  491. if (node->name_lookup_warned) {
  492. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  493. log_warn(LD_CONFIG,
  494. "You specified a server \"%s\" by name, but the directory "
  495. "authorities do not have any key registered for this "
  496. "nickname -- so it could be used by any server, not just "
  497. "the one you meant. "
  498. "To make sure you get the same server in the future, refer "
  499. "to it by key, as \"$%s\".", nickname, fp);
  500. node->name_lookup_warned = 1;
  501. }
  502. }
  503. if (smartlist_len(matches))
  504. choice = smartlist_get(matches, 0);
  505. smartlist_free(matches);
  506. return choice;
  507. }
  508. }
  509. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  510. const char *
  511. node_get_nickname(const node_t *node)
  512. {
  513. tor_assert(node);
  514. if (node->rs)
  515. return node->rs->nickname;
  516. else if (node->ri)
  517. return node->ri->nickname;
  518. else
  519. return NULL;
  520. }
  521. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  522. * latest consensus. */
  523. int
  524. node_is_named(const node_t *node)
  525. {
  526. const char *named_id;
  527. const char *nickname = node_get_nickname(node);
  528. if (!nickname)
  529. return 0;
  530. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  531. if (!named_id)
  532. return 0;
  533. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  534. }
  535. /** Return true iff <b>node</b> appears to be a directory authority or
  536. * directory cache */
  537. int
  538. node_is_dir(const node_t *node)
  539. {
  540. if (node->rs)
  541. return node->rs->dir_port != 0;
  542. else if (node->ri)
  543. return node->ri->dir_port != 0;
  544. else
  545. return 0;
  546. }
  547. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  548. * is, a routerdecriptor or a microdescriptor. */
  549. int
  550. node_has_descriptor(const node_t *node)
  551. {
  552. return (node->ri ||
  553. (node->rs && node->md));
  554. }
  555. /** Return the router_purpose of <b>node</b>. */
  556. int
  557. node_get_purpose(const node_t *node)
  558. {
  559. if (node->ri)
  560. return node->ri->purpose;
  561. else
  562. return ROUTER_PURPOSE_GENERAL;
  563. }
  564. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  565. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  566. * <b>verbose_nickname_out</b> */
  567. void
  568. node_get_verbose_nickname(const node_t *node,
  569. char *verbose_name_out)
  570. {
  571. const char *nickname = node_get_nickname(node);
  572. int is_named = node_is_named(node);
  573. verbose_name_out[0] = '$';
  574. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  575. DIGEST_LEN);
  576. if (!nickname)
  577. return;
  578. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  579. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  580. }
  581. /** Return true iff it seems that <b>node</b> allows circuits to exit
  582. * through it directlry from the client. */
  583. int
  584. node_allows_single_hop_exits(const node_t *node)
  585. {
  586. if (node && node->ri)
  587. return node->ri->allow_single_hop_exits;
  588. else
  589. return 0;
  590. }
  591. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  592. * actually permit anything to exit, or we don't know its exit policy */
  593. int
  594. node_exit_policy_rejects_all(const node_t *node)
  595. {
  596. if (node->rejects_all)
  597. return 1;
  598. if (node->ri)
  599. return node->ri->policy_is_reject_star;
  600. else if (node->md)
  601. return node->md->exit_policy == NULL ||
  602. short_policy_is_reject_star(node->md->exit_policy);
  603. else
  604. return 1;
  605. }
  606. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  607. * addr + TCP port) for <b>node</b>. Caller must free all elements
  608. * using tor_free() and free the list using smartlist_free().
  609. *
  610. * XXX this is potentially a memory fragmentation hog -- if on
  611. * critical path consider the option of having the caller allocate the
  612. * memory
  613. */
  614. smartlist_t *
  615. node_get_all_orports(const node_t *node)
  616. {
  617. smartlist_t *sl = smartlist_new();
  618. if (node->ri != NULL) {
  619. if (node->ri->addr != 0) {
  620. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  621. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  622. ap->port = node->ri->or_port;
  623. smartlist_add(sl, ap);
  624. }
  625. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  626. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  627. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  628. ap->port = node->ri->or_port;
  629. smartlist_add(sl, ap);
  630. }
  631. } else if (node->rs != NULL) {
  632. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  633. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  634. ap->port = node->rs->or_port;
  635. smartlist_add(sl, ap);
  636. }
  637. return sl;
  638. }
  639. /** Wrapper around node_get_prim_orport for backward
  640. compatibility. */
  641. void
  642. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  643. {
  644. tor_addr_port_t ap;
  645. node_get_prim_orport(node, &ap);
  646. tor_addr_copy(addr_out, &ap.addr);
  647. }
  648. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  649. * seem to have one. */
  650. uint32_t
  651. node_get_prim_addr_ipv4h(const node_t *node)
  652. {
  653. if (node->ri) {
  654. return node->ri->addr;
  655. } else if (node->rs) {
  656. return node->rs->addr;
  657. }
  658. return 0;
  659. }
  660. /** Copy a string representation of an IP address for <b>node</b> into
  661. * the <b>len</b>-byte buffer at <b>buf</b>. */
  662. void
  663. node_get_address_string(const node_t *node, char *buf, size_t len)
  664. {
  665. if (node->ri) {
  666. strlcpy(buf, node->ri->address, len);
  667. } else if (node->rs) {
  668. tor_addr_t addr;
  669. tor_addr_from_ipv4h(&addr, node->rs->addr);
  670. tor_addr_to_str(buf, &addr, len, 0);
  671. } else {
  672. buf[0] = '\0';
  673. }
  674. }
  675. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  676. * one. */
  677. long
  678. node_get_declared_uptime(const node_t *node)
  679. {
  680. if (node->ri)
  681. return node->ri->uptime;
  682. else
  683. return -1;
  684. }
  685. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  686. const char *
  687. node_get_platform(const node_t *node)
  688. {
  689. /* If we wanted, we could record the version in the routerstatus_t, since
  690. * the consensus lists it. We don't, though, so this function just won't
  691. * work with microdescriptors. */
  692. if (node->ri)
  693. return node->ri->platform;
  694. else
  695. return NULL;
  696. }
  697. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  698. time_t
  699. node_get_published_on(const node_t *node)
  700. {
  701. if (node->ri)
  702. return node->ri->cache_info.published_on;
  703. else
  704. return 0;
  705. }
  706. /** Return true iff <b>node</b> is one representing this router. */
  707. int
  708. node_is_me(const node_t *node)
  709. {
  710. return router_digest_is_me(node->identity);
  711. }
  712. /** Return <b>node</b> declared family (as a list of names), or NULL if
  713. * the node didn't declare a family. */
  714. const smartlist_t *
  715. node_get_declared_family(const node_t *node)
  716. {
  717. if (node->ri && node->ri->declared_family)
  718. return node->ri->declared_family;
  719. else if (node->md && node->md->family)
  720. return node->md->family;
  721. else
  722. return NULL;
  723. }
  724. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  725. * <b>node</b>, else 0.
  726. *
  727. * We prefer the IPv6 address if the router has an IPv6 address and
  728. * i) the node_t says that it prefers IPv6
  729. * or
  730. * ii) the router has no IPv4 address. */
  731. int
  732. node_ipv6_preferred(const node_t *node)
  733. {
  734. tor_addr_port_t ipv4_addr;
  735. node_assert_ok(node);
  736. if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  737. if (node->ri)
  738. return !tor_addr_is_null(&node->ri->ipv6_addr);
  739. if (node->md)
  740. return !tor_addr_is_null(&node->md->ipv6_addr);
  741. if (node->rs)
  742. return !tor_addr_is_null(&node->rs->ipv6_addr);
  743. }
  744. return 0;
  745. }
  746. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  747. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  748. * port was copied, else return non-zero.*/
  749. int
  750. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  751. {
  752. node_assert_ok(node);
  753. tor_assert(ap_out);
  754. if (node->ri) {
  755. if (node->ri->addr == 0 || node->ri->or_port == 0)
  756. return -1;
  757. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  758. ap_out->port = node->ri->or_port;
  759. return 0;
  760. }
  761. if (node->rs) {
  762. if (node->rs->addr == 0 || node->rs->or_port == 0)
  763. return -1;
  764. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  765. ap_out->port = node->rs->or_port;
  766. return 0;
  767. }
  768. return -1;
  769. }
  770. /** Copy the preferred OR port (IP address and TCP port) for
  771. * <b>node</b> into *<b>ap_out</b>. */
  772. void
  773. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  774. {
  775. const or_options_t *options = get_options();
  776. tor_assert(ap_out);
  777. /* Cheap implementation of config option ClientUseIPv6 -- simply
  778. don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
  779. client running with bridges. See #4455 for more on this subject.
  780. Note that this filter is too strict since we're hindering not
  781. only clients! Erring on the safe side shouldn't be a problem
  782. though. XXX move this check to where outgoing connections are
  783. made? -LN */
  784. if ((options->ClientUseIPv6 || options->UseBridges) &&
  785. node_ipv6_preferred(node)) {
  786. node_get_pref_ipv6_orport(node, ap_out);
  787. } else {
  788. node_get_prim_orport(node, ap_out);
  789. }
  790. }
  791. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  792. * <b>node</b> into *<b>ap_out</b>. */
  793. void
  794. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  795. {
  796. node_assert_ok(node);
  797. tor_assert(ap_out);
  798. /* We prefer the microdesc over a potential routerstatus here. They
  799. are not being synchronised atm so there might be a chance that
  800. they differ at some point, f.ex. when flipping
  801. UseMicrodescriptors? -LN */
  802. if (node->ri) {
  803. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  804. ap_out->port = node->ri->ipv6_orport;
  805. } else if (node->md) {
  806. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  807. ap_out->port = node->md->ipv6_orport;
  808. } else if (node->rs) {
  809. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  810. ap_out->port = node->rs->ipv6_orport;
  811. }
  812. }
  813. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  814. * each router when the GeoIP database is reloaded, and on all new routers. */
  815. void
  816. node_set_country(node_t *node)
  817. {
  818. if (node->rs)
  819. node->country = geoip_get_country_by_ip(node->rs->addr);
  820. else if (node->ri)
  821. node->country = geoip_get_country_by_ip(node->ri->addr);
  822. else
  823. node->country = -1;
  824. }
  825. /** Set the country code of all routers in the routerlist. */
  826. void
  827. nodelist_refresh_countries(void)
  828. {
  829. smartlist_t *nodes = nodelist_get_list();
  830. SMARTLIST_FOREACH(nodes, node_t *, node,
  831. node_set_country(node));
  832. }
  833. /** Return true iff router1 and router2 have similar enough network addresses
  834. * that we should treat them as being in the same family */
  835. static INLINE int
  836. addrs_in_same_network_family(const tor_addr_t *a1,
  837. const tor_addr_t *a2)
  838. {
  839. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  840. }
  841. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  842. * (case-insensitive), or if <b>node's</b> identity key digest
  843. * matches a hexadecimal value stored in <b>nickname</b>. Return
  844. * false otherwise. */
  845. static int
  846. node_nickname_matches(const node_t *node, const char *nickname)
  847. {
  848. const char *n = node_get_nickname(node);
  849. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  850. return 1;
  851. return hex_digest_nickname_matches(nickname,
  852. node->identity,
  853. n,
  854. node_is_named(node));
  855. }
  856. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  857. static INLINE int
  858. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  859. {
  860. if (!lst) return 0;
  861. SMARTLIST_FOREACH(lst, const char *, name, {
  862. if (node_nickname_matches(node, name))
  863. return 1;
  864. });
  865. return 0;
  866. }
  867. /** Return true iff r1 and r2 are in the same family, but not the same
  868. * router. */
  869. int
  870. nodes_in_same_family(const node_t *node1, const node_t *node2)
  871. {
  872. const or_options_t *options = get_options();
  873. /* Are they in the same family because of their addresses? */
  874. if (options->EnforceDistinctSubnets) {
  875. tor_addr_t a1, a2;
  876. node_get_addr(node1, &a1);
  877. node_get_addr(node2, &a2);
  878. if (addrs_in_same_network_family(&a1, &a2))
  879. return 1;
  880. }
  881. /* Are they in the same family because the agree they are? */
  882. {
  883. const smartlist_t *f1, *f2;
  884. f1 = node_get_declared_family(node1);
  885. f2 = node_get_declared_family(node2);
  886. if (f1 && f2 &&
  887. node_in_nickname_smartlist(f1, node2) &&
  888. node_in_nickname_smartlist(f2, node1))
  889. return 1;
  890. }
  891. /* Are they in the same option because the user says they are? */
  892. if (options->NodeFamilySets) {
  893. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  894. if (routerset_contains_node(rs, node1) &&
  895. routerset_contains_node(rs, node2))
  896. return 1;
  897. });
  898. }
  899. return 0;
  900. }
  901. /**
  902. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  903. * the smartlist <b>sl</b>.
  904. *
  905. * This is used to make sure we don't pick siblings in a single path, or
  906. * pick more than one relay from a family for our entry guard list.
  907. * Note that a node may be added to <b>sl</b> more than once if it is
  908. * part of <b>node</b>'s family for more than one reason.
  909. */
  910. void
  911. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  912. {
  913. const smartlist_t *all_nodes = nodelist_get_list();
  914. const smartlist_t *declared_family;
  915. const or_options_t *options = get_options();
  916. tor_assert(node);
  917. declared_family = node_get_declared_family(node);
  918. /* Let's make sure that we have the node itself, if it's a real node. */
  919. {
  920. const node_t *real_node = node_get_by_id(node->identity);
  921. if (real_node)
  922. smartlist_add(sl, (node_t*)real_node);
  923. }
  924. /* First, add any nodes with similar network addresses. */
  925. if (options->EnforceDistinctSubnets) {
  926. tor_addr_t node_addr;
  927. node_get_addr(node, &node_addr);
  928. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  929. tor_addr_t a;
  930. node_get_addr(node2, &a);
  931. if (addrs_in_same_network_family(&a, &node_addr))
  932. smartlist_add(sl, (void*)node2);
  933. } SMARTLIST_FOREACH_END(node2);
  934. }
  935. /* Now, add all nodes in the declared_family of this node, if they
  936. * also declare this node to be in their family. */
  937. if (declared_family) {
  938. /* Add every r such that router declares familyness with node, and node
  939. * declares familyhood with router. */
  940. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  941. const node_t *node2;
  942. const smartlist_t *family2;
  943. if (!(node2 = node_get_by_nickname(name, 0)))
  944. continue;
  945. if (!(family2 = node_get_declared_family(node2)))
  946. continue;
  947. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  948. if (node_nickname_matches(node, name2)) {
  949. smartlist_add(sl, (void*)node2);
  950. break;
  951. }
  952. } SMARTLIST_FOREACH_END(name2);
  953. } SMARTLIST_FOREACH_END(name);
  954. }
  955. /* If the user declared any families locally, honor those too. */
  956. if (options->NodeFamilySets) {
  957. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  958. if (routerset_contains_node(rs, node)) {
  959. routerset_get_all_nodes(sl, rs, NULL, 0);
  960. }
  961. });
  962. }
  963. }
  964. /** Find a router that's up, that has this IP address, and
  965. * that allows exit to this address:port, or return NULL if there
  966. * isn't a good one.
  967. * Don't exit enclave to excluded relays -- it wouldn't actually
  968. * hurt anything, but this way there are fewer confused users.
  969. */
  970. const node_t *
  971. router_find_exact_exit_enclave(const char *address, uint16_t port)
  972. {/*XXXX MOVE*/
  973. uint32_t addr;
  974. struct in_addr in;
  975. tor_addr_t a;
  976. const or_options_t *options = get_options();
  977. if (!tor_inet_aton(address, &in))
  978. return NULL; /* it's not an IP already */
  979. addr = ntohl(in.s_addr);
  980. tor_addr_from_ipv4h(&a, addr);
  981. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  982. if (node_get_addr_ipv4h(node) == addr &&
  983. node->is_running &&
  984. compare_tor_addr_to_node_policy(&a, port, node) ==
  985. ADDR_POLICY_ACCEPTED &&
  986. !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
  987. return node;
  988. });
  989. return NULL;
  990. }
  991. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  992. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  993. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  994. * bandwidth.
  995. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  996. */
  997. int
  998. node_is_unreliable(const node_t *node, int need_uptime,
  999. int need_capacity, int need_guard)
  1000. {
  1001. if (need_uptime && !node->is_stable)
  1002. return 1;
  1003. if (need_capacity && !node->is_fast)
  1004. return 1;
  1005. if (need_guard && !node->is_possible_guard)
  1006. return 1;
  1007. return 0;
  1008. }
  1009. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1010. * addr:port, return 0 if any might accept it. */
  1011. int
  1012. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1013. int need_uptime)
  1014. {
  1015. addr_policy_result_t r;
  1016. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1017. if (node->is_running &&
  1018. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1019. r = compare_tor_addr_to_node_policy(addr, port, node);
  1020. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1021. return 0; /* this one could be ok. good enough. */
  1022. }
  1023. } SMARTLIST_FOREACH_END(node);
  1024. return 1; /* all will reject. */
  1025. }
  1026. /** Mark the router with ID <b>digest</b> as running or non-running
  1027. * in our routerlist. */
  1028. void
  1029. router_set_status(const char *digest, int up)
  1030. {
  1031. node_t *node;
  1032. tor_assert(digest);
  1033. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1034. trusted_dir_server_t *, d,
  1035. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1036. d->is_running = up);
  1037. node = node_get_mutable_by_id(digest);
  1038. if (node) {
  1039. #if 0
  1040. log_debug(LD_DIR,"Marking router %s as %s.",
  1041. node_describe(node), up ? "up" : "down");
  1042. #endif
  1043. if (!up && node_is_me(node) && !net_is_disabled())
  1044. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1045. "addresses reachable?");
  1046. node->is_running = up;
  1047. }
  1048. router_dir_info_changed();
  1049. }
  1050. /** True iff, the last time we checked whether we had enough directory info
  1051. * to build circuits, the answer was "yes". */
  1052. static int have_min_dir_info = 0;
  1053. /** True iff enough has changed since the last time we checked whether we had
  1054. * enough directory info to build circuits that our old answer can no longer
  1055. * be trusted. */
  1056. static int need_to_update_have_min_dir_info = 1;
  1057. /** String describing what we're missing before we have enough directory
  1058. * info. */
  1059. static char dir_info_status[128] = "";
  1060. /** Return true iff we have enough networkstatus and router information to
  1061. * start building circuits. Right now, this means "more than half the
  1062. * networkstatus documents, and at least 1/4 of expected routers." */
  1063. //XXX should consider whether we have enough exiting nodes here.
  1064. int
  1065. router_have_minimum_dir_info(void)
  1066. {
  1067. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1068. update_router_have_minimum_dir_info();
  1069. need_to_update_have_min_dir_info = 0;
  1070. }
  1071. return have_min_dir_info;
  1072. }
  1073. /** Called when our internal view of the directory has changed. This can be
  1074. * when the authorities change, networkstatuses change, the list of routerdescs
  1075. * changes, or number of running routers changes.
  1076. */
  1077. void
  1078. router_dir_info_changed(void)
  1079. {
  1080. need_to_update_have_min_dir_info = 1;
  1081. rend_hsdir_routers_changed();
  1082. }
  1083. /** Return a string describing what we're missing before we have enough
  1084. * directory info. */
  1085. const char *
  1086. get_dir_info_status_string(void)
  1087. {
  1088. return dir_info_status;
  1089. }
  1090. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1091. * them seem like ones we'd use, and how many of <em>those</em> we have
  1092. * descriptors for. Store the former in *<b>num_usable</b> and the latter in
  1093. * *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
  1094. * routers in <b>in_set</b>. If <b>exit_only</b> is true, only consider nodes
  1095. * with the Exit flag.
  1096. */
  1097. static void
  1098. count_usable_descriptors(int *num_present, int *num_usable,
  1099. const networkstatus_t *consensus,
  1100. const or_options_t *options, time_t now,
  1101. routerset_t *in_set, int exit_only)
  1102. {
  1103. const int md = (consensus->flavor == FLAV_MICRODESC);
  1104. *num_present = 0, *num_usable=0;
  1105. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1106. {
  1107. if (exit_only && ! rs->is_exit)
  1108. continue;
  1109. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1110. continue;
  1111. if (client_would_use_router(rs, now, options)) {
  1112. const char * const digest = rs->descriptor_digest;
  1113. int present;
  1114. ++*num_usable; /* the consensus says we want it. */
  1115. if (md)
  1116. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1117. else
  1118. present = NULL != router_get_by_descriptor_digest(digest);
  1119. if (present) {
  1120. /* we have the descriptor listed in the consensus. */
  1121. ++*num_present;
  1122. }
  1123. }
  1124. }
  1125. SMARTLIST_FOREACH_END(rs);
  1126. log_debug(LD_DIR, "%d usable, %d present (%s).", *num_usable, *num_present,
  1127. md ? "microdescs" : "descs");
  1128. }
  1129. /** We just fetched a new set of descriptors. Compute how far through
  1130. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1131. * the controller of our progress. */
  1132. int
  1133. count_loading_descriptors_progress(void)
  1134. {
  1135. int num_present = 0, num_usable=0;
  1136. time_t now = time(NULL);
  1137. const networkstatus_t *consensus =
  1138. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1139. double fraction;
  1140. if (!consensus)
  1141. return 0; /* can't count descriptors if we have no list of them */
  1142. count_usable_descriptors(&num_present, &num_usable,
  1143. consensus, get_options(), now, NULL, 0);
  1144. if (num_usable == 0)
  1145. return 0; /* don't div by 0 */
  1146. fraction = num_present / (num_usable/4.);
  1147. if (fraction > 1.0)
  1148. return 0; /* it's not the number of descriptors holding us back */
  1149. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1150. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1151. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1152. }
  1153. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1154. * network and router information to build circuits. Clear the value of
  1155. * need_to_update_have_min_dir_info. */
  1156. static void
  1157. update_router_have_minimum_dir_info(void)
  1158. {
  1159. int num_present = 0, num_usable=0;
  1160. int num_exit_present = 0, num_exit_usable = 0;
  1161. time_t now = time(NULL);
  1162. int res;
  1163. const or_options_t *options = get_options();
  1164. const networkstatus_t *consensus =
  1165. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1166. int using_md;
  1167. if (!consensus) {
  1168. if (!networkstatus_get_latest_consensus())
  1169. strlcpy(dir_info_status, "We have no usable consensus.",
  1170. sizeof(dir_info_status));
  1171. else
  1172. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1173. sizeof(dir_info_status));
  1174. res = 0;
  1175. goto done;
  1176. }
  1177. if (should_delay_dir_fetches(get_options())) {
  1178. log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
  1179. strlcpy(dir_info_status, "No live bridge descriptors.",
  1180. sizeof(dir_info_status));
  1181. res = 0;
  1182. goto done;
  1183. }
  1184. using_md = consensus->flavor == FLAV_MICRODESC;
  1185. count_usable_descriptors(&num_present, &num_usable, consensus, options, now,
  1186. NULL, 0);
  1187. count_usable_descriptors(&num_exit_present, &num_exit_usable,
  1188. consensus, options, now, options->ExitNodes, 1);
  1189. /* What fraction of desired server descriptors do we need before we will
  1190. * build circuits? */
  1191. #define FRAC_USABLE_NEEDED .75
  1192. /* What fraction of desired _exit_ server descriptors do we need before we
  1193. * will build circuits? */
  1194. #define FRAC_EXIT_USABLE_NEEDED .5
  1195. if (num_present < num_usable * FRAC_USABLE_NEEDED) {
  1196. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1197. "We have only %d/%d usable %sdescriptors.",
  1198. num_present, num_usable, using_md ? "micro" : "");
  1199. res = 0;
  1200. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1201. goto done;
  1202. } else if (num_present < 2) {
  1203. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1204. "Only %d %sdescriptor%s here and believed reachable!",
  1205. num_present, using_md ? "micro" : "", num_present ? "" : "s");
  1206. res = 0;
  1207. goto done;
  1208. } else if (num_exit_present < num_exit_usable * FRAC_EXIT_USABLE_NEEDED) {
  1209. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1210. "We have only %d/%d usable exit node descriptors.",
  1211. num_exit_present, num_exit_usable);
  1212. res = 0;
  1213. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1214. goto done;
  1215. }
  1216. /* Check for entry nodes. */
  1217. if (options->EntryNodes) {
  1218. count_usable_descriptors(&num_present, &num_usable, consensus, options,
  1219. now, options->EntryNodes, 0);
  1220. if (!num_usable || !num_present) {
  1221. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1222. "We have only %d/%d usable entry node %sdescriptors.",
  1223. num_present, num_usable, using_md?"micro":"");
  1224. res = 0;
  1225. goto done;
  1226. }
  1227. }
  1228. res = 1;
  1229. done:
  1230. if (res && !have_min_dir_info) {
  1231. log(LOG_NOTICE, LD_DIR,
  1232. "We now have enough directory information to build circuits.");
  1233. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1234. control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
  1235. }
  1236. if (!res && have_min_dir_info) {
  1237. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1238. log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1239. "Our directory information is no longer up-to-date "
  1240. "enough to build circuits: %s", dir_info_status);
  1241. /* a) make us log when we next complete a circuit, so we know when Tor
  1242. * is back up and usable, and b) disable some activities that Tor
  1243. * should only do while circuits are working, like reachability tests
  1244. * and fetching bridge descriptors only over circuits. */
  1245. can_complete_circuit = 0;
  1246. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1247. }
  1248. have_min_dir_info = res;
  1249. need_to_update_have_min_dir_info = 0;
  1250. }