SwitchPinger_admin.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #define DEFAULT_TIMEOUT 2000
  23. struct Context
  24. {
  25. struct SwitchPinger* switchPinger;
  26. struct Admin* admin;
  27. struct Allocator* alloc;
  28. };
  29. struct Ping
  30. {
  31. struct Context* context;
  32. String* txid;
  33. String* path;
  34. };
  35. static void adminPingOnResponse(enum SwitchPinger_Result result,
  36. uint64_t label,
  37. String* data,
  38. uint32_t millisecondsLag,
  39. uint32_t version,
  40. void* vping)
  41. {
  42. struct Ping* ping = vping;
  43. String* resultStr = SwitchPinger_resultString(result);
  44. uint8_t path[20];
  45. AddrTools_printPath(path, label);
  46. String* pathStr = &(String) { .bytes = (char*) path, .len = 19 };
  47. Dict response = Dict_CONST(
  48. String_CONST("version"), Int_OBJ(version), NULL
  49. );
  50. Dict d = Dict_CONST(String_CONST("rpath"), String_OBJ(pathStr), response);
  51. if (result == SwitchPinger_Result_LABEL_MISMATCH) {
  52. response = d;
  53. }
  54. response = Dict_CONST(String_CONST("result"), String_OBJ(resultStr), response);
  55. response = Dict_CONST(String_CONST("path"), String_OBJ(ping->path), response);
  56. response = Dict_CONST(String_CONST("ms"), Int_OBJ(millisecondsLag), response);
  57. d = Dict_CONST(String_CONST("data"), String_OBJ(data), response);
  58. if (data) {
  59. response = d;
  60. }
  61. Admin_sendMessage(&response, ping->txid, ping->context->admin);
  62. }
  63. static void adminPing(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  64. {
  65. struct Context* context = vcontext;
  66. String* pathStr = Dict_getString(args, String_CONST("path"));
  67. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  68. String* data = Dict_getString(args, String_CONST("data"));
  69. uint32_t timeout = (timeoutPtr) ? *timeoutPtr : DEFAULT_TIMEOUT;
  70. uint64_t path;
  71. String* err = NULL;
  72. if (pathStr->len != 19 || AddrTools_parsePath(&path, (uint8_t*) pathStr->bytes)) {
  73. err = String_CONST("path was not parsable.");
  74. } else {
  75. struct SwitchPinger_Ping* ping = SwitchPinger_newPing(path,
  76. data,
  77. timeout,
  78. adminPingOnResponse,
  79. context->alloc,
  80. context->switchPinger);
  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. SwitchPinger_sendPing(ping);
  90. }
  91. }
  92. if (err) {
  93. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(err), NULL);
  94. Admin_sendMessage(&d, txid, context->admin);
  95. }
  96. }
  97. void SwitchPinger_admin_register(struct SwitchPinger* sp,
  98. struct Admin* admin,
  99. struct Allocator* alloc)
  100. {
  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. }), admin);
  112. }