ReachabilityCollector_admin.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/List.h"
  17. #include "crypto/Key.h"
  18. #include "subnode/ReachabilityCollector.h"
  19. #include "subnode/ReachabilityCollector_admin.h"
  20. #include "util/AddrTools.h"
  21. #include "util/Identity.h"
  22. struct Context {
  23. struct Admin* admin;
  24. struct Allocator* alloc;
  25. struct ReachabilityCollector* rc;
  26. Identity
  27. };
  28. #define NODES_PER_PAGE 8
  29. static void getPeerInfo(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  30. {
  31. struct Context* ctx = Identity_check((struct Context*) vcontext);
  32. int page = 0;
  33. int64_t* pageP = Dict_getIntC(args, "page");
  34. if (pageP && *pageP > 0) { page = *pageP; }
  35. List* peerList = List_new(requestAlloc);
  36. for (int i = page * NODES_PER_PAGE, j = 0; j < NODES_PER_PAGE; i++, j++) {
  37. struct ReachabilityCollector_PeerInfo* pi = ReachabilityCollector_getPeerInfo(ctx->rc, i);
  38. if (!pi) { break; }
  39. Dict* pid = Dict_new(requestAlloc);
  40. Dict_putStringC(pid, "addr", Address_toString(&pi->addr, requestAlloc), requestAlloc);
  41. uint8_t rpath[20];
  42. AddrTools_printPath(rpath, pi->pathThemToUs);
  43. Dict_putStringC(
  44. pid, "pathThemToUs", String_newBinary(rpath, 19, requestAlloc), requestAlloc);
  45. Dict_putIntC(pid, "querying", pi->querying, requestAlloc);
  46. List_addDict(peerList, pid, requestAlloc);
  47. }
  48. Dict* out = Dict_new(requestAlloc);
  49. Dict_putListC(out, "peers", peerList, requestAlloc);
  50. Dict_putStringCC(out, "error", "none", requestAlloc);
  51. Admin_sendMessage(out, txid, ctx->admin);
  52. }
  53. void ReachabilityCollector_admin_register(struct ReachabilityCollector* rc,
  54. struct Admin* admin,
  55. struct Allocator* alloc)
  56. {
  57. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  58. .admin = admin,
  59. .alloc = alloc,
  60. .rc = rc
  61. }));
  62. Identity_set(ctx);
  63. Admin_registerFunction("ReachabilityCollector_getPeerInfo", getPeerInfo, ctx, true,
  64. ((struct Admin_FunctionArg[]) {
  65. { .name = "page", .required = true, .type = "Int" }
  66. }), admin);
  67. }