describe.c 5.4 KB

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