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/error_cause/missing_mandatory_parameter_cause.h" 11 12 #include <stddef.h> 13 14 #include <cstdint> 15 #include <string> 16 #include <type_traits> 17 #include <vector> 18 19 #include "absl/types/optional.h" 20 #include "api/array_view.h" 21 #include "net/dcsctp/common/str_join.h" 22 #include "net/dcsctp/packet/bounded_byte_reader.h" 23 #include "net/dcsctp/packet/bounded_byte_writer.h" 24 #include "net/dcsctp/packet/tlv_trait.h" 25 #include "rtc_base/logging.h" 26 #include "rtc_base/strings/string_builder.h" 27 28 namespace dcsctp { 29 30 // https://tools.ietf.org/html/rfc4960#section-3.3.10.2 31 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 33 // | Cause Code=2 | Cause Length=8+N*2 | 34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 35 // | Number of missing params=N | 36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 37 // | Missing Param Type #1 | Missing Param Type #2 | 38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 39 // | Missing Param Type #N-1 | Missing Param Type #N | 40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 41 constexpr int MissingMandatoryParameterCause::kType; 42 43 absl::optional<MissingMandatoryParameterCause> Parse(rtc::ArrayView<const uint8_t> data)44MissingMandatoryParameterCause::Parse(rtc::ArrayView<const uint8_t> data) { 45 absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data); 46 if (!reader.has_value()) { 47 return absl::nullopt; 48 } 49 50 uint32_t count = reader->Load32<4>(); 51 if (reader->variable_data_size() / kMissingParameterSize != count) { 52 RTC_DLOG(LS_WARNING) << "Invalid number of missing parameters"; 53 return absl::nullopt; 54 } 55 56 std::vector<uint16_t> missing_parameter_types; 57 missing_parameter_types.reserve(count); 58 for (uint32_t i = 0; i < count; ++i) { 59 BoundedByteReader<kMissingParameterSize> sub_reader = 60 reader->sub_reader<kMissingParameterSize>(i * kMissingParameterSize); 61 62 missing_parameter_types.push_back(sub_reader.Load16<0>()); 63 } 64 return MissingMandatoryParameterCause(missing_parameter_types); 65 } 66 SerializeTo(std::vector<uint8_t> & out) const67void MissingMandatoryParameterCause::SerializeTo( 68 std::vector<uint8_t>& out) const { 69 size_t variable_size = 70 missing_parameter_types_.size() * kMissingParameterSize; 71 BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, variable_size); 72 73 writer.Store32<4>(missing_parameter_types_.size()); 74 75 for (size_t i = 0; i < missing_parameter_types_.size(); ++i) { 76 BoundedByteWriter<kMissingParameterSize> sub_writer = 77 writer.sub_writer<kMissingParameterSize>(i * kMissingParameterSize); 78 79 sub_writer.Store16<0>(missing_parameter_types_[i]); 80 } 81 } 82 ToString() const83std::string MissingMandatoryParameterCause::ToString() const { 84 rtc::StringBuilder sb; 85 sb << "Missing Mandatory Parameter, missing_parameter_types=" 86 << StrJoin(missing_parameter_types_, ","); 87 return sb.Release(); 88 } 89 90 } // namespace dcsctp 91