Kea 2.7.5
ncr_generator.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <dhcpsrv/cfgmgr.h>
11#include <dhcpsrv/dhcpsrv_log.h>
14#include <stdint.h>
15#include <vector>
16
17using namespace isc;
18using namespace isc::dhcp;
19using namespace isc::dhcp_ddns;
20
21namespace {
22
38template<typename LeasePtrType, typename IdentifierType>
39void queueNCRCommon(const NameChangeType& chg_type, const LeasePtrType& lease,
40 const IdentifierType& identifier, const std::string& label,
41 NetworkPtr subnet) {
42 // Check if there is a need for update.
43 if (lease->hostname_.empty() || (!lease->fqdn_fwd_ && !lease->fqdn_rev_)
44 || !CfgMgr::instance().getD2ClientMgr().ddnsEnabled()) {
47 .arg(label)
48 .arg(lease->addr_.toText());
49
50 return;
51 }
52
53 ConflictResolutionMode conflict_resolution_mode = CHECK_WITH_DHCID;
54 util::Optional<double> ddns_ttl_percent;
55 if (subnet) {
56 auto mode = subnet->getDdnsConflictResolutionMode();
57 if (!mode.empty()) {
58 conflict_resolution_mode = StringToConflictResolutionMode(mode);
59 }
60
61 ddns_ttl_percent = subnet->getDdnsTtlPercent();
62 }
63
64 try {
65 // Create DHCID
66 std::vector<uint8_t> hostname_wire;
67 OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
68 D2Dhcid dhcid = D2Dhcid(identifier, hostname_wire);
69
70 // Calculate the TTL based on lease life time.
71 uint32_t ttl = calculateDdnsTtl(lease->valid_lft_, ddns_ttl_percent);
72
73 // Create name change request.
75 (new NameChangeRequest(chg_type, lease->fqdn_fwd_, lease->fqdn_rev_,
76 lease->hostname_, lease->addr_.toText(),
77 dhcid, lease->cltt_ + ttl,
78 ttl, conflict_resolution_mode));
79
81 .arg(label)
82 .arg(chg_type == CHG_ADD ? "add" : "remove")
83 .arg(ncr->toText());
84
85 // Send name change request.
86 CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
87
88 } catch (const std::exception& ex) {
90 .arg(label)
91 .arg(chg_type == CHG_ADD ? "add" : "remove")
92 .arg(lease->addr_.toText())
93 .arg(ex.what());
94 }
95}
96
97} // end of anonymous namespace
98
99namespace isc {
100namespace dhcp {
101
102void queueNCR(const NameChangeType& chg_type, const Lease4Ptr& lease) {
103 if (lease) {
104 // Figure out from the lease's subnet if we should use conflict resolution.
105 // If there's no subnet, something hinky is going on so we'll set it true.
106 Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()
107 ->getCfgSubnets4()->getSubnet(lease->subnet_id_);
108
109 // Client id takes precedence over HW address.
110 if (lease->client_id_) {
111 queueNCRCommon(chg_type, lease, lease->client_id_->getClientId(),
112 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_), subnet);
113 } else {
114 // Client id is not specified for the lease. Use HW address
115 // instead.
116 queueNCRCommon(chg_type, lease, lease->hwaddr_,
117 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_), subnet);
118 }
119 }
120}
121
122void queueNCR(const NameChangeType& chg_type, const Lease6Ptr& lease) {
123 // DUID is required to generate NCR.
124 if (lease && (lease->type_ != Lease::TYPE_PD) && lease->duid_) {
125 // Figure out from the lease's subnet if we should use conflict resolution.
126 // If there's no subnet, something hinky is going on so we'll set it true.
127 Subnet6Ptr subnet = CfgMgr::instance().getCurrentCfg()
128 ->getCfgSubnets6()->getSubnet(lease->subnet_id_);
129 queueNCRCommon(chg_type, lease, *(lease->duid_),
130 Pkt6::makeLabel(lease->duid_, lease->hwaddr_), subnet);
131 }
132}
133
134uint32_t calculateDdnsTtl(uint32_t lease_lft, const util::Optional<double>& ddns_ttl_percent) {
135 // If we have a configured percentage use it to calculate TTL.
136 if (!ddns_ttl_percent.unspecified() && (ddns_ttl_percent.get() > 0.0)) {
137 uint32_t new_lft = static_cast<uint32_t>(round(ddns_ttl_percent.get() * lease_lft));
138 if (new_lft > 0) {
139 return (new_lft);
140 } else {
143 .arg(ddns_ttl_percent.get())
144 .arg(lease_lft);
145 }
146 }
147
148 // Per RFC 4702 DDNS RR TTL should be given by:
149 // ((lease life time / 3) < 10 minutes) ? 10 minutes : (lease life time / 3)
150 if (lease_lft < 1800) {
151 return (600);
152 }
153
154 return (lease_lft / 3);
155}
156
157}
158}
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:25
static void writeFqdn(const std::string &fqdn, std::vector< uint8_t > &buf, const bool downcase=false)
Append FQDN into a buffer.
static std::string makeLabel(const HWAddrPtr &hwaddr, const ClientIdPtr &client_id, const uint32_t transid)
Returns text representation of the given packet identifiers.
Definition pkt4.cc:397
static std::string makeLabel(const DuidPtr duid, const uint32_t transid, const HWAddrPtr &hwaddr)
Returns text representation of the given packet identifiers.
Definition pkt6.cc:691
Container class for handling the DHCID value within a NameChangeRequest.
Definition ncr_msg.h:113
Represents a DHCP-DDNS client request.
Definition ncr_msg.h:254
A template representing an optional value.
Definition optional.h:36
Defines the D2ClientMgr class.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
ConflictResolutionMode StringToConflictResolutionMode(const std::string &mode_str)
Function which converts string to ConflictResolutionMode enum values.
Definition ncr_msg.cc:45
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:241
NameChangeType
Defines the types of DNS updates that can be requested.
Definition ncr_msg.h:45
const int DHCPSRV_DBG_TRACE_DETAIL_DATA
Additional information.
Definition dhcpsrv_log.h:43
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
uint32_t calculateDdnsTtl(uint32_t lease_lft, const util::Optional< double > &ddns_ttl_percent)
Calculates TTL for a DNS resource record based on lease life time.
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition subnet.h:461
const isc::log::MessageID DHCPSRV_QUEUE_NCR
void queueNCR(const NameChangeType &chg_type, const Lease4Ptr &lease)
Creates name change request from the DHCPv4 lease.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:508
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition subnet.h:626
const isc::log::MessageID DHCPSRV_QUEUE_NCR_FAILED
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Definition dhcpsrv_log.h:38
const isc::log::MessageID DHCPSRV_DDNS_TTL_PERCENT_TOO_SMALL
const isc::log::MessageID DHCPSRV_QUEUE_NCR_SKIP
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
Definition network.h:73
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:295
Defines the logger used by the top-level component of kea-lfc.
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:49