Kea 1.5.0
nc_trans.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2017 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 <d2/d2_log.h>
10#include <d2/nc_trans.h>
11#include <dns/rdata.h>
12#include <util/random/qid_gen.h>
13
14#include <sstream>
15
16using namespace isc::util;
17using namespace isc::util::random;
18
19namespace isc {
20namespace d2 {
21
22// Common transaction states
28
30
31// Common transaction events
39
41
43
47 DdnsDomainPtr& forward_domain,
48 DdnsDomainPtr& reverse_domain,
49 D2CfgMgrPtr& cfg_mgr)
50 : io_service_(io_service), ncr_(ncr), forward_domain_(forward_domain),
51 reverse_domain_(reverse_domain), dns_client_(), dns_update_request_(),
52 dns_update_status_(DNSClient::OTHER), dns_update_response_(),
53 forward_change_completed_(false), reverse_change_completed_(false),
54 current_server_list_(), current_server_(), next_server_pos_(0),
55 update_attempts_(0), cfg_mgr_(cfg_mgr), tsig_key_() {
58 if (!io_service_) {
59 isc_throw(NameChangeTransactionError, "IOServicePtr cannot be null");
60 }
61
62 if (!ncr_) {
64 "NameChangeRequest cannot be null");
65 }
66
67 if (ncr_->isForwardChange() && !(forward_domain_)) {
69 "Forward change must have a forward domain");
70 }
71
72 if (ncr_->isReverseChange() && !(reverse_domain_)) {
74 "Reverse change must have a reverse domain");
75 }
76
77 if (!cfg_mgr_) {
79 "Configuration manager cannot be null");
80 }
81}
82
84}
85
86void
89 DHCP_DDNS_STARTING_TRANSACTION)
90 .arg(getRequestId());
91
94}
95
96void
98 // Stow the completion status and re-enter the run loop with the event
99 // set to indicate IO completed.
100 // runModel is exception safe so we are good to call it here.
101 // It won't exit until we hit the next IO wait or the state model ends.
102 setDnsUpdateStatus(status);
104 DHCP_DDNS_UPDATE_RESPONSE_RECEIVED)
105 .arg(getRequestId())
106 .arg(current_server_->toText())
107 .arg(responseString());
108
110}
111
112std::string
114 std::ostringstream stream;
115 switch (getDnsUpdateStatus()) {
117 stream << "SUCCESS, rcode: ";
118 if (getDnsUpdateResponse()) {
119 stream << getDnsUpdateResponse()->getRcode().toText();
120 } else {
121 stream << " update response is NULL";
122 }
123 break;
125 stream << "TIMEOUT";
126 break;
128 stream << "IO_STOPPED";
129 break;
131 stream << "INVALID_RESPONSE";
132 break;
133 case DNSClient::OTHER:
134 stream << "OTHER";
135 break;
136 default:
137 stream << "UNKNOWN("
138 << static_cast<int>(getDnsUpdateStatus()) << ")";
139 break;
140
141 }
142
143 return (stream.str());
144}
145
146std::string
148 std::ostringstream stream;
149 stream << "Status: " << (getNcrStatus() == dhcp_ddns::ST_COMPLETED
150 ? "Completed, " : "Failed, ")
151 << "Event: " << getEventLabel(getNextEvent()) << ", ";
152
153 if (ncr_->isForwardChange()) {
154 stream << " Forward change:" << (getForwardChangeCompleted()
155 ? " completed, " : " failed, ");
156 }
157
158 if (ncr_->isReverseChange()) {
159 stream << " Reverse change:" << (getReverseChangeCompleted()
160 ? " completed, " : " failed, ");
161 }
162
163 stream << " request: " << ncr_->toText();
164 return (stream.str());
165}
166
167
168void
169NameChangeTransaction::sendUpdate(const std::string& comment) {
170 try {
171 ++update_attempts_;
172 // @todo add logic to add/replace TSIG key info in request if
173 // use_tsig_ is true. We should be able to navigate to the TSIG key
174 // for the current server. If not we would need to add that.
175
176 D2ParamsPtr d2_params = cfg_mgr_->getD2Params();
177 dns_client_->doUpdate(*io_service_, current_server_->getIpAddress(),
178 current_server_->getPort(), *dns_update_request_,
179 d2_params->getDnsServerTimeout(), tsig_key_);
180 // Message is on its way, so the next event should be NOP_EVT.
183 DHCP_DDNS_UPDATE_REQUEST_SENT)
184 .arg(getRequestId())
185 .arg(comment)
186 .arg(current_server_->toText());
187 } catch (const std::exception& ex) {
188 // We were unable to initiate the send.
189 // It is presumed that any throw from doUpdate is due to a programmatic
190 // error, such as an unforeseen permutation of data, rather than an IO
191 // failure. IO errors should be caught by the underlying asiolink
192 // mechanisms and manifested as an unsuccessful IO status in the
193 // DNSClient callback. Any problem here most likely means the request
194 // is corrupt in some way and cannot be completed, therefore we will
195 // log it and transition it to failure.
196 LOG_ERROR(d2_to_dns_logger, DHCP_DDNS_TRANS_SEND_ERROR)
197 .arg(getRequestId())
198 .arg(ex.what());
200 }
201}
202
203void
205 // Call superclass impl first.
207
208 // Define NCT events.
209 defineEvent(SELECT_SERVER_EVT, "SELECT_SERVER_EVT");
210 defineEvent(SERVER_SELECTED_EVT, "SERVER_SELECTED_EVT");
211 defineEvent(SERVER_IO_ERROR_EVT, "SERVER_IO_ERROR_EVT");
212 defineEvent(NO_MORE_SERVERS_EVT, "NO_MORE_SERVERS_EVT");
213 defineEvent(IO_COMPLETED_EVT, "IO_COMPLETED_EVT");
214 defineEvent(UPDATE_OK_EVT, "UPDATE_OK_EVT");
215 defineEvent(UPDATE_FAILED_EVT, "UPDATE_FAILED_EVT");
216}
217
218void
220 // Call superclass impl first.
222
223 // Verify NCT events.
231}
232
233void
235 // Call superclass impl first.
237 // This class is "abstract" in that it does not supply handlers for its
238 // states, derivations must do that therefore they must define them.
239}
240
241void
243 // Call superclass impl first.
245
246 // Verify NCT states. This ensures that derivations provide the handlers.
252}
253
254void
255NameChangeTransaction::onModelFailure(const std::string& explanation) {
257 LOG_ERROR(d2_to_dns_logger, DHCP_DDNS_STATE_MODEL_UNEXPECTED_ERROR)
258 .arg(getRequestId())
259 .arg(explanation);
260}
261
262void
264 if (update_attempts_ < MAX_UPDATE_TRIES_PER_SERVER) {
265 // Re-enter the current state with same server selected.
267 } else {
268 // Transition to given fail_to_state state if we are out
269 // of retries.
270 transition(fail_to_state, SERVER_IO_ERROR_EVT);
271 }
272}
273
274void
276 dns_update_request_ = request;
277}
278
279void
281 update_attempts_ = 0;
282 dns_update_request_.reset();
283}
284
285void
287 dns_update_status_ = status;
288}
289
290void
292 dns_update_response_ = response;
293}
294
295void
297 dns_update_response_.reset();
298}
299
300void
302 forward_change_completed_ = value;
303}
304
305void
307 reverse_change_completed_ = value;
308}
309
310void
312 update_attempts_ = value;
313}
314
317 if (!domain) {
319 "prepNewRequest - domain cannot be null");
320 }
321
322 try {
323 // Create a "blank" update request.
325 OUTBOUND));
326 // Set the query id
327 request->setId(QidGenerator::getInstance().generateQid());
328 // Construct the Zone Section.
329 dns::Name zone_name(domain->getName());
330 request->setZone(zone_name, dns::RRClass::IN());
331 return (request);
332 } catch (const std::exception& ex) {
333 isc_throw(NameChangeTransactionError, "Cannot create new request :"
334 << ex.what());
335 }
336}
337
338void
340 if (!rrset) {
342 "addLeaseAddressRdata - RRset cannot cannot be null");
343 }
344
345 try {
346 // Manufacture an RData from the lease address then add it to the RR.
348 if (ncr_->isV4()) {
349 rdata.reset(new dns::rdata::in::A(ncr_->getIpAddress()));
350 } else {
351 rdata.reset(new dns::rdata::in::AAAA(ncr_->getIpAddress()));
352 }
353 rrset->addRdata(rdata);
354 } catch (const std::exception& ex) {
355 isc_throw(NameChangeTransactionError, "Cannot add address rdata: "
356 << ex.what());
357 }
358}
359
360void
362 if (!rrset) {
364 "addDhcidRdata - RRset cannot cannot be null");
365 }
366
367 try {
368 const std::vector<uint8_t>& ncr_dhcid = ncr_->getDhcid().getBytes();
369 util::InputBuffer buffer(ncr_dhcid.data(), ncr_dhcid.size());
370 dns::rdata::ConstRdataPtr rdata (new dns::rdata::in::
371 DHCID(buffer, ncr_dhcid.size()));
372 rrset->addRdata(rdata);
373 } catch (const std::exception& ex) {
374 isc_throw(NameChangeTransactionError, "Cannot add DCHID rdata: "
375 << ex.what());
376 }
377
378}
379
380void
382 if (!rrset) {
384 "addPtrRdata - RRset cannot cannot be null");
385 }
386
387 try {
388 dns::rdata::ConstRdataPtr rdata(new dns::rdata::generic::
389 PTR(getNcr()->getFqdn()));
390 rrset->addRdata(rdata);
391 } catch (const std::exception& ex) {
392 isc_throw(NameChangeTransactionError, "Cannot add PTR rdata: "
393 << ex.what());
394 }
395}
396
399 return (ncr_);
400}
401
402const TransactionKey&
404 return (ncr_->getDhcid());
405}
406
407std::string
409 return (ncr_->getRequestId());
410}
411
414 return (ncr_->getStatus());
415}
416
419 return (forward_domain_);
420}
421
424 return (reverse_domain_);
425}
426
427void
429 if (!domain) {
431 "initServerSelection called with an empty domain");
432 }
433
434 // Set the tsig_key to that of the DdnsDomain.
435 TSIGKeyInfoPtr tsig_key_info = domain->getTSIGKeyInfo();
436 if (tsig_key_info) {
437 tsig_key_ = tsig_key_info->getTSIGKey();
438 } else {
439 tsig_key_.reset();
440 }
441
442 current_server_list_ = domain->getServers();
443 next_server_pos_ = 0;
444 current_server_.reset();
445}
446
447bool
449 if ((current_server_list_) &&
450 (next_server_pos_ < current_server_list_->size())) {
451 current_server_ = (*current_server_list_)[next_server_pos_];
452 // Toss out any previous response.
453 dns_update_response_.reset();
454
455 // @todo Protocol is set on DNSClient constructor. We need
456 // to propagate a configuration value downward, probably starting
457 // at global, then domain, then server
458 // Once that is supported we need to add it here.
459 dns_client_.reset(new DNSClient(dns_update_response_ , this,
461 ++next_server_pos_;
462 return (true);
463 }
464
465 return (false);
466}
467
468const DNSClientPtr&
470 return (dns_client_);
471}
472
473const DnsServerInfoPtr&
475 return (current_server_);
476}
477
478void
480 return (ncr_->setStatus(status));
481}
482
485 return (dns_update_request_);
486}
487
490 return (dns_update_status_);
491}
492
495 return (dns_update_response_);
496}
497
498bool
500 return (forward_change_completed_);
501}
502
503bool
505 return (reverse_change_completed_);
506}
507
508size_t
510 return (update_attempts_);
511}
512
513const dns::RRType&
515 return (ncr_->isV4() ? dns::RRType::A() : dns::RRType::AAAA());
516}
517
518} // namespace isc::d2
519} // namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The D2UpdateMessage encapsulates a DNS Update message.
The DNSClient class handles communication with the DNS server.
Definition: dns_client.h:49
Status
A status code of the DNSClient.
Definition: dns_client.h:60
@ IO_STOPPED
IO was stopped.
Definition: dns_client.h:63
@ TIMEOUT
No response, timeout.
Definition: dns_client.h:62
@ OTHER
Other, unclassified error.
Definition: dns_client.h:65
@ INVALID_RESPONSE
Response received but invalid.
Definition: dns_client.h:64
@ SUCCESS
Response received and is ok.
Definition: dns_client.h:61
Thrown if the transaction encounters a general error.
Definition: nc_trans.h:27
static const int SELECTING_FWD_SERVER_ST
State in which forward DNS server selection is done.
Definition: nc_trans.h:91
void retryTransition(const int fail_to_state)
Determines the state and next event based on update attempts.
Definition: nc_trans.cc:263
virtual void onModelFailure(const std::string &explanation)
Handler for fatal model execution errors.
Definition: nc_trans.cc:255
virtual void operator()(DNSClient::Status status)
Serves as the DNSClient IO completion event handler.
Definition: nc_trans.cc:97
static const int PROCESS_TRANS_FAILED_ST
State which processes an unsuccessful transaction conclusion.
Definition: nc_trans.h:105
static const unsigned int MAX_UPDATE_TRIES_PER_SERVER
Maximum times to attempt a single update on a given server.
Definition: nc_trans.h:154
static const int READY_ST
State from which a transaction is started.
Definition: nc_trans.h:83
const D2UpdateMessagePtr & getDnsUpdateResponse() const
Fetches the most recent DNS update response packet.
Definition: nc_trans.cc:494
static const int PROCESS_TRANS_OK_ST
State which processes successful transaction conclusion.
Definition: nc_trans.h:102
static const int UPDATE_OK_EVT
Issued when the attempted update successfully completed.
Definition: nc_trans.h:135
const DNSClientPtr & getDNSClient() const
Fetches the DNSClient instance.
Definition: nc_trans.cc:469
virtual void verifyStates()
Validates the contents of the set of states.
Definition: nc_trans.cc:242
void startTransaction()
Begins execution of the transaction.
Definition: nc_trans.cc:87
virtual D2UpdateMessagePtr prepNewRequest(DdnsDomainPtr domain)
Creates a new DNS update request based on the given domain.
Definition: nc_trans.cc:316
NameChangeTransaction(asiolink::IOServicePtr &io_service, dhcp_ddns::NameChangeRequestPtr &ncr, DdnsDomainPtr &forward_domain, DdnsDomainPtr &reverse_domain, D2CfgMgrPtr &cfg_mgr)
Constructor.
Definition: nc_trans.cc:45
static const int UPDATE_FAILED_EVT
Issued when the attempted update fails to complete.
Definition: nc_trans.h:141
const D2UpdateMessagePtr & getDnsUpdateRequest() const
Fetches the current DNS update request packet.
Definition: nc_trans.cc:484
const dns::RRType & getAddressRRType() const
Returns the DHCP data type for the lease address.
Definition: nc_trans.cc:514
const dhcp_ddns::NameChangeRequestPtr & getNcr() const
Fetches the NameChangeRequest for this transaction.
Definition: nc_trans.cc:398
void initServerSelection(const DdnsDomainPtr &domain)
Initializes server selection from the given DDNS domain.
Definition: nc_trans.cc:428
static const int IO_COMPLETED_EVT
Issued when a DNS update packet exchange has completed.
Definition: nc_trans.h:130
static const int NCT_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: nc_trans.h:108
static const int SELECT_SERVER_EVT
Issued when a server needs to be selected.
Definition: nc_trans.h:113
static const int SERVER_IO_ERROR_EVT
Issued when an update fails due to an IO error.
Definition: nc_trans.h:119
std::string getRequestId() const
Fetches the request id that identifies this transaction.
Definition: nc_trans.cc:408
virtual void defineStates()
Adds states defined by NameChangeTransaction to the state set.
Definition: nc_trans.cc:234
const TransactionKey & getTransactionKey() const
Fetches the unique key that identifies this transaction.
Definition: nc_trans.cc:403
void setUpdateAttempts(const size_t value)
Sets the update attempt count to the given value.
Definition: nc_trans.cc:311
void addLeaseAddressRdata(dns::RRsetPtr &rrset)
Adds an RData for the lease address to the given RRset.
Definition: nc_trans.cc:339
bool getForwardChangeCompleted() const
Returns whether the forward change has completed or not.
Definition: nc_trans.cc:499
virtual void sendUpdate(const std::string &comment="")
Send the update request to the current server.
Definition: nc_trans.cc:169
void setForwardChangeCompleted(const bool value)
Sets the forward change completion flag to the given value.
Definition: nc_trans.cc:301
void addPtrRdata(dns::RRsetPtr &rrset)
Adds an RData for the lease FQDN to the given RRset.
Definition: nc_trans.cc:381
void setDnsUpdateResponse(D2UpdateMessagePtr &response)
Sets the update response packet to the given packet.
Definition: nc_trans.cc:291
bool selectNextServer()
Selects the next server in the current server list.
Definition: nc_trans.cc:448
void setNcrStatus(const dhcp_ddns::NameChangeStatus &status)
Sets the status of the transaction's NameChangeRequest.
Definition: nc_trans.cc:479
DdnsDomainPtr & getForwardDomain()
Fetches the forward DdnsDomain.
Definition: nc_trans.cc:418
virtual void verifyEvents()
Validates the contents of the set of events.
Definition: nc_trans.cc:219
void addDhcidRdata(dns::RRsetPtr &rrset)
Adds an RData for the lease client's DHCID to the given RRset.
Definition: nc_trans.cc:361
void clearDnsUpdateRequest()
Destroys the current update request packet and resets update attempts count.
Definition: nc_trans.cc:280
static const int SELECTING_REV_SERVER_ST
State in which reverse DNS server selection is done.
Definition: nc_trans.h:99
DNSClient::Status getDnsUpdateStatus() const
Fetches the most recent DNS update status.
Definition: nc_trans.cc:489
void setDnsUpdateStatus(const DNSClient::Status &status)
Sets the update status to the given status value.
Definition: nc_trans.cc:286
void setDnsUpdateRequest(D2UpdateMessagePtr &request)
Sets the update request packet to the given packet.
Definition: nc_trans.cc:275
bool getReverseChangeCompleted() const
Returns whether the reverse change has completed or not.
Definition: nc_trans.cc:504
static const int NO_MORE_SERVERS_EVT
Issued when there are no more servers from which to select.
Definition: nc_trans.h:125
dhcp_ddns::NameChangeStatus getNcrStatus() const
Fetches the NameChangeRequest status of the transaction.
Definition: nc_trans.cc:413
static const int NCT_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: nc_trans.h:144
virtual void defineEvents()
Adds events defined by NameChangeTransaction to the event set.
Definition: nc_trans.cc:204
void clearDnsUpdateResponse()
Destroys the current update response packet.
Definition: nc_trans.cc:296
std::string responseString() const
Returns a string version of the current response status and rcode.
Definition: nc_trans.cc:113
void setReverseChangeCompleted(const bool value)
Sets the reverse change completion flag to the given value.
Definition: nc_trans.cc:306
size_t getUpdateAttempts() const
Fetches the update attempt count for the current update.
Definition: nc_trans.cc:509
const DnsServerInfoPtr & getCurrentServer() const
Fetches the currently selected server.
Definition: nc_trans.cc:474
static const int SERVER_SELECTED_EVT
Issued when a server has been selected.
Definition: nc_trans.h:116
DdnsDomainPtr & getReverseDomain()
Fetches the reverse DdnsDomain.
Definition: nc_trans.cc:423
std::string transactionOutcomeString() const
Returns a string version of transaction outcome.
Definition: nc_trans.cc:147
virtual ~NameChangeTransaction()
Destructor.
Definition: nc_trans.cc:83
Container class for handling the DHCID value within a NameChangeRequest.
Definition: ncr_msg.h:86
The Name class encapsulates DNS names.
Definition: name.h:223
static const RRClass & IN()
Definition: rrclass.h:325
The RRType class encapsulates DNS resource record types.
Definition: rrtype.h:106
static const RRType & AAAA()
Definition: rrtype.h:467
static const RRType & A()
Definition: rrtype.h:677
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition: buffer.h:81
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
Definition: state_model.cc:184
virtual void runModel(unsigned int event)
Processes events through the state model.
Definition: state_model.cc:113
virtual void defineEvents()
Populates the set of events.
Definition: state_model.cc:221
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
Definition: state_model.cc:304
virtual void verifyStates()
Validates the contents of the set of states.
Definition: state_model.cc:245
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.
Definition: state_model.cc:211
unsigned int getNextEvent() const
Fetches the model's next event.
Definition: state_model.cc:346
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
Definition: state_model.cc:168
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
Definition: state_model.cc:256
virtual void verifyEvents()
Validates the contents of the set of events.
Definition: state_model.cc:229
static const int NOP_EVT
Signifies that no event has occurred.
Definition: state_model.h:289
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
Definition: state_model.cc:385
void startModel(const int start_state)
Begins execution of the model.
Definition: state_model.cc:101
virtual void defineStates()
Populates the set of states.
Definition: state_model.cc:237
unsigned int getCurrState() const
Fetches the model's current state.
Definition: state_model.cc:331
static QidGenerator & getInstance()
Returns the singleton instance of the QidGenerator.
Definition: qid_gen.cc:26
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#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
boost::shared_ptr< D2UpdateMessage > D2UpdateMessagePtr
Pointer to the DNS Update Message.
boost::shared_ptr< DdnsDomain > DdnsDomainPtr
Defines a pointer for DdnsDomain instances.
Definition: d2_config.h:586
boost::shared_ptr< D2CfgMgr > D2CfgMgrPtr
Defines a shared pointer to D2CfgMgr.
Definition: d2_cfg_mgr.h:297
boost::shared_ptr< DnsServerInfo > DnsServerInfoPtr
Defines a pointer for DnsServerInfo instances.
Definition: d2_config.h:509
isc::log::Logger d2_to_dns_logger("d2-to-dns")
Definition: d2_log.h:20
boost::shared_ptr< TSIGKeyInfo > TSIGKeyInfoPtr
Defines a pointer for TSIGKeyInfo instances.
Definition: d2_config.h:398
boost::shared_ptr< DNSClient > DNSClientPtr
Definition: dns_client.h:22
boost::shared_ptr< D2Params > D2ParamsPtr
Defines a pointer for D2Params instances.
Definition: d2_config.h:251
NameChangeStatus
Defines the runtime processing status values for requests.
Definition: ncr_msg.h:52
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:214
boost::shared_ptr< const Rdata > ConstRdataPtr
Definition: rdata.h:72
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:53
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
Definition: log_dbglevels.h:71
Definition: edns.h:19
Defines the logger used by the top-level component of kea-dhcp-ddns.
This file defines the class NameChangeTransaction.