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