option_space.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2012, 2013 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_SPACE_H
  15. #define OPTION_SPACE_H
  16. #include <exceptions/exceptions.h>
  17. #include <boost/shared_ptr.hpp>
  18. #include <map>
  19. #include <stdint.h>
  20. #include <string>
  21. #define DHCP4_OPTION_SPACE "dhcp4"
  22. #define DHCP6_OPTION_SPACE "dhcp6"
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief Exception to be thrown when invalid option space
  26. /// is specified.
  27. class InvalidOptionSpace : public Exception {
  28. public:
  29. InvalidOptionSpace(const char* file, size_t line, const char* what) :
  30. isc::Exception(file, line, what) { };
  31. };
  32. /// OptionSpace forward declaration.
  33. class OptionSpace;
  34. /// A pointer to OptionSpace object.
  35. typedef boost::shared_ptr<OptionSpace> OptionSpacePtr;
  36. /// A collection of option spaces.
  37. typedef std::map<std::string, OptionSpacePtr> OptionSpaceCollection;
  38. /// @brief DHCP option space.
  39. ///
  40. /// This class represents single option space. The option spaces are used
  41. /// to group DHCP options having unique option codes. The special type
  42. /// of the option space is so called "vendor specific option space".
  43. /// It groups sub-options being sent within Vendor Encapsulated Options.
  44. /// For DHCPv4 it is the option with code 43. The option spaces are
  45. /// assigned to option instances represented by isc::dhcp::Option and
  46. /// other classes derived from it. Each particular option may belong to
  47. /// multiple option spaces.
  48. /// This class may be used to represent any DHCPv4 option space. If the
  49. /// option space is to group DHCPv4 Vendor Encapsulated Options then
  50. /// "vendor space" flag must be set using \ref OptionSpace::setVendorSpace
  51. /// or the argument passed to the constructor. In theory, this class can
  52. /// be also used to represent non-vendor specific DHCPv6 option space
  53. /// but this is discouraged. For DHCPv6 option spaces the OptionSpace6
  54. /// class should be used instead.
  55. ///
  56. /// @note this class is intended to be used to represent DHCPv4 option
  57. /// spaces only. However, it hasn't been called OptionSpace4 (that would
  58. /// suggest that it is specific to DHCPv4) because it can be also
  59. /// used to represent some DHCPv6 option spaces and is a base class
  60. /// for \ref OptionSpace6. Thus, if one declared the container as follows:
  61. /// @code
  62. /// std::vector<OptionSpace4> container;
  63. /// @endcode
  64. /// it would suggest that the container holds DHCPv4 option spaces while
  65. /// it could hold both DHCPv4 and DHCPv6 option spaces as the OptionSpace6
  66. /// object could be upcast to OptionSpace4. This confusion does not appear
  67. /// when OptionSpace is used as a name for the base class.
  68. class OptionSpace {
  69. public:
  70. /// @brief Constructor.
  71. ///
  72. /// @param name option space name.
  73. /// @param vendor_space boolean value that indicates that the object
  74. /// describes the vendor specific option space.
  75. ///
  76. /// @throw isc::dhcp::InvalidOptionSpace if given option space name
  77. /// contains invalid characters or is empty. This constructor uses
  78. /// \ref validateName function to check that the specified name is
  79. /// correct.
  80. OptionSpace(const std::string& name, const bool vendor_space = false);
  81. /// @brief Return option space name.
  82. ///
  83. /// @return option space name.
  84. const std::string& getName() const { return (name_); }
  85. /// @brief Mark option space as non-vendor space.
  86. void clearVendorSpace() {
  87. vendor_space_ = false;
  88. }
  89. /// @brief Check if option space is vendor specific.
  90. ///
  91. /// @return boolean value that indicates if the object describes
  92. /// the vendor specific option space.
  93. bool isVendorSpace() const { return (vendor_space_); }
  94. /// @brief Mark option space as vendor specific.
  95. void setVendorSpace() {
  96. vendor_space_ = true;
  97. }
  98. /// @brief Checks that the provided option space name is valid.
  99. ///
  100. /// It is expected that option space name consists of upper or
  101. /// lower case letters or digits. Also, it may contain underscores
  102. /// or dashes. Other characters are prohibited. The empty option
  103. /// space names are invalid.
  104. ///
  105. /// @param name option space name to be validated.
  106. ///
  107. /// @return true if the option space is valid, else it returns false.
  108. static bool validateName(const std::string& name);
  109. private:
  110. std::string name_; ///< Holds option space name.
  111. bool vendor_space_; ///< Is this the vendor space?
  112. };
  113. /// @brief DHCPv6 option space with enterprise number assigned.
  114. ///
  115. /// This class extends the base class with the support for enterprise numbers.
  116. /// The enterprise numbers are assigned by IANA to various organizations
  117. /// and they are carried as uint32_t integers in DHCPv6 Vendor Specific
  118. /// Information Options (VSIO). For more information refer to RFC3315.
  119. /// All option spaces that group VSIO options must have enterprise number
  120. /// set. It can be set using a constructor or \ref setVendorSpace function.
  121. /// The extra functionality of this class (enterprise numbers) allows to
  122. /// represent DHCPv6 vendor-specific option spaces but this class is also
  123. /// intended to be used for all other DHCPv6 option spaces. That way all
  124. /// DHCPv6 option spaces can be stored in the container holding OptionSpace6
  125. /// objects. Also, it is easy to mark vendor-specific option space as non-vendor
  126. /// specific option space (and the other way around) without a need to cast
  127. /// between OptionSpace and OptionSpace6 types.
  128. class OptionSpace6 : public OptionSpace {
  129. public:
  130. /// @brief Constructor for non-vendor-specific options.
  131. ///
  132. /// This constructor marks option space as non-vendor specific.
  133. ///
  134. /// @param name option space name.
  135. ///
  136. /// @throw isc::dhcp::InvalidOptionSpace if given option space name
  137. /// contains invalid characters or is empty. This constructor uses
  138. /// \ref OptionSpace::validateName function to check that the specified
  139. /// name is correct.
  140. OptionSpace6(const std::string& name);
  141. /// @brief Constructor for vendor-specific options.
  142. ///
  143. /// This constructor marks option space as vendor specific and sets
  144. /// enterprise number to a given value.
  145. ///
  146. /// @param name option space name.
  147. /// @param enterprise_number enterprise number.
  148. ///
  149. /// @throw isc::dhcp::InvalidOptionSpace if given option space name
  150. /// contains invalid characters or is empty. This constructor uses
  151. /// \ref OptionSpace::validateName function to check that the specified
  152. /// name is correct.
  153. OptionSpace6(const std::string& name, const uint32_t enterprise_number);
  154. /// @brief Return enterprise number for the option space.
  155. ///
  156. /// @return enterprise number.
  157. uint32_t getEnterpriseNumber() const { return (enterprise_number_); }
  158. /// @brief Mark option space as vendor specific.
  159. ///
  160. /// @param enterprise_number enterprise number.
  161. void setVendorSpace(const uint32_t enterprise_number);
  162. private:
  163. uint32_t enterprise_number_; ///< IANA assigned enterprise number.
  164. };
  165. } // namespace isc::dhcp
  166. } // namespace isc
  167. #endif // OPTION_SPACE_H