InterfaceController_admin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_toString(&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, "ifNum", stats[i].ifNum, alloc);
  100. if (stats[i].user) {
  101. Dict_putStringC(d, "user", stats[i].user, alloc);
  102. }
  103. Dict_putIntC(d, "receivedPackets", stats[i].receivedPackets, alloc);
  104. List_addDict(list, d, alloc);
  105. }
  106. Dict* resp = Dict_new(alloc);
  107. Dict_putListC(resp, "peers", list, alloc);
  108. Dict_putIntC(resp, "total", count, alloc);
  109. if (i < count) {
  110. Dict_putIntC(resp, "more", 1, alloc);
  111. }
  112. Admin_sendMessage(resp, txid, context->admin);
  113. }
  114. static void adminDisconnectPeer(Dict* args,
  115. void* vcontext,
  116. String* txid,
  117. struct Allocator* requestAlloc)
  118. {
  119. struct Context* context = Identity_check((struct Context*)vcontext);
  120. String* pubkeyString = Dict_getStringC(args, "pubkey");
  121. // parse the key
  122. uint8_t pubkey[32];
  123. uint8_t addr[16];
  124. int error = Key_parse(pubkeyString, pubkey, addr);
  125. char* errorMsg = NULL;
  126. if (error) {
  127. errorMsg = "bad key";
  128. } else {
  129. // try to remove the peer if the key is valid
  130. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  131. if (error) {
  132. errorMsg = "no peer found for that key";
  133. }
  134. }
  135. Dict* response = Dict_new(requestAlloc);
  136. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  137. if (error) {
  138. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  139. }
  140. Admin_sendMessage(response, txid, context->admin);
  141. }
  142. static void adminResetPeering(Dict* args,
  143. void* vcontext,
  144. String* txid,
  145. struct Allocator* requestAlloc)
  146. {
  147. struct Context* context = Identity_check((struct Context*)vcontext);
  148. String* pubkeyString = Dict_getStringC(args, "pubkey");
  149. int error = 0;
  150. char* errorMsg = NULL;
  151. if (pubkeyString) {
  152. // parse the key
  153. uint8_t pubkey[32];
  154. uint8_t addr[16];
  155. error = Key_parse(pubkeyString, pubkey, addr);
  156. if (error) {
  157. errorMsg = "bad key";
  158. } else {
  159. InterfaceController_resetPeering(context->ic, pubkey);
  160. }
  161. } else {
  162. // reset all
  163. InterfaceController_resetPeering(context->ic, NULL);
  164. }
  165. Dict* response = Dict_new(requestAlloc);
  166. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  167. if (error) {
  168. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  169. }
  170. Admin_sendMessage(response, txid, context->admin);
  171. }
  172. static void timestampPackets(Dict* args,
  173. void* vcontext,
  174. String* txid,
  175. struct Allocator* requestAlloc)
  176. {
  177. struct Context* context = Identity_check((struct Context*)vcontext);
  178. int64_t* enable = Dict_getIntC(args, "enable");
  179. Dict* response = Dict_new(requestAlloc);
  180. Dict_putStringCC(response, "error", "none", requestAlloc);
  181. if (!enable) {
  182. Dict_putIntC(response, "enabled", context->ic->timestampPackets, requestAlloc);
  183. } else {
  184. context->ic->timestampPackets = *enable;
  185. }
  186. Admin_sendMessage(response, txid, context->admin);
  187. }
  188. /*
  189. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  190. {
  191. struct Context* context = Identity_check((struct Context*)vcontext);
  192. String* pubkeyString = Dict_getStringC(args, "pubkey");
  193. // parse the key
  194. uint8_t pubkey[32];
  195. uint8_t addr[16];
  196. int error = Key_parse(pubkeyString, pubkey, addr);
  197. char* errorMsg = NULL;
  198. if (error) {
  199. errorMsg = "bad key";
  200. } else {
  201. // try to remove the peer if the key is valid
  202. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  203. if (error) {
  204. errorMsg = "no peer found for that key";
  205. }
  206. }
  207. Dict* response = Dict_new(requestAlloc);
  208. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  209. if (error) {
  210. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  211. }
  212. Admin_sendMessage(response, txid, context->admin);
  213. }*/
  214. void InterfaceController_admin_register(struct InterfaceController* ic,
  215. struct Admin* admin,
  216. struct Allocator* allocator)
  217. {
  218. struct Allocator* alloc = Allocator_child(allocator);
  219. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  220. .alloc = alloc,
  221. .ic = ic,
  222. .admin = admin
  223. }));
  224. Identity_set(ctx);
  225. Admin_registerFunction("InterfaceController_interfaces", adminInterfaces, ctx, true,
  226. ((struct Admin_FunctionArg[]) {
  227. { .name = "page", .required = 0, .type = "Int" }
  228. }), admin);
  229. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  230. ((struct Admin_FunctionArg[]) {
  231. { .name = "page", .required = 0, .type = "Int" }
  232. }), admin);
  233. Admin_registerFunction("InterfaceController_resetPeering", adminResetPeering, ctx, true,
  234. ((struct Admin_FunctionArg[]) {
  235. { .name = "pubkey", .required = 0, .type = "String" }
  236. }), admin);
  237. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  238. ((struct Admin_FunctionArg[]) {
  239. { .name = "pubkey", .required = 1, .type = "String" }
  240. }), admin);
  241. Admin_registerFunction("InterfaceController_timestampPackets", timestampPackets, ctx, true,
  242. ((struct Admin_FunctionArg[]) {
  243. { .name = "enable", .required = 0, .type = "Int" }
  244. }), admin);
  245. }