nodelist.c 21 KB

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