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/in6/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::in6 {
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_in6 SocketAddrInfo::getSockAddr() {
63 return currentAddrInfo != nullptr
64 ? *reinterpret_cast<sockaddr_in6*>(currentAddrInfo->ai_addr) // cppcheck-suppress internalAstError
65 : sockaddr_in6{.sin6_family = AF_INET6, .sin6_port{}, .sin6_flowinfo{}, .sin6_addr{}, .sin6_scope_id{}};
66 }
67
68 std::string SocketAddrInfo::getCanonName() {
69 return currentAddrInfo->ai_canonname != nullptr ? currentAddrInfo->ai_canonname : std::string{};
70 }
71
72 void SocketAddrInfo::logAddressInfo() {
73 if (currentAddrInfo != nullptr) {
74 static char hostBfr[NI_MAXHOST];
75 static char servBfr[NI_MAXSERV];
76 std::memset(hostBfr, 0, NI_MAXHOST);
77 std::memset(servBfr, 0, NI_MAXSERV);
78
79 getnameinfo(currentAddrInfo->ai_addr,
80 currentAddrInfo->ai_addrlen,
81 hostBfr,
82 sizeof(hostBfr),
83 servBfr,
84 sizeof(servBfr),
85 NI_NUMERICHOST | NI_NUMERICSERV);
86
87 struct sockaddr_in* aiAddr = reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr);
88
89 const std::string format = "AddressInfo:\n"
90 " ai_next = %v\n"
91 " ai_flags = %v\n"
92 " ai_family = %v (PF_INET = %v, PF_INET6 = %v)\n"
93 " ai_socktype = %v (SOCK_STREAM = %v, SOCK_DGRAM = %v)\n"
94 " ai_protocol = %v (IPPROTO_TCP = %v, IPPROTO_UDP = %v)\n"
95 " ai_addrlen = %v (sockaddr_in = %v, "
96 "sockaddr_in6 = %v)\n"
97 " ai_addr = sin_family: %v (AF_INET = %v, "
98 "AF_INET6 = %v)\n"
99 " sin_addr: %v\n"
100 " sin_port: %v";
101
102 el::Loggers::getLogger("default")->trace(format.c_str(),
103 currentAddrInfo->ai_next,
104 currentAddrInfo->ai_flags,
105 currentAddrInfo->ai_family,
106 PF_INET,
107 PF_INET6,
108 currentAddrInfo->ai_socktype,
109 SOCK_STREAM,
110 SOCK_DGRAM,
111 currentAddrInfo->ai_protocol,
112 IPPROTO_TCP,
113 IPPROTO_UDP,
114 currentAddrInfo->ai_addrlen,
115 sizeof(struct sockaddr_in),
116 sizeof(struct sockaddr_in6),
117 aiAddr->sin_family,
118 AF_INET,
119 AF_INET6,
120 hostBfr,
121 servBfr);
122 }
123 }
124
125} // namespace net::in6