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/sack_chunk.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.4
31
32 // 0 1 2 3
33 // 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
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 // | Type = 3 |Chunk Flags | Chunk Length |
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 // | Cumulative TSN Ack |
38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 // | Advertised Receiver Window Credit (a_rwnd) |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 // | Number of Gap Ack Blocks = N | Number of Duplicate TSNs = X |
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 // | Gap Ack Block #1 Start | Gap Ack Block #1 End |
44 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 // / /
46 // \ ... \
47 // / /
48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 // | Gap Ack Block #N Start | Gap Ack Block #N End |
50 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 // | Duplicate TSN 1 |
52 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 // / /
54 // \ ... \
55 // / /
56 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 // | Duplicate TSN X |
58 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 constexpr int SackChunk::kType;
60
Parse(rtc::ArrayView<const uint8_t> data)61 absl::optional<SackChunk> SackChunk::Parse(rtc::ArrayView<const uint8_t> data) {
62 absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data);
63 if (!reader.has_value()) {
64 return absl::nullopt;
65 }
66
67 TSN tsn_ack(reader->Load32<4>());
68 uint32_t a_rwnd = reader->Load32<8>();
69 uint16_t nbr_of_gap_blocks = reader->Load16<12>();
70 uint16_t nbr_of_dup_tsns = reader->Load16<14>();
71
72 if (reader->variable_data_size() != nbr_of_gap_blocks * kGapAckBlockSize +
73 nbr_of_dup_tsns * kDupTsnBlockSize) {
74 RTC_DLOG(LS_WARNING) << "Invalid number of gap blocks or duplicate TSNs";
75 return absl::nullopt;
76 }
77
78 std::vector<GapAckBlock> gap_ack_blocks;
79 gap_ack_blocks.reserve(nbr_of_gap_blocks);
80 size_t offset = 0;
81 for (int i = 0; i < nbr_of_gap_blocks; ++i) {
82 BoundedByteReader<kGapAckBlockSize> sub_reader =
83 reader->sub_reader<kGapAckBlockSize>(offset);
84
85 uint16_t start = sub_reader.Load16<0>();
86 uint16_t end = sub_reader.Load16<2>();
87 gap_ack_blocks.emplace_back(start, end);
88 offset += kGapAckBlockSize;
89 }
90
91 std::set<TSN> duplicate_tsns;
92 for (int i = 0; i < nbr_of_dup_tsns; ++i) {
93 BoundedByteReader<kDupTsnBlockSize> sub_reader =
94 reader->sub_reader<kDupTsnBlockSize>(offset);
95
96 duplicate_tsns.insert(TSN(sub_reader.Load32<0>()));
97 offset += kDupTsnBlockSize;
98 }
99 RTC_DCHECK(offset == reader->variable_data_size());
100
101 return SackChunk(tsn_ack, a_rwnd, gap_ack_blocks, duplicate_tsns);
102 }
103
SerializeTo(std::vector<uint8_t> & out) const104 void SackChunk::SerializeTo(std::vector<uint8_t>& out) const {
105 int nbr_of_gap_blocks = gap_ack_blocks_.size();
106 int nbr_of_dup_tsns = duplicate_tsns_.size();
107 size_t variable_size =
108 nbr_of_gap_blocks * kGapAckBlockSize + nbr_of_dup_tsns * kDupTsnBlockSize;
109 BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, variable_size);
110
111 writer.Store32<4>(*cumulative_tsn_ack_);
112 writer.Store32<8>(a_rwnd_);
113 writer.Store16<12>(nbr_of_gap_blocks);
114 writer.Store16<14>(nbr_of_dup_tsns);
115
116 size_t offset = 0;
117 for (int i = 0; i < nbr_of_gap_blocks; ++i) {
118 BoundedByteWriter<kGapAckBlockSize> sub_writer =
119 writer.sub_writer<kGapAckBlockSize>(offset);
120
121 sub_writer.Store16<0>(gap_ack_blocks_[i].start);
122 sub_writer.Store16<2>(gap_ack_blocks_[i].end);
123 offset += kGapAckBlockSize;
124 }
125
126 for (TSN tsn : duplicate_tsns_) {
127 BoundedByteWriter<kDupTsnBlockSize> sub_writer =
128 writer.sub_writer<kDupTsnBlockSize>(offset);
129
130 sub_writer.Store32<0>(*tsn);
131 offset += kDupTsnBlockSize;
132 }
133
134 RTC_DCHECK(offset == variable_size);
135 }
136
ToString() const137 std::string SackChunk::ToString() const {
138 rtc::StringBuilder sb;
139 sb << "SACK, cum_ack_tsn=" << *cumulative_tsn_ack()
140 << ", a_rwnd=" << a_rwnd();
141 for (const GapAckBlock& gap : gap_ack_blocks_) {
142 uint32_t first = *cumulative_tsn_ack_ + gap.start;
143 uint32_t last = *cumulative_tsn_ack_ + gap.end;
144 sb << ", gap=" << first << "--" << last;
145 }
146 if (!duplicate_tsns_.empty()) {
147 sb << ", dup_tsns="
148 << StrJoin(duplicate_tsns(), ",",
149 [](rtc::StringBuilder& sb, TSN tsn) { sb << *tsn; });
150 }
151
152 return sb.Release();
153 }
154
155 } // namespace dcsctp
156