perf_pkt4.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_PKT4_H
  15. #define __PERF_PKT4_H
  16. #include <time.h>
  17. #include <boost/shared_ptr.hpp>
  18. #include <dhcp/pkt4.h>
  19. #include "localized_option.h"
  20. namespace isc {
  21. namespace perfdhcp {
  22. /// \brief PerfPkt4 (DHCPv4 packet)
  23. ///
  24. /// This class extends functionality of \ref isc::dhcp::Pkt4 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 PerfPkt4 : public dhcp::Pkt4 {
  49. public:
  50. /// Localized option pointer type.
  51. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  52. /// \brief Constructor, used for outgoing DHCP messages.
  53. ///
  54. /// Creates new DHCPv4 message using provided buffer.
  55. /// Transaction id and its offset are specified through this
  56. /// constructor so as they are stored in outgoing message
  57. /// when client class calls \ref PerfPkt4::rawPack.
  58. ///
  59. /// \note This constructor should be used only for outgoing
  60. /// messages that are created from raw buffer (e.g. read from
  61. /// template files).
  62. ///
  63. /// \param buf buffer holiding contents of the message (this can
  64. /// be directly read from template file).
  65. /// \param len length of the data in the buffer.
  66. /// \param transid_offset transaction id offset in outgoing message.
  67. /// \param transid transaction id to be stored in outgoing message.
  68. PerfPkt4(const uint8_t* buf,
  69. uint32_t len,
  70. size_t transid_offset,
  71. uint32_t transid);
  72. /// Constructor, used for incoming DHCP messages.
  73. ///
  74. /// Creates new DHCPv6 message using provided buffer. New object
  75. /// will keep copy of contents of provided buffer. If buffer contains
  76. /// options at custom offsets (e.g. if packet was read from
  77. /// template file) additional information about options'
  78. /// offsets has to be provided - see
  79. /// \ref isc::perfdhcp::LocalizedOption for details.
  80. ///
  81. /// Transaction id offset point to location of raw data where
  82. /// transaction id field is stored. The transaction id will
  83. /// be read from this location when PerfPkt4::rawUnpack is
  84. /// called. The transid_ class member will be updated accordingly.
  85. ///
  86. /// \note use this constructor only in case you want to create
  87. /// incoming DHCPv6 object from the raw buffer
  88. /// and you know options offsets. Options offsets are
  89. /// specified from perfdhcp command line by the user.
  90. ///
  91. /// \param buf pointer to a buffer of received packet content.
  92. /// \param len size of buffer of packet content.
  93. /// \param transid_offset transaction id offset in a message.
  94. PerfPkt4(const uint8_t* buf,
  95. uint32_t len,
  96. size_t transid_offset);
  97. /// \brief Returns transaction id offset in packet buffer
  98. ///
  99. /// return transaction id offset in packet buffer
  100. size_t getTransIdOffset() const { return transid_offset_; };
  101. /// \brief Prepares on-wire format from raw buffer
  102. ///
  103. /// The method copies user buffer to output buffer and
  104. /// extracts transaction id from it based on transaction id
  105. /// offset provided in constructor.
  106. ///
  107. /// \note: Use this method to prepare on-wire DHCPv6 message
  108. /// when you use template packets that require replacement
  109. /// of selected options' contents before sending.
  110. ///
  111. /// \retrun false, id pack operation failed.
  112. bool rawPack();
  113. /// \brief Handles limited binary packet parsing for packets with
  114. /// custom offsets of options and transaction id
  115. ///
  116. /// Function handles reception of packets that have non-default values
  117. /// of options or transaction id offsets. Use
  118. /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
  119. /// Each option should be of the: isc::perfdhcp::LocalizedOption
  120. /// type with offset value indicated.
  121. ///
  122. /// \return false, if unpack operation failed.
  123. bool rawUnpack();
  124. /// \brief Update packet timestamp with current time
  125. ///
  126. /// \throw isc::Unexpected if timestamp update failed
  127. void updateTimestamp();
  128. private:
  129. size_t transid_offset_; ///< transaction id offset
  130. };
  131. } // namespace perfdhcp
  132. } // namespace isc
  133. #endif // __PERF_PKT4_H