Kea 2.5.9
csv_lease_file6.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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
11
12#include <ctime>
13
14using namespace isc::asiolink;
15using namespace isc::data;
16using namespace isc::util;
17
18namespace isc {
19namespace dhcp {
20
21CSVLeaseFile6::CSVLeaseFile6(const std::string& filename)
22 : VersionedCSVFile(filename) {
23 initColumns();
24}
25
26void
27CSVLeaseFile6::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
33}
34
35void
37 // Bump the number of write attempts
38 ++writes_;
39
40 if (((!(lease.duid_)) || (*(lease.duid_) == DUID::EMPTY())) &&
41 (lease.state_ != Lease::STATE_DECLINED)) {
43 isc_throw(BadValue, "Lease6: " << lease.addr_.toText() << ", state: "
44 << Lease::basicStatesToText(lease.state_) << ", has no DUID");
45 }
46
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
78 throw;
79 }
80
81 // Bump the number of leases written
83}
84
85bool
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;
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) {
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
152void
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
182CSVLeaseFile6::readType(const CSVRow& row) {
183 return (static_cast<Lease::Type>
184 (row.readAndConvertAt<int>(getColumnIndex("lease_type"))));
185}
186
188CSVLeaseFile6::readAddress(const CSVRow& row) {
189 IOAddress address(row.readAt(getColumnIndex("address")));
190 return (address);
191}
192
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 time_t cltt =
222 static_cast<time_t>(row.readAndConvertAt<uint64_t>(getColumnIndex("expire"))
223 - readValid(row));
224 return (static_cast<uint32_t>(cltt));
225}
226
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
247bool
248CSVLeaseFile6::readFqdnFwd(const CSVRow& row) {
249 bool fqdn_fwd = row.readAndConvertAt<bool>(getColumnIndex("fqdn_fwd"));
250 return (fqdn_fwd);
251}
252
253bool
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
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.
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
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
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
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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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
bool next(Lease6Ptr &lease)
Reads next lease from the CSV file.
void append(const Lease6 &lease)
Appends the lease record to the CSV file.
virtual void open(const bool seek_to_end=false)
Opens a lease file.
CSVLeaseFile6(const std::string &filename)
Constructor.
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition: duid.cc:50
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition: duid.cc:55
uint32_t write_leases_
Number of lease written.
uint32_t read_leases_
Number of leases read.
uint32_t reads_
Number of attempts to read a lease.
void clearStatistics()
Clears the statistics.
uint32_t writes_
Number of attempts to write a lease.
uint32_t write_errs_
Number of errors when writing.
uint32_t read_errs_
Number of errors when reading.
size_t getColumnCount() const
Returns the number of columns in the file.
Definition: csv_file.h:403
static CSVRow EMPTY_ROW()
Represents empty row.
Definition: csv_file.h:491
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
Definition: csv_file.h:486
size_t getColumnIndex(const std::string &col_name) const
Returns the index of the column having specified name.
Definition: csv_file.cc:237
Represents a single row of the CSV file.
Definition: csv_file.h:51
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.h:156
std::string readAtEscaped(const size_t at) const
Retrieves a value from the internal container, free of escaped characters.
Definition: csv_file.cc:66
void writeAt(const size_t at, const char *value)
Replaces the value at specified index.
Definition: csv_file.cc:84
std::string readAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.cc:60
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
A template representing an optional value.
Definition: optional.h:36
T valueOr(T const &or_value) const
Retrieves the encapsulated value if specified, or the given value otherwise.
Definition: optional.h:124
Implements a CSV file that supports multiple versions of the file's "schema".
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
void setMinimumValidColumns(const std::string &column_name)
Sets the minimum number of valid columns based on a given column.
bool next(CSVRow &row)
Reads next row from the file file.
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.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static const uint32_t HWADDR_SOURCE_UNKNOWN
Used when actual origin is not known, e.g.
Definition: hwaddr.h:41
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_MEMFILE_READ_HWADDR_FAIL
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:136
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:505
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
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition: dhcp4.h:56
Defines the logger used by the top-level component of kea-lfc.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24
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
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:513
Lease::Type type_
Lease type.
Definition: lease.h:518
uint32_t iaid_
Identity Association Identifier (IAID)
Definition: lease.h:530
uint32_t preferred_lft_
Preferred lifetime.
Definition: lease.h:539
DuidPtr duid_
Client identifier.
Definition: lease.h:533
uint8_t prefixlen_
IPv6 prefix length.
Definition: lease.h:523
SubnetID subnet_id_
Subnet identifier.
Definition: lease.h:154
uint32_t pool_id_
The pool id.
Definition: lease.h:159
uint32_t valid_lft_
Valid lifetime.
Definition: lease.h:125
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition: lease.cc:92
static const uint32_t STATE_DECLINED
Declined lease.
Definition: lease.h:72
Type
Type of lease or pool.
Definition: lease.h:46
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition: lease.h:49
std::string hostname_
Client hostname.
Definition: lease.h:164
uint32_t state_
Holds the lease state(s).
Definition: lease.h:190
bool fqdn_fwd_
Forward zone updated?
Definition: lease.h:169
time_t cltt_
Client last transmission time.
Definition: lease.h:143
HWAddrPtr hwaddr_
Client's MAC/hardware address.
Definition: lease.h:179
bool fqdn_rev_
Reverse zone updated?
Definition: lease.h:174
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition: lease.h:120