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
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 namespace std _GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 namespace experimental
76 /** @addtogroup networking-ts
80 #ifdef _GLIBCXX_HAVE_NETDB_H
81 /** Error codes for resolver errors.
85 enum class resolver_errc : int {
86 host_not_found = EAI_NONAME,
87 host_not_found_try_again = EAI_AGAIN,
88 service_not_found = EAI_SERVICE
91 /// Error category for resolver errors.
92 inline const error_category& resolver_category() noexcept // TODO non-inline
94 struct __cat : error_category
96 const char* name() const noexcept { return "resolver"; }
97 std::string message(int __e) const { return ::gai_strerror(__e); }
98 virtual void __message(int) { } // TODO dual ABI XXX
104 error_code make_error_code(resolver_errc __e) noexcept
105 { return error_code(static_cast<int>(__e), resolver_category()); }
107 error_condition make_error_condition(resolver_errc __e) noexcept
108 { return error_condition(static_cast<int>(__e), resolver_category()); }
113 using port_type = uint_least16_t; ///< Type used for port numbers.
114 using scope_id_type = uint_least32_t; ///< Type used for IPv6 scope IDs.
116 /// Convenience alias for constraining allocators for strings.
117 template<typename _Alloc>
119 = enable_if_t<std::is_same<typename _Alloc::value_type, char>::value,
120 std::basic_string<char, std::char_traits<char>, _Alloc>>;
122 /** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses.
126 struct v4_mapped_t {};
127 constexpr v4_mapped_t v4_mapped;
136 using uint_type = uint_least32_t;
138 struct bytes_type : array<unsigned char, 4>
140 template<typename... _Tp>
142 bytes_type(_Tp... __tp)
143 : array<unsigned char, 4>{{static_cast<unsigned char>(__tp)...}}
146 for (auto __b : *this)
148 __throw_out_of_range("invalid address_v4::bytes_type value");
154 constexpr address_v4() noexcept : _M_addr(0) { }
156 constexpr address_v4(const address_v4& a) noexcept = default;
159 address_v4(const bytes_type& __b)
160 : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
164 address_v4(uint_type __val) : _M_addr(_S_hton_32(__val))
166 #if UINT_LEAST32_MAX > 0xFFFFFFFF
167 if (__val > 0xFFFFFFFF)
168 __throw_out_of_range("invalid address_v4::uint_type value");
173 address_v4& operator=(const address_v4& a) noexcept = default;
176 constexpr bool is_unspecified() const noexcept { return to_uint() == 0; }
179 is_loopback() const noexcept
180 { return (to_uint() & 0xFF000000) == 0x7F000000; }
183 is_multicast() const noexcept
184 { return (to_uint() & 0xF0000000) == 0xE0000000; }
187 to_bytes() const noexcept
190 (_M_addr >> 24) & 0xFF,
191 (_M_addr >> 16) & 0xFF,
192 (_M_addr >> 8) & 0xFF,
198 to_uint() const noexcept { return _S_ntoh_32(_M_addr); }
200 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
201 template<typename _Allocator = allocator<char>>
202 __string_with<_Allocator>
203 to_string(const _Allocator& __a = _Allocator()) const
205 __string_with<_Allocator> __str(__a);
206 __str.resize(INET6_ADDRSTRLEN);
207 if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
208 __str.erase(__str.find('\0'));
216 static constexpr address_v4 any() noexcept { return address_v4{}; }
219 address_v4 loopback() noexcept { return address_v4{0x7F000001}; }
222 address_v4 broadcast() noexcept { return address_v4{0xFFFFFFFF}; }
225 template<typename _InternetProtocol>
226 friend class basic_endpoint;
228 friend address_v4 make_address_v4(const char*, error_code&) noexcept;
230 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
231 static constexpr uint16_t _S_hton_16(uint16_t __h) { return __h; }
232 static constexpr uint16_t _S_ntoh_16(uint16_t __n) { return __n; }
233 static constexpr uint32_t _S_hton_32(uint32_t __h) { return __h; }
234 static constexpr uint32_t _S_ntoh_32(uint32_t __n) { return __n; }
236 static constexpr uint16_t
237 _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); }
239 static constexpr uint16_t
240 _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); }
242 static constexpr uint32_t
243 _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); }
245 static constexpr uint32_t
246 _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); }
249 in_addr_t _M_addr; // network byte order
257 struct bytes_type : array<unsigned char, 16>
259 template<typename... _Tp> explicit constexpr bytes_type(_Tp... __t)
260 : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}} { }
264 constexpr address_v6() noexcept : _M_bytes(), _M_scope_id() { }
266 constexpr address_v6(const address_v6& __a) noexcept = default;
269 address_v6(const bytes_type& __bytes, scope_id_type __scope = 0)
270 : _M_bytes(__bytes), _M_scope_id(__scope)
274 address_v6& operator=(const address_v6& __a) noexcept = default;
277 void scope_id(scope_id_type __id) noexcept { _M_scope_id = __id; }
279 constexpr scope_id_type scope_id() const noexcept { return _M_scope_id; }
282 is_unspecified() const noexcept
284 for (int __i = 0; __i < 16; ++__i)
285 if (_M_bytes[__i] != 0x00)
287 return _M_scope_id == 0;
291 is_loopback() const noexcept
293 for (int __i = 0; __i < 15; ++__i)
294 if (_M_bytes[__i] != 0x00)
296 return _M_bytes[15] == 0x01 && _M_scope_id == 0;
300 is_multicast() const noexcept { return _M_bytes[0] == 0xFF; }
303 is_link_local() const noexcept
304 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0x80; }
307 is_site_local() const noexcept
308 { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0xC0; }
311 is_v4_mapped() const noexcept
313 const bytes_type& __b = _M_bytes;
314 return __b[0] == 0 && __b[1] == 0 && __b[ 2] == 0 && __b[ 3] == 0
315 && __b[4] == 0 && __b[5] == 0 && __b[ 6] == 0 && __b[ 7] == 0
316 && __b[8] == 0 && __b[9] == 0 && __b[10] == 0xFF && __b[11] == 0xFF;
320 is_multicast_node_local() const noexcept
321 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x01; }
324 is_multicast_link_local() const noexcept
325 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x02; }
328 is_multicast_site_local() const noexcept
329 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x05; }
332 is_multicast_org_local() const noexcept
333 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x08; }
336 is_multicast_global() const noexcept
337 { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x0b; }
339 constexpr bytes_type to_bytes() const noexcept { return _M_bytes; }
341 #ifdef _GLIBCXX_HAVE_ARPA_INET_H
342 template<typename _Allocator = allocator<char>>
343 __string_with<_Allocator>
344 to_string(const _Allocator& __a = _Allocator()) const
346 __string_with<_Allocator> __str(__a);
347 __str.resize(INET6_ADDRSTRLEN);
348 if (inet_ntop(AF_INET6, &_M_bytes, &__str.front(), __str.size()))
349 __str.erase(__str.find('\0'));
358 static constexpr address_v6
364 static constexpr address_v6
368 __addr._M_bytes[15] = 1;
373 template<typename _InternetProtocol>
374 friend class basic_endpoint;
376 friend constexpr bool
377 operator==(const address_v6&, const address_v6&) noexcept;
379 friend constexpr bool
380 operator< (const address_v6&, const address_v6&) noexcept;
383 scope_id_type _M_scope_id;
386 /// Exception type thrown on misuse of IPv4 addresses as IPv6 or vice versa.
387 class bad_address_cast : public bad_cast
390 bad_address_cast() { }
392 const char* what() const noexcept { return "bad address cast"; }
395 /// An IPv4 or IPv6 address.
400 constexpr address() noexcept : _M_v4(), _M_is_v4(true) { }
403 address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4)
406 ::new (std::addressof(_M_v4)) address_v4(__a.to_v4());
408 ::new (std::addressof(_M_v6)) address_v6(__a.to_v6());
412 address(const address_v4& __a) noexcept : _M_v4(__a), _M_is_v4(true) { }
415 address(const address_v6& __a) noexcept : _M_v6(__a), _M_is_v4(false) { }
419 operator=(const address& __a) noexcept
429 operator=(const address_v4& __a) noexcept
431 ::new (std::addressof(_M_v4)) address_v4(__a);
437 operator=(const address_v6& __a) noexcept
439 ::new (std::addressof(_M_v6)) address_v6(__a);
446 constexpr bool is_v4() const noexcept { return _M_is_v4; }
447 constexpr bool is_v6() const noexcept { return !_M_is_v4; }
453 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
461 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
466 is_unspecified() const noexcept
467 { return _M_is_v4 ? _M_v4.is_unspecified() : _M_v6.is_unspecified(); }
470 is_loopback() const noexcept
471 { return _M_is_v4 ? _M_v4.is_loopback() : _M_v6.is_loopback(); }
474 is_multicast() const noexcept
475 { return _M_is_v4 ? _M_v4.is_multicast() : _M_v6.is_multicast(); }
477 template<typename _Allocator = allocator<char>>
478 __string_with<_Allocator>
479 to_string(const _Allocator& __a = _Allocator()) const
482 return to_v4().to_string(__a);
483 return to_v6().to_string(__a);
487 template<typename _InternetProtocol>
488 friend class basic_endpoint;
490 friend constexpr bool
491 operator==(const address&, const address&) noexcept;
493 friend constexpr bool
494 operator<(const address&, const address&) noexcept;
504 /** ip::address_v4 comparisons
509 operator==(const address_v4& __a, const address_v4& __b) noexcept
510 { return __a.to_uint() == __b.to_uint(); }
513 operator!=(const address_v4& __a, const address_v4& __b) noexcept
514 { return !(__a == __b); }
517 operator< (const address_v4& __a, const address_v4& __b) noexcept
518 { return __a.to_uint() < __b.to_uint(); }
521 operator> (const address_v4& __a, const address_v4& __b) noexcept
522 { return __b < __a; }
525 operator<=(const address_v4& __a, const address_v4& __b) noexcept
526 { return !(__b < __a); }
529 operator>=(const address_v4& __a, const address_v4& __b) noexcept
530 { return !(__a < __b); }
534 /** ip::address_v6 comparisons
539 operator==(const address_v6& __a, const address_v6& __b) noexcept
541 const auto& __aa = __a._M_bytes;
542 const auto& __bb = __b._M_bytes;
544 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
546 return __i == 16 ? __a.scope_id() == __b.scope_id() : false;
550 operator!=(const address_v6& __a, const address_v6& __b) noexcept
551 { return !(__a == __b); }
554 operator< (const address_v6& __a, const address_v6& __b) noexcept
556 const auto& __aa = __a._M_bytes;
557 const auto& __bb = __b._M_bytes;
559 for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
561 return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i];
565 operator> (const address_v6& __a, const address_v6& __b) noexcept
566 { return __b < __a; }
569 operator<=(const address_v6& __a, const address_v6& __b) noexcept
570 { return !(__b < __a); }
573 operator>=(const address_v6& __a, const address_v6& __b) noexcept
574 { return !(__a < __b); }
578 /** ip::address comparisons
583 operator==(const address& __a, const address& __b) noexcept
586 return __b.is_v4() ? __a._M_v4 == __b._M_v4 : false;
587 return __b.is_v4() ? false : __a._M_v6 == __b._M_v6;
591 operator!=(const address& __a, const address& __b) noexcept
592 { return !(__a == __b); }
595 operator< (const address& __a, const address& __b) noexcept
598 return __b.is_v4() ? __a._M_v4 < __b._M_v4 : true;
599 return __b.is_v4() ? false : __a._M_v6 < __b._M_v6;
603 operator> (const address& __a, const address& __b) noexcept
604 { return __b < __a; }
607 operator<=(const address& __a, const address& __b) noexcept
608 { return !(__b < __a); }
611 operator>=(const address& __a, const address& __b) noexcept
612 { return !(__a < __b); }
616 /** ip::address_v4 creation
621 make_address_v4(const address_v4::bytes_type& __b)
622 { return address_v4{__b}; }
625 make_address_v4(address_v4::uint_type __val)
626 { return address_v4{__val}; }
629 make_address_v4(v4_mapped_t, const address_v6& __a)
631 if (!__a.is_v4_mapped())
632 _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
634 const auto __v6b = __a.to_bytes();
635 return address_v4::bytes_type(__v6b[12], __v6b[13], __v6b[14], __v6b[15]);
639 make_address_v4(const char* __str, error_code& __ec) noexcept
642 const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr);
649 __ec = std::make_error_code(std::errc::invalid_argument);
651 __ec.assign(errno, generic_category());
656 make_address_v4(const char* __str)
657 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
660 make_address_v4(const string& __str, error_code& __ec) noexcept
661 { return make_address_v4(__str.c_str(), __ec); }
664 make_address_v4(const string& __str)
665 { return make_address_v4(__str.c_str()); }
668 make_address_v4(string_view __str, error_code& __ec) noexcept
670 char __buf[INET_ADDRSTRLEN];
671 auto __len = __str.copy(__buf, sizeof(__buf));
672 if (__len == sizeof(__buf))
674 __ec = std::make_error_code(std::errc::invalid_argument);
679 return make_address_v4(__buf, __ec);
683 make_address_v4(string_view __str)
684 { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
688 /** ip::address_v6 creation
693 make_address_v6(const address_v6::bytes_type& __b, scope_id_type __scope = 0)
694 { return address_v6{__b, __scope}; }
697 make_address_v6(v4_mapped_t, const address_v4& __a) noexcept
699 const address_v4::bytes_type __v4b = __a.to_bytes();
700 address_v6::bytes_type __v6b(0, 0, 0, 0, 0, 0, 0, 0,
702 __v4b[0], __v4b[1], __v4b[2], __v4b[3]);
703 return address_v6(__v6b);
707 __make_address_v6(const char* __addr, const char* __scope, error_code& __ec)
709 address_v6::bytes_type __b;
710 int __res = ::inet_pton(AF_INET6, __addr, __b.data());
720 unsigned long __val = std::strtoul(__scope, &__eptr, 10);
721 if (__eptr != __scope && !*__eptr
722 && __val <= numeric_limits<scope_id_type>::max())
724 return { __b, static_cast<scope_id_type>(__val) };
726 __ec = std::make_error_code(std::errc::invalid_argument);
729 __ec = std::make_error_code(std::errc::invalid_argument);
731 __ec.assign(errno, generic_category());
736 make_address_v6(const char* __str, error_code& __ec) noexcept
738 auto __p = __builtin_strchr(__str, '%');
740 return __make_address_v6(__str, nullptr, __ec);
743 bool __skip_leading_zero = true;
744 while (__str < __p && __out < std::end(__buf))
746 if (!__skip_leading_zero || *__str != '0')
748 if (*__str == ':' || *__str == '.')
749 __skip_leading_zero = true;
751 __skip_leading_zero = false;
756 if (__out == std::end(__buf))
758 __ec = std::make_error_code(std::errc::invalid_argument);
764 return __make_address_v6(__buf, __p + 1, __ec);
769 make_address_v6(const char* __str)
770 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
773 make_address_v6(const string& __str, error_code& __ec) noexcept
775 auto __pos = __str.find('%');
776 if (__pos == string::npos)
777 return __make_address_v6(__str.c_str(), nullptr, __ec);
780 bool __skip_leading_zero = true;
782 while (__n < __pos && __out < std::end(__buf))
784 if (!__skip_leading_zero || __str[__n] != '0')
786 if (__str[__n] == ':' || __str[__n] == '.')
787 __skip_leading_zero = true;
789 __skip_leading_zero = false;
794 if (__out == std::end(__buf))
796 __ec = std::make_error_code(std::errc::invalid_argument);
802 return __make_address_v6(__buf, __str.c_str() + __pos + 1, __ec);
807 make_address_v6(const string& __str)
808 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
811 make_address_v6(string_view __str, error_code& __ec) noexcept
815 char* __scope = nullptr;
816 bool __skip_leading_zero = true;
818 while (__n < __str.length() && __out < std::end(__buf))
820 if (__str[__n] == '%')
823 __out = std::end(__buf);
828 __skip_leading_zero = true;
831 else if (!__skip_leading_zero || __str[__n] != '0')
833 if (__str[__n] == ':' || __str[__n] == '.')
834 __skip_leading_zero = true;
836 __skip_leading_zero = false;
842 if (__out == std::end(__buf))
844 __ec = std::make_error_code(std::errc::invalid_argument);
850 return __make_address_v6(__buf, __scope, __ec);
855 make_address_v6(string_view __str)
856 { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
860 /** ip::address creation
865 make_address(const char* __str, error_code& __ec) noexcept
868 address_v6 __v6a = make_address_v6(__str, __ec);
873 address_v4 __v4a = make_address_v4(__str, __ec);
881 make_address(const char* __str)
882 { return make_address(__str, __throw_on_error{"make_address"}); }
885 make_address(const string& __str, error_code& __ec) noexcept; // TODO
888 make_address(const string& __str)
889 { return make_address(__str, __throw_on_error{"make_address"}); }
892 make_address(string_view __str, error_code& __ec) noexcept
894 if (__str.rfind('\0') != string_view::npos)
895 return make_address(__str.data(), __ec);
896 return make_address(__str.to_string(), __ec); // TODO don't allocate
900 make_address(string_view __str)
901 { return make_address(__str, __throw_on_error{"make_address"}); }
906 template<typename _CharT, typename _Traits>
907 inline basic_ostream<_CharT, _Traits>&
908 operator<<(basic_ostream<_CharT, _Traits>& __os, const address& __a)
909 { return __os << __a.to_string(); }
911 /// ip::address_v4 I/O
912 template<typename _CharT, typename _Traits>
913 inline basic_ostream<_CharT, _Traits>&
914 operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v4& __a)
915 { return __os << __a.to_string(); }
917 /// ip::address_v6 I/O
918 template<typename _CharT, typename _Traits>
919 inline basic_ostream<_CharT, _Traits>&
920 operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v6& __a)
921 { return __os << __a.to_string(); }
923 template<typename> class basic_address_iterator; // not defined
925 template<> class basic_address_iterator<address_v4>
929 using value_type = address_v4;
930 using difference_type = ptrdiff_t;
931 using pointer = const address_v4*;
932 using reference = const address_v4&;
933 using iterator_category = input_iterator_tag;
936 basic_address_iterator(const address_v4& __a) noexcept
937 : _M_address(__a) { }
940 reference operator*() const noexcept { return _M_address; }
941 pointer operator->() const noexcept { return &_M_address; }
943 basic_address_iterator&
944 operator++() noexcept
946 _M_address = value_type(_M_address.to_uint() + 1);
950 basic_address_iterator operator++(int) noexcept
957 basic_address_iterator& operator--() noexcept
959 _M_address = value_type(_M_address.to_uint() - 1);
963 basic_address_iterator
964 operator--(int) noexcept
972 operator==(const basic_address_iterator& __rhs) const noexcept
973 { return _M_address == __rhs._M_address; }
976 operator!=(const basic_address_iterator& __rhs) const noexcept
977 { return _M_address != __rhs._M_address; }
980 address_v4 _M_address;
983 using address_v4_iterator = basic_address_iterator<address_v4>;
985 template<> class basic_address_iterator<address_v6>
989 using value_type = address_v6;
990 using difference_type = ptrdiff_t;
991 using pointer = const address_v6*;
992 using reference = const address_v6&;
993 using iterator_category = input_iterator_tag;
996 basic_address_iterator(const address_v6& __a) noexcept
997 : _M_address(__a) { }
1000 reference operator*() const noexcept { return _M_address; }
1001 pointer operator->() const noexcept { return &_M_address; }
1003 basic_address_iterator&
1004 operator++() noexcept; // TODO
1006 basic_address_iterator
1007 operator++(int) noexcept
1014 basic_address_iterator&
1015 operator--() noexcept; // TODO
1017 basic_address_iterator
1018 operator--(int) noexcept
1026 operator==(const basic_address_iterator& __rhs) const noexcept
1027 { return _M_address == __rhs._M_address; }
1030 operator!=(const basic_address_iterator& __rhs) const noexcept
1031 { return _M_address != __rhs._M_address; }
1034 address_v6 _M_address;
1037 using address_v6_iterator = basic_address_iterator<address_v6>;
1039 template<typename> class basic_address_range; // not defined
1041 /** An IPv6 address range.
1045 template<> class basic_address_range<address_v4>
1050 using iterator = basic_address_iterator<address_v4>;
1054 basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
1056 basic_address_range(const address_v4& __first,
1057 const address_v4& __last) noexcept
1058 : _M_begin(__first), _M_end(__last) { }
1062 iterator begin() const noexcept { return _M_begin; }
1063 iterator end() const noexcept { return _M_end; }
1064 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
1067 size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); }
1070 find(const address_v4& __addr) const noexcept
1072 if (*_M_begin <= __addr && __addr < *_M_end)
1073 return iterator{__addr};
1082 using address_v4_range = basic_address_range<address_v4>;
1086 /** An IPv6 address range.
1090 template<> class basic_address_range<address_v6>
1095 using iterator = basic_address_iterator<address_v6>;
1099 basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
1100 basic_address_range(const address_v6& __first,
1101 const address_v6& __last) noexcept
1102 : _M_begin(__first), _M_end(__last) { }
1106 iterator begin() const noexcept { return _M_begin; }
1107 iterator end() const noexcept { return _M_end; }
1108 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
1111 find(const address_v6& __addr) const noexcept
1113 if (*_M_begin <= __addr && __addr < *_M_end)
1114 return iterator{__addr};
1123 using address_v6_range = basic_address_range<address_v6>;
1128 operator==(const network_v4& __a, const network_v4& __b) noexcept;
1131 operator==(const network_v6& __a, const network_v6& __b) noexcept;
1134 /// An IPv4 network address.
1139 constexpr network_v4() noexcept : _M_addr(), _M_prefix_len(0) { }
1142 network_v4(const address_v4& __addr, int __prefix_len)
1143 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1145 if (_M_prefix_len < 0 || _M_prefix_len > 32)
1146 __throw_out_of_range("network_v4: invalid prefix length");
1150 network_v4(const address_v4& __addr, const address_v4& __mask)
1151 : _M_addr(__addr), _M_prefix_len(__builtin_popcount(__mask.to_uint()))
1153 if (_M_prefix_len != 0)
1155 address_v4::uint_type __mask_uint = __mask.to_uint();
1156 if (__builtin_ctz(__mask_uint) != (32 - _M_prefix_len))
1157 __throw_invalid_argument("network_v4: invalid mask");
1158 if ((__mask_uint & 0x80000000) == 0)
1159 __throw_invalid_argument("network_v4: invalid mask");
1165 constexpr address_v4 address() const noexcept { return _M_addr; }
1166 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1168 constexpr address_v4
1169 netmask() const noexcept
1171 address_v4::uint_type __val = address_v4::broadcast().to_uint();
1172 __val >>= (32 - _M_prefix_len);
1173 __val <<= (32 - _M_prefix_len);
1174 return address_v4{__val};
1177 constexpr address_v4
1178 network() const noexcept
1179 { return address_v4{_M_addr.to_uint() & netmask().to_uint()}; }
1181 constexpr address_v4
1182 broadcast() const noexcept
1183 { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; }
1186 hosts() const noexcept
1189 return { address(), *++address_v4_iterator(address()) };
1190 return { network(), broadcast() };
1193 constexpr network_v4
1194 canonical() const noexcept
1195 { return network_v4(network(), prefix_length()); }
1197 constexpr bool is_host() const noexcept { return _M_prefix_len == 32; }
1200 is_subnet_of(const network_v4& __other) const noexcept
1202 if (__other.prefix_length() < prefix_length())
1204 network_v4 __net(address(), __other.prefix_length());
1205 return __net.canonical() == __other.canonical();
1210 template<typename _Allocator = allocator<char>>
1211 __string_with<_Allocator>
1212 to_string(const _Allocator& __a = _Allocator()) const
1214 return address().to_string(__a) + '/'
1215 + std::to_string(prefix_length());
1223 /// An IPv6 network address.
1228 constexpr network_v6() noexcept : _M_addr(), _M_prefix_len(0) { }
1231 network_v6(const address_v6& __addr, int __prefix_len)
1232 : _M_addr(__addr), _M_prefix_len(__prefix_len)
1234 if (_M_prefix_len < 0 || _M_prefix_len > 128)
1235 __throw_out_of_range("network_v6: invalid prefix length");
1239 constexpr address_v6 address() const noexcept { return _M_addr; }
1240 constexpr int prefix_length() const noexcept { return _M_prefix_len; }
1242 constexpr address_v6 network() const noexcept; // TODO
1245 hosts() const noexcept
1248 return { address(), *++address_v6_iterator(address()) };
1249 return {}; // { network(), XXX broadcast() XXX }; // TODO
1252 constexpr network_v6
1253 canonical() const noexcept
1254 { return network_v6{network(), prefix_length()}; }
1256 constexpr bool is_host() const noexcept { return _M_prefix_len == 128; }
1259 is_subnet_of(const network_v6& __other) const noexcept
1261 if (__other.prefix_length() < prefix_length())
1263 network_v6 __net(address(), __other.prefix_length());
1264 return __net.canonical() == __other.canonical();
1269 template<typename _Allocator = allocator<char>>
1270 __string_with<_Allocator>
1271 to_string(const _Allocator& __a = _Allocator()) const
1273 return address().to_string(__a) + '/'
1274 + std::to_string(prefix_length());
1283 /** ip::network_v4 comparisons
1288 operator==(const network_v4& __a, const network_v4& __b) noexcept
1290 return __a.address() == __b.address()
1291 && __a.prefix_length() == __b.prefix_length();
1295 operator!=(const network_v4& __a, const network_v4& __b) noexcept
1296 { return !(__a == __b); }
1300 /** ip::network_v6 comparisons
1305 operator==(const network_v6& __a, const network_v6& __b) noexcept
1307 return __a.address() == __b.address()
1308 && __a.prefix_length() == __b.prefix_length();
1312 operator!=(const network_v6& __a, const network_v6& __b) noexcept
1313 { return !(__a == __b); }
1317 /** ip::network_v4 creation
1322 make_network_v4(const address_v4& __a, int __prefix_len)
1323 { return network_v4{__a, __prefix_len}; }
1326 make_network_v4(const address_v4& __a, const address_v4& __mask)
1327 { return network_v4{ __a, __mask }; }
1329 network_v4 make_network_v4(const char*, error_code&) noexcept; // TODO
1332 make_network_v4(const char* __str)
1333 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1335 network_v4 make_network_v4(const string&, error_code&) noexcept; // TODO
1338 make_network_v4(const string& __str)
1339 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1341 network_v4 make_network_v4(string_view, error_code&) noexcept; // TODO
1344 make_network_v4(string_view __str)
1345 { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
1349 /** ip::network_v6 creation
1354 make_network_v6(const address_v6& __a, int __prefix_len)
1355 { return network_v6{__a, __prefix_len}; }
1357 network_v6 make_network_v6(const char*, error_code&) noexcept; // TODO
1360 make_network_v6(const char* __str)
1361 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1363 network_v6 make_network_v6(const string&, error_code&) noexcept; // TODO
1366 make_network_v6(const string& __str)
1367 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1369 network_v6 make_network_v6(string_view, error_code&) noexcept; // TODO
1372 make_network_v6(string_view __str)
1373 { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
1377 /// ip::network_v4 I/O
1378 template<typename _CharT, typename _Traits>
1379 inline basic_ostream<_CharT, _Traits>&
1380 operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v4& __net)
1381 { return __os << __net.to_string(); }
1383 /// ip::network_v6 I/O
1384 template<typename _CharT, typename _Traits>
1385 inline basic_ostream<_CharT, _Traits>&
1386 operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v6& __net)
1387 { return __os << __net.to_string(); }
1390 template<typename _InternetProtocol>
1391 class basic_endpoint
1395 using protocol_type = _InternetProtocol;
1400 basic_endpoint() noexcept : _M_data()
1401 { _M_data._M_v4.sin_family = protocol_type::v4().family(); }
1404 basic_endpoint(const protocol_type& __proto,
1405 port_type __port_num) noexcept
1408 __glibcxx_assert(__proto == protocol_type::v4()
1409 || __proto == protocol_type::v6());
1411 _M_data._M_v4.sin_family = __proto.family();
1412 _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
1416 basic_endpoint(const ip::address& __addr,
1417 port_type __port_num) noexcept
1422 _M_data._M_v4.sin_family = protocol_type::v4().family();
1423 _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
1424 _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
1429 _M_data._M_v6.sin6_family = protocol_type::v6().family();
1430 _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num);
1431 __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
1432 __addr._M_v6._M_bytes.data(), 16);
1433 _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
1438 constexpr protocol_type protocol() const noexcept
1440 return _M_data._M_v4.sin_family == AF_INET6
1441 ? protocol_type::v6() : protocol_type::v4();
1444 constexpr ip::address
1445 address() const noexcept
1448 if (protocol().family() == AF_INET6)
1450 __builtin_memcpy(&__addr._M_v6._M_bytes,
1451 _M_data._M_v6.sin6_addr.s6_addr, 16);
1452 __addr._M_is_v4 = false;
1456 __builtin_memcpy(&__addr._M_v4._M_addr,
1457 &_M_data._M_v4.sin_addr.s_addr, 4);
1463 address(const ip::address& __addr) noexcept
1468 _M_data._M_v6.sin6_family = protocol_type::v6().family();
1469 __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
1470 __addr._M_v6._M_bytes.data(), 16);
1471 _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
1475 _M_data._M_v4.sin_family = protocol_type::v4().family();
1476 _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
1481 port() const noexcept
1482 { return address_v4::_S_ntoh_16(_M_data._M_v4.sin_port); }
1485 port(port_type __port_num) noexcept
1486 { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); }
1488 void* data() noexcept { return &_M_data; }
1489 const void* data() const noexcept { return &_M_data; }
1490 constexpr size_t size() const noexcept
1492 return protocol().family() == AF_INET6
1493 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
1499 if ((protocol().family() == AF_INET6 && __s != sizeof(sockaddr_in6))
1500 || (protocol().family() == AF_INET && __s != sizeof(sockaddr_in)))
1501 __throw_length_error("net::ip::basic_endpoint::resize");
1504 constexpr size_t capacity() const noexcept { return sizeof(_M_data); }
1514 /** basic_endpoint comparisons
1518 template<typename _InternetProtocol>
1520 operator==(const basic_endpoint<_InternetProtocol>& __a,
1521 const basic_endpoint<_InternetProtocol>& __b)
1522 { return __a.address() == __b.address() && __a.port() == __b.port(); }
1524 template<typename _InternetProtocol>
1526 operator!=(const basic_endpoint<_InternetProtocol>& __a,
1527 const basic_endpoint<_InternetProtocol>& __b)
1528 { return !(__a == __b); }
1530 template<typename _InternetProtocol>
1532 operator< (const basic_endpoint<_InternetProtocol>& __a,
1533 const basic_endpoint<_InternetProtocol>& __b)
1535 return __a.address() < __b.address()
1536 || (!(__b.address() < __a.address()) && __a.port() < __b.port());
1539 template<typename _InternetProtocol>
1541 operator> (const basic_endpoint<_InternetProtocol>& __a,
1542 const basic_endpoint<_InternetProtocol>& __b)
1543 { return __b < __a; }
1545 template<typename _InternetProtocol>
1547 operator<=(const basic_endpoint<_InternetProtocol>& __a,
1548 const basic_endpoint<_InternetProtocol>& __b)
1549 { return !(__b < __a); }
1551 template<typename _InternetProtocol>
1553 operator>=(const basic_endpoint<_InternetProtocol>& __a,
1554 const basic_endpoint<_InternetProtocol>& __b)
1555 { return !(__a < __b); }
1559 /// basic_endpoint I/O
1560 template<typename _CharT, typename _Traits, typename _InternetProtocol>
1561 inline basic_ostream<_CharT, _Traits>&
1562 operator<<(basic_ostream<_CharT, _Traits>& __os,
1563 const basic_endpoint<_InternetProtocol>& __ep)
1565 basic_ostringstream<_CharT, _Traits> __ss;
1567 == basic_endpoint<_InternetProtocol>::protocol_type::v6())
1568 __ss << '[' << __ep.address() << ']';
1570 __ss << __ep.address();
1571 __ss << ':' << __ep.port();
1576 /** Type representing a single result of name/address resolution.
1580 template<typename _InternetProtocol>
1581 class basic_resolver_entry
1585 using protocol_type = _InternetProtocol;
1586 using endpoint_type = typename _InternetProtocol::endpoint;
1589 basic_resolver_entry() { }
1591 basic_resolver_entry(const endpoint_type& __ep,
1592 string_view __h, string_view __s)
1593 : _M_ep(__ep), _M_host(__h), _M_svc(__s) { }
1596 endpoint_type endpoint() const { return _M_ep; }
1597 operator endpoint_type() const { return _M_ep; }
1599 template<typename _Allocator = allocator<char>>
1600 __string_with<_Allocator>
1601 host_name(const _Allocator& __a = _Allocator()) const
1602 { return { _M_host, __a }; }
1604 template<typename _Allocator = allocator<char>>
1605 __string_with<_Allocator>
1606 service_name(const _Allocator& __a = _Allocator()) const
1607 { return { _M_svc, __a }; }
1610 basic_endpoint<_InternetProtocol> _M_ep;
1615 template<typename _InternetProtocol>
1617 operator==(const basic_resolver_entry<_InternetProtocol>& __a,
1618 const basic_resolver_entry<_InternetProtocol>& __b)
1620 return __a.endpoint() == __b.endpoint()
1621 && __a.host_name() == __b.host_name()
1622 && __a.service_name() == __b.service_name();
1625 template<typename _InternetProtocol>
1627 operator!=(const basic_resolver_entry<_InternetProtocol>& __a,
1628 const basic_resolver_entry<_InternetProtocol>& __b)
1629 { return !(__a == __b); }
1633 /** Base class defining flags for name/address resolution.
1642 __flags_passive = AI_PASSIVE,
1643 __flags_canonical_name = AI_CANONNAME,
1644 __flags_numeric_host = AI_NUMERICHOST,
1645 #ifdef AI_NUMERICSERV
1646 __flags_numeric_service = AI_NUMERICSERV,
1648 __flags_v4_mapped = AI_V4MAPPED,
1649 __flags_all_matching = AI_ALL,
1650 __flags_address_configured = AI_ADDRCONFIG
1652 static constexpr flags passive = __flags_passive;
1653 static constexpr flags canonical_name = __flags_canonical_name;
1654 static constexpr flags numeric_host = __flags_numeric_host;
1655 #ifdef AI_NUMERICSERV
1656 static constexpr flags numeric_service = __flags_numeric_service;
1658 static constexpr flags v4_mapped = __flags_v4_mapped;
1659 static constexpr flags all_matching = __flags_all_matching;
1660 static constexpr flags address_configured = __flags_address_configured;
1663 resolver_base() = default;
1664 ~resolver_base() = default;
1667 constexpr resolver_base::flags
1668 operator&(resolver_base::flags __f1, resolver_base::flags __f2)
1669 { return resolver_base::flags( int(__f1) & int(__f2) ); }
1671 constexpr resolver_base::flags
1672 operator|(resolver_base::flags __f1, resolver_base::flags __f2)
1673 { return resolver_base::flags( int(__f1) | int(__f2) ); }
1675 constexpr resolver_base::flags
1676 operator^(resolver_base::flags __f1, resolver_base::flags __f2)
1677 { return resolver_base::flags( int(__f1) ^ int(__f2) ); }
1679 constexpr resolver_base::flags
1680 operator~(resolver_base::flags __f)
1681 { return resolver_base::flags( ~int(__f) ); }
1683 inline resolver_base::flags&
1684 operator&=(resolver_base::flags& __f1, resolver_base::flags __f2)
1685 { return __f1 = (__f1 & __f2); }
1687 inline resolver_base::flags&
1688 operator|=(resolver_base::flags& __f1, resolver_base::flags __f2)
1689 { return __f1 = (__f1 | __f2); }
1691 inline resolver_base::flags&
1692 operator^=(resolver_base::flags& __f1, resolver_base::flags __f2)
1693 { return __f1 = (__f1 ^ __f2); }
1695 // TODO define resolver_base::flags static constants for C++14 mode
1699 /** Container for results of name/address resolution.
1703 template<typename _InternetProtocol>
1704 class basic_resolver_results
1708 using protocol_type = _InternetProtocol;
1709 using endpoint_type = typename protocol_type::endpoint;
1710 using value_type = basic_resolver_entry<protocol_type>;
1711 using const_reference = const value_type&;
1712 using reference = value_type&;
1713 using const_iterator = typename forward_list<value_type>::const_iterator;
1714 using iterator = const_iterator;
1715 using difference_type = ptrdiff_t;
1716 using size_type = size_t;
1718 // construct / copy / destroy:
1720 basic_resolver_results() = default;
1722 basic_resolver_results(const basic_resolver_results&) = default;
1724 basic_resolver_results(basic_resolver_results&&) noexcept = default;
1726 basic_resolver_results&
1727 operator=(const basic_resolver_results&) = default;
1729 basic_resolver_results&
1730 operator=(basic_resolver_results&&) = default;
1732 ~basic_resolver_results() = default;
1735 size_type size() const noexcept { return _M_size; }
1736 size_type max_size() const noexcept { return _M_results.max_size(); }
1738 _GLIBCXX_NODISCARD bool
1739 empty() const noexcept { return _M_results.empty(); }
1742 const_iterator begin() const { return _M_results.begin(); }
1743 const_iterator end() const { return _M_results.end(); }
1744 const_iterator cbegin() const { return _M_results.begin(); }
1745 const_iterator cend() const { return _M_results.end(); }
1749 swap(basic_resolver_results& __that) noexcept
1750 { _M_results.swap(__that._M_results); }
1753 friend class basic_resolver<protocol_type>;
1755 basic_resolver_results(string_view, string_view, resolver_base::flags,
1756 error_code&, protocol_type* = nullptr);
1758 basic_resolver_results(const endpoint_type&, error_code&);
1760 forward_list<value_type> _M_results;
1764 template<typename _InternetProtocol>
1766 operator==(const basic_resolver_results<_InternetProtocol>& __a,
1767 const basic_resolver_results<_InternetProtocol>& __b)
1769 return __a.size() == __b.size()
1770 && std::equal(__a.begin(), __a.end(), __b.begin());
1773 template<typename _InternetProtocol>
1775 operator!=(const basic_resolver_results<_InternetProtocol>& __a,
1776 const basic_resolver_results<_InternetProtocol>& __b)
1777 { return !(__a == __b); }
1781 /// Perform name/address resolution.
1782 template<typename _InternetProtocol>
1783 class basic_resolver : public resolver_base
1788 using executor_type = io_context::executor_type;
1789 using protocol_type = _InternetProtocol;
1790 using endpoint_type = typename _InternetProtocol::endpoint;
1791 using results_type = basic_resolver_results<_InternetProtocol>;
1793 // construct / copy / destroy:
1795 explicit basic_resolver(io_context& __ctx) : _M_ctx(&__ctx) { }
1797 basic_resolver(const basic_resolver&) = delete;
1799 basic_resolver(basic_resolver&& __rhs) noexcept
1800 : _M_ctx(__rhs._M_ctx)
1801 { } // TODO move state/tasks etc.
1803 ~basic_resolver() { cancel(); }
1805 basic_resolver& operator=(const basic_resolver&) = delete;
1807 basic_resolver& operator=(basic_resolver&& __rhs)
1810 _M_ctx = __rhs._M_ctx;
1811 // TODO move state/tasks etc.
1815 // basic_resolver operations:
1817 executor_type get_executor() noexcept { return _M_ctx->get_executor(); }
1819 void cancel() { } // TODO
1822 resolve(string_view __host_name, string_view __service_name)
1824 return resolve(__host_name, __service_name, resolver_base::flags(),
1825 __throw_on_error{"basic_resolver::resolve"});
1829 resolve(string_view __host_name, string_view __service_name,
1832 return resolve(__host_name, __service_name, resolver_base::flags(),
1837 resolve(string_view __host_name, string_view __service_name, flags __f)
1839 return resolve(__host_name, __service_name, __f,
1840 __throw_on_error{"basic_resolver::resolve"});
1844 resolve(string_view __host_name, string_view __service_name, flags __f,
1846 { return {__host_name, __service_name, __f, __ec}; }
1848 template<typename _CompletionToken>
1849 __deduced_t<_CompletionToken, void(error_code, results_type)>
1850 async_resolve(string_view __host_name, string_view __service_name,
1851 _CompletionToken&& __token)
1853 return async_resolve(__host_name, __service_name,
1854 resolver_base::flags(),
1855 forward<_CompletionToken>(__token));
1858 template<typename _CompletionToken>
1859 __deduced_t<_CompletionToken, void(error_code, results_type)>
1860 async_resolve(string_view __host_name, string_view __service_name,
1861 flags __f, _CompletionToken&& __token); // TODO
1864 resolve(const protocol_type& __protocol,
1865 string_view __host_name, string_view __service_name)
1867 return resolve(__protocol, __host_name, __service_name,
1868 resolver_base::flags(),
1869 __throw_on_error{"basic_resolver::resolve"});
1873 resolve(const protocol_type& __protocol,
1874 string_view __host_name, string_view __service_name,
1877 return resolve(__protocol, __host_name, __service_name,
1878 resolver_base::flags(), __ec);
1882 resolve(const protocol_type& __protocol,
1883 string_view __host_name, string_view __service_name, flags __f)
1885 return resolve(__protocol, __host_name, __service_name, __f,
1886 __throw_on_error{"basic_resolver::resolve"});
1890 resolve(const protocol_type& __protocol,
1891 string_view __host_name, string_view __service_name,
1892 flags __f, error_code& __ec)
1893 { return {__host_name, __service_name, __f, __ec, &__protocol}; }
1895 template<typename _CompletionToken>
1896 __deduced_t<_CompletionToken, void(error_code, results_type)>
1897 async_resolve(const protocol_type& __protocol,
1898 string_view __host_name, string_view __service_name,
1899 _CompletionToken&& __token)
1901 return async_resolve(__protocol, __host_name, __service_name,
1902 resolver_base::flags(),
1903 forward<_CompletionToken>(__token));
1906 template<typename _CompletionToken>
1907 __deduced_t<_CompletionToken, void(error_code, results_type)>
1908 async_resolve(const protocol_type& __protocol,
1909 string_view __host_name, string_view __service_name,
1910 flags __f, _CompletionToken&& __token); // TODO
1913 resolve(const endpoint_type& __ep)
1914 { return resolve(__ep, __throw_on_error{"basic_resolver::resolve"}); }
1917 resolve(const endpoint_type& __ep, error_code& __ec)
1918 { return { __ep, __ec }; }
1920 template<typename _CompletionToken> // TODO
1921 __deduced_t<_CompletionToken, void(error_code, results_type)>
1922 async_resolve(const endpoint_type& __ep, _CompletionToken&& __token);
1928 /// Private constructor to synchronously resolve host and service names.
1929 template<typename _InternetProtocol>
1930 basic_resolver_results<_InternetProtocol>::
1931 basic_resolver_results(string_view __host_name, string_view __service_name,
1932 resolver_base::flags __f, error_code& __ec,
1933 protocol_type* __protocol)
1935 #ifdef _GLIBCXX_HAVE_NETDB_H
1937 const char* __h = __host_name.data()
1938 ? (__host = __host_name.to_string()).c_str()
1941 const char* __s = __service_name.data()
1942 ? (__svc = __service_name.to_string()).c_str()
1945 ::addrinfo __hints{ };
1946 __hints.ai_flags = static_cast<int>(__f);
1949 __hints.ai_family = __protocol->family();
1950 __hints.ai_socktype = __protocol->type();
1951 __hints.ai_protocol = __protocol->protocol();
1955 auto __p = endpoint_type{}.protocol();
1956 __hints.ai_family = AF_UNSPEC;
1957 __hints.ai_socktype = __p.type();
1958 __hints.ai_protocol = __p.protocol();
1961 struct __scoped_addrinfo
1963 ~__scoped_addrinfo() { if (_M_p) ::freeaddrinfo(_M_p); }
1964 ::addrinfo* _M_p = nullptr;
1967 if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p))
1969 __ec.assign(__err, resolver_category());
1975 auto __tail = _M_results.before_begin();
1976 for (auto __ai = __sai._M_p; __ai != nullptr; __ai = __ai->ai_next)
1978 if (__ai->ai_family == AF_INET || __ai->ai_family == AF_INET6)
1980 if (__ai->ai_addrlen <= __ep.capacity())
1981 __builtin_memcpy(__ep.data(), __ai->ai_addr, __ai->ai_addrlen);
1982 __ep.resize(__ai->ai_addrlen);
1983 __tail = _M_results.emplace_after(__tail, __ep, __host, __svc);
1988 __ec = std::make_error_code(errc::operation_not_supported);
1992 /// Private constructor to synchronously resolve an endpoint.
1993 template<typename _InternetProtocol>
1994 basic_resolver_results<_InternetProtocol>::
1995 basic_resolver_results(const endpoint_type& __ep, error_code& __ec)
1997 #ifdef _GLIBCXX_HAVE_NETDB_H
1998 char __host_name[256];
1999 char __service_name[128];
2001 if (__ep.protocol().type() == SOCK_DGRAM)
2002 __flags |= NI_DGRAM;
2003 auto __sa = static_cast<const sockaddr*>(__ep.data());
2004 int __err = ::getnameinfo(__sa, __ep.size(),
2005 __host_name, sizeof(__host_name),
2006 __service_name, sizeof(__service_name),
2010 __flags |= NI_NUMERICSERV;
2011 __err = ::getnameinfo(__sa, __ep.size(),
2012 __host_name, sizeof(__host_name),
2013 __service_name, sizeof(__service_name),
2017 __ec.assign(__err, resolver_category());
2021 _M_results.emplace_front(__ep, __host_name, __service_name);
2025 __ec = std::make_error_code(errc::operation_not_supported);
2029 /** The name of the local host.
2033 template<typename _Allocator>
2034 __string_with<_Allocator>
2035 host_name(const _Allocator& __a, error_code& __ec)
2037 #ifdef HOST_NAME_MAX
2038 constexpr size_t __maxlen = HOST_NAME_MAX;
2040 constexpr size_t __maxlen = 256;
2042 char __buf[__maxlen + 1];
2043 if (::gethostname(__buf, __maxlen) == -1)
2044 __ec.assign(errno, generic_category());
2045 __buf[__maxlen] = '\0';
2046 return { __buf, __a };
2049 template<typename _Allocator>
2050 inline __string_with<_Allocator>
2051 host_name(const _Allocator& __a)
2052 { return host_name(__a, __throw_on_error{"host_name"}); }
2055 host_name(error_code& __ec)
2056 { return host_name(std::allocator<char>{}, __ec); }
2060 { return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); }
2064 /// The TCP byte-stream protocol.
2069 using endpoint = basic_endpoint<tcp>; ///< A TCP endpoint.
2070 using resolver = basic_resolver<tcp>; ///< A TCP resolver.
2071 using socket = basic_stream_socket<tcp>; ///< A TCP socket.
2072 using acceptor = basic_socket_acceptor<tcp>; ///< A TCP acceptor.
2073 using iostream = basic_socket_iostream<tcp>; /// A TCP iostream.
2075 #ifdef _GLIBCXX_HAVE_NETINET_TCP_H
2076 /// Disable coalescing of small segments (i.e. the Nagle algorithm).
2077 struct no_delay : __sockopt_crtp<no_delay, bool>
2079 using __sockopt_crtp::__sockopt_crtp;
2081 static const int _S_level = IPPROTO_TCP;
2082 static const int _S_name = TCP_NODELAY;
2088 /// A protocol object representing IPv4 TCP.
2089 static constexpr tcp v4() noexcept { return tcp(AF_INET); }
2090 /// A protocol object representing IPv6 TCP.
2091 static constexpr tcp v6() noexcept { return tcp(AF_INET6); }
2095 constexpr int family() const noexcept { return _M_family; }
2096 constexpr int type() const noexcept { return SOCK_STREAM; }
2097 constexpr int protocol() const noexcept { return IPPROTO_TCP; }
2100 constexpr explicit tcp(int __family) : _M_family(__family) { }
2110 operator==(const tcp& __a, const tcp& __b)
2111 { return __a.family() == __b.family(); }
2114 operator!=(const tcp& __a, const tcp& __b)
2115 { return !(__a == __b); }
2119 /// The UDP datagram protocol.
2124 using endpoint = basic_endpoint<udp>;
2125 using resolver = basic_resolver<udp>;
2126 using socket = basic_datagram_socket<udp>;
2129 static constexpr udp v4() noexcept { return udp(AF_INET); }
2130 static constexpr udp v6() noexcept { return udp(AF_INET6); }
2134 constexpr int family() const noexcept { return _M_family; }
2135 constexpr int type() const noexcept { return SOCK_DGRAM; }
2136 constexpr int protocol() const noexcept { return IPPROTO_UDP; }
2139 constexpr explicit udp(int __family) : _M_family(__family) { }
2149 operator==(const udp& __a, const udp& __b)
2150 { return __a.family() == __b.family(); }
2153 operator!=(const udp& __a, const udp& __b)
2154 { return !(__a == __b); }
2158 /// Restrict a socket created for an IPv6 protocol to IPv6 only.
2159 struct v6_only : __sockopt_crtp<v6_only, bool>
2161 using __sockopt_crtp::__sockopt_crtp;
2163 static const int _S_level = IPPROTO_IPV6;
2164 static const int _S_name = IPV6_V6ONLY;
2169 /// Set the default number of hops (TTL) for outbound datagrams.
2170 struct hops : __sockopt_crtp<hops>
2172 using __sockopt_crtp::__sockopt_crtp;
2174 template<typename _Protocol>
2176 level(const _Protocol& __p) const noexcept
2177 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2179 template<typename _Protocol>
2181 name(const _Protocol& __p) const noexcept
2182 { return __p.family() == AF_INET6 ? IPV6_UNICAST_HOPS : IP_TTL; }
2184 } // namespace unicast
2188 /// Request that a socket joins a multicast group.
2192 join_group(const address&);
2195 join_group(const address_v4&, const address_v4& = address_v4::any());
2198 join_group(const address_v6&, unsigned int = 0);
2200 template<typename _Protocol>
2202 level(const _Protocol& __p) const noexcept
2203 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2205 template<typename _Protocol>
2207 name(const _Protocol& __p) const noexcept
2209 return __p.family() == AF_INET6
2210 ? IPV6_JOIN_GROUP : IP_ADD_MEMBERSHIP;
2212 template<typename _Protocol>
2214 data(const _Protocol&) noexcept
2215 { return std::addressof(_M_value); }
2217 template<typename _Protocol>
2219 data(const _Protocol&) const noexcept
2220 { return std::addressof(_M_value); }
2222 template<typename _Protocol>
2224 size(const _Protocol& __p) const noexcept
2226 return __p.family() == AF_INET6
2227 ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
2230 template<typename _Protocol>
2232 resize(const _Protocol& __p, size_t __s)
2234 if (__s != size(__p))
2235 __throw_length_error("invalid value for socket option resize");
2246 /// Request that a socket leaves a multicast group.
2250 leave_group(const address&);
2253 leave_group(const address_v4&, const address_v4& = address_v4::any());
2256 leave_group(const address_v6&, unsigned int = 0);
2258 template<typename _Protocol>
2260 level(const _Protocol& __p) const noexcept
2261 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2263 template<typename _Protocol>
2265 name(const _Protocol& __p) const noexcept
2267 return __p.family() == AF_INET6
2268 ? IPV6_LEAVE_GROUP : IP_DROP_MEMBERSHIP;
2270 template<typename _Protocol>
2272 data(const _Protocol&) noexcept
2273 { return std::addressof(_M_value); }
2275 template<typename _Protocol>
2277 data(const _Protocol&) const noexcept
2278 { return std::addressof(_M_value); }
2280 template<typename _Protocol>
2282 size(const _Protocol& __p) const noexcept
2284 return __p.family() == AF_INET6
2285 ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
2288 template<typename _Protocol>
2290 resize(const _Protocol& __p, size_t __s)
2292 if (__s != size(__p))
2293 __throw_length_error("invalid value for socket option resize");
2304 /// Specify the network interface for outgoing multicast datagrams.
2305 class outbound_interface
2308 outbound_interface(const address_v4&);
2311 outbound_interface(unsigned int);
2313 template<typename _Protocol>
2315 level(const _Protocol& __p) const noexcept
2316 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2318 template<typename _Protocol>
2320 name(const _Protocol& __p) const noexcept
2322 return __p.family() == AF_INET6
2323 ? IPV6_MULTICAST_IF : IP_MULTICAST_IF;
2326 template<typename _Protocol>
2328 data(const _Protocol&) const noexcept
2329 { return std::addressof(_M_value); }
2331 template<typename _Protocol>
2333 size(const _Protocol& __p) const noexcept
2335 return __p.family() == AF_INET6
2336 ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
2346 /// Set the default number of hops (TTL) for outbound datagrams.
2347 struct hops : __sockopt_crtp<hops>
2349 using __sockopt_crtp::__sockopt_crtp;
2351 template<typename _Protocol>
2353 level(const _Protocol& __p) const noexcept
2354 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2356 template<typename _Protocol>
2358 name(const _Protocol& __p) const noexcept
2360 return __p.family() == AF_INET6
2361 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL;
2365 /// Set whether datagrams are delivered back to the local application.
2366 struct enable_loopback : __sockopt_crtp<enable_loopback>
2368 using __sockopt_crtp::__sockopt_crtp;
2370 template<typename _Protocol>
2372 level(const _Protocol& __p) const noexcept
2373 { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
2375 template<typename _Protocol>
2377 name(const _Protocol& __p) const noexcept
2379 return __p.family() == AF_INET6
2380 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP;
2384 } // namespace multicast
2391 } // namespace experimental
2394 struct is_error_condition_enum<experimental::net::v1::ip::resolver_errc>
2395 : public true_type {};
2398 template<typename _Tp> struct hash;
2400 struct hash<experimental::net::v1::ip::address>
2401 : __hash_base<size_t, experimental::net::v1::ip::address>
2404 operator()(const experimental::net::v1::ip::address& __a) const
2407 return _Hash_impl::hash(__a.to_v4());
2409 return _Hash_impl::hash(__a.to_v6());
2414 struct hash<experimental::net::v1::ip::address_v4>
2415 : __hash_base<size_t, experimental::net::v1::ip::address_v4>
2418 operator()(const experimental::net::v1::ip::address_v4& __a) const
2419 { return _Hash_impl::hash(__a.to_bytes()); }
2422 template<> struct hash<experimental::net::v1::ip::address_v6>
2423 : __hash_base<size_t, experimental::net::v1::ip::address_v6>
2426 operator()(const experimental::net::v1::ip::address_v6& __a) const
2427 { return _Hash_impl::hash(__a.to_bytes()); }
2430 _GLIBCXX_END_NAMESPACE_VERSION
2435 #endif // _GLIBCXX_EXPERIMENTAL_INTERNET