1
0

SwitchPinger_admin.c 4.6 KB

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