From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4E2B73858417; Thu, 8 Feb 2024 15:50:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E2B73858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707407459; bh=/1PZcxC9YF8v4QGRHsFMhFdvO0Pd/mCqgh5rRUyNN+8=; h=From:To:Subject:Date:From; b=svifGsl08OXwU8yMM5FJ6+eKbwjStlSN01gbn0PU+bqGCYIMm445wCzNpdp7sEnv6 j3X8+WOi8buzbGwc6wG1sh+eujadoQR/THO2lhbpiKkJ04lZxIxu5IS1RAk1VvD8Su c57QGYCcd01o68QtLicJjLMdE4fP9yitOYNw4CuE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-8305] libstdc++: Implement some missing functions for net::ip::network_v6 X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 3c04a1533b32362c7c28fc32b05623dda45a1b44 X-Git-Newrev: 10c7e4276686581e89f4927184c7508daaf30712 Message-Id: <20240208155059.4E2B73858417@sourceware.org> Date: Thu, 8 Feb 2024 15:50:59 +0000 (GMT) List-Id: https://gcc.gnu.org/g:10c7e4276686581e89f4927184c7508daaf30712 commit r13-8305-g10c7e4276686581e89f4927184c7508daaf30712 Author: Jonathan Wakely Date: Thu Feb 1 10:08:05 2024 +0000 libstdc++: Implement some missing functions for net::ip::network_v6 libstdc++-v3/ChangeLog: * include/experimental/internet (network_v6::network): Define. (network_v6::hosts): Finish implementing. (network_v6::to_string): Do not concatenate std::string to arbitrary std::basic_string specialization. * testsuite/experimental/net/internet/network/v6/cons.cc: New test. (cherry picked from commit 5b069117e261ae0b438aee0cdff8707827810adf) Diff: --- libstdc++-v3/include/experimental/internet | 26 ++++++-- .../experimental/net/internet/network/v6/cons.cc | 74 ++++++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 173913a8cec5..5f48a06e5ab4 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -1355,17 +1355,35 @@ namespace ip constexpr address_v6 address() const noexcept { return _M_addr; } constexpr int prefix_length() const noexcept { return _M_prefix_len; } - constexpr address_v6 network() const noexcept; // TODO + _GLIBCXX17_CONSTEXPR address_v6 + network() const noexcept + { + address_v6::bytes_type __bytes = _M_addr.to_bytes(); + int __nbytes = (_M_prefix_len + 7) / 8; + for (int __n = __nbytes; __n < 16; ++__n) + __bytes[__n] = 0; + if (int __zbits = (__nbytes * 8) - _M_prefix_len) + __bytes[__nbytes - 1] &= 0xFF << __zbits; + return address_v6(__bytes, _M_addr.scope_id()); + } address_v6_range hosts() const noexcept { if (is_host()) return { address(), *++address_v6_iterator(address()) }; - return {}; // { network(), XXX broadcast() XXX }; // TODO + + address_v6::bytes_type __bytes = _M_addr.to_bytes(); + int __nbytes = (_M_prefix_len + 7) / 8; + for (int __n = __nbytes; __n < 16; ++__n) + __bytes[__n] = 0xFF; + if (int __bits = (__nbytes * 8) - _M_prefix_len) + __bytes[__nbytes - 1] |= (1 << __bits) - 1; + address_v6 __last(__bytes, _M_addr.scope_id()); + return { network(), *++address_v6_iterator(__last) }; } - constexpr network_v6 + _GLIBCXX17_CONSTEXPR network_v6 canonical() const noexcept { return network_v6{network(), prefix_length()}; } @@ -1387,7 +1405,7 @@ namespace ip to_string(const _Allocator& __a = _Allocator()) const { return address().to_string(__a) + '/' - + std::to_string(prefix_length()); + + std::to_string(prefix_length()).c_str(); } private: diff --git a/libstdc++-v3/testsuite/experimental/net/internet/network/v6/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/network/v6/cons.cc new file mode 100644 index 000000000000..9b4b791bd023 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/network/v6/cons.cc @@ -0,0 +1,74 @@ +// { dg-do run { target c++14 } } +// { dg-require-effective-target net_ts_ip } +// { dg-add-options net_ts } + +#include +#include +#include + +using std::experimental::net::ip::network_v6; +using std::experimental::net::ip::address_v6; + +constexpr void +test01() +{ + network_v6 n0; + VERIFY( n0.address().is_unspecified() ); + VERIFY( n0.prefix_length() == 0 ); +} + +constexpr void +test02() +{ + address_v6 a0; + network_v6 n0{ a0, 0 }; + VERIFY( n0.address() == a0 ); + VERIFY( n0.prefix_length() == 0 ); + + address_v6 a1{ address_v6::bytes_type{ 1, 2, 4, 8, 16, 32, 64, 128, + 3, 7, 47, 71, 83, 165, 199, 255 } }; + network_v6 n1{ a1, 99}; + VERIFY( n1.address() == a1 ); + VERIFY( n1.prefix_length() == 99 ); +} + +void +test02_errors() +{ + address_v6 a0; + try + { + network_v6{a0, -1}; + VERIFY(false); + } + catch(const std::out_of_range&) + { + } + + try + { + network_v6{a0, 129}; + VERIFY(false); + } + catch(const std::out_of_range&) + { + } +} + +constexpr bool +test_constexpr() +{ + test01(); + test02(); + return true; +} + +int +main() +{ + test01(); + test02(); + test02_errors(); + + static_assert( test_constexpr(), "valid in constant expressions" ); +}