perf_pkt6.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "localized_option.h"
  20. namespace isc {
  21. namespace perfdhcp {
  22. /// \brief PerfPkt6 (DHCPv6 packet)
  23. ///
  24. /// This class extends functionality of \ref isc::dhcp::Pkt6 by
  25. /// adding ability to specify options offset in DHCP message
  26. /// and override options' contents with new option.
  27. /// This approach is useful when we create paket object from
  28. /// raw template buffer from file and we want to use it as
  29. /// a base to create test packets to be sent to DHCP server.
  30. ///
  31. /// Some of the contents of such a template packets always
  32. /// have to be replaced e.g. transaction id, IA_NA. Other
  33. /// contents (options) may be changed e.g. elapsed time,
  34. /// server id.
  35. ///
  36. /// In order to create packet from raw template buffer
  37. /// we have to pass this buffer along with transaction id
  38. /// offset. Class will read transaction id from the buffer.
  39. /// Next, in order to replace contents of selected options
  40. /// in a template packet, we need to add these selected options
  41. /// to packet object using addOption() method. Please note
  42. /// that options must be of the
  43. /// \ref isc::perfdhcp::LocalizedOption type.
  44. ///
  45. /// \note: if you don't use template files simply use constructors
  46. /// inherited from parent class and isc::dhcp::Option type instead
  47. ///
  48. class PerfPkt6 : public dhcp::Pkt6 {
  49. public:
  50. /// \brief Represents offset value.
  51. ///
  52. /// This class represent offsets for DHCP message fields
  53. /// like transaction id. Constructors of PerfPkt6 take
  54. /// number of arguments of integer type so it is easy to
  55. /// mess up arguments of constructors and for example
  56. /// swap transaction id with its offset.
  57. /// Use of this class implies that client class has to
  58. /// explicitely use constructor of this class to pass
  59. /// offset value. This should prevent mistakes and save some
  60. /// time on debugging.
  61. class Offset {
  62. public:
  63. /// \brief Default constructor
  64. explicit Offset() :
  65. offset_(1) { };
  66. /// \brief Constructor
  67. ///
  68. /// \param offset offset value
  69. explicit Offset(size_t offset) :
  70. offset_(offset) { };
  71. /// \brief Returns offset value.
  72. ///
  73. /// \return offset value.
  74. size_t get() const { return offset_; };
  75. private:
  76. size_t offset_; ///< offset value
  77. };
  78. /// Localized option pointer type.
  79. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  80. /// \brief Constructor, used for outgoing DHCP messages.
  81. ///
  82. /// Creates new DHCPv6 message using provided buffer.
  83. /// Transaction id and its offset are specified through this
  84. /// constructor so as they are stored in outgoing message
  85. /// when client class calls \ref PerfPkt6::rawPack.
  86. ///
  87. /// \note This constructor should be used only for outgoing
  88. /// messages that are created from raw buffer (e.g. read from
  89. /// template files).
  90. ///
  91. /// \param buf buffer holiding contents of the message (this can
  92. /// be directly read from template file).
  93. /// \param len length of the data in the buffer.
  94. /// \param transid transaction id to be stored in outgoing message.
  95. /// \param transid_offset transaction id offset in outgoing message.
  96. PerfPkt6(const uint8_t* buf,
  97. uint32_t len,
  98. uint32_t transid,
  99. const Offset& transid_offset);
  100. /// Constructor, used for incoming DHCP messages.
  101. ///
  102. /// Creates new DHCPv6 message using provided buffer. New object
  103. /// will keep copy of contents of provided buffer. If buffer contains
  104. /// options at custom offsets (e.g. if packet was read from
  105. /// template file) additional information about options'
  106. /// offsets has to be provided - see
  107. /// \ref isc::perfdhcp::LocalizedOption for details.
  108. ///
  109. /// Transaction id offset point to location of raw data where
  110. /// transaction id field is stored. The transaction id will
  111. /// be read from this location when PerfPkt6::rawUnpack is
  112. /// called. The transid_ class member will be updated accordingly.
  113. ///
  114. /// \note use this constructor only in case you want to create
  115. /// incoming DHCPv6 object from the raw buffer
  116. /// and you know options offsets. Options offsets are
  117. /// specified from perfdhcp command line by the user.
  118. ///
  119. /// \param buf pointer to a buffer of received packet content.
  120. /// \param len size of buffer of packet content.
  121. /// \param transid_offset transaction id offset in a message.
  122. PerfPkt6(const uint8_t* buf,
  123. uint32_t len,
  124. const Offset& transid_offset);
  125. /// \brief Returns transaction id offset in packet buffer
  126. ///
  127. /// return transaction id offset in packet buffer
  128. size_t getTransIdOffset() const { return transid_offset_; };
  129. /// \brief Prepares on-wire format from raw buffer
  130. ///
  131. /// The method copies user buffer to output buffer and
  132. /// extracts transaction id from it based on transaction id
  133. /// offset provided in constructor.
  134. ///
  135. /// \note: Use this method to prepare on-wire DHCPv6 message
  136. /// when you use template packets that require replacement
  137. /// of selected options' contents before sending.
  138. ///
  139. /// \return false, id pack operation failed.
  140. bool rawPack();
  141. /// \brief Handles limited binary packet parsing for packets with
  142. /// custom offsets of options and transaction id
  143. ///
  144. /// Function handles reception of packets that have non-default values
  145. /// of options or transaction id offsets. Use
  146. /// \ref isc::dhcp::Pkt6::addOption to specify which options to parse.
  147. /// Each option should be of the: isc::perfdhcp::LocalizedOption
  148. /// type with offset value indicated.
  149. ///
  150. /// \return false, if unpack operation failed.
  151. bool rawUnpack();
  152. /// \brief Update packet timestamp with current time
  153. ///
  154. /// \throw isc::Unexpected if timestamp update failed
  155. void updateTimestamp();
  156. private:
  157. /// \brief Updates options in the output buffer
  158. ///
  159. /// The method uses options collection added to object
  160. /// of this class with \ref dhcp::Pkt6::addOption to
  161. /// on-wire data. Option objects has to be of
  162. /// \ref perfdhcp::LocalizedOption type and should
  163. /// have non-zero values of offsets specified.
  164. /// This method will use these offsets to seek to
  165. /// given position in output buffer and update option
  166. /// on-wire data with contents of option's buffer.
  167. ///
  168. /// \throw isc::Unexpected if options update failed.
  169. void rawPackOptions();
  170. /// \brief Reads contents of specified options from buffer
  171. ///
  172. /// The method reads options data from the copy of the buffer
  173. /// provided in constructor and stores data in options
  174. /// objects that belong to options collection.
  175. /// Client class that constructs this object has to create
  176. /// options collection prior to calling \ref rawUnpack
  177. /// method that in turn calls this method.
  178. /// If option is not added to options collection, it will
  179. /// not be added by this method. This method will rather
  180. /// skip update of such an option even if it is present
  181. /// in packet's buffer.
  182. ///
  183. /// \throw isc::Unexpected if options unpack failed.
  184. void rawUnpackOptions();
  185. size_t transid_offset_; ///< transaction id offset
  186. };
  187. } // namespace perfdhcp
  188. } // namespace isc
  189. #endif // __PERF_PKT6_H