Commit 93ffcbb3 authored by Stephen Morris's avatar Stephen Morris

[2342] Now have addLease and getLease6 methods apparently working

parent c7cb0c33
......@@ -160,6 +160,11 @@ struct Lease4 {
std::string comments_;
/// @todo: Add DHCPv4 failover related fields here
/// @brief Constructor
///
/// Initialize fields that don't have a default constructor.
Lease4() : addr_(0) {}
};
/// @brief Pointer to a Lease4 structure.
......@@ -271,6 +276,11 @@ struct Lease6 {
std::string comments_;
/// @todo: Add DHCPv6 failover related fields here
/// @brief Constructor
///
/// Initialize fields that don't have a default constructor.
Lease6() : addr_(0) {}
};
/// @brief Pointer to a Lease6 structure.
......@@ -309,12 +319,22 @@ public:
/// @brief Adds an IPv4 lease.
///
/// @param lease lease to be added
virtual bool addLease(Lease4Ptr lease) = 0;
///
/// @result true if the lease was added, false if not (because a lease
/// with the same address was already there).
///
/// @exception DbOperationError Database function failed
virtual bool addLease(const Lease4Ptr& lease) = 0;
/// @brief Adds an IPv6 lease.
///
/// @param lease lease to be added
virtual bool addLease(Lease6Ptr lease) = 0;
///
/// @result true if the lease was added, false if not (because a lease
/// with the same address was already there).
///
/// @exception DbOperationError Database function failed
virtual bool addLease(const Lease6Ptr& lease) = 0;
/// @brief Returns existing IPv4 lease for specified IPv4 address and subnet_id
///
......@@ -326,7 +346,7 @@ public:
/// @param subnet_id ID of the subnet the lease must belong to
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr,
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr,
SubnetID subnet_id) const = 0;
/// @brief Returns an IPv4 lease for specified IPv4 address
......@@ -342,7 +362,7 @@ public:
/// @param subnet_id ID of the subnet the lease must belong to
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const = 0;
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
/// @brief Returns existing IPv4 leases for specified hardware address.
///
......@@ -402,7 +422,7 @@ public:
/// @param addr address of the searched lease
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const = 0;
virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const = 0;
/// @brief Returns existing IPv6 leases for a given DUID+IA combination
///
......@@ -433,28 +453,28 @@ public:
/// @param lease4 The lease to be updated.
///
/// If no such lease is present, an exception will be thrown.
virtual void updateLease4(Lease4Ptr lease4) = 0;
virtual void updateLease4(const Lease4Ptr& lease4) = 0;
/// @brief Updates IPv4 lease.
///
/// @param lease4 The lease to be updated.
///
/// If no such lease is present, an exception will be thrown.
virtual void updateLease6(Lease6Ptr lease6) = 0;
virtual void updateLease6(const Lease6Ptr& lease6) = 0;
/// @brief Deletes a lease.
///
/// @param addr IPv4 address of the lease to be deleted.
///
/// @return true if deletion was successful, false if no such lease exists
virtual bool deleteLease4(uint32_t addr) = 0;
virtual bool deleteLease4(const isc::asiolink::IOAddress& addr) = 0;
/// @brief Deletes a lease.
///
/// @param addr IPv4 address of the lease to be deleted.
///
/// @return true if deletion was successful, false if no such lease exists
virtual bool deleteLease6(isc::asiolink::IOAddress addr) = 0;
virtual bool deleteLease6(const isc::asiolink::IOAddress& addr) = 0;
/// @brief Returns backend name.
///
......@@ -482,10 +502,27 @@ public:
/// Also if B>C, some database upgrade procedure may be triggered
virtual std::pair<uint32_t, uint32_t> getVersion() const = 0;
/// @brief Commit Transactions
///
/// Commits all pending database operations. On databases that don't
/// support transactions, this is a no-op.
///
/// @exception DbOperationError if the commit failed.
virtual void commit() = 0;
/// @brief Rollback Transactions
///
/// Rolls back all pending database operations. On databases that don't
/// support transactions, this is a no-op.
///
/// @exception DbOperationError if the rollback failed.
virtual void rollback() = 0;
/// @todo: Add host management here
/// As host reservation is outside of scope for 2012, support for hosts
/// is currently postponed.
protected:
/// @brief returns value of the parameter
std::string getParameter(const std::string& name) const;
......
......@@ -16,14 +16,64 @@
#include <iomanip>
#include <string>
#include <config.h>
#include <time.h>
#include <dhcp/mysql_lease_mgr.h>
#include <asiolink/io_address.h>
using namespace std;
namespace isc {
namespace dhcp {
// Time conversion methods.
//
// Note that the MySQL TIMESTAMP data type (used for "expire") converts data
// from the current timezone to UTC for storage, and from UTC to the current
// timezone for retrieval. This means that the external interface - cltt -
// must be local time.
void
MySqlLeaseMgr::convertFromLeaseTime(time_t cltt, uint32_t valid_lft,
MYSQL_TIME& expire, uint32_t& lease_time) {
// Calculate expiry time and convert to various date/time fields.
time_t expire_time = cltt + valid_lft;
struct tm expire_tm;
(void) localtime_r(&expire_time, &expire_tm);
// Place in output expire structure.
expire.year = expire_tm.tm_year + 1900;
expire.month = expire_tm.tm_mon + 1; // Note different base
expire.day = expire_tm.tm_mday;
expire.hour = expire_tm.tm_hour;
expire.minute = expire_tm.tm_min;
expire.second = expire_tm.tm_sec;
expire.second_part = 0; // No fractional seconds
expire.neg = static_cast<my_bool>(0); // Not negative
// Set the lease time.
lease_time = valid_lft;
}
void
MySqlLeaseMgr::convertToLeaseTime(const MYSQL_TIME& expire, uint32_t lease_time,
time_t& cltt, uint32_t& valid_lft) {
valid_lft = lease_time;
// Copy across fields from MYSQL_TIME structure.
struct tm expire_tm;
expire_tm.tm_year = expire.year - 1900;
expire_tm.tm_mon = expire.month - 1;
expire_tm.tm_mday = expire.day;
expire_tm.tm_hour = expire.hour;
expire_tm.tm_min = expire.minute;
expire_tm.tm_sec = expire.second;
// Convert to local time
cltt = mktime(&expire_tm) - valid_lft;
}
void
MySqlLeaseMgr::openDatabase() {
......@@ -111,8 +161,18 @@ MySqlLeaseMgr::prepareStatements() {
raw_statements_.resize(NUM_STATEMENTS, std::string(""));
// Now allocate the statements
prepareStatement(SELECT_VERSION,
prepareStatement(GET_LEASE6,
"SELECT hwaddr, client_id, "
"lease_time, expire, subnet_id, pref_lifetime, "
"lease_type, iaid, prefix_len "
"FROM lease6 WHERE address = ?");
prepareStatement(GET_VERSION,
"SELECT version, minor FROM schema_version");
prepareStatement(INSERT_LEASE6,
"INSERT INTO lease6(address, hwaddr, client_id, "
"lease_time, expire, subnet_id, pref_lifetime, "
"lease_type, iaid, prefix_len) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
......@@ -152,23 +212,122 @@ MySqlLeaseMgr::~MySqlLeaseMgr() {
}
bool
MySqlLeaseMgr::addLease(Lease4Ptr /* lease */) {
MySqlLeaseMgr::addLease(const Lease4Ptr& /* lease */) {
return (false);
}
bool
MySqlLeaseMgr::addLease(Lease6Ptr /* lease */) {
return (false);
MySqlLeaseMgr::addLease(const Lease6Ptr& lease) {
my_bool MLM_FALSE = 0;
MYSQL_BIND bind[10];
memset(bind, 0, sizeof(bind));
// address: varchar(40)
std::string addr6 = lease->addr_.toText();
unsigned long addr6_length = addr6.size();
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = const_cast<char*>(addr6.c_str());
bind[0].buffer_length = addr6_length;
bind[0].length = &addr6_length;
bind[0].is_null = &MLM_FALSE;
// hwaddr: binary(20)
unsigned long hwaddr_length = lease->hwaddr_.size();
bind[1].buffer_type = MYSQL_TYPE_BLOB;
bind[1].buffer = reinterpret_cast<char*>(&(lease->hwaddr_[0]));
bind[1].buffer_length = hwaddr_length;
bind[1].length = &hwaddr_length;
bind[1].is_null = &MLM_FALSE;
// client_id: varchar(128)
vector<uint8_t> clientid = lease->duid_->getDuid();
unsigned long clientid_length = clientid.size();
bind[2].buffer_type = MYSQL_TYPE_BLOB;
bind[2].buffer = reinterpret_cast<char*>(&(clientid[0]));
bind[2].buffer_length = clientid_length;
bind[2].length = &clientid_length;
bind[2].is_null = &MLM_FALSE;
// The lease structure holds the client last transmission time (cltt_)
// and the valid lifetime (valid_lft_). For convenience, the data stored
// in the database is expiry time (expire) and lease time (lease+time).
// The relationship is given by:
//
// lease_time - valid_lft_
// expire = cltt_ + valid_lft_
MYSQL_TIME mysql_expire;
uint32_t lease_time;
convertFromLeaseTime(lease->cltt_, lease->valid_lft_,
mysql_expire, lease_time);
// lease_time: unsigned int
bind[3].buffer_type = MYSQL_TYPE_LONG;
bind[3].buffer = reinterpret_cast<char*>(&lease_time);
bind[3].is_unsigned = 1;
bind[3].is_null = &MLM_FALSE;
// expire: timestamp
bind[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
bind[4].buffer = reinterpret_cast<char*>(&mysql_expire);
bind[4].buffer_length = sizeof(mysql_expire);
bind[4].is_null = &MLM_FALSE;
// subnet_id: unsigned int
bind[5].buffer_type = MYSQL_TYPE_LONG;
bind[5].buffer = reinterpret_cast<char*>(&(lease->subnet_id_));
bind[5].is_unsigned = static_cast<my_bool>(1);
bind[5].is_null = &MLM_FALSE;
// pref_lifetime: unsigned int
bind[6].buffer_type = MYSQL_TYPE_LONG;
bind[6].buffer = reinterpret_cast<char*>(&(lease->preferred_lft_));
bind[6].is_unsigned = static_cast<my_bool>(1);
bind[6].is_null = &MLM_FALSE;
// lease_type: tinyint
uint8_t lease_type = lease->type_; // Needed for int -> uint8_t conversion
bind[7].buffer_type = MYSQL_TYPE_TINY;
bind[7].buffer = reinterpret_cast<char*>(&lease_type);
bind[7].is_unsigned = static_cast<my_bool>(1);
bind[7].is_null = &MLM_FALSE;
// iaid: unsigned int
bind[8].buffer_type = MYSQL_TYPE_LONG;
bind[8].buffer = reinterpret_cast<char*>(&(lease->iaid_));
bind[8].is_unsigned = static_cast<my_bool>(1);
bind[8].is_null = &MLM_FALSE;
// prefix_len: unsigned tinyint
bind[9].buffer_type = MYSQL_TYPE_TINY;
bind[9].buffer = reinterpret_cast<char*>(&(lease->prefixlen_));
bind[9].is_unsigned = static_cast<my_bool>(1);
bind[9].is_null = &MLM_FALSE;
// Bind the parameters to the statement
my_bool status = mysql_stmt_bind_param(statements_[INSERT_LEASE6], bind);
checkError(status, INSERT_LEASE6, "unable to bind parameters");
// Execute the statement
status = mysql_stmt_execute(statements_[INSERT_LEASE6]);
checkError(status, INSERT_LEASE6, "unable to execute");
// ... and find out whether a row as inserted.
return (mysql_stmt_affected_rows(statements_[INSERT_LEASE6]) == 1);
}
Lease4Ptr
MySqlLeaseMgr::getLease4(isc::asiolink::IOAddress /* addr */,
MySqlLeaseMgr::getLease4(const isc::asiolink::IOAddress& /* addr */,
SubnetID /* subnet_id */) const {
return (Lease4Ptr());
}
Lease4Ptr
MySqlLeaseMgr::getLease4(isc::asiolink::IOAddress /* addr */) const {
MySqlLeaseMgr::getLease4(const isc::asiolink::IOAddress& /* addr */) const {
return (Lease4Ptr());
}
......@@ -195,8 +354,147 @@ MySqlLeaseMgr::getLease4(const ClientId& /* clientid */,
}
Lease6Ptr
MySqlLeaseMgr::getLease6(isc::asiolink::IOAddress /* addr */) const {
return (Lease6Ptr());
MySqlLeaseMgr::getLease6(const isc::asiolink::IOAddress& addr) const {
my_bool MLM_FALSE = 0; // MySqlLeaseMgr false
my_bool MLM_TRUE = 1; // MySqlLeaseMgr true
// Set up the WHERE clause value
MYSQL_BIND inbind[1];
memset(inbind, 0, sizeof(inbind));
std::string addr6 = addr.toText();
unsigned long addr6_length = addr6.size();
inbind[0].buffer_type = MYSQL_TYPE_STRING;
inbind[0].buffer = const_cast<char*>(addr6.c_str());
inbind[0].buffer_length = addr6_length;
inbind[0].length = &addr6_length;
inbind[0].is_null = &MLM_FALSE;
// Bind the parameters to the statement
my_bool status = mysql_stmt_bind_param(statements_[GET_LEASE6], inbind);
checkError(status, GET_LEASE6, "unable to bind WHERE clause parameter");
// Output values
MYSQL_BIND outbind[9];
memset(outbind, 0, sizeof(outbind));
// address: Not obtained - because of the WHERE clause, it will always be
// the same as the input parameter.
// hwaddr: varbinary(20)
uint8_t hwaddr[20];
unsigned long hwaddr_length;
outbind[0].buffer_type = MYSQL_TYPE_BLOB;
outbind[0].buffer = reinterpret_cast<char*>(hwaddr);
outbind[0].buffer_length = sizeof(hwaddr);
outbind[0].length = &hwaddr_length;
// client_id: varbinary(128)
uint8_t clientid[128];
unsigned long clientid_length;
outbind[1].buffer_type = MYSQL_TYPE_BLOB;
outbind[1].buffer = reinterpret_cast<char*>(clientid);
outbind[1].buffer_length = sizeof(clientid);
outbind[1].length = &clientid_length;
// lease_time: unsigned int
unsigned lease_time;
outbind[2].buffer_type = MYSQL_TYPE_LONG;
outbind[2].buffer = reinterpret_cast<char*>(&lease_time);
outbind[2].is_unsigned = MLM_TRUE;
// expire: timestamp
MYSQL_TIME mysql_expire;
outbind[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
outbind[3].buffer = reinterpret_cast<char*>(&mysql_expire);
outbind[3].buffer_length = sizeof(mysql_expire);
// subnet_id: unsigned int
unsigned subnet_id;
outbind[4].buffer_type = MYSQL_TYPE_LONG;
outbind[4].buffer = reinterpret_cast<char*>(&subnet_id);
outbind[4].is_unsigned = MLM_TRUE;
// pref_lifetime: unsigned int
unsigned pref_lifetime;
outbind[5].buffer_type = MYSQL_TYPE_LONG;
outbind[5].buffer = reinterpret_cast<char*>(&pref_lifetime);
outbind[5].is_unsigned = MLM_TRUE;
// lease_type: tinyint
uint8_t lease_type;
outbind[6].buffer_type = MYSQL_TYPE_TINY;
outbind[6].buffer = reinterpret_cast<char*>(&lease_type);
outbind[6].is_unsigned = MLM_TRUE;
// iaid: unsigned int
unsigned iaid;
outbind[7].buffer_type = MYSQL_TYPE_LONG;
outbind[7].buffer = reinterpret_cast<char*>(&iaid);
outbind[7].is_unsigned = MLM_TRUE;
// prefix_len: unsigned tinyint
uint8_t prefixlen;
outbind[8].buffer_type = MYSQL_TYPE_TINY;
outbind[8].buffer = reinterpret_cast<char*>(&prefixlen);
outbind[8].is_unsigned = MLM_TRUE;
// Bind the parameters to the statement
status = mysql_stmt_bind_result(statements_[GET_LEASE6], outbind);
checkError(status, GET_LEASE6, "unable to bind SELECT caluse parameters");
// Execute the statement
status = mysql_stmt_execute(statements_[GET_LEASE6]);
checkError(status, GET_LEASE6, "unable to execute");
// Fetch the data.
Lease6Ptr result(new Lease6());
status = mysql_stmt_fetch(statements_[GET_LEASE6]);
if (status == 0) {
// Success - put the data in the lease object
result->addr_ = addr;
result->hwaddr_ = vector<uint8_t>(&hwaddr[0], &hwaddr[hwaddr_length]);
result->duid_.reset(new DUID(clientid, clientid_length));
convertToLeaseTime(mysql_expire, lease_time,
result->cltt_, result->valid_lft_);
result->subnet_id_ = subnet_id;
result->preferred_lft_ = pref_lifetime;
switch (lease_type) {
case Lease6::LEASE_IA_NA:
result->type_ = Lease6::LEASE_IA_NA;
break;
case Lease6::LEASE_IA_TA:
result->type_ = Lease6::LEASE_IA_TA;
break;
case Lease6::LEASE_IA_PD:
result->type_ = Lease6::LEASE_IA_PD;
break;
default:
isc_throw(BadValue, "invalid lease type returned for <" <<
raw_statements_[GET_LEASE6] << ">");
}
result->iaid_ = iaid;
result->prefixlen_ = prefixlen;
// As the address is the primary key in the table, we can't return
// two rows, so we don't bother checking.
} else if (status == 1) {
checkError(status, GET_LEASE6, "unable to fetch results");
} else {
// We are ignoring truncation for now, so the only other result is
// no data was found. In that case, we returrn a null Lease6 structure.
// This has already been set, so ther action is a no-op.
}
return (result);
}
Lease6Collection
......@@ -211,20 +509,20 @@ MySqlLeaseMgr::getLease6(const DUID& /* duid */, uint32_t /* iaid */,
}
void
MySqlLeaseMgr::updateLease4(Lease4Ptr /* lease4 */) {
MySqlLeaseMgr::updateLease4(const Lease4Ptr& /* lease4 */) {
}
void
MySqlLeaseMgr::updateLease6(Lease6Ptr /* lease6 */) {
MySqlLeaseMgr::updateLease6(const Lease6Ptr& /* lease6 */) {
}
bool
MySqlLeaseMgr::deleteLease4(uint32_t /* addr */) {
MySqlLeaseMgr::deleteLease4(const isc::asiolink::IOAddress& /* addr */) {
return (false);
}
bool
MySqlLeaseMgr::deleteLease6(isc::asiolink::IOAddress /* addr */) {
MySqlLeaseMgr::deleteLease6(const isc::asiolink::IOAddress& /* addr */) {
return (false);
}
......@@ -244,10 +542,10 @@ MySqlLeaseMgr::getVersion() const {
uint32_t minor; // Minor version number
// Execute the prepared statement
int status = mysql_stmt_execute(statements_[SELECT_VERSION]);
int status = mysql_stmt_execute(statements_[GET_VERSION]);
if (status != 0) {
isc_throw(DbOperationError, "unable to execute <"
<< raw_statements_[SELECT_VERSION] << "> - reason: " <<
<< raw_statements_[GET_VERSION] << "> - reason: " <<
mysql_error(mysql_));
}
......@@ -265,14 +563,14 @@ MySqlLeaseMgr::getVersion() const {
bind[1].buffer = &minor;
bind[1].buffer_length = sizeof(minor);
status = mysql_stmt_bind_result(statements_[SELECT_VERSION], bind);
status = mysql_stmt_bind_result(statements_[GET_VERSION], bind);
if (status != 0) {
isc_throw(DbOperationError, "unable to bind result set: " <<
mysql_error(mysql_));
}
// Get the result
status = mysql_stmt_fetch(statements_[SELECT_VERSION]);
status = mysql_stmt_fetch(statements_[GET_VERSION]);
if (status != 0) {
isc_throw(DbOperationError, "unable to obtain result set: " <<
mysql_error(mysql_));
......@@ -281,5 +579,19 @@ MySqlLeaseMgr::getVersion() const {
return (std::make_pair(major, minor));
}
void
MySqlLeaseMgr::commit() {
if (mysql_commit(mysql_) != 0) {
isc_throw(DbOperationError, "commit failed: " << mysql_error(mysql_));
}
}
void
MySqlLeaseMgr::rollback() {
if (mysql_rollback(mysql_) != 0) {
isc_throw(DbOperationError, "rollback failed: " << mysql_error(mysql_));
}
}
}; // end of isc::dhcp namespace
}; // end of isc namespace
......@@ -15,6 +15,7 @@
#ifndef __MYSQL_LEASE_MGR_H
#define __MYSQL_LEASE_MGR_H
#include <time.h>
#include <mysql.h>
#include <dhcp/lease_mgr.h>
......@@ -51,12 +52,22 @@ public:
/// @brief Adds an IPv4 lease.
///
/// @param lease lease to be added
virtual bool addLease(Lease4Ptr lease);
///
/// @result true if the lease was added, false if not (because a lease
/// with the same address was already there).
///
/// @exception DbOperationError Database function failed
virtual bool addLease(const Lease4Ptr& lease);
/// @brief Adds an IPv6 lease.
///
/// @param lease lease to be added
virtual bool addLease(Lease6Ptr lease);
///
/// @result true if the lease was added, false if not (because a lease
/// with the same address was already there).
///
/// @exception DbOperationError Database function failed
virtual bool addLease(const Lease6Ptr& lease);
/// @brief Returns existing IPv4 lease for specified IPv4 address and subnet_id
///
......@@ -68,7 +79,7 @@ public:
/// @param subnet_id ID of the subnet the lease must belong to
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr,
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr,
SubnetID subnet_id) const;
/// @brief Returns an IPv4 lease for specified IPv4 address
......@@ -84,7 +95,7 @@ public:
/// @param subnet_id ID of the subnet the lease must belong to
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const;
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
/// @brief Returns existing IPv4 leases for specified hardware address.