1 /* 2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 #include "net/dcsctp/packet/chunk/abort_chunk.h" 11 12 #include <stdint.h> 13 14 #include <utility> 15 #include <vector> 16 17 #include "absl/types/optional.h" 18 #include "api/array_view.h" 19 #include "net/dcsctp/packet/bounded_byte_reader.h" 20 #include "net/dcsctp/packet/bounded_byte_writer.h" 21 #include "net/dcsctp/packet/error_cause/error_cause.h" 22 #include "net/dcsctp/packet/tlv_trait.h" 23 24 namespace dcsctp { 25 26 // https://tools.ietf.org/html/rfc4960#section-3.3.7 27 28 // 0 1 2 3 29 // 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 30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 31 // | Type = 6 |Reserved |T| Length | 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 33 // \ \ 34 // / zero or more Error Causes / 35 // \ \ 36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 37 constexpr int AbortChunk::kType; 38 Parse(rtc::ArrayView<const uint8_t> data)39absl::optional<AbortChunk> AbortChunk::Parse( 40 rtc::ArrayView<const uint8_t> data) { 41 absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data); 42 if (!reader.has_value()) { 43 return absl::nullopt; 44 } 45 absl::optional<Parameters> error_causes = 46 Parameters::Parse(reader->variable_data()); 47 if (!error_causes.has_value()) { 48 return absl::nullopt; 49 } 50 uint8_t flags = reader->Load8<1>(); 51 bool filled_in_verification_tag = (flags & (1 << kFlagsBitT)) == 0; 52 return AbortChunk(filled_in_verification_tag, *std::move(error_causes)); 53 } 54 SerializeTo(std::vector<uint8_t> & out) const55void AbortChunk::SerializeTo(std::vector<uint8_t>& out) const { 56 rtc::ArrayView<const uint8_t> error_causes = error_causes_.data(); 57 BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, error_causes.size()); 58 writer.Store8<1>(filled_in_verification_tag_ ? 0 : (1 << kFlagsBitT)); 59 writer.CopyToVariableData(error_causes); 60 } 61 ToString() const62std::string AbortChunk::ToString() const { 63 return "ABORT"; 64 } 65 } // namespace dcsctp 66