Kea 2.7.1
lease.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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#include <dhcpsrv/lease.h>
12#include <util/pointer_util.h>
13#include <util/str.h>
14#include <boost/algorithm/string.hpp>
15#include <boost/scoped_ptr.hpp>
16#include <sstream>
17#include <iostream>
18
19using namespace isc::asiolink;
20using namespace isc::util;
21using namespace isc::data;
22using namespace std;
23
24namespace isc {
25namespace dhcp {
26
27const uint32_t Lease::STATE_DEFAULT = 0;
28const uint32_t Lease::STATE_DECLINED = 1;
29const uint32_t Lease::STATE_EXPIRED_RECLAIMED = 2;
30const uint32_t Lease::STATE_RELEASED = 3;
31
32std::string
33Lease::lifetimeToText(uint32_t lifetime) {
34 ostringstream repr;
35 if (lifetime == INFINITY_LFT) {
36 repr << "infinity";
37 } else {
38 repr << lifetime;
39 }
40 return repr.str();
41}
42
44 uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
45 const bool fqdn_fwd, const bool fqdn_rev,
46 const std::string& hostname, const HWAddrPtr& hwaddr)
47 : addr_(addr), valid_lft_(valid_lft), current_valid_lft_(valid_lft),
48 reuseable_valid_lft_(0),
49 cltt_(cltt), current_cltt_(cltt), subnet_id_(subnet_id), pool_id_(0),
50 hostname_(boost::algorithm::to_lower_copy(hostname)), fqdn_fwd_(fqdn_fwd),
51 fqdn_rev_(fqdn_rev), hwaddr_(hwaddr), state_(STATE_DEFAULT) {
52}
53
54std::string
56 switch (type) {
57 case Lease::TYPE_V4:
58 return string("V4");
59 case Lease::TYPE_NA:
60 return string("IA_NA");
61 case Lease::TYPE_TA:
62 return string("IA_TA");
63 case Lease::TYPE_PD:
64 return string("IA_PD");
65 break;
66 default: {
67 stringstream tmp;
68 tmp << "unknown (" << type << ")";
69 return (tmp.str());
70 }
71 }
72}
73
75Lease::textToType(const std::string& text) {
76 if (text == "V4") {
77 return (TYPE_V4);
78
79 } else if (text == "IA_NA") {
80 return (TYPE_NA);
81
82 } else if (text == "IA_TA") {
83 return (TYPE_TA);
84
85 } else if (text == "IA_PD") {
86 return (TYPE_PD);
87 }
88
89 isc_throw(BadValue, "unsupported lease type " << text);
90}
91
92std::string
93Lease::basicStatesToText(const uint32_t state) {
94 switch (state) {
95 case STATE_DEFAULT:
96 return ("default");
97 case STATE_DECLINED:
98 return ("declined");
100 return ("expired-reclaimed");
101 case STATE_RELEASED:
102 return ("released");
103 default:
104 // The default case will be handled further on
105 ;
106 }
107 std::ostringstream s;
108 s << "unknown (" << state << ")";
109 return s.str();
110}
111
112bool
114 return ((valid_lft_ != INFINITY_LFT) && (getExpirationTime() < time(NULL)));
115}
116
117bool
121
122bool
124 return (state_ == STATE_DECLINED);
125}
126
127int64_t
129 return (static_cast<int64_t>(cltt_) + valid_lft_);
130}
131
132bool
133Lease::hasIdenticalFqdn(const Lease& other) const {
134 return (boost::algorithm::iequals(hostname_, other.hostname_) &&
135 fqdn_fwd_ == other.fqdn_fwd_ &&
136 fqdn_rev_ == other.fqdn_rev_);
137}
138
139void
141 if (!element) {
142 isc_throw(BadValue, "parsed lease data is null");
143 }
144
145 if (element->getType() != Element::map) {
146 isc_throw(BadValue, "parsed lease data is not a JSON map");
147 }
148
149 if (!lease) {
150 isc_throw(Unexpected, "pointer to parsed lease is null");
151 }
152
153 // IP address.
154 ConstElementPtr ip_address = element->get("ip-address");
155 if (!ip_address || (ip_address->getType() != Element::string)) {
156 isc_throw(BadValue, "ip-address not present in the parsed lease"
157 " or it is not a string");
158 }
159
160 boost::scoped_ptr<asiolink::IOAddress> io_address;
161 try {
162 io_address.reset(new asiolink::IOAddress(ip_address->stringValue()));
163
164 } catch (const std::exception& ex) {
165 isc_throw(BadValue, "invalid IP address " << ip_address->stringValue()
166 << " in the parsed lease");
167 }
168
169 lease->addr_ = *io_address;
170
171 // Subnet identifier.
172 ConstElementPtr subnet_id = element->get("subnet-id");
173 if (!subnet_id || (subnet_id->getType() != Element::integer)) {
174 isc_throw(BadValue, "subnet-id not present in the parsed lease"
175 " or it is not an integer");
176 }
177
178 if (subnet_id->intValue() <= 0) {
179 isc_throw(BadValue, "subnet-id " << subnet_id->intValue() << " is not"
180 << " a positive integer");
181 } else if (subnet_id->intValue() > numeric_limits<uint32_t>::max()) {
182 isc_throw(BadValue, "subnet-id " << subnet_id->intValue() << " is not"
183 << " a 32 bit unsigned integer");
184 }
185
186 lease->subnet_id_ = SubnetID(subnet_id->intValue());
187
188 // Pool identifier.
189 ConstElementPtr pool_id = element->get("pool-id");
190 if (pool_id) {
191 if (pool_id->getType() != Element::integer) {
192 isc_throw(BadValue, "pool-id is not an integer");
193 }
194
195 if (pool_id->intValue() <= 0) {
196 isc_throw(BadValue, "pool-id " << pool_id->intValue() << " is not"
197 << " a positive integer");
198 } else if (pool_id->intValue() > numeric_limits<uint32_t>::max()) {
199 isc_throw(BadValue, "pool-id " << pool_id->intValue() << " is not"
200 << " a 32 bit unsigned integer");
201 }
202
203 lease->pool_id_ = pool_id->intValue();
204 }
205
206 // Hardware address.
207 ConstElementPtr hw_address = element->get("hw-address");
208 if (hw_address) {
209 if (hw_address->getType() != Element::string) {
210 isc_throw(BadValue, "hw-address is not a string in the parsed lease");
211
212 }
213
214 try {
215 HWAddr parsed_hw_address = HWAddr::fromText(hw_address->stringValue());
216 lease->hwaddr_.reset(new HWAddr(parsed_hw_address.hwaddr_, HTYPE_ETHER));
217
218 } catch (const std::exception& ex) {
219 isc_throw(BadValue, "invalid hardware address "
220 << hw_address->stringValue() << " in the parsed lease");
221 }
222 }
223
224 // cltt
225 ConstElementPtr cltt = element->get("cltt");
226 if (!cltt || (cltt->getType() != Element::integer)) {
227 isc_throw(BadValue, "cltt is not present in the parsed lease"
228 " or it is not an integer");
229 }
230
231 if (cltt->intValue() <= 0) {
232 isc_throw(BadValue, "cltt " << cltt->intValue() << " is not a"
233 " positive integer in the parsed lease");
234 }
235
236 lease->cltt_ = static_cast<time_t>(cltt->intValue());
237
238 // valid lifetime
239 ConstElementPtr valid_lifetime = element->get("valid-lft");
240 if (!valid_lifetime || (valid_lifetime->getType() != Element::integer)) {
241 isc_throw(BadValue, "valid-lft is not present in the parsed lease"
242 " or it is not an integer");
243 }
244
245 if (valid_lifetime->intValue() < 0) {
246 isc_throw(BadValue, "valid-lft " << valid_lifetime->intValue()
247 << " is negative in the parsed lease");
248 }
249
250 lease->valid_lft_ = valid_lifetime->intValue();
251
252 // fqdn-fwd
253 ConstElementPtr fqdn_fwd = element->get("fqdn-fwd");
254 if (!fqdn_fwd || fqdn_fwd->getType() != Element::boolean) {
255 isc_throw(BadValue, "fqdn-fwd is not present in the parsed lease"
256 " or it is not a boolean value");
257 }
258
259 lease->fqdn_fwd_ = fqdn_fwd->boolValue();
260
261 // fqdn-fwd
262 ConstElementPtr fqdn_rev = element->get("fqdn-rev");
263 if (!fqdn_rev || (fqdn_rev->getType() != Element::boolean)) {
264 isc_throw(BadValue, "fqdn-rev is not present in the parsed lease"
265 " or it is not a boolean value");
266 }
267
268 lease->fqdn_rev_ = fqdn_rev->boolValue();
269
270 // hostname
271 ConstElementPtr hostname = element->get("hostname");
272 if (!hostname || (hostname->getType() != Element::string)) {
273 isc_throw(BadValue, "hostname is not present in the parsed lease"
274 " or it is not a string value");
275 }
276
277 lease->hostname_ = hostname->stringValue();
278 boost::algorithm::to_lower(lease->hostname_);
279
280 // state
281 ConstElementPtr state = element->get("state");
282 if (!state || (state->getType() != Element::integer)) {
283 isc_throw(BadValue, "state is not present in the parsed lease"
284 " or it is not an integer");
285 }
286
287 if ((state->intValue() < 0) || (state->intValue() > Lease::STATE_EXPIRED_RECLAIMED)) {
288 isc_throw(BadValue, "state " << state->intValue()
289 << " must be in range [0.."
291 }
292
293 lease->state_ = state->intValue();
294
295 // user context
296 ConstElementPtr ctx = element->get("user-context");
297 if (ctx) {
298 if (ctx->getType() != Element::map) {
299 isc_throw(BadValue, "user context is not a map");
300 }
301 lease->setContext(ctx);
302 }
303
304 lease->updateCurrentExpirationTime();
305}
306
307void
311
312void
314 to.current_cltt_ = from.cltt_;
315 to.current_valid_lft_ = from.valid_lft_;
316}
317
319 const HWAddrPtr& hw_address,
320 const ClientIdPtr& client_id,
321 const uint32_t valid_lifetime,
322 const time_t cltt,
323 const SubnetID subnet_id,
324 const bool fqdn_fwd,
325 const bool fqdn_rev,
326 const std::string& hostname)
327 : Lease(address, valid_lifetime, subnet_id, cltt, fqdn_fwd,
328 fqdn_rev, hostname, hw_address),
329 client_id_(client_id), remote_id_(), relay_id_() {
330}
331
332Lease4::Lease4() : Lease(0, 0, 0, 0, false, false, "", HWAddrPtr()) {
333}
334
335std::string
336Lease4::statesToText(const uint32_t state) {
337 return (Lease::basicStatesToText(state));
338}
339
340const std::vector<uint8_t>&
342 if (!client_id_) {
343 static std::vector<uint8_t> empty_vec;
344 return (empty_vec);
345 }
346
347 return (client_id_->getClientId());
348}
349
350const std::vector<uint8_t>&
352 if (!hwaddr_) {
353 static std::vector<uint8_t> empty_vec;
354 return (empty_vec);
355 }
356 return (hwaddr_->hwaddr_);
357}
358
359bool
361 const ClientIdPtr& client_id) const {
362 // If client id matches, lease matches.
363 if (equalValues(client_id, client_id_)) {
364 return (true);
365
366 } else if (!client_id || !client_id_) {
367 // If client id is unspecified, use HW address.
368 if (equalValues(hw_address, hwaddr_)) {
369 return (true);
370 }
371 }
372
373 return (false);
374}
375
376void
377Lease4::decline(uint32_t probation_period) {
378 hwaddr_.reset(new HWAddr());
379 client_id_.reset();
380 cltt_ = time(NULL);
381 hostname_ = string("");
382 fqdn_fwd_ = false;
383 fqdn_rev_ = false;
385 valid_lft_ = probation_period;
386}
387
390 // Prepare the map
392 contextToElement(map);
393 map->set("ip-address", Element::create(addr_.toText()));
394 map->set("subnet-id", Element::create(static_cast<long int>(subnet_id_)));
395 if (pool_id_) {
396 map->set("pool-id", Element::create(static_cast<long int>(pool_id_)));
397 }
398 map->set("hw-address", Element::create(hwaddr_->toText(false)));
399
400 if (client_id_) {
401 map->set("client-id", Element::create(client_id_->toText()));
402 }
403
404 map->set("cltt", Element::create(cltt_));
405 map->set("valid-lft", Element::create(static_cast<long int>(valid_lft_)));
406
407 map->set("fqdn-fwd", Element::create(fqdn_fwd_));
408 map->set("fqdn-rev", Element::create(fqdn_rev_));
409 map->set("hostname", Element::create(hostname_));
410
411 map->set("state", Element::create(static_cast<int>(state_)));
412
413 return (map);
414}
415
418 Lease4Ptr lease(new Lease4());
419
420 // Extract common lease properties into the lease.
421 fromElementCommon(boost::dynamic_pointer_cast<Lease>(lease), element);
422
423 // Validate ip-address, which must be an IPv4 address.
424 if (!lease->addr_.isV4()) {
425 isc_throw(BadValue, "address " << lease->addr_ << " it not an IPv4 address");
426 }
427
428 // Make sure the hw-addres is present.
429 if (!lease->hwaddr_) {
430 isc_throw(BadValue, "hw-address not present in the parsed lease");
431 }
432
433 // Client identifier is IPv4 specific.
434 ConstElementPtr client_id = element->get("client-id");
435 if (client_id) {
436 if (client_id->getType() != Element::string) {
437 isc_throw(BadValue, "client identifier is not a string in the"
438 " parsed lease");
439 }
440
441 try {
442 lease->client_id_ = ClientId::fromText(client_id->stringValue());
443
444 } catch (const std::exception& ex) {
445 isc_throw(BadValue, "invalid client identifier "
446 << client_id->stringValue() << " in the parsed lease");
447 }
448 }
449
450 return (lease);
451}
452
454 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
455 SubnetID subnet_id, const HWAddrPtr& hwaddr, uint8_t prefixlen)
456 : Lease(addr, valid, subnet_id, 0/*cltt*/, false, false, "", hwaddr),
457 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
458 preferred_lft_(preferred), reuseable_preferred_lft_(0),
459 extended_info_action_(ExtendedInfoAction::ACTION_IGNORE) {
460 if (!duid) {
461 isc_throw(BadValue, "DUID is mandatory for an IPv6 lease");
462 }
463
464 if (prefixlen != 128) {
465 if (type != Lease::TYPE_PD) {
466 isc_throw(BadValue, "prefixlen must be 128 for non prefix type");
467 }
468 }
469
470 cltt_ = time(NULL);
472}
473
475 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
476 SubnetID subnet_id, const bool fqdn_fwd, const bool fqdn_rev,
477 const std::string& hostname, const HWAddrPtr& hwaddr,
478 uint8_t prefixlen)
479 : Lease(addr, valid, subnet_id, 0/*cltt*/,
480 fqdn_fwd, fqdn_rev, hostname, hwaddr),
481 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
482 preferred_lft_(preferred), reuseable_preferred_lft_(0),
483 extended_info_action_(ExtendedInfoAction::ACTION_IGNORE) {
484
485 if (!duid) {
486 isc_throw(BadValue, "DUID is mandatory for an IPv6 lease");
487 }
488
489 if (prefixlen != 128) {
490 if (type != Lease::TYPE_PD) {
491 isc_throw(BadValue, "prefixlen must be 128 for non prefix type");
492 }
493 }
494
495 cltt_ = time(NULL);
497}
498
500 : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, false, false, "",
501 HWAddrPtr()), type_(TYPE_NA), prefixlen_(0), iaid_(0),
502 duid_(DuidPtr()), preferred_lft_(0), reuseable_preferred_lft_(0),
503 extended_info_action_(ExtendedInfoAction::ACTION_IGNORE) {
504}
505
506std::string
507Lease6::statesToText(const uint32_t state) {
508 return (Lease::basicStatesToText(state));
509}
510
511const std::vector<uint8_t>&
513 if (!duid_) {
514 static std::vector<uint8_t> empty_vec;
515 return (empty_vec);
516 }
517
518 return (duid_->getDuid());
519}
520
521void
522Lease6::decline(uint32_t probation_period) {
523 hwaddr_.reset();
524 duid_.reset(new DUID(DUID::EMPTY()));
525 preferred_lft_ = 0;
526 valid_lft_ = probation_period;
527 cltt_ = time(NULL);
528 hostname_ = string("");
529 fqdn_fwd_ = false;
530 fqdn_rev_ = false;
532}
533
534std::string
536 ostringstream stream;
537
539 stream << "Type: " << typeToText(type_) << "("
540 << static_cast<int>(type_) << ")\n"
541 << "Address: " << addr_ << "\n"
542 << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
543 << "IAID: " << iaid_ << "\n"
544 << "Pref life: " << lifetimeToText(preferred_lft_) << "\n"
545 << "Valid life: " << lifetimeToText(valid_lft_) << "\n"
546 << "Cltt: " << cltt_ << "\n"
547 << "DUID: " << (duid_?duid_->toText():"(none)") << "\n"
548 << "Hardware addr: " << (hwaddr_?hwaddr_->toText(false):"(none)") << "\n"
549 << "Subnet ID: " << subnet_id_ << "\n"
550 << "Pool ID: " << pool_id_ << "\n"
551 << "State: " << statesToText(state_) << "\n";
552
553 if (getContext()) {
554 stream << "User context: " << getContext()->str() << "\n";
555 }
556
557 return (stream.str());
558}
559
560std::string
562 ostringstream stream;
563
564 stream << "Address: " << addr_ << "\n"
565 << "Valid life: " << lifetimeToText(valid_lft_) << "\n"
566 << "Cltt: " << cltt_ << "\n"
567 << "Hardware addr: " << (hwaddr_ ? hwaddr_->toText(false) : "(none)") << "\n"
568 << "Client id: " << (client_id_ ? client_id_->toText() : "(none)") << "\n"
569 << "Subnet ID: " << subnet_id_ << "\n"
570 << "Pool ID: " << pool_id_ << "\n"
571 << "State: " << statesToText(state_) << "\n"
572 << "Relay ID: " << (relay_id_.empty() ? "(none)" :
573 str::dumpAsHex(&relay_id_[0], relay_id_.size())) << "\n"
574 << "Remote ID: " << (remote_id_.empty() ? "(none)" :
575 str::dumpAsHex(&remote_id_[0], remote_id_.size())) << "\n";
576
577 if (getContext()) {
578 stream << "User context: " << getContext()->str() << "\n";
579 }
580
581 return (stream.str());
582}
583
584bool
585Lease4::operator==(const Lease4& other) const {
586 return (nullOrEqualValues(hwaddr_, other.hwaddr_) &&
587 nullOrEqualValues(client_id_, other.client_id_) &&
588 addr_ == other.addr_ &&
589 subnet_id_ == other.subnet_id_ &&
590 pool_id_ == other.pool_id_ &&
591 valid_lft_ == other.valid_lft_ &&
592 current_valid_lft_ == other.current_valid_lft_ &&
593 reuseable_valid_lft_ == other.reuseable_valid_lft_ &&
594 cltt_ == other.cltt_ &&
595 current_cltt_ == other.current_cltt_ &&
596 hostname_ == other.hostname_ &&
597 fqdn_fwd_ == other.fqdn_fwd_ &&
598 fqdn_rev_ == other.fqdn_rev_ &&
599 state_ == other.state_ &&
600 nullOrEqualValues(getContext(), other.getContext()));
601}
602
603bool
604Lease6::operator==(const Lease6& other) const {
605 return (nullOrEqualValues(duid_, other.duid_) &&
606 nullOrEqualValues(hwaddr_, other.hwaddr_) &&
607 addr_ == other.addr_ &&
608 type_ == other.type_ &&
609 prefixlen_ == other.prefixlen_ &&
610 iaid_ == other.iaid_ &&
611 preferred_lft_ == other.preferred_lft_ &&
612 reuseable_preferred_lft_ == other.reuseable_preferred_lft_ &&
613 valid_lft_ == other.valid_lft_ &&
614 current_valid_lft_ == other.current_valid_lft_ &&
615 reuseable_valid_lft_ == other.reuseable_valid_lft_ &&
616 cltt_ == other.cltt_ &&
617 current_cltt_ == other.current_cltt_ &&
618 subnet_id_ == other.subnet_id_ &&
619 pool_id_ == other.pool_id_ &&
620 hostname_ == other.hostname_ &&
621 fqdn_fwd_ == other.fqdn_fwd_ &&
622 fqdn_rev_ == other.fqdn_rev_ &&
623 state_ == other.state_ &&
624 nullOrEqualValues(getContext(), other.getContext()));
625}
626
629 // Prepare the map
631 contextToElement(map);
632 map->set("ip-address", Element::create(addr_.toText()));
633 map->set("type", Element::create(typeToText(type_)));
634 if (type_ == Lease::TYPE_PD) {
635 map->set("prefix-len", Element::create(prefixlen_));
636 }
637 map->set("iaid", Element::create(static_cast<long int>(iaid_)));
638 map->set("duid", Element::create(duid_->toText()));
639 map->set("subnet-id", Element::create(static_cast<long int>(subnet_id_)));
640 if (pool_id_) {
641 map->set("pool-id", Element::create(static_cast<long int>(pool_id_)));
642 }
643
644 map->set("cltt", Element::create(cltt_));
645 map->set("preferred-lft", Element::create(static_cast<long int>(preferred_lft_)));
646 map->set("valid-lft", Element::create(static_cast<long int>(valid_lft_)));
647
648 map->set("fqdn-fwd", Element::create(fqdn_fwd_));
649 map->set("fqdn-rev", Element::create(fqdn_rev_));
650 map->set("hostname", Element::create(hostname_));
651
652 if (hwaddr_) {
653 map->set("hw-address", Element::create(hwaddr_->toText(false)));
654 }
655
656 map->set("state", Element::create(static_cast<long int>(state_)));
657
658 return (map);
659}
660
663 Lease6Ptr lease(new Lease6());
664
665 // Extract common lease properties into the lease.
666 fromElementCommon(boost::dynamic_pointer_cast<Lease>(lease), element);
667
668 // Validate ip-address, which must be an IPv6 address.
669 if (!lease->addr_.isV6()) {
670 isc_throw(BadValue, "address " << lease->addr_ << " it not an IPv6 address");
671 }
672
673 // lease type
674 ConstElementPtr lease_type = element->get("type");
675 if (!lease_type || (lease_type->getType() != Element::string)) {
676 isc_throw(BadValue, "type is not present in the parsed lease"
677 " or it is not a string value");
678 }
679
680 lease->type_ = textToType(lease_type->stringValue());
681
682 // prefix length
683 if (lease->type_ != Lease::TYPE_PD) {
684 lease->prefixlen_ = 128;
685 } else {
686 ConstElementPtr prefix_len = element->get("prefix-len");
687 if (!prefix_len || (prefix_len->getType() != Element::integer)) {
688 isc_throw(BadValue, "prefix-len is not present in the parsed lease"
689 " or it is not an integer");
690 }
691
692 if ((prefix_len->intValue() < 1) || (prefix_len->intValue() > 128)) {
693 isc_throw(BadValue, "prefix-len " << prefix_len->intValue()
694 << " must be in range of [1..128]");
695 }
696
697 lease->prefixlen_ = static_cast<uint8_t>(prefix_len->intValue());
698 }
699
700 // IAID
701 ConstElementPtr iaid = element->get("iaid");
702 if (!iaid || (iaid->getType() != Element::integer)) {
703 isc_throw(BadValue, "iaid is not present in the parsed lease"
704 " or it is not an integer");
705 }
706
707 if (iaid->intValue() < 0) {
708 isc_throw(BadValue, "iaid " << iaid->intValue() << " must not be negative");
709 }
710
711 lease->iaid_ = static_cast<uint32_t>(iaid->intValue());
712
713 // DUID
714 ConstElementPtr duid = element->get("duid");
715 if (!duid || (duid->getType() != Element::string)) {
716 isc_throw(BadValue, "duid not present in the parsed lease"
717 " or it is not a string");
718 }
719
720 try {
721 DUID parsed_duid = DUID::fromText(duid->stringValue());
722 lease->duid_.reset(new DUID(parsed_duid.getDuid()));
723
724 } catch (const std::exception& ex) {
725 isc_throw(BadValue, "invalid DUID "
726 << duid->stringValue() << " in the parsed lease");
727 }
728
729 // preferred lifetime
730 ConstElementPtr preferred_lft = element->get("preferred-lft");
731 if (!preferred_lft || (preferred_lft->getType() != Element::integer)) {
732 isc_throw(BadValue, "preferred-lft is not present in the parsed lease"
733 " or is not an integer");
734 }
735
736 if (preferred_lft->intValue() < 0) {
737 isc_throw(BadValue, "preferred-lft " << preferred_lft->intValue()
738 << " must not be negative");
739 }
740
741 lease->preferred_lft_ = static_cast<uint32_t>(preferred_lft->intValue());
742
743 return (lease);
744}
745
746std::ostream&
747operator<<(std::ostream& os, const Lease& lease) {
748 os << lease.toText();
749 return (os);
750}
751
752} // namespace isc::dhcp
753} // namespace isc
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 when an unexpected error condition occurs.
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 ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition duid.cc:73
Holds DUID (DHCPv6 Unique Identifier)
Definition duid.h:142
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
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:508
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< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition dhcp4.h:56
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:295
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
bool nullOrEqualValues(const T &ptr1, const T &ptr2)
This function checks if two pointers are both null or both are non-null and they point to equal value...
bool equalValues(const T &ptr1, const T &ptr2)
This function checks if two pointers are non-null and values are equal.
Defines the logger used by the top-level component of kea-lfc.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
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 IPv4 address.
Definition lease.h:303
ClientIdPtr client_id_
Client identifier.
Definition lease.h:309
void decline(uint32_t probation_period)
Sets IPv4 lease to declined state.
Definition lease.cc:377
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv4.
Definition lease.cc:336
bool operator==(const Lease4 &other) const
Compare two leases for equality.
Definition lease.cc:585
std::vector< uint8_t > remote_id_
Remote identifier for Bulk Lease Query.
Definition lease.h:493
std::vector< uint8_t > relay_id_
Relay identifier for Bulk Lease Query.
Definition lease.h:496
const std::vector< uint8_t > & getClientIdVector() const
Returns a client identifier.
Definition lease.cc:341
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition lease.cc:389
bool belongsToClient(const HWAddrPtr &hw_address, const ClientIdPtr &client_id) const
Check if the lease belongs to the client with the given identifiers.
Definition lease.cc:360
Lease4()
Default constructor.
Definition lease.cc:332
virtual std::string toText() const
Convert lease to printable form.
Definition lease.cc:561
static Lease4Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv4 lease created from JSON representation.
Definition lease.cc:417
Structure that holds a lease for IPv6 address and/or prefix.
Definition lease.h:516
const std::vector< uint8_t > & getDuidVector() const
Returns a reference to a vector representing a DUID.
Definition lease.cc:512
ExtendedInfoAction
Action on extended info tables.
Definition lease.h:553
virtual std::string toText() const
Convert Lease to Printable Form.
Definition lease.cc:535
bool operator==(const Lease6 &other) const
Compare two leases for equality.
Definition lease.cc:604
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv6.
Definition lease.cc:507
Lease6()
Constructor.
Definition lease.cc:499
Lease::Type type_
Lease type.
Definition lease.h:521
uint32_t reuseable_preferred_lft_
Remaining preferred lifetime.
Definition lease.h:550
uint32_t iaid_
Identity Association Identifier (IAID)
Definition lease.h:533
uint32_t preferred_lft_
Preferred lifetime.
Definition lease.h:542
DuidPtr duid_
Client identifier.
Definition lease.h:536
static Lease6Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv6 lease created from JSON representation.
Definition lease.cc:662
uint8_t prefixlen_
IPv6 prefix length.
Definition lease.h:526
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition lease.cc:628
void decline(uint32_t probation_period)
Sets IPv6 lease to declined state.
Definition lease.cc:522
a common structure for IPv4 and IPv6 leases
Definition lease.h:31
void updateCurrentExpirationTime()
Update lease current expiration time with new value, so that additional operations can be done withou...
Definition lease.cc:308
bool hasIdenticalFqdn(const Lease &other) const
Returns true if the other lease has equal FQDN data.
Definition lease.cc:133
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire)
Definition lease.h:34
uint32_t reuseable_valid_lft_
Remaining valid lifetime.
Definition lease.h:140
static std::string lifetimeToText(uint32_t lifetime)
Print lifetime.
Definition lease.cc:33
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
Definition lease.cc:123
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
Definition lease.cc:118
static const uint32_t STATE_DEFAULT
A lease in the default state.
Definition lease.h:69
uint32_t current_valid_lft_
Current valid lifetime.
Definition lease.h:133
SubnetID subnet_id_
Subnet identifier.
Definition lease.h:157
uint32_t pool_id_
The pool id.
Definition lease.h:162
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
Definition lease.cc:351
uint32_t valid_lft_
Valid lifetime.
Definition lease.h:128
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition lease.cc:93
static const uint32_t STATE_DECLINED
Declined lease.
Definition lease.h:72
bool expired() const
returns true if the lease is expired
Definition lease.cc:113
static const uint32_t STATE_RELEASED
Released lease held in the database for lease affinity.
Definition lease.h:78
Lease(const isc::asiolink::IOAddress &addr, uint32_t valid_lft, SubnetID subnet_id, time_t cltt, const bool fqdn_fwd, const bool fqdn_rev, const std::string &hostname, const HWAddrPtr &hwaddr)
Constructor.
Definition lease.cc:43
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Definition lease.h:75
Type
Type of lease or pool.
Definition lease.h:46
@ TYPE_TA
the lease contains temporary IPv6 address
Definition lease.h:48
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:49
@ TYPE_V4
IPv4 lease.
Definition lease.h:50
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47
static void syncCurrentExpirationTime(const Lease &from, Lease &to)
Sync lease current expiration time with new value from another lease, so that additional operations c...
Definition lease.cc:313
std::string hostname_
Client hostname.
Definition lease.h:167
uint32_t state_
Holds the lease state(s).
Definition lease.h:193
int64_t getExpirationTime() const
Returns lease expiration time.
Definition lease.cc:128
bool fqdn_fwd_
Forward zone updated?
Definition lease.h:172
time_t cltt_
Client last transmission time.
Definition lease.h:146
static void fromElementCommon(const LeasePtr &lease, const data::ConstElementPtr &element)
Sets common (for v4 and v6) properties of the lease object.
Definition lease.cc:140
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:55
static Type textToType(const std::string &text)
Converts type name to the actual type.
Definition lease.cc:75
HWAddrPtr hwaddr_
Client's MAC/hardware address.
Definition lease.h:182
bool fqdn_rev_
Reverse zone updated?
Definition lease.h:177
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition lease.h:123
time_t current_cltt_
Current client last transmission time.
Definition lease.h:152