nodelist.c 24 KB

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