InterfaceController_admin.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <https://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. #ifdef HAS_ETH_INTERFACE
  23. #include "interface/ETHInterface.h"
  24. #endif
  25. #include "net/InterfaceController.h"
  26. #include "net/InterfaceController_admin.h"
  27. #include "util/AddrTools.h"
  28. struct Context
  29. {
  30. struct Allocator* alloc;
  31. struct InterfaceController* ic;
  32. struct Admin* admin;
  33. Identity
  34. };
  35. // typical peer record is around 140 benc chars, so can't have very many in 1023
  36. #define ENTRIES_PER_PAGE 6
  37. static void adminInterfaces(Dict* args,
  38. void* vcontext,
  39. String* txid,
  40. struct Allocator* alloc)
  41. {
  42. struct Context* context = Identity_check((struct Context*)vcontext);
  43. int64_t* page = Dict_getIntC(args, "page");
  44. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  45. int count = InterfaceController_ifaceCount(context->ic);
  46. //int count = InterfaceController_getIface(context->ic, alloc, &stats);
  47. List* list = List_new(alloc);
  48. for (int counter = 0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  49. struct InterfaceController_Iface* iface = InterfaceController_getIface(context->ic, i);
  50. Dict* d = Dict_new(alloc);
  51. Dict_putIntC(d, "ifNum", iface->ifNum, alloc);
  52. Dict_putStringC(d, "name", iface->name, alloc);
  53. char* bs = InterfaceController_beaconStateString(iface->beaconState);
  54. Dict_putStringCC(d, "beaconState", bs, alloc);
  55. List_addDict(list, d, alloc);
  56. }
  57. Dict* resp = Dict_new(alloc);
  58. Dict_putListC(resp, "ifaces", list, alloc);
  59. Dict_putIntC(resp, "total", count, alloc);
  60. if (i < count) { Dict_putIntC(resp, "more", 1, alloc); }
  61. Admin_sendMessage(resp, txid, context->admin);
  62. }
  63. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  64. {
  65. struct Context* context = Identity_check((struct Context*)vcontext);
  66. struct InterfaceController_PeerStats* stats = NULL;
  67. int64_t* page = Dict_getIntC(args, "page");
  68. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  69. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  70. List* list = List_new(alloc);
  71. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  72. Dict* d = Dict_new(alloc);
  73. Dict_putIntC(d, "bytesIn", stats[i].bytesIn, alloc);
  74. Dict_putIntC(d, "bytesOut", stats[i].bytesOut, alloc);
  75. Dict_putIntC(d, "recvKbps", stats[i].recvKbps, alloc);
  76. Dict_putIntC(d, "sendKbps", stats[i].sendKbps, alloc);
  77. Dict_putStringC(d, "addr", Address_toStringKey(&stats[i].addr, alloc), alloc);
  78. String* lladdrString;
  79. #ifdef HAS_ETH_INTERFACE
  80. if (ETHInterface_Sockaddr_SIZE == stats[i].lladdr->addrLen) {
  81. struct ETHInterface_Sockaddr* eth = (struct ETHInterface_Sockaddr*) stats[i].lladdr;
  82. uint8_t printedMac[18];
  83. AddrTools_printMac(printedMac, eth->mac);
  84. lladdrString = String_new(printedMac, alloc);
  85. } else {
  86. lladdrString = String_new(Sockaddr_print(stats[i].lladdr, alloc), alloc);
  87. }
  88. #else
  89. lladdrString = String_new(Sockaddr_print(stats[i].lladdr, alloc), alloc);
  90. #endif
  91. Dict_putStringC(d, "lladdr", lladdrString, alloc);
  92. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  93. Dict_putStringC(d, "state", stateString, alloc);
  94. Dict_putIntC(d, "last", stats[i].timeOfLastMessage, alloc);
  95. Dict_putIntC(d, "isIncoming", stats[i].isIncomingConnection, alloc);
  96. Dict_putIntC(d, "duplicates", stats[i].duplicates, alloc);
  97. Dict_putIntC(d, "lostPackets", stats[i].lostPackets, alloc);
  98. Dict_putIntC(d, "receivedOutOfRange", stats[i].receivedOutOfRange, alloc);
  99. Dict_putIntC(d, "noiseProto", stats[i].noiseProto, alloc);
  100. Dict_putIntC(d, "ifNum", stats[i].ifNum, alloc);
  101. if (stats[i].user) {
  102. Dict_putStringC(d, "user", stats[i].user, alloc);
  103. }
  104. Dict_putIntC(d, "receivedPackets", stats[i].receivedPackets, alloc);
  105. List_addDict(list, d, alloc);
  106. }
  107. Dict* resp = Dict_new(alloc);
  108. Dict_putListC(resp, "peers", list, alloc);
  109. Dict_putIntC(resp, "total", count, alloc);
  110. if (i < count) {
  111. Dict_putIntC(resp, "more", 1, alloc);
  112. }
  113. Admin_sendMessage(resp, txid, context->admin);
  114. }
  115. static void adminDisconnectPeer(Dict* args,
  116. void* vcontext,
  117. String* txid,
  118. struct Allocator* requestAlloc)
  119. {
  120. struct Context* context = Identity_check((struct Context*)vcontext);
  121. String* pubkeyString = Dict_getStringC(args, "pubkey");
  122. // parse the key
  123. uint8_t pubkey[32];
  124. uint8_t addr[16];
  125. int count = 0;
  126. char* errorMsg = NULL;
  127. if (Key_parse(pubkeyString, pubkey, addr)) {
  128. errorMsg = "bad key";
  129. } else {
  130. // try to remove the peer if the key is valid
  131. count = InterfaceController_disconnectPeer(context->ic,pubkey);
  132. if (count == 0) {
  133. errorMsg = "no peer found for that key";
  134. }
  135. }
  136. Dict* response = Dict_new(requestAlloc);
  137. Dict_putIntC(response, "success", errorMsg ? 0 : 1, requestAlloc);
  138. if (errorMsg) {
  139. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  140. }
  141. Dict_putIntC(response, "sessionsDisconnected", count, requestAlloc);
  142. Admin_sendMessage(response, txid, context->admin);
  143. }
  144. static void adminResetPeering(Dict* args,
  145. void* vcontext,
  146. String* txid,
  147. struct Allocator* requestAlloc)
  148. {
  149. struct Context* context = Identity_check((struct Context*)vcontext);
  150. String* pubkeyString = Dict_getStringC(args, "pubkey");
  151. int error = 0;
  152. char* errorMsg = NULL;
  153. if (pubkeyString) {
  154. // parse the key
  155. uint8_t pubkey[32];
  156. uint8_t addr[16];
  157. error = Key_parse(pubkeyString, pubkey, addr);
  158. if (error) {
  159. errorMsg = "bad key";
  160. } else {
  161. InterfaceController_resetPeering(context->ic, pubkey);
  162. }
  163. } else {
  164. // reset all
  165. InterfaceController_resetPeering(context->ic, NULL);
  166. }
  167. Dict* response = Dict_new(requestAlloc);
  168. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  169. if (error) {
  170. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  171. }
  172. Admin_sendMessage(response, txid, context->admin);
  173. }
  174. /*
  175. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  176. {
  177. struct Context* context = Identity_check((struct Context*)vcontext);
  178. String* pubkeyString = Dict_getStringC(args, "pubkey");
  179. // parse the key
  180. uint8_t pubkey[32];
  181. uint8_t addr[16];
  182. int error = Key_parse(pubkeyString, pubkey, addr);
  183. char* errorMsg = NULL;
  184. if (error) {
  185. errorMsg = "bad key";
  186. } else {
  187. // try to remove the peer if the key is valid
  188. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  189. if (error) {
  190. errorMsg = "no peer found for that key";
  191. }
  192. }
  193. Dict* response = Dict_new(requestAlloc);
  194. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  195. if (error) {
  196. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  197. }
  198. Admin_sendMessage(response, txid, context->admin);
  199. }*/
  200. void InterfaceController_admin_register(struct InterfaceController* ic,
  201. struct Admin* admin,
  202. struct Allocator* allocator)
  203. {
  204. struct Allocator* alloc = Allocator_child(allocator);
  205. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  206. .alloc = alloc,
  207. .ic = ic,
  208. .admin = admin
  209. }));
  210. Identity_set(ctx);
  211. Admin_registerFunction("InterfaceController_interfaces", adminInterfaces, ctx, true,
  212. ((struct Admin_FunctionArg[]) {
  213. { .name = "page", .required = 0, .type = "Int" }
  214. }), admin);
  215. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  216. ((struct Admin_FunctionArg[]) {
  217. { .name = "page", .required = 0, .type = "Int" }
  218. }), admin);
  219. Admin_registerFunction("InterfaceController_resetPeering", adminResetPeering, ctx, true,
  220. ((struct Admin_FunctionArg[]) {
  221. { .name = "pubkey", .required = 0, .type = "String" }
  222. }), admin);
  223. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  224. ((struct Admin_FunctionArg[]) {
  225. { .name = "pubkey", .required = 1, .type = "String" }
  226. }), admin);
  227. }