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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "net/in/SocketAddrInfo.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "log/Logger.h"
47
48#include <cstring>
49#include <netinet/in.h>
50
51#endif /* DOXYGEN_SHOULD_SKIP_THIS */
52
53namespace net::in {
54
56 if (addrInfo != nullptr) {
57 freeaddrinfo(addrInfo);
58 }
59 }
60
61 int SocketAddrInfo::resolve(const std::string& node, const std::string& service, const addrinfo& hints) {
62 if (addrInfo != nullptr) {
63 freeaddrinfo(addrInfo);
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 }
75
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 }
91
92 sockaddr_in SocketAddrInfo::getSockAddr() {
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 }
96
98 return currentAddrInfo->ai_canonname != nullptr ? currentAddrInfo->ai_canonname : std::string{};
99 }
100
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
108 getnameinfo(currentAddrInfo->ai_addr,
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 }
153
154} // namespace net::in
struct addrinfo * addrInfo
struct addrinfo * currentAddrInfo
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
Definition netdb.cpp:62