host_mgr.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. namespace {
  20. /// @brief Convenience function returning a pointer to the hosts configuration.
  21. ///
  22. /// This function is called by the @c HostMgr methods requiring access to the
  23. /// host reservations specified in the DHCP server configuration.
  24. ///
  25. /// @return A pointer to the const hosts reservation configuration.
  26. isc::dhcp::ConstCfgHostsPtr getCfgHosts() {
  27. return (isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgHosts());
  28. }
  29. } // end of anonymous namespace
  30. namespace isc {
  31. namespace dhcp {
  32. using namespace isc::asiolink;
  33. boost::scoped_ptr<HostMgr>&
  34. HostMgr::getHostMgrPtr() {
  35. static boost::scoped_ptr<HostMgr> host_mgr_ptr;
  36. return (host_mgr_ptr);
  37. }
  38. void
  39. HostMgr::create(const std::string&) {
  40. getHostMgrPtr().reset(new HostMgr());
  41. /// @todo Initialize alternate_source here, using the parameter.
  42. /// For example: alternate_source.reset(new MysqlHostDataSource(access)).
  43. }
  44. HostMgr&
  45. HostMgr::instance() {
  46. boost::scoped_ptr<HostMgr>& host_mgr_ptr = getHostMgrPtr();
  47. if (!host_mgr_ptr) {
  48. create();
  49. }
  50. return (*host_mgr_ptr);
  51. }
  52. ConstHostCollection
  53. HostMgr::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const {
  54. ConstHostCollection hosts = getCfgHosts()->getAll(hwaddr, duid);
  55. if (alternate_source) {
  56. ConstHostCollection hosts_plus = alternate_source->getAll(hwaddr, duid);
  57. hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end());
  58. }
  59. return (hosts);
  60. }
  61. ConstHostCollection
  62. HostMgr::getAll4(const IOAddress& address) const {
  63. ConstHostCollection hosts = getCfgHosts()->getAll4(address);
  64. if (alternate_source) {
  65. ConstHostCollection hosts_plus = alternate_source->getAll4(address);
  66. hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end());
  67. }
  68. return (hosts);
  69. }
  70. ConstHostPtr
  71. HostMgr::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  72. const DuidPtr& duid) const {
  73. ConstHostPtr host = getCfgHosts()->get4(subnet_id, hwaddr, duid);
  74. if (!host && alternate_source) {
  75. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  76. HOSTS_MGR_ALTERNATE_GET4_SUBNET_ID_HWADDR_DUID)
  77. .arg(subnet_id)
  78. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  79. .arg(duid ? duid->toText() : "(duid)");
  80. host = alternate_source->get4(subnet_id, hwaddr, duid);
  81. }
  82. return (host);
  83. }
  84. ConstHostPtr
  85. HostMgr::get4(const SubnetID& subnet_id,
  86. const asiolink::IOAddress& address) const {
  87. ConstHostPtr host = getCfgHosts()->get4(subnet_id, address);
  88. if (!host && alternate_source) {
  89. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  90. HOSTS_MGR_ALTERNATE_GET4_SUBNET_ID_ADDRESS4)
  91. .arg(subnet_id)
  92. .arg(address.toText());
  93. host = alternate_source->get4(subnet_id, address);
  94. }
  95. return (host);
  96. }
  97. ConstHostPtr
  98. HostMgr::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  99. const HWAddrPtr& hwaddr) const {
  100. ConstHostPtr host = getCfgHosts()->get6(subnet_id, duid, hwaddr);
  101. if (!host && alternate_source) {
  102. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  103. HOSTS_MGR_ALTERNATE_GET6_SUBNET_ID_DUID_HWADDR)
  104. .arg(subnet_id)
  105. .arg(duid ? duid->toText() : "(duid)")
  106. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)");
  107. host = alternate_source->get6(subnet_id, duid, hwaddr);
  108. }
  109. return (host);
  110. }
  111. ConstHostPtr
  112. HostMgr::get6(const IOAddress& prefix, const uint8_t prefix_len) const {
  113. ConstHostPtr host = getCfgHosts()->get6(prefix, prefix_len);
  114. if (!host && alternate_source) {
  115. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  116. HOSTS_MGR_ALTERNATE_GET6_PREFIX)
  117. .arg(prefix.toText())
  118. .arg(static_cast<int>(prefix_len));
  119. host = alternate_source->get6(prefix, prefix_len);
  120. }
  121. return (host);
  122. }
  123. ConstHostPtr
  124. HostMgr::get6(const SubnetID& subnet_id,
  125. const asiolink::IOAddress& addr) const {
  126. ConstHostPtr host = getCfgHosts()->get6(subnet_id, addr);
  127. if (!host && alternate_source) {
  128. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE,
  129. HOSTS_MGR_ALTERNATE_GET6_SUBNET_ID_ADDRESS6)
  130. .arg(subnet_id)
  131. .arg(addr.toText());
  132. host = alternate_source->get6(subnet_id, addr);
  133. }
  134. return (host);
  135. }
  136. void
  137. HostMgr::add(const HostPtr&) {
  138. isc_throw(isc::NotImplemented, "HostMgr::add is not implemented");
  139. }
  140. } // end of isc::dhcp namespace
  141. } // end of isc namespace