DHCPIpUdpEncoder.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file DHCPIpUdpEncoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <limits.h>
  30. #include <misc/ipv4_proto.h>
  31. #include <misc/udp_proto.h>
  32. #include <misc/byteorder.h>
  33. #include <dhcpclient/DHCPIpUdpEncoder.h>
  34. #define DHCP_SERVER_PORT 67
  35. #define DHCP_CLIENT_PORT 68
  36. struct combined_header {
  37. struct ipv4_header ip;
  38. struct udp_header udp;
  39. } __attribute__((packed));
  40. static void output_handler_recv (DHCPIpUdpEncoder *o, uint8_t *data)
  41. {
  42. DebugObject_Access(&o->d_obj);
  43. // remember output packet
  44. o->data = data;
  45. // receive payload
  46. PacketRecvInterface_Receiver_Recv(o->input, o->data + sizeof(struct combined_header));
  47. }
  48. static void input_handler_done (DHCPIpUdpEncoder *o, int data_len)
  49. {
  50. DebugObject_Access(&o->d_obj);
  51. struct combined_header *header = (void *)o->data;
  52. // write IP header
  53. struct ipv4_header *iph = &header->ip;
  54. iph->version4_ihl4 = IPV4_MAKE_VERSION_IHL(sizeof(*iph));
  55. iph->ds = hton8(0);
  56. iph->total_length = hton16(sizeof(struct combined_header) + data_len);
  57. iph->identification = hton16(0);
  58. iph->flags3_fragmentoffset13 = hton16(0);
  59. iph->ttl = hton8(64);
  60. iph->protocol = hton8(IPV4_PROTOCOL_UDP);
  61. iph->checksum = hton16(0);
  62. iph->source_address = hton32(0x00000000);
  63. iph->destination_address = hton32(0xFFFFFFFF);
  64. // compute and write IP header checksum
  65. uint32_t checksum = ipv4_checksum((uint8_t *)iph, sizeof(*iph));
  66. iph->checksum = checksum;
  67. // write UDP header
  68. struct udp_header *udph = &header->udp;
  69. udph->source_port = hton16(DHCP_CLIENT_PORT);
  70. udph->dest_port = hton16(DHCP_SERVER_PORT);
  71. udph->length = hton16(sizeof(*udph) + data_len);
  72. udph->checksum = hton16(0);
  73. // compute checksum
  74. checksum = udp_checksum((uint8_t *)udph, sizeof(*udph) + data_len, iph->source_address, iph->destination_address);
  75. udph->checksum = checksum;
  76. // finish packet
  77. PacketRecvInterface_Done(&o->output, sizeof(struct combined_header) + data_len);
  78. }
  79. void DHCPIpUdpEncoder_Init (DHCPIpUdpEncoder *o, PacketRecvInterface *input, BPendingGroup *pg)
  80. {
  81. ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - sizeof(struct combined_header))
  82. // init arguments
  83. o->input = input;
  84. // init input
  85. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  86. // init output
  87. PacketRecvInterface_Init(&o->output, sizeof(struct combined_header) + PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  88. DebugObject_Init(&o->d_obj);
  89. }
  90. void DHCPIpUdpEncoder_Free (DHCPIpUdpEncoder *o)
  91. {
  92. DebugObject_Free(&o->d_obj);
  93. // free output
  94. PacketRecvInterface_Free(&o->output);
  95. }
  96. PacketRecvInterface * DHCPIpUdpEncoder_GetOutput (DHCPIpUdpEncoder *o)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. return &o->output;
  100. }