cfg_hosts.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright (C) 2014-2015 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 CFG_HOSTS_H
  15. #define CFG_HOSTS_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/hwaddr.h>
  19. #include <dhcpsrv/base_host_data_source.h>
  20. #include <dhcpsrv/host.h>
  21. #include <dhcpsrv/host_container.h>
  22. #include <dhcpsrv/subnet_id.h>
  23. #include <dhcpsrv/writable_host_data_source.h>
  24. #include <boost/shared_ptr.hpp>
  25. #include <vector>
  26. namespace isc {
  27. namespace dhcp {
  28. /// @brief Represents the host reservations specified in the configuration file.
  29. ///
  30. /// This class holds a collection of the host reservations (@c Host objects)
  31. /// which can be retrieved using different search criteria.
  32. ///
  33. /// In the typical case the reservations are searched using the client's MAC
  34. /// address or DUID and a subnet that the client is connected to. The
  35. /// reservations can be also retrieved using other parameters, such as reserved
  36. /// IP address.
  37. ///
  38. /// The reservations are added to this object by the configuration parsers,
  39. /// when the new configuration is applied for the server. The reservations
  40. /// are retrieved by the @c HostMgr class when the server is allocating or
  41. /// renewing an address or prefix for the particular client.
  42. class CfgHosts : public BaseHostDataSource, public WritableHostDataSource {
  43. public:
  44. /// @brief Destructor.
  45. virtual ~CfgHosts() { }
  46. /// @brief Return all hosts for the specified HW address or DUID.
  47. ///
  48. /// This method returns all @c Host objects which represent reservations
  49. /// for the specified HW address or DUID. Note, that this method may
  50. /// return multiple reservations because a particular client may have
  51. /// reservations in multiple subnets and the same client may be identified
  52. /// by HW address or DUID. The server is unable to verify that the specific
  53. /// DUID and HW address belong to the same client, until the client sends
  54. /// a DHCP message.
  55. ///
  56. /// @param hwaddr HW address of the client or NULL if no HW address
  57. /// available.
  58. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  59. ///
  60. /// @return Collection of const @c Host objects.
  61. virtual ConstHostCollection
  62. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const;
  63. /// @brief Non-const version of the @c getAll const method.
  64. ///
  65. /// @param hwaddr HW address of the client or NULL if no HW address
  66. /// available.
  67. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  68. ///
  69. /// @return Collection of non-const @c Host objects.
  70. virtual HostCollection
  71. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr());
  72. /// @brief Returns a collection of hosts using the specified IPv4 address.
  73. ///
  74. /// This method may return multiple @c Host objects if they are connected
  75. /// to different subnets.
  76. ///
  77. /// @param address IPv4 address for which the @c Host object is searched.
  78. ///
  79. /// @return Collection of const @c Host objects.
  80. virtual ConstHostCollection
  81. getAll4(const asiolink::IOAddress& address) const;
  82. /// @brief Returns a collection of hosts using the specified IPv4 address.
  83. ///
  84. /// This method may return multiple @c Host objects if they are connected
  85. /// to different subnets.
  86. ///
  87. /// @param address IPv4 address for which the @c Host object is searched.
  88. ///
  89. /// @return Collection of const @c Host objects.
  90. virtual HostCollection
  91. getAll4(const asiolink::IOAddress& address);
  92. /// @brief Returns a collection of hosts using the specified IPv6 address.
  93. ///
  94. /// This method may return multiple @c Host objects if they are connected
  95. /// to different subnets.
  96. ///
  97. /// @param address IPv6 address for which the @c Host object is searched.
  98. ///
  99. /// @return Collection of const @c Host objects.
  100. virtual ConstHostCollection
  101. getAll6(const asiolink::IOAddress& address) const;
  102. /// @brief Returns a collection of hosts using the specified IPv6 address.
  103. ///
  104. /// This method may return multiple @c Host objects if they are connected
  105. /// to different subnets.
  106. ///
  107. /// @param address IPv6 address for which the @c Host object is searched.
  108. ///
  109. /// @return Collection of const @c Host objects.
  110. virtual HostCollection
  111. getAll6(const asiolink::IOAddress& address);
  112. /// @brief Returns a host connected to the IPv4 subnet and matching
  113. /// specified identifiers.
  114. ///
  115. /// @param subnet_id Subnet identifier.
  116. /// @param hwaddr HW address of the client or NULL if no HW address
  117. /// available.
  118. /// @param duid client id or NULL if not available.
  119. ///
  120. /// @return Const @c Host object using a specified HW address or DUID.
  121. /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
  122. /// been found.
  123. virtual ConstHostPtr
  124. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  125. const DuidPtr& duid = DuidPtr()) const;
  126. /// @brief Returns a host connected to the IPv4 subnet and matching
  127. /// specified identifiers.
  128. ///
  129. /// @param subnet_id Subnet identifier.
  130. /// @param hwaddr HW address of the client or NULL if no HW address
  131. /// available.
  132. /// @param duid client id or NULL if not available.
  133. ///
  134. /// @return Non-const @c Host object using a specified HW address or DUID.
  135. /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
  136. /// been found.
  137. virtual HostPtr
  138. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  139. const DuidPtr& duid = DuidPtr());
  140. /// @brief Returns a host connected to the IPv4 subnet and having
  141. /// a reservation for a specified IPv4 address.
  142. ///
  143. /// @param subnet_id Subnet identifier.
  144. /// @param address reserved IPv4 address.
  145. ///
  146. /// @return Const @c Host object using a specified IPv4 address.
  147. virtual ConstHostPtr
  148. get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  149. /// @brief Returns a host connected to the IPv6 subnet and matching
  150. /// the specified identifiers.
  151. ///
  152. /// @param subnet_id Subnet identifier.
  153. /// @param hwaddr HW address of the client or NULL if no HW address
  154. /// available.
  155. /// @param duid DUID or NULL if not available.
  156. ///
  157. /// @return Const @c Host object using a specified HW address or DUID.
  158. /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
  159. /// been found.
  160. virtual ConstHostPtr
  161. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  162. const HWAddrPtr& hwaddr = HWAddrPtr()) const;
  163. /// @brief Returns a host connected to the IPv6 subnet and matching the
  164. /// specified identifiers.
  165. ///
  166. /// @param subnet_id Subnet identifier.
  167. /// @param hwaddr HW address of the client or NULL if no HW address
  168. /// available.
  169. /// @param duid DUID or NULL if not available.
  170. ///
  171. /// @return Non-const @c Host object using a specified HW address or DUID.
  172. /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
  173. /// been found.
  174. virtual HostPtr
  175. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  176. const HWAddrPtr& hwaddr = HWAddrPtr());
  177. /// @brief Returns a host using the specified IPv6 prefix.
  178. ///
  179. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  180. /// @param prefix_len IPv6 prefix length.
  181. ///
  182. /// @throw isc::NotImplemented
  183. virtual ConstHostPtr
  184. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
  185. /// @brief Returns a host using the specified IPv6 prefix.
  186. ///
  187. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  188. /// @param prefix_len IPv6 prefix length.
  189. ///
  190. /// @throw isc::NotImplemented
  191. virtual HostPtr
  192. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len);
  193. /// @brief Returns a host connected to the IPv6 subnet and having
  194. /// a reservation for a specified IPv6 address.
  195. ///
  196. /// @param subnet_id Subnet identifier.
  197. /// @param address reserved IPv6 address.
  198. ///
  199. /// @return Const @c Host object using a specified IPv6 address.
  200. virtual ConstHostPtr
  201. get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  202. /// @brief Returns a host connected to the IPv6 subnet and having
  203. /// a reservation for a specified IPv6 address.
  204. ///
  205. /// @param subnet_id Subnet identifier.
  206. /// @param address reserved IPv6 address.
  207. ///
  208. /// @return Const @c Host object using a specified IPv6 address.
  209. virtual HostPtr
  210. get6(const SubnetID& subnet_id, const asiolink::IOAddress& address);
  211. /// @brief Adds a new host to the collection.
  212. ///
  213. /// @param host Pointer to the new @c Host object being added.
  214. ///
  215. /// @throw DuplicateHost If a host for a particular HW address or DUID
  216. /// has already been added to the IPv4 or IPv6 subnet.
  217. virtual void add(const HostPtr& host);
  218. /// @brief Return backend type
  219. ///
  220. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  221. ///
  222. /// @return Type of the backend.
  223. virtual std::string getType() const {
  224. return (std::string("configuration file"));
  225. }
  226. private:
  227. /// @brief Returns @c Host objects for the specific identifier and type.
  228. ///
  229. /// This private method is called by the @c CfgHosts::getAllInternal
  230. /// method which finds the @c Host objects using HW address or DUID.
  231. /// The retrieved objects are appended to the @c storage container.
  232. ///
  233. /// @param identifier Binary representation of the HW addres or DUID (or
  234. /// other future identifier).
  235. /// @param identifier_type The type of the supplied identifier.
  236. /// @param [out] storage Container to which the retreived objects are
  237. /// appended.
  238. /// @tparam One of the @c ConstHostCollection of @c HostCollection.
  239. template<typename Storage>
  240. void getAllInternal(const std::vector<uint8_t>& identifier,
  241. const Host::IdentifierType& identifier_type,
  242. Storage& storage) const;
  243. /// @brief Returns @c Host objects for the specified HW address or DUID.
  244. ///
  245. /// This private method is called by the @c CfgHosts::getAll methods to
  246. /// retrieve the @c Host objects using HW address or DUID. The retrieved
  247. /// objects are appended to the @c storage container.
  248. ///
  249. /// @param hwaddr HW address identifying a host.
  250. /// @param duid DUID identifying a host.
  251. /// @param [out] storage Container to which the retrieved objects are
  252. /// appended.
  253. /// @tparam One of the @c ConstHostCollection or @c HostCollection.
  254. template<typename Storage>
  255. void getAllInternal(const HWAddrPtr& hwaddr, const DuidPtr& duid,
  256. Storage& storage) const;
  257. /// @brief Returns @c Host objects for the specified IPv4 address.
  258. ///
  259. /// This private method is called by the @c CfgHosts::getAll4 methods
  260. /// to retrieve the @c Host for which the specified IPv4 address is
  261. /// reserved. The retrieved objects are appended to the @c storage
  262. /// container.
  263. ///
  264. /// @param address IPv4 address.
  265. /// @param [out] storage Container to which the retrieved objects are
  266. /// appended.
  267. /// @tparam One of the @c ConstHostCollection or @c HostCollection.
  268. template<typename Storage>
  269. void getAllInternal4(const asiolink::IOAddress& address,
  270. Storage& storage) const;
  271. /// @brief Returns @c Host objects for the specified IPv6 address.
  272. ///
  273. /// This private method is called by the @c CfgHosts::getAll6 methods
  274. /// to retrieve the @c Host for which the specified IPv6 address is
  275. /// reserved. The retrieved objects are appended to the @c storage
  276. /// container.
  277. ///
  278. /// @param address IPv6 address.
  279. /// @param [out] storage Container to which the retrieved objects are
  280. /// appended.
  281. /// @tparam One of the @c ConstHostCollection or @c HostCollection.
  282. template<typename Storage>
  283. void getAllInternal6(const asiolink::IOAddress& address,
  284. Storage& storage) const;
  285. /// @brief Returns @c Host objects for the specified (Subnet-id,IPv6 address) tuple.
  286. ///
  287. /// This private method is called by the @c CfgHosts::getAll6 methods
  288. /// to retrieve the @c Host for which the specified IPv6 address is
  289. /// reserved and is in specified subnet-id. The retrieved objects are
  290. /// appended to the @c storage container.
  291. ///
  292. /// @param subnet_id Subnet Identifier.
  293. /// @param address IPv6 address.
  294. /// @param [out] storage Container to which the retrieved objects are
  295. /// appended.
  296. /// @tparam One of the @c ConstHostCollection or @c HostCollection.
  297. template<typename Storage>
  298. void
  299. getAllInternal6(const SubnetID& subnet_id,
  300. const asiolink::IOAddress& address,
  301. Storage& storage) const;
  302. /// @brief Returns @c Host object connected to a subnet.
  303. ///
  304. /// This private method returns a pointer to the @c Host object identified
  305. /// by the HW address or DUID and connected to an IPv4 or IPv6 subnet.
  306. ///
  307. /// @param subnet_id IPv4 or IPv6 subnet identifier.
  308. /// @param subnet6 A boolean flag which indicates if the subnet identifier
  309. /// points to a IPv4 (if false) or IPv6 subnet (if true).
  310. /// @param hwaddr HW address identifying a host.
  311. /// @param duid DUID identifying a host.
  312. ///
  313. /// @return Pointer to the found host, or NULL if no host found.
  314. /// @throw isc::dhcp::DuplicateHost if method found more than one matching
  315. /// @c Host object.
  316. HostPtr getHostInternal(const SubnetID& subnet_id,
  317. const bool subnet6,
  318. const HWAddrPtr& hwaddr,
  319. const DuidPtr& duid) const;
  320. /// @brief Returns the @c Host object holding reservation for the IPv6
  321. /// address and connected to the specific subnet.
  322. ///
  323. /// This private method is called by the public @c get6 method variants.
  324. ///
  325. /// @param subnet_id IPv6 subnet identifier.
  326. /// @param address IPv6 address.
  327. /// @tparam ReturnType One of @c HostPtr or @c ConstHostPtr
  328. /// @tparam One of the @c ConstHostCollection or @c HostCollection.
  329. ///
  330. /// @return Pointer to the found host, or NULL if no host found.
  331. /// @throw isc::dhcp::DuplicateHost if method found more than one matching
  332. /// @c Host object.
  333. template<typename ReturnType, typename Storage>
  334. ReturnType getHostInternal6(const SubnetID& subnet_id,
  335. const asiolink::IOAddress& adddress) const;
  336. /// @brief Adds a new host to the v4 collection.
  337. ///
  338. /// This is an internal method called by public @ref add.
  339. ///
  340. /// @param host Pointer to the new @c Host object being added.
  341. ///
  342. /// @throw DuplicateHost If a host for a particular HW address or DUID
  343. /// has already been added to the IPv4 subnet.
  344. virtual void add4(const HostPtr& host);
  345. /// @brief Adds a new host to the v6 collection.
  346. ///
  347. /// This is an internal method called by public @ref add.
  348. ///
  349. /// @param host Pointer to the new @c Host object being added.
  350. ///
  351. /// @throw DuplicateHost If a host for a particular HW address or DUID
  352. /// or for the particular address or prefix has already been added to
  353. /// the IPv6 subnet.
  354. virtual void add6(const HostPtr& host);
  355. /// @brief Multi-index container holding @c Host objects.
  356. ///
  357. /// It can be used for finding hosts by the following criteria:
  358. /// - IPv4 address
  359. /// - DUID
  360. /// - HW/MAC address
  361. HostContainer hosts_;
  362. /// @brief Multi-index container holding @c Host objects with v6 reservations.
  363. ///
  364. /// It can be used for finding hosts by the following criteria:
  365. /// - IPv6 address
  366. /// - IPv6 prefix
  367. HostContainer6 hosts6_;
  368. };
  369. /// @name Pointers to the @c CfgHosts objects.
  370. //@{
  371. /// @brief Non-const pointer.
  372. typedef boost::shared_ptr<CfgHosts> CfgHostsPtr;
  373. /// @brief Const pointer.
  374. typedef boost::shared_ptr<const CfgHosts> ConstCfgHostsPtr;
  375. //@}
  376. }
  377. }
  378. #endif // CFG_HOSTS_H