SNode.C
Loading...
Searching...
No Matches
net::in::SocketAddrInfo Class Reference

#include <SocketAddrInfo.h>

Collaboration diagram for net::in::SocketAddrInfo:

Public Member Functions

 SocketAddrInfo ()=default
 ~SocketAddrInfo ()
int resolve (const std::string &node, const std::string &service, const addrinfo &hints)
bool useNext ()
sockaddr_in getSockAddr ()
std::string getCanonName ()
void logAddressInfo ()

Private Attributes

struct addrinfo * addrInfo = nullptr
struct addrinfo * currentAddrInfo = nullptr

Detailed Description

Definition at line 57 of file SocketAddrInfo.h.

Constructor & Destructor Documentation

◆ SocketAddrInfo()

net::in::SocketAddrInfo::SocketAddrInfo ( )
default

◆ ~SocketAddrInfo()

net::in::SocketAddrInfo::~SocketAddrInfo ( )

Definition at line 56 of file SocketAddrInfo.cpp.

56 {
57 if (addrInfo != nullptr) {
59 }
60 }
struct addrinfo * addrInfo
void freeaddrinfo(struct addrinfo *res)
Definition netdb.cpp:67

References addrInfo.

Member Function Documentation

◆ getCanonName()

std::string net::in::SocketAddrInfo::getCanonName ( )

Definition at line 98 of file SocketAddrInfo.cpp.

98 {
99 return currentAddrInfo->ai_canonname != nullptr ? currentAddrInfo->ai_canonname : std::string{};
100 }
struct addrinfo * currentAddrInfo

References currentAddrInfo.

Referenced by net::in::SocketAddress::init().

Here is the caller graph for this function:

◆ getSockAddr()

sockaddr_in net::in::SocketAddrInfo::getSockAddr ( )

Definition at line 93 of file SocketAddrInfo.cpp.

93 {
94 return currentAddrInfo != nullptr ? *reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr) // cppcheck-suppress internalAstError
95 : sockaddr_in{.sin_family = AF_INET, .sin_port{}, .sin_addr{}, .sin_zero{}};
96 }

References currentAddrInfo.

Referenced by net::in::SocketAddress::init(), and net::in::SocketAddress::useNext().

Here is the caller graph for this function:

◆ logAddressInfo()

void net::in::SocketAddrInfo::logAddressInfo ( )

Definition at line 102 of file SocketAddrInfo.cpp.

102 {
103 if (currentAddrInfo != nullptr) {
104 static char hostBfr[NI_MAXHOST];
105 static char servBfr[NI_MAXSERV];
106 std::memset(hostBfr, 0, NI_MAXHOST);
107 std::memset(servBfr, 0, NI_MAXSERV);
108
110 currentAddrInfo->ai_addrlen,
111 hostBfr,
112 sizeof(hostBfr),
113 servBfr,
114 sizeof(servBfr),
115 NI_NUMERICHOST | NI_NUMERICSERV);
116
117 const struct sockaddr_in* aiAddr = reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr);
118
119 std::ostringstream formatted;
120 formatted << "AddressInfo:\n"
121 << " ai_next = " << currentAddrInfo->ai_next << "\n"
122 << " ai_flags = " << currentAddrInfo->ai_flags << "\n"
123 << " ai_family = " << currentAddrInfo->ai_family << " (PF_INET = " << PF_INET << ", PF_INET6 = " << PF_INET6
124 << ")\n"
125 << " ai_socktype = " << currentAddrInfo->ai_socktype << " (SOCK_STREAM = " << SOCK_STREAM
126 << ", SOCK_DGRAM = " << SOCK_DGRAM << ")\n"
127 << " ai_protocol = " << currentAddrInfo->ai_protocol << " (IPPROTO_TCP = " << IPPROTO_TCP
128 << ", IPPROTO_UDP = " << IPPROTO_UDP << ")\n"
129 << " ai_addrlen = " << currentAddrInfo->ai_addrlen << " (sockaddr_in = " << sizeof(struct sockaddr_in)
130 << ", sockaddr_in6 = " << sizeof(struct sockaddr_in6) << ")\n"
131 << " ai_addr = sin_family: " << aiAddr->sin_family << " (AF_INET = " << AF_INET
132 << ", AF_INET6 = " << AF_INET6 << ")\n"
133 << " sin_addr: " << hostBfr << "\n"
134 << " sin_port: " << servBfr;
135
136 LOG(TRACE) << formatted.str();
137 }
138 }
#define LOG(level)
Definition Logger.h:148
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 currentAddrInfo, logger::LogMessage::LogMessage(), and logger::TRACE.

Here is the call graph for this function:

◆ resolve()

int net::in::SocketAddrInfo::resolve ( const std::string & node,
const std::string & service,
const addrinfo & hints )

Definition at line 62 of file SocketAddrInfo.cpp.

62 {
63 if (addrInfo != nullptr) {
65 addrInfo = nullptr;
66 }
67
68 int aiErrCode = 0;
69
70 if ((aiErrCode = core::system::getaddrinfo(node.c_str(), service.c_str(), &hints, &addrInfo)) == 0) {
72 }
73
74 return aiErrCode;
75 }
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
Definition netdb.cpp:62

References addrInfo, currentAddrInfo, and core::system::getaddrinfo().

Here is the call graph for this function:

◆ useNext()

bool net::in::SocketAddrInfo::useNext ( )

Definition at line 77 of file SocketAddrInfo.cpp.

77 {
78 // Lookup for a AddrInfo holding a ai_next AddrInfo which differ from the current one.
79 // Especially localhost on IPv4 can lead to more than one entries representing the same SockAddr.
80 // Thus, do not use this further SockAddr.
81 while (currentAddrInfo != nullptr && currentAddrInfo->ai_next != nullptr &&
82 std::memcmp(currentAddrInfo->ai_addr, currentAddrInfo->ai_next->ai_addr, sizeof(sockaddr_in)) == 0) {
84 }
85
86 if (currentAddrInfo != nullptr) {
88 }
89
90 return currentAddrInfo != nullptr;
91 }

References currentAddrInfo.

Referenced by net::in::SocketAddress::useNext().

Here is the caller graph for this function:

Member Data Documentation

◆ addrInfo

struct addrinfo* net::in::SocketAddrInfo::addrInfo = nullptr
private

Definition at line 72 of file SocketAddrInfo.h.

Referenced by resolve(), and ~SocketAddrInfo().

◆ currentAddrInfo

struct addrinfo* net::in::SocketAddrInfo::currentAddrInfo = nullptr
private

Definition at line 73 of file SocketAddrInfo.h.

Referenced by getCanonName(), getSockAddr(), logAddressInfo(), resolve(), and useNext().


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