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/parameter/incoming_ssn_reset_request_parameter.h" 11 12 #include <stddef.h> 13 14 #include <cstdint> 15 #include <string> 16 #include <utility> 17 #include <vector> 18 19 #include "absl/types/optional.h" 20 #include "api/array_view.h" 21 #include "net/dcsctp/packet/bounded_byte_reader.h" 22 #include "net/dcsctp/packet/bounded_byte_writer.h" 23 #include "net/dcsctp/packet/tlv_trait.h" 24 #include "rtc_base/strings/string_builder.h" 25 26 namespace dcsctp { 27 28 // https://tools.ietf.org/html/rfc6525#section-4.2 29 30 // 0 1 2 3 31 // 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 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 33 // | Parameter Type = 14 | Parameter Length = 8 + 2 * N | 34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 35 // | Re-configuration Request Sequence Number | 36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 37 // | Stream Number 1 (optional) | Stream Number 2 (optional) | 38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 39 // / ...... / 40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 41 // | Stream Number N-1 (optional) | Stream Number N (optional) | 42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 43 constexpr int IncomingSSNResetRequestParameter::kType; 44 45 absl::optional<IncomingSSNResetRequestParameter> Parse(rtc::ArrayView<const uint8_t> data)46IncomingSSNResetRequestParameter::Parse(rtc::ArrayView<const uint8_t> data) { 47 absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data); 48 if (!reader.has_value()) { 49 return absl::nullopt; 50 } 51 52 ReconfigRequestSN request_sequence_number(reader->Load32<4>()); 53 54 size_t stream_count = reader->variable_data_size() / kStreamIdSize; 55 std::vector<StreamID> stream_ids; 56 stream_ids.reserve(stream_count); 57 for (size_t i = 0; i < stream_count; ++i) { 58 BoundedByteReader<kStreamIdSize> sub_reader = 59 reader->sub_reader<kStreamIdSize>(i * kStreamIdSize); 60 61 stream_ids.push_back(StreamID(sub_reader.Load16<0>())); 62 } 63 64 return IncomingSSNResetRequestParameter(request_sequence_number, 65 std::move(stream_ids)); 66 } 67 SerializeTo(std::vector<uint8_t> & out) const68void IncomingSSNResetRequestParameter::SerializeTo( 69 std::vector<uint8_t>& out) const { 70 size_t variable_size = stream_ids_.size() * kStreamIdSize; 71 BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, variable_size); 72 73 writer.Store32<4>(*request_sequence_number_); 74 75 for (size_t i = 0; i < stream_ids_.size(); ++i) { 76 BoundedByteWriter<kStreamIdSize> sub_writer = 77 writer.sub_writer<kStreamIdSize>(i * kStreamIdSize); 78 sub_writer.Store16<0>(*stream_ids_[i]); 79 } 80 } 81 ToString() const82std::string IncomingSSNResetRequestParameter::ToString() const { 83 rtc::StringBuilder sb; 84 sb << "Incoming SSN Reset Request, req_seq_nbr=" 85 << *request_sequence_number(); 86 return sb.Release(); 87 } 88 89 } // namespace dcsctp 90