Kea: ncr_generator.cc Source File

| Kea 2.2.0

ncr_generator.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2020 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
9#include <dhcp/option_data_types.h>
10#include <dhcpsrv/cfgmgr.h>
11#include <dhcpsrv/dhcpsrv_log.h>
12#include <dhcpsrv/d2_client_mgr.h>
13#include <dhcpsrv/ncr_generator.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 const bool use_conflict_resolution = true) {
42
43 // Check if there is a need for update.
44 if (lease->hostname_.empty() || (!lease->fqdn_fwd_ && !lease->fqdn_rev_)
45 || !CfgMgr::instance().getD2ClientMgr().ddnsEnabled()) {
46 LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
47 DHCPSRV_QUEUE_NCR_SKIP)
48 .arg(label)
49 .arg(lease->addr_.toText());
50
51 return;
52 }
53
54 try {
55 // Create DHCID
56 std::vector<uint8_t> hostname_wire;
57 OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
58 D2Dhcid dhcid = D2Dhcid(identifier, hostname_wire);
59
60 // Calculate the TTL based on lease life time.
61 uint32_t ttl = calculateDdnsTtl(lease->valid_lft_);
62
63 // Create name change request.
64 NameChangeRequestPtr ncr
65 (new NameChangeRequest(chg_type, lease->fqdn_fwd_, lease->fqdn_rev_,
66 lease->hostname_, lease->addr_.toText(),
67 dhcid, lease->cltt_ + ttl,
68 ttl, use_conflict_resolution));
69
70 LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL_DATA, DHCPSRV_QUEUE_NCR)
71 .arg(label)
72 .arg(chg_type == CHG_ADD ? "add" : "remove")
73 .arg(ncr->toText());
74
75 // Send name change request.
76 CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
77
78 } catch (const std::exception& ex) {
79 LOG_ERROR(dhcpsrv_logger, DHCPSRV_QUEUE_NCR_FAILED)
80 .arg(label)
81 .arg(chg_type == CHG_ADD ? "add" : "remove")
82 .arg(lease->addr_.toText())
83 .arg(ex.what());
84 }
85}
86
87} // end of anonymous namespace
88
89namespace isc {
90namespace dhcp {
91
92 void queueNCR(const NameChangeType& chg_type, const Lease4Ptr& lease) {
93 if (lease) {
94 // Figure out from the lease's subnet if we should use conflict resolution.
95 // If there's no subnet, something hinky is going on so we'll set it true.
96 bool use_cr = true;
97 Subnet4Ptr subnet = CfgMgr::instance().getCurrentCfg()
98 ->getCfgSubnets4()->getSubnet(lease->subnet_id_);
99 if (subnet) {
100 // We should always have subnet.
101 use_cr = subnet->getDdnsUseConflictResolution();
102 }
103
104 // Client id takes precedence over HW address.
105 if (lease->client_id_) {
106 queueNCRCommon(chg_type, lease, lease->client_id_->getClientId(),
107 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_), use_cr);
108
109 } else {
110 // Client id is not specified for the lease. Use HW address
111 // instead.
112 queueNCRCommon(chg_type, lease, lease->hwaddr_,
113 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_), use_cr);
114 }
115 }
116}
117
118 void queueNCR(const NameChangeType& chg_type, const Lease6Ptr& lease) {
119 // DUID is required to generate NCR.
120 if (lease && (lease->type_ != Lease::TYPE_PD) && lease->duid_) {
121 // Figure out from the lease's subnet if we should use conflict resolution.
122 // If there's no subnet, something hinky is going on so we'll set it true.
123 bool use_cr = true;
124 Subnet6Ptr subnet = CfgMgr::instance().getCurrentCfg()
125 ->getCfgSubnets6()->getSubnet(lease->subnet_id_);
126 if (subnet) {
127 // We should always have subnet.
128 use_cr = subnet->getDdnsUseConflictResolution();
129 }
130
131 queueNCRCommon(chg_type, lease, *(lease->duid_),
132 Pkt6::makeLabel(lease->duid_, lease->hwaddr_), use_cr);
133 }
134}
135
136uint32_t calculateDdnsTtl(uint32_t lease_lft) {
137 // Per RFC 4702 DDNS RR TTL should be given by:
138 // ((lease life time / 3) < 10 minutes) ? 10 minutes : (lease life time / 3)
139 if (lease_lft < 1800) {
140 return (600);
141 }
142
143 return (lease_lft / 3);
144}
145
146}
147}
cfgmgr.h
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
isc::dhcp::CfgMgr::getCurrentCfg
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:161
isc::dhcp::Pkt4::makeLabel
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
isc::dhcp::Pkt6::makeLabel
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:580
isc::dhcp_ddns::D2Dhcid
Container class for handling the DHCID value within a NameChangeRequest.
Definition: ncr_msg.h:86
isc::dhcp_ddns::NameChangeRequest
Represents a DHCP-DDNS client request.
Definition: ncr_msg.h:227
d2_client_mgr.h
Defines the D2ClientMgr class.
dhcpsrv_log.h
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
LOG_DEBUG
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
isc::dhcp_ddns
Definition: dhcp_ddns_log.cc:14
isc::dhcp_ddns::NameChangeRequestPtr
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:212
isc::dhcp_ddns::NameChangeType
NameChangeType
Defines the types of DNS updates that can be requested.
Definition: ncr_msg.h:46
isc::dhcp_ddns::CHG_ADD
@ CHG_ADD
Definition: ncr_msg.h:47
isc::dhcp
Definition: dhcp4/client_handler.cc:20
isc::dhcp::DHCPSRV_DBG_TRACE_DETAIL_DATA
const int DHCPSRV_DBG_TRACE_DETAIL_DATA
Additional information.
Definition: dhcpsrv_log.h:43
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
isc::dhcp::Subnet4Ptr
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:524
isc::dhcp::DHCPSRV_QUEUE_NCR
const isc::log::MessageID DHCPSRV_QUEUE_NCR
Definition: dhcpsrv_messages.h:218
isc::dhcp::queueNCR
void queueNCR(const NameChangeType &chg_type, const Lease4Ptr &lease)
Creates name change request from the DHCPv4 lease.
Definition: ncr_generator.cc:92
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:503
isc::dhcp::Subnet6Ptr
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:672
isc::dhcp::DHCPSRV_QUEUE_NCR_FAILED
const isc::log::MessageID DHCPSRV_QUEUE_NCR_FAILED
Definition: dhcpsrv_messages.h:219
isc::dhcp::DHCPSRV_DBG_TRACE_DETAIL
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Definition: dhcpsrv_log.h:38
isc::dhcp::calculateDdnsTtl
uint32_t calculateDdnsTtl(uint32_t lease_lft)
Calculates TTL for a DNS resource record based on lease life time.
Definition: ncr_generator.cc:136
isc::dhcp::DHCPSRV_QUEUE_NCR_SKIP
const isc::log::MessageID DHCPSRV_QUEUE_NCR_SKIP
Definition: dhcpsrv_messages.h:220
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:284
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
ncr_generator.h
option_data_types.h
isc::dhcp::Lease::TYPE_PD
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition: lease.h:49