My Project 3.5.5
C++ Distributed Hash Table
Loading...
Searching...
No Matches
sockaddr.h
1/*
2 * Copyright (C) 2014-2025 Savoir-faire Linux Inc.
3 * Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "def.h"
22
23#include <fmt/core.h>
24#include <fmt/format.h>
25#include <fmt/ostream.h>
26
27#ifndef _WIN32
28#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31#ifdef __ANDROID__
32typedef uint16_t in_port_t;
33#endif
34#else
35#include <iso646.h>
36#include <stdint.h>
37#include <winsock2.h>
38#include <ws2def.h>
39#include <ws2tcpip.h>
40typedef uint16_t sa_family_t;
41typedef uint16_t in_port_t;
42#endif
43
44#include <string>
45#include <memory>
46#include <vector>
47#include <stdexcept>
48#include <stdlib.h>
49
50#include <cstring>
51#include <cstddef>
52
53namespace dht {
54
55OPENDHT_PUBLIC void print_addr(std::ostream& os, const sockaddr* sa, socklen_t slen);
56OPENDHT_PUBLIC std::string print_addr(const sockaddr* sa, socklen_t slen);
57OPENDHT_PUBLIC std::string print_addr(const sockaddr_storage& ss, socklen_t sslen);
58
62class OPENDHT_PUBLIC SockAddr {
63public:
64 SockAddr() {}
65 SockAddr(const SockAddr& o) {
66 set(o.get(), o.getLength());
67 }
68 SockAddr(SockAddr&& o) noexcept : addr(std::move(o.addr)), len(o.len) {
69 o.len = 0;
70 }
71
75 SockAddr(const sockaddr* sa, socklen_t length) {
76 if (length > static_cast<socklen_t>(sizeof(sockaddr_storage)))
77 throw std::runtime_error("Socket address length is too large");
78 set(sa, length);
79 }
80 SockAddr(const sockaddr* sa) {
81 socklen_t len = 0;
82 if (sa) {
83 if (sa->sa_family == AF_INET)
84 len = sizeof(sockaddr_in);
85 else if(sa->sa_family == AF_INET6)
86 len = sizeof(sockaddr_in6);
87 else
88 throw std::runtime_error("Unknown address family");
89 }
90 set(sa, len);
91 }
92
96 SockAddr(const sockaddr_storage& ss, socklen_t len) : SockAddr((const sockaddr*)&ss, len) {}
97
98 static std::vector<SockAddr> resolve(const std::string& host, const std::string& service = {});
99
100 bool operator<(const SockAddr& o) const {
101 if (len != o.len)
102 return len < o.len;
103 return std::memcmp((const uint8_t*)get(), (const uint8_t*)o.get(), len) < 0;
104 }
105
106 bool equals(const SockAddr& o) const {
107 return len == o.len
108 && std::memcmp((const uint8_t*)get(), (const uint8_t*)o.get(), len) == 0;
109 }
110 SockAddr& operator=(const SockAddr& o) {
111 set(o.get(), o.getLength());
112 return *this;
113 }
114 SockAddr& operator=(SockAddr&& o) {
115 len = o.len;
116 o.len = 0;
117 addr = std::move(o.addr);
118 return *this;
119 }
120
121 std::string toString() const {
122 return print_addr(get(), getLength());
123 }
124
128 sa_family_t getFamily() const { return len ? addr->sa_family : AF_UNSPEC; }
129
135 void setFamily(sa_family_t af) {
136 socklen_t new_length;
137 switch(af) {
138 case AF_INET:
139 new_length = sizeof(sockaddr_in);
140 break;
141 case AF_INET6:
142 new_length = sizeof(sockaddr_in6);
143 break;
144 default:
145 new_length = 0;
146 }
147 if (new_length != len) {
148 len = new_length;
149 if (len) addr.reset((sockaddr*)::calloc(len, 1));
150 else addr.reset();
151 }
152 if (len)
153 addr->sa_family = af;
154 }
155
159 void setAny() {
160 auto family = getFamily();
161 switch(family) {
162 case AF_INET:
163 getIPv4().sin_addr.s_addr = htonl(INADDR_ANY);
164 break;
165 case AF_INET6:
166 getIPv6().sin6_addr = in6addr_any;
167 break;
168 }
169 }
170
174 void setLoopback() {
175 auto family = getFamily();
176 switch(family) {
177 case AF_INET:
178 getIPv4().sin_addr.s_addr = htonl(INADDR_LOOPBACK);
179 break;
180 case AF_INET6:
181 getIPv6().sin6_addr = in6addr_loopback;
182 break;
183 }
184 }
185
190 in_port_t getPort() const {
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 switch(getFamily()) {
206 case AF_INET:
207 getIPv4().sin_port = htons(p);
208 break;
209 case AF_INET6:
210 getIPv6().sin6_port = htons(p);
211 break;
212 }
213 }
214
219 void setAddress(const char* address);
220
225 socklen_t getLength() const { return len; }
226
230 explicit operator bool() const noexcept {
231 return len;
232 }
233
238 const sockaddr* get() const { return addr.get(); }
239
244 sockaddr* get() { return addr.get(); }
245
246 inline const sockaddr_in& getIPv4() const {
247 return *reinterpret_cast<const sockaddr_in*>(get());
248 }
249 inline const sockaddr_in6& getIPv6() const {
250 return *reinterpret_cast<const sockaddr_in6*>(get());
251 }
252 inline sockaddr_in& getIPv4() {
253 return *reinterpret_cast<sockaddr_in*>(get());
254 }
255 inline sockaddr_in6& getIPv6() {
256 return *reinterpret_cast<sockaddr_in6*>(get());
257 }
258
263 inline sockaddr* release() {
264 len = 0;
265 return addr.release();
266 }
267
271 bool isLoopback() const;
272
276 bool isPrivate() const;
277
278 bool isUnspecified() const;
279
280 bool isMappedIPv4() const;
281 SockAddr getMappedIPv4();
282 SockAddr getMappedIPv6();
283
288 struct ipCmp {
289 bool operator()(const SockAddr& a, const SockAddr& b) const {
290 if (a.len != b.len)
291 return a.len < b.len;
292 socklen_t start, len;
293 switch(a.getFamily()) {
294 case AF_INET:
295 start = offsetof(sockaddr_in, sin_addr);
296 len = sizeof(in_addr);
297 break;
298 case AF_INET6:
299 start = offsetof(sockaddr_in6, sin6_addr);
300 // don't consider more than 64 bits (IPv6)
301 len = 8;
302 break;
303 default:
304 start = 0;
305 len = a.len;
306 break;
307 }
308 return std::memcmp((uint8_t*)a.get()+start,
309 (uint8_t*)b.get()+start, len) < 0;
310 }
311 };
312 friend std::ostream& operator<< (std::ostream& s, const SockAddr& h) {
313 print_addr(s, h.get(), h.getLength());
314 return s;
315 }
316
317private:
318 struct free_delete { void operator()(void* p) { ::free(p); } };
319 std::unique_ptr<sockaddr, free_delete> addr {};
320 socklen_t len {0};
321
322 void set(const sockaddr* sa, socklen_t length) {
323 if (len != length) {
324 len = length;
325 if (len) addr.reset((sockaddr*)::malloc(len));
326 else addr.reset();
327 }
328 if (len)
329 std::memcpy((uint8_t*)get(), (const uint8_t*)sa, len);
330 }
331
332};
333
334OPENDHT_PUBLIC bool operator==(const SockAddr& a, const SockAddr& b);
335
336}
337
338#if FMT_VERSION >= 90000
339template <> struct fmt::formatter<dht::SockAddr> : ostream_formatter {};
340#endif
SockAddr(const sockaddr *sa, socklen_t length)
Definition sockaddr.h:75
SockAddr(const sockaddr_storage &ss, socklen_t len)
Definition sockaddr.h:96
sockaddr * release()
Definition sockaddr.h:263
void setFamily(sa_family_t af)
Definition sockaddr.h:135
void setLoopback()
Definition sockaddr.h:174
void setPort(in_port_t p)
Definition sockaddr.h:204
const sockaddr * get() const
Definition sockaddr.h:238
sockaddr * get()
Definition sockaddr.h:244
socklen_t getLength() const
Definition sockaddr.h:225
void setAddress(const char *address)
void setAny()
Definition sockaddr.h:159
sa_family_t getFamily() const
Definition sockaddr.h:128
bool isPrivate() const
bool isLoopback() const
in_port_t getPort() const
Definition sockaddr.h:190