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