nodelist.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. const or_options_t *options = get_options();
  777. tor_assert(ap_out);
  778. /* Cheap implementation of config option ClientUseIPv6 -- simply
  779. don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
  780. client running with bridges. See #4455 for more on this subject.
  781. Note that this filter is too strict since we're hindering not
  782. only clients! Erring on the safe side shouldn't be a problem
  783. though. XXX move this check to where outgoing connections are
  784. made? -LN */
  785. if ((options->ClientUseIPv6 || options->UseBridges) &&
  786. node_ipv6_preferred(node)) {
  787. node_get_pref_ipv6_orport(node, ap_out);
  788. } else {
  789. node_get_prim_orport(node, ap_out);
  790. }
  791. }
  792. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  793. * <b>node</b> into *<b>ap_out</b>. */
  794. void
  795. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  796. {
  797. node_assert_ok(node);
  798. tor_assert(ap_out);
  799. /* We prefer the microdesc over a potential routerstatus here. They
  800. are not being synchronised atm so there might be a chance that
  801. they differ at some point, f.ex. when flipping
  802. UseMicrodescriptors? -LN */
  803. if (node->ri) {
  804. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  805. ap_out->port = node->ri->ipv6_orport;
  806. } else if (node->md) {
  807. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  808. ap_out->port = node->md->ipv6_orport;
  809. } else if (node->rs) {
  810. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  811. ap_out->port = node->rs->ipv6_orport;
  812. }
  813. }
  814. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  815. * each router when the GeoIP database is reloaded, and on all new routers. */
  816. void
  817. node_set_country(node_t *node)
  818. {
  819. if (node->rs)
  820. node->country = geoip_get_country_by_ip(node->rs->addr);
  821. else if (node->ri)
  822. node->country = geoip_get_country_by_ip(node->ri->addr);
  823. else
  824. node->country = -1;
  825. }
  826. /** Set the country code of all routers in the routerlist. */
  827. void
  828. nodelist_refresh_countries(void)
  829. {
  830. smartlist_t *nodes = nodelist_get_list();
  831. SMARTLIST_FOREACH(nodes, node_t *, node,
  832. node_set_country(node));
  833. }
  834. /** Return true iff router1 and router2 have similar enough network addresses
  835. * that we should treat them as being in the same family */
  836. static INLINE int
  837. addrs_in_same_network_family(const tor_addr_t *a1,
  838. const tor_addr_t *a2)
  839. {
  840. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  841. }
  842. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  843. * (case-insensitive), or if <b>node's</b> identity key digest
  844. * matches a hexadecimal value stored in <b>nickname</b>. Return
  845. * false otherwise. */
  846. static int
  847. node_nickname_matches(const node_t *node, const char *nickname)
  848. {
  849. const char *n = node_get_nickname(node);
  850. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  851. return 1;
  852. return hex_digest_nickname_matches(nickname,
  853. node->identity,
  854. n,
  855. node_is_named(node));
  856. }
  857. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  858. static INLINE int
  859. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  860. {
  861. if (!lst) return 0;
  862. SMARTLIST_FOREACH(lst, const char *, name, {
  863. if (node_nickname_matches(node, name))
  864. return 1;
  865. });
  866. return 0;
  867. }
  868. /** Return true iff r1 and r2 are in the same family, but not the same
  869. * router. */
  870. int
  871. nodes_in_same_family(const node_t *node1, const node_t *node2)
  872. {
  873. const or_options_t *options = get_options();
  874. /* Are they in the same family because of their addresses? */
  875. if (options->EnforceDistinctSubnets) {
  876. tor_addr_t a1, a2;
  877. node_get_addr(node1, &a1);
  878. node_get_addr(node2, &a2);
  879. if (addrs_in_same_network_family(&a1, &a2))
  880. return 1;
  881. }
  882. /* Are they in the same family because the agree they are? */
  883. {
  884. const smartlist_t *f1, *f2;
  885. f1 = node_get_declared_family(node1);
  886. f2 = node_get_declared_family(node2);
  887. if (f1 && f2 &&
  888. node_in_nickname_smartlist(f1, node2) &&
  889. node_in_nickname_smartlist(f2, node1))
  890. return 1;
  891. }
  892. /* Are they in the same option because the user says they are? */
  893. if (options->NodeFamilySets) {
  894. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  895. if (routerset_contains_node(rs, node1) &&
  896. routerset_contains_node(rs, node2))
  897. return 1;
  898. });
  899. }
  900. return 0;
  901. }
  902. /**
  903. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  904. * the smartlist <b>sl</b>.
  905. *
  906. * This is used to make sure we don't pick siblings in a single path, or
  907. * pick more than one relay from a family for our entry guard list.
  908. * Note that a node may be added to <b>sl</b> more than once if it is
  909. * part of <b>node</b>'s family for more than one reason.
  910. */
  911. void
  912. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  913. {
  914. const smartlist_t *all_nodes = nodelist_get_list();
  915. const smartlist_t *declared_family;
  916. const or_options_t *options = get_options();
  917. tor_assert(node);
  918. declared_family = node_get_declared_family(node);
  919. /* Let's make sure that we have the node itself, if it's a real node. */
  920. {
  921. const node_t *real_node = node_get_by_id(node->identity);
  922. if (real_node)
  923. smartlist_add(sl, (node_t*)real_node);
  924. }
  925. /* First, add any nodes with similar network addresses. */
  926. if (options->EnforceDistinctSubnets) {
  927. tor_addr_t node_addr;
  928. node_get_addr(node, &node_addr);
  929. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  930. tor_addr_t a;
  931. node_get_addr(node2, &a);
  932. if (addrs_in_same_network_family(&a, &node_addr))
  933. smartlist_add(sl, (void*)node2);
  934. } SMARTLIST_FOREACH_END(node2);
  935. }
  936. /* Now, add all nodes in the declared_family of this node, if they
  937. * also declare this node to be in their family. */
  938. if (declared_family) {
  939. /* Add every r such that router declares familyness with node, and node
  940. * declares familyhood with router. */
  941. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  942. const node_t *node2;
  943. const smartlist_t *family2;
  944. if (!(node2 = node_get_by_nickname(name, 0)))
  945. continue;
  946. if (!(family2 = node_get_declared_family(node2)))
  947. continue;
  948. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  949. if (node_nickname_matches(node, name2)) {
  950. smartlist_add(sl, (void*)node2);
  951. break;
  952. }
  953. } SMARTLIST_FOREACH_END(name2);
  954. } SMARTLIST_FOREACH_END(name);
  955. }
  956. /* If the user declared any families locally, honor those too. */
  957. if (options->NodeFamilySets) {
  958. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  959. if (routerset_contains_node(rs, node)) {
  960. routerset_get_all_nodes(sl, rs, NULL, 0);
  961. }
  962. });
  963. }
  964. }
  965. /** Find a router that's up, that has this IP address, and
  966. * that allows exit to this address:port, or return NULL if there
  967. * isn't a good one.
  968. * Don't exit enclave to excluded relays -- it wouldn't actually
  969. * hurt anything, but this way there are fewer confused users.
  970. */
  971. const node_t *
  972. router_find_exact_exit_enclave(const char *address, uint16_t port)
  973. {/*XXXX MOVE*/
  974. uint32_t addr;
  975. struct in_addr in;
  976. tor_addr_t a;
  977. const or_options_t *options = get_options();
  978. if (!tor_inet_aton(address, &in))
  979. return NULL; /* it's not an IP already */
  980. addr = ntohl(in.s_addr);
  981. tor_addr_from_ipv4h(&a, addr);
  982. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  983. if (node_get_addr_ipv4h(node) == addr &&
  984. node->is_running &&
  985. compare_tor_addr_to_node_policy(&a, port, node) ==
  986. ADDR_POLICY_ACCEPTED &&
  987. !routerset_contains_node(options->_ExcludeExitNodesUnion, node))
  988. return node;
  989. });
  990. return NULL;
  991. }
  992. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  993. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  994. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  995. * bandwidth.
  996. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  997. */
  998. int
  999. node_is_unreliable(const node_t *node, int need_uptime,
  1000. int need_capacity, int need_guard)
  1001. {
  1002. if (need_uptime && !node->is_stable)
  1003. return 1;
  1004. if (need_capacity && !node->is_fast)
  1005. return 1;
  1006. if (need_guard && !node->is_possible_guard)
  1007. return 1;
  1008. return 0;
  1009. }
  1010. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1011. * addr:port, return 0 if any might accept it. */
  1012. int
  1013. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1014. int need_uptime)
  1015. {
  1016. addr_policy_result_t r;
  1017. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1018. if (node->is_running &&
  1019. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1020. r = compare_tor_addr_to_node_policy(addr, port, node);
  1021. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1022. return 0; /* this one could be ok. good enough. */
  1023. }
  1024. } SMARTLIST_FOREACH_END(node);
  1025. return 1; /* all will reject. */
  1026. }
  1027. /** Mark the router with ID <b>digest</b> as running or non-running
  1028. * in our routerlist. */
  1029. void
  1030. router_set_status(const char *digest, int up)
  1031. {
  1032. node_t *node;
  1033. tor_assert(digest);
  1034. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1035. trusted_dir_server_t *, d,
  1036. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1037. d->is_running = up);
  1038. node = node_get_mutable_by_id(digest);
  1039. if (node) {
  1040. #if 0
  1041. log_debug(LD_DIR,"Marking router %s as %s.",
  1042. node_describe(node), up ? "up" : "down");
  1043. #endif
  1044. if (!up && node_is_me(node) && !net_is_disabled())
  1045. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1046. "addresses reachable?");
  1047. node->is_running = up;
  1048. }
  1049. router_dir_info_changed();
  1050. }
  1051. /** True iff, the last time we checked whether we had enough directory info
  1052. * to build circuits, the answer was "yes". */
  1053. static int have_min_dir_info = 0;
  1054. /** True iff enough has changed since the last time we checked whether we had
  1055. * enough directory info to build circuits that our old answer can no longer
  1056. * be trusted. */
  1057. static int need_to_update_have_min_dir_info = 1;
  1058. /** String describing what we're missing before we have enough directory
  1059. * info. */
  1060. static char dir_info_status[128] = "";
  1061. /** Return true iff we have enough networkstatus and router information to
  1062. * start building circuits. Right now, this means "more than half the
  1063. * networkstatus documents, and at least 1/4 of expected routers." */
  1064. //XXX should consider whether we have enough exiting nodes here.
  1065. int
  1066. router_have_minimum_dir_info(void)
  1067. {
  1068. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1069. update_router_have_minimum_dir_info();
  1070. need_to_update_have_min_dir_info = 0;
  1071. }
  1072. return have_min_dir_info;
  1073. }
  1074. /** Called when our internal view of the directory has changed. This can be
  1075. * when the authorities change, networkstatuses change, the list of routerdescs
  1076. * changes, or number of running routers changes.
  1077. */
  1078. void
  1079. router_dir_info_changed(void)
  1080. {
  1081. need_to_update_have_min_dir_info = 1;
  1082. rend_hsdir_routers_changed();
  1083. }
  1084. /** Return a string describing what we're missing before we have enough
  1085. * directory info. */
  1086. const char *
  1087. get_dir_info_status_string(void)
  1088. {
  1089. return dir_info_status;
  1090. }
  1091. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1092. * them seem like ones we'd use, and how many of <em>those</em> we have
  1093. * descriptors for. Store the former in *<b>num_usable</b> and the latter in
  1094. * *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
  1095. * routers in <b>in_set</b>. If <b>exit_only</b> is true, only consider nodes
  1096. * with the Exit flag.
  1097. */
  1098. static void
  1099. count_usable_descriptors(int *num_present, int *num_usable,
  1100. const networkstatus_t *consensus,
  1101. const or_options_t *options, time_t now,
  1102. routerset_t *in_set, int exit_only)
  1103. {
  1104. const int md = (consensus->flavor == FLAV_MICRODESC);
  1105. *num_present = 0, *num_usable=0;
  1106. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1107. {
  1108. if (exit_only && ! rs->is_exit)
  1109. continue;
  1110. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1111. continue;
  1112. if (client_would_use_router(rs, now, options)) {
  1113. const char * const digest = rs->descriptor_digest;
  1114. int present;
  1115. ++*num_usable; /* the consensus says we want it. */
  1116. if (md)
  1117. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1118. else
  1119. present = NULL != router_get_by_descriptor_digest(digest);
  1120. if (present) {
  1121. /* we have the descriptor listed in the consensus. */
  1122. ++*num_present;
  1123. }
  1124. }
  1125. }
  1126. SMARTLIST_FOREACH_END(rs);
  1127. log_debug(LD_DIR, "%d usable, %d present (%s).", *num_usable, *num_present,
  1128. md ? "microdescs" : "descs");
  1129. }
  1130. /** We just fetched a new set of descriptors. Compute how far through
  1131. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1132. * the controller of our progress. */
  1133. int
  1134. count_loading_descriptors_progress(void)
  1135. {
  1136. int num_present = 0, num_usable=0;
  1137. time_t now = time(NULL);
  1138. const networkstatus_t *consensus =
  1139. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1140. double fraction;
  1141. if (!consensus)
  1142. return 0; /* can't count descriptors if we have no list of them */
  1143. count_usable_descriptors(&num_present, &num_usable,
  1144. consensus, get_options(), now, NULL, 0);
  1145. if (num_usable == 0)
  1146. return 0; /* don't div by 0 */
  1147. fraction = num_present / (num_usable/4.);
  1148. if (fraction > 1.0)
  1149. return 0; /* it's not the number of descriptors holding us back */
  1150. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1151. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1152. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1153. }
  1154. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1155. * network and router information to build circuits. Clear the value of
  1156. * need_to_update_have_min_dir_info. */
  1157. static void
  1158. update_router_have_minimum_dir_info(void)
  1159. {
  1160. int num_present = 0, num_usable=0;
  1161. int num_exit_present = 0, num_exit_usable = 0;
  1162. time_t now = time(NULL);
  1163. int res;
  1164. const or_options_t *options = get_options();
  1165. const networkstatus_t *consensus =
  1166. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1167. int using_md;
  1168. if (!consensus) {
  1169. if (!networkstatus_get_latest_consensus())
  1170. strlcpy(dir_info_status, "We have no usable consensus.",
  1171. sizeof(dir_info_status));
  1172. else
  1173. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1174. sizeof(dir_info_status));
  1175. res = 0;
  1176. goto done;
  1177. }
  1178. if (should_delay_dir_fetches(get_options())) {
  1179. log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
  1180. strlcpy(dir_info_status, "No live bridge descriptors.",
  1181. sizeof(dir_info_status));
  1182. res = 0;
  1183. goto done;
  1184. }
  1185. using_md = consensus->flavor == FLAV_MICRODESC;
  1186. count_usable_descriptors(&num_present, &num_usable, consensus, options, now,
  1187. NULL, 0);
  1188. count_usable_descriptors(&num_exit_present, &num_exit_usable,
  1189. consensus, options, now, options->ExitNodes, 1);
  1190. /* What fraction of desired server descriptors do we need before we will
  1191. * build circuits? */
  1192. #define FRAC_USABLE_NEEDED .75
  1193. /* What fraction of desired _exit_ server descriptors do we need before we
  1194. * will build circuits? */
  1195. #define FRAC_EXIT_USABLE_NEEDED .5
  1196. if (num_present < num_usable * FRAC_USABLE_NEEDED) {
  1197. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1198. "We have only %d/%d usable %sdescriptors.",
  1199. num_present, num_usable, using_md ? "micro" : "");
  1200. res = 0;
  1201. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1202. goto done;
  1203. } else if (num_present < 2) {
  1204. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1205. "Only %d %sdescriptor%s here and believed reachable!",
  1206. num_present, using_md ? "micro" : "", num_present ? "" : "s");
  1207. res = 0;
  1208. goto done;
  1209. } else if (num_exit_present < num_exit_usable * FRAC_EXIT_USABLE_NEEDED) {
  1210. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1211. "We have only %d/%d usable exit node descriptors.",
  1212. num_exit_present, num_exit_usable);
  1213. res = 0;
  1214. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1215. goto done;
  1216. }
  1217. /* Check for entry nodes. */
  1218. if (options->EntryNodes) {
  1219. count_usable_descriptors(&num_present, &num_usable, consensus, options,
  1220. now, options->EntryNodes, 0);
  1221. if (!num_usable || !num_present) {
  1222. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1223. "We have only %d/%d usable entry node %sdescriptors.",
  1224. num_present, num_usable, using_md?"micro":"");
  1225. res = 0;
  1226. goto done;
  1227. }
  1228. }
  1229. res = 1;
  1230. done:
  1231. if (res && !have_min_dir_info) {
  1232. log(LOG_NOTICE, LD_DIR,
  1233. "We now have enough directory information to build circuits.");
  1234. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1235. control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
  1236. }
  1237. if (!res && have_min_dir_info) {
  1238. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1239. log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1240. "Our directory information is no longer up-to-date "
  1241. "enough to build circuits: %s", dir_info_status);
  1242. /* a) make us log when we next complete a circuit, so we know when Tor
  1243. * is back up and usable, and b) disable some activities that Tor
  1244. * should only do while circuits are working, like reachability tests
  1245. * and fetching bridge descriptors only over circuits. */
  1246. can_complete_circuit = 0;
  1247. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1248. }
  1249. have_min_dir_info = res;
  1250. need_to_update_have_min_dir_info = 0;
  1251. }