InterfaceController_admin.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  30. // typical peer record is around 140 benc chars, so can't have very many in 1023
  31. #define ENTRIES_PER_PAGE 6
  32. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  33. {
  34. struct Context* context = vcontext;
  35. struct Allocator* alloc = Allocator_child(context->alloc);
  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 = context->ic->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 = NULL;
  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 = List_addDict(list, d, alloc);
  71. }
  72. Dict response = Dict_CONST(
  73. String_CONST("peers"), List_OBJ(list), Dict_CONST(
  74. String_CONST("total"), Int_OBJ(count), NULL
  75. ));
  76. if (i < count) {
  77. response = Dict_CONST(
  78. String_CONST("more"), Int_OBJ(1), response
  79. );
  80. }
  81. Admin_sendMessage(&response, txid, context->admin);
  82. Allocator_free(alloc);
  83. }
  84. static void adminDisconnectPeer(Dict* args,
  85. void* vcontext,
  86. String* txid,
  87. struct Allocator* requestAlloc)
  88. {
  89. struct Context* context = vcontext;
  90. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  91. // parse the key
  92. uint8_t pubkey[32];
  93. uint8_t addr[16];
  94. int error = Key_parse(pubkeyString, pubkey, addr);
  95. char* errorMsg = NULL;
  96. if (error) {
  97. errorMsg = "bad key";
  98. }
  99. else {
  100. // try to remove the peer if the key is valid
  101. error = context->ic->disconnectPeer(context->ic,pubkey);
  102. if (error) {
  103. errorMsg = "no peer found for that key";
  104. }
  105. }
  106. Dict* response = Dict_new(requestAlloc);
  107. Dict_putInt(response, String_CONST("sucess"), error ? 0 : 1, requestAlloc);
  108. if (error) {
  109. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  110. }
  111. Admin_sendMessage(response, txid, context->admin);
  112. }
  113. void InterfaceController_admin_register(struct InterfaceController* ic,
  114. struct Admin* admin,
  115. struct Allocator* alloc)
  116. {
  117. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  118. .alloc = alloc,
  119. .ic = ic,
  120. .admin = admin
  121. }));
  122. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, true,
  123. ((struct Admin_FunctionArg[]) {
  124. { .name = "page", .required = 0, .type = "Int" }
  125. }), admin);
  126. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  127. ((struct Admin_FunctionArg[]) {
  128. { .name = "pubkey", .required = 1, .type = "String" }
  129. }), admin);
  130. }