cfgmgr.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright (C) 2012 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 <string>
  17. #include <map>
  18. #include <vector>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/noncopyable.hpp>
  21. #include <asiolink/io_address.h>
  22. #include <util/buffer.h>
  23. #include <dhcp/option.h>
  24. namespace isc {
  25. namespace dhcp {
  26. class Pool6;
  27. class Subnet6;
  28. /// @brief this class specifes parameter value
  29. ///
  30. /// This class is used to store configuration parameters, like lifetime or T1.
  31. /// It defines 3 parameters: min/default/max values. There are 2 constructors:
  32. /// - simple (just one value that sets all parameters)
  33. /// - extended (that sets default value and two thresholds)
  34. template <class T>
  35. class Triplet {
  36. public:
  37. /// @brief base type to Triple conversion
  38. ///
  39. /// Typically: uint32_t to Triplet assignment. It is very convenient
  40. /// to be able to simply write Triplet<uint32_t> x = 7;
  41. Triplet<T>& operator = (T base_type) {
  42. return Triplet<T>(base_type);
  43. }
  44. /// @brief triplet to base type conversion
  45. ///
  46. /// Typically: Triplet to uint32_t assignment. It is very convenient
  47. /// to be able to simply write uint32_t z = x; (where x is a Triplet)
  48. operator T () const {
  49. return (default_);
  50. }
  51. /// @brief sets a fixed value
  52. Triplet(T value)
  53. :min_(value), default_(value), max_(value) {
  54. }
  55. /// @brief sets the default value and thresholds
  56. ///
  57. /// @throw BadValue if min <= def <= max rule is violated
  58. Triplet(T min, T def, T max)
  59. :min_(min), default_(def), max_(max) {
  60. if ( (min_>def) || (def > max_) ) {
  61. isc_throw(BadValue, "Invalid triplet values.");
  62. }
  63. }
  64. /// @brief returns a minimum allowed value
  65. T getMin() const { return min_;}
  66. /// @brief returns the default value
  67. T get() const { return default_;}
  68. /// @brief returns value with a hint
  69. ///
  70. /// DHCP protocol treats any values sent by a client as hints.
  71. /// This is a method that implements that. We can assign any value
  72. /// from configured range that client asks.
  73. T get(T hint) const {
  74. if (hint <= min_) {
  75. return (min_);
  76. }
  77. if (hint >= max_) {
  78. return (max_);
  79. }
  80. return (hint);
  81. }
  82. /// @brief returns a maximum allowed value
  83. T getMax() const { return max_; }
  84. protected:
  85. /// @brief the minimum value
  86. T min_;
  87. /// @brief the default value
  88. T default_;
  89. /// @brief the maximum value
  90. T max_;
  91. };
  92. class Pool {
  93. public:
  94. uint32_t getId() const {
  95. return (id_);
  96. }
  97. Triplet<uint32_t> getValid() const {
  98. return (valid_);
  99. }
  100. const isc::asiolink::IOAddress& getFirstAddress() const {
  101. return (first_);
  102. }
  103. const isc::asiolink::IOAddress& getLastAddress() const {
  104. return (last_);
  105. }
  106. Triplet<uint32_t> getT1() const {
  107. return (t1_);
  108. }
  109. Triplet<uint32_t> getT2() const {
  110. return (t2_);
  111. }
  112. /// @brief checks if specified address is in range
  113. bool inRange(const isc::asiolink::IOAddress& addr);
  114. protected:
  115. /// @brief protected constructor
  116. Pool(const isc::asiolink::IOAddress& first,
  117. const isc::asiolink::IOAddress& last,
  118. const Triplet<uint32_t>& t1,
  119. const Triplet<uint32_t>& t2,
  120. const Triplet<uint32_t>& valid_lifetime);
  121. static uint32_t getNextID() {
  122. static uint32_t id = 0;
  123. return (id++);
  124. }
  125. /// @brief pool-id
  126. ///
  127. /// This ID is used to indentify this specific pool.
  128. uint32_t id_;
  129. isc::asiolink::IOAddress first_;
  130. isc::asiolink::IOAddress last_;
  131. Triplet<uint32_t> t1_;
  132. Triplet<uint32_t> t2_;
  133. Triplet<uint32_t> valid_;
  134. std::string comments_;
  135. ///uint128_t available_leases_;
  136. ///uint128_t total_leases_;
  137. };
  138. class Pool6 : public Pool {
  139. public:
  140. typedef enum {
  141. TYPE_IA,
  142. TYPE_TA,
  143. TYPE_PD
  144. } Pool6Type;
  145. Pool6(Pool6Type type, const isc::asiolink::IOAddress& first,
  146. const isc::asiolink::IOAddress& last,
  147. const Triplet<uint32_t>& t1,
  148. const Triplet<uint32_t>& t2,
  149. const Triplet<uint32_t>& preferred_lifetime,
  150. const Triplet<uint32_t>& valid_lifetime);
  151. Pool6(Pool6Type type, const isc::asiolink::IOAddress& addr,
  152. uint8_t prefix_len,
  153. const Triplet<uint32_t>& t1,
  154. const Triplet<uint32_t>& t2,
  155. const Triplet<uint32_t>& preferred_lifetime,
  156. const Triplet<uint32_t>& valid_lifetime);
  157. Pool6Type getType() const {
  158. return (type_);
  159. }
  160. Triplet<uint32_t> getPreferred() const {
  161. return (preferred_);
  162. }
  163. protected:
  164. Pool6Type type_;
  165. /// @brief prefix length
  166. /// used by TYPE_PD only (zeroed for other types)
  167. uint8_t prefix_len_;
  168. Triplet<uint32_t> preferred_;
  169. };
  170. typedef boost::shared_ptr<Pool> PoolPtr;
  171. typedef boost::shared_ptr<Pool6> Pool6Ptr;
  172. typedef std::vector<Pool6Ptr> Pool6Collection;
  173. class Subnet {
  174. public:
  175. /// @brief checks if specified address is in range
  176. bool inRange(const isc::asiolink::IOAddress& addr);
  177. protected:
  178. /// @brief protected constructor
  179. //
  180. /// By making the constructor protected, we make sure that noone will
  181. /// ever instantiate that class. Pool4 and Pool6 should be used instead.
  182. Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len);
  183. static uint32_t getNextID() {
  184. static uint32_t id = 0;
  185. return (id++);
  186. }
  187. /// @brief subnet-id
  188. uint32_t id_;
  189. isc::asiolink::IOAddress prefix_;
  190. uint8_t prefix_len_;
  191. Pool6Collection pool_;
  192. };
  193. class Subnet6 : public Subnet {
  194. public:
  195. Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length);
  196. Pool6Ptr getPool6(const isc::asiolink::IOAddress& hint = isc::asiolink::IOAddress("::"));
  197. void addPool6(const Pool6Ptr& pool);
  198. const Pool6Collection& getPools() const {
  199. return pools_;
  200. }
  201. protected:
  202. /// collection of pools in that list
  203. Pool6Collection pools_;
  204. };
  205. typedef boost::shared_ptr<Subnet6> Subnet6Ptr;
  206. typedef std::vector<Subnet6Ptr> Subnet6Collection;
  207. class CfgMgr : public boost::noncopyable {
  208. public:
  209. static CfgMgr& instance();
  210. /// @brief get subnet by address
  211. ///
  212. /// Finds a matching subnet, based on an address. This can be used
  213. /// in two cases: when trying to find an appropriate lease based on
  214. /// a) relay link address (that must be the address that is on link)
  215. /// b) our global address on the interface the message was received on
  216. /// (for directly connected clients)
  217. Subnet6Ptr getSubnet6(const isc::asiolink::IOAddress& hint);
  218. /// @brief get subnet by interface-id
  219. ///
  220. /// Another possibility is to find a subnet based on interface-id.
  221. /// @todo This method is not currently supported.
  222. Subnet6Ptr getSubnet6(OptionPtr interfaceId);
  223. void addSubnet6(const Subnet6Ptr& subnet);
  224. protected:
  225. /// @brief Protected constructor.
  226. CfgMgr();
  227. virtual ~CfgMgr();
  228. Subnet6Collection subnets6_;
  229. };
  230. } // namespace isc::dhcp
  231. } // namespace isc
  232. #endif