nodelist.c 25 KB

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