]>
Commit | Line | Data |
---|---|---|
e5989e71 JW |
1 | // <experimental/internet> -*- C++ -*- |
2 | ||
7adcbafe | 3 | // Copyright (C) 2015-2022 Free Software Foundation, Inc. |
e5989e71 JW |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library 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 General Public License for more details. | |
15 | ||
16 | // Under Section 7 of GPL version 3, you are granted additional | |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
24 | ||
25 | /** @file experimental/internet | |
26 | * This is a TS C++ Library header. | |
3084625d | 27 | * @ingroup networking-ts |
e5989e71 JW |
28 | */ |
29 | ||
30 | #ifndef _GLIBCXX_EXPERIMENTAL_INTERNET | |
31 | #define _GLIBCXX_EXPERIMENTAL_INTERNET | |
32 | ||
33 | #pragma GCC system_header | |
34 | ||
35 | #if __cplusplus >= 201402L | |
36 | ||
37 | #include <experimental/netfwd> | |
82a0f2fd JW |
38 | #include <experimental/io_context> |
39 | #include <experimental/bits/net.h> | |
e5989e71 JW |
40 | #include <array> |
41 | #include <forward_list> | |
42 | #include <sstream> | |
e5989e71 | 43 | #include <cstdint> |
e5989e71 | 44 | #include <experimental/string_view> |
82a0f2fd JW |
45 | #ifdef _GLIBCXX_HAVE_UNISTD_H |
46 | # include <unistd.h> | |
47 | #endif | |
48 | #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H | |
49 | # include <sys/socket.h> // AF_INET, AF_INET6, SOCK_DGRAM, SOCK_STREAM | |
50 | #endif | |
51 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H | |
52 | # include <arpa/inet.h> // inet_ntop | |
53 | #endif | |
b5445359 | 54 | #ifdef _GLIBCXX_HAVE_NETINET_IN_H |
2e0b1c6c | 55 | # include <netinet/in.h> // IPPROTO_IP, IPPROTO_IPV6, in_addr, in6_addr |
b5445359 | 56 | #endif |
82a0f2fd JW |
57 | #ifdef _GLIBCXX_HAVE_NETINET_TCP_H |
58 | # include <netinet/tcp.h> // TCP_NODELAY | |
59 | #endif | |
60 | #ifdef _GLIBCXX_HAVE_NETDB_H | |
61 | # include <netdb.h> // getaddrinfo etc. | |
62 | #endif | |
e5989e71 | 63 | |
cd67d138 JW |
64 | #if defined _WIN32 && __has_include(<ws2tcpip.h>) |
65 | # include <ws2tcpip.h> | |
66 | #endif | |
67 | ||
e5989e71 JW |
68 | namespace std _GLIBCXX_VISIBILITY(default) |
69 | { | |
a70a4be9 | 70 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
e5989e71 JW |
71 | namespace experimental |
72 | { | |
73 | namespace net | |
74 | { | |
75 | inline namespace v1 | |
76 | { | |
77 | namespace ip | |
78 | { | |
3084625d JW |
79 | /** @addtogroup networking-ts |
80 | * @{ | |
e5989e71 JW |
81 | */ |
82 | ||
83 | /** Error codes for resolver errors. | |
84 | * @{ | |
85 | */ | |
86 | ||
87 | enum class resolver_errc : int { | |
9ee35a86 | 88 | #ifdef _GLIBCXX_HAVE_NETDB_H |
e5989e71 JW |
89 | host_not_found = EAI_NONAME, |
90 | host_not_found_try_again = EAI_AGAIN, | |
91 | service_not_found = EAI_SERVICE | |
feec7ef6 JW |
92 | // N.B. POSIX defines additional errors that have no enumerator here: |
93 | // EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_SOCKTYPE, EAI_SYSTEM | |
94 | // Some C libraries define additional errors: | |
95 | // EAI_BADHINTS, EAI_OVERFLOW, EAI_PROTOCOL | |
96 | // Some C libraries define additional (obsolete?) errors: | |
97 | // EAI_ADDRFAMILY, EAI_NODATA | |
9ee35a86 | 98 | #endif |
e5989e71 JW |
99 | }; |
100 | ||
101 | /// Error category for resolver errors. | |
102 | inline const error_category& resolver_category() noexcept // TODO non-inline | |
103 | { | |
104 | struct __cat : error_category | |
105 | { | |
106 | const char* name() const noexcept { return "resolver"; } | |
9ee35a86 JW |
107 | std::string message(int __e) const { |
108 | #ifdef _GLIBCXX_HAVE_NETDB_H | |
109 | return ::gai_strerror(__e); | |
110 | #else | |
111 | return "name resolution requires <netdb.h>"; | |
112 | #endif | |
113 | } | |
e5989e71 JW |
114 | virtual void __message(int) { } // TODO dual ABI XXX |
115 | }; | |
116 | static __cat __c; | |
117 | return __c; | |
118 | } | |
119 | ||
3f4aa457 | 120 | inline error_code make_error_code(resolver_errc __e) noexcept |
e5989e71 JW |
121 | { return error_code(static_cast<int>(__e), resolver_category()); } |
122 | ||
3f4aa457 | 123 | inline error_condition make_error_condition(resolver_errc __e) noexcept |
e5989e71 JW |
124 | { return error_condition(static_cast<int>(__e), resolver_category()); } |
125 | ||
feec7ef6 JW |
126 | /// @cond undocumented |
127 | inline error_code | |
128 | __make_resolver_error_code(int __ai_err, | |
129 | [[__maybe_unused__]] int __sys_err) noexcept | |
130 | { | |
131 | #ifdef EAI_SYSTEM | |
132 | if (__builtin_expect(__ai_err == EAI_SYSTEM, 0)) | |
133 | return error_code(__sys_err, std::generic_category()); | |
134 | #endif | |
135 | return error_code(__ai_err, resolver_category()); | |
136 | } | |
137 | /// @endcond | |
138 | ||
e5989e71 JW |
139 | /// @} |
140 | ||
f26e72d8 JW |
141 | using port_type = uint_least16_t; ///< Type used for port numbers. |
142 | using scope_id_type = uint_least32_t; ///< Type used for IPv6 scope IDs. | |
e5989e71 JW |
143 | |
144 | /// Convenience alias for constraining allocators for strings. | |
145 | template<typename _Alloc> | |
146 | using __string_with | |
147 | = enable_if_t<std::is_same<typename _Alloc::value_type, char>::value, | |
148 | std::basic_string<char, std::char_traits<char>, _Alloc>>; | |
149 | ||
9ee35a86 JW |
150 | constexpr errc |
151 | __unsupported_err() noexcept | |
152 | { | |
153 | #if defined EAFNOSUPPORT | |
154 | return std::errc::address_family_not_supported; | |
155 | #else | |
156 | return std::errc::operation_not_supported; | |
157 | #endif | |
158 | } | |
159 | ||
e5989e71 JW |
160 | /** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses. |
161 | * @{ | |
162 | */ | |
163 | ||
164 | struct v4_mapped_t {}; | |
165 | constexpr v4_mapped_t v4_mapped; | |
166 | ||
f0b88346 | 167 | /// @} |
e5989e71 JW |
168 | |
169 | /// An IPv4 address. | |
170 | class address_v4 | |
171 | { | |
172 | public: | |
173 | // types: | |
f26e72d8 | 174 | using uint_type = uint_least32_t; |
e5989e71 JW |
175 | |
176 | struct bytes_type : array<unsigned char, 4> | |
177 | { | |
178 | template<typename... _Tp> | |
179 | explicit constexpr | |
180 | bytes_type(_Tp... __tp) | |
181 | : array<unsigned char, 4>{{static_cast<unsigned char>(__tp)...}} | |
182 | { | |
183 | #if UCHAR_MAX > 0xFF | |
184 | for (auto __b : *this) | |
185 | if (__b > 0xFF) | |
186 | __throw_out_of_range("invalid address_v4::bytes_type value"); | |
187 | #endif | |
188 | } | |
189 | }; | |
190 | ||
191 | // constructors: | |
192 | constexpr address_v4() noexcept : _M_addr(0) { } | |
193 | ||
194 | constexpr address_v4(const address_v4& a) noexcept = default; | |
195 | ||
196 | constexpr | |
197 | address_v4(const bytes_type& __b) | |
198 | : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3]) | |
199 | { } | |
200 | ||
201 | explicit constexpr | |
187fdaea | 202 | address_v4(uint_type __val) : _M_addr(_S_hton_32(__val)) |
e5989e71 JW |
203 | { |
204 | #if UINT_LEAST32_MAX > 0xFFFFFFFF | |
205 | if (__val > 0xFFFFFFFF) | |
206 | __throw_out_of_range("invalid address_v4::uint_type value"); | |
207 | #endif | |
208 | } | |
209 | ||
210 | // assignment: | |
211 | address_v4& operator=(const address_v4& a) noexcept = default; | |
212 | ||
213 | // members: | |
214 | constexpr bool is_unspecified() const noexcept { return to_uint() == 0; } | |
215 | ||
216 | constexpr bool | |
217 | is_loopback() const noexcept | |
218 | { return (to_uint() & 0xFF000000) == 0x7F000000; } | |
219 | ||
220 | constexpr bool | |
221 | is_multicast() const noexcept | |
222 | { return (to_uint() & 0xF0000000) == 0xE0000000; } | |
223 | ||
224 | constexpr bytes_type | |
225 | to_bytes() const noexcept | |
226 | { | |
227 | return bytes_type{ | |
228 | (_M_addr >> 24) & 0xFF, | |
229 | (_M_addr >> 16) & 0xFF, | |
230 | (_M_addr >> 8) & 0xFF, | |
231 | _M_addr & 0xFF | |
232 | }; | |
233 | } | |
234 | ||
187fdaea JW |
235 | constexpr uint_type |
236 | to_uint() const noexcept { return _S_ntoh_32(_M_addr); } | |
e5989e71 JW |
237 | |
238 | template<typename _Allocator = allocator<char>> | |
239 | __string_with<_Allocator> | |
240 | to_string(const _Allocator& __a = _Allocator()) const | |
241 | { | |
9ee35a86 | 242 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H |
e5989e71 | 243 | __string_with<_Allocator> __str(__a); |
9ee35a86 | 244 | __str.resize(INET_ADDRSTRLEN); |
e5989e71 JW |
245 | if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size())) |
246 | __str.erase(__str.find('\0')); | |
247 | else | |
248 | __str.resize(0); | |
249 | return __str; | |
9ee35a86 JW |
250 | #else |
251 | std::__throw_system_error((int)__unsupported_err()); | |
82a0f2fd | 252 | #endif |
9ee35a86 | 253 | } |
e5989e71 JW |
254 | |
255 | // static members: | |
256 | static constexpr address_v4 any() noexcept { return address_v4{}; } | |
257 | ||
258 | static constexpr | |
259 | address_v4 loopback() noexcept { return address_v4{0x7F000001}; } | |
260 | ||
261 | static constexpr | |
262 | address_v4 broadcast() noexcept { return address_v4{0xFFFFFFFF}; } | |
263 | ||
264 | private: | |
265 | template<typename _InternetProtocol> | |
266 | friend class basic_endpoint; | |
267 | ||
268 | friend address_v4 make_address_v4(const char*, error_code&) noexcept; | |
269 | ||
270 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
187fdaea JW |
271 | static constexpr uint16_t _S_hton_16(uint16_t __h) { return __h; } |
272 | static constexpr uint16_t _S_ntoh_16(uint16_t __n) { return __n; } | |
273 | static constexpr uint32_t _S_hton_32(uint32_t __h) { return __h; } | |
274 | static constexpr uint32_t _S_ntoh_32(uint32_t __n) { return __n; } | |
e5989e71 JW |
275 | #else |
276 | static constexpr uint16_t | |
187fdaea | 277 | _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); } |
e5989e71 JW |
278 | |
279 | static constexpr uint16_t | |
187fdaea | 280 | _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); } |
e5989e71 JW |
281 | |
282 | static constexpr uint32_t | |
187fdaea | 283 | _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); } |
e5989e71 JW |
284 | |
285 | static constexpr uint32_t | |
187fdaea | 286 | _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); } |
e5989e71 JW |
287 | #endif |
288 | ||
cd67d138 | 289 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H |
e5989e71 | 290 | in_addr_t _M_addr; // network byte order |
cd67d138 JW |
291 | #else |
292 | uint32_t _M_addr; | |
293 | #endif | |
e5989e71 JW |
294 | }; |
295 | ||
296 | /// An IPv6 address. | |
297 | class address_v6 | |
298 | { | |
299 | public: | |
300 | // types: | |
301 | struct bytes_type : array<unsigned char, 16> | |
302 | { | |
39073938 JW |
303 | template<typename... _Tp> |
304 | explicit constexpr | |
305 | bytes_type(_Tp... __t) | |
306 | : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}} | |
307 | { } | |
e5989e71 JW |
308 | }; |
309 | ||
310 | // constructors: | |
311 | constexpr address_v6() noexcept : _M_bytes(), _M_scope_id() { } | |
312 | ||
313 | constexpr address_v6(const address_v6& __a) noexcept = default; | |
314 | ||
315 | constexpr | |
316 | address_v6(const bytes_type& __bytes, scope_id_type __scope = 0) | |
317 | : _M_bytes(__bytes), _M_scope_id(__scope) | |
318 | { } | |
319 | ||
320 | // assignment: | |
321 | address_v6& operator=(const address_v6& __a) noexcept = default; | |
322 | ||
323 | // members: | |
324 | void scope_id(scope_id_type __id) noexcept { _M_scope_id = __id; } | |
325 | ||
326 | constexpr scope_id_type scope_id() const noexcept { return _M_scope_id; } | |
327 | ||
328 | constexpr bool | |
329 | is_unspecified() const noexcept | |
330 | { | |
331 | for (int __i = 0; __i < 16; ++__i) | |
332 | if (_M_bytes[__i] != 0x00) | |
333 | return false; | |
334 | return _M_scope_id == 0; | |
335 | } | |
336 | ||
337 | constexpr bool | |
338 | is_loopback() const noexcept | |
339 | { | |
340 | for (int __i = 0; __i < 15; ++__i) | |
341 | if (_M_bytes[__i] != 0x00) | |
342 | return false; | |
343 | return _M_bytes[15] == 0x01 && _M_scope_id == 0; | |
344 | } | |
345 | ||
346 | constexpr bool | |
347 | is_multicast() const noexcept { return _M_bytes[0] == 0xFF; } | |
348 | ||
349 | constexpr bool | |
350 | is_link_local() const noexcept | |
351 | { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0x80; } | |
352 | ||
353 | constexpr bool | |
354 | is_site_local() const noexcept | |
355 | { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0xC0; } | |
356 | ||
357 | constexpr bool | |
358 | is_v4_mapped() const noexcept | |
359 | { | |
360 | const bytes_type& __b = _M_bytes; | |
361 | return __b[0] == 0 && __b[1] == 0 && __b[ 2] == 0 && __b[ 3] == 0 | |
362 | && __b[4] == 0 && __b[5] == 0 && __b[ 6] == 0 && __b[ 7] == 0 | |
363 | && __b[8] == 0 && __b[9] == 0 && __b[10] == 0xFF && __b[11] == 0xFF; | |
364 | } | |
365 | ||
366 | constexpr bool | |
367 | is_multicast_node_local() const noexcept | |
368 | { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x01; } | |
369 | ||
370 | constexpr bool | |
371 | is_multicast_link_local() const noexcept | |
372 | { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x02; } | |
373 | ||
374 | constexpr bool | |
375 | is_multicast_site_local() const noexcept | |
376 | { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x05; } | |
377 | ||
378 | constexpr bool | |
379 | is_multicast_org_local() const noexcept | |
380 | { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x08; } | |
381 | ||
382 | constexpr bool | |
383 | is_multicast_global() const noexcept | |
384 | { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x0b; } | |
385 | ||
386 | constexpr bytes_type to_bytes() const noexcept { return _M_bytes; } | |
387 | ||
388 | template<typename _Allocator = allocator<char>> | |
389 | __string_with<_Allocator> | |
390 | to_string(const _Allocator& __a = _Allocator()) const | |
391 | { | |
9ee35a86 | 392 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H |
e5989e71 | 393 | __string_with<_Allocator> __str(__a); |
d1a821b9 JW |
394 | __str.resize(INET6_ADDRSTRLEN + (_M_scope_id ? 11 : 0)); |
395 | char* const __p = &__str.front(); | |
396 | if (inet_ntop(AF_INET6, &_M_bytes, __p, __str.size())) | |
397 | { | |
398 | auto __end = __str.find('\0'); | |
399 | if (unsigned long __scope = _M_scope_id) | |
400 | { | |
401 | __end += | |
402 | #if _GLIBCXX_USE_C99_STDIO | |
403 | __builtin_snprintf(__p + __end, __str.size() - __end, | |
404 | "%%%lu", __scope); | |
405 | #else | |
406 | __builtin_sprintf(__p + __end, "%%%lu", __scope); | |
407 | #endif | |
408 | } | |
409 | __str.erase(__end); | |
410 | } | |
e5989e71 JW |
411 | else |
412 | __str.resize(0); | |
413 | return __str; | |
9ee35a86 JW |
414 | #else |
415 | std::__throw_system_error((int)__unsupported_err()); | |
82a0f2fd | 416 | #endif |
9ee35a86 | 417 | } |
e5989e71 JW |
418 | |
419 | // static members: | |
970ba719 | 420 | |
e5989e71 JW |
421 | static constexpr address_v6 |
422 | any() noexcept | |
423 | { | |
970ba719 | 424 | return {}; |
e5989e71 JW |
425 | } |
426 | ||
427 | static constexpr address_v6 | |
428 | loopback() noexcept | |
429 | { | |
d1a821b9 | 430 | return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}; |
e5989e71 JW |
431 | } |
432 | ||
433 | private: | |
434 | template<typename _InternetProtocol> | |
435 | friend class basic_endpoint; | |
436 | ||
437 | friend constexpr bool | |
438 | operator==(const address_v6&, const address_v6&) noexcept; | |
439 | ||
440 | friend constexpr bool | |
441 | operator< (const address_v6&, const address_v6&) noexcept; | |
442 | ||
443 | bytes_type _M_bytes; | |
444 | scope_id_type _M_scope_id; | |
445 | }; | |
446 | ||
447 | /// Exception type thrown on misuse of IPv4 addresses as IPv6 or vice versa. | |
448 | class bad_address_cast : public bad_cast | |
449 | { | |
450 | public: | |
451 | bad_address_cast() { } | |
452 | ||
453 | const char* what() const noexcept { return "bad address cast"; } | |
454 | }; | |
455 | ||
456 | /// An IPv4 or IPv6 address. | |
457 | class address | |
458 | { | |
459 | public: | |
460 | // constructors: | |
461 | constexpr address() noexcept : _M_v4(), _M_is_v4(true) { } | |
462 | ||
37326651 | 463 | #if __cpp_constexpr_dynamic_alloc |
e5989e71 | 464 | constexpr |
37326651 | 465 | #endif |
e5989e71 JW |
466 | address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4) |
467 | { | |
468 | if (_M_is_v4) | |
5ea8803e | 469 | std::_Construct(std::addressof(_M_v4), __a.to_v4()); |
e5989e71 | 470 | else |
5ea8803e | 471 | std::_Construct(std::addressof(_M_v6), __a.to_v6()); |
e5989e71 JW |
472 | } |
473 | ||
474 | constexpr | |
475 | address(const address_v4& __a) noexcept : _M_v4(__a), _M_is_v4(true) { } | |
476 | ||
477 | constexpr | |
478 | address(const address_v6& __a) noexcept : _M_v6(__a), _M_is_v4(false) { } | |
479 | ||
480 | // assignment: | |
481 | address& | |
482 | operator=(const address& __a) noexcept | |
483 | { | |
484 | if (__a._M_is_v4) | |
485 | *this = __a.to_v4(); | |
486 | else | |
487 | *this = __a.to_v6(); | |
488 | return *this; | |
489 | } | |
490 | ||
491 | address& | |
492 | operator=(const address_v4& __a) noexcept | |
493 | { | |
5ea8803e | 494 | std::_Construct(std::addressof(_M_v4), __a); |
e5989e71 JW |
495 | _M_is_v4 = true; |
496 | return *this; | |
497 | } | |
498 | ||
499 | address& | |
500 | operator=(const address_v6& __a) noexcept | |
501 | { | |
5ea8803e | 502 | std::_Construct(std::addressof(_M_v6), __a); |
e5989e71 JW |
503 | _M_is_v4 = false; |
504 | return *this; | |
505 | } | |
506 | ||
507 | // members: | |
508 | ||
509 | constexpr bool is_v4() const noexcept { return _M_is_v4; } | |
510 | constexpr bool is_v6() const noexcept { return !_M_is_v4; } | |
511 | ||
512 | constexpr address_v4 | |
513 | to_v4() const | |
514 | { | |
515 | if (!is_v4()) | |
516 | _GLIBCXX_THROW_OR_ABORT(bad_address_cast()); | |
517 | return _M_v4; | |
518 | } | |
519 | ||
520 | constexpr address_v6 | |
521 | to_v6() const | |
522 | { | |
523 | if (!is_v6()) | |
524 | _GLIBCXX_THROW_OR_ABORT(bad_address_cast()); | |
525 | return _M_v6; | |
526 | } | |
527 | ||
528 | constexpr bool | |
529 | is_unspecified() const noexcept | |
530 | { return _M_is_v4 ? _M_v4.is_unspecified() : _M_v6.is_unspecified(); } | |
531 | ||
532 | constexpr bool | |
533 | is_loopback() const noexcept | |
534 | { return _M_is_v4 ? _M_v4.is_loopback() : _M_v6.is_loopback(); } | |
535 | ||
536 | constexpr bool | |
537 | is_multicast() const noexcept | |
538 | { return _M_is_v4 ? _M_v4.is_multicast() : _M_v6.is_multicast(); } | |
539 | ||
540 | template<typename _Allocator = allocator<char>> | |
541 | __string_with<_Allocator> | |
542 | to_string(const _Allocator& __a = _Allocator()) const | |
543 | { | |
544 | if (_M_is_v4) | |
545 | return to_v4().to_string(__a); | |
546 | return to_v6().to_string(__a); | |
547 | } | |
548 | ||
549 | private: | |
550 | template<typename _InternetProtocol> | |
551 | friend class basic_endpoint; | |
552 | ||
553 | friend constexpr bool | |
554 | operator==(const address&, const address&) noexcept; | |
555 | ||
556 | friend constexpr bool | |
557 | operator<(const address&, const address&) noexcept; | |
558 | ||
559 | union { | |
560 | address_v4 _M_v4; | |
561 | address_v6 _M_v6; | |
562 | bool _M_uninit; | |
563 | }; | |
564 | bool _M_is_v4; | |
565 | }; | |
566 | ||
567 | /** ip::address_v4 comparisons | |
568 | * @{ | |
569 | */ | |
570 | ||
571 | constexpr bool | |
572 | operator==(const address_v4& __a, const address_v4& __b) noexcept | |
573 | { return __a.to_uint() == __b.to_uint(); } | |
574 | ||
575 | constexpr bool | |
576 | operator!=(const address_v4& __a, const address_v4& __b) noexcept | |
577 | { return !(__a == __b); } | |
578 | ||
579 | constexpr bool | |
580 | operator< (const address_v4& __a, const address_v4& __b) noexcept | |
581 | { return __a.to_uint() < __b.to_uint(); } | |
582 | ||
583 | constexpr bool | |
584 | operator> (const address_v4& __a, const address_v4& __b) noexcept | |
585 | { return __b < __a; } | |
586 | ||
587 | constexpr bool | |
588 | operator<=(const address_v4& __a, const address_v4& __b) noexcept | |
589 | { return !(__b < __a); } | |
590 | ||
591 | constexpr bool | |
592 | operator>=(const address_v4& __a, const address_v4& __b) noexcept | |
593 | { return !(__a < __b); } | |
594 | ||
f0b88346 | 595 | /// @} |
e5989e71 JW |
596 | |
597 | /** ip::address_v6 comparisons | |
598 | * @{ | |
599 | */ | |
600 | ||
601 | constexpr bool | |
602 | operator==(const address_v6& __a, const address_v6& __b) noexcept | |
603 | { | |
604 | const auto& __aa = __a._M_bytes; | |
605 | const auto& __bb = __b._M_bytes; | |
606 | int __i = 0; | |
d5f7e049 | 607 | for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i) |
e5989e71 JW |
608 | ; |
609 | return __i == 16 ? __a.scope_id() == __b.scope_id() : false; | |
610 | } | |
611 | ||
612 | constexpr bool | |
613 | operator!=(const address_v6& __a, const address_v6& __b) noexcept | |
614 | { return !(__a == __b); } | |
615 | ||
616 | constexpr bool | |
617 | operator< (const address_v6& __a, const address_v6& __b) noexcept | |
618 | { | |
619 | const auto& __aa = __a._M_bytes; | |
620 | const auto& __bb = __b._M_bytes; | |
621 | int __i = 0; | |
d5f7e049 | 622 | for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i) |
e5989e71 JW |
623 | ; |
624 | return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i]; | |
625 | } | |
626 | ||
627 | constexpr bool | |
628 | operator> (const address_v6& __a, const address_v6& __b) noexcept | |
629 | { return __b < __a; } | |
630 | ||
631 | constexpr bool | |
632 | operator<=(const address_v6& __a, const address_v6& __b) noexcept | |
633 | { return !(__b < __a); } | |
634 | ||
635 | constexpr bool | |
636 | operator>=(const address_v6& __a, const address_v6& __b) noexcept | |
637 | { return !(__a < __b); } | |
638 | ||
f0b88346 | 639 | /// @} |
e5989e71 JW |
640 | |
641 | /** ip::address comparisons | |
642 | * @{ | |
643 | */ | |
644 | ||
645 | constexpr bool | |
646 | operator==(const address& __a, const address& __b) noexcept | |
647 | { | |
648 | if (__a.is_v4()) | |
649 | return __b.is_v4() ? __a._M_v4 == __b._M_v4 : false; | |
650 | return __b.is_v4() ? false : __a._M_v6 == __b._M_v6; | |
651 | } | |
652 | ||
653 | constexpr bool | |
654 | operator!=(const address& __a, const address& __b) noexcept | |
655 | { return !(__a == __b); } | |
656 | ||
657 | constexpr bool | |
658 | operator< (const address& __a, const address& __b) noexcept | |
659 | { | |
660 | if (__a.is_v4()) | |
661 | return __b.is_v4() ? __a._M_v4 < __b._M_v4 : true; | |
662 | return __b.is_v4() ? false : __a._M_v6 < __b._M_v6; | |
663 | } | |
664 | ||
665 | constexpr bool | |
666 | operator> (const address& __a, const address& __b) noexcept | |
667 | { return __b < __a; } | |
668 | ||
669 | constexpr bool | |
670 | operator<=(const address& __a, const address& __b) noexcept | |
671 | { return !(__b < __a); } | |
672 | ||
673 | constexpr bool | |
674 | operator>=(const address& __a, const address& __b) noexcept | |
675 | { return !(__a < __b); } | |
676 | ||
f0b88346 | 677 | /// @} |
e5989e71 JW |
678 | |
679 | /** ip::address_v4 creation | |
680 | * @{ | |
681 | */ | |
682 | ||
683 | constexpr address_v4 | |
684 | make_address_v4(const address_v4::bytes_type& __b) | |
685 | { return address_v4{__b}; } | |
686 | ||
687 | constexpr address_v4 | |
688 | make_address_v4(address_v4::uint_type __val) | |
689 | { return address_v4{__val}; } | |
690 | ||
691 | constexpr address_v4 | |
692 | make_address_v4(v4_mapped_t, const address_v6& __a) | |
693 | { | |
694 | if (!__a.is_v4_mapped()) | |
695 | _GLIBCXX_THROW_OR_ABORT(bad_address_cast()); | |
696 | ||
697 | const auto __v6b = __a.to_bytes(); | |
698 | return address_v4::bytes_type(__v6b[12], __v6b[13], __v6b[14], __v6b[15]); | |
699 | } | |
700 | ||
701 | inline address_v4 | |
702 | make_address_v4(const char* __str, error_code& __ec) noexcept | |
703 | { | |
9ee35a86 | 704 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H |
e5989e71 JW |
705 | address_v4 __a; |
706 | const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr); | |
707 | if (__res == 1) | |
708 | { | |
709 | __ec.clear(); | |
710 | return __a; | |
711 | } | |
712 | if (__res == 0) | |
713 | __ec = std::make_error_code(std::errc::invalid_argument); | |
714 | else | |
715 | __ec.assign(errno, generic_category()); | |
9ee35a86 JW |
716 | #else |
717 | __ec = std::make_error_code(__unsupported_err()); | |
718 | #endif | |
e5989e71 JW |
719 | return {}; |
720 | } | |
721 | ||
722 | inline address_v4 | |
723 | make_address_v4(const char* __str) | |
724 | { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); } | |
725 | ||
726 | inline address_v4 | |
727 | make_address_v4(const string& __str, error_code& __ec) noexcept | |
728 | { return make_address_v4(__str.c_str(), __ec); } | |
729 | ||
730 | inline address_v4 | |
731 | make_address_v4(const string& __str) | |
732 | { return make_address_v4(__str.c_str()); } | |
733 | ||
734 | inline address_v4 | |
735 | make_address_v4(string_view __str, error_code& __ec) noexcept | |
736 | { | |
cd67d138 | 737 | char __buf[16]; // INET_ADDRSTRLEN isn't defined on Windows |
e5989e71 JW |
738 | auto __len = __str.copy(__buf, sizeof(__buf)); |
739 | if (__len == sizeof(__buf)) | |
740 | { | |
741 | __ec = std::make_error_code(std::errc::invalid_argument); | |
742 | return {}; | |
743 | } | |
744 | __ec.clear(); | |
745 | __buf[__len] = '\0'; | |
746 | return make_address_v4(__buf, __ec); | |
747 | } | |
748 | ||
749 | inline address_v4 | |
750 | make_address_v4(string_view __str) | |
751 | { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); } | |
752 | ||
f0b88346 | 753 | /// @} |
e5989e71 JW |
754 | |
755 | /** ip::address_v6 creation | |
756 | * @{ | |
757 | */ | |
758 | ||
759 | constexpr address_v6 | |
760 | make_address_v6(const address_v6::bytes_type& __b, scope_id_type __scope = 0) | |
761 | { return address_v6{__b, __scope}; } | |
762 | ||
763 | constexpr address_v6 | |
764 | make_address_v6(v4_mapped_t, const address_v4& __a) noexcept | |
765 | { | |
766 | const address_v4::bytes_type __v4b = __a.to_bytes(); | |
767 | address_v6::bytes_type __v6b(0, 0, 0, 0, 0, 0, 0, 0, | |
768 | 0, 0, 0xFF, 0xFF, | |
769 | __v4b[0], __v4b[1], __v4b[2], __v4b[3]); | |
770 | return address_v6(__v6b); | |
771 | } | |
772 | ||
773 | inline address_v6 | |
774 | __make_address_v6(const char* __addr, const char* __scope, error_code& __ec) | |
775 | { | |
9ee35a86 | 776 | #ifdef _GLIBCXX_HAVE_ARPA_INET_H |
e5989e71 | 777 | address_v6::bytes_type __b; |
9ee35a86 | 778 | const int __res = ::inet_pton(AF_INET6, __addr, __b.data()); |
e5989e71 JW |
779 | if (__res == 1) |
780 | { | |
781 | __ec.clear(); | |
782 | if (!__scope) | |
783 | { | |
784 | return { __b }; | |
785 | } | |
786 | ||
787 | char* __eptr; | |
788 | unsigned long __val = std::strtoul(__scope, &__eptr, 10); | |
789 | if (__eptr != __scope && !*__eptr | |
790 | && __val <= numeric_limits<scope_id_type>::max()) | |
791 | { | |
792 | return { __b, static_cast<scope_id_type>(__val) }; | |
793 | } | |
794 | __ec = std::make_error_code(std::errc::invalid_argument); | |
795 | } | |
796 | else if (__res == 0) | |
797 | __ec = std::make_error_code(std::errc::invalid_argument); | |
798 | else | |
799 | __ec.assign(errno, generic_category()); | |
9ee35a86 JW |
800 | #else |
801 | __ec = std::make_error_code(__unsupported_err()); | |
802 | #endif | |
e5989e71 JW |
803 | return {}; |
804 | } | |
805 | ||
806 | inline address_v6 | |
807 | make_address_v6(const char* __str, error_code& __ec) noexcept | |
808 | { | |
82a0f2fd | 809 | auto __p = __builtin_strchr(__str, '%'); |
e5989e71 JW |
810 | if (__p == nullptr) |
811 | return __make_address_v6(__str, nullptr, __ec); | |
812 | char __buf[64]; | |
813 | char* __out = __buf; | |
814 | bool __skip_leading_zero = true; | |
815 | while (__str < __p && __out < std::end(__buf)) | |
816 | { | |
817 | if (!__skip_leading_zero || *__str != '0') | |
818 | { | |
819 | if (*__str == ':' || *__str == '.') | |
820 | __skip_leading_zero = true; | |
821 | else | |
822 | __skip_leading_zero = false; | |
823 | *__out = *__str; | |
824 | } | |
825 | __str++; | |
826 | } | |
827 | if (__out == std::end(__buf)) | |
970ba719 JW |
828 | { |
829 | __ec = std::make_error_code(std::errc::invalid_argument); | |
830 | return {}; | |
831 | } | |
e5989e71 JW |
832 | else |
833 | { | |
834 | *__out = '\0'; | |
835 | return __make_address_v6(__buf, __p + 1, __ec); | |
836 | } | |
837 | } | |
838 | ||
839 | inline address_v6 | |
840 | make_address_v6(const char* __str) | |
841 | { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); } | |
842 | ||
843 | inline address_v6 | |
844 | make_address_v6(const string& __str, error_code& __ec) noexcept | |
845 | { | |
846 | auto __pos = __str.find('%'); | |
847 | if (__pos == string::npos) | |
848 | return __make_address_v6(__str.c_str(), nullptr, __ec); | |
849 | char __buf[64]; | |
850 | char* __out = __buf; | |
851 | bool __skip_leading_zero = true; | |
852 | size_t __n = 0; | |
853 | while (__n < __pos && __out < std::end(__buf)) | |
854 | { | |
855 | if (!__skip_leading_zero || __str[__n] != '0') | |
856 | { | |
857 | if (__str[__n] == ':' || __str[__n] == '.') | |
858 | __skip_leading_zero = true; | |
859 | else | |
860 | __skip_leading_zero = false; | |
861 | *__out = __str[__n]; | |
862 | } | |
863 | __n++; | |
864 | } | |
865 | if (__out == std::end(__buf)) | |
970ba719 JW |
866 | { |
867 | __ec = std::make_error_code(std::errc::invalid_argument); | |
868 | return {}; | |
869 | } | |
e5989e71 JW |
870 | else |
871 | { | |
872 | *__out = '\0'; | |
873 | return __make_address_v6(__buf, __str.c_str() + __pos + 1, __ec); | |
874 | } | |
875 | } | |
876 | ||
877 | inline address_v6 | |
878 | make_address_v6(const string& __str) | |
879 | { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); } | |
880 | ||
881 | inline address_v6 | |
882 | make_address_v6(string_view __str, error_code& __ec) noexcept | |
883 | { | |
884 | char __buf[64]; | |
885 | char* __out = __buf; | |
886 | char* __scope = nullptr; | |
887 | bool __skip_leading_zero = true; | |
888 | size_t __n = 0; | |
889 | while (__n < __str.length() && __out < std::end(__buf)) | |
890 | { | |
891 | if (__str[__n] == '%') | |
892 | { | |
893 | if (__scope) | |
894 | __out = std::end(__buf); | |
895 | else | |
896 | { | |
897 | *__out = '\0'; | |
898 | __scope = ++__out; | |
899 | __skip_leading_zero = true; | |
900 | } | |
901 | } | |
902 | else if (!__skip_leading_zero || __str[__n] != '0') | |
903 | { | |
904 | if (__str[__n] == ':' || __str[__n] == '.') | |
905 | __skip_leading_zero = true; | |
906 | else | |
907 | __skip_leading_zero = false; | |
908 | *__out = __str[__n]; | |
909 | __out++; | |
910 | } | |
911 | __n++; | |
912 | } | |
913 | if (__out == std::end(__buf)) | |
970ba719 JW |
914 | { |
915 | __ec = std::make_error_code(std::errc::invalid_argument); | |
916 | return {}; | |
917 | } | |
e5989e71 JW |
918 | else |
919 | { | |
920 | *__out = '\0'; | |
921 | return __make_address_v6(__buf, __scope, __ec); | |
922 | } | |
923 | } | |
924 | ||
925 | inline address_v6 | |
926 | make_address_v6(string_view __str) | |
927 | { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); } | |
928 | ||
f0b88346 | 929 | /// @} |
e5989e71 JW |
930 | |
931 | /** ip::address creation | |
932 | * @{ | |
933 | */ | |
934 | ||
935 | inline address | |
936 | make_address(const char* __str, error_code& __ec) noexcept | |
937 | { | |
938 | address __a; | |
939 | address_v6 __v6a = make_address_v6(__str, __ec); | |
940 | if (!__ec) | |
941 | __a = __v6a; | |
942 | else | |
943 | { | |
944 | address_v4 __v4a = make_address_v4(__str, __ec); | |
945 | if (!__ec) | |
946 | __a = __v4a; | |
947 | } | |
948 | return __a; | |
949 | } | |
950 | ||
951 | inline address | |
952 | make_address(const char* __str) | |
953 | { return make_address(__str, __throw_on_error{"make_address"}); } | |
954 | ||
955 | inline address | |
956 | make_address(const string& __str, error_code& __ec) noexcept; // TODO | |
957 | ||
958 | inline address | |
959 | make_address(const string& __str) | |
960 | { return make_address(__str, __throw_on_error{"make_address"}); } | |
961 | ||
962 | inline address | |
963 | make_address(string_view __str, error_code& __ec) noexcept | |
964 | { | |
965 | if (__str.rfind('\0') != string_view::npos) | |
966 | return make_address(__str.data(), __ec); | |
967 | return make_address(__str.to_string(), __ec); // TODO don't allocate | |
968 | } | |
969 | ||
970 | inline address | |
971 | make_address(string_view __str) | |
972 | { return make_address(__str, __throw_on_error{"make_address"}); } | |
973 | ||
f0b88346 | 974 | /// @} |
e5989e71 JW |
975 | |
976 | /// ip::address I/O | |
977 | template<typename _CharT, typename _Traits> | |
978 | inline basic_ostream<_CharT, _Traits>& | |
979 | operator<<(basic_ostream<_CharT, _Traits>& __os, const address& __a) | |
980 | { return __os << __a.to_string(); } | |
981 | ||
982 | /// ip::address_v4 I/O | |
983 | template<typename _CharT, typename _Traits> | |
984 | inline basic_ostream<_CharT, _Traits>& | |
985 | operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v4& __a) | |
986 | { return __os << __a.to_string(); } | |
987 | ||
988 | /// ip::address_v6 I/O | |
989 | template<typename _CharT, typename _Traits> | |
990 | inline basic_ostream<_CharT, _Traits>& | |
991 | operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v6& __a) | |
992 | { return __os << __a.to_string(); } | |
993 | ||
994 | template<typename> class basic_address_iterator; // not defined | |
995 | ||
996 | template<> class basic_address_iterator<address_v4> | |
997 | { | |
998 | public: | |
999 | // types: | |
f26e72d8 JW |
1000 | using value_type = address_v4; |
1001 | using difference_type = ptrdiff_t; | |
1002 | using pointer = const address_v4*; | |
1003 | using reference = const address_v4&; | |
1004 | using iterator_category = input_iterator_tag; | |
e5989e71 JW |
1005 | |
1006 | // constructors: | |
1007 | basic_address_iterator(const address_v4& __a) noexcept | |
1008 | : _M_address(__a) { } | |
1009 | ||
1010 | // members: | |
1011 | reference operator*() const noexcept { return _M_address; } | |
1012 | pointer operator->() const noexcept { return &_M_address; } | |
1013 | ||
1014 | basic_address_iterator& | |
1015 | operator++() noexcept | |
1016 | { | |
1017 | _M_address = value_type(_M_address.to_uint() + 1); | |
1018 | return *this; | |
1019 | } | |
1020 | ||
1021 | basic_address_iterator operator++(int) noexcept | |
1022 | { | |
1023 | auto __tmp = *this; | |
1024 | ++*this; | |
1025 | return __tmp; | |
1026 | } | |
1027 | ||
1028 | basic_address_iterator& operator--() noexcept | |
1029 | { | |
1030 | _M_address = value_type(_M_address.to_uint() - 1); | |
1031 | return *this; | |
1032 | } | |
1033 | ||
1034 | basic_address_iterator | |
1035 | operator--(int) noexcept | |
1036 | { | |
1037 | auto __tmp = *this; | |
1038 | --*this; | |
1039 | return __tmp; | |
1040 | } | |
1041 | ||
1042 | bool | |
1043 | operator==(const basic_address_iterator& __rhs) const noexcept | |
1044 | { return _M_address == __rhs._M_address; } | |
1045 | ||
1046 | bool | |
1047 | operator!=(const basic_address_iterator& __rhs) const noexcept | |
1048 | { return _M_address != __rhs._M_address; } | |
1049 | ||
1050 | private: | |
1051 | address_v4 _M_address; | |
1052 | }; | |
1053 | ||
f26e72d8 | 1054 | using address_v4_iterator = basic_address_iterator<address_v4>; |
e5989e71 JW |
1055 | |
1056 | template<> class basic_address_iterator<address_v6> | |
1057 | { | |
1058 | public: | |
1059 | // types: | |
f26e72d8 JW |
1060 | using value_type = address_v6; |
1061 | using difference_type = ptrdiff_t; | |
1062 | using pointer = const address_v6*; | |
1063 | using reference = const address_v6&; | |
1064 | using iterator_category = input_iterator_tag; | |
e5989e71 JW |
1065 | |
1066 | // constructors: | |
1067 | basic_address_iterator(const address_v6& __a) noexcept | |
1068 | : _M_address(__a) { } | |
1069 | ||
1070 | // members: | |
1071 | reference operator*() const noexcept { return _M_address; } | |
1072 | pointer operator->() const noexcept { return &_M_address; } | |
1073 | ||
1074 | basic_address_iterator& | |
1075 | operator++() noexcept; // TODO | |
1076 | ||
1077 | basic_address_iterator | |
1078 | operator++(int) noexcept | |
1079 | { | |
1080 | auto __tmp = *this; | |
1081 | ++*this; | |
1082 | return __tmp; | |
1083 | } | |
1084 | ||
1085 | basic_address_iterator& | |
1086 | operator--() noexcept; // TODO | |
1087 | ||
1088 | basic_address_iterator | |
1089 | operator--(int) noexcept | |
1090 | { | |
1091 | auto __tmp = *this; | |
1092 | --*this; | |
1093 | return __tmp; | |
1094 | } | |
1095 | ||
1096 | bool | |
1097 | operator==(const basic_address_iterator& __rhs) const noexcept | |
1098 | { return _M_address == __rhs._M_address; } | |
1099 | ||
1100 | bool | |
1101 | operator!=(const basic_address_iterator& __rhs) const noexcept | |
1102 | { return _M_address != __rhs._M_address; } | |
1103 | ||
1104 | private: | |
1105 | address_v6 _M_address; | |
1106 | }; | |
1107 | ||
f26e72d8 | 1108 | using address_v6_iterator = basic_address_iterator<address_v6>; |
e5989e71 JW |
1109 | |
1110 | template<typename> class basic_address_range; // not defined | |
1111 | ||
1112 | /** An IPv6 address range. | |
1113 | * @{ | |
1114 | */ | |
1115 | ||
1116 | template<> class basic_address_range<address_v4> | |
1117 | { | |
1118 | public: | |
1119 | // types: | |
1120 | ||
f26e72d8 | 1121 | using iterator = basic_address_iterator<address_v4>; |
e5989e71 JW |
1122 | |
1123 | // constructors: | |
1124 | ||
1125 | basic_address_range() noexcept : _M_begin({}), _M_end({}) { } | |
1126 | ||
1127 | basic_address_range(const address_v4& __first, | |
1128 | const address_v4& __last) noexcept | |
1129 | : _M_begin(__first), _M_end(__last) { } | |
1130 | ||
1131 | // members: | |
1132 | ||
1133 | iterator begin() const noexcept { return _M_begin; } | |
1134 | iterator end() const noexcept { return _M_end; } | |
d715f554 | 1135 | _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; } |
e5989e71 JW |
1136 | |
1137 | size_t | |
1138 | size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); } | |
1139 | ||
1140 | iterator | |
1141 | find(const address_v4& __addr) const noexcept | |
1142 | { | |
1143 | if (*_M_begin <= __addr && __addr < *_M_end) | |
1144 | return iterator{__addr}; | |
1145 | return end(); | |
1146 | } | |
1147 | ||
1148 | private: | |
1149 | iterator _M_begin; | |
1150 | iterator _M_end; | |
1151 | }; | |
1152 | ||
f26e72d8 | 1153 | using address_v4_range = basic_address_range<address_v4>; |
e5989e71 | 1154 | |
f0b88346 | 1155 | /// @} |
e5989e71 JW |
1156 | |
1157 | /** An IPv6 address range. | |
1158 | * @{ | |
1159 | */ | |
1160 | ||
1161 | template<> class basic_address_range<address_v6> | |
1162 | { | |
1163 | public: | |
1164 | // types: | |
1165 | ||
f26e72d8 | 1166 | using iterator = basic_address_iterator<address_v6>; |
e5989e71 JW |
1167 | |
1168 | // constructors: | |
1169 | ||
1170 | basic_address_range() noexcept : _M_begin({}), _M_end({}) { } | |
1171 | basic_address_range(const address_v6& __first, | |
1172 | const address_v6& __last) noexcept | |
1173 | : _M_begin(__first), _M_end(__last) { } | |
1174 | ||
1175 | // members: | |
1176 | ||
1177 | iterator begin() const noexcept { return _M_begin; } | |
1178 | iterator end() const noexcept { return _M_end; } | |
d715f554 | 1179 | _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; } |
e5989e71 JW |
1180 | |
1181 | iterator | |
1182 | find(const address_v6& __addr) const noexcept | |
1183 | { | |
1184 | if (*_M_begin <= __addr && __addr < *_M_end) | |
1185 | return iterator{__addr}; | |
1186 | return end(); | |
1187 | } | |
1188 | ||
1189 | private: | |
1190 | iterator _M_begin; | |
1191 | iterator _M_end; | |
1192 | }; | |
1193 | ||
f26e72d8 | 1194 | using address_v6_range = basic_address_range<address_v6>; |
e5989e71 | 1195 | |
f0b88346 | 1196 | /// @} |
3084625d | 1197 | |
e5989e71 JW |
1198 | bool |
1199 | operator==(const network_v4& __a, const network_v4& __b) noexcept; | |
1200 | ||
1201 | bool | |
1202 | operator==(const network_v6& __a, const network_v6& __b) noexcept; | |
1203 | ||
e5989e71 JW |
1204 | |
1205 | /// An IPv4 network address. | |
1206 | class network_v4 | |
1207 | { | |
1208 | public: | |
1209 | // constructors: | |
1210 | constexpr network_v4() noexcept : _M_addr(), _M_prefix_len(0) { } | |
1211 | ||
1212 | constexpr | |
1213 | network_v4(const address_v4& __addr, int __prefix_len) | |
1214 | : _M_addr(__addr), _M_prefix_len(__prefix_len) | |
1215 | { | |
1216 | if (_M_prefix_len < 0 || _M_prefix_len > 32) | |
1217 | __throw_out_of_range("network_v4: invalid prefix length"); | |
1218 | } | |
1219 | ||
1220 | constexpr | |
1221 | network_v4(const address_v4& __addr, const address_v4& __mask) | |
1222 | : _M_addr(__addr), _M_prefix_len(__builtin_popcount(__mask.to_uint())) | |
1223 | { | |
1224 | if (_M_prefix_len != 0) | |
1225 | { | |
1226 | address_v4::uint_type __mask_uint = __mask.to_uint(); | |
1227 | if (__builtin_ctz(__mask_uint) != (32 - _M_prefix_len)) | |
1228 | __throw_invalid_argument("network_v4: invalid mask"); | |
1229 | if ((__mask_uint & 0x80000000) == 0) | |
1230 | __throw_invalid_argument("network_v4: invalid mask"); | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | // members: | |
1235 | ||
1236 | constexpr address_v4 address() const noexcept { return _M_addr; } | |
1237 | constexpr int prefix_length() const noexcept { return _M_prefix_len; } | |
1238 | ||
1239 | constexpr address_v4 | |
1240 | netmask() const noexcept | |
1241 | { | |
1242 | address_v4::uint_type __val = address_v4::broadcast().to_uint(); | |
1243 | __val >>= (32 - _M_prefix_len); | |
1244 | __val <<= (32 - _M_prefix_len); | |
1245 | return address_v4{__val}; | |
1246 | } | |
1247 | ||
1248 | constexpr address_v4 | |
1249 | network() const noexcept | |
1250 | { return address_v4{_M_addr.to_uint() & netmask().to_uint()}; } | |
1251 | ||
1252 | constexpr address_v4 | |
1253 | broadcast() const noexcept | |
1254 | { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; } | |
1255 | ||
1256 | address_v4_range | |
1257 | hosts() const noexcept | |
1258 | { | |
1259 | if (is_host()) | |
1260 | return { address(), *++address_v4_iterator(address()) }; | |
1261 | return { network(), broadcast() }; | |
1262 | } | |
1263 | ||
1264 | constexpr network_v4 | |
1265 | canonical() const noexcept | |
1266 | { return network_v4(network(), prefix_length()); } | |
1267 | ||
1268 | constexpr bool is_host() const noexcept { return _M_prefix_len == 32; } | |
1269 | ||
1270 | constexpr bool | |
1271 | is_subnet_of(const network_v4& __other) const noexcept | |
1272 | { | |
1273 | if (__other.prefix_length() < prefix_length()) | |
1274 | { | |
1275 | network_v4 __net(address(), __other.prefix_length()); | |
1276 | return __net.canonical() == __other.canonical(); | |
1277 | } | |
1278 | return false; | |
1279 | } | |
1280 | ||
1281 | template<typename _Allocator = allocator<char>> | |
1282 | __string_with<_Allocator> | |
1283 | to_string(const _Allocator& __a = _Allocator()) const | |
1284 | { | |
1285 | return address().to_string(__a) + '/' | |
1286 | + std::to_string(prefix_length()); | |
1287 | } | |
1288 | ||
1289 | private: | |
1290 | address_v4 _M_addr; | |
1291 | int _M_prefix_len; | |
1292 | }; | |
1293 | ||
1294 | /// An IPv6 network address. | |
1295 | class network_v6 | |
1296 | { | |
1297 | public: | |
1298 | // constructors: | |
1299 | constexpr network_v6() noexcept : _M_addr(), _M_prefix_len(0) { } | |
1300 | ||
1301 | constexpr | |
1302 | network_v6(const address_v6& __addr, int __prefix_len) | |
1303 | : _M_addr(__addr), _M_prefix_len(__prefix_len) | |
1304 | { | |
1305 | if (_M_prefix_len < 0 || _M_prefix_len > 128) | |
1306 | __throw_out_of_range("network_v6: invalid prefix length"); | |
1307 | } | |
1308 | ||
1309 | // members: | |
1310 | constexpr address_v6 address() const noexcept { return _M_addr; } | |
1311 | constexpr int prefix_length() const noexcept { return _M_prefix_len; } | |
1312 | ||
1313 | constexpr address_v6 network() const noexcept; // TODO | |
1314 | ||
1315 | address_v6_range | |
1316 | hosts() const noexcept | |
1317 | { | |
1318 | if (is_host()) | |
1319 | return { address(), *++address_v6_iterator(address()) }; | |
1320 | return {}; // { network(), XXX broadcast() XXX }; // TODO | |
1321 | } | |
1322 | ||
1323 | constexpr network_v6 | |
1324 | canonical() const noexcept | |
1325 | { return network_v6{network(), prefix_length()}; } | |
1326 | ||
1327 | constexpr bool is_host() const noexcept { return _M_prefix_len == 128; } | |
1328 | ||
1329 | constexpr bool | |
1330 | is_subnet_of(const network_v6& __other) const noexcept | |
1331 | { | |
1332 | if (__other.prefix_length() < prefix_length()) | |
1333 | { | |
1334 | network_v6 __net(address(), __other.prefix_length()); | |
1335 | return __net.canonical() == __other.canonical(); | |
1336 | } | |
1337 | return false; | |
1338 | } | |
1339 | ||
1340 | template<typename _Allocator = allocator<char>> | |
1341 | __string_with<_Allocator> | |
1342 | to_string(const _Allocator& __a = _Allocator()) const | |
1343 | { | |
1344 | return address().to_string(__a) + '/' | |
1345 | + std::to_string(prefix_length()); | |
1346 | } | |
1347 | ||
1348 | private: | |
1349 | address_v6 _M_addr; | |
1350 | int _M_prefix_len; | |
1351 | }; | |
1352 | ||
1353 | ||
1354 | /** ip::network_v4 comparisons | |
1355 | * @{ | |
1356 | */ | |
1357 | ||
1358 | inline bool | |
1359 | operator==(const network_v4& __a, const network_v4& __b) noexcept | |
1360 | { | |
1361 | return __a.address() == __b.address() | |
1362 | && __a.prefix_length() == __b.prefix_length(); | |
1363 | } | |
1364 | ||
1365 | inline bool | |
1366 | operator!=(const network_v4& __a, const network_v4& __b) noexcept | |
1367 | { return !(__a == __b); } | |
1368 | ||
f0b88346 | 1369 | /// @} |
e5989e71 JW |
1370 | |
1371 | /** ip::network_v6 comparisons | |
1372 | * @{ | |
1373 | */ | |
1374 | ||
1375 | inline bool | |
1376 | operator==(const network_v6& __a, const network_v6& __b) noexcept | |
1377 | { | |
1378 | return __a.address() == __b.address() | |
1379 | && __a.prefix_length() == __b.prefix_length(); | |
1380 | } | |
1381 | ||
1382 | inline bool | |
1383 | operator!=(const network_v6& __a, const network_v6& __b) noexcept | |
1384 | { return !(__a == __b); } | |
1385 | ||
f0b88346 | 1386 | /// @} |
e5989e71 JW |
1387 | |
1388 | /** ip::network_v4 creation | |
1389 | * @{ | |
1390 | */ | |
1391 | ||
1392 | inline network_v4 | |
1393 | make_network_v4(const address_v4& __a, int __prefix_len) | |
1394 | { return network_v4{__a, __prefix_len}; } | |
1395 | ||
3f4aa457 | 1396 | inline network_v4 |
e5989e71 JW |
1397 | make_network_v4(const address_v4& __a, const address_v4& __mask) |
1398 | { return network_v4{ __a, __mask }; } | |
1399 | ||
1400 | network_v4 make_network_v4(const char*, error_code&) noexcept; // TODO | |
1401 | ||
1402 | inline network_v4 | |
1403 | make_network_v4(const char* __str) | |
1404 | { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); } | |
1405 | ||
1406 | network_v4 make_network_v4(const string&, error_code&) noexcept; // TODO | |
1407 | ||
1408 | inline network_v4 | |
1409 | make_network_v4(const string& __str) | |
1410 | { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); } | |
1411 | ||
1412 | network_v4 make_network_v4(string_view, error_code&) noexcept; // TODO | |
1413 | ||
1414 | inline network_v4 | |
1415 | make_network_v4(string_view __str) | |
1416 | { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); } | |
1417 | ||
f0b88346 | 1418 | /// @} |
e5989e71 JW |
1419 | |
1420 | /** ip::network_v6 creation | |
1421 | * @{ | |
1422 | */ | |
1423 | ||
1424 | inline network_v6 | |
1425 | make_network_v6(const address_v6& __a, int __prefix_len) | |
1426 | { return network_v6{__a, __prefix_len}; } | |
1427 | ||
1428 | network_v6 make_network_v6(const char*, error_code&) noexcept; // TODO | |
1429 | ||
1430 | inline network_v6 | |
1431 | make_network_v6(const char* __str) | |
1432 | { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); } | |
1433 | ||
1434 | network_v6 make_network_v6(const string&, error_code&) noexcept; // TODO | |
1435 | ||
1436 | inline network_v6 | |
1437 | make_network_v6(const string& __str) | |
1438 | { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); } | |
1439 | ||
1440 | network_v6 make_network_v6(string_view, error_code&) noexcept; // TODO | |
1441 | ||
1442 | inline network_v6 | |
1443 | make_network_v6(string_view __str) | |
1444 | { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); } | |
1445 | ||
f0b88346 | 1446 | /// @} |
e5989e71 JW |
1447 | |
1448 | /// ip::network_v4 I/O | |
1449 | template<typename _CharT, typename _Traits> | |
1450 | inline basic_ostream<_CharT, _Traits>& | |
1451 | operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v4& __net) | |
1452 | { return __os << __net.to_string(); } | |
1453 | ||
1454 | /// ip::network_v6 I/O | |
1455 | template<typename _CharT, typename _Traits> | |
1456 | inline basic_ostream<_CharT, _Traits>& | |
1457 | operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v6& __net) | |
1458 | { return __os << __net.to_string(); } | |
1459 | ||
1460 | /// An IP endpoint. | |
1461 | template<typename _InternetProtocol> | |
1462 | class basic_endpoint | |
1463 | { | |
1464 | public: | |
1465 | // types: | |
f26e72d8 | 1466 | using protocol_type = _InternetProtocol; |
e5989e71 JW |
1467 | |
1468 | // constructors: | |
1469 | ||
1470 | constexpr | |
1471 | basic_endpoint() noexcept : _M_data() | |
1472 | { _M_data._M_v4.sin_family = protocol_type::v4().family(); } | |
1473 | ||
1474 | constexpr | |
1475 | basic_endpoint(const protocol_type& __proto, | |
1476 | port_type __port_num) noexcept | |
1477 | : _M_data() | |
1478 | { | |
1479 | __glibcxx_assert(__proto == protocol_type::v4() | |
1480 | || __proto == protocol_type::v6()); | |
1481 | ||
1482 | _M_data._M_v4.sin_family = __proto.family(); | |
187fdaea | 1483 | _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); |
e5989e71 JW |
1484 | } |
1485 | ||
1486 | constexpr | |
1487 | basic_endpoint(const ip::address& __addr, | |
1488 | port_type __port_num) noexcept | |
1489 | : _M_data() | |
1490 | { | |
1491 | if (__addr.is_v4()) | |
1492 | { | |
1493 | _M_data._M_v4.sin_family = protocol_type::v4().family(); | |
187fdaea | 1494 | _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); |
e5989e71 JW |
1495 | _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr; |
1496 | } | |
1497 | else | |
1498 | { | |
1499 | _M_data._M_v6 = {}; | |
1500 | _M_data._M_v6.sin6_family = protocol_type::v6().family(); | |
187fdaea | 1501 | _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num); |
82a0f2fd JW |
1502 | __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr, |
1503 | __addr._M_v6._M_bytes.data(), 16); | |
e5989e71 JW |
1504 | _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id; |
1505 | } | |
1506 | } | |
1507 | ||
1508 | // members: | |
1509 | constexpr protocol_type protocol() const noexcept | |
1510 | { | |
39073938 | 1511 | return _M_is_v6() ? protocol_type::v6() : protocol_type::v4(); |
e5989e71 JW |
1512 | } |
1513 | ||
1514 | constexpr ip::address | |
1515 | address() const noexcept | |
1516 | { | |
1517 | ip::address __addr; | |
39073938 | 1518 | if (_M_is_v6()) |
e5989e71 | 1519 | { |
82a0f2fd JW |
1520 | __builtin_memcpy(&__addr._M_v6._M_bytes, |
1521 | _M_data._M_v6.sin6_addr.s6_addr, 16); | |
e5989e71 JW |
1522 | __addr._M_is_v4 = false; |
1523 | } | |
1524 | else | |
1525 | { | |
82a0f2fd JW |
1526 | __builtin_memcpy(&__addr._M_v4._M_addr, |
1527 | &_M_data._M_v4.sin_addr.s_addr, 4); | |
e5989e71 JW |
1528 | } |
1529 | return __addr; | |
1530 | } | |
1531 | ||
1532 | void | |
1533 | address(const ip::address& __addr) noexcept | |
1534 | { | |
1535 | if (__addr.is_v6()) | |
1536 | { | |
1537 | _M_data._M_v6 = {}; | |
1538 | _M_data._M_v6.sin6_family = protocol_type::v6().family(); | |
82a0f2fd JW |
1539 | __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr, |
1540 | __addr._M_v6._M_bytes.data(), 16); | |
e5989e71 JW |
1541 | _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id; |
1542 | } | |
1543 | else | |
1544 | { | |
1545 | _M_data._M_v4.sin_family = protocol_type::v4().family(); | |
1546 | _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr; | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | constexpr port_type | |
1551 | port() const noexcept | |
187fdaea | 1552 | { return address_v4::_S_ntoh_16(_M_data._M_v4.sin_port); } |
e5989e71 JW |
1553 | |
1554 | void | |
1555 | port(port_type __port_num) noexcept | |
187fdaea | 1556 | { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); } |
e5989e71 JW |
1557 | |
1558 | void* data() noexcept { return &_M_data; } | |
39073938 | 1559 | |
e5989e71 | 1560 | const void* data() const noexcept { return &_M_data; } |
39073938 | 1561 | |
e5989e71 | 1562 | constexpr size_t size() const noexcept |
39073938 | 1563 | { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); } |
e5989e71 JW |
1564 | |
1565 | void | |
1566 | resize(size_t __s) | |
1567 | { | |
39073938 | 1568 | if (__s != size()) |
e5989e71 JW |
1569 | __throw_length_error("net::ip::basic_endpoint::resize"); |
1570 | } | |
1571 | ||
1572 | constexpr size_t capacity() const noexcept { return sizeof(_M_data); } | |
1573 | ||
1574 | private: | |
1575 | union | |
1576 | { | |
1577 | sockaddr_in _M_v4; | |
1578 | sockaddr_in6 _M_v6; | |
1579 | } _M_data; | |
39073938 JW |
1580 | |
1581 | constexpr bool _M_is_v6() const noexcept | |
1582 | { return _M_data._M_v4.sin_family == AF_INET6; } | |
e5989e71 JW |
1583 | }; |
1584 | ||
1585 | /** basic_endpoint comparisons | |
1586 | * @{ | |
1587 | */ | |
1588 | ||
1589 | template<typename _InternetProtocol> | |
1590 | inline bool | |
1591 | operator==(const basic_endpoint<_InternetProtocol>& __a, | |
1592 | const basic_endpoint<_InternetProtocol>& __b) | |
1593 | { return __a.address() == __b.address() && __a.port() == __b.port(); } | |
1594 | ||
1595 | template<typename _InternetProtocol> | |
1596 | inline bool | |
1597 | operator!=(const basic_endpoint<_InternetProtocol>& __a, | |
1598 | const basic_endpoint<_InternetProtocol>& __b) | |
1599 | { return !(__a == __b); } | |
1600 | ||
1601 | template<typename _InternetProtocol> | |
1602 | inline bool | |
1603 | operator< (const basic_endpoint<_InternetProtocol>& __a, | |
1604 | const basic_endpoint<_InternetProtocol>& __b) | |
1605 | { | |
1606 | return __a.address() < __b.address() | |
1607 | || (!(__b.address() < __a.address()) && __a.port() < __b.port()); | |
1608 | } | |
1609 | ||
1610 | template<typename _InternetProtocol> | |
1611 | inline bool | |
1612 | operator> (const basic_endpoint<_InternetProtocol>& __a, | |
1613 | const basic_endpoint<_InternetProtocol>& __b) | |
1614 | { return __b < __a; } | |
1615 | ||
1616 | template<typename _InternetProtocol> | |
1617 | inline bool | |
1618 | operator<=(const basic_endpoint<_InternetProtocol>& __a, | |
1619 | const basic_endpoint<_InternetProtocol>& __b) | |
1620 | { return !(__b < __a); } | |
1621 | ||
1622 | template<typename _InternetProtocol> | |
1623 | inline bool | |
1624 | operator>=(const basic_endpoint<_InternetProtocol>& __a, | |
1625 | const basic_endpoint<_InternetProtocol>& __b) | |
1626 | { return !(__a < __b); } | |
1627 | ||
f0b88346 | 1628 | /// @} |
e5989e71 JW |
1629 | |
1630 | /// basic_endpoint I/O | |
1631 | template<typename _CharT, typename _Traits, typename _InternetProtocol> | |
1632 | inline basic_ostream<_CharT, _Traits>& | |
1633 | operator<<(basic_ostream<_CharT, _Traits>& __os, | |
1634 | const basic_endpoint<_InternetProtocol>& __ep) | |
1635 | { | |
1636 | basic_ostringstream<_CharT, _Traits> __ss; | |
1637 | if (__ep.protocol() | |
1638 | == basic_endpoint<_InternetProtocol>::protocol_type::v6()) | |
1639 | __ss << '[' << __ep.address() << ']'; | |
1640 | else | |
1641 | __ss << __ep.address(); | |
1642 | __ss << ':' << __ep.port(); | |
1643 | __os << __ss.str(); | |
1644 | return __os; | |
1645 | } | |
1646 | ||
1647 | /** Type representing a single result of name/address resolution. | |
1648 | * @{ | |
1649 | */ | |
1650 | ||
1651 | template<typename _InternetProtocol> | |
1652 | class basic_resolver_entry | |
1653 | { | |
1654 | public: | |
1655 | // types: | |
f26e72d8 JW |
1656 | using protocol_type = _InternetProtocol; |
1657 | using endpoint_type = typename _InternetProtocol::endpoint; | |
e5989e71 JW |
1658 | |
1659 | // constructors: | |
1660 | basic_resolver_entry() { } | |
1661 | ||
1662 | basic_resolver_entry(const endpoint_type& __ep, | |
1663 | string_view __h, string_view __s) | |
1664 | : _M_ep(__ep), _M_host(__h), _M_svc(__s) { } | |
1665 | ||
1666 | // members: | |
1667 | endpoint_type endpoint() const { return _M_ep; } | |
1668 | operator endpoint_type() const { return _M_ep; } | |
1669 | ||
1670 | template<typename _Allocator = allocator<char>> | |
1671 | __string_with<_Allocator> | |
1672 | host_name(const _Allocator& __a = _Allocator()) const | |
1673 | { return { _M_host, __a }; } | |
1674 | ||
1675 | template<typename _Allocator = allocator<char>> | |
1676 | __string_with<_Allocator> | |
1677 | service_name(const _Allocator& __a = _Allocator()) const | |
1678 | { return { _M_svc, __a }; } | |
1679 | ||
1680 | private: | |
1681 | basic_endpoint<_InternetProtocol> _M_ep; | |
1682 | string _M_host; | |
1683 | string _M_svc; | |
1684 | }; | |
1685 | ||
1686 | template<typename _InternetProtocol> | |
1687 | inline bool | |
1688 | operator==(const basic_resolver_entry<_InternetProtocol>& __a, | |
1689 | const basic_resolver_entry<_InternetProtocol>& __b) | |
1690 | { | |
1691 | return __a.endpoint() == __b.endpoint() | |
1692 | && __a.host_name() == __b.host_name() | |
1693 | && __a.service_name() == __b.service_name(); | |
1694 | } | |
1695 | ||
1696 | template<typename _InternetProtocol> | |
1697 | inline bool | |
1698 | operator!=(const basic_resolver_entry<_InternetProtocol>& __a, | |
1699 | const basic_resolver_entry<_InternetProtocol>& __b) | |
1700 | { return !(__a == __b); } | |
1701 | ||
f0b88346 | 1702 | /// @} |
e5989e71 JW |
1703 | |
1704 | /** Base class defining flags for name/address resolution. | |
1705 | * @{ | |
1706 | */ | |
1707 | ||
1708 | class resolver_base | |
1709 | { | |
1710 | public: | |
49adc066 JW |
1711 | enum flags : int { }; |
1712 | static constexpr flags passive = (flags)AI_PASSIVE; | |
1713 | static constexpr flags canonical_name = (flags)AI_CANONNAME; | |
1714 | static constexpr flags numeric_host = (flags)AI_NUMERICHOST; | |
cbe0bca4 | 1715 | #ifdef AI_NUMERICSERV |
49adc066 | 1716 | static constexpr flags numeric_service = (flags)AI_NUMERICSERV; |
cbe0bca4 | 1717 | #endif |
cd67d138 | 1718 | #ifdef AI_V4MAPPED |
49adc066 | 1719 | static constexpr flags v4_mapped = (flags)AI_V4MAPPED; |
cd67d138 JW |
1720 | #endif |
1721 | #ifdef AI_ALL | |
49adc066 | 1722 | static constexpr flags all_matching = (flags)AI_ALL; |
cd67d138 JW |
1723 | #endif |
1724 | #ifdef AI_ADDRCONFIG | |
49adc066 | 1725 | static constexpr flags address_configured = (flags)AI_ADDRCONFIG; |
cd67d138 | 1726 | #endif |
e5989e71 | 1727 | |
8aadb4fe JW |
1728 | friend constexpr flags |
1729 | operator&(flags __f1, flags __f2) noexcept | |
1730 | { return flags( int(__f1) & int(__f2) ); } | |
e5989e71 | 1731 | |
8aadb4fe JW |
1732 | friend constexpr flags |
1733 | operator|(flags __f1, flags __f2) noexcept | |
1734 | { return flags( int(__f1) | int(__f2) ); } | |
e5989e71 | 1735 | |
8aadb4fe JW |
1736 | friend constexpr flags |
1737 | operator^(flags __f1, flags __f2) noexcept | |
1738 | { return flags( int(__f1) ^ int(__f2) ); } | |
e5989e71 | 1739 | |
8aadb4fe JW |
1740 | friend constexpr flags |
1741 | operator~(flags __f) noexcept | |
1742 | { return flags( ~int(__f) ); } | |
e5989e71 | 1743 | |
8aadb4fe JW |
1744 | friend constexpr flags& |
1745 | operator&=(flags& __f1, flags __f2) noexcept | |
1746 | { return __f1 = (__f1 & __f2); } | |
e5989e71 | 1747 | |
8aadb4fe JW |
1748 | friend constexpr flags& |
1749 | operator|=(flags& __f1, flags __f2) noexcept | |
1750 | { return __f1 = (__f1 | __f2); } | |
e5989e71 | 1751 | |
8aadb4fe JW |
1752 | friend constexpr flags& |
1753 | operator^=(flags& __f1, flags __f2) noexcept | |
1754 | { return __f1 = (__f1 ^ __f2); } | |
e5989e71 | 1755 | |
8aadb4fe JW |
1756 | protected: |
1757 | resolver_base() = default; | |
1758 | ~resolver_base() = default; | |
1759 | }; | |
e5989e71 | 1760 | |
49adc066 | 1761 | // TODO define resolver_base::flags static constants in .so for C++14 mode |
e5989e71 | 1762 | |
f0b88346 | 1763 | /// @} |
e5989e71 JW |
1764 | |
1765 | /** Container for results of name/address resolution. | |
1766 | * @{ | |
1767 | */ | |
1768 | ||
1769 | template<typename _InternetProtocol> | |
1770 | class basic_resolver_results | |
1771 | { | |
1772 | public: | |
1773 | // types: | |
f26e72d8 JW |
1774 | using protocol_type = _InternetProtocol; |
1775 | using endpoint_type = typename protocol_type::endpoint; | |
1776 | using value_type = basic_resolver_entry<protocol_type>; | |
1777 | using const_reference = const value_type&; | |
1778 | using reference = value_type&; | |
1779 | using const_iterator = typename forward_list<value_type>::const_iterator; | |
1780 | using iterator = const_iterator; | |
1781 | using difference_type = ptrdiff_t; | |
1782 | using size_type = size_t; | |
e5989e71 JW |
1783 | |
1784 | // construct / copy / destroy: | |
1785 | ||
1786 | basic_resolver_results() = default; | |
1787 | ||
1788 | basic_resolver_results(const basic_resolver_results&) = default; | |
1789 | ||
1790 | basic_resolver_results(basic_resolver_results&&) noexcept = default; | |
1791 | ||
1792 | basic_resolver_results& | |
1793 | operator=(const basic_resolver_results&) = default; | |
1794 | ||
1795 | basic_resolver_results& | |
1796 | operator=(basic_resolver_results&&) = default; | |
1797 | ||
1798 | ~basic_resolver_results() = default; | |
1799 | ||
1800 | // size: | |
1801 | size_type size() const noexcept { return _M_size; } | |
1802 | size_type max_size() const noexcept { return _M_results.max_size(); } | |
d9d34449 JW |
1803 | |
1804 | _GLIBCXX_NODISCARD bool | |
1805 | empty() const noexcept { return _M_results.empty(); } | |
e5989e71 JW |
1806 | |
1807 | // element access: | |
1808 | const_iterator begin() const { return _M_results.begin(); } | |
1809 | const_iterator end() const { return _M_results.end(); } | |
1810 | const_iterator cbegin() const { return _M_results.begin(); } | |
1811 | const_iterator cend() const { return _M_results.end(); } | |
1812 | ||
1813 | // swap: | |
1814 | void | |
1815 | swap(basic_resolver_results& __that) noexcept | |
1816 | { _M_results.swap(__that._M_results); } | |
1817 | ||
1818 | private: | |
1819 | friend class basic_resolver<protocol_type>; | |
1820 | ||
1821 | basic_resolver_results(string_view, string_view, resolver_base::flags, | |
1822 | error_code&, protocol_type* = nullptr); | |
1823 | ||
1824 | basic_resolver_results(const endpoint_type&, error_code&); | |
1825 | ||
1826 | forward_list<value_type> _M_results; | |
1827 | size_t _M_size = 0; | |
1828 | }; | |
1829 | ||
1830 | template<typename _InternetProtocol> | |
1831 | inline bool | |
1832 | operator==(const basic_resolver_results<_InternetProtocol>& __a, | |
1833 | const basic_resolver_results<_InternetProtocol>& __b) | |
1834 | { | |
1835 | return __a.size() == __b.size() | |
1836 | && std::equal(__a.begin(), __a.end(), __b.begin()); | |
1837 | } | |
1838 | ||
1839 | template<typename _InternetProtocol> | |
1840 | inline bool | |
1841 | operator!=(const basic_resolver_results<_InternetProtocol>& __a, | |
1842 | const basic_resolver_results<_InternetProtocol>& __b) | |
1843 | { return !(__a == __b); } | |
1844 | ||
f0b88346 | 1845 | /// @} |
e5989e71 JW |
1846 | |
1847 | /// Perform name/address resolution. | |
1848 | template<typename _InternetProtocol> | |
1849 | class basic_resolver : public resolver_base | |
1850 | { | |
1851 | public: | |
1852 | // types: | |
1853 | ||
f26e72d8 JW |
1854 | using executor_type = io_context::executor_type; |
1855 | using protocol_type = _InternetProtocol; | |
1856 | using endpoint_type = typename _InternetProtocol::endpoint; | |
1857 | using results_type = basic_resolver_results<_InternetProtocol>; | |
e5989e71 JW |
1858 | |
1859 | // construct / copy / destroy: | |
1860 | ||
1861 | explicit basic_resolver(io_context& __ctx) : _M_ctx(&__ctx) { } | |
1862 | ||
1863 | basic_resolver(const basic_resolver&) = delete; | |
1864 | ||
1865 | basic_resolver(basic_resolver&& __rhs) noexcept | |
1866 | : _M_ctx(__rhs._M_ctx) | |
1867 | { } // TODO move state/tasks etc. | |
1868 | ||
1869 | ~basic_resolver() { cancel(); } | |
1870 | ||
1871 | basic_resolver& operator=(const basic_resolver&) = delete; | |
1872 | ||
1873 | basic_resolver& operator=(basic_resolver&& __rhs) | |
1874 | { | |
1875 | cancel(); | |
1876 | _M_ctx = __rhs._M_ctx; | |
1877 | // TODO move state/tasks etc. | |
1878 | return *this; | |
1879 | } | |
1880 | ||
1881 | // basic_resolver operations: | |
1882 | ||
1883 | executor_type get_executor() noexcept { return _M_ctx->get_executor(); } | |
1884 | ||
1885 | void cancel() { } // TODO | |
1886 | ||
1887 | results_type | |
1888 | resolve(string_view __host_name, string_view __service_name) | |
1889 | { | |
1890 | return resolve(__host_name, __service_name, resolver_base::flags(), | |
1891 | __throw_on_error{"basic_resolver::resolve"}); | |
1892 | } | |
1893 | ||
1894 | results_type | |
1895 | resolve(string_view __host_name, string_view __service_name, | |
1896 | error_code& __ec) | |
1897 | { | |
1898 | return resolve(__host_name, __service_name, resolver_base::flags(), | |
1899 | __ec); | |
1900 | } | |
1901 | ||
1902 | results_type | |
1903 | resolve(string_view __host_name, string_view __service_name, flags __f) | |
1904 | { | |
1905 | return resolve(__host_name, __service_name, __f, | |
1906 | __throw_on_error{"basic_resolver::resolve"}); | |
1907 | } | |
1908 | ||
1909 | results_type | |
1910 | resolve(string_view __host_name, string_view __service_name, flags __f, | |
1911 | error_code& __ec) | |
1912 | { return {__host_name, __service_name, __f, __ec}; } | |
1913 | ||
1914 | template<typename _CompletionToken> | |
1915 | __deduced_t<_CompletionToken, void(error_code, results_type)> | |
1916 | async_resolve(string_view __host_name, string_view __service_name, | |
1917 | _CompletionToken&& __token) | |
1918 | { | |
1919 | return async_resolve(__host_name, __service_name, | |
1920 | resolver_base::flags(), | |
1921 | forward<_CompletionToken>(__token)); | |
1922 | } | |
1923 | ||
1924 | template<typename _CompletionToken> | |
1925 | __deduced_t<_CompletionToken, void(error_code, results_type)> | |
1926 | async_resolve(string_view __host_name, string_view __service_name, | |
1927 | flags __f, _CompletionToken&& __token); // TODO | |
1928 | ||
1929 | results_type | |
1930 | resolve(const protocol_type& __protocol, | |
1931 | string_view __host_name, string_view __service_name) | |
1932 | { | |
1933 | return resolve(__protocol, __host_name, __service_name, | |
1934 | resolver_base::flags(), | |
1935 | __throw_on_error{"basic_resolver::resolve"}); | |
1936 | } | |
1937 | ||
1938 | results_type | |
1939 | resolve(const protocol_type& __protocol, | |
1940 | string_view __host_name, string_view __service_name, | |
1941 | error_code& __ec) | |
1942 | { | |
1943 | return resolve(__protocol, __host_name, __service_name, | |
1944 | resolver_base::flags(), __ec); | |
1945 | } | |
1946 | ||
1947 | results_type | |
1948 | resolve(const protocol_type& __protocol, | |
1949 | string_view __host_name, string_view __service_name, flags __f) | |
1950 | { | |
1951 | return resolve(__protocol, __host_name, __service_name, __f, | |
1952 | __throw_on_error{"basic_resolver::resolve"}); | |
1953 | } | |
1954 | ||
1955 | results_type | |
1956 | resolve(const protocol_type& __protocol, | |
1957 | string_view __host_name, string_view __service_name, | |
1958 | flags __f, error_code& __ec) | |
1959 | { return {__host_name, __service_name, __f, __ec, &__protocol}; } | |
1960 | ||
1961 | template<typename _CompletionToken> | |
1962 | __deduced_t<_CompletionToken, void(error_code, results_type)> | |
1963 | async_resolve(const protocol_type& __protocol, | |
1964 | string_view __host_name, string_view __service_name, | |
1965 | _CompletionToken&& __token) | |
1966 | { | |
1967 | return async_resolve(__protocol, __host_name, __service_name, | |
1968 | resolver_base::flags(), | |
1969 | forward<_CompletionToken>(__token)); | |
1970 | } | |
1971 | ||
1972 | template<typename _CompletionToken> | |
1973 | __deduced_t<_CompletionToken, void(error_code, results_type)> | |
1974 | async_resolve(const protocol_type& __protocol, | |
1975 | string_view __host_name, string_view __service_name, | |
1976 | flags __f, _CompletionToken&& __token); // TODO | |
1977 | ||
1978 | results_type | |
1979 | resolve(const endpoint_type& __ep) | |
1980 | { return resolve(__ep, __throw_on_error{"basic_resolver::resolve"}); } | |
1981 | ||
1982 | results_type | |
1983 | resolve(const endpoint_type& __ep, error_code& __ec) | |
1984 | { return { __ep, __ec }; } | |
1985 | ||
1986 | template<typename _CompletionToken> // TODO | |
1987 | __deduced_t<_CompletionToken, void(error_code, results_type)> | |
1988 | async_resolve(const endpoint_type& __ep, _CompletionToken&& __token); | |
1989 | ||
1990 | private: | |
1991 | io_context* _M_ctx; | |
1992 | }; | |
1993 | ||
1994 | /// Private constructor to synchronously resolve host and service names. | |
1995 | template<typename _InternetProtocol> | |
1996 | basic_resolver_results<_InternetProtocol>:: | |
1997 | basic_resolver_results(string_view __host_name, string_view __service_name, | |
1998 | resolver_base::flags __f, error_code& __ec, | |
1999 | protocol_type* __protocol) | |
2000 | { | |
82a0f2fd | 2001 | #ifdef _GLIBCXX_HAVE_NETDB_H |
e5989e71 JW |
2002 | string __host; |
2003 | const char* __h = __host_name.data() | |
2004 | ? (__host = __host_name.to_string()).c_str() | |
2005 | : nullptr; | |
2006 | string __svc; | |
2007 | const char* __s = __service_name.data() | |
2008 | ? (__svc = __service_name.to_string()).c_str() | |
2009 | : nullptr; | |
2010 | ||
2011 | ::addrinfo __hints{ }; | |
2012 | __hints.ai_flags = static_cast<int>(__f); | |
2013 | if (__protocol) | |
2014 | { | |
2015 | __hints.ai_family = __protocol->family(); | |
2016 | __hints.ai_socktype = __protocol->type(); | |
2017 | __hints.ai_protocol = __protocol->protocol(); | |
2018 | } | |
2019 | else | |
2020 | { | |
2021 | auto __p = endpoint_type{}.protocol(); | |
2022 | __hints.ai_family = AF_UNSPEC; | |
2023 | __hints.ai_socktype = __p.type(); | |
2024 | __hints.ai_protocol = __p.protocol(); | |
2025 | } | |
2026 | ||
2027 | struct __scoped_addrinfo | |
2028 | { | |
2029 | ~__scoped_addrinfo() { if (_M_p) ::freeaddrinfo(_M_p); } | |
2030 | ::addrinfo* _M_p = nullptr; | |
2031 | } __sai; | |
2032 | ||
2033 | if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p)) | |
2034 | { | |
feec7ef6 | 2035 | __ec = ip::__make_resolver_error_code(__err, errno); |
e5989e71 JW |
2036 | return; |
2037 | } | |
2038 | __ec.clear(); | |
2039 | ||
2040 | endpoint_type __ep; | |
2041 | auto __tail = _M_results.before_begin(); | |
2042 | for (auto __ai = __sai._M_p; __ai != nullptr; __ai = __ai->ai_next) | |
2043 | { | |
2044 | if (__ai->ai_family == AF_INET || __ai->ai_family == AF_INET6) | |
2045 | { | |
2046 | if (__ai->ai_addrlen <= __ep.capacity()) | |
82a0f2fd | 2047 | __builtin_memcpy(__ep.data(), __ai->ai_addr, __ai->ai_addrlen); |
e5989e71 JW |
2048 | __ep.resize(__ai->ai_addrlen); |
2049 | __tail = _M_results.emplace_after(__tail, __ep, __host, __svc); | |
2050 | _M_size++; | |
2051 | } | |
2052 | } | |
82a0f2fd JW |
2053 | #else |
2054 | __ec = std::make_error_code(errc::operation_not_supported); | |
2055 | #endif | |
e5989e71 JW |
2056 | } |
2057 | ||
2058 | /// Private constructor to synchronously resolve an endpoint. | |
2059 | template<typename _InternetProtocol> | |
2060 | basic_resolver_results<_InternetProtocol>:: | |
2061 | basic_resolver_results(const endpoint_type& __ep, error_code& __ec) | |
2062 | { | |
82a0f2fd | 2063 | #ifdef _GLIBCXX_HAVE_NETDB_H |
feec7ef6 JW |
2064 | char __host_name[1025]; // glibc NI_MAXHOST |
2065 | char __service_name[32]; // glibc NI_MAXSERV | |
e5989e71 JW |
2066 | int __flags = 0; |
2067 | if (__ep.protocol().type() == SOCK_DGRAM) | |
2068 | __flags |= NI_DGRAM; | |
2069 | auto __sa = static_cast<const sockaddr*>(__ep.data()); | |
2070 | int __err = ::getnameinfo(__sa, __ep.size(), | |
2071 | __host_name, sizeof(__host_name), | |
2072 | __service_name, sizeof(__service_name), | |
2073 | __flags); | |
2074 | if (__err) | |
2075 | { | |
2076 | __flags |= NI_NUMERICSERV; | |
2077 | __err = ::getnameinfo(__sa, __ep.size(), | |
2078 | __host_name, sizeof(__host_name), | |
2079 | __service_name, sizeof(__service_name), | |
2080 | __flags); | |
2081 | } | |
2082 | if (__err) | |
feec7ef6 | 2083 | __ec = ip::__make_resolver_error_code(__err, errno); |
e5989e71 JW |
2084 | else |
2085 | { | |
2086 | __ec.clear(); | |
2087 | _M_results.emplace_front(__ep, __host_name, __service_name); | |
2088 | _M_size = 1; | |
2089 | } | |
82a0f2fd JW |
2090 | #else |
2091 | __ec = std::make_error_code(errc::operation_not_supported); | |
2092 | #endif | |
e5989e71 JW |
2093 | } |
2094 | ||
2095 | /** The name of the local host. | |
2096 | * @{ | |
2097 | */ | |
2098 | ||
2099 | template<typename _Allocator> | |
2100 | __string_with<_Allocator> | |
2101 | host_name(const _Allocator& __a, error_code& __ec) | |
2102 | { | |
2103 | #ifdef HOST_NAME_MAX | |
2104 | constexpr size_t __maxlen = HOST_NAME_MAX; | |
2105 | #else | |
2106 | constexpr size_t __maxlen = 256; | |
2107 | #endif | |
2108 | char __buf[__maxlen + 1]; | |
2109 | if (::gethostname(__buf, __maxlen) == -1) | |
2110 | __ec.assign(errno, generic_category()); | |
2111 | __buf[__maxlen] = '\0'; | |
2112 | return { __buf, __a }; | |
2113 | } | |
2114 | ||
2115 | template<typename _Allocator> | |
2116 | inline __string_with<_Allocator> | |
2117 | host_name(const _Allocator& __a) | |
2118 | { return host_name(__a, __throw_on_error{"host_name"}); } | |
2119 | ||
2120 | inline string | |
2121 | host_name(error_code& __ec) | |
2122 | { return host_name(std::allocator<char>{}, __ec); } | |
2123 | ||
2124 | inline string | |
2125 | host_name() | |
2126 | { return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); } | |
2127 | ||
f0b88346 | 2128 | /// @} |
e5989e71 | 2129 | |
9ee35a86 | 2130 | #ifdef IPPROTO_TCP |
e5989e71 JW |
2131 | /// The TCP byte-stream protocol. |
2132 | class tcp | |
2133 | { | |
2134 | public: | |
2135 | // types: | |
f26e72d8 JW |
2136 | using endpoint = basic_endpoint<tcp>; ///< A TCP endpoint. |
2137 | using resolver = basic_resolver<tcp>; ///< A TCP resolver. | |
2138 | using socket = basic_stream_socket<tcp>; ///< A TCP socket. | |
2139 | using acceptor = basic_socket_acceptor<tcp>; ///< A TCP acceptor. | |
2140 | using iostream = basic_socket_iostream<tcp>; /// A TCP iostream. | |
e5989e71 | 2141 | |
9ee35a86 | 2142 | #ifdef TCP_NODELAY |
e5989e71 JW |
2143 | /// Disable coalescing of small segments (i.e. the Nagle algorithm). |
2144 | struct no_delay : __sockopt_crtp<no_delay, bool> | |
2145 | { | |
2146 | using __sockopt_crtp::__sockopt_crtp; | |
2e0b1c6c | 2147 | using __sockopt_crtp::operator=; |
e5989e71 JW |
2148 | |
2149 | static const int _S_level = IPPROTO_TCP; | |
2150 | static const int _S_name = TCP_NODELAY; | |
2151 | }; | |
82a0f2fd | 2152 | #endif |
e5989e71 JW |
2153 | |
2154 | // static members: | |
2155 | ||
2156 | /// A protocol object representing IPv4 TCP. | |
2157 | static constexpr tcp v4() noexcept { return tcp(AF_INET); } | |
2158 | /// A protocol object representing IPv6 TCP. | |
2159 | static constexpr tcp v6() noexcept { return tcp(AF_INET6); } | |
2160 | ||
2161 | tcp() = delete; | |
2162 | ||
2163 | constexpr int family() const noexcept { return _M_family; } | |
2164 | constexpr int type() const noexcept { return SOCK_STREAM; } | |
2165 | constexpr int protocol() const noexcept { return IPPROTO_TCP; } | |
2166 | ||
2167 | private: | |
2168 | constexpr explicit tcp(int __family) : _M_family(__family) { } | |
2169 | ||
2170 | int _M_family; | |
2171 | }; | |
2172 | ||
2173 | /** tcp comparisons | |
2174 | * @{ | |
2175 | */ | |
2176 | ||
39073938 JW |
2177 | constexpr bool |
2178 | operator==(const tcp& __a, const tcp& __b) noexcept | |
e5989e71 JW |
2179 | { return __a.family() == __b.family(); } |
2180 | ||
39073938 JW |
2181 | constexpr bool |
2182 | operator!=(const tcp& __a, const tcp& __b) noexcept | |
e5989e71 JW |
2183 | { return !(__a == __b); } |
2184 | ||
f0b88346 | 2185 | /// @} |
9ee35a86 | 2186 | #endif // IPPROTO_TCP |
e5989e71 | 2187 | |
9ee35a86 | 2188 | #ifdef IPPROTO_UDP |
e5989e71 JW |
2189 | /// The UDP datagram protocol. |
2190 | class udp | |
2191 | { | |
2192 | public: | |
2193 | // types: | |
f26e72d8 JW |
2194 | using endpoint = basic_endpoint<udp>; |
2195 | using resolver = basic_resolver<udp>; | |
2196 | using socket = basic_datagram_socket<udp>; | |
e5989e71 JW |
2197 | |
2198 | // static members: | |
2199 | static constexpr udp v4() noexcept { return udp(AF_INET); } | |
2200 | static constexpr udp v6() noexcept { return udp(AF_INET6); } | |
2201 | ||
2202 | udp() = delete; | |
2203 | ||
2204 | constexpr int family() const noexcept { return _M_family; } | |
2205 | constexpr int type() const noexcept { return SOCK_DGRAM; } | |
2206 | constexpr int protocol() const noexcept { return IPPROTO_UDP; } | |
2207 | ||
2208 | private: | |
2209 | constexpr explicit udp(int __family) : _M_family(__family) { } | |
2210 | ||
2211 | int _M_family; | |
2212 | }; | |
2213 | ||
2214 | /** udp comparisons | |
2215 | * @{ | |
2216 | */ | |
2217 | ||
39073938 JW |
2218 | constexpr bool |
2219 | operator==(const udp& __a, const udp& __b) noexcept | |
e5989e71 JW |
2220 | { return __a.family() == __b.family(); } |
2221 | ||
39073938 JW |
2222 | constexpr bool |
2223 | operator!=(const udp& __a, const udp& __b) noexcept | |
e5989e71 JW |
2224 | { return !(__a == __b); } |
2225 | ||
f0b88346 | 2226 | /// @} |
9ee35a86 JW |
2227 | #endif // IPPROTO_UDP |
2228 | ||
2229 | #if defined IPPROTO_IP && defined IPPROTO_IPV6 | |
e5989e71 JW |
2230 | |
2231 | /// Restrict a socket created for an IPv6 protocol to IPv6 only. | |
2e0b1c6c | 2232 | class v6_only : public __sockopt_crtp<v6_only, bool> |
e5989e71 | 2233 | { |
2e0b1c6c | 2234 | public: |
e5989e71 | 2235 | using __sockopt_crtp::__sockopt_crtp; |
2e0b1c6c | 2236 | using __sockopt_crtp::operator=; |
e5989e71 | 2237 | |
2e0b1c6c JW |
2238 | private: |
2239 | friend __sockopt_crtp<v6_only, bool>; | |
e5989e71 JW |
2240 | static const int _S_level = IPPROTO_IPV6; |
2241 | static const int _S_name = IPV6_V6ONLY; | |
2242 | }; | |
2243 | ||
a70a4be9 JW |
2244 | namespace unicast |
2245 | { | |
e5989e71 | 2246 | /// Set the default number of hops (TTL) for outbound datagrams. |
2e0b1c6c | 2247 | class hops : public __sockopt_crtp<hops> |
e5989e71 | 2248 | { |
2e0b1c6c | 2249 | public: |
e5989e71 | 2250 | using __sockopt_crtp::__sockopt_crtp; |
2e0b1c6c | 2251 | using __sockopt_crtp::operator=; |
e5989e71 JW |
2252 | |
2253 | template<typename _Protocol> | |
2254 | int | |
2255 | level(const _Protocol& __p) const noexcept | |
2256 | { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; } | |
2257 | ||
2258 | template<typename _Protocol> | |
2259 | int | |
2260 | name(const _Protocol& __p) const noexcept | |
2261 | { return __p.family() == AF_INET6 ? IPV6_UNICAST_HOPS : IP_TTL; } | |
2262 | }; | |
e5989e71 JW |
2263 | } // namespace unicast |
2264 | ||
a70a4be9 JW |
2265 | namespace multicast |
2266 | { | |
2e0b1c6c | 2267 | class __mcastopt |
e5989e71 | 2268 | { |
2e0b1c6c | 2269 | public: |
e5989e71 | 2270 | explicit |
2e0b1c6c JW |
2271 | __mcastopt(const address& __grp) noexcept |
2272 | : __mcastopt(__grp.is_v4() ? __mcastopt(__grp.to_v4()) : __mcastopt(__grp.to_v6())) | |
2273 | { } | |
e5989e71 JW |
2274 | |
2275 | explicit | |
2e0b1c6c JW |
2276 | __mcastopt(const address_v4& __grp, |
2277 | const address_v4& __iface = address_v4::any()) noexcept | |
2278 | { | |
2279 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
2280 | _M_v4.imr_multiaddr.s_addr = __grp.to_uint(); | |
2281 | _M_v4.imr_interface.s_addr = __iface.to_uint(); | |
2282 | #else | |
2283 | _M_v4.imr_multiaddr.s_addr = __builtin_bswap32(__grp.to_uint()); | |
2284 | _M_v4.imr_interface.s_addr = __builtin_bswap32(__iface.to_uint()); | |
2285 | #endif | |
2286 | } | |
e5989e71 JW |
2287 | |
2288 | explicit | |
2e0b1c6c JW |
2289 | __mcastopt(const address_v6& __grp, unsigned int __iface = 0) noexcept |
2290 | { | |
2291 | const auto __addr = __grp.to_bytes(); | |
2292 | __builtin_memcpy(_M_v6.ipv6mr_multiaddr.s6_addr, __addr.data(), 16); | |
2293 | _M_v6.ipv6mr_interface = __iface; | |
2294 | } | |
e5989e71 JW |
2295 | |
2296 | template<typename _Protocol> | |
2297 | int | |
2298 | level(const _Protocol& __p) const noexcept | |
2299 | { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; } | |
2300 | ||
e5989e71 JW |
2301 | template<typename _Protocol> |
2302 | const void* | |
2e0b1c6c JW |
2303 | data(const _Protocol& __p) const noexcept |
2304 | { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; } | |
e5989e71 JW |
2305 | |
2306 | template<typename _Protocol> | |
2307 | size_t | |
2308 | size(const _Protocol& __p) const noexcept | |
2e0b1c6c | 2309 | { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); } |
e5989e71 | 2310 | |
2e0b1c6c JW |
2311 | private: |
2312 | ipv6_mreq _M_v6 = {}; | |
2313 | ip_mreq _M_v4 = {}; | |
e5989e71 JW |
2314 | }; |
2315 | ||
2e0b1c6c JW |
2316 | /// Request that a socket joins a multicast group. |
2317 | class join_group : private __mcastopt | |
e5989e71 | 2318 | { |
2e0b1c6c JW |
2319 | public: |
2320 | using __mcastopt::__mcastopt; | |
2321 | using __mcastopt::level; | |
2322 | using __mcastopt::data; | |
2323 | using __mcastopt::size; | |
e5989e71 JW |
2324 | |
2325 | template<typename _Protocol> | |
2326 | int | |
2327 | name(const _Protocol& __p) const noexcept | |
2328 | { | |
2e0b1c6c JW |
2329 | if (__p.family() == AF_INET6) |
2330 | return IPV6_JOIN_GROUP; | |
2331 | return IP_ADD_MEMBERSHIP; | |
e5989e71 | 2332 | } |
2e0b1c6c | 2333 | }; |
e5989e71 | 2334 | |
2e0b1c6c JW |
2335 | /// Request that a socket leaves a multicast group. |
2336 | class leave_group : private __mcastopt | |
2337 | { | |
2338 | public: | |
2339 | using __mcastopt::__mcastopt; | |
2340 | using __mcastopt::level; | |
2341 | using __mcastopt::data; | |
2342 | using __mcastopt::size; | |
e5989e71 JW |
2343 | |
2344 | template<typename _Protocol> | |
2e0b1c6c JW |
2345 | int |
2346 | name(const _Protocol& __p) const noexcept | |
e5989e71 | 2347 | { |
2e0b1c6c JW |
2348 | if (__p.family() == AF_INET6) |
2349 | return IPV6_LEAVE_GROUP; | |
2350 | return IP_DROP_MEMBERSHIP; | |
e5989e71 | 2351 | } |
e5989e71 JW |
2352 | }; |
2353 | ||
2354 | /// Specify the network interface for outgoing multicast datagrams. | |
2355 | class outbound_interface | |
2356 | { | |
2e0b1c6c | 2357 | public: |
e5989e71 | 2358 | explicit |
2e0b1c6c JW |
2359 | outbound_interface(const address_v4& __v4) noexcept |
2360 | { | |
2361 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
2362 | _M_v4.s_addr = __v4.to_uint(); | |
2363 | #else | |
2364 | _M_v4.s_addr = __builtin_bswap32(__v4.to_uint()); | |
2365 | #endif | |
2366 | } | |
e5989e71 JW |
2367 | |
2368 | explicit | |
2e0b1c6c JW |
2369 | outbound_interface(unsigned int __v6) noexcept |
2370 | : _M_v4(), _M_v6(__v6) | |
2371 | { } | |
e5989e71 JW |
2372 | |
2373 | template<typename _Protocol> | |
2374 | int | |
2375 | level(const _Protocol& __p) const noexcept | |
2376 | { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; } | |
2377 | ||
2378 | template<typename _Protocol> | |
2379 | int | |
2380 | name(const _Protocol& __p) const noexcept | |
2381 | { | |
2382 | return __p.family() == AF_INET6 | |
2383 | ? IPV6_MULTICAST_IF : IP_MULTICAST_IF; | |
2384 | } | |
2385 | ||
2386 | template<typename _Protocol> | |
2387 | const void* | |
2e0b1c6c JW |
2388 | data(const _Protocol& __p) const noexcept |
2389 | { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; } | |
e5989e71 JW |
2390 | |
2391 | template<typename _Protocol> | |
2392 | size_t | |
2393 | size(const _Protocol& __p) const noexcept | |
2e0b1c6c | 2394 | { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); } |
e5989e71 | 2395 | |
2e0b1c6c JW |
2396 | private: |
2397 | in_addr _M_v4; | |
2398 | unsigned _M_v6 = 0; | |
e5989e71 JW |
2399 | }; |
2400 | ||
2401 | /// Set the default number of hops (TTL) for outbound datagrams. | |
2e0b1c6c | 2402 | class hops : public __sockopt_crtp<hops> |
e5989e71 | 2403 | { |
2e0b1c6c | 2404 | public: |
e5989e71 | 2405 | using __sockopt_crtp::__sockopt_crtp; |
2e0b1c6c | 2406 | using __sockopt_crtp::operator=; |
e5989e71 JW |
2407 | |
2408 | template<typename _Protocol> | |
2409 | int | |
2410 | level(const _Protocol& __p) const noexcept | |
2411 | { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; } | |
2412 | ||
2413 | template<typename _Protocol> | |
2414 | int | |
2415 | name(const _Protocol& __p) const noexcept | |
2416 | { | |
2417 | return __p.family() == AF_INET6 | |
2418 | ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL; | |
2419 | } | |
2420 | }; | |
2421 | ||
2422 | /// Set whether datagrams are delivered back to the local application. | |
2e0b1c6c | 2423 | class enable_loopback : public __sockopt_crtp<enable_loopback, bool> |
e5989e71 | 2424 | { |
2e0b1c6c | 2425 | public: |
e5989e71 | 2426 | using __sockopt_crtp::__sockopt_crtp; |
2e0b1c6c | 2427 | using __sockopt_crtp::operator=; |
e5989e71 JW |
2428 | |
2429 | template<typename _Protocol> | |
2430 | int | |
2431 | level(const _Protocol& __p) const noexcept | |
2432 | { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; } | |
2433 | ||
2434 | template<typename _Protocol> | |
2435 | int | |
2436 | name(const _Protocol& __p) const noexcept | |
2437 | { | |
2438 | return __p.family() == AF_INET6 | |
2439 | ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP; | |
2440 | } | |
2441 | }; | |
2442 | ||
e5989e71 JW |
2443 | } // namespace multicast |
2444 | ||
9ee35a86 JW |
2445 | #endif // IPPROTO_IP && IPPROTO_IPV6 |
2446 | ||
f0b88346 | 2447 | /// @} |
e5989e71 JW |
2448 | |
2449 | } // namespace ip | |
2450 | } // namespace v1 | |
2451 | } // namespace net | |
2452 | } // namespace experimental | |
2453 | ||
2454 | template<> | |
2455 | struct is_error_condition_enum<experimental::net::v1::ip::resolver_errc> | |
2456 | : public true_type {}; | |
2457 | ||
2458 | // hash support | |
2459 | template<typename _Tp> struct hash; | |
2460 | template<> | |
2461 | struct hash<experimental::net::v1::ip::address> | |
2462 | : __hash_base<size_t, experimental::net::v1::ip::address> | |
2463 | { | |
2464 | size_t | |
d7aa21a3 | 2465 | operator()(const experimental::net::v1::ip::address& __a) const |
e5989e71 JW |
2466 | { |
2467 | if (__a.is_v4()) | |
2468 | return _Hash_impl::hash(__a.to_v4()); | |
2469 | else | |
2470 | return _Hash_impl::hash(__a.to_v6()); | |
2471 | } | |
2472 | }; | |
2473 | ||
2474 | template<> | |
2475 | struct hash<experimental::net::v1::ip::address_v4> | |
2476 | : __hash_base<size_t, experimental::net::v1::ip::address_v4> | |
2477 | { | |
2478 | size_t | |
d7aa21a3 | 2479 | operator()(const experimental::net::v1::ip::address_v4& __a) const |
e5989e71 JW |
2480 | { return _Hash_impl::hash(__a.to_bytes()); } |
2481 | }; | |
2482 | ||
2483 | template<> struct hash<experimental::net::v1::ip::address_v6> | |
2484 | : __hash_base<size_t, experimental::net::v1::ip::address_v6> | |
2485 | { | |
2486 | size_t | |
d7aa21a3 | 2487 | operator()(const experimental::net::v1::ip::address_v6& __a) const |
e5989e71 JW |
2488 | { return _Hash_impl::hash(__a.to_bytes()); } |
2489 | }; | |
2490 | ||
a70a4be9 | 2491 | _GLIBCXX_END_NAMESPACE_VERSION |
e5989e71 JW |
2492 | } // namespace std |
2493 | ||
2494 | #endif // C++14 | |
2495 | ||
2496 | #endif // _GLIBCXX_EXPERIMENTAL_INTERNET |