Debian Codegraph @

chunk_selective_ack.go

raw
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package sctp
5
6 import (
7 "encoding/binary"
8 "errors"
9 "fmt"
10 )
11
12 /*
13 chunkSelectiveAck represents an SCTP Chunk of type SACK
14
15 This chunk is sent to the peer endpoint to acknowledge received DATA
16 chunks and to inform the peer endpoint of gaps in the received
17 subsequences of DATA chunks as represented by their TSNs.
18 0 1 2 3
19 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
20 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 | Type = 3 |Chunk Flags | Chunk Length |
22 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 | Cumulative TSN Ack |
24 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25 | Advertised Receiver Window Credit (a_rwnd) |
26 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27 | Number of Gap Ack Blocks = N | Number of Duplicate TSNs = X |
28 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29 | Gap Ack Block #1 Start | Gap Ack Block #1 End |
30 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 / /
32 \ ... \
33 / /
34 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 | Gap Ack Block #N Start | Gap Ack Block #N End |
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Duplicate TSN 1 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 / /
40 \ ... \
41 / /
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | Duplicate TSN X |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 */
46
47 type gapAckBlock struct {
48 start uint16
49 end uint16
50 }
51
52 // Selective ack chunk errors.
53 var (
54 ErrChunkTypeNotSack = errors.New("ChunkType is not of type SACK")
55 ErrSackSizeNotLargeEnoughInfo = errors.New("SACK Chunk size is not large enough to contain header")
56 ErrSackSizeNotMatchPredicted = errors.New("SACK Chunk size does not match predicted amount from header values")
57 )
58
59 // String makes gapAckBlock printable.
60 func (g gapAckBlock) String() string {
61 return fmt.Sprintf("%d - %d", g.start, g.end)
62 }
63
64 type chunkSelectiveAck struct {
65 chunkHeader
66 cumulativeTSNAck uint32
67 advertisedReceiverWindowCredit uint32
68 gapAckBlocks []gapAckBlock
69 duplicateTSN []uint32
70 }
71
72 const (
73 selectiveAckHeaderSize = 12
74 )
75
76 func (s *chunkSelectiveAck) unmarshal(raw []byte) error {
77 if err := s.chunkHeader.unmarshal(raw); err != nil {
78 return err
79 }
80
81 if s.typ != ctSack {
82 return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotSack, s.typ.String())
83 }
84
85 if len(s.raw) < selectiveAckHeaderSize {
86 return fmt.Errorf("%w: %v remaining, needs %v bytes", ErrSackSizeNotLargeEnoughInfo,
87 len(s.raw), selectiveAckHeaderSize)
88 }
89
90 s.cumulativeTSNAck = binary.BigEndian.Uint32(s.raw[0:])
91 s.advertisedReceiverWindowCredit = binary.BigEndian.Uint32(s.raw[4:])
92 s.gapAckBlocks = make([]gapAckBlock, binary.BigEndian.Uint16(s.raw[8:]))
93 s.duplicateTSN = make([]uint32, binary.BigEndian.Uint16(s.raw[10:]))
94
95 if len(s.raw) != selectiveAckHeaderSize+(4*len(s.gapAckBlocks)+(4*len(s.duplicateTSN))) {
96 return ErrSackSizeNotMatchPredicted
97 }
98
99 offset := selectiveAckHeaderSize
100 for i := range s.gapAckBlocks {
101 s.gapAckBlocks[i].start = binary.BigEndian.Uint16(s.raw[offset:])
102 s.gapAckBlocks[i].end = binary.BigEndian.Uint16(s.raw[offset+2:])
103 offset += 4
104 }
105 for i := range s.duplicateTSN {
106 s.duplicateTSN[i] = binary.BigEndian.Uint32(s.raw[offset:])
107 offset += 4
108 }
109
110 return nil
111 }
112
113 func (s *chunkSelectiveAck) marshal() ([]byte, error) {
114 sackRaw := make([]byte, selectiveAckHeaderSize+(4*len(s.gapAckBlocks)+(4*len(s.duplicateTSN))))
115 binary.BigEndian.PutUint32(sackRaw[0:], s.cumulativeTSNAck)
116 binary.BigEndian.PutUint32(sackRaw[4:], s.advertisedReceiverWindowCredit)
117 binary.BigEndian.PutUint16(sackRaw[8:], uint16(len(s.gapAckBlocks))) //nolint:gosec // G115
118 binary.BigEndian.PutUint16(sackRaw[10:], uint16(len(s.duplicateTSN))) //nolint:gosec // G115
119 offset := selectiveAckHeaderSize
120 for _, g := range s.gapAckBlocks {
121 binary.BigEndian.PutUint16(sackRaw[offset:], g.start)
122 binary.BigEndian.PutUint16(sackRaw[offset+2:], g.end)
123 offset += 4
124 }
125 for _, t := range s.duplicateTSN {
126 binary.BigEndian.PutUint32(sackRaw[offset:], t)
127 offset += 4
128 }
129
130 s.chunkHeader.typ = ctSack
131 s.chunkHeader.raw = sackRaw
132
133 return s.chunkHeader.marshal()
134 }
135
136 func (s *chunkSelectiveAck) check() (abort bool, err error) {
137 return false, nil
138 }
139
140 // String makes chunkSelectiveAck printable.
141 func (s *chunkSelectiveAck) String() string {
142 res := fmt.Sprintf("SACK cumTsnAck=%d arwnd=%d dupTsn=%d",
143 s.cumulativeTSNAck,
144 s.advertisedReceiverWindowCredit,
145 s.duplicateTSN)
146
147 for _, gap := range s.gapAckBlocks {
148 res = fmt.Sprintf("%s\n gap ack: %s", res, gap)
149 }
150
151 return res
152 }