InterfaceController_admin.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "benc/Int.h"
  20. #include "crypto/Key.h"
  21. #include "interface/InterfaceController.h"
  22. #include "interface/InterfaceController_admin.h"
  23. #include "util/AddrTools.h"
  24. struct Context
  25. {
  26. struct Allocator* alloc;
  27. struct InterfaceController* ic;
  28. struct Admin* admin;
  29. Identity
  30. };
  31. // typical peer record is around 140 benc chars, so can't have very many in 1023
  32. #define ENTRIES_PER_PAGE 6
  33. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  34. {
  35. struct Context* context = Identity_check((struct Context*)vcontext);
  36. struct InterfaceController_peerStats* stats = NULL;
  37. int64_t* page = Dict_getInt(args, String_CONST("page"));
  38. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  39. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  40. String* bytesIn = String_CONST("bytesIn");
  41. String* bytesOut = String_CONST("bytesOut");
  42. String* pubKey = String_CONST("publicKey");
  43. String* state = String_CONST("state");
  44. String* last = String_CONST("last");
  45. String* switchLabel = String_CONST("switchLabel");
  46. String* isIncoming = String_CONST("isIncoming");
  47. String* user = String_CONST("user");
  48. String* duplicates = String_CONST("duplicates");
  49. String* lostPackets = String_CONST("lostPackets");
  50. String* receivedOutOfRange = String_CONST("receivedOutOfRange");
  51. List* list = List_new(alloc);
  52. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  53. Dict* d = Dict_new(alloc);
  54. Dict_putInt(d, bytesIn, stats[i].bytesIn, alloc);
  55. Dict_putInt(d, bytesOut, stats[i].bytesOut, alloc);
  56. Dict_putString(d, pubKey, Key_stringify(stats[i].pubKey, alloc), alloc);
  57. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  58. Dict_putString(d, state, stateString, alloc);
  59. Dict_putInt(d, last, stats[i].timeOfLastMessage, alloc);
  60. uint8_t labelStack[20];
  61. AddrTools_printPath(labelStack, stats[i].switchLabel);
  62. Dict_putString(d, switchLabel, String_new((char*)labelStack, alloc), alloc);
  63. Dict_putInt(d, isIncoming, stats[i].isIncomingConnection, alloc);
  64. Dict_putInt(d, duplicates, stats[i].duplicates, alloc);
  65. Dict_putInt(d, lostPackets, stats[i].lostPackets, alloc);
  66. Dict_putInt(d, receivedOutOfRange, stats[i].receivedOutOfRange, alloc);
  67. if (stats[i].isIncomingConnection) {
  68. Dict_putString(d, user, stats[i].user, alloc);
  69. }
  70. List_addDict(list, d, alloc);
  71. }
  72. Dict* resp = Dict_new(alloc);
  73. Dict_putList(resp, String_CONST("peers"), list, alloc);
  74. Dict_putInt(resp, String_CONST("total"), count, alloc);
  75. if (i < count) {
  76. Dict_putInt(resp, String_CONST("more"), 1, alloc);
  77. }
  78. Admin_sendMessage(resp, txid, context->admin);
  79. }
  80. static void adminDisconnectPeer(Dict* args,
  81. void* vcontext,
  82. String* txid,
  83. struct Allocator* requestAlloc)
  84. {
  85. struct Context* context = Identity_check((struct Context*)vcontext);
  86. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  87. // parse the key
  88. uint8_t pubkey[32];
  89. uint8_t addr[16];
  90. int error = Key_parse(pubkeyString, pubkey, addr);
  91. char* errorMsg = NULL;
  92. if (error) {
  93. errorMsg = "bad key";
  94. } else {
  95. // try to remove the peer if the key is valid
  96. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  97. if (error) {
  98. errorMsg = "no peer found for that key";
  99. }
  100. }
  101. Dict* response = Dict_new(requestAlloc);
  102. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  103. if (error) {
  104. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  105. }
  106. Admin_sendMessage(response, txid, context->admin);
  107. }
  108. /*
  109. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  110. {
  111. struct Context* context = Identity_check((struct Context*)vcontext);
  112. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  113. // parse the key
  114. uint8_t pubkey[32];
  115. uint8_t addr[16];
  116. int error = Key_parse(pubkeyString, pubkey, addr);
  117. char* errorMsg = NULL;
  118. if (error) {
  119. errorMsg = "bad key";
  120. } else {
  121. // try to remove the peer if the key is valid
  122. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  123. if (error) {
  124. errorMsg = "no peer found for that key";
  125. }
  126. }
  127. Dict* response = Dict_new(requestAlloc);
  128. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  129. if (error) {
  130. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  131. }
  132. Admin_sendMessage(response, txid, context->admin);
  133. }*/
  134. void InterfaceController_admin_register(struct InterfaceController* ic,
  135. struct Admin* admin,
  136. struct Allocator* alloc)
  137. {
  138. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  139. .alloc = alloc,
  140. .ic = ic,
  141. .admin = admin
  142. }));
  143. Identity_set(ctx);
  144. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, true,
  145. ((struct Admin_FunctionArg[]) {
  146. { .name = "page", .required = 0, .type = "Int" }
  147. }), admin);
  148. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  149. ((struct Admin_FunctionArg[]) {
  150. { .name = "pubkey", .required = 1, .type = "String" }
  151. }), admin);
  152. }