Kea  2.5.2
host_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2023 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 
7 #ifndef HOST_MGR_H
8 #define HOST_MGR_H
9 
10 #include <asiolink/io_service.h>
14 #include <dhcpsrv/host.h>
15 #include <dhcpsrv/subnet_id.h>
16 #include <boost/noncopyable.hpp>
17 #include <boost/scoped_ptr.hpp>
18 #include <string>
19 #include <cstdint>
20 
21 namespace isc {
22 namespace dhcp {
23 
24 // Enum flags to define a target of the host manager functions.
26  // The operation target not specified. Consider nothing.
28  // Consider only the CfgHosts instance.
29  PRIMARY_SOURCE = 1, // 1 << 1
30  // Consider only the alternate sources.
31  ALTERNATE_SOURCES = 2, // 1 << 2
32  // Consider both CfgInstance and alternate sources.
33  ALL_SOURCES = 3 // PRIMARY_SOURCE | ALTERNATE_SOURCES
34 };
35 
68 class HostMgr : public boost::noncopyable, public BaseHostDataSource {
69 public:
70 
77  static void create();
78 
88  static void addBackend(const std::string& access);
89 
94  static bool delBackend(const std::string& db_type);
95 
105  static bool delBackend(const std::string& db_type,
106  const std::string& access,
107  bool if_unusable = false);
108 
110  static void delAllBackends();
111 
119  static bool checkCacheBackend(bool logging = false);
120 
129  static HostMgr& instance();
130 
152  getAll(const Host::IdentifierType& identifier_type,
153  const uint8_t* identifier_begin,
154  const size_t identifier_len,
155  const HostMgrOperationTarget target) const;
156 
159  virtual ConstHostCollection
160  getAll(const Host::IdentifierType& identifier_type,
161  const uint8_t* identifier_begin,
162  const size_t identifier_len) const;
163 
181  getAll4(const SubnetID& subnet_id, const HostMgrOperationTarget target) const;
182 
185  virtual ConstHostCollection
186  getAll4(const SubnetID& subnet_id) const;
187 
205  getAll6(const SubnetID& subnet_id,
206  const HostMgrOperationTarget target) const;
207 
210  virtual ConstHostCollection
211  getAll6(const SubnetID& subnet_id) const;
212 
223  getAllbyHostname(const std::string& hostname,
224  const HostMgrOperationTarget target) const;
225 
228  virtual ConstHostCollection
229  getAllbyHostname(const std::string& hostname) const;
230 
242  getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id,
243  const HostMgrOperationTarget target) const;
244 
247  virtual ConstHostCollection
248  getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id) const;
249 
261  getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id,
262  const HostMgrOperationTarget target) const;
263 
266  virtual ConstHostCollection
267  getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id) const;
268 
293  virtual ConstHostCollection
294  getPage4(const SubnetID& subnet_id,
295  size_t& source_index,
296  uint64_t lower_host_id,
297  const HostPageSize& page_size) const;
298 
323  virtual ConstHostCollection
324  getPage6(const SubnetID& subnet_id,
325  size_t& source_index,
326  uint64_t lower_host_id,
327  const HostPageSize& page_size) const;
328 
351  virtual ConstHostCollection
352  getPage4(size_t& source_index,
353  uint64_t lower_host_id,
354  const HostPageSize& page_size) const;
355 
378  virtual ConstHostCollection
379  getPage6(size_t& source_index,
380  uint64_t lower_host_id,
381  const HostPageSize& page_size) const;
382 
398  getAll4(const asiolink::IOAddress& address,
399  const HostMgrOperationTarget target) const;
400 
403  virtual ConstHostCollection
404  getAll4(const asiolink::IOAddress& address) const;
405 
423  get4Any(const SubnetID& subnet_id,
424  const Host::IdentifierType& identifier_type,
425  const uint8_t* identifier_begin,
426  const size_t identifier_len,
427  const HostMgrOperationTarget target) const;
428 
431  virtual ConstHostPtr
432  get4Any(const SubnetID& subnet_id,
433  const Host::IdentifierType& identifier_type,
434  const uint8_t* identifier_begin,
435  const size_t identifier_len) const;
436 
452  get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
453  const uint8_t* identifier_begin, const size_t identifier_len,
454  const HostMgrOperationTarget target) const;
455 
458  virtual ConstHostPtr
459  get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
460  const uint8_t* identifier_begin, const size_t identifier_len) const;
461 
475  get4(const SubnetID& subnet_id, const asiolink::IOAddress& address,
476  const HostMgrOperationTarget target) const;
477 
480  virtual ConstHostPtr
481  get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
482 
508  getAll4(const SubnetID& subnet_id,
509  const asiolink::IOAddress& address,
510  const HostMgrOperationTarget target) const;
511 
514  virtual ConstHostCollection
515  getAll4(const SubnetID& subnet_id,
516  const asiolink::IOAddress& address) const;
517 
535  get6Any(const SubnetID& subnet_id,
536  const Host::IdentifierType& identifier_type,
537  const uint8_t* identifier_begin,
538  const size_t identifier_len,
539  const HostMgrOperationTarget target) const;
540 
543  virtual ConstHostPtr
544  get6Any(const SubnetID& subnet_id,
545  const Host::IdentifierType& identifier_type,
546  const uint8_t* identifier_begin,
547  const size_t identifier_len) const;
548 
564  get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
565  const uint8_t* identifier_begin, const size_t identifier_len,
566  const HostMgrOperationTarget target) const;
567 
570  virtual ConstHostPtr
571  get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
572  const uint8_t* identifier_begin, const size_t identifier_len) const;
573 
585  get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len,
586  const HostMgrOperationTarget target) const;
587 
590  virtual ConstHostPtr
591  get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
592 
601  get6(const SubnetID& subnet_id, const asiolink::IOAddress& addr,
602  const HostMgrOperationTarget target) const;
603 
606  virtual ConstHostPtr
607  get6(const SubnetID& subnet_id, const asiolink::IOAddress& addr) const;
608 
634  getAll6(const SubnetID& subnet_id,
635  const asiolink::IOAddress& address,
636  const HostMgrOperationTarget target) const;
637 
640  virtual ConstHostCollection
641  getAll6(const SubnetID& subnet_id,
642  const asiolink::IOAddress& address) const;
643 
646  virtual ConstHostCollection
647  getAll6(const asiolink::IOAddress& address) const;
648 
664  getAll6(const asiolink::IOAddress& address,
665  const HostMgrOperationTarget target) const;
666 
674  void add(const HostPtr& host, const HostMgrOperationTarget target);
675 
678  virtual void add(const HostPtr& host);
679 
693  bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr,
694  const HostMgrOperationTarget target);
695 
698  virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr);
699 
712  bool
713  del4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
714  const uint8_t* identifier_begin, const size_t identifier_len,
715  const HostMgrOperationTarget target);
716 
719  virtual bool
720  del4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
721  const uint8_t* identifier_begin, const size_t identifier_len);
722 
735  bool
736  del6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
737  const uint8_t* identifier_begin, const size_t identifier_len,
738  const HostMgrOperationTarget target);
739 
742  virtual bool
743  del6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
744  const uint8_t* identifier_begin, const size_t identifier_len);
745 
752  void update(HostPtr const& host, const HostMgrOperationTarget target);
753 
756  void update(HostPtr const& host);
757 
763  virtual std::string getType() const {
764  return (std::string("host_mgr"));
765  }
766 
771  return (alternate_sources_);
772  }
773 
779 
783  bool getNegativeCaching() const {
784  return (negative_caching_);
785  }
786 
789  void setNegativeCaching(bool negative_caching) {
790  negative_caching_ = negative_caching;
791  }
792 
796  bool getDisableSingleQuery() const {
797  return (disable_single_query_);
798  }
799 
802  void setDisableSingleQuery(bool disable_single_query) {
803  disable_single_query_ = disable_single_query;
804  }
805 
824  virtual bool setIPReservationsUnique(const bool unique);
825 
831  bool getIPReservationsUnique() const {
832  return (ip_reservations_unique_);
833  }
834 
838  static void setIOService(const isc::asiolink::IOServicePtr& io_service) {
839  io_service_ = io_service;
840  }
841 
844  return (io_service_);
845  }
846 
847 protected:
848 
855 
862 
866  virtual void cache(ConstHostPtr host) const;
867 
875  virtual void cacheNegative(const SubnetID& ipv4_subnet_id,
876  const SubnetID& ipv6_subnet_id,
877  const Host::IdentifierType& identifier_type,
878  const uint8_t* identifier_begin,
879  const size_t identifier_len) const;
880 
881 private:
882 
889  bool ip_reservations_unique_;
890 
893  ip_reservations_unique_(true) { }
894 
896  HostDataSourceList alternate_sources_;
897 
899  CacheHostDataSourcePtr cache_ptr_;
900 
903  static boost::scoped_ptr<HostMgr>& getHostMgrPtr();
904 
906  static isc::asiolink::IOServicePtr io_service_;
907 };
908 
909 } // namespace dhcp
910 } // namespace isc
911 
912 #endif // HOST_MGR_H
Base interface for the classes implementing simple data source for host reservations.
Host Manager.
Definition: host_mgr.h:68
ConstHostCollection getAll6(const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts in a DHCPv6 subnet.
Definition: host_mgr.cc:173
static void setIOService(const isc::asiolink::IOServicePtr &io_service)
Sets IO service to be used by the Host Manager.
Definition: host_mgr.h:838
ConstHostCollection getAll4(const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts in a DHCPv4 subnet.
Definition: host_mgr.cc:153
static void delAllBackends()
Delete all alternate backends.
Definition: host_mgr.cc:81
static void create()
Creates new instance of the HostMgr.
Definition: host_mgr.cc:54
bool negative_caching_
The negative caching flag.
Definition: host_mgr.h:854
virtual void cacheNegative(const SubnetID &ipv4_subnet_id, const SubnetID &ipv6_subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Cache a negative answer.
Definition: host_mgr.cc:942
virtual ConstHostCollection getPage6(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv6 subnet.
Definition: host_mgr.cc:292
bool getNegativeCaching() const
Returns the negative caching flag.
Definition: host_mgr.h:783
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
Definition: host_mgr.cc:960
ConstHostPtr get6Any(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target) const
Returns any host connected to the IPv6 subnet.
Definition: host_mgr.cc:592
void setDisableSingleQuery(bool disable_single_query)
Sets the disable single query flag.
Definition: host_mgr.h:802
virtual std::string getType() const
Return backend type.
Definition: host_mgr.h:763
ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts with a hostname in a DHCPv4 subnet.
Definition: host_mgr.cc:213
bool del4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target)
Attempts to delete a host by (subnet4-id, identifier, identifier-type, operation-target)
Definition: host_mgr.cc:826
HostDataSourceList & getHostDataSourceList()
Returns the host data source list.
Definition: host_mgr.h:770
virtual void cache(ConstHostPtr host) const
Cache an answer.
Definition: host_mgr.cc:929
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
Definition: host_mgr.cc:64
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition: host_mgr.cc:59
ConstHostCollection getAll(const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target) const
Return all hosts connected to any subnet for which reservations have been made using a specified iden...
Definition: host_mgr.cc:125
ConstHostPtr get4Any(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target) const
Returns any host connected to the IPv4 subnet.
Definition: host_mgr.cc:403
bool getDisableSingleQuery() const
Returns the disable single query flag.
Definition: host_mgr.h:796
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
Definition: host_mgr.h:843
bool del6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target)
Attempts to delete a host by (subnet6-id, identifier, identifier-type, operation-target)
Definition: host_mgr.cc:863
void add(const HostPtr &host, const HostMgrOperationTarget target)
Adds a new host to the alternate data source.
Definition: host_mgr.cc:766
bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr, const HostMgrOperationTarget target)
Attempts to delete hosts by address.
Definition: host_mgr.cc:796
void setNegativeCaching(bool negative_caching)
Sets the negative caching flag.
Definition: host_mgr.h:789
ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts with a hostname in a DHCPv6 subnet.
Definition: host_mgr.cc:237
bool disable_single_query_
The disable single query flag.
Definition: host_mgr.h:861
virtual ConstHostCollection getPage4(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv4 subnet.
Definition: host_mgr.cc:261
HostDataSourcePtr getHostDataSource() const
Returns the first host data source.
Definition: host_mgr.cc:86
static bool checkCacheBackend(bool logging=false)
Check for the cache host backend.
Definition: host_mgr.cc:94
bool getIPReservationsUnique() const
Returns the boolean flag indicating if the IP reservations must be unique or can be non-unique.
Definition: host_mgr.h:831
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition: host_mgr.cc:116
void update(HostPtr const &host, const HostMgrOperationTarget target)
Implements BaseHostDataSource::update() for alternate sources.
Definition: host_mgr.cc:900
ConstHostCollection getAllbyHostname(const std::string &hostname, const HostMgrOperationTarget target) const
Return all hosts with a hostname.
Definition: host_mgr.cc:193
ConstHostPtr get4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target) const
Returns a host connected to the IPv4 subnet.
Definition: host_mgr.cc:467
ConstHostPtr get6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, const HostMgrOperationTarget target) const
Returns a host connected to the IPv6 subnet.
Definition: host_mgr.cc:652
Wraps value holding size of the page with host reservations.
IdentifierType
Type of the host identifier.
Definition: host.h:307
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition: host.h:807
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition: host.h:813
std::vector< HostDataSourcePtr > HostDataSourceList
HostDataSource list.
boost::shared_ptr< CacheHostDataSource > CacheHostDataSourcePtr
CacheHostDataSource pointer.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:25
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:810
HostMgrOperationTarget
Definition: host_mgr.h:25
@ PRIMARY_SOURCE
Definition: host_mgr.h:29
@ ALTERNATE_SOURCES
Definition: host_mgr.h:31
@ UNSPECIFIED_SOURCE
Definition: host_mgr.h:27
@ ALL_SOURCES
Definition: host_mgr.h:33
Defines the logger used by the top-level component of kea-lfc.