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 Lease::lifetimeToText(uint32_t lifetime) {
31 if (lifetime == INFINITY_LFT) {
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) {
57 return string(
"IA_NA");
59 return string(
"IA_TA");
61 return string(
"IA_PD");
65 tmp <<
"unknown (" << type <<
")";
76 }
else if (text ==
"IA_NA") {
79 }
else if (text ==
"IA_TA") {
82 }
else if (text ==
"IA_PD") {
97 return (
"expired-reclaimed");
102 std::ostringstream s;
103 s <<
"unknown (" << state <<
")";
140 if (element->getType() != Element::map) {
151 if (!ip_address || (ip_address->getType() != Element::string)) {
153 " or it is not a string");
156 boost::scoped_ptr<asiolink::IOAddress> io_address;
160 }
catch (
const std::exception& ex) {
162 <<
" in the parsed lease");
165 lease->addr_ = *io_address;
169 if (!subnet_id || (subnet_id->getType() != Element::integer)) {
171 " or it is not a number");
174 if (subnet_id->intValue() <= 0) {
176 <<
" a positive integer");
179 lease->subnet_id_ =
SubnetID(subnet_id->intValue());
184 if (hw_address->getType() != Element::string) {
193 }
catch (
const std::exception& ex) {
195 << hw_address->stringValue() <<
" in the parsed lease");
201 if (!cltt || (cltt->getType() != Element::integer)) {
203 " or it is not a number");
206 if (cltt->intValue() <= 0) {
208 " positive integer in the parsed lease");
211 lease->cltt_ =
static_cast<time_t
>(cltt->intValue());
215 if (!valid_lifetime || (valid_lifetime->getType() != Element::integer)) {
217 " or it is not a number");
220 if (valid_lifetime->intValue() < 0) {
222 <<
" is negative in the parsed lease");
225 lease->valid_lft_ = valid_lifetime->intValue();
229 if (!fqdn_fwd || fqdn_fwd->getType() != Element::boolean) {
231 " or it is not a boolean value");
234 lease->fqdn_fwd_ = fqdn_fwd->boolValue();
238 if (!fqdn_rev || (fqdn_rev->getType() != Element::boolean)) {
240 " or it is not a boolean value");
243 lease->fqdn_rev_ = fqdn_rev->boolValue();
247 if (!hostname || (hostname->getType() != Element::string)) {
249 " or it is not a string value");
252 lease->hostname_ = hostname->stringValue();
253 boost::algorithm::to_lower(lease->hostname_);
257 if (!state || (state->getType() != Element::integer)) {
259 " or it is not a number");
264 <<
" must be in range [0.."
265 << Lease::STATE_EXPIRED_RECLAIMED <<
"]");
268 lease->state_ = state->intValue();
273 if (ctx->getType() != Element::map) {
276 lease->setContext(ctx);
279 lease->updateCurrentExpirationTime();
294 :
Lease(other.addr_, other.valid_lft_,
295 other.subnet_id_, other.cltt_, other.fqdn_fwd_,
296 other.fqdn_rev_, other.hostname_, other.hwaddr_) {
324 const uint32_t valid_lifetime,
329 const std::string& hostname)
331 :
Lease(address, valid_lifetime, subnet_id, cltt, fqdn_fwd,
332 fqdn_rev, hostname, hw_address),
333 client_id_(client_id) {
341 const std::vector<uint8_t>&
344 static std::vector<uint8_t> empty_vec;
351 const std::vector<uint8_t>&
354 static std::vector<uint8_t> empty_vec;
391 if (
this != &other) {
429 map->set(
"ip-address", Element::create(
addr_.
toText()));
430 map->set(
"subnet-id", Element::create(static_cast<long int>(
subnet_id_)));
431 map->set(
"hw-address", Element::create(
hwaddr_->toText(
false)));
434 map->set(
"client-id", Element::create(
client_id_->toText()));
437 map->set(
"cltt", Element::create(
cltt_));
438 map->set(
"valid-lft", Element::create(static_cast<long int>(
valid_lft_)));
440 map->set(
"fqdn-fwd", Element::create(
fqdn_fwd_));
441 map->set(
"fqdn-rev", Element::create(
fqdn_rev_));
442 map->set(
"hostname", Element::create(
hostname_));
444 map->set(
"state", Element::create(static_cast<int>(
state_)));
457 if (!lease->addr_.isV4()) {
462 if (!lease->hwaddr_) {
470 if (client_id->getType() != Element::string) {
478 }
catch (
const std::exception& ex) {
480 << client_id->stringValue() <<
" in the parsed lease");
488 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
490 :
Lease(addr, valid, subnet_id, 0, false, false,
"", hwaddr),
491 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
492 preferred_lft_(preferred), reuseable_preferred_lft_(0) {
502 DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
503 SubnetID subnet_id,
const bool fqdn_fwd,
const bool fqdn_rev,
504 const std::string& hostname,
const HWAddrPtr& hwaddr,
506 :
Lease(addr, valid, subnet_id, 0,
507 fqdn_fwd, fqdn_rev, hostname, hwaddr),
508 type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
509 preferred_lft_(preferred), reuseable_preferred_lft_(0) {
520 HWAddrPtr()), type_(TYPE_NA), prefixlen_(0), iaid_(0),
521 duid_(
DuidPtr()), preferred_lft_(0), reuseable_preferred_lft_(0) {
529 const std::vector<uint8_t>&
532 static std::vector<uint8_t> empty_vec;
536 return (
duid_->getDuid());
554 ostringstream stream;
558 <<
static_cast<int>(
type_) <<
")\n"
559 <<
"Address: " <<
addr_ <<
"\n"
560 <<
"Prefix length: " << static_cast<int>(
prefixlen_) <<
"\n"
561 <<
"IAID: " <<
iaid_ <<
"\n"
564 <<
"Cltt: " <<
cltt_ <<
"\n"
565 <<
"DUID: " << (
duid_?
duid_->toText():
"(none)") <<
"\n"
566 <<
"Hardware addr: " << (
hwaddr_?
hwaddr_->toText(
false):
"(none)") <<
"\n"
571 stream <<
"User context: " <<
getContext()->str() <<
"\n";
574 return (stream.str());
579 ostringstream stream;
581 stream <<
"Address: " <<
addr_ <<
"\n"
583 <<
"Cltt: " <<
cltt_ <<
"\n"
584 <<
"Hardware addr: " << (
hwaddr_ ?
hwaddr_->toText(
false) :
"(none)") <<
"\n"
590 stream <<
"User context: " <<
getContext()->str() <<
"\n";
593 return (stream.str());
643 map->set(
"ip-address", Element::create(
addr_.
toText()));
646 map->set(
"prefix-len", Element::create(
prefixlen_));
648 map->set(
"iaid", Element::create(static_cast<long int>(
iaid_)));
649 map->set(
"duid", Element::create(
duid_->toText()));
650 map->set(
"subnet-id", Element::create(static_cast<long int>(
subnet_id_)));
652 map->set(
"cltt", Element::create(
cltt_));
653 map->set(
"preferred-lft", Element::create(static_cast<long int>(
preferred_lft_)));
654 map->set(
"valid-lft", Element::create(static_cast<long int>(
valid_lft_)));
656 map->set(
"fqdn-fwd", Element::create(
fqdn_fwd_));
657 map->set(
"fqdn-rev", Element::create(
fqdn_rev_));
658 map->set(
"hostname", Element::create(
hostname_));
661 map->set(
"hw-address", Element::create(
hwaddr_->toText(
false)));
664 map->set(
"state", Element::create(static_cast<long int>(
state_)));
677 if (!lease->addr_.isV6()) {
683 if (!lease_type || (lease_type->getType() != Element::string)) {
685 " or it is not a string value");
688 lease->type_ =
textToType(lease_type->stringValue());
693 if (!prefix_len || (prefix_len->getType() != Element::integer)) {
695 " or it is not a number");
698 if ((prefix_len->intValue() < 1) || (prefix_len->intValue() > 128)) {
700 <<
" must be in range of [1..128]");
703 lease->prefixlen_ =
static_cast<uint8_t
>(prefix_len->intValue());
708 if (!iaid || (iaid->getType() != Element::integer)) {
710 " or it is not a number");
713 if (iaid->intValue() < 0) {
717 lease->iaid_ =
static_cast<uint32_t
>(iaid->intValue());
721 if (!duid || (duid->getType() != Element::string)) {
723 " or it is not a string");
730 }
catch (
const std::exception& ex) {
732 << duid->stringValue() <<
" in the parsed lease");
737 if (!preferred_lft || (preferred_lft->getType() != Element::integer)) {
739 " or is not a number");
742 if (preferred_lft->intValue() < 0) {
744 <<
" must not be negative");
747 lease->preferred_lft_ =
static_cast<uint32_t
>(preferred_lft->intValue());
virtual std::string toText() const
Convert Lease to Printable Form.
static std::string lifetimeToText(uint32_t lifetime)
Print lifetime.
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
bool operator==(const Lease6 &other) const
Compare two leases for equality.
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)
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Structure that holds a lease for IPv4 address.
uint32_t reuseable_preferred_lft_
Remaining preferred lifetime.
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
virtual std::string toText() const
Convert lease to printable form.
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?
const std::vector< uint8_t > & getClientIdVector() const
Returns a client identifier.
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_
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
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.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
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...
static void syncCurrentExpirationTime(const Lease &from, Lease &to)
Sync lease current expiration time with new value from another lease, so that additional operations c...
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.
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
bool equalValues(const T &ptr1, const T &ptr2)
This function checks if two pointers are non-null and values are equal.
void updateCurrentExpirationTime()
Update lease current expiration time with new value, so that additional operations can be done withou...
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.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
uint8_t prefixlen_
IPv6 prefix length.
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
the lease contains temporary IPv6 address
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...
Lease4 & operator=(const Lease4 &other)
Assignment operator.
time_t current_cltt_
Current client last transmission time.
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
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.
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv4.
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire)
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.
std::string toText() const
Convert the address to a string.
Hardware type that represents information from DHCPv4 packet.
uint32_t reuseable_valid_lft_
Remaining valid lifetime.
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
static const uint32_t STATE_DEFAULT
A lease in the default state.
const std::vector< uint8_t > & getDuidVector() const
Returns a reference to a vector representing a DUID.
bool belongsToClient(const HWAddrPtr &hw_address, const ClientIdPtr &client_id) const
Check if the lease belongs to the client with the given identifiers.
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
static const uint32_t STATE_DECLINED
Declined lease.
uint32_t valid_lft_
Valid lifetime.
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.
bool expired() const
returns true if the lease is expired
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
uint32_t current_valid_lft_
Current valid lifetime.
bool operator==(const Lease4 &other) const
Compare two leases for equality.
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)