host_mgr.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 HOST_MGR_H
  15. #define HOST_MGR_H
  16. #include <dhcp/duid.h>
  17. #include <dhcp/hwaddr.h>
  18. #include <dhcpsrv/base_host_data_source.h>
  19. #include <dhcpsrv/host.h>
  20. #include <dhcpsrv/subnet_id.h>
  21. #include <boost/noncopyable.hpp>
  22. #include <boost/scoped_ptr.hpp>
  23. #include <string>
  24. namespace isc {
  25. namespace dhcp {
  26. /// @brief Host Manager.
  27. ///
  28. /// This is a singleton class which provides access to multiple sources of
  29. /// information about static host reservations. These sources are also referred
  30. /// to as host data sources. Each source derives (directly or indirectly) from
  31. /// the @c BaseHostDataSource.
  32. ///
  33. /// The @c HostMgr is a central point for providing information about the host
  34. /// reservations. Internally, it relays the queries (calls to the appropriate
  35. /// methods declared in the @c BaseHostDataSource) to the data sources it is
  36. /// connected to. The @c HostMgr is always connected to the server's
  37. /// configuration, accessible through the @c CfgHosts object in the @c CfgMgr.
  38. /// The @c CfgHosts object holds all reservations specified in the DHCP server
  39. /// configuration file. If a particular reservation is not found in the
  40. /// @c CfgHosts object, the @c HostMgr will try to find it using the alternate
  41. /// host data storage. The alternate host data storage is usually a database
  42. /// (e.g. SQL database), accessible through a dedicated host data source
  43. /// object (a.k.a. database backend). This datasource is responsible for
  44. /// managing the connection with the database and forming appropriate queries
  45. /// to retrieve (or update) the information about the reservations.
  46. ///
  47. /// The use of the alternate host data source is optional and usually requires
  48. /// additional configuration to be specified by the server administrator.
  49. /// For example, for the SQL database the user's credentials, database address,
  50. /// and database name are required. The @c HostMgr passes these parameters
  51. /// to an appropriate datasource which is responsible for opening a connection
  52. /// and maintaining it.
  53. ///
  54. /// It is possible to switch to a different alternate data source or disable
  55. /// the use of the alternate datasource, e.g. as a result of server's
  56. /// reconfiguration. However, the use of the primary host data source (i.e.
  57. /// reservations specified in the configuration file) can't be disabled.
  58. ///
  59. /// @todo Implement alternate host data sources: MySQL, PostgreSQL, etc.
  60. class HostMgr : public boost::noncopyable, BaseHostDataSource {
  61. public:
  62. /// @brief Creates new instance of the @c HostMgr.
  63. ///
  64. /// If an instance of the @c HostMgr already exists, it will be replaced
  65. /// by the new instance. Thus, any instances of the alternate host data
  66. /// sources will be dropped.
  67. ///
  68. /// @param access Host data source access parameters for the alternate
  69. /// host data source. It holds "keyword=value" pairs, separated by spaces.
  70. /// The supported values are specific to the alternate data source in use.
  71. /// However, the "type" parameter will be common and it will specify which
  72. /// data source is to be used. Currently, no parameters are supported
  73. /// and the parameter is ignored.
  74. static void create(const std::string& access = "");
  75. /// @brief Returns a sole instance of the @c HostMgr.
  76. ///
  77. /// This method should be used to retrieve an instance of the @c HostMgr
  78. /// to be used to gather/manage host reservations. It returns an instance
  79. /// of the @c HostMgr created by the @c create method. If such instance
  80. /// doesn't exist yet, it is created using the @c create method with the
  81. /// default value of the data access string, which configures the host
  82. /// manager to not use the alternate host data source.
  83. static HostMgr& instance();
  84. /// @brief Returns all hosts for the specified HW address or DUID.
  85. ///
  86. /// This method returns all @c Host objects representing reservations for
  87. /// the specified HW address or/and DUID as documented in the
  88. /// @c BaseHostDataSource::getAll.
  89. ///
  90. /// It retrieves reservations from both primary and alternate host data
  91. /// source as a single collection of @c Host objects, i.e. if matching
  92. /// reservations are in both sources, all of them are returned.
  93. ///
  94. /// Note that returned collection may contain duplicates. It is the
  95. /// caller's responsibility to check for duplicates.
  96. ///
  97. /// @param hwaddr HW address of the client or NULL if no HW address
  98. /// available.
  99. /// @param duid client id or NULL of not available.
  100. ///
  101. /// @return Collection of const @c Host objects.
  102. virtual ConstHostCollection
  103. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const;
  104. /// @brief Returns a collection of hosts using the specified IPv4 address.
  105. ///
  106. /// This method may return multiple @c Host objects if they are connected to
  107. /// different subnets.
  108. ///
  109. /// If matching reservations are both in the primary and the alternate
  110. /// data source, all of them are returned.
  111. ///
  112. /// @param address IPv4 address for which the @c Host object is searched.
  113. ///
  114. /// @return Collection of const @c Host objects.
  115. virtual ConstHostCollection
  116. getAll4(const asiolink::IOAddress& address) const;
  117. /// @brief Returns a host connected to the IPv4 subnet.
  118. ///
  119. /// This method returns a single reservation for the particular host
  120. /// (identified by the HW address or DUID) as documented in the
  121. /// @c BaseHostDataSource::get4.
  122. ///
  123. /// @param subnet_id Subnet identifier.
  124. /// @param hwaddr HW address of the client or NULL if no HW address
  125. /// available.
  126. /// @param duid client id or NULL if not available.
  127. ///
  128. /// @return Const @c Host object using a specified HW address or DUID.
  129. virtual ConstHostPtr
  130. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  131. const DuidPtr& duid = DuidPtr()) const;
  132. /// @brief Returns a host connected to the IPv4 subnet and having
  133. /// a reservation for a specified IPv4 address.
  134. ///
  135. /// This method returns a single reservation for the particular host
  136. /// (identified by the HW address or DUID) as documented in the
  137. /// @c BaseHostDataSource::get4.
  138. ///
  139. /// @param subnet_id Subnet identifier.
  140. /// @param address reserved IPv4 address.
  141. ///
  142. /// @return Const @c Host object using a specified IPv4 address.
  143. virtual ConstHostPtr
  144. get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  145. /// @brief Returns a host connected to the IPv6 subnet.
  146. ///
  147. /// This method returns a host connected to the IPv6 subnet and identified
  148. /// by the HW address or DUID, as described in the
  149. /// @c BaseHostDataSource::get6.
  150. ///
  151. /// @param subnet_id Subnet identifier.
  152. /// @param hwaddr HW address of the client or NULL if no HW address
  153. /// available.
  154. /// @param duid DUID or NULL if not available.
  155. ///
  156. /// @return Const @c Host object using a specified HW address or DUID.
  157. virtual ConstHostPtr
  158. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  159. const HWAddrPtr& hwaddr = HWAddrPtr()) const;
  160. /// @brief Returns a host using the specified IPv6 prefix.
  161. ///
  162. /// This method returns a host using specified IPv6 prefix, as described
  163. /// in the @c BaseHostDataSource::get6.
  164. ///
  165. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  166. /// @param prefix_len IPv6 prefix length.
  167. ///
  168. /// @return Const @c Host object using a specified HW address or DUID.
  169. virtual ConstHostPtr
  170. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
  171. /// @brief Returns a host from specific subnet and reserved address.
  172. ///
  173. /// @param subnet_id subnet identfier.
  174. /// @param addr specified address.
  175. ///
  176. /// @return Const @c host object that has a reservation for specified address.
  177. virtual ConstHostPtr
  178. get6(const SubnetID& subnet_id, const asiolink::IOAddress& addr) const;
  179. /// @brief Adds a new host to the alternate data source.
  180. ///
  181. /// This method will throw an exception if no alternate data source is
  182. /// in use.
  183. ///
  184. /// @param host Pointer to the new @c Host object being added.
  185. virtual void add(const HostPtr& host);
  186. /// @brief Return backend type
  187. ///
  188. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  189. ///
  190. /// @return Type of the backend.
  191. virtual std::string getType() const {
  192. return (std::string("host_mgr"));
  193. }
  194. /// @brief Returns pointer to the host data source
  195. ///
  196. /// May return NULL
  197. /// @return pointer to the host data source (or NULL)
  198. HostDataSourcePtr getHostDataSource() const {
  199. return (alternate_source_);
  200. }
  201. private:
  202. /// @brief Private default constructor.
  203. HostMgr() { }
  204. /// @brief Pointer to an alternate host data source.
  205. ///
  206. /// If this pointer is NULL, the source is not in use.
  207. HostDataSourcePtr alternate_source_;
  208. /// @brief Returns a pointer to the currently used instance of the
  209. /// @c HostMgr.
  210. static boost::scoped_ptr<HostMgr>& getHostMgrPtr();
  211. };
  212. }
  213. }
  214. #endif // HOST_MGR_H