SNode.C
Loading...
Searching...
No Matches
ConfigAddress.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "net/in/config/ConfigAddress.h"
43
44#include "net/config/ConfigAddressBase.hpp" // IWYU pragma: keep
45#include "net/config/ConfigAddressLocal.hpp" // IWYU pragma: keep
46#include "net/config/ConfigAddressRemote.hpp" // IWYU pragma: keep
47#include "net/config/ConfigAddressReverse.hpp" // IWYU pragma: keep
48#include "net/config/ConfigSection.hpp"
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52#include "core/system/netdb.h"
53#include "utils/PreserveErrno.h"
54
55#include <limits>
56
57#endif /* DOXYGEN_SHOULD_SKIP_THIS */
58
59#define XSTR(s) STR(s)
60#define STR(s) #s
61
62namespace net::in::config {
63
64 template <template <typename SocketAddress> typename ConfigAddressType>
66 [[maybe_unused]] const std::string& addressOptionName,
67 [[maybe_unused]] const std::string& addressOptionDescription)
68 : ConfigSection(instance, this)
69 , ConfigAddressType<net::in::SocketAddress>(this) {
70 numericReverseOpt = addFlag( //
71 "--numeric-reverse{true}",
72 "Suppress reverse host name lookup",
73 "bool",
74 XSTR(IN_NUMERIC_REVERSE),
75 CLI::IsMember({"true", "false"}));
76 }
77
78 template <template <typename SocketAddress> typename ConfigAddressType>
79 ConfigAddressReverse<ConfigAddressType>& ConfigAddressReverse<ConfigAddressType>::setNumericReverse(bool numeric) {
80 setDefaultValue(numericReverseOpt, numeric ? "true" : "false");
81
82 return *this;
83 }
84
85 template <template <typename SocketAddress> typename ConfigAddressType>
86 bool ConfigAddressReverse<ConfigAddressType>::getNumericReverse() const {
87 return numericReverseOpt->as<bool>();
88 }
89
90 template <template <typename SocketAddress> typename ConfigAddressType>
91 SocketAddress ConfigAddressReverse<ConfigAddressType>::getSocketAddress(const SocketAddress::SockAddr& sockAddr,
92 SocketAddress::SockLen sockAddrLen) {
93 SocketAddress socketAddress;
94 try {
95 socketAddress = SocketAddress(sockAddr, sockAddrLen, numericReverseOpt->as<bool>());
96 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
97 try {
98 socketAddress = Super::getSocketAddress(sockAddr, sockAddrLen);
99 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
100 throw;
101 }
103
104 return socketAddress;
105 }
107 template <template <typename SocketAddress> typename ConfigAddressType>
108 ConfigAddress<ConfigAddressType>::ConfigAddress(net::config::ConfigInstance* instance,
109 [[maybe_unused]] const std::string& addressOptionName,
110 [[maybe_unused]] const std::string& addressOptionDescription)
111 : ConfigSection(instance, this)
112 , ConfigAddressType<net::in::SocketAddress>(this) {
113 hostOpt = addOption( //
114 "--host",
115 "Host name or IPv4 address",
116 "hostname|IPv4",
117 "0.0.0.0",
118 CLI::TypeValidator<std::string>());
119
120 portOpt = addOption( //
121 "--port",
122 "Port number",
123 "port",
124 0,
125 CLI::Range(std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max()));
126
127 numericOpt = addFlag( //
128 "--numeric{true}",
129 "Suppress host name lookup",
130 "bool",
131 XSTR(IN_NUMERIC),
132 CLI::IsMember({"true", "false"}));
133
134 numericReverseOpt = addFlag( //
135 "--numeric-reverse{true}",
136 "Suppress reverse host name lookup",
137 "bool",
138 XSTR(IN_NUMERIC_REVERSE),
139 CLI::IsMember({"true", "false"}));
140 }
141
142 template <template <typename SocketAddress> typename ConfigAddressType>
143 SocketAddress* ConfigAddress<ConfigAddressType>::init() {
144 SocketAddress* socketAddress = new SocketAddress(hostOpt->as<std::string>(), portOpt->as<uint16_t>());
145
146 try {
147 socketAddress->init({.aiFlags = (aiFlags & ~AI_NUMERICHOST) | (numericOpt->as<bool>() ? AI_NUMERICHOST : 0),
148 .aiSockType = aiSockType,
149 .aiProtocol = aiProtocol});
150 } catch ([[maybe_unused]] const core::socket::SocketAddress::BadSocketAddress& badSocketAddress) {
151 delete socketAddress;
152 socketAddress = nullptr;
153
154 throw;
155 }
156
157 return socketAddress;
158 }
159
160 template <template <typename SocketAddress> typename ConfigAddressType>
161 SocketAddress ConfigAddress<ConfigAddressType>::getSocketAddress(const SocketAddress::SockAddr& sockAddr,
162 SocketAddress::SockLen sockAddrLen) {
163 SocketAddress socketAddress;
164 try {
165 socketAddress = SocketAddress(sockAddr, sockAddrLen, numericReverseOpt->as<bool>());
166 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
167 try {
168 socketAddress = Super::getSocketAddress(sockAddr, sockAddrLen);
169 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
170 throw;
171 }
172 }
173
174 return socketAddress;
175 }
176
177 template <template <typename SocketAddress> typename ConfigAddressType>
178 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setSocketAddress(const SocketAddress& socketAddress) {
179 setHost(socketAddress.getHost());
180 setPort(socketAddress.getPort());
181
182 return *this;
183 }
184
185 template <template <typename SocketAddress> typename ConfigAddressType>
186 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setHost(const std::string& ipOrHostname) {
187 const utils::PreserveErrno preserveErrno;
188
189 setDefaultValue(hostOpt, ipOrHostname);
190 required(hostOpt, false);
191
192 return *this;
193 }
194
195 template <template <typename SocketAddress> typename ConfigAddressType>
196 std::string ConfigAddress<ConfigAddressType>::getHost() const {
197 return hostOpt->as<std::string>();
198 }
199
200 template <template <typename SocketAddress> typename ConfigAddressType>
201 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setPort(uint16_t port) {
202 const utils::PreserveErrno preserveErrno;
203
204 setDefaultValue(portOpt, port);
205 required(portOpt, false);
206
207 return *this;
208 }
209
210 template <template <typename SocketAddress> typename ConfigAddressType>
211 uint16_t ConfigAddress<ConfigAddressType>::getPort() const {
212 return portOpt->as<uint16_t>();
213 }
214
215 template <template <typename SocketAddress> typename ConfigAddressType>
216 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setNumeric(bool numeric) {
217 const utils::PreserveErrno preserveErrno;
218
219 setDefaultValue(numericOpt, numeric ? "true" : "false");
220
221 return *this;
222 }
223
224 template <template <typename SocketAddress> typename ConfigAddressType>
225 bool ConfigAddress<ConfigAddressType>::getNumeric() const {
226 return numericOpt->as<bool>();
227 }
228
229 template <template <typename SocketAddress> typename ConfigAddressType>
230 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setNumericReverse(bool numeric) {
231 const utils::PreserveErrno preserveErrno;
232
233 setDefaultValue(numericReverseOpt, numeric ? "true" : "false");
234
235 return *this;
236 }
237
238 template <template <typename SocketAddress> typename ConfigAddressType>
239 bool ConfigAddress<ConfigAddressType>::getNumericReverse() const {
240 return numericReverseOpt->as<bool>();
241 }
242
243 template <template <typename SocketAddressT> typename ConfigAddressTypeT>
244 void ConfigAddress<ConfigAddressTypeT>::configurable(bool configurable) {
245 this->setConfigurable(hostOpt, configurable);
246 this->setConfigurable(portOpt, configurable);
247 }
248
249 template <template <typename SocketAddress> typename ConfigAddressType>
250 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setAiFlags(int aiFlags) {
251 this->aiFlags = aiFlags;
252
253 return *this;
254 }
255
256 template <template <typename SocketAddress> typename ConfigAddressType>
257 int ConfigAddress<ConfigAddressType>::getAiFlags() const {
258 return aiFlags;
259 }
260
261 template <template <typename SocketAddress> typename ConfigAddressType>
262 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setAiSockType(int aiSockType) {
263 this->aiSockType = aiSockType;
264
265 return *this;
266 }
267
268 template <template <typename SocketAddress> typename ConfigAddressType>
269 int ConfigAddress<ConfigAddressType>::getAiSockType() const {
270 return aiSockType;
271 }
272
273 template <template <typename SocketAddress> typename ConfigAddressType>
274 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setAiProtocol(int aiProtocol) {
275 this->aiProtocol = aiProtocol;
276
277 return *this;
278 }
279
280 template <template <typename SocketAddress> typename ConfigAddressType>
281 int ConfigAddress<ConfigAddressType>::getAiProtocol() const {
282 return aiProtocol;
283 }
284
285 template <template <typename SocketAddress> typename ConfigAddressType>
286 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setHostRequired(bool required) {
287 this->required(hostOpt, required);
288
289 return *this;
290 }
291
292 template <template <typename SocketAddress> typename ConfigAddressType>
293 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setPortRequired(bool required) {
294 this->required(portOpt, required);
295
296 return *this;
297 }
298
299} // namespace net::in::config
300
301template class net::config::ConfigAddress<net::in::SocketAddress>;
302template class net::config::ConfigAddressLocal<net::in::SocketAddress>;
303template class net::config::ConfigAddressRemote<net::in::SocketAddress>;
304template class net::config::ConfigAddressReverse<net::in::SocketAddress>;
305template class net::config::ConfigAddressBase<net::in::SocketAddress>;
306template class net::in::config::ConfigAddress<net::config::ConfigAddressLocal>;
307template class net::in::config::ConfigAddress<net::config::ConfigAddressRemote>;
308template class net::in::config::ConfigAddressReverse<net::config::ConfigAddressReverse>;
#define XSTR(s)
SocketAddress(const std::string &ipOrHostname, uint16_t port)
std::string getHost() const
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen, bool numeric=true)
void init(const Hints &hints={.aiFlags=0,.aiSockType=0,.aiProtocol=0})
uint16_t getPort() const
ConfigAddressReverse(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddressReverse & setNumericReverse(bool numeric=true)
ConfigAddressTypeT< SocketAddress > Super
SocketAddress getSocketAddress(const SocketAddress::SockAddr &sockAddr, SocketAddress::SockLen sockAddrLen)
SocketAddress getSocketAddress(const SocketAddress::SockAddr &sockAddr, SocketAddress::SockLen sockAddrLen)
ConfigAddress & setAiSockType(int aiSockType)
ConfigAddress & setPortRequired(bool required=true)
ConfigAddress & setSocketAddress(const SocketAddress &socketAddress)
ConfigAddressTypeT< net::in::SocketAddress > Super
ConfigAddress(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddress & setHostRequired(bool required=true)
ConfigAddress & setHost(const std::string &ipOrHostname)
SocketAddress * init() final
ConfigAddress & setAiFlags(int aiFlags)
ConfigAddress & setNumeric(bool numeric=true)
ConfigAddress & setAiProtocol(int aiProtocol)
void configurable(bool configurable=true) final
ConfigAddress & setNumericReverse(bool numeric=true)
ConfigAddress & setPort(uint16_t port)
#define STR(a)
Definition clients.h:48