xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/sack_chunk.h (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 #ifndef NET_DCSCTP_PACKET_CHUNK_SACK_CHUNK_H_
11 #define NET_DCSCTP_PACKET_CHUNK_SACK_CHUNK_H_
12 #include <stddef.h>
13 
14 #include <cstdint>
15 #include <set>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/strings/string_view.h"
21 #include "api/array_view.h"
22 #include "net/dcsctp/packet/chunk/chunk.h"
23 #include "net/dcsctp/packet/tlv_trait.h"
24 
25 namespace dcsctp {
26 
27 // https://tools.ietf.org/html/rfc4960#section-3.3.4
28 struct SackChunkConfig : ChunkConfig {
29   static constexpr int kType = 3;
30   static constexpr size_t kHeaderSize = 16;
31   static constexpr size_t kVariableLengthAlignment = 4;
32 };
33 
34 class SackChunk : public Chunk, public TLVTrait<SackChunkConfig> {
35  public:
36   static constexpr int kType = SackChunkConfig::kType;
37 
38   struct GapAckBlock {
GapAckBlockGapAckBlock39     GapAckBlock(uint16_t start, uint16_t end) : start(start), end(end) {}
40 
41     uint16_t start;
42     uint16_t end;
43 
44     bool operator==(const GapAckBlock& other) const {
45       return start == other.start && end == other.end;
46     }
47   };
48 
SackChunk(TSN cumulative_tsn_ack,uint32_t a_rwnd,std::vector<GapAckBlock> gap_ack_blocks,std::set<TSN> duplicate_tsns)49   SackChunk(TSN cumulative_tsn_ack,
50             uint32_t a_rwnd,
51             std::vector<GapAckBlock> gap_ack_blocks,
52             std::set<TSN> duplicate_tsns)
53       : cumulative_tsn_ack_(cumulative_tsn_ack),
54         a_rwnd_(a_rwnd),
55         gap_ack_blocks_(std::move(gap_ack_blocks)),
56         duplicate_tsns_(std::move(duplicate_tsns)) {}
57   static absl::optional<SackChunk> Parse(rtc::ArrayView<const uint8_t> data);
58 
59   void SerializeTo(std::vector<uint8_t>& out) const override;
60   std::string ToString() const override;
61 
cumulative_tsn_ack()62   TSN cumulative_tsn_ack() const { return cumulative_tsn_ack_; }
a_rwnd()63   uint32_t a_rwnd() const { return a_rwnd_; }
gap_ack_blocks()64   rtc::ArrayView<const GapAckBlock> gap_ack_blocks() const {
65     return gap_ack_blocks_;
66   }
duplicate_tsns()67   const std::set<TSN>& duplicate_tsns() const { return duplicate_tsns_; }
68 
69  private:
70   static constexpr size_t kGapAckBlockSize = 4;
71   static constexpr size_t kDupTsnBlockSize = 4;
72 
73   const TSN cumulative_tsn_ack_;
74   const uint32_t a_rwnd_;
75   std::vector<GapAckBlock> gap_ack_blocks_;
76   std::set<TSN> duplicate_tsns_;
77 };
78 }  // namespace dcsctp
79 
80 #endif  // NET_DCSCTP_PACKET_CHUNK_SACK_CHUNK_H_
81