describe.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "core/or/or.h"
  7. #include "feature/nodelist/describe.h"
  8. #include "feature/relay/router.h"
  9. #include "core/or/extend_info_st.h"
  10. #include "feature/nodelist/node_st.h"
  11. #include "feature/nodelist/routerinfo_st.h"
  12. #include "feature/nodelist/routerstatus_st.h"
  13. /**
  14. * Longest allowed output of format_node_description, plus 1 character for
  15. * NUL. This allows space for:
  16. * "$FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF~xxxxxxxxxxxxxxxxxxx at"
  17. * " [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]"
  18. * plus a terminating NUL.
  19. */
  20. #define NODE_DESC_BUF_LEN (MAX_VERBOSE_NICKNAME_LEN+4+TOR_ADDR_BUF_LEN)
  21. /** Use <b>buf</b> (which must be at least NODE_DESC_BUF_LEN bytes long) to
  22. * hold a human-readable description of a node with identity digest
  23. * <b>id_digest</b>, named-status <b>is_named</b>, nickname <b>nickname</b>,
  24. * and address <b>addr</b> or <b>addr32h</b>.
  25. *
  26. * The <b>nickname</b> and <b>addr</b> fields are optional and may be set to
  27. * NULL. The <b>addr32h</b> field is optional and may be set to 0.
  28. *
  29. * Return a pointer to the front of <b>buf</b>.
  30. */
  31. static const char *
  32. format_node_description(char *buf,
  33. const char *id_digest,
  34. int is_named,
  35. const char *nickname,
  36. const tor_addr_t *addr,
  37. uint32_t addr32h)
  38. {
  39. char *cp;
  40. if (!buf)
  41. return "<NULL BUFFER>";
  42. buf[0] = '$';
  43. base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
  44. cp = buf+1+HEX_DIGEST_LEN;
  45. if (nickname) {
  46. buf[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  47. strlcpy(buf+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  48. cp += strlen(cp);
  49. }
  50. if (addr32h || addr) {
  51. memcpy(cp, " at ", 4);
  52. cp += 4;
  53. if (addr) {
  54. tor_addr_to_str(cp, addr, TOR_ADDR_BUF_LEN, 0);
  55. } else {
  56. struct in_addr in;
  57. in.s_addr = htonl(addr32h);
  58. tor_inet_ntoa(&in, cp, INET_NTOA_BUF_LEN);
  59. }
  60. }
  61. return buf;
  62. }
  63. /** Return a human-readable description of the routerinfo_t <b>ri</b>.
  64. *
  65. * This function is not thread-safe. Each call to this function invalidates
  66. * previous values returned by this function.
  67. */
  68. const char *
  69. router_describe(const routerinfo_t *ri)
  70. {
  71. static char buf[NODE_DESC_BUF_LEN];
  72. if (!ri)
  73. return "<null>";
  74. return format_node_description(buf,
  75. ri->cache_info.identity_digest,
  76. 0,
  77. ri->nickname,
  78. NULL,
  79. ri->addr);
  80. }
  81. /** Return a human-readable description of the node_t <b>node</b>.
  82. *
  83. * This function is not thread-safe. Each call to this function invalidates
  84. * previous values returned by this function.
  85. */
  86. const char *
  87. node_describe(const node_t *node)
  88. {
  89. static char buf[NODE_DESC_BUF_LEN];
  90. const char *nickname = NULL;
  91. uint32_t addr32h = 0;
  92. int is_named = 0;
  93. if (!node)
  94. return "<null>";
  95. if (node->rs) {
  96. nickname = node->rs->nickname;
  97. is_named = node->rs->is_named;
  98. addr32h = node->rs->addr;
  99. } else if (node->ri) {
  100. nickname = node->ri->nickname;
  101. addr32h = node->ri->addr;
  102. }
  103. return format_node_description(buf,
  104. node->identity,
  105. is_named,
  106. nickname,
  107. NULL,
  108. addr32h);
  109. }
  110. /** Return a human-readable description of the routerstatus_t <b>rs</b>.
  111. *
  112. * This function is not thread-safe. Each call to this function invalidates
  113. * previous values returned by this function.
  114. */
  115. const char *
  116. routerstatus_describe(const routerstatus_t *rs)
  117. {
  118. static char buf[NODE_DESC_BUF_LEN];
  119. if (!rs)
  120. return "<null>";
  121. return format_node_description(buf,
  122. rs->identity_digest,
  123. rs->is_named,
  124. rs->nickname,
  125. NULL,
  126. rs->addr);
  127. }
  128. /** Return a human-readable description of the extend_info_t <b>ei</b>.
  129. *
  130. * This function is not thread-safe. Each call to this function invalidates
  131. * previous values returned by this function.
  132. */
  133. const char *
  134. extend_info_describe(const extend_info_t *ei)
  135. {
  136. static char buf[NODE_DESC_BUF_LEN];
  137. if (!ei)
  138. return "<null>";
  139. return format_node_description(buf,
  140. ei->identity_digest,
  141. 0,
  142. ei->nickname,
  143. &ei->addr,
  144. 0);
  145. }
  146. /** Set <b>buf</b> (which must have MAX_VERBOSE_NICKNAME_LEN+1 bytes) to the
  147. * verbose representation of the identity of <b>router</b>. The format is:
  148. * A dollar sign.
  149. * The upper-case hexadecimal encoding of the SHA1 hash of router's identity.
  150. * A "=" if the router is named (no longer implemented); a "~" if it is not.
  151. * The router's nickname.
  152. **/
  153. void
  154. router_get_verbose_nickname(char *buf, const routerinfo_t *router)
  155. {
  156. buf[0] = '$';
  157. base16_encode(buf+1, HEX_DIGEST_LEN+1, router->cache_info.identity_digest,
  158. DIGEST_LEN);
  159. buf[1+HEX_DIGEST_LEN] = '~';
  160. strlcpy(buf+1+HEX_DIGEST_LEN+1, router->nickname, MAX_NICKNAME_LEN+1);
  161. }