From ef5973b56420a5a1846bfc6da8c1b4f40dc38fda Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Fri, 13 Nov 2015 22:02:04 +0100 Subject: [PATCH] [4121] Cloned LibDHCP::packOptions and improved the v4 version --- src/lib/dhcp/libdhcp++.cc | 33 ++++++++++++++++++++++++ src/lib/dhcp/libdhcp++.h | 10 +++++++ src/lib/dhcp/pkt4.cc | 2 +- src/lib/dhcp/pkt6.cc | 2 +- src/lib/dhcp/tests/libdhcp++_unittest.cc | 7 ++--- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/lib/dhcp/libdhcp++.cc b/src/lib/dhcp/libdhcp++.cc index 069f09a04a..bbc7f82367 100644 --- a/src/lib/dhcp/libdhcp++.cc +++ b/src/lib/dhcp/libdhcp++.cc @@ -682,6 +682,39 @@ LibDHCP::packOptions(isc::util::OutputBuffer& buf, } } +void +LibDHCP::packOptions4(isc::util::OutputBuffer& buf, + const OptionCollection& options) { + OptionCollection::const_iterator it = options.begin(); + for (; it != options.end(); ++it) { + // Some options must be last + if ((it->first == DHO_DHCP_AGENT_OPTIONS) || + (it->first == DHO_END)) { + continue; + } + it->second->pack(buf); + } + // Add the RAI option if it exists + it = options.find(DHO_DHCP_AGENT_OPTIONS); + if (it != options.end()) { + it->second->pack(buf); + } + // And at the end the END option + it = options.find(DHO_END); + if (it != options.end()) { + it->second->pack(buf); + } +} + +void +LibDHCP::packOptions6(isc::util::OutputBuffer& buf, + const OptionCollection& options) { + for (OptionCollection::const_iterator it = options.begin(); + it != options.end(); ++it) { + it->second->pack(buf); + } +} + void LibDHCP::OptionFactoryRegister(Option::Universe u, uint16_t opt_type, Option::Factory* factory) { diff --git a/src/lib/dhcp/libdhcp++.h b/src/lib/dhcp/libdhcp++.h index 57fac075bf..eaac60d7a4 100644 --- a/src/lib/dhcp/libdhcp++.h +++ b/src/lib/dhcp/libdhcp++.h @@ -134,9 +134,19 @@ public: /// /// @param buf output buffer (assembled options will be stored here) /// @param options collection of options to store to + + /// Generic version: to be used when there is no specific order static void packOptions(isc::util::OutputBuffer& buf, const isc::dhcp::OptionCollection& options); + /// DHCPv4 version: put some options last + static void packOptions4(isc::util::OutputBuffer& buf, + const isc::dhcp::OptionCollection& options); + + /// DHCPv6 version (currently same than the generic one) + static void packOptions6(isc::util::OutputBuffer& buf, + const isc::dhcp::OptionCollection& options); + /// @brief Parses provided buffer as DHCPv6 options and creates /// Option objects. /// diff --git a/src/lib/dhcp/pkt4.cc b/src/lib/dhcp/pkt4.cc index cae19d0754..e1e6673499 100644 --- a/src/lib/dhcp/pkt4.cc +++ b/src/lib/dhcp/pkt4.cc @@ -143,7 +143,7 @@ Pkt4::pack() { // write DHCP magic cookie buffer_out_.writeUint32(DHCP_OPTIONS_COOKIE); - LibDHCP::packOptions(buffer_out_, options_); + LibDHCP::packOptions4(buffer_out_, options_); // add END option that indicates end of options // (End option is very simple, just a 255 octet) diff --git a/src/lib/dhcp/pkt6.cc b/src/lib/dhcp/pkt6.cc index 33b4633722..6f95ef930a 100644 --- a/src/lib/dhcp/pkt6.cc +++ b/src/lib/dhcp/pkt6.cc @@ -244,7 +244,7 @@ Pkt6::packUDP() { buffer_out_.writeUint8( (transid_) & 0xff ); // the rest are options - LibDHCP::packOptions(buffer_out_, options_); + LibDHCP::packOptions6(buffer_out_, options_); } catch (const Exception& e) { // An exception is thrown and message will be written to Logger diff --git a/src/lib/dhcp/tests/libdhcp++_unittest.cc b/src/lib/dhcp/tests/libdhcp++_unittest.cc index 048e31f361..b80931818a 100644 --- a/src/lib/dhcp/tests/libdhcp++_unittest.cc +++ b/src/lib/dhcp/tests/libdhcp++_unittest.cc @@ -342,7 +342,7 @@ TEST_F(LibDhcpTest, packOptions6) { OutputBuffer assembled(512); - EXPECT_NO_THROW(LibDHCP::packOptions(assembled, opts)); + EXPECT_NO_THROW(LibDHCP::packOptions6(assembled, opts)); EXPECT_EQ(sizeof(v6packed), assembled.getLength()); EXPECT_EQ(0, memcmp(assembled.getData(), v6packed, sizeof(v6packed))); } @@ -528,15 +528,16 @@ TEST_F(LibDhcpTest, packOptions4) { // the map. This gurantees that options are packed in the same order // they were added. Otherwise, options would get sorted by code and // the resulting buffer wouldn't match with the reference buffer. + // But this doesn't apply to the RAI code which is always the last one opts.insert(make_pair(opt1->getType(), opt1)); opts.insert(make_pair(opt1->getType(), opt2)); + opts.insert(make_pair(rai->getType(), rai)); 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(), rai)); OutputBuffer buf(100); - EXPECT_NO_THROW(LibDHCP::packOptions(buf, opts)); + EXPECT_NO_THROW(LibDHCP::packOptions4(buf, opts)); ASSERT_EQ(buf.getLength(), sizeof(v4_opts)); EXPECT_EQ(0, memcmp(v4_opts, buf.getData(), sizeof(v4_opts))); } -- 2.47.2