ReachabilityCollector_admin.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "util/AddrTools.h"
  20. #include "util/Identity.h"
  21. struct Context {
  22. struct Admin* admin;
  23. struct Allocator* alloc;
  24. struct ReachabilityCollector* rc;
  25. Identity
  26. };
  27. #define NODES_PER_PAGE 8
  28. static void getPeerInfo(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  29. {
  30. struct Context* ctx = Identity_check((struct Context*) vcontext);
  31. int page = 0;
  32. int64_t* pageP = Dict_getIntC(args, "page");
  33. if (pageP && *pageP > 0) { page = *pageP; }
  34. List* peerList = List_new(requestAlloc);
  35. for (int i = page * NODES_PER_PAGE, j = 0; j < NODES_PER_PAGE; i++, j++) {
  36. struct ReachabilityCollector_PeerInfo* pi = ReachabilityCollector_getPeerInfo(ctx->rc, i);
  37. if (!pi) { break; }
  38. Dict* pid = Dict_new(requestAlloc);
  39. Dict_putStringC(pid, "addr", Address_toString(&pi->addr, requestAlloc), requestAlloc);
  40. uint8_t rpath[20];
  41. AddrTools_printPath(rpath, pi->pathThemToUs);
  42. Dict_putStringC(
  43. pid, "pathThemToUs", String_newBinary(rpath, 19, requestAlloc), requestAlloc);
  44. Dict_putIntC(pid, "querying", pi->querying, requestAlloc);
  45. List_addDict(peerList, pid, requestAlloc);
  46. }
  47. Dict* out = Dict_new(requestAlloc);
  48. Dict_putListC(out, "peers", peerList, requestAlloc);
  49. Dict_putStringCC(out, "error", "none", requestAlloc);
  50. Admin_sendMessage(out, txid, ctx->admin);
  51. }
  52. void ReachabilityCollector_admin_register(struct ReachabilityCollector* rc,
  53. struct Admin* admin,
  54. struct Allocator* alloc)
  55. {
  56. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  57. .admin = admin,
  58. .alloc = alloc,
  59. .rc = rc
  60. }));
  61. Identity_set(ctx);
  62. Admin_registerFunction("ReachabilityCollector_getPeerInfo", getPeerInfo, ctx, true,
  63. ((struct Admin_FunctionArg[]) {
  64. { .name = "page", .required = true, .type = "Int" }
  65. }), admin);
  66. }