describe.c 5.5 KB

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