localized_option.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace isc {
  18. namespace perfdhcp {
  19. /// \brief DHCP option at specific offset
  20. ///
  21. /// This class represents DHCP option with data placed 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 options with contents that have to be
  26. /// replaced before sending: e.g. DUID can be randomized.
  27. /// If option of this type 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. /// \ref 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,
  47. uint16_t type,
  48. const dhcp::OptionBuffer& data) :
  49. dhcp::Option(u, type, data),
  50. offset_(0) {
  51. }
  52. /// \brief Constructor, used to create localized option from buffer
  53. ///
  54. /// \param u specifies universe (V4 or V6)
  55. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  56. /// \param data content of the option
  57. /// \param offset location of option in a packet (zero is default)
  58. LocalizedOption(dhcp::Option::Universe u,
  59. uint16_t type,
  60. const dhcp::OptionBuffer& data,
  61. const size_t offset) :
  62. dhcp::Option(u, type, data),
  63. offset_(offset) {
  64. }
  65. /// \brief Constructor, sets default (0) option offset
  66. ///
  67. /// This contructor is similar to the previous one, but it does not take
  68. /// the whole vector<uint8_t>, but rather subset of it.
  69. ///
  70. /// \param u specifies universe (V4 or V6)
  71. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  72. /// \param first iterator to the first element that should be copied
  73. /// \param last iterator to the next element after the last one
  74. /// to be copied.
  75. LocalizedOption(dhcp::Option::Universe u,
  76. uint16_t type,
  77. dhcp::OptionBufferConstIter first,
  78. dhcp::OptionBufferConstIter last) :
  79. dhcp::Option(u, type, first, last),
  80. offset_(0) {
  81. }
  82. /// \brief Constructor, used to create option from buffer iterators
  83. ///
  84. /// This contructor is similar to the previous one, but it does not take
  85. /// the whole vector<uint8_t>, but rather subset of it.
  86. ///
  87. /// \param u specifies universe (V4 or V6)
  88. /// \param type option type (0-255 for V4 and 0-65535 for V6)
  89. /// \param first iterator to the first element that should be copied
  90. /// \param last iterator to the next element after the last one
  91. /// to be copied.
  92. /// \param offset offset of option in a packet (zero is default)
  93. LocalizedOption(dhcp::Option::Universe u,
  94. uint16_t type,
  95. dhcp::OptionBufferConstIter first,
  96. dhcp::OptionBufferConstIter last, const size_t offset) :
  97. dhcp::Option(u, type, first, last),
  98. offset_(offset) {
  99. }
  100. /// \brief Returns offset of an option in a DHCP packet.
  101. ///
  102. /// \return option offset in a packet
  103. size_t getOffset() const { return offset_; };
  104. private:
  105. size_t offset_; ///< Offset of DHCP option in a packet
  106. };
  107. } // namespace perfdhcp
  108. } // namespace isc
  109. #endif // __LOCALIZED_OPTION_H