SessionManager_admin.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "interface/SessionManager.h"
  23. #include "interface/SessionManager_admin.h"
  24. #include "util/AddrTools.h"
  25. struct Context
  26. {
  27. struct Allocator* alloc;
  28. struct SessionManager* sm;
  29. struct Admin* admin;
  30. };
  31. // typical peer record is around 140 benc chars, so can't have very many in 1023
  32. #define ENTRIES_PER_PAGE 64
  33. static void getHandles(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  34. {
  35. struct Context* context = vcontext;
  36. struct Allocator* alloc = Allocator_child(context->alloc);
  37. int64_t* page = Dict_getInt(args, String_CONST("page"));
  38. uint32_t i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  39. struct SessionManager_HandleList* hList = SessionManager_getHandleList(context->sm, alloc);
  40. List* list = List_new(alloc);
  41. for (int counter=0; i < hList->count && counter++ < ENTRIES_PER_PAGE; i++) {
  42. List_addInt(list, hList->handles[i], alloc);
  43. }
  44. Dict* r = Dict_new(alloc);
  45. Dict_putList(r, String_CONST("handles"), list, alloc);
  46. Dict_putInt(r, String_CONST("total"), hList->count, alloc);
  47. String* more = String_CONST("more");
  48. if (i < hList->count) {
  49. Dict_putInt(r, more, 1, alloc);
  50. }
  51. Admin_sendMessage(r, txid, context->admin);
  52. Allocator_free(alloc);
  53. }
  54. static void sessionStats(Dict* args,
  55. void* vcontext,
  56. String* txid,
  57. struct Allocator* alloc)
  58. {
  59. struct Context* context = vcontext;
  60. int64_t* handleP = Dict_getInt(args, String_CONST("handle"));
  61. uint32_t handle = *handleP;
  62. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  63. uint8_t* ip6 = SessionManager_getIp6(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. // both or neither
  71. Assert_true(ip6);
  72. uint8_t printedAddr[40];
  73. AddrTools_printIp(printedAddr, ip6);
  74. Dict_putString(r, String_CONST("ip6"), String_new(printedAddr, alloc), alloc);
  75. Dict_putString(r,
  76. String_CONST("state"),
  77. String_new(CryptoAuth_stateString(session->cryptoAuthState), alloc),
  78. alloc);
  79. struct ReplayProtector* rp = CryptoAuth_getReplayProtector(session->internal);
  80. Dict_putInt(r, String_CONST("duplicates"), rp->duplicates, alloc);
  81. Dict_putInt(r, String_CONST("lostPackets"), rp->lostPackets, alloc);
  82. Dict_putInt(r, String_CONST("receivedOutOfRange"), rp->receivedOutOfRange, alloc);
  83. struct Address addr;
  84. uint8_t* key = CryptoAuth_getHerPublicKey(session->internal);
  85. Bits_memcpyConst(addr.key, key, 32);
  86. addr.path = session->knownSwitchLabel;
  87. addr.protocolVersion = session->version;
  88. Dict_putString(r, String_CONST("addr"), Address_toString(&addr, alloc), alloc);
  89. Dict_putString(r, String_CONST("publicKey"), Key_stringify(key, alloc), alloc);
  90. Dict_putInt(r, String_CONST("version"), session->version, alloc);
  91. Dict_putInt(r, String_CONST("handle"),
  92. Endian_bigEndianToHost32(session->receiveHandle_be), alloc);
  93. Dict_putInt(r, String_CONST("sendHandle"),
  94. Endian_bigEndianToHost32(session->sendHandle_be), alloc);
  95. Dict_putInt(r, String_CONST("timeOfLastIn"), session->timeOfLastIn, alloc);
  96. Dict_putInt(r, String_CONST("timeOfLastOut"), session->timeOfLastOut, alloc);
  97. Dict_putString(r, String_CONST("deprecation"),
  98. String_CONST("publicKey,version will soon be removed"), alloc);
  99. Admin_sendMessage(r, txid, context->admin);
  100. return;
  101. }
  102. void SessionManager_admin_register(struct SessionManager* sm,
  103. struct Admin* admin,
  104. struct Allocator* alloc)
  105. {
  106. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  107. .alloc = alloc,
  108. .sm = sm,
  109. .admin = admin
  110. }));
  111. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  112. ((struct Admin_FunctionArg[]) {
  113. { .name = "page", .required = 0, .type = "Int" }
  114. }), admin);
  115. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  116. ((struct Admin_FunctionArg[]) {
  117. { .name = "handle", .required = 1, .type = "Int" }
  118. }), admin);
  119. }