host_mgr.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include <config.h>
  7. #include <dhcpsrv/cfg_hosts.h>
  8. #include <dhcpsrv/cfgmgr.h>
  9. #include <dhcpsrv/host_mgr.h>
  10. #include <dhcpsrv/hosts_log.h>
  11. #include <dhcpsrv/host_data_source_factory.h>
  12. namespace {
  13. /// @brief Convenience function returning a pointer to the hosts configuration.
  14. ///
  15. /// This function is called by the @c HostMgr methods requiring access to the
  16. /// host reservations specified in the DHCP server configuration.
  17. ///
  18. /// @return A pointer to the const hosts reservation configuration.
  19. isc::dhcp::ConstCfgHostsPtr getCfgHosts() {
  20. return (isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgHosts());
  21. }
  22. } // end of anonymous namespace
  23. namespace isc {
  24. namespace dhcp {
  25. using namespace isc::asiolink;
  26. boost::scoped_ptr<HostMgr>&
  27. HostMgr::getHostMgrPtr() {
  28. static boost::scoped_ptr<HostMgr> host_mgr_ptr;
  29. return (host_mgr_ptr);
  30. }
  31. void
  32. HostMgr::create(const std::string& access) {
  33. getHostMgrPtr().reset(new HostMgr());
  34. if (!access.empty()) {
  35. // If the user specified parameters, let's pass them to the create
  36. // method. It will destroy any prior instances and will create
  37. // the new one.
  38. HostDataSourceFactory::create(access);
  39. } else {
  40. // Ok, no parameters were specified. We should destroy the existing
  41. // instance.
  42. HostDataSourceFactory::destroy();
  43. }
  44. // Now store the host data source pointer. It may be NULL. That's ok as
  45. // NULL value indicates that there's no host data source configured.
  46. getHostMgrPtr()->alternate_source_ =
  47. HostDataSourceFactory::getHostDataSourcePtr();
  48. }
  49. HostMgr&
  50. HostMgr::instance() {
  51. boost::scoped_ptr<HostMgr>& host_mgr_ptr = getHostMgrPtr();
  52. if (!host_mgr_ptr) {
  53. create();
  54. }
  55. return (*host_mgr_ptr);
  56. }
  57. ConstHostCollection
  58. HostMgr::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const {
  59. ConstHostCollection hosts = getCfgHosts()->getAll(hwaddr, duid);
  60. if (alternate_source_) {
  61. ConstHostCollection hosts_plus = alternate_source_->getAll(hwaddr, duid);
  62. hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end());
  63. }
  64. return (hosts);
  65. }
  66. ConstHostCollection
  67. HostMgr::getAll(const Host::IdentifierType& identifier_type,
  68. const uint8_t* identifier_begin,
  69. const size_t identifier_len) const {
  70. ConstHostCollection hosts = getCfgHosts()->getAll(identifier_type,
  71. identifier_begin,
  72. identifier_len);
  73. if (alternate_source_) {
  74. ConstHostCollection hosts_plus =
  75. alternate_source_->getAll(identifier_type, identifier_begin,
  76. identifier_len);
  77. hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end());
  78. }
  79. return (hosts);
  80. }
  81. ConstHostCollection
  82. HostMgr::getAll4(const IOAddress& address) const {
  83. ConstHostCollection hosts = getCfgHosts()->getAll4(address);
  84. if (alternate_source_) {
  85. ConstHostCollection hosts_plus = alternate_source_->getAll4(address);
  86. hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end());
  87. }
  88. return (hosts);
  89. }
  90. ConstHostPtr
  91. HostMgr::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  92. const DuidPtr& duid) const {
  93. ConstHostPtr host = getCfgHosts()->get4(subnet_id, hwaddr, duid);
  94. if (!host && alternate_source_) {
  95. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  96. HOSTS_MGR_ALTERNATE_GET4_SUBNET_ID_HWADDR_DUID)
  97. .arg(subnet_id)
  98. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  99. .arg(duid ? duid->toText() : "(duid)");
  100. if (duid) {
  101. host = alternate_source_->get4(subnet_id, HWAddrPtr(), duid);
  102. }
  103. if (!host && hwaddr) {
  104. host = alternate_source_->get4(subnet_id, hwaddr, DuidPtr());
  105. }
  106. }
  107. return (host);
  108. }
  109. ConstHostPtr
  110. HostMgr::get4(const SubnetID& subnet_id,
  111. const Host::IdentifierType& identifier_type,
  112. const uint8_t* identifier_begin,
  113. const size_t identifier_len) const {
  114. ConstHostPtr host = getCfgHosts()->get4(subnet_id, identifier_type,
  115. identifier_begin, identifier_len);
  116. if (!host && alternate_source_) {
  117. host = alternate_source_->get4(subnet_id, identifier_type,
  118. identifier_begin, identifier_len);
  119. }
  120. return (host);
  121. }
  122. ConstHostPtr
  123. HostMgr::get4(const SubnetID& subnet_id,
  124. const asiolink::IOAddress& address) const {
  125. ConstHostPtr host = getCfgHosts()->get4(subnet_id, address);
  126. if (!host && alternate_source_) {
  127. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  128. HOSTS_MGR_ALTERNATE_GET4_SUBNET_ID_ADDRESS4)
  129. .arg(subnet_id)
  130. .arg(address.toText());
  131. host = alternate_source_->get4(subnet_id, address);
  132. }
  133. return (host);
  134. }
  135. ConstHostPtr
  136. HostMgr::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  137. const HWAddrPtr& hwaddr) const {
  138. ConstHostPtr host = getCfgHosts()->get6(subnet_id, duid, hwaddr);
  139. if (!host && alternate_source_) {
  140. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  141. HOSTS_MGR_ALTERNATE_GET6_SUBNET_ID_DUID_HWADDR)
  142. .arg(subnet_id)
  143. .arg(duid ? duid->toText() : "(duid)")
  144. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)");
  145. if (duid) {
  146. host = alternate_source_->get6(subnet_id, duid, HWAddrPtr());
  147. }
  148. if (!host && hwaddr) {
  149. host = alternate_source_->get6(subnet_id, DuidPtr(), hwaddr);
  150. }
  151. }
  152. return (host);
  153. }
  154. ConstHostPtr
  155. HostMgr::get6(const IOAddress& prefix, const uint8_t prefix_len) const {
  156. ConstHostPtr host = getCfgHosts()->get6(prefix, prefix_len);
  157. if (!host && alternate_source_) {
  158. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  159. HOSTS_MGR_ALTERNATE_GET6_PREFIX)
  160. .arg(prefix.toText())
  161. .arg(static_cast<int>(prefix_len));
  162. host = alternate_source_->get6(prefix, prefix_len);
  163. }
  164. return (host);
  165. }
  166. ConstHostPtr
  167. HostMgr::get6(const SubnetID& subnet_id,
  168. const Host::IdentifierType& identifier_type,
  169. const uint8_t* identifier_begin,
  170. const size_t identifier_len) const {
  171. ConstHostPtr host = getCfgHosts()->get6(subnet_id, identifier_type,
  172. identifier_begin, identifier_len);
  173. if (!host && alternate_source_) {
  174. host = alternate_source_->get6(subnet_id, identifier_type,
  175. identifier_begin, identifier_len);
  176. }
  177. return (host);
  178. }
  179. ConstHostPtr
  180. HostMgr::get6(const SubnetID& subnet_id,
  181. const asiolink::IOAddress& addr) const {
  182. ConstHostPtr host = getCfgHosts()->get6(subnet_id, addr);
  183. if (!host && alternate_source_) {
  184. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  185. HOSTS_MGR_ALTERNATE_GET6_SUBNET_ID_ADDRESS6)
  186. .arg(subnet_id)
  187. .arg(addr.toText());
  188. host = alternate_source_->get6(subnet_id, addr);
  189. }
  190. return (host);
  191. }
  192. void
  193. HostMgr::add(const HostPtr& host) {
  194. if (!alternate_source_) {
  195. isc_throw(NoHostDataSourceManager, "unable to add new host because there is "
  196. "no alternate host data source present");
  197. }
  198. alternate_source_->add(host);
  199. }
  200. } // end of isc::dhcp namespace
  201. } // end of isc namespace