diff options
author | Thomas Markwalder <tmark@isc.org> | 2021-07-30 17:09:40 +0200 |
---|---|---|
committer | Thomas Markwalder <tmark@isc.org> | 2021-08-05 19:16:44 +0200 |
commit | a8f61ee95daea74a70b7281cd518703225cba556 (patch) | |
tree | 7fbb39bb204046a708a1383513dde3039d103cf7 /src/lib/dhcpsrv | |
parent | [#1993] hammer.py: support for NETCONF (diff) | |
download | kea-a8f61ee95daea74a70b7281cd518703225cba556.tar.xz kea-a8f61ee95daea74a70b7281cd518703225cba556.zip |
[#1529] Avoid duplicating qualifying suffix
src/lib/dhcpsrv/d2_client_mgr.*
D2ClientMgr::qualifyName() - modified to only add the qualifying
suffix if the input name does not already end with it.
src/lib/dhcpsrv/tests/d2_client_unittest.cc
TEST_F(D2ClientMgrParamsTest, qualifyNameWithoutDuplicatingSuffix) -
new test
Diffstat (limited to 'src/lib/dhcpsrv')
-rw-r--r-- | src/lib/dhcpsrv/d2_client_mgr.cc | 43 | ||||
-rw-r--r-- | src/lib/dhcpsrv/d2_client_mgr.h | 3 | ||||
-rw-r--r-- | src/lib/dhcpsrv/tests/d2_client_unittest.cc | 36 |
3 files changed, 77 insertions, 5 deletions
diff --git a/src/lib/dhcpsrv/d2_client_mgr.cc b/src/lib/dhcpsrv/d2_client_mgr.cc index 6671471309..3ba12d172c 100644 --- a/src/lib/dhcpsrv/d2_client_mgr.cc +++ b/src/lib/dhcpsrv/d2_client_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2021 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 @@ -186,14 +186,47 @@ D2ClientMgr::qualifyName(const std::string& partial_name, gen_name << partial_name; std::string suffix = ddns_params.getQualifyingSuffix(); + bool suffix_present = true; if (!suffix.empty()) { std::string str = gen_name.str(); - size_t len = str.length(); - if ((len > 0) && (str[len - 1] != '.')) { - gen_name << "."; + auto suffix_rit = suffix.rbegin(); + if (*suffix_rit == '.') { + ++suffix_rit; + } + + auto gen_rit = str.rbegin(); + if (*gen_rit == '.') { + ++gen_rit; } - gen_name << suffix; + while (suffix_rit != suffix.rend()) { + if ((gen_rit == str.rend()) || + (*suffix_rit != *gen_rit)) { + // They don't match. + suffix_present = false; + break; + } + + ++suffix_rit; + ++gen_rit; + } + + // Catch the case where name has suffix embedded. + // input: foo.barexample.com suffix: example.com + if ((suffix_present) && (suffix_rit == suffix.rend())) { + if (*gen_rit != '.') { + suffix_present = false; + } + } + + if (!suffix_present) { + size_t len = str.length(); + if ((len > 0) && (str[len - 1] != '.')) { + gen_name << "."; + } + + gen_name << suffix; + } } std::string str = gen_name.str(); diff --git a/src/lib/dhcpsrv/d2_client_mgr.h b/src/lib/dhcpsrv/d2_client_mgr.h index bfa611d8ad..33eb55a398 100644 --- a/src/lib/dhcpsrv/d2_client_mgr.h +++ b/src/lib/dhcpsrv/d2_client_mgr.h @@ -184,6 +184,9 @@ public: /// /// <partial_name>.<qualifying-suffix>. /// + /// Note that the qualifying suffix will only be appended if the + /// input name does not already end with that suffix. + /// /// @param partial_name domain name to qualify /// @param ddns_params DDNS behavioral configuration parameters /// @param trailing_dot A boolean value which when true guarantees the diff --git a/src/lib/dhcpsrv/tests/d2_client_unittest.cc b/src/lib/dhcpsrv/tests/d2_client_unittest.cc index f246c152d8..6b900877b1 100644 --- a/src/lib/dhcpsrv/tests/d2_client_unittest.cc +++ b/src/lib/dhcpsrv/tests/d2_client_unittest.cc @@ -665,6 +665,42 @@ TEST_F(D2ClientMgrParamsTest, qualifyName) { } +/// @brief Tests the qualifyName method's ability to avoid duplicating +/// qualifying suffix. +TEST_F(D2ClientMgrParamsTest, qualifyNameWithoutDuplicatingSuffix) { + D2ClientMgr mgr; + bool do_not_dot = false; + bool do_dot = true; + + // Create enabled configuration + subnet_->setDdnsSendUpdates(true); + subnet_->setDdnsOverrideNoUpdate(false); + subnet_->setDdnsOverrideClientUpdate(false); + subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER); + subnet_->setDdnsGeneratedPrefix("prefix"); + subnet_->setDdnsQualifyingSuffix("suffix.com"); + subnet_->setHostnameCharSet(""); + subnet_->setHostnameCharReplacement(""); + + // Verify that the qualifying suffix does not get appended when the + // input name has the suffix but no trailing dot. + std::string partial_name = "somehost.suffix.com"; + std::string qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot); + EXPECT_EQ("somehost.suffix.com.", qualified_name); + + // Verify that the qualifying suffix does not get appended when the + // input name has the suffix and a trailing dot. + partial_name = "somehost.suffix.com."; + qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot); + EXPECT_EQ("somehost.suffix.com.", qualified_name); + + // Verify that the qualifying suffix does get appended when the + // input name has the suffix embedded in it. + partial_name = "somehost.almostsuffix.com"; + qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot); + EXPECT_EQ("somehost.almostsuffix.com.suffix.com.", qualified_name); +} + /// @brief Tests the generateFdqn method's ability to construct FQDNs TEST_F(D2ClientMgrParamsTest, generateFqdn) { D2ClientMgr mgr; |