My Project: /home/noptrix/ba/blackarch/packages/opendht-c/src/opendht/include/opendht/sockaddr.h Source File

My Project 3.7.7 C++ Distributed Hash Table

Loading...
Searching...
No Matches
sockaddr.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "def.h"
6
7#include <fmt/core.h>
8#include <fmt/format.h>
9#include <fmt/ostream.h>
10
11#ifndef _WIN32
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <arpa/inet.h>
15#ifdef __ANDROID__
16typedef uint16_t in_port_t;
17#endif
18#else
19#include <iso646.h>
20#include <stdint.h>
21#include <winsock2.h>
22#include <ws2def.h>
23#include <ws2tcpip.h>
24typedef uint16_t sa_family_t;
25typedef uint16_t in_port_t;
26#endif
27
28#include <string>
29#include <memory>
30#include <vector>
31#include <stdexcept>
32#include <stdlib.h>
33
34#include <cstring>
35#include <cstddef>
36
37namespace dht {
38
39OPENDHT_PUBLIC void print_addr(std::ostream& os, const sockaddr* sa, socklen_t slen);
40OPENDHT_PUBLIC std::string print_addr(const sockaddr* sa, socklen_t slen);
41OPENDHT_PUBLIC std::string print_addr(const sockaddr_storage& ss, socklen_t sslen);
42
46 class OPENDHT_PUBLIC SockAddr
47{
48public:
49 SockAddr() {}
50 SockAddr(const SockAddr& o) { set(o.get(), o.getLength()); }
51 SockAddr(SockAddr&& o) noexcept
52 : addr(std::move(o.addr))
53 , len(o.len)
54 {
55 o.len = 0;
56 }
57
61 SockAddr(const sockaddr* sa, socklen_t length)
62 {
63 if (length > static_cast<socklen_t>(sizeof(sockaddr_storage)))
64 throw std::runtime_error("Socket address length is too large");
65 set(sa, length);
66 }
67 SockAddr(const sockaddr* sa)
68 {
69 socklen_t len = 0;
70 if (sa) {
71 if (sa->sa_family == AF_INET)
72 len = sizeof(sockaddr_in);
73 else if (sa->sa_family == AF_INET6)
74 len = sizeof(sockaddr_in6);
75 else
76 throw std::runtime_error("Unknown address family");
77 }
78 set(sa, len);
79 }
80
84 SockAddr(const sockaddr_storage& ss, socklen_t len)
85 : SockAddr((const sockaddr*) &ss, len)
86 {}
87
88 static std::vector<SockAddr> resolve(const std::string& host, const std::string& service = {});
89
90 bool operator<(const SockAddr& o) const
91 {
92 if (len != o.len)
93 return len < o.len;
94 return std::memcmp((const uint8_t*) get(), (const uint8_t*) o.get(), len) < 0;
95 }
96
97 bool equals(const SockAddr& o) const
98 {
99 return len == o.len && std::memcmp((const uint8_t*) get(), (const uint8_t*) o.get(), len) == 0;
100 }
101 bool operator==(const SockAddr& o) const { return equals(o); }
102 bool operator!=(const SockAddr& o) const { return !equals(o); }
103
104 SockAddr& operator=(const SockAddr& o)
105 {
106 set(o.get(), o.getLength());
107 return *this;
108 }
109 SockAddr& operator=(SockAddr&& o)
110 {
111 len = o.len;
112 o.len = 0;
113 addr = std::move(o.addr);
114 return *this;
115 }
116
117 std::string toString() const { return print_addr(get(), getLength()); }
118
122 sa_family_t getFamily() const { return len ? addr->sa_family : AF_UNSPEC; }
123
129 void setFamily(sa_family_t af)
130 {
131 socklen_t new_length;
132 switch (af) {
133 case AF_INET:
134 new_length = sizeof(sockaddr_in);
135 break;
136 case AF_INET6:
137 new_length = sizeof(sockaddr_in6);
138 break;
139 default:
140 new_length = 0;
141 }
142 if (new_length != len) {
143 len = new_length;
144 if (len)
145 addr.reset((sockaddr*) ::calloc(len, 1));
146 else
147 addr.reset();
148 }
149 if (len)
150 addr->sa_family = af;
151 }
152
156 void setAny()
157 {
158 auto family = getFamily();
159 switch (family) {
160 case AF_INET:
161 getIPv4().sin_addr.s_addr = htonl(INADDR_ANY);
162 break;
163 case AF_INET6:
164 getIPv6().sin6_addr = in6addr_any;
165 break;
166 }
167 }
168
172 void setLoopback()
173 {
174 auto family = getFamily();
175 switch (family) {
176 case AF_INET:
177 getIPv4().sin_addr.s_addr = htonl(INADDR_LOOPBACK);
178 break;
179 case AF_INET6:
180 getIPv6().sin6_addr = in6addr_loopback;
181 break;
182 }
183 }
184
189 in_port_t getPort() const
190 {
191 switch (getFamily()) {
192 case AF_INET:
193 return ntohs(getIPv4().sin_port);
194 case AF_INET6:
195 return ntohs(getIPv6().sin6_port);
196 default:
197 return 0;
198 }
199 }
200
204 void setPort(in_port_t p)
205 {
206 switch (getFamily()) {
207 case AF_INET:
208 getIPv4().sin_port = htons(p);
209 break;
210 case AF_INET6:
211 getIPv6().sin6_port = htons(p);
212 break;
213 }
214 }
215
220 void setAddress(const char* address);
221
226 socklen_t getLength() const { return len; }
227
231 explicit operator bool() const noexcept { return len; }
232
237 const sockaddr* get() const { return addr.get(); }
238
243 sockaddr* get() { return addr.get(); }
244
245 inline const sockaddr_in& getIPv4() const { return *reinterpret_cast<const sockaddr_in*>(get()); }
246 inline const sockaddr_in6& getIPv6() const { return *reinterpret_cast<const sockaddr_in6*>(get()); }
247 inline sockaddr_in& getIPv4() { return *reinterpret_cast<sockaddr_in*>(get()); }
248 inline sockaddr_in6& getIPv6() { return *reinterpret_cast<sockaddr_in6*>(get()); }
249
254 inline sockaddr* release()
255 {
256 len = 0;
257 return addr.release();
258 }
259
263 bool isLoopback() const;
264
268 bool isPrivate() const;
269
270 bool isUnspecified() const;
271
272 bool isMappedIPv4() const;
273 SockAddr getMappedIPv4();
274 SockAddr getMappedIPv6();
275
280 struct ipCmp
281 {
282 bool operator()(const SockAddr& a, const SockAddr& b) const
283 {
284 if (a.len != b.len)
285 return a.len < b.len;
286 socklen_t start, len;
287 switch (a.getFamily()) {
288 case AF_INET:
289 start = offsetof(sockaddr_in, sin_addr);
290 len = sizeof(in_addr);
291 break;
292 case AF_INET6:
293 start = offsetof(sockaddr_in6, sin6_addr);
294 // don't consider more than 64 bits (IPv6)
295 len = 8;
296 break;
297 default:
298 start = 0;
299 len = a.len;
300 break;
301 }
302 return std::memcmp((uint8_t*) a.get() + start, (uint8_t*) b.get() + start, len) < 0;
303 }
304 };
305 friend std::ostream& operator<<(std::ostream& s, const SockAddr& h)
306 {
307 print_addr(s, h.get(), h.getLength());
308 return s;
309 }
310
311private:
312 struct free_delete
313 {
314 void operator()(void* p) { ::free(p); }
315 };
316 std::unique_ptr<sockaddr, free_delete> addr {};
317 socklen_t len {0};
318
319 void set(const sockaddr* sa, socklen_t length)
320 {
321 if (len != length) {
322 len = length;
323 if (len)
324 addr.reset((sockaddr*) ::malloc(len));
325 else
326 addr.reset();
327 }
328 if (len)
329 std::memcpy((uint8_t*) get(), (const uint8_t*) sa, len);
330 }
331};
332
333} // namespace dht
334
335#if FMT_VERSION >= 90000
336template<>
337struct fmt::formatter<dht::SockAddr> : ostream_formatter
338{};
339#endif
dht::SockAddr
Definition sockaddr.h:47
dht::SockAddr::SockAddr
SockAddr(const sockaddr *sa, socklen_t length)
Definition sockaddr.h:61
dht::SockAddr::SockAddr
SockAddr(const sockaddr_storage &ss, socklen_t len)
Definition sockaddr.h:84
dht::SockAddr::release
sockaddr * release()
Definition sockaddr.h:254
dht::SockAddr::setFamily
void setFamily(sa_family_t af)
Definition sockaddr.h:129
dht::SockAddr::setLoopback
void setLoopback()
Definition sockaddr.h:172
dht::SockAddr::setPort
void setPort(in_port_t p)
Definition sockaddr.h:204
dht::SockAddr::get
const sockaddr * get() const
Definition sockaddr.h:237
dht::SockAddr::get
sockaddr * get()
Definition sockaddr.h:243
dht::SockAddr::getLength
socklen_t getLength() const
Definition sockaddr.h:226
dht::SockAddr::setAddress
void setAddress(const char *address)
dht::SockAddr::setAny
void setAny()
Definition sockaddr.h:156
dht::SockAddr::getFamily
sa_family_t getFamily() const
Definition sockaddr.h:122
dht::SockAddr::isPrivate
bool isPrivate() const
dht::SockAddr::isLoopback
bool isLoopback() const
dht::SockAddr::getPort
in_port_t getPort() const
Definition sockaddr.h:189
dht
Definition callbacks.h:17
dht::SockAddr::ipCmp
Definition sockaddr.h:281