perf_pkt6.h 9.5 KB

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