Kea 3.3.3
host_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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
17#include <cstdint>
18#include <string>
19
20#include <boost/noncopyable.hpp>
21#include <boost/scoped_ptr.hpp>
22
23namespace isc {
24namespace dhcp {
25
26// Enum flags to define a target of the host manager functions.
27enum HostMgrOperationTarget : uint16_t {
28 // The operation target not specified. Consider nothing.
30 // Consider only the CfgHosts instance.
31 PRIMARY_SOURCE = 1, // 1 << 1
32 // Consider only the alternate sources.
33 ALTERNATE_SOURCES = 2, // 1 << 2
34 // Consider both CfgInstance and alternate sources.
35 ALL_SOURCES = 3 // PRIMARY_SOURCE | ALTERNATE_SOURCES
36};
37
70class HostMgr : public boost::noncopyable, public BaseHostDataSource {
71public:
72
79 static void create();
80
90 static void addBackend(const std::string& access);
91
96 static bool delBackend(const std::string& db_type);
97
107 static bool delBackend(const std::string& db_type,
108 const std::string& access,
109 bool if_unusable = false);
110
112 static void delAllBackends();
113
121 static bool checkCacheBackend(bool logging = false);
122
131 static HostMgr& instance();
132
154 getAll(const Host::IdentifierType& identifier_type,
155 const uint8_t* identifier_begin,
156 const size_t identifier_len,
157 const HostMgrOperationTarget target) const;
158
161 virtual ConstHostCollection
162 getAll(const Host::IdentifierType& identifier_type,
163 const uint8_t* identifier_begin,
164 const size_t identifier_len) const;
165
183 getAll4(const SubnetID& subnet_id, const HostMgrOperationTarget target) const;
184
187 virtual ConstHostCollection
188 getAll4(const SubnetID& subnet_id) const;
189
207 getAll6(const SubnetID& subnet_id,
208 const HostMgrOperationTarget target) const;
209
212 virtual ConstHostCollection
213 getAll6(const SubnetID& subnet_id) const;
214
225 getAllbyHostname(const std::string& hostname,
226 const HostMgrOperationTarget target) const;
227
230 virtual ConstHostCollection
231 getAllbyHostname(const std::string& hostname) const;
232
244 getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id,
245 const HostMgrOperationTarget target) const;
246
249 virtual ConstHostCollection
250 getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id) const;
251
263 getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id,
264 const HostMgrOperationTarget target) const;
265
268 virtual ConstHostCollection
269 getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id) const;
270
295 virtual ConstHostCollection
296 getPage4(const SubnetID& subnet_id,
297 size_t& source_index,
298 uint64_t lower_host_id,
299 const HostPageSize& page_size) const;
300
325 virtual ConstHostCollection
326 getPage6(const SubnetID& subnet_id,
327 size_t& source_index,
328 uint64_t lower_host_id,
329 const HostPageSize& page_size) const;
330
353 virtual ConstHostCollection
354 getPage4(size_t& source_index,
355 uint64_t lower_host_id,
356 const HostPageSize& page_size) const;
357
380 virtual ConstHostCollection
381 getPage6(size_t& source_index,
382 uint64_t lower_host_id,
383 const HostPageSize& page_size) const;
384
400 getAll4(const asiolink::IOAddress& address,
401 const HostMgrOperationTarget target) const;
402
405 virtual ConstHostCollection
406 getAll4(const asiolink::IOAddress& address) const;
407
425 get4Any(const SubnetID& subnet_id,
426 const Host::IdentifierType& identifier_type,
427 const uint8_t* identifier_begin,
428 const size_t identifier_len,
429 const HostMgrOperationTarget target) const;
430
433 virtual ConstHostPtr
434 get4Any(const SubnetID& subnet_id,
435 const Host::IdentifierType& identifier_type,
436 const uint8_t* identifier_begin,
437 const size_t identifier_len) const;
438
454 get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
455 const uint8_t* identifier_begin, const size_t identifier_len,
456 const HostMgrOperationTarget target) const;
457
460 virtual ConstHostPtr
461 get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
462 const uint8_t* identifier_begin, const size_t identifier_len) const;
463
477 get4(const SubnetID& subnet_id, const asiolink::IOAddress& address,
478 const HostMgrOperationTarget target) const;
479
482 virtual ConstHostPtr
483 get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
484
510 getAll4(const SubnetID& subnet_id,
511 const asiolink::IOAddress& address,
512 const HostMgrOperationTarget target) const;
513
516 virtual ConstHostCollection
517 getAll4(const SubnetID& subnet_id,
518 const asiolink::IOAddress& address) const;
519
537 get6Any(const SubnetID& subnet_id,
538 const Host::IdentifierType& identifier_type,
539 const uint8_t* identifier_begin,
540 const size_t identifier_len,
541 const HostMgrOperationTarget target) const;
542
545 virtual ConstHostPtr
546 get6Any(const SubnetID& subnet_id,
547 const Host::IdentifierType& identifier_type,
548 const uint8_t* identifier_begin,
549 const size_t identifier_len) const;
550
566 get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
567 const uint8_t* identifier_begin, const size_t identifier_len,
568 const HostMgrOperationTarget target) const;
569
572 virtual ConstHostPtr
573 get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
574 const uint8_t* identifier_begin, const size_t identifier_len) const;
575
587 get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len,
588 const HostMgrOperationTarget target) const;
589
592 virtual ConstHostPtr
593 get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
594
603 get6(const SubnetID& subnet_id, const asiolink::IOAddress& addr,
604 const HostMgrOperationTarget target) const;
605
608 virtual ConstHostPtr
609 get6(const SubnetID& subnet_id, const asiolink::IOAddress& addr) const;
610
636 getAll6(const SubnetID& subnet_id,
637 const asiolink::IOAddress& address,
638 const HostMgrOperationTarget target) const;
639
642 virtual ConstHostCollection
643 getAll6(const SubnetID& subnet_id,
644 const asiolink::IOAddress& address) const;
645
648 virtual ConstHostCollection
649 getAll6(const asiolink::IOAddress& address) const;
650
666 getAll6(const asiolink::IOAddress& address,
667 const HostMgrOperationTarget target) const;
668
676 void add(const HostPtr& host, const HostMgrOperationTarget target);
677
680 virtual void add(const HostPtr& host);
681
695 bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr,
696 const HostMgrOperationTarget target);
697
700 virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr);
701
714 bool
715 del4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
716 const uint8_t* identifier_begin, const size_t identifier_len,
717 const HostMgrOperationTarget target);
718
721 virtual bool
722 del4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
723 const uint8_t* identifier_begin, const size_t identifier_len);
724
737 bool
738 del6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
739 const uint8_t* identifier_begin, const size_t identifier_len,
740 const HostMgrOperationTarget target);
741
744 virtual bool
745 del6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
746 const uint8_t* identifier_begin, const size_t identifier_len);
747
754 void update(HostPtr const& host, const HostMgrOperationTarget target);
755
758 void update(HostPtr const& host);
759
765 virtual std::string getType() const {
766 return (std::string("host_mgr"));
767 }
768
773 return (alternate_sources_);
774 }
775
781
785 bool getNegativeCaching() const {
786 return (negative_caching_);
787 }
788
791 void setNegativeCaching(bool negative_caching) {
792 negative_caching_ = negative_caching;
793 }
794
799 return (disable_single_query_);
800 }
801
804 void setDisableSingleQuery(bool disable_single_query) {
805 disable_single_query_ = disable_single_query;
806 }
807
826 virtual bool setIPReservationsUnique(const bool unique);
827
834 return (ip_reservations_unique_);
835 }
836
837protected:
838
845
852
856 virtual void cache(ConstHostPtr host) const;
857
865 virtual void cacheNegative(const SubnetID& ipv4_subnet_id,
866 const SubnetID& ipv6_subnet_id,
867 const Host::IdentifierType& identifier_type,
868 const uint8_t* identifier_begin,
869 const size_t identifier_len) const;
870
871private:
872
879 bool ip_reservations_unique_;
880
882 HostMgr() : negative_caching_(false), disable_single_query_(false),
883 ip_reservations_unique_(true) { }
884
886 HostDataSourceList alternate_sources_;
887
889 CacheHostDataSourcePtr cache_ptr_;
890
893 static boost::scoped_ptr<HostMgr>& getHostMgrPtr();
894};
895
896} // namespace dhcp
897} // namespace isc
898
899#endif // HOST_MGR_H
Base interface for the classes implementing simple data source for host reservations.
ConstHostCollection getAll6(const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts in a DHCPv6 subnet.
Definition host_mgr.cc:171
ConstHostCollection getAll4(const SubnetID &subnet_id, const HostMgrOperationTarget target) const
Return all hosts in a DHCPv4 subnet.
Definition host_mgr.cc:151
static void delAllBackends()
Delete all alternate backends.
Definition host_mgr.cc:79
static void create()
Creates new instance of the HostMgr.
Definition host_mgr.cc:52
bool negative_caching_
The negative caching flag.
Definition host_mgr.h:844
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:940
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:290
bool getNegativeCaching() const
Returns the negative caching flag.
Definition host_mgr.h:785
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
Definition host_mgr.cc:958
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:590
void setDisableSingleQuery(bool disable_single_query)
Sets the disable single query flag.
Definition host_mgr.h:804
virtual std::string getType() const
Return backend type.
Definition host_mgr.h:765
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:211
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:824
virtual void cache(ConstHostPtr host) const
Cache an answer.
Definition host_mgr.cc:927
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
Definition host_mgr.cc:62
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition host_mgr.cc:57
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:123
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:401
bool getDisableSingleQuery() const
Returns the disable single query flag.
Definition host_mgr.h:798
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:861
void add(const HostPtr &host, const HostMgrOperationTarget target)
Adds a new host to the alternate data source.
Definition host_mgr.cc:764
bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr, const HostMgrOperationTarget target)
Attempts to delete hosts by address.
Definition host_mgr.cc:794
void setNegativeCaching(bool negative_caching)
Sets the negative caching flag.
Definition host_mgr.h:791
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:235
bool disable_single_query_
The disable single query flag.
Definition host_mgr.h:851
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:259
HostDataSourcePtr getHostDataSource() const
Returns the first host data source.
Definition host_mgr.cc:84
static bool checkCacheBackend(bool logging=false)
Check for the cache host backend.
Definition host_mgr.cc:92
bool getIPReservationsUnique() const
Returns the boolean flag indicating if the IP reservations must be unique or can be non-unique.
Definition host_mgr.h:833
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition host_mgr.cc:114
void update(HostPtr const &host, const HostMgrOperationTarget target)
Implements BaseHostDataSource::update() for alternate sources.
Definition host_mgr.cc:898
ConstHostCollection getAllbyHostname(const std::string &hostname, const HostMgrOperationTarget target) const
Return all hosts with a hostname.
Definition host_mgr.cc:191
HostDataSourceList & getHostDataSourceList()
Returns the host data source list.
Definition host_mgr.h:772
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:465
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:650
Wraps value holding size of the page with host reservations.
IdentifierType
Type of the host identifier.
Definition host.h:340
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
HostMgrOperationTarget
Definition host_mgr.h:27
@ PRIMARY_SOURCE
Definition host_mgr.h:31
@ ALTERNATE_SOURCES
Definition host_mgr.h:33
@ UNSPECIFIED_SOURCE
Definition host_mgr.h:29
@ ALL_SOURCES
Definition host_mgr.h:35
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition host.h:840
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition host.h:846
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:843
Defines the logger used by the top-level component of kea-lfc.