pool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef POOL_H
  7. #define POOL_H
  8. #include <asiolink/io_address.h>
  9. #include <dhcp/option6_pdexclude.h>
  10. #include <boost/shared_ptr.hpp>
  11. #include <cc/data.h>
  12. #include <dhcpsrv/cfg_option.h>
  13. #include <dhcpsrv/lease.h>
  14. #include <boost/shared_ptr.hpp>
  15. #include <vector>
  16. namespace isc {
  17. namespace dhcp {
  18. /// @brief base class for Pool4 and Pool6
  19. ///
  20. /// Stores information about pool of IPv4 or IPv6 addresses.
  21. /// That is a basic component of a configuration.
  22. class Pool {
  23. public:
  24. /// @note:
  25. /// PoolType enum was removed. Please use Lease::Type instead
  26. /// @brief returns Pool-id
  27. ///
  28. /// @return pool-id value
  29. /// Pool-id is an unique value that can be used to identify a pool.
  30. uint32_t getId() const {
  31. return (id_);
  32. }
  33. /// @brief Returns the first address in a pool.
  34. ///
  35. /// @return first address in a pool
  36. const isc::asiolink::IOAddress& getFirstAddress() const {
  37. return (first_);
  38. }
  39. /// @brief Returns the last address in a pool.
  40. /// @return last address in a pool
  41. const isc::asiolink::IOAddress& getLastAddress() const {
  42. return (last_);
  43. }
  44. /// @brief Checks if a given address is in the range.
  45. ///
  46. /// @return true, if the address is in pool
  47. bool inRange(const isc::asiolink::IOAddress& addr) const;
  48. /// @brief Returns pool type (v4, v6 non-temporary, v6 temp, v6 prefix)
  49. /// @return returns pool type
  50. Lease::Type getType() const {
  51. return (type_);
  52. }
  53. /// @brief returns textual representation of the pool
  54. ///
  55. /// @return textual representation
  56. virtual std::string toText() const;
  57. /// @brief virtual destructor
  58. ///
  59. /// We need Pool to be a polymorphic class, so we could dynamic cast
  60. /// from PoolPtr to Pool6Ptr if we need to. A class becomes polymorphic,
  61. /// when there is at least one virtual method.
  62. virtual ~Pool() {
  63. }
  64. /// @brief Returns the number of all leases in this pool.
  65. ///
  66. /// Note that this is the upper bound, assuming that no leases are used
  67. /// and there are no host reservations. This is just a theoretical calculation.
  68. /// @return number of possible leases in this pool
  69. uint64_t getCapacity() const {
  70. return (capacity_);
  71. }
  72. /// @brief Returns pointer to the option data configuration for this pool.
  73. CfgOptionPtr getCfgOption() {
  74. return (cfg_option_);
  75. }
  76. /// @brief Returns const pointer to the option data configuration for
  77. /// this pool.
  78. ConstCfgOptionPtr getCfgOption() const {
  79. return (cfg_option_);
  80. }
  81. /// @brief Returns const pointer to the user context.
  82. data::ConstElementPtr getContext() const {
  83. return (user_context_);
  84. }
  85. /// @brief Sets user context.
  86. /// @param ctx user context to be stored.
  87. void setUserContext(const data::ConstElementPtr& ctx) {
  88. user_context_ = ctx;
  89. }
  90. protected:
  91. /// @brief protected constructor
  92. ///
  93. /// This constructor is protected to prevent anyone from instantiating
  94. /// Pool class directly. Instances of Pool4 and Pool6 should be created
  95. /// instead.
  96. ///
  97. /// @param type type of lease that will be served from this pool
  98. /// @param first first address of a range
  99. /// @param last last address of a range
  100. Pool(Lease::Type type,
  101. const isc::asiolink::IOAddress& first,
  102. const isc::asiolink::IOAddress& last);
  103. /// @brief returns the next unique Pool-ID
  104. ///
  105. /// @return the next unique Pool-ID
  106. static uint32_t getNextID() {
  107. static uint32_t id = 0;
  108. return (id++);
  109. }
  110. /// @brief pool-id
  111. ///
  112. /// This ID is used to identify this specific pool.
  113. uint32_t id_;
  114. /// @brief The first address in a pool
  115. isc::asiolink::IOAddress first_;
  116. /// @brief The last address in a pool
  117. isc::asiolink::IOAddress last_;
  118. /// @brief Comments field
  119. ///
  120. /// @todo: This field is currently not used.
  121. std::string comments_;
  122. /// @brief defines a lease type that will be served from this pool
  123. Lease::Type type_;
  124. /// @brief Stores number of possible leases.
  125. ///
  126. /// This could be calculated on the fly, but the calculations are somewhat
  127. /// involved, so it is more efficient to calculate it once and just store
  128. /// the result. Note that for very large pools, the number is capped at
  129. /// max value of uint64_t.
  130. uint64_t capacity_;
  131. /// @brief Pointer to the option data configuration for this pool.
  132. CfgOptionPtr cfg_option_;
  133. /// @brief Pointer to the user context (may be NULL)
  134. data::ConstElementPtr user_context_;
  135. };
  136. /// @brief Pool information for IPv4 addresses
  137. ///
  138. /// It holds information about pool4, i.e. a range of IPv4 address space that
  139. /// is configured for DHCP allocation.
  140. class Pool4 : public Pool {
  141. public:
  142. /// @brief the constructor for Pool4 "min-max" style definition
  143. ///
  144. /// @param first the first address in a pool
  145. /// @param last the last address in a pool
  146. Pool4(const isc::asiolink::IOAddress& first,
  147. const isc::asiolink::IOAddress& last);
  148. /// @brief the constructor for Pool4 "prefix/len" style definition
  149. ///
  150. /// @param prefix specifies prefix of the pool
  151. /// @param prefix_len specifies length of the prefix of the pool
  152. Pool4(const isc::asiolink::IOAddress& prefix,
  153. uint8_t prefix_len);
  154. };
  155. /// @brief a pointer an IPv4 Pool
  156. typedef boost::shared_ptr<Pool4> Pool4Ptr;
  157. /// @brief Pool information for IPv6 addresses and prefixes
  158. ///
  159. /// It holds information about pool6, i.e. a range of IPv6 address space that
  160. /// is configured for DHCP allocation.
  161. class Pool6 : public Pool {
  162. public:
  163. /// @brief the constructor for Pool6 "min-max" style definition
  164. ///
  165. /// @throw BadValue if PD is define (PD can be only prefix/len)
  166. ///
  167. /// @param type type of the pool (IA or TA)
  168. /// @param first the first address in a pool
  169. /// @param last the last address in a pool
  170. Pool6(Lease::Type type, const isc::asiolink::IOAddress& first,
  171. const isc::asiolink::IOAddress& last);
  172. /// @brief the constructor for Pool6 "prefix/len" style definition
  173. ///
  174. /// For addressed, this is just a prefix/len definition. For prefixes,
  175. /// there is one extra additional parameter delegated_len. It specifies
  176. /// a size of delegated prefixes that the pool will be split into. For
  177. /// example pool 2001:db8::/56, delegated_len=64 means that there is a
  178. /// pool 2001:db8::/56. It will be split into 256 prefixes of length /64,
  179. /// e.g. 2001:db8:0:1::/64, 2001:db8:0:2::/64 etc.
  180. ///
  181. /// Naming convention:
  182. /// A smaller prefix length yields a shorter prefix which describes a larger
  183. /// set of addresses. A larger length yields a longer prefix which describes
  184. /// a smaller set of addresses.
  185. ///
  186. /// Obviously, prefix_len must define shorter or equal prefix length than
  187. /// delegated_len, so prefix_len <= delegated_len. Note that it is slightly
  188. /// confusing: bigger (larger) prefix actually has smaller prefix length,
  189. /// e.g. /56 is a bigger prefix than /64, but has shorter (smaller) prefix
  190. /// length.
  191. ///
  192. /// @throw BadValue if delegated_len is defined for non-PD types or
  193. /// when delegated_len < prefix_len
  194. ///
  195. /// @param type type of the pool (IA, TA or PD)
  196. /// @param prefix specifies prefix of the pool
  197. /// @param prefix_len specifies prefix length of the pool
  198. /// @param delegated_len specifies length of the delegated prefixes
  199. Pool6(Lease::Type type, const isc::asiolink::IOAddress& prefix,
  200. uint8_t prefix_len, uint8_t delegated_len = 128);
  201. /// @brief Constructor for DHCPv6 prefix pool with an excluded prefix.
  202. ///
  203. /// If @c excluded_prefix is equal to '::' and the @c excluded_prefix_len
  204. /// is equal to 0, the excluded prefix is assumed to be unspecified for
  205. /// the pool. In this case, the server will not send the Prefix Exclude
  206. /// option to a client.
  207. ///
  208. /// @param prefix specified a prefix of the pool.
  209. /// @param prefix_len specifies prefix length of the pool.
  210. /// @param delegated_len specifies length of the delegated prefixes.
  211. /// @param excluded_prefix specifies an excluded prefix as per RFC6603.
  212. /// @param excluded_prefix_len specifies length of an excluded prefix.
  213. Pool6(const asiolink::IOAddress& prefix, const uint8_t prefix_len,
  214. const uint8_t delegated_len,
  215. const asiolink::IOAddress& excluded_prefix,
  216. const uint8_t excluded_prefix_len);
  217. /// @brief returns pool type
  218. ///
  219. /// @return pool type
  220. Lease::Type getType() const {
  221. return (type_);
  222. }
  223. /// @brief returns delegated prefix length
  224. ///
  225. /// This may be useful for "prefix/len" style definition for
  226. /// addresses, but is mostly useful for prefix pools.
  227. /// @return prefix length (1-128)
  228. uint8_t getLength() const {
  229. return (prefix_len_);
  230. }
  231. /// @brief Returns instance of the pool specific Prefix Exclude option.
  232. ///
  233. /// @return An instance of the Prefix Exclude option (RFC 6603) or NULL
  234. /// if such option hasn't been specified for the pool.
  235. Option6PDExcludePtr getPrefixExcludeOption() const {
  236. return (pd_exclude_option_);
  237. }
  238. /// @brief returns textual representation of the pool
  239. ///
  240. /// @return textual representation
  241. virtual std::string toText() const;
  242. private:
  243. /// @brief Generic method initializing a DHCPv6 pool.
  244. ///
  245. /// This method should be called by the constructors to initialize
  246. /// DHCPv6 pools.
  247. ///
  248. /// @param Lease/pool type.
  249. /// @param prefix An address or delegated prefix (depending on the
  250. /// pool type specified as @c type).
  251. /// @param prefix_len Prefix length. If a pool is an address pool,
  252. /// this value should be set to 128.
  253. /// @param delegated_len Length of the delegated prefixes. If a pool
  254. /// is an address pool, this value should be set to 128.
  255. /// @param excluded_prefix An excluded prefix as per RFC6603. This
  256. /// value should only be specified for prefix pools. The value of
  257. /// '::' means "unspecified".
  258. /// @param excluded_prefix_len Length of the excluded prefix. This
  259. /// is only specified for prefix pools. The value of 0 should be
  260. /// used when @c excluded_prefix is not specified.
  261. void init(const Lease::Type& type,
  262. const asiolink::IOAddress& prefix,
  263. const uint8_t prefix_len,
  264. const uint8_t delegated_len,
  265. const asiolink::IOAddress& excluded_prefix,
  266. const uint8_t excluded_prefix_len);
  267. /// @brief Defines prefix length (for TYPE_PD only)
  268. uint8_t prefix_len_;
  269. /// @brief A pointer to the Prefix Exclude option (RFC 6603).
  270. Option6PDExcludePtr pd_exclude_option_;
  271. };
  272. /// @brief a pointer an IPv6 Pool
  273. typedef boost::shared_ptr<Pool6> Pool6Ptr;
  274. /// @brief a pointer to either IPv4 or IPv6 Pool
  275. typedef boost::shared_ptr<Pool> PoolPtr;
  276. /// @brief a container for either IPv4 or IPv6 Pools
  277. typedef std::vector<PoolPtr> PoolCollection;
  278. } // end of isc::dhcp namespace
  279. } // end of isc namespace
  280. #endif // POOL_H