nodelist.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "address.h"
  8. #include "config.h"
  9. #include "dirserv.h"
  10. #include "geoip.h"
  11. #include "microdesc.h"
  12. #include "networkstatus.h"
  13. #include "nodelist.h"
  14. #include "policies.h"
  15. #include "router.h"
  16. #include "routerlist.h"
  17. #include "routerset.h"
  18. #include <string.h>
  19. static void nodelist_drop_node(node_t *node, int remove_from_ht);
  20. static void node_free(node_t *node);
  21. /** A nodelist_t holds a node_t object for every router we're "willing to use
  22. * for something". Specifically, it should hold a node_t for every node that
  23. * is currently in the routerlist, or currently in the consensus we're using.
  24. */
  25. typedef struct nodelist_t {
  26. /* A list of all the nodes. */
  27. smartlist_t *nodes;
  28. /* Hash table to map from node ID digest to node. */
  29. HT_HEAD(nodelist_map, node_t) nodes_by_id;
  30. } nodelist_t;
  31. static INLINE unsigned int
  32. node_id_hash(const node_t *node)
  33. {
  34. #if SIZEOF_INT == 4
  35. const uint32_t *p = (const uint32_t*)node->identity;
  36. return p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4];
  37. #elif SIZEOF_INT == 8
  38. const uint64_t *p = (const uint32_t*)node->identity;
  39. const uint32_t *p32 = (const uint32_t*)node->identity;
  40. return p[0] ^ p[1] ^ p32[4];
  41. #endif
  42. }
  43. static INLINE unsigned int
  44. node_id_eq(const node_t *node1, const node_t *node2)
  45. {
  46. return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
  47. }
  48. HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
  49. HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
  50. 0.6, malloc, realloc, free);
  51. /** The global nodelist. */
  52. static nodelist_t *the_nodelist=NULL;
  53. /** Create an empty nodelist if we haven't done so already. */
  54. static void
  55. init_nodelist(void)
  56. {
  57. if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
  58. the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
  59. HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
  60. the_nodelist->nodes = smartlist_new();
  61. }
  62. }
  63. /** As node_get_by_id, but returns a non-const pointer */
  64. node_t *
  65. node_get_mutable_by_id(const char *identity_digest)
  66. {
  67. node_t search, *node;
  68. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  69. return NULL;
  70. memcpy(&search.identity, identity_digest, DIGEST_LEN);
  71. node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
  72. return node;
  73. }
  74. /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
  75. * if no such node exists. */
  76. const node_t *
  77. node_get_by_id(const char *identity_digest)
  78. {
  79. return node_get_mutable_by_id(identity_digest);
  80. }
  81. /** Internal: return the node_t whose identity_digest is
  82. * <b>identity_digest</b>. If none exists, create a new one, add it to the
  83. * nodelist, and return it.
  84. *
  85. * Requires that the nodelist be initialized.
  86. */
  87. static node_t *
  88. node_get_or_create(const char *identity_digest)
  89. {
  90. node_t *node;
  91. if ((node = node_get_mutable_by_id(identity_digest)))
  92. return node;
  93. node = tor_malloc_zero(sizeof(node_t));
  94. memcpy(node->identity, identity_digest, DIGEST_LEN);
  95. HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
  96. smartlist_add(the_nodelist->nodes, node);
  97. node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
  98. node->country = -1;
  99. return node;
  100. }
  101. /** Called when a node's address changes. */
  102. static void
  103. node_addrs_changed(node_t *node)
  104. {
  105. node->last_reachable = node->last_reachable6 = 0;
  106. node->testing_since = node->testing_since6 = 0;
  107. node->country = -1;
  108. }
  109. /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
  110. * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
  111. * to the previous routerinfo.
  112. */
  113. node_t *
  114. nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
  115. {
  116. node_t *node;
  117. const char *id_digest;
  118. int had_router = 0;
  119. tor_assert(ri);
  120. init_nodelist();
  121. id_digest = ri->cache_info.identity_digest;
  122. node = node_get_or_create(id_digest);
  123. if (node->ri) {
  124. if (!routers_have_same_or_addrs(node->ri, ri)) {
  125. node_addrs_changed(node);
  126. }
  127. had_router = 1;
  128. if (ri_old_out)
  129. *ri_old_out = node->ri;
  130. } else {
  131. if (ri_old_out)
  132. *ri_old_out = NULL;
  133. }
  134. node->ri = ri;
  135. if (node->country == -1)
  136. node_set_country(node);
  137. if (authdir_mode(get_options()) && !had_router) {
  138. const char *discard=NULL;
  139. uint32_t status = dirserv_router_get_status(ri, &discard);
  140. dirserv_set_node_flags_from_authoritative_status(node, status);
  141. }
  142. return node;
  143. }
  144. /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
  145. *
  146. * Called when a new microdesc has arrived and the usable consensus flavor
  147. * is "microdesc".
  148. **/
  149. node_t *
  150. nodelist_add_microdesc(microdesc_t *md)
  151. {
  152. networkstatus_t *ns =
  153. networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  154. const routerstatus_t *rs;
  155. node_t *node;
  156. if (ns == NULL)
  157. return NULL;
  158. init_nodelist();
  159. /* Microdescriptors don't carry an identity digest, so we need to figure
  160. * it out by looking up the routerstatus. */
  161. rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
  162. if (rs == NULL)
  163. return NULL;
  164. node = node_get_mutable_by_id(rs->identity_digest);
  165. if (node) {
  166. if (node->md)
  167. node->md->held_by_nodes--;
  168. node->md = md;
  169. md->held_by_nodes++;
  170. }
  171. return node;
  172. }
  173. /** Tell the nodelist that the current usable consensus is <b>ns</b>.
  174. * This makes the nodelist change all of the routerstatus entries for
  175. * the nodes, drop nodes that no longer have enough info to get used,
  176. * and grab microdescriptors into nodes as appropriate.
  177. */
  178. void
  179. nodelist_set_consensus(networkstatus_t *ns)
  180. {
  181. const or_options_t *options = get_options();
  182. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  183. int client = !server_mode(options);
  184. init_nodelist();
  185. if (ns->flavor == FLAV_MICRODESC)
  186. (void) get_microdesc_cache(); /* Make sure it exists first. */
  187. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  188. node->rs = NULL);
  189. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  190. node_t *node = node_get_or_create(rs->identity_digest);
  191. node->rs = rs;
  192. if (ns->flavor == FLAV_MICRODESC) {
  193. if (node->md == NULL ||
  194. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  195. if (node->md)
  196. node->md->held_by_nodes--;
  197. node->md = microdesc_cache_lookup_by_digest256(NULL,
  198. rs->descriptor_digest);
  199. if (node->md)
  200. node->md->held_by_nodes++;
  201. }
  202. }
  203. node_set_country(node);
  204. /* If we're not an authdir, believe others. */
  205. if (!authdir) {
  206. node->is_valid = rs->is_valid;
  207. node->is_running = rs->is_flagged_running;
  208. node->is_fast = rs->is_fast;
  209. node->is_stable = rs->is_stable;
  210. node->is_possible_guard = rs->is_possible_guard;
  211. node->is_exit = rs->is_exit;
  212. node->is_bad_directory = rs->is_bad_directory;
  213. node->is_bad_exit = rs->is_bad_exit;
  214. node->is_hs_dir = rs->is_hs_dir;
  215. node->ipv6_preferred = 0;
  216. if (client && options->ClientPreferIPv6ORPort == 1 &&
  217. (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
  218. (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
  219. node->ipv6_preferred = 1;
  220. }
  221. } SMARTLIST_FOREACH_END(rs);
  222. nodelist_purge();
  223. if (! authdir) {
  224. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  225. /* We have no routerstatus for this router. Clear flags so we can skip
  226. * it, maybe.*/
  227. if (!node->rs) {
  228. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  229. * would have removed it. */
  230. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  231. /* Clear all flags. */
  232. node->is_valid = node->is_running = node->is_hs_dir =
  233. node->is_fast = node->is_stable =
  234. node->is_possible_guard = node->is_exit =
  235. node->is_bad_exit = node->is_bad_directory =
  236. node->ipv6_preferred = 0;
  237. }
  238. }
  239. } SMARTLIST_FOREACH_END(node);
  240. }
  241. }
  242. /** Helper: return true iff a node has a usable amount of information*/
  243. static INLINE int
  244. node_is_usable(const node_t *node)
  245. {
  246. return (node->rs) || (node->ri);
  247. }
  248. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  249. * node with <b>identity_digest</b>. */
  250. void
  251. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  252. {
  253. node_t *node = node_get_mutable_by_id(identity_digest);
  254. if (node && node->md == md) {
  255. node->md = NULL;
  256. md->held_by_nodes--;
  257. }
  258. }
  259. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  260. void
  261. nodelist_remove_routerinfo(routerinfo_t *ri)
  262. {
  263. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  264. if (node && node->ri == ri) {
  265. node->ri = NULL;
  266. if (! node_is_usable(node)) {
  267. nodelist_drop_node(node, 1);
  268. node_free(node);
  269. }
  270. }
  271. }
  272. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  273. * with.) */
  274. static void
  275. nodelist_drop_node(node_t *node, int remove_from_ht)
  276. {
  277. node_t *tmp;
  278. int idx;
  279. if (remove_from_ht) {
  280. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  281. tor_assert(tmp == node);
  282. }
  283. idx = node->nodelist_idx;
  284. tor_assert(idx >= 0);
  285. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  286. smartlist_del(the_nodelist->nodes, idx);
  287. if (idx < smartlist_len(the_nodelist->nodes)) {
  288. tmp = smartlist_get(the_nodelist->nodes, idx);
  289. tmp->nodelist_idx = idx;
  290. }
  291. node->nodelist_idx = -1;
  292. }
  293. /** Release storage held by <b>node</b> */
  294. static void
  295. node_free(node_t *node)
  296. {
  297. if (!node)
  298. return;
  299. if (node->md)
  300. node->md->held_by_nodes--;
  301. tor_assert(node->nodelist_idx == -1);
  302. tor_free(node);
  303. }
  304. /** Remove all entries from the nodelist that don't have enough info to be
  305. * usable for anything. */
  306. void
  307. nodelist_purge(void)
  308. {
  309. node_t **iter;
  310. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  311. return;
  312. /* Remove the non-usable nodes. */
  313. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  314. node_t *node = *iter;
  315. if (node->md && !node->rs) {
  316. /* An md is only useful if there is an rs. */
  317. node->md->held_by_nodes--;
  318. node->md = NULL;
  319. }
  320. if (node_is_usable(node)) {
  321. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  322. } else {
  323. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  324. nodelist_drop_node(node, 0);
  325. node_free(node);
  326. }
  327. }
  328. nodelist_assert_ok();
  329. }
  330. /** Release all storage held by the nodelist. */
  331. void
  332. nodelist_free_all(void)
  333. {
  334. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  335. return;
  336. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  337. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  338. node->nodelist_idx = -1;
  339. node_free(node);
  340. } SMARTLIST_FOREACH_END(node);
  341. smartlist_free(the_nodelist->nodes);
  342. tor_free(the_nodelist);
  343. }
  344. /** Check that the nodelist is internally consistent, and consistent with
  345. * the directory info it's derived from.
  346. */
  347. void
  348. nodelist_assert_ok(void)
  349. {
  350. routerlist_t *rl = router_get_routerlist();
  351. networkstatus_t *ns = networkstatus_get_latest_consensus();
  352. digestmap_t *dm;
  353. if (!the_nodelist)
  354. return;
  355. dm = digestmap_new();
  356. /* every routerinfo in rl->routers should be in the nodelist. */
  357. if (rl) {
  358. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  359. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  360. tor_assert(node && node->ri == ri);
  361. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  362. node->identity, DIGEST_LEN));
  363. tor_assert(! digestmap_get(dm, node->identity));
  364. digestmap_set(dm, node->identity, (void*)node);
  365. } SMARTLIST_FOREACH_END(ri);
  366. }
  367. /* every routerstatus in ns should be in the nodelist */
  368. if (ns) {
  369. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  370. const node_t *node = node_get_by_id(rs->identity_digest);
  371. tor_assert(node && node->rs == rs);
  372. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  373. digestmap_set(dm, node->identity, (void*)node);
  374. if (ns->flavor == FLAV_MICRODESC) {
  375. /* If it's a microdesc consensus, every entry that has a
  376. * microdescriptor should be in the nodelist.
  377. */
  378. microdesc_t *md =
  379. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  380. tor_assert(md == node->md);
  381. if (md)
  382. tor_assert(md->held_by_nodes >= 1);
  383. }
  384. } SMARTLIST_FOREACH_END(rs);
  385. }
  386. /* The nodelist should have no other entries, and its entries should be
  387. * well-formed. */
  388. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  389. tor_assert(digestmap_get(dm, node->identity) != NULL);
  390. tor_assert(node_sl_idx == node->nodelist_idx);
  391. } SMARTLIST_FOREACH_END(node);
  392. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  393. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  394. digestmap_free(dm, NULL);
  395. }
  396. /** Return a list of a node_t * for every node we know about. The caller
  397. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  398. * you must, but you must not add or remove nodes.) */
  399. smartlist_t *
  400. nodelist_get_list(void)
  401. {
  402. init_nodelist();
  403. return the_nodelist->nodes;
  404. }
  405. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  406. * or $DIGEST~name, return the node with the matching identity digest and
  407. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  408. * is not well-formed. */
  409. const node_t *
  410. node_get_by_hex_id(const char *hex_id)
  411. {
  412. char digest_buf[DIGEST_LEN];
  413. char nn_buf[MAX_NICKNAME_LEN+1];
  414. char nn_char='\0';
  415. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  416. const node_t *node = node_get_by_id(digest_buf);
  417. if (!node)
  418. return NULL;
  419. if (nn_char) {
  420. const char *real_name = node_get_nickname(node);
  421. if (!real_name || strcasecmp(real_name, nn_buf))
  422. return NULL;
  423. if (nn_char == '=') {
  424. const char *named_id =
  425. networkstatus_get_router_digest_by_nickname(nn_buf);
  426. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  427. return NULL;
  428. }
  429. }
  430. return node;
  431. }
  432. return NULL;
  433. }
  434. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  435. * the corresponding node_t, or NULL if none exists. Warn the user if
  436. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  437. * nickname, but the Named flag isn't set for that router. */
  438. const node_t *
  439. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  440. {
  441. const node_t *node;
  442. if (!the_nodelist)
  443. return NULL;
  444. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  445. if ((node = node_get_by_hex_id(nickname)) != NULL)
  446. return node;
  447. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  448. return NULL;
  449. /* Okay, so if we get here, the nickname is just a nickname. Is there
  450. * a binding for it in the consensus? */
  451. {
  452. const char *named_id =
  453. networkstatus_get_router_digest_by_nickname(nickname);
  454. if (named_id)
  455. return node_get_by_id(named_id);
  456. }
  457. /* Is it marked as owned-by-someone-else? */
  458. if (networkstatus_nickname_is_unnamed(nickname)) {
  459. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  460. "router that holds it, but not one listed in the current "
  461. "consensus.", escaped(nickname));
  462. return NULL;
  463. }
  464. /* Okay, so the name is not canonical for anybody. */
  465. {
  466. smartlist_t *matches = smartlist_new();
  467. const node_t *choice = NULL;
  468. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  469. if (!strcasecmp(node_get_nickname(node), nickname))
  470. smartlist_add(matches, node);
  471. } SMARTLIST_FOREACH_END(node);
  472. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  473. int any_unwarned = 0;
  474. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  475. if (!node->name_lookup_warned) {
  476. node->name_lookup_warned = 1;
  477. any_unwarned = 1;
  478. }
  479. } SMARTLIST_FOREACH_END(node);
  480. if (any_unwarned) {
  481. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  482. "but none is listed as Named in the directory consensus. "
  483. "Choosing one arbitrarily.", nickname);
  484. }
  485. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  486. char fp[HEX_DIGEST_LEN+1];
  487. node_t *node = smartlist_get(matches, 0);
  488. if (node->name_lookup_warned) {
  489. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  490. log_warn(LD_CONFIG,
  491. "You specified a server \"%s\" by name, but the directory "
  492. "authorities do not have any key registered for this "
  493. "nickname -- so it could be used by any server, not just "
  494. "the one you meant. "
  495. "To make sure you get the same server in the future, refer "
  496. "to it by key, as \"$%s\".", nickname, fp);
  497. node->name_lookup_warned = 1;
  498. }
  499. }
  500. if (smartlist_len(matches))
  501. choice = smartlist_get(matches, 0);
  502. smartlist_free(matches);
  503. return choice;
  504. }
  505. }
  506. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  507. const char *
  508. node_get_nickname(const node_t *node)
  509. {
  510. tor_assert(node);
  511. if (node->rs)
  512. return node->rs->nickname;
  513. else if (node->ri)
  514. return node->ri->nickname;
  515. else
  516. return NULL;
  517. }
  518. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  519. * latest consensus. */
  520. int
  521. node_is_named(const node_t *node)
  522. {
  523. const char *named_id;
  524. const char *nickname = node_get_nickname(node);
  525. if (!nickname)
  526. return 0;
  527. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  528. if (!named_id)
  529. return 0;
  530. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  531. }
  532. /** Return true iff <b>node</b> appears to be a directory authority or
  533. * directory cache */
  534. int
  535. node_is_dir(const node_t *node)
  536. {
  537. if (node->rs)
  538. return node->rs->dir_port != 0;
  539. else if (node->ri)
  540. return node->ri->dir_port != 0;
  541. else
  542. return 0;
  543. }
  544. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  545. * is, a routerdecriptor or a microdescriptor. */
  546. int
  547. node_has_descriptor(const node_t *node)
  548. {
  549. return (node->ri ||
  550. (node->rs && node->md));
  551. }
  552. /** Return the router_purpose of <b>node</b>. */
  553. int
  554. node_get_purpose(const node_t *node)
  555. {
  556. if (node->ri)
  557. return node->ri->purpose;
  558. else
  559. return ROUTER_PURPOSE_GENERAL;
  560. }
  561. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  562. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  563. * <b>verbose_nickname_out</b> */
  564. void
  565. node_get_verbose_nickname(const node_t *node,
  566. char *verbose_name_out)
  567. {
  568. const char *nickname = node_get_nickname(node);
  569. int is_named = node_is_named(node);
  570. verbose_name_out[0] = '$';
  571. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  572. DIGEST_LEN);
  573. if (!nickname)
  574. return;
  575. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  576. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  577. }
  578. /** Return true iff it seems that <b>node</b> allows circuits to exit
  579. * through it directlry from the client. */
  580. int
  581. node_allows_single_hop_exits(const node_t *node)
  582. {
  583. if (node && node->ri)
  584. return node->ri->allow_single_hop_exits;
  585. else
  586. return 0;
  587. }
  588. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  589. * actually permit anything to exit, or we don't know its exit policy */
  590. int
  591. node_exit_policy_rejects_all(const node_t *node)
  592. {
  593. if (node->rejects_all)
  594. return 1;
  595. if (node->ri)
  596. return node->ri->policy_is_reject_star;
  597. else if (node->md)
  598. return node->md->exit_policy == NULL ||
  599. short_policy_is_reject_star(node->md->exit_policy);
  600. else
  601. return 1;
  602. }
  603. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  604. * addr + TCP port) for <b>node</b>. Caller must free all elements
  605. * using tor_free() and free the list using smartlist_free().
  606. *
  607. * XXX this is potentially a memory fragmentation hog -- if on
  608. * critical path consider the option of having the caller allocate the
  609. * memory
  610. */
  611. smartlist_t *
  612. node_get_all_orports(const node_t *node)
  613. {
  614. smartlist_t *sl = smartlist_new();
  615. if (node->ri != NULL) {
  616. if (node->ri->addr != 0) {
  617. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  618. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  619. ap->port = node->ri->or_port;
  620. smartlist_add(sl, ap);
  621. }
  622. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  623. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  624. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  625. ap->port = node->ri->or_port;
  626. smartlist_add(sl, ap);
  627. }
  628. } else if (node->rs != NULL) {
  629. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  630. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  631. ap->port = node->rs->or_port;
  632. smartlist_add(sl, ap);
  633. }
  634. return sl;
  635. }
  636. /** Wrapper around node_get_prim_orport for backward
  637. compatibility. */
  638. void
  639. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  640. {
  641. tor_addr_port_t ap;
  642. node_get_prim_orport(node, &ap);
  643. tor_addr_copy(addr_out, &ap.addr);
  644. }
  645. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  646. * seem to have one. */
  647. uint32_t
  648. node_get_prim_addr_ipv4h(const node_t *node)
  649. {
  650. if (node->ri) {
  651. return node->ri->addr;
  652. } else if (node->rs) {
  653. return node->rs->addr;
  654. }
  655. return 0;
  656. }
  657. /** Copy a string representation of an IP address for <b>node</b> into
  658. * the <b>len</b>-byte buffer at <b>buf</b>. */
  659. void
  660. node_get_address_string(const node_t *node, char *buf, size_t len)
  661. {
  662. if (node->ri) {
  663. strlcpy(buf, node->ri->address, len);
  664. } else if (node->rs) {
  665. tor_addr_t addr;
  666. tor_addr_from_ipv4h(&addr, node->rs->addr);
  667. tor_addr_to_str(buf, &addr, len, 0);
  668. } else {
  669. buf[0] = '\0';
  670. }
  671. }
  672. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  673. * one. */
  674. long
  675. node_get_declared_uptime(const node_t *node)
  676. {
  677. if (node->ri)
  678. return node->ri->uptime;
  679. else
  680. return -1;
  681. }
  682. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  683. const char *
  684. node_get_platform(const node_t *node)
  685. {
  686. /* If we wanted, we could record the version in the routerstatus_t, since
  687. * the consensus lists it. We don't, though, so this function just won't
  688. * work with microdescriptors. */
  689. if (node->ri)
  690. return node->ri->platform;
  691. else
  692. return NULL;
  693. }
  694. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  695. time_t
  696. node_get_published_on(const node_t *node)
  697. {
  698. if (node->ri)
  699. return node->ri->cache_info.published_on;
  700. else
  701. return 0;
  702. }
  703. /** Return true iff <b>node</b> is one representing this router. */
  704. int
  705. node_is_me(const node_t *node)
  706. {
  707. return router_digest_is_me(node->identity);
  708. }
  709. /** Return <b>node</b> declared family (as a list of names), or NULL if
  710. * the node didn't declare a family. */
  711. const smartlist_t *
  712. node_get_declared_family(const node_t *node)
  713. {
  714. if (node->ri && node->ri->declared_family)
  715. return node->ri->declared_family;
  716. else if (node->md && node->md->family)
  717. return node->md->family;
  718. else
  719. return NULL;
  720. }
  721. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  722. * <b>node</b>, else 0.
  723. *
  724. * We prefer the IPv6 address if the router has an IPv6 address and
  725. * i) the node_t says that it prefers IPv6
  726. * or
  727. * ii) the router has no IPv4 address. */
  728. int
  729. node_ipv6_preferred(const node_t *node)
  730. {
  731. tor_addr_port_t ipv4_addr;
  732. node_assert_ok(node);
  733. if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  734. if (node->ri)
  735. return !tor_addr_is_null(&node->ri->ipv6_addr);
  736. if (node->md)
  737. return !tor_addr_is_null(&node->md->ipv6_addr);
  738. if (node->rs)
  739. return !tor_addr_is_null(&node->rs->ipv6_addr);
  740. }
  741. return 0;
  742. }
  743. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  744. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  745. * port was copied, else return non-zero.*/
  746. int
  747. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  748. {
  749. node_assert_ok(node);
  750. tor_assert(ap_out);
  751. if (node->ri) {
  752. if (node->ri->addr == 0 || node->ri->or_port == 0)
  753. return -1;
  754. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  755. ap_out->port = node->ri->or_port;
  756. return 0;
  757. }
  758. if (node->rs) {
  759. if (node->rs->addr == 0 || node->rs->or_port == 0)
  760. return -1;
  761. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  762. ap_out->port = node->rs->or_port;
  763. return 0;
  764. }
  765. return -1;
  766. }
  767. /** Copy the preferred OR port (IP address and TCP port) for
  768. * <b>node</b> into *<b>ap_out</b>. */
  769. void
  770. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  771. {
  772. tor_assert(ap_out);
  773. /* Cheap implementation of config option ClientUseIPv6 -- simply
  774. don't prefer IPv6 when ClientUseIPv6 is not set. (See #4455 for
  775. more on this subject.) Note that this filter is too strict since
  776. we're hindering not only clients! Erring on the safe side
  777. shouldn't be a problem though. XXX move this check to where
  778. outgoing connections are made? -LN */
  779. if (get_options()->ClientUseIPv6 == 1 && node_ipv6_preferred(node))
  780. node_get_pref_ipv6_orport(node, ap_out);
  781. else
  782. node_get_prim_orport(node, ap_out);
  783. }
  784. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  785. * <b>node</b> into *<b>ap_out</b>. */
  786. void
  787. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  788. {
  789. node_assert_ok(node);
  790. tor_assert(ap_out);
  791. /* We prefer the microdesc over a potential routerstatus here. They
  792. are not being synchronised atm so there might be a chance that
  793. they differ at some point, f.ex. when flipping
  794. UseMicrodescriptors? -LN */
  795. if (node->ri) {
  796. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  797. ap_out->port = node->ri->ipv6_orport;
  798. } else if (node->md) {
  799. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  800. ap_out->port = node->md->ipv6_orport;
  801. } else if (node->rs) {
  802. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  803. ap_out->port = node->rs->ipv6_orport;
  804. }
  805. }
  806. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  807. * each router when the GeoIP database is reloaded, and on all new routers. */
  808. void
  809. node_set_country(node_t *node)
  810. {
  811. if (node->rs)
  812. node->country = geoip_get_country_by_ip(node->rs->addr);
  813. else if (node->ri)
  814. node->country = geoip_get_country_by_ip(node->ri->addr);
  815. else
  816. node->country = -1;
  817. }
  818. /** Set the country code of all routers in the routerlist. */
  819. void
  820. nodelist_refresh_countries(void)
  821. {
  822. smartlist_t *nodes = nodelist_get_list();
  823. SMARTLIST_FOREACH(nodes, node_t *, node,
  824. node_set_country(node));
  825. }
  826. /** Return true iff router1 and router2 have similar enough network addresses
  827. * that we should treat them as being in the same family */
  828. static INLINE int
  829. addrs_in_same_network_family(const tor_addr_t *a1,
  830. const tor_addr_t *a2)
  831. {
  832. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  833. }
  834. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  835. * (case-insensitive), or if <b>node's</b> identity key digest
  836. * matches a hexadecimal value stored in <b>nickname</b>. Return
  837. * false otherwise. */
  838. static int
  839. node_nickname_matches(const node_t *node, const char *nickname)
  840. {
  841. const char *n = node_get_nickname(node);
  842. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  843. return 1;
  844. return hex_digest_nickname_matches(nickname,
  845. node->identity,
  846. n,
  847. node_is_named(node));
  848. }
  849. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  850. static INLINE int
  851. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  852. {
  853. if (!lst) return 0;
  854. SMARTLIST_FOREACH(lst, const char *, name, {
  855. if (node_nickname_matches(node, name))
  856. return 1;
  857. });
  858. return 0;
  859. }
  860. /** Return true iff r1 and r2 are in the same family, but not the same
  861. * router. */
  862. int
  863. nodes_in_same_family(const node_t *node1, const node_t *node2)
  864. {
  865. const or_options_t *options = get_options();
  866. /* Are they in the same family because of their addresses? */
  867. if (options->EnforceDistinctSubnets) {
  868. tor_addr_t a1, a2;
  869. node_get_addr(node1, &a1);
  870. node_get_addr(node2, &a2);
  871. if (addrs_in_same_network_family(&a1, &a2))
  872. return 1;
  873. }
  874. /* Are they in the same family because the agree they are? */
  875. {
  876. const smartlist_t *f1, *f2;
  877. f1 = node_get_declared_family(node1);
  878. f2 = node_get_declared_family(node2);
  879. if (f1 && f2 &&
  880. node_in_nickname_smartlist(f1, node2) &&
  881. node_in_nickname_smartlist(f2, node1))
  882. return 1;
  883. }
  884. /* Are they in the same option because the user says they are? */
  885. if (options->NodeFamilySets) {
  886. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  887. if (routerset_contains_node(rs, node1) &&
  888. routerset_contains_node(rs, node2))
  889. return 1;
  890. });
  891. }
  892. return 0;
  893. }
  894. /**
  895. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  896. * the smartlist <b>sl</b>.
  897. *
  898. * This is used to make sure we don't pick siblings in a single path, or
  899. * pick more than one relay from a family for our entry guard list.
  900. * Note that a node may be added to <b>sl</b> more than once if it is
  901. * part of <b>node</b>'s family for more than one reason.
  902. */
  903. void
  904. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  905. {
  906. const smartlist_t *all_nodes = nodelist_get_list();
  907. const smartlist_t *declared_family;
  908. const or_options_t *options = get_options();
  909. tor_assert(node);
  910. declared_family = node_get_declared_family(node);
  911. /* Let's make sure that we have the node itself, if it's a real node. */
  912. {
  913. const node_t *real_node = node_get_by_id(node->identity);
  914. if (real_node)
  915. smartlist_add(sl, (node_t*)real_node);
  916. }
  917. /* First, add any nodes with similar network addresses. */
  918. if (options->EnforceDistinctSubnets) {
  919. tor_addr_t node_addr;
  920. node_get_addr(node, &node_addr);
  921. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  922. tor_addr_t a;
  923. node_get_addr(node2, &a);
  924. if (addrs_in_same_network_family(&a, &node_addr))
  925. smartlist_add(sl, (void*)node2);
  926. } SMARTLIST_FOREACH_END(node2);
  927. }
  928. /* Now, add all nodes in the declared_family of this node, if they
  929. * also declare this node to be in their family. */
  930. if (declared_family) {
  931. /* Add every r such that router declares familyness with node, and node
  932. * declares familyhood with router. */
  933. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  934. const node_t *node2;
  935. const smartlist_t *family2;
  936. if (!(node2 = node_get_by_nickname(name, 0)))
  937. continue;
  938. if (!(family2 = node_get_declared_family(node2)))
  939. continue;
  940. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  941. if (node_nickname_matches(node, name2)) {
  942. smartlist_add(sl, (void*)node2);
  943. break;
  944. }
  945. } SMARTLIST_FOREACH_END(name2);
  946. } SMARTLIST_FOREACH_END(name);
  947. }
  948. /* If the user declared any families locally, honor those too. */
  949. if (options->NodeFamilySets) {
  950. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  951. if (routerset_contains_node(rs, node)) {
  952. routerset_get_all_nodes(sl, rs, NULL, 0);
  953. }
  954. });
  955. }
  956. }
  957. /** Find a router that's up, that has this IP address, and
  958. * that allows exit to this address:port, or return NULL if there
  959. * isn't a good one.
  960. * Don't exit enclave to excluded relays -- it wouldn't actually
  961. * hurt anything, but this way there are fewer confused users.
  962. */
  963. const node_t *
  964. router_find_exact_exit_enclave(const char *address, uint16_t port)
  965. {/*XXXX MOVE*/
  966. uint32_t addr;
  967. struct in_addr in;
  968. tor_addr_t a;
  969. const or_options_t *options = get_options();
  970. if (!tor_inet_aton(address, &in))
  971. return NULL; /* it's not an IP already */
  972. addr = ntohl(in.s_addr);
  973. tor_addr_from_ipv4h(&a, addr);
  974. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  975. if (node_get_addr_ipv4h(node) == addr &&
  976. node->is_running &&
  977. compare_tor_addr_to_node_policy(&a, port, node) ==
  978. ADDR_POLICY_ACCEPTED &&
  979. !routerset_contains_node(options->_ExcludeExitNodesUnion, node))
  980. return node;
  981. });
  982. return NULL;
  983. }
  984. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  985. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  986. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  987. * bandwidth.
  988. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  989. */
  990. int
  991. node_is_unreliable(const node_t *node, int need_uptime,
  992. int need_capacity, int need_guard)
  993. {
  994. if (need_uptime && !node->is_stable)
  995. return 1;
  996. if (need_capacity && !node->is_fast)
  997. return 1;
  998. if (need_guard && !node->is_possible_guard)
  999. return 1;
  1000. return 0;
  1001. }
  1002. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1003. * addr:port, return 0 if any might accept it. */
  1004. int
  1005. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1006. int need_uptime)
  1007. {
  1008. addr_policy_result_t r;
  1009. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1010. if (node->is_running &&
  1011. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1012. r = compare_tor_addr_to_node_policy(addr, port, node);
  1013. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1014. return 0; /* this one could be ok. good enough. */
  1015. }
  1016. } SMARTLIST_FOREACH_END(node);
  1017. return 1; /* all will reject. */
  1018. }