nodelist.c 23 KB

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