BSDMessageTypeWrapper.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "interface/tuntap/BSDMessageTypeWrapper.h"
  16. #include "interface/Interface.h"
  17. #include "interface/InterfaceWrapper.h"
  18. #include "util/platform/Sockaddr.h"
  19. #include "memory/Allocator.h"
  20. #include "util/Assert.h"
  21. #include "util/Identity.h"
  22. #include "wire/Ethernet.h"
  23. #include "wire/Message.h"
  24. #include "wire/Error.h"
  25. /**
  26. * OSX and BSD is expect you to send the platform dependent address family type
  27. * rather than the ethertype, this InterfaceWrapper converts AF_INET and AF_INET6 to the
  28. * corrisponding ethertypes and back.
  29. */
  30. struct BSDMessageTypeWrapper_pvt
  31. {
  32. struct Interface generic;
  33. struct Interface* const wrapped;
  34. const uint16_t afInet_be;
  35. const uint16_t afInet6_be;
  36. struct Log* const logger;
  37. Identity
  38. };
  39. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  40. {
  41. struct BSDMessageTypeWrapper_pvt* ctx =
  42. Identity_cast((struct BSDMessageTypeWrapper_pvt*)iface->receiverContext);
  43. if (msg->length < 4) {
  44. return Error_NONE;
  45. }
  46. uint16_t afType_be = ((uint16_t*) msg->bytes)[1];
  47. uint16_t ethertype = 0;
  48. if (afType_be == ctx->afInet_be) {
  49. ethertype = Ethernet_TYPE_IP4;
  50. } else if (afType_be == ctx->afInet6_be) {
  51. ethertype = Ethernet_TYPE_IP6;
  52. } else {
  53. Log_debug(ctx->logger, "Message of unhandled aftype [0x%04x]",
  54. Endian_bigEndianToHost16(afType_be));
  55. return Error_NONE;
  56. }
  57. ((uint16_t*) msg->bytes)[0] = 0;
  58. ((uint16_t*) msg->bytes)[1] = ethertype;
  59. return Interface_receiveMessage(&ctx->generic, msg);
  60. }
  61. static uint8_t sendMessage(struct Message* msg, struct Interface* iface)
  62. {
  63. struct BSDMessageTypeWrapper_pvt* ctx = Identity_cast((struct BSDMessageTypeWrapper_pvt*)iface);
  64. Assert_true(msg->length >= 4);
  65. uint16_t ethertype = ((uint16_t*) msg->bytes)[1];
  66. uint16_t afType_be = 0;
  67. if (ethertype == Ethernet_TYPE_IP6) {
  68. afType_be = ctx->afInet6_be;
  69. } else if (ethertype == Ethernet_TYPE_IP4) {
  70. afType_be = ctx->afInet_be;
  71. } else {
  72. Assert_always(!"Unsupported ethertype");
  73. }
  74. ((uint16_t*) msg->bytes)[0] = 0;
  75. ((uint16_t*) msg->bytes)[1] = afType_be;
  76. return Interface_sendMessage(ctx->wrapped, msg);
  77. }
  78. struct Interface* BSDMessageTypeWrapper_new(struct Interface* wrapped, struct Log* logger)
  79. {
  80. struct BSDMessageTypeWrapper_pvt* context =
  81. Allocator_clone(wrapped->allocator, (&(struct BSDMessageTypeWrapper_pvt) {
  82. .wrapped = wrapped,
  83. .afInet6_be = Endian_hostToBigEndian16(Sockaddr_AF_INET6),
  84. .afInet_be = Endian_hostToBigEndian16(Sockaddr_AF_INET),
  85. .logger = logger
  86. }));
  87. Identity_set(context);
  88. InterfaceWrapper_wrap(wrapped, sendMessage, receiveMessage, &context->generic);
  89. return &context->generic;
  90. }