1
0

ReachabilityCollector_admin.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static List* numList(void* buf, uint32_t numberSz, struct Allocator* alloc)
  29. {
  30. List* l = List_new(alloc);
  31. for (int i = 0; i < LinkState_SLOTS; i++) {
  32. int64_t num;
  33. if (numberSz == 2) {
  34. num = ((uint16_t*)buf)[i];
  35. } else if (numberSz == 4) {
  36. num = ((uint32_t*)buf)[i];
  37. } else {
  38. Assert_failure("unexpected number size");
  39. }
  40. List_addInt(l, num, alloc);
  41. }
  42. return l;
  43. }
  44. #define NODES_PER_PAGE 8
  45. static void getPeerInfo(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  46. {
  47. struct Context* ctx = Identity_check((struct Context*) vcontext);
  48. int page = 0;
  49. int64_t* pageP = Dict_getIntC(args, "page");
  50. if (pageP && *pageP > 0) { page = *pageP; }
  51. List* peerList = List_new(requestAlloc);
  52. for (int i = page * NODES_PER_PAGE, j = 0; j < NODES_PER_PAGE; i++, j++) {
  53. struct ReachabilityCollector_PeerInfo* pi = ReachabilityCollector_getPeerInfo(ctx->rc, i);
  54. if (!pi) { break; }
  55. Dict* pid = Dict_new(requestAlloc);
  56. Dict_putStringC(pid, "addr", Address_toString(&pi->addr, requestAlloc), requestAlloc);
  57. uint8_t rpath[20];
  58. AddrTools_printPath(rpath, pi->pathThemToUs);
  59. Dict_putStringC(
  60. pid, "pathThemToUs", String_newBinary(rpath, 19, requestAlloc), requestAlloc);
  61. Dict_putIntC(pid, "querying", pi->querying, requestAlloc);
  62. Dict_putListC(
  63. pid, "lagSlots", numList(pi->linkState.lagSlots, 2, requestAlloc), requestAlloc);
  64. Dict_putListC(
  65. pid, "dropSlots", numList(pi->linkState.dropSlots, 2, requestAlloc), requestAlloc);
  66. Dict_putListC(
  67. pid, "kbRecvSlots", numList(pi->linkState.kbRecvSlots, 4, requestAlloc), requestAlloc);
  68. Dict_putIntC(
  69. pid, "samples", pi->linkState.samples, requestAlloc);
  70. List_addDict(peerList, pid, requestAlloc);
  71. }
  72. Dict* out = Dict_new(requestAlloc);
  73. Dict_putListC(out, "peers", peerList, requestAlloc);
  74. Dict_putStringCC(out, "error", "none", requestAlloc);
  75. Admin_sendMessage(out, txid, ctx->admin);
  76. }
  77. void ReachabilityCollector_admin_register(struct ReachabilityCollector* rc,
  78. struct Admin* admin,
  79. struct Allocator* alloc)
  80. {
  81. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  82. .admin = admin,
  83. .alloc = alloc,
  84. .rc = rc
  85. }));
  86. Identity_set(ctx);
  87. Admin_registerFunction("ReachabilityCollector_getPeerInfo", getPeerInfo, ctx, true,
  88. ((struct Admin_FunctionArg[]) {
  89. { .name = "page", .required = true, .type = "Int" }
  90. }), admin);
  91. }