SearchRunner_admin.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "util/AddrTools.h"
  24. struct Context {
  25. struct Admin* admin;
  26. struct Allocator* allocator;
  27. struct SearchRunner* runner;
  28. Identity
  29. };
  30. static void showActiveSearch(Dict* args, void* vctx, String* txid, struct Allocator* alloc)
  31. {
  32. struct Context* ctx = Identity_check((struct Context*) vctx);
  33. int number = *(Dict_getInt(args, String_CONST("number")));
  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. }
  54. void SearchRunner_admin_register(struct SearchRunner* runner,
  55. struct Admin* admin,
  56. struct Allocator* alloc)
  57. {
  58. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  59. .admin = admin,
  60. .allocator = alloc,
  61. .runner = runner
  62. }));
  63. Identity_set(ctx);
  64. Admin_registerFunction("SearchRunner_showActiveSearch", showActiveSearch, ctx, true,
  65. ((struct Admin_FunctionArg[]) {
  66. { .name = "number", .required = 1, .type = "Int" }
  67. }), admin);
  68. }