perf_pkt6.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /// Localized option pointer type.
  51. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  52. /// Constructor, used in message transmission
  53. ///
  54. /// Creates new DHCPv6 message using provided buffer. New object
  55. /// will keep copy of contents of provided buffer. If buffer contains
  56. /// options at custom offsets (e.g. if packet was read from
  57. /// template file) additional information about options'
  58. /// offsets has to be provided - see
  59. /// \ref isc::perfdhcp::LocalizedOption for details.
  60. ///
  61. /// Transaction id is not considered DHCP option so
  62. /// we pass it to constructor as extra argument. This is
  63. /// required if transaction id offset differs from the
  64. /// default one for DHCPv6 messages (ocets 2-4).
  65. ///
  66. /// \note use this constructor only in case you want to create
  67. /// DHCPv6 message (incoming or outgoing) from the raw buffer
  68. /// and you know options offsets. Options offsets are
  69. /// specified from perfdhcp command line by the user.
  70. ///
  71. /// \param buf pointer to a buffer of received packet content
  72. /// \param len size of buffer of packet content
  73. /// \param xid_off transaction id offset in a packet
  74. PerfPkt6(const uint8_t* buf,
  75. uint32_t len,
  76. size_t transid_offset_);
  77. /// \brief Returns transaction id offset in packet buffer
  78. ///
  79. /// return transaction id offset in packet buffer
  80. size_t getTransIdOffset() const { return transid_offset_; };
  81. /// \brief Returns current packet timestamp
  82. ///
  83. /// \return current packet timestamp
  84. timespec getTimestamp() const { return time_stamp_; }
  85. /// \brief Prepares on-wire format from raw buffer
  86. ///
  87. /// The method copies user buffer to output buffer and
  88. /// extracts transaction id from it based on transaction id
  89. /// offset provided in constructor.
  90. ///
  91. /// \note: Use this method to prepare on-wire DHCPv6 message
  92. /// when you use template packets that require replacement
  93. /// of selected options' contents before sending.
  94. ///
  95. /// \retrun false, id pack operation failed.
  96. bool rawPack();
  97. /// \brief Handles limited binary packet parsing for packets with
  98. /// custom offsets of options and transaction id
  99. ///
  100. /// Function handles reception of packets that have non-default values
  101. /// of options or transaction id offsets. Use
  102. /// \ref isc::dhcp::Pkt6::addOption to specify which options to parse.
  103. /// Each option should be of the: isc::perfdhcp::LocalizedOption
  104. /// type with offset value indicated.
  105. ///
  106. /// \return false, if unpack operation failed.
  107. bool rawUnpack();
  108. /// \brief Update packet timestamp with current time
  109. ///
  110. /// \throw isc::Unexpected if timestamp update failed
  111. void updateTimestamp();
  112. private:
  113. /// \brief Updates options in the output buffer
  114. ///
  115. /// The method uses options collection added to object
  116. /// of this class with \ref dhcp::Pkt6::addOption to
  117. /// on-wire data. Option objects has to be of
  118. /// \ref perfdhcp::LocalizedOption type and should
  119. /// have non-zero values of offsets specified.
  120. /// This method will use these offsets to seek to
  121. /// given position in output buffer and update option
  122. /// on-wire data with contents of option's buffer.
  123. ///
  124. /// \throw isc::Unexpected if options update failed.
  125. void rawPackOptions();
  126. /// \brief Reads contents of specified options from buffer
  127. ///
  128. /// The method reads options data from the copy of the buffer
  129. /// provided in constructor and stores data in options
  130. /// objects that belong to options collection.
  131. /// Client class that constructs this object has to create
  132. /// options collection prior to calling \ref rawUnpack
  133. /// method that in turn calls this method.
  134. /// If option is not added to options collection, it will
  135. /// not be added by this method. This method will rather
  136. /// skip update of such an option even if it is present
  137. /// in packet's buffer.
  138. ///
  139. /// \throw isc::Unexpected if options unpack failed.
  140. void rawUnpackOptions();
  141. size_t transid_offset_; ///< transaction id offset
  142. timespec time_stamp_; ///< time stamp of last pack or unpack
  143. };
  144. } // namespace perfdhcp
  145. } // namespace isc
  146. #endif // __PERF_PKT6_H