nodelist.c 48 KB

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