Kea: entry.cc Source File

| Kea 3.2.0-git

entry.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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#include <config.h>
8
9#include <host_cache.h>
10#include <util/encode/encode.h>
11
12#include <boost/foreach.hpp>
13
14using namespace std;
15using namespace isc::asiolink;
16using namespace isc::data;
17using namespace isc::dhcp;
18
19namespace isc {
20namespace host_cache {
21
24 ElementPtr
25toElement(const ConstHostPtr& host) {
26 // Prepare the map
27 ElementPtr map = Element::createMap();
28 // Set the user context
29 host->contextToElement(map);
30 // Set the identifier
31 Host::IdentifierType id_type = host->getIdentifierType();
32 if (id_type == Host::IDENT_HWADDR) {
33 HWAddrPtr hwaddr = host->getHWAddress();
34 map->set("hw-address", Element::create(hwaddr->toText(false)));
35 } else if (id_type == Host::IDENT_DUID) {
36 DuidPtr duid = host->getDuid();
37 map->set("duid", Element::create(duid->toText()));
38 } else if (id_type == Host::IDENT_CIRCUIT_ID) {
39 const vector<uint8_t>& bin = host->getIdentifier();
40 string circuit_id = util::encode::encodeHex(bin);
41 map->set("circuit-id", Element::create(circuit_id));
42 } else if (id_type == Host::IDENT_CLIENT_ID) {
43 const vector<uint8_t>& bin = host->getIdentifier();
44 string client_id = util::encode::encodeHex(bin);
45 map->set("client-id", Element::create(client_id));
46 } else if (id_type == Host::IDENT_FLEX) {
47 const vector<uint8_t>& bin = host->getIdentifier();
48 string client_id = util::encode::encodeHex(bin);
49 map->set("flex-id", Element::create(client_id));
50 } else {
51 isc_throw(ToElementError, "invalid identifier type: " << id_type);
52 }
53
54 // Set the IPv4 reservation (if not 0.0.0.0 which may not be re-read)
55 const IOAddress& address = host->getIPv4Reservation();
56 if (!address.isV4Zero()) {
57 map->set("ip-address", Element::create(address.toText()));
58 }
59
60 // Set IPv6 reservations (ip-addresses)
61 const IPv6ResrvRange& na_resv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
62 ElementPtr resvs = Element::createList();
63 BOOST_FOREACH(auto const& resv, na_resv) {
64 resvs->add(Element::create(resv.second.toText()));
65 }
66 if (resvs->size() > 0) {
67 map->set("ip-addresses", resvs);
68 }
69 // Set reservations (prefixes)
70 const IPv6ResrvRange& pd_resv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
71 resvs = Element::createList();
72 BOOST_FOREACH(auto const& resv, pd_resv) {
73 resvs->add(Element::create(resv.second.toText()));
74 }
75 if (resvs->size() > 0) {
76 map->set("prefixes", resvs);
77 }
78
79 // Set the hostname
80 const string& hostname = host->getHostname();
81 map->set("hostname", Element::create(hostname));
82
83 // Set next-server
84 const IOAddress& next_server = host->getNextServer();
85 map->set("next-server", Element::create(next_server.toText()));
86 // Set server-hostname
87 const string& server_hostname = host->getServerHostname();
88 map->set("server-hostname", Element::create(server_hostname));
89 // Set boot-file-name
90 const string& boot_file_name = host->getBootFileName();
91 map->set("boot-file-name", Element::create(boot_file_name));
92
93 // Set IPv4 client-classes
94 const ClientClasses& cclasses4 = host->getClientClasses4();
95 ElementPtr classes = Element::createList();
96 for (auto const& cclass : cclasses4) {
97 classes->add(Element::create(cclass));
98 }
99 map->set("client-classes4", classes);
100 // Set IPv6 client-classes
101 const ClientClasses& cclasses6 = host->getClientClasses6();
102 classes = Element::createList();
103 for (auto const& cclass : cclasses6) {
104 classes->add(Element::create(cclass));
105 }
106 map->set("client-classes6", classes);
107
108 // Set IPv4 option-data
109 ConstCfgOptionPtr opts = host->getCfgOption4();
110 map->set("option-data4", opts->toElement());
111 // Set IPv6 option-data
112 opts = host->getCfgOption6();
113 map->set("option-data6", opts->toElement());
114
115 // Set IPv4 subnet-id
116 SubnetID subnet_id = host->getIPv4SubnetID();
117 map->set("subnet-id4", Element::create(static_cast<long long>(subnet_id)));
118 // Set IPv6 subnet-id
119 subnet_id = host->getIPv6SubnetID();
120 map->set("subnet-id6", Element::create(static_cast<long long>(subnet_id)));
121
122 // Set host-id
123 uint64_t hid = host->getHostId();
124 map->set("host-id", Element::create(static_cast<int64_t>(hid)));
125
126 // Set negative
127 if (host->getNegative()) {
128 map->set("negative", Element::create(true));
129 }
130
131 return (map);
132}
133
134} // end of namespace isc::host_cache
135} // end of namespace isc
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
isc::ToElementError
Cannot unparse error.
Definition cfg_to_element.h:19
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::isV4Zero
bool isV4Zero() const
Convenience function to check if it is an IPv4 zero address.
Definition io_address.h:129
isc::asiolink::IOAddress::toText
std::string toText() const
Convert the address to a string.
Definition io_address.cc:60
isc::dhcp::ClientClasses
Container for storing client class names.
Definition classify.h:110
isc::dhcp::Host::IdentifierType
IdentifierType
Type of the host identifier.
Definition host.h:337
isc::dhcp::Host::IDENT_HWADDR
@ IDENT_HWADDR
Definition host.h:338
isc::dhcp::Host::IDENT_FLEX
@ IDENT_FLEX
Flexible host identifier.
Definition host.h:342
isc::dhcp::Host::IDENT_DUID
@ IDENT_DUID
Definition host.h:339
isc::dhcp::Host::IDENT_CLIENT_ID
@ IDENT_CLIENT_ID
Definition host.h:341
isc::dhcp::Host::IDENT_CIRCUIT_ID
@ IDENT_CIRCUIT_ID
Definition host.h:340
isc::dhcp::IPv6Resrv::TYPE_PD
@ TYPE_PD
Definition host.h:171
isc::dhcp::IPv6Resrv::TYPE_NA
@ TYPE_NA
Definition host.h:170
encode.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
host_cache.h
isc::asiolink
Definition addr_utilities.cc:170
isc::data
Definition base_stamped_element.cc:12
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DuidPtr
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
isc::dhcp::IPv6ResrvRange
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition host.h:273
isc::dhcp::HWAddrPtr
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
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::dhcp::ConstCfgOptionPtr
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:976
isc::host_cache
Definition container.h:22
isc::host_cache::toElement
ElementPtr toElement(const ConstHostPtr &host)
Unparse a host cache entry.
Definition entry.cc:25
isc::util::encode::encodeHex
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18