ETHInterface_admin.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "admin/angel/Hermes.h"
  17. #include "benc/Int.h"
  18. #include "admin/Admin.h"
  19. #include "crypto/Key.h"
  20. #include "exception/Jmp.h"
  21. #include "interface/ETHInterface.h"
  22. #include "memory/Allocator.h"
  23. #include "interface/InterfaceController.h"
  24. #include "util/AddrTools.h"
  25. #include "util/Identity.h"
  26. struct Context
  27. {
  28. struct EventBase* eventBase;
  29. struct Allocator* alloc;
  30. struct Log* logger;
  31. struct Admin* admin;
  32. struct InterfaceController* ic;
  33. struct Hermes* hermes;
  34. Identity
  35. };
  36. static void beginConnection(Dict* args,
  37. void* vcontext,
  38. String* txid,
  39. struct Allocator* requestAlloc)
  40. {
  41. struct Context* ctx = Identity_check((struct Context*) vcontext);
  42. String* password = Dict_getString(args, String_CONST("password"));
  43. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  44. String* macAddress = Dict_getString(args, String_CONST("macAddress"));
  45. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  46. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  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, 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_putString(out, String_CONST("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_getString(args, String_CONST("bindDevice"));
  79. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  80. struct Interface* 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_putString(out, String_CONST("error"), String_CONST(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. int ifNum = InterfaceController_regIface(ctx->ic, ethIf, ifname, alloc);
  94. Dict* out = Dict_new(requestAlloc);
  95. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  96. Dict_putInt(out, String_CONST("interfaceNumber"), ifNum, requestAlloc);
  97. Admin_sendMessage(out, txid, ctx->admin);
  98. }
  99. static void beacon(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  100. {
  101. int64_t* stateP = Dict_getInt(args, String_CONST("state"));
  102. int64_t* ifNumP = Dict_getInt(args, String_CONST("interfaceNumber"));
  103. uint32_t ifNum = (ifNumP) ? ((uint32_t) *ifNumP) : 0;
  104. uint32_t state = (stateP) ? ((uint32_t) *stateP) : 0xffffffff;
  105. struct Context* ctx = Identity_check((struct Context*) vcontext);
  106. char* error = NULL;
  107. int ret = InterfaceController_beaconState(ctx->ic, ifNum, state);
  108. if (ret == InterfaceController_beaconState_NO_SUCH_IFACE) {
  109. error = "invalid interfaceNumber";
  110. } else if (ret == InterfaceController_beaconState_INVALID_STATE) {
  111. error = "invalid state";
  112. } else if (ret) {
  113. error = "internal";
  114. }
  115. if (error) {
  116. Dict* out = Dict_new(requestAlloc);
  117. Dict_putString(out, String_CONST("error"), String_CONST(error), requestAlloc);
  118. Admin_sendMessage(out, txid, ctx->admin);
  119. return;
  120. }
  121. char* stateStr = "disabled";
  122. if (state == InterfaceController_beaconState_newState_ACCEPT) {
  123. stateStr = "accepting";
  124. } else if (state == InterfaceController_beaconState_newState_SEND) {
  125. stateStr = "sending and accepting";
  126. }
  127. Dict out = Dict_CONST(
  128. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  129. String_CONST("state"), Int_OBJ(state), Dict_CONST(
  130. String_CONST("stateName"), String_OBJ(String_CONST(stateStr)), NULL
  131. )));
  132. Admin_sendMessage(&out, txid, ctx->admin);
  133. }
  134. // We don't have the security clearences in this process to run ETHInterface_listDevices() directly.
  135. // So we send a message to the angel process which will do it for us.
  136. struct ListDevicesCtx {
  137. struct Allocator* alloc;
  138. String* txid;
  139. struct Context* ctx;
  140. Identity
  141. };
  142. static void listDevicesCallback(Dict* responseMessage, void* context)
  143. {
  144. struct ListDevicesCtx* ldc = Identity_check((struct ListDevicesCtx*) context);
  145. Admin_sendMessage(responseMessage, ldc->txid, ldc->ctx->admin);
  146. Allocator_free(ldc->alloc);
  147. }
  148. static void listDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  149. {
  150. struct Context* const ctx = Identity_check((struct Context*) vcontext);
  151. struct Allocator* const respAlloc = Allocator_child(ctx->alloc);
  152. struct ListDevicesCtx* const ldc = Allocator_malloc(respAlloc, sizeof(struct ListDevicesCtx));
  153. ldc->alloc = respAlloc;
  154. ldc->txid = String_clone(txid, respAlloc);
  155. ldc->ctx = ctx;
  156. Identity_set(ldc);
  157. Dict* const call = Dict_new(respAlloc);
  158. Dict_putString(call, String_CONST("q"), String_CONST("ETHInterface_listDevices"), respAlloc);
  159. struct Jmp jmp;
  160. Jmp_try(jmp) {
  161. Hermes_callAngel(call, listDevicesCallback, ldc, respAlloc, &jmp.handler, ctx->hermes);
  162. } Jmp_catch {
  163. Dict* out = Dict_new(requestAlloc);
  164. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  165. Admin_sendMessage(out, txid, ctx->admin);
  166. return;
  167. }
  168. }
  169. void ETHInterface_admin_register(struct EventBase* base,
  170. struct Allocator* alloc,
  171. struct Log* logger,
  172. struct Admin* admin,
  173. struct InterfaceController* ic,
  174. struct Hermes* hermes)
  175. {
  176. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  177. .eventBase = base,
  178. .alloc = alloc,
  179. .logger = logger,
  180. .admin = admin,
  181. .ic = ic,
  182. .hermes = hermes
  183. }));
  184. Identity_set(ctx);
  185. Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
  186. ((struct Admin_FunctionArg[]) {
  187. { .name = "bindDevice", .required = 1, .type = "String" }
  188. }), admin);
  189. Admin_registerFunction("ETHInterface_beginConnection",
  190. beginConnection, ctx, true, ((struct Admin_FunctionArg[]) {
  191. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  192. { .name = "password", .required = 0, .type = "String" },
  193. { .name = "publicKey", .required = 1, .type = "String" },
  194. { .name = "macAddress", .required = 1, .type = "String" }
  195. }), admin);
  196. Admin_registerFunction("ETHInterface_beacon", beacon, ctx, true,
  197. ((struct Admin_FunctionArg[]) {
  198. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  199. { .name = "state", .required = 0, .type = "Int" }
  200. }), admin);
  201. Admin_registerFunction("ETHInterface_listDevices", listDevices, ctx, true, NULL, admin);
  202. }