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 55 of file SocketAddrInfo.cpp.

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

Member Function Documentation

◆ getCanonName()

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

Definition at line 97 of file SocketAddrInfo.cpp.

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

◆ getSockAddr()

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

Definition at line 92 of file SocketAddrInfo.cpp.

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

◆ logAddressInfo()

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

Definition at line 101 of file SocketAddrInfo.cpp.

101 {
102 if (currentAddrInfo != nullptr) {
103 static char hostBfr[NI_MAXHOST];
104 static char servBfr[NI_MAXSERV];
105 std::memset(hostBfr, 0, NI_MAXHOST);
106 std::memset(servBfr, 0, NI_MAXSERV);
107
109 currentAddrInfo->ai_addrlen,
110 hostBfr,
111 sizeof(hostBfr),
112 servBfr,
113 sizeof(servBfr),
114 NI_NUMERICHOST | NI_NUMERICSERV);
115
116 struct sockaddr_in* aiAddr = reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr);
117
118 const std::string format = "AddressInfo:\n"
119 " ai_next = %v\n"
120 " ai_flags = %v\n"
121 " ai_family = %v (PF_INET = %v, PF_INET6 = %v)\n"
122 " ai_socktype = %v (SOCK_STREAM = %v, SOCK_DGRAM = %v)\n"
123 " ai_protocol = %v (IPPROTO_TCP = %v, IPPROTO_UDP = %v)\n"
124 " ai_addrlen = %v (sockaddr_in = %v, "
125 "sockaddr_in6 = %v)\n"
126 " ai_addr = sin_family: %v (AF_INET = %v, "
127 "AF_INET6 = %v)\n"
128 " sin_addr: %v\n"
129 " sin_port: %v";
130
131 el::Loggers::getLogger("default")->trace(format.c_str(),
132 currentAddrInfo->ai_next,
133 currentAddrInfo->ai_flags,
134 currentAddrInfo->ai_family,
135 PF_INET,
136 PF_INET6,
137 currentAddrInfo->ai_socktype,
138 SOCK_STREAM,
139 SOCK_DGRAM,
140 currentAddrInfo->ai_protocol,
141 IPPROTO_TCP,
142 IPPROTO_UDP,
143 currentAddrInfo->ai_addrlen,
144 sizeof(struct sockaddr_in),
145 sizeof(struct sockaddr_in6),
146 aiAddr->sin_family,
147 AF_INET,
148 AF_INET6,
149 hostBfr,
150 servBfr);
151 }
152 }
int getnameinfo(const sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition netdb.cpp:72

◆ resolve()

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

Definition at line 61 of file SocketAddrInfo.cpp.

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

◆ useNext()

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

Definition at line 76 of file SocketAddrInfo.cpp.

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

Member Data Documentation

◆ addrInfo

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

Definition at line 72 of file SocketAddrInfo.h.

◆ currentAddrInfo

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

Definition at line 73 of file SocketAddrInfo.h.


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