SNode.C
Loading...
Searching...
No Matches
SocketAddrInfo.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "net/in/SocketAddrInfo.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "log/Logger.h"
25
26#include <cstring>
27#include <netinet/in.h>
28
29#endif /* DOXYGEN_SHOULD_SKIP_THIS */
30
31namespace net::in {
32
33 SocketAddrInfo::~SocketAddrInfo() {
34 if (addrInfo != nullptr) {
35 freeaddrinfo(addrInfo);
36 }
37 }
38
39 int SocketAddrInfo::resolve(const std::string& node, const std::string& service, const addrinfo& hints) {
40 if (addrInfo != nullptr) {
41 freeaddrinfo(addrInfo);
42 addrInfo = nullptr;
43 }
44
45 int aiErrCode = 0;
46
47 if ((aiErrCode = core::system::getaddrinfo(node.c_str(), service.c_str(), &hints, &addrInfo)) == 0) {
48 currentAddrInfo = addrInfo;
49 }
50
51 return aiErrCode;
52 }
53
54 bool SocketAddrInfo::useNext() {
55 if (currentAddrInfo != nullptr) {
56 currentAddrInfo = currentAddrInfo->ai_next;
57 }
58
59 return currentAddrInfo != nullptr;
60 }
61
62 sockaddr_in SocketAddrInfo::getSockAddr() {
63 return currentAddrInfo != nullptr ? *reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr) // cppcheck-suppress internalAstError
64 : sockaddr_in{.sin_family = AF_INET, .sin_port{}, .sin_addr{}, .sin_zero{}};
65 }
66
67 std::string SocketAddrInfo::getCanonName() {
68 return currentAddrInfo->ai_canonname != nullptr ? currentAddrInfo->ai_canonname : std::string{};
69 }
70
71 void SocketAddrInfo::logAddressInfo() {
72 if (currentAddrInfo != nullptr) {
73 static char hostBfr[NI_MAXHOST];
74 static char servBfr[NI_MAXSERV];
75 std::memset(hostBfr, 0, NI_MAXHOST);
76 std::memset(servBfr, 0, NI_MAXSERV);
77
78 getnameinfo(currentAddrInfo->ai_addr,
79 currentAddrInfo->ai_addrlen,
80 hostBfr,
81 sizeof(hostBfr),
82 servBfr,
83 sizeof(servBfr),
84 NI_NUMERICHOST | NI_NUMERICSERV);
85
86 struct sockaddr_in* aiAddr = reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr);
87
88 const std::string format = "AddressInfo:\n"
89 " ai_next = %v\n"
90 " ai_flags = %v\n"
91 " ai_family = %v (PF_INET = %v, PF_INET6 = %v)\n"
92 " ai_socktype = %v (SOCK_STREAM = %v, SOCK_DGRAM = %v)\n"
93 " ai_protocol = %v (IPPROTO_TCP = %v, IPPROTO_UDP = %v)\n"
94 " ai_addrlen = %v (sockaddr_in = %v, "
95 "sockaddr_in6 = %v)\n"
96 " ai_addr = sin_family: %v (AF_INET = %v, "
97 "AF_INET6 = %v)\n"
98 " sin_addr: %v\n"
99 " sin_port: %v";
100
101 el::Loggers::getLogger("default")->trace(format.c_str(),
102 currentAddrInfo->ai_next,
103 currentAddrInfo->ai_flags,
104 currentAddrInfo->ai_family,
105 PF_INET,
106 PF_INET6,
107 currentAddrInfo->ai_socktype,
108 SOCK_STREAM,
109 SOCK_DGRAM,
110 currentAddrInfo->ai_protocol,
111 IPPROTO_TCP,
112 IPPROTO_UDP,
113 currentAddrInfo->ai_addrlen,
114 sizeof(struct sockaddr_in),
115 sizeof(struct sockaddr_in6),
116 aiAddr->sin_family,
117 AF_INET,
118 AF_INET6,
119 hostBfr,
120 servBfr);
121 }
122 }
123
124} // namespace net::in