diff --git a/net/dcsctp/packet/BUILD.gn b/net/dcsctp/packet/BUILD.gn index 08bdb0f5a5..7abccc004b 100644 --- a/net/dcsctp/packet/BUILD.gn +++ b/net/dcsctp/packet/BUILD.gn @@ -98,6 +98,8 @@ rtc_library("parameter") { "parameter/state_cookie_parameter.h", "parameter/supported_extensions_parameter.cc", "parameter/supported_extensions_parameter.h", + "parameter/zero_checksum_acceptable_chunk_parameter.cc", + "parameter/zero_checksum_acceptable_chunk_parameter.h", ] absl_deps = [ "//third_party/abseil-cpp/absl/algorithm:container", @@ -323,6 +325,7 @@ if (rtc_include_tests) { "parameter/ssn_tsn_reset_request_parameter_test.cc", "parameter/state_cookie_parameter_test.cc", "parameter/supported_extensions_parameter_test.cc", + "parameter/zero_checksum_acceptable_chunk_parameter_test.cc", "sctp_packet_test.cc", "tlv_trait_test.cc", ] diff --git a/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.cc b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.cc new file mode 100644 index 0000000000..75f7d3c487 --- /dev/null +++ b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.cc @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#include "net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.h" + +#include + +#include + +#include "absl/types/optional.h" +#include "api/array_view.h" + +namespace dcsctp { + +// https://www.ietf.org/archive/id/draft-tuexen-tsvwg-sctp-zero-checksum-00.html#section-3 + +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | Type = 0x8001 | Length = 4 | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +constexpr int ZeroChecksumAcceptableChunkParameter::kType; + +absl::optional +ZeroChecksumAcceptableChunkParameter::Parse( + rtc::ArrayView data) { + if (!ParseTLV(data).has_value()) { + return absl::nullopt; + } + return ZeroChecksumAcceptableChunkParameter(); +} + +void ZeroChecksumAcceptableChunkParameter::SerializeTo( + std::vector& out) const { + AllocateTLV(out); +} + +std::string ZeroChecksumAcceptableChunkParameter::ToString() const { + return "Zero Checksum Acceptable"; +} +} // namespace dcsctp diff --git a/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.h b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.h new file mode 100644 index 0000000000..9ae2ec8280 --- /dev/null +++ b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef NET_DCSCTP_PACKET_PARAMETER_ZERO_CHECKSUM_ACCEPTABLE_CHUNK_PARAMETER_H_ +#define NET_DCSCTP_PACKET_PARAMETER_ZERO_CHECKSUM_ACCEPTABLE_CHUNK_PARAMETER_H_ +#include +#include + +#include +#include + +#include "absl/strings/string_view.h" +#include "api/array_view.h" +#include "net/dcsctp/packet/parameter/parameter.h" +#include "net/dcsctp/packet/tlv_trait.h" + +namespace dcsctp { + +// https://datatracker.ietf.org/doc/draft-tuexen-tsvwg-sctp-zero-checksum/ +struct ZeroChecksumAcceptableChunkParameterConfig : ParameterConfig { + static constexpr int kType = 0x8001; + static constexpr size_t kHeaderSize = 4; + static constexpr size_t kVariableLengthAlignment = 0; +}; + +class ZeroChecksumAcceptableChunkParameter + : public Parameter, + public TLVTrait { + public: + static constexpr int kType = + ZeroChecksumAcceptableChunkParameterConfig::kType; + + ZeroChecksumAcceptableChunkParameter() {} + + static absl::optional Parse( + rtc::ArrayView data); + + void SerializeTo(std::vector& out) const override; + std::string ToString() const override; +}; + +} // namespace dcsctp + +#endif // NET_DCSCTP_PACKET_PARAMETER_ZERO_CHECKSUM_ACCEPTABLE_CHUNK_PARAMETER_H_ diff --git a/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter_test.cc b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter_test.cc new file mode 100644 index 0000000000..8a004e1788 --- /dev/null +++ b/net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter_test.cc @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#include "net/dcsctp/packet/parameter/zero_checksum_acceptable_chunk_parameter.h" + +#include + +#include +#include + +#include "net/dcsctp/testing/testing_macros.h" +#include "rtc_base/gunit.h" +#include "test/gmock.h" +#include "test/gtest.h" + +namespace dcsctp { +namespace { +using ::testing::ElementsAre; + +TEST(ZeroChecksumAcceptableChunkParameterTest, SerializeAndDeserialize) { + ZeroChecksumAcceptableChunkParameter parameter; + + std::vector serialized; + parameter.SerializeTo(serialized); + + EXPECT_THAT(serialized, ElementsAre(0x80, 0x01, 0x00, 0x04)); + + ASSERT_HAS_VALUE_AND_ASSIGN( + ZeroChecksumAcceptableChunkParameter deserialized, + ZeroChecksumAcceptableChunkParameter::Parse(serialized)); +} + +TEST(ZeroChecksumAcceptableChunkParameterTest, FailToDeserialize) { + std::vector invalid = {0x00, 0x00, 0x00, 0x00}; + + EXPECT_FALSE( + ZeroChecksumAcceptableChunkParameter::Parse(invalid).has_value()); +} + +TEST(ZeroChecksumAcceptableChunkParameterTest, HasToString) { + ZeroChecksumAcceptableChunkParameter parameter; + + EXPECT_EQ(parameter.ToString(), "Zero Checksum Acceptable"); +} + +} // namespace +} // namespace dcsctp