option_vendor_class.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (C) 2014 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_VENDOR_CLASS_H
  15. #define OPTION_VENDOR_CLASS_H
  16. #include <dhcp/dhcp4.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcp/opaque_data_tuple.h>
  19. #include <dhcp/option.h>
  20. #include <util/buffer.h>
  21. #include <boost/shared_ptr.hpp>
  22. #include <stdint.h>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief This class encapsulates DHCPv6 Vendor Class and DHCPv4 V-I Vendor
  26. /// Class options.
  27. ///
  28. /// The format of DHCPv6 Vendor Class option (16) is described in section 22.16
  29. /// of RFC3315 and the format of the DHCPv4 V-I Vendor Class option (124) is
  30. /// described in section 3 of RFC3925. Each of these options carries enterprise
  31. /// id followed by the collection of tuples carring opaque data. A single tuple
  32. /// consists of the field holding opaque data length and the actual data.
  33. /// In case of the DHCPv4 V-I Vendor Class each tuple is preceded by the
  34. /// 4-byte long enterprise id. Also, the field which carries the length of
  35. /// the tuple is 1-byte long for DHCPv4 V-I Vendor Class and 2-bytes long
  36. /// for the DHCPv6 Vendor Class option.
  37. ///
  38. /// Whether the encapsulated format is DHCPv4 V-I Vendor Class or DHCPv6
  39. /// Vendor Class option is controlled by the @c u (universe) parameter passed
  40. /// to the constructor.
  41. ///
  42. /// @todo Currently, the enterprise id field is set to a value of the first
  43. /// enterprise id occurrence in the parsed option. At some point we should
  44. /// be able to differentiate between enterprise ids.
  45. class OptionVendorClass : public Option {
  46. public:
  47. /// @brief Collection of opaque data tuples carried by the option.
  48. typedef std::vector<OpaqueDataTuple> TuplesCollection;
  49. /// @brief Constructor.
  50. ///
  51. /// This constructor instance of the DHCPv4 V-I Vendor Class option (124)
  52. /// or DHCPv6 Vendor Class option (16), depending on universe specified.
  53. /// If the universe is v4, the constructor adds one empty tuple to the
  54. /// option, as per RFC3925, section 3. the complete option must hold at
  55. /// least one data-len field for opaque data. If the specified universe
  56. /// is v6, the constructor adds no tuples.
  57. ///
  58. /// @param u universe (v4 or v6).
  59. /// @param vendor_id vendor enterprise id (unique 32-bit integer).
  60. OptionVendorClass(Option::Universe u, const uint32_t vendor_id);
  61. /// @brief Constructor.
  62. ///
  63. /// This constructor creates an instance of the option using a buffer with
  64. /// on-wire data. It may throw an exception if the @c unpack method throws.
  65. ///
  66. /// @param u universe (v4 or v6)
  67. /// @param begin Iterator pointing to the beginning of the buffer holding an
  68. /// option.
  69. /// @param end Iterator pointing to the end of the buffer holding an option.
  70. OptionVendorClass(Option::Universe u, OptionBufferConstIter begin,
  71. OptionBufferConstIter end);
  72. /// @brief Renders option into the buffer in the wire format.
  73. ///
  74. /// @param [out] buf Buffer to which the option is rendered.
  75. virtual void pack(isc::util::OutputBuffer& buf);
  76. /// @brief Parses buffer holding an option.
  77. ///
  78. /// This function parses the buffer holding an option and initializes option
  79. /// properties: enterprise ids and the collection of tuples.
  80. ///
  81. /// @param begin Iterator pointing to the beginning of the buffer holding an
  82. /// option.
  83. /// @param end Iterator pointing to the end of the buffer holding an option.
  84. virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end);
  85. /// @brief Returns enterprise id.
  86. uint32_t getVendorId() const {
  87. return (vendor_id_);
  88. }
  89. /// @brief Adds a new opaque data tuple to the option.
  90. ///
  91. /// @param tuple Tuple to be added.
  92. /// @throw isc::BadValue if the type of the tuple doesn't match the
  93. /// universe this option belongs to.
  94. void addTuple(const OpaqueDataTuple& tuple);
  95. /// @brief Replaces tuple at the specified index with a new tuple.
  96. ///
  97. /// This function replaces an opaque data tuple at the specified position
  98. /// with the new tuple. If the specified index is out of range an exception
  99. /// is thrown.
  100. ///
  101. /// @param at Index at which the tuple should be replaced.
  102. /// @param tuple Tuple to be set.
  103. /// @throw isc::OutOfRange if the tuple position is out of range.
  104. /// @throw isc::BadValue if the type of the tuple doesn't match the
  105. /// universe this option belongs to.
  106. void setTuple(const size_t at, const OpaqueDataTuple& tuple);
  107. /// @brief Returns opaque data tuple at the specified position.
  108. ///
  109. /// If the specified position is out of range an exception is thrown.
  110. ///
  111. /// @param at Index at which the tuple should be replaced.
  112. /// @throw isc::OutOfRange if the tuple position is out of range.
  113. OpaqueDataTuple getTuple(const size_t at) const;
  114. /// @brief Returns the number of opaque data tuples added to the option.
  115. size_t getTuplesNum() const {
  116. return (tuples_.size());
  117. }
  118. /// @brief Returns collection of opaque data tuples carried in the option.
  119. const TuplesCollection& getTuples() const {
  120. return (tuples_);
  121. }
  122. /// @brief Checks if the Vendor Class holds the opaque data tuple with the
  123. /// specified string.
  124. ///
  125. /// @param tuple_str String representation of the tuple being searched.
  126. /// @return true if the specified tuple exists for this option.
  127. bool hasTuple(const std::string& tuple_str) const;
  128. /// @brief Returns the full length of the option, including option header.
  129. virtual uint16_t len();
  130. /// @brief Returns text representation of the option.
  131. ///
  132. /// @param indent Number of space characters before text.
  133. /// @return Text representation of the option.
  134. virtual std::string toText(int indent = 0);
  135. private:
  136. /// @brief Returns option code appropriate for the specified universe.
  137. ///
  138. /// This function is called by the constructor to map the specified
  139. /// universe to the option code.
  140. ///
  141. /// @param u universe (V4 or V6).
  142. /// @return DHCPv4 V-I Vendor Class or DHCPv6 Vendor Class option code.
  143. static uint16_t getOptionCode(Option::Universe u) {
  144. return (u == V4 ? DHO_VIVCO_SUBOPTIONS : D6O_VENDOR_CLASS);
  145. }
  146. /// @brief Returns the tuple length field type for the given universe.
  147. ///
  148. /// This function returns the length field type which should be used
  149. /// for the opaque data tuples being added to this option.
  150. ///
  151. /// @return Tuple length field type for the universe this option belongs to.
  152. OpaqueDataTuple::LengthFieldType getLengthFieldType() const {
  153. return (getUniverse() == V4 ? OpaqueDataTuple::LENGTH_1_BYTE :
  154. OpaqueDataTuple::LENGTH_2_BYTES);
  155. }
  156. /// @brief Returns minimal length of the option for the given universe.
  157. uint16_t getMinimalLength() const {
  158. return (getUniverse() == Option::V4 ? 7 : 6);
  159. }
  160. /// @brief Enterprise ID.
  161. uint32_t vendor_id_;
  162. /// @brief Collection of opaque data tuples carried by the option.
  163. TuplesCollection tuples_;
  164. };
  165. /// @brief Defines a pointer to the @c OptionVendorClass.
  166. typedef boost::shared_ptr<OptionVendorClass> OptionVendorClassPtr;
  167. }
  168. }
  169. #endif // OPTION_VENDOR_CLASS_H