xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/parameter/supported_extensions_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/supported_extensions_parameter.h"
11 
12 #include <cstdint>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "api/array_view.h"
19 #include "net/dcsctp/common/str_join.h"
20 #include "net/dcsctp/packet/bounded_byte_reader.h"
21 #include "net/dcsctp/packet/bounded_byte_writer.h"
22 #include "net/dcsctp/packet/tlv_trait.h"
23 #include "rtc_base/strings/string_builder.h"
24 
25 namespace dcsctp {
26 
27 // https://tools.ietf.org/html/rfc5061#section-4.2.7
28 
29 //   0                   1                   2                   3
30 //   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
31 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 //  |     Parameter Type = 0x8008   |      Parameter Length         |
33 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 //  | CHUNK TYPE 1  |  CHUNK TYPE 2 |  CHUNK TYPE 3 |  CHUNK TYPE 4 |
35 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 //  |                             ....                              |
37 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 //  | CHUNK TYPE N  |      PAD      |      PAD      |      PAD      |
39 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 constexpr int SupportedExtensionsParameter::kType;
41 
42 absl::optional<SupportedExtensionsParameter>
Parse(rtc::ArrayView<const uint8_t> data)43 SupportedExtensionsParameter::Parse(rtc::ArrayView<const uint8_t> data) {
44   absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data);
45   if (!reader.has_value()) {
46     return absl::nullopt;
47   }
48 
49   std::vector<uint8_t> chunk_types(reader->variable_data().begin(),
50                                    reader->variable_data().end());
51   return SupportedExtensionsParameter(std::move(chunk_types));
52 }
53 
SerializeTo(std::vector<uint8_t> & out) const54 void SupportedExtensionsParameter::SerializeTo(
55     std::vector<uint8_t>& out) const {
56   BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, chunk_types_.size());
57   writer.CopyToVariableData(chunk_types_);
58 }
59 
ToString() const60 std::string SupportedExtensionsParameter::ToString() const {
61   rtc::StringBuilder sb;
62   sb << "Supported Extensions (" << StrJoin(chunk_types_, ", ") << ")";
63   return sb.Release();
64 }
65 }  // namespace dcsctp
66