SNode.C
Loading...
Searching...
No Matches
ConfigSocketServer.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#include "net/in6/stream/config/ConfigSocketServer.h"
21
22#include "net/config/stream/ConfigSocketServer.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#ifdef __GNUC__
27#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-Wfloat-equal"
29#ifdef __has_warning
30#if __has_warning("-Wweak-vtables")
31#pragma GCC diagnostic ignored "-Wweak-vtables"
32#endif
33#if __has_warning("-Wcovered-switch-default")
34#pragma GCC diagnostic ignored "-Wcovered-switch-default"
35#endif
36#endif
37#endif
38#include "utils/CLI11.hpp"
39#ifdef __GNUC__
40#pragma GCC diagnostic pop
41#endif
42
43#include "core/system/netdb.h"
44
45#include <netinet/in.h>
46#include <string>
47
48#endif /* DOXYGEN_SHOULD_SKIP_THIS */
49
50#define XSTR(s) STR(s)
51#define STR(s) #s
52
53namespace net::in6::stream::config {
54
55 ConfigSocketServer::ConfigSocketServer(net::config::ConfigInstance* instance)
57 net::in6::config::ConfigAddress<net::config::ConfigAddressLocal>::setPortRequired();
58
59 net::in6::config::ConfigAddress<net::config::ConfigAddressLocal>::setAiFlags(AI_PASSIVE);
60 net::in6::config::ConfigAddress<net::config::ConfigAddressLocal>::setAiSockType(SOCK_STREAM);
61 net::in6::config::ConfigAddress<net::config::ConfigAddressLocal>::setAiProtocol(IPPROTO_TCP);
62
63 reusePortOpt = net::config::ConfigPhysicalSocket::addSocketOption( //
64 "--reuse-port{true}",
65 SOL_SOCKET,
66 SO_REUSEPORT,
67 "Reuse socket address",
68 "bool",
69 XSTR(REUSE_PORT),
70 CLI::IsMember({"true", "false"}));
71
72 iPv6OnlyOpt = net::config::ConfigPhysicalSocket::addSocketOption( //
73 "--ipv6-only{true}",
74 IPPROTO_IPV6,
75 IPV6_V6ONLY,
76 "Turn of IPv6 dual stack mode",
77 "bool",
78 XSTR(IPV6_ONLY),
79 CLI::IsMember({"true", "false"}));
80 }
81
84
86 if (reusePort) {
87 addSocketOption(SOL_SOCKET, SO_REUSEPORT, 1);
88 } else {
89 removeSocketOption(SO_REUSEPORT);
90 }
91
93 ->default_val(reusePort ? "true" : "false")
94 ->clear();
95
96 return *this;
97 }
98
100 return reusePortOpt->as<bool>();
101 }
102
104 if (iPv6Only) {
105 addSocketOption(IPPROTO_IPV6, IPV6_V6ONLY, 1);
106 } else {
107 removeSocketOption(IPV6_V6ONLY);
108 }
109
110 iPv6OnlyOpt //
111 ->default_val(iPv6Only ? "true" : "false")
112 ->clear();
113
114 return *this;
115 }
116
118 return iPv6OnlyOpt->as<bool>();
119 }
120
121} // namespace net::in6::stream::config
122
123template class net::config::stream::ConfigSocketServer<net::in6::config::ConfigAddress, net::in6::config::ConfigAddressReverse>;
#define XSTR(s)
ConfigSocketServer & setIPv6Only(bool iPv6Only=true)
ConfigSocketServer(net::config::ConfigInstance *instance)
ConfigSocketServer & setReusePort(bool reusePort=true)
#define STR(a)
Definition clients.h:26