radius_host_data_source.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright (C) 2015-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 RADIUS_HOST_DATA_SOURCE_H
  7. #define RADIUS_HOST_DATA_SOURCE_H
  8. #include <dhcpsrv/base_host_data_source.h>
  9. #include <dhcpsrv/db_exceptions.h>
  10. namespace isc {
  11. namespace dhcp {
  12. /// @brief MySQL Host Data Source
  13. ///
  14. /// This class implements the @ref isc::dhcp::BaseHostDataSource interface to
  15. /// a radius protocol.
  16. class RadiusHostDataSource: public BaseHostDataSource {
  17. public:
  18. /// @brief Constructor
  19. ///
  20. /// Uses the following keywords in the parameters passed to it to
  21. /// connect to the database:
  22. /// - password - Password for radius
  23. /// - host - Host to which to connect (optional, defaults to "localhost")
  24. /// - port - Port to witch to connect (optional, defaults to 1812)
  25. ///
  26. /// @param parameters A data structure relating keywords and values
  27. /// concerned with the database.
  28. ///
  29. /// @throw isc::dhcp::NoPassword Mandatory password not given
  30. /// @throw isc::dhcp::DbOpenError Error opening the database
  31. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  32. /// failed.
  33. RadiusHostDataSource(const DatabaseConnection::ParameterMap& parameters);
  34. /// @brief Virtual destructor.
  35. ///
  36. virtual ~RadiusHostDataSource();
  37. /// @brief Return all hosts for the specified HW address or DUID.
  38. ///
  39. /// This method returns all @c Host objects which represent reservations
  40. /// for the specified HW address or DUID. Note, that this method may
  41. /// return multiple reservations because a particular client may have
  42. /// reservations in multiple subnets and the same client may be identified
  43. /// by HW address or DUID. The server is unable to verify that the specific
  44. /// DUID and HW address belong to the same client, until the client sends
  45. /// a DHCP message.
  46. ///
  47. /// Specifying both hardware address and DUID is allowed for this method
  48. /// and results in returning all objects that are associated with hardware
  49. /// address OR duid. For example: if one host is associated with the
  50. /// specified hardware address and another host is associated with the
  51. /// specified DUID, two hosts will be returned.
  52. ///
  53. /// @param hwaddr HW address of the client or NULL if no HW address
  54. /// available.
  55. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  56. ///
  57. /// @return Collection of const @c Host objects.
  58. virtual ConstHostCollection
  59. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const;
  60. /// @brief Return all hosts connected to any subnet for which reservations
  61. /// have been made using a specified identifier.
  62. ///
  63. /// This method returns all @c Host objects which represent reservations
  64. /// for a specified identifier. This method may return multiple hosts
  65. /// because a particular client may have reservations in multiple subnets.
  66. ///
  67. /// @param identifier_type Identifier type.
  68. /// @param identifier_begin Pointer to a beginning of a buffer containing
  69. /// an identifier.
  70. /// @param identifier_len Identifier length.
  71. ///
  72. /// @return Collection of const @c Host objects.
  73. virtual ConstHostCollection
  74. getAll(const Host::IdentifierType& identifier_type,
  75. const uint8_t* identifier_begin, const size_t identifier_len) const;
  76. /// @brief Returns a collection of hosts using the specified IPv4 address.
  77. ///
  78. /// This method may return multiple @c Host objects if they are connected
  79. /// to different subnets.
  80. ///
  81. /// @param address IPv4 address for which the @c Host object is searched.
  82. ///
  83. /// @return Collection of const @c Host objects.
  84. virtual ConstHostCollection
  85. getAll4(const asiolink::IOAddress& address) const;
  86. /// @brief Returns a host connected to the IPv4 subnet.
  87. ///
  88. /// Implementations of this method should guard against the case when
  89. /// multiple instances of the @c Host are present, e.g. when two
  90. /// @c Host objects are found, one for the DUID, another one for the
  91. /// HW address. In such case, an implementation of this method
  92. /// should throw an MultipleRecords exception.
  93. ///
  94. /// @param subnet_id Subnet identifier.
  95. /// @param hwaddr HW address of the client or NULL if no HW address
  96. /// available.
  97. /// @param duid client id or NULL if not available.
  98. ///
  99. /// @return Const @c Host object using a specified HW address or DUID.
  100. virtual ConstHostPtr
  101. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  102. const DuidPtr& duid = DuidPtr()) const;
  103. /// @brief Returns a host connected to the IPv4 subnet.
  104. ///
  105. /// @param subnet_id Subnet identifier.
  106. /// @param identifier_type Identifier type.
  107. /// @param identifier_begin Pointer to a beginning of a buffer containing
  108. /// an identifier.
  109. /// @param identifier_len Identifier length.
  110. ///
  111. /// @return Const @c Host object for which reservation has been made using
  112. /// the specified identifier.
  113. virtual ConstHostPtr
  114. get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
  115. const uint8_t* identifier_begin, const size_t identifier_len) const;
  116. /// @brief Returns a host connected to the IPv4 subnet and having
  117. /// a reservation for a specified IPv4 address.
  118. ///
  119. /// One of the use cases for this method is to detect collisions between
  120. /// dynamically allocated addresses and reserved addresses. When the new
  121. /// address is assigned to a client, the allocation mechanism should check
  122. /// if this address is not reserved for some other host and do not allocate
  123. /// this address if reservation is present.
  124. ///
  125. /// Implementations of this method should guard against invalid addresses,
  126. /// such as IPv6 address.
  127. ///
  128. /// @param subnet_id Subnet identifier.
  129. /// @param address reserved IPv4 address.
  130. ///
  131. /// @return Const @c Host object using a specified IPv4 address.
  132. virtual ConstHostPtr
  133. get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  134. /// @brief Returns a host connected to the IPv6 subnet.
  135. ///
  136. /// Implementations of this method should guard against the case when
  137. /// multiple instances of the @c Host are present, e.g. when two
  138. /// @c Host objects are found, one for the DUID, another one for the
  139. /// HW address. In such case, an implementation of this method
  140. /// should throw an MultipleRecords exception.
  141. ///
  142. /// @param subnet_id Subnet identifier.
  143. /// @param hwaddr HW address of the client or NULL if no HW address
  144. /// available.
  145. /// @param duid DUID or NULL if not available.
  146. ///
  147. /// @return Const @c Host object using a specified HW address or DUID.
  148. virtual ConstHostPtr
  149. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  150. const HWAddrPtr& hwaddr = HWAddrPtr()) const;
  151. /// @brief Returns a host connected to the IPv6 subnet.
  152. ///
  153. /// @param subnet_id Subnet identifier.
  154. /// @param identifier_type Identifier type.
  155. /// @param identifier_begin Pointer to a beginning of a buffer containing
  156. /// an identifier.
  157. /// @param identifier_len Identifier length.
  158. ///
  159. /// @return Const @c Host object for which reservation has been made using
  160. /// the specified identifier.
  161. virtual ConstHostPtr
  162. get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
  163. const uint8_t* identifier_begin, const size_t identifier_len) const;
  164. /// @brief Returns a host using the specified IPv6 prefix.
  165. ///
  166. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  167. /// @param prefix_len IPv6 prefix length.
  168. ///
  169. /// @return Const @c Host object using a specified HW address or DUID.
  170. virtual ConstHostPtr
  171. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
  172. /// @brief Returns a host connected to the IPv6 subnet and having
  173. /// a reservation for a specified IPv6 address or prefix.
  174. ///
  175. /// @param subnet_id Subnet identifier.
  176. /// @param address reserved IPv6 address/prefix.
  177. ///
  178. /// @return Const @c Host object using a specified IPv6 address/prefix.
  179. virtual ConstHostPtr
  180. get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  181. /// @brief Adds a new host to the collection.
  182. ///
  183. /// It is not possible to add a new host in radius backend.
  184. //
  185. /// The implementations of this method should guard against duplicate
  186. /// reservations for the same host, where possible. For example, when the
  187. /// reservation for the same HW address and subnet id is added twice, the
  188. /// addHost method should throw an DuplicateEntry exception. Note, that
  189. /// usually it is impossible to guard against adding duplicated host, where
  190. /// one instance is identified by HW address, another one by DUID.
  191. ///
  192. /// @param host Pointer to the new @c Host object being added.
  193. virtual void add(const HostPtr& host);
  194. /// @brief Attempts to delete a host by (subnet-id, address)
  195. ///
  196. /// It is not possible to delete a host in radius backend.
  197. ///
  198. /// This method supports both v4 and v6.
  199. ///
  200. /// @param subnet_id subnet identifier.
  201. /// @param addr specified address.
  202. /// @return true if deletion was successful, false if the host was not there.
  203. /// @throw various exceptions in case of errors
  204. virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr);
  205. /// @brief Attempts to delete a host by (subnet4-id, identifier type, identifier)
  206. ///
  207. /// It is not possible to delete a host in radius backend.
  208. ///
  209. /// This method supports v4 hosts only.
  210. ///
  211. /// @param subnet_id subnet identifier.
  212. /// @param addr specified address.
  213. /// @return true if deletion was successful, false if the host was not there.
  214. /// @throw various exceptions in case of errors
  215. virtual bool del4(const SubnetID& subnet_id,
  216. const Host::IdentifierType& identifier_type,
  217. const uint8_t* identifier_begin, const size_t identifier_len);
  218. /// @brief Attempts to delete a host by (subnet6-id, identifier type, identifier)
  219. ///
  220. /// It is not possible to delete a host in radius backend.
  221. ///
  222. /// This method supports v6 hosts only.
  223. ///
  224. /// @param subnet_id subnet identifier.
  225. /// @param addr specified address.
  226. /// @return true if deletion was successful, false if the host was not there.
  227. /// @throw various exceptions in case of errors
  228. virtual bool del6(const SubnetID& subnet_id,
  229. const Host::IdentifierType& identifier_type,
  230. const uint8_t* identifier_begin, const size_t identifier_len);
  231. /// @brief Return backend type
  232. ///
  233. /// Returns the type of the backend (e.g. "radius", "memfile" etc.)
  234. ///
  235. /// @return Type of the backend.
  236. virtual std::string getType() const {
  237. return (std::string("radius"));
  238. }
  239. /// @brief Returns backend name.
  240. ///
  241. /// Each backend have specific name.
  242. ///
  243. /// @return "radius".
  244. virtual std::string getName() const;
  245. /// @brief Returns description of the backend.
  246. ///
  247. /// This description may be multiline text that describes the backend.
  248. ///
  249. /// @return Description of the backend.
  250. virtual std::string getDescription() const;
  251. /// @brief Returns backend version.
  252. ///
  253. /// @return Version number stored in the database, as a pair of unsigned
  254. /// integers. "first" is the major version number, "second" the
  255. /// minor number.
  256. ///
  257. /// @throw isc::dhcp::DbOperationError An operation on the open database
  258. /// has failed.
  259. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  260. /// @brief Commit Transactions
  261. ///
  262. /// Not relevant for radius backend.
  263. ///
  264. /// Commits all pending database operations.
  265. virtual void commit();
  266. /// @brief Rollback Transactions
  267. ///
  268. /// Not relevant for radius backend.
  269. ///
  270. /// Rolls back all pending database operations.
  271. virtual void rollback();
  272. private:
  273. };
  274. }
  275. }
  276. #endif // RADIUS_HOST_DATA_SOURCE_H