SNode.C
Loading...
Searching...
No Matches
net::in::SocketAddress Class Referencefinal

#include <SocketAddress.h>

Inheritance diagram for net::in::SocketAddress:
Collaboration diagram for net::in::SocketAddress:

Classes

struct  Hints
 

Public Member Functions

 SocketAddress ()
 
 SocketAddress (const std::string &ipOrHostname)
 
 SocketAddress (uint16_t port)
 
 SocketAddress (const std::string &ipOrHostname, uint16_t port)
 
 SocketAddress (const SockAddr &sockAddr, SockLen sockAddrLen, bool numeric=true)
 
void init (const Hints &hints={.aiFlags=0,.aiSockType=0,.aiProtocol=0})
 
bool useNext () override
 
SocketAddresssetHost (const std::string &ipOrHostname)
 
std::string getHost () const
 
SocketAddresssetPort (uint16_t port)
 
uint16_t getPort () const
 
std::string getCanonName () const
 
std::string toString (bool expanded=true) const override
 
std::string getEndpoint (const std::string_view &format={}) const override
 
- Public Member Functions inherited from net::SocketAddress< sockaddr_in >
 SocketAddress (sa_family_t af, SockLen sockAddrLen=sizeof(SockAddr))
 
 SocketAddress (const SocketAddress &socketAddress)
 
 SocketAddress (const SockAddr &sockAddr, SockLen sockAddrLen)
 
SocketAddressoperator= (const SocketAddress &socketAddress)
 
const sockaddr & getSockAddr ()
 
const SockLengetSockAddrLen () const
 
sa_family_t getAddressFamily () const
 
- Public Member Functions inherited from core::socket::SocketAddress
virtual ~SocketAddress ()
 

Private Types

using Super = net::SocketAddress< sockaddr_in >
 

Private Attributes

std::string host = "0.0.0.0"
 
uint16_t port = 0
 
std::string canonName
 
std::shared_ptr< SocketAddrInfosocketAddrInfo
 

Additional Inherited Members

- Public Types inherited from net::SocketAddress< sockaddr_in >
using SockAddr = sockaddr_in
 
using SockLen = socklen_t
 
- Protected Attributes inherited from net::SocketAddress< sockaddr_in >
SockAddr sockAddr
 
SockLen sockAddrLen
 

Detailed Description

Definition at line 65 of file SocketAddress.h.

Member Typedef Documentation

◆ Super

using net::in::SocketAddress::Super = net::SocketAddress<sockaddr_in>
private

Definition at line 67 of file SocketAddress.h.

Constructor & Destructor Documentation

◆ SocketAddress() [1/5]

net::in::SocketAddress::SocketAddress ( )

Definition at line 58 of file SocketAddress.cpp.

59 : Super(AF_INET)
60 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
61 }
net::SocketAddress< sockaddr_in > Super
std::shared_ptr< SocketAddrInfo > socketAddrInfo

References net::SocketAddress< SockAddrT >::SocketAddress(), and socketAddrInfo.

Referenced by SocketAddress(), SocketAddress(), and SocketAddress().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SocketAddress() [2/5]

net::in::SocketAddress::SocketAddress ( const std::string &  ipOrHostname)
explicit

Definition at line 63 of file SocketAddress.cpp.

64 : SocketAddress() {
65 setHost(ipOrHostname);
66 }
SocketAddress & setHost(const std::string &ipOrHostname)

References setHost(), and SocketAddress().

Here is the call graph for this function:

◆ SocketAddress() [3/5]

net::in::SocketAddress::SocketAddress ( uint16_t  port)
explicit

Definition at line 68 of file SocketAddress.cpp.

69 : SocketAddress() {
71 }
SocketAddress & setPort(uint16_t port)

References setPort(), and SocketAddress().

Here is the call graph for this function:

◆ SocketAddress() [4/5]

net::in::SocketAddress::SocketAddress ( const std::string &  ipOrHostname,
uint16_t  port 
)

Definition at line 73 of file SocketAddress.cpp.

74 : SocketAddress() {
75 setHost(ipOrHostname);
77 }

