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