SearchRunner_admin.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/Dict.h"
  17. #include "benc/String.h"
  18. #include "dht/Address.h"
  19. #include "dht/dhtcore/SearchRunner_admin.h"
  20. #include "dht/dhtcore/SearchRunner.h"
  21. #include "dht/Address.h"
  22. #include "memory/Allocator.h"
  23. struct Context {
  24. struct Admin* admin;
  25. struct Allocator* allocator;
  26. struct SearchRunner* runner;
  27. Identity
  28. };
  29. static void showActiveSearch(Dict* args, void* vctx, String* txid)
  30. {
  31. struct Context* ctx = Identity_cast((struct Context*) vctx);
  32. int number = *(Dict_getInt(args, String_CONST("number")));
  33. struct Allocator* alloc = Allocator_child(ctx->allocator);
  34. struct SearchRunner_SearchData* search =
  35. SearchRunner_showActiveSearch(ctx->runner, number, alloc);
  36. Dict* dict = Dict_new(alloc);
  37. // Nothing is an error
  38. Dict_putString(dict, String_new("error", alloc), String_new("none", alloc), alloc);
  39. if (number < search->activeSearches) {
  40. uint8_t target[40];
  41. AddrTools_printIp(target, search->target);
  42. Dict_putString(dict, String_new("target", alloc), String_new((char*)target, alloc), alloc);
  43. uint8_t lastNodeAsked[60];
  44. Address_print(lastNodeAsked, &search->lastNodeAsked);
  45. Dict_putString(dict,
  46. String_new("lastNodeAsked", alloc),
  47. String_new((char*)lastNodeAsked, alloc),
  48. alloc);
  49. Dict_putInt(dict, String_new("totalRequests", alloc), search->totalRequests, alloc);
  50. }
  51. Dict_putInt(dict, String_new("activeSearches", alloc), search->activeSearches, alloc);
  52. Admin_sendMessage(dict, txid, ctx->admin);
  53. Allocator_free(alloc);
  54. }
  55. void SearchRunner_admin_register(struct SearchRunner* runner,
  56. struct Admin* admin,
  57. struct Allocator* alloc)
  58. {
  59. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  60. .admin = admin,
  61. .allocator = alloc,
  62. .runner = runner
  63. }));
  64. Identity_set(ctx);
  65. Admin_registerFunction("SearchRunner_showActiveSearch", showActiveSearch, ctx, true,
  66. ((struct Admin_FunctionArg[]) {
  67. { .name = "number", .required = 1, .type = "Int" }
  68. }), admin);
  69. }