1
0

ETHInterface_admin.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "benc/Int.h"
  16. #include "admin/Admin.h"
  17. #include "exception/Jmp.h"
  18. #include "interface/ETHInterface.h"
  19. #include "memory/Allocator.h"
  20. #include "interface/InterfaceController.h"
  21. #include "util/Base32.h"
  22. struct Context
  23. {
  24. struct EventBase* eventBase;
  25. struct Allocator* allocator;
  26. struct Log* logger;
  27. struct Admin* admin;
  28. struct InterfaceController* ic;
  29. uint32_t ifCount;
  30. struct ETHInterface** ifaces;
  31. };
  32. static int isValidIfNum(struct Context* ctx, int64_t* interfaceNumber, char** error)
  33. {
  34. if (ctx->ifCount == 0) {
  35. *error = "no interfaces are setup, call ETHInterface_new() first";
  36. } else if (interfaceNumber && (*interfaceNumber >= ctx->ifCount || *interfaceNumber < 0)) {
  37. *error = "invalid interfaceNumber";
  38. } else {
  39. return true;
  40. }
  41. return false;
  42. }
  43. static void beginConnection(Dict* args, void* vcontext, String* txid)
  44. {
  45. struct Context* ctx = vcontext;
  46. String* password = Dict_getString(args, String_CONST("password"));
  47. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  48. String* macAddress = Dict_getString(args, String_CONST("macAddress"));
  49. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  50. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  51. char* error = NULL;
  52. uint8_t pkBytes[32];
  53. if (!isValidIfNum(ctx, interfaceNumber, &error)) {
  54. // fall through
  55. } else if (!publicKey
  56. || publicKey->len < 52
  57. || (publicKey->len > 52 && publicKey->bytes[52] != '.'))
  58. {
  59. error = "publicKey must be 52 characters long.";
  60. } else if (Base32_decode(pkBytes, 32, (uint8_t*)publicKey->bytes, 52) != 32) {
  61. error = "failed to parse publicKey.";
  62. } else {
  63. struct ETHInterface* ethif = ctx->ifaces[ifNum];
  64. switch (ETHInterface_beginConnection(macAddress->bytes, pkBytes, password, ethif)) {
  65. case ETHInterface_beginConnection_OUT_OF_SPACE:
  66. error = "no more space to register with the switch.";
  67. break;
  68. case ETHInterface_beginConnection_BAD_KEY:
  69. error = "invalid cjdns public key.";
  70. break;
  71. case ETHInterface_beginConnection_BAD_IFACE:
  72. error = "unable to parse device name.";
  73. break;
  74. case 0:
  75. error = "none";
  76. break;
  77. default:
  78. error = "unknown error";
  79. }
  80. }
  81. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(error)), NULL);
  82. Admin_sendMessage(&out, txid, ctx->admin);
  83. }
  84. static void newInterface(Dict* args, void* vcontext, String* txid)
  85. {
  86. struct Context* const ctx = vcontext;
  87. String* const bindDevice = Dict_getString(args, String_CONST("bindDevice"));
  88. struct Allocator* const alloc = Allocator_child(ctx->allocator);
  89. struct ETHInterface* ethIf = NULL;
  90. struct Jmp jmp;
  91. Jmp_try(jmp) {
  92. ethIf = ETHInterface_new(
  93. ctx->eventBase, bindDevice->bytes, alloc, &jmp.handler, ctx->logger, ctx->ic);
  94. } Jmp_catch {
  95. String* errStr = String_CONST(jmp.message);
  96. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  97. Admin_sendMessage(&out, txid, ctx->admin);
  98. Allocator_free(alloc);
  99. return;
  100. }
  101. // sizeof(struct ETHInterface*) the size of a pointer.
  102. ctx->ifaces = Allocator_realloc(ctx->allocator,
  103. ctx->ifaces,
  104. sizeof(struct ETHInterface*) * (ctx->ifCount + 1));
  105. ctx->ifaces[ctx->ifCount] = ethIf;
  106. Dict out = Dict_CONST(
  107. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  108. String_CONST("interfaceNumber"), Int_OBJ(ctx->ifCount), NULL
  109. ));
  110. Admin_sendMessage(&out, txid, ctx->admin);
  111. ctx->ifCount++;
  112. }
  113. static void beacon(Dict* args, void* vcontext, String* txid)
  114. {
  115. int64_t* stateP = Dict_getInt(args, String_CONST("state"));
  116. int64_t* ifNumP = Dict_getInt(args, String_CONST("interfaceNumber"));
  117. uint32_t ifNum = (ifNumP) ? ((uint32_t) *ifNumP) : 0;
  118. struct Context* ctx = (struct Context*) vcontext;
  119. char* error = NULL;
  120. if (!isValidIfNum(ctx, ifNumP, &error)) {
  121. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(error)), NULL);
  122. Admin_sendMessage(&out, txid, ctx->admin);
  123. } else {
  124. struct ETHInterface* ethIf = ctx->ifaces[ifNum];
  125. int newState = (stateP) ? *stateP : 0;
  126. int64_t state = ETHInterface_beacon(ethIf, (stateP) ? &newState : NULL);
  127. char* stateStr = "disabled";
  128. if (state == ETHInterface_beacon_ACCEPTING) {
  129. stateStr = "accepting";
  130. } else if (state == ETHInterface_beacon_ACCEPTING_AND_SENDING) {
  131. stateStr = "sending and accepting";
  132. }
  133. Dict out = Dict_CONST(
  134. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  135. String_CONST("state"), Int_OBJ(state), Dict_CONST(
  136. String_CONST("stateName"), String_OBJ(String_CONST(stateStr)), NULL
  137. )));
  138. Admin_sendMessage(&out, txid, ctx->admin);
  139. }
  140. }
  141. void ETHInterface_admin_register(struct EventBase* base,
  142. struct Allocator* allocator,
  143. struct Log* logger,
  144. struct Admin* admin,
  145. struct InterfaceController* ic)
  146. {
  147. struct Context* ctx = Allocator_clone(allocator, (&(struct Context) {
  148. .eventBase = base,
  149. .allocator = allocator,
  150. .logger = logger,
  151. .admin = admin,
  152. .ic = ic
  153. }));
  154. Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
  155. ((struct Admin_FunctionArg[]) {
  156. { .name = "bindDevice", .required = 1, .type = "String" }
  157. }), admin);
  158. Admin_registerFunction("ETHInterface_beginConnection",
  159. beginConnection, ctx, true, ((struct Admin_FunctionArg[]) {
  160. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  161. { .name = "password", .required = 0, .type = "String" },
  162. { .name = "publicKey", .required = 1, .type = "String" },
  163. { .name = "macAddress", .required = 1, .type = "String" }
  164. }), admin);
  165. Admin_registerFunction("ETHInterface_beacon", beacon, ctx, true,
  166. ((struct Admin_FunctionArg[]) {
  167. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  168. { .name = "state", .required = 0, .type = "Int" }
  169. }), admin);
  170. }