SocketWrapper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/Iface.h"
  16. #include "interface/tuntap/SocketWrapper.h"
  17. #include "util/platform/Sockaddr.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Assert.h"
  20. #include "util/Identity.h"
  21. #include "wire/Ethernet.h"
  22. #include "wire/Headers.h"
  23. #include "wire/Message.h"
  24. #include "wire/Error.h"
  25. struct SocketWrapper_pvt
  26. {
  27. struct SocketWrapper pub;
  28. struct Log* logger;
  29. Identity
  30. };
  31. static Iface_DEFUN incomingFromSocket(struct Message* msg, struct Iface* externalIf)
  32. {
  33. struct SocketWrapper_pvt* ctx =
  34. Identity_containerOf(externalIf, struct SocketWrapper_pvt, pub.externalIf);
  35. if (!ctx->pub.internalIf.connectedIf) {
  36. Log_debug(ctx->logger, "DROP message for socket not inited");
  37. return Error(msg, "INVALID");
  38. }
  39. // get ess packet type
  40. uint8_t type = Er_assert(Message_epop8h(msg));
  41. Log_debug(ctx->logger, "Packet type [%d]", type);
  42. if (type == SocketWrapper_TYPE_TUN_PACKET) {
  43. // skip tun packet length
  44. Er_assert(Message_epop32be(msg));
  45. return Iface_next(&ctx->pub.internalIf, msg);
  46. }
  47. // skip all other types
  48. return Error(msg, "INVALID");
  49. }
  50. static Iface_DEFUN incomingFromUs(struct Message* msg, struct Iface* internalIf)
  51. {
  52. struct SocketWrapper_pvt* ctx =
  53. Identity_containerOf(internalIf, struct SocketWrapper_pvt, pub.internalIf);
  54. if (!ctx->pub.externalIf.connectedIf) {
  55. Log_debug(ctx->logger, "DROP message for socket not inited");
  56. return Error(msg, "INVALID");
  57. }
  58. // send payload length
  59. Er_assert(Message_epush32be(msg, Message_getLength(msg)));
  60. // mark this as a normal tun packet
  61. Er_assert(Message_epush8(msg, SocketWrapper_TYPE_TUN_PACKET));
  62. return Iface_next(&ctx->pub.externalIf, msg);
  63. }
  64. struct SocketWrapper* SocketWrapper_new(struct Allocator* alloc, struct Log* log)
  65. {
  66. struct SocketWrapper_pvt* context =
  67. Allocator_calloc(alloc, sizeof(struct SocketWrapper_pvt), 1);
  68. Identity_set(context);
  69. context->pub.externalIf.send = incomingFromSocket;
  70. context->pub.internalIf.send = incomingFromUs;
  71. context->logger = log;
  72. return &context->pub;
  73. }
  74. Er_DEFUN(void SocketWrapper_addAddress(struct Iface* rawSocketIf,
  75. uint8_t* ipv6Addr,
  76. struct Log* logger,
  77. struct Allocator* alloc))
  78. {
  79. size_t len = 16 /* IPv6 Address length */ + 1 /* Type prefix length */;
  80. struct Message* out = Message_new(0, len, alloc);
  81. Er(Message_epush(out, ipv6Addr, 16));
  82. Er(Message_epush8(out, SocketWrapper_TYPE_CONF_ADD_IPV6_ADDRESS));
  83. Iface_send(rawSocketIf, out);
  84. Er_ret();
  85. }
  86. Er_DEFUN(void SocketWrapper_setMTU(struct Iface* rawSocketIf,
  87. uint32_t mtu,
  88. struct Log* logger,
  89. struct Allocator* alloc))
  90. {
  91. size_t len = 4 /* MTU var size */ + 1 /* Type prefix length */;
  92. struct Message* out = Message_new(0, len, alloc);
  93. Er(Message_epush32be(out, mtu));
  94. Er(Message_epush8(out, SocketWrapper_TYPE_CONF_SET_MTU));
  95. Iface_send(rawSocketIf, out);
  96. Er_ret();
  97. }