xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/reconfig_chunk.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/chunk/reconfig_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/parameter/parameter.h"
22 #include "net/dcsctp/packet/tlv_trait.h"
23 
24 namespace dcsctp {
25 
26 // https://tools.ietf.org/html/rfc6525#section-3.1
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 = 130    |  Chunk Flags  |      Chunk Length             |
32 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 //  \                                                               \
34 //  /                  Re-configuration Parameter                   /
35 //  \                                                               \
36 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 //  \                                                               \
38 //  /             Re-configuration Parameter (optional)             /
39 //  \                                                               \
40 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 constexpr int ReConfigChunk::kType;
42 
Parse(rtc::ArrayView<const uint8_t> data)43 absl::optional<ReConfigChunk> ReConfigChunk::Parse(
44     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   absl::optional<Parameters> parameters =
51       Parameters::Parse(reader->variable_data());
52   if (!parameters.has_value()) {
53     return absl::nullopt;
54   }
55 
56   return ReConfigChunk(*std::move(parameters));
57 }
58 
SerializeTo(std::vector<uint8_t> & out) const59 void ReConfigChunk::SerializeTo(std::vector<uint8_t>& out) const {
60   rtc::ArrayView<const uint8_t> parameters = parameters_.data();
61   BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, parameters.size());
62   writer.CopyToVariableData(parameters);
63 }
64 
ToString() const65 std::string ReConfigChunk::ToString() const {
66   return "RE-CONFIG";
67 }
68 
69 }  // namespace dcsctp
70