diff options
Diffstat (limited to 'src/lib/dhcp_ddns')
-rw-r--r-- | src/lib/dhcp_ddns/.gitignore | 1 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/ncr_io.cc | 30 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/ncr_io.h | 29 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/ncr_msg.cc | 23 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/ncr_msg.h | 19 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/ncr_udp.cc | 7 | ||||
-rw-r--r-- | src/lib/dhcp_ddns/tests/ncr_unittests.cc | 23 |
7 files changed, 126 insertions, 6 deletions
diff --git a/src/lib/dhcp_ddns/.gitignore b/src/lib/dhcp_ddns/.gitignore index 6388b8cdd6..c632c2e100 100644 --- a/src/lib/dhcp_ddns/.gitignore +++ b/src/lib/dhcp_ddns/.gitignore @@ -1,2 +1,3 @@ /dhcp_ddns_messages.cc /dhcp_ddns_messages.h +/s-messages diff --git a/src/lib/dhcp_ddns/ncr_io.cc b/src/lib/dhcp_ddns/ncr_io.cc index 4f3f1d7289..7e7174ac00 100644 --- a/src/lib/dhcp_ddns/ncr_io.cc +++ b/src/lib/dhcp_ddns/ncr_io.cc @@ -15,9 +15,39 @@ #include <dhcp_ddns/dhcp_ddns_log.h> #include <dhcp_ddns/ncr_io.h> +#include <boost/algorithm/string/predicate.hpp> + namespace isc { namespace dhcp_ddns { +NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str) { + if (boost::iequals(protocol_str, "UDP")) { + return (NCR_UDP); + } + + if (boost::iequals(protocol_str, "TCP")) { + return (NCR_TCP); + } + + isc_throw(BadValue, "Invalid NameChangeRequest protocol:" << protocol_str); +} + +std::string ncrProtocolToString(NameChangeProtocol protocol) { + switch (protocol) { + case NCR_UDP: + return ("UDP"); + case NCR_TCP: + return ("TCP"); + default: + break; + } + + std::ostringstream stream; + stream << "UNKNOWN(" << protocol << ")"; + return (stream.str()); +} + + //************************** NameChangeListener *************************** NameChangeListener::NameChangeListener(RequestReceiveHandler& diff --git a/src/lib/dhcp_ddns/ncr_io.h b/src/lib/dhcp_ddns/ncr_io.h index 94d08f7e80..a5e513a31e 100644 --- a/src/lib/dhcp_ddns/ncr_io.h +++ b/src/lib/dhcp_ddns/ncr_io.h @@ -66,6 +66,35 @@ namespace isc { namespace dhcp_ddns { +/// @brief Defines the list of socket protocols supported. +/// Currently only UDP is implemented. +/// @todo TCP is intended to be implemented prior 1.0 release. +/// @todo Give some thought to an ANY protocol which might try +/// first as UDP then as TCP, etc. +enum NameChangeProtocol { + NCR_UDP, + NCR_TCP +}; + +/// @brief Function which converts labels to NameChangeProtocol enum values. +/// +/// @param protocol_str text to convert to an enum. +/// Valid string values: "UDP", "TCP" +/// +/// @return NameChangeProtocol value which maps to the given string. +/// +/// @throw isc::BadValue if given a string value which does not map to an +/// enum value. +extern NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str); + +/// @brief Function which converts NameChangeProtocol enums to text labels. +/// +/// @param protocol enum value to convert to label +/// +/// @return std:string containing the text label if the value is valid, or +/// "UNKNOWN" if not. +extern std::string ncrProtocolToString(NameChangeProtocol protocol); + /// @brief Exception thrown if an NcrListenerError encounters a general error. class NcrListenerError : public isc::Exception { public: diff --git a/src/lib/dhcp_ddns/ncr_msg.cc b/src/lib/dhcp_ddns/ncr_msg.cc index 418eaa0a1f..ae1816ff53 100644 --- a/src/lib/dhcp_ddns/ncr_msg.cc +++ b/src/lib/dhcp_ddns/ncr_msg.cc @@ -18,15 +18,36 @@ #include <asiolink/io_error.h> #include <cryptolink/cryptolink.h> +#include <boost/algorithm/string/predicate.hpp> #include <botan/sha2_32.h> #include <sstream> #include <limits> + namespace isc { namespace dhcp_ddns { +NameChangeFormat stringToNcrFormat(const std::string& fmt_str) { + if (boost::iequals(fmt_str, "JSON")) { + return FMT_JSON; + } + + isc_throw(BadValue, "Invalid NameChangeRequest format:" << fmt_str); +} + + +std::string ncrFormatToString(NameChangeFormat format) { + if (format == FMT_JSON) { + return ("JSON"); + } + + std::ostringstream stream; + stream << "UNKNOWN(" << format << ")"; + return (stream.str()); +} + /********************************* D2Dhcid ************************************/ namespace { @@ -590,7 +611,7 @@ NameChangeRequest::toText() const { << "Reverse Change: " << (reverse_change_ ? "yes" : "no") << std::endl << "FQDN: [" << fqdn_ << "]" << std::endl - << "IP Address: [" << ip_io_address_.toText() << "]" << std::endl + << "IP Address: [" << ip_io_address_ << "]" << std::endl << "DHCID: [" << dhcid_.toStr() << "]" << std::endl << "Lease Expires On: " << getLeaseExpiresOnStr() << std::endl << "Lease Length: " << lease_length_ << std::endl; diff --git a/src/lib/dhcp_ddns/ncr_msg.h b/src/lib/dhcp_ddns/ncr_msg.h index 47a7be642e..5dab9e8dd8 100644 --- a/src/lib/dhcp_ddns/ncr_msg.h +++ b/src/lib/dhcp_ddns/ncr_msg.h @@ -70,6 +70,25 @@ enum NameChangeFormat { FMT_JSON }; +/// @brief Function which converts labels to NameChangeFormat enum values. +/// +/// @param format_str text to convert to an enum. +/// Valid string values: "JSON" +/// +/// @return NameChangeFormat value which maps to the given string. +/// +/// @throw isc::BadValue if given a string value which does not map to an +/// enum value. +extern NameChangeFormat stringToNcrFormat(const std::string& fmt_str); + +/// @brief Function which converts NameChangeFormat enums to text labels. +/// +/// @param format enum value to convert to label +/// +/// @return std:string containing the text label if the value is valid, or +/// "UNKNOWN" if not. +extern std::string ncrFormatToString(NameChangeFormat format); + /// @brief Container class for handling the DHCID value within a /// NameChangeRequest. It provides conversion to and from string for JSON /// formatting, but stores the data internally as unsigned bytes. diff --git a/src/lib/dhcp_ddns/ncr_udp.cc b/src/lib/dhcp_ddns/ncr_udp.cc index 9e83f7ccb2..c69ad93374 100644 --- a/src/lib/dhcp_ddns/ncr_udp.cc +++ b/src/lib/dhcp_ddns/ncr_udp.cc @@ -94,7 +94,7 @@ NameChangeUDPListener::~NameChangeUDPListener() { void NameChangeUDPListener::open(isc::asiolink::IOService& io_service) { // create our endpoint and bind the the low level socket to it. - isc::asiolink::UDPEndpoint endpoint(ip_address_.getAddress(), port_); + isc::asiolink::UDPEndpoint endpoint(ip_address_, port_); // Create the low level socket. try { @@ -227,7 +227,7 @@ NameChangeUDPSender::~NameChangeUDPSender() { void NameChangeUDPSender::open(isc::asiolink::IOService& io_service) { // create our endpoint and bind the the low level socket to it. - isc::asiolink::UDPEndpoint endpoint(ip_address_.getAddress(), port_); + isc::asiolink::UDPEndpoint endpoint(ip_address_, port_); // Create the low level socket. try { @@ -252,8 +252,7 @@ NameChangeUDPSender::open(isc::asiolink::IOService& io_service) { // Create the server endpoint server_endpoint_.reset(new isc::asiolink:: - UDPEndpoint(server_address_.getAddress(), - server_port_)); + UDPEndpoint(server_address_, server_port_)); send_callback_->setDataSource(server_endpoint_); } diff --git a/src/lib/dhcp_ddns/tests/ncr_unittests.cc b/src/lib/dhcp_ddns/tests/ncr_unittests.cc index 9386a5fcbf..c66b8919cc 100644 --- a/src/lib/dhcp_ddns/tests/ncr_unittests.cc +++ b/src/lib/dhcp_ddns/tests/ncr_unittests.cc @@ -12,7 +12,7 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -#include <dhcp_ddns/ncr_msg.h> +#include <dhcp_ddns/ncr_io.h> #include <dhcp/duid.h> #include <dhcp/hwaddr.h> #include <util/time_utilities.h> @@ -608,5 +608,26 @@ TEST(NameChangeRequestTest, ipAddresses) { ASSERT_THROW(ncr.setIpAddress("x001:1::f3"),NcrMessageError); } +/// @brief Tests conversion of NameChangeFormat between enum and strings. +TEST(NameChangeFormatTest, formatEnumConversion){ + ASSERT_EQ(stringToNcrFormat("JSON"), dhcp_ddns::FMT_JSON); + ASSERT_EQ(stringToNcrFormat("jSoN"), dhcp_ddns::FMT_JSON); + ASSERT_THROW(stringToNcrFormat("bogus"), isc::BadValue); + + ASSERT_EQ(ncrFormatToString(dhcp_ddns::FMT_JSON), "JSON"); +} + +/// @brief Tests conversion of NameChangeProtocol between enum and strings. +TEST(NameChangeProtocolTest, protocolEnumConversion){ + ASSERT_EQ(stringToNcrProtocol("UDP"), dhcp_ddns::NCR_UDP); + ASSERT_EQ(stringToNcrProtocol("udP"), dhcp_ddns::NCR_UDP); + ASSERT_EQ(stringToNcrProtocol("TCP"), dhcp_ddns::NCR_TCP); + ASSERT_EQ(stringToNcrProtocol("Tcp"), dhcp_ddns::NCR_TCP); + ASSERT_THROW(stringToNcrProtocol("bogus"), isc::BadValue); + + ASSERT_EQ(ncrProtocolToString(dhcp_ddns::NCR_UDP), "UDP"); + ASSERT_EQ(ncrProtocolToString(dhcp_ddns::NCR_TCP), "TCP"); +} + } // end of anonymous namespace |