ETHInterface_admin.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <http://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_getString(args, String_CONST("password"));
  41. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  42. String* macAddress = Dict_getString(args, String_CONST("macAddress"));
  43. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  44. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  45. char* error = "none";
  46. uint8_t pkBytes[32];
  47. struct ETHInterface_Sockaddr sockaddr = {
  48. .generic = {
  49. .addrLen = ETHInterface_Sockaddr_SIZE
  50. }
  51. };
  52. if (Key_parse(publicKey, pkBytes, NULL)) {
  53. error = "invalid publicKey";
  54. } else if (macAddress->len < 17 || AddrTools_parseMac(sockaddr.mac, macAddress->bytes)) {
  55. error = "invalid macAddress";
  56. } else {
  57. int ret = InterfaceController_bootstrapPeer(
  58. ctx->ic, ifNum, pkBytes, &sockaddr.generic, password, ctx->alloc);
  59. if (ret == InterfaceController_bootstrapPeer_BAD_IFNUM) {
  60. error = "invalid interfaceNumber";
  61. } else if (ret == InterfaceController_bootstrapPeer_BAD_KEY) {
  62. error = "invalid publicKey";
  63. } else if (ret == InterfaceController_bootstrapPeer_OUT_OF_SPACE) {
  64. error = "no more space to register with the switch.";
  65. } else if (ret) {
  66. error = "InterfaceController_bootstrapPeer(internal_error)";
  67. }
  68. }
  69. Dict* out = Dict_new(requestAlloc);
  70. Dict_putString(out, String_CONST("error"), String_new(error, requestAlloc), requestAlloc);
  71. Admin_sendMessage(out, txid, ctx->admin);
  72. }
  73. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  74. {
  75. struct Context* const ctx = Identity_check((struct Context*) vcontext);
  76. String* const bindDevice = Dict_getString(args, String_CONST("bindDevice"));
  77. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  78. struct ETHInterface* ethIf = NULL;
  79. struct Jmp jmp;
  80. Jmp_try(jmp) {
  81. ethIf = ETHInterface_new(
  82. ctx->eventBase, bindDevice->bytes, alloc, &jmp.handler, ctx->logger);
  83. } Jmp_catch {
  84. Dict* out = Dict_new(requestAlloc);
  85. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  86. Admin_sendMessage(out, txid, ctx->admin);
  87. Allocator_free(alloc);
  88. return;
  89. }
  90. String* ifname = String_printf(requestAlloc, "ETH/%s", bindDevice->bytes);
  91. struct InterfaceController_Iface* ici = InterfaceController_newIface(ctx->ic, ifname, alloc);
  92. Iface_plumb(&ici->addrIf, &ethIf->generic.iface);
  93. Dict* out = Dict_new(requestAlloc);
  94. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  95. Dict_putInt(out, String_CONST("interfaceNumber"), ici->ifNum, requestAlloc);
  96. Admin_sendMessage(out, txid, ctx->admin);
  97. }
  98. static void beacon(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  99. {
  100. int64_t* stateP = Dict_getInt(args, String_CONST("state"));
  101. int64_t* ifNumP = Dict_getInt(args, String_CONST("interfaceNumber"));
  102. uint32_t ifNum = (ifNumP) ? ((uint32_t) *ifNumP) : 0;
  103. uint32_t state = (stateP) ? ((uint32_t) *stateP) : 0xffffffff;
  104. struct Context* ctx = Identity_check((struct Context*) vcontext);
  105. char* error = NULL;
  106. int ret = InterfaceController_beaconState(ctx->ic, ifNum, state);
  107. if (ret == InterfaceController_beaconState_NO_SUCH_IFACE) {
  108. error = "invalid interfaceNumber";
  109. } else if (ret == InterfaceController_beaconState_INVALID_STATE) {
  110. error = "invalid state";
  111. } else if (ret) {
  112. error = "internal";
  113. }
  114. if (error) {
  115. Dict* out = Dict_new(requestAlloc);
  116. Dict_putString(out, String_CONST("error"), String_CONST(error), requestAlloc);
  117. Admin_sendMessage(out, txid, ctx->admin);
  118. return;
  119. }
  120. char* stateStr = "disabled";
  121. if (state == InterfaceController_beaconState_newState_ACCEPT) {
  122. stateStr = "accepting";
  123. } else if (state == InterfaceController_beaconState_newState_SEND) {
  124. stateStr = "sending and accepting";
  125. }
  126. Dict out = Dict_CONST(
  127. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  128. String_CONST("state"), Int_OBJ(state), Dict_CONST(
  129. String_CONST("stateName"), String_OBJ(String_CONST(stateStr)), NULL
  130. )));
  131. Admin_sendMessage(&out, txid, ctx->admin);
  132. }
  133. static void listDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  134. {
  135. struct Context* ctx = Identity_check((struct Context*) vcontext);
  136. List* devices = NULL;
  137. struct Jmp jmp;
  138. Jmp_try(jmp) {
  139. devices = ETHInterface_listDevices(requestAlloc, &jmp.handler);
  140. } Jmp_catch {
  141. Dict* out = Dict_new(requestAlloc);
  142. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  143. Admin_sendMessage(out, txid, ctx->admin);
  144. return;
  145. }
  146. Dict* out = Dict_new(requestAlloc);
  147. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  148. Dict_putList(out, String_CONST("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. }), admin);
  176. Admin_registerFunction("ETHInterface_beacon", beacon, ctx, true,
  177. ((struct Admin_FunctionArg[]) {
  178. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  179. { .name = "state", .required = 0, .type = "Int" }
  180. }), admin);
  181. Admin_registerFunction("ETHInterface_listDevices", listDevices, ctx, true, NULL, admin);
  182. }