2012-10-16 16:39:32 +01:00
|
|
|
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef __MYSQL_LEASE_MGR_H
|
|
|
|
#define __MYSQL_LEASE_MGR_H
|
|
|
|
|
2012-10-17 18:37:22 +01:00
|
|
|
#include <mysql.h>
|
2012-10-16 16:39:32 +01:00
|
|
|
#include <dhcp/lease_mgr.h>
|
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace dhcp {
|
|
|
|
|
|
|
|
/// @brief Abstract Lease Manager
|
|
|
|
///
|
|
|
|
/// This is a concrete API for the backend for the MySQL database.
|
|
|
|
class MySqlLeaseMgr : public LeaseMgr {
|
|
|
|
public:
|
|
|
|
/// @brief Constructor
|
|
|
|
///
|
|
|
|
/// Uses the following keywords in the parameters passed to it to
|
|
|
|
/// connect to the database:
|
|
|
|
/// - name - Name of the database to which to connect
|
|
|
|
/// - host - Host name to which to connect
|
|
|
|
/// - user - Username under which to connect.
|
|
|
|
/// - password - Password for "user" on the database.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
MySqlLeaseMgr(const ParameterMap& parameters);
|
|
|
|
|
|
|
|
/// @brief Destructor (closes database)
|
|
|
|
virtual ~MySqlLeaseMgr();
|
|
|
|
|
|
|
|
/// @brief Adds an IPv4 lease.
|
|
|
|
///
|
|
|
|
/// @param lease lease to be added
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual bool addLease(Lease4Ptr lease);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Adds an IPv6 lease.
|
|
|
|
///
|
|
|
|
/// @param lease lease to be added
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual bool addLease(Lease6Ptr lease);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv4 lease for specified IPv4 address and subnet_id
|
|
|
|
///
|
|
|
|
/// This method is used to get a lease for specific subnet_id. There can be
|
|
|
|
/// at most one lease for any given subnet, so this method returns a single
|
|
|
|
/// pointer.
|
|
|
|
///
|
|
|
|
/// @param addr address of the searched lease
|
|
|
|
/// @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,
|
2012-10-17 18:37:22 +01:00
|
|
|
SubnetID subnet_id) const;
|
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.
|
|
|
|
/// For other query types (by hardware addr, by client-id) there can be
|
|
|
|
/// 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
|
|
|
|
/// @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)
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const;
|
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-10-17 18:37:22 +01:00
|
|
|
virtual Lease4Collection getLease4(const 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)
|
|
|
|
virtual Lease4Ptr getLease4(const 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-10-17 18:37:22 +01:00
|
|
|
virtual Lease4Collection getLease4(const ClientId& clientid) 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)
|
|
|
|
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.
|
|
|
|
/// The assumtion here is that there will not be site or link-local
|
|
|
|
/// addresses used, so there is no way of having address duplication.
|
|
|
|
///
|
|
|
|
/// @param addr address of the searched lease
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual Lease6Ptr getLease6(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.
|
|
|
|
///
|
|
|
|
/// @param duid client DUID
|
|
|
|
/// @param iaid IA identifier
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
|
|
|
virtual Lease6Collection getLease6(const DUID& duid,
|
2012-10-17 18:37:22 +01:00
|
|
|
uint32_t iaid) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns existing IPv6 lease for a given DUID+IA combination
|
|
|
|
///
|
|
|
|
/// @param duid client DUID
|
|
|
|
/// @param iaid IA identifier
|
|
|
|
/// @param subnet_id subnet id of the subnet the lease belongs to
|
|
|
|
///
|
|
|
|
/// @return smart pointer to the lease (or NULL if a lease is not found)
|
|
|
|
virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid,
|
2012-10-17 18:37:22 +01:00
|
|
|
SubnetID subnet_id) const;
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Updates IPv4 lease.
|
|
|
|
///
|
|
|
|
/// @param lease4 The lease to be updated.
|
|
|
|
///
|
|
|
|
/// If no such lease is present, an exception will be thrown.
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual void updateLease4(Lease4Ptr lease4);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Updates IPv4 lease.
|
|
|
|
///
|
|
|
|
/// @param lease4 The lease to be updated.
|
|
|
|
///
|
|
|
|
/// If no such lease is present, an exception will be thrown.
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual void updateLease6(Lease6Ptr lease6);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @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
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual bool deleteLease4(uint32_t addr);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @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
|
2012-10-17 18:37:22 +01:00
|
|
|
virtual bool deleteLease6(isc::asiolink::IOAddress addr);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
/// @brief Returns backend name.
|
|
|
|
///
|
|
|
|
/// Each backend have specific name, e.g. "mysql" or "sqlite".
|
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-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.
|
|
|
|
///
|
|
|
|
/// @todo: We will need to implement 3 version functions eventually:
|
|
|
|
/// A. abstract API version
|
|
|
|
/// B. backend version
|
|
|
|
/// C. database version (stored in the database scheme)
|
|
|
|
///
|
|
|
|
/// and then check that:
|
|
|
|
/// B>=A and B=C (it is ok to have newer backend, as it should be backward
|
|
|
|
/// compatible)
|
|
|
|
/// Also if B>C, some database upgrade procedure may be triggered
|
|
|
|
virtual std::pair<uint32_t, uint32_t> getVersion() const;
|
|
|
|
|
|
|
|
private:
|
2012-10-18 11:33:51 +01:00
|
|
|
/// @brief Enum of Statements
|
|
|
|
///
|
|
|
|
/// This is provided to set indexes into a list of prepared statements.
|
|
|
|
enum StatementIndex {
|
|
|
|
SELECT_VERSION, // Obtain version number
|
|
|
|
NUM_STATEMENTS // Number of statements
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief Prepare Single Statement
|
|
|
|
///
|
|
|
|
/// Creates a prepared statement from the text given and adds it to the
|
|
|
|
/// statements_ vector at the given index.
|
|
|
|
///
|
|
|
|
/// @param index Index into the statements_ vector into which the text
|
|
|
|
/// should be placed. The vector must be big enough for the index
|
|
|
|
/// to be valid, else an exception will be thrown.
|
|
|
|
/// @param text Text of the SQL statement to be prepared.
|
|
|
|
///
|
|
|
|
/// @exception DbOperationError MySQL operation failed, exception will give
|
|
|
|
/// text indicating the reason.
|
|
|
|
/// @exception InvalidParameter 'index' is not valid for the vector.
|
|
|
|
void prepareStatement(StatementIndex index, const char* text);
|
|
|
|
|
|
|
|
/// @brief Prepare statements
|
|
|
|
///
|
|
|
|
/// Creates the prepared statements for all of the SQL statements used
|
|
|
|
/// by the MySQL backend.
|
|
|
|
void prepareStatements();
|
|
|
|
|
2012-10-17 18:37:22 +01:00
|
|
|
/// @brief Open Database
|
|
|
|
///
|
|
|
|
/// Opens the database using the information supplied in the parameters
|
|
|
|
/// passed to the constructor.
|
|
|
|
///
|
|
|
|
/// @exception DbOpenError Error opening the database
|
|
|
|
void openDatabase();
|
|
|
|
|
|
|
|
// Members
|
2012-10-18 11:33:51 +01:00
|
|
|
MYSQL* mysql_; ///< MySQL context object
|
|
|
|
std::vector<std::string> raw_statements_; ///< Raw text of statements
|
|
|
|
std::vector<MYSQL_STMT*> statements_; ///< Prepared statements
|
2012-10-16 16:39:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}; // end of isc::dhcp namespace
|
|
|
|
}; // end of isc namespace
|
|
|
|
|
|
|
|
#endif // __MYSQL_LEASE_MGR_H
|