host_mgr.cc 5.9 KB

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