]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/experimental/internet
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / internet
index cd19de59e709a14745d6b7c578f35efc72151789..f04163dc4534f0ee5a3f4be447b844e5802047fd 100644 (file)
@@ -1,6 +1,6 @@
 // <experimental/internet> -*- C++ -*-
 
-// Copyright (C) 2015-2021 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
@@ -32,6 +32,8 @@
 
 #pragma GCC system_header
 
+#include <bits/requires_hosted.h> // experimental is currently omitted
+
 #if __cplusplus >= 201402L
 
 #include <experimental/netfwd>
@@ -42,6 +44,7 @@
 #include <sstream>
 #include <cstdint>
 #include <experimental/string_view>
+#include <bits/charconv.h>
 #ifdef _GLIBCXX_HAVE_UNISTD_H
 # include <unistd.h>
 #endif
@@ -52,7 +55,7 @@
 # include <arpa/inet.h>                // inet_ntop
 #endif
 #ifdef _GLIBCXX_HAVE_NETINET_IN_H
-# include <netinet/in.h>       // IPPROTO_IP
+# 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
 # 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
@@ -72,20 +79,26 @@ inline namespace v1
 {
 namespace ip
 {
-
   /** @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.
@@ -94,7 +107,13 @@ namespace ip
     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;
@@ -107,8 +126,20 @@ namespace ip
   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
+
+  /// @}
 
   using port_type = uint_least16_t;    ///< Type used for port numbers.
   using scope_id_type = uint_least32_t;        ///< Type used for IPv6 scope IDs.
@@ -119,6 +150,16 @@ namespace ip
       = 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.
    * @{
    */
@@ -157,7 +198,12 @@ namespace ip
 
     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
@@ -186,31 +232,49 @@ namespace ip
     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_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{}; }
@@ -246,7 +310,11 @@ namespace ip
     _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.
@@ -256,8 +324,11 @@ namespace ip
     // 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:
@@ -338,11 +409,11 @@ namespace ip
 
     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 + (_M_scope_id ? 11 : 0));
        char* const __p = &__str.front();
@@ -364,8 +435,10 @@ namespace ip
        else
          __str.resize(0);
        return __str;
-      }
+#else
+       std::__throw_system_error((int)__unsupported_err());
 #endif
+      }
 
     // static members:
 
@@ -411,13 +484,15 @@ namespace ip
     // 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
@@ -440,7 +515,7 @@ namespace ip
     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;
     }
@@ -448,7 +523,7 @@ namespace ip
     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;
     }
@@ -650,6 +725,7 @@ namespace ip
   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)
@@ -661,6 +737,9 @@ namespace ip
       __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 {};
   }
 
@@ -679,7 +758,7 @@ namespace ip
   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))
       {
@@ -718,8 +797,9 @@ namespace ip
   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();
@@ -741,6 +821,9 @@ namespace ip
       __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 {};
   }
 
@@ -894,7 +977,8 @@ namespace ip
   { 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)
@@ -1136,10 +1220,10 @@ namespace ip
 
   /// @}
 
-  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;
 
 
@@ -1180,10 +1264,10 @@ namespace ip
     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
@@ -1192,7 +1276,12 @@ namespace ip
 
     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
@@ -1223,8 +1312,23 @@ namespace ip
       __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:
@@ -1296,14 +1400,14 @@ namespace ip
    * @{
    */
 
-  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); }
 
@@ -1313,14 +1417,14 @@ namespace ip
    * @{
    */
 
-  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); }
 
@@ -1398,6 +1502,7 @@ namespace ip
     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
@@ -1408,23 +1513,45 @@ namespace ip
 
       // 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_16(__port_num);
+         }
       }
 
-      constexpr
+      _GLIBCXX20_CONSTEXPR
       basic_endpoint(const ip::address& __addr,
                     port_type __port_num) noexcept
       : _M_data()
@@ -1433,42 +1560,48 @@ namespace ip
          {
            _M_data._M_v4.sin_family = protocol_type::v4().family();
            _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
-           _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
+           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_16(__port_num);
-           __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
-                            __addr._M_v6._M_bytes.data(), 16);
+           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
@@ -1476,7 +1609,7 @@ namespace ip
       {
        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);
@@ -1484,6 +1617,7 @@ namespace ip
          }
        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;
          }
@@ -1491,25 +1625,37 @@ namespace ip
 
       constexpr port_type
       port() const noexcept
-      { return address_v4::_S_ntoh_16(_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_16(__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");
       }
 
@@ -1521,6 +1667,16 @@ namespace ip
        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
@@ -1528,19 +1684,19 @@ namespace ip
    */
 
   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)
     {
@@ -1549,19 +1705,19 @@ namespace ip
     }
 
   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); }
@@ -1649,62 +1805,57 @@ namespace ip
   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
 
   /// @}
 
@@ -1978,7 +2129,7 @@ namespace ip
 
       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();
@@ -2007,8 +2158,8 @@ namespace ip
     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;
@@ -2026,7 +2177,7 @@ namespace ip
                                __flags);
        }
       if (__err)
-       __ec.assign(__err, resolver_category());
+       __ec = ip::__make_resolver_error_code(__err, errno);
       else
        {
          __ec.clear();
@@ -2037,6 +2188,7 @@ namespace ip
       __ec = std::make_error_code(errc::operation_not_supported);
 #endif
     }
+#endif // IPPROTO_TCP || IPPROTO_UDP
 
   /** The name of the local host.
    * @{
@@ -2073,6 +2225,7 @@ namespace ip
 
   /// @}
 
+#ifdef IPPROTO_TCP
   /// The TCP byte-stream protocol.
   class tcp
   {
@@ -2082,13 +2235,14 @@ namespace ip
     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.
+    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;
@@ -2118,16 +2272,18 @@ namespace ip
    * @{
    */
 
-  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
   {
@@ -2157,21 +2313,28 @@ namespace ip
    * @{
    */
 
-  inline 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;
   };
@@ -2179,9 +2342,11 @@ namespace ip
   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
@@ -2197,130 +2362,111 @@ namespace ip
 
   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
@@ -2337,28 +2483,25 @@ namespace ip
 
       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
@@ -2375,9 +2518,11 @@ namespace ip
     };
 
     /// 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
@@ -2395,6 +2540,8 @@ namespace ip
 
   } // namespace multicast
 
+#endif // IPPROTO_IP && IPPROTO_IPV6
+
   /// @}
 
 } // namespace ip