cfgmgr.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 CFGMGR_H
  15. #define CFGMGR_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_definition.h>
  19. #include <dhcpsrv/pool.h>
  20. #include <dhcpsrv/subnet.h>
  21. #include <util/buffer.h>
  22. #include <boost/shared_ptr.hpp>
  23. #include <boost/noncopyable.hpp>
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. namespace isc {
  28. namespace dhcp {
  29. /// @brief Configuration Manager
  30. ///
  31. /// This singleton class holds the whole configuration for DHCPv4 and DHCPv6
  32. /// servers. It currently holds information about zero or more subnets6.
  33. /// Each subnet may contain zero or more pools. Pool4 and Pool6 is the most
  34. /// basic "chunk" of configuration. It contains a range of assigneable
  35. /// addresses.
  36. ///
  37. /// Below is a sketch of configuration inheritance (not implemented yet).
  38. /// Let's investigate the following configuration:
  39. ///
  40. /// @code
  41. /// preferred-lifetime 500;
  42. /// valid-lifetime 1000;
  43. /// subnet6 2001:db8:1::/48 {
  44. /// pool6 2001::db8:1::1 - 2001::db8:1::ff;
  45. /// };
  46. /// subnet6 2001:db8:2::/48 {
  47. /// valid-lifetime 2000;
  48. /// pool6 2001::db8:2::1 - 2001::db8:2::ff;
  49. /// };
  50. /// @endcode
  51. ///
  52. /// Parameters defined in a global scope are applicable to everything until
  53. /// they are overwritten in a smaller scope, in this case subnet6.
  54. /// In the example above, the first subnet6 has preferred lifetime of 500s
  55. /// and a valid lifetime of 1000s. The second subnet has preferred lifetime
  56. /// of 500s, but valid lifetime of 2000s.
  57. ///
  58. /// Parameter inheritance is likely to be implemented in configuration handling
  59. /// routines, so there is no storage capability in a global scope for
  60. /// subnet-specific parameters.
  61. ///
  62. /// @todo: Implement Subnet4 support (ticket #2237)
  63. /// @todo: Implement option definition support
  64. /// @todo: Implement parameter inheritance
  65. class CfgMgr : public boost::noncopyable {
  66. public:
  67. /// @brief returns a single instance of Configuration Manager
  68. ///
  69. /// CfgMgr is a singleton and this method is the only way of
  70. /// accessing it.
  71. static CfgMgr& instance();
  72. /// @brief Add new option definition.
  73. ///
  74. /// @param def option definition to be added.
  75. /// @param option_space name of the option space to add definition to.
  76. ///
  77. /// @throw isc::dhcp::DuplicateOptionDefinition when the particular
  78. /// option definition already exists.
  79. /// @throw isc::dhcp::MalformedOptionDefinition when the pointer to
  80. /// an option definition is NULL.
  81. /// @throw isc::BadValue when the option space name is empty or
  82. /// when trying to override the standard option (in dhcp4 or dhcp6
  83. /// option space).
  84. void addOptionDef(const OptionDefinitionPtr& def,
  85. const std::string& option_space);
  86. /// @brief Return option definitions for particular option space.
  87. ///
  88. /// @param option_space option space.
  89. ///
  90. /// @return pointer to the collection of option definitions for
  91. /// the particular option space. The option collection is empty
  92. /// if no option exists for the option space specified.
  93. OptionDefContainerPtr
  94. getOptionDefs(const std::string& option_space) const;
  95. /// @brief Return option definition for a particular option space and code.
  96. ///
  97. /// @param option_space option space.
  98. /// @param option_code option code.
  99. ///
  100. /// @return an option definition or NULL pointer if option definition
  101. /// has not been found.
  102. OptionDefinitionPtr getOptionDef(const std::string& option_space,
  103. const uint16_t option_code) const;
  104. /// @brief get IPv6 subnet by address
  105. ///
  106. /// Finds a matching subnet, based on an address. This can be used
  107. /// in two cases: when trying to find an appropriate lease based on
  108. /// a) relay link address (that must be the address that is on link)
  109. /// b) our global address on the interface the message was received on
  110. /// (for directly connected clients)
  111. ///
  112. /// @param hint an address that belongs to a searched subnet
  113. ///
  114. /// @return a subnet object
  115. Subnet6Ptr getSubnet6(const isc::asiolink::IOAddress& hint);
  116. /// @brief get IPv6 subnet by interface-id
  117. ///
  118. /// Another possibility to find a subnet is based on interface-id.
  119. ///
  120. /// @param interface_id content of interface-id option returned by a relay
  121. ///
  122. /// @return a subnet object
  123. /// @todo This method is not currently supported.
  124. Subnet6Ptr getSubnet6(OptionPtr interface_id);
  125. /// @brief adds an IPv6 subnet
  126. ///
  127. /// @param subnet new subnet to be added.
  128. void addSubnet6(const Subnet6Ptr& subnet);
  129. /// @brief Delete all option definitions.
  130. void deleteOptionDefs();
  131. /// @todo: Add subnet6 removal routines. Currently it is not possible
  132. /// to remove subnets. The only case where subnet6 removal would be
  133. /// needed is a dynamic server reconfiguration - a use case that is not
  134. /// planned to be supported any time soon.
  135. /// @brief removes all IPv6 subnets
  136. ///
  137. /// This method removes all existing IPv6 subnets. It is used during
  138. /// reconfiguration - old configuration is wiped and new definitions
  139. /// are used to recreate subnets.
  140. ///
  141. /// @todo Implement more intelligent approach. Note that comparison
  142. /// between old and new configuration is tricky. For example: is
  143. /// 2000::/64 and 2000::/48 the same subnet or is it something
  144. /// completely new?
  145. void deleteSubnets6();
  146. /// @brief get IPv4 subnet by address
  147. ///
  148. /// Finds a matching subnet, based on an address. This can be used
  149. /// in two cases: when trying to find an appropriate lease based on
  150. /// a) relay link address (that must be the address that is on link)
  151. /// b) our global address on the interface the message was received on
  152. /// (for directly connected clients)
  153. ///
  154. /// @param hint an address that belongs to a searched subnet
  155. ///
  156. /// @return a subnet object
  157. Subnet4Ptr getSubnet4(const isc::asiolink::IOAddress& hint);
  158. /// @brief adds a subnet4
  159. void addSubnet4(const Subnet4Ptr& subnet);
  160. /// @brief removes all IPv4 subnets
  161. ///
  162. /// This method removes all existing IPv4 subnets. It is used during
  163. /// reconfiguration - old configuration is wiped and new definitions
  164. /// are used to recreate subnets.
  165. ///
  166. /// @todo Implement more intelligent approach. Note that comparison
  167. /// between old and new configuration is tricky. For example: is
  168. /// 192.0.2.0/23 and 192.0.2.0/24 the same subnet or is it something
  169. /// completely new?
  170. void deleteSubnets4();
  171. protected:
  172. /// @brief Protected constructor.
  173. ///
  174. /// This constructor is protected for 2 reasons. First, it forbids any
  175. /// instantiations of this class (CfgMgr is a singleton). Second, it
  176. /// allows derived class to instantiate it. That is useful for testing
  177. /// purposes.
  178. CfgMgr();
  179. /// @brief virtual desctructor
  180. virtual ~CfgMgr();
  181. /// @brief a container for IPv6 subnets.
  182. ///
  183. /// That is a simple vector of pointers. It does not make much sense to
  184. /// optimize access time (e.g. using a map), because typical search
  185. /// pattern will use calling inRange() method on each subnet until
  186. /// a match is found.
  187. Subnet6Collection subnets6_;
  188. /// @brief a container for IPv4 subnets.
  189. ///
  190. /// That is a simple vector of pointers. It does not make much sense to
  191. /// optimize access time (e.g. using a map), because typical search
  192. /// pattern will use calling inRange() method on each subnet until
  193. /// a match is found.
  194. Subnet4Collection subnets4_;
  195. private:
  196. /// A map containing option definitions for various option spaces.
  197. /// They key of this map is the name of the option space. The
  198. /// value is the the option container holding option definitions
  199. /// for the particular option space.
  200. typedef std::map<std::string, OptionDefContainerPtr> OptionDefsMap;
  201. /// A map containing option definitions for different option spaces.
  202. /// The map key holds an option space name.
  203. OptionDefsMap option_def_spaces_;
  204. };
  205. } // namespace isc::dhcp
  206. } // namespace isc
  207. #endif // CFGMGR_H