ETHInterface_admin.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "interface/ETHInterface_admin.h"
  16. #include "interface/ETHInterface.h"
  17. #include "benc/Int.h"
  18. #include "admin/Admin.h"
  19. #include "crypto/Key.h"
  20. #include "exception/Jmp.h"
  21. #include "memory/Allocator.h"
  22. #include "net/InterfaceController.h"
  23. #include "util/AddrTools.h"
  24. #include "util/Identity.h"
  25. struct Context
  26. {
  27. struct EventBase* eventBase;
  28. struct Allocator* alloc;
  29. struct Log* logger;
  30. struct Admin* admin;
  31. struct InterfaceController* ic;
  32. Identity
  33. };
  34. static void beginConnection(Dict* args,
  35. void* vcontext,
  36. String* txid,
  37. struct Allocator* requestAlloc)
  38. {
  39. struct Context* ctx = Identity_check((struct Context*) vcontext);
  40. String* password = Dict_getStringC(args, "password");
  41. String* login = Dict_getStringC(args, "login");
  42. String* publicKey = Dict_getStringC(args, "publicKey");
  43. String* macAddress = Dict_getStringC(args, "macAddress");
  44. int64_t* interfaceNumber = Dict_getIntC(args, "interfaceNumber");
  45. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  46. String* peerName = Dict_getStringC(args, "peerName");
  47. char* error = "none";
  48. uint8_t pkBytes[32];
  49. struct ETHInterface_Sockaddr sockaddr = {
  50. .generic = {
  51. .addrLen = ETHInterface_Sockaddr_SIZE
  52. }
  53. };
  54. if (Key_parse(publicKey, pkBytes, NULL)) {
  55. error = "invalid publicKey";
  56. } else if (macAddress->len < 17 || AddrTools_parseMac(sockaddr.mac, macAddress->bytes)) {
  57. error = "invalid macAddress";
  58. } else {
  59. int ret = InterfaceController_bootstrapPeer(
  60. ctx->ic, ifNum, pkBytes, &sockaddr.generic, password, login, peerName, ctx->alloc);
  61. if (ret == InterfaceController_bootstrapPeer_BAD_IFNUM) {
  62. error = "invalid interfaceNumber";
  63. } else if (ret == InterfaceController_bootstrapPeer_BAD_KEY) {
  64. error = "invalid publicKey";
  65. } else if (ret == InterfaceController_bootstrapPeer_OUT_OF_SPACE) {
  66. error = "no more space to register with the switch.";
  67. } else if (ret) {
  68. error = "InterfaceController_bootstrapPeer(internal_error)";
  69. }
  70. }
  71. Dict* out = Dict_new(requestAlloc);
  72. Dict_putStringC(out, "error", String_new(error, requestAlloc), requestAlloc);
  73. Admin_sendMessage(out, txid, ctx->admin);
  74. }
  75. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  76. {
  77. struct Context* const ctx = Identity_check((struct Context*) vcontext);
  78. String* const bindDevice = Dict_getStringC(args, "bindDevice");
  79. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  80. struct ETHInterface* ethIf = NULL;
  81. struct Jmp jmp;
  82. Jmp_try(jmp) {
  83. ethIf = ETHInterface_new(
  84. ctx->eventBase, bindDevice->bytes, alloc, &jmp.handler, ctx->logger);
  85. } Jmp_catch {
  86. Dict* out = Dict_new(requestAlloc);
  87. Dict_putStringCC(out, "error", jmp.message, requestAlloc);
  88. Admin_sendMessage(out, txid, ctx->admin);
  89. Allocator_free(alloc);
  90. return;
  91. }
  92. String* ifname = String_printf(requestAlloc, "ETH/%s", bindDevice->bytes);
  93. struct InterfaceController_Iface* ici = InterfaceController_newIface(ctx->ic, ifname, alloc);
  94. Iface_plumb(&ici->addrIf, &ethIf->generic.iface);
  95. Dict* out = Dict_new(requestAlloc);
  96. Dict_putStringCC(out, "error", "none", requestAlloc);
  97. Dict_putIntC(out, "interfaceNumber", ici->ifNum, requestAlloc);
  98. Admin_sendMessage(out, txid, ctx->admin);
  99. }
  100. static void beacon(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  101. {
  102. int64_t* stateP = Dict_getIntC(args, "state");
  103. int64_t* ifNumP = Dict_getIntC(args, "interfaceNumber");
  104. uint32_t ifNum = (ifNumP) ? ((uint32_t) *ifNumP) : 0;
  105. uint32_t state = (stateP) ? ((uint32_t) *stateP) : 0xffffffff;
  106. struct Context* ctx = Identity_check((struct Context*) vcontext);
  107. char* error = NULL;
  108. int ret = InterfaceController_beaconState(ctx->ic, ifNum, state);
  109. if (ret == InterfaceController_beaconState_NO_SUCH_IFACE) {
  110. error = "invalid interfaceNumber";
  111. } else if (ret == InterfaceController_beaconState_INVALID_STATE) {
  112. error = "invalid state";
  113. } else if (ret) {
  114. error = "internal";
  115. }
  116. if (error) {
  117. Dict* out = Dict_new(requestAlloc);
  118. Dict_putStringCC(out, "error", error, requestAlloc);
  119. Admin_sendMessage(out, txid, ctx->admin);
  120. return;
  121. }
  122. char* stateStr = "disabled";
  123. if (state == InterfaceController_beaconState_newState_ACCEPT) {
  124. stateStr = "accepting";
  125. } else if (state == InterfaceController_beaconState_newState_SEND) {
  126. stateStr = "sending and accepting";
  127. }
  128. Dict out = Dict_CONST(
  129. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  130. String_CONST("state"), Int_OBJ(state), Dict_CONST(
  131. String_CONST("stateName"), String_OBJ(String_CONST(stateStr)), NULL
  132. )));
  133. Admin_sendMessage(&out, txid, ctx->admin);
  134. }
  135. static void listDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  136. {
  137. struct Context* ctx = Identity_check((struct Context*) vcontext);
  138. List* devices = NULL;
  139. struct Jmp jmp;
  140. Jmp_try(jmp) {
  141. devices = ETHInterface_listDevices(requestAlloc, &jmp.handler);
  142. } Jmp_catch {
  143. Dict* out = Dict_new(requestAlloc);
  144. Dict_putStringCC(out, "error", jmp.message, requestAlloc);
  145. Admin_sendMessage(out, txid, ctx->admin);
  146. return;
  147. }
  148. Dict* out = Dict_new(requestAlloc);
  149. Dict_putStringCC(out, "error", "none", requestAlloc);
  150. Dict_putListC(out, "devices", devices, requestAlloc);
  151. Admin_sendMessage(out, txid, ctx->admin);
  152. }
  153. void ETHInterface_admin_register(struct EventBase* base,
  154. struct Allocator* alloc,
  155. struct Log* logger,
  156. struct Admin* admin,
  157. struct InterfaceController* ic)
  158. {
  159. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  160. .eventBase = base,
  161. .alloc = alloc,
  162. .logger = logger,
  163. .admin = admin,
  164. .ic = ic
  165. }));
  166. Identity_set(ctx);
  167. Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
  168. ((struct Admin_FunctionArg[]) {
  169. { .name = "bindDevice", .required = 1, .type = "String" }
  170. }), admin);
  171. Admin_registerFunction("ETHInterface_beginConnection",
  172. beginConnection, ctx, true, ((struct Admin_FunctionArg[]) {
  173. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  174. { .name = "password", .required = 0, .type = "String" },
  175. { .name = "publicKey", .required = 1, .type = "String" },
  176. { .name = "macAddress", .required = 1, .type = "String" },
  177. { .name = "login", .required = 0, .type = "String" }
  178. }), admin);
  179. Admin_registerFunction("ETHInterface_beacon", beacon, ctx, true,
  180. ((struct Admin_FunctionArg[]) {
  181. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  182. { .name = "state", .required = 0, .type = "Int" }
  183. }), admin);
  184. Admin_registerFunction("ETHInterface_listDevices", listDevices, ctx, true, NULL, admin);
  185. }