localized_option.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 __LOCALIZED_OPTION_H
  15. #define __LOCALIZED_OPTION_H
  16. #include <dhcp/pkt6.h>
  17. namespace isc {
  18. namespace perfdhcp {
  19. /// \brief DHCP option at specific offset
  20. ///
  21. /// This class represents DHCP option at specified
  22. /// offset in DHCP message.
  23. /// Objects of this type are intended to be used when DHCP packets
  24. /// are created from templates (e.g. read from template file).
  25. /// Such packets have number of that have to be replaced before
  26. /// sending: e.g. DUID can be randomized. If option of this type
  27. /// is added to \ref PerfPkt6 options collection,
  28. /// \ref perfdhcp::PerfPkt6 will call \ref getOffset on this object
  29. /// to retrieve user-defined option position and replace contents of
  30. /// the output buffer at this offset before packet is sent to the server.
  31. /// (\see perfdhcp::PerfPkt6::rawPack).
  32. /// In order to read on-wire data from incoming packet client class
  33. /// has to specify options of \ref perfdhcp::LocalizedOption type
  34. /// with expected offsets of these options in a packet. The
  35. /// \ret perfdhcp::PerfPkt6 will use offsets to read fragments
  36. /// of packet and store them in options' buffers.
  37. /// (\see perfdhcp::PerfPkt6::rawUnpack).
  38. ///
  39. class LocalizedOption : public dhcp::Option {
  40. public:
  41. /// \brief Constructor, sets default (0) option offset
  42. ///
  43. /// \param u specifies universe (V4 or V6)
  44. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  45. /// \param data content of the option
  46. LocalizedOption(dhcp::Option::Universe u, uint16_t type, const dhcp::OptionBuffer& data) :
  47. dhcp::Option(u, type, data),
  48. offset_(0) {
  49. }
  50. /// \brief Constructor, used to create localized option from buffer
  51. ///
  52. /// \param u specifies universe (V4 or V6)
  53. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  54. /// \param data content of the option
  55. /// \param offset location of option in a packet (zero is default)
  56. LocalizedOption(dhcp::Option::Universe u, uint16_t type, const dhcp::OptionBuffer& data,
  57. const size_t offset) :
  58. dhcp::Option(u, type, data),
  59. offset_(offset) {
  60. }
  61. /// \brief Constructor, sets default (0) option offset
  62. ///
  63. /// This contructor is similar to the previous one, but it does not take
  64. /// the whole vector<uint8_t>, but rather subset of it.
  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. LocalizedOption(dhcp::Option::Universe u, uint16_t type, dhcp::OptionBufferConstIter first,
  72. dhcp::OptionBufferConstIter last) :
  73. dhcp::Option(u, type, first, last),
  74. offset_(0) {
  75. }
  76. /// \brief Constructor, used to create positioned option from buffer iterators
  77. ///
  78. /// This contructor is similar to the previous one, but it does not take
  79. /// the whole vector<uint8_t>, but rather subset of it.
  80. ///
  81. /// \param u specifies universe (V4 or V6)
  82. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  83. /// \param first iterator to the first element that should be copied
  84. /// \param last iterator to the next element after the last one
  85. /// to be copied.
  86. /// \param offset offset of option in a packet (zero is default)
  87. LocalizedOption(dhcp::Option::Universe u, uint16_t type, dhcp::OptionBufferConstIter first,
  88. dhcp::OptionBufferConstIter last, const size_t offset) :
  89. dhcp::Option(u, type, first, last),
  90. offset_(offset) {
  91. }
  92. /// \brief Returns offset of an option in a DHCP packet.
  93. ///
  94. /// \return option offset in a packet
  95. size_t getOffset() const { return offset_; };
  96. private:
  97. size_t offset_; ///< Offset of DHCP option in a packet
  98. };
  99. } // namespace perfdhcp
  100. } // namespace isc
  101. #endif // __LOCALIZED_OPTION_H