xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/parameter/parameter.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/parameter.h"
11 
12 #include <stddef.h>
13 
14 #include <cstdint>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/memory/memory.h"
21 #include "absl/types/optional.h"
22 #include "api/array_view.h"
23 #include "net/dcsctp/common/math.h"
24 #include "net/dcsctp/packet/bounded_byte_reader.h"
25 #include "net/dcsctp/packet/parameter/add_incoming_streams_request_parameter.h"
26 #include "net/dcsctp/packet/parameter/add_outgoing_streams_request_parameter.h"
27 #include "net/dcsctp/packet/parameter/forward_tsn_supported_parameter.h"
28 #include "net/dcsctp/packet/parameter/heartbeat_info_parameter.h"
29 #include "net/dcsctp/packet/parameter/incoming_ssn_reset_request_parameter.h"
30 #include "net/dcsctp/packet/parameter/outgoing_ssn_reset_request_parameter.h"
31 #include "net/dcsctp/packet/parameter/reconfiguration_response_parameter.h"
32 #include "net/dcsctp/packet/parameter/ssn_tsn_reset_request_parameter.h"
33 #include "net/dcsctp/packet/parameter/state_cookie_parameter.h"
34 #include "net/dcsctp/packet/parameter/supported_extensions_parameter.h"
35 #include "net/dcsctp/packet/tlv_trait.h"
36 #include "rtc_base/logging.h"
37 #include "rtc_base/strings/string_builder.h"
38 
39 namespace dcsctp {
40 
41 constexpr size_t kParameterHeaderSize = 4;
42 
Add(const Parameter & p)43 Parameters::Builder& Parameters::Builder::Add(const Parameter& p) {
44   // https://tools.ietf.org/html/rfc4960#section-3.2.1
45   // "If the length of the parameter is not a multiple of 4 bytes, the sender
46   // pads the parameter at the end (i.e., after the Parameter Value field) with
47   // all zero bytes."
48   if (data_.size() % 4 != 0) {
49     data_.resize(RoundUpTo4(data_.size()));
50   }
51 
52   p.SerializeTo(data_);
53   return *this;
54 }
55 
descriptors() const56 std::vector<ParameterDescriptor> Parameters::descriptors() const {
57   rtc::ArrayView<const uint8_t> span(data_);
58   std::vector<ParameterDescriptor> result;
59   while (!span.empty()) {
60     BoundedByteReader<kParameterHeaderSize> header(span);
61     uint16_t type = header.Load16<0>();
62     uint16_t length = header.Load16<2>();
63     result.emplace_back(type, span.subview(0, length));
64     size_t length_with_padding = RoundUpTo4(length);
65     if (length_with_padding > span.size()) {
66       break;
67     }
68     span = span.subview(length_with_padding);
69   }
70   return result;
71 }
72 
Parse(rtc::ArrayView<const uint8_t> data)73 absl::optional<Parameters> Parameters::Parse(
74     rtc::ArrayView<const uint8_t> data) {
75   // Validate the parameter descriptors
76   rtc::ArrayView<const uint8_t> span(data);
77   while (!span.empty()) {
78     if (span.size() < kParameterHeaderSize) {
79       RTC_DLOG(LS_WARNING) << "Insufficient parameter length";
80       return absl::nullopt;
81     }
82     BoundedByteReader<kParameterHeaderSize> header(span);
83     uint16_t length = header.Load16<2>();
84     if (length < kParameterHeaderSize || length > span.size()) {
85       RTC_DLOG(LS_WARNING) << "Invalid parameter length field";
86       return absl::nullopt;
87     }
88     size_t length_with_padding = RoundUpTo4(length);
89     if (length_with_padding > span.size()) {
90       break;
91     }
92     span = span.subview(length_with_padding);
93   }
94   return Parameters(std::vector<uint8_t>(data.begin(), data.end()));
95 }
96 }  // namespace dcsctp
97