routerinfo.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "core/or/or.h"
  7. #include "feature/nodelist/nodelist.h"
  8. #include "feature/nodelist/routerinfo.h"
  9. #include "feature/nodelist/node_st.h"
  10. #include "feature/nodelist/routerinfo_st.h"
  11. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  12. * <b>router</b> into *<b>ap_out</b>. */
  13. void
  14. router_get_prim_orport(const routerinfo_t *router, tor_addr_port_t *ap_out)
  15. {
  16. tor_assert(ap_out != NULL);
  17. tor_addr_from_ipv4h(&ap_out->addr, router->addr);
  18. ap_out->port = router->or_port;
  19. }
  20. int
  21. router_has_orport(const routerinfo_t *router, const tor_addr_port_t *orport)
  22. {
  23. return
  24. (tor_addr_eq_ipv4h(&orport->addr, router->addr) &&
  25. orport->port == router->or_port) ||
  26. (tor_addr_eq(&orport->addr, &router->ipv6_addr) &&
  27. orport->port == router->ipv6_orport);
  28. }
  29. /** Return a smartlist of tor_addr_port_t's with all the OR ports of
  30. <b>ri</b>. Note that freeing of the items in the list as well as
  31. the smartlist itself is the callers responsibility. */
  32. smartlist_t *
  33. router_get_all_orports(const routerinfo_t *ri)
  34. {
  35. tor_assert(ri);
  36. node_t fake_node;
  37. memset(&fake_node, 0, sizeof(fake_node));
  38. /* we don't modify ri, fake_node is passed as a const node_t *
  39. */
  40. fake_node.ri = (routerinfo_t *)ri;
  41. return node_get_all_orports(&fake_node);
  42. }
  43. /** Given a router purpose, convert it to a string. Don't call this on
  44. * ROUTER_PURPOSE_UNKNOWN: The whole point of that value is that we don't
  45. * know its string representation. */
  46. const char *
  47. router_purpose_to_string(uint8_t p)
  48. {
  49. switch (p)
  50. {
  51. case ROUTER_PURPOSE_GENERAL: return "general";
  52. case ROUTER_PURPOSE_BRIDGE: return "bridge";
  53. case ROUTER_PURPOSE_CONTROLLER: return "controller";
  54. default:
  55. tor_assert(0);
  56. }
  57. return NULL;
  58. }
  59. /** Given a string, convert it to a router purpose. */
  60. uint8_t
  61. router_purpose_from_string(const char *s)
  62. {
  63. if (!strcmp(s, "general"))
  64. return ROUTER_PURPOSE_GENERAL;
  65. else if (!strcmp(s, "bridge"))
  66. return ROUTER_PURPOSE_BRIDGE;
  67. else if (!strcmp(s, "controller"))
  68. return ROUTER_PURPOSE_CONTROLLER;
  69. else
  70. return ROUTER_PURPOSE_UNKNOWN;
  71. }