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 NULL;
  38. }
  39. // get ess packet type
  40. uint8_t type = Message_pop8(msg, NULL);
  41. Log_debug(ctx->logger, "Packet type [%d]", type);
  42. if (type == SocketWrapper_TYPE_TUN_PACKET) {
  43. // skip tun packet length
  44. Message_pop32(msg, NULL);
  45. return Iface_next(&ctx->pub.internalIf, msg);
  46. }
  47. // skip all other types
  48. return NULL;
  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 NULL;
  57. }
  58. // send payload length
  59. Message_push32(msg, msg->length, NULL);
  60. // mark this as a normal tun packet
  61. Message_push8(msg, SocketWrapper_TYPE_TUN_PACKET, NULL);
  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. void SocketWrapper_addAddress(struct Iface* rawSocketIf,
  75. uint8_t* ipv6Addr,
  76. struct Log* logger,
  77. struct Except* eh,
  78. struct Allocator* alloc)
  79. {
  80. size_t len = 16 /* IPv6 Address length */ + 1 /* Type prefix length */;
  81. struct Message* out = Message_new(0, len, alloc);
  82. Message_push(out, ipv6Addr, 16, eh);
  83. Message_push8(out, SocketWrapper_TYPE_CONF_ADD_IPV6_ADDRESS, eh);
  84. Iface_send(rawSocketIf, out);
  85. }
  86. void SocketWrapper_setMTU(struct Iface* rawSocketIf,
  87. uint32_t mtu,
  88. struct Log* logger,
  89. struct Except* eh,
  90. struct Allocator* alloc)
  91. {
  92. size_t len = 4 /* MTU var size */ + 1 /* Type prefix length */;
  93. struct Message* out = Message_new(0, len, alloc);
  94. Message_push32(out, mtu, eh);
  95. Message_push8(out, SocketWrapper_TYPE_CONF_SET_MTU, eh);
  96. Iface_send(rawSocketIf, out);
  97. }