ETHInterface_admin.c 8.0 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 "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. int64_t* versionP = Dict_getIntC(args, "version");
  47. int version = Version_DEFAULT_ASSUMPTION;
  48. if (versionP) { version = *versionP; }
  49. char* error = "none";
  50. uint8_t pkBytes[32];
  51. struct ETHInterface_Sockaddr sockaddr = {
  52. .generic = {
  53. .addrLen = ETHInterface_Sockaddr_SIZE
  54. }
  55. };
  56. if (Key_parse(publicKey, pkBytes, NULL)) {
  57. error = "invalid publicKey";
  58. } else if (macAddress->len < 17 || AddrTools_parseMac(sockaddr.mac, macAddress->bytes)) {
  59. error = "invalid macAddress";
  60. } else {
  61. int ret = InterfaceController_bootstrapPeer(
  62. ctx->ic, ifNum, pkBytes, &sockaddr.generic, password, login, peerName, version);
  63. if (ret == InterfaceController_bootstrapPeer_BAD_IFNUM) {
  64. error = "invalid interfaceNumber";
  65. } else if (ret == InterfaceController_bootstrapPeer_BAD_KEY) {
  66. error = "invalid publicKey";
  67. } else if (ret == InterfaceController_bootstrapPeer_OUT_OF_SPACE) {
  68. error = "no more space to register with the switch.";
  69. } else if (ret) {
  70. error = "InterfaceController_bootstrapPeer(internal_error)";
  71. }
  72. }
  73. Dict* out = Dict_new(requestAlloc);
  74. Dict_putStringC(out, "error", String_new(error, requestAlloc), requestAlloc);
  75. Admin_sendMessage(out, txid, ctx->admin);
  76. }
  77. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  78. {
  79. struct Context* const ctx = Identity_check((struct Context*) vcontext);
  80. String* const bindDevice = Dict_getStringC(args, "bindDevice");
  81. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  82. struct Er_Ret* er = NULL;
  83. struct ETHInterface* ethIf =
  84. Er_check(&er, ETHInterface_new(ctx->eventBase, bindDevice->bytes, alloc, ctx->logger));
  85. if (er) {
  86. Dict* out = Dict_new(requestAlloc);
  87. Dict_putStringCC(out, "error", er->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. struct Er_Ret* er = NULL;
  139. List* devices = Er_check(&er, ETHInterface_listDevices(requestAlloc));
  140. if (er) {
  141. Dict* out = Dict_new(requestAlloc);
  142. Dict_putStringCC(out, "error", er->message, requestAlloc);
  143. Admin_sendMessage(out, txid, ctx->admin);
  144. return;
  145. }
  146. Dict* out = Dict_new(requestAlloc);
  147. Dict_putStringCC(out, "error", "none", requestAlloc);
  148. Dict_putListC(out, "devices", devices, requestAlloc);
  149. Admin_sendMessage(out, txid, ctx->admin);
  150. }
  151. void ETHInterface_admin_register(struct EventBase* base,
  152. struct Allocator* alloc,
  153. struct Log* logger,
  154. struct Admin* admin,
  155. struct InterfaceController* ic)
  156. {
  157. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  158. .eventBase = base,
  159. .alloc = alloc,
  160. .logger = logger,
  161. .admin = admin,
  162. .ic = ic
  163. }));
  164. Identity_set(ctx);
  165. Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
  166. ((struct Admin_FunctionArg[]) {
  167. { .name = "bindDevice", .required = 1, .type = "String" }
  168. }), admin);
  169. Admin_registerFunction("ETHInterface_beginConnection",
  170. beginConnection, ctx, true, ((struct Admin_FunctionArg[]) {
  171. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  172. { .name = "password", .required = 0, .type = "String" },
  173. { .name = "publicKey", .required = 1, .type = "String" },
  174. { .name = "macAddress", .required = 1, .type = "String" },
  175. { .name = "login", .required = 0, .type = "String" },
  176. { .name = "peerName", .required = 0, .type = "String" },
  177. { .name = "version", .required = 0, .type = "Int" },
  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. }