nodelist.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. init_nodelist();
  181. if (ns->flavor == FLAV_MICRODESC)
  182. (void) get_microdesc_cache(); /* Make sure it exists first. */
  183. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  184. node->rs = NULL);
  185. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  186. node_t *node = node_get_or_create(rs->identity_digest);
  187. node->rs = rs;
  188. if (ns->flavor == FLAV_MICRODESC) {
  189. if (node->md == NULL ||
  190. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  191. if (node->md)
  192. node->md->held_by_nodes--;
  193. node->md = microdesc_cache_lookup_by_digest256(NULL,
  194. rs->descriptor_digest);
  195. if (node->md)
  196. node->md->held_by_nodes++;
  197. }
  198. }
  199. node_set_country(node);
  200. /* If we're not an authdir, believe others. */
  201. if (!authdir) {
  202. node->is_valid = rs->is_valid;
  203. node->is_running = rs->is_flagged_running;
  204. node->is_fast = rs->is_fast;
  205. node->is_stable = rs->is_stable;
  206. node->is_possible_guard = rs->is_possible_guard;
  207. node->is_exit = rs->is_exit;
  208. node->is_bad_directory = rs->is_bad_directory;
  209. node->is_bad_exit = rs->is_bad_exit;
  210. node->is_hs_dir = rs->is_hs_dir;
  211. if (options->ClientPreferIPv6ORPort == 1)
  212. node->ipv6_preferred = !tor_addr_is_null(&rs->ipv6_addr);
  213. }
  214. } SMARTLIST_FOREACH_END(rs);
  215. nodelist_purge();
  216. if (! authdir) {
  217. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  218. /* We have no routerstatus for this router. Clear flags so we can skip
  219. * it, maybe.*/
  220. if (!node->rs) {
  221. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  222. * would have removed it. */
  223. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  224. /* Clear all flags. */
  225. node->is_valid = node->is_running = node->is_hs_dir =
  226. node->is_fast = node->is_stable =
  227. node->is_possible_guard = node->is_exit =
  228. node->is_bad_exit = node->is_bad_directory =
  229. node->ipv6_preferred = 0;
  230. }
  231. }
  232. } SMARTLIST_FOREACH_END(node);
  233. }
  234. }
  235. /** Helper: return true iff a node has a usable amount of information*/
  236. static INLINE int
  237. node_is_usable(const node_t *node)
  238. {
  239. return (node->rs) || (node->ri);
  240. }
  241. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  242. * node with <b>identity_digest</b>. */
  243. void
  244. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  245. {
  246. node_t *node = node_get_mutable_by_id(identity_digest);
  247. if (node && node->md == md) {
  248. node->md = NULL;
  249. md->held_by_nodes--;
  250. }
  251. }
  252. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  253. void
  254. nodelist_remove_routerinfo(routerinfo_t *ri)
  255. {
  256. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  257. if (node && node->ri == ri) {
  258. node->ri = NULL;
  259. if (! node_is_usable(node)) {
  260. nodelist_drop_node(node, 1);
  261. node_free(node);
  262. }
  263. }
  264. }
  265. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  266. * with.) */
  267. static void
  268. nodelist_drop_node(node_t *node, int remove_from_ht)
  269. {
  270. node_t *tmp;
  271. int idx;
  272. if (remove_from_ht) {
  273. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  274. tor_assert(tmp == node);
  275. }
  276. idx = node->nodelist_idx;
  277. tor_assert(idx >= 0);
  278. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  279. smartlist_del(the_nodelist->nodes, idx);
  280. if (idx < smartlist_len(the_nodelist->nodes)) {
  281. tmp = smartlist_get(the_nodelist->nodes, idx);
  282. tmp->nodelist_idx = idx;
  283. }
  284. node->nodelist_idx = -1;
  285. }
  286. /** Release storage held by <b>node</b> */
  287. static void
  288. node_free(node_t *node)
  289. {
  290. if (!node)
  291. return;
  292. if (node->md)
  293. node->md->held_by_nodes--;
  294. tor_assert(node->nodelist_idx == -1);
  295. tor_free(node);
  296. }
  297. /** Remove all entries from the nodelist that don't have enough info to be
  298. * usable for anything. */
  299. void
  300. nodelist_purge(void)
  301. {
  302. node_t **iter;
  303. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  304. return;
  305. /* Remove the non-usable nodes. */
  306. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  307. node_t *node = *iter;
  308. if (node->md && !node->rs) {
  309. /* An md is only useful if there is an rs. */
  310. node->md->held_by_nodes--;
  311. node->md = NULL;
  312. }
  313. if (node_is_usable(node)) {
  314. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  315. } else {
  316. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  317. nodelist_drop_node(node, 0);
  318. node_free(node);
  319. }
  320. }
  321. nodelist_assert_ok();
  322. }
  323. /** Release all storage held by the nodelist. */
  324. void
  325. nodelist_free_all(void)
  326. {
  327. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  328. return;
  329. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  330. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  331. node->nodelist_idx = -1;
  332. node_free(node);
  333. } SMARTLIST_FOREACH_END(node);
  334. smartlist_free(the_nodelist->nodes);
  335. tor_free(the_nodelist);
  336. }
  337. /** Check that the nodelist is internally consistent, and consistent with
  338. * the directory info it's derived from.
  339. */
  340. void
  341. nodelist_assert_ok(void)
  342. {
  343. routerlist_t *rl = router_get_routerlist();
  344. networkstatus_t *ns = networkstatus_get_latest_consensus();
  345. digestmap_t *dm;
  346. if (!the_nodelist)
  347. return;
  348. dm = digestmap_new();
  349. /* every routerinfo in rl->routers should be in the nodelist. */
  350. if (rl) {
  351. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  352. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  353. tor_assert(node && node->ri == ri);
  354. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  355. node->identity, DIGEST_LEN));
  356. tor_assert(! digestmap_get(dm, node->identity));
  357. digestmap_set(dm, node->identity, (void*)node);
  358. } SMARTLIST_FOREACH_END(ri);
  359. }
  360. /* every routerstatus in ns should be in the nodelist */
  361. if (ns) {
  362. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  363. const node_t *node = node_get_by_id(rs->identity_digest);
  364. tor_assert(node && node->rs == rs);
  365. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  366. digestmap_set(dm, node->identity, (void*)node);
  367. if (ns->flavor == FLAV_MICRODESC) {
  368. /* If it's a microdesc consensus, every entry that has a
  369. * microdescriptor should be in the nodelist.
  370. */
  371. microdesc_t *md =
  372. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  373. tor_assert(md == node->md);
  374. if (md)
  375. tor_assert(md->held_by_nodes >= 1);
  376. }
  377. } SMARTLIST_FOREACH_END(rs);
  378. }
  379. /* The nodelist should have no other entries, and its entries should be
  380. * well-formed. */
  381. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  382. tor_assert(digestmap_get(dm, node->identity) != NULL);
  383. tor_assert(node_sl_idx == node->nodelist_idx);
  384. } SMARTLIST_FOREACH_END(node);
  385. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  386. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  387. digestmap_free(dm, NULL);
  388. }
  389. /** Return a list of a node_t * for every node we know about. The caller
  390. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  391. * you must, but you must not add or remove nodes.) */
  392. smartlist_t *
  393. nodelist_get_list(void)
  394. {
  395. init_nodelist();
  396. return the_nodelist->nodes;
  397. }
  398. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  399. * or $DIGEST~name, return the node with the matching identity digest and
  400. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  401. * is not well-formed. */
  402. const node_t *
  403. node_get_by_hex_id(const char *hex_id)
  404. {
  405. char digest_buf[DIGEST_LEN];
  406. char nn_buf[MAX_NICKNAME_LEN+1];
  407. char nn_char='\0';
  408. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  409. const node_t *node = node_get_by_id(digest_buf);
  410. if (!node)
  411. return NULL;
  412. if (nn_char) {
  413. const char *real_name = node_get_nickname(node);
  414. if (!real_name || strcasecmp(real_name, nn_buf))
  415. return NULL;
  416. if (nn_char == '=') {
  417. const char *named_id =
  418. networkstatus_get_router_digest_by_nickname(nn_buf);
  419. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  420. return NULL;
  421. }
  422. }
  423. return node;
  424. }
  425. return NULL;
  426. }
  427. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  428. * the corresponding node_t, or NULL if none exists. Warn the user if
  429. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  430. * nickname, but the Named flag isn't set for that router. */
  431. const node_t *
  432. node_get_by_nickname(const char *nickname, int warn_if_unnamed)
  433. {
  434. const node_t *node;
  435. if (!the_nodelist)
  436. return NULL;
  437. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  438. if ((node = node_get_by_hex_id(nickname)) != NULL)
  439. return node;
  440. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  441. return NULL;
  442. /* Okay, so if we get here, the nickname is just a nickname. Is there
  443. * a binding for it in the consensus? */
  444. {
  445. const char *named_id =
  446. networkstatus_get_router_digest_by_nickname(nickname);
  447. if (named_id)
  448. return node_get_by_id(named_id);
  449. }
  450. /* Is it marked as owned-by-someone-else? */
  451. if (networkstatus_nickname_is_unnamed(nickname)) {
  452. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  453. "router that holds it, but not one listed in the current "
  454. "consensus.", escaped(nickname));
  455. return NULL;
  456. }
  457. /* Okay, so the name is not canonical for anybody. */
  458. {
  459. smartlist_t *matches = smartlist_new();
  460. const node_t *choice = NULL;
  461. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  462. if (!strcasecmp(node_get_nickname(node), nickname))
  463. smartlist_add(matches, node);
  464. } SMARTLIST_FOREACH_END(node);
  465. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  466. int any_unwarned = 0;
  467. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  468. if (!node->name_lookup_warned) {
  469. node->name_lookup_warned = 1;
  470. any_unwarned = 1;
  471. }
  472. } SMARTLIST_FOREACH_END(node);
  473. if (any_unwarned) {
  474. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  475. "but none is listed as Named in the directory consensus. "
  476. "Choosing one arbitrarily.", nickname);
  477. }
  478. } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
  479. char fp[HEX_DIGEST_LEN+1];
  480. node_t *node = smartlist_get(matches, 0);
  481. if (node->name_lookup_warned) {
  482. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  483. log_warn(LD_CONFIG,
  484. "You specified a server \"%s\" by name, but the directory "
  485. "authorities do not have any key registered for this "
  486. "nickname -- so it could be used by any server, not just "
  487. "the one you meant. "
  488. "To make sure you get the same server in the future, refer "
  489. "to it by key, as \"$%s\".", nickname, fp);
  490. node->name_lookup_warned = 1;
  491. }
  492. }
  493. if (smartlist_len(matches))
  494. choice = smartlist_get(matches, 0);
  495. smartlist_free(matches);
  496. return choice;
  497. }
  498. }
  499. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  500. const char *
  501. node_get_nickname(const node_t *node)
  502. {
  503. tor_assert(node);
  504. if (node->rs)
  505. return node->rs->nickname;
  506. else if (node->ri)
  507. return node->ri->nickname;
  508. else
  509. return NULL;
  510. }
  511. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  512. * latest consensus. */
  513. int
  514. node_is_named(const node_t *node)
  515. {
  516. const char *named_id;
  517. const char *nickname = node_get_nickname(node);
  518. if (!nickname)
  519. return 0;
  520. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  521. if (!named_id)
  522. return 0;
  523. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  524. }
  525. /** Return true iff <b>node</b> appears to be a directory authority or
  526. * directory cache */
  527. int
  528. node_is_dir(const node_t *node)
  529. {
  530. if (node->rs)
  531. return node->rs->dir_port != 0;
  532. else if (node->ri)
  533. return node->ri->dir_port != 0;
  534. else
  535. return 0;
  536. }
  537. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  538. * is, a routerdecriptor or a microdescriptor. */
  539. int
  540. node_has_descriptor(const node_t *node)
  541. {
  542. return (node->ri ||
  543. (node->rs && node->md));
  544. }
  545. /** Return the router_purpose of <b>node</b>. */
  546. int
  547. node_get_purpose(const node_t *node)
  548. {
  549. if (node->ri)
  550. return node->ri->purpose;
  551. else
  552. return ROUTER_PURPOSE_GENERAL;
  553. }
  554. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  555. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  556. * <b>verbose_nickname_out</b> */
  557. void
  558. node_get_verbose_nickname(const node_t *node,
  559. char *verbose_name_out)
  560. {
  561. const char *nickname = node_get_nickname(node);
  562. int is_named = node_is_named(node);
  563. verbose_name_out[0] = '$';
  564. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  565. DIGEST_LEN);
  566. if (!nickname)
  567. return;
  568. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  569. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  570. }
  571. /** Return true iff it seems that <b>node</b> allows circuits to exit
  572. * through it directlry from the client. */
  573. int
  574. node_allows_single_hop_exits(const node_t *node)
  575. {
  576. if (node && node->ri)
  577. return node->ri->allow_single_hop_exits;
  578. else
  579. return 0;
  580. }
  581. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  582. * actually permit anything to exit, or we don't know its exit policy */
  583. int
  584. node_exit_policy_rejects_all(const node_t *node)
  585. {
  586. if (node->rejects_all)
  587. return 1;
  588. if (node->ri)
  589. return node->ri->policy_is_reject_star;
  590. else if (node->md)
  591. return node->md->exit_policy == NULL ||
  592. short_policy_is_reject_star(node->md->exit_policy);
  593. else
  594. return 1;
  595. }
  596. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  597. * addr + TCP port) for <b>node</b>. Caller must free all elements
  598. * using tor_free() and free the list using smartlist_free().
  599. *
  600. * XXX this is potentially a memory fragmentation hog -- if on
  601. * critical path consider the option of having the caller allocate the
  602. * memory
  603. */
  604. smartlist_t *
  605. node_get_all_orports(const node_t *node)
  606. {
  607. smartlist_t *sl = smartlist_new();
  608. if (node->ri != NULL) {
  609. if (node->ri->addr != 0) {
  610. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  611. tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
  612. ap->port = node->ri->or_port;
  613. smartlist_add(sl, ap);
  614. }
  615. if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
  616. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  617. tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
  618. ap->port = node->ri->or_port;
  619. smartlist_add(sl, ap);
  620. }
  621. } else if (node->rs != NULL) {
  622. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
  623. tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
  624. ap->port = node->rs->or_port;
  625. smartlist_add(sl, ap);
  626. }
  627. return sl;
  628. }
  629. /** Wrapper around node_get_prim_orport for backward
  630. compatibility. */
  631. void
  632. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  633. {
  634. tor_addr_port_t ap;
  635. node_get_prim_orport(node, &ap);
  636. tor_addr_copy(addr_out, &ap.addr);
  637. }
  638. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  639. * seem to have one. */
  640. uint32_t
  641. node_get_prim_addr_ipv4h(const node_t *node)
  642. {
  643. if (node->ri) {
  644. return node->ri->addr;
  645. } else if (node->rs) {
  646. return node->rs->addr;
  647. }
  648. return 0;
  649. }
  650. /** Copy a string representation of an IP address for <b>node</b> into
  651. * the <b>len</b>-byte buffer at <b>buf</b>. */
  652. void
  653. node_get_address_string(const node_t *node, char *buf, size_t len)
  654. {
  655. if (node->ri) {
  656. strlcpy(buf, node->ri->address, len);
  657. } else if (node->rs) {
  658. tor_addr_t addr;
  659. tor_addr_from_ipv4h(&addr, node->rs->addr);
  660. tor_addr_to_str(buf, &addr, len, 0);
  661. } else {
  662. buf[0] = '\0';
  663. }
  664. }
  665. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  666. * one. */
  667. long
  668. node_get_declared_uptime(const node_t *node)
  669. {
  670. if (node->ri)
  671. return node->ri->uptime;
  672. else
  673. return -1;
  674. }
  675. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  676. const char *
  677. node_get_platform(const node_t *node)
  678. {
  679. /* If we wanted, we could record the version in the routerstatus_t, since
  680. * the consensus lists it. We don't, though, so this function just won't
  681. * work with microdescriptors. */
  682. if (node->ri)
  683. return node->ri->platform;
  684. else
  685. return NULL;
  686. }
  687. /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
  688. time_t
  689. node_get_published_on(const node_t *node)
  690. {
  691. if (node->ri)
  692. return node->ri->cache_info.published_on;
  693. else
  694. return 0;
  695. }
  696. /** Return true iff <b>node</b> is one representing this router. */
  697. int
  698. node_is_me(const node_t *node)
  699. {
  700. return router_digest_is_me(node->identity);
  701. }
  702. /** Return <b>node</b> declared family (as a list of names), or NULL if
  703. * the node didn't declare a family. */
  704. const smartlist_t *
  705. node_get_declared_family(const node_t *node)
  706. {
  707. if (node->ri && node->ri->declared_family)
  708. return node->ri->declared_family;
  709. else if (node->md && node->md->family)
  710. return node->md->family;
  711. else
  712. return NULL;
  713. }
  714. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  715. * <b>node</b>, else 0.
  716. *
  717. * We prefer the IPv6 address if the router has an IPv6 address and
  718. * i) the node_t says that we do prefer IPv6
  719. * or
  720. * ii) the router has no IPv4 address. */
  721. int
  722. node_ipv6_preferred(const node_t *node)
  723. {
  724. node_assert_ok(node);
  725. if (node->ri)
  726. return (!tor_addr_is_null(&node->ri->ipv6_addr)
  727. && (node->ipv6_preferred || node->ri->addr == 0));
  728. if (node->rs)
  729. return (!tor_addr_is_null(&node->rs->ipv6_addr)
  730. && (node->ipv6_preferred || node->rs->addr == 0));
  731. return 0;
  732. }
  733. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  734. * <b>node</b> into *<b>ap_out</b>. */
  735. void
  736. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  737. {
  738. node_assert_ok(node);
  739. tor_assert(ap_out);
  740. if (node->ri) {
  741. tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
  742. ap_out->port = node->ri->or_port;
  743. } else if (node->rs) {
  744. tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
  745. ap_out->port = node->rs->or_port;
  746. }
  747. }
  748. /** Copy the preferred OR port (IP address and TCP port) for
  749. * <b>node</b> into *<b>ap_out</b>. */
  750. void
  751. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  752. {
  753. tor_assert(ap_out);
  754. /* Cheap implementation of config option ClientUseIPv6 -- simply
  755. don't prefer IPv6 when ClientUseIPv6 is not set. (See #4455 for
  756. more on this subject.) Note that this filter is too strict since
  757. we're hindering not only clients! Erring on the safe side
  758. shouldn't be a problem though. XXX move this check to where
  759. outgoing connections are made? -LN */
  760. if (get_options()->ClientUseIPv6 == 1 && node_ipv6_preferred(node))
  761. node_get_pref_ipv6_orport(node, ap_out);
  762. else
  763. node_get_prim_orport(node, ap_out);
  764. }
  765. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  766. * <b>node</b> into *<b>ap_out</b>. */
  767. void
  768. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  769. {
  770. node_assert_ok(node);
  771. tor_assert(ap_out);
  772. if (node->ri) {
  773. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  774. ap_out->port = node->ri->ipv6_orport;
  775. } else if (node->rs) {
  776. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  777. ap_out->port = node->rs->ipv6_orport;
  778. }
  779. }