1
0

ETHInterface_admin.c 7.3 KB

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