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
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>
65 ConfigAddressReverse<ConfigAddressType>::ConfigAddressReverse(net::config::ConfigInstance* instance,
66 const std::string& addressOptionName,
67 const std::string& addressOptionDescription)
68 : Super(instance, addressOptionName, addressOptionDescription) {
69 numericReverseOpt = Super::addFlag( //
70 "--numeric-reverse{true}",
71 "Suppress reverse host name lookup",
72 "bool",
73 XSTR(IN_NUMERIC_REVERSE),
74 CLI::IsMember({"true", "false"}));
75 }
76
77 template <template <typename SocketAddress> typename ConfigAddressType>
78 ConfigAddressReverse<ConfigAddressType>& ConfigAddressReverse<ConfigAddressType>::setNumericReverse(bool numeric) {
80 ->default_str(numeric ? "true" : "false")
81 ->clear();
82
83 return *this;
84 }
85
86 template <template <typename SocketAddress> typename ConfigAddressType>
87 bool ConfigAddressReverse<ConfigAddressType>::getNumericReverse() const {
88 return numericReverseOpt->as<bool>();
89 }
90
91 template <template <typename SocketAddress> typename ConfigAddressType>
92 SocketAddress ConfigAddressReverse<ConfigAddressType>::getSocketAddress(const SocketAddress::SockAddr& sockAddr,
93 SocketAddress::SockLen sockAddrLen) {
94 SocketAddress socketAddress;
95 try {
96 socketAddress = SocketAddress(sockAddr, sockAddrLen, numericReverseOpt->as<bool>());
97 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
98 try {
99 socketAddress = Super::getSocketAddress(sockAddr, sockAddrLen);
100 } catch ([[maybe_unused]] const SocketAddress::BadSocketAddress& badSocketAddress) {
101 throw;
102 }
103 }
104
105 return socketAddress;
106 }
107
108 template <template <typename SocketAddress> typename ConfigAddressType>
109 ConfigAddress<ConfigAddressType>::ConfigAddress(net::config::ConfigInstance* instance,
110 const std::string& addressOptionName,
111 const std::string& addressOptionDescription)
112 : Super(instance, addressOptionName, addressOptionDescription) {
113 hostOpt = Super::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 = Super::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 = Super::addFlag( //
128 "--numeric{true}",
129 "Suppress host name lookup",
130 "bool",
131 XSTR(IN_NUMERIC),
132 CLI::IsMember({"true", "false"}));
133
134 numericReverseOpt = Super::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 hostOpt //
190 ->default_val(ipOrHostname)
191 ->clear();
192 Super::required(hostOpt, false);
193
194 return *this;
195 }
196
197 template <template <typename SocketAddress> typename ConfigAddressType>
198 std::string ConfigAddress<ConfigAddressType>::getHost() const {
199 return hostOpt->as<std::string>();
200 }
201
202 template <template <typename SocketAddress> typename ConfigAddressType>
203 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setPort(uint16_t port) {
204 const utils::PreserveErrno preserveErrno;
205
206 portOpt //
207 ->default_val(port)
208 ->clear();
209 Super::required(portOpt, false);
210
211 return *this;
212 }
213
214 template <template <typename SocketAddress> typename ConfigAddressType>
215 uint16_t ConfigAddress<ConfigAddressType>::getPort() const {
216 return portOpt->as<uint16_t>();
217 }
218
219 template <template <typename SocketAddress> typename ConfigAddressType>
220 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setNumeric(bool numeric) {
221 const utils::PreserveErrno preserveErrno;
222
223 numericOpt //
224 ->default_str(numeric ? "true" : "false")
225 ->clear();
226
227 return *this;
228 }
229
230 template <template <typename SocketAddress> typename ConfigAddressType>
231 bool ConfigAddress<ConfigAddressType>::getNumeric() const {
232 return numericOpt->as<bool>();
233 }
234
235 template <template <typename SocketAddress> typename ConfigAddressType>
236 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setNumericReverse(bool numeric) {
238 ->default_str(numeric ? "true" : "false")
239 ->clear();
240
241 return *this;
242 }
243
244 template <template <typename SocketAddress> typename ConfigAddressType>
245 bool ConfigAddress<ConfigAddressType>::getNumericReverse() const {
246 return numericReverseOpt->as<bool>();
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 Super::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 Super::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)
uint16_t getPort() const
ConfigAddressReverse(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddressReverse & setNumericReverse(bool numeric=true)
SocketAddress getSocketAddress(const SocketAddress::SockAddr &sockAddr, SocketAddress::SockLen sockAddrLen)
SocketAddress getSocketAddress(const SocketAddress::SockAddr &sockAddr, SocketAddress::SockLen sockAddrLen)
ConfigAddress & setPortRequired(bool required=true)
ConfigAddress & setSocketAddress(const SocketAddress &socketAddress)
ConfigAddress(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddress & setAiSockType(int aiSocktype)
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)
ConfigAddress & setNumericReverse(bool numeric=true)
ConfigAddress & setPort(uint16_t port)
#define STR(a)
Definition clients.h:48