perf_pkt4.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 the functionality of \ref isc::dhcp::Pkt4 by adding the
  25. /// ability to specify an options offset in the DHCP message and to override
  26. /// options' contents. This is particularly useful when we create a packet
  27. /// object using a template file (i.e. do not build it dynamically). The client
  28. /// class should read data from the template file and pass it to this class in
  29. /// a buffer.
  30. ///
  31. /// The contents of such a packet can be later partially replaced, notably the
  32. /// selected options and the transaction ID. (The transaction ID and its
  33. /// offset in the template file are passed via the constructor.)
  34. ///
  35. /// In order to replace contents of the options, the client class has to
  36. /// create a collection of \ref LocalizedOption, adding them using
  37. /// \ref dhcp::Pkt4::addOption.
  38. ///
  39. /// \note If you don't use template files simply use constructors
  40. /// inherited from parent class and isc::dhcp::Option type instead
  41. class PerfPkt4 : public dhcp::Pkt4 {
  42. public:
  43. /// Localized option pointer type.
  44. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  45. /// \brief Constructor, used for outgoing and incoming messages
  46. ///
  47. /// This constructor initializes the transaction ID and
  48. /// transaction id offset of the packet with default
  49. /// values.
  50. ///
  51. /// \param buf buffer holding contents of the message.
  52. /// \param len length of the data in the buffer.
  53. PerfPkt4(const uint8_t* buf, size_t len);
  54. /// \brief Constructor, used for outgoing DHCP messages.
  55. ///
  56. /// Creates new DHCPv4 message using the provided buffer. The
  57. /// transaction ID and its offset are specified via this
  58. /// constructor. The transaction ID is stored in the outgoing message
  59. /// when the client class calls \ref PerfPkt4::rawPack.
  60. ///
  61. /// \note This constructor should be used only for outgoing
  62. /// messages that are created from template files.
  63. ///
  64. /// \param buf buffer holding contents of the message (this can
  65. /// be directly read from template file).
  66. /// \param len length of the data in the buffer.
  67. /// \param transid_offset transaction id offset in outgoing message.
  68. /// \param transid transaction id to be stored in outgoing message.
  69. PerfPkt4(const uint8_t* buf,
  70. size_t len,
  71. size_t transid_offset,
  72. uint32_t transid);
  73. /// Constructor, used for incoming DHCP messages.
  74. ///
  75. /// Creates new DHCPv4 message using the provided buffer.
  76. /// The client class may indicate which options are to be read
  77. /// from the buffer. Such options should be added to the
  78. /// options collection using \ref dhcp::Pkt4::addOption: these
  79. /// options should be of \ref LocalizedOption class.
  80. ///
  81. /// The transaction ID offset points to the location where the
  82. /// transaction ID field is stored. The transaction ID will
  83. /// be read from this location when \ref PerfPkt4::rawUnpack is
  84. /// called. The transid_ class member will be updated accordingly.
  85. ///
  86. /// \note Use this constructor only in the case where you want to create
  87. /// an incoming DHCPv4 object from the raw buffer
  88. /// and you know the options offsets. The options offsets are
  89. /// specified on the 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. size_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 the buffer provided in the constructor to the
  104. /// output buffer and replaces the transaction ID and selected
  105. /// options with new data.
  106. ///
  107. /// \note Use this method to prepare an on-wire DHCPv4 message
  108. /// when you use template packets that require replacement
  109. /// of selected options' contents before sending.
  110. ///
  111. /// \return 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. /// This method handles the parsing of packets that have custom offsets
  117. /// of options or transaction ID. Use
  118. /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
  119. /// Options should be of the \ref isc::perfdhcp::LocalizedOption
  120. /// type with offset values provided. Each added option will
  121. /// be updated with actual data read from the binary packet buffer.
  122. ///
  123. /// \return false If unpack operation failed.
  124. bool rawUnpack();
  125. private:
  126. size_t transid_offset_; ///< transaction id offset
  127. };
  128. } // namespace perfdhcp
  129. } // namespace isc
  130. #endif // __PERF_PKT4_H