1 // <experimental/internet> -*- C++ -*-
3 // Copyright (C) 2015-2021 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file experimental/internet
26 * This is a TS C++ Library header.
27 * @ingroup networking-ts
30 #ifndef _GLIBCXX_EXPERIMENTAL_INTERNET
31 #define _GLIBCXX_EXPERIMENTAL_INTERNET
33 #pragma GCC system_header
35 #if __cplusplus >= 201402L
37 #include <experimental/netfwd>
38 #include <experimental/io_context>
39 #include <experimental/bits/net.h>
41 #include <forward_list>
44 #include <experimental/string_view>
45 #ifdef _GLIBCXX_HAVE_UNISTD_H
48 #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
49 # include <sys/socket.h> // AF_INET, AF_INET6, SOCK_DGRAM, SOCK_STREAM
51 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
52 # include <arpa/inet.h> // inet_ntop
54 #ifdef _GLIBCXX_HAVE_NETINET_IN_H
55 # include <netinet/in.h> // IPPROTO_IP, IPPROTO_IPV6, in_addr, in6_addr
57 #ifdef _GLIBCXX_HAVE_NETINET_TCP_H
58 # include <netinet/tcp.h> // TCP_NODELAY
60 #ifdef _GLIBCXX_HAVE_NETDB_H
61 # include <netdb.h> // getaddrinfo etc.
64 #if defined _WIN32 && __has_include(<ws2tcpip.h>)
65 # include <ws2tcpip.h>
68 namespace std _GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 namespace experimental
79 /** @addtogroup networking-ts
83 /** Error codes for resolver errors.
87 enum class resolver_errc : int {
88 #ifdef _GLIBCXX_HAVE_NETDB_H
89 host_not_found = EAI_NONAME,
90 host_not_found_try_again = EAI_AGAIN,
91 service_not_found = EAI_SERVICE
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
101 /// Error category for resolver errors.
102 inline const error_category& resolver_category() noexcept // TODO non-inline
104 struct __cat : error_category
106 const char* name() const noexcept { return "resolver"; }
107 std::string message(int __e) const {
108 #ifdef _GLIBCXX_HAVE_NETDB_H
109 return ::gai_strerror(__e);
111 return "name resolution requires <netdb.h>";
114 virtual void __message(int) { } // TODO dual ABI XXX
120 inline error_code make_error_code(resolver_errc __e) noexcept
121 { return error_code(static_cast<int>(__e), resolver_category()); }
123 inline error_condition make_error_condition(resolver_errc __e) noexcept
124 { return error_condition(static_cast<int>(__e), resolver_category()); }
126 /// @cond undocumented
128 __make_resolver_error_code(int __ai_err,
129 [[__maybe_unused__]] int __sys_err) noexcept
132 if (__builtin_expect(__ai_err == EAI_SYSTEM, 0))
133 return error_code(__sys_err, std::generic_category());
135 return error_code(__ai_err, resolver_category());
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.
144 /// Convenience alias for constraining allocators for strings.
145 template<typename _Alloc>
147 = enable_if_t<std::is_same<typename _Alloc::value_type, char>::value,
148 std::basic_string<char, std::char_traits<char>, _Alloc>>;
151 __unsupported_err() noexcept
153 #if defined EAFNOSUPPORT
154 return std::errc::address_family_not_supported;
156 return std::errc::operation_not_supported;
160 /** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses.
164 struct v4_mapped_t {};
165 constexpr v4_mapped_t v4_mapped;
174 using uint_type = uint_least32_t;
176 struct bytes_type : array<unsigned char, 4>
178 template<typename... _Tp>
180 bytes_type(_Tp... __tp)
181 : array<unsigned char, 4>{{static_cast<unsigned char>(__tp)...}}
184 for (auto __b : *this)
186 __throw_out_of_range("invalid address_v4::bytes_type value");
192 constexpr address_v4() noexcept : _M_addr(0) { }
194 constexpr address_v4(const address_v4& a) noexcept = default;
197 address_v4(const bytes_type& __b)
198 : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
202 address_v4(uint_type __val) : _M_addr(_S_hton_32(__val))
204 #if UINT_LEAST32_MAX > 0xFFFFFFFF
205 if (__val > 0xFFFFFFFF)
206 __throw_out_of_range("invalid address_v4::uint_type value");
211 address_v4& operator=(const address_v4& a) noexcept = default;
214 constexpr bool is_unspecified() const noexcept { return to_uint() == 0; }
217 is_loopback() const noexcept
218 { return (to_uint() & 0xFF000000) == 0x7F000000; }
221 is_multicast() const noexcept
222 { return (to_uint() & 0xF0000000) == 0xE0000000; }
225 to_bytes() const noexcept
228 (_M_addr >> 24) & 0xFF,
229 (_M_addr >> 16) & 0xFF,
230 (_M_addr >> 8) & 0xFF,
236 to_uint() const noexcept { return _S_ntoh_32(_M_addr); }
238 template<typename _Allocator = allocator<char>>
239 __string_with<_Allocator>
240 to_string(const _Allocator& __a = _Allocator()) const
242 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
243 __string_with<_Allocator> __str(__a);
244 __str.resize(INET_ADDRSTRLEN);
245 if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
246 __str.erase(__str.find('\0'));
251 std::__throw_system_error((int)__unsupported_err());
256 static constexpr address_v4 any() noexcept { return address_v4{}; }
259 address_v4 loopback() noexcept { return address_v4{0x7F000001}; }
262 address_v4 broadcast() noexcept { return address_v4{0xFFFFFFFF}; }
265 template<typename _InternetProtocol>
266 friend class basic_endpoint;
268 friend address_v4 make_address_v4(const char*, error_code&) noexcept;
270 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
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; }
276 static constexpr uint16_t
277 _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); }
279 static constexpr uint16_t
280 _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); }
282 static constexpr uint32_t
283 _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); }
285 static constexpr uint32_t
286 _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); }
289 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
290 in_addr_t _M_addr; // network byte order
301 struct bytes_type : array<unsigned char, 16>
303 template<typename... _Tp>
305 bytes_type(_Tp... __t)
306 : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}}
311 constexpr address_v6() noexcept : _M_bytes(), _M_scope_id() { }
313 constexpr address_v6(const address_v6& __a) noexcept = default;
316 address_v6(const bytes_type& __bytes, scope_id_type __scope = 0)
317 : _M_bytes(__bytes), _M_scope_id(__scope)
321 address_v6& operator=(const address_v6& __a) noexcept = default;
324 void scope_id(scope_id_type __id) noexcept { _M_scope_id = __id; }
326 constexpr scope_id_type scope_id() const noexcept { return _M_scope_id; }
329 is_unspecified() const noexcept
331 for (int __i = 0; __i < 16; ++__i)
332 if (_M_bytes[__i] != 0x00)
334 return _M_scope_id == 0;
338 is_loopback() const noexcept
340 for (int __i = 0; __i < 15; ++__i)
341 if (_M_bytes[__i] != 0x00)
343 return _M_bytes[15] == 0x01 && _M_scope_id == 0;
347 is_multicast() const noexcept { return _M_bytes[0] == 0xFF; }
350 is_link_local() const noexcept
351 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0x80; }
354 is_site_local() const noexcept
355 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0xC0; }
358 is_v4_mapped() const noexcept
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;
367 is_multicast_node_local() const noexcept
368 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x01; }
371 is_multicast_link_local() const noexcept
372 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x02; }
375 is_multicast_site_local() const noexcept
376 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x05; }
379 is_multicast_org_local() const noexcept
380 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x08; }
383 is_multicast_global() const noexcept
384 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x0b; }
386 constexpr bytes_type to_bytes() const noexcept { return _M_bytes; }
388 template<typename _Allocator = allocator<char>>
389 __string_with<_Allocator>
390 to_string(const _Allocator& __a = _Allocator()) const
392 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
393 __string_with<_Allocator> __str(__a);
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()))
398 auto __end = __str.find('\0');
399 if (unsigned long __scope = _M_scope_id)
402 #if _GLIBCXX_USE_C99_STDIO
403 __builtin_snprintf(__p + __end, __str.size() - __end,
406 __builtin_sprintf(__p + __end, "%%%lu", __scope);
415 std::__throw_system_error((int)__unsupported_err());
421 static constexpr address_v6
427 static constexpr address_v6
430 return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
434 template<typename _InternetProtocol>
435 friend class basic_endpoint;
437 friend constexpr bool
438 operator==(const address_v6&, const address_v6&) noexcept;
440 friend constexpr bool
441 operator< (const address_v6&, const address_v6&) noexcept;
444 scope_id_type _M_scope_id;
447 /// Exception type thrown on misuse of IPv4 addresses as IPv6 or vice versa.
448 class bad_address_cast : public bad_cast
451 bad_address_cast() { }
453 const char* what() const noexcept { return "bad address cast"; }
456 /// An IPv4 or IPv6 address.
461 constexpr address() noexcept : _M_v4(), _M_is_v4(true) { }
463 #if __cpp_constexpr_dynamic_alloc
466 address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4)
469 std::_Construct(std::addressof(_M_v4), __a.to_v4());
471 std::_Construct(std::addressof(_M_v6), __a.to_v6());
475 address(const address_v4& __a) noexcept : _M_v4(__a), _M_is_v4(true) { }
478 address(const address_v6& __a) noexcept : _M_v6(__a), _M_is_v4(false) { }
482 operator=(const address& __a) noexcept
492 operator=(const address_v4& __a) noexcept
494 std::_Construct(std::addressof(_M_v4), __a);
500 operator=(const address_v6& __a) noexcept
502 std::_Construct(std::addressof(_M_v6), __a);
509 constexpr bool is_v4() const noexcept { return _M_is_v4; }
510 constexpr bool is_v6() const noexcept { return !_M_is_v4; }
516 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
524 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
529 is_unspecified() const noexcept
530 { return _M_is_v4 ? _M_v4.is_unspecified() : _M_v6.is_unspecified(); }
533 is_loopback() const noexcept
534 { return _M_is_v4 ? _M_v4.is_loopback() : _M_v6.is_loopback(); }
537 is_multicast() const noexcept
538 { return _M_is_v4 ? _M_v4.is_multicast() : _M_v6.is_multicast(); }
540 template<typename _Allocator = allocator<char>>
541 __string_with<_Allocator>
542 to_string(const _Allocator& __a = _Allocator()) const
545 return to_v4().to_string(__a);
546 return to_v6().to_string(__a);
550 template<typename _InternetProtocol>
551 friend class basic_endpoint;
553 friend constexpr bool
554 operator==(const address&, const address&) noexcept;
556 friend constexpr bool
557 operator<(const address&, const address&) noexcept;
567 /** ip::address_v4 comparisons
572 operator==(const address_v4& __a, const address_v4& __b) noexcept
573 { return __a.to_uint() == __b.to_uint(); }
576 operator!=(const address_v4& __a, const address_v4& __b) noexcept
577 { return !(__a == __b); }
580 operator< (const address_v4& __a, const address_v4& __b) noexcept
581 { return __a.to_uint() < __b.to_uint(); }
584 operator> (const address_v4& __a, const address_v4& __b) noexcept
585 { return __b < __a; }
588 operator<=(const address_v4& __a, const address_v4& __b) noexcept
589 { return !(__b < __a); }
592 operator>=(const address_v4& __a, const address_v4& __b) noexcept
593 { return !(__a < __b); }
597 /** ip::address_v6 comparisons
602 operator==(const address_v6& __a, const address_v6& __b) noexcept
604 const auto& __aa = __a._M_bytes;
605 const auto& __bb = __b._M_bytes;
607 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
609 return __i == 16 ? __a.scope_id() == __b.scope_id() : false;
613 operator!=(const address_v6& __a, const address_v6& __b) noexcept
614 { return !(__a == __b); }
617 operator< (const address_v6& __a, const address_v6& __b) noexcept
619 const auto& __aa = __a._M_bytes;
620 const auto& __bb = __b._M_bytes;
622 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
624 return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i];
628 operator> (const address_v6& __a, const address_v6& __b) noexcept
629 { return __b < __a; }
632 operator<=(const address_v6& __a, const address_v6& __b) noexcept
633 { return !(__b < __a); }
636 operator>=(const address_v6& __a, const address_v6& __b) noexcept
637 { return !(__a < __b); }
641 /** ip::address comparisons
646 operator==(const address& __a, const address& __b) noexcept
649 return __b.is_v4() ? __a._M_v4 == __b._M_v4 : false;
650 return __b.is_v4() ? false : __a._M_v6 == __b._M_v6;
654 operator!=(const address& __a, const address& __b) noexcept
655 { return !(__a == __b); }
658 operator< (const address& __a, const address& __b) noexcept
661 return __b.is_v4() ? __a._M_v4 < __b._M_v4 : true;
662 return __b.is_v4() ? false : __a._M_v6 < __b._M_v6;
666 operator> (const address& __a, const address& __b) noexcept
667 { return __b < __a; }
670 operator<=(const address& __a, const address& __b) noexcept
671 { return !(__b < __a); }
674 operator>=(const address& __a, const address& __b) noexcept
675 { return !(__a < __b); }
679 /** ip::address_v4 creation
684 make_address_v4(const address_v4::bytes_type& __b)
685 { return address_v4{__b}; }
688 make_address_v4(address_v4::uint_type __val)
689 { return address_v4{__val}; }
692 make_address_v4(v4_mapped_t, const address_v6& __a)
694 if (!__a.is_v4_mapped())
695 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
697 const auto __v6b = __a.to_bytes();
698 return address_v4::bytes_type(__v6b[12], __v6b[13], __v6b[14], __v6b[15]);
702 make_address_v4(const char* __str, error_code& __ec) noexcept
704 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
706 const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr);
713 __ec = std::make_error_code(std::errc::invalid_argument);
715 __ec.assign(errno, generic_category());
717 __ec = std::make_error_code(__unsupported_err());
723 make_address_v4(const char* __str)
724 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
727 make_address_v4(const string& __str, error_code& __ec) noexcept
728 { return make_address_v4(__str.c_str(), __ec); }
731 make_address_v4(const string& __str)
732 { return make_address_v4(__str.c_str()); }
735 make_address_v4(string_view __str, error_code& __ec) noexcept
737 char __buf[16]; // INET_ADDRSTRLEN isn't defined on Windows
738 auto __len = __str.copy(__buf, sizeof(__buf));
739 if (__len == sizeof(__buf))
741 __ec = std::make_error_code(std::errc::invalid_argument);
746 return make_address_v4(__buf, __ec);
750 make_address_v4(string_view __str)
751 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
755 /** ip::address_v6 creation
760 make_address_v6(const address_v6::bytes_type& __b, scope_id_type __scope = 0)
761 { return address_v6{__b, __scope}; }
764 make_address_v6(v4_mapped_t, const address_v4& __a) noexcept
766 const address_v4::bytes_type __v4b = __a.to_bytes();
767 address_v6::bytes_type __v6b(0, 0, 0, 0, 0, 0, 0, 0,
769 __v4b[0], __v4b[1], __v4b[2], __v4b[3]);
770 return address_v6(__v6b);
774 __make_address_v6(const char* __addr, const char* __scope, error_code& __ec)
776 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
777 address_v6::bytes_type __b;
778 const int __res = ::inet_pton(AF_INET6, __addr, __b.data());
788 unsigned long __val = std::strtoul(__scope, &__eptr, 10);
789 if (__eptr != __scope && !*__eptr
790 && __val <= numeric_limits<scope_id_type>::max())
792 return { __b, static_cast<scope_id_type>(__val) };
794 __ec = std::make_error_code(std::errc::invalid_argument);
797 __ec = std::make_error_code(std::errc::invalid_argument);
799 __ec.assign(errno, generic_category());
801 __ec = std::make_error_code(__unsupported_err());
807 make_address_v6(const char* __str, error_code& __ec) noexcept
809 auto __p = __builtin_strchr(__str, '%');
811 return __make_address_v6(__str, nullptr, __ec);
814 bool __skip_leading_zero = true;
815 while (__str < __p && __out < std::end(__buf))
817 if (!__skip_leading_zero || *__str != '0')
819 if (*__str == ':' || *__str == '.')
820 __skip_leading_zero = true;
822 __skip_leading_zero = false;
827 if (__out == std::end(__buf))
829 __ec = std::make_error_code(std::errc::invalid_argument);
835 return __make_address_v6(__buf, __p + 1, __ec);
840 make_address_v6(const char* __str)
841 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
844 make_address_v6(const string& __str, error_code& __ec) noexcept
846 auto __pos = __str.find('%');
847 if (__pos == string::npos)
848 return __make_address_v6(__str.c_str(), nullptr, __ec);
851 bool __skip_leading_zero = true;
853 while (__n < __pos && __out < std::end(__buf))
855 if (!__skip_leading_zero || __str[__n] != '0')
857 if (__str[__n] == ':' || __str[__n] == '.')
858 __skip_leading_zero = true;
860 __skip_leading_zero = false;
865 if (__out == std::end(__buf))
867 __ec = std::make_error_code(std::errc::invalid_argument);
873 return __make_address_v6(__buf, __str.c_str() + __pos + 1, __ec);
878 make_address_v6(const string& __str)
879 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
882 make_address_v6(string_view __str, error_code& __ec) noexcept
886 char* __scope = nullptr;
887 bool __skip_leading_zero = true;
889 while (__n < __str.length() && __out < std::end(__buf))
891 if (__str[__n] == '%')
894 __out = std::end(__buf);
899 __skip_leading_zero = true;
902 else if (!__skip_leading_zero || __str[__n] != '0')
904 if (__str[__n] == ':' || __str[__n] == '.')
905 __skip_leading_zero = true;
907 __skip_leading_zero = false;
913 if (__out == std::end(__buf))
915 __ec = std::make_error_code(std::errc::invalid_argument);
921 return __make_address_v6(__buf, __scope, __ec);
926 make_address_v6(string_view __str)
927 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
931 /** ip::address creation
936 make_address(const char* __str, error_code& __ec) noexcept
939 address_v6 __v6a = make_address_v6(__str, __ec);
944 address_v4 __v4a = make_address_v4(__str, __ec);
952 make_address(const char* __str)
953 { return make_address(__str, __throw_on_error{"make_address"}); }
956 make_address(const string& __str, error_code& __ec) noexcept; // TODO
959 make_address(const string& __str)
960 { return make_address(__str, __throw_on_error{"make_address"}); }
963 make_address(string_view __str, error_code& __ec) noexcept
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
971 make_address(string_view __str)
972 { return make_address(__str, __throw_on_error{"make_address"}); }
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(); }
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(); }
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(); }
994 template<typename> class basic_address_iterator; // not defined
996 template<> class basic_address_iterator<address_v4>
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;
1007 basic_address_iterator(const address_v4& __a) noexcept
1008 : _M_address(__a) { }
1011 reference operator*() const noexcept { return _M_address; }
1012 pointer operator->() const noexcept { return &_M_address; }
1014 basic_address_iterator&
1015 operator++() noexcept
1017 _M_address = value_type(_M_address.to_uint() + 1);
1021 basic_address_iterator operator++(int) noexcept
1028 basic_address_iterator& operator--() noexcept
1030 _M_address = value_type(_M_address.to_uint() - 1);
1034 basic_address_iterator
1035 operator--(int) noexcept
1043 operator==(const basic_address_iterator& __rhs) const noexcept
1044 { return _M_address == __rhs._M_address; }
1047 operator!=(const basic_address_iterator& __rhs) const noexcept
1048 { return _M_address != __rhs._M_address; }
1051 address_v4 _M_address;
1054 using address_v4_iterator = basic_address_iterator<address_v4>;
1056 template<> class basic_address_iterator<address_v6>
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;
1067 basic_address_iterator(const address_v6& __a) noexcept
1068 : _M_address(__a) { }
1071 reference operator*() const noexcept { return _M_address; }
1072 pointer operator->() const noexcept { return &_M_address; }
1074 basic_address_iterator&
1075 operator++() noexcept; // TODO
1077 basic_address_iterator
1078 operator++(int) noexcept
1085 basic_address_iterator&
1086 operator--() noexcept; // TODO
1088 basic_address_iterator
1089 operator--(int) noexcept
1097 operator==(const basic_address_iterator& __rhs) const noexcept
1098 { return _M_address == __rhs._M_address; }
1101 operator!=(const basic_address_iterator& __rhs) const noexcept
1102 { return _M_address != __rhs._M_address; }
1105 address_v6 _M_address;
1108 using address_v6_iterator = basic_address_iterator<address_v6>;
1110 template<typename> class basic_address_range; // not defined
1112 /** An IPv6 address range.
1116 template<> class basic_address_range<address_v4>
1121 using iterator = basic_address_iterator<address_v4>;
1125 basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
1127 basic_address_range(const address_v4& __first,
1128 const address_v4& __last) noexcept
1129 : _M_begin(__first), _M_end(__last) { }
1133 iterator begin() const noexcept { return _M_begin; }
1134 iterator end() const noexcept { return _M_end; }
1135 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
1138 size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); }
1141 find(const address_v4& __addr) const noexcept
1143 if (*_M_begin <= __addr && __addr < *_M_end)
1144 return iterator{__addr};
1153 using address_v4_range = basic_address_range<address_v4>;
1157 /** An IPv6 address range.
1161 template<> class basic_address_range<address_v6>
1166 using iterator = basic_address_iterator<address_v6>;
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) { }
1177 iterator begin() const noexcept { return _M_begin; }
1178 iterator end() const noexcept { return _M_end; }
1179 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
1182 find(const address_v6& __addr) const noexcept
1184 if (*_M_begin <= __addr && __addr < *_M_end)
1185 return iterator{__addr};
1194 using address_v6_range = basic_address_range<address_v6>;
1199 operator==(const network_v4& __a, const network_v4& __b) noexcept;
1202 operator==(const network_v6& __a, const network_v6& __b) noexcept;
1205 /// An IPv4 network address.
1210 constexpr network_v4() noexcept : _M_addr(), _M_prefix_len(0) { }
1213 network_v4(const address_v4& __addr, int __prefix_len)
1214 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1216 if (_M_prefix_len < 0 || _M_prefix_len > 32)
1217 __throw_out_of_range("network_v4: invalid prefix length");
1221 network_v4(const address_v4& __addr, const address_v4& __mask)
1222 : _M_addr(__addr), _M_prefix_len(__builtin_popcount(__mask.to_uint()))
1224 if (_M_prefix_len != 0)
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");
1236 constexpr address_v4 address() const noexcept { return _M_addr; }
1237 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1239 constexpr address_v4
1240 netmask() const noexcept
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};
1248 constexpr address_v4
1249 network() const noexcept
1250 { return address_v4{_M_addr.to_uint() & netmask().to_uint()}; }
1252 constexpr address_v4
1253 broadcast() const noexcept
1254 { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; }
1257 hosts() const noexcept
1260 return { address(), *++address_v4_iterator(address()) };
1261 return { network(), broadcast() };
1264 constexpr network_v4
1265 canonical() const noexcept
1266 { return network_v4(network(), prefix_length()); }
1268 constexpr bool is_host() const noexcept { return _M_prefix_len == 32; }
1271 is_subnet_of(const network_v4& __other) const noexcept
1273 if (__other.prefix_length() < prefix_length())
1275 network_v4 __net(address(), __other.prefix_length());
1276 return __net.canonical() == __other.canonical();
1281 template<typename _Allocator = allocator<char>>
1282 __string_with<_Allocator>
1283 to_string(const _Allocator& __a = _Allocator()) const
1285 return address().to_string(__a) + '/'
1286 + std::to_string(prefix_length());
1294 /// An IPv6 network address.
1299 constexpr network_v6() noexcept : _M_addr(), _M_prefix_len(0) { }
1302 network_v6(const address_v6& __addr, int __prefix_len)
1303 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1305 if (_M_prefix_len < 0 || _M_prefix_len > 128)
1306 __throw_out_of_range("network_v6: invalid prefix length");
1310 constexpr address_v6 address() const noexcept { return _M_addr; }
1311 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1313 constexpr address_v6 network() const noexcept; // TODO
1316 hosts() const noexcept
1319 return { address(), *++address_v6_iterator(address()) };
1320 return {}; // { network(), XXX broadcast() XXX }; // TODO
1323 constexpr network_v6
1324 canonical() const noexcept
1325 { return network_v6{network(), prefix_length()}; }
1327 constexpr bool is_host() const noexcept { return _M_prefix_len == 128; }
1330 is_subnet_of(const network_v6& __other) const noexcept
1332 if (__other.prefix_length() < prefix_length())
1334 network_v6 __net(address(), __other.prefix_length());
1335 return __net.canonical() == __other.canonical();
1340 template<typename _Allocator = allocator<char>>
1341 __string_with<_Allocator>
1342 to_string(const _Allocator& __a = _Allocator()) const
1344 return address().to_string(__a) + '/'
1345 + std::to_string(prefix_length());
1354 /** ip::network_v4 comparisons
1359 operator==(const network_v4& __a, const network_v4& __b) noexcept
1361 return __a.address() == __b.address()
1362 && __a.prefix_length() == __b.prefix_length();
1366 operator!=(const network_v4& __a, const network_v4& __b) noexcept
1367 { return !(__a == __b); }
1371 /** ip::network_v6 comparisons
1376 operator==(const network_v6& __a, const network_v6& __b) noexcept
1378 return __a.address() == __b.address()
1379 && __a.prefix_length() == __b.prefix_length();
1383 operator!=(const network_v6& __a, const network_v6& __b) noexcept
1384 { return !(__a == __b); }
1388 /** ip::network_v4 creation
1393 make_network_v4(const address_v4& __a, int __prefix_len)
1394 { return network_v4{__a, __prefix_len}; }
1397 make_network_v4(const address_v4& __a, const address_v4& __mask)
1398 { return network_v4{ __a, __mask }; }
1400 network_v4 make_network_v4(const char*, error_code&) noexcept; // TODO
1403 make_network_v4(const char* __str)
1404 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1406 network_v4 make_network_v4(const string&, error_code&) noexcept; // TODO
1409 make_network_v4(const string& __str)
1410 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1412 network_v4 make_network_v4(string_view, error_code&) noexcept; // TODO
1415 make_network_v4(string_view __str)
1416 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1420 /** ip::network_v6 creation
1425 make_network_v6(const address_v6& __a, int __prefix_len)
1426 { return network_v6{__a, __prefix_len}; }
1428 network_v6 make_network_v6(const char*, error_code&) noexcept; // TODO
1431 make_network_v6(const char* __str)
1432 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1434 network_v6 make_network_v6(const string&, error_code&) noexcept; // TODO
1437 make_network_v6(const string& __str)
1438 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1440 network_v6 make_network_v6(string_view, error_code&) noexcept; // TODO
1443 make_network_v6(string_view __str)
1444 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
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(); }
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(); }
1461 template<typename _InternetProtocol>
1462 class basic_endpoint
1466 using protocol_type = _InternetProtocol;
1471 basic_endpoint() noexcept : _M_data()
1472 { _M_data._M_v4.sin_family = protocol_type::v4().family(); }
1475 basic_endpoint(const protocol_type& __proto,
1476 port_type __port_num) noexcept
1479 __glibcxx_assert(__proto == protocol_type::v4()
1480 || __proto == protocol_type::v6());
1482 _M_data._M_v4.sin_family = __proto.family();
1483 _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
1487 basic_endpoint(const ip::address& __addr,
1488 port_type __port_num) noexcept
1493 _M_data._M_v4.sin_family = protocol_type::v4().family();
1494 _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
1495 _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
1500 _M_data._M_v6.sin6_family = protocol_type::v6().family();
1501 _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num);
1502 __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
1503 __addr._M_v6._M_bytes.data(), 16);
1504 _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
1509 constexpr protocol_type protocol() const noexcept
1511 return _M_is_v6() ? protocol_type::v6() : protocol_type::v4();
1514 constexpr ip::address
1515 address() const noexcept
1520 __builtin_memcpy(&__addr._M_v6._M_bytes,
1521 _M_data._M_v6.sin6_addr.s6_addr, 16);
1522 __addr._M_is_v4 = false;
1526 __builtin_memcpy(&__addr._M_v4._M_addr,
1527 &_M_data._M_v4.sin_addr.s_addr, 4);
1533 address(const ip::address& __addr) noexcept
1538 _M_data._M_v6.sin6_family = protocol_type::v6().family();
1539 __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
1540 __addr._M_v6._M_bytes.data(), 16);
1541 _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
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;
1551 port() const noexcept
1552 { return address_v4::_S_ntoh_16(_M_data._M_v4.sin_port); }
1555 port(port_type __port_num) noexcept
1556 { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); }
1558 void* data() noexcept { return &_M_data; }
1560 const void* data() const noexcept { return &_M_data; }
1562 constexpr size_t size() const noexcept
1563 { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); }
1569 __throw_length_error("net::ip::basic_endpoint::resize");
1572 constexpr size_t capacity() const noexcept { return sizeof(_M_data); }
1581 constexpr bool _M_is_v6() const noexcept
1582 { return _M_data._M_v4.sin_family == AF_INET6; }
1585 /** basic_endpoint comparisons
1589 template<typename _InternetProtocol>
1591 operator==(const basic_endpoint<_InternetProtocol>& __a,
1592 const basic_endpoint<_InternetProtocol>& __b)
1593 { return __a.address() == __b.address() && __a.port() == __b.port(); }
1595 template<typename _InternetProtocol>
1597 operator!=(const basic_endpoint<_InternetProtocol>& __a,
1598 const basic_endpoint<_InternetProtocol>& __b)
1599 { return !(__a == __b); }
1601 template<typename _InternetProtocol>
1603 operator< (const basic_endpoint<_InternetProtocol>& __a,
1604 const basic_endpoint<_InternetProtocol>& __b)
1606 return __a.address() < __b.address()
1607 || (!(__b.address() < __a.address()) && __a.port() < __b.port());
1610 template<typename _InternetProtocol>
1612 operator> (const basic_endpoint<_InternetProtocol>& __a,
1613 const basic_endpoint<_InternetProtocol>& __b)
1614 { return __b < __a; }
1616 template<typename _InternetProtocol>
1618 operator<=(const basic_endpoint<_InternetProtocol>& __a,
1619 const basic_endpoint<_InternetProtocol>& __b)
1620 { return !(__b < __a); }
1622 template<typename _InternetProtocol>
1624 operator>=(const basic_endpoint<_InternetProtocol>& __a,
1625 const basic_endpoint<_InternetProtocol>& __b)
1626 { return !(__a < __b); }
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)
1636 basic_ostringstream<_CharT, _Traits> __ss;
1638 == basic_endpoint<_InternetProtocol>::protocol_type::v6())
1639 __ss << '[' << __ep.address() << ']';
1641 __ss << __ep.address();
1642 __ss << ':' << __ep.port();
1647 /** Type representing a single result of name/address resolution.
1651 template<typename _InternetProtocol>
1652 class basic_resolver_entry
1656 using protocol_type = _InternetProtocol;
1657 using endpoint_type = typename _InternetProtocol::endpoint;
1660 basic_resolver_entry() { }
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) { }
1667 endpoint_type endpoint() const { return _M_ep; }
1668 operator endpoint_type() const { return _M_ep; }
1670 template<typename _Allocator = allocator<char>>
1671 __string_with<_Allocator>
1672 host_name(const _Allocator& __a = _Allocator()) const
1673 { return { _M_host, __a }; }
1675 template<typename _Allocator = allocator<char>>
1676 __string_with<_Allocator>
1677 service_name(const _Allocator& __a = _Allocator()) const
1678 { return { _M_svc, __a }; }
1681 basic_endpoint<_InternetProtocol> _M_ep;
1686 template<typename _InternetProtocol>
1688 operator==(const basic_resolver_entry<_InternetProtocol>& __a,
1689 const basic_resolver_entry<_InternetProtocol>& __b)
1691 return __a.endpoint() == __b.endpoint()
1692 && __a.host_name() == __b.host_name()
1693 && __a.service_name() == __b.service_name();
1696 template<typename _InternetProtocol>
1698 operator!=(const basic_resolver_entry<_InternetProtocol>& __a,
1699 const basic_resolver_entry<_InternetProtocol>& __b)
1700 { return !(__a == __b); }
1704 /** Base class defining flags for name/address resolution.
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;
1715 #ifdef AI_NUMERICSERV
1716 static constexpr flags numeric_service = (flags)AI_NUMERICSERV;
1719 static constexpr flags v4_mapped = (flags)AI_V4MAPPED;
1722 static constexpr flags all_matching = (flags)AI_ALL;
1724 #ifdef AI_ADDRCONFIG
1725 static constexpr flags address_configured = (flags)AI_ADDRCONFIG;
1728 friend constexpr flags
1729 operator&(flags __f1, flags __f2) noexcept
1730 { return flags( int(__f1) & int(__f2) ); }
1732 friend constexpr flags
1733 operator|(flags __f1, flags __f2) noexcept
1734 { return flags( int(__f1) | int(__f2) ); }
1736 friend constexpr flags
1737 operator^(flags __f1, flags __f2) noexcept
1738 { return flags( int(__f1) ^ int(__f2) ); }
1740 friend constexpr flags
1741 operator~(flags __f) noexcept
1742 { return flags( ~int(__f) ); }
1744 friend constexpr flags&
1745 operator&=(flags& __f1, flags __f2) noexcept
1746 { return __f1 = (__f1 & __f2); }
1748 friend constexpr flags&
1749 operator|=(flags& __f1, flags __f2) noexcept
1750 { return __f1 = (__f1 | __f2); }
1752 friend constexpr flags&
1753 operator^=(flags& __f1, flags __f2) noexcept
1754 { return __f1 = (__f1 ^ __f2); }
1757 resolver_base() = default;
1758 ~resolver_base() = default;
1761 // TODO define resolver_base::flags static constants in .so for C++14 mode
1765 /** Container for results of name/address resolution.
1769 template<typename _InternetProtocol>
1770 class basic_resolver_results
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;
1784 // construct / copy / destroy:
1786 basic_resolver_results() = default;
1788 basic_resolver_results(const basic_resolver_results&) = default;
1790 basic_resolver_results(basic_resolver_results&&) noexcept = default;
1792 basic_resolver_results&
1793 operator=(const basic_resolver_results&) = default;
1795 basic_resolver_results&
1796 operator=(basic_resolver_results&&) = default;
1798 ~basic_resolver_results() = default;
1801 size_type size() const noexcept { return _M_size; }
1802 size_type max_size() const noexcept { return _M_results.max_size(); }
1804 _GLIBCXX_NODISCARD bool
1805 empty() const noexcept { return _M_results.empty(); }
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(); }
1815 swap(basic_resolver_results& __that) noexcept
1816 { _M_results.swap(__that._M_results); }
1819 friend class basic_resolver<protocol_type>;
1821 basic_resolver_results(string_view, string_view, resolver_base::flags,
1822 error_code&, protocol_type* = nullptr);
1824 basic_resolver_results(const endpoint_type&, error_code&);
1826 forward_list<value_type> _M_results;
1830 template<typename _InternetProtocol>
1832 operator==(const basic_resolver_results<_InternetProtocol>& __a,
1833 const basic_resolver_results<_InternetProtocol>& __b)
1835 return __a.size() == __b.size()
1836 && std::equal(__a.begin(), __a.end(), __b.begin());
1839 template<typename _InternetProtocol>
1841 operator!=(const basic_resolver_results<_InternetProtocol>& __a,
1842 const basic_resolver_results<_InternetProtocol>& __b)
1843 { return !(__a == __b); }
1847 /// Perform name/address resolution.
1848 template<typename _InternetProtocol>
1849 class basic_resolver : public resolver_base
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>;
1859 // construct / copy / destroy:
1861 explicit basic_resolver(io_context& __ctx) : _M_ctx(&__ctx) { }
1863 basic_resolver(const basic_resolver&) = delete;
1865 basic_resolver(basic_resolver&& __rhs) noexcept
1866 : _M_ctx(__rhs._M_ctx)
1867 { } // TODO move state/tasks etc.
1869 ~basic_resolver() { cancel(); }
1871 basic_resolver& operator=(const basic_resolver&) = delete;
1873 basic_resolver& operator=(basic_resolver&& __rhs)
1876 _M_ctx = __rhs._M_ctx;
1877 // TODO move state/tasks etc.
1881 // basic_resolver operations:
1883 executor_type get_executor() noexcept { return _M_ctx->get_executor(); }
1885 void cancel() { } // TODO
1888 resolve(string_view __host_name, string_view __service_name)
1890 return resolve(__host_name, __service_name, resolver_base::flags(),
1891 __throw_on_error{"basic_resolver::resolve"});
1895 resolve(string_view __host_name, string_view __service_name,
1898 return resolve(__host_name, __service_name, resolver_base::flags(),
1903 resolve(string_view __host_name, string_view __service_name, flags __f)
1905 return resolve(__host_name, __service_name, __f,
1906 __throw_on_error{"basic_resolver::resolve"});
1910 resolve(string_view __host_name, string_view __service_name, flags __f,
1912 { return {__host_name, __service_name, __f, __ec}; }
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)
1919 return async_resolve(__host_name, __service_name,
1920 resolver_base::flags(),
1921 forward<_CompletionToken>(__token));
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
1930 resolve(const protocol_type& __protocol,
1931 string_view __host_name, string_view __service_name)
1933 return resolve(__protocol, __host_name, __service_name,
1934 resolver_base::flags(),
1935 __throw_on_error{"basic_resolver::resolve"});
1939 resolve(const protocol_type& __protocol,
1940 string_view __host_name, string_view __service_name,
1943 return resolve(__protocol, __host_name, __service_name,
1944 resolver_base::flags(), __ec);
1948 resolve(const protocol_type& __protocol,
1949 string_view __host_name, string_view __service_name, flags __f)
1951 return resolve(__protocol, __host_name, __service_name, __f,
1952 __throw_on_error{"basic_resolver::resolve"});
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}; }
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)
1967 return async_resolve(__protocol, __host_name, __service_name,
1968 resolver_base::flags(),
1969 forward<_CompletionToken>(__token));
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
1979 resolve(const endpoint_type& __ep)
1980 { return resolve(__ep, __throw_on_error{"basic_resolver::resolve"}); }
1983 resolve(const endpoint_type& __ep, error_code& __ec)
1984 { return { __ep, __ec }; }
1986 template<typename _CompletionToken> // TODO
1987 __deduced_t<_CompletionToken, void(error_code, results_type)>
1988 async_resolve(const endpoint_type& __ep, _CompletionToken&& __token);
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)
2001 #ifdef _GLIBCXX_HAVE_NETDB_H
2003 const char* __h = __host_name.data()
2004 ? (__host = __host_name.to_string()).c_str()
2007 const char* __s = __service_name.data()
2008 ? (__svc = __service_name.to_string()).c_str()
2011 ::addrinfo __hints{ };
2012 __hints.ai_flags = static_cast<int>(__f);
2015 __hints.ai_family = __protocol->family();
2016 __hints.ai_socktype = __protocol->type();
2017 __hints.ai_protocol = __protocol->protocol();
2021 auto __p = endpoint_type{}.protocol();
2022 __hints.ai_family = AF_UNSPEC;
2023 __hints.ai_socktype = __p.type();
2024 __hints.ai_protocol = __p.protocol();
2027 struct __scoped_addrinfo
2029 ~__scoped_addrinfo() { if (_M_p) ::freeaddrinfo(_M_p); }
2030 ::addrinfo* _M_p = nullptr;
2033 if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p))
2035 __ec = ip::__make_resolver_error_code(__err, errno);
2041 auto __tail = _M_results.before_begin();
2042 for (auto __ai = __sai._M_p; __ai != nullptr; __ai = __ai->ai_next)
2044 if (__ai->ai_family == AF_INET || __ai->ai_family == AF_INET6)
2046 if (__ai->ai_addrlen <= __ep.capacity())
2047 __builtin_memcpy(__ep.data(), __ai->ai_addr, __ai->ai_addrlen);
2048 __ep.resize(__ai->ai_addrlen);
2049 __tail = _M_results.emplace_after(__tail, __ep, __host, __svc);
2054 __ec = std::make_error_code(errc::operation_not_supported);
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)
2063 #ifdef _GLIBCXX_HAVE_NETDB_H
2064 char __host_name[1025]; // glibc NI_MAXHOST
2065 char __service_name[32]; // glibc NI_MAXSERV
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),
2076 __flags |= NI_NUMERICSERV;
2077 __err = ::getnameinfo(__sa, __ep.size(),
2078 __host_name, sizeof(__host_name),
2079 __service_name, sizeof(__service_name),
2083 __ec = ip::__make_resolver_error_code(__err, errno);
2087 _M_results.emplace_front(__ep, __host_name, __service_name);
2091 __ec = std::make_error_code(errc::operation_not_supported);
2095 /** The name of the local host.
2099 template<typename _Allocator>
2100 __string_with<_Allocator>
2101 host_name(const _Allocator& __a, error_code& __ec)
2103 #ifdef HOST_NAME_MAX
2104 constexpr size_t __maxlen = HOST_NAME_MAX;
2106 constexpr size_t __maxlen = 256;
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 };
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"}); }
2121 host_name(error_code& __ec)
2122 { return host_name(std::allocator<char>{}, __ec); }
2126 { return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); }
2131 /// The TCP byte-stream protocol.
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.
2143 /// Disable coalescing of small segments (i.e. the Nagle algorithm).
2144 struct no_delay : __sockopt_crtp<no_delay, bool>
2146 using __sockopt_crtp::__sockopt_crtp;
2147 using __sockopt_crtp::operator=;
2149 static const int _S_level = IPPROTO_TCP;
2150 static const int _S_name = TCP_NODELAY;
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); }
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; }
2168 constexpr explicit tcp(int __family) : _M_family(__family) { }
2178 operator==(const tcp& __a, const tcp& __b) noexcept
2179 { return __a.family() == __b.family(); }
2182 operator!=(const tcp& __a, const tcp& __b) noexcept
2183 { return !(__a == __b); }
2186 #endif // IPPROTO_TCP
2189 /// The UDP datagram protocol.
2194 using endpoint = basic_endpoint<udp>;
2195 using resolver = basic_resolver<udp>;
2196 using socket = basic_datagram_socket<udp>;
2199 static constexpr udp v4() noexcept { return udp(AF_INET); }
2200 static constexpr udp v6() noexcept { return udp(AF_INET6); }
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; }
2209 constexpr explicit udp(int __family) : _M_family(__family) { }
2219 operator==(const udp& __a, const udp& __b) noexcept
2220 { return __a.family() == __b.family(); }
2223 operator!=(const udp& __a, const udp& __b) noexcept
2224 { return !(__a == __b); }
2227 #endif // IPPROTO_UDP
2229 #if defined IPPROTO_IP && defined IPPROTO_IPV6
2231 /// Restrict a socket created for an IPv6 protocol to IPv6 only.
2232 class v6_only : public __sockopt_crtp<v6_only, bool>
2235 using __sockopt_crtp::__sockopt_crtp;
2236 using __sockopt_crtp::operator=;
2239 friend __sockopt_crtp<v6_only, bool>;
2240 static const int _S_level = IPPROTO_IPV6;
2241 static const int _S_name = IPV6_V6ONLY;
2246 /// Set the default number of hops (TTL) for outbound datagrams.
2247 class hops : public __sockopt_crtp<hops>
2250 using __sockopt_crtp::__sockopt_crtp;
2251 using __sockopt_crtp::operator=;
2253 template<typename _Protocol>
2255 level(const _Protocol& __p) const noexcept
2256 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2258 template<typename _Protocol>
2260 name(const _Protocol& __p) const noexcept
2261 { return __p.family() == AF_INET6 ? IPV6_UNICAST_HOPS : IP_TTL; }
2263 } // namespace unicast
2271 __mcastopt(const address& __grp) noexcept
2272 : __mcastopt(__grp.is_v4() ? __mcastopt(__grp.to_v4()) : __mcastopt(__grp.to_v6()))
2276 __mcastopt(const address_v4& __grp,
2277 const address_v4& __iface = address_v4::any()) noexcept
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();
2283 _M_v4.imr_multiaddr.s_addr = __builtin_bswap32(__grp.to_uint());
2284 _M_v4.imr_interface.s_addr = __builtin_bswap32(__iface.to_uint());
2289 __mcastopt(const address_v6& __grp, unsigned int __iface = 0) noexcept
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;
2296 template<typename _Protocol>
2298 level(const _Protocol& __p) const noexcept
2299 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2301 template<typename _Protocol>
2303 data(const _Protocol& __p) const noexcept
2304 { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
2306 template<typename _Protocol>
2308 size(const _Protocol& __p) const noexcept
2309 { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
2312 ipv6_mreq _M_v6 = {};
2316 /// Request that a socket joins a multicast group.
2317 class join_group : private __mcastopt
2320 using __mcastopt::__mcastopt;
2321 using __mcastopt::level;
2322 using __mcastopt::data;
2323 using __mcastopt::size;
2325 template<typename _Protocol>
2327 name(const _Protocol& __p) const noexcept
2329 if (__p.family() == AF_INET6)
2330 return IPV6_JOIN_GROUP;
2331 return IP_ADD_MEMBERSHIP;
2335 /// Request that a socket leaves a multicast group.
2336 class leave_group : private __mcastopt
2339 using __mcastopt::__mcastopt;
2340 using __mcastopt::level;
2341 using __mcastopt::data;
2342 using __mcastopt::size;
2344 template<typename _Protocol>
2346 name(const _Protocol& __p) const noexcept
2348 if (__p.family() == AF_INET6)
2349 return IPV6_LEAVE_GROUP;
2350 return IP_DROP_MEMBERSHIP;
2354 /// Specify the network interface for outgoing multicast datagrams.
2355 class outbound_interface
2359 outbound_interface(const address_v4& __v4) noexcept
2361 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
2362 _M_v4.s_addr = __v4.to_uint();
2364 _M_v4.s_addr = __builtin_bswap32(__v4.to_uint());
2369 outbound_interface(unsigned int __v6) noexcept
2370 : _M_v4(), _M_v6(__v6)
2373 template<typename _Protocol>
2375 level(const _Protocol& __p) const noexcept
2376 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2378 template<typename _Protocol>
2380 name(const _Protocol& __p) const noexcept
2382 return __p.family() == AF_INET6
2383 ? IPV6_MULTICAST_IF : IP_MULTICAST_IF;
2386 template<typename _Protocol>
2388 data(const _Protocol& __p) const noexcept
2389 { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
2391 template<typename _Protocol>
2393 size(const _Protocol& __p) const noexcept
2394 { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
2401 /// Set the default number of hops (TTL) for outbound datagrams.
2402 class hops : public __sockopt_crtp<hops>
2405 using __sockopt_crtp::__sockopt_crtp;
2406 using __sockopt_crtp::operator=;
2408 template<typename _Protocol>
2410 level(const _Protocol& __p) const noexcept
2411 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2413 template<typename _Protocol>
2415 name(const _Protocol& __p) const noexcept
2417 return __p.family() == AF_INET6
2418 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL;
2422 /// Set whether datagrams are delivered back to the local application.
2423 class enable_loopback : public __sockopt_crtp<enable_loopback, bool>
2426 using __sockopt_crtp::__sockopt_crtp;
2427 using __sockopt_crtp::operator=;
2429 template<typename _Protocol>
2431 level(const _Protocol& __p) const noexcept
2432 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2434 template<typename _Protocol>
2436 name(const _Protocol& __p) const noexcept
2438 return __p.family() == AF_INET6
2439 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP;
2443 } // namespace multicast
2445 #endif // IPPROTO_IP && IPPROTO_IPV6
2452 } // namespace experimental
2455 struct is_error_condition_enum<experimental::net::v1::ip::resolver_errc>
2456 : public true_type {};
2459 template<typename _Tp> struct hash;
2461 struct hash<experimental::net::v1::ip::address>
2462 : __hash_base<size_t, experimental::net::v1::ip::address>
2465 operator()(const experimental::net::v1::ip::address& __a) const
2468 return _Hash_impl::hash(__a.to_v4());
2470 return _Hash_impl::hash(__a.to_v6());
2475 struct hash<experimental::net::v1::ip::address_v4>
2476 : __hash_base<size_t, experimental::net::v1::ip::address_v4>
2479 operator()(const experimental::net::v1::ip::address_v4& __a) const
2480 { return _Hash_impl::hash(__a.to_bytes()); }
2483 template<> struct hash<experimental::net::v1::ip::address_v6>
2484 : __hash_base<size_t, experimental::net::v1::ip::address_v6>
2487 operator()(const experimental::net::v1::ip::address_v6& __a) const
2488 { return _Hash_impl::hash(__a.to_bytes()); }
2491 _GLIBCXX_END_NAMESPACE_VERSION
2496 #endif // _GLIBCXX_EXPERIMENTAL_INTERNET