Commit 5054d0ed authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[2320] Lease4 and Lease6 now have common ancestor.

parent 2039944b
...@@ -32,13 +32,18 @@ using namespace std; ...@@ -32,13 +32,18 @@ using namespace std;
namespace isc { namespace isc {
namespace dhcp { namespace dhcp {
Lease::Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
uint32_t valid_lft, SubnetID subnet_id, time_t cltt)
:addr_(addr), t1_(t1), t2_(t2), valid_lft_(valid_lft), cltt_(cltt),
subnet_id_(subnet_id), fixed_(false), fqdn_fwd_(false), fqdn_rev_(false) {
}
Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr, Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr,
DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid, DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
uint32_t t1, uint32_t t2, SubnetID subnet_id, uint8_t prefixlen) uint32_t t1, uint32_t t2, SubnetID subnet_id, uint8_t prefixlen)
: addr_(addr), type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid), : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/),
preferred_lft_(preferred), valid_lft_(valid), t1_(t1), t2_(t2), type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
subnet_id_(subnet_id), fixed_(false), fqdn_fwd_(false), preferred_lft_(preferred) {
fqdn_rev_(false) {
if (!duid) { if (!duid) {
isc_throw(InvalidOperation, "DUID must be specified for a lease"); isc_throw(InvalidOperation, "DUID must be specified for a lease");
} }
...@@ -46,7 +51,7 @@ Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr, ...@@ -46,7 +51,7 @@ Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr,
cltt_ = time(NULL); cltt_ = time(NULL);
} }
bool Lease6::expired() const { bool Lease::expired() const {
// Let's use int64 to avoid problems with negative/large uint32 values // Let's use int64 to avoid problems with negative/large uint32 values
int64_t expire_time = cltt_ + valid_lft_; int64_t expire_time = cltt_ + valid_lft_;
...@@ -63,7 +68,7 @@ std::string LeaseMgr::getParameter(const std::string& name) const { ...@@ -63,7 +68,7 @@ std::string LeaseMgr::getParameter(const std::string& name) const {
} }
std::string std::string
Lease6::toText() { Lease6::toText() const {
ostringstream stream; ostringstream stream;
stream << "Type: " << static_cast<int>(type_) << " ("; stream << "Type: " << static_cast<int>(type_) << " (";
...@@ -91,6 +96,21 @@ Lease6::toText() { ...@@ -91,6 +96,21 @@ Lease6::toText() {
return (stream.str()); return (stream.str());
} }
std::string
Lease4::toText() const {
ostringstream stream;
stream << "Address: " << addr_.toText() << "\n"
<< "Valid life: " << valid_lft_ << "\n"
<< "T1: " << t1_ << "\n"
<< "T2: " << t2_ << "\n"
<< "Cltt: " << cltt_ << "\n"
<< "Subnet ID: " << subnet_id_ << "\n";
return (stream.str());
}
bool bool
Lease4::operator==(const Lease4& other) const { Lease4::operator==(const Lease4& other) const {
return ( return (
......
...@@ -108,38 +108,19 @@ public: ...@@ -108,38 +108,19 @@ public:
isc::Exception(file, line, what) {} isc::Exception(file, line, what) {}
}; };
/// @brief Structure that holds a lease for IPv4 address /// @brief a common structure for IPv4 and IPv6 leases
/// ///
/// For performance reasons it is a simple structure, not a class. If we chose /// This structure holds all information that is common between IPv4 and IPv6
/// make it a class, all fields would have to made private and getters/setters /// leases.
/// would be required. As this is a critical part of the code that will be used struct Lease {
/// extensively, direct access is warranted.
struct Lease4 {
/// @brief Maximum size of a hardware address
static const size_t HWADDR_MAX = 20;
/// IPv4 address
isc::asiolink::IOAddress addr_;
/// @brief Address extension
///
/// It is envisaged that in some cases IPv4 address will be accompanied
/// with some additional data. One example of such use are Address + Port
/// solutions (or Port-restricted Addresses), where several clients may get
/// the same address, but different port ranges. This feature is not
/// expected to be widely used. Under normal circumstances, the value
/// should be 0.
uint32_t ext_;
/// @brief Hardware address Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
std::vector<uint8_t> hwaddr_; uint32_t valid_lft, SubnetID subnet_id, time_t cltt);
/// @brief Client identifier /// @brief IPv4 ot IPv6 address
/// ///
/// @todo Should this be a pointer to a client ID or the ID itself? /// IPv4, IPv6 address or, in the case of a prefix delegation, the prefix.
/// Compare with the DUID in the Lease6 structure. isc::asiolink::IOAddress addr_;
ClientIdPtr client_id_;
/// @brief Renewal timer /// @brief Renewal timer
/// ///
...@@ -201,6 +182,46 @@ struct Lease4 { ...@@ -201,6 +182,46 @@ struct Lease4 {
/// system administrator. /// system administrator.
std::string comments_; std::string comments_;
/// @brief Convert Lease to Printable Form
///
/// @return String form of the lease
virtual std::string toText() const = 0;
/// @brief returns true if the lease is expired
/// @return true if the lease is expired
bool expired() const;
};
/// @brief Structure that holds a lease for IPv4 address
///
/// For performance reasons it is a simple structure, not a class. If we chose
/// make it a class, all fields would have to made private and getters/setters
/// would be required. As this is a critical part of the code that will be used
/// extensively, direct access is warranted.
struct Lease4 : public Lease {
/// @brief Maximum size of a hardware address
static const size_t HWADDR_MAX = 20;
/// @brief Address extension
///
/// It is envisaged that in some cases IPv4 address will be accompanied
/// with some additional data. One example of such use are Address + Port
/// solutions (or Port-restricted Addresses), where several clients may get
/// the same address, but different port ranges. This feature is not
/// expected to be widely used. Under normal circumstances, the value
/// should be 0.
uint32_t ext_;
/// @brief Hardware address
std::vector<uint8_t> hwaddr_;
/// @brief Client identifier
///
/// @todo Should this be a pointer to a client ID or the ID itself?
/// Compare with the DUID in the Lease6 structure.
ClientIdPtr client_id_;
/// @brief Constructor /// @brief Constructor
/// ///
/// @param addr IPv4 address as unsigned 32-bit integer in network byte /// @param addr IPv4 address as unsigned 32-bit integer in network byte
...@@ -215,18 +236,16 @@ struct Lease4 { ...@@ -215,18 +236,16 @@ struct Lease4 {
Lease4(uint32_t addr, const uint8_t* hwaddr, size_t hwaddr_len, Lease4(uint32_t addr, const uint8_t* hwaddr, size_t hwaddr_len,
const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft, const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
time_t cltt, uint32_t subnet_id) time_t cltt, uint32_t subnet_id)
: addr_(addr), ext_(0), hwaddr_(hwaddr, hwaddr + hwaddr_len), : Lease(addr, 0, 0, valid_lft, subnet_id, cltt),
client_id_(new ClientId(clientid, clientid_len)), t1_(0), t2_(0), ext_(0), hwaddr_(hwaddr, hwaddr + hwaddr_len),
valid_lft_(valid_lft), cltt_(cltt), subnet_id_(subnet_id), client_id_(new ClientId(clientid, clientid_len)) {
fixed_(false), hostname_(), fqdn_fwd_(false), fqdn_rev_(false), }
comments_()
{}
/// @brief Default Constructor /// @brief Default Constructor
/// ///
/// Initialize fields that don't have a default constructor. /// Initialize fields that don't have a default constructor.
Lease4() : addr_(0), fixed_(false), fqdn_fwd_(false), fqdn_rev_(false) Lease4() : Lease(0, 0, 0, 0, 0, 0) {
{} }
/// @brief Compare two leases for equality /// @brief Compare two leases for equality
/// ///
...@@ -240,6 +259,11 @@ struct Lease4 { ...@@ -240,6 +259,11 @@ struct Lease4 {
return (!operator==(other)); return (!operator==(other));
} }
/// @brief Convert Lease to Printable Form
///
/// @return String form of the lease
virtual std::string toText() const;
/// @todo: Add DHCPv4 failover related fields here /// @todo: Add DHCPv4 failover related fields here
}; };
...@@ -257,7 +281,7 @@ typedef std::vector<Lease4Ptr> Lease4Collection; ...@@ -257,7 +281,7 @@ typedef std::vector<Lease4Ptr> Lease4Collection;
/// make it a class, all fields would have to made private and getters/setters /// make it a class, all fields would have to made private and getters/setters
/// would be required. As this is a critical part of the code that will be used /// would be required. As this is a critical part of the code that will be used
/// extensively, direct access is warranted. /// extensively, direct access is warranted.
struct Lease6 { struct Lease6 : public Lease {
/// @brief Type of lease contents /// @brief Type of lease contents
typedef enum { typedef enum {
...@@ -266,11 +290,6 @@ struct Lease6 { ...@@ -266,11 +290,6 @@ struct Lease6 {
LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation) LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation)
} LeaseType; } LeaseType;
/// @brief IPv6 address
///
/// IPv6 address or, in the case of a prefix delegation, the prefix.
isc::asiolink::IOAddress addr_;
/// @brief Lease type /// @brief Lease type
/// ///
/// One of normal address, temporary address, or prefix. /// One of normal address, temporary address, or prefix.
...@@ -297,69 +316,6 @@ struct Lease6 { ...@@ -297,69 +316,6 @@ struct Lease6 {
/// assigned or renewed (cltt), expressed in seconds. /// assigned or renewed (cltt), expressed in seconds.
uint32_t preferred_lft_; uint32_t preferred_lft_;
/// @brief valid lifetime
///
/// This parameter specifies the valid lifetime since the lease waa
/// assigned/renewed (cltt), expressed in seconds.
uint32_t valid_lft_;
/// @brief T1 timer
///
/// Specifies renewal time. Although technically it is a property of the
/// IA container and not the address itself, since our data model does not
/// define a separate IA entity, we are keeping it in the lease. In the
/// case of multiple addresses/prefixes for the same IA, each must have
/// consistent T1 and T2 values. This is specified in seconds since cltt.
/// The value will also be useful for failover to calculate the next
/// expected client transmission time.
uint32_t t1_;
/// @brief T2 timer
///
/// Specifies rebinding time. Although technically it is a property of the
/// IA container and not the address itself, since our data model does not
/// define a separate IA entity, we are keeping it in the lease. In the
/// case of multiple addresses/prefixes for the same IA, each must have
/// consistent T1 and T2 values. This is specified in seconds since cltt.
uint32_t t2_;
/// @brief Client last transmission time
///
/// Specifies a timestamp giving the time when the last transmission from a
/// client was received.
time_t cltt_;
/// @brief Subnet identifier
///
/// Specifies the identification of the subnet to which the lease belongs.
SubnetID subnet_id_;
/// @brief Fixed lease?
///
/// Fixed leases are kept after they are released/expired.
bool fixed_;
/// @brief Client hostname
///
/// This field may be empty
std::string hostname_;
/// @brief Forward zone updated?
///
/// Set true if the DNS AAAA record for this lease has been updated.
bool fqdn_fwd_;
/// @brief Reverse zone updated?
///
/// Set true if the DNS PTR record for this lease has been updated.
bool fqdn_rev_;
/// @brief Lease comments
///
/// Currently not used. It may be used for keeping comments made by the
/// system administrator.
std::string comments_;
/// @todo: Add DHCPv6 failover related fields here /// @todo: Add DHCPv6 failover related fields here
/// @brief Constructor /// @brief Constructor
...@@ -370,18 +326,9 @@ struct Lease6 { ...@@ -370,18 +326,9 @@ struct Lease6 {
/// @brief Constructor /// @brief Constructor
/// ///
/// Initialize fields that don't have a default constructor. /// Initialize fields that don't have a default constructor.
Lease6() : addr_("::"), type_(LEASE_IA_NA), fixed_(false), fqdn_fwd_(false), Lease6() : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0),
fqdn_rev_(false) type_(LEASE_IA_NA) {
{} }
/// @brief Convert Lease6 to Printable Form
///
/// @return String form of the lease
std::string toText();
/// @brief returns true if the lease is expired
/// @return true if the lease is expired
bool expired() const;
/// @brief Compare two leases for equality /// @brief Compare two leases for equality
/// ///
...@@ -394,6 +341,11 @@ struct Lease6 { ...@@ -394,6 +341,11 @@ struct Lease6 {
bool operator!=(const Lease6& other) const { bool operator!=(const Lease6& other) const {
return (!operator==(other)); return (!operator==(other));
} }
/// @brief Convert Lease to Printable Form
///
/// @return String form of the lease
virtual std::string toText() const;
}; };
/// @brief Pointer to a Lease6 structure. /// @brief Pointer to a Lease6 structure.
......
...@@ -225,8 +225,8 @@ protected: ...@@ -225,8 +225,8 @@ protected:
// IPv6 address that are unique. That particular key is a member // IPv6 address that are unique. That particular key is a member
// of the Lease6 structure, is of type IOAddress and can be accessed // of the Lease6 structure, is of type IOAddress and can be accessed
// by doing &Lease6::addr_ // by doing &Lease6::addr_
boost::multi_index::ordered_unique< boost::multi_index::ordered_unique<
boost::multi_index::member<Lease6, isc::asiolink::IOAddress, &Lease6::addr_> boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
> >
> >
> Lease6Storage; // Let the whole contraption be called Lease6Storage. > Lease6Storage; // Let the whole contraption be called Lease6Storage.
...@@ -238,4 +238,3 @@ protected: ...@@ -238,4 +238,3 @@ protected:
}; // end of isc namespace }; // end of isc namespace
#endif // MEMFILE_LEASE_MGR #endif // MEMFILE_LEASE_MGR
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment