protocol_util.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef PROTOCOL_UTIL_H
  15. #define PROTOCOL_UTIL_H
  16. #include <dhcp/pkt4.h>
  17. #include <util/buffer.h>
  18. #include <stdint.h>
  19. namespace isc {
  20. namespace dhcp {
  21. /// @brief Writes ethernet frame header into a buffer.
  22. ///
  23. /// @param src_hw_addr source HW address.
  24. /// @param dst_hw_addr destination HW address.
  25. /// @param [out] out_buf buffer where a header is written.
  26. void writeEthernetHeader(const uint8_t* src_hw_addr,
  27. const uint8_t* dest_hw_addr,
  28. util::OutputBuffer& out_buf);
  29. /// @brief Writes both IP and UDP header into output buffer
  30. ///
  31. /// This utility function assembles IP and UDP packet headers for the
  32. /// provided DHCPv4 message. The source and destination addreses and
  33. /// ports stored in the Pkt4 object are copied as source and destination
  34. /// addresses and ports into IP/UDP headers.
  35. ///
  36. /// @param pkt DHCPv4 packet to be sent in IP packet
  37. /// @param [out] out_buf buffer where an IP header is written
  38. void writeIpUdpHeader(const Pkt4Ptr& pkt, util::OutputBuffer& out_buf);
  39. /// @brief Calculates checksum for provided buffer
  40. ///
  41. /// This function returns the sum of 16-bit values from the provided
  42. /// buffer. If the third parameter is specified, it indicates the
  43. /// initial checksum value. This parameter can be a result of
  44. /// calcChecksum function's invocation on different data buffer.
  45. /// The IP or UDP checksum value is a complement of the result returned
  46. /// by this function. However, this function does not compute complement
  47. /// of the summed values. It must be calculated outside of this function
  48. /// before writing the value to the packet buffer.
  49. ///
  50. /// @param buf buffer for which the checksum is calculated.
  51. /// @param buf_size size of the buffer for which checksum is calculated.
  52. /// @param sum initial checksum value, other values will be added to it.
  53. ///
  54. /// @return calculated checksum.
  55. uint16_t calcChecksum(const uint8_t* buf, const uint32_t buf_size,
  56. uint32_t sum = 0);
  57. }
  58. }
  59. #endif // PROTOCOL_UTIL_H