1
0

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. #include "util/AddrTools.h"
  23. #define DEFAULT_TIMEOUT 2000
  24. struct Context
  25. {
  26. struct SwitchPinger* switchPinger;
  27. struct Admin* admin;
  28. struct Allocator* alloc;
  29. };
  30. struct Ping
  31. {
  32. struct Context* context;
  33. String* txid;
  34. String* path;
  35. };
  36. static void adminPingOnResponse(enum SwitchPinger_Result result,
  37. uint64_t label,
  38. String* data,
  39. uint32_t millisecondsLag,
  40. uint32_t version,
  41. void* vping)
  42. {
  43. struct Ping* ping = vping;
  44. String* resultStr = SwitchPinger_resultString(result);
  45. uint8_t path[20];
  46. AddrTools_printPath(path, label);
  47. String* pathStr = &(String) { .bytes = (char*) path, .len = 19 };
  48. Dict response = Dict_CONST(
  49. String_CONST("version"), Int_OBJ(version), NULL
  50. );
  51. Dict d = Dict_CONST(String_CONST("rpath"), String_OBJ(pathStr), response);
  52. if (result == SwitchPinger_Result_LABEL_MISMATCH) {
  53. response = d;
  54. }
  55. response = Dict_CONST(String_CONST("result"), String_OBJ(resultStr), response);
  56. response = Dict_CONST(String_CONST("path"), String_OBJ(ping->path), response);
  57. response = Dict_CONST(String_CONST("ms"), Int_OBJ(millisecondsLag), response);
  58. d = Dict_CONST(String_CONST("data"), String_OBJ(data), response);
  59. if (data) {
  60. response = d;
  61. }
  62. Admin_sendMessage(&response, ping->txid, ping->context->admin);
  63. }
  64. static void adminPing(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  65. {
  66. struct Context* context = vcontext;
  67. String* pathStr = Dict_getString(args, String_CONST("path"));
  68. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  69. String* data = Dict_getString(args, String_CONST("data"));
  70. uint32_t timeout = (timeoutPtr) ? *timeoutPtr : DEFAULT_TIMEOUT;
  71. uint64_t path;
  72. String* err = NULL;
  73. if (pathStr->len != 19 || AddrTools_parsePath(&path, (uint8_t*) pathStr->bytes)) {
  74. err = String_CONST("path was not parsable.");
  75. } else {
  76. struct SwitchPinger_Ping* ping = SwitchPinger_newPing(path,
  77. data,
  78. timeout,
  79. adminPingOnResponse,
  80. context->alloc,
  81. context->switchPinger);
  82. if (!ping) {
  83. err = String_CONST("no open slots to store ping, try later.");
  84. } else {
  85. ping->onResponseContext = Allocator_clone(ping->pingAlloc, (&(struct Ping) {
  86. .context = context,
  87. .txid = String_clone(txid, ping->pingAlloc),
  88. .path = String_clone(pathStr, ping->pingAlloc)
  89. }));
  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. }