11 #include <boost/algorithm/string.hpp> 12 #include <boost/scoped_ptr.hpp> 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;
29 uint32_t valid_lft,
SubnetID subnet_id, time_t cltt,
30 const bool fqdn_fwd,
const bool fqdn_rev,
31 const std::string& hostname,
const HWAddrPtr& hwaddr)
32 :addr_(addr), t1_(t1), t2_(t2), valid_lft_(valid_lft), cltt_(cltt),
33 subnet_id_(subnet_id), hostname_(hostname), fqdn_fwd_(fqdn_fwd),
34 fqdn_rev_(fqdn_rev), hwaddr_(hwaddr), state_(STATE_DEFAULT) {
44 return string(
"IA_NA");
46 return string(
"IA_TA");
48 return string(
"IA_PD");
52 tmp <<
"unknown (" << type <<
")";
63 }
else if (text ==
"IA_NA") {
66 }
else if (text ==
"IA_TA") {
69 }
else if (text ==
"IA_PD") {
84 return (
"expired-reclaimed");
90 s <<
"unknown (" << state <<
")";
127 if (element->getType() != Element::map) {
138 if (!ip_address || (ip_address->getType() != Element::string)) {
140 " or it is not a string");
143 boost::scoped_ptr<asiolink::IOAddress> io_address;
147 }
catch (
const std::exception& ex) {
149 <<
" in the parsed lease");
152 lease->addr_ = *io_address;
156 if (!subnet_id || (subnet_id->getType() != Element::integer)) {
158 " or it is not a number");
161 if (subnet_id->intValue() <= 0) {
163 <<
" a positive integer");
166 lease->subnet_id_ =
SubnetID(subnet_id->intValue());
171 if (hw_address->getType() != Element::string) {
180 }
catch (
const std::exception& ex) {
182 << hw_address->stringValue() <<
" in the parsed lease");
188 if (!cltt || (cltt->getType() != Element::integer)) {
190 " or it is not a number");
193 if (cltt->intValue() <= 0) {
195 " positive integer in the parsed lease");
198 lease->cltt_ = static_cast<time_t>(cltt->intValue());
202 if (!valid_lifetime || (valid_lifetime->getType() != Element::integer)) {
204 " or it is not a number");
207 if (valid_lifetime->intValue() < 0) {
209 <<
" is negative in the parsed lease");
212 lease->valid_lft_ = valid_lifetime->intValue();
216 if (!fqdn_fwd || fqdn_fwd->getType() != Element::boolean) {
218 " or it is not a boolean value");
221 lease->fqdn_fwd_ = fqdn_fwd->boolValue();
225 if (!fqdn_rev || (fqdn_rev->getType() != Element::boolean)) {
227 " or it is not a boolean value");
230 lease->fqdn_rev_ = fqdn_rev->boolValue();
234 if (!hostname || (hostname->getType() != Element::string)) {
236 " or it is not a string value");
239 lease->hostname_ = hostname->stringValue();
243 if (!state || (state->getType() != Element::integer)) {
245 " or it is not a number");
250 <<
" must be in range [0.." 254 lease->state_ = state->intValue();
259 if (ctx->getType() != Element::map) {
262 lease->setContext(ctx);
267 :
Lease(other.addr_, other.t1_, other.t2_, other.valid_lft_,
268 other.subnet_id_, other.cltt_, other.fqdn_fwd_,
269 other.fqdn_rev_, other.hostname_, other.hwaddr_) {
297 const uint32_t valid_lifetime,
304 const std::string& hostname)
306 :
Lease(address, t1, t2, valid_lifetime, subnet_id, cltt, fqdn_fwd,
307 fqdn_rev, hostname, hw_address),
308 client_id_(client_id) {
316 const std::vector<uint8_t>&
319 static std::vector<uint8_t> empty_vec;
326 const std::vector<uint8_t>&
329 static std::vector<uint8_t> empty_vec;
368 if (
this != &other) {
405 map->set(
"ip-address", Element::create(
addr_.
toText()));
406 map->set(
"subnet-id", Element::create(static_cast<long int>(
subnet_id_)));
407 map->set(
"hw-address", Element::create(
hwaddr_->toText(
false)));
410 map->set(
"client-id", Element::create(
client_id_->toText()));
413 map->set(
"cltt", Element::create(
cltt_));
414 map->set(
"valid-lft", Element::create(static_cast<long int>(
valid_lft_)));
416 map->set(
"fqdn-fwd", Element::create(
fqdn_fwd_));
417 map->set(
"fqdn-rev", Element::create(
fqdn_rev_));
418 map->set(
"hostname", Element::create(
hostname_));
420 map->set(
"state", Element::create(static_cast<int>(
state_)));
433 if (!lease->addr_.isV4()) {
438 if (!lease->hwaddr_) {
446 if (client_id->getType() != Element::string) {
454 }
catch (
const std::exception& ex) {
456 << client_id->stringValue() <<
" in the parsed lease");
464 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
465 uint32_t t1, uint32_t t2,
SubnetID subnet_id,
466 const HWAddrPtr& hwaddr, uint8_t prefixlen)
467 :
Lease(addr, t1, t2, valid, subnet_id, 0, false, false,
"", hwaddr),
468 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
469 preferred_lft_(preferred) {
478 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
479 uint32_t t1, uint32_t t2,
SubnetID subnet_id,
480 const bool fqdn_fwd,
const bool fqdn_rev,
481 const std::string& hostname,
const HWAddrPtr& hwaddr,
483 :
Lease(addr, t1, t2, valid, subnet_id, 0,
484 fqdn_fwd, fqdn_rev, hostname, hwaddr),
485 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
486 preferred_lft_(preferred) {
496 HWAddrPtr()), type_(TYPE_NA), prefixlen_(0), iaid_(0),
497 duid_(
DuidPtr()), preferred_lft_(0) {
505 const std::vector<uint8_t>&
508 static std::vector<uint8_t> empty_vec;
512 return (
duid_->getDuid());
532 ostringstream stream;
536 << static_cast<int>(
type_) <<
")\n" 537 <<
"Address: " <<
addr_ <<
"\n" 538 <<
"Prefix length: " << static_cast<int>(
prefixlen_) <<
"\n" 539 <<
"IAID: " <<
iaid_ <<
"\n" 542 <<
"Cltt: " <<
cltt_ <<
"\n" 543 <<
"DUID: " << (
duid_?
duid_->toText():
"(none)") <<
"\n" 544 <<
"Hardware addr: " << (
hwaddr_?
hwaddr_->toText(
false):
"(none)") <<
"\n" 549 stream <<
"User context: " <<
getContext()->str() <<
"\n";
552 return (stream.str());
557 ostringstream stream;
559 stream <<
"Address: " <<
addr_ <<
"\n" 561 <<
"T1: " <<
t1_ <<
"\n" 562 <<
"T2: " <<
t2_ <<
"\n" 563 <<
"Cltt: " <<
cltt_ <<
"\n" 564 <<
"Hardware addr: " << (
hwaddr_ ?
hwaddr_->toText(
false) :
"(none)") <<
"\n" 570 stream <<
"User context: " <<
getContext()->str() <<
"\n";
573 return (stream.str());
620 map->set(
"ip-address", Element::create(
addr_.
toText()));
623 map->set(
"prefix-len", Element::create(
prefixlen_));
625 map->set(
"iaid", Element::create(static_cast<long int>(
iaid_)));
626 map->set(
"duid", Element::create(
duid_->toText()));
627 map->set(
"subnet-id", Element::create(static_cast<long int>(
subnet_id_)));
629 map->set(
"cltt", Element::create(
cltt_));
630 map->set(
"preferred-lft", Element::create(static_cast<long int>(
preferred_lft_)));
631 map->set(
"valid-lft", Element::create(static_cast<long int>(
valid_lft_)));
633 map->set(
"fqdn-fwd", Element::create(
fqdn_fwd_));
634 map->set(
"fqdn-rev", Element::create(
fqdn_rev_));
635 map->set(
"hostname", Element::create(
hostname_));
638 map->set(
"hw-address", Element::create(
hwaddr_->toText(
false)));
641 map->set(
"state", Element::create(static_cast<long int>(
state_)));
654 if (!lease->addr_.isV6()) {
660 if (!lease_type || (lease_type->getType() != Element::string)) {
662 " or it is not a string value");
665 lease->type_ =
textToType(lease_type->stringValue());
670 if (!prefix_len || (prefix_len->getType() != Element::integer)) {
672 " or it is not a number");
675 if ((prefix_len->intValue() < 1) || (prefix_len->intValue() > 128)) {
677 <<
" must be in range of [1..128]");
680 lease->prefixlen_ = static_cast<uint8_t>(prefix_len->intValue());
685 if (!iaid || (iaid->getType() != Element::integer)) {
687 " or it is not a number");
690 if (iaid->intValue() < 0) {
694 lease->iaid_ = static_cast<uint32_t>(iaid->intValue());
698 if (!duid || (duid->getType() != Element::string)) {
700 " or it is not a string");
707 }
catch (
const std::exception& ex) {
709 << duid->stringValue() <<
" in the parsed lease");
714 if (!preferred_lft || (preferred_lft->getType() != Element::integer)) {
716 " or is not a number");
719 if (preferred_lft->intValue() < 0) {
721 <<
" must not be negative");
724 lease->preferred_lft_ = static_cast<uint32_t>(preferred_lft->intValue());
uint32_t state_
Holds the lease state(s).
boost::shared_ptr< DUID > DuidPtr
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
HWAddrPtr hwaddr_
Client's MAC/hardware address.
uint32_t iaid_
Identity Association Identifier (IAID)
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Structure that holds a lease for IPv4 address.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
bool fqdn_rev_
Reverse zone updated?
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
static std::string typeToText(Type type)
returns text representation of a lease type
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
time_t cltt_
Client last transmission time.
uint32_t preferred_lft_
preferred lifetime
static const DUID & EMPTY()
Defines the constant "empty" DUID.
bool fqdn_fwd_
Forward zone updated?
virtual std::string toText() const
Convert Lease to Printable Form.
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
static Lease4Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv4 lease created from JSON representation.
the lease contains IPv6 prefix (for prefix delegation)
boost::shared_ptr< Element > ElementPtr
std::vector< uint8_t > hwaddr_
static DUID fromText(const std::string &text)
Create DUID from the textual format.
void decline(uint32_t probation_period)
Sets IPv4 lease to declined state.
std::string hostname_
Client hostname.
SubnetID subnet_id_
Subnet identifier.
virtual std::string toText() const =0
Convert Lease to Printable Form.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Holds DUID (DHCPv6 Unique Identifier)
#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...
uint32_t t1_
Renewal timer.
int64_t getExpirationTime() const
Returns lease expiration time.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
A generic exception that is thrown when an unexpected error condition occurs.
Lease4()
Default constructor.
bool equalValues(const T &ptr1, const T &ptr2)
This function checks if two pointers are non-null and values are equal.
boost::shared_ptr< const Element > ConstElementPtr
DuidPtr duid_
Client identifier.
std::vector< uint8_t > duid_
The actual content of the DUID.
Structure that holds a lease for IPv6 address and/or prefix.
ClientIdPtr client_id_
Client identifier.
Lease::Type type_
Lease type.
bool belongsToClient(const HWAddrPtr &hw_address, const ClientIdPtr &client_id) const
Check if the lease belongs to the client with the given identifiers.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
uint8_t prefixlen_
IPv6 prefix length.
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
the lease contains temporary IPv6 address
bool operator==(const Lease4 &other) const
Compare two leases for equality.
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
the lease contains non-temporary IPv6 address
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 expired() const
returns true if the lease is expired
Lease4 & operator=(const Lease4 &other)
Assignment operator.
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Defines the logger used by the top-level component of kea-dhcp-ddns.
static Lease6Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv6 lease created from JSON representation.
static Type textToType(const std::string &text)
Converts type name to the actual type.
std::string toText() const
Convert the address to a string.
const std::vector< uint8_t > & getDuidVector() const
Returns a reference to a vector representing a DUID.
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv4.
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
A wrapper interface for the ASIO library.
a common structure for IPv4 and IPv6 leases
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv6.
static HWAddr fromText(const std::string &text, const uint16_t htype=HTYPE_ETHER)
Creates instance of the hardware address from textual format.
Type
Type of lease or pool.
std::ostream & operator<<(std::ostream &os, const OpaqueDataTuple &tuple)
Inserts the OpaqueDataTuple as a string into stream.
Holds Client identifier or client IPv4 address.
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).
void decline(uint32_t probation_period)
Sets IPv6 lease to declined state.
Hardware type that represents information from DHCPv4 packet.
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
static const uint32_t STATE_DEFAULT
A lease in the default state.
bool operator==(const Lease6 &other) const
Compare two leases for equality.
static const uint32_t STATE_DECLINED
Declined lease.
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
uint32_t t2_
Rebinding timer.
uint32_t valid_lft_
Valid lifetime.
const std::vector< uint8_t > & getClientIdVector() const
Returns a client identifier.
The IOAddress class represents an IP addresses (version agnostic)
static void fromElementCommon(const LeasePtr &lease, const data::ConstElementPtr &element)
Sets common (for v4 and v6) properties of the lease object.
virtual std::string toText() const
Convert lease to printable form.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
bool hasIdenticalFqdn(const Lease &other) const
Returns true if the other lease has equal FQDN data.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)