19#include <boost/foreach.hpp>
47 if (authKey_.empty()) {
68 std::vector<uint8_t> bin;
71 }
catch (
const std::exception& ex) {
78 return (authKey_ == other.authKey_);
83 return (authKey_ != other.authKey_);
88 const uint8_t prefix_len)
90 pd_exclude_option_() {
92 set(type, prefix, prefix_len);
97 const uint8_t prefix_len) {
100 <<
"' for new IPv6 reservation");
102 }
else if (prefix_len > 128) {
104 <<
static_cast<int>(prefix_len)
105 <<
"' for new IPv6 reservation");
107 }
else if ((type ==
TYPE_NA) && (prefix_len != 128)) {
109 <<
static_cast<int>(prefix_len)
110 <<
"' for reserved IPv6 address, expected 128");
111 }
else if ((type ==
TYPE_PD) && (prefix_len != 128)) {
113 if (first_address != prefix) {
115 <<
" exceeds prefix/prefix-len pair: " << first_address
116 <<
"/" <<
static_cast<uint32_t
>(prefix_len));
122 prefix_len_ = prefix_len;
123 pd_exclude_option_.reset();
128 const uint8_t excluded_prefix_len) {
129 if (excluded_prefix_len == 0) {
130 pd_exclude_option_.reset();
134 excluded_prefix_len));
140 std::ostringstream s;
144 s <<
"/" <<
static_cast<int>(prefix_len_);
146 if (display_pd_exclude_option && pd_exclude_option_) {
147 s <<
" (excluded_prefix="
148 << pd_exclude_option_->getExcludedPrefix(prefix_,
149 prefix_len_).toText()
151 <<
static_cast<int>(pd_exclude_option_->getExcludedPrefixLength())
160 if (pd_exclude_option_) {
161 std::ostringstream s;
162 s << pd_exclude_option_->getExcludedPrefix(prefix_,
163 prefix_len_).toText()
165 <<
static_cast<int>(pd_exclude_option_->getExcludedPrefixLength());
174 return (type_ == other.type_ &&
175 prefix_ == other.prefix_ &&
176 prefix_len_ == other.prefix_len_);
181 return (!
operator==(other));
184Host::Host(
const uint8_t* identifier,
const size_t identifier_len,
188 const std::string& hostname,
189 const std::string& dhcp4_client_classes,
190 const std::string& dhcp6_client_classes,
192 const std::string& server_host_name,
193 const std::string& boot_file_name,
196 : identifier_type_(identifier_type),
197 identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
198 ipv6_subnet_id_(ipv6_subnet_id),
200 hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
201 dhcp6_client_classes_(dhcp6_client_classes),
203 server_host_name_(server_host_name), boot_file_name_(boot_file_name),
204 host_id_(0), cfg_option4_(new
CfgOption()),
205 cfg_option6_(new
CfgOption()), negative_(false),
222Host::Host(
const std::string& identifier,
const std::string& identifier_name,
225 const std::string& hostname,
226 const std::string& dhcp4_client_classes,
227 const std::string& dhcp6_client_classes,
229 const std::string& server_host_name,
230 const std::string& boot_file_name,
233 identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
234 ipv6_subnet_id_(ipv6_subnet_id),
236 hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
237 dhcp6_client_classes_(dhcp6_client_classes),
239 server_host_name_(server_host_name), boot_file_name_(boot_file_name),
240 host_id_(0), cfg_option4_(new
CfgOption()),
241 cfg_option6_(new
CfgOption()), negative_(false),
274const std::vector<uint8_t>&
276 return (identifier_value_);
281 return (identifier_type_);
286 if (identifier_name ==
"hw-address") {
289 }
else if (identifier_name ==
"duid") {
292 }
else if (identifier_name ==
"circuit-id") {
295 }
else if (identifier_name ==
"client-id") {
297 }
else if (identifier_name ==
"flex-id") {
301 << identifier_name <<
"'");
321 identifier_value_.size()));
326 const size_t length) {
328 std::ostringstream s;
348 s <<
"(invalid-type)";
350 std::vector<uint8_t> vec(value, value + length);
359 return (
"hw-address");
365 return (
"circuit-id");
368 return (
"client-id");
376 return (
"(unknown)");
388 <<
" length " << len);
391 identifier_type_ = type;
392 identifier_value_.assign(identifier, identifier + len);
398 if (identifier.empty()) {
413 bool too_long =
false;
416 if (binary.empty()) {
420 size_t len = binary.size();
425 <<
" length " << len);
429 identifier_value_.swap(binary);
438 << identifier <<
"'");
444 identifier_type_ = type;
449 if (!address.
isV4()) {
454 << address <<
"' address");
456 ipv4_reservation_ = address;
469 " host reservation for " << reservation.
toText());
478 return (ipv6_reservations_.equal_range(type));
484 ipv6_reservations_.end()));
489 return (!ipv6_reservations_.empty());
495 if (std::distance(reservations.first, reservations.second) > 0) {
496 BOOST_FOREACH(
auto const& it, reservations) {
497 if (it.second == reservation) {
509 addClientClassInternal(dhcp4_client_classes_, class_name);
515 addClientClassInternal(dhcp6_client_classes_, class_name);
520 const std::string& class_name) {
522 if (!trimmed.empty()) {
529 if (!next_server.
isV4()) {
531 <<
"' is not a valid IPv4 address");
534 << next_server <<
"'");
537 next_server_ = next_server;
546 server_host_name_ = server_host_name;
555 boot_file_name_ = boot_file_name;
608 for (
auto const& cclass : cclasses) {
611 map->set(
"client-classes", classes);
614 map->set(
"option-data", opts->toElement());
647 BOOST_FOREACH(
auto const& resv, na_resv) {
650 map->set(
"ip-addresses", resvs);
654 bool has_pd_exclude_option(
false);
655 BOOST_FOREACH(
auto const& resv, pd_resv) {
656 if (resv.second.getPDExclude()) {
657 has_pd_exclude_option =
true;
661 map->set(
"prefixes", resvs);
663 if (has_pd_exclude_option) {
665 BOOST_FOREACH(
auto const& resv, pd_resv) {
668 map->set(
"excluded-prefixes", options);
676 for (
auto const& cclass : cclasses) {
679 map->set(
"client-classes", classes);
683 map->set(
"option-data", opts->toElement());
694 if (!cfg_option4_->isEncapsulated()) {
695 cfg_option4_->encapsulate();
697 if (!cfg_option6_->isEncapsulated()) {
698 cfg_option6_->encapsulate();
704 std::ostringstream s;
710 if (ipv4_subnet_id_ != SUBNET_ID_UNUSED) {
711 s <<
" ipv4_subnet_id=" << ipv4_subnet_id_;
715 if (ipv6_subnet_id_ != SUBNET_ID_UNUSED) {
716 s <<
" ipv6_subnet_id=" << ipv6_subnet_id_;
720 s <<
" hostname=" << (hostname_.empty() ?
"(empty)" : hostname_);
723 s <<
" ipv4_reservation=" << (ipv4_reservation_.isV4Zero() ?
"(no)" :
724 ipv4_reservation_.toText());
727 s <<
" siaddr=" << (next_server_.isV4Zero() ?
"(no)" :
728 next_server_.toText());
731 s <<
" sname=" << (server_host_name_.empty() ?
"(empty)" : server_host_name_);
734 s <<
" file=" << (boot_file_name_.empty() ?
"(empty)" : boot_file_name_);
736 s <<
" key=" << (key_.toText().empty() ?
"(empty)" : key_.toText());
740 if (ipv6_reservations_.empty()) {
741 s <<
" ipv6_reservations=(none)";
746 for (
auto const& resrv : ipv6_reservations_) {
747 s <<
" ipv6_reservation"
749 <<
"=" << resrv.second.toText();
755 for (
auto const& cclass : dhcp4_client_classes_) {
763 for (
auto const& cclass : dhcp6_client_classes_) {
771 s <<
" negative cached";
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
The IOAddress class represents an IP addresses (version agnostic).
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
bool isV4Zero() const
Convenience function to check if it is an IPv4 zero address.
std::string toText() const
Convert the address to a string.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
bool isV6Multicast() const
checks whether and address is IPv6 and is multicast
bool isV4Bcast() const
Convenience function to check if it is an IPv4 broadcast address.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
bool operator!=(const AuthKey &other) const
Inequality operator.
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...
std::string toText() const
Return text format for keys.
AuthKey(const std::vector< uint8_t > &key)
Constructor.
void setAuthKey(const std::vector< uint8_t > &key)
Set auth key value from binary.
bool operator==(const AuthKey &other) const
Equality operator.
Represents option data configuration for the DHCP server.
Container for storing client class names.
void insert(const ClientClass &class_name)
Insert an element.
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
Holds DUID (DHCPv6 Unique Identifier).
static constexpr size_t MAX_DUID_LEN
maximum duid size
void addClientClass4(const std::string &class_name)
Adds new client class for DHCPv4.
void setServerHostname(const std::string &server_host_name)
Sets new value for server hostname (sname).
static size_t getIdentifierMaxLength(const IdentifierType &type)
Get maximum identifier length.
void encapsulateOptions() const
Encapsulates host-specific options with their suboptions.
const ClientClasses & getClientClasses6() const
Returns classes which DHCPv6 client is associated with.
CfgOptionPtr getCfgOption4()
Returns pointer to the DHCPv4 option data configuration for this host.
std::string toText() const
Returns information about the host in the textual format.
void addClientClass6(const std::string &class_name)
Adds new client class for DHCPv6.
IdentifierType getIdentifierType() const
Returns the identifier type.
void setIdentifier(const uint8_t *identifier, const size_t len, const IdentifierType &type)
Replaces currently used identifier with a new identifier.
const asiolink::IOAddress & getIPv4Reservation() const
Returns reserved IPv4 address.
void setIdentifierType(const IdentifierType &type)
Set the identifier type.
const std::string & getHostname() const
Returns reserved hostname.
IPv6ResrvRange getIPv6Reservations() const
Returns all IPv6 reservations.
const std::string & getBootFileName() const
Returns value of boot file name (file).
bool hasIPv6Reservation() const
Checks if there is at least one IPv6 reservation for this host.
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.
const std::vector< uint8_t > & getIdentifier() const
Returns the identifier in a binary form.
isc::data::ElementPtr toElement6() const
Unparses (converts to Element representation) IPv6 host.
void addReservation(const IPv6Resrv &reservation)
Adds new IPv6 reservation.
const ClientClasses & getClientClasses4() const
Returns classes which DHCPv4 client is associated with.
static std::string getIdentifierName(const IdentifierType &type)
Returns name of the identifier of a specified type.
const std::string & getServerHostname() const
Returns value of server hostname (sname).
void setBootFileName(const std::string &boot_file_name)
Sets new value for boot file name (file).
void setNextServer(const asiolink::IOAddress &next_server)
Sets new value for next server field (siaddr).
CfgOptionPtr getCfgOption6()
Returns pointer to the DHCPv6 option data configuration for this host.
bool hasReservation(const IPv6Resrv &reservation) const
Checks if specified IPv6 reservation exists for the host.
const asiolink::IOAddress & getNextServer() const
Returns value of next server field (siaddr).
std::string getIdentifierAsText() const
Returns host identifier in a textual form.
IdentifierType
Type of the host identifier.
@ IDENT_FLEX
Flexible host identifier.
isc::data::ElementPtr toElement4() const
Unparses (converts to Element representation) IPv4 host.
DuidPtr getDuid() const
Returns DUID for which the reservations are made.
void removeIPv4Reservation()
Removes the IPv4 reservation.
HWAddrPtr getHWAddress() const
Returns hardware address for which the reservations are made.
void setIPv4Reservation(const asiolink::IOAddress &address)
Sets new IPv4 reservation.
IPv6 reservation for a host.
void set(const Type &type, const asiolink::IOAddress &prefix, const uint8_t prefix_len)
Sets a new prefix and prefix length.
std::string PDExcludetoText() const
Returns information about the Prefix Exclude option of the reservation the textual format.
Type getType() const
Returns reservation type.
Type
Type of the reservation.
void setPDExclude(const asiolink::IOAddress &excluded_prefix, const uint8_t excluded_prefix_len)
Sets the Prefix Exclude option.
IPv6Resrv(const Type &type, const asiolink::IOAddress &prefix, const uint8_t prefix_len=128)
Constructor.
bool operator==(const IPv6Resrv &other) const
Equality operator.
std::string toText(bool display_pd_exclude_option=true) const
Returns information about the reservation in the textual format.
bool operator!=(const IPv6Resrv &other) const
Inequality operator.
DHCPv6 option class representing Prefix Exclude Option (RFC 6603).
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A wrapper interface for the ASIO library.
IOAddress firstAddrInPrefix(const IOAddress &prefix, uint8_t len)
This code is based on similar code from the Dibbler project.
std::vector< uint8_t > random(size_t len)
Generate random value.
boost::shared_ptr< Element > ElementPtr
std::string ClientClass
Defines a single class name.
boost::shared_ptr< DUID > DuidPtr
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
const uint8_t AUTH_KEY_LEN
Maximum length of authentication keys - 128 bits.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
std::pair< IPv6Resrv::Type, IPv6Resrv > IPv6ResrvTuple
@ HTYPE_ETHER
Ethernet 10Mbps.
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
vector< uint8_t > quotedStringToBinary(const string "ed_string)
Converts a string in quotes into vector.
string trim(const string &input)
Trim leading and trailing spaces.
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Hardware type that represents information from DHCPv4 packet.
static const size_t MAX_HWADDR_LEN
Maximum size of a hardware address.