xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/idata_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/idata_chunk.h"
11 
12 #include <stdint.h>
13 
14 #include <string>
15 #include <type_traits>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "api/array_view.h"
20 #include "net/dcsctp/packet/bounded_byte_reader.h"
21 #include "net/dcsctp/packet/bounded_byte_writer.h"
22 #include "net/dcsctp/packet/chunk/data_common.h"
23 #include "rtc_base/strings/string_builder.h"
24 
25 namespace dcsctp {
26 
27 // https://tools.ietf.org/html/rfc8260#section-2.1
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 //  |   Type = 64   |  Res  |I|U|B|E|       Length = Variable       |
33 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 //  |                              TSN                              |
35 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 //  |        Stream Identifier      |           Reserved            |
37 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 //  |                      Message Identifier                       |
39 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 //  |    Payload Protocol Identifier / Fragment Sequence Number     |
41 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 //  \                                                               \
43 //  /                           User Data                           /
44 //  \                                                               \
45 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 constexpr int IDataChunk::kType;
47 
Parse(rtc::ArrayView<const uint8_t> data)48 absl::optional<IDataChunk> IDataChunk::Parse(
49     rtc::ArrayView<const uint8_t> data) {
50   absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data);
51   if (!reader.has_value()) {
52     return absl::nullopt;
53   }
54   uint8_t flags = reader->Load8<1>();
55   TSN tsn(reader->Load32<4>());
56   StreamID stream_identifier(reader->Load16<8>());
57   MID message_id(reader->Load32<12>());
58   uint32_t ppid_or_fsn = reader->Load32<16>();
59 
60   Options options;
61   options.is_end = Data::IsEnd((flags & (1 << kFlagsBitEnd)) != 0);
62   options.is_beginning =
63       Data::IsBeginning((flags & (1 << kFlagsBitBeginning)) != 0);
64   options.is_unordered = IsUnordered((flags & (1 << kFlagsBitUnordered)) != 0);
65   options.immediate_ack =
66       ImmediateAckFlag((flags & (1 << kFlagsBitImmediateAck)) != 0);
67 
68   return IDataChunk(tsn, stream_identifier, message_id,
69                     PPID(options.is_beginning ? ppid_or_fsn : 0),
70                     FSN(options.is_beginning ? 0 : ppid_or_fsn),
71                     std::vector<uint8_t>(reader->variable_data().begin(),
72                                          reader->variable_data().end()),
73                     options);
74 }
75 
SerializeTo(std::vector<uint8_t> & out) const76 void IDataChunk::SerializeTo(std::vector<uint8_t>& out) const {
77   BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, payload().size());
78 
79   writer.Store8<1>(
80       (*options().is_end ? (1 << kFlagsBitEnd) : 0) |
81       (*options().is_beginning ? (1 << kFlagsBitBeginning) : 0) |
82       (*options().is_unordered ? (1 << kFlagsBitUnordered) : 0) |
83       (*options().immediate_ack ? (1 << kFlagsBitImmediateAck) : 0));
84   writer.Store32<4>(*tsn());
85   writer.Store16<8>(*stream_id());
86   writer.Store32<12>(*message_id());
87   writer.Store32<16>(options().is_beginning ? *ppid() : *fsn());
88   writer.CopyToVariableData(payload());
89 }
90 
ToString() const91 std::string IDataChunk::ToString() const {
92   rtc::StringBuilder sb;
93   sb << "I-DATA, type=" << (options().is_unordered ? "unordered" : "ordered")
94      << "::"
95      << (*options().is_beginning && *options().is_end
96              ? "complete"
97              : *options().is_beginning ? "first"
98                                        : *options().is_end ? "last" : "middle")
99      << ", tsn=" << *tsn() << ", stream_id=" << *stream_id()
100      << ", message_id=" << *message_id();
101 
102   if (*options().is_beginning) {
103     sb << ", ppid=" << *ppid();
104   } else {
105     sb << ", fsn=" << *fsn();
106   }
107   sb << ", length=" << payload().size();
108   return sb.Release();
109 }
110 
111 }  // namespace dcsctp
112