UDPInterface_admin.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "memory/Allocator.h"
  19. #include "net/InterfaceController.h"
  20. #include "util/events/UDPAddrIface.h"
  21. #include "util/events/EventBase.h"
  22. #include "util/events/FakeNetwork.h"
  23. #include "util/platform/Sockaddr.h"
  24. #include "crypto/Key.h"
  25. struct Context
  26. {
  27. struct EventBase* eventBase;
  28. struct Allocator* alloc;
  29. struct Log* logger;
  30. struct Admin* admin;
  31. struct AddrIface* udpIf;
  32. struct InterfaceController* ic;
  33. struct FakeNetwork* fakeNet;
  34. };
  35. static void beginConnection(Dict* args,
  36. void* vcontext,
  37. String* txid,
  38. struct Allocator* requestAlloc)
  39. {
  40. struct Context* ctx = vcontext;
  41. String* password = Dict_getString(args, String_CONST("password"));
  42. String* login = Dict_getString(args, String_CONST("login"));
  43. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  44. String* address = Dict_getString(args, String_CONST("address"));
  45. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  46. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  47. String* peerName = Dict_getString(args, String_CONST("peerName"));
  48. String* error = NULL;
  49. Log_debug(ctx->logger, "Peering with [%s]", publicKey->bytes);
  50. struct Sockaddr_storage ss;
  51. uint8_t pkBytes[32];
  52. int ret;
  53. if (interfaceNumber && *interfaceNumber < 0) {
  54. error = String_CONST("negative interfaceNumber");
  55. } else if ((ret = Key_parse(publicKey, pkBytes, NULL))) {
  56. error = String_CONST(Key_parse_strerror(ret));
  57. } else if (Sockaddr_parse(address->bytes, &ss)) {
  58. error = String_CONST("unable to parse ip address and port.");
  59. } else if (Sockaddr_getFamily(&ss.addr) != Sockaddr_getFamily(ctx->udpIf->addr)) {
  60. error = String_CONST("different address type than this socket is bound to.");
  61. } else {
  62. struct Sockaddr* addr = &ss.addr;
  63. char* addrPtr = NULL;
  64. int addrLen = Sockaddr_getAddress(&ss.addr, &addrPtr);
  65. Assert_true(addrLen > 0);
  66. struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
  67. if (Bits_isZero(addrPtr, addrLen)) {
  68. // unspec'd address, convert to loopback
  69. if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET) {
  70. addr = Sockaddr_clone(Sockaddr_LOOPBACK, tempAlloc);
  71. } else if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET6) {
  72. addr = Sockaddr_clone(Sockaddr_LOOPBACK6, tempAlloc);
  73. } else {
  74. Assert_failure("Sockaddr which is not AF_INET nor AF_INET6");
  75. }
  76. Sockaddr_setPort(addr, Sockaddr_getPort(&ss.addr));
  77. }
  78. int ret = InterfaceController_bootstrapPeer(
  79. ctx->ic, ifNum, pkBytes, addr, password, login, peerName, ctx->alloc);
  80. Allocator_free(tempAlloc);
  81. if (ret) {
  82. switch(ret) {
  83. case InterfaceController_bootstrapPeer_BAD_IFNUM:
  84. error = String_CONST("no such interface for interfaceNumber");
  85. break;
  86. case InterfaceController_bootstrapPeer_BAD_KEY:
  87. error = String_CONST("invalid cjdns public key.");
  88. break;
  89. case InterfaceController_bootstrapPeer_OUT_OF_SPACE:
  90. error = String_CONST("no more space to register with the switch.");
  91. break;
  92. default:
  93. error = String_CONST("unknown error");
  94. break;
  95. }
  96. } else {
  97. error = String_CONST("none");
  98. }
  99. }
  100. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
  101. Admin_sendMessage(&out, txid, ctx->admin);
  102. }
  103. static struct AddrIface* setupLibuvUDP(struct Context* ctx,
  104. struct Sockaddr* addr,
  105. String* txid,
  106. struct Allocator* alloc)
  107. {
  108. struct UDPAddrIface* udpIf = NULL;
  109. struct Jmp jmp;
  110. Jmp_try(jmp) {
  111. udpIf = UDPAddrIface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger);
  112. } Jmp_catch {
  113. String* errStr = String_CONST(jmp.message);
  114. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  115. Admin_sendMessage(&out, txid, ctx->admin);
  116. Allocator_free(alloc);
  117. return NULL;
  118. }
  119. return &udpIf->generic;
  120. }
  121. static struct AddrIface* setupFakeUDP(struct FakeNetwork* fakeNet,
  122. struct Sockaddr* addr,
  123. struct Allocator* alloc)
  124. {
  125. struct FakeNetwork_UDPIface* fni = FakeNetwork_iface(fakeNet, addr, alloc);
  126. return &fni->generic;
  127. }
  128. static void newInterface2(struct Context* ctx,
  129. struct Sockaddr* addr,
  130. String* txid,
  131. struct Allocator* requestAlloc)
  132. {
  133. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  134. struct AddrIface* ai;
  135. if (ctx->fakeNet) {
  136. ai = setupFakeUDP(ctx->fakeNet, addr, alloc);
  137. } else {
  138. ai = setupLibuvUDP(ctx, addr, txid, alloc);
  139. }
  140. if (!ai) { return; }
  141. ctx->udpIf = ai;
  142. struct InterfaceController_Iface* ici =
  143. InterfaceController_newIface(ctx->ic, String_CONST("UDP"), alloc);
  144. Iface_plumb(&ici->addrIf, &ai->iface);
  145. Dict* out = Dict_new(requestAlloc);
  146. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  147. Dict_putInt(out, String_CONST("interfaceNumber"), ici->ifNum, requestAlloc);
  148. char* printedAddr = Sockaddr_print(ai->addr, requestAlloc);
  149. Dict_putString(out,
  150. String_CONST("bindAddress"),
  151. String_CONST(printedAddr),
  152. requestAlloc);
  153. Admin_sendMessage(out, txid, ctx->admin);
  154. }
  155. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  156. {
  157. struct Context* ctx = vcontext;
  158. String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
  159. struct Sockaddr_storage addr;
  160. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  161. Dict out = Dict_CONST(
  162. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  163. );
  164. Admin_sendMessage(&out, txid, ctx->admin);
  165. return;
  166. }
  167. newInterface2(ctx, &addr.addr, txid, requestAlloc);
  168. }
  169. void UDPInterface_admin_register(struct EventBase* base,
  170. struct Allocator* alloc,
  171. struct Log* logger,
  172. struct Admin* admin,
  173. struct InterfaceController* ic,
  174. struct FakeNetwork* fakeNet)
  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. .fakeNet = fakeNet
  183. }));
  184. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true,
  185. ((struct Admin_FunctionArg[]) {
  186. { .name = "bindAddress", .required = 0, .type = "String" }
  187. }), admin);
  188. Admin_registerFunction("UDPInterface_beginConnection", beginConnection, ctx, true,
  189. ((struct Admin_FunctionArg[]) {
  190. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  191. { .name = "password", .required = 0, .type = "String" },
  192. { .name = "publicKey", .required = 1, .type = "String" },
  193. { .name = "address", .required = 1, .type = "String" },
  194. { .name = "login", .required = 0, .type = "String" }
  195. }), admin);
  196. }