SessionManager_admin.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/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_getInt(args, String_CONST("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_putList(r, String_CONST("handles"), list, alloc);
  47. Dict_putInt(r, String_CONST("total"), hList->length, alloc);
  48. String* more = String_CONST("more");
  49. if (i < hList->length) {
  50. Dict_putInt(r, more, 1, alloc);
  51. }
  52. Admin_sendMessage(r, txid, context->admin);
  53. Allocator_free(alloc);
  54. }
  55. static void sessionStats(Dict* args,
  56. void* vcontext,
  57. String* txid,
  58. struct Allocator* alloc)
  59. {
  60. struct Context* context = Identity_check((struct Context*) vcontext);
  61. int64_t* handleP = Dict_getInt(args, String_CONST("handle"));
  62. uint32_t handle = *handleP;
  63. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  64. Dict* r = Dict_new(alloc);
  65. if (!session) {
  66. Dict_putString(r, String_CONST("error"), String_CONST("no such session"), alloc);
  67. Admin_sendMessage(r, txid, context->admin);
  68. return;
  69. }
  70. uint8_t printedAddr[40];
  71. AddrTools_printIp(printedAddr, session->caSession->herIp6);
  72. Dict_putString(r, String_CONST("ip6"), String_new(printedAddr, alloc), alloc);
  73. String* state =
  74. String_new(CryptoAuth_stateString(CryptoAuth_getState(session->caSession)), alloc);
  75. Dict_putString(r, String_CONST("state"), state, alloc);
  76. struct ReplayProtector* rp = &session->caSession->replayProtector;
  77. Dict_putInt(r, String_CONST("duplicates"), rp->duplicates, alloc);
  78. Dict_putInt(r, String_CONST("lostPackets"), rp->lostPackets, alloc);
  79. Dict_putInt(r, String_CONST("receivedOutOfRange"), rp->receivedOutOfRange, alloc);
  80. struct Address addr;
  81. Bits_memcpy(addr.key, session->caSession->herPublicKey, 32);
  82. addr.path = session->sendSwitchLabel;
  83. addr.protocolVersion = session->version;
  84. Dict_putString(r, String_CONST("addr"), Address_toString(&addr, alloc), alloc);
  85. Dict_putString(r, String_CONST("publicKey"),
  86. Key_stringify(session->caSession->herPublicKey, alloc), alloc);
  87. Dict_putInt(r, String_CONST("version"), session->version, alloc);
  88. Dict_putInt(r, String_CONST("handle"), session->receiveHandle, alloc);
  89. Dict_putInt(r, String_CONST("sendHandle"), session->sendHandle, alloc);
  90. Dict_putInt(r, String_CONST("timeOfLastIn"), session->timeOfLastIn, alloc);
  91. Dict_putInt(r, String_CONST("timeOfLastOut"), session->timeOfLastOut, alloc);
  92. Dict_putString(r, String_CONST("deprecation"),
  93. String_CONST("publicKey,version will soon be removed"), alloc);
  94. Admin_sendMessage(r, txid, context->admin);
  95. return;
  96. }
  97. void SessionManager_admin_register(struct SessionManager* sm,
  98. struct Admin* admin,
  99. struct Allocator* alloc)
  100. {
  101. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  102. .alloc = alloc,
  103. .sm = sm,
  104. .admin = admin
  105. }));
  106. Identity_set(ctx);
  107. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  108. ((struct Admin_FunctionArg[]) {
  109. { .name = "page", .required = 0, .type = "Int" }
  110. }), admin);
  111. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  112. ((struct Admin_FunctionArg[]) {
  113. { .name = "handle", .required = 1, .type = "Int" }
  114. }), admin);
  115. }