Kea 2.7.6
d2_client_cfg.cc
Go to the documentation of this file.
1// Copyright (C) 2013-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#include <config.h>
8
9#include <dhcp_ddns/ncr_udp.h>
11#include <dhcpsrv/cfgmgr.h>
12#include <dhcpsrv/dhcpsrv_log.h>
13
14#include <string>
15
16using namespace std;
17using namespace isc::asiolink;
18using namespace isc::data;
19using namespace isc::util;
20
21namespace isc {
22namespace dhcp {
23
25const char* D2ClientConfig::DFT_SERVER_IP = "127.0.0.1";
26const size_t D2ClientConfig::DFT_SERVER_PORT = 53001;
27const char* D2ClientConfig::DFT_V4_SENDER_IP = "0.0.0.0";
28const char* D2ClientConfig::DFT_V6_SENDER_IP = "::";
29const size_t D2ClientConfig::DFT_SENDER_PORT = 0;
30const size_t D2ClientConfig::DFT_MAX_QUEUE_SIZE = 1024;
31const char* D2ClientConfig::DFT_NCR_PROTOCOL = "UDP";
32const char* D2ClientConfig::DFT_NCR_FORMAT = "JSON";
36const char* D2ClientConfig::DFT_GENERATED_PREFIX = "myhost";
39
42 if (mode_str == "never") {
44 }
45
46 if (mode_str == "always") {
48 }
49
50 if (mode_str == "when-present") {
52 }
53
54 if (mode_str == "when-not-present") {
56 }
57
59 "Invalid ReplaceClientNameMode: " << mode_str);
60}
61
62std::string
64 switch (mode) {
66 return ("never");
68 return ("always");
70 return ("when-present");
72 return ("when-not-present");
73 default:
74 break;
75 }
76
77 std::ostringstream stream;
78 stream << "unknown(" << mode << ")";
79 return (stream.str());
80}
81
82D2ClientConfig::D2ClientConfig(const bool enable_updates,
83 const isc::asiolink::IOAddress& server_ip,
84 const size_t server_port,
85 const isc::asiolink::IOAddress& sender_ip,
86 const size_t sender_port,
87 const size_t max_queue_size,
88 const dhcp_ddns::
89 NameChangeProtocol& ncr_protocol,
90 const dhcp_ddns::
91 NameChangeFormat& ncr_format)
92 : enable_updates_(enable_updates),
93 server_ip_(server_ip),
94 server_port_(server_port),
95 sender_ip_(sender_ip),
96 sender_port_(sender_port),
97 max_queue_size_(max_queue_size),
98 ncr_protocol_(ncr_protocol),
99 ncr_format_(ncr_format) {
101}
102
104 : enable_updates_(false),
105 server_ip_(isc::asiolink::IOAddress(DFT_SERVER_IP)),
106 server_port_(DFT_SERVER_PORT),
107 sender_ip_(isc::asiolink::IOAddress(DFT_V4_SENDER_IP)),
108 sender_port_(DFT_SENDER_PORT),
109 max_queue_size_(DFT_MAX_QUEUE_SIZE),
110 ncr_protocol_(dhcp_ddns::stringToNcrProtocol(DFT_NCR_PROTOCOL)),
111 ncr_format_(dhcp_ddns::stringToNcrFormat(DFT_NCR_FORMAT)) {
113}
114
116
117void
119 enable_updates_ = enable;
120}
121
122void
124 if (ncr_format_ != dhcp_ddns::FMT_JSON) {
125 isc_throw(D2ClientError, "D2ClientConfig: NCR Format: "
126 << dhcp_ddns::ncrFormatToString(ncr_format_)
127 << " is not yet supported");
128 }
129
130 if (ncr_protocol_ != dhcp_ddns::NCR_UDP) {
131 isc_throw(D2ClientError, "D2ClientConfig: NCR Protocol: "
132 << dhcp_ddns::ncrProtocolToString(ncr_protocol_)
133 << " is not yet supported");
134 }
135
136 if (sender_ip_.getFamily() != server_ip_.getFamily()) {
137 isc_throw(D2ClientError, "D2ClientConfig: address family mismatch: "
138 << "server-ip: " << server_ip_.toText()
139 << " is: " << (server_ip_.isV4() ? "IPv4" : "IPv6")
140 << " while sender-ip: " << sender_ip_.toText()
141 << " is: " << (sender_ip_.isV4() ? "IPv4" : "IPv6"));
142 }
143
144 if (server_ip_ == sender_ip_ && server_port_ == sender_port_) {
145 isc_throw(D2ClientError, "D2ClientConfig: server and sender cannot"
146 " share the exact same IP address/port: "
147 << server_ip_.toText() << "/" << server_port_);
148 }
149
152}
153
154bool
156 return ((enable_updates_ == other.enable_updates_) &&
157 (server_ip_ == other.server_ip_) &&
158 (server_port_ == other.server_port_) &&
159 (sender_ip_ == other.sender_ip_) &&
160 (sender_port_ == other.sender_port_) &&
161 (max_queue_size_ == other.max_queue_size_) &&
162 (ncr_protocol_ == other.ncr_protocol_) &&
163 (ncr_format_ == other.ncr_format_));
164}
165
166bool
168 return (!(*this == other));
169}
170
171std::string
173 std::ostringstream stream;
174
175 stream << "enable_updates: " << (enable_updates_ ? "yes" : "no");
176 if (enable_updates_) {
177 stream << ", server-ip: " << server_ip_.toText()
178 << ", server-port: " << server_port_
179 << ", sender-ip: " << sender_ip_.toText()
180 << ", sender-port: " << sender_port_
181 << ", max-queue-size: " << max_queue_size_
182 << ", ncr-protocol: " << ncrProtocolToString(ncr_protocol_)
183 << ", ncr-format: " << ncrFormatToString(ncr_format_);
184 }
185
186
187 return (stream.str());
188}
189
193 // Set user context
194 contextToElement(result);
195 // Set enable-updates
196 result->set("enable-updates", Element::create(enable_updates_));
197 // Set server-ip
198 result->set("server-ip", Element::create(server_ip_.toText()));
199 // Set server-port
200 result->set("server-port", Element::create(static_cast<long long>(server_port_)));
201 // Set sender-ip
202 result->set("sender-ip", Element::create(sender_ip_.toText()));
203 // Set sender-port
204 result->set("sender-port", Element::create(static_cast<long long>(sender_port_)));
205 // Set max-queue-size
206 result->set("max-queue-size", Element::create(static_cast<long long>(max_queue_size_)));
207 // Set ncr-protocol
208 result->set("ncr-protocol", Element::create(dhcp_ddns::ncrProtocolToString(ncr_protocol_)));
209 // Set ncr-format
210 result->set("ncr-format", Element::create(dhcp_ddns::ncrFormatToString(ncr_format_)));
211 return (result);
212}
213
214std::ostream&
215operator<<(std::ostream& os, const D2ClientConfig& config) {
216 os << config.toText();
217 return (os);
218}
219
220} // namespace dhcp
221} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Acts as a storage vault for D2 client configuration.
std::string toText() const
Generates a string representation of the class contents.
static const bool DFT_OVERRIDE_CLIENT_UPDATE
static const char * DFT_NCR_FORMAT
static ReplaceClientNameMode stringToReplaceClientNameMode(const std::string &mode_str)
Converts labels to ReplaceClientNameMode enum values.
ReplaceClientNameMode
Defines the client name replacement modes.
static std::string replaceClientNameModeToString(const ReplaceClientNameMode &mode)
Converts NameChangeFormat enums to text labels.
static const char * DFT_V4_SENDER_IP
static const size_t DFT_SENDER_PORT
virtual void validateContents()
Validates member values.
bool operator==(const D2ClientConfig &other) const
Compares two D2ClientConfigs for equality.
static const char * DFT_HOSTNAME_CHAR_REPLACEMENT
static const char * DFT_NCR_PROTOCOL
void enableUpdates(bool enable)
Sets enable-updates flag to the given value.
static const bool DFT_OVERRIDE_NO_UPDATE
virtual ~D2ClientConfig()
Destructor.
static const char * DFT_SERVER_IP
Default configuration constants.
static const char * DFT_V6_SENDER_IP
bool operator!=(const D2ClientConfig &other) const
Compares two D2ClientConfigs for inequality.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
static const size_t DFT_SERVER_PORT
static const char * DFT_GENERATED_PREFIX
static const char * DFT_REPLACE_CLIENT_NAME_MODE
static const char * DFT_HOSTNAME_CHAR_SET
static const size_t DFT_MAX_QUEUE_SIZE
D2ClientConfig()
Default constructor The default constructor creates an instance that has updates disabled.
An exception that is thrown if an error occurs while configuring the D2 DHCP DDNS client.
Defines the D2ClientConfig class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::string ncrProtocolToString(NameChangeProtocol protocol)
Function which converts NameChangeProtocol enums to text labels.
Definition ncr_io.cc:36
std::string ncrFormatToString(NameChangeFormat format)
Function which converts NameChangeFormat enums to text labels.
Definition ncr_msg.cc:35
Defines the logger used by the top-level component of kea-lfc.
This file provides UDP socket based implementation for sending and receiving NameChangeRequests.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.