perf_pkt4.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2012 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. #include "pkt_transform.h"
  21. namespace isc {
  22. namespace perfdhcp {
  23. /// \brief PerfPkt4 (DHCPv4 packet)
  24. ///
  25. /// This class extends the functionality of \ref isc::dhcp::Pkt4 by adding the
  26. /// ability to specify an options offset in the DHCP message and to override
  27. /// options' contents. This is particularly useful when we create a packet
  28. /// object using a template file (i.e. do not build it dynamically). The client
  29. /// class should read data from the template file and pass it to this class in
  30. /// a buffer.
  31. ///
  32. /// The contents of such a packet can be later partially replaced, notably the
  33. /// selected options and the transaction ID. (The transaction ID and its
  34. /// offset in the template file are passed via the constructor.)
  35. ///
  36. /// In order to replace contents of the options, the client class has to
  37. /// create a collection of \ref LocalizedOption, adding them using
  38. /// \ref dhcp::Pkt4::addOption.
  39. ///
  40. /// \note If you don't use template files simply use constructors
  41. /// inherited from parent class and isc::dhcp::Option type instead
  42. class PerfPkt4 : public dhcp::Pkt4 {
  43. public:
  44. /// Localized option pointer type.
  45. typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
  46. /// \brief Constructor, used to create messages from packet
  47. /// template files.
  48. ///
  49. /// Creates a new DHCPv4 message using the provided buffer.
  50. /// The transaction ID and its offset are specified via this
  51. /// constructor. The transaction ID is stored in outgoing message
  52. /// when client class calls \ref PerfPkt4::rawPack. Transaction id
  53. /// offset value is used for incoming and outgoing messages to
  54. /// identify transaction ID field's position in incoming and outgoing
  55. /// messages.
  56. ///
  57. /// \param buf buffer holding contents of the message (this can
  58. /// be directly read from template file).
  59. /// \param len length of the data in the buffer.
  60. /// \param transid_offset transaction id offset in a message.
  61. /// \param transid transaction id to be stored in outgoing message.
  62. PerfPkt4(const uint8_t* buf,
  63. size_t len,
  64. size_t transid_offset = 1,
  65. uint32_t transid = 0);
  66. /// \brief Returns transaction id offset in packet buffer
  67. ///
  68. /// \return Transaction ID offset in packet buffer
  69. size_t getTransidOffset() const { return transid_offset_; };
  70. /// \brief Prepares on-wire format from raw buffer.
  71. ///
  72. /// The method copies the buffer provided in the constructor to the
  73. /// output buffer and replaces the transaction ID and selected
  74. /// options with new data.
  75. ///
  76. /// \note Use this method to prepare an on-wire DHCPv4 message
  77. /// when you use template packets that require replacement
  78. /// of selected options' contents before sending.
  79. ///
  80. /// \return false ID pack operation failed.
  81. bool rawPack();
  82. /// \brief Handles limited binary packet parsing for packets with
  83. /// custom offsets of options and transaction ID
  84. ///
  85. /// This method handles the parsing of packets that have custom offsets
  86. /// of options or transaction ID. Use
  87. /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
  88. /// Options should be of the \ref isc::perfdhcp::LocalizedOption
  89. /// type with offset values provided. Each added option will
  90. /// be updated with actual data read from the binary packet buffer.
  91. ///
  92. /// \return false If unpack operation failed.
  93. bool rawUnpack();
  94. /// \brief Replace contents of buffer with data.
  95. ///
  96. /// Function replaces part of the buffer with data from vector.
  97. ///
  98. /// \param dest_pos position in buffer where data is replaced.
  99. /// \param first beginning of data range in source vector.
  100. /// \param last end of data range in source vector.
  101. void writeAt(size_t dest_pos,
  102. std::vector<uint8_t>::iterator first,
  103. std::vector<uint8_t>::iterator last);
  104. /// \brief Replace contents of buffer with value.
  105. ///
  106. /// Function replaces part of buffer with value.
  107. ///
  108. /// \param dest_pos position in buffer where value is
  109. /// to be written.
  110. /// \param val value to be written.
  111. template<typename T>
  112. void writeValueAt(size_t dest_pos, T val) {
  113. PktTransform::writeValueAt<T>(data_, dest_pos, val);
  114. }
  115. private:
  116. size_t transid_offset_; ///< transaction id offset
  117. };
  118. typedef boost::shared_ptr<PerfPkt4> PerfPkt4Ptr;
  119. } // namespace perfdhcp
  120. } // namespace isc
  121. #endif // PERF_PKT4_H