nodelist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 "config.h"
  8. #include "dirserv.h"
  9. #include "microdesc.h"
  10. #include "networkstatus.h"
  11. #include "nodelist.h"
  12. #include "policies.h"
  13. #include "router.h"
  14. #include "routerlist.h"
  15. #include <string.h>
  16. static void nodelist_drop_node(node_t *node, int remove_from_ht);
  17. static void node_free(node_t *node);
  18. /** A nodelist_t holds a node_t object for every router we're "willing to use
  19. * for something". Specifically, it should hold a node_t for every node that
  20. * is currently in the routerlist, or currently in the consensus we're using.
  21. */
  22. typedef struct nodelist_t {
  23. /* A list of all the nodes. */
  24. smartlist_t *nodes;
  25. /* Hash table to map from node ID digest to node. */
  26. HT_HEAD(nodelist_map, node_t) nodes_by_id;
  27. } nodelist_t;
  28. static INLINE unsigned int
  29. node_id_hash(const node_t *node)
  30. {
  31. #if SIZEOF_INT == 4
  32. const uint32_t *p = (const uint32_t*)node->identity;
  33. return p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4];
  34. #elif SIZEOF_INT == 8
  35. const uint64_t *p = (const uint32_t*)node->identity;
  36. const uint32_t *p32 = (const uint32_t*)node->identity;
  37. return p[0] ^ p[1] ^ p32[4];
  38. #endif
  39. }
  40. static INLINE unsigned int
  41. node_id_eq(const node_t *node1, const node_t *node2)
  42. {
  43. return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
  44. }
  45. HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
  46. HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
  47. 0.6, malloc, realloc, free);
  48. /** The global nodelist. */
  49. static nodelist_t *the_nodelist=NULL;
  50. /** Create an empty nodelist if we haven't done so already. */
  51. static void
  52. init_nodelist(void)
  53. {
  54. if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
  55. the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
  56. HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
  57. the_nodelist->nodes = smartlist_new();
  58. }
  59. }
  60. /** As node_get_by_id, but returns a non-const pointer */
  61. node_t *
  62. node_get_mutable_by_id(const char *identity_digest)
  63. {
  64. node_t search, *node;
  65. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  66. return NULL;
  67. memcpy(&search.identity, identity_digest, DIGEST_LEN);
  68. node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
  69. return node;
  70. }
  71. /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
  72. * if no such node exists. */
  73. const node_t *
  74. node_get_by_id(const char *identity_digest)
  75. {
  76. return node_get_mutable_by_id(identity_digest);
  77. }
  78. /** Internal: return the node_t whose identity_digest is
  79. * <b>identity_digest</b>. If none exists, create a new one, add it to the
  80. * nodelist, and return it.
  81. *
  82. * Requires that the nodelist be initialized.
  83. */
  84. static node_t *
  85. node_get_or_create(const char *identity_digest)
  86. {
  87. node_t *node;
  88. if ((node = node_get_mutable_by_id(identity_digest)))
  89. return node;
  90. node = tor_malloc_zero(sizeof(node_t));
  91. memcpy(node->identity, identity_digest, DIGEST_LEN);
  92. HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
  93. smartlist_add(the_nodelist->nodes, node);
  94. node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
  95. node->country = -1;
  96. return node;
  97. }
  98. /** Called when a node's address changes. */
  99. static void
  100. node_addrs_changed(node_t *node)
  101. {
  102. node->last_reachable = node->last_reachable6 = 0;
  103. node->testing_since = node->testing_since6 = 0;
  104. node->country = -1;
  105. }
  106. /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
  107. * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
  108. * to the previous routerinfo.
  109. */
  110. node_t *
  111. nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
  112. {
  113. node_t *node;
  114. const char *id_digest;
  115. int had_router = 0;
  116. tor_assert(ri);
  117. init_nodelist();
  118. id_digest = ri->cache_info.identity_digest;
  119. node = node_get_or_create(id_digest);
  120. if (node->ri) {
  121. if (!routers_have_same_or_addrs(node->ri, ri)) {
  122. node_addrs_changed(node);
  123. }
  124. had_router = 1;
  125. if (ri_old_out)
  126. *ri_old_out = node->ri;
  127. } else {
  128. if (ri_old_out)
  129. *ri_old_out = NULL;
  130. }
  131. node->ri = ri;
  132. if (node->country == -1)
  133. node_set_country(node);
  134. if (authdir_mode(get_options()) && !had_router) {
  135. const char *discard=NULL;
  136. uint32_t status = dirserv_router_get_status(ri, &discard);
  137. dirserv_set_node_flags_from_authoritative_status(node, status);
  138. }
  139. return node;
  140. }
  141. /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
  142. *
  143. * Called when a new microdesc has arrived and the usable consensus flavor
  144. * is "microdesc".
  145. **/
  146. node_t *
  147. nodelist_add_microdesc(microdesc_t *md)
  148. {
  149. networkstatus_t *ns =
  150. networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  151. const routerstatus_t *rs;
  152. node_t *node;
  153. if (ns == NULL)
  154. return NULL;
  155. init_nodelist();
  156. /* Microdescriptors don't carry an identity digest, so we need to figure
  157. * it out by looking up the routerstatus. */
  158. rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
  159. if (rs == NULL)
  160. return NULL;
  161. node = node_get_mutable_by_id(rs->identity_digest);
  162. if (node) {
  163. if (node->md)
  164. node->md->held_by_nodes--;
  165. node->md = md;
  166. md->held_by_nodes++;
  167. }
  168. return node;
  169. }
  170. /** Tell the nodelist that the current usable consensus is <b>ns</b>.
  171. * This makes the nodelist change all of the routerstatus entries for
  172. * the nodes, drop nodes that no longer have enough info to get used,
  173. * and grab microdescriptors into nodes as appropriate.
  174. */
  175. void
  176. nodelist_set_consensus(networkstatus_t *ns)
  177. {
  178. const or_options_t *options = get_options();
  179. int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
  180. int client = !server_mode(options);
  181. init_nodelist();
  182. if (ns->flavor == FLAV_MICRODESC)
  183. (void) get_microdesc_cache(); /* Make sure it exists first. */
  184. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  185. node->rs = NULL);
  186. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  187. node_t *node = node_get_or_create(rs->identity_digest);
  188. node->rs = rs;
  189. if (ns->flavor == FLAV_MICRODESC) {
  190. if (node->md == NULL ||
  191. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  192. if (node->md)
  193. node->md->held_by_nodes--;
  194. node->md = microdesc_cache_lookup_by_digest256(NULL,
  195. rs->descriptor_digest);
  196. if (node->md)
  197. node->md->held_by_nodes++;
  198. }
  199. }
  200. node_set_country(node);
  201. /* If we're not an authdir, believe others. */
  202. if (!authdir) {
  203. node->is_valid = rs->is_valid;
  204. node->is_running = rs->is_flagged_running;
  205. node->is_fast = rs->is_fast;
  206. node->is_stable = rs->is_stable;
  207. node->is_possible_guard = rs->is_possible_guard;
  208. node->is_exit = rs->is_exit;
  209. node->is_bad_directory = rs->is_bad_directory;
  210. node->is_bad_exit = rs->is_bad_exit;
  211. node->is_hs_dir = rs->is_hs_dir;
  212. node->ipv6_preferred = 0;
  213. if (client && options->ClientPreferIPv6ORPort == 1 &&
  214. (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
  215. (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
  216. node->ipv6_preferred = 1;
  217. }
  218. } SMARTLIST_FOREACH_END(rs);
  219. nodelist_purge();
  220. if (! authdir) {
  221. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  222. /* We have no routerstatus for this router. Clear flags so we can skip
  223. * it, maybe.*/
  224. if (!node->rs) {
  225. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  226. * would have removed it. */
  227. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  228. /* Clear all flags. */
  229. node->is_valid = node->is_running = node->is_hs_dir =
  230. node->is_fast = node->is_stable =
  231. node->is_possible_guard = node->is_exit =
  232. node->is_bad_exit = node->is_bad_directory =
  233. node->ipv6_preferred = 0;
  234. }
  235. }
  236. } SMARTLIST_FOREACH_END(node);
  237. }
  238. }
  239. /** Helper: return true iff a node has a usable amount of information*/
  240. static INLINE int
  241. node_is_usable(const node_t *node)
  242. {
  243. return (node->rs) || (node->ri);
  244. }
  245. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  246. * node with <b>identity_digest</b>. */
  247. void
  248. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  249. {
  250. node_t *node = node_get_mutable_by_id(identity_digest);
  251. if (node && node->md == md) {
  252. node->md = NULL;
  253. md->held_by_nodes--;
  254. }
  255. }
  256. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  257. void
  258. nodelist_remove_routerinfo(routerinfo_t *ri)
  259. {
  260. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  261. if (node && node->ri == ri) {
  262. node->ri = NULL;
  263. if (! node_is_usable(node)) {
  264. nodelist_drop_node(node, 1);
  265. node_free(node);
  266. }
  267. }
  268. }
  269. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  270. * with.) */
  271. static void
  272. nodelist_drop_node(node_t *node, int remove_from_ht)
  273. {
  274. node_t *tmp;
  275. int idx;
  276. if (remove_from_ht) {
  277. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  278. tor_assert(tmp == node);
  279. }
  280. idx = node->nodelist_idx;
  281. tor_assert(idx >= 0);
  282. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  283. smartlist_del(the_nodelist->nodes, idx);
  284. if (idx < smartlist_len(the_nodelist->nodes)) {
  285. tmp = smartlist_get(the_nodelist->nodes, idx);
  286. tmp->nodelist_idx = idx;
  287. }
  288. node->nodelist_idx = -1;
  289. }
  290. /** Release storage held by <b>node</b> */
  291. static void
  292. node_free(node_t *node)
  293. {
  294. if (!node)
  295. return;
  296. if (node->md)
  297. node->md->held_by_nodes--;
  298. tor_assert(node->nodelist_idx == -1);
  299. tor_free(node);
  300. }
  301. /** Remove all entries from the nodelist that don't have enough info to be
  302. * usable for anything. */
  303. void
  304. nodelist_purge(void)
  305. {
  306. node_t **iter;
  307. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  308. return;
  309. /* Remove the non-usable nodes. */
  310. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  311. node_t *node = *iter;
  312. if (node->md && !node->rs) {
  313. /* An md is only useful if there is an rs. */
  314. node->md->held_by_nodes--;
  315. node->md = NULL;
  316. }
  317. if (node_is_usable(node)) {
  318. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  319. } else {
  320. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  321. nodelist_drop_node(node, 0);
  322. node_free(node);
  323. }
  324. }
  325. nodelist_assert_ok();
  326. }
  327. /** Release all storage held by the nodelist. */
  328. void
  329. nodelist_free_all(void)
  330. {
  331. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  332. return;
  333. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  334. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  335. node->nodelist_idx = -1;
  336. node_free(node);
  337. } SMARTLIST_FOREACH_END(node);
  338. smartlist_free(the_nodelist->nodes);
  339. tor_free(the_nodelist);
  340. }
  341. /** Check that the nodelist is internally consistent, and consistent with
  342. * the directory info it's derived from.
  343. */
  344. void
  345. nodelist_assert_ok(void)
  346. {
  347. routerlist_t *rl = router_get_routerlist();
  348. networkstatus_t *ns = networkstatus_get_latest_consensus();
  349. digestmap_t *dm;
  350. if (!the_nodelist)
  351. return;
  352. dm = digestmap_new();
  353. /* every routerinfo in rl->routers should be in the nodelist. */
  354. if (rl) {
  355. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  356. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  357. tor_assert(node && node->ri == ri);
  358. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  359. node->identity, DIGEST_LEN));
  360. tor_assert(! digestmap_get(dm, node->identity));
  361. digestmap_set(dm, node->identity, (void*)node);
  362. } SMARTLIST_FOREACH_END(ri);
  363. }
  364. /* every routerstatus in ns should be in the nodelist */
  365. if (ns) {
  366. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  367. const node_t *node = node_get_by_id(rs->identity_digest);
  368. tor_assert(node && node->rs == rs);
  369. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  370. digestmap_set(dm, node->identity, (void*)node);
  371. if (ns->flavor == FLAV_MICRODESC) {
  372. /* If it's a microdesc consensus, every entry that has a
  373. * microdescriptor should be in the nodelist.
  374. */
  375. microdesc_t *md =
  376. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  377. tor_assert(md == node->md);
  378. if (md)
  379. tor_assert(md->held_by_nodes >= 1);
  380. }
  381. } SMARTLIST_FOREACH_END(rs);
  382. }
  383. /* The nodelist should have no other entries, and its entries should be
  384. * well-formed. */
  385. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  386. tor_assert(digestmap_get(dm, node->identity) != NULL);
  387. tor_assert(node_sl_idx == node->nodelist_idx);
  388. } SMARTLIST_FOREACH_END(node);
  389. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  390. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  391. digestmap_free(dm, NULL);
  392. }
  393. /** Return a list of a node_t * for every node we know about. The caller
  394. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  395. * you must, but you must not add or remove nodes.) */
  396. smartlist_t *
  397. nodelist_get_list(void)
  398. {
  399. init_nodelist();
  400. return the_nodelist->nodes;
  401. }
  402. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  403. * or $DIGEST~name, return the node with the matching identity digest and
  404. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  405. * is not well-formed. */
  406. const node_t *
  407. node_get_by_hex_id(const char *hex_id)
  408. {
  409. char digest_buf[DIGEST_LEN];
  410. char nn_buf[MAX_NICKNAME_LEN+1];
  411. char nn_char='\0';
  412. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  413. const node_t *node = node_get_by_id(digest_buf);
  414. if (!node)
  415. return NULL;
  416. if (nn_char) {
  417. const char *real_name = node_get_nickname(node);
  418. if (!real_name || strcasecmp(real_name, nn_buf))
  419. return NULL;
  420. if (nn_char == '=') {
  421. const char *named_id =
  422. networkstatus_get_router_digest_by_nickname(nn_buf);
  423. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  424. return NULL;
  425. }
  426. }
  427. return node;
  428. }
  429. return NULL;
  430. }
  431. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  432. * the corresponding node_t, or NULL if none exists. Warn the user if
  433. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  434. * nickname, but the Named flag isn't set for that router. */
  435. const node_t *
  436. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  437. {
  438. const node_t *node;
  439. if (!the_nodelist)
  440. return NULL;
  441. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  442. if ((node = node_get_by_hex_id(nickname)) != NULL)
  443. return node;
  444. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  445. return NULL;
  446. /* Okay, so if we get here, the nickname is just a nickname. Is there
  447. * a binding for it in the consensus? */
  448. {
  449. const char *named_id =
  450. networkstatus_get_router_digest_by_nickname(nickname);
  451. if (named_id)
  452. return node_get_by_id(named_id);
  453. }
  454. /* Is it marked as owned-by-someone-else? */
  455. if (networkstatus_nickname_is_unnamed(nickname)) {
  456. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  457. "router that holds it, but not one listed in the current "
  458. "consensus.", escaped(nickname));
  459. return NULL;
  460. }
  461. /* Okay, so the name is not canonical for anybody. */
  462. {
  463. smartlist_t *matches = smartlist_new();
  464. const node_t *choice = NULL;
  465. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  466. if (!strcasecmp(node_get_nickname(node), nickname))
  467. smartlist_add(matches, node);
  468. } SMARTLIST_FOREACH_END(node);
  469. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  470. int any_unwarned = 0;
  471. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  472. if (!node->name_lookup_warned) {
  473. node->name_lookup_warned = 1;
  474. any_unwarned = 1;
  475. }
  476. } SMARTLIST_FOREACH_END(node);
  477. if (any_unwarned) {
  478. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  479. "but none is listed as Named in the directory consensus. "
  480. "Choosing one arbitrarily.", nickname);
  481. }
  482. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  483. char fp[HEX_DIGEST_LEN+1];
  484. node_t *node = smartlist_get(matches, 0);
  485. if (node->name_lookup_warned) {
  486. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  487. log_warn(LD_CONFIG,
  488. "You specified a server \"%s\" by name, but the directory "
  489. "authorities do not have any key registered for this "
  490. "nickname -- so it could be used by any server, not just "
  491. "the one you meant. "
  492. "To make sure you get the same server in the future, refer "
  493. "to it by key, as \"$%s\".", nickname, fp);
  494. node->name_lookup_warned = 1;
  495. }
  496. }
  497. if (smartlist_len(matches))
  498. choice = smartlist_get(matches, 0);
  499. smartlist_free(matches);
  500. return choice;
  501. }
  502. }
  503. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  504. const char *
  505. node_get_nickname(const node_t *node)
  506. {
  507. tor_assert(node);
  508. if (node->rs)
  509. return node->rs->nickname;
  510. else if (node->ri)
  511. return node->ri->nickname;
  512. else
  513. return NULL;
  514. }
  515. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  516. * latest consensus. */
  517. int
  518. node_is_named(const node_t *node)
  519. {
  520. const char *named_id;
  521. const char *nickname = node_get_nickname(node);
  522. if (!nickname)
  523. return 0;
  524. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  525. if (!named_id)
  526. return 0;
  527. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  528. }
  529. /** Return true iff <b>node</b> appears to be a directory authority or
  530. * directory cache */
  531. int
  532. node_is_dir(const node_t *node)
  533. {
  534. if (node->rs)
  535. return node->rs->dir_port != 0;
  536. else if (node->ri)
  537. return node->ri->dir_port != 0;
  538. else
  539. return 0;
  540. }
  541. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  542. * is, a routerdecriptor or a microdescriptor. */
  543. int
  544. node_has_descriptor(const node_t *node)
  545. {
  546. return (node->ri ||
  547. (node->rs && node->md));
  548. }
  549. /** Return the router_purpose of <b>node</b>. */
  550. int
  551. node_get_purpose(const node_t *node)
  552. {
  553. if (node->ri)
  554. return node->ri->purpose;
  555. else
  556. return ROUTER_PURPOSE_GENERAL;
  557. }
  558. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  559. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  560. * <b>verbose_nickname_out</b> */
  561. void
  562. node_get_verbose_nickname(const node_t *node,
  563. char *verbose_name_out)
  564. {
  565. const char *nickname = node_get_nickname(node);
  566. int is_named = node_is_named(node);
  567. verbose_name_out[0] = '$';
  568. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  569. DIGEST_LEN);
  570. if (!nickname)
  571. return;
  572. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  573. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  574. }
  575. /** Return true iff it seems that <b>node</b> allows circuits to exit
  576. * through it directlry from the client. */
  577. int
  578. node_allows_single_hop_exits(const node_t *node)
  579. {
  580. if (node && node->ri)
  581. return node->ri->allow_single_hop_exits;
  582. else
  583. return 0;
  584. }
  585. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  586. * actually permit anything to exit, or we don't know its exit policy */
  587. int
  588. node_exit_policy_rejects_all(const node_t *node)
  589. {
  590. if (node->rejects_all)
  591. return 1;
  592. if (node->ri)
  593. return node->ri->policy_is_reject_star;
  594. else if (node->md)
  595. return node->md->exit_policy == NULL ||
  596. short_policy_is_reject_star(node->md->exit_policy);
  597. else
  598. return 1;
  599. }
  600. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  601. * addr + TCP port) for <b>node</b>. Caller must free all elements
  602. * using tor_free() and free the list using smartlist_free().
  603. *
  604. * XXX this is potentially a memory fragmentation hog -- if on
  605. * critical path consider the option of having the caller allocate the
  606. * memory
  607. */
  608. smartlist_t *
  609. node_get_all_orports(const node_t *node)
  610. {
  611. smartlist_t *sl = smartlist_new();
  612. if (node->ri != NULL) {
  613. if (node->ri->addr != 0) {
  614. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  615. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  616. ap->port = node->ri->or_port;
  617. smartlist_add(sl, ap);
  618. }
  619. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  620. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  621. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  622. ap->port = node->ri->or_port;
  623. smartlist_add(sl, ap);
  624. }
  625. } else if (node->rs != NULL) {
  626. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  627. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  628. ap->port = node->rs->or_port;
  629. smartlist_add(sl, ap);
  630. }
  631. return sl;
  632. }
  633. /** Wrapper around node_get_prim_orport for backward
  634. compatibility. */
  635. void
  636. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  637. {
  638. tor_addr_port_t ap;
  639. node_get_prim_orport(node, &ap);
  640. tor_addr_copy(addr_out, &ap.addr);
  641. }
  642. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  643. * seem to have one. */
  644. uint32_t
  645. node_get_prim_addr_ipv4h(const node_t *node)
  646. {
  647. if (node->ri) {
  648. return node->ri->addr;
  649. } else if (node->rs) {
  650. return node->rs->addr;
  651. }
  652. return 0;
  653. }
  654. /** Copy a string representation of an IP address for <b>node</b> into
  655. * the <b>len</b>-byte buffer at <b>buf</b>. */
  656. void
  657. node_get_address_string(const node_t *node, char *buf, size_t len)
  658. {
  659. if (node->ri) {
  660. strlcpy(buf, node->ri->address, len);
  661. } else if (node->rs) {
  662. tor_addr_t addr;
  663. tor_addr_from_ipv4h(&addr, node->rs->addr);
  664. tor_addr_to_str(buf, &addr, len, 0);
  665. } else {
  666. buf[0] = '\0';
  667. }
  668. }
  669. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  670. * one. */
  671. long
  672. node_get_declared_uptime(const node_t *node)
  673. {
  674. if (node->ri)
  675. return node->ri->uptime;
  676. else
  677. return -1;
  678. }
  679. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  680. const char *
  681. node_get_platform(const node_t *node)
  682. {
  683. /* If we wanted, we could record the version in the routerstatus_t, since
  684. * the consensus lists it. We don't, though, so this function just won't
  685. * work with microdescriptors. */
  686. if (node->ri)
  687. return node->ri->platform;
  688. else
  689. return NULL;
  690. }
  691. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  692. time_t
  693. node_get_published_on(const node_t *node)
  694. {
  695. if (node->ri)
  696. return node->ri->cache_info.published_on;
  697. else
  698. return 0;
  699. }
  700. /** Return true iff <b>node</b> is one representing this router. */
  701. int
  702. node_is_me(const node_t *node)
  703. {
  704. return router_digest_is_me(node->identity);
  705. }
  706. /** Return <b>node</b> declared family (as a list of names), or NULL if
  707. * the node didn't declare a family. */
  708. const smartlist_t *
  709. node_get_declared_family(const node_t *node)
  710. {
  711. if (node->ri && node->ri->declared_family)
  712. return node->ri->declared_family;
  713. else if (node->md && node->md->family)
  714. return node->md->family;
  715. else
  716. return NULL;
  717. }
  718. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  719. * <b>node</b>, else 0.
  720. *
  721. * We prefer the IPv6 address if the router has an IPv6 address and
  722. * i) the node_t says that we do prefer IPv6
  723. * or
  724. * ii) the router has no IPv4 address. */
  725. int
  726. node_ipv6_preferred(const node_t *node)
  727. {
  728. tor_addr_port_t ipv4_addr;
  729. node_assert_ok(node);
  730. if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  731. if (node->ri)
  732. return !tor_addr_is_null(&node->ri->ipv6_addr);
  733. if (node->md)
  734. return !tor_addr_is_null(&node->md->ipv6_addr);
  735. if (node->rs)
  736. return !tor_addr_is_null(&node->rs->ipv6_addr);
  737. }
  738. return 0;
  739. }
  740. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  741. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  742. * port was copied, else return non-zero.*/
  743. int
  744. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  745. {
  746. node_assert_ok(node);
  747. tor_assert(ap_out);
  748. if (node->ri) {
  749. if (node->ri->addr == 0 || node->ri->or_port == 0)
  750. return -1;
  751. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  752. ap_out->port = node->ri->or_port;
  753. return 0;
  754. }
  755. if (node->rs) {
  756. if (node->rs->addr == 0 || node->rs->or_port == 0)
  757. return -1;
  758. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  759. ap_out->port = node->rs->or_port;
  760. return 0;
  761. }
  762. return -1;
  763. }
  764. /** Copy the preferred OR port (IP address and TCP port) for
  765. * <b>node</b> into *<b>ap_out</b>. */
  766. void
  767. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  768. {
  769. tor_assert(ap_out);
  770. /* Cheap implementation of config option ClientUseIPv6 -- simply
  771. don't prefer IPv6 when ClientUseIPv6 is not set. (See #4455 for
  772. more on this subject.) Note that this filter is too strict since
  773. we're hindering not only clients! Erring on the safe side
  774. shouldn't be a problem though. XXX move this check to where
  775. outgoing connections are made? -LN */
  776. if (get_options()->ClientUseIPv6 == 1 && node_ipv6_preferred(node))
  777. node_get_pref_ipv6_orport(node, ap_out);
  778. else
  779. node_get_prim_orport(node, ap_out);
  780. }
  781. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  782. * <b>node</b> into *<b>ap_out</b>. */
  783. void
  784. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  785. {
  786. node_assert_ok(node);
  787. tor_assert(ap_out);
  788. if (node->ri) {
  789. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  790. ap_out->port = node->ri->ipv6_orport;
  791. } else if (node->rs) {
  792. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  793. ap_out->port = node->rs->ipv6_orport;
  794. }
  795. }