option_string.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2013,2015 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 OPTION_STRING_H
  15. #define OPTION_STRING_H
  16. #include <dhcp/option.h>
  17. #include <util/buffer.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <string>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Class which represents an option carrying a single string value.
  23. ///
  24. /// This class represents an option carrying a single string value.
  25. /// Currently this class imposes that the minimal length of the carried
  26. /// string is 1.
  27. ///
  28. /// @todo In the future this class may be extended with some more string
  29. /// content checks and encoding methods if required.
  30. class OptionString : public Option {
  31. public:
  32. /// @brief Constructor, used to create options to be sent.
  33. ///
  34. /// This constructor creates an instance of option which carries a
  35. /// string value specified as constructor's parameter. This constructor
  36. /// is most often used to create an instance of an option which will
  37. /// be sent in the outgoing packet.
  38. ///
  39. /// @param u universe (V4 or V6).
  40. /// @param type option code.
  41. /// @param value a string value to be carried by the option.
  42. ///
  43. /// @throw isc::OutOfRange if provided string is empty.
  44. OptionString(const Option::Universe u, const uint16_t type,
  45. const std::string& value);
  46. /// @brief Constructor, used for receiving options.
  47. ///
  48. /// This constructor creates an instance of the option from the provided
  49. /// chunk of buffer. This buffer may hold the data received on the wire.
  50. ///
  51. /// @param u universe (V4 or V6).
  52. /// @param type option code.
  53. /// @param begin iterator pointing to the first byte of the buffer chunk.
  54. /// @param end iterator pointing to the last byte of the buffer chunk.
  55. ///
  56. /// @throw isc::OutOfRange if provided buffer is truncated.
  57. OptionString(const Option::Universe u, const uint16_t type,
  58. OptionBufferConstIter begin, OptionBufferConstIter end);
  59. /// @brief Returns length of the whole option, including header.
  60. ///
  61. /// @return length of the whole option.
  62. virtual uint16_t len();
  63. /// @brief Returns the string value held by the option.
  64. ///
  65. /// @return string value held by the option.
  66. std::string getValue() const;
  67. /// @brief Sets the string value to be held by the option.
  68. ///
  69. /// @param value string value to be set.
  70. ///
  71. /// @throw isc::OutOfRange if a string value to be set is empty.
  72. void setValue(const std::string& value);
  73. /// @brief Creates on-wire format of the option.
  74. ///
  75. /// This function creates on-wire format of the option and appends it to
  76. /// the data existing in the provided buffer. The internal buffer's pointer
  77. /// is moved to the end of stored data.
  78. ///
  79. /// @param [out] buf output buffer where the option will be stored.
  80. virtual void pack(isc::util::OutputBuffer& buf);
  81. /// @brief Decodes option data from the provided buffer.
  82. ///
  83. /// This function decodes option data from the provided buffer. Note that
  84. /// it does not decode the option code and length, so the iterators must
  85. /// point to the begining and end of the option payload respectively.
  86. /// The size of the decoded payload must be at least 1 byte.
  87. ///
  88. /// @param begin the iterator pointing to the option payload.
  89. /// @param end the iterator pointing to the end of the option payload.
  90. ///
  91. /// @throw isc::OutOfRange if provided buffer is truncated.
  92. virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end);
  93. /// @brief Returns option information in the textual format.
  94. ///
  95. /// @param indent Number of space characters to be inserted before
  96. /// the text.
  97. ///
  98. /// @return Option information in the textual format.
  99. virtual std::string toText(int indent = 0);
  100. /// @brief Returns actual value of the option in string format.
  101. ///
  102. /// This method is used in client classification.
  103. /// @return Content of the option.
  104. virtual std::string toString();
  105. };
  106. /// Pointer to the OptionString object.
  107. typedef boost::shared_ptr<OptionString> OptionStringPtr;
  108. } // namespace isc::dhcp
  109. } // namespace isc
  110. #endif // OPTION_STRING_H