From 593eacf16fe69fade5eeb71704ac25c6e046421b Mon Sep 17 00:00:00 2001 From: Stephen Morris Date: Tue, 18 Dec 2012 12:28:39 +0000 Subject: [PATCH] [2524] Add logging messages to memory file database code Also make the hardware address to string function common to both database modules. --- src/lib/dhcpsrv/Makefile.am | 1 + src/lib/dhcpsrv/dhcpsrv_messages.mes | 148 ++++++++++++++++------- src/lib/dhcpsrv/hwaddr.cc | 42 +++++++ src/lib/dhcpsrv/hwaddr.h | 47 +++++++ src/lib/dhcpsrv/lease_mgr.h | 11 +- src/lib/dhcpsrv/memfile_lease_mgr.cc | 66 +++++++--- src/lib/dhcpsrv/memfile_lease_mgr.h | 1 + src/lib/dhcpsrv/mysql_lease_mgr.cc | 28 ----- src/lib/dhcpsrv/mysql_lease_mgr.h | 1 + src/lib/dhcpsrv/tests/Makefile.am | 1 + src/lib/dhcpsrv/tests/hwaddr_unittest.cc | 46 +++++++ 11 files changed, 302 insertions(+), 90 deletions(-) create mode 100644 src/lib/dhcpsrv/hwaddr.cc create mode 100644 src/lib/dhcpsrv/hwaddr.h create mode 100644 src/lib/dhcpsrv/tests/hwaddr_unittest.cc diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index 37fd953ace..3e2891b63a 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -34,6 +34,7 @@ libb10_dhcpsrv_la_SOURCES += addr_utilities.cc addr_utilities.h libb10_dhcpsrv_la_SOURCES += alloc_engine.cc alloc_engine.h libb10_dhcpsrv_la_SOURCES += dhcpsrv_log.cc dhcpsrv_log.h libb10_dhcpsrv_la_SOURCES += cfgmgr.cc cfgmgr.h +libb10_dhcpsrv_la_SOURCES += hwaddr.cc hwaddr.h libb10_dhcpsrv_la_SOURCES += lease_mgr.cc lease_mgr.h libb10_dhcpsrv_la_SOURCES += lease_mgr_factory.cc lease_mgr_factory.h libb10_dhcpsrv_la_SOURCES += memfile_lease_mgr.cc memfile_lease_mgr.h diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.mes b/src/lib/dhcpsrv/dhcpsrv_messages.mes index dcc45abab7..90f7a41f0a 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.mes +++ b/src/lib/dhcpsrv/dhcpsrv_messages.mes @@ -16,13 +16,78 @@ $NAMESPACE isc::dhcp % DHCPSRV_INVALID_ACCESS invalid database access string: %1 This is logged when an attempt has been made to parse a database access string -and the attempt ended in error. The access string in question - which should -be of the form 'keyword=value keyword=value...' is included in the message. +and the attempt ended in error. The access string in question - which +should be of the form 'keyword=value keyword=value...' is included in +the message. + +% DHCPSRV_MEMFILE_ADD_ADDR4 adding IPv4 lease with address %1 +A debug message issued when the server is about to add an IPv4 lease +with the specified address to the memory file backend database. + +% DHCPSRV_MEMFILE_ADD_ADDR6 adding IPv6 lease with address %1 +A debug message issued when the server is about to add an IPv6 lease +with the specified address to the memory file backend database. % DHCPSRV_MEMFILE_DB opening memory file lease database: %1 -This informational message is logged when a DHCP server (either V4 or V6) is -about to open a memory file lease database. The parameters of the connection -are logged. +This informational message is logged when a DHCP server (either V4 or +V6) is about to open a memory file lease database. The parameters of +the connection including database name and username needed to access it +(but not the password if any) are logged. + +% DHCPSRV_MEMFILE_DELETE_ADDR deleting lease for address %1 +A debug message issued when the server is attempting to delete a lease +for the specified address from the memory file database for the specified +address. + +% DHCPSRV_MEMFILE_GET_ADDR4 obtaining IPv4 lease for address %1 +A debug message issued when the server is attempting to obtain an IPv4 +lease from the memory file database for the specified address. + +% DHCPSRV_MEMFILE_GET_ADDR6 obtaining IPv6 lease for address %1 +A debug message issued when the server is attempting to obtain an IPv6 +lease from the memory file database for the specified address. + +% DHCPSRV_MEMFILE_GET_CLIENTID obtaining IPv4 leases for client ID %1 +A debug message issued when the server is attempting to obtain a set of +IPv4 leases from the memory file database for a client with the specified +client identification. + +% DHCPSRV_MEMFILE_GET_HWADDR obtaining IPv4 leases for hardware address %1 +A debug message issued when the server is attempting to obtain a set of +IPv4 leases from the memory file database for a client with the specified +hardware address. + +% DHCPSRV_MEMFILE_GET_IAID_DUID obtaining IPv4 leases for IAID %1 and DUID %2 +A debug message issued when the server is attempting to obtain a set of +IPv6 lease from the memory file database for a client with the specified +IAID (Identity Association ID) and DUID (DHCP Unique Identifier). + +% DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID obtaining IPv4 leases for IAID %1, Subnet ID %2 and DUID %3 +A debug message issued when the server is attempting to obtain an IPv6 +lease from the memory file database for a client with the specified IAID +(Identity Association ID), Subnet ID and DUID (DHCP Unique Identifier). + +% DHCPSRV_MEMFILE_GET_SUBID_CLIENTID obtaining IPv4 lease for subnet ID %1 and client ID %2 +A debug message issued when the server is attempting to obtain an IPv4 +lease from the memory file database for a client with the specified +subnet ID and client ID. + +% DHCPSRV_MEMFILE_GET_SUBID_HWADDR obtaining IPv4 lease for subnet ID %1 and hardware address %2 +A debug message issued when the server is attempting to obtain an IPv4 +lease from the memory file database for a client with the specified +subnet ID and hardware address. + +% DHCPSRV_MEMFILE_GET_VERSION obtaining schema version information +A debug message issued when the server is about to obtain schema version +information from the memory file database. + +% DHCPSRV_MEMFILE_UPDATE_ADDR4 updating IPv4 lease for address %1 +A debug message issued when the server is attempting to update IPv4 +lease from the memory file database for the specified address. + +% DHCPSRV_MEMFILE_UPDATE_ADDR6 updating IPv6 lease for address %1 +A debug message issued when the server is attempting to update IPv6 +lease from the memory file database for the specified address. % DHCPSRV_MYSQL_ADD_ADDR4 adding IPv4 lease with address %1 A debug message issued when the server is about to add an IPv4 lease @@ -33,70 +98,71 @@ A debug message issued when the server is about to add an IPv6 lease with the specified address to the MySQL backend database. % DHCPSRV_MYSQL_DB opening MySQL lease database: %1 -This informational message is logged when a DHCP server (either V4 or V6) is -about to open a MySQL lease database. The parameters of the connection -including database name and username needed to access it (but not the -password if any) are logged. +This informational message is logged when a DHCP server (either V4 or +V6) is about to open a MySQL lease database. The parameters of the +connection including database name and username needed to access it +(but not the password if any) are logged. % DHCPSRV_MYSQL_DELETE_ADDR deleting lease for address %1 -A debug message issued when the server is attempting to delete -a lease for the specified address from the database for the specified address. +A debug message issued when the server is attempting to delete a lease for +the specified address from the MySQL database for the specified address. % DHCPSRV_MYSQL_GET_ADDR4 obtaining IPv4 lease for address %1 -A debug message issued when the server is attempting to obtain an -IPv4 lease from the database for the specified address. +A debug message issued when the server is attempting to obtain an IPv4 +lease from the MySQL database for the specified address. % DHCPSRV_MYSQL_GET_ADDR6 obtaining IPv6 lease for address %1 -A debug message issued when the server is attempting to obtain an -IPv6 lease from the database for the specified address. +A debug message issued when the server is attempting to obtain an IPv6 +lease from the MySQL database for the specified address. % DHCPSRV_MYSQL_GET_CLIENTID obtaining IPv4 leases for client ID %1 -A debug message issued when the server is attempting to obtain a set of -IPv4 leases from the database for a client with the specified client -identification. +A debug message issued when the server is attempting to obtain a set +of IPv4 leases from the MySQL database for a client with the specified +client identification. % DHCPSRV_MYSQL_GET_HWADDR obtaining IPv4 leases for hardware address %1 -A debug message issued when the server is attempting to obtain a set of -IPv4 leases from the database for a client with the specified hardware -address. +A debug message issued when the server is attempting to obtain a set +of IPv4 leases from the MySQL database for a client with the specified +hardware address. % DHCPSRV_MYSQL_GET_IAID_DUID obtaining IPv4 leases for IAID %1 and DUID %2 -A debug message issued when the server is attempting to obtain a set of IPv6 -lease from the database for a client with the specified IAID (Identity Association ID) and -DUID (DHCP Unique Identifier). +A debug message issued when the server is attempting to obtain a set of +IPv6 lease from the MySQL database for a client with the specified IAID +(Identity Association ID) and DUID (DHCP Unique Identifier). % DHCPSRV_MYSQL_GET_IAID_SUBID_DUID obtaining IPv4 leases for IAID %1, Subnet ID %2 and DUID %3 -A debug message issued when the server is attempting to obtain an IPv6 lease -from the database for a client with the specified IAID (Identity Association ID), -Subnet ID and DUID (DHCP Unique Identifier). +A debug message issued when the server is attempting to obtain an IPv6 +lease from the MySQL database for a client with the specified IAID +(Identity Association ID), Subnet ID and DUID (DHCP Unique Identifier). % DHCPSRV_MYSQL_GET_SUBID_CLIENTID obtaining IPv4 lease for subnet ID %1 and client ID %2 A debug message issued when the server is attempting to obtain an IPv4 -lease from the database for a client with the specified subnet ID and -client ID. +lease from the MySQL database for a client with the specified subnet ID +and client ID. % DHCPSRV_MYSQL_GET_SUBID_HWADDR obtaining IPv4 lease for subnet ID %1 and hardware address %2 A debug message issued when the server is attempting to obtain an IPv4 -lease from the database for a client with the specified subnet ID and -hardware address. +lease from the MySQL database for a client with the specified subnet ID +and hardware address. % DHCPSRV_MYSQL_GET_VERSION obtaining schema version information A debug message issued when the server is about to obtain schema version -information from the database. +information from the MySQL database. % DHCPSRV_MYSQL_UPDATE_ADDR4 updating IPv4 lease for address %1 -A debug message issued when the server is attempting to update -IPv4 lease from the database for the specified address. +A debug message issued when the server is attempting to update IPv4 +lease from the MySQL database for the specified address. % DHCPSRV_MYSQL_UPDATE_ADDR6 updating IPv6 lease for address %1 -A debug message issued when the server is attempting to update -IPv6 lease from the database for the specified address. +A debug message issued when the server is attempting to update IPv6 +lease from the MySQL database for the specified address. % DHCPSRV_NOTYPE_DB no 'type' keyword to determine database backend: %1 -This is an error message, logged when an attempt has been made to access a -database backend, but where no 'type' keyword has been included in the access -string. The access string (less any passwords) is included in the message. +This is an error message, logged when an attempt has been made to access +a database backend, but where no 'type' keyword has been included in +the access string. The access string (less any passwords) is included +in the message. % DHCPSRV_UNKNOWN_DB unknown database type: %1 -The database access string specified a database type (given in the message) -that is unknown to the software. This is a configuration error. +The database access string specified a database type (given in the +message) that is unknown to the software. This is a configuration error. diff --git a/src/lib/dhcpsrv/hwaddr.cc b/src/lib/dhcpsrv/hwaddr.cc new file mode 100644 index 0000000000..25716e08ea --- /dev/null +++ b/src/lib/dhcpsrv/hwaddr.cc @@ -0,0 +1,42 @@ +// 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. + + +#include + +#include +#include +#include +#include + +namespace isc { +namespace dhcp { + +std::string +hardwareAddressString(const HWAddr& hwaddr) { + std::ostringstream stream; + + for (size_t i = 0; i < hwaddr.size(); ++i) { + if (i > 0) { + stream << ":"; + } + stream << std::setw(2) << std::hex << std::setfill('0') + << static_cast(hwaddr[i]); + } + + return (stream.str()); +} + +}; // namespace dhcp +}; // namespace isc diff --git a/src/lib/dhcpsrv/hwaddr.h b/src/lib/dhcpsrv/hwaddr.h new file mode 100644 index 0000000000..a735792afa --- /dev/null +++ b/src/lib/dhcpsrv/hwaddr.h @@ -0,0 +1,47 @@ +// 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 __HWADDR_H +#define __HWADDR_H + +#include +#include +#include + +namespace isc { +namespace dhcp { + +/// @brief Hardware Address +typedef std::vector HWAddr; + +/// @brief Produce string representation of hardware address +/// +/// Returns a string containing the hardware address. This is only used for +/// logging. +/// +/// @note Six characters is an arbitrary length, chosen to provide a +/// suitably wide string. +/// +/// @todo Create a "hardware address" class of which this will be a member. +/// +/// @param hwaddr Hardware address to convert to string form +/// +/// @return String form of the hardware address. +std::string +hardwareAddressString(const HWAddr& hwaddr); + +}; // namespace dhcp +}; // namespace isc + +#endif // __HWADDR_H diff --git a/src/lib/dhcpsrv/lease_mgr.h b/src/lib/dhcpsrv/lease_mgr.h index 4f82fef70c..427e315179 100644 --- a/src/lib/dhcpsrv/lease_mgr.h +++ b/src/lib/dhcpsrv/lease_mgr.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -25,6 +26,7 @@ #include #include +#include #include #include #include @@ -61,8 +63,6 @@ /// Nevertheless, we hope to have failover protocol eventually implemented in /// the Kea. -#include - namespace isc { namespace dhcp { @@ -418,9 +418,6 @@ typedef std::vector Lease6Collection; /// see the documentation of those classes for details. class LeaseMgr { public: - /// Client hardware address - typedef std::vector HWAddr; - /// Database configuration parameter map typedef std::map ParameterMap; @@ -475,7 +472,7 @@ public: /// @param hwaddr hardware address of the client /// /// @return lease collection - virtual Lease4Collection getLease4(const HWAddr& hwaddr) const = 0; + virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0; /// @brief Returns existing IPv4 leases for specified hardware address /// and a subnet @@ -487,7 +484,7 @@ public: /// @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, + virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr, SubnetID subnet_id) const = 0; /// @brief Returns existing IPv4 lease for specified client-id diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc index b71b16618d..5b5fac9df7 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -12,6 +12,7 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. +#include #include #include @@ -28,11 +29,16 @@ Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters) Memfile_LeaseMgr::~Memfile_LeaseMgr() { } -bool Memfile_LeaseMgr::addLease(const Lease4Ptr&) { +bool Memfile_LeaseMgr::addLease(const Lease4Ptr& lease) { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_ADD_ADDR4) + .arg(lease->addr_.toText()); return (false); } bool Memfile_LeaseMgr::addLease(const Lease6Ptr& lease) { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_ADD_ADDR6) + .arg(lease->addr_.toText()); + if (getLease6(lease->addr_)) { // there is a lease with specified address already return (false); @@ -41,30 +47,46 @@ bool Memfile_LeaseMgr::addLease(const Lease6Ptr& lease) { return (true); } -Lease4Ptr Memfile_LeaseMgr::getLease4(const isc::asiolink::IOAddress&) const { +Lease4Ptr Memfile_LeaseMgr::getLease4( + const isc::asiolink::IOAddress& addr) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_ADDR4) + .arg(addr.toText()); + return (Lease4Ptr()); } -Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& ) const { +Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_HWADDR) + .arg(hardwareAddressString(hwaddr)); + return (Lease4Collection()); } -Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr&, - SubnetID) const { +Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr, + SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_SUBID_HWADDR) + .arg(subnet_id).arg(hardwareAddressString(hwaddr)); return (Lease4Ptr()); } +Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& clientid) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_CLIENTID) + .arg(clientid.toText()); + return (Lease4Collection()); +} -Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId&, - SubnetID) const { +Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId& clientid, + SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_SUBID_CLIENTID) + .arg(subnet_id).arg(clientid.toText()); return (Lease4Ptr()); } -Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& ) const { - return (Lease4Collection()); -} +Lease6Ptr Memfile_LeaseMgr::getLease6( + const isc::asiolink::IOAddress& addr) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_ADDR6) + .arg(addr.toText()); -Lease6Ptr Memfile_LeaseMgr::getLease6(const isc::asiolink::IOAddress& addr) const { Lease6Storage::iterator l = storage6_.find(addr); if (l == storage6_.end()) { return (Lease6Ptr()); @@ -73,12 +95,20 @@ Lease6Ptr Memfile_LeaseMgr::getLease6(const isc::asiolink::IOAddress& addr) cons } } -Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& , uint32_t ) const { +Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& duid, + uint32_t iaid) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_GET_IAID_DUID) + .arg(iaid).arg(duid.toText()); + return (Lease6Collection()); } Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, + DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID) + .arg(iaid).arg(subnet_id).arg(duid.toText()); + /// @todo: Slow, naive implementation. Write it using additional indexes for (Lease6Storage::iterator l = storage6_.begin(); l != storage6_.end(); ++l) { if ( (*((*l)->duid_) == duid) && @@ -90,14 +120,22 @@ Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID& duid, uint32_t iaid, return (Lease6Ptr()); } -void Memfile_LeaseMgr::updateLease4(const Lease4Ptr& ) { +void Memfile_LeaseMgr::updateLease4(const Lease4Ptr& lease) { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_UPDATE_ADDR4) + .arg(lease->addr_.toText()); + } -void Memfile_LeaseMgr::updateLease6(const Lease6Ptr& ) { +void Memfile_LeaseMgr::updateLease6(const Lease6Ptr& lease) { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_UPDATE_ADDR6) + .arg(lease->addr_.toText()); + } bool Memfile_LeaseMgr::deleteLease(const isc::asiolink::IOAddress& addr) { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_MEMFILE_DELETE_ADDR) + .arg(addr.toText()); if (addr.isV4()) { // V4 not implemented yet return (false); diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.h b/src/lib/dhcpsrv/memfile_lease_mgr.h index 268b722a83..85813ec40f 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.h +++ b/src/lib/dhcpsrv/memfile_lease_mgr.h @@ -15,6 +15,7 @@ #ifndef MEMFILE_LEASE_MGR_H #define MEMFILE_LEASE_MGR_H +#include #include #include diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index deb4288d18..36cc9456b8 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -192,34 +192,6 @@ TaggedStatement tagged_statements[] = { {MySqlLeaseMgr::NUM_STATEMENTS, NULL} }; -/// @brief Produce string representation of hardware address -/// -/// Returns a string containing the hardware address. This is only used for -/// logging. -/// -/// @note Six characters is an arbitrary length, chosen to provide a -/// suitably wide string. -/// -/// @todo Create a "hardware address" class of which this will be a member. -/// -/// @param hwaddr Hardware address to convert to string form -/// -/// @return String form of the hardware address. -std::string -hardwareAddressString(const LeaseMgr::HWAddr& hwaddr) { - std::ostringstream stream; - - for (size_t i = 0; i < hwaddr.size(); ++i) { - if (i > 0) { - stream << ":"; - } - stream << std::setw(2) << std::setfill('0') - << static_cast(hwaddr[i]); - } - - return (stream.str()); -} - }; // Anonymous namespace diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 68b0e1c5d9..210373113c 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -15,6 +15,7 @@ #ifndef MYSQL_LEASE_MGR_H #define MYSQL_LEASE_MGR_H +#include #include #include diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index 8de5fda57c..6be863e887 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -30,6 +30,7 @@ libdhcpsrv_unittests_SOURCES = run_unittests.cc libdhcpsrv_unittests_SOURCES += addr_utilities_unittest.cc libdhcpsrv_unittests_SOURCES += alloc_engine_unittest.cc libdhcpsrv_unittests_SOURCES += cfgmgr_unittest.cc +libdhcpsrv_unittests_SOURCES += hwaddr_unittest.cc libdhcpsrv_unittests_SOURCES += lease_mgr_factory_unittest.cc libdhcpsrv_unittests_SOURCES += lease_mgr_unittest.cc libdhcpsrv_unittests_SOURCES += memfile_lease_mgr_unittest.cc diff --git a/src/lib/dhcpsrv/tests/hwaddr_unittest.cc b/src/lib/dhcpsrv/tests/hwaddr_unittest.cc new file mode 100644 index 0000000000..f6b769d906 --- /dev/null +++ b/src/lib/dhcpsrv/tests/hwaddr_unittest.cc @@ -0,0 +1,46 @@ +// 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. + +#include + +#include + +#include + +using namespace isc::dhcp; + +namespace { + +TEST(HwaddrTest, stringConversion) { + + // Check that an empty vector returns an appropriate string + HWAddr hwaddr; + std::string result = hardwareAddressString(hwaddr); + EXPECT_EQ(std::string(""), result); + + // ... that a single-byte string is OK + hwaddr.push_back(0xc3); + result = hardwareAddressString(hwaddr); + EXPECT_EQ(std::string("c3"), result); + + // ... and that a multi-byte string works + hwaddr.push_back(0x7); + hwaddr.push_back(0xa2); + hwaddr.push_back(0xe8); + hwaddr.push_back(0x42); + result = hardwareAddressString(hwaddr); + EXPECT_EQ(std::string("c3:07:a2:e8:42"), result); +} + +}; // Anonymous namespace -- GitLab