UDPInterface_admin.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 struct AddrIface* setupLibuvUDP(struct Context* ctx,
  36. struct Sockaddr* addr,
  37. String* txid,
  38. struct Allocator* alloc)
  39. {
  40. struct UDPAddrIface* udpIf = NULL;
  41. struct Jmp jmp;
  42. Jmp_try(jmp) {
  43. udpIf = UDPAddrIface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger);
  44. } Jmp_catch {
  45. String* errStr = String_CONST(jmp.message);
  46. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  47. Admin_sendMessage(&out, txid, ctx->admin);
  48. Allocator_free(alloc);
  49. return NULL;
  50. }
  51. return &udpIf->generic;
  52. }
  53. static struct AddrIface* setupFakeUDP(struct FakeNetwork* fakeNet,
  54. struct Sockaddr* addr,
  55. struct Allocator* alloc)
  56. {
  57. struct FakeNetwork_UDPIface* fni = FakeNetwork_iface(fakeNet, addr, alloc);
  58. return &fni->generic;
  59. }
  60. static void newInterface2(struct Context* ctx,
  61. struct Sockaddr* addr,
  62. String* txid,
  63. struct Allocator* requestAlloc)
  64. {
  65. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  66. struct AddrIface* ai;
  67. if (ctx->fakeNet) {
  68. ai = setupFakeUDP(ctx->fakeNet, addr, alloc);
  69. } else {
  70. ai = setupLibuvUDP(ctx, addr, txid, alloc);
  71. }
  72. if (!ai) { return; }
  73. ctx->udpIf = ai;
  74. char* printedAddr = Sockaddr_print(ai->addr, requestAlloc);
  75. String* ifName = String_printf(alloc, "UDP/%s", printedAddr);
  76. Dict* out = Dict_new(requestAlloc);
  77. char* err;
  78. struct InterfaceController_Iface* ici = NULL;
  79. int ret = InterfaceController_newIface(ctx->ic, ifName, false, alloc, &ici);
  80. if (!ret) {
  81. Iface_plumb(&ici->addrIf, &ai->iface);
  82. Dict_putString(out,
  83. String_CONST("bindAddress"),
  84. String_CONST(printedAddr),
  85. requestAlloc);
  86. Dict_putString(out, String_CONST("ifName"), ifName, requestAlloc);
  87. err = "none";
  88. } else if (ret == InterfaceController_newIface_NAME_EXISTS) {
  89. err = "InterfaceController_newIface() -> name_exists";
  90. } else {
  91. err = "unknown";
  92. }
  93. Dict_putString(out, String_CONST("error"), String_CONST(err), requestAlloc);
  94. Admin_sendMessage(out, txid, ctx->admin);
  95. }
  96. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  97. {
  98. struct Context* ctx = vcontext;
  99. String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
  100. struct Sockaddr_storage addr;
  101. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  102. Dict out = Dict_CONST(
  103. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  104. );
  105. Admin_sendMessage(&out, txid, ctx->admin);
  106. return;
  107. }
  108. newInterface2(ctx, &addr.addr, txid, requestAlloc);
  109. }
  110. void UDPInterface_admin_register(struct EventBase* base,
  111. struct Allocator* alloc,
  112. struct Log* logger,
  113. struct Admin* admin,
  114. struct InterfaceController* ic,
  115. struct FakeNetwork* fakeNet)
  116. {
  117. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  118. .eventBase = base,
  119. .alloc = alloc,
  120. .logger = logger,
  121. .admin = admin,
  122. .ic = ic,
  123. .fakeNet = fakeNet
  124. }));
  125. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true,
  126. ((struct Admin_FunctionArg[]) {
  127. { .name = "bindAddress", .required = 0, .type = "String" }
  128. }), admin);
  129. }