DHCPIpUdpEncoder.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @file DHCPIpUdpEncoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <limits.h>
  23. #include <misc/ipv4_proto.h>
  24. #include <misc/udp_proto.h>
  25. #include <misc/byteorder.h>
  26. #include <dhcpclient/DHCPIpUdpEncoder.h>
  27. #define DHCP_SERVER_PORT 67
  28. #define DHCP_CLIENT_PORT 68
  29. struct combined_header {
  30. struct ipv4_header ip;
  31. struct udp_header udp;
  32. } __attribute__((packed));
  33. static void output_handler_recv (DHCPIpUdpEncoder *o, uint8_t *data)
  34. {
  35. DebugObject_Access(&o->d_obj);
  36. // remember output packet
  37. o->data = data;
  38. // receive payload
  39. PacketRecvInterface_Receiver_Recv(o->input, o->data + sizeof(struct combined_header));
  40. }
  41. static void input_handler_done (DHCPIpUdpEncoder *o, int data_len)
  42. {
  43. DebugObject_Access(&o->d_obj);
  44. struct combined_header *header = (void *)o->data;
  45. // write IP header
  46. struct ipv4_header *iph = &header->ip;
  47. iph->version4_ihl4 = IPV4_MAKE_VERSION_IHL(sizeof(*iph));
  48. iph->ds = hton8(0);
  49. iph->total_length = hton16(sizeof(struct combined_header) + data_len);
  50. iph->identification = hton16(0);
  51. iph->flags3_fragmentoffset13 = hton16(0);
  52. iph->ttl = hton8(64);
  53. iph->protocol = hton8(IPV4_PROTOCOL_UDP);
  54. iph->checksum = hton16(0);
  55. iph->source_address = hton32(0x00000000);
  56. iph->destination_address = hton32(0xFFFFFFFF);
  57. // compute and write IP header checksum
  58. uint32_t checksum = ipv4_checksum((uint8_t *)iph, sizeof(*iph));
  59. iph->checksum = checksum;
  60. // write UDP header
  61. struct udp_header *udph = &header->udp;
  62. udph->source_port = hton16(DHCP_CLIENT_PORT);
  63. udph->dest_port = hton16(DHCP_SERVER_PORT);
  64. udph->length = hton16(sizeof(*udph) + data_len);
  65. udph->checksum = hton16(0);
  66. // finish packet
  67. PacketRecvInterface_Done(&o->output, sizeof(struct combined_header) + data_len);
  68. }
  69. void DHCPIpUdpEncoder_Init (DHCPIpUdpEncoder *o, PacketRecvInterface *input, BPendingGroup *pg)
  70. {
  71. ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - sizeof(struct combined_header))
  72. // init arguments
  73. o->input = input;
  74. // init input
  75. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  76. // init output
  77. PacketRecvInterface_Init(&o->output, sizeof(struct combined_header) + PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  78. DebugObject_Init(&o->d_obj);
  79. }
  80. void DHCPIpUdpEncoder_Free (DHCPIpUdpEncoder *o)
  81. {
  82. DebugObject_Free(&o->d_obj);
  83. // free output
  84. PacketRecvInterface_Free(&o->output);
  85. }
  86. PacketRecvInterface * DHCPIpUdpEncoder_GetOutput (DHCPIpUdpEncoder *o)
  87. {
  88. DebugObject_Access(&o->d_obj);
  89. return &o->output;
  90. }