1
0

SwitchPinger_admin.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/String.h"
  17. #include "benc/Dict.h"
  18. #include "benc/Int.h"
  19. #include "dht/Address.h"
  20. #include "net/SwitchPinger.h"
  21. #include "net/SwitchPinger_admin.h"
  22. #include "util/Endian.h"
  23. #include "util/AddrTools.h"
  24. #include "crypto/Key.h"
  25. #define DEFAULT_TIMEOUT 2000
  26. struct Context
  27. {
  28. struct SwitchPinger* switchPinger;
  29. struct Admin* admin;
  30. struct Allocator* alloc;
  31. };
  32. struct Ping
  33. {
  34. struct Context* context;
  35. String* txid;
  36. String* path;
  37. };
  38. static void adminPingOnResponse(struct SwitchPinger_Response* resp, void* vping)
  39. {
  40. struct Allocator* pingAlloc = resp->ping->pingAlloc;
  41. struct Ping* ping = vping;
  42. Dict* rd = Dict_new(pingAlloc);
  43. if (resp->res == SwitchPinger_Result_LABEL_MISMATCH) {
  44. uint8_t path[20] = {0};
  45. AddrTools_printPath(path, resp->label);
  46. String* pathStr = String_new(path, pingAlloc);
  47. Dict_putStringC(rd, "rpath", pathStr, pingAlloc);
  48. }
  49. Dict_putIntC(rd, "version", resp->version, pingAlloc);
  50. Dict_putIntC(rd, "ms", resp->milliseconds, pingAlloc);
  51. Dict_putStringC(rd, "result", SwitchPinger_resultString(resp->res), pingAlloc);
  52. Dict_putStringC(rd, "path", ping->path, pingAlloc);
  53. if (resp->data) {
  54. Dict_putStringC(rd, "data", resp->data, pingAlloc);
  55. }
  56. if (!Bits_isZero(resp->key, 32)) {
  57. Dict_putStringC(rd, "key", Key_stringify(resp->key, pingAlloc), pingAlloc);
  58. }
  59. Admin_sendMessage(rd, ping->txid, ping->context->admin);
  60. }
  61. static void adminPing(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  62. {
  63. struct Context* context = vcontext;
  64. String* pathStr = Dict_getStringC(args, "path");
  65. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  66. String* data = Dict_getStringC(args, "data");
  67. int64_t* keyPing = Dict_getIntC(args, "keyPing");
  68. uint32_t timeout = (timeoutPtr) ? *timeoutPtr : DEFAULT_TIMEOUT;
  69. uint64_t path;
  70. String* err = NULL;
  71. if (pathStr->len != 19 || AddrTools_parsePath(&path, (uint8_t*) pathStr->bytes)) {
  72. err = String_CONST("path was not parsable.");
  73. } else {
  74. struct SwitchPinger_Ping* ping = SwitchPinger_newPing(path,
  75. data,
  76. timeout,
  77. adminPingOnResponse,
  78. context->alloc,
  79. context->switchPinger);
  80. if (keyPing && *keyPing) { ping->type = SwitchPinger_Type_KEYPING; }
  81. if (!ping) {
  82. err = String_CONST("no open slots to store ping, try later.");
  83. } else {
  84. ping->onResponseContext = Allocator_clone(ping->pingAlloc, (&(struct Ping) {
  85. .context = context,
  86. .txid = String_clone(txid, ping->pingAlloc),
  87. .path = String_clone(pathStr, ping->pingAlloc)
  88. }));
  89. }
  90. }
  91. if (err) {
  92. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(err), NULL);
  93. Admin_sendMessage(&d, txid, context->admin);
  94. }
  95. }
  96. void SwitchPinger_admin_register(struct SwitchPinger* sp,
  97. struct Admin* admin,
  98. struct Allocator* allocator)
  99. {
  100. struct Allocator* alloc = Allocator_child(allocator);
  101. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  102. .switchPinger = sp,
  103. .alloc = alloc,
  104. .admin = admin
  105. }));
  106. Admin_registerFunction("SwitchPinger_ping", adminPing, ctx, true,
  107. ((struct Admin_FunctionArg[]) {
  108. { .name = "path", .required = 1, .type = "String" },
  109. { .name = "timeout", .required = 0, .type = "Int" },
  110. { .name = "data", .required = 0, .type = "String" },
  111. { .name = "keyPing", .required = 0, .type = "Int" }
  112. }), admin);
  113. }