Kea 2.5.8
d2_client_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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#ifndef D2_CLIENT_MGR_H
8#define D2_CLIENT_MGR_H
9
14#include <asiolink/io_address.h>
15#include <dhcp_ddns/ncr_io.h>
17#include <dhcpsrv/srv_config.h>
19#include <util/str.h>
20
21#include <boost/algorithm/string.hpp>
22#include <boost/shared_ptr.hpp>
23#include <boost/noncopyable.hpp>
24
25#include <stdint.h>
26#include <string>
27#include <vector>
28#include <sstream>
29
30namespace isc {
31namespace dhcp {
32
43typedef
44std::function<void(const dhcp_ddns::NameChangeSender::Result result,
46
81 boost::noncopyable {
82public:
88
91
96 void setD2ClientConfig(D2ClientConfigPtr& new_config);
97
101 bool ddnsEnabled();
102
107
156 void analyzeFqdn(const bool client_s, const bool client_n, bool& server_s,
157 bool& server_n, const DdnsParams& ddns_params) const;
158
177 std::string generateFqdn(const asiolink::IOAddress& address,
178 const DdnsParams& ddns_params,
179 const bool trailing_dot = true) const;
180
199 std::string qualifyName(const std::string& partial_name,
200 const DdnsParams& ddns_params,
201 const bool trailing_dot) const;
202
215 template <class T>
216 void adjustFqdnFlags(const T& fqdn, T& fqdn_resp,
217 const DdnsParams& ddns_params);
218
236 template <class T>
237 void getUpdateDirections(const T& fqdn_resp, bool& forward, bool& reverse);
238
266 template <class T>
267 void adjustDomainName(const T& fqdn, T& fqdn_resp,
268 const DdnsParams& ddns_params);
269
290 void startSender(D2ClientErrorHandler error_handler,
291 const isc::asiolink::IOServicePtr& io_service);
292
308 void startSender(D2ClientErrorHandler error_handler);
309
314 bool amSending() const;
315
324 void stopSender();
325
338
350 Result result,
352
354 size_t getQueueSize() const;
355
357 size_t getQueueMaxSize() const;
358
370 const dhcp_ddns::NameChangeRequestPtr& peekAt(const size_t index) const;
371
376 void clearQueue();
377
384 void runReadyIO();
385
395 void suspendUpdates();
396
398 void stop();
399
400protected:
411 virtual void operator ()(const dhcp_ddns::NameChangeSender::Result result,
413
424 int getSelectFd();
425
433
434private:
435
437 D2ClientConfigPtr d2_client_config_;
438
440 dhcp_ddns::NameChangeSenderPtr name_change_sender_;
441
444 boost::shared_ptr<asiolink::IOService> private_io_service_;
445
448 D2ClientErrorHandler client_error_handler_;
449
451 int registered_select_fd_;
452};
453
454template <class T>
455void
456D2ClientMgr::adjustFqdnFlags(const T& fqdn, T& fqdn_resp, const DdnsParams& ddns_params) {
457 bool server_s = false;
458 bool server_n = false;
459 analyzeFqdn(fqdn.getFlag(T::FLAG_S), fqdn.getFlag(T::FLAG_N),
460 server_s, server_n, ddns_params);
461
462 // Reset the flags to zero to avoid triggering N and S both 1 check.
463 fqdn_resp.resetFlags();
464
465 // Set S and N flags.
466 fqdn_resp.setFlag(T::FLAG_S, server_s);
467 fqdn_resp.setFlag(T::FLAG_N, server_n);
468
469 // Set O flag true if server S overrides client S.
470 fqdn_resp.setFlag(T::FLAG_O, (fqdn.getFlag(T::FLAG_S) != server_s));
471}
472
473template <class T>
474void
476 bool& forward, bool& reverse) {
477 forward = fqdn_resp.getFlag(T::FLAG_S);
478 reverse = !(fqdn_resp.getFlag(T::FLAG_N));
479}
480
481template <class T>
482void
483D2ClientMgr::adjustDomainName(const T& fqdn, T& fqdn_resp, const DdnsParams& ddns_params) {
484 // If we're configured to replace it or the supplied name is blank
485 // set the response name to blank.
488 fqdn.getDomainName().empty()) {
489 fqdn_resp.setDomainName("", T::PARTIAL);
490 } else {
491 // Sanitize the name the client sent us, if we're configured to do so.
492 std::string client_name = fqdn.getDomainName();
493
495 if (sanitizer) {
496 // We need the raw text form, so we can replace escaped chars
497 dns::Name tmp(client_name);
498 std::string raw_name = tmp.toRawText();
499
500 // We do not know if the sanitizer's regexp preserves dots, so
501 // we'll scrub it label by label. Yeah, lucky us.
502 // Using boost::split is simpler than using dns::Name::split() as
503 // that returns Names which have trailing dots etc.
504 std::vector<std::string> labels;
505 boost::algorithm::split(labels, raw_name, boost::is_any_of("."));
506 std::stringstream ss;
507 bool first = true;
508 for (auto const& label : labels) {
509 if (!first) {
510 ss << ".";
511 } else {
512 first = false;
513 }
514
515 ss << sanitizer->scrub(label);
516 }
517
518 client_name = ss.str();
519 }
520
521 // If the supplied name is partial, qualify it by adding the suffix.
522 if (fqdn.getDomainNameType() == T::PARTIAL) {
523 fqdn_resp.setDomainName(qualifyName(client_name, ddns_params, true), T::FULL);
524 } else {
525 fqdn_resp.setDomainName(client_name, T::FULL);
526 }
527 }
528}
529
530
532typedef boost::shared_ptr<D2ClientMgr> D2ClientMgrPtr;
533
534
535} // namespace isc
536} // namespace dhcp
537
538#endif
ReplaceClientNameMode
Defines the client name replacement modes.
Definition: d2_client_cfg.h:76
D2ClientMgr isolates Kea from the details of being a D2 client.
Definition: d2_client_mgr.h:81
void invokeClientErrorHandler(const dhcp_ddns::NameChangeSender::Result result, dhcp_ddns::NameChangeRequestPtr &ncr)
Calls the client's error handler.
std::string generateFqdn(const asiolink::IOAddress &address, const DdnsParams &ddns_params, const bool trailing_dot=true) const
Builds a FQDN based on the configuration and given IP address.
void analyzeFqdn(const bool client_s, const bool client_n, bool &server_s, bool &server_n, const DdnsParams &ddns_params) const
Determines server flags based on configuration and client flags.
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS is enabled.
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
void getUpdateDirections(const T &fqdn_resp, bool &forward, bool &reverse)
Get directional update flags based on server FQDN flags.
void stop()
Stop the sender.
void suspendUpdates()
Suspends sending requests.
int getRegisteredSelectFd()
Fetches the select-fd that is currently registered.
void adjustDomainName(const T &fqdn, T &fqdn_resp, const DdnsParams &ddns_params)
Set server FQDN name based on configuration and a given FQDN.
void sendRequest(dhcp_ddns::NameChangeRequestPtr &ncr)
Send the given NameChangeRequests to kea-dhcp-ddns.
size_t getQueueSize() const
Returns the number of NCRs queued for transmission.
void clearQueue()
Removes all NCRs queued for transmission.
void stopSender()
Disables sending NameChangeRequests to kea-dhcp-ddns.
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
bool amSending() const
Returns true if the sender is in send mode, false otherwise.
int getSelectFd()
Fetches the sender's select-fd.
void runReadyIO()
Processes sender IO events.
void adjustFqdnFlags(const T &fqdn, T &fqdn_resp, const DdnsParams &ddns_params)
Set server FQDN flags based on configuration and a given FQDN.
~D2ClientMgr()
Destructor.
D2ClientMgr()
Constructor.
size_t getQueueMaxSize() const
Returns the maximum number of NCRs allowed in the queue.
virtual void operator()(const dhcp_ddns::NameChangeSender::Result result, dhcp_ddns::NameChangeRequestPtr &ncr)
Function operator implementing the NCR sender callback.
const dhcp_ddns::NameChangeRequestPtr & peekAt(const size_t index) const
Returns the nth NCR queued for transmission.
std::string qualifyName(const std::string &partial_name, const DdnsParams &ddns_params, const bool trailing_dot) const
Adds a qualifying suffix to a given domain name.
void startSender(D2ClientErrorHandler error_handler, const isc::asiolink::IOServicePtr &io_service)
Enables sending NameChangeRequests to kea-dhcp-ddns.
Convenience container for conveying DDNS behavioral parameters It is intended to be created per Packe...
Definition: srv_config.h:48
D2ClientConfig::ReplaceClientNameMode getReplaceClientNameMode() const
Returns how Kea should handle the domain-name supplied by the client.
Definition: srv_config.cc:1057
isc::util::str::StringSanitizerPtr getHostnameSanitizer() const
Returns a regular expression string sanitizer.
Definition: srv_config.cc:1102
Abstract class for defining application layer send callbacks.
Definition: ncr_io.h:490
Abstract interface for sending NameChangeRequests.
Definition: ncr_io.h:468
Result
Defines the outcome of an asynchronous NCR send.
Definition: ncr_io.h:478
The Name class encapsulates DNS names.
Definition: name.h:219
std::string toRawText(bool omit_final_dot=false) const
Convert the LabelSequence to a string without escape sequences.
Definition: name.cc:509
Defines the D2ClientConfig class.
boost::shared_ptr< NameChangeSender > NameChangeSenderPtr
Defines a smart pointer to an instance of a sender.
Definition: ncr_io.h:857
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:241
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
boost::shared_ptr< D2ClientMgr > D2ClientMgrPtr
Defines a pointer for D2ClientMgr instances.
std::function< void(const dhcp_ddns::NameChangeSender::Result result, dhcp_ddns::NameChangeRequestPtr &ncr)> D2ClientErrorHandler
Defines the type for D2 IO error handler.
Definition: d2_client_mgr.h:45
std::unique_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition: str.h:263
Defines the logger used by the top-level component of kea-lfc.
This file defines abstract classes for exchanging NameChangeRequests.