SessionManager_admin.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/List.h"
  19. #include "dht/Address.h"
  20. #include "net/SessionManager.h"
  21. #include "net/SessionManager_admin.h"
  22. #include "util/AddrTools.h"
  23. #include "util/Identity.h"
  24. struct Context
  25. {
  26. struct Allocator* alloc;
  27. struct SessionManager* sm;
  28. struct Admin* admin;
  29. Identity
  30. };
  31. #define ENTRIES_PER_PAGE 64
  32. static void getHandles(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  33. {
  34. struct Context* context = Identity_check((struct Context*) vcontext);
  35. struct Allocator* alloc = Allocator_child(context->alloc);
  36. int64_t* page = Dict_getIntC(args, "page");
  37. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  38. struct SessionManager_HandleList* hList = SessionManager_getHandleList(context->sm, alloc);
  39. List* list = List_new(alloc);
  40. for (int counter = 0; i < hList->length && counter++ < ENTRIES_PER_PAGE; i++) {
  41. List_addInt(list, hList->handles[i], alloc);
  42. }
  43. Dict* r = Dict_new(alloc);
  44. Dict_putListC(r, "handles", list, alloc);
  45. Dict_putIntC(r, "total", hList->length, alloc);
  46. if (i < hList->length) {
  47. Dict_putIntC(r, "more", 1, alloc);
  48. }
  49. Admin_sendMessage(r, txid, context->admin);
  50. Allocator_free(alloc);
  51. }
  52. static void outputSession(struct Context* context,
  53. struct SessionManager_Session* session,
  54. String* txid,
  55. struct Allocator* alloc)
  56. {
  57. Dict* r = Dict_new(alloc);
  58. if (!session) {
  59. Dict_putStringCC(r, "error", "no such session", alloc);
  60. Admin_sendMessage(r, txid, context->admin);
  61. return;
  62. }
  63. struct Address addr = {0};
  64. Ca_getHerPubKey(session->caSession, addr.key);
  65. Address_getPrefix(&addr);
  66. uint8_t printedAddr[40];
  67. AddrTools_printIp(printedAddr, addr.ip6.bytes);
  68. Dict_putStringC(r, "ip6", String_new(printedAddr, alloc), alloc);
  69. String* state =
  70. String_new(Ca_stateString(Ca_getState(session->caSession)), alloc);
  71. Dict_putStringC(r, "state", state, alloc);
  72. RTypes_CryptoStats_t stats;
  73. Ca_stats(session->caSession, &stats);
  74. Dict_putIntC(r, "duplicates", stats.duplicate_packets, alloc);
  75. Dict_putIntC(r, "lostPackets", stats.lost_packets, alloc);
  76. Dict_putIntC(r, "receivedOutOfRange", stats.received_unexpected, alloc);
  77. Dict_putIntC(r, "noiseProto", stats.noise_proto, alloc);
  78. addr.path = session->paths[0].label;
  79. addr.protocolVersion = session->version;
  80. Dict_putStringC(r, "addr", Address_toStringKey(&addr, alloc), alloc);
  81. Dict_putIntC(r, "handle", session->receiveHandle, alloc);
  82. Dict_putIntC(r, "sendHandle", session->sendHandle, alloc);
  83. Dict_putIntC(r, "metric", SessionManager_effectiveMetric(session), alloc);
  84. Dict_putIntC(r, "timeOfLastUsage", session->timeOfLastUsage, alloc);
  85. Admin_sendMessage(r, txid, context->admin);
  86. return;
  87. }
  88. static void sessionStats(Dict* args,
  89. void* vcontext,
  90. String* txid,
  91. struct Allocator* alloc)
  92. {
  93. struct Context* context = Identity_check((struct Context*) vcontext);
  94. int64_t* handleP = Dict_getIntC(args, "handle");
  95. uint32_t handle = *handleP;
  96. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  97. outputSession(context, session, txid, alloc);
  98. }
  99. static struct SessionManager_Session* sessionForIP(Dict* args,
  100. struct Context* context,
  101. String* txid,
  102. struct Allocator* alloc)
  103. {
  104. String* ip6Str = Dict_getStringC(args, "ip6");
  105. uint8_t ip6Binary[16] = {0};
  106. Dict* r = Dict_new(alloc);
  107. if (AddrTools_parseIp(ip6Binary, ip6Str->bytes)) {
  108. Dict_putStringCC(r, "error", "malformed_ip", alloc);
  109. Admin_sendMessage(r, txid, context->admin);
  110. return NULL;
  111. }
  112. struct SessionManager_Session* session = SessionManager_sessionForIp6(ip6Binary, context->sm);
  113. if (!session) {
  114. Dict_putStringCC(r, "error", "no such session", alloc);
  115. Admin_sendMessage(r, txid, context->admin);
  116. return NULL;
  117. }
  118. return session;
  119. }
  120. static void sessionStatsByIP(Dict* args,
  121. void* vcontext,
  122. String* txid,
  123. struct Allocator* alloc)
  124. {
  125. struct Context* context = Identity_check((struct Context*) vcontext);
  126. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  127. if (!session) { return; }
  128. outputSession(context, session, txid, alloc);
  129. }
  130. static void resetCA(Dict* args,
  131. void* vcontext,
  132. String* txid,
  133. struct Allocator* alloc)
  134. {
  135. struct Context* context = Identity_check((struct Context*) vcontext);
  136. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  137. if (!session) { return; }
  138. Ca_reset(session->caSession);
  139. Dict* r = Dict_new(alloc);
  140. Dict_putStringCC(r, "error", "none", alloc);
  141. Admin_sendMessage(r, txid, context->admin);
  142. }
  143. void SessionManager_admin_register(struct SessionManager* sm,
  144. struct Admin* admin,
  145. struct Allocator* alloc)
  146. {
  147. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  148. .alloc = alloc,
  149. .sm = sm,
  150. .admin = admin
  151. }));
  152. Identity_set(ctx);
  153. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  154. ((struct Admin_FunctionArg[]) {
  155. { .name = "page", .required = 0, .type = "Int" }
  156. }), admin);
  157. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  158. ((struct Admin_FunctionArg[]) {
  159. { .name = "handle", .required = 1, .type = "Int" }
  160. }), admin);
  161. Admin_registerFunction("SessionManager_sessionStatsByIP", sessionStatsByIP, ctx, true,
  162. ((struct Admin_FunctionArg[]) {
  163. { .name = "ip6", .required = 1, .type = "String" }
  164. }), admin);
  165. Admin_registerFunction("SessionManager_resetCA", resetCA, ctx, true,
  166. ((struct Admin_FunctionArg[]) {
  167. { .name = "ip6", .required = 1, .type = "String" }
  168. }), admin);
  169. }