1 // <experimental/internet> -*- C++ -*-
3 // Copyright (C) 2015-2022 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 #include <bits/requires_hosted.h> // experimental is currently omitted
37 #if __cplusplus >= 201402L
39 #include <experimental/netfwd>
40 #include <experimental/io_context>
41 #include <experimental/bits/net.h>
43 #include <forward_list>
46 #include <experimental/string_view>
47 #ifdef _GLIBCXX_HAVE_UNISTD_H
50 #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
51 # include <sys/socket.h> // AF_INET, AF_INET6, SOCK_DGRAM, SOCK_STREAM
53 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
54 # include <arpa/inet.h> // inet_ntop
56 #ifdef _GLIBCXX_HAVE_NETINET_IN_H
57 # include <netinet/in.h> // IPPROTO_IP, IPPROTO_IPV6, in_addr, in6_addr
59 #ifdef _GLIBCXX_HAVE_NETINET_TCP_H
60 # include <netinet/tcp.h> // TCP_NODELAY
62 #ifdef _GLIBCXX_HAVE_NETDB_H
63 # include <netdb.h> // getaddrinfo etc.
66 #if defined _WIN32 && __has_include(<ws2tcpip.h>)
67 # include <ws2tcpip.h>
70 namespace std _GLIBCXX_VISIBILITY(default)
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 namespace experimental
81 /** @addtogroup networking-ts
85 /** Error codes for resolver errors.
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
103 /// Error category for resolver errors.
104 inline const error_category& resolver_category() noexcept // TODO non-inline
106 struct __cat : error_category
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);
113 return "name resolution requires <netdb.h>";
116 virtual void __message(int) { } // TODO dual ABI XXX
122 inline error_code make_error_code(resolver_errc __e) noexcept
123 { return error_code(static_cast<int>(__e), resolver_category()); }
125 inline error_condition make_error_condition(resolver_errc __e) noexcept
126 { return error_condition(static_cast<int>(__e), resolver_category()); }
128 /// @cond undocumented
130 __make_resolver_error_code(int __ai_err,
131 [[__maybe_unused__]] int __sys_err) noexcept
134 if (__builtin_expect(__ai_err == EAI_SYSTEM, 0))
135 return error_code(__sys_err, std::generic_category());
137 return error_code(__ai_err, resolver_category());
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.
146 /// Convenience alias for constraining allocators for strings.
147 template<typename _Alloc>
149 = enable_if_t<std::is_same<typename _Alloc::value_type, char>::value,
150 std::basic_string<char, std::char_traits<char>, _Alloc>>;
153 __unsupported_err() noexcept
155 #if defined EAFNOSUPPORT
156 return std::errc::address_family_not_supported;
158 return std::errc::operation_not_supported;
162 /** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses.
166 struct v4_mapped_t {};
167 constexpr v4_mapped_t v4_mapped;
176 using uint_type = uint_least32_t;
178 struct bytes_type : array<unsigned char, 4>
180 template<typename... _Tp>
182 bytes_type(_Tp... __tp)
183 : array<unsigned char, 4>{{static_cast<unsigned char>(__tp)...}}
186 for (auto __b : *this)
188 __throw_out_of_range("invalid address_v4::bytes_type value");
194 constexpr address_v4() noexcept : _M_addr(0) { }
196 constexpr address_v4(const address_v4& a) noexcept = default;
199 address_v4(const bytes_type& __b)
200 : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
204 address_v4(uint_type __val) : _M_addr(_S_hton_32(__val))
206 #if UINT_LEAST32_MAX > 0xFFFFFFFF
207 if (__val > 0xFFFFFFFF)
208 __throw_out_of_range("invalid address_v4::uint_type value");
213 address_v4& operator=(const address_v4& a) noexcept = default;
216 constexpr bool is_unspecified() const noexcept { return to_uint() == 0; }
219 is_loopback() const noexcept
220 { return (to_uint() & 0xFF000000) == 0x7F000000; }
223 is_multicast() const noexcept
224 { return (to_uint() & 0xF0000000) == 0xE0000000; }
227 to_bytes() const noexcept
230 (_M_addr >> 24) & 0xFF,
231 (_M_addr >> 16) & 0xFF,
232 (_M_addr >> 8) & 0xFF,
238 to_uint() const noexcept { return _S_ntoh_32(_M_addr); }
240 template<typename _Allocator = allocator<char>>
241 __string_with<_Allocator>
242 to_string(const _Allocator& __a = _Allocator()) const
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'));
253 std::__throw_system_error((int)__unsupported_err());
258 static constexpr address_v4 any() noexcept { return address_v4{}; }
261 address_v4 loopback() noexcept { return address_v4{0x7F000001}; }
264 address_v4 broadcast() noexcept { return address_v4{0xFFFFFFFF}; }
267 template<typename _InternetProtocol>
268 friend class basic_endpoint;
270 friend address_v4 make_address_v4(const char*, error_code&) noexcept;
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; }
278 static constexpr uint16_t
279 _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); }
281 static constexpr uint16_t
282 _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); }
284 static constexpr uint32_t
285 _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); }
287 static constexpr uint32_t
288 _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); }
291 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
292 in_addr_t _M_addr; // network byte order
303 struct bytes_type : array<unsigned char, 16>
305 template<typename... _Tp>
307 bytes_type(_Tp... __t)
308 : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}}
313 constexpr address_v6() noexcept : _M_bytes(), _M_scope_id() { }
315 constexpr address_v6(const address_v6& __a) noexcept = default;
318 address_v6(const bytes_type& __bytes, scope_id_type __scope = 0)
319 : _M_bytes(__bytes), _M_scope_id(__scope)
323 address_v6& operator=(const address_v6& __a) noexcept = default;
326 void scope_id(scope_id_type __id) noexcept { _M_scope_id = __id; }
328 constexpr scope_id_type scope_id() const noexcept { return _M_scope_id; }
331 is_unspecified() const noexcept
333 for (int __i = 0; __i < 16; ++__i)
334 if (_M_bytes[__i] != 0x00)
336 return _M_scope_id == 0;
340 is_loopback() const noexcept
342 for (int __i = 0; __i < 15; ++__i)
343 if (_M_bytes[__i] != 0x00)
345 return _M_bytes[15] == 0x01 && _M_scope_id == 0;
349 is_multicast() const noexcept { return _M_bytes[0] == 0xFF; }
352 is_link_local() const noexcept
353 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0x80; }
356 is_site_local() const noexcept
357 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0xC0; }
360 is_v4_mapped() const noexcept
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;
369 is_multicast_node_local() const noexcept
370 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x01; }
373 is_multicast_link_local() const noexcept
374 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x02; }
377 is_multicast_site_local() const noexcept
378 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x05; }
381 is_multicast_org_local() const noexcept
382 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x08; }
385 is_multicast_global() const noexcept
386 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x0b; }
388 constexpr bytes_type to_bytes() const noexcept { return _M_bytes; }
390 template<typename _Allocator = allocator<char>>
391 __string_with<_Allocator>
392 to_string(const _Allocator& __a = _Allocator()) const
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()))
400 auto __end = __str.find('\0');
401 if (unsigned long __scope = _M_scope_id)
404 #if _GLIBCXX_USE_C99_STDIO
405 __builtin_snprintf(__p + __end, __str.size() - __end,
408 __builtin_sprintf(__p + __end, "%%%lu", __scope);
417 std::__throw_system_error((int)__unsupported_err());
423 static constexpr address_v6
429 static constexpr address_v6
432 return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
436 template<typename _InternetProtocol>
437 friend class basic_endpoint;
439 friend constexpr bool
440 operator==(const address_v6&, const address_v6&) noexcept;
442 friend constexpr bool
443 operator< (const address_v6&, const address_v6&) noexcept;
446 scope_id_type _M_scope_id;
449 /// Exception type thrown on misuse of IPv4 addresses as IPv6 or vice versa.
450 class bad_address_cast : public bad_cast
453 bad_address_cast() { }
455 const char* what() const noexcept { return "bad address cast"; }
458 /// An IPv4 or IPv6 address.
463 constexpr address() noexcept : _M_v4(), _M_is_v4(true) { }
465 #if __cpp_constexpr_dynamic_alloc
468 address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4)
471 std::_Construct(std::addressof(_M_v4), __a.to_v4());
473 std::_Construct(std::addressof(_M_v6), __a.to_v6());
477 address(const address_v4& __a) noexcept : _M_v4(__a), _M_is_v4(true) { }
480 address(const address_v6& __a) noexcept : _M_v6(__a), _M_is_v4(false) { }
484 operator=(const address& __a) noexcept
494 operator=(const address_v4& __a) noexcept
496 std::_Construct(std::addressof(_M_v4), __a);
502 operator=(const address_v6& __a) noexcept
504 std::_Construct(std::addressof(_M_v6), __a);
511 constexpr bool is_v4() const noexcept { return _M_is_v4; }
512 constexpr bool is_v6() const noexcept { return !_M_is_v4; }
518 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
526 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
531 is_unspecified() const noexcept
532 { return _M_is_v4 ? _M_v4.is_unspecified() : _M_v6.is_unspecified(); }
535 is_loopback() const noexcept
536 { return _M_is_v4 ? _M_v4.is_loopback() : _M_v6.is_loopback(); }
539 is_multicast() const noexcept
540 { return _M_is_v4 ? _M_v4.is_multicast() : _M_v6.is_multicast(); }
542 template<typename _Allocator = allocator<char>>
543 __string_with<_Allocator>
544 to_string(const _Allocator& __a = _Allocator()) const
547 return to_v4().to_string(__a);
548 return to_v6().to_string(__a);
552 template<typename _InternetProtocol>
553 friend class basic_endpoint;
555 friend constexpr bool
556 operator==(const address&, const address&) noexcept;
558 friend constexpr bool
559 operator<(const address&, const address&) noexcept;
569 /** ip::address_v4 comparisons
574 operator==(const address_v4& __a, const address_v4& __b) noexcept
575 { return __a.to_uint() == __b.to_uint(); }
578 operator!=(const address_v4& __a, const address_v4& __b) noexcept
579 { return !(__a == __b); }
582 operator< (const address_v4& __a, const address_v4& __b) noexcept
583 { return __a.to_uint() < __b.to_uint(); }
586 operator> (const address_v4& __a, const address_v4& __b) noexcept
587 { return __b < __a; }
590 operator<=(const address_v4& __a, const address_v4& __b) noexcept
591 { return !(__b < __a); }
594 operator>=(const address_v4& __a, const address_v4& __b) noexcept
595 { return !(__a < __b); }
599 /** ip::address_v6 comparisons
604 operator==(const address_v6& __a, const address_v6& __b) noexcept
606 const auto& __aa = __a._M_bytes;
607 const auto& __bb = __b._M_bytes;
609 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
611 return __i == 16 ? __a.scope_id() == __b.scope_id() : false;
615 operator!=(const address_v6& __a, const address_v6& __b) noexcept
616 { return !(__a == __b); }
619 operator< (const address_v6& __a, const address_v6& __b) noexcept
621 const auto& __aa = __a._M_bytes;
622 const auto& __bb = __b._M_bytes;
624 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
626 return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i];
630 operator> (const address_v6& __a, const address_v6& __b) noexcept
631 { return __b < __a; }
634 operator<=(const address_v6& __a, const address_v6& __b) noexcept
635 { return !(__b < __a); }
638 operator>=(const address_v6& __a, const address_v6& __b) noexcept
639 { return !(__a < __b); }
643 /** ip::address comparisons
648 operator==(const address& __a, const address& __b) noexcept
651 return __b.is_v4() ? __a._M_v4 == __b._M_v4 : false;
652 return __b.is_v4() ? false : __a._M_v6 == __b._M_v6;
656 operator!=(const address& __a, const address& __b) noexcept
657 { return !(__a == __b); }
660 operator< (const address& __a, const address& __b) noexcept
663 return __b.is_v4() ? __a._M_v4 < __b._M_v4 : true;
664 return __b.is_v4() ? false : __a._M_v6 < __b._M_v6;
668 operator> (const address& __a, const address& __b) noexcept
669 { return __b < __a; }
672 operator<=(const address& __a, const address& __b) noexcept
673 { return !(__b < __a); }
676 operator>=(const address& __a, const address& __b) noexcept
677 { return !(__a < __b); }
681 /** ip::address_v4 creation
686 make_address_v4(const address_v4::bytes_type& __b)
687 { return address_v4{__b}; }
690 make_address_v4(address_v4::uint_type __val)
691 { return address_v4{__val}; }
694 make_address_v4(v4_mapped_t, const address_v6& __a)
696 if (!__a.is_v4_mapped())
697 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
699 const auto __v6b = __a.to_bytes();
700 return address_v4::bytes_type(__v6b[12], __v6b[13], __v6b[14], __v6b[15]);
704 make_address_v4(const char* __str, error_code& __ec) noexcept
706 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
708 const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr);
715 __ec = std::make_error_code(std::errc::invalid_argument);
717 __ec.assign(errno, generic_category());
719 __ec = std::make_error_code(__unsupported_err());
725 make_address_v4(const char* __str)
726 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
729 make_address_v4(const string& __str, error_code& __ec) noexcept
730 { return make_address_v4(__str.c_str(), __ec); }
733 make_address_v4(const string& __str)
734 { return make_address_v4(__str.c_str()); }
737 make_address_v4(string_view __str, error_code& __ec) noexcept
739 char __buf[16]; // INET_ADDRSTRLEN isn't defined on Windows
740 auto __len = __str.copy(__buf, sizeof(__buf));
741 if (__len == sizeof(__buf))
743 __ec = std::make_error_code(std::errc::invalid_argument);
748 return make_address_v4(__buf, __ec);
752 make_address_v4(string_view __str)
753 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
757 /** ip::address_v6 creation
762 make_address_v6(const address_v6::bytes_type& __b, scope_id_type __scope = 0)
763 { return address_v6{__b, __scope}; }
766 make_address_v6(v4_mapped_t, const address_v4& __a) noexcept
768 const address_v4::bytes_type __v4b = __a.to_bytes();
769 address_v6::bytes_type __v6b(0, 0, 0, 0, 0, 0, 0, 0,
771 __v4b[0], __v4b[1], __v4b[2], __v4b[3]);
772 return address_v6(__v6b);
776 __make_address_v6(const char* __addr, const char* __scope, error_code& __ec)
778 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
779 address_v6::bytes_type __b;
780 const int __res = ::inet_pton(AF_INET6, __addr, __b.data());
790 unsigned long __val = std::strtoul(__scope, &__eptr, 10);
791 if (__eptr != __scope && !*__eptr
792 && __val <= numeric_limits<scope_id_type>::max())
794 return { __b, static_cast<scope_id_type>(__val) };
796 __ec = std::make_error_code(std::errc::invalid_argument);
799 __ec = std::make_error_code(std::errc::invalid_argument);
801 __ec.assign(errno, generic_category());
803 __ec = std::make_error_code(__unsupported_err());
809 make_address_v6(const char* __str, error_code& __ec) noexcept
811 auto __p = __builtin_strchr(__str, '%');
813 return __make_address_v6(__str, nullptr, __ec);
816 bool __skip_leading_zero = true;
817 while (__str < __p && __out < std::end(__buf))
819 if (!__skip_leading_zero || *__str != '0')
821 if (*__str == ':' || *__str == '.')
822 __skip_leading_zero = true;
824 __skip_leading_zero = false;
829 if (__out == std::end(__buf))
831 __ec = std::make_error_code(std::errc::invalid_argument);
837 return __make_address_v6(__buf, __p + 1, __ec);
842 make_address_v6(const char* __str)
843 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
846 make_address_v6(const string& __str, error_code& __ec) noexcept
848 auto __pos = __str.find('%');
849 if (__pos == string::npos)
850 return __make_address_v6(__str.c_str(), nullptr, __ec);
853 bool __skip_leading_zero = true;
855 while (__n < __pos && __out < std::end(__buf))
857 if (!__skip_leading_zero || __str[__n] != '0')
859 if (__str[__n] == ':' || __str[__n] == '.')
860 __skip_leading_zero = true;
862 __skip_leading_zero = false;
867 if (__out == std::end(__buf))
869 __ec = std::make_error_code(std::errc::invalid_argument);
875 return __make_address_v6(__buf, __str.c_str() + __pos + 1, __ec);
880 make_address_v6(const string& __str)
881 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
884 make_address_v6(string_view __str, error_code& __ec) noexcept
888 char* __scope = nullptr;
889 bool __skip_leading_zero = true;
891 while (__n < __str.length() && __out < std::end(__buf))
893 if (__str[__n] == '%')
896 __out = std::end(__buf);
901 __skip_leading_zero = true;
904 else if (!__skip_leading_zero || __str[__n] != '0')
906 if (__str[__n] == ':' || __str[__n] == '.')
907 __skip_leading_zero = true;
909 __skip_leading_zero = false;
915 if (__out == std::end(__buf))
917 __ec = std::make_error_code(std::errc::invalid_argument);
923 return __make_address_v6(__buf, __scope, __ec);
928 make_address_v6(string_view __str)
929 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
933 /** ip::address creation
938 make_address(const char* __str, error_code& __ec) noexcept
941 address_v6 __v6a = make_address_v6(__str, __ec);
946 address_v4 __v4a = make_address_v4(__str, __ec);
954 make_address(const char* __str)
955 { return make_address(__str, __throw_on_error{"make_address"}); }
958 make_address(const string& __str, error_code& __ec) noexcept; // TODO
961 make_address(const string& __str)
962 { return make_address(__str, __throw_on_error{"make_address"}); }
965 make_address(string_view __str, error_code& __ec) noexcept
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
973 make_address(string_view __str)
974 { return make_address(__str, __throw_on_error{"make_address"}); }
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(); }
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(); }
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(); }
996 template<typename> class basic_address_iterator; // not defined
998 template<> class basic_address_iterator<address_v4>
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;
1009 basic_address_iterator(const address_v4& __a) noexcept
1010 : _M_address(__a) { }
1013 reference operator*() const noexcept { return _M_address; }
1014 pointer operator->() const noexcept { return &_M_address; }
1016 basic_address_iterator&
1017 operator++() noexcept
1019 _M_address = value_type(_M_address.to_uint() + 1);
1023 basic_address_iterator operator++(int) noexcept
1030 basic_address_iterator& operator--() noexcept
1032 _M_address = value_type(_M_address.to_uint() - 1);
1036 basic_address_iterator
1037 operator--(int) noexcept
1045 operator==(const basic_address_iterator& __rhs) const noexcept
1046 { return _M_address == __rhs._M_address; }
1049 operator!=(const basic_address_iterator& __rhs) const noexcept
1050 { return _M_address != __rhs._M_address; }
1053 address_v4 _M_address;
1056 using address_v4_iterator = basic_address_iterator<address_v4>;
1058 template<> class basic_address_iterator<address_v6>
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;
1069 basic_address_iterator(const address_v6& __a) noexcept
1070 : _M_address(__a) { }
1073 reference operator*() const noexcept { return _M_address; }
1074 pointer operator->() const noexcept { return &_M_address; }
1076 basic_address_iterator&
1077 operator++() noexcept; // TODO
1079 basic_address_iterator
1080 operator++(int) noexcept
1087 basic_address_iterator&
1088 operator--() noexcept; // TODO
1090 basic_address_iterator
1091 operator--(int) noexcept
1099 operator==(const basic_address_iterator& __rhs) const noexcept
1100 { return _M_address == __rhs._M_address; }
1103 operator!=(const basic_address_iterator& __rhs) const noexcept
1104 { return _M_address != __rhs._M_address; }
1107 address_v6 _M_address;
1110 using address_v6_iterator = basic_address_iterator<address_v6>;
1112 template<typename> class basic_address_range; // not defined
1114 /** An IPv6 address range.
1118 template<> class basic_address_range<address_v4>
1123 using iterator = basic_address_iterator<address_v4>;
1127 basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
1129 basic_address_range(const address_v4& __first,
1130 const address_v4& __last) noexcept
1131 : _M_begin(__first), _M_end(__last) { }
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; }
1140 size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); }
1143 find(const address_v4& __addr) const noexcept
1145 if (*_M_begin <= __addr && __addr < *_M_end)
1146 return iterator{__addr};
1155 using address_v4_range = basic_address_range<address_v4>;
1159 /** An IPv6 address range.
1163 template<> class basic_address_range<address_v6>
1168 using iterator = basic_address_iterator<address_v6>;
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) { }
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; }
1184 find(const address_v6& __addr) const noexcept
1186 if (*_M_begin <= __addr && __addr < *_M_end)
1187 return iterator{__addr};
1196 using address_v6_range = basic_address_range<address_v6>;
1201 operator==(const network_v4& __a, const network_v4& __b) noexcept;
1204 operator==(const network_v6& __a, const network_v6& __b) noexcept;
1207 /// An IPv4 network address.
1212 constexpr network_v4() noexcept : _M_addr(), _M_prefix_len(0) { }
1215 network_v4(const address_v4& __addr, int __prefix_len)
1216 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1218 if (_M_prefix_len < 0 || _M_prefix_len > 32)
1219 __throw_out_of_range("network_v4: invalid prefix length");
1223 network_v4(const address_v4& __addr, const address_v4& __mask)
1224 : _M_addr(__addr), _M_prefix_len(__builtin_popcount(__mask.to_uint()))
1226 if (_M_prefix_len != 0)
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");
1238 constexpr address_v4 address() const noexcept { return _M_addr; }
1239 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1241 constexpr address_v4
1242 netmask() const noexcept
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};
1250 constexpr address_v4
1251 network() const noexcept
1252 { return address_v4{_M_addr.to_uint() & netmask().to_uint()}; }
1254 constexpr address_v4
1255 broadcast() const noexcept
1256 { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; }
1259 hosts() const noexcept
1262 return { address(), *++address_v4_iterator(address()) };
1263 return { network(), broadcast() };
1266 constexpr network_v4
1267 canonical() const noexcept
1268 { return network_v4(network(), prefix_length()); }
1270 constexpr bool is_host() const noexcept { return _M_prefix_len == 32; }
1273 is_subnet_of(const network_v4& __other) const noexcept
1275 if (__other.prefix_length() < prefix_length())
1277 network_v4 __net(address(), __other.prefix_length());
1278 return __net.canonical() == __other.canonical();
1283 template<typename _Allocator = allocator<char>>
1284 __string_with<_Allocator>
1285 to_string(const _Allocator& __a = _Allocator()) const
1287 return address().to_string(__a) + '/'
1288 + std::to_string(prefix_length());
1296 /// An IPv6 network address.
1301 constexpr network_v6() noexcept : _M_addr(), _M_prefix_len(0) { }
1304 network_v6(const address_v6& __addr, int __prefix_len)
1305 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1307 if (_M_prefix_len < 0 || _M_prefix_len > 128)
1308 __throw_out_of_range("network_v6: invalid prefix length");
1312 constexpr address_v6 address() const noexcept { return _M_addr; }
1313 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1315 constexpr address_v6 network() const noexcept; // TODO
1318 hosts() const noexcept
1321 return { address(), *++address_v6_iterator(address()) };
1322 return {}; // { network(), XXX broadcast() XXX }; // TODO
1325 constexpr network_v6
1326 canonical() const noexcept
1327 { return network_v6{network(), prefix_length()}; }
1329 constexpr bool is_host() const noexcept { return _M_prefix_len == 128; }
1332 is_subnet_of(const network_v6& __other) const noexcept
1334 if (__other.prefix_length() < prefix_length())
1336 network_v6 __net(address(), __other.prefix_length());
1337 return __net.canonical() == __other.canonical();
1342 template<typename _Allocator = allocator<char>>
1343 __string_with<_Allocator>
1344 to_string(const _Allocator& __a = _Allocator()) const
1346 return address().to_string(__a) + '/'
1347 + std::to_string(prefix_length());
1356 /** ip::network_v4 comparisons
1361 operator==(const network_v4& __a, const network_v4& __b) noexcept
1363 return __a.address() == __b.address()
1364 && __a.prefix_length() == __b.prefix_length();
1368 operator!=(const network_v4& __a, const network_v4& __b) noexcept
1369 { return !(__a == __b); }
1373 /** ip::network_v6 comparisons
1378 operator==(const network_v6& __a, const network_v6& __b) noexcept
1380 return __a.address() == __b.address()
1381 && __a.prefix_length() == __b.prefix_length();
1385 operator!=(const network_v6& __a, const network_v6& __b) noexcept
1386 { return !(__a == __b); }
1390 /** ip::network_v4 creation
1395 make_network_v4(const address_v4& __a, int __prefix_len)
1396 { return network_v4{__a, __prefix_len}; }
1399 make_network_v4(const address_v4& __a, const address_v4& __mask)
1400 { return network_v4{ __a, __mask }; }
1402 network_v4 make_network_v4(const char*, error_code&) noexcept; // TODO
1405 make_network_v4(const char* __str)
1406 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1408 network_v4 make_network_v4(const string&, error_code&) noexcept; // TODO
1411 make_network_v4(const string& __str)
1412 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1414 network_v4 make_network_v4(string_view, error_code&) noexcept; // TODO
1417 make_network_v4(string_view __str)
1418 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1422 /** ip::network_v6 creation
1427 make_network_v6(const address_v6& __a, int __prefix_len)
1428 { return network_v6{__a, __prefix_len}; }
1430 network_v6 make_network_v6(const char*, error_code&) noexcept; // TODO
1433 make_network_v6(const char* __str)
1434 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1436 network_v6 make_network_v6(const string&, error_code&) noexcept; // TODO
1439 make_network_v6(const string& __str)
1440 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1442 network_v6 make_network_v6(string_view, error_code&) noexcept; // TODO
1445 make_network_v6(string_view __str)
1446 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
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(); }
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(); }
1463 template<typename _InternetProtocol>
1464 class basic_endpoint
1468 using protocol_type = _InternetProtocol;
1473 basic_endpoint() noexcept : _M_data()
1474 { _M_data._M_v4.sin_family = protocol_type::v4().family(); }
1477 basic_endpoint(const protocol_type& __proto,
1478 port_type __port_num) noexcept
1481 __glibcxx_assert(__proto == protocol_type::v4()
1482 || __proto == protocol_type::v6());
1484 _M_data._M_v4.sin_family = __proto.family();
1485 _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
1489 basic_endpoint(const ip::address& __addr,
1490 port_type __port_num) noexcept
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;
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;
1511 constexpr protocol_type protocol() const noexcept
1513 return _M_is_v6() ? protocol_type::v6() : protocol_type::v4();
1516 constexpr ip::address
1517 address() const noexcept
1522 __builtin_memcpy(&__addr._M_v6._M_bytes,
1523 _M_data._M_v6.sin6_addr.s6_addr, 16);
1524 __addr._M_is_v4 = false;
1528 __builtin_memcpy(&__addr._M_v4._M_addr,
1529 &_M_data._M_v4.sin_addr.s_addr, 4);
1535 address(const ip::address& __addr) noexcept
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;
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;
1553 port() const noexcept
1554 { return address_v4::_S_ntoh_16(_M_data._M_v4.sin_port); }
1557 port(port_type __port_num) noexcept
1558 { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); }
1560 void* data() noexcept { return &_M_data; }
1562 const void* data() const noexcept { return &_M_data; }
1564 constexpr size_t size() const noexcept
1565 { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); }
1571 __throw_length_error("net::ip::basic_endpoint::resize");
1574 constexpr size_t capacity() const noexcept { return sizeof(_M_data); }
1583 constexpr bool _M_is_v6() const noexcept
1584 { return _M_data._M_v4.sin_family == AF_INET6; }
1587 /** basic_endpoint comparisons
1591 template<typename _InternetProtocol>
1593 operator==(const basic_endpoint<_InternetProtocol>& __a,
1594 const basic_endpoint<_InternetProtocol>& __b)
1595 { return __a.address() == __b.address() && __a.port() == __b.port(); }
1597 template<typename _InternetProtocol>
1599 operator!=(const basic_endpoint<_InternetProtocol>& __a,
1600 const basic_endpoint<_InternetProtocol>& __b)
1601 { return !(__a == __b); }
1603 template<typename _InternetProtocol>
1605 operator< (const basic_endpoint<_InternetProtocol>& __a,
1606 const basic_endpoint<_InternetProtocol>& __b)
1608 return __a.address() < __b.address()
1609 || (!(__b.address() < __a.address()) && __a.port() < __b.port());
1612 template<typename _InternetProtocol>
1614 operator> (const basic_endpoint<_InternetProtocol>& __a,
1615 const basic_endpoint<_InternetProtocol>& __b)
1616 { return __b < __a; }
1618 template<typename _InternetProtocol>
1620 operator<=(const basic_endpoint<_InternetProtocol>& __a,
1621 const basic_endpoint<_InternetProtocol>& __b)
1622 { return !(__b < __a); }
1624 template<typename _InternetProtocol>
1626 operator>=(const basic_endpoint<_InternetProtocol>& __a,
1627 const basic_endpoint<_InternetProtocol>& __b)
1628 { return !(__a < __b); }
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)
1638 basic_ostringstream<_CharT, _Traits> __ss;
1640 == basic_endpoint<_InternetProtocol>::protocol_type::v6())
1641 __ss << '[' << __ep.address() << ']';
1643 __ss << __ep.address();
1644 __ss << ':' << __ep.port();
1649 /** Type representing a single result of name/address resolution.
1653 template<typename _InternetProtocol>
1654 class basic_resolver_entry
1658 using protocol_type = _InternetProtocol;
1659 using endpoint_type = typename _InternetProtocol::endpoint;
1662 basic_resolver_entry() { }
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) { }
1669 endpoint_type endpoint() const { return _M_ep; }
1670 operator endpoint_type() const { return _M_ep; }
1672 template<typename _Allocator = allocator<char>>
1673 __string_with<_Allocator>
1674 host_name(const _Allocator& __a = _Allocator()) const
1675 { return { _M_host, __a }; }
1677 template<typename _Allocator = allocator<char>>
1678 __string_with<_Allocator>
1679 service_name(const _Allocator& __a = _Allocator()) const
1680 { return { _M_svc, __a }; }
1683 basic_endpoint<_InternetProtocol> _M_ep;
1688 template<typename _InternetProtocol>
1690 operator==(const basic_resolver_entry<_InternetProtocol>& __a,
1691 const basic_resolver_entry<_InternetProtocol>& __b)
1693 return __a.endpoint() == __b.endpoint()
1694 && __a.host_name() == __b.host_name()
1695 && __a.service_name() == __b.service_name();
1698 template<typename _InternetProtocol>
1700 operator!=(const basic_resolver_entry<_InternetProtocol>& __a,
1701 const basic_resolver_entry<_InternetProtocol>& __b)
1702 { return !(__a == __b); }
1706 /** Base class defining flags for name/address resolution.
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;
1721 static constexpr flags v4_mapped = (flags)AI_V4MAPPED;
1724 static constexpr flags all_matching = (flags)AI_ALL;
1726 #ifdef AI_ADDRCONFIG
1727 static constexpr flags address_configured = (flags)AI_ADDRCONFIG;
1730 friend constexpr flags
1731 operator&(flags __f1, flags __f2) noexcept
1732 { return flags( int(__f1) & int(__f2) ); }
1734 friend constexpr flags
1735 operator|(flags __f1, flags __f2) noexcept
1736 { return flags( int(__f1) | int(__f2) ); }
1738 friend constexpr flags
1739 operator^(flags __f1, flags __f2) noexcept
1740 { return flags( int(__f1) ^ int(__f2) ); }
1742 friend constexpr flags
1743 operator~(flags __f) noexcept
1744 { return flags( ~int(__f) ); }
1746 friend constexpr flags&
1747 operator&=(flags& __f1, flags __f2) noexcept
1748 { return __f1 = (__f1 & __f2); }
1750 friend constexpr flags&
1751 operator|=(flags& __f1, flags __f2) noexcept
1752 { return __f1 = (__f1 | __f2); }
1754 friend constexpr flags&
1755 operator^=(flags& __f1, flags __f2) noexcept
1756 { return __f1 = (__f1 ^ __f2); }
1759 resolver_base() = default;
1760 ~resolver_base() = default;
1763 // TODO define resolver_base::flags static constants in .so for C++14 mode
1767 /** Container for results of name/address resolution.
1771 template<typename _InternetProtocol>
1772 class basic_resolver_results
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;
1786 // construct / copy / destroy:
1788 basic_resolver_results() = default;
1790 basic_resolver_results(const basic_resolver_results&) = default;
1792 basic_resolver_results(basic_resolver_results&&) noexcept = default;
1794 basic_resolver_results&
1795 operator=(const basic_resolver_results&) = default;
1797 basic_resolver_results&
1798 operator=(basic_resolver_results&&) = default;
1800 ~basic_resolver_results() = default;
1803 size_type size() const noexcept { return _M_size; }
1804 size_type max_size() const noexcept { return _M_results.max_size(); }
1806 _GLIBCXX_NODISCARD bool
1807 empty() const noexcept { return _M_results.empty(); }
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(); }
1817 swap(basic_resolver_results& __that) noexcept
1818 { _M_results.swap(__that._M_results); }
1821 friend class basic_resolver<protocol_type>;
1823 basic_resolver_results(string_view, string_view, resolver_base::flags,
1824 error_code&, protocol_type* = nullptr);
1826 basic_resolver_results(const endpoint_type&, error_code&);
1828 forward_list<value_type> _M_results;
1832 template<typename _InternetProtocol>
1834 operator==(const basic_resolver_results<_InternetProtocol>& __a,
1835 const basic_resolver_results<_InternetProtocol>& __b)
1837 return __a.size() == __b.size()
1838 && std::equal(__a.begin(), __a.end(), __b.begin());
1841 template<typename _InternetProtocol>
1843 operator!=(const basic_resolver_results<_InternetProtocol>& __a,
1844 const basic_resolver_results<_InternetProtocol>& __b)
1845 { return !(__a == __b); }
1849 /// Perform name/address resolution.
1850 template<typename _InternetProtocol>
1851 class basic_resolver : public resolver_base
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>;
1861 // construct / copy / destroy:
1863 explicit basic_resolver(io_context& __ctx) : _M_ctx(&__ctx) { }
1865 basic_resolver(const basic_resolver&) = delete;
1867 basic_resolver(basic_resolver&& __rhs) noexcept
1868 : _M_ctx(__rhs._M_ctx)
1869 { } // TODO move state/tasks etc.
1871 ~basic_resolver() { cancel(); }
1873 basic_resolver& operator=(const basic_resolver&) = delete;
1875 basic_resolver& operator=(basic_resolver&& __rhs)
1878 _M_ctx = __rhs._M_ctx;
1879 // TODO move state/tasks etc.
1883 // basic_resolver operations:
1885 executor_type get_executor() noexcept { return _M_ctx->get_executor(); }
1887 void cancel() { } // TODO
1890 resolve(string_view __host_name, string_view __service_name)
1892 return resolve(__host_name, __service_name, resolver_base::flags(),
1893 __throw_on_error{"basic_resolver::resolve"});
1897 resolve(string_view __host_name, string_view __service_name,
1900 return resolve(__host_name, __service_name, resolver_base::flags(),
1905 resolve(string_view __host_name, string_view __service_name, flags __f)
1907 return resolve(__host_name, __service_name, __f,
1908 __throw_on_error{"basic_resolver::resolve"});
1912 resolve(string_view __host_name, string_view __service_name, flags __f,
1914 { return {__host_name, __service_name, __f, __ec}; }
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)
1921 return async_resolve(__host_name, __service_name,
1922 resolver_base::flags(),
1923 forward<_CompletionToken>(__token));
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
1932 resolve(const protocol_type& __protocol,
1933 string_view __host_name, string_view __service_name)
1935 return resolve(__protocol, __host_name, __service_name,
1936 resolver_base::flags(),
1937 __throw_on_error{"basic_resolver::resolve"});
1941 resolve(const protocol_type& __protocol,
1942 string_view __host_name, string_view __service_name,
1945 return resolve(__protocol, __host_name, __service_name,
1946 resolver_base::flags(), __ec);
1950 resolve(const protocol_type& __protocol,
1951 string_view __host_name, string_view __service_name, flags __f)
1953 return resolve(__protocol, __host_name, __service_name, __f,
1954 __throw_on_error{"basic_resolver::resolve"});
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}; }
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)
1969 return async_resolve(__protocol, __host_name, __service_name,
1970 resolver_base::flags(),
1971 forward<_CompletionToken>(__token));
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
1981 resolve(const endpoint_type& __ep)
1982 { return resolve(__ep, __throw_on_error{"basic_resolver::resolve"}); }
1985 resolve(const endpoint_type& __ep, error_code& __ec)
1986 { return { __ep, __ec }; }
1988 template<typename _CompletionToken> // TODO
1989 __deduced_t<_CompletionToken, void(error_code, results_type)>
1990 async_resolve(const endpoint_type& __ep, _CompletionToken&& __token);
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)
2003 #ifdef _GLIBCXX_HAVE_NETDB_H
2005 const char* __h = __host_name.data()
2006 ? (__host = __host_name.to_string()).c_str()
2009 const char* __s = __service_name.data()
2010 ? (__svc = __service_name.to_string()).c_str()
2013 ::addrinfo __hints{ };
2014 __hints.ai_flags = static_cast<int>(__f);
2017 __hints.ai_family = __protocol->family();
2018 __hints.ai_socktype = __protocol->type();
2019 __hints.ai_protocol = __protocol->protocol();
2023 auto __p = endpoint_type{}.protocol();
2024 __hints.ai_family = AF_UNSPEC;
2025 __hints.ai_socktype = __p.type();
2026 __hints.ai_protocol = __p.protocol();
2029 struct __scoped_addrinfo
2031 ~__scoped_addrinfo() { if (_M_p) ::freeaddrinfo(_M_p); }
2032 ::addrinfo* _M_p = nullptr;
2035 if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p))
2037 __ec = ip::__make_resolver_error_code(__err, errno);
2043 auto __tail = _M_results.before_begin();
2044 for (auto __ai = __sai._M_p; __ai != nullptr; __ai = __ai->ai_next)
2046 if (__ai->ai_family == AF_INET || __ai->ai_family == AF_INET6)
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);
2056 __ec = std::make_error_code(errc::operation_not_supported);
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)
2065 #ifdef _GLIBCXX_HAVE_NETDB_H
2066 char __host_name[1025]; // glibc NI_MAXHOST
2067 char __service_name[32]; // glibc NI_MAXSERV
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),
2078 __flags |= NI_NUMERICSERV;
2079 __err = ::getnameinfo(__sa, __ep.size(),
2080 __host_name, sizeof(__host_name),
2081 __service_name, sizeof(__service_name),
2085 __ec = ip::__make_resolver_error_code(__err, errno);
2089 _M_results.emplace_front(__ep, __host_name, __service_name);
2093 __ec = std::make_error_code(errc::operation_not_supported);
2097 /** The name of the local host.
2101 template<typename _Allocator>
2102 __string_with<_Allocator>
2103 host_name(const _Allocator& __a, error_code& __ec)
2105 #ifdef HOST_NAME_MAX
2106 constexpr size_t __maxlen = HOST_NAME_MAX;
2108 constexpr size_t __maxlen = 256;
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 };
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"}); }
2123 host_name(error_code& __ec)
2124 { return host_name(std::allocator<char>{}, __ec); }
2128 { return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); }
2133 /// The TCP byte-stream protocol.
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.
2145 /// Disable coalescing of small segments (i.e. the Nagle algorithm).
2146 struct no_delay : __sockopt_crtp<no_delay, bool>
2148 using __sockopt_crtp::__sockopt_crtp;
2149 using __sockopt_crtp::operator=;
2151 static const int _S_level = IPPROTO_TCP;
2152 static const int _S_name = TCP_NODELAY;
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); }
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; }
2170 constexpr explicit tcp(int __family) : _M_family(__family) { }
2180 operator==(const tcp& __a, const tcp& __b) noexcept
2181 { return __a.family() == __b.family(); }
2184 operator!=(const tcp& __a, const tcp& __b) noexcept
2185 { return !(__a == __b); }
2188 #endif // IPPROTO_TCP
2191 /// The UDP datagram protocol.
2196 using endpoint = basic_endpoint<udp>;
2197 using resolver = basic_resolver<udp>;
2198 using socket = basic_datagram_socket<udp>;
2201 static constexpr udp v4() noexcept { return udp(AF_INET); }
2202 static constexpr udp v6() noexcept { return udp(AF_INET6); }
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; }
2211 constexpr explicit udp(int __family) : _M_family(__family) { }
2221 operator==(const udp& __a, const udp& __b) noexcept
2222 { return __a.family() == __b.family(); }
2225 operator!=(const udp& __a, const udp& __b) noexcept
2226 { return !(__a == __b); }
2229 #endif // IPPROTO_UDP
2231 #if defined IPPROTO_IP && defined IPPROTO_IPV6
2233 /// Restrict a socket created for an IPv6 protocol to IPv6 only.
2234 class v6_only : public __sockopt_crtp<v6_only, bool>
2237 using __sockopt_crtp::__sockopt_crtp;
2238 using __sockopt_crtp::operator=;
2241 friend __sockopt_crtp<v6_only, bool>;
2242 static const int _S_level = IPPROTO_IPV6;
2243 static const int _S_name = IPV6_V6ONLY;
2248 /// Set the default number of hops (TTL) for outbound datagrams.
2249 class hops : public __sockopt_crtp<hops>
2252 using __sockopt_crtp::__sockopt_crtp;
2253 using __sockopt_crtp::operator=;
2255 template<typename _Protocol>
2257 level(const _Protocol& __p) const noexcept
2258 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2260 template<typename _Protocol>
2262 name(const _Protocol& __p) const noexcept
2263 { return __p.family() == AF_INET6 ? IPV6_UNICAST_HOPS : IP_TTL; }
2265 } // namespace unicast
2273 __mcastopt(const address& __grp) noexcept
2274 : __mcastopt(__grp.is_v4() ? __mcastopt(__grp.to_v4()) : __mcastopt(__grp.to_v6()))
2278 __mcastopt(const address_v4& __grp,
2279 const address_v4& __iface = address_v4::any()) noexcept
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();
2285 _M_v4.imr_multiaddr.s_addr = __builtin_bswap32(__grp.to_uint());
2286 _M_v4.imr_interface.s_addr = __builtin_bswap32(__iface.to_uint());
2291 __mcastopt(const address_v6& __grp, unsigned int __iface = 0) noexcept
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;
2298 template<typename _Protocol>
2300 level(const _Protocol& __p) const noexcept
2301 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2303 template<typename _Protocol>
2305 data(const _Protocol& __p) const noexcept
2306 { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
2308 template<typename _Protocol>
2310 size(const _Protocol& __p) const noexcept
2311 { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
2314 ipv6_mreq _M_v6 = {};
2318 /// Request that a socket joins a multicast group.
2319 class join_group : private __mcastopt
2322 using __mcastopt::__mcastopt;
2323 using __mcastopt::level;
2324 using __mcastopt::data;
2325 using __mcastopt::size;
2327 template<typename _Protocol>
2329 name(const _Protocol& __p) const noexcept
2331 if (__p.family() == AF_INET6)
2332 return IPV6_JOIN_GROUP;
2333 return IP_ADD_MEMBERSHIP;
2337 /// Request that a socket leaves a multicast group.
2338 class leave_group : private __mcastopt
2341 using __mcastopt::__mcastopt;
2342 using __mcastopt::level;
2343 using __mcastopt::data;
2344 using __mcastopt::size;
2346 template<typename _Protocol>
2348 name(const _Protocol& __p) const noexcept
2350 if (__p.family() == AF_INET6)
2351 return IPV6_LEAVE_GROUP;
2352 return IP_DROP_MEMBERSHIP;
2356 /// Specify the network interface for outgoing multicast datagrams.
2357 class outbound_interface
2361 outbound_interface(const address_v4& __v4) noexcept
2363 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
2364 _M_v4.s_addr = __v4.to_uint();
2366 _M_v4.s_addr = __builtin_bswap32(__v4.to_uint());
2371 outbound_interface(unsigned int __v6) noexcept
2372 : _M_v4(), _M_v6(__v6)
2375 template<typename _Protocol>
2377 level(const _Protocol& __p) const noexcept
2378 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2380 template<typename _Protocol>
2382 name(const _Protocol& __p) const noexcept
2384 return __p.family() == AF_INET6
2385 ? IPV6_MULTICAST_IF : IP_MULTICAST_IF;
2388 template<typename _Protocol>
2390 data(const _Protocol& __p) const noexcept
2391 { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
2393 template<typename _Protocol>
2395 size(const _Protocol& __p) const noexcept
2396 { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
2403 /// Set the default number of hops (TTL) for outbound datagrams.
2404 class hops : public __sockopt_crtp<hops>
2407 using __sockopt_crtp::__sockopt_crtp;
2408 using __sockopt_crtp::operator=;
2410 template<typename _Protocol>
2412 level(const _Protocol& __p) const noexcept
2413 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2415 template<typename _Protocol>
2417 name(const _Protocol& __p) const noexcept
2419 return __p.family() == AF_INET6
2420 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL;
2424 /// Set whether datagrams are delivered back to the local application.
2425 class enable_loopback : public __sockopt_crtp<enable_loopback, bool>
2428 using __sockopt_crtp::__sockopt_crtp;
2429 using __sockopt_crtp::operator=;
2431 template<typename _Protocol>
2433 level(const _Protocol& __p) const noexcept
2434 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2436 template<typename _Protocol>
2438 name(const _Protocol& __p) const noexcept
2440 return __p.family() == AF_INET6
2441 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP;
2445 } // namespace multicast
2447 #endif // IPPROTO_IP && IPPROTO_IPV6
2454 } // namespace experimental
2457 struct is_error_condition_enum<experimental::net::v1::ip::resolver_errc>
2458 : public true_type {};
2461 template<typename _Tp> struct hash;
2463 struct hash<experimental::net::v1::ip::address>
2464 : __hash_base<size_t, experimental::net::v1::ip::address>
2467 operator()(const experimental::net::v1::ip::address& __a) const
2470 return _Hash_impl::hash(__a.to_v4());
2472 return _Hash_impl::hash(__a.to_v6());
2477 struct hash<experimental::net::v1::ip::address_v4>
2478 : __hash_base<size_t, experimental::net::v1::ip::address_v4>
2481 operator()(const experimental::net::v1::ip::address_v4& __a) const
2482 { return _Hash_impl::hash(__a.to_bytes()); }
2485 template<> struct hash<experimental::net::v1::ip::address_v6>
2486 : __hash_base<size_t, experimental::net::v1::ip::address_v6>
2489 operator()(const experimental::net::v1::ip::address_v6& __a) const
2490 { return _Hash_impl::hash(__a.to_bytes()); }
2493 _GLIBCXX_END_NAMESPACE_VERSION
2498 #endif // _GLIBCXX_EXPERIMENTAL_INTERNET