Janitor_admin.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "benc/List.h"
  16. #include "memory/Allocator.h"
  17. #include "admin/Admin.h"
  18. #include "dht/dhtcore/Janitor.h"
  19. #include "dht/dhtcore/Janitor_admin.h"
  20. struct Context {
  21. struct Admin* admin;
  22. struct Allocator* alloc;
  23. struct Janitor* janitor;
  24. Identity
  25. };
  26. static struct RumorMill* getRumorMill(struct Context* ctx, String* name)
  27. {
  28. if (String_equals(String_CONST("externalMill"), name)) {
  29. return ctx->janitor->externalMill;
  30. } else if (String_equals(String_CONST("linkMill"), name)) {
  31. return ctx->janitor->linkMill;
  32. } else if (String_equals(String_CONST("nodeMill"), name)) {
  33. return ctx->janitor->nodeMill;
  34. } else if (String_equals(String_CONST("dhtMill"), name)) {
  35. return ctx->janitor->dhtMill;
  36. } else if (String_equals(String_CONST("splitMill"), name)) {
  37. return ctx->janitor->splitMill;
  38. } else {
  39. return NULL;
  40. }
  41. }
  42. #define ENTRIES_PER_PAGE 4
  43. static void dumpRumorMill(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  44. {
  45. struct Context* ctx = Identity_check((struct Context*) vcontext);
  46. Dict* out = Dict_new(requestAlloc);
  47. struct RumorMill* rm = getRumorMill(ctx, Dict_getStringC(args, "mill"));
  48. if (!rm) {
  49. Dict_putStringC(out,
  50. "error",
  51. String_CONST("mill must be one of "
  52. "[externalMill,linkMill,nodeMill,dhtMill,splitMill]"),
  53. requestAlloc);
  54. Admin_sendMessage(out, txid, ctx->admin);
  55. return;
  56. }
  57. int64_t* page = Dict_getIntC(args, "page");
  58. int ctr = (page) ? *page * ENTRIES_PER_PAGE : 0;
  59. List* table = List_new(requestAlloc);
  60. for (int i = 0; i < ENTRIES_PER_PAGE && ctr < rm->count; i++) {
  61. String* addr = Address_toStringKey(&rm->addresses[ctr++], requestAlloc);
  62. List_addString(table, addr, requestAlloc);
  63. }
  64. Dict_putListC(out, "addresses", table, requestAlloc);
  65. Dict_putIntC(out, "total", rm->count, requestAlloc);
  66. Admin_sendMessage(out, txid, ctx->admin);
  67. }
  68. void Janitor_admin_register(struct Janitor* janitor, struct Admin* admin, struct Allocator* alloc)
  69. {
  70. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  71. .admin = admin,
  72. .alloc = alloc,
  73. .janitor = janitor
  74. }));
  75. Identity_set(ctx);
  76. Admin_registerFunction("Janitor_dumpRumorMill", dumpRumorMill, ctx, true,
  77. ((struct Admin_FunctionArg[]) {
  78. { .name = "page", .required = 1, .type = "Int" },
  79. { .name = "mill", .required = 1, .type = "String" },
  80. }), admin);
  81. }