cfgmgr.h 11 KB

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