localized_option.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 LOCALIZED_OPTION_H
  15. #define LOCALIZED_OPTION_H
  16. #include <dhcp/pkt6.h>
  17. #include <dhcp/option6_ia.h>
  18. #include <util/buffer.h>
  19. namespace isc {
  20. namespace perfdhcp {
  21. /// \brief DHCP option at specific offset
  22. ///
  23. /// This class represents DHCP option with data placed at specified
  24. /// offset in DHCP message.
  25. /// Objects of this type are intended to be used when DHCP packets
  26. /// are created from templates (e.g. read from template file).
  27. /// Such packets have number of options with contents that have to be
  28. /// replaced before sending: e.g. DUID can be randomized.
  29. /// If option of this type is added to \ref PerfPkt6 options collection,
  30. /// \ref perfdhcp::PerfPkt6 will call \ref getOffset on this object
  31. /// to retrieve user-defined option position and replace contents of
  32. /// the output buffer at this offset before packet is sent to the server.
  33. /// (\see perfdhcp::PerfPkt6::rawPack).
  34. /// In order to read on-wire data from incoming packet client class
  35. /// has to specify options of \ref perfdhcp::LocalizedOption type
  36. /// with expected offsets of these options in a packet. The
  37. /// \ref perfdhcp::PerfPkt6 will use offsets to read fragments
  38. /// of packet and store them in options' buffers.
  39. /// (\see perfdhcp::PerfPkt6::rawUnpack).
  40. ///
  41. class LocalizedOption : public dhcp::Option {
  42. public:
  43. /// \brief Constructor, used to create localized option from buffer.
  44. ///
  45. /// This constructor creates localized option using whole provided
  46. /// option buffer.
  47. ///
  48. /// \param u universe (V4 or V6).
  49. /// \param type option type (0-255 for V4 and 0-65535 for V6).
  50. /// Option values 0 and 255 (v4) and 0 (v6) are not valid option
  51. /// codes but they are accepted here for the server testing purposes.
  52. /// \param data content of the option.
  53. /// \param offset location of option in a packet (zero is default).
  54. LocalizedOption(dhcp::Option::Universe u,
  55. uint16_t type,
  56. const dhcp::OptionBuffer& data,
  57. const size_t offset = 0) :
  58. dhcp::Option(u, type, data),
  59. offset_(offset), option_valid_(true) {
  60. }
  61. /// \brief Constructor, used to create option from buffer iterators.
  62. ///
  63. /// This constructor creates localized option using part of the
  64. /// option buffer pointed by iterators.
  65. ///
  66. /// \param u specifies universe (V4 or V6)
  67. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  68. /// \param first iterator to the first element that should be copied
  69. /// \param last iterator to the next element after the last one
  70. /// to be copied.
  71. /// \param offset offset of option in a packet (zero is default)
  72. LocalizedOption(dhcp::Option::Universe u,
  73. uint16_t type,
  74. dhcp::OptionBufferConstIter first,
  75. dhcp::OptionBufferConstIter last,
  76. const size_t offset = 0) :
  77. dhcp::Option(u, type, first, last),
  78. offset_(offset), option_valid_(true) {
  79. }
  80. /// \brief Copy constructor, creates LocalizedOption from Option6IA.
  81. ///
  82. /// This copy constructor creates regular option from Option6IA.
  83. /// The data from Option6IA data members are copied to
  84. /// option buffer in appropriate sequence.
  85. ///
  86. /// \param opt_ia option to be copied.
  87. /// \param offset location of the option in a packet.
  88. LocalizedOption(const boost::shared_ptr<dhcp::Option6IA>& opt_ia,
  89. const size_t offset) :
  90. dhcp::Option(Option::V6, 0, dhcp::OptionBuffer()),
  91. offset_(offset), option_valid_(false) {
  92. // If given option is NULL we will mark this new option
  93. // as invalid. User may query if option is valid when
  94. // object is created.
  95. if (opt_ia) {
  96. // Set universe and type.
  97. universe_ = opt_ia->getUniverse();
  98. type_ = opt_ia->getType();
  99. util::OutputBuffer buf(opt_ia->len() - opt_ia->getHeaderLen());
  100. try {
  101. // Try to pack option data into the temporary buffer.
  102. opt_ia->pack(buf);
  103. if (buf.getLength() > 0) {
  104. const char* buf_data = static_cast<const char*>(buf.getData());
  105. // Option has been packed along with option type flag
  106. // and transaction id so we have to skip first 4 bytes
  107. // when copying temporary buffer option buffer.
  108. data_.assign(buf_data + 4, buf_data + buf.getLength());
  109. }
  110. option_valid_ = true;
  111. } catch (const Exception&) {
  112. // If there was an exception somewhere when packing
  113. // the data into the buffer we assume that option is
  114. // not valid and should not be used.
  115. option_valid_ = false;
  116. }
  117. } else {
  118. option_valid_ = false;
  119. }
  120. }
  121. /// \brief Returns offset of an option in a DHCP packet.
  122. ///
  123. /// \return option offset in a packet
  124. size_t getOffset() const { return offset_; };
  125. /// \brief Checks if option is valid.
  126. ///
  127. /// \return true, if option is valid.
  128. virtual bool valid() {
  129. return (Option::valid() && option_valid_);
  130. }
  131. private:
  132. size_t offset_; ///< Offset of DHCP option in a packet
  133. bool option_valid_; ///< Is option valid.
  134. };
  135. } // namespace isc::perfdhcp
  136. } // namespace isc
  137. #endif // LOCALIZED_OPTION_H