My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
sockaddr.h
1/*
2 * Copyright (C) 2014-2023 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
175 in_port_t getPort() const {
176 switch(getFamily()) {
177 case AF_INET:
178 return ntohs(getIPv4().sin_port);
179 case AF_INET6:
180 return ntohs(getIPv6().sin6_port);
181 default:
182 return 0;
183 }
184 }
185
189 void setPort(in_port_t p) {
190 switch(getFamily()) {
191 case AF_INET:
192 getIPv4().sin_port = htons(p);
193 break;
194 case AF_INET6:
195 getIPv6().sin6_port = htons(p);
196 break;
197 }
198 }
199
204 void setAddress(const char* address);
205
210 socklen_t getLength() const { return len; }
211
215 explicit operator bool() const noexcept {
216 return len;
217 }
218
223 const sockaddr* get() const { return addr.get(); }
224
229 sockaddr* get() { return addr.get(); }
230
231 inline const sockaddr_in& getIPv4() const {
232 return *reinterpret_cast<const sockaddr_in*>(get());
233 }
234 inline const sockaddr_in6& getIPv6() const {
235 return *reinterpret_cast<const sockaddr_in6*>(get());
236 }
237 inline sockaddr_in& getIPv4() {
238 return *reinterpret_cast<sockaddr_in*>(get());
239 }
240 inline sockaddr_in6& getIPv6() {
241 return *reinterpret_cast<sockaddr_in6*>(get());
242 }
243
248 inline sockaddr* release() {
249 len = 0;
250 return addr.release();
251 }
252
256 bool isLoopback() const;
257
261 bool isPrivate() const;
262
263 bool isUnspecified() const;
264
265 bool isMappedIPv4() const;
266 SockAddr getMappedIPv4();
267 SockAddr getMappedIPv6();
268
273 struct ipCmp {
274 bool operator()(const SockAddr& a, const SockAddr& b) const {
275 if (a.len != b.len)
276 return a.len < b.len;
277 socklen_t start, len;
278 switch(a.getFamily()) {
279 case AF_INET:
280 start = offsetof(sockaddr_in, sin_addr);
281 len = sizeof(in_addr);
282 break;
283 case AF_INET6:
284 start = offsetof(sockaddr_in6, sin6_addr);
285 // don't consider more than 64 bits (IPv6)
286 len = 8;
287 break;
288 default:
289 start = 0;
290 len = a.len;
291 break;
292 }
293 return std::memcmp((uint8_t*)a.get()+start,
294 (uint8_t*)b.get()+start, len) < 0;
295 }
296 };
297 OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const SockAddr& h) {
298 print_addr(s, h.get(), h.getLength());
299 return s;
300 }
301
302private:
303 struct free_delete { void operator()(void* p) { ::free(p); } };
304 std::unique_ptr<sockaddr, free_delete> addr {};
305 socklen_t len {0};
306
307 void set(const sockaddr* sa, socklen_t length) {
308 if (len != length) {
309 len = length;
310 if (len) addr.reset((sockaddr*)::malloc(len));
311 else addr.reset();
312 }
313 if (len)
314 std::memcpy((uint8_t*)get(), (const uint8_t*)sa, len);
315 }
316
317};
318
319OPENDHT_PUBLIC bool operator==(const SockAddr& a, const SockAddr& b);
320
321}
322
323#if FMT_VERSION >= 90000
324template <> struct fmt::formatter<dht::SockAddr> : ostream_formatter {};
325#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:248
void setFamily(sa_family_t af)
Definition sockaddr.h:135
void setPort(in_port_t p)
Definition sockaddr.h:189
const sockaddr * get() const
Definition sockaddr.h:223
sockaddr * get()
Definition sockaddr.h:229
socklen_t getLength() const
Definition sockaddr.h:210
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:175