fmt_serverstatus.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "core/or/or.h"
  6. #include "feature/control/fmt_serverstatus.h"
  7. #include "app/config/config.h"
  8. #include "feature/dirauth/voteflags.h"// XXXX remove
  9. #include "feature/nodelist/nodelist.h"
  10. #include "feature/relay/router.h"
  11. #include "feature/nodelist/node_st.h"
  12. #include "feature/nodelist/routerinfo_st.h"
  13. /**
  14. * Allocate and return a description of the status of the server <b>desc</b>,
  15. * for use in a v1-style router-status line. The server is listed
  16. * as running iff <b>is_live</b> is true.
  17. *
  18. * This is deprecated: it's only used for controllers that want outputs in
  19. * the old format.
  20. */
  21. static char *
  22. list_single_server_status(const routerinfo_t *desc, int is_live)
  23. {
  24. char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4]; /* !nickname=$hexdigest\0 */
  25. char *cp;
  26. const node_t *node;
  27. tor_assert(desc);
  28. cp = buf;
  29. if (!is_live) {
  30. *cp++ = '!';
  31. }
  32. node = node_get_by_id(desc->cache_info.identity_digest);
  33. if (node && node->is_valid) {
  34. strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
  35. cp += strlen(cp);
  36. *cp++ = '=';
  37. }
  38. *cp++ = '$';
  39. base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
  40. DIGEST_LEN);
  41. return tor_strdup(buf);
  42. }
  43. /** Based on the routerinfo_ts in <b>routers</b>, allocate the
  44. * contents of a v1-style router-status line, and store it in
  45. * *<b>router_status_out</b>. Return 0 on success, -1 on failure.
  46. *
  47. * If for_controller is true, include the routers with very old descriptors.
  48. *
  49. * This is deprecated: it's only used for controllers that want outputs in
  50. * the old format.
  51. */
  52. int
  53. list_server_status_v1(smartlist_t *routers, char **router_status_out,
  54. int for_controller)
  55. {
  56. /* List of entries in a router-status style: An optional !, then an optional
  57. * equals-suffixed nickname, then a dollar-prefixed hexdigest. */
  58. smartlist_t *rs_entries;
  59. time_t now = time(NULL);
  60. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  61. const or_options_t *options = get_options();
  62. /* We include v2 dir auths here too, because they need to answer
  63. * controllers. Eventually we'll deprecate this whole function;
  64. * see also networkstatus_getinfo_by_purpose(). */
  65. int authdir = authdir_mode_publishes_statuses(options);
  66. tor_assert(router_status_out);
  67. rs_entries = smartlist_new();
  68. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  69. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  70. tor_assert(node);
  71. if (authdir) {
  72. /* Update router status in routerinfo_t. */
  73. dirserv_set_router_is_running(ri, now);
  74. }
  75. if (for_controller) {
  76. char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
  77. char *cp = name_buf;
  78. if (!node->is_running)
  79. *cp++ = '!';
  80. router_get_verbose_nickname(cp, ri);
  81. smartlist_add_strdup(rs_entries, name_buf);
  82. } else if (ri->cache_info.published_on >= cutoff) {
  83. smartlist_add(rs_entries, list_single_server_status(ri,
  84. node->is_running));
  85. }
  86. } SMARTLIST_FOREACH_END(ri);
  87. *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
  88. SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
  89. smartlist_free(rs_entries);
  90. return 0;
  91. }