From: Tomek Mrugalski Date: Thu, 5 Jan 2017 13:18:23 +0000 (+0100) Subject: [5032] empty "relay" structure is no longer allowed. X-Git-Tag: trac5044_base~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b9c2085a5dd66951d3cf583b162bc77291067bff;p=thirdparty%2Fkea.git [5032] empty "relay" structure is no longer allowed. --- diff --git a/doc/guide/dhcp4-srv.xml b/doc/guide/dhcp4-srv.xml index 9dcf8ba894..c42c5f58fb 100644 --- a/doc/guide/dhcp4-srv.xml +++ b/doc/guide/dhcp4-srv.xml @@ -3203,6 +3203,9 @@ src/lib/dhcpsrv/cfg_host_operations.cc --> + If "relay" is specified, the "ip-address" parameter within + it is mandatory. +
diff --git a/doc/guide/dhcp6-srv.xml b/doc/guide/dhcp6-srv.xml index 3c8064bc7f..2ac1fccac6 100644 --- a/doc/guide/dhcp6-srv.xml +++ b/doc/guide/dhcp6-srv.xml @@ -3410,6 +3410,9 @@ If not specified, the default value is: + If "relay" is specified, the "ip-address" parameter within + it is mandatory. +
diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc index 3ff2634b0e..cf3e116b76 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc @@ -809,12 +809,16 @@ RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg, } // Now create the default value. - isc::asiolink::IOAddress ip(family_ == Option::V4 ? "0.0.0.0" : "::"); + 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 (...) { @@ -838,6 +842,11 @@ RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg, } } + if (!ip_address_specified) { + isc_throw(DhcpConfigError, "'relay' specified, but mandatory 'ip-address' " + "paramter in it is missing"); + } + // Ok, we're done with parsing. Let's store the result in the structure // we were given as configuration storage. *cfg = isc::dhcp::Subnet::RelayInfo(ip); diff --git a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc index 4cdc8bea2a..203b496ad1 100644 --- a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc @@ -2463,6 +2463,19 @@ TEST_F(ParseConfigTest, validRelayInfo4) { " }"; ElementPtr json = Element::fromJSON(config_str); + // We need to set the default ip-address to something. + Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0"))); + + RelayInfoParser parser(Option::V4); + + // Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser + EXPECT_NO_THROW(parser.parse(result, json)); + EXPECT_EQ("192.0.2.1", result->addr_.toText()); +} + +/// @brief Checks that a bogus relay info structure for IPv4 is rejected. +TEST_F(ParseConfigTest, bogusRelayInfo4) { + // Invalid config (wrong family type of the ip-address field) std::string config_str_bogus1 = " {" @@ -2477,19 +2490,24 @@ TEST_F(ParseConfigTest, validRelayInfo4) { " }"; ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2); + // Invalid config (ip-address is mandatory) + std::string config_str_bogus3 = + " {" + " }"; + ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3); + // We need to set the default ip-address to something. - Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0"))); + Subnet::RelayInfoPtr result(new Subnet::RelayInfo(IOAddress::IPV4_ZERO_ADDRESS())); RelayInfoParser parser(Option::V4); - // Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser - EXPECT_NO_THROW(parser.parse(result, json)); - EXPECT_EQ("192.0.2.1", result->addr_.toText()); - - // Let's check negative scenario (wrong family type) + // wrong family type EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError); - // Let's check negative scenario (too large byte values in pseudo-IPv4 addr) + // Too large byte values in pseudo-IPv4 addr + EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError); + + // Mandatory ip-address is missing. What a pity. EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError); } @@ -2503,6 +2521,18 @@ TEST_F(ParseConfigTest, validRelayInfo6) { " }"; ElementPtr json = Element::fromJSON(config_str); + // We need to set the default ip-address to something. + Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::"))); + + RelayInfoParser parser(Option::V6); + // Subnet4 parser will pass :: to the RelayInfoParser + EXPECT_NO_THROW(parser.parse(result, json)); + EXPECT_EQ("2001:db8::1", result->addr_.toText()); +} + +/// @brief Checks that a valid relay info structure for IPv6 can be handled +TEST_F(ParseConfigTest, bogusRelayInfo6) { + // Invalid config (wrong family type of the ip-address field std::string config_str_bogus1 = " {" @@ -2517,19 +2547,25 @@ TEST_F(ParseConfigTest, validRelayInfo6) { " }"; ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2); + // Missing mandatory ip-address field. + std::string config_str_bogus3 = + " {" + " }"; + ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3); + // We need to set the default ip-address to something. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::"))); RelayInfoParser parser(Option::V6); - // Subnet4 parser will pass :: to the RelayInfoParser - EXPECT_NO_THROW(parser.parse(result, json)); - EXPECT_EQ("2001:db8::1", result->addr_.toText()); - // Let's check negative scenario (wrong family type) + // Negative scenario (wrong family type) EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError); - // Unparseable text that looks like IPv6 address, but has too many colons + // Looks like IPv6 address, but has too many colons EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError); + + // Mandatory ip-address is missing. What a pity. + EXPECT_THROW(parser.parse(result, json_bogus3), DhcpConfigError); } // There's no test for ControlSocketParser, as it is tested in the DHCPv4 code