SessionManager_admin.c 7.0 KB

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