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
 
- 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
 
using SockLen
 
- 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 57 of file SocketAddress.cpp.

58 : Super(AF_INET6)
59 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
60 }
net::SocketAddress< sockaddr_in6 > Super
std::shared_ptr< SocketAddrInfo > socketAddrInfo

References SocketAddress().

Referenced by SocketAddress(), 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 62 of file SocketAddress.cpp.

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

References SocketAddress().

Here is the call graph for this function:

◆ SocketAddress() [3/5]

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

Definition at line 67 of file SocketAddress.cpp.

68 : SocketAddress() {
70 }
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 72 of file SocketAddress.cpp.

73 : SocketAddress() {
74 setHost(ipOrHostname);
76 }

References setPort(), and SocketAddress().

Here is the call graph for this function:

◆ SocketAddress() [5/5]

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

Definition at line 78 of file SocketAddress.cpp.

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

Referenced by SocketAddress().

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 170 of file SocketAddress.cpp.

170 {
171 return canonName;
172 }

◆ getHost()

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

Definition at line 156 of file SocketAddress.cpp.

156 {
157 return host;
158 }

◆ getPort()

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

Definition at line 166 of file SocketAddress.cpp.

166 {
167 return port;
168 }

References port.

◆ init()

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

Definition at line 117 of file SocketAddress.cpp.

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

◆ setHost()

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

Definition at line 150 of file SocketAddress.cpp.

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

◆ setPort()

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

Definition at line 160 of file SocketAddress.cpp.

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

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 174 of file SocketAddress.cpp.

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

◆ useNext()

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

Reimplemented from core::socket::SocketAddress.

Definition at line 179 of file SocketAddress.cpp.

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

Member Data Documentation

◆ canonName

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

Definition at line 99 of file SocketAddress.h.

◆ host

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

Definition at line 96 of file SocketAddress.h.

◆ port

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

Definition at line 97 of file SocketAddress.h.

Referenced by getPort(), and setPort().

◆ socketAddrInfo

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

Definition at line 101 of file SocketAddress.h.


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