base_host_data_source.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2014 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 BASE_HOST_DATA_SOURCE_H
  15. #define BASE_HOST_DATA_SOURCE_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/hwaddr.h>
  19. #include <dhcpsrv/host.h>
  20. #include <exceptions/exceptions.h>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Exception thrown when the duplicate @c Host object is detected.
  24. class DuplicateHost : public Exception {
  25. public:
  26. DuplicateHost(const char* file, size_t line, const char* what) :
  27. isc::Exception(file, line, what) { };
  28. };
  29. /// @brief Base interface for the classes implementing simple data source
  30. /// for host reservations.
  31. ///
  32. /// This abstract class defines an interface for the classes implementing
  33. /// basic data source for host reservations. This interface allows for
  34. /// adding new reservations (represented by @c Host objects) and retrieving
  35. /// these reservations using various parameters such as HW address or DUID,
  36. /// subnet identifier (either IPv4 or IPv6) or reserved IP address.
  37. ///
  38. /// This interface DOES NOT specify the methods to manage existing
  39. /// host reservations such as to remove one IPv6 reservation but leave
  40. /// other reservations. It also lacks the methods used for preparing
  41. /// the data to be added to the SQL database: commit, rollback etc.
  42. /// Such methods are declared in other interfaces.
  43. class BaseHostDataSource {
  44. public:
  45. /// @brief Default destructor implementation.
  46. virtual ~BaseHostDataSource() {
  47. }
  48. /// @brief Return all hosts for the specified HW address or DUID.
  49. ///
  50. /// This method returns all @c Host objects which represent reservations
  51. /// for the specified HW address or DUID. Note, that this method may
  52. /// return multiple reservations because a particular client may have
  53. /// reservations in multiple subnets and the same client may be identified
  54. /// by HW address or DUID. The server is unable to verify that the specific
  55. /// DUID and HW address belong to the same client, until the client sends
  56. /// a DHCP message.
  57. ///
  58. /// Specifying both hardware address and DUID is allowed for this method
  59. /// and results in returning all objects that are associated with hardware
  60. /// address OR duid. For example: if one host is associated with the
  61. /// specified hardware address and another host is associated with the
  62. /// specified DUID, two hosts will be returned.
  63. ///
  64. /// @param hwaddr HW address of the client or NULL if no HW address
  65. /// available.
  66. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  67. ///
  68. /// @return Collection of const @c Host objects.
  69. virtual ConstHostCollection
  70. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const = 0;
  71. /// @brief Returns a collection of hosts using the specified IPv4 address.
  72. ///
  73. /// This method may return multiple @c Host objects if they are connected
  74. /// to different subnets.
  75. ///
  76. /// @param address IPv4 address for which the @c Host object is searched.
  77. ///
  78. /// @return Collection of const @c Host objects.
  79. virtual ConstHostCollection
  80. getAll4(const asiolink::IOAddress& address) const = 0;
  81. /// @brief Returns a host connected to the IPv4 subnet.
  82. ///
  83. /// Implementations of this method should guard against the case when
  84. /// mutliple instances of the @c Host are present, e.g. when two
  85. /// @c Host objects are found, one for the DUID, another one for the
  86. /// HW address. In such case, an implementation of this method
  87. /// should throw an exception.
  88. ///
  89. /// @param subnet_id Subnet identifier.
  90. /// @param hwaddr HW address of the client or NULL if no HW address
  91. /// available.
  92. /// @param duid client id or NULL if not available.
  93. ///
  94. /// @return Const @c Host object using a specified HW address or DUID.
  95. virtual ConstHostPtr
  96. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  97. const DuidPtr& duid = DuidPtr()) const = 0;
  98. /// @brief Returns a host connected to the IPv6 subnet.
  99. ///
  100. /// Implementations of this method should guard against the case when
  101. /// mutliple instances of the @c Host are present, e.g. when two
  102. /// @c Host objects are found, one for the DUID, another one for the
  103. /// HW address. In such case, an implementation of this method
  104. /// should throw an exception.
  105. ///
  106. /// @param subnet_id Subnet identifier.
  107. /// @param hwaddr HW address of the client or NULL if no HW address
  108. /// available.
  109. /// @param duid DUID or NULL if not available.
  110. ///
  111. /// @return Const @c Host object using a specified HW address or DUID.
  112. virtual ConstHostPtr
  113. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  114. const HWAddrPtr& hwaddr = HWAddrPtr()) const = 0;
  115. /// @brief Returns a host using the specified IPv6 prefix.
  116. ///
  117. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  118. /// @param prefix_len IPv6 prefix length.
  119. ///
  120. /// @return Const @c Host object using a specified HW address or DUID.
  121. virtual ConstHostPtr
  122. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const = 0;
  123. /// @brief Adds a new host to the collection.
  124. ///
  125. /// The implementations of this method should guard against duplicate
  126. /// reservations for the same host, where possible. For example, when the
  127. /// reservation for the same HW address and subnet id is added twice, the
  128. /// implementation should throw an exception. Note, that usually it is
  129. /// impossible to guard against adding duplicated host, where one instance
  130. /// is identified by HW address, another one by DUID.
  131. ///
  132. /// @param host Pointer to the new @c Host object being added.
  133. virtual void add(const HostPtr& host) = 0;
  134. };
  135. }
  136. }
  137. #endif // BASE_HOST_DATA_SOURCE_H