| Kea 3.0.2-git

csv_lease_file6.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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 <dhcpsrv/dhcpsrv_log.h>
10#include <dhcpsrv/csv_lease_file6.h>
11
12#include <ctime>
13
14 using namespace isc::asiolink;
15 using namespace isc::data;
16 using namespace isc::util;
17
18 namespace isc {
19 namespace dhcp {
20
21 CSVLeaseFile6::CSVLeaseFile6(const std::string& filename)
22 : VersionedCSVFile(filename) {
23 initColumns();
24}
25
26 void
27 CSVLeaseFile6::open(const bool seek_to_end) {
28 // Call the base class to open the file
29 VersionedCSVFile::open(seek_to_end);
30
31 // and clear any statistics we may have
32 clearStatistics();
33}
34
35 void
36 CSVLeaseFile6::append(const Lease6& lease) {
37 // Bump the number of write attempts
38 ++writes_;
39
40 if (((!(lease.duid_)) || (*(lease.duid_) == DUID::EMPTY())) &&
41 (lease.state_ != Lease::STATE_DECLINED)) {
42 ++write_errs_;
43 isc_throw(BadValue, "Lease6: " << lease.addr_.toText() << ", state: "
44 << Lease::basicStatesToText(lease.state_) << ", has no DUID");
45 }
46
47 CSVRow row(getColumnCount());
48 row.writeAt(getColumnIndex("address"), lease.addr_.toText());
49 row.writeAt(getColumnIndex("duid"), lease.duid_->toText());
50 row.writeAt(getColumnIndex("valid_lifetime"), lease.valid_lft_);
51 row.writeAt(getColumnIndex("expire"), static_cast<uint64_t>(lease.cltt_) + lease.valid_lft_);
52 row.writeAt(getColumnIndex("subnet_id"), lease.subnet_id_);
53 row.writeAt(getColumnIndex("pref_lifetime"), lease.preferred_lft_);
54 row.writeAt(getColumnIndex("lease_type"), lease.type_);
55 row.writeAt(getColumnIndex("iaid"), lease.iaid_);
56 row.writeAt(getColumnIndex("prefix_len"),
57 static_cast<int>(lease.prefixlen_));
58 row.writeAt(getColumnIndex("fqdn_fwd"), lease.fqdn_fwd_);
59 row.writeAt(getColumnIndex("fqdn_rev"), lease.fqdn_rev_);
60 row.writeAtEscaped(getColumnIndex("hostname"), lease.hostname_);
61 // We may not have hardware information.
62 if (lease.hwaddr_) {
63 row.writeAt(getColumnIndex("hwaddr"), lease.hwaddr_->toText(false));
64 row.writeAt(getColumnIndex("hwtype"), lease.hwaddr_->htype_);
65 row.writeAt(getColumnIndex("hwaddr_source"), lease.hwaddr_->source_);
66 }
67 row.writeAt(getColumnIndex("state"), lease.state_);
68 // User context is optional.
69 if (lease.getContext()) {
70 row.writeAtEscaped(getColumnIndex("user_context"), lease.getContext()->str());
71 }
72 row.writeAt(getColumnIndex("pool_id"), lease.pool_id_);
73 try {
74 VersionedCSVFile::append(row);
75 } catch (const std::exception&) {
76 // Catch any errors so we can bump the error counter than rethrow it
77 ++write_errs_;
78 throw;
79 }
80
81 // Bump the number of leases written
82 ++write_leases_;
83}
84
85 bool
86 CSVLeaseFile6::next(Lease6Ptr& lease) {
87 // Bump the number of read attempts
88 ++reads_;
89
90 // Read the CSV row and try to create a lease from the values read.
91 // This may easily result in exception. We don't want this function
92 // to throw exceptions, so we catch them all and rather return the
93 // false value.
94 try {
95 // Get the row of CSV values.
96 CSVRow row;
97 VersionedCSVFile::next(row);
98 // The empty row signals EOF.
99 if (row == CSVFile::EMPTY_ROW()) {
100 lease.reset();
101 return (true);
102 }
103
104 Lease::Type type = readType(row);
105 uint8_t prefixlen = 128;
106 if (type == Lease::TYPE_PD) {
107 prefixlen = readPrefixLen(row);
108 }
109
110 lease.reset(new Lease6(type, readAddress(row), readDUID(row),
111 readIAID(row), readPreferred(row),
112 readValid(row),
113 readSubnetID(row),
114 readHWAddr(row),
115 prefixlen));
116
117 lease->cltt_ = readCltt(row);
118 lease->fqdn_fwd_ = readFqdnFwd(row);
119 lease->fqdn_rev_ = readFqdnRev(row);
120 lease->hostname_ = readHostname(row);
121 lease->state_ = readState(row);
122
123 if ((*lease->duid_ == DUID::EMPTY())
124 && lease->state_ != Lease::STATE_DECLINED) {
125 isc_throw(isc::BadValue,
126 "The Empty DUID is only valid for declined leases");
127 }
128
129 ConstElementPtr ctx = readContext(row);
130 if (ctx) {
131 lease->setContext(ctx);
132 }
133
134 lease->pool_id_ = readPoolID(row);
135 } catch (const std::exception& ex) {
136 // bump the read error count
137 ++read_errs_;
138
139 // The lease might have been created, so let's set it back to NULL to
140 // signal that lease hasn't been parsed.
141 lease.reset();
142 setReadMsg(ex.what());
143 return (false);
144 }
145
146 // bump the number of leases read
147 ++read_leases_;
148
149 return (true);
150}
151
152 void
153CSVLeaseFile6::initColumns() {
154 addColumn("address", "1.0");
155 addColumn("duid", "1.0");
156 addColumn("valid_lifetime", "1.0");
157 addColumn("expire", "1.0");
158 addColumn("subnet_id", "1.0");
159 addColumn("pref_lifetime", "1.0");
160 addColumn("lease_type", "1.0");
161 addColumn("iaid", "1.0");
162 addColumn("prefix_len", "1.0");
163 addColumn("fqdn_fwd", "1.0");
164 addColumn("fqdn_rev", "1.0");
165 addColumn("hostname", "1.0");
166 addColumn("hwaddr", "2.0");
167 addColumn("state", "3.0", "0" /* == STATE_DEFAULT */);
168 addColumn("user_context", "3.1");
169 // Default not added for hwtype and hwaddr_source, because they depend on
170 // hwaddr having value. When a CSV lease having a hwaddr is upgraded to 4.0,
171 // hwtype will have value "1" meaning HTYPE_ETHER and
172 // hwaddr_source will have value "0" meaning HWADDR_SOURCE_UNKNOWN.
173 addColumn("hwtype", "4.0");
174 addColumn("hwaddr_source", "4.0");
175 addColumn("pool_id", "5.0", "0");
176
177 // Any file with less than hostname is invalid
178 setMinimumValidColumns("hostname");
179}
180
181 Lease::Type
182CSVLeaseFile6::readType(const CSVRow& row) {
183 return (static_cast<Lease::Type>
184 (row.readAndConvertAt<int>(getColumnIndex("lease_type"))));
185}
186
187 IOAddress
188CSVLeaseFile6::readAddress(const CSVRow& row) {
189 IOAddress address(row.readAt(getColumnIndex("address")));
190 return (address);
191}
192
193 DuidPtr
194CSVLeaseFile6::readDUID(const util::CSVRow& row) {
195 DuidPtr duid(new DUID(DUID::fromText(row.readAt(getColumnIndex("duid")))));
196 return (duid);
197}
198
199uint32_t
200CSVLeaseFile6::readIAID(const CSVRow& row) {
201 uint32_t iaid = row.readAndConvertAt<uint32_t>(getColumnIndex("iaid"));
202 return (iaid);
203}
204
205uint32_t
206CSVLeaseFile6::readPreferred(const CSVRow& row) {
207 uint32_t pref =
208 row.readAndConvertAt<uint32_t>(getColumnIndex("pref_lifetime"));
209 return (pref);
210}
211
212uint32_t
213CSVLeaseFile6::readValid(const CSVRow& row) {
214 uint32_t valid =
215 row.readAndConvertAt<uint32_t>(getColumnIndex("valid_lifetime"));
216 return (valid);
217}
218
219uint32_t
220CSVLeaseFile6::readCltt(const CSVRow& row) {
221 uint32_t cltt =
222 static_cast<uint32_t>(row.readAndConvertAt<uint64_t>(getColumnIndex("expire"))
223 - readValid(row));
224 return (cltt);
225}
226
227 SubnetID
228CSVLeaseFile6::readSubnetID(const CSVRow& row) {
229 SubnetID subnet_id =
230 row.readAndConvertAt<SubnetID>(getColumnIndex("subnet_id"));
231 return (subnet_id);
232}
233
234uint32_t
235CSVLeaseFile6::readPoolID(const CSVRow& row) {
236 uint32_t pool_id =
237 row.readAndConvertAt<uint32_t>(getColumnIndex("pool_id"));
238 return (pool_id);
239}
240
241uint8_t
242CSVLeaseFile6::readPrefixLen(const CSVRow& row) {
243 int prefixlen = row.readAndConvertAt<int>(getColumnIndex("prefix_len"));
244 return (static_cast<uint8_t>(prefixlen));
245}
246
247 bool
248CSVLeaseFile6::readFqdnFwd(const CSVRow& row) {
249 bool fqdn_fwd = row.readAndConvertAt<bool>(getColumnIndex("fqdn_fwd"));
250 return (fqdn_fwd);
251}
252
253 bool
254CSVLeaseFile6::readFqdnRev(const CSVRow& row) {
255 bool fqdn_rev = row.readAndConvertAt<bool>(getColumnIndex("fqdn_rev"));
256 return (fqdn_rev);
257}
258
259std::string
260CSVLeaseFile6::readHostname(const CSVRow& row) {
261 std::string hostname = row.readAtEscaped(getColumnIndex("hostname"));
262 return (hostname);
263}
264
265 HWAddrPtr
266CSVLeaseFile6::readHWAddr(const CSVRow& row) {
267
268 try {
269 uint16_t const hwtype(readHWType(row).valueOr(HTYPE_ETHER));
270 HWAddr hwaddr(
271 HWAddr::fromText(row.readAt(getColumnIndex("hwaddr")), hwtype));
272 if (hwaddr.hwaddr_.empty()) {
273 return (HWAddrPtr());
274 }
275 hwaddr.source_ =
276 readHWAddrSource(row).valueOr(HWAddr::HWADDR_SOURCE_UNKNOWN);
277
280
281 // Let's return a pointer to new freshly created copy.
282 return (HWAddrPtr(new HWAddr(hwaddr)));
283
284 } catch (const std::exception& ex) {
285 // That's worse. There was something in the file, but its conversion
286 // to HWAddr failed. Let's log it on warning and carry on.
287 LOG_WARN(dhcpsrv_logger, DHCPSRV_MEMFILE_READ_HWADDR_FAIL)
288 .arg(ex.what());
289
290 return (HWAddrPtr());
291 }
292}
293
294uint32_t
295CSVLeaseFile6::readState(const util::CSVRow& row) {
296 uint32_t state = row.readAndConvertAt<uint32_t>(getColumnIndex("state"));
297 return (state);
298}
299
300 ConstElementPtr
301CSVLeaseFile6::readContext(const util::CSVRow& row) {
302 std::string user_context = row.readAtEscaped(getColumnIndex("user_context"));
303 if (user_context.empty()) {
304 return (ConstElementPtr());
305 }
306 ConstElementPtr ctx = Element::fromJSON(user_context);
307 if (!ctx || (ctx->getType() != Element::map)) {
308 isc_throw(isc::BadValue, "user context '" << user_context
309 << "' is not a JSON map");
310 }
311 return (ctx);
312}
313
314 Optional<uint16_t>
315CSVLeaseFile6::readHWType(const CSVRow& row) {
316 size_t const index(getColumnIndex("hwtype"));
317 if (row.readAt(index).empty()) {
318 return Optional<uint16_t>();
319 }
320 return row.readAndConvertAt<uint16_t>(index);
321}
322
323 Optional<uint32_t>
324CSVLeaseFile6::readHWAddrSource(const CSVRow& row) {
325 size_t const index(getColumnIndex("hwaddr_source"));
326 if (row.readAt(index).empty()) {
327 return Optional<uint16_t>();
328 }
329 return row.readAndConvertAt<uint32_t>(index);
330}
331
332} // end of namespace isc::dhcp
333} // end of namespace isc
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::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:45
isc::asiolink::IOAddress::toText
std::string toText() const
Convert the address to a string.
Definition io_address.cc:61
isc::data::Element::fromJSON
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition data.cc:798
isc::data::Element::map
@ map
Definition data.h:147
isc::dhcp::CSVLeaseFile6::next
bool next(Lease6Ptr &lease)
Reads next lease from the CSV file.
Definition csv_lease_file6.cc:86
isc::dhcp::CSVLeaseFile6::append
void append(const Lease6 &lease)
Appends the lease record to the CSV file.
Definition csv_lease_file6.cc:36
isc::dhcp::CSVLeaseFile6::open
virtual void open(const bool seek_to_end=false)
Opens a lease file.
Definition csv_lease_file6.cc:27
isc::dhcp::CSVLeaseFile6::CSVLeaseFile6
CSVLeaseFile6(const std::string &filename)
Constructor.
Definition csv_lease_file6.cc:21
isc::dhcp::DUID::fromText
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition duid.cc:50
isc::dhcp::DUID::EMPTY
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition duid.cc:55
isc::dhcp::LeaseFileStats::write_leases_
uint32_t write_leases_
Number of lease written.
Definition lease_file_stats.h:85
isc::dhcp::LeaseFileStats::read_leases_
uint32_t read_leases_
Number of leases read.
Definition lease_file_stats.h:76
isc::dhcp::LeaseFileStats::reads_
uint32_t reads_
Number of attempts to read a lease.
Definition lease_file_stats.h:73
isc::dhcp::LeaseFileStats::clearStatistics
void clearStatistics()
Clears the statistics.
Definition lease_file_stats.h:62
isc::dhcp::LeaseFileStats::writes_
uint32_t writes_
Number of attempts to write a lease.
Definition lease_file_stats.h:82
isc::dhcp::LeaseFileStats::write_errs_
uint32_t write_errs_
Number of errors when writing.
Definition lease_file_stats.h:88
isc::dhcp::LeaseFileStats::read_errs_
uint32_t read_errs_
Number of errors when reading.
Definition lease_file_stats.h:79
isc::util::CSVFile::getColumnCount
size_t getColumnCount() const
Returns the number of columns in the file.
Definition csv_file.h:403
isc::util::CSVFile::EMPTY_ROW
static CSVRow EMPTY_ROW()
Represents empty row.
Definition csv_file.h:491
isc::util::CSVFile::setReadMsg
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
Definition csv_file.h:486
isc::util::CSVFile::append
void append(const CSVRow &row) const
Writes the CSV row into the file.
Definition csv_file.cc:167
isc::util::CSVFile::getColumnIndex
size_t getColumnIndex(const std::string &col_name) const
Returns the index of the column having specified name.
Definition csv_file.cc:237
isc::util::CSVRow
Represents a single row of the CSV file.
Definition csv_file.h:51
isc::util::CSVRow::readAndConvertAt
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
Definition csv_file.h:156
isc::util::CSVRow::readAtEscaped
std::string readAtEscaped(const size_t at) const
Retrieves a value from the internal container, free of escaped characters.
Definition csv_file.cc:66
isc::util::CSVRow::writeAt
void writeAt(const size_t at, const char *value)
Replaces the value at specified index.
Definition csv_file.cc:84
isc::util::CSVRow::readAt
std::string readAt(const size_t at) const
Retrieves a value from the internal container.
Definition csv_file.cc:60
isc::util::CSVRow::writeAtEscaped
void writeAtEscaped(const size_t at, const std::string &value)
Replaces the value at the specified index with a value that has had special characters escaped.
Definition csv_file.cc:90
isc::util::Optional
A template representing an optional value.
Definition optional.h:36
isc::util::VersionedCSVFile::open
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
Definition versioned_csv_file.cc:61
isc::util::VersionedCSVFile::VersionedCSVFile
VersionedCSVFile(const std::string &filename)
Constructor.
Definition versioned_csv_file.cc:14
isc::util::VersionedCSVFile::setMinimumValidColumns
void setMinimumValidColumns(const std::string &column_name)
Sets the minimum number of valid columns based on a given column.
Definition versioned_csv_file.cc:33
isc::util::VersionedCSVFile::next
bool next(CSVRow &row)
Reads next row from the file file.
Definition versioned_csv_file.cc:124
isc::util::VersionedCSVFile::addColumn
void addColumn(const std::string &col_name, const std::string &version, const std::string &default_value="")
Adds metadata for a single column to the schema.
Definition versioned_csv_file.cc:24
csv_lease_file6.h
dhcpsrv_log.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::dhcp::HWAddr::HWADDR_SOURCE_UNKNOWN
static const uint32_t HWADDR_SOURCE_UNKNOWN
Used when actual origin is not known, e.g.
Definition hwaddr.h:41
LOG_WARN
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::asiolink
Definition addr_utilities.cc:170
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
isc::dhcp::DHCPSRV_MEMFILE_READ_HWADDR_FAIL
const isc::log::MessageID DHCPSRV_MEMFILE_READ_HWADDR_FAIL
Definition dhcpsrv_messages.h:142
isc::dhcp::DuidPtr
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
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::HTYPE_ETHER
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition dhcp4.h:56
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::data::UserContext::getContext
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition user_context.h:24
isc::dhcp::HWAddr::fromText
static HWAddr fromText(const std::string &text, const uint16_t htype=HTYPE_ETHER)
Creates instance of the hardware address from textual format.
Definition hwaddr.cc:69
isc::dhcp::Lease6
Structure that holds a lease for IPv6 address and/or prefix.
Definition lease.h:536
isc::dhcp::Lease6::type_
Lease::Type type_
Lease type.
Definition lease.h:541
isc::dhcp::Lease6::iaid_
uint32_t iaid_
Identity Association Identifier (IAID).
Definition lease.h:553
isc::dhcp::Lease6::preferred_lft_
uint32_t preferred_lft_
Preferred lifetime.
Definition lease.h:562
isc::dhcp::Lease6::duid_
DuidPtr duid_
Client identifier.
Definition lease.h:556
isc::dhcp::Lease6::prefixlen_
uint8_t prefixlen_
IPv6 prefix length.
Definition lease.h:546
isc::dhcp::Lease::subnet_id_
SubnetID subnet_id_
Subnet identifier.
Definition lease.h:160
isc::dhcp::Lease::pool_id_
uint32_t pool_id_
The pool id.
Definition lease.h:165
isc::dhcp::Lease::valid_lft_
uint32_t valid_lft_
Valid lifetime.
Definition lease.h:131
isc::dhcp::Lease::basicStatesToText
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition lease.cc:94
isc::dhcp::Lease::STATE_DECLINED
static const uint32_t STATE_DECLINED
Declined lease.
Definition lease.h:72
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::Lease::TYPE_PD
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:49
isc::dhcp::Lease::hostname_
std::string hostname_
Client hostname.
Definition lease.h:170
isc::dhcp::Lease::state_
uint32_t state_
Holds the lease state(s).
Definition lease.h:196
isc::dhcp::Lease::fqdn_fwd_
bool fqdn_fwd_
Forward zone updated?
Definition lease.h:175
isc::dhcp::Lease::cltt_
time_t cltt_
Client last transmission time.
Definition lease.h:149
isc::dhcp::Lease::hwaddr_
HWAddrPtr hwaddr_
Client's MAC/hardware address.
Definition lease.h:185
isc::dhcp::Lease::fqdn_rev_
bool fqdn_rev_
Reverse zone updated?
Definition lease.h:180
isc::dhcp::Lease::addr_
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition lease.h:126