References setHost(), setPort(), and SocketAddress().

Referenced by web::http::legacy::in::EventSource(), web::http::tls::in::EventSource(), web::http::legacy::in::EventSource(), web::http::tls::in::EventSource(), tls::getClient(), legacy::getLegacyClient(), net::in::config::ConfigAddress< ConfigAddressTypeT >::init(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SocketAddress() [5/5]

net::in::SocketAddress::SocketAddress ( const SockAddr sockAddr,
SockLen  sockAddrLen,
bool  numeric = true 
)

Definition at line 79 of file SocketAddress.cpp.

81 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
82 char hostC[NI_MAXHOST];
83 char servC[NI_MAXSERV];
84 std::memset(hostC, 0, NI_MAXHOST);
85 std::memset(servC, 0, NI_MAXSERV);
86
87 const int aiErrCode = core::system::getnameinfo(reinterpret_cast<const sockaddr*>(&sockAddr),
88 sizeof(sockAddr),
89 hostC,
90 NI_MAXHOST,
91 servC,
92 NI_MAXSERV,
93 NI_NUMERICSERV | (numeric ? NI_NUMERICHOST : NI_NAMEREQD));
94 if (aiErrCode == 0) {
95 this->host = hostC;
96 this->port = (servC[0] != '\0') ? static_cast<uint16_t>(std::stoul(servC)) : 0;
97 } else {
98 core::socket::State state = core::socket::STATE_OK;
99 switch (aiErrCode) {
100 case EAI_AGAIN:
101 case EAI_MEMORY:
102 state = core::socket::STATE_ERROR;
103 break;
104 default:
105 state = core::socket::STATE_FATAL;
106 break;
107 }
108
110 state, aiErrCode == EAI_SYSTEM ? strerror(errno) : gai_strerror(aiErrCode), aiErrCode == EAI_SYSTEM ? errno : aiErrCode);
111 }
112 }
int getnameinfo(const sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition netdb.cpp:72

References host, port, net::SocketAddress< SockAddrT >::SocketAddress(), and socketAddrInfo.

Referenced by net::in::config::ConfigAddressReverse< ConfigAddressTypeT >::getSocketAddress(), and net::in::config::ConfigAddress< ConfigAddressTypeT >::getSocketAddress().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ getCanonName()

std::string net::in::SocketAddress::getCanonName ( ) const

Definition at line 167 of file SocketAddress.cpp.

167 {
168 return canonName;
169 }

References canonName.

Referenced by legacy::getLegacyClient().

Here is the caller graph for this function:

◆ getEndpoint()

std::string net::in::SocketAddress::getEndpoint ( const std::string_view &  format = {}) const
overridevirtual

Reimplemented from core::socket::SocketAddress.

Definition at line 177 of file SocketAddress.cpp.

177 {
178 return std::vformat(format, std::make_format_args((port)));
179 }

References port.

◆ getHost()

std::string net::in::SocketAddress::getHost ( ) const

Definition at line 153 of file SocketAddress.cpp.

153 {
154 return host;
155 }

References host.

Referenced by net::in::config::ConfigAddress< ConfigAddressTypeT >::setSocketAddress().

Here is the caller graph for this function:

◆ getPort()

uint16_t net::in::SocketAddress::getPort ( ) const

Definition at line 163 of file SocketAddress.cpp.

163 {
164 return port;
165 }

References port.

Referenced by net::in::config::ConfigAddress< ConfigAddressTypeT >::setSocketAddress().

Here is the caller graph for this function:

◆ init()

void net::in::SocketAddress::init ( const Hints hints = {.aiFlags = 0, .aiSockType = 0, .aiProtocol = 0})

Definition at line 114 of file SocketAddress.cpp.

114 {
115 addrinfo addrInfoHints{};
116
117 addrInfoHints.ai_family = Super::getAddressFamily();
118 addrInfoHints.ai_flags =
119 hints.aiFlags | AI_ADDRCONFIG | AI_CANONNAME /*| AI_CANONIDN*/; // AI_CANONIDN produces a still reachable memory leak
120 addrInfoHints.ai_socktype = hints.aiSockType;
121 addrInfoHints.ai_protocol = hints.aiProtocol;
122
123 const int aiErrCode = socketAddrInfo->resolve(host, std::to_string(port), addrInfoHints);
124 if (aiErrCode == 0) {
125 sockAddr = socketAddrInfo->getSockAddr();
126 canonName = socketAddrInfo->getCanonName();
127 } else {
128 core::socket::State state = core::socket::STATE_OK;
129
130 switch (aiErrCode) {
131 case EAI_AGAIN:
132 case EAI_MEMORY:
133 state = core::socket::STATE_ERROR;
134 break;
135 default:
136 state = core::socket::STATE_FATAL;
137 break;
138 }
139
141 host + ":" + std::to_string(port) + ": " +
142 (aiErrCode == EAI_SYSTEM ? strerror(errno) : gai_strerror(aiErrCode)),
143 (aiErrCode == EAI_SYSTEM ? errno : aiErrCode));
144 }
145 }

