2015-03-04 16:19:36 +01:00
|
|
|
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
|
2012-10-16 16:39:32 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2012-10-29 13:43:31 +00:00
|
|
|
#ifndef MYSQL_LEASE_MGR_H
|
|
|
|
#define MYSQL_LEASE_MGR_H
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2013-01-10 16:32:24 +01:00
|
|
|
#include <dhcp/hwaddr.h>
|
2012-11-16 11:19:19 +00:00
|
|
|
#include <dhcpsrv/lease_mgr.h>
|
2015-04-17 13:39:45 +02:00
|
|
|
#include <dhcpsrv/mysql_connection.h>
|
2012-11-16 11:19:19 +00:00
|
|
|
|
2012-11-06 12:58:10 +00:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2013-03-07 23:00:23 +00:00
|
|
|
#include <boost/utility.hpp>
|
2015-12-01 09:50:22 +01:00
|
|
|
#include <mysql.h>
|
2012-11-16 11:19:19 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace dhcp {
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
// Forward declaration of the Lease exchange objects. These classes are defined
|
2012-11-06 12:58:10 +00:00
|
|
|
// in the .cc file.
|
2012-11-21 12:44:18 +00:00
|
|
|
class MySqlLease4Exchange;
|
2012-11-06 12:58:10 +00:00
|
|
|
class MySqlLease6Exchange;
|
|
|
|
|
|
|
|
|
2012-10-24 19:18:33 +01:00
|
|
|
/// @brief MySQL Lease Manager
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
2012-11-26 19:17:27 +00:00
|
|
|
/// This class provides the \ref isc::dhcp::LeaseMgr interface to the MySQL
|
|
|
|
/// database. Use of this backend presupposes that a MySQL database is
|
|
|
|
/// available and that the Kea schema has been created within it.
|
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
class MySqlLeaseMgr : public LeaseMgr {
|
|
|
|
public:
|
2015-04-17 13:39:45 +02:00
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @brief Constructor
|
|
|
|
///
|
|
|
|
/// Uses the following keywords in the parameters passed to it to
|
|
|
|
/// connect to the database:
|
2012-10-24 19:18:33 +01:00
|
|
|
/// - name - Name of the database to which to connect (mandatory)
|
|
|
|
/// - host - Host to which to connect (optional, defaults to "localhost")
|
|
|
|
/// - user - Username under which to connect (optional)
|
|
|
|
/// - password - Password for "user" on the database (optional)
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
|
|
|
/// If the database is successfully opened, the version number in the
|
|
|
|
/// schema_version table will be checked against hard-coded value in
|
|
|
|
/// the implementation file.
|
|
|
|
///
|
|
|
|
/// Finally, all the SQL commands are pre-compiled.
|
|
|
|
///
|
|
|
|
/// @param parameters A data structure relating keywords and values
|
|
|
|
/// concerned with the database.
|
2012-10-24 19:18:33 +01:00
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
|
|
|
|
/// @throw isc::dhcp::DbOpenError Error opening the database
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2015-08-21 18:27:41 +02:00
|
|
|
MySqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Destructor (closes database)
|
|
|
|
virtual ~MySqlLeaseMgr();
|
|
|
|
|
2015-06-19 14:30:51 +02:00
|
|
|
/// @brief Local version of getDBVersion() class method
|
|
|
|
static std::string getDBVersion();
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Adds an IPv4 lease
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
|
|
|
/// @param lease lease to be added
|
2012-10-20 22:52:25 +01:00
|
|
|
///
|
|
|
|
/// @result true if the lease was added, false if not (because a lease
|
|
|
|
/// with the same address was already there).
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual bool addLease(const Lease4Ptr& lease);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Adds an IPv6 lease
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
|
|
|
/// @param lease lease to be added
|
2012-10-20 22:52:25 +01:00
|
|
|
///
|
|
|
|
/// @result true if the lease was added, false if not (because a lease
|
|
|
|
/// with the same address was already there).
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual bool addLease(const Lease6Ptr& lease);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns an IPv4 lease for specified IPv4 address
|
|
|
|
///
|
|
|
|
/// This method return a lease that is associated with a given address.
|
2012-12-07 11:18:47 +00:00
|
|
|
/// For other query types (by hardware addr, by Client ID) there can be
|
2012-10-16 16:39:32 +01:00
|
|
|
/// several leases in different subnets (e.g. for mobile clients that
|
|
|
|
/// got address in different subnets). However, for a single address
|
|
|
|
/// there can be only one lease, so this method returns a pointer to
|
|
|
|
/// a single lease, not a container of leases.
|
|
|
|
///
|
|
|
|
/// @param addr address of the searched lease
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-10-22 16:07:34 +01:00
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @brief Returns existing IPv4 leases for specified hardware address.
|
|
|
|
///
|
|
|
|
/// Although in the usual case there will be only one lease, for mobile
|
|
|
|
/// clients or clients with multiple static/fixed/reserved leases there
|
|
|
|
/// can be more than one. Thus return type is a container, not a single
|
|
|
|
/// pointer.
|
|
|
|
///
|
|
|
|
/// @param hwaddr hardware address of the client
|
|
|
|
///
|
|
|
|
/// @return lease collection
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2013-01-10 16:32:24 +01:00
|
|
|
virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv4 leases for specified hardware address
|
|
|
|
/// and a subnet
|
|
|
|
///
|
|
|
|
/// There can be at most one lease for a given HW address in a single
|
|
|
|
/// pool, so this method with either return a single lease or NULL.
|
|
|
|
///
|
|
|
|
/// @param hwaddr hardware address of the client
|
|
|
|
/// @param subnet_id identifier of the subnet that lease must belong to
|
|
|
|
///
|
|
|
|
/// @return a pointer to the lease (or NULL if a lease is not found)
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2013-01-10 16:32:24 +01:00
|
|
|
virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
|
2012-10-17 18:37:22 +01:00
|
|
|
SubnetID subnet_id) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv4 lease for specified client-id
|
|
|
|
///
|
|
|
|
/// Although in the usual case there will be only one lease, for mobile
|
|
|
|
/// clients or clients with multiple static/fixed/reserved leases there
|
|
|
|
/// can be more than one. Thus return type is a container, not a single
|
|
|
|
/// pointer.
|
|
|
|
///
|
|
|
|
/// @param clientid client identifier
|
|
|
|
///
|
|
|
|
/// @return lease collection
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual Lease4Collection getLease4(const ClientId& clientid) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2013-11-15 12:03:21 +01:00
|
|
|
/// @brief Returns IPv4 lease for the specified client identifier, HW
|
|
|
|
/// address and subnet identifier.
|
|
|
|
///
|
|
|
|
/// @param client_id A client identifier.
|
|
|
|
/// @param hwaddr Hardware address.
|
|
|
|
/// @param subnet_id A subnet identifier.
|
|
|
|
///
|
|
|
|
/// @return A pointer to the lease or NULL if the lease is not found.
|
|
|
|
/// @throw isc::NotImplemented On every call as this function is currently
|
|
|
|
/// not implemented for the MySQL backend.
|
|
|
|
virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
|
|
|
|
SubnetID subnet_id) const;
|
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @brief Returns existing IPv4 lease for specified client-id
|
|
|
|
///
|
|
|
|
/// There can be at most one lease for a given HW address in a single
|
|
|
|
/// pool, so this method with either return a single lease or NULL.
|
|
|
|
///
|
|
|
|
/// @param clientid client identifier
|
|
|
|
/// @param subnet_id identifier of the subnet that lease must belong to
|
|
|
|
///
|
|
|
|
/// @return a pointer to the lease (or NULL if a lease is not found)
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-16 16:39:32 +01:00
|
|
|
virtual Lease4Ptr getLease4(const ClientId& clientid,
|
2012-10-17 18:37:22 +01:00
|
|
|
SubnetID subnet_id) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv6 lease for a given IPv6 address.
|
|
|
|
///
|
|
|
|
/// For a given address, we assume that there will be only one lease.
|
2013-03-06 08:41:51 -06:00
|
|
|
/// The assumption here is that there will not be site or link-local
|
2012-10-16 16:39:32 +01:00
|
|
|
/// addresses used, so there is no way of having address duplication.
|
|
|
|
///
|
2013-09-06 14:51:59 +02:00
|
|
|
/// @param type specifies lease type: (NA, TA or PD)
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @param addr address of the searched lease
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
2012-10-24 19:18:33 +01:00
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::BadValue record retrieved from database had an invalid
|
|
|
|
/// lease type field.
|
2012-11-30 15:49:42 +00:00
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2013-09-18 15:44:48 +02:00
|
|
|
virtual Lease6Ptr getLease6(Lease::Type type,
|
2013-09-06 14:51:59 +02:00
|
|
|
const isc::asiolink::IOAddress& addr) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv6 leases for a given DUID+IA combination
|
|
|
|
///
|
|
|
|
/// Although in the usual case there will be only one lease, for mobile
|
|
|
|
/// clients or clients with multiple static/fixed/reserved leases there
|
|
|
|
/// can be more than one. Thus return type is a container, not a single
|
|
|
|
/// pointer.
|
|
|
|
///
|
2013-09-06 14:51:59 +02:00
|
|
|
/// @param type specifies lease type: (NA, TA or PD)
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @param duid client DUID
|
|
|
|
/// @param iaid IA identifier
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::BadValue record retrieved from database had an invalid
|
|
|
|
/// lease type field.
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2013-09-18 15:44:48 +02:00
|
|
|
virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
|
2013-09-06 19:30:35 +02:00
|
|
|
uint32_t iaid) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv6 lease for a given DUID+IA combination
|
|
|
|
///
|
2013-09-06 14:51:59 +02:00
|
|
|
/// @param type specifies lease type: (NA, TA or PD)
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @param duid client DUID
|
|
|
|
/// @param iaid IA identifier
|
|
|
|
/// @param subnet_id subnet id of the subnet the lease belongs to
|
|
|
|
///
|
2013-09-06 18:16:50 +02:00
|
|
|
/// @return lease collection (may be empty if no lease is found)
|
2012-11-30 15:49:42 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::BadValue record retrieved from database had an invalid
|
|
|
|
/// lease type field.
|
|
|
|
/// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
|
|
|
|
/// fit into the space allocated for the result. This indicates a
|
|
|
|
/// programming error.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2013-09-18 15:44:48 +02:00
|
|
|
virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
|
2013-09-06 18:16:50 +02:00
|
|
|
uint32_t iaid, SubnetID subnet_id) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2015-08-20 19:25:38 +02:00
|
|
|
/// @brief Returns a collection of expired DHCPv6 leases.
|
|
|
|
///
|
|
|
|
/// This method returns at most @c max_leases expired leases. The leases
|
|
|
|
/// returned haven't been reclaimed, i.e. the database query must exclude
|
|
|
|
/// reclaimed leases from the results returned.
|
|
|
|
///
|
|
|
|
/// @param [out] expired_leases A container to which expired leases returned
|
|
|
|
/// by the database backend are added.
|
|
|
|
/// @param max_leases A maximum number of leases to be returned. If this
|
|
|
|
/// value is set to 0, all expired (but not reclaimed) leases are returned.
|
|
|
|
virtual void getExpiredLeases6(Lease6Collection& expired_leases,
|
|
|
|
const size_t max_leases) const;
|
|
|
|
|
|
|
|
|
|
|
|
/// @brief Returns a collection of expired DHCPv4 leases.
|
|
|
|
///
|
|
|
|
/// This method returns at most @c max_leases expired leases. The leases
|
|
|
|
/// returned haven't been reclaimed, i.e. the database query must exclude
|
|
|
|
/// reclaimed leases from the results returned.
|
|
|
|
///
|
|
|
|
/// @param [out] expired_leases A container to which expired leases returned
|
|
|
|
/// by the database backend are added.
|
|
|
|
/// @param max_leases A maximum number of leases to be returned. If this
|
|
|
|
/// value is set to 0, all expired (but not reclaimed) leases are returned.
|
|
|
|
virtual void getExpiredLeases4(Lease4Collection& expired_leases,
|
|
|
|
const size_t max_leases) const;
|
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @brief Updates IPv4 lease.
|
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// Updates the record of the lease in the database (as identified by the
|
|
|
|
/// address) with the data in the passed lease object.
|
|
|
|
///
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @param lease4 The lease to be updated.
|
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
|
|
|
|
/// exist.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual void updateLease4(const Lease4Ptr& lease4);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-11-08 13:14:19 +00:00
|
|
|
/// @brief Updates IPv6 lease.
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// Updates the record of the lease in the database (as identified by the
|
|
|
|
/// address) with the data in the passed lease object.
|
|
|
|
///
|
2012-11-08 13:14:19 +00:00
|
|
|
/// @param lease6 The lease to be updated.
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
|
|
|
|
/// exist.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual void updateLease6(const Lease6Ptr& lease6);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-12-10 11:21:24 +00:00
|
|
|
/// @brief Deletes a lease.
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
2012-12-10 11:21:24 +00:00
|
|
|
/// @param addr Address of the lease to be deleted. This can be an IPv4
|
|
|
|
/// address or an IPv6 address.
|
2012-10-16 16:39:32 +01:00
|
|
|
///
|
|
|
|
/// @return true if deletion was successful, false if no such lease exists
|
2012-10-24 19:18:33 +01:00
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-12-10 11:21:24 +00:00
|
|
|
virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2015-08-20 19:25:38 +02:00
|
|
|
/// @brief Deletes all expired-reclaimed DHCPv4 leases.
|
|
|
|
///
|
|
|
|
/// @param secs Number of seconds since expiration of leases before
|
|
|
|
/// they can be removed. Leases which have expired later than this
|
|
|
|
/// time will not be deleted.
|
2015-09-03 11:35:25 +02:00
|
|
|
///
|
|
|
|
/// @return Number of leases deleted.
|
|
|
|
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
|
2015-08-20 19:25:38 +02:00
|
|
|
|
|
|
|
/// @brief Deletes all expired-reclaimed DHCPv6 leases.
|
|
|
|
///
|
|
|
|
/// @param secs Number of seconds since expiration of leases before
|
|
|
|
/// they can be removed. Leases which have expired later than this
|
|
|
|
/// time will not be deleted.
|
2015-09-03 11:35:25 +02:00
|
|
|
///
|
|
|
|
/// @return Number of leases deleted.
|
|
|
|
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
|
2015-08-20 19:25:38 +02:00
|
|
|
|
2012-11-13 12:14:55 +00:00
|
|
|
/// @brief Return backend type
|
|
|
|
///
|
|
|
|
/// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
|
|
|
|
///
|
|
|
|
/// @return Type of the backend.
|
|
|
|
virtual std::string getType() const {
|
|
|
|
return (std::string("mysql"));
|
|
|
|
}
|
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
/// @brief Returns backend name.
|
|
|
|
///
|
|
|
|
/// Each backend have specific name, e.g. "mysql" or "sqlite".
|
2012-11-13 12:14:55 +00:00
|
|
|
///
|
|
|
|
/// @return Name of the backend.
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual std::string getName() const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns description of the backend.
|
|
|
|
///
|
|
|
|
/// This description may be multiline text that describes the backend.
|
2012-11-13 12:14:55 +00:00
|
|
|
///
|
|
|
|
/// @return Description of the backend.
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual std::string getDescription() const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns backend version.
|
|
|
|
///
|
|
|
|
/// @return Version number as a pair of unsigned integers. "first" is the
|
|
|
|
/// major version number, "second" the minor number.
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-10-16 16:39:32 +01:00
|
|
|
virtual std::pair<uint32_t, uint32_t> getVersion() const;
|
|
|
|
|
2012-10-20 22:52:25 +01:00
|
|
|
/// @brief Commit Transactions
|
|
|
|
///
|
|
|
|
/// Commits all pending database operations. On databases that don't
|
|
|
|
/// support transactions, this is a no-op.
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw DbOperationError Iif the commit failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual void commit();
|
|
|
|
|
|
|
|
/// @brief Rollback Transactions
|
|
|
|
///
|
|
|
|
/// Rolls back all pending database operations. On databases that don't
|
|
|
|
/// support transactions, this is a no-op.
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw DbOperationError If the rollback failed.
|
2012-10-20 22:52:25 +01:00
|
|
|
virtual void rollback();
|
|
|
|
|
2012-10-22 10:49:51 +01:00
|
|
|
/// @brief Statement Tags
|
2012-10-18 11:33:51 +01:00
|
|
|
///
|
2012-10-22 10:49:51 +01:00
|
|
|
/// The contents of the enum are indexes into the list of SQL statements
|
2012-10-18 11:33:51 +01:00
|
|
|
enum StatementIndex {
|
2015-09-07 13:58:22 +02:00
|
|
|
DELETE_LEASE4, // Delete from lease4 by address
|
|
|
|
DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
|
|
|
|
DELETE_LEASE6, // Delete from lease6 by address
|
|
|
|
DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
|
|
|
|
GET_LEASE4_ADDR, // Get lease4 by address
|
|
|
|
GET_LEASE4_CLIENTID, // Get lease4 by client ID
|
|
|
|
GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
|
|
|
|
GET_LEASE4_HWADDR, // Get lease4 by HW address
|
|
|
|
GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
|
|
|
|
GET_LEASE4_EXPIRE, // Get lease4 by expiration.
|
|
|
|
GET_LEASE6_ADDR, // Get lease6 by address
|
|
|
|
GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
|
|
|
|
GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
|
|
|
|
GET_LEASE6_EXPIRE, // Get lease6 by expiration.
|
|
|
|
GET_VERSION, // Obtain version number
|
|
|
|
INSERT_LEASE4, // Add entry to lease4 table
|
|
|
|
INSERT_LEASE6, // Add entry to lease6 table
|
|
|
|
UPDATE_LEASE4, // Update a Lease4 entry
|
|
|
|
UPDATE_LEASE6, // Update a Lease6 entry
|
|
|
|
NUM_STATEMENTS // Number of statements
|
2012-10-18 11:33:51 +01:00
|
|
|
};
|
|
|
|
|
2012-11-04 20:20:21 +00:00
|
|
|
private:
|
2012-11-21 12:44:18 +00:00
|
|
|
/// @brief Add Lease Common Code
|
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// This method performs the common actions for both flavours (V4 and V6)
|
|
|
|
/// of the addLease method. It binds the contents of the lease object to
|
|
|
|
/// the prepared statement and adds it to the database.
|
2012-11-21 12:44:18 +00:00
|
|
|
///
|
|
|
|
/// @param stindex Index of statemnent being executed
|
|
|
|
/// @param bind MYSQL_BIND array that has been created for the type
|
|
|
|
/// of lease in question.
|
|
|
|
///
|
|
|
|
/// @return true if the lease was added, false if it was not added because
|
|
|
|
/// a lease with that address already exists in the database.
|
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-11-26 19:17:27 +00:00
|
|
|
bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
|
2012-11-21 12:44:18 +00:00
|
|
|
|
2012-11-23 12:05:55 +00:00
|
|
|
/// @brief Get Lease Collection Common Code
|
|
|
|
///
|
|
|
|
/// This method performs the common actions for obtaining multiple leases
|
|
|
|
/// from the database.
|
|
|
|
///
|
|
|
|
/// @param stindex Index of statement being executed
|
2012-11-26 19:17:27 +00:00
|
|
|
/// @param bind MYSQL_BIND array for input parameters
|
2012-11-23 12:05:55 +00:00
|
|
|
/// @param exchange Exchange object to use
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @param lease LeaseCollection object returned. Note that any leases in
|
|
|
|
/// the collection when this method is called are not erased: the
|
|
|
|
/// new data is appended to the end.
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param single If true, only a single data item is to be retrieved.
|
|
|
|
/// If more than one is present, a MultipleRecords exception will
|
|
|
|
/// be thrown.
|
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
|
|
|
/// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
|
|
|
|
/// from the database where only one was expected.
|
2012-11-23 12:05:55 +00:00
|
|
|
template <typename Exchange, typename LeaseCollection>
|
2012-11-26 19:17:27 +00:00
|
|
|
void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
|
2012-11-23 18:23:21 +00:00
|
|
|
Exchange& exchange, LeaseCollection& result,
|
|
|
|
bool single = false) const;
|
2012-11-23 12:05:55 +00:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Get Lease Collection
|
2012-11-06 12:58:10 +00:00
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// Gets a collection of Lease4 objects. This is just an interface to
|
|
|
|
/// the get lease collection common code.
|
2012-11-06 12:58:10 +00:00
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param stindex Index of statement being executed
|
2012-11-26 19:17:27 +00:00
|
|
|
/// @param bind MYSQL_BIND array for input parameters
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @param lease LeaseCollection object returned. Note that any leases in
|
|
|
|
/// the collection when this method is called are not erased: the
|
|
|
|
/// new data is appended to the end.
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
|
|
|
/// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
|
|
|
|
/// from the database where only one was expected.
|
2012-11-26 19:17:27 +00:00
|
|
|
void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
|
2012-11-23 18:23:21 +00:00
|
|
|
Lease4Collection& result) const {
|
2012-11-26 19:17:27 +00:00
|
|
|
getLeaseCollection(stindex, bind, exchange4_, result);
|
2012-11-23 18:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Get Lease Collection
|
|
|
|
///
|
|
|
|
/// Gets a collection of Lease6 objects. This is just an interface to
|
|
|
|
/// the get lease collection common code.
|
|
|
|
///
|
|
|
|
/// @param stindex Index of statement being executed
|
2012-11-26 19:17:27 +00:00
|
|
|
/// @param bind MYSQL_BIND array for input parameters
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @param lease LeaseCollection object returned. Note that any existing
|
|
|
|
/// data in the collection is erased first.
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
|
|
|
/// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
|
|
|
|
/// from the database where only one was expected.
|
2012-11-26 19:17:27 +00:00
|
|
|
void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
|
2012-11-23 18:23:21 +00:00
|
|
|
Lease6Collection& result) const {
|
2012-11-26 19:17:27 +00:00
|
|
|
getLeaseCollection(stindex, bind, exchange6_, result);
|
2012-11-23 18:23:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @brief Get Lease4 Common Code
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// This method performs the common actions for the various getLease4()
|
|
|
|
/// methods. It acts as an interface to the getLeaseCollection() method,
|
|
|
|
/// but retrieveing only a single lease.
|
|
|
|
///
|
|
|
|
/// @param stindex Index of statement being executed
|
2012-11-26 19:17:27 +00:00
|
|
|
/// @param bind MYSQL_BIND array for input parameters
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param lease Lease4 object returned
|
2012-11-26 19:17:27 +00:00
|
|
|
void getLease(StatementIndex stindex, MYSQL_BIND* bind,
|
2012-11-23 18:23:21 +00:00
|
|
|
Lease4Ptr& result) const;
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @brief Get Lease6 Common Code
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// This method performs the common actions for the various getLease46)
|
2012-11-23 18:23:21 +00:00
|
|
|
/// methods. It acts as an interface to the getLeaseCollection() method,
|
|
|
|
/// but retrieveing only a single lease.
|
|
|
|
///
|
|
|
|
/// @param stindex Index of statement being executed
|
2012-11-26 19:17:27 +00:00
|
|
|
/// @param bind MYSQL_BIND array for input parameters
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param lease Lease6 object returned
|
2012-11-26 19:17:27 +00:00
|
|
|
void getLease(StatementIndex stindex, MYSQL_BIND* bind,
|
2012-11-23 18:23:21 +00:00
|
|
|
Lease6Ptr& result) const;
|
|
|
|
|
2015-09-07 13:29:17 +02:00
|
|
|
|
|
|
|
/// @brief Get expired leases common code.
|
|
|
|
///
|
|
|
|
/// This method retrieves expired and not reclaimed leases from the
|
|
|
|
/// lease database. The returned leases are ordered by the expiration
|
|
|
|
/// time. The maximum number of leases to be returned is specified
|
|
|
|
/// as an argument.
|
|
|
|
///
|
|
|
|
/// @param [out] expired_leases Reference to the container where the
|
|
|
|
/// retrieved leases are put.
|
|
|
|
/// @param max_leases Maximum number of leases to be returned.
|
|
|
|
/// @param statement_index One of the @c GET_LEASE4_EXPIRE or
|
|
|
|
/// @c GET_LEASE6_EXPIRE.
|
|
|
|
///
|
|
|
|
/// @tparam One of the @c Lease4Collection or @c Lease6Collection.
|
|
|
|
template<typename LeaseCollection>
|
|
|
|
void getExpiredLeasesCommon(LeaseCollection& expired_leases,
|
|
|
|
const size_t max_leases,
|
|
|
|
StatementIndex statement_index) const;
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Update lease common code
|
|
|
|
///
|
|
|
|
/// Holds the common code for updating a lease. It binds the parameters
|
|
|
|
/// to the prepared statement, executes it, then checks how many rows
|
|
|
|
/// were affected.
|
2012-11-06 12:58:10 +00:00
|
|
|
///
|
|
|
|
/// @param stindex Index of prepared statement to be executed
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param bind Array of MYSQL_BIND objects representing the parameters.
|
2012-11-06 12:58:10 +00:00
|
|
|
/// (Note that the number is determined by the number of parameters
|
|
|
|
/// in the statement.)
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @param lease Pointer to the lease object whose record is being updated.
|
2012-11-06 12:58:10 +00:00
|
|
|
///
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @throw NoSuchLease Could not update a lease because no lease matches
|
|
|
|
/// the address given.
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2012-11-23 18:23:21 +00:00
|
|
|
template <typename LeasePtr>
|
2012-11-26 19:17:27 +00:00
|
|
|
void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
|
|
|
|
const LeasePtr& lease);
|
2012-11-23 18:23:21 +00:00
|
|
|
|
|
|
|
/// @brief Delete lease common code
|
|
|
|
///
|
|
|
|
/// Holds the common code for deleting a lease. It binds the parameters
|
|
|
|
/// to the prepared statement, executes the statement and checks to
|
|
|
|
/// see how many rows were deleted.
|
|
|
|
///
|
|
|
|
/// @param stindex Index of prepared statement to be executed
|
|
|
|
/// @param bind Array of MYSQL_BIND objects representing the parameters.
|
|
|
|
/// (Note that the number is determined by the number of parameters
|
|
|
|
/// in the statement.)
|
|
|
|
///
|
2015-09-07 13:58:22 +02:00
|
|
|
/// @return Number of deleted leases.
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2015-09-07 13:58:22 +02:00
|
|
|
uint64_t deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
|
|
|
|
|
|
|
|
/// @brief Delete expired-reclaimed leases.
|
|
|
|
///
|
|
|
|
/// @param secs Number of seconds since expiration of leases before
|
|
|
|
/// they can be removed. Leases which have expired later than this
|
|
|
|
/// time will not be deleted.
|
|
|
|
/// @param statement_index One of the @c DELETE_LEASE4_STATE_EXPIRED or
|
|
|
|
/// @c DELETE_LEASE6_STATE_EXPIRED.
|
|
|
|
///
|
|
|
|
/// @return Number of leases deleted.
|
|
|
|
uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
|
|
|
|
StatementIndex statement_index);
|
|
|
|
|
2012-11-06 12:58:10 +00:00
|
|
|
|
2012-10-20 22:52:25 +01:00
|
|
|
/// @brief Check Error and Throw Exception
|
|
|
|
///
|
|
|
|
/// Virtually all MySQL functions return a status which, if non-zero,
|
2015-10-23 16:41:27 -04:00
|
|
|
/// indicates an error. This function centralizes the error checking
|
|
|
|
/// code.
|
|
|
|
///
|
|
|
|
/// It is used to determine whether or not the function succeeded, and
|
|
|
|
/// in the event of failures, decide whether or not those failures are
|
|
|
|
/// recoverable.
|
|
|
|
///
|
|
|
|
/// If the error is recoverable, the method will throw a DbOperationError.
|
|
|
|
/// In the error is deemed unrecoverable, such as a loss of connectivity
|
|
|
|
/// with the server, this method will log the error and call exit(-1);
|
|
|
|
///
|
|
|
|
/// @todo Calling exit() is viewed as a short term solution for Kea 1.0.
|
|
|
|
/// Two tickets are likely to alter this behavior, first is #3639, which
|
|
|
|
/// calls for the ability to attempt to reconnect to the database. The
|
|
|
|
/// second ticket, #4087 which calls for the implementation of a generic,
|
|
|
|
/// FatalException class which will propagate outward.
|
2012-10-20 22:52:25 +01:00
|
|
|
///
|
|
|
|
/// @param status Status code: non-zero implies an error
|
|
|
|
/// @param index Index of statement that caused the error
|
|
|
|
/// @param what High-level description of the error
|
|
|
|
///
|
2012-11-08 12:14:10 +00:00
|
|
|
/// @throw isc::dhcp::DbOperationError An operation on the open database has
|
|
|
|
/// failed.
|
2015-10-23 16:41:27 -04:00
|
|
|
void checkError(int status, StatementIndex index,
|
|
|
|
const char* what) const;
|
2012-10-20 22:52:25 +01:00
|
|
|
|
2012-10-17 18:37:22 +01:00
|
|
|
// Members
|
2012-11-06 12:58:10 +00:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// The exchange objects are used for transfer of data to/from the database.
|
|
|
|
/// They are pointed-to objects as the contents may change in "const" calls,
|
|
|
|
/// while the rest of this object does not. (At alternative would be to
|
|
|
|
/// declare them as "mutable".)
|
2012-11-21 12:44:18 +00:00
|
|
|
boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
|
|
|
|
boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
|
2015-04-17 13:39:45 +02:00
|
|
|
|
2015-08-24 15:18:49 +02:00
|
|
|
/// @brief MySQL connection
|
|
|
|
MySqlConnection conn_;
|
2012-10-16 16:39:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}; // end of isc::dhcp namespace
|
|
|
|
}; // end of isc namespace
|
|
|
|
|
2012-10-29 13:43:31 +00:00
|
|
|
#endif // MYSQL_LEASE_MGR_H
|