Kea 3.3.3
base_host_data_source.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 BASE_HOST_DATA_SOURCE_H
8#define BASE_HOST_DATA_SOURCE_H
9
10#include <asiolink/io_address.h>
12#include <dhcpsrv/host.h>
14
15#include <cstdint>
16#include <limits>
17#include <vector>
18
19#include <boost/shared_ptr.hpp>
20
21namespace isc {
22namespace dhcp {
23
25class DuplicateHost : public Exception {
26public:
27 DuplicateHost(const char* file, size_t line, const char* what) :
28 isc::Exception(file, line, what) { }
29};
30
32class HostNotFound : public Exception {
33public:
34 HostNotFound(const char* file, size_t line, const char* what) :
35 isc::Exception(file, line, what) { }
36};
37
40class ReservedAddress : public Exception {
41public:
42 ReservedAddress(const char* file, size_t line, const char* what) :
43 isc::Exception(file, line, what) { }
44};
45
49public:
50 BadHostAddress(const char* file, size_t line, const char* what) :
51 isc::BadValue(file, line, what) { }
52};
53
56public:
57
63 explicit HostPageSize(const size_t page_size) : page_size_(page_size) {
64 if (page_size_ == 0) {
65 isc_throw(OutOfRange, "page size of retrieved hosts must not be 0");
66 }
67 if (page_size_ > std::numeric_limits<uint32_t>::max()) {
68 isc_throw(OutOfRange, "page size of retrieved hosts must not be greate than "
69 << std::numeric_limits<uint32_t>::max());
70 }
71 }
72
73 const size_t page_size_;
74};
75
91public:
92
102 enum IdType : uint16_t {
105 };
106
109
123 virtual ConstHostCollection
124 getAll(const Host::IdentifierType& identifier_type,
125 const uint8_t* identifier_begin,
126 const size_t identifier_len) const = 0;
127
136 virtual ConstHostCollection
137 getAll4(const SubnetID& subnet_id) const = 0;
138
147 virtual ConstHostCollection
148 getAll6(const SubnetID& subnet_id) const = 0;
149
162 virtual ConstHostCollection
163 getAllbyHostname(const std::string& hostname) const = 0;
164
174 virtual ConstHostCollection
175 getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id) const = 0;
176
186 virtual ConstHostCollection
187 getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id) const = 0;
188
206 virtual ConstHostCollection
207 getPage4(const SubnetID& subnet_id,
208 size_t& source_index,
209 uint64_t lower_host_id,
210 const HostPageSize& page_size) const = 0;
211
229 virtual ConstHostCollection
230 getPage6(const SubnetID& subnet_id,
231 size_t& source_index,
232 uint64_t lower_host_id,
233 const HostPageSize& page_size) const = 0;
234
251 virtual ConstHostCollection
252 getPage4(size_t& source_index,
253 uint64_t lower_host_id,
254 const HostPageSize& page_size) const = 0;
255
272 virtual ConstHostCollection
273 getPage6(size_t& source_index,
274 uint64_t lower_host_id,
275 const HostPageSize& page_size) const = 0;
276
285 virtual ConstHostCollection
286 getAll4(const asiolink::IOAddress& address) const = 0;
287
298 virtual ConstHostPtr
299 get4(const SubnetID& subnet_id,
300 const Host::IdentifierType& identifier_type,
301 const uint8_t* identifier_begin,
302 const size_t identifier_len) const = 0;
303
320 virtual ConstHostPtr
321 get4(const SubnetID& subnet_id,
322 const asiolink::IOAddress& address) const = 0;
323
347 virtual ConstHostCollection
348 getAll4(const SubnetID& subnet_id,
349 const asiolink::IOAddress& address) const = 0;
350
361 virtual ConstHostPtr
362 get6(const SubnetID& subnet_id,
363 const Host::IdentifierType& identifier_type,
364 const uint8_t* identifier_begin,
365 const size_t identifier_len) const = 0;
366
373 virtual ConstHostPtr
374 get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const = 0;
375
383 virtual ConstHostPtr
384 get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const = 0;
385
409 virtual ConstHostCollection
410 getAll6(const SubnetID& subnet_id,
411 const asiolink::IOAddress& address) const = 0;
412
434 virtual ConstHostCollection
435 getAll6(const asiolink::IOAddress& address) const = 0;
436
447 virtual void add(const HostPtr& host) = 0;
448
457 virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr) = 0;
458
470 virtual bool del4(const SubnetID& subnet_id,
471 const Host::IdentifierType& identifier_type,
472 const uint8_t* identifier_begin, const size_t identifier_len) = 0;
473
485 virtual bool del6(const SubnetID& subnet_id,
486 const Host::IdentifierType& identifier_type,
487 const uint8_t* identifier_begin, const size_t identifier_len) = 0;
488
496 virtual void update(HostPtr const& host) {
497 bool deleted(false);
498 if (host->getIPv4SubnetID() != SUBNET_ID_UNUSED) {
499 std::vector<uint8_t> const& identifier(host->getIdentifier());
500 deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
501 identifier.size());
502 } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) {
503 std::vector<uint8_t> const& identifier(host->getIdentifier());
504 deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
505 identifier.size());
506 } else {
507 isc_throw(HostNotFound, "Mandatory 'subnet-id' parameter missing.");
508 }
509 if (!deleted) {
510 isc_throw(HostNotFound, "Host not updated (not found).");
511 }
512 add(host);
513 }
514
520 virtual std::string getType() const = 0;
521
530
535 virtual void commit() {}
536
541 virtual void rollback() {}
542
561 virtual bool setIPReservationsUnique(const bool unique) = 0;
562
568 virtual bool isUnusable() {
569 return (false);
570 }
571};
572
574typedef boost::shared_ptr<BaseHostDataSource> HostDataSourcePtr;
575
577typedef std::vector<HostDataSourcePtr> HostDataSourceList;
578
579} // namespace dhcp
580} // namespace isc
581
582#endif // BASE_HOST_DATA_SOURCE_H
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
BadValue(const char *file, size_t line, const char *what)
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
BadHostAddress(const char *file, size_t line, const char *what)
Base interface for the classes implementing simple data source for host reservations.
virtual std::string getType() const =0
Return backend type.
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
virtual ConstHostCollection getAll6(const asiolink::IOAddress &address) const =0
Returns all hosts having a reservation for a specified address or delegated prefix (lease) in all sub...
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const =0
Return all hosts with a hostname in a DHCPv6 subnet.
virtual void add(const HostPtr &host)=0
Adds a new host to the collection.
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const =0
Return all hosts in a DHCPv6 subnet.
virtual void commit()
Commit Transactions.
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const =0
Return all hosts with a hostname in a DHCPv4 subnet.
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)=0
Attempts to delete hosts by (subnet-id, address).
virtual ConstHostCollection getPage4(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts in a DHCPv4 subnet.
virtual ConstHostPtr get4(const SubnetID &subnet_id, const asiolink::IOAddress &address) const =0
Returns a host connected to the IPv4 subnet and having a reservation for a specified IPv4 address.
virtual ConstHostPtr get4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const =0
Returns a host connected to the IPv4 subnet.
virtual ConstHostCollection getAll4(const SubnetID &subnet_id, const asiolink::IOAddress &address) const =0
Returns all hosts connected to the IPv4 subnet and having a reservation for a specified address.
virtual bool del6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)=0
Attempts to delete a host by (subnet-id6, identifier, identifier-type).
virtual bool setIPReservationsUnique(const bool unique)=0
Controls whether IP reservations are unique or non-unique.
virtual ConstHostPtr get6(const SubnetID &subnet_id, const asiolink::IOAddress &address) const =0
Returns a host connected to the IPv6 subnet and having a reservation for a specified IPv6 address or ...
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
virtual ConstHostCollection getPage6(size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts.
virtual ConstHostCollection getAll4(const asiolink::IOAddress &address) const =0
Returns a collection of hosts using the specified IPv4 address.
virtual ConstHostCollection getPage4(size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts.
virtual bool del4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)=0
Attempts to delete a host by (subnet-id4, identifier, identifier-type).
virtual ConstHostCollection getPage6(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts in a DHCPv6 subnet.
virtual ConstHostPtr get6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const =0
Returns a host connected to the IPv6 subnet.
virtual ConstHostPtr get6(const asiolink::IOAddress &prefix, const uint8_t prefix_len) const =0
Returns a host using the specified IPv6 prefix.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const =0
Return all hosts with a hostname.
virtual void update(HostPtr const &host)
Attempts to update an existing host entry.
IdType
Specifies the type of an identifier.
virtual ConstHostCollection getAll6(const SubnetID &subnet_id, const asiolink::IOAddress &address) const =0
Returns all hosts connected to the IPv6 subnet and having a reservation for a specified address or de...
virtual ~BaseHostDataSource()
Default destructor implementation.
virtual ConstHostCollection getAll(const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const =0
Return all hosts connected to any subnet for which reservations have been made using a specified iden...
virtual void rollback()
Rollback Transactions.
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const =0
Return all hosts in a DHCPv4 subnet.
DuplicateHost(const char *file, size_t line, const char *what)
Exception thrown when a Host object is expected, but none are found.
HostNotFound(const char *file, size_t line, const char *what)
Wraps value holding size of the page with host reservations.
const size_t page_size_
Holds page size.
HostPageSize(const size_t page_size)
Constructor.
IdentifierType
Type of the host identifier.
Definition host.h:340
ReservedAddress(const char *file, size_t line, const char *what)
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
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.
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.