From 87d41d9184394800084502c1a3b80cc056325d03 Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Wed, 21 Feb 2018 17:48:01 +0100 Subject: [PATCH] [5533] Added a negative cache flag (host manager tests to do) --- src/lib/dhcpsrv/host.cc | 13 ++++++++++--- src/lib/dhcpsrv/host.h | 17 ++++++++++++++++- src/lib/dhcpsrv/host_mgr.cc | 22 ++++++++++++++++++++++ src/lib/dhcpsrv/tests/host_unittest.cc | 11 +++++++++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/lib/dhcpsrv/host.cc b/src/lib/dhcpsrv/host.cc index 562bfb4702..134c0acc58 100644 --- a/src/lib/dhcpsrv/host.cc +++ b/src/lib/dhcpsrv/host.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -92,7 +92,8 @@ Host::Host(const uint8_t* identifier, const size_t identifier_len, dhcp6_client_classes_(dhcp6_client_classes), next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()), server_host_name_(server_host_name), boot_file_name_(boot_file_name), - host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) { + host_id_(0), cfg_option4_(new CfgOption()), + cfg_option6_(new CfgOption()), negative_(false) { // Initialize host identifier. setIdentifier(identifier, identifier_len, identifier_type); @@ -125,7 +126,8 @@ Host::Host(const std::string& identifier, const std::string& identifier_name, dhcp6_client_classes_(dhcp6_client_classes), next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()), server_host_name_(server_host_name), boot_file_name_(boot_file_name), - host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) { + host_id_(0), cfg_option4_(new CfgOption()), + cfg_option6_(new CfgOption()), negative_(false) { // Initialize host identifier. setIdentifier(identifier, identifier_name); @@ -589,6 +591,11 @@ Host::toText() const { << "=" << *cclass; } + // Add negative cached. + if (negative_) { + s << " negative cached"; + } + return (s.str()); } diff --git a/src/lib/dhcpsrv/host.h b/src/lib/dhcpsrv/host.h index cf3466924b..20d8e4e554 100644 --- a/src/lib/dhcpsrv/host.h +++ b/src/lib/dhcpsrv/host.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -546,6 +546,18 @@ public: return (host_id_); } + /// @brief Sets the negative cached flag. + /// + /// @param negative New valie for negative cache flag. + void setNegative(bool negative) { + negative_ = negative; + } + + /// @brief Return the negative cache flag value. + bool getNegative() const { + return (negative_); + } + /// @brief Unparses (converts to Element representation) IPv4 host /// /// @return Element representation of the host @@ -604,6 +616,9 @@ private: CfgOptionPtr cfg_option4_; /// @brief Pointer to the DHCPv6 option data configuration for this host. CfgOptionPtr cfg_option6_; + + /// @brief Negative cached flag. + bool negative_; }; /// @brief Pointer to the @c Host object. diff --git a/src/lib/dhcpsrv/host_mgr.cc b/src/lib/dhcpsrv/host_mgr.cc index 8b62d54235..5892e480f5 100644 --- a/src/lib/dhcpsrv/host_mgr.cc +++ b/src/lib/dhcpsrv/host_mgr.cc @@ -133,6 +133,9 @@ HostMgr::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr, host = (*it)->get4(subnet_id, hwaddr, DuidPtr()); } } + if (host && host->getNegative()) { + return (ConstHostPtr()); + } return (host); } @@ -168,6 +171,9 @@ HostMgr::get4(const SubnetID& subnet_id, .arg((*it)->getType()) .arg(host->toText()); + if (host->getNegative()) { + return (ConstHostPtr()); + } return (host); } } @@ -194,6 +200,9 @@ HostMgr::get4(const SubnetID& subnet_id, !host && it != alternate_sources_.end(); ++it) { host = (*it)->get4(subnet_id, address); } + if (host && host->getNegative()) { + return (ConstHostPtr()); + } return (host); } @@ -220,6 +229,9 @@ HostMgr::get6(const SubnetID& subnet_id, const DuidPtr& duid, host = (*it)->get6(subnet_id, DuidPtr(), hwaddr); } } + if (host && host->getNegative()) { + return (ConstHostPtr()); + } return (host); } @@ -237,6 +249,9 @@ HostMgr::get6(const IOAddress& prefix, const uint8_t prefix_len) const { !host && it != alternate_sources_.end(); ++it) { host = (*it)->get6(prefix, prefix_len); } + if (host && host->getNegative()) { + return (ConstHostPtr()); + } return (host); } @@ -272,6 +287,10 @@ HostMgr::get6(const SubnetID& subnet_id, identifier_len)) .arg((*it)->getType()) .arg(host->toText()); + + if (host->getNegative()) { + return (ConstHostPtr()); + } return (host); } } @@ -300,6 +319,9 @@ HostMgr::get6(const SubnetID& subnet_id, !host && it != alternate_sources_.end(); ++it) { host = (*it)->get6(subnet_id, addr); } + if (host && host->getNegative()) { + return (ConstHostPtr()); + } return (host); } diff --git a/src/lib/dhcpsrv/tests/host_unittest.cc b/src/lib/dhcpsrv/tests/host_unittest.cc index d3603fbae7..88216ed548 100644 --- a/src/lib/dhcpsrv/tests/host_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -669,6 +669,7 @@ TEST_F(HostTest, setValues) { ASSERT_EQ("192.0.2.3", host->getIPv4Reservation().toText()); ASSERT_EQ("some-host.example.org", host->getHostname()); ASSERT_FALSE(host->getContext()); + ASSERT_FALSE(host->getNegative()); host->setIPv4SubnetID(SubnetID(123)); host->setIPv6SubnetID(SubnetID(234)); @@ -679,6 +680,7 @@ TEST_F(HostTest, setValues) { host->setBootFileName("bootfile.efi"); std::string user_context = "{ \"foo\": \"bar\" }"; host->setContext(Element::fromJSON(user_context)); + host->setNegative(true); EXPECT_EQ(123, host->getIPv4SubnetID()); EXPECT_EQ(234, host->getIPv6SubnetID()); @@ -689,6 +691,7 @@ TEST_F(HostTest, setValues) { EXPECT_EQ("bootfile.efi", host->getBootFileName()); ASSERT_TRUE(host->getContext()); EXPECT_EQ(user_context, host->getContext()->str()); + EXPECT_TRUE(host->getNegative()); // Remove IPv4 reservation. host->removeIPv4Reservation(); @@ -981,6 +984,7 @@ TEST_F(HostTest, toText) { host->setHostname(""); host->removeIPv4Reservation(); host->setIPv4SubnetID(0); + host->setNegative(true); EXPECT_EQ("hwaddr=010203040506 ipv6_subnet_id=2" " hostname=(empty) ipv4_reservation=(no)" @@ -990,7 +994,8 @@ TEST_F(HostTest, toText) { " ipv6_reservation0=2001:db8:1::cafe" " ipv6_reservation1=2001:db8:1::1" " ipv6_reservation2=2001:db8:1:1::/64" - " ipv6_reservation3=2001:db8:1:2::/64", + " ipv6_reservation3=2001:db8:1:2::/64" + " negative cached", host->toText()); // Create host identified by DUID, instead of HWADDR, with a very @@ -1139,6 +1144,8 @@ TEST_F(HostTest, unparse) { // Add some classes. host->addClientClass4("modem"); host->addClientClass4("router"); + // Set invisible negative cache. + host->setNegative(true); EXPECT_EQ("{ " "\"boot-file-name\": \"\", " -- 2.47.3