perf_pkt6.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 __PERF_PKT6_H
  15. #define __PERF_PKT6_H
  16. #include <time.h>
  17. #include <dhcp/pkt6.h>
  18. namespace isc {
  19. namespace perfdhcp {
  20. /// \brief PerfPkt6 (DHCPv6 packet)
  21. ///
  22. /// This class extends functionality of \ref isc::dhcp::Pkt6 by
  23. /// adding ability to specify options offset in DHCP message
  24. /// and override options' contents with new option.
  25. /// This approach is useful when we create paket object from
  26. /// raw template buffer from file and we want to use it as
  27. /// a base to create test packets to be sent to DHCP server.
  28. ///
  29. /// Some of the contents of such a template packets always
  30. /// have to be replaced e.g. transaction id, IA_NA. Other
  31. /// contents (options) may be changed e.g. elapsed time,
  32. /// server id.
  33. ///
  34. /// In order to create packet from raw template buffer
  35. /// we have to pass this buffer along with transaction id
  36. /// offset. Class will read transaction id from the buffer.
  37. /// Next, in order to replace contents of selected options
  38. /// in a template packet, we need to add these selected options
  39. /// to packet object using addOption() method. Please note
  40. /// that options must be of the
  41. /// \ref isc::perfdhcp::PerfPkt6::PositionedOption type.
  42. ///
  43. /// This class also records timestamps of last pack/unpack
  44. /// operation on the packet. This is to track DHCP server
  45. /// performance based on packet's send/receive duration.
  46. ///
  47. /// \note: if you don't use template files simply use constructors
  48. /// inherited from parent class and isc::dhcp::Option type instead
  49. ///
  50. class PerfPkt6 : public dhcp::Pkt6 {
  51. public:
  52. /// \brief Class represents position of DHCP option in a packet
  53. class OptionPosition {
  54. public:
  55. /// \brief Class constructor
  56. ///
  57. /// Applies default value of option position
  58. explicit OptionPosition() : position_(0) { };
  59. /// \brief Class constructor
  60. ///
  61. /// \param position position of option in DHCP packet
  62. explicit OptionPosition(size_t position) : position_(position) { };
  63. /// \brief Returns position of DHCP option in a packet
  64. ///
  65. /// \return position of DHCP option in packet
  66. size_t get() const { return position_; }
  67. private:
  68. size_t position_; ///< position of option in a packet
  69. ///< Zero is default position
  70. };
  71. /// \brief DHCPv6 option at specific offset
  72. ///
  73. /// This class represents DHCPv6 option at specified
  74. /// offset in DHCPv6 message.
  75. ///
  76. class PositionedOption : public dhcp::Option {
  77. public:
  78. /// \brief Constructor, used to create positioned option from buffer
  79. ///
  80. ///
  81. /// \param u specifies universe (V4 or V6)
  82. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  83. /// \param data content of the option
  84. /// \param position absolute position of option in a packet (zero is default)
  85. PositionedOption(dhcp::Option::Universe u, uint16_t type, const dhcp::OptionBuffer& data,
  86. const OptionPosition& position);
  87. /// \brief Constructor, used to create positioned option from buffer iterators
  88. ///
  89. /// This contructor is similar to the previous one, but it does not take
  90. /// the whole vector<uint8_t>, but rather subset of it.
  91. ///
  92. /// \param u specifies universe (V4 or V6)
  93. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  94. /// \param first iterator to the first element that should be copied
  95. /// \param last iterator to the next element after the last one
  96. /// to be copied.
  97. /// \param position absolute position of option in a packet (zero is default)
  98. PositionedOption(dhcp::Option::Universe u, uint16_t type, dhcp::OptionBufferConstIter first,
  99. dhcp::OptionBufferConstIter last, const OptionPosition& position);
  100. /// \brief Returns absolute position (offset) of an option in a
  101. /// DHCPv6 packet.
  102. ///
  103. /// \return absolute position (offset) of an option in a packet
  104. size_t getOptionPosition() const { return position_.get(); };
  105. private:
  106. OptionPosition position_; ///< Absolute position of DHCPv6 option in a packet
  107. };
  108. /// Constructor, used in message transmission
  109. ///
  110. /// Creates new DHCPv6 message using provided buffer. New object
  111. /// will keep copy of contents of provided buffer. If buffer contains
  112. /// options at custom offsets (e.g. if packet was read from
  113. /// template file) additional information about options'
  114. /// offsets has to be provided - see
  115. /// \ref isc::perfdhcp::PositionedOption for details.
  116. ///
  117. /// Transaction id is not considered DHCP option so
  118. /// we pass it to constructor as extra argument. This is
  119. /// required if transaction id offset differs from the
  120. /// default one for DHCPv6 messages (ocets 2-4).
  121. ///
  122. /// \note use this constructor only in case you want to create
  123. /// DHCPv6 message (incoming or outgoing) from the raw buffer
  124. /// and you know options offsets. Options offsets are
  125. /// specified from perfdhcp command line by the user.
  126. ///
  127. /// \param buf pointer to a buffer of received packet content
  128. /// \param len size of buffer of packet content
  129. /// \param xid_off transaction id offset in a packet
  130. PerfPkt6(const uint8_t* buf,
  131. uint32_t len,
  132. const OptionPosition& transid_offset);
  133. /// \brief Prepare on-wire packet format and record timestamp
  134. ///
  135. /// Prepares on-wire format of packet and all its options.
  136. /// This method wraps dhcp::Pkt6::pack() function to
  137. /// update packet timestamp.
  138. ///
  139. /// \note Use this function if you don't use template files
  140. /// to construct DHCPv6 packets.
  141. ///
  142. /// \throw isc::Unexpected if pack and timestamp update failed
  143. void stampedPack();
  144. /// \brief Handles binary packet parsing and updates timestamp
  145. ///
  146. /// This method wraps dhcp::Pkt6::unpack() function to
  147. /// update packet timestamp.
  148. ///
  149. /// \note Use this function if you don't use template files
  150. /// and custom options offsets to construct DHCPv6 packets.
  151. ///
  152. /// \throw isc::Unexpected if function failed
  153. void stampedUnpack();
  154. /// \brief Prepares on-wire format from raw buffer
  155. ///
  156. /// The method copies user buffer to output buffer and
  157. /// extracts transaction id from it based on transaction id
  158. /// offset provided in constructor.
  159. /// Eventually, this method updates packet timestamp.
  160. ///
  161. /// \note: Use this method to prepare on-wire DHCPv6 message
  162. /// when you use template packets that require replacement
  163. /// of selected options contents before sending.
  164. ///
  165. /// \throw isc::Unexepected if function failed
  166. void stampedRawPack();
  167. /// \brief Handles limited binary packet parsing for packets with
  168. /// custom offsets of options and transaction id
  169. ///
  170. /// Function handles reception of packets that have non-default values
  171. /// of options or transaction id offsets. Use
  172. /// \ref isc::dhcp::Pkt6::addOption to specify which options to parse.
  173. /// Each option should be of the: isc::perfdhcp::PerfPkt6::PositionedOption
  174. /// type with offset value indicated.
  175. ///
  176. /// \throw isc::Unexpected if function failed
  177. void stampedRawUnpack();
  178. private:
  179. /// \brief Updates options in the output buffer
  180. ///
  181. /// This method updates options in the output buffer
  182. /// with the ones provided with
  183. /// \ref isc::dhcp::Pkt6::addOption. It is expected
  184. /// that these options will be of the
  185. /// \ref isc::perfdhcp::PerfPkt6::PositionedOption type
  186. /// with their position (offset) specified.
  187. ///
  188. /// throw isc::Unexpected if options update fails
  189. void updateOptions();
  190. /// \brief Update packet timestamp with current time
  191. ///
  192. /// \throw isc::Unexpected if timestamp update failed
  193. void updateTimestamp();
  194. OptionPosition transid_offset_; ///< transaction id offset
  195. timespec time_stamp_; ///< time stamp of last pack or unpack
  196. };
  197. } // namespace perfdhcp
  198. } // namespace isc
  199. #endif // __PERF_PKT6_H