InterfaceController_admin.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/AddressCalc.h"
  21. #include "crypto/Key.h"
  22. #include "net/InterfaceController.h"
  23. #include "net/InterfaceController_admin.h"
  24. #include "util/AddrTools.h"
  25. struct Context
  26. {
  27. struct Allocator* alloc;
  28. struct InterfaceController* ic;
  29. struct Admin* admin;
  30. Identity
  31. };
  32. // typical peer record is around 140 benc chars, so can't have very many in 1023
  33. #define ENTRIES_PER_PAGE 6
  34. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  35. {
  36. struct Context* context = Identity_check((struct Context*)vcontext);
  37. struct InterfaceController_PeerStats* stats = NULL;
  38. int64_t* page = Dict_getInt(args, String_CONST("page"));
  39. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  40. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  41. String* bytesIn = String_CONST("bytesIn");
  42. String* bytesOut = String_CONST("bytesOut");
  43. String* pubKey = String_CONST("publicKey");
  44. String* addr = String_CONST("addr");
  45. String* state = String_CONST("state");
  46. String* last = String_CONST("last");
  47. String* switchLabel = String_CONST("switchLabel");
  48. String* isIncoming = String_CONST("isIncoming");
  49. String* user = String_CONST("user");
  50. String* version = String_CONST("version");
  51. String* recvKbps = String_CONST("recvKbps");
  52. String* sendKbps = String_CONST("sendKbps");
  53. String* duplicates = String_CONST("duplicates");
  54. String* lostPackets = String_CONST("lostPackets");
  55. String* receivedOutOfRange = String_CONST("receivedOutOfRange");
  56. List* list = List_new(alloc);
  57. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  58. Dict* d = Dict_new(alloc);
  59. Dict_putInt(d, bytesIn, stats[i].bytesIn, alloc);
  60. Dict_putInt(d, bytesOut, stats[i].bytesOut, alloc);
  61. Dict_putInt(d, recvKbps, stats[i].recvKbps, alloc);
  62. Dict_putInt(d, sendKbps, stats[i].sendKbps, alloc);
  63. Dict_putString(d, addr, Address_toString(&stats[i].addr, alloc), alloc);
  64. Dict_putString(d, pubKey, Key_stringify(stats[i].addr.key, alloc), alloc);
  65. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  66. Dict_putString(d, state, stateString, alloc);
  67. Dict_putInt(d, last, stats[i].timeOfLastMessage, alloc);
  68. uint8_t labelStack[20];
  69. AddrTools_printPath(labelStack, stats[i].addr.path);
  70. Dict_putString(d, switchLabel, String_new((char*)labelStack, alloc), alloc);
  71. Dict_putInt(d, isIncoming, stats[i].isIncomingConnection, alloc);
  72. Dict_putInt(d, duplicates, stats[i].duplicates, alloc);
  73. Dict_putInt(d, lostPackets, stats[i].lostPackets, alloc);
  74. Dict_putInt(d, receivedOutOfRange, stats[i].receivedOutOfRange, alloc);
  75. if (stats[i].user) {
  76. Dict_putString(d, user, stats[i].user, alloc);
  77. }
  78. uint8_t address[16];
  79. AddressCalc_addressForPublicKey(address, stats[i].addr.key);
  80. Dict_putInt(d, version, stats[i].addr.protocolVersion, alloc);
  81. List_addDict(list, d, alloc);
  82. }
  83. Dict* resp = Dict_new(alloc);
  84. Dict_putList(resp, String_CONST("peers"), list, alloc);
  85. Dict_putInt(resp, String_CONST("total"), count, alloc);
  86. if (i < count) {
  87. Dict_putInt(resp, String_CONST("more"), 1, alloc);
  88. }
  89. Dict_putString(resp, String_CONST("deprecation"),
  90. String_CONST("publicKey,switchLabel,version will soon be removed"), alloc);
  91. Admin_sendMessage(resp, txid, context->admin);
  92. }
  93. static void adminDisconnectPeer(Dict* args,
  94. void* vcontext,
  95. String* txid,
  96. struct Allocator* requestAlloc)
  97. {
  98. struct Context* context = Identity_check((struct Context*)vcontext);
  99. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  100. // parse the key
  101. uint8_t pubkey[32];
  102. uint8_t addr[16];
  103. int error = Key_parse(pubkeyString, pubkey, addr);
  104. char* errorMsg = NULL;
  105. if (error) {
  106. errorMsg = "bad key";
  107. } else {
  108. // try to remove the peer if the key is valid
  109. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  110. if (error) {
  111. errorMsg = "no peer found for that key";
  112. }
  113. }
  114. Dict* response = Dict_new(requestAlloc);
  115. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  116. if (error) {
  117. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  118. }
  119. Admin_sendMessage(response, txid, context->admin);
  120. }
  121. static void adminResetPeering(Dict* args,
  122. void* vcontext,
  123. String* txid,
  124. struct Allocator* requestAlloc)
  125. {
  126. struct Context* context = Identity_check((struct Context*)vcontext);
  127. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  128. int error = 0;
  129. char* errorMsg = NULL;
  130. if (pubkeyString) {
  131. // parse the key
  132. uint8_t pubkey[32];
  133. uint8_t addr[16];
  134. error = Key_parse(pubkeyString, pubkey, addr);
  135. if (error) {
  136. errorMsg = "bad key";
  137. } else {
  138. InterfaceController_resetPeering(context->ic, pubkey);
  139. }
  140. } else {
  141. // reset all
  142. InterfaceController_resetPeering(context->ic, NULL);
  143. }
  144. Dict* response = Dict_new(requestAlloc);
  145. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  146. if (error) {
  147. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  148. }
  149. Admin_sendMessage(response, txid, context->admin);
  150. }
  151. /*
  152. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  153. {
  154. struct Context* context = Identity_check((struct Context*)vcontext);
  155. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  156. // parse the key
  157. uint8_t pubkey[32];
  158. uint8_t addr[16];
  159. int error = Key_parse(pubkeyString, pubkey, addr);
  160. char* errorMsg = NULL;
  161. if (error) {
  162. errorMsg = "bad key";
  163. } else {
  164. // try to remove the peer if the key is valid
  165. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  166. if (error) {
  167. errorMsg = "no peer found for that key";
  168. }
  169. }
  170. Dict* response = Dict_new(requestAlloc);
  171. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  172. if (error) {
  173. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  174. }
  175. Admin_sendMessage(response, txid, context->admin);
  176. }*/
  177. void InterfaceController_admin_register(struct InterfaceController* ic,
  178. struct Admin* admin,
  179. struct Allocator* allocator)
  180. {
  181. struct Allocator* alloc = Allocator_child(allocator);
  182. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  183. .alloc = alloc,
  184. .ic = ic,
  185. .admin = admin
  186. }));
  187. Identity_set(ctx);
  188. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  189. ((struct Admin_FunctionArg[]) {
  190. { .name = "page", .required = 0, .type = "Int" }
  191. }), admin);
  192. Admin_registerFunction("InterfaceController_resetPeering", adminResetPeering, ctx, true,
  193. ((struct Admin_FunctionArg[]) {
  194. { .name = "pubkey", .required = 0, .type = "String" }
  195. }), admin);
  196. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  197. ((struct Admin_FunctionArg[]) {
  198. { .name = "pubkey", .required = 1, .type = "String" }
  199. }), admin);
  200. }