From: Francis Dupont Date: Fri, 10 Feb 2017 13:13:03 +0000 (+0100) Subject: [5126] Added some explicits and flatten RelayInfoParser X-Git-Tag: trac5124a_base~26^2~5 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=dcea68cc089e6eca569916fa9e694ca99ad595e6;p=thirdparty%2Fkea.git [5126] Added some explicits and flatten RelayInfoParser --- diff --git a/src/lib/dhcpsrv/parsers/dbaccess_parser.h b/src/lib/dhcpsrv/parsers/dbaccess_parser.h index 8cb716d7d2..1893a480ec 100644 --- a/src/lib/dhcpsrv/parsers/dbaccess_parser.h +++ b/src/lib/dhcpsrv/parsers/dbaccess_parser.h @@ -43,7 +43,7 @@ public: /// @brief Constructor /// /// @param db_type Specifies database type (lease or hosts) - DbAccessParser(DBType db_type); + explicit DbAccessParser(DBType db_type); /// The destructor. virtual ~DbAccessParser() diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc index 4730c95b91..2c0cf62c37 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc @@ -729,57 +729,31 @@ RelayInfoParser::RelayInfoParser(const Option::Universe& family) : family_(family) { }; +// Can't use a constructor as a function +namespace { +IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } +}; + +IOAddress +RelayInfoParser::getIOAddress(ConstElementPtr scope, + const std::string& name) { + return (getAndConvert(scope, name, "address")); +} + void RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg, ConstElementPtr relay_info) { - // Let's start with some sanity checks. - if (!relay_info || !cfg) { - isc_throw(DhcpConfigError, "Logic error: RelayInfoParser::parse() called " - "with at least one NULL parameter."); - } - - if (relay_info->getType() != Element::map) { - isc_throw(DhcpConfigError, "Configuration error: RelayInfoParser::parse() " - "called with non-map parameter"); - } - - // Now create the default value. - isc::asiolink::IOAddress ip(family_ == Option::V4 ? IOAddress::IPV4_ZERO_ADDRESS() - : IOAddress::IPV6_ZERO_ADDRESS()); - - // Now iterate over all parameters. Currently there's only one supported - // parameter, so it should be an easy thing to check. - bool ip_address_specified = false; - BOOST_FOREACH(ConfigPair param, relay_info->mapValue()) { - if (param.first == "ip-address") { - ip_address_specified = true; - - try { - ip = asiolink::IOAddress(param.second->stringValue()); - } catch (...) { - isc_throw(DhcpConfigError, "Failed to parse ip-address " - "value: " << param.second - << " (" << param.second->getPosition() << ")"); - } - - // Check if the address family matches. - if ( (ip.isV4() && family_ != Option::V4) || - (ip.isV6() && family_ != Option::V6) ) { - isc_throw(DhcpConfigError, "ip-address field " << ip.toText() - << " does not have IP address of expected family type: " - << (family_ == Option::V4 ? "IPv4" : "IPv6") - << " (" << param.second->getPosition() << ")"); - } - } else { - isc_throw(NotImplemented, - "parser error: RelayInfoParser parameter not supported: " - << param.second); - } - } - - if (!ip_address_specified) { - isc_throw(DhcpConfigError, "'relay' specified, but mandatory 'ip-address' " - "paramter in it is missing"); + // There is only one parameter which is mandatory + IOAddress ip = getIOAddress(relay_info, "ip-address"); + + // Check if the address family matches. + if ((ip.isV4() && family_ != Option::V4) || + (ip.isV6() && family_ != Option::V6) ) { + isc_throw(DhcpConfigError, "ip-address field " << ip.toText() + << " does not have IP address of expected family type: " + << (family_ == Option::V4 ? "IPv4" : "IPv6") + << " (" << getPosition("ip-address", relay_info) << ")"); } // Ok, we're done with parsing. Let's store the result in the structure @@ -1065,11 +1039,6 @@ SubnetConfigParser::createSubnet(ConstElementPtr params) { //**************************** D2ClientConfigParser ********************** -// Can't use a constructor as a function -namespace { -IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } -}; - IOAddress D2ClientConfigParser::getIOAddress(ConstElementPtr scope, const std::string& name) { diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.h b/src/lib/dhcpsrv/parsers/dhcp_parsers.h index 6d84cb731c..19ea14cb72 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.h +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.h @@ -466,7 +466,7 @@ public: /// @brief Constructor. /// /// @param address_family Address family: @c AF_INET or @c AF_INET6. - OptionDataParser(const uint16_t address_family); + explicit OptionDataParser(const uint16_t address_family); /// @brief Parses ElementPtr containing option definition /// @@ -578,7 +578,7 @@ public: /// @brief Constructor. /// /// @param address_family Address family: @c AF_INET or AF_INET6 - OptionDataListParser(const uint16_t address_family); + explicit OptionDataListParser(const uint16_t address_family); /// @brief Parses a list of options, instantiates them and stores in cfg /// @@ -721,7 +721,7 @@ public: /// @brief constructor /// @param family specifies protocol family (IPv4 or IPv6) - RelayInfoParser(const isc::dhcp::Option::Universe& family); + explicit RelayInfoParser(const isc::dhcp::Option::Universe& family); /// @brief parses the actual relay parameters /// @@ -733,7 +733,18 @@ public: void parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg, isc::data::ConstElementPtr relay_info); -protected: +private: + + /// @brief Returns a value converted to IOAddress + /// + /// Instantiation of getAndConvert() to IOAddress + /// + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter + /// @return an IOAddress value + isc::asiolink::IOAddress + getIOAddress(isc::data::ConstElementPtr scope, const std::string& name); + /// Protocol family (IPv4 or IPv6) Option::Universe family_; }; @@ -765,7 +776,7 @@ public: /// @brief constructor /// /// @param family address family: @c AF_INET or @c AF_INET6 - SubnetConfigParser(uint16_t family); + explicit SubnetConfigParser(uint16_t family); /// @brief virtual destructor (does nothing) virtual ~SubnetConfigParser() { } diff --git a/src/lib/dhcpsrv/parsers/ifaces_config_parser.h b/src/lib/dhcpsrv/parsers/ifaces_config_parser.h index a4c346e36d..d23a716dc5 100644 --- a/src/lib/dhcpsrv/parsers/ifaces_config_parser.h +++ b/src/lib/dhcpsrv/parsers/ifaces_config_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -29,7 +29,7 @@ public: /// @brief Constructor /// /// @param protocol AF_INET for DHCPv4 and AF_INET6 for DHCPv6. - IfacesConfigParser(const uint16_t protocol); + explicit IfacesConfigParser(const uint16_t protocol); /// @brief Parses content of the "interfaces-config". ///