perf_pkt4.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.
  27. /// This is in particular useful when we create packet object using
  28. /// template file (do not build it dynamically). Client class
  29. /// should read data from template file and pass data to this class
  30. /// as buffer.
  31. /// Contents of such packet can be later partially replaced: preciselly
  32. /// selected options and transaction id can be replaced.
  33. /// Transaction id and its offset in template file is passed via
  34. /// constructor.
  35. /// In order to replace contents of options client class has to
  36. /// create collection of \ref LocalizedOption by 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. ///
  42. class PerfPkt4 : public dhcp::Pkt4 {
  43. public:
  44. /// Localized option pointer type.
  45. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  46. /// \brief Constructor, used for outgoing and incoming messages
  47. ///
  48. /// This constructor initializes transaction id and
  49. /// transaction id offset of the packet with default
  50. /// values.
  51. ///
  52. /// \param buf buffer holding contents of the message.
  53. /// \param len length of the data in the buffer.
  54. PerfPkt4(const uint8_t* buf, size_t len);
  55. /// \brief Constructor, used for outgoing DHCP messages.
  56. ///
  57. /// Creates new DHCPv4 message using provided buffer.
  58. /// Transaction id and its offset are specified via this
  59. /// constructor. Transaction id is stored in outgoing message
  60. /// when client class calls \ref PerfPkt4::rawPack.
  61. ///
  62. /// \note This constructor should be used only for outgoing
  63. /// messages that are created from template files.
  64. ///
  65. /// \param buf buffer holding contents of the message (this can
  66. /// be directly read from template file).
  67. /// \param len length of the data in the buffer.
  68. /// \param transid_offset transaction id offset in outgoing message.
  69. /// \param transid transaction id to be stored in outgoing message.
  70. PerfPkt4(const uint8_t* buf,
  71. size_t len,
  72. size_t transid_offset,
  73. uint32_t transid);
  74. /// Constructor, used for incoming DHCP messages.
  75. ///
  76. /// Creates new DHCPv4 message using provided buffer.
  77. /// Client class may indicate which options are to be read
  78. /// from the buffer. Such options should be added to
  79. /// options collection using dhcp::Pkt4::addOption. Such
  80. /// options should be of \ref LocalizedOption class.
  81. ///
  82. /// Transaction id offset points to location where
  83. /// transaction id field is stored. The transaction id will
  84. /// be read from this location when PerfPkt4::rawUnpack is
  85. /// called. The transid_ class member will be updated accordingly.
  86. ///
  87. /// \note use this constructor only in case you want to create
  88. /// incoming DHCPv4 object from the raw buffer
  89. /// and you know options offsets. Options offsets are
  90. /// specified from perfdhcp command line by the user.
  91. ///
  92. /// \param buf pointer to a buffer of received packet content.
  93. /// \param len size of buffer of packet content.
  94. /// \param transid_offset transaction id offset in a message.
  95. PerfPkt4(const uint8_t* buf,
  96. size_t len,
  97. size_t transid_offset);
  98. /// \brief Returns transaction id offset in packet buffer
  99. ///
  100. /// return transaction id offset in packet buffer
  101. size_t getTransIdOffset() const { return transid_offset_; };
  102. /// \brief Prepares on-wire format from raw buffer.
  103. ///
  104. /// The method copies buffer provided in constructor to
  105. /// output buffer and replaces transaction id and selected
  106. /// options with new data.
  107. ///
  108. /// \note: Use this method to prepare on-wire DHCPv4 message
  109. /// when you use template packets that require replacement
  110. /// of selected options' contents before sending.
  111. ///
  112. /// \return false, id pack operation failed.
  113. bool rawPack();
  114. /// \brief Handles limited binary packet parsing for packets with
  115. /// custom offsets of options and transaction id
  116. ///
  117. /// Function handles parsing of packets that have custom offsets
  118. /// of options or transaction id. Use
  119. /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
  120. /// Options should be of the: isc::perfdhcp::LocalizedOption
  121. /// type with offset values provided. Each added option will
  122. /// be updated with actual data read from the binary packet buffer.
  123. ///
  124. /// \return false, if unpack operation failed.
  125. bool rawUnpack();
  126. /// \brief Update packet timestamp with current time
  127. ///
  128. /// \throw isc::Unexpected if timestamp update failed
  129. void updateTimestamp();
  130. private:
  131. size_t transid_offset_; ///< transaction id offset
  132. };
  133. } // namespace perfdhcp
  134. } // namespace isc
  135. #endif // __PERF_PKT4_H