123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // Copyright (C) 2012 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 <string>
- #include <map>
- #include <vector>
- #include <boost/shared_ptr.hpp>
- #include <boost/noncopyable.hpp>
- #include <asiolink/io_address.h>
- #include <util/buffer.h>
- #include <dhcp/option.h>
- namespace isc {
- namespace dhcp {
- class Pool6;
- class Subnet6;
- /// @brief this class specifes parameter value
- ///
- /// This class is used to store configuration parameters, like lifetime or T1.
- /// It defines 3 parameters: min/default/max values. There are 2 constructors:
- /// - simple (just one value that sets all parameters)
- /// - extended (that sets default value and two thresholds)
- template <class T>
- class Triplet {
- public:
- /// @brief base type to Triple conversion
- ///
- /// Typically: uint32_t to Triplet assignment. It is very convenient
- /// to be able to simply write Triplet<uint32_t> x = 7;
- Triplet<T>& operator = (T base_type) {
- return Triplet<T>(base_type);
- }
- /// @brief triplet to base type conversion
- ///
- /// Typically: Triplet to uint32_t assignment. It is very convenient
- /// to be able to simply write uint32_t z = x; (where x is a Triplet)
- operator T () const {
- return (default_);
- }
- /// @brief sets a fixed value
- Triplet(T value)
- :min_(value), default_(value), max_(value) {
- }
- /// @brief sets the default value and thresholds
- ///
- /// @throw BadValue if min <= def <= max rule is violated
- Triplet(T min, T def, T max)
- :min_(min), default_(def), max_(max) {
- if ( (min_>def) || (def > max_) ) {
- isc_throw(BadValue, "Invalid triplet values.");
- }
- }
- /// @brief returns a minimum allowed value
- T getMin() const { return min_;}
- /// @brief returns the default value
- T get() const { return default_;}
- /// @brief returns value with a hint
- ///
- /// DHCP protocol treats any values sent by a client as hints.
- /// This is a method that implements that. We can assign any value
- /// from configured range that client asks.
- T get(T hint) const {
- if (hint <= min_) {
- return (min_);
- }
- if (hint >= max_) {
- return (max_);
- }
- return (hint);
- }
- /// @brief returns a maximum allowed value
- T getMax() const { return max_; }
- protected:
- /// @brief the minimum value
- T min_;
- /// @brief the default value
- T default_;
- /// @brief the maximum value
- T max_;
- };
- class Pool {
- public:
- uint32_t getId() const {
- return (id_);
- }
- Triplet<uint32_t> getValid() const {
- return (valid_);
- }
- const isc::asiolink::IOAddress& getFirstAddress() const {
- return (first_);
- }
- const isc::asiolink::IOAddress& getLastAddress() const {
- return (last_);
- }
- Triplet<uint32_t> getT1() const {
- return (t1_);
- }
- Triplet<uint32_t> getT2() const {
- return (t2_);
- }
- /// @brief checks if specified address is in range
- bool inRange(const isc::asiolink::IOAddress& addr);
- protected:
- /// @brief protected constructor
- Pool(const isc::asiolink::IOAddress& first,
- const isc::asiolink::IOAddress& last,
- const Triplet<uint32_t>& t1,
- const Triplet<uint32_t>& t2,
- const Triplet<uint32_t>& valid_lifetime);
- static uint32_t getNextID() {
- static uint32_t id = 0;
- return (id++);
- }
- /// @brief pool-id
- ///
- /// This ID is used to indentify this specific pool.
- uint32_t id_;
- isc::asiolink::IOAddress first_;
- isc::asiolink::IOAddress last_;
- Triplet<uint32_t> t1_;
- Triplet<uint32_t> t2_;
- Triplet<uint32_t> valid_;
- std::string comments_;
- ///uint128_t available_leases_;
- ///uint128_t total_leases_;
- };
- class Pool6 : public Pool {
- public:
- typedef enum {
- TYPE_IA,
- TYPE_TA,
- TYPE_PD
- } Pool6Type;
- Pool6(Pool6Type type, const isc::asiolink::IOAddress& first,
- const isc::asiolink::IOAddress& last,
- const Triplet<uint32_t>& t1,
- const Triplet<uint32_t>& t2,
- const Triplet<uint32_t>& preferred_lifetime,
- const Triplet<uint32_t>& valid_lifetime);
- Pool6(Pool6Type type, const isc::asiolink::IOAddress& addr,
- uint8_t prefix_len,
- const Triplet<uint32_t>& t1,
- const Triplet<uint32_t>& t2,
- const Triplet<uint32_t>& preferred_lifetime,
- const Triplet<uint32_t>& valid_lifetime);
- Pool6Type getType() const {
- return (type_);
- }
- Triplet<uint32_t> getPreferred() const {
- return (preferred_);
- }
- protected:
- Pool6Type type_;
- /// @brief prefix length
- /// used by TYPE_PD only (zeroed for other types)
- uint8_t prefix_len_;
- Triplet<uint32_t> preferred_;
- };
- typedef boost::shared_ptr<Pool> PoolPtr;
- typedef boost::shared_ptr<Pool6> Pool6Ptr;
- typedef std::vector<Pool6Ptr> Pool6Collection;
- class Subnet {
- public:
- /// @brief checks if specified address is in range
- bool inRange(const isc::asiolink::IOAddress& addr);
- protected:
- /// @brief protected constructor
- //
- /// By making the constructor protected, we make sure that noone will
- /// ever instantiate that class. Pool4 and Pool6 should be used instead.
- Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len);
- static uint32_t getNextID() {
- static uint32_t id = 0;
- return (id++);
- }
- /// @brief subnet-id
- uint32_t id_;
- isc::asiolink::IOAddress prefix_;
- uint8_t prefix_len_;
- Pool6Collection pool_;
- };
- class Subnet6 : public Subnet {
- public:
- Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length);
- Pool6Ptr getPool6(const isc::asiolink::IOAddress& hint = isc::asiolink::IOAddress("::"));
- void addPool6(const Pool6Ptr& pool);
- const Pool6Collection& getPools() const {
- return pools_;
- }
- protected:
- /// collection of pools in that list
- Pool6Collection pools_;
- };
- typedef boost::shared_ptr<Subnet6> Subnet6Ptr;
- typedef std::vector<Subnet6Ptr> Subnet6Collection;
- class CfgMgr : public boost::noncopyable {
- public:
- static CfgMgr& instance();
- /// @brief get 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)
- Subnet6Ptr getSubnet6(const isc::asiolink::IOAddress& hint);
- /// @brief get subnet by interface-id
- ///
- /// Another possibility is to find a subnet based on interface-id.
- /// @todo This method is not currently supported.
- Subnet6Ptr getSubnet6(OptionPtr interfaceId);
- void addSubnet6(const Subnet6Ptr& subnet);
- protected:
- /// @brief Protected constructor.
- CfgMgr();
- virtual ~CfgMgr();
- Subnet6Collection subnets6_;
- };
- } // namespace isc::dhcp
- } // namespace isc
- #endif
|