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/in6/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::in6 {
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 if (currentAddrInfo != nullptr) {
79 }
80
81 return currentAddrInfo != nullptr;
82 }
83
84 sockaddr_in6 SocketAddrInfo::getSockAddr() {
85 return currentAddrInfo != nullptr
86 ? *reinterpret_cast<sockaddr_in6*>(currentAddrInfo->ai_addr) // cppcheck-suppress internalAstError
87 : sockaddr_in6{.sin6_family = AF_INET6, .sin6_port{}, .sin6_flowinfo{}, .sin6_addr{}, .sin6_scope_id{}};
88 }
89
91 return currentAddrInfo->ai_canonname != nullptr ? currentAddrInfo->ai_canonname : std::string{};
92 }
93
95 if (currentAddrInfo != nullptr) {
96 static char hostBfr[NI_MAXHOST];
97 static char servBfr[NI_MAXSERV];
98 std::memset(hostBfr, 0, NI_MAXHOST);
99 std::memset(servBfr, 0, NI_MAXSERV);
100
101 getnameinfo(currentAddrInfo->ai_addr,
102 currentAddrInfo->ai_addrlen,
103 hostBfr,
104 sizeof(hostBfr),
105 servBfr,
106 sizeof(servBfr),
107 NI_NUMERICHOST | NI_NUMERICSERV);
108
109 struct sockaddr_in* aiAddr = reinterpret_cast<sockaddr_in*>(currentAddrInfo->ai_addr);
110
111 const std::string format = "AddressInfo:\n"
112 " ai_next = %v\n"
113 " ai_flags = %v\n"
114 " ai_family = %v (PF_INET = %v, PF_INET6 = %v)\n"
115 " ai_socktype = %v (SOCK_STREAM = %v, SOCK_DGRAM = %v)\n"
116 " ai_protocol = %v (IPPROTO_TCP = %v, IPPROTO_UDP = %v)\n"
117 " ai_addrlen = %v (sockaddr_in = %v, "
118 "sockaddr_in6 = %v)\n"
119 " ai_addr = sin_family: %v (AF_INET = %v, "
120 "AF_INET6 = %v)\n"
121 " sin_addr: %v\n"
122 " sin_port: %v";
123
124 el::Loggers::getLogger("default")->trace(format.c_str(),
125 currentAddrInfo->ai_next,
126 currentAddrInfo->ai_flags,
127 currentAddrInfo->ai_family,
128 PF_INET,
129 PF_INET6,
130 currentAddrInfo->ai_socktype,
131 SOCK_STREAM,
132 SOCK_DGRAM,
133 currentAddrInfo->ai_protocol,
134 IPPROTO_TCP,
135 IPPROTO_UDP,
136 currentAddrInfo->ai_addrlen,
137 sizeof(struct sockaddr_in),
138 sizeof(struct sockaddr_in6),
139 aiAddr->sin_family,
140 AF_INET,
141 AF_INET6,
142 hostBfr,
143 servBfr);
144 }
145 }
146
147} // namespace net::in6
struct addrinfo * currentAddrInfo
struct addrinfo * addrInfo
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
Definition netdb.cpp:62