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 42 of file SocketAddress.h.

Member Typedef Documentation

◆ Super

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

Definition at line 44 of file SocketAddress.h.

Constructor & Destructor Documentation

◆ SocketAddress() [1/5]

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

Definition at line 35 of file SocketAddress.cpp.

36 : Super(AF_INET6)
37 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
38 }
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 40 of file SocketAddress.cpp.

41 : SocketAddress() {
42 setHost(ipOrHostname);
43 }
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 45 of file SocketAddress.cpp.

46 : SocketAddress() {
48 }
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 50 of file SocketAddress.cpp.

51 : SocketAddress() {
52 setHost(ipOrHostname);
54 }

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

58 , socketAddrInfo(std::make_shared<SocketAddrInfo>()) {
59 char hostC[NI_MAXHOST];
60 char servC[NI_MAXSERV];
61 std::memset(hostC, 0, NI_MAXHOST);
62 std::memset(servC, 0, NI_MAXSERV);
63
64 const int aiErrCode = core::system::getnameinfo(reinterpret_cast<const sockaddr*>(&sockAddr),
65 sizeof(sockAddr),
66 hostC,
67 NI_MAXHOST,
68 servC,
69 NI_MAXSERV,
70 NI_NUMERICSERV | (numeric ? NI_NUMERICHOST : NI_NAMEREQD));
71 if (aiErrCode == 0) {
72 if (servC[0] != '\0') {
73 this->port = static_cast<uint16_t>(std::stoul(servC));
74 }
75
76 this->host = hostC;
77 this->canonName = hostC;
78 } else {
79 core::socket::State state = core::socket::STATE_OK;
80 switch (aiErrCode) {
81 case EAI_AGAIN:
82 case EAI_MEMORY:
83 state = core::socket::STATE_ERROR;
84 break;
85 default:
86 state = core::socket::STATE_FATAL;
87 break;
88 }
89
91 state, aiErrCode == EAI_SYSTEM ? strerror(errno) : gai_strerror(aiErrCode), aiErrCode == EAI_SYSTEM ? errno : aiErrCode);
92 }
93 }
int getnameinfo(const sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition netdb.cpp:40

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

148 {
149 return canonName;
150 }

◆ getHost()

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

Definition at line 134 of file SocketAddress.cpp.

134 {
135 return host;
136 }

◆ getPort()

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

Definition at line 144 of file SocketAddress.cpp.

144 {
145 return port;
146 }

References port.

◆ init()

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

Definition at line 95 of file SocketAddress.cpp.

95 {
96 addrinfo addrInfoHints{};
97
98 addrInfoHints.ai_family = Super::getAddressFamily();
99 addrInfoHints.ai_flags =
100 hints.aiFlags | AI_ADDRCONFIG | AI_CANONNAME /*| AI_CANONIDN*/ | AI_ALL; // AI_CANONIDN produces a still reachable memory leak
101 addrInfoHints.ai_socktype = hints.aiSockType;
102 addrInfoHints.ai_protocol = hints.aiProtocol;
103
104 const int aiErrCode = socketAddrInfo->resolve(host, std::to_string(port), addrInfoHints);
105 if (aiErrCode == 0) {
106 sockAddr = socketAddrInfo->getSockAddr();
107 canonName = socketAddrInfo->getCanonName();
108 } else {
109 core::socket::State state = core::socket::STATE_OK;
110
111 switch (aiErrCode) {
112 case EAI_AGAIN:
113 case EAI_MEMORY:
114 state = core::socket::STATE_ERROR;
115 break;
116 default:
117 state = core::socket::STATE_FATAL;
118 break;
119 }
120
122 host + ":" + std::to_string(port) + " - " +
123 (aiErrCode == EAI_SYSTEM ? strerror(errno) : gai_strerror(aiErrCode)),
124 (aiErrCode == EAI_SYSTEM ? errno : aiErrCode));
125 }
126 }

◆ setHost()

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

Definition at line 128 of file SocketAddress.cpp.

128 {
129 this->host = ipOrHostname;
130
131 return *this;
132 }

◆ setPort()

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

Definition at line 138 of file SocketAddress.cpp.

138 {
139 this->port = port;
140
141 return *this;
142 }

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

152 {
153 return std::string(host).append(expanded ? std::string(":").append(std::to_string(port)).append(" (").append(canonName).append(")")
154 : "");
155 }

◆ useNext()

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

Reimplemented from core::socket::SocketAddress.

Definition at line 157 of file SocketAddress.cpp.

157 {
158 const bool useNext = socketAddrInfo->useNext();
159
160 sockAddr = socketAddrInfo->getSockAddr();
161
162 return useNext;
163 }

Member Data Documentation

◆ canonName

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

Definition at line 77 of file SocketAddress.h.

◆ host

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

Definition at line 74 of file SocketAddress.h.

◆ port

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

Definition at line 75 of file SocketAddress.h.

Referenced by getPort(), and setPort().

◆ socketAddrInfo

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

Definition at line 79 of file SocketAddress.h.


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