nodelist.c 43 KB

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