Kea 3.3.3
host.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_H
8#define HOST_H
9
10#include <asiolink/io_address.h>
11#include <cc/data.h>
12#include <cc/user_context.h>
13#include <dhcp/classify.h>
14#include <dhcp/duid.h>
15#include <dhcp/hwaddr.h>
17#include <dhcpsrv/cfg_option.h>
18#include <dhcpsrv/subnet_id.h>
19
20#include <cstdint>
21#include <list>
22#include <map>
23#include <string>
24#include <utility>
25
26#include <boost/algorithm/string.hpp>
27#include <boost/shared_ptr.hpp>
28
29namespace isc {
30namespace dhcp {
31
36const size_t ADDRESS6_TEXT_MAX_LEN = 39;
37
40const size_t CLIENT_CLASSES_MAX_LEN = 255;
41
46const size_t HOSTNAME_MAX_LEN = 255;
47
49const size_t OPTION_VALUE_MAX_LEN = 4096;
50
53
55const size_t OPTION_SPACE_MAX_LEN = 128;
56
58const size_t USER_CONTEXT_MAX_LEN = 8192;
59
61const size_t SERVER_HOSTNAME_MAX_LEN = 64;
62
64const size_t BOOT_FILE_NAME_MAX_LEN = 128;
65
67const uint8_t AUTH_KEY_LEN = 16;
68
71
73typedef uint64_t HostID;
74
79class AuthKey {
80public:
86 AuthKey(const std::vector<uint8_t>& key);
87
96 AuthKey(const std::string& key);
97
102 AuthKey();
103
104 // @brief Get random string.
109 static std::vector<uint8_t> getRandomKeyString();
110
114 // If the size is greater than 16 bytes, we resize to 16 bytes.
117 void setAuthKey(const std::vector<uint8_t>& key);
118
126 void setAuthKey(const std::string& key);
127
131 const std::vector<uint8_t>& getAuthKey() const {
132 return authKey_;
133 }
134
138 std::string toText() const;
139
145 bool operator==(const AuthKey& other) const;
146
151 bool operator!=(const AuthKey& other) const;
152
153private:
154 std::vector<uint8_t> authKey_;
155};
156
167public:
168
172 enum Type : uint16_t {
175 };
176
190 IPv6Resrv(const Type& type,
191 const asiolink::IOAddress& prefix,
192 const uint8_t prefix_len = 128);
193
196 return (prefix_);
197 }
198
200 uint8_t getPrefixLen() const {
201 return (prefix_len_);
202 }
203
209 Type getType() const {
210 return (type_);
211 }
212
217 return (pd_exclude_option_);
218 }
219
230 void set(const Type& type, const asiolink::IOAddress& prefix,
231 const uint8_t prefix_len);
232
239 void setPDExclude(const asiolink::IOAddress& excluded_prefix,
240 const uint8_t excluded_prefix_len);
241
246 std::string toText(bool display_pd_exclude_option = true) const;
247
250 std::string PDExcludetoText() const;
251
257 bool operator==(const IPv6Resrv& other) const;
258
262 bool operator!=(const IPv6Resrv& other) const;
263
264private:
265
266 Type type_;
267 asiolink::IOAddress prefix_;
268 uint8_t prefix_len_;
269 Option6PDExcludePtr pd_exclude_option_;
270};
271
273typedef std::multimap<IPv6Resrv::Type, IPv6Resrv> IPv6ResrvCollection;
274typedef IPv6ResrvCollection::const_iterator IPv6ResrvIterator;
275typedef std::pair<IPv6Resrv::Type, IPv6Resrv> IPv6ResrvTuple;
276typedef std::pair<IPv6ResrvIterator, IPv6ResrvIterator> IPv6ResrvRange;
277
330class Host : public data::UserContext {
331public:
332
347
351
382 Host(const uint8_t* identifier, const size_t identifier_len,
383 const IdentifierType& identifier_type,
384 const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
385 const asiolink::IOAddress& ipv4_reservation,
386 const std::string& hostname = "",
387 const std::string& dhcp4_client_classes = "",
388 const std::string& dhcp6_client_classes = "",
390 const std::string& server_host_name = "",
391 const std::string& boot_file_name = "",
392 const AuthKey& auth_key = AuthKey(""));
393
434 Host(const std::string& identifier, const std::string& identifier_name,
435 const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
436 const asiolink::IOAddress& ipv4_reservation,
437 const std::string& hostname = "",
438 const std::string& dhcp4_client_classes = "",
439 const std::string& dhcp6_client_classes = "",
441 const std::string& server_host_name = "",
442 const std::string& boot_file_name = "",
443 const AuthKey& auth_key = AuthKey(""));
444
450 static size_t getIdentifierMaxLength(const IdentifierType& type);
451
462 void setIdentifier(const uint8_t* identifier, const size_t len,
463 const IdentifierType& type);
464
475 void setIdentifier(const std::string& identifier, const std::string& name);
476
481 HWAddrPtr getHWAddress() const;
482
487 DuidPtr getDuid() const;
488
493 const std::vector<uint8_t>& getIdentifier() const;
494
498
503 static IdentifierType getIdentifierType(const std::string& identifier_name);
504
508 std::string getIdentifierAsText() const;
509
511 static std::string getIdentifierName(const IdentifierType& type);
512
519 static std::string getIdentifierAsText(const IdentifierType& type,
520 const uint8_t* value,
521 const size_t length);
522
526 void setIPv4SubnetID(const SubnetID ipv4_subnet_id) {
527 ipv4_subnet_id_ = ipv4_subnet_id;
528 }
529
533 void setIPv6SubnetID(const SubnetID ipv6_subnet_id) {
534 ipv6_subnet_id_ = ipv6_subnet_id;
535 }
536
539 return (ipv4_subnet_id_);
540 }
541
544 return (ipv6_subnet_id_);
545 }
546
555 void setIPv4Reservation(const asiolink::IOAddress& address);
556
561
566 return (ipv4_reservation_);
567 }
568
572 void addReservation(const IPv6Resrv& reservation);
573
581
587
591 bool hasIPv6Reservation() const;
592
599 bool hasReservation(const IPv6Resrv& reservation) const;
600
604 void setHostname(const std::string& hostname) {
605 hostname_ = hostname;
606 }
607
609 const std::string& getHostname() const {
610 return (hostname_);
611 }
612
614 std::string getLowerHostname() const {
615 return (boost::algorithm::to_lower_copy(hostname_));
616 }
617
621 void addClientClass4(const std::string& class_name);
622
625 return (dhcp4_client_classes_);
626 }
627
631 void addClientClass6(const std::string& class_name);
632
635 return (dhcp6_client_classes_);
636 }
637
644 void setNextServer(const asiolink::IOAddress& next_server);
645
648 return (next_server_);
649 }
650
656 void setServerHostname(const std::string& server_host_name);
657
659 const std::string& getServerHostname() const {
660 return (server_host_name_);
661 }
662
668 void setBootFileName(const std::string& boot_file_name);
669
671 const std::string& getBootFileName() const {
672 return (boot_file_name_);
673 }
674
681 return (cfg_option4_);
682 }
683
687 return (cfg_option4_);
688 }
689
696 return (cfg_option6_);
697 }
698
702 return (cfg_option6_);
703 }
704
709 void encapsulateOptions() const;
710
712 std::string toText() const;
713
716 void setHostId(HostID id) {
717 host_id_ = id;
718 }
719
723 return (host_id_);
724 }
725
731 void setNegative(bool negative) {
732 negative_ = negative;
733 }
734
737 bool getNegative() const {
738 return (negative_);
739 }
740
745
750
754 void setKey(const AuthKey& key) {
755 key_ = key;
756 }
757
761 AuthKey getKey() const {
762 return(key_);
763 }
764
765protected:
766
772 void setIdentifierType(const IdentifierType& type);
773
774private:
775
786 void addClientClassInternal(ClientClasses& classes,
787 const std::string& class_name);
788
790 IdentifierType identifier_type_;
792 std::vector<uint8_t> identifier_value_;
794 SubnetID ipv4_subnet_id_;
796 SubnetID ipv6_subnet_id_;
798 asiolink::IOAddress ipv4_reservation_;
800 IPv6ResrvCollection ipv6_reservations_;
802 std::string hostname_;
804 ClientClasses dhcp4_client_classes_;
806 ClientClasses dhcp6_client_classes_;
808 asiolink::IOAddress next_server_;
810 std::string server_host_name_;
812 std::string boot_file_name_;
813
816 uint64_t host_id_;
817
819 CfgOptionPtr cfg_option4_;
821 CfgOptionPtr cfg_option6_;
822
828 bool negative_;
829
836 AuthKey key_;
837};
838
840typedef boost::shared_ptr<Host> HostPtr;
841
843typedef boost::shared_ptr<const Host> ConstHostPtr;
844
846typedef std::vector<ConstHostPtr> ConstHostCollection;
847
849typedef std::vector<HostPtr> HostCollection;
850
851}
852}
853
854#endif // HOST_H
Defines elements for storing the names of client classes.
Authentication keys.
Definition host.h:79
AuthKey()
Constructor.
Definition host.cc:36
bool operator!=(const AuthKey &other) const
Inequality operator.
Definition host.cc:82
static std::vector< uint8_t > getRandomKeyString()
Random string is generated by default will be used for the keys to be used for signing Reconfigure Me...
Definition host.cc:41
std::string toText() const
Return text format for keys.
Definition host.cc:46
AuthKey(const std::vector< uint8_t > &key)
Constructor.
Definition host.cc:28
const std::vector< uint8_t > & getAuthKey() const
Return auth key.
Definition host.h:131
void setAuthKey(const std::vector< uint8_t > &key)
Set auth key value from binary.
Definition host.cc:54
bool operator==(const AuthKey &other) const
Equality operator.
Definition host.cc:77
Container for storing client class names.
Definition classify.h:111
void addClientClass4(const std::string &class_name)
Adds new client class for DHCPv4.
Definition host.cc:508
void setServerHostname(const std::string &server_host_name)
Sets new value for server hostname (sname).
Definition host.cc:541
static size_t getIdentifierMaxLength(const IdentifierType &type)
Get maximum identifier length.
Definition host.cc:259
void setHostId(HostID id)
Sets Host ID (primary key in MySQL and PostgreSQL backends).
Definition host.h:716
void encapsulateOptions() const
Encapsulates host-specific options with their suboptions.
Definition host.cc:693
const ClientClasses & getClientClasses6() const
Returns classes which DHCPv6 client is associated with.
Definition host.h:634
CfgOptionPtr getCfgOption4()
Returns pointer to the DHCPv4 option data configuration for this host.
Definition host.h:680
void setNegative(bool negative)
Sets the negative cached flag.
Definition host.h:731
std::string toText() const
Returns information about the host in the textual format.
Definition host.cc:703
void addClientClass6(const std::string &class_name)
Adds new client class for DHCPv6.
Definition host.cc:514
SubnetID getIPv4SubnetID() const
Returns subnet identifier for IPv4 reservation.
Definition host.h:538
IdentifierType getIdentifierType() const
Returns the identifier type.
Definition host.cc:280
std::string getLowerHostname() const
Returns reserved hostname in lower case.
Definition host.h:614
void setIdentifier(const uint8_t *identifier, const size_t len, const IdentifierType &type)
Replaces currently used identifier with a new identifier.
Definition host.cc:381
SubnetID getIPv6SubnetID() const
Returns subnet identifier for IPv6 reservations.
Definition host.h:543
void setHostname(const std::string &hostname)
Sets new hostname.
Definition host.h:604
const asiolink::IOAddress & getIPv4Reservation() const
Returns reserved IPv4 address.
Definition host.h:565
static const IdentifierType LAST_IDENTIFIER_TYPE
Constant pointing to the last identifier of the IdentifierType enumeration.
Definition host.h:350
void setIdentifierType(const IdentifierType &type)
Set the identifier type.
Definition host.cc:443
const std::string & getHostname() const
Returns reserved hostname.
Definition host.h:609
IPv6ResrvRange getIPv6Reservations() const
Returns all IPv6 reservations.
Definition host.cc:482
const std::string & getBootFileName() const
Returns value of boot file name (file).
Definition host.h:671
bool hasIPv6Reservation() const
Checks if there is at least one IPv6 reservation for this host.
Definition host.cc:488
Host(const uint8_t *identifier, const size_t identifier_len, const IdentifierType &identifier_type, const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id, const asiolink::IOAddress &ipv4_reservation, const std::string &hostname="", const std::string &dhcp4_client_classes="", const std::string &dhcp6_client_classes="", const asiolink::IOAddress &next_server=asiolink::IOAddress::IPV4_ZERO_ADDRESS(), const std::string &server_host_name="", const std::string &boot_file_name="", const AuthKey &auth_key=AuthKey(""))
Constructor.
Definition host.cc:184
const std::vector< uint8_t > & getIdentifier() const
Returns the identifier in a binary form.
Definition host.cc:275
isc::data::ElementPtr toElement6() const
Unparses (converts to Element representation) IPv6 host.
Definition host.cc:620
ConstCfgOptionPtr getCfgOption4() const
Returns const pointer to the DHCPv4 option data configuration for this host.
Definition host.h:686
void addReservation(const IPv6Resrv &reservation)
Adds new IPv6 reservation.
Definition host.cc:465
const ClientClasses & getClientClasses4() const
Returns classes which DHCPv4 client is associated with.
Definition host.h:624
static std::string getIdentifierName(const IdentifierType &type)
Returns name of the identifier of a specified type.
Definition host.cc:356
const std::string & getServerHostname() const
Returns value of server hostname (sname).
Definition host.h:659
void setBootFileName(const std::string &boot_file_name)
Sets new value for boot file name (file).
Definition host.cc:550
void setNextServer(const asiolink::IOAddress &next_server)
Sets new value for next server field (siaddr).
Definition host.cc:528
CfgOptionPtr getCfgOption6()
Returns pointer to the DHCPv6 option data configuration for this host.
Definition host.h:695
bool hasReservation(const IPv6Resrv &reservation) const
Checks if specified IPv6 reservation exists for the host.
Definition host.cc:493
const asiolink::IOAddress & getNextServer() const
Returns value of next server field (siaddr).
Definition host.h:647
ConstCfgOptionPtr getCfgOption6() const
Returns const pointer to the DHCPv6 option data configuration for this host.
Definition host.h:701
std::string getIdentifierAsText() const
Returns host identifier in a textual form.
Definition host.cc:319
IdentifierType
Type of the host identifier.
Definition host.h:340
@ IDENT_FLEX
Flexible host identifier.
Definition host.h:345
@ IDENT_CLIENT_ID
Definition host.h:344
@ IDENT_CIRCUIT_ID
Definition host.h:343
AuthKey getKey() const
Returns the key.
Definition host.h:761
isc::data::ElementPtr toElement4() const
Unparses (converts to Element representation) IPv4 host.
Definition host.cc:559
DuidPtr getDuid() const
Returns DUID for which the reservations are made.
Definition host.cc:312
void setIPv4SubnetID(const SubnetID ipv4_subnet_id)
Sets new IPv4 subnet identifier.
Definition host.h:526
void removeIPv4Reservation()
Removes the IPv4 reservation.
Definition host.cc:460
void setIPv6SubnetID(const SubnetID ipv6_subnet_id)
Sets new IPv6 subnet identifier.
Definition host.h:533
HWAddrPtr getHWAddress() const
Returns hardware address for which the reservations are made.
Definition host.cc:306
void setKey(const AuthKey &key)
sets key.
Definition host.h:754
HostID getHostId() const
Returns Host ID (primary key in MySQL and PostgreSQL backends).
Definition host.h:722
bool getNegative() const
Return the negative cache flag value.
Definition host.h:737
void setIPv4Reservation(const asiolink::IOAddress &address)
Sets new IPv4 reservation.
Definition host.cc:448
IPv6 reservation for a host.
Definition host.h:166
Type
Type of the reservation.
Definition host.h:172
void set(const Type &type, const asiolink::IOAddress &prefix, const uint8_t prefix_len)
Sets a new prefix and prefix length.
Definition host.cc:96
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Definition host.h:195
std::string PDExcludetoText() const
Returns information about the Prefix Exclude option of the reservation the textual format.
Definition host.cc:159
Type getType() const
Returns reservation type.
Definition host.h:209
void setPDExclude(const asiolink::IOAddress &excluded_prefix, const uint8_t excluded_prefix_len)
Sets the Prefix Exclude option.
Definition host.cc:127
uint8_t getPrefixLen() const
Returns prefix length.
Definition host.h:200
IPv6Resrv(const Type &type, const asiolink::IOAddress &prefix, const uint8_t prefix_len=128)
Constructor.
Definition host.cc:86
Option6PDExcludePtr getPDExclude() const
Returns the Prefix Exclude option.
Definition host.h:216
bool operator==(const IPv6Resrv &other) const
Equality operator.
Definition host.cc:173
std::string toText(bool display_pd_exclude_option=true) const
Returns information about the reservation in the textual format.
Definition host.cc:139
bool operator!=(const IPv6Resrv &other) const
Inequality operator.
Definition host.cc:180
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
Definition host.h:274
const size_t OPTION_FORMATTED_VALUE_MAX_LEN
Maximum length of option value specified in textual format.
Definition host.h:52
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition cfg_option.h:983
boost::shared_ptr< Option6PDExclude > Option6PDExcludePtr
Pointer to the Option6PDExclude object.
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
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
const size_t CLIENT_CLASSES_MAX_LEN
Maximum length of classes stored in a dhcp4/6_client_classes columns.
Definition host.h:40
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition host.h:276
std::vector< HostPtr > HostCollection
Collection of the Host objects.
Definition host.h:849
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:166
const size_t TEXT_AUTH_KEY_LEN
Maximum length of authentication keys (coded in hexadecimal).
Definition host.h:70
const uint8_t AUTH_KEY_LEN
Maximum length of authentication keys - 128 bits.
Definition host.h:67
std::multimap< IPv6Resrv::Type, IPv6Resrv > IPv6ResrvCollection
Collection of IPv6 reservations for the host.
Definition host.h:273
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
uint64_t HostID
HostID (used only when storing in MySQL or PostgreSQL backends).
Definition host.h:73
const size_t ADDRESS6_TEXT_MAX_LEN
Maximum size of an IPv6 address represented as a text string.
Definition host.h:36
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition host.h:843
const size_t HOSTNAME_MAX_LEN
Maximum length of the hostname stored in DNS.
Definition host.h:46
const size_t USER_CONTEXT_MAX_LEN
Maximum length of user context.
Definition host.h:58
std::pair< IPv6Resrv::Type, IPv6Resrv > IPv6ResrvTuple
Definition host.h:275
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
Definition host.h:49
const size_t SERVER_HOSTNAME_MAX_LEN
Maximum length of the server hostname.
Definition host.h:61
const size_t BOOT_FILE_NAME_MAX_LEN
Maximum length of the boot file name.
Definition host.h:64
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:986
const size_t OPTION_SPACE_MAX_LEN
Maximum length of option space name.
Definition host.h:55
Defines the logger used by the top-level component of kea-lfc.
Base class for user context.