Kea: ncr_generator.cc Source File

| Kea 1.5.0

ncr_generator.cc
Go to the documentation of this file.
1// Copyright (C) 2015 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
36template<typename LeasePtrType, typename IdentifierType>
37void queueNCRCommon(const NameChangeType& chg_type, const LeasePtrType& lease,
38 const IdentifierType& identifier, const std::string& label) {
39
40 // Check if there is a need for update.
41 if (lease->hostname_.empty() || (!lease->fqdn_fwd_ && !lease->fqdn_rev_)
42 || !CfgMgr::instance().getD2ClientMgr().ddnsEnabled()) {
43 LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
44 DHCPSRV_QUEUE_NCR_SKIP)
45 .arg(label)
46 .arg(lease->addr_.toText());
47
48 return;
49 }
50
51 try {
52 // Create DHCID
53 std::vector<uint8_t> hostname_wire;
54 OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
55 D2Dhcid dhcid = D2Dhcid(identifier, hostname_wire);
56
57 // Create name change request.
58 NameChangeRequestPtr ncr
59 (new NameChangeRequest(chg_type, lease->fqdn_fwd_, lease->fqdn_rev_,
60 lease->hostname_, lease->addr_.toText(),
61 dhcid, lease->cltt_ + lease->valid_lft_,
62 lease->valid_lft_));
63
64 LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL_DATA, DHCPSRV_QUEUE_NCR)
65 .arg(label)
66 .arg(chg_type == CHG_ADD ? "add" : "remove")
67 .arg(ncr->toText());
68
69 // Send name change request.
70 CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
71
72 } catch (const std::exception& ex) {
73 LOG_ERROR(dhcpsrv_logger, DHCPSRV_QUEUE_NCR_FAILED)
74 .arg(label)
75 .arg(chg_type == CHG_ADD ? "add" : "remove")
76 .arg(lease->addr_.toText())
77 .arg(ex.what());
78 }
79}
80
81} // end of anonymous namespace
82
83namespace isc {
84namespace dhcp {
85
86 void queueNCR(const NameChangeType& chg_type, const Lease4Ptr& lease) {
87 if (lease) {
88 // Client id takes precedence over HW address.
89 if (lease->client_id_) {
90 queueNCRCommon(chg_type, lease, lease->client_id_->getClientId(),
91 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_));
92
93 } else {
94 // Client id is not specified for the lease. Use HW address
95 // instead.
96 queueNCRCommon(chg_type, lease, lease->hwaddr_,
97 Pkt4::makeLabel(lease->hwaddr_, lease->client_id_));
98 }
99 }
100}
101
102 void queueNCR(const NameChangeType& chg_type, const Lease6Ptr& lease) {
103 // DUID is required to generate NCR.
104 if (lease && (lease->type_ != Lease::TYPE_PD) && lease->duid_) {
105 queueNCRCommon(chg_type, lease, *(lease->duid_),
106 Pkt6::makeLabel(lease->duid_, lease->hwaddr_));
107 }
108}
109
110}
111}
cfgmgr.h
isc::dhcp::CfgMgr::getD2ClientMgr
D2ClientMgr & getD2ClientMgr()
Fetches the DHCP-DDNS manager.
Definition: cfgmgr.cc:65
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
isc::dhcp::D2ClientMgr::ddnsEnabled
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS is enabled.
Definition: d2_client_mgr.cc:106
isc::dhcp::D2ClientMgr::sendRequest
void sendRequest(dhcp_ddns::NameChangeRequestPtr &ncr)
Send the given NameChangeRequests to kea-dhcp-ddns.
Definition: d2_client_mgr.cc:283
isc::dhcp::OptionDataTypeUtil::writeFqdn
static void writeFqdn(const std::string &fqdn, std::vector< uint8_t > &buf, const bool downcase=false)
Append FQDN into a buffer.
Definition: option_data_types.cc:333
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:360
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:585
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:214
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: ctrl_dhcp4_srv.cc:75
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::queueNCR
void queueNCR(const NameChangeType &chg_type, const Lease4Ptr &lease)
Creates name change request from the DHCPv4 lease.
Definition: ncr_generator.cc:86
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:463
isc::dhcp::DHCPSRV_DBG_TRACE_DETAIL
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Definition: dhcpsrv_log.h:38
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:248
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
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:41