Kea: base_host_data_source.h Source File

| Kea 3.2.0-git

base_host_data_source.h
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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>
11#include <database/database_connection.h>
12#include <dhcpsrv/host.h>
13#include <exceptions/exceptions.h>
14#include <boost/shared_ptr.hpp>
15
16#include <limits>
17#include <vector>
18
19namespace isc {
20namespace dhcp {
21
23 class DuplicateHost : public Exception {
24public:
25 DuplicateHost(const char* file, size_t line, const char* what) :
26 isc::Exception(file, line, what) { }
27};
28
30 class HostNotFound : public Exception {
31public:
32 HostNotFound(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) { }
34};
35
38 class ReservedAddress : public Exception {
39public:
40 ReservedAddress(const char* file, size_t line, const char* what) :
41 isc::Exception(file, line, what) { }
42};
43
46 class BadHostAddress : public isc::BadValue {
47public:
48 BadHostAddress(const char* file, size_t line, const char* what) :
49 isc::BadValue(file, line, what) { }
50};
51
53 class HostPageSize {
54public:
55
61 explicit HostPageSize(const size_t page_size) : page_size_(page_size) {
62 if (page_size_ == 0) {
63 isc_throw(OutOfRange, "page size of retrieved hosts must not be 0");
64 }
65 if (page_size_ > std::numeric_limits<uint32_t>::max()) {
66 isc_throw(OutOfRange, "page size of retrieved hosts must not be greate than "
67 << std::numeric_limits<uint32_t>::max());
68 }
69 }
70
71 const size_t page_size_;
72};
73
88 class BaseHostDataSource {
89public:
90
100 enum IdType {
101 ID_HWADDR = 0,
102 ID_DUID = 1
103 };
104
106 virtual ~BaseHostDataSource() { }
107
121 virtual ConstHostCollection
122 getAll(const Host::IdentifierType& identifier_type,
123 const uint8_t* identifier_begin,
124 const size_t identifier_len) const = 0;
125
134 virtual ConstHostCollection
135 getAll4(const SubnetID& subnet_id) const = 0;
136
145 virtual ConstHostCollection
146 getAll6(const SubnetID& subnet_id) const = 0;
147
160 virtual ConstHostCollection
161 getAllbyHostname(const std::string& hostname) const = 0;
162
172 virtual ConstHostCollection
173 getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id) const = 0;
174
184 virtual ConstHostCollection
185 getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id) const = 0;
186
204 virtual ConstHostCollection
205 getPage4(const SubnetID& subnet_id,
206 size_t& source_index,
207 uint64_t lower_host_id,
208 const HostPageSize& page_size) const = 0;
209
227 virtual ConstHostCollection
228 getPage6(const SubnetID& subnet_id,
229 size_t& source_index,
230 uint64_t lower_host_id,
231 const HostPageSize& page_size) const = 0;
232
249 virtual ConstHostCollection
250 getPage4(size_t& source_index,
251 uint64_t lower_host_id,
252 const HostPageSize& page_size) const = 0;
253
270 virtual ConstHostCollection
271 getPage6(size_t& source_index,
272 uint64_t lower_host_id,
273 const HostPageSize& page_size) const = 0;
274
283 virtual ConstHostCollection
284 getAll4(const asiolink::IOAddress& address) const = 0;
285
296 virtual ConstHostPtr
297 get4(const SubnetID& subnet_id,
298 const Host::IdentifierType& identifier_type,
299 const uint8_t* identifier_begin,
300 const size_t identifier_len) const = 0;
301
318 virtual ConstHostPtr
319 get4(const SubnetID& subnet_id,
320 const asiolink::IOAddress& address) const = 0;
321
345 virtual ConstHostCollection
346 getAll4(const SubnetID& subnet_id,
347 const asiolink::IOAddress& address) const = 0;
348
359 virtual ConstHostPtr
360 get6(const SubnetID& subnet_id,
361 const Host::IdentifierType& identifier_type,
362 const uint8_t* identifier_begin,
363 const size_t identifier_len) const = 0;
364
371 virtual ConstHostPtr
372 get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const = 0;
373
381 virtual ConstHostPtr
382 get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const = 0;
383
407 virtual ConstHostCollection
408 getAll6(const SubnetID& subnet_id,
409 const asiolink::IOAddress& address) const = 0;
410
432 virtual ConstHostCollection
433 getAll6(const asiolink::IOAddress& address) const = 0;
434
445 virtual void add(const HostPtr& host) = 0;
446
455 virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr) = 0;
456
468 virtual bool del4(const SubnetID& subnet_id,
469 const Host::IdentifierType& identifier_type,
470 const uint8_t* identifier_begin, const size_t identifier_len) = 0;
471
483 virtual bool del6(const SubnetID& subnet_id,
484 const Host::IdentifierType& identifier_type,
485 const uint8_t* identifier_begin, const size_t identifier_len) = 0;
486
494 virtual void update(HostPtr const& host) {
495 bool deleted(false);
496 if (host->getIPv4SubnetID() != SUBNET_ID_UNUSED) {
497 std::vector<uint8_t> const& identifier(host->getIdentifier());
498 deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
499 identifier.size());
500 } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) {
501 std::vector<uint8_t> const& identifier(host->getIdentifier());
502 deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
503 identifier.size());
504 } else {
505 isc_throw(HostNotFound, "Mandatory 'subnet-id' parameter missing.");
506 }
507 if (!deleted) {
508 isc_throw(HostNotFound, "Host not updated (not found).");
509 }
510 add(host);
511 }
512
518 virtual std::string getType() const = 0;
519
525 virtual isc::db::DatabaseConnection::ParameterMap getParameters() const {
526 return (isc::db::DatabaseConnection::ParameterMap());
527 }
528
533 virtual void commit() {}
534
539 virtual void rollback() {}
540
559 virtual bool setIPReservationsUnique(const bool unique) = 0;
560
566 virtual bool isUnusable() {
567 return (false);
568 }
569};
570
572 typedef boost::shared_ptr<BaseHostDataSource> HostDataSourcePtr;
573
575 typedef std::vector<HostDataSourcePtr> HostDataSourceList;
576
577} // namespace dhcp
578} // namespace isc
579
580#endif // BASE_HOST_DATA_SOURCE_H
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::BadValue::BadValue
BadValue(const char *file, size_t line, const char *what)
Definition exceptions/exceptions.h:134
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition database_connection.h:137
isc::dhcp::BadHostAddress::BadHostAddress
BadHostAddress(const char *file, size_t line, const char *what)
Definition base_host_data_source.h:48
isc::dhcp::BaseHostDataSource
Base interface for the classes implementing simple data source for host reservations.
Definition base_host_data_source.h:88
isc::dhcp::BaseHostDataSource::getType
virtual std::string getType() const =0
Return backend type.
isc::dhcp::BaseHostDataSource::isUnusable
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
Definition base_host_data_source.h:566
isc::dhcp::BaseHostDataSource::getAll6
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...
isc::dhcp::BaseHostDataSource::getAllbyHostname6
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const =0
Return all hosts with a hostname in a DHCPv6 subnet.
isc::dhcp::BaseHostDataSource::add
virtual void add(const HostPtr &host)=0
Adds a new host to the collection.
isc::dhcp::BaseHostDataSource::getAll6
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const =0
Return all hosts in a DHCPv6 subnet.
isc::dhcp::BaseHostDataSource::commit
virtual void commit()
Commit Transactions.
Definition base_host_data_source.h:533
isc::dhcp::BaseHostDataSource::getAllbyHostname4
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const =0
Return all hosts with a hostname in a DHCPv4 subnet.
isc::dhcp::BaseHostDataSource::del
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)=0
Attempts to delete hosts by (subnet-id, address).
isc::dhcp::BaseHostDataSource::getPage4
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.
isc::dhcp::BaseHostDataSource::get4
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.
isc::dhcp::BaseHostDataSource::get4
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.
isc::dhcp::BaseHostDataSource::getAll4
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.
isc::dhcp::BaseHostDataSource::del6
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).
isc::dhcp::BaseHostDataSource::setIPReservationsUnique
virtual bool setIPReservationsUnique(const bool unique)=0
Controls whether IP reservations are unique or non-unique.
isc::dhcp::BaseHostDataSource::get6
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 ...
isc::dhcp::BaseHostDataSource::getParameters
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
Definition base_host_data_source.h:525
isc::dhcp::BaseHostDataSource::IdType
IdType
Specifies the type of an identifier.
Definition base_host_data_source.h:100
isc::dhcp::BaseHostDataSource::ID_HWADDR
@ ID_HWADDR
Hardware address.
Definition base_host_data_source.h:101
isc::dhcp::BaseHostDataSource::ID_DUID
@ ID_DUID
DUID/client-id.
Definition base_host_data_source.h:102
isc::dhcp::BaseHostDataSource::getPage6
virtual ConstHostCollection getPage6(size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts.
isc::dhcp::BaseHostDataSource::getAll4
virtual ConstHostCollection getAll4(const asiolink::IOAddress &address) const =0
Returns a collection of hosts using the specified IPv4 address.
isc::dhcp::BaseHostDataSource::getPage4
virtual ConstHostCollection getPage4(size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const =0
Returns range of hosts.
isc::dhcp::BaseHostDataSource::del4
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).
isc::dhcp::BaseHostDataSource::getPage6
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.
isc::dhcp::BaseHostDataSource::get6
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.
isc::dhcp::BaseHostDataSource::get6
virtual ConstHostPtr get6(const asiolink::IOAddress &prefix, const uint8_t prefix_len) const =0
Returns a host using the specified IPv6 prefix.
isc::dhcp::BaseHostDataSource::getAllbyHostname
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const =0
Return all hosts with a hostname.
isc::dhcp::BaseHostDataSource::update
virtual void update(HostPtr const &host)
Attempts to update an existing host entry.
Definition base_host_data_source.h:494
isc::dhcp::BaseHostDataSource::getAll6
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...
isc::dhcp::BaseHostDataSource::~BaseHostDataSource
virtual ~BaseHostDataSource()
Default destructor implementation.
Definition base_host_data_source.h:106
isc::dhcp::BaseHostDataSource::getAll
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...
isc::dhcp::BaseHostDataSource::rollback
virtual void rollback()
Rollback Transactions.
Definition base_host_data_source.h:539
isc::dhcp::BaseHostDataSource::getAll4
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const =0
Return all hosts in a DHCPv4 subnet.
isc::dhcp::DuplicateHost::DuplicateHost
DuplicateHost(const char *file, size_t line, const char *what)
Definition base_host_data_source.h:25
isc::dhcp::HostNotFound
Exception thrown when a Host object is expected, but none are found.
Definition base_host_data_source.h:30
isc::dhcp::HostNotFound::HostNotFound
HostNotFound(const char *file, size_t line, const char *what)
Definition base_host_data_source.h:32
isc::dhcp::HostPageSize
Wraps value holding size of the page with host reservations.
Definition base_host_data_source.h:53
isc::dhcp::HostPageSize::page_size_
const size_t page_size_
Holds page size.
Definition base_host_data_source.h:71
isc::dhcp::HostPageSize::HostPageSize
HostPageSize(const size_t page_size)
Constructor.
Definition base_host_data_source.h:61
isc::dhcp::Host::IdentifierType
IdentifierType
Type of the host identifier.
Definition host.h:337
isc::dhcp::ReservedAddress::ReservedAddress
ReservedAddress(const char *file, size_t line, const char *what)
Definition base_host_data_source.h:40
database_connection.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
host.h
io_address.h
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::HostDataSourcePtr
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
Definition base_host_data_source.h:572
isc::dhcp::HostPtr
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition host.h:837
isc::dhcp::ConstHostCollection
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition host.h:843
isc::dhcp::HostDataSourceList
std::vector< HostDataSourcePtr > HostDataSourceList
HostDataSource list.
Definition base_host_data_source.h:575
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::ConstHostPtr
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition host.h:840
isc::util::file
Definition filesystem.cc:29
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18