Kea 3.1.1
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
25toElement(const ConstHostPtr& host) {
26 // Prepare the map
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);
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();
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
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Cannot unparse error.
Container for storing client class names.
Definition classify.h:110
IdentifierType
Type of the host identifier.
Definition host.h:337
@ IDENT_FLEX
Flexible host identifier.
Definition host.h:342
@ IDENT_CLIENT_ID
Definition host.h:341
@ IDENT_CIRCUIT_ID
Definition host.h:340
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition host.h:273
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
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:840
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:976
ElementPtr toElement(const ConstHostPtr &host)
Unparse a host cache entry.
Definition entry.cc:25
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
Defines the logger used by the top-level component of kea-lfc.