nodelist.c 48 KB

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