]> 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 f6d6ef34504f20364299676976ce685fd5fc43cb..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
 # 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
@@ -85,6 +92,12 @@ namespace ip
     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
   };
 
@@ -113,6 +126,19 @@ 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.
@@ -172,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
@@ -201,12 +232,17 @@ 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
@@ -216,17 +252,28 @@ namespace ip
       __string_with<_Allocator>
       to_string(const _Allocator& __a = _Allocator()) const
       {
-#ifdef _GLIBCXX_HAVE_ARPA_INET_H
+       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(INET_ADDRSTRLEN);
-       if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
-         __str.erase(__str.find('\0'));
-       else
-         __str.resize(0);
-       return __str;
+#if __cpp_lib_string_resize_and_overwrite
+       __str.resize_and_overwrite(15, __write);
 #else
-       std::__throw_system_error((int)__unsupported_err());
+       __str.resize(15);
+       __str.resize(__write(&__str.front(), 15));
 #endif
+       return __str;
       }
 
     // static members:
@@ -263,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.
@@ -433,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
@@ -462,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;
     }
@@ -470,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;
     }
@@ -705,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))
       {
@@ -924,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)
@@ -1166,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;
 
 
@@ -1210,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
@@ -1222,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
@@ -1253,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:
@@ -1326,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); }
 
@@ -1343,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); }
 
@@ -1428,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
@@ -1438,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()
@@ -1463,20 +1560,25 @@ 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_is_v6() ? protocol_type::v6() : protocol_type::v4();
@@ -1485,19 +1587,21 @@ namespace ip
       constexpr ip::address
       address() const noexcept
       {
-       ip::address __addr;
        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
@@ -1505,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);
@@ -1513,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;
          }
@@ -1520,17 +1625,31 @@ 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
+      constexpr size_t
+      size() const noexcept
       { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); }
 
       void
@@ -1549,8 +1668,15 @@ namespace ip
        sockaddr_in6    _M_v6;
       } _M_data;
 
-      constexpr bool _M_is_v6() const noexcept
-      { return _M_data._M_v4.sin_family == AF_INET6; }
+      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
@@ -1558,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)
     {
@@ -1579,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); }
@@ -1686,9 +1812,15 @@ namespace ip
 #ifdef AI_NUMERICSERV
     static constexpr flags numeric_service     = (flags)AI_NUMERICSERV;
 #endif
+#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
 
     friend constexpr flags
     operator&(flags __f1, flags __f2) noexcept
@@ -1997,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();
@@ -2026,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;
@@ -2045,7 +2177,7 @@ namespace ip
                                __flags);
        }
       if (__err)
-       __ec.assign(__err, resolver_category());
+       __ec = ip::__make_resolver_error_code(__err, errno);
       else
        {
          __ec.clear();
@@ -2056,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.
    * @{
@@ -2102,7 +2235,7 @@ 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 TCP_NODELAY
     /// Disable coalescing of small segments (i.e. the Nagle algorithm).