1
0

SwitchPinger_admin.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "net/SwitchPinger.h"
  19. #include "net/SwitchPinger_admin.h"
  20. #include "util/Endian.h"
  21. #include "util/AddrTools.h"
  22. #include "crypto/Key.h"
  23. #include "util/Hex.h"
  24. #include "util/platform/Sockaddr.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. if (!Bits_isZero(&resp->lladdr, sizeof resp->lladdr)) {
  60. struct Sockaddr_storage ss;
  61. if (resp->lladdr.addr.udp4.type == Control_LlAddr_Udp4_TYPE) {
  62. Sockaddr_t* sa = Sockaddr_initFromBytes(
  63. &ss, resp->lladdr.addr.udp4.addr, Sockaddr_AF_INET);
  64. Sockaddr_setPort(sa, Endian_bigEndianToHost16(resp->lladdr.addr.udp4.port_be));
  65. Dict_putStringCC(
  66. rd, "lladdr", Sockaddr_print(sa, pingAlloc), pingAlloc);
  67. } else if (resp->lladdr.addr.udp4.type == Control_LlAddr_Udp6_TYPE) {
  68. Sockaddr_t* sa = Sockaddr_initFromBytes(
  69. &ss, resp->lladdr.addr.udp6.addr, Sockaddr_AF_INET6);
  70. Sockaddr_setPort(sa, Endian_bigEndianToHost16(resp->lladdr.addr.udp6.port_be));
  71. Dict_putStringCC(
  72. rd, "lladdr", Sockaddr_print(sa, pingAlloc), pingAlloc);
  73. } else {
  74. Dict_putStringCC(
  75. rd,
  76. "lladdrUnknown",
  77. Hex_print(&resp->lladdr, sizeof resp->lladdr, pingAlloc),
  78. pingAlloc
  79. );
  80. }
  81. }
  82. Admin_sendMessage(rd, ping->txid, ping->context->admin);
  83. }
  84. static void adminPing(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  85. {
  86. struct Context* context = vcontext;
  87. String* pathStr = Dict_getStringC(args, "path");
  88. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  89. String* data = Dict_getStringC(args, "data");
  90. int64_t* keyPing = Dict_getIntC(args, "keyPing");
  91. int64_t* lladdr = Dict_getIntC(args, "lladdr");
  92. uint32_t timeout = (timeoutPtr) ? *timeoutPtr : DEFAULT_TIMEOUT;
  93. uint64_t path;
  94. char* err = NULL;
  95. if (keyPing && *keyPing && lladdr && *lladdr) {
  96. err = "Cannot be both keyping and lladdr";
  97. } else if (pathStr->len != 19 || AddrTools_parsePath(&path, (uint8_t*) pathStr->bytes)) {
  98. err = "path was not parsable.";
  99. } else {
  100. struct SwitchPinger_Ping* ping = SwitchPinger_newPing(path,
  101. data,
  102. timeout,
  103. adminPingOnResponse,
  104. context->alloc,
  105. context->switchPinger);
  106. if (!ping) {
  107. err = "no open slots to store ping, try later.";
  108. } else {
  109. if (keyPing && *keyPing) {
  110. ping->type = SwitchPinger_Type_KEYPING;
  111. } else if (lladdr && *lladdr) {
  112. ping->type = SwitchPinger_Type_LLADDR;
  113. }
  114. ping->onResponseContext = Allocator_clone(ping->pingAlloc, (&(struct Ping) {
  115. .context = context,
  116. .txid = String_clone(txid, ping->pingAlloc),
  117. .path = String_clone(pathStr, ping->pingAlloc)
  118. }));
  119. }
  120. }
  121. if (err) {
  122. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  123. Admin_sendMessage(&d, txid, context->admin);
  124. }
  125. }
  126. void SwitchPinger_admin_register(struct SwitchPinger* sp,
  127. struct Admin* admin,
  128. struct Allocator* allocator)
  129. {
  130. struct Allocator* alloc = Allocator_child(allocator);
  131. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  132. .switchPinger = sp,
  133. .alloc = alloc,
  134. .admin = admin
  135. }));
  136. Admin_registerFunction("SwitchPinger_ping", adminPing, ctx, true,
  137. ((struct Admin_FunctionArg[]) {
  138. { .name = "path", .required = 1, .type = "String" },
  139. { .name = "timeout", .required = 0, .type = "Int" },
  140. { .name = "data", .required = 0, .type = "String" },
  141. { .name = "keyPing", .required = 0, .type = "Int" },
  142. { .name = "lladdr", .required = 0, .type = "Int" },
  143. }), admin);
  144. }