pkt_transform.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2011 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 __PKT_TRANSFORM_H
  15. #define __PKT_TRANSFORM_H
  16. #include <dhcp/option.h>
  17. #include "localized_option.h"
  18. namespace isc {
  19. namespace perfdhcp {
  20. /// \brief Read and write raw data to DHCP packets.
  21. ///
  22. /// This class provides static functions to read raw
  23. /// data from packet buffer and write raw data to packet
  24. /// buffer. When reading data with unpack() method, the
  25. /// corresponding options objects are updated.
  26. /// When writing to the packet buffer with pack(),
  27. /// options objects carry input data to be written.
  28. /// This class is used both by \ref PerfPkt4 and
  29. /// \ref PerfPkt6 classes in case DHCP packets are created
  30. /// from template files. In this case, some of the template
  31. /// packet's options are replaced before sending it to
  32. /// server. Offset of specific options are provided from
  33. /// command line by perfdhcp tool user and passed in
  34. /// options collection.
  35. class PktTransform {
  36. public:
  37. /// \brief Prepares on-wire format from raw buffer.
  38. ///
  39. /// The method copies input buffer and options contents
  40. /// to output buffer. Input buffer must contain whole
  41. /// initial packet data. Parts of this data will be
  42. /// overriden by options data specified in options
  43. /// collection. Such options must have their offsets within
  44. /// a packet specified (see \ref LocalizedOption to find out
  45. /// how to specify options offset).
  46. ///
  47. /// \note Specified options must fit into size of the
  48. /// initial packet data. Call to this function will fail
  49. /// if option's offset + its size is beyond packet's size.
  50. ///
  51. /// \param universe universe used, V4 or V6
  52. /// \param in_buffer input buffer holiding intial packet
  53. /// data, this can be directly read from template file
  54. /// \param options options collection with offsets
  55. /// \param transid_offset offset of transaction id in a packet,
  56. /// transatcion id will be written to output buffer at this
  57. /// offset
  58. /// \param transid transaction id value
  59. /// \param out_buffer output buffer holding "packed" data
  60. ///
  61. /// \return false, if pack operation failed.
  62. static bool pack(const dhcp::Option::Universe universe,
  63. const dhcp::OptionBuffer& in_buffer,
  64. const dhcp::Option::OptionCollection& options,
  65. const size_t transid_offset,
  66. const uint32_t transid,
  67. util::OutputBuffer& out_buffer);
  68. /// \brief Handles selective binary packet parsing.
  69. ///
  70. /// Function handles parsing of packets that have non-default
  71. /// options or transaction id offsets. Client class has to use
  72. /// \ref isc::dhcp::Pkt6::addOption to specify which options to parse.
  73. /// Each option should be of the \ref isc::perfdhcp::LocalizedOption
  74. /// type with offset value specified.
  75. ///
  76. /// \param universe universe used, V4 or V6
  77. /// \param in_buffer input buffer to be parsed
  78. /// \param options options collection with options offsets
  79. /// \param transid_offset offset of transaction id in input buffer
  80. /// \param transid transaction id value read from input buffer
  81. /// \return false, if unpack operation failed.
  82. static bool unpack(const dhcp::Option::Universe universe,
  83. const dhcp::OptionBuffer& in_buffer,
  84. const dhcp::Option::OptionCollection& options,
  85. const size_t transid_offset,
  86. uint32_t& transid);
  87. private:
  88. /// \brief Replaces contents of options in a buffer.
  89. ///
  90. /// The method uses localized options collection to
  91. /// replace parts of packet data (e.g. data read
  92. /// from template file).
  93. /// This private method is called from \ref PktTransform::pack
  94. ///
  95. /// \param in_buffer input buffer holding initial packet data.
  96. /// \param out_buffer output buffer with "packed" options.
  97. /// \param options options collection with actual data and offsets.
  98. /// \throw isc::Unexpected if options update failed.
  99. static void packOptions(const dhcp::OptionBuffer& in_buffer,
  100. const dhcp::Option::OptionCollection& options,
  101. util::OutputBuffer& out_buffer);
  102. /// \brief Reads contents of specified options from buffer.
  103. ///
  104. /// The method reads options data from the input buffer
  105. /// and stores it in options objects. Offsets of options
  106. /// must be specified.
  107. /// (see \ref LocalizedOption to find out how to specify
  108. /// option offset).
  109. /// This private method is called by \ref PktTransform::unpack.
  110. ///
  111. /// \note This method iterates through all options in
  112. /// options collection, checks offset of the option
  113. /// in input buffer and reads data from the buffer to
  114. /// update option's buffer. If provided options collection
  115. /// is empty, call to this function will have no effect.
  116. ///
  117. /// \param universe universe used, V4 or V6
  118. /// \param in_buffer input buffer to be parsed.
  119. /// \param options oprions collection with their offsets
  120. /// in input buffer specified.
  121. /// \throw isc::Unexpected if options unpack failed.
  122. static void unpackOptions(const dhcp::OptionBuffer& in_buffer,
  123. const dhcp::Option::OptionCollection& options);
  124. };
  125. } // namespace perfdhcp
  126. } // namespace isc
  127. #endif // __PKT_TRANSFORM_H