// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. #ifndef CFGMGR_H #define CFGMGR_H #include #include #include #include #include #include #include #include #include #include #include #include #include namespace isc { namespace dhcp { /// @brief Configuration Manager /// /// This singleton class holds the whole configuration for DHCPv4 and DHCPv6 /// servers. It currently holds information about: /// - Zero or more subnets. Each subnet may contain zero or more pools. Pool4 /// and Pool6 is the most basic "chunk" of configuration. It contains a range /// of assignable addresses. /// - Hook libraries. Hooks library names are stored here, but only up to the /// point that the libraries are reloaded. In more detail: libraries /// containing hooks callouts can only be loaded and reloaded safely (or, more /// accurately, unloaded safely) when no data structure in the server contains /// a reference to any memory allocated by a function in them. In practice /// this means when there are no packets being activly processed. Rather than /// take a chance that the configuration code will do the unload/load at the /// right time, the configuration code sets the names of the new libraries in /// this object and the server decides when to reconfigure the hooks. The /// presence or absence of the names of the hooks libraries here is an /// indication of whether the libraries should be reloaded. /// /// Below is a sketch of configuration inheritance (not implemented yet). /// Let's investigate the following configuration: /// /// @code /// preferred-lifetime 500; /// valid-lifetime 1000; /// subnet6 2001:db8:1::/48 { /// pool6 2001::db8:1::1 - 2001::db8:1::ff; /// }; /// subnet6 2001:db8:2::/48 { /// valid-lifetime 2000; /// pool6 2001::db8:2::1 - 2001::db8:2::ff; /// }; /// @endcode /// /// Parameters defined in a global scope are applicable to everything until /// they are overwritten in a smaller scope, in this case subnet6. /// In the example above, the first subnet6 has preferred lifetime of 500s /// and a valid lifetime of 1000s. The second subnet has preferred lifetime /// of 500s, but valid lifetime of 2000s. /// /// Parameter inheritance is likely to be implemented in configuration handling /// routines, so there is no storage capability in a global scope for /// subnet-specific parameters. /// /// ARE THESE DONE? /// @todo: Implement Subnet4 support (ticket #2237) /// @todo: Implement option definition support /// @todo: Implement parameter inheritance class CfgMgr : public boost::noncopyable { public: /// @brief returns a single instance of Configuration Manager /// /// CfgMgr is a singleton and this method is the only way of /// accessing it. static CfgMgr& instance(); /// @brief Add new option definition. /// /// @param def option definition to be added. /// @param option_space name of the option space to add definition to. /// /// @throw isc::dhcp::DuplicateOptionDefinition when the particular /// option definition already exists. /// @throw isc::dhcp::MalformedOptionDefinition when the pointer to /// an option definition is NULL. /// @throw isc::BadValue when the option space name is empty or /// when trying to override the standard option (in dhcp4 or dhcp6 /// option space). void addOptionDef(const OptionDefinitionPtr& def, const std::string& option_space); /// @brief Return option definitions for particular option space. /// /// @param option_space option space. /// /// @return pointer to the collection of option definitions for /// the particular option space. The option collection is empty /// if no option exists for the option space specified. OptionDefContainerPtr getOptionDefs(const std::string& option_space) const; /// @brief Return option definition for a particular option space and code. /// /// @param option_space option space. /// @param option_code option code. /// /// @return an option definition or NULL pointer if option definition /// has not been found. OptionDefinitionPtr getOptionDef(const std::string& option_space, const uint16_t option_code) const; /// @brief Adds new DHCPv4 option space to the collection. /// /// @param space option space to be added. /// /// @throw isc::dhcp::InvalidOptionSpace invalid option space /// has been specified. void addOptionSpace4(const OptionSpacePtr& space); /// @brief Adds new DHCPv6 option space to the collection. /// /// @param space option space to be added. /// /// @throw isc::dhcp::InvalidOptionSpace invalid option space /// has been specified. void addOptionSpace6(const OptionSpacePtr& space); /// @brief Return option spaces for DHCPv4. /// /// @return A collection of option spaces. const OptionSpaceCollection& getOptionSpaces4() const { return (spaces4_); } /// @brief Return option spaces for DHCPv6. /// /// @return A collection of option spaces. const OptionSpaceCollection& getOptionSpaces6() const { return (spaces6_); } /// @brief get IPv6 subnet by address /// /// Finds a matching subnet, based on an address. This can be used /// in two cases: when trying to find an appropriate lease based on /// a) relay link address (that must be the address that is on link) /// b) our global address on the interface the message was received on /// (for directly connected clients) /// /// @param hint an address that belongs to a searched subnet /// /// @return a subnet object (or NULL if no suitable match was fount) Subnet6Ptr getSubnet6(const isc::asiolink::IOAddress& hint); /// @brief get IPv6 subnet by interface name /// /// Finds a matching local subnet, based on interface name. This /// is used for selecting subnets that were explicitly marked by the /// user as reachable over specified network interface. /// @param iface_name interface name /// @return a subnet object (or NULL if no suitable match was fount) Subnet6Ptr getSubnet6(const std::string& iface_name); /// @brief get IPv6 subnet by interface-id /// /// Another possibility to find a subnet is based on interface-id. /// /// @param interface_id content of interface-id option returned by a relay /// /// @return a subnet object Subnet6Ptr getSubnet6(OptionPtr interface_id); /// @brief adds an IPv6 subnet /// /// @param subnet new subnet to be added. void addSubnet6(const Subnet6Ptr& subnet); /// @brief Delete all option definitions. void deleteOptionDefs(); /// @todo: Add subnet6 removal routines. Currently it is not possible /// to remove subnets. The only case where subnet6 removal would be /// needed is a dynamic server reconfiguration - a use case that is not /// planned to be supported any time soon. /// @brief removes all IPv6 subnets /// /// This method removes all existing IPv6 subnets. It is used during /// reconfiguration - old configuration is wiped and new definitions /// are used to recreate subnets. /// /// @todo Implement more intelligent approach. Note that comparison /// between old and new configuration is tricky. For example: is /// 2000::/64 and 2000::/48 the same subnet or is it something /// completely new? void deleteSubnets6(); /// @brief get IPv4 subnet by address /// /// Finds a matching subnet, based on an address. This can be used /// in two cases: when trying to find an appropriate lease based on /// a) relay link address (that must be the address that is on link) /// b) our global address on the interface the message was received on /// (for directly connected clients) /// /// @param hint an address that belongs to a searched subnet /// /// @return a subnet object Subnet4Ptr getSubnet4(const isc::asiolink::IOAddress& hint); /// @brief adds a subnet4 void addSubnet4(const Subnet4Ptr& subnet); /// @brief removes all IPv4 subnets /// /// This method removes all existing IPv4 subnets. It is used during /// reconfiguration - old configuration is wiped and new definitions /// are used to recreate subnets. /// /// @todo Implement more intelligent approach. Note that comparison /// between old and new configuration is tricky. For example: is /// 192.0.2.0/23 and 192.0.2.0/24 the same subnet or is it something /// completely new? void deleteSubnets4(); /// @brief returns path do the data directory /// /// This method returns a path to writeable directory that DHCP servers /// can store data in. /// @return data directory std::string getDataDir(); /// @brief Sets list of hooks libraries /// /// Sets the list of hooks libraries. It is possible for there to be no /// hooks libraries, in which case this is indicated by an emopty vector. /// /// @param hooks_libraries Vector (possibly empty) of current hook libraries. void setHooksLibraries(const std::vector& hooks_libraries); /// @brief Get and clear list of hooks libraries /// /// Gets the currently-set vector of hooks libraries. If there is no data /// (as opposed to an empty vector), there has been no change to the data /// since the last time this method was called. Should there be a necessity /// to know this information, it can be obtained from the HooksManager. /// /// @return Pointer to vector of strings listing the hooks libraries. This /// may be empty. boost::shared_ptr > getAndClearHooksLibraries(); protected: /// @brief Protected constructor. /// /// This constructor is protected for 2 reasons. First, it forbids any /// instantiations of this class (CfgMgr is a singleton). Second, it /// allows derived class to instantiate it. That is useful for testing /// purposes. CfgMgr(); /// @brief virtual destructor virtual ~CfgMgr(); /// @brief a container for IPv6 subnets. /// /// That is a simple vector of pointers. It does not make much sense to /// optimize access time (e.g. using a map), because typical search /// pattern will use calling inRange() method on each subnet until /// a match is found. Subnet6Collection subnets6_; /// @brief a container for IPv4 subnets. /// /// That is a simple vector of pointers. It does not make much sense to /// optimize access time (e.g. using a map), because typical search /// pattern will use calling inRange() method on each subnet until /// a match is found. Subnet4Collection subnets4_; private: /// @brief A collection of option definitions. /// /// A collection of option definitions that can be accessed /// using option space name they belong to. OptionSpaceContainer option_def_spaces_; /// @brief Container for defined DHCPv6 option spaces. OptionSpaceCollection spaces6_; /// @brief Container for defined DHCPv4 option spaces. OptionSpaceCollection spaces4_; /// @brief directory where data files (e.g. server-id) are stored std::string datadir_; /// @brief Hooks libraries /// /// Unlike other configuration items that can be referenced all the time, /// this is a "one-shot" item. When the list of libraries is altered, the /// server needs to know about the change: once the libraries are loaded, /// the list is ignored. As configuration updates cause an update of the /// entire configuration and we wish to reload the libraries only if the /// list has changed, we could check the library list against that stored /// in the hooks manager. Unfortunately, since the libraries are reloaded /// when a new packet is received, this would mean a set of string /// comparisons on each packet. Instead, the data is flagged to indicate /// that it has changed. /// /// The parsing checks the set of hooks libraries in the configuration /// against the list stored in the HooksManager and only updates the data /// here if they have changed. Although a flag could be used to indicate /// a change, a more streamlined approach is used: the data in this object /// is cleared when it is read. As the data is a vector of strings and as /// an empty vector is valid data, we'll store the data as a shared pointer /// to a vector of strings. The pointer is zeroed when the data is read. boost::shared_ptr > hooks_libraries_; }; } // namespace isc::dhcp } // namespace isc #endif // CFGMGR_H