cfg_hosts.h 20 KB

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