ETHInterface_admin.c 7.8 KB

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