1
0

PacketHeaderToUDPAddrInterface.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/Interface.h"
  16. #include "interface/addressable/PacketHeaderToUDPAddrInterface.h"
  17. #include "memory/Allocator.h"
  18. #include "util/platform/Sockaddr.h"
  19. #include "util/Assert.h"
  20. #include "util/Identity.h"
  21. #include "util/Checksum.h"
  22. #include "wire/Headers.h"
  23. #include "wire/Message.h"
  24. #include "wire/Error.h"
  25. struct PacketHeaderToUDPAddrInterface_pvt
  26. {
  27. struct AddrInterface pub;
  28. struct Interface* wrapped;
  29. Identity
  30. };
  31. static uint8_t sendMessage(struct Message* message, struct Interface* iface)
  32. {
  33. struct PacketHeaderToUDPAddrInterface_pvt* context =
  34. Identity_cast((struct PacketHeaderToUDPAddrInterface_pvt*) iface);
  35. struct Sockaddr_storage ss;
  36. Message_pop(message, &ss, context->pub.addr->addrLen, NULL);
  37. struct Sockaddr* addr = &ss.addr;
  38. struct Headers_UDPHeader udp;
  39. udp.srcPort_be = Endian_hostToBigEndian16(Sockaddr_getPort(context->pub.addr));
  40. udp.destPort_be = Endian_hostToBigEndian16(Sockaddr_getPort(addr));
  41. udp.length_be = Endian_hostToBigEndian16(message->length + Headers_UDPHeader_SIZE);
  42. udp.checksum_be = 0;
  43. Message_push(message, &udp, sizeof(struct Headers_UDPHeader), NULL);
  44. struct Headers_IP6Header ip = {
  45. .nextHeader = 17,
  46. .hopLimit = 255,
  47. };
  48. ip.payloadLength_be = Endian_hostToBigEndian16(message->length);
  49. Headers_setIpVersion(&ip);
  50. uint8_t* addrPtr = NULL;
  51. Assert_true(Sockaddr_getAddress(addr, &addrPtr) == 16);
  52. Bits_memcpyConst(ip.destinationAddr, addrPtr, 16);
  53. Assert_true(Sockaddr_getAddress(context->pub.addr, &addrPtr) == 16);
  54. Bits_memcpyConst(ip.sourceAddr, addrPtr, 16);
  55. uint16_t checksum = Checksum_udpIp6(ip.sourceAddr, message->bytes, message->length);
  56. ((struct Headers_UDPHeader*)message->bytes)->checksum_be = checksum;
  57. Message_push(message, &ip, sizeof(struct Headers_IP6Header), NULL);
  58. return Interface_sendMessage(context->wrapped, message);
  59. }
  60. static uint8_t receiveMessage(struct Message* message, struct Interface* iface)
  61. {
  62. struct PacketHeaderToUDPAddrInterface_pvt* context =
  63. Identity_cast((struct PacketHeaderToUDPAddrInterface_pvt*) iface->receiverContext);
  64. if (message->length < Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE) {
  65. // runt
  66. return Error_NONE;
  67. }
  68. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  69. // udp
  70. if (ip->nextHeader != 17) {
  71. return Error_NONE;
  72. }
  73. struct Allocator* alloc = Allocator_child(message->alloc);
  74. struct Sockaddr* addr = Sockaddr_clone(context->pub.addr, alloc);
  75. uint8_t* addrPtr = NULL;
  76. Assert_true(Sockaddr_getAddress(addr, &addrPtr) == 16);
  77. Bits_memcpyConst(addrPtr, ip->sourceAddr, 16);
  78. struct Headers_UDPHeader* udp = (struct Headers_UDPHeader*) (&ip[1]);
  79. Sockaddr_setPort(addr, Endian_bigEndianToHost16(udp->srcPort_be));
  80. if (Sockaddr_getPort(context->pub.addr) != Endian_bigEndianToHost16(udp->destPort_be)) {
  81. // not the right port
  82. return Error_NONE;
  83. }
  84. Message_shift(message, -(Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE), NULL);
  85. Message_push(message, addr, addr->addrLen, NULL);
  86. return Interface_receiveMessage(&context->pub.generic, message);
  87. }
  88. struct AddrInterface* PacketHeaderToUDPAddrInterface_new(struct Interface* toWrap,
  89. struct Allocator* alloc,
  90. struct Sockaddr* addr)
  91. {
  92. struct PacketHeaderToUDPAddrInterface_pvt* context =
  93. Allocator_malloc(alloc, sizeof(struct PacketHeaderToUDPAddrInterface_pvt));
  94. Bits_memcpyConst(context, (&(struct PacketHeaderToUDPAddrInterface_pvt) {
  95. .pub = {
  96. .generic = {
  97. .sendMessage = sendMessage,
  98. .senderContext = context,
  99. .allocator = alloc
  100. }
  101. },
  102. .wrapped = toWrap
  103. }), sizeof(struct PacketHeaderToUDPAddrInterface_pvt));
  104. Identity_set(context);
  105. context->pub.addr = Sockaddr_clone(addr, alloc);
  106. toWrap->receiveMessage = receiveMessage;
  107. toWrap->receiverContext = context;
  108. return &context->pub;
  109. }