References net::in::SocketAddress::Hints::aiProtocol, net::in::SocketAddress::Hints::aiSockType, canonName, net::SocketAddress< SockAddrT >::getAddressFamily(), net::in::SocketAddrInfo::getCanonName(), net::in::SocketAddrInfo::getSockAddr(), host, port, and socketAddrInfo.

Referenced by legacy::getLegacyClient().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setHost()

SocketAddress & net::in::SocketAddress::setHost ( const std::string &  ipOrHostname)

Definition at line 147 of file SocketAddress.cpp.

147 {
148 this->host = ipOrHostname;
149
150 return *this;
151 }

References host.

Referenced by SocketAddress(), and SocketAddress().

Here is the caller graph for this function:

◆ setPort()

SocketAddress & net::in::SocketAddress::setPort ( uint16_t  port)

Definition at line 157 of file SocketAddress.cpp.

157 {
158 this->port = port;
159
160 return *this;
161 }

References port.

Referenced by SocketAddress(), and SocketAddress().

Here is the caller graph for this function:

◆ toString()

std::string net::in::SocketAddress::toString ( bool  expanded = true) const
overridevirtual

Implements core::socket::SocketAddress.

Definition at line 171 of file SocketAddress.cpp.

171 {
172 return std::string(host).append(std::string(":")
173 .append(std::to_string(port))
174 .append(expanded && !canonName.empty() ? std::string(" (").append(canonName).append(")") : ""));
175 }

References canonName, host, and port.

Referenced by tls::getClient(), legacy::getLegacyClient(), main(), express::legacy::in::Server(), and express::tls::in::Server().

Here is the caller graph for this function:

◆ useNext()

bool net::in::SocketAddress::useNext ( )
overridevirtual

Reimplemented from core::socket::SocketAddress.

Definition at line 181 of file SocketAddress.cpp.

181 {
182 const bool useNext = socketAddrInfo->useNext();
183
184 if (useNext) {
185 sockAddr = socketAddrInfo->getSockAddr();
186 }
187
188 return useNext;
189 }

References net::in::SocketAddrInfo::getSockAddr(), socketAddrInfo, and net::in::SocketAddrInfo::useNext().

Here is the call graph for this function:

Member Data Documentation

◆ canonName

std::string net::in::SocketAddress::canonName
private

Definition at line 101 of file SocketAddress.h.

Referenced by getCanonName(), init(), and toString().

◆ host

std::string net::in::SocketAddress::host = "0.0.0.0"
private

Definition at line 98 of file SocketAddress.h.

Referenced by getHost(), init(), setHost(), SocketAddress(), and toString().

◆ port

uint16_t net::in::SocketAddress::port = 0
private

Definition at line 99 of file SocketAddress.h.

Referenced by getEndpoint(), getPort(), init(), setPort(), SocketAddress(), and toString().

◆ socketAddrInfo

std::shared_ptr<SocketAddrInfo> net::in::SocketAddress::socketAddrInfo
private

Definition at line 103 of file SocketAddress.h.

Referenced by init(), SocketAddress(), SocketAddress(), and useNext().


The documentation for this class was generated from the following files: