// <experimental/internet> -*- C++ -*-
-// Copyright (C) 2015-2019 Free Software Foundation, Inc.
+// Copyright (C) 2015-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
/** @file experimental/internet
* This is a TS C++ Library header.
+ * @ingroup networking-ts
*/
#ifndef _GLIBCXX_EXPERIMENTAL_INTERNET
#pragma GCC system_header
+#include <bits/requires_hosted.h> // experimental is currently omitted
+
#if __cplusplus >= 201402L
#include <experimental/netfwd>
#include <sstream>
#include <cstdint>
#include <experimental/string_view>
+#include <bits/charconv.h>
#ifdef _GLIBCXX_HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef _GLIBCXX_HAVE_ARPA_INET_H
# include <arpa/inet.h> // inet_ntop
#endif
+#ifdef _GLIBCXX_HAVE_NETINET_IN_H
+# include <netinet/in.h> // IPPROTO_IP, IPPROTO_IPV6, in_addr, in6_addr
+#endif
#ifdef _GLIBCXX_HAVE_NETINET_TCP_H
# include <netinet/tcp.h> // TCP_NODELAY
#endif
# include <netdb.h> // getaddrinfo etc.
#endif
+#if defined _WIN32 && __has_include(<ws2tcpip.h>)
+# include <ws2tcpip.h>
+#endif
+
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
{
namespace ip
{
-
- /**
- * @ingroup networking
- * @{
+ /** @addtogroup networking-ts
+ * @{
*/
-#ifdef _GLIBCXX_HAVE_NETDB_H
/** Error codes for resolver errors.
* @{
*/
enum class resolver_errc : int {
+#ifdef _GLIBCXX_HAVE_NETDB_H
host_not_found = EAI_NONAME,
host_not_found_try_again = EAI_AGAIN,
service_not_found = EAI_SERVICE
+ // N.B. POSIX defines additional errors that have no enumerator here:
+ // EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_SOCKTYPE, EAI_SYSTEM
+ // Some C libraries define additional errors:
+ // EAI_BADHINTS, EAI_OVERFLOW, EAI_PROTOCOL
+ // Some C libraries define additional (obsolete?) errors:
+ // EAI_ADDRFAMILY, EAI_NODATA
+#endif
};
/// Error category for resolver errors.
struct __cat : error_category
{
const char* name() const noexcept { return "resolver"; }
- std::string message(int __e) const { return ::gai_strerror(__e); }
+ std::string message(int __e) const {
+#ifdef _GLIBCXX_HAVE_NETDB_H
+ return ::gai_strerror(__e);
+#else
+ return "name resolution requires <netdb.h>";
+#endif
+ }
virtual void __message(int) { } // TODO dual ABI XXX
};
static __cat __c;
return __c;
}
- error_code make_error_code(resolver_errc __e) noexcept
+ inline error_code make_error_code(resolver_errc __e) noexcept
{ return error_code(static_cast<int>(__e), resolver_category()); }
- error_condition make_error_condition(resolver_errc __e) noexcept
+ inline error_condition make_error_condition(resolver_errc __e) noexcept
{ return error_condition(static_cast<int>(__e), resolver_category()); }
- /// @}
+ /// @cond undocumented
+ inline error_code
+ __make_resolver_error_code(int __ai_err,
+ [[__maybe_unused__]] int __sys_err) noexcept
+ {
+#ifdef EAI_SYSTEM
+ if (__builtin_expect(__ai_err == EAI_SYSTEM, 0))
+ return error_code(__sys_err, std::generic_category());
#endif
+ return error_code(__ai_err, resolver_category());
+ }
+ /// @endcond
- typedef uint_least16_t port_type; ///< Type used for port numbers.
- typedef uint_least32_t scope_id_type; ///< Type used for IPv6 scope IDs.
+ /// @}
+
+ using port_type = uint_least16_t; ///< Type used for port numbers.
+ using scope_id_type = uint_least32_t; ///< Type used for IPv6 scope IDs.
/// Convenience alias for constraining allocators for strings.
template<typename _Alloc>
= enable_if_t<std::is_same<typename _Alloc::value_type, char>::value,
std::basic_string<char, std::char_traits<char>, _Alloc>>;
+ constexpr errc
+ __unsupported_err() noexcept
+ {
+#if defined EAFNOSUPPORT
+ return std::errc::address_family_not_supported;
+#else
+ return std::errc::operation_not_supported;
+#endif
+ }
+
/** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses.
* @{
*/
struct v4_mapped_t {};
constexpr v4_mapped_t v4_mapped;
- // @}
+ /// @}
/// An IPv4 address.
class address_v4
{
public:
// types:
- typedef uint_least32_t uint_type;
+ using uint_type = uint_least32_t;
struct bytes_type : array<unsigned char, 4>
{
constexpr
address_v4(const bytes_type& __b)
- : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
+#if __has_builtin(__builtin_bit_cast)
+ : _M_addr(__builtin_bit_cast(uint_type, __b))
+#else
+ : _M_addr(_S_hton_32((__b[0] << 24) | (__b[1] << 16)
+ | (__b[2] << 8) | __b[3]))
+#endif
{ }
explicit constexpr
- address_v4(uint_type __val) : _M_addr(_S_hton(__val))
+ address_v4(uint_type __val) : _M_addr(_S_hton_32(__val))
{
#if UINT_LEAST32_MAX > 0xFFFFFFFF
if (__val > 0xFFFFFFFF)
constexpr bytes_type
to_bytes() const noexcept
{
+#if __has_builtin(__builtin_bit_cast)
+ return __builtin_bit_cast(bytes_type, _M_addr);
+#else
+ auto __host = to_uint();
return bytes_type{
- (_M_addr >> 24) & 0xFF,
- (_M_addr >> 16) & 0xFF,
- (_M_addr >> 8) & 0xFF,
- _M_addr & 0xFF
+ (__host >> 24) & 0xFF,
+ (__host >> 16) & 0xFF,
+ (__host >> 8) & 0xFF,
+ __host & 0xFF
};
+#endif
}
- constexpr uint_type to_uint() const noexcept { return _S_ntoh(_M_addr); }
+ constexpr uint_type
+ to_uint() const noexcept { return _S_ntoh_32(_M_addr); }
-#ifdef _GLIBCXX_HAVE_ARPA_INET_H
template<typename _Allocator = allocator<char>>
__string_with<_Allocator>
to_string(const _Allocator& __a = _Allocator()) const
{
+ auto __write = [__addr = to_uint()](char* __p, size_t) {
+ auto __to_chars = [](char* __p, uint8_t __v) {
+ unsigned __n = __v >= 100u ? 3 : __v >= 10u ? 2 : 1;
+ std::__detail::__to_chars_10_impl(__p, __n, __v);
+ return __p + __n;
+ };
+ const auto __begin = __p;
+ __p = __to_chars(__p, uint8_t(__addr >> 24));
+ for (int __i = 2; __i >= 0; __i--) {
+ *__p++ = '.';
+ __p = __to_chars(__p, uint8_t(__addr >> (__i * 8)));
+ }
+ return __p - __begin;
+ };
__string_with<_Allocator> __str(__a);
- __str.resize(INET6_ADDRSTRLEN);
- if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
- __str.erase(__str.find('\0'));
- else
- __str.resize(0);
+#if __cpp_lib_string_resize_and_overwrite
+ __str.resize_and_overwrite(15, __write);
+#else
+ __str.resize(15);
+ __str.resize(__write(&__str.front(), 15));
+#endif
return __str;
}
-#endif
// static members:
static constexpr address_v4 any() noexcept { return address_v4{}; }
friend address_v4 make_address_v4(const char*, error_code&) noexcept;
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
- static constexpr uint16_t _S_hton(uint16_t __h) { return __h; }
- static constexpr uint16_t _S_ntoh(uint16_t __n) { return __n; }
- static constexpr uint32_t _S_hton(uint32_t __h) { return __h; }
- static constexpr uint32_t _S_ntoh(uint32_t __n) { return __n; }
+ static constexpr uint16_t _S_hton_16(uint16_t __h) { return __h; }
+ static constexpr uint16_t _S_ntoh_16(uint16_t __n) { return __n; }
+ static constexpr uint32_t _S_hton_32(uint32_t __h) { return __h; }
+ static constexpr uint32_t _S_ntoh_32(uint32_t __n) { return __n; }
#else
static constexpr uint16_t
- _S_hton(uint16_t __h) { return __builtin_bswap16(__h); }
+ _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); }
static constexpr uint16_t
- _S_ntoh(uint16_t __n) { return __builtin_bswap16(__n); }
+ _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); }
static constexpr uint32_t
- _S_hton(uint32_t __h) { return __builtin_bswap32(__h); }
+ _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); }
static constexpr uint32_t
- _S_ntoh(uint32_t __n) { return __builtin_bswap32(__n); }
+ _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); }
#endif
+#ifdef _GLIBCXX_HAVE_ARPA_INET_H
in_addr_t _M_addr; // network byte order
+#else
+ uint32_t _M_addr;
+#endif
};
/// An IPv6 address.
// types:
struct bytes_type : array<unsigned char, 16>
{
- template<typename... _Tp> explicit constexpr bytes_type(_Tp... __t)
- : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}} { }
+ template<typename... _Tp>
+ explicit constexpr
+ bytes_type(_Tp... __t)
+ : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}}
+ { }
};
// constructors:
constexpr bytes_type to_bytes() const noexcept { return _M_bytes; }
-#ifdef _GLIBCXX_HAVE_ARPA_INET_H
template<typename _Allocator = allocator<char>>
__string_with<_Allocator>
to_string(const _Allocator& __a = _Allocator()) const
{
+#ifdef _GLIBCXX_HAVE_ARPA_INET_H
__string_with<_Allocator> __str(__a);
- __str.resize(INET6_ADDRSTRLEN);
- if (inet_ntop(AF_INET6, &_M_bytes, &__str.front(), __str.size()))
- __str.erase(__str.find('\0'));
+ __str.resize(INET6_ADDRSTRLEN + (_M_scope_id ? 11 : 0));
+ char* const __p = &__str.front();
+ if (inet_ntop(AF_INET6, &_M_bytes, __p, __str.size()))
+ {
+ auto __end = __str.find('\0');
+ if (unsigned long __scope = _M_scope_id)
+ {
+ __end +=
+#if _GLIBCXX_USE_C99_STDIO
+ __builtin_snprintf(__p + __end, __str.size() - __end,
+ "%%%lu", __scope);
+#else
+ __builtin_sprintf(__p + __end, "%%%lu", __scope);
+#endif
+ }
+ __str.erase(__end);
+ }
else
__str.resize(0);
return __str;
- }
+#else
+ std::__throw_system_error((int)__unsupported_err());
#endif
+ }
// static members:
+
static constexpr address_v6
any() noexcept
{
- address_v6 __addr;
- __builtin_memcpy(&__addr._M_bytes, in6addr_any.s6_addr, 16);
- return __addr;
+ return {};
}
static constexpr address_v6
loopback() noexcept
{
- address_v6 __addr;
- __builtin_memcpy(&__addr._M_bytes, in6addr_loopback.s6_addr, 16);
- return __addr;
+ return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
}
private:
// constructors:
constexpr address() noexcept : _M_v4(), _M_is_v4(true) { }
+#if __cpp_constexpr_dynamic_alloc
constexpr
+#endif
address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4)
{
if (_M_is_v4)
- ::new (std::addressof(_M_v4)) address_v4(__a.to_v4());
+ std::_Construct(std::addressof(_M_v4), __a.to_v4());
else
- ::new (std::addressof(_M_v6)) address_v6(__a.to_v6());
+ std::_Construct(std::addressof(_M_v6), __a.to_v6());
}
constexpr
address&
operator=(const address_v4& __a) noexcept
{
- ::new (std::addressof(_M_v4)) address_v4(__a);
+ std::_Construct(std::addressof(_M_v4), __a);
_M_is_v4 = true;
return *this;
}
address&
operator=(const address_v6& __a) noexcept
{
- ::new (std::addressof(_M_v6)) address_v6(__a);
+ std::_Construct(std::addressof(_M_v6), __a);
_M_is_v4 = false;
return *this;
}
operator>=(const address_v4& __a, const address_v4& __b) noexcept
{ return !(__a < __b); }
- // @}
+ /// @}
/** ip::address_v6 comparisons
* @{
const auto& __aa = __a._M_bytes;
const auto& __bb = __b._M_bytes;
int __i = 0;
- for (; __aa[__i] == __bb[__i] && __i < 16; ++__i)
+ for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
;
return __i == 16 ? __a.scope_id() == __b.scope_id() : false;
}
const auto& __aa = __a._M_bytes;
const auto& __bb = __b._M_bytes;
int __i = 0;
- for (; __aa[__i] == __bb[__i] && __i < 16; ++__i)
+ for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
;
return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i];
}
operator>=(const address_v6& __a, const address_v6& __b) noexcept
{ return !(__a < __b); }
- // @}
+ /// @}
/** ip::address comparisons
* @{
operator>=(const address& __a, const address& __b) noexcept
{ return !(__a < __b); }
- // @}
+ /// @}
/** ip::address_v4 creation
* @{
inline address_v4
make_address_v4(const char* __str, error_code& __ec) noexcept
{
+#ifdef _GLIBCXX_HAVE_ARPA_INET_H
address_v4 __a;
const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr);
if (__res == 1)
__ec = std::make_error_code(std::errc::invalid_argument);
else
__ec.assign(errno, generic_category());
+#else
+ __ec = std::make_error_code(__unsupported_err());
+#endif
return {};
}
inline address_v4
make_address_v4(string_view __str, error_code& __ec) noexcept
{
- char __buf[INET_ADDRSTRLEN];
+ char __buf[16]; // INET_ADDRSTRLEN isn't defined on Windows
auto __len = __str.copy(__buf, sizeof(__buf));
if (__len == sizeof(__buf))
{
make_address_v4(string_view __str)
{ return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
- // @}
+ /// @}
/** ip::address_v6 creation
* @{
inline address_v6
__make_address_v6(const char* __addr, const char* __scope, error_code& __ec)
{
+#ifdef _GLIBCXX_HAVE_ARPA_INET_H
address_v6::bytes_type __b;
- int __res = ::inet_pton(AF_INET6, __addr, __b.data());
+ const int __res = ::inet_pton(AF_INET6, __addr, __b.data());
if (__res == 1)
{
__ec.clear();
__ec = std::make_error_code(std::errc::invalid_argument);
else
__ec.assign(errno, generic_category());
+#else
+ __ec = std::make_error_code(__unsupported_err());
+#endif
return {};
}
__str++;
}
if (__out == std::end(__buf))
- __ec = std::make_error_code(std::errc::invalid_argument);
+ {
+ __ec = std::make_error_code(std::errc::invalid_argument);
+ return {};
+ }
else
{
*__out = '\0';
__n++;
}
if (__out == std::end(__buf))
- __ec = std::make_error_code(std::errc::invalid_argument);
+ {
+ __ec = std::make_error_code(std::errc::invalid_argument);
+ return {};
+ }
else
{
*__out = '\0';
__n++;
}
if (__out == std::end(__buf))
- __ec = std::make_error_code(std::errc::invalid_argument);
+ {
+ __ec = std::make_error_code(std::errc::invalid_argument);
+ return {};
+ }
else
{
*__out = '\0';
make_address_v6(string_view __str)
{ return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
- // @}
+ /// @}
/** ip::address creation
* @{
{ return make_address(__str, __throw_on_error{"make_address"}); }
inline address
- make_address(const string& __str, error_code& __ec) noexcept; // TODO
+ make_address(const string& __str, error_code& __ec) noexcept
+ { return make_address(__str.c_str(), __ec); }
inline address
make_address(const string& __str)
make_address(string_view __str)
{ return make_address(__str, __throw_on_error{"make_address"}); }
- // @}
+ /// @}
/// ip::address I/O
template<typename _CharT, typename _Traits>
{
public:
// types:
- typedef address_v4 value_type;
- typedef ptrdiff_t difference_type;
- typedef const address_v4* pointer;
- typedef const address_v4& reference;
- typedef input_iterator_tag iterator_category;
+ using value_type = address_v4;
+ using difference_type = ptrdiff_t;
+ using pointer = const address_v4*;
+ using reference = const address_v4&;
+ using iterator_category = input_iterator_tag;
// constructors:
basic_address_iterator(const address_v4& __a) noexcept
address_v4 _M_address;
};
- typedef basic_address_iterator<address_v4> address_v4_iterator;
+ using address_v4_iterator = basic_address_iterator<address_v4>;
template<> class basic_address_iterator<address_v6>
{
public:
// types:
- typedef address_v6 value_type;
- typedef ptrdiff_t difference_type;
- typedef const address_v6* pointer;
- typedef const address_v6& reference;
- typedef input_iterator_tag iterator_category;
+ using value_type = address_v6;
+ using difference_type = ptrdiff_t;
+ using pointer = const address_v6*;
+ using reference = const address_v6&;
+ using iterator_category = input_iterator_tag;
// constructors:
basic_address_iterator(const address_v6& __a) noexcept
address_v6 _M_address;
};
- typedef basic_address_iterator<address_v6> address_v6_iterator;
+ using address_v6_iterator = basic_address_iterator<address_v6>;
template<typename> class basic_address_range; // not defined
public:
// types:
- typedef basic_address_iterator<address_v4> iterator;
+ using iterator = basic_address_iterator<address_v4>;
// constructors:
iterator begin() const noexcept { return _M_begin; }
iterator end() const noexcept { return _M_end; }
- bool empty() const noexcept { return _M_begin == _M_end; }
+ _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
size_t
size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); }
iterator _M_end;
};
- typedef basic_address_range<address_v4> address_v4_range;
+ using address_v4_range = basic_address_range<address_v4>;
- // @}
+ /// @}
/** An IPv6 address range.
* @{
public:
// types:
- typedef basic_address_iterator<address_v6> iterator;
+ using iterator = basic_address_iterator<address_v6>;
// constructors:
iterator begin() const noexcept { return _M_begin; }
iterator end() const noexcept { return _M_end; }
- bool empty() const noexcept { return _M_begin == _M_end; }
+ _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
iterator
find(const address_v6& __addr) const noexcept
iterator _M_end;
};
- typedef basic_address_range<address_v6> address_v6_range;
+ using address_v6_range = basic_address_range<address_v6>;
- bool
+ /// @}
+
+ constexpr bool
operator==(const network_v4& __a, const network_v4& __b) noexcept;
- bool
+ constexpr bool
operator==(const network_v6& __a, const network_v6& __b) noexcept;
- // @}
/// An IPv4 network address.
class network_v4
constexpr address_v4
netmask() const noexcept
{
- address_v4::uint_type __val = address_v4::broadcast().to_uint();
- __val >>= (32 - _M_prefix_len);
- __val <<= (32 - _M_prefix_len);
- return address_v4{__val};
+ address_v4 __m;
+ if (_M_prefix_len)
+ __m = address_v4(0xFFFFFFFFu << (32 - _M_prefix_len));
+ return __m;
}
constexpr address_v4
constexpr address_v4
broadcast() const noexcept
- { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; }
+ {
+ auto __b = _M_addr.to_uint();
+ if (_M_prefix_len < 32)
+ __b |= 0xFFFFFFFFu >> _M_prefix_len;
+ return address_v4{__b};
+ }
address_v4_range
hosts() const noexcept
__string_with<_Allocator>
to_string(const _Allocator& __a = _Allocator()) const
{
- return address().to_string(__a) + '/'
- + std::to_string(prefix_length());
+ auto __str = address().to_string(__a);
+ const unsigned __addrlen = __str.length();
+ const unsigned __preflen = prefix_length() >= 10 ? 2 : 1;
+ auto __write = [=](char* __p, size_t __n) {
+ __p[__addrlen] = '/';
+ std::__detail::__to_chars_10_impl(__p + __addrlen + 1, __preflen,
+ (unsigned char)prefix_length());
+ return __n;
+ };
+ const unsigned __len = __addrlen + 1 + __preflen;
+#if __cpp_lib_string_resize_and_overwrite
+ __str.resize_and_overwrite(__len, __write);
+#else
+ __str.resize(__len);
+ __write(&__str.front(), __len);
+#endif
+ return __str;
}
private:
* @{
*/
- inline bool
+ constexpr bool
operator==(const network_v4& __a, const network_v4& __b) noexcept
{
return __a.address() == __b.address()
&& __a.prefix_length() == __b.prefix_length();
}
- inline bool
+ constexpr bool
operator!=(const network_v4& __a, const network_v4& __b) noexcept
{ return !(__a == __b); }
- // @}
+ /// @}
/** ip::network_v6 comparisons
* @{
*/
- inline bool
+ constexpr bool
operator==(const network_v6& __a, const network_v6& __b) noexcept
{
return __a.address() == __b.address()
&& __a.prefix_length() == __b.prefix_length();
}
- inline bool
+ constexpr bool
operator!=(const network_v6& __a, const network_v6& __b) noexcept
{ return !(__a == __b); }
- // @}
+ /// @}
/** ip::network_v4 creation
* @{
make_network_v4(const address_v4& __a, int __prefix_len)
{ return network_v4{__a, __prefix_len}; }
- network_v4
+ inline network_v4
make_network_v4(const address_v4& __a, const address_v4& __mask)
{ return network_v4{ __a, __mask }; }
make_network_v4(string_view __str)
{ return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
- // @}
+ /// @}
/** ip::network_v6 creation
* @{
make_network_v6(string_view __str)
{ return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
- // @}
+ /// @}
/// ip::network_v4 I/O
template<typename _CharT, typename _Traits>
operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v6& __net)
{ return __os << __net.to_string(); }
+#if defined IPPROTO_TCP || defined IPPROTO_UDP
/// An IP endpoint.
template<typename _InternetProtocol>
class basic_endpoint
{
public:
// types:
- typedef _InternetProtocol protocol_type;
+ using protocol_type = _InternetProtocol;
// constructors:
- constexpr
+ _GLIBCXX20_CONSTEXPR
basic_endpoint() noexcept : _M_data()
- { _M_data._M_v4.sin_family = protocol_type::v4().family(); }
+ {
+ _M_data._M_v4.sin_family = protocol_type::v4().family();
+ // If in_addr contains a union, make the correct member active:
+ if (std::__is_constant_evaluated())
+ std::_Construct(&_M_data._M_v4.sin_addr.s_addr);
+ }
- constexpr
+ _GLIBCXX20_CONSTEXPR
basic_endpoint(const protocol_type& __proto,
port_type __port_num) noexcept
: _M_data()
{
- __glibcxx_assert(__proto == protocol_type::v4()
- || __proto == protocol_type::v6());
+ if (__proto == protocol_type::v4())
+ {
+ _M_data._M_v4.sin_family = protocol_type::v4().family();
+ _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
+ if (std::__is_constant_evaluated())
+ std::_Construct(&_M_data._M_v4.sin_addr.s_addr);
+ }
+ else if (__proto == protocol_type::v6())
+ {
+ std::_Construct(&_M_data._M_v6);
+ _M_data._M_v6.sin6_family = __proto.family();
+ _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num);
+ _M_data._M_v6.sin6_scope_id = 0;
+ if (std::__is_constant_evaluated())
+ std::_Construct(&_M_data._M_v6.sin6_addr.s6_addr);
+ }
+ else
+ {
+ __glibcxx_assert(__proto == protocol_type::v4()
+ || __proto == protocol_type::v6());
- _M_data._M_v4.sin_family = __proto.family();
- _M_data._M_v4.sin_port = address_v4::_S_hton(__port_num);
+ }
}
- constexpr
+ _GLIBCXX20_CONSTEXPR
basic_endpoint(const ip::address& __addr,
port_type __port_num) noexcept
: _M_data()
if (__addr.is_v4())
{
_M_data._M_v4.sin_family = protocol_type::v4().family();
- _M_data._M_v4.sin_port = address_v4::_S_hton(__port_num);
- _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
+ _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
+ std::_Construct(&_M_data._M_v4.sin_addr.s_addr,
+ __addr._M_v4._M_addr);
}
else
{
- _M_data._M_v6 = {};
+ std::_Construct(&_M_data._M_v6);
_M_data._M_v6.sin6_family = protocol_type::v6().family();
- _M_data._M_v6.sin6_port = address_v4::_S_hton(__port_num);
- __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
- __addr._M_v6._M_bytes.data(), 16);
+ _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num);
+ if (std::__is_constant_evaluated())
+ std::_Construct(&_M_data._M_v6.sin6_addr.s6_addr);
+ uint8_t* __s6a = _M_data._M_v6.sin6_addr.s6_addr;
+ for (int __i = 0; __i < 16; ++__i)
+ __s6a[__i] = __addr._M_v6._M_bytes[__i];
_M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
}
}
// members:
+
constexpr protocol_type protocol() const noexcept
{
- return _M_data._M_v4.sin_family == AF_INET6
- ? protocol_type::v6() : protocol_type::v4();
+ return _M_is_v6() ? protocol_type::v6() : protocol_type::v4();
}
constexpr ip::address
address() const noexcept
{
- ip::address __addr;
- if (protocol().family() == AF_INET6)
+ if (_M_is_v6())
{
- __builtin_memcpy(&__addr._M_v6._M_bytes,
- _M_data._M_v6.sin6_addr.s6_addr, 16);
- __addr._M_is_v4 = false;
+ address_v6 __v6;
+ const uint8_t* __s6a = _M_data._M_v6.sin6_addr.s6_addr;
+ for (int __i = 0; __i < 16; ++__i)
+ __v6._M_bytes[__i] = __s6a[__i];
+ __v6._M_scope_id = _M_data._M_v6.sin6_scope_id;
+ return __v6;
}
else
{
- __builtin_memcpy(&__addr._M_v4._M_addr,
- &_M_data._M_v4.sin_addr.s_addr, 4);
+ address_v4 __v4;
+ __v4._M_addr = _M_data._M_v4.sin_addr.s_addr;
+ return __v4;
}
- return __addr;
}
void
{
if (__addr.is_v6())
{
- _M_data._M_v6 = {};
+ std::_Construct(&_M_data._M_v6);
_M_data._M_v6.sin6_family = protocol_type::v6().family();
__builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
__addr._M_v6._M_bytes.data(), 16);
}
else
{
+ std::_Construct(&_M_data._M_v4);
_M_data._M_v4.sin_family = protocol_type::v4().family();
_M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
}
constexpr port_type
port() const noexcept
- { return address_v4::_S_ntoh(_M_data._M_v4.sin_port); }
+ {
+ port_type __p = 0;
+ if (_M_is_v6())
+ __p = _M_data._M_v6.sin6_port;
+ else
+ __p = _M_data._M_v4.sin_port;
+ return address_v4::_S_ntoh_16(__p);
+ }
void
port(port_type __port_num) noexcept
- { _M_data._M_v4.sin_port = address_v4::_S_hton(__port_num); }
+ {
+ __port_num = address_v4::_S_hton_16(__port_num);
+ if (_M_is_v6())
+ _M_data._M_v6.sin6_port = __port_num;
+ else
+ _M_data._M_v4.sin_port = __port_num;
+ }
void* data() noexcept { return &_M_data; }
+
const void* data() const noexcept { return &_M_data; }
- constexpr size_t size() const noexcept
- {
- return protocol().family() == AF_INET6
- ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
- }
+
+ constexpr size_t
+ size() const noexcept
+ { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); }
void
resize(size_t __s)
{
- if ((protocol().family() == AF_INET6 && __s != sizeof(sockaddr_in6))
- || (protocol().family() == AF_INET && __s != sizeof(sockaddr_in)))
+ if (__s != size())
__throw_length_error("net::ip::basic_endpoint::resize");
}
sockaddr_in _M_v4;
sockaddr_in6 _M_v6;
} _M_data;
+
+ constexpr bool
+ _M_is_v6() const noexcept
+ {
+ // For constexpr eval we can just detect which union member is active.
+ // i.e. emulate P2641R1's std::is_active_member(&_M_data._M_v6)).
+ if (std::__is_constant_evaluated())
+ return __builtin_constant_p(_M_data._M_v6.sin6_family);
+ return _M_data._M_v6.sin6_family == AF_INET6;
+ }
};
/** basic_endpoint comparisons
*/
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator==(const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{ return __a.address() == __b.address() && __a.port() == __b.port(); }
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator!=(const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{ return !(__a == __b); }
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator< (const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{
}
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator> (const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{ return __b < __a; }
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator<=(const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{ return !(__b < __a); }
template<typename _InternetProtocol>
- inline bool
+ constexpr bool
operator>=(const basic_endpoint<_InternetProtocol>& __a,
const basic_endpoint<_InternetProtocol>& __b)
{ return !(__a < __b); }
- // @}
+ /// @}
/// basic_endpoint I/O
template<typename _CharT, typename _Traits, typename _InternetProtocol>
{
public:
// types:
- typedef _InternetProtocol protocol_type;
- typedef typename _InternetProtocol::endpoint endpoint_type;
+ using protocol_type = _InternetProtocol;
+ using endpoint_type = typename _InternetProtocol::endpoint;
// constructors:
basic_resolver_entry() { }
const basic_resolver_entry<_InternetProtocol>& __b)
{ return !(__a == __b); }
- // @}
+ /// @}
/** Base class defining flags for name/address resolution.
* @{
class resolver_base
{
public:
- enum flags : int
- {
- __flags_passive = AI_PASSIVE,
- __flags_canonical_name = AI_CANONNAME,
- __flags_numeric_host = AI_NUMERICHOST,
+ enum flags : int { };
+ static constexpr flags passive = (flags)AI_PASSIVE;
+ static constexpr flags canonical_name = (flags)AI_CANONNAME;
+ static constexpr flags numeric_host = (flags)AI_NUMERICHOST;
#ifdef AI_NUMERICSERV
- __flags_numeric_service = AI_NUMERICSERV,
+ static constexpr flags numeric_service = (flags)AI_NUMERICSERV;
#endif
- __flags_v4_mapped = AI_V4MAPPED,
- __flags_all_matching = AI_ALL,
- __flags_address_configured = AI_ADDRCONFIG
- };
- static constexpr flags passive = __flags_passive;
- static constexpr flags canonical_name = __flags_canonical_name;
- static constexpr flags numeric_host = __flags_numeric_host;
-#ifdef AI_NUMERICSERV
- static constexpr flags numeric_service = __flags_numeric_service;
+#ifdef AI_V4MAPPED
+ static constexpr flags v4_mapped = (flags)AI_V4MAPPED;
+#endif
+#ifdef AI_ALL
+ static constexpr flags all_matching = (flags)AI_ALL;
+#endif
+#ifdef AI_ADDRCONFIG
+ static constexpr flags address_configured = (flags)AI_ADDRCONFIG;
#endif
- static constexpr flags v4_mapped = __flags_v4_mapped;
- static constexpr flags all_matching = __flags_all_matching;
- static constexpr flags address_configured = __flags_address_configured;
- protected:
- resolver_base() = default;
- ~resolver_base() = default;
- };
+ friend constexpr flags
+ operator&(flags __f1, flags __f2) noexcept
+ { return flags( int(__f1) & int(__f2) ); }
- constexpr resolver_base::flags
- operator&(resolver_base::flags __f1, resolver_base::flags __f2)
- { return resolver_base::flags( int(__f1) & int(__f2) ); }
+ friend constexpr flags
+ operator|(flags __f1, flags __f2) noexcept
+ { return flags( int(__f1) | int(__f2) ); }
- constexpr resolver_base::flags
- operator|(resolver_base::flags __f1, resolver_base::flags __f2)
- { return resolver_base::flags( int(__f1) | int(__f2) ); }
+ friend constexpr flags
+ operator^(flags __f1, flags __f2) noexcept
+ { return flags( int(__f1) ^ int(__f2) ); }
- constexpr resolver_base::flags
- operator^(resolver_base::flags __f1, resolver_base::flags __f2)
- { return resolver_base::flags( int(__f1) ^ int(__f2) ); }
+ friend constexpr flags
+ operator~(flags __f) noexcept
+ { return flags( ~int(__f) ); }
- constexpr resolver_base::flags
- operator~(resolver_base::flags __f)
- { return resolver_base::flags( ~int(__f) ); }
+ friend constexpr flags&
+ operator&=(flags& __f1, flags __f2) noexcept
+ { return __f1 = (__f1 & __f2); }
- inline resolver_base::flags&
- operator&=(resolver_base::flags& __f1, resolver_base::flags __f2)
- { return __f1 = (__f1 & __f2); }
+ friend constexpr flags&
+ operator|=(flags& __f1, flags __f2) noexcept
+ { return __f1 = (__f1 | __f2); }
- inline resolver_base::flags&
- operator|=(resolver_base::flags& __f1, resolver_base::flags __f2)
- { return __f1 = (__f1 | __f2); }
+ friend constexpr flags&
+ operator^=(flags& __f1, flags __f2) noexcept
+ { return __f1 = (__f1 ^ __f2); }
- inline resolver_base::flags&
- operator^=(resolver_base::flags& __f1, resolver_base::flags __f2)
- { return __f1 = (__f1 ^ __f2); }
+ protected:
+ resolver_base() = default;
+ ~resolver_base() = default;
+ };
- // TODO define resolver_base::flags static constants for C++14 mode
+ // TODO define resolver_base::flags static constants in .so for C++14 mode
- // @}
+ /// @}
/** Container for results of name/address resolution.
* @{
{
public:
// types:
- typedef _InternetProtocol protocol_type;
- typedef typename protocol_type::endpoint endpoint_type;
- typedef basic_resolver_entry<protocol_type> value_type;
- typedef const value_type& const_reference;
- typedef value_type& reference;
- typedef typename forward_list<value_type>::const_iterator const_iterator;
- typedef const_iterator iterator;
- typedef ptrdiff_t difference_type;
- typedef size_t size_type;
+ using protocol_type = _InternetProtocol;
+ using endpoint_type = typename protocol_type::endpoint;
+ using value_type = basic_resolver_entry<protocol_type>;
+ using const_reference = const value_type&;
+ using reference = value_type&;
+ using const_iterator = typename forward_list<value_type>::const_iterator;
+ using iterator = const_iterator;
+ using difference_type = ptrdiff_t;
+ using size_type = size_t;
// construct / copy / destroy:
// size:
size_type size() const noexcept { return _M_size; }
size_type max_size() const noexcept { return _M_results.max_size(); }
- bool empty() const noexcept { return _M_results.empty(); }
+
+ _GLIBCXX_NODISCARD bool
+ empty() const noexcept { return _M_results.empty(); }
// element access:
const_iterator begin() const { return _M_results.begin(); }
const basic_resolver_results<_InternetProtocol>& __b)
{ return !(__a == __b); }
- // @}
+ /// @}
/// Perform name/address resolution.
template<typename _InternetProtocol>
public:
// types:
- typedef io_context::executor_type executor_type;
- typedef _InternetProtocol protocol_type;
- typedef typename _InternetProtocol::endpoint endpoint_type;
- typedef basic_resolver_results<_InternetProtocol> results_type;
+ using executor_type = io_context::executor_type;
+ using protocol_type = _InternetProtocol;
+ using endpoint_type = typename _InternetProtocol::endpoint;
+ using results_type = basic_resolver_results<_InternetProtocol>;
// construct / copy / destroy:
if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p))
{
- __ec.assign(__err, resolver_category());
+ __ec = ip::__make_resolver_error_code(__err, errno);
return;
}
__ec.clear();
basic_resolver_results(const endpoint_type& __ep, error_code& __ec)
{
#ifdef _GLIBCXX_HAVE_NETDB_H
- char __host_name[256];
- char __service_name[128];
+ char __host_name[1025]; // glibc NI_MAXHOST
+ char __service_name[32]; // glibc NI_MAXSERV
int __flags = 0;
if (__ep.protocol().type() == SOCK_DGRAM)
__flags |= NI_DGRAM;
__flags);
}
if (__err)
- __ec.assign(__err, resolver_category());
+ __ec = ip::__make_resolver_error_code(__err, errno);
else
{
__ec.clear();
__ec = std::make_error_code(errc::operation_not_supported);
#endif
}
+#endif // IPPROTO_TCP || IPPROTO_UDP
/** The name of the local host.
* @{
host_name()
{ return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); }
- // @}
+ /// @}
+#ifdef IPPROTO_TCP
/// The TCP byte-stream protocol.
class tcp
{
public:
// types:
- typedef basic_endpoint<tcp> endpoint; ///< A TCP endpoint.
- typedef basic_resolver<tcp> resolver; ///< A TCP resolver.
- typedef basic_stream_socket<tcp> socket; ///< A TCP socket.
- typedef basic_socket_acceptor<tcp> acceptor; ///< A TCP acceptor.
- typedef basic_socket_iostream<tcp> iostream; /// A TCP iostream.
+ using endpoint = basic_endpoint<tcp>; ///< A TCP endpoint.
+ using resolver = basic_resolver<tcp>; ///< A TCP resolver.
+ using socket = basic_stream_socket<tcp>; ///< A TCP socket.
+ using acceptor = basic_socket_acceptor<tcp>; ///< A TCP acceptor.
+ using iostream = basic_socket_iostream<tcp>; ///< A TCP iostream.
-#ifdef _GLIBCXX_HAVE_NETINET_TCP_H
+#ifdef TCP_NODELAY
/// Disable coalescing of small segments (i.e. the Nagle algorithm).
struct no_delay : __sockopt_crtp<no_delay, bool>
{
using __sockopt_crtp::__sockopt_crtp;
+ using __sockopt_crtp::operator=;
static const int _S_level = IPPROTO_TCP;
static const int _S_name = TCP_NODELAY;
* @{
*/
- inline bool
- operator==(const tcp& __a, const tcp& __b)
+ constexpr bool
+ operator==(const tcp& __a, const tcp& __b) noexcept
{ return __a.family() == __b.family(); }
- inline bool
- operator!=(const tcp& __a, const tcp& __b)
+ constexpr bool
+ operator!=(const tcp& __a, const tcp& __b) noexcept
{ return !(__a == __b); }
- // @}
+ /// @}
+#endif // IPPROTO_TCP
+#ifdef IPPROTO_UDP
/// The UDP datagram protocol.
class udp
{
public:
// types:
- typedef basic_endpoint<udp> endpoint;
- typedef basic_resolver<udp> resolver;
- typedef basic_datagram_socket<udp> socket;
+ using endpoint = basic_endpoint<udp>;
+ using resolver = basic_resolver<udp>;
+ using socket = basic_datagram_socket<udp>;
// static members:
static constexpr udp v4() noexcept { return udp(AF_INET); }
* @{
*/
- bool
- operator==(const udp& __a, const udp& __b)
+ constexpr bool
+ operator==(const udp& __a, const udp& __b) noexcept
{ return __a.family() == __b.family(); }
- inline bool
- operator!=(const udp& __a, const udp& __b)
+ constexpr bool
+ operator!=(const udp& __a, const udp& __b) noexcept
{ return !(__a == __b); }
- // @}
+ /// @}
+#endif // IPPROTO_UDP
+
+#if defined IPPROTO_IP && defined IPPROTO_IPV6
/// Restrict a socket created for an IPv6 protocol to IPv6 only.
- struct v6_only : __sockopt_crtp<v6_only, bool>
+ class v6_only : public __sockopt_crtp<v6_only, bool>
{
+ public:
using __sockopt_crtp::__sockopt_crtp;
+ using __sockopt_crtp::operator=;
+ private:
+ friend __sockopt_crtp<v6_only, bool>;
static const int _S_level = IPPROTO_IPV6;
static const int _S_name = IPV6_V6ONLY;
};
namespace unicast
{
/// Set the default number of hops (TTL) for outbound datagrams.
- struct hops : __sockopt_crtp<hops>
+ class hops : public __sockopt_crtp<hops>
{
+ public:
using __sockopt_crtp::__sockopt_crtp;
+ using __sockopt_crtp::operator=;
template<typename _Protocol>
int
namespace multicast
{
- /// Request that a socket joins a multicast group.
- struct join_group
+ class __mcastopt
{
+ public:
explicit
- join_group(const address&);
+ __mcastopt(const address& __grp) noexcept
+ : __mcastopt(__grp.is_v4() ? __mcastopt(__grp.to_v4()) : __mcastopt(__grp.to_v6()))
+ { }
explicit
- join_group(const address_v4&, const address_v4& = address_v4::any());
+ __mcastopt(const address_v4& __grp,
+ const address_v4& __iface = address_v4::any()) noexcept
+ {
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ _M_v4.imr_multiaddr.s_addr = __grp.to_uint();
+ _M_v4.imr_interface.s_addr = __iface.to_uint();
+#else
+ _M_v4.imr_multiaddr.s_addr = __builtin_bswap32(__grp.to_uint());
+ _M_v4.imr_interface.s_addr = __builtin_bswap32(__iface.to_uint());
+#endif
+ }
explicit
- join_group(const address_v6&, unsigned int = 0);
+ __mcastopt(const address_v6& __grp, unsigned int __iface = 0) noexcept
+ {
+ const auto __addr = __grp.to_bytes();
+ __builtin_memcpy(_M_v6.ipv6mr_multiaddr.s6_addr, __addr.data(), 16);
+ _M_v6.ipv6mr_interface = __iface;
+ }
template<typename _Protocol>
int
level(const _Protocol& __p) const noexcept
{ return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
- template<typename _Protocol>
- int
- name(const _Protocol& __p) const noexcept
- {
- return __p.family() == AF_INET6
- ? IPV6_JOIN_GROUP : IP_ADD_MEMBERSHIP;
- }
- template<typename _Protocol>
- void*
- data(const _Protocol&) noexcept
- { return std::addressof(_M_value); }
-
template<typename _Protocol>
const void*
- data(const _Protocol&) const noexcept
- { return std::addressof(_M_value); }
+ data(const _Protocol& __p) const noexcept
+ { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
template<typename _Protocol>
size_t
size(const _Protocol& __p) const noexcept
- {
- return __p.family() == AF_INET6
- ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
- }
-
- template<typename _Protocol>
- void
- resize(const _Protocol& __p, size_t __s)
- {
- if (__s != size(__p))
- __throw_length_error("invalid value for socket option resize");
- }
+ { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
- protected:
- union
- {
- ipv6_mreq _M_v6;
- ip_mreq _M_v4;
- } _M_value;
+ private:
+ ipv6_mreq _M_v6 = {};
+ ip_mreq _M_v4 = {};
};
- /// Request that a socket leaves a multicast group.
- struct leave_group
+ /// Request that a socket joins a multicast group.
+ class join_group : private __mcastopt
{
- explicit
- leave_group(const address&);
-
- explicit
- leave_group(const address_v4&, const address_v4& = address_v4::any());
-
- explicit
- leave_group(const address_v6&, unsigned int = 0);
-
- template<typename _Protocol>
- int
- level(const _Protocol& __p) const noexcept
- { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
+ public:
+ using __mcastopt::__mcastopt;
+ using __mcastopt::level;
+ using __mcastopt::data;
+ using __mcastopt::size;
template<typename _Protocol>
int
name(const _Protocol& __p) const noexcept
{
- return __p.family() == AF_INET6
- ? IPV6_LEAVE_GROUP : IP_DROP_MEMBERSHIP;
+ if (__p.family() == AF_INET6)
+ return IPV6_JOIN_GROUP;
+ return IP_ADD_MEMBERSHIP;
}
- template<typename _Protocol>
- void*
- data(const _Protocol&) noexcept
- { return std::addressof(_M_value); }
-
- template<typename _Protocol>
- const void*
- data(const _Protocol&) const noexcept
- { return std::addressof(_M_value); }
+ };
- template<typename _Protocol>
- size_t
- size(const _Protocol& __p) const noexcept
- {
- return __p.family() == AF_INET6
- ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
- }
+ /// Request that a socket leaves a multicast group.
+ class leave_group : private __mcastopt
+ {
+ public:
+ using __mcastopt::__mcastopt;
+ using __mcastopt::level;
+ using __mcastopt::data;
+ using __mcastopt::size;
template<typename _Protocol>
- void
- resize(const _Protocol& __p, size_t __s)
+ int
+ name(const _Protocol& __p) const noexcept
{
- if (__s != size(__p))
- __throw_length_error("invalid value for socket option resize");
+ if (__p.family() == AF_INET6)
+ return IPV6_LEAVE_GROUP;
+ return IP_DROP_MEMBERSHIP;
}
-
- protected:
- union
- {
- ipv6_mreq _M_v6;
- ip_mreq _M_v4;
- } _M_value;
};
/// Specify the network interface for outgoing multicast datagrams.
class outbound_interface
{
+ public:
explicit
- outbound_interface(const address_v4&);
+ outbound_interface(const address_v4& __v4) noexcept
+ {
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ _M_v4.s_addr = __v4.to_uint();
+#else
+ _M_v4.s_addr = __builtin_bswap32(__v4.to_uint());
+#endif
+ }
explicit
- outbound_interface(unsigned int);
+ outbound_interface(unsigned int __v6) noexcept
+ : _M_v4(), _M_v6(__v6)
+ { }
template<typename _Protocol>
int
template<typename _Protocol>
const void*
- data(const _Protocol&) const noexcept
- { return std::addressof(_M_value); }
+ data(const _Protocol& __p) const noexcept
+ { return __p.family() == AF_INET6 ? &_M_v6 : &_M_v4; }
template<typename _Protocol>
size_t
size(const _Protocol& __p) const noexcept
- {
- return __p.family() == AF_INET6
- ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
- }
+ { return __p.family() == AF_INET6 ? sizeof(_M_v6) : sizeof(_M_v4); }
- protected:
- union {
- unsigned _M_v6;
- in_addr _M_v4;
- } _M_value;
+ private:
+ in_addr _M_v4;
+ unsigned _M_v6 = 0;
};
/// Set the default number of hops (TTL) for outbound datagrams.
- struct hops : __sockopt_crtp<hops>
+ class hops : public __sockopt_crtp<hops>
{
+ public:
using __sockopt_crtp::__sockopt_crtp;
+ using __sockopt_crtp::operator=;
template<typename _Protocol>
int
};
/// Set whether datagrams are delivered back to the local application.
- struct enable_loopback : __sockopt_crtp<enable_loopback>
+ class enable_loopback : public __sockopt_crtp<enable_loopback, bool>
{
+ public:
using __sockopt_crtp::__sockopt_crtp;
+ using __sockopt_crtp::operator=;
template<typename _Protocol>
int
} // namespace multicast
- // @}
+#endif // IPPROTO_IP && IPPROTO_IPV6
+
+ /// @}
} // namespace ip
} // namespace v1
: __hash_base<size_t, experimental::net::v1::ip::address>
{
size_t
- operator()(const argument_type& __a) const
+ operator()(const experimental::net::v1::ip::address& __a) const
{
if (__a.is_v4())
return _Hash_impl::hash(__a.to_v4());
: __hash_base<size_t, experimental::net::v1::ip::address_v4>
{
size_t
- operator()(const argument_type& __a) const
+ operator()(const experimental::net::v1::ip::address_v4& __a) const
{ return _Hash_impl::hash(__a.to_bytes()); }
};
: __hash_base<size_t, experimental::net::v1::ip::address_v6>
{
size_t
- operator()(const argument_type& __a) const
+ operator()(const experimental::net::v1::ip::address_v6& __a) const
{ return _Hash_impl::hash(__a.to_bytes()); }
};