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

#include <SocketAddress.h>

Inheritance diagram for net::in6::SocketAddress:
Collaboration diagram for net::in6::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_in6 >
 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_in6 >
 

Private Attributes

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

Additional Inherited Members

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

Detailed Description

Definition at line 64 of file SocketAddress.h.

Member Typedef Documentation

◆ Super

using net::in6::SocketAddress::Super = net::SocketAddress<sockaddr_in6>
private

Definition at line 66 of file SocketAddress.h.

Constructor & Destructor Documentation

◆ SocketAddress() [1/5]

net::in6::SocketAddress::SocketAddress ( )

Definition at line 58 of file SocketAddress.cpp.

59 : Super(AF_INET6)
60 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
61 }
net::SocketAddress< sockaddr_in6 > 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::in6::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::in6::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::in6::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 net::in6::config::ConfigAddress< ConfigAddressTypeT >::init().

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

◆ SocketAddress() [5/5]

net::in6::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 if (servC[0] != '\0') {
96 this->port = static_cast<uint16_t>(std::stoul(servC));
97 }
98
99 this->host = hostC;
100 this->canonName = hostC;
101 } else {
102 core::socket::State state = core::socket::STATE_OK;
103 switch (aiErrCode) {
104 case EAI_AGAIN:
105 case EAI_MEMORY:
106 state = core::socket::STATE_ERROR;
107 break;
108 default:
109 state = core::socket::STATE_FATAL;
110 break;
111 }
112
114 state, aiErrCode == EAI_SYSTEM ? strerror(errno) : gai_strerror(aiErrCode), aiErrCode == EAI_SYSTEM ? errno : aiErrCode);
115 }
116 }
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 canonName, host, port, net::SocketAddress< SockAddrT >::SocketAddress(), and socketAddrInfo.

Referenced by net::in6::config::ConfigAddressReverse< ConfigAddressTypeT >::getSocketAddress(), and net::in6::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::in6::SocketAddress::getCanonName ( ) const

Definition at line 171 of file SocketAddress.cpp.

171 {
172 return canonName;
173 }

References canonName.

◆ getEndpoint()

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

Reimplemented from core::socket::SocketAddress.

Definition at line 180 of file SocketAddress.cpp.

180 {
181 return std::vformat(format, std::make_format_args((port)));
182 }

References port.

◆ getHost()

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

Definition at line 157 of file SocketAddress.cpp.

157 {
158 return host;
159 }

References host.

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

Here is the caller graph for this function:

◆ getPort()

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

Definition at line 167 of file SocketAddress.cpp.

167 {
168 return port;
169 }

References port.

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

Here is the caller graph for this function:

◆ init()

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

Definition at line 118 of file SocketAddress.cpp.

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

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

Here is the call graph for this function:

◆ setHost()

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

Definition at line 151 of file SocketAddress.cpp.

151 {
152 this->host = ipOrHostname;
153
154 return *this;
155 }

References host.

Referenced by SocketAddress(), and SocketAddress().

Here is the caller graph for this function:

◆ setPort()

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

Definition at line 161 of file SocketAddress.cpp.

161 {
162 this->port = port;
163
164 return *this;
165 }

References port.

Referenced by SocketAddress(), and SocketAddress().

Here is the caller graph for this function:

◆ toString()

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

Implements core::socket::SocketAddress.

Definition at line 175 of file SocketAddress.cpp.

175 {
176 return std::string(host).append(expanded ? std::string(":").append(std::to_string(port)).append(" (").append(canonName).append(")")
177 : "");
178 }

References canonName, host, and port.

Referenced by main(), express::legacy::in6::Server(), and express::tls::in6::Server().

Here is the caller graph for this function:

◆ useNext()

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

Reimplemented from core::socket::SocketAddress.

Definition at line 184 of file SocketAddress.cpp.

184 {
185 const bool useNext = socketAddrInfo->useNext();
186
187 if (useNext) {
188 sockAddr = socketAddrInfo->getSockAddr();
189 }
190
191 return useNext;
192 }

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

Here is the call graph for this function:

Member Data Documentation

◆ canonName

std::string net::in6::SocketAddress::canonName
private

Definition at line 100 of file SocketAddress.h.

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

◆ host

std::string net::in6::SocketAddress::host = "::"
private

Definition at line 97 of file SocketAddress.h.

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

◆ port

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

Definition at line 98 of file SocketAddress.h.

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

◆ socketAddrInfo

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

Definition at line 102 of file SocketAddress.h.

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


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