diff options
author | Marcin Siodelski <marcin@isc.org> | 2016-05-18 13:25:35 +0200 |
---|---|---|
committer | Marcin Siodelski <marcin@isc.org> | 2016-05-18 13:25:35 +0200 |
commit | baa4290727f6600f5094980a213ae1869d28bcbd (patch) | |
tree | ac3024fe0e3a0c5c2a009f04c4bc04f34348af3d /src/lib/dhcp | |
parent | [master] Added ChangeLog entry 1115 for #4481 (diff) | |
download | kea-baa4290727f6600f5094980a213ae1869d28bcbd.tar.xz kea-baa4290727f6600f5094980a213ae1869d28bcbd.zip |
[4498] Removed server specific method for unpacking DHCPv4 options.
Diffstat (limited to 'src/lib/dhcp')
-rw-r--r-- | src/lib/dhcp/libdhcp++.cc | 11 | ||||
-rw-r--r-- | src/lib/dhcp/tests/libdhcp++_unittest.cc | 203 |
2 files changed, 193 insertions, 21 deletions
diff --git a/src/lib/dhcp/libdhcp++.cc b/src/lib/dhcp/libdhcp++.cc index 700b734bc5..be9b61805d 100644 --- a/src/lib/dhcp/libdhcp++.cc +++ b/src/lib/dhcp/libdhcp++.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2016 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 @@ -15,6 +15,7 @@ #include <dhcp/option6_iaaddr.h> #include <dhcp/option_definition.h> #include <dhcp/option_int_array.h> +#include <dhcp/option_space.h> #include <dhcp/std_option_defs.h> #include <dhcp/docsis3_option_defs.h> #include <exceptions/exceptions.h> @@ -447,7 +448,14 @@ size_t LibDHCP::unpackOptions4(const OptionBuffer& buf, OptionDefContainer option_defs; if (option_space == "dhcp4") { option_defs = LibDHCP::getOptionDefs(Option::V4); + } else { + OptionDefContainerPtr option_defs_ptr = + LibDHCP::getRuntimeOptionDefs(option_space); + if (option_defs_ptr) { + option_defs = *option_defs_ptr; + } } + // @todo Once we implement other option spaces we should add else clause // here and gather option definitions for them. For now leaving option_defs // empty will imply creation of generic Option. @@ -521,6 +529,7 @@ size_t LibDHCP::unpackOptions4(const OptionBuffer& buf, opt = OptionPtr(new Option(Option::V4, opt_type, buf.begin() + offset, buf.begin() + offset + opt_len)); + opt->setEncapsulatedSpace(DHCP4_OPTION_SPACE); } else { // The option definition has been found. Use it to create // the option instance from the provided buffer chunk. diff --git a/src/lib/dhcp/tests/libdhcp++_unittest.cc b/src/lib/dhcp/tests/libdhcp++_unittest.cc index d6e8393d53..3bc108a03e 100644 --- a/src/lib/dhcp/tests/libdhcp++_unittest.cc +++ b/src/lib/dhcp/tests/libdhcp++_unittest.cc @@ -51,7 +51,19 @@ const uint16_t OPTION_CM_MAC = 1026; class LibDhcpTest : public ::testing::Test { public: - LibDhcpTest() { } + /// @brief Constructor. + /// + /// Removes runtime option definitions. + LibDhcpTest() { + LibDHCP::clearRuntimeOptionDefs(); + } + + /// @brief Destructor. + /// + /// Removes runtime option definitions. + virtual ~LibDhcpTest() { + LibDHCP::clearRuntimeOptionDefs(); + } /// @brief Generic factory function to create any option. /// @@ -510,16 +522,20 @@ TEST_F(LibDhcpTest, unpackOptions6) { /// For simplicity, we assign data of the length 3 for each /// of them. static uint8_t v4_opts[] = { - 12, 3, 0, 1, 2, // Hostname - 60, 3, 10, 11, 12, // Class Id - 14, 3, 20, 21, 22, // Merit Dump File - 254, 3, 30, 31, 32, // Reserved - 128, 3, 40, 41, 42, // Vendor specific - 0x52, 0x19, // RAI + 12, 3, 0, 1, 2, // Hostname + 60, 3, 10, 11, 12, // Class Id + 14, 3, 20, 21, 22, // Merit Dump File + 254, 3, 30, 31, 32, // Reserved + 128, 3, 40, 41, 42, // Vendor specific + 125, 7, 0, 0, 9, 0xBF, // V-I Vendor-Specific Information + 2, 0xDC, 0, // Suboption of VIVSI + 43, 2, // Vendor Specific Information + 0xDC, 0, // VSI suboption + 0x52, 0x19, // RAI 0x01, 0x04, 0x20, 0x00, 0x00, 0x02, // Agent Circuit ID 0x02, 0x06, 0x20, 0xE5, 0x2A, 0xB8, 0x15, 0x14, // Agent Remote ID 0x09, 0x09, 0x00, 0x00, 0x11, 0x8B, 0x04, // Vendor Specific Information - 0x01, 0x02, 0x03, 0x00 // Vendor Specific Information continued + 0x01, 0x02, 0x03, 0x00 // Vendor Specific Information continued }; // This test verifies that pack options for v4 is working correctly. @@ -539,6 +555,13 @@ TEST_F(LibDhcpTest, packOptions4) { OptionPtr opt4(new Option(Option::V4,254, payload[3])); OptionPtr opt5(new Option(Option::V4,128, payload[4])); + OptionVendorPtr vivsi(new OptionVendor(Option::V4, 0x9BF)); + vivsi->addOption(OptionPtr(new Option(Option::V4, 0xDC, OptionBuffer()))); + + OptionPtr vsi(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS, + OptionBuffer())); + vsi->addOption(OptionPtr(new Option(Option::V4, 0xDC, OptionBuffer()))); + // Add RAI option, which comprises 3 sub-options. // Get the option definition for RAI option. This option is represented @@ -556,19 +579,19 @@ TEST_F(LibDhcpTest, packOptions4) { // Create Ciruit ID sub-option and add to RAI. OptionPtr circuit_id(new Option(Option::V4, RAI_OPTION_AGENT_CIRCUIT_ID, - OptionBuffer(v4_opts + 29, - v4_opts + 33))); + OptionBuffer(v4_opts + 42, + v4_opts + 46))); rai->addOption(circuit_id); // Create Remote ID option and add to RAI. OptionPtr remote_id(new Option(Option::V4, RAI_OPTION_REMOTE_ID, - OptionBuffer(v4_opts + 35, v4_opts + 41))); + OptionBuffer(v4_opts + 48, v4_opts + 54))); rai->addOption(remote_id); // Create Vendor Specific Information and add to RAI. - OptionPtr vsi(new Option(Option::V4, RAI_OPTION_VSI, - OptionBuffer(v4_opts + 43, v4_opts + 52))); - rai->addOption(vsi); + OptionPtr rai_vsi(new Option(Option::V4, RAI_OPTION_VSI, + OptionBuffer(v4_opts + 56, v4_opts + 65))); + rai->addOption(rai_vsi); isc::dhcp::OptionCollection opts; // list of options // Note that we insert each option under the same option code into @@ -580,6 +603,8 @@ TEST_F(LibDhcpTest, packOptions4) { opts.insert(make_pair(opt1->getType(), opt3)); opts.insert(make_pair(opt1->getType(), opt4)); opts.insert(make_pair(opt1->getType(), opt5)); + opts.insert(make_pair(opt1->getType(), vivsi)); + opts.insert(make_pair(opt1->getType(), vsi)); opts.insert(make_pair(opt1->getType(), rai)); OutputBuffer buf(100); @@ -659,19 +684,50 @@ TEST_F(LibDhcpTest, unpackOptions4) { EXPECT_EQ(0, memcmp(&option14->getValue()[0], v4_opts + 12, 3)); // data len=3 x = options.find(254); - ASSERT_FALSE(x == options.end()); // option 3 should exist + ASSERT_FALSE(x == options.end()); // option 4 should exist EXPECT_EQ(254, x->second->getType()); // this should be option 254 ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3 EXPECT_EQ(5, x->second->len()); // total option length 5 EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4_opts + 17, 3)); // data len=3 x = options.find(128); - ASSERT_FALSE(x == options.end()); // option 3 should exist - EXPECT_EQ(128, x->second->getType()); // this should be option 254 + ASSERT_FALSE(x == options.end()); // option 5 should exist + EXPECT_EQ(128, x->second->getType()); // this should be option 128 ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3 EXPECT_EQ(5, x->second->len()); // total option length 5 EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4_opts + 22, 3)); // data len=3 + // Verify that V-I Vendor Specific Information option is parsed correctly. + x = options.find(125); + ASSERT_FALSE(x == options.end()); + OptionVendorPtr vivsi = boost::dynamic_pointer_cast<OptionVendor>(x->second); + ASSERT_TRUE(vivsi); + EXPECT_EQ(DHO_VIVSO_SUBOPTIONS, vivsi->getType()); + EXPECT_EQ(2495, vivsi->getVendorId()); + OptionCollection suboptions = vivsi->getOptions(); + + // There should be one suboption of V-I VSI. + ASSERT_EQ(1, suboptions.size()); + OptionPtr eso = suboptions.begin()->second; + ASSERT_TRUE(eso); + EXPECT_EQ(0xdc, eso->getType()); + EXPECT_EQ(2, eso->len()); + + // Vendor Specific Information option + x = options.find(43); + ASSERT_FALSE(x == options.end()); + OptionPtr vsi = x->second; + ASSERT_TRUE(vsi); + EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, vsi->getType()); + suboptions = vsi->getOptions(); + + // There should be one suboption of VSI. + ASSERT_EQ(1, suboptions.size()); + eso = suboptions.begin()->second; + ASSERT_TRUE(eso); + EXPECT_EQ(0xdc, eso->getType()); + EXPECT_EQ(2, eso->len()); + // Checking DHCP Relay Agent Information Option. x = options.find(DHO_DHCP_AGENT_OPTIONS); ASSERT_FALSE(x == options.end()); @@ -694,21 +750,21 @@ TEST_F(LibDhcpTest, unpackOptions4) { ASSERT_TRUE(rai_option); EXPECT_EQ(RAI_OPTION_AGENT_CIRCUIT_ID, rai_option->getType()); ASSERT_EQ(6, rai_option->len()); - EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 29, 4)); + EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 42, 4)); // Check that Remote ID option is among parsed options. rai_option = rai->getOption(RAI_OPTION_REMOTE_ID); ASSERT_TRUE(rai_option); EXPECT_EQ(RAI_OPTION_REMOTE_ID, rai_option->getType()); ASSERT_EQ(8, rai_option->len()); - EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 35, 6)); + EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 48, 6)); // Check that Vendor Specific Information option is among parsed options. rai_option = rai->getOption(RAI_OPTION_VSI); ASSERT_TRUE(rai_option); EXPECT_EQ(RAI_OPTION_VSI, rai_option->getType()); ASSERT_EQ(11, rai_option->len()); - EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 43, 9)); + EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 56, 9)); // Make sure, that option other than those above is not present. EXPECT_FALSE(rai->getOption(10)); @@ -725,6 +781,113 @@ TEST_F(LibDhcpTest, unpackOptions4) { } +// Check parsing of an empty option +TEST_F(LibDhcpTest, unpackEmptyOption) { + // Create option definition for the option code 1 without fields. + OptionDefinitionPtr opt_def(new OptionDefinition("option-empty", 1, + "empty", false)); + + // Use it as runtime option definition. + OptionDefSpaceContainer defs; + defs.addItem(opt_def, "space-empty"); + LibDHCP::setRuntimeOptionDefs(defs); + LibDHCP::commitRuntimeOptionDefs(); + + // Create the buffer holding the structure of the empty option. + const uint8_t raw_data[] = { + 0x01, // option code = 1 + 0x00 // option length = 0 + }; + size_t raw_data_len = sizeof(raw_data) / sizeof(uint8_t); + OptionBuffer buf(raw_data, raw_data + raw_data_len); + + // Parse options. + OptionCollection options; + ASSERT_NO_THROW(LibDHCP::unpackOptions4(buf, "space-empty", + options)); + + // There should be one option. + ASSERT_EQ(1, options.size()); + OptionPtr option_empty = options.begin()->second; + ASSERT_TRUE(option_empty); + EXPECT_EQ(1, option_empty->getType()); + EXPECT_EQ(2, option_empty->len()); +} + +// This test verifies that the following option structure can be parsed: +// - option (option space 'foobar') +// - sub option (option space 'foo') +// - sub option (option space 'bar') +// @todo Add more thorough unit tests for unpackOptions. +TEST_F(LibDhcpTest, unpackSubOptions) { + // Create option definition for each level of encapsulation. Each option + // definition is for the option code 1. Options may have the same + // option code because they belong to different option spaces. + + // Top level option encapsulates options which belong to 'space-foo'. + OptionDefinitionPtr opt_def(new OptionDefinition("option-foobar", 1, "uint32", + "space-foo"));\ + // Middle option encapsulates options which belong to 'space-bar' + OptionDefinitionPtr opt_def2(new OptionDefinition("option-foo", 1, "uint16", + "space-bar")); + // Low level option doesn't encapsulate any option space. + OptionDefinitionPtr opt_def3(new OptionDefinition("option-bar", 1, + "uint8")); + + // Register created option definitions as runtime option definitions. + OptionDefSpaceContainer defs; + ASSERT_NO_THROW(defs.addItem(opt_def, "space-foobar")); + ASSERT_NO_THROW(defs.addItem(opt_def2, "space-foo")); + ASSERT_NO_THROW(defs.addItem(opt_def3, "space-bar")); + LibDHCP::setRuntimeOptionDefs(defs); + LibDHCP::commitRuntimeOptionDefs(); + + // Create the buffer holding the structure of options. + const uint8_t raw_data[] = { + // First option starts here. + 0x01, // option code = 1 + 0x0B, // option length = 11 + 0x00, 0x01, 0x02, 0x03, // This option carries uint32 value + // Sub option starts here. + 0x01, // option code = 1 + 0x05, // option length = 5 + 0x01, 0x02, // this option carries uint16 value + // Last option starts here. + 0x01, // option code = 1 + 0x01, // option length = 1 + 0x00 // This option carries a single uint8 + // value and has no sub options. + }; + size_t raw_data_len = sizeof(raw_data) / sizeof(uint8_t); + OptionBuffer buf(raw_data, raw_data + raw_data_len); + + // Parse options. + OptionCollection options; + ASSERT_NO_THROW(LibDHCP::unpackOptions4(buf, "space-foobar", options)); + + // There should be one top level option. + ASSERT_EQ(1, options.size()); + boost::shared_ptr<OptionInt<uint32_t> > option_foobar = + boost::dynamic_pointer_cast<OptionInt<uint32_t> >(options.begin()-> + second); + ASSERT_TRUE(option_foobar); + EXPECT_EQ(1, option_foobar->getType()); + EXPECT_EQ(0x00010203, option_foobar->getValue()); + // There should be a middle level option held in option_foobar. + boost::shared_ptr<OptionInt<uint16_t> > option_foo = + boost::dynamic_pointer_cast<OptionInt<uint16_t> >(option_foobar-> + getOption(1)); + ASSERT_TRUE(option_foo); + EXPECT_EQ(1, option_foo->getType()); + EXPECT_EQ(0x0102, option_foo->getValue()); + // Finally, there should be a low level option under option_foo. + boost::shared_ptr<OptionInt<uint8_t> > option_bar = + boost::dynamic_pointer_cast<OptionInt<uint8_t> >(option_foo->getOption(1)); + ASSERT_TRUE(option_bar); + EXPECT_EQ(1, option_bar->getType()); + EXPECT_EQ(0x0, option_bar->getValue()); +} + TEST_F(LibDhcpTest, isStandardOption4) { // Get all option codes that are not occupied by standard options. const uint16_t unassigned_codes[] = { 84, 96, 102, 103, 104, 105, 106, 107, 108, |