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