SNode.C
Loading...
Searching...
No Matches
ConfigAddress.h
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#ifndef NET_IN_CONFIG_CONFIGADDRESS_H
21#define NET_IN_CONFIG_CONFIGADDRESS_H
22
23#include "net/config/ConfigAddressLocal.h"
24#include "net/config/ConfigAddressRemote.h"
25#include "net/config/ConfigAddressReverse.h"
26#include "net/in/SocketAddress.h"
27
28namespace net::config {
29 class ConfigInstance;
30} // namespace net::config
31
32#ifndef DOXYGEN_SHOULD_SKIP_THIS
33
34#include <cstdint>
35#include <string>
36
37namespace CLI {
38 class Option;
39} // namespace CLI
40
41#endif /* DOXYGEN_SHOULD_SKIP_THIS */
42
43namespace net::in::config {
44
45 template <template <typename SocketAddressT> typename ConfigAddressTypeT>
46 class ConfigAddressReverse : public ConfigAddressTypeT<net::in::SocketAddress> {
47 private:
48 using Super = ConfigAddressTypeT<SocketAddress>;
49
50 protected:
51 explicit ConfigAddressReverse(net::config::ConfigInstance* instance,
52 const std::string& addressOptionName,
53 const std::string& addressOptionDescription);
54
55 public:
56 SocketAddress getSocketAddress(const SocketAddress::SockAddr& sockAddr, SocketAddress::SockLen sockAddrLen);
57
58 ConfigAddressReverse& setNumericReverse(bool numeric = true);
59 bool getNumericReverse() const;
60
61 private:
62 CLI::Option* numericReverseOpt = nullptr;
63 };
64
65 template <template <typename SocketAddressT> typename ConfigAddressTypeT>
66 class ConfigAddress : public ConfigAddressTypeT<net::in::SocketAddress> {
67 private:
68 using Super = ConfigAddressTypeT<net::in::SocketAddress>;
69
70 protected:
71 explicit ConfigAddress(net::config::ConfigInstance* instance,
72 const std::string& addressOptionName,
73 const std::string& addressOptionDescription);
74
75 private:
76 SocketAddress* init() final;
77
78 public:
79 using Super::getSocketAddress;
80 SocketAddress getSocketAddress(const SocketAddress::SockAddr& sockAddr, SocketAddress::SockLen sockAddrLen);
81
82 ConfigAddress& setSocketAddress(const SocketAddress& socketAddress);
83
84 ConfigAddress& setHost(const std::string& ipOrHostname);
85 std::string getHost() const;
86
87 ConfigAddress& setPort(uint16_t port);
88 uint16_t getPort() const;
89
90 ConfigAddress& setNumeric(bool numeric = true);
91 bool getNumeric() const;
92
93 ConfigAddress& setNumericReverse(bool numeric = true);
94 bool getNumericReverse() const;
95
96 protected:
97 ConfigAddress& setAiFlags(int aiFlags);
98 int getAiFlags() const;
99
100 ConfigAddress& setAiSockType(int aiSocktype);
101 int getAiSockType() const;
102
103 ConfigAddress& setAiProtocol(int aiProtocol);
104 int getAiProtocol() const;
105
106 ConfigAddress& setHostRequired(bool required = true);
107 ConfigAddress& setPortRequired(bool required = true);
108
109 private:
110 CLI::Option* hostOpt = nullptr;
111 CLI::Option* portOpt = nullptr;
112 CLI::Option* numericOpt = nullptr;
113 CLI::Option* numericReverseOpt = nullptr;
114
115 int aiFlags = 0;
116 int aiSockType = 0;
117 int aiProtocol = 0;
118 };
119
120} // namespace net::in::config
121
122extern template class net::config::ConfigAddress<net::in::SocketAddress>;
123extern template class net::config::ConfigAddressLocal<net::in::SocketAddress>;
124extern template class net::config::ConfigAddressRemote<net::in::SocketAddress>;
125extern template class net::config::ConfigAddressReverse<net::in::SocketAddress>;
126extern template class net::config::ConfigAddressBase<net::in::SocketAddress>;
127extern template class net::in::config::ConfigAddress<net::config::ConfigAddressLocal>;
128extern template class net::in::config::ConfigAddress<net::config::ConfigAddressRemote>;
129extern template class net::in::config::ConfigAddressReverse<net::config::ConfigAddressReverse>;
130
131#endif // NET_IN_CONFIG_CONFIGADDRESS_H
Socket & operator=(Socket &&) noexcept=delete
virtual ~Socket()
Definition Socket.hpp:34
Socket & operator=(const Socket &)=default
Socket(const Socket &)=default
Config & getConfig() const
Definition Socket.hpp:38
std::shared_ptr< Config > config
Definition Socket.h:50
Socket(Socket &&) noexcept=delete
Socket(const std::string &name)
Definition Socket.hpp:29
void onReceivedFromPeer(std::size_t available) final
void setTimeout(const utils::Timeval &timeout) final
std::size_t readFromPeer(char *chunk, std::size_t chunkLen) final
const SocketAddress & getRemoteAddress() const final
SocketConnectionT(const std::string &instanceName, PhysicalSocket &&physicalSocket, const std::function< void()> &onDisconnect, const std::string &configuredServer, const SocketAddress &localAddress, const SocketAddress &remoteAddress, const utils::Timeval &readTimeout, const utils::Timeval &writeTimeout, std::size_t readBlockSize, std::size_t writeBlockSize, const utils::Timeval &terminateTimeout)
void doWriteShutdown(const std::function< void()> &onShutdown) override
bool streamToPeer(core::pipe::Source *source) final
void shutdownWrite(bool forceClose) final
void sendToPeer(const char *chunk, std::size_t chunkLen) final
const SocketAddress & getLocalAddress() const final
const std::string & getConfiguredServer() const
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0
core::socket::stream::SocketContext * newSocketContext
core::socket::stream::SocketContext * socketContext
void connectSocketContext(const std::shared_ptr< SocketContextFactory > &socketContextFactory)
void switchSocketContext(SocketContext *newSocketContext)
virtual const core::socket::SocketAddress & getLocalAddress() const =0
virtual void setTimeout(const utils::Timeval &timeout)=0
void sentToPeer(const std::vector< uint8_t > &data)
const std::string & getConnectionName() const
void setSocketContext(SocketContext *socketContext)
virtual const core::socket::SocketAddress & getRemoteAddress() const =0
void sendToPeer(const std::string &data)
virtual void shutdownWrite(bool forceClose)=0
virtual std::size_t readFromPeer(char *chunk, std::size_t chunkLen)=0
SocketConnection(const std::string &instanceName, int fd, const std::string &configuredServer)
virtual bool streamToPeer(core::pipe::Source *source)=0
const std::string & getInstanceName() const
SocketConnector(const SocketConnector &socketConnector)
std::function< void(const SocketAddress &, core::socket::State)> onStatus
std::function< void(SocketConnection *)> onConnected
std::shared_ptr< core::socket::stream::SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> onDisconnect
std::function< void(SocketConnection *)> onConnect
SocketConnector(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
SocketConnection(const std::string &instanceName, PhysicalSocket &&physicalSocket, const std::function< void(SocketConnection *)> &onDisconnect, const std::string &configuredServer, const SocketAddress &localAddress, const SocketAddress &remoteAddress, const utils::Timeval &readTimeout, const utils::Timeval &writeTimeout, std::size_t readBlockSize, std::size_t writeBlockSize, const utils::Timeval &terminateTimeout)
SocketConnector(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
SocketConnector(const SocketConnector &socketConnector)
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, ValueType defaultValue)
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, ValueType defaultValue, const CLI::Validator &additionalValidator)
CLI::Option * addOption(const std::string &name, const std::string &description, const std::string &typeName, ValueType defaultValue)
CLI::Option * addOption(const std::string &name, const std::string &description, const std::string &typeName, ValueType defaultValue, const CLI::Validator &additionalValidator)
SocketAddress & setPort(uint16_t port)
SocketAddress(const std::string &ipOrHostname, uint16_t port)
SocketAddress & setHost(const std::string &ipOrHostname)
SocketAddress(uint16_t port)
SocketAddress(const std::string &ipOrHostname)
std::string getHost() const
std::string getCanonName() const
std::shared_ptr< SocketAddrInfo > socketAddrInfo
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen, bool numeric=true)
std::string toString(bool expanded=true) const override
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)
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)
PhysicalSocket(PhysicalSocket &&) noexcept=default
PhysicalSocket(int type, int protocol)
PhysicalSocketClient(PhysicalSocketClient &&) noexcept=default
PhysicalSocket(PhysicalSocket &&) noexcept=default
void connect(const std::string &ipOrHostname, uint16_t port, const std::string &bindHost, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void connect(const std::string &ipOrHostname, uint16_t port, const std::string &bindHost, uint16_t bindPort, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void connect(const std::string &ipOrHostname, uint16_t port, uint16_t bindPort, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void connect(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
ConfigSocketClient(net::config::ConfigInstance *instance)
int getPeerName(typename SocketAddress::SockAddr &remoteSockAddr, typename SocketAddress::SockLen &remoteSockAddrLen)
PhysicalSocket(const PhysicalSocket &)=delete
PhysicalSocket(int domain, int type, int protocol)
int setSockopt(int level, int optname, const void *optval, typename SocketAddress::SockLen optlen) const
int getSockError(int &cErrno) const
PhysicalSocket(PhysicalSocket &&physicalSocket) noexcept=default
PhysicalSocket & operator=(PhysicalSocket &)=delete
PhysicalSocket & operator=(const PhysicalSocket &)=delete
int open(const std::map< int, const PhysicalSocketOption > &socketOptions, Flags flags)
PhysicalSocket(PhysicalSocket &)=delete
int bind(SocketAddress &bindAddress)
PhysicalSocket & operator=(PhysicalSocket &&) noexcept=default
PhysicalSocket(int fd, const SocketAddress &bindAddress)
int getSockName(typename SocketAddress::SockAddr &localSockAddr, typename SocketAddress::SockLen &localSockAddrLen)
SocketAddress getBindAddress() const
int getSockopt(int level, int optname, void *optval, typename SocketAddress::SockLen *optlen) const
int connect(SocketAddress &remoteAddress)
double getMsd() const
Definition Timeval.cpp:167
const timeval * operator&() const
Definition Timeval.cpp:178
bool operator>(const Timeval &timeVal) const
Definition Timeval.cpp:127
friend Timeval operator*(double mul, const Timeval &timeVal)
Definition Timeval.cpp:201
bool operator<(const Timeval &timeVal) const
Definition Timeval.cpp:122
Timeval operator*(double mul) const
Definition Timeval.cpp:94
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:37
timeval * operator&()
Definition Timeval.cpp:147
Timeval operator-() const
Definition Timeval.cpp:118
static Timeval currentTime()
Definition Timeval.cpp:54
timeval timeVal
Definition Timeval.h:77
bool operator<=(const Timeval &timeVal) const
Definition Timeval.cpp:131
int getMs() const
Definition Timeval.cpp:155
bool operator!=(const Timeval &timeVal) const
Definition Timeval.cpp:143
timespec getTimespec() const
Definition Timeval.cpp:151
Timeval operator-(const Timeval &timeVal) const
Definition Timeval.cpp:85
Timeval & operator=(const timeval &timeVal)
Definition Timeval.cpp:70
Timeval & operator+=(const Timeval &timeVal)
Definition Timeval.cpp:103
bool operator==(const Timeval &timeVal) const
Definition Timeval.cpp:139
Timeval & operator-=(const Timeval &timeVal)
Definition Timeval.cpp:108
Timeval() noexcept
Definition Timeval.cpp:33
Timeval operator+(const Timeval &timeVal) const
Definition Timeval.cpp:76
Timeval & operator=(const Timeval &timeVal)
Definition Timeval.cpp:61
Timeval & operator*=(double mul)
Definition Timeval.cpp:113
bool operator>=(const Timeval &timeVal) const
Definition Timeval.cpp:135
Timeval(double time) noexcept
Definition Timeval.cpp:43
Timeval(const Timeval &timeVal) noexcept=default
Timeval(const timeval &timeVal) noexcept
Definition Timeval.cpp:50
const Timeval & normalize()
Definition Timeval.cpp:182
int main(int argc, char *argv[])
Definition Config.h:37