Debian Codegraph @

chunk_i_forward_tsn.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 "strings"
11 )
12
13 // This chunk be used by the data sender to inform the data
14 // receiver to adjust its cumulative received TSN.
15 //
16 // 0 1 2 3
17 // 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
18 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19 // | Type = 194 | Flags = 0x00 | Length = Variable |
20 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 // | New Cumulative TSN |
22 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 // | Stream Identifier | Reserved |U| |
24 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25 // | Message Identifier |
26 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27 // \ /
28 // / \
29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 // | Stream Identifier | Reserved |U| |
31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 // | Message Identifier |
33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 // https://www.rfc-editor.org/rfc/rfc8260.html#section-2.3.1
35
36 type chunkIForwardTSN struct {
37 chunkHeader
38
39 // This indicates the new cumulative TSN to the data receiver.
40 newCumulativeTSN uint32
41
42 streams []chunkIForwardTSNStream
43 }
44
45 const (
46 iForwardTSNEntryLength = 8
47 maxIForwardTSNStreams = ((1 << 16) - 1 - chunkHeaderSize - newCumulativeTSNLength) / iForwardTSNEntryLength
48 )
49
50 // I-FORWARD-TSN chunk errors.
51 var (
52 errIForwardTSNChunkTooShort = errors.New("i-forward-tsn chunk too short")
53 errIForwardTSNChunkInvalidLength = errors.New("i-forward-tsn chunk invalid length")
54 errIForwardTSNTooManyStreams = errors.New("i-forward-tsn chunk contains too many streams")
55 )
56
57 type iForwardTSNStreamKey struct {
58 identifier uint16
59 unordered bool
60 }
61
62 func (c *chunkIForwardTSN) unmarshal(raw []byte) error {
63 if err := c.chunkHeader.unmarshal(raw); err != nil {
64 return err
65 }
66
67 if len(c.raw) < newCumulativeTSNLength {
68 return errIForwardTSNChunkTooShort
69 }
70 c.streams = nil
71
72 c.newCumulativeTSN = binary.BigEndian.Uint32(c.raw[0:])
73
74 streamBytes := len(c.raw) - newCumulativeTSNLength
75 if streamBytes%iForwardTSNEntryLength != 0 {
76 return fmt.Errorf("%w: %w", ErrMarshalStreamFailed, errIForwardTSNChunkInvalidLength)
77 }
78
79 streamCount := streamBytes / iForwardTSNEntryLength
80 if streamCount > maxIForwardTSNStreams {
81 return fmt.Errorf("%w: %d exceeds %d", errIForwardTSNTooManyStreams, streamCount, maxIForwardTSNStreams)
82 }
83
84 streams := make([]chunkIForwardTSNStream, 0, streamCount)
85 for i := range streamCount {
86 offset := newCumulativeTSNLength + i*iForwardTSNEntryLength
87 s := chunkIForwardTSNStream{}
88 if err := s.unmarshal(c.raw[offset : offset+iForwardTSNEntryLength]); err != nil {
89 return fmt.Errorf("%w: %w", ErrMarshalStreamFailed, err)
90 }
91
92 streams = append(streams, s)
93 }
94 c.streams = normalizeIForwardTSNStreams(streams)
95
96 return nil
97 }
98
99 func (c *chunkIForwardTSN) marshal() ([]byte, error) {
100 c.streams = normalizeIForwardTSNStreams(c.streams)
101 if _, err := c.check(); err != nil {
102 return nil, err
103 }
104
105 out := make([]byte, newCumulativeTSNLength+len(c.streams)*iForwardTSNEntryLength)
106 binary.BigEndian.PutUint32(out[0:], c.newCumulativeTSN)
107
108 offset := newCumulativeTSNLength
109 for _, s := range c.streams {
110 b, err := s.marshal()
111 if err != nil {
112 return nil, fmt.Errorf("%w: %v", ErrMarshalStreamFailed, err) //nolint:errorlint
113 }
114
115 copy(out[offset:], b)
116 offset += len(b)
117 }
118
119 c.typ = ctIForwardTSN
120 c.raw = out
121
122 return c.chunkHeader.marshal()
123 }
124
125 func (c *chunkIForwardTSN) check() (abort bool, err error) {
126 c.streams = normalizeIForwardTSNStreams(c.streams)
127
128 if len(c.streams) > maxIForwardTSNStreams {
129 return true, fmt.Errorf("%w: %d exceeds %d", errIForwardTSNTooManyStreams, len(c.streams), maxIForwardTSNStreams)
130 }
131
132 return false, nil
133 }
134
135 func normalizeIForwardTSNStreams(streams []chunkIForwardTSNStream) []chunkIForwardTSNStream {
136 if len(streams) < 2 {
137 return streams
138 }
139
140 normalized := make([]chunkIForwardTSNStream, 0, len(streams))
141 forwarded := make(map[iForwardTSNStreamKey]int, len(streams))
142 for _, stream := range streams {
143 key := iForwardTSNStreamKey{
144 identifier: stream.identifier,
145 unordered: stream.unordered,
146 }
147 if idx, ok := forwarded[key]; ok {
148 if sna32LT(normalized[idx].messageIdentifier, stream.messageIdentifier) {
149 normalized[idx].messageIdentifier = stream.messageIdentifier
150 }
151
152 continue
153 }
154
155 forwarded[key] = len(normalized)
156 normalized = append(normalized, stream)
157 }
158
159 return normalized
160 }
161
162 // String makes chunkIForwardTSN printable.
163 func (c *chunkIForwardTSN) String() string {
164 var res strings.Builder
165 fmt.Fprintf(&res, "New Cumulative TSN: %d\n", c.newCumulativeTSN)
166 for _, s := range c.streams {
167 fmt.Fprintf(&res, " - si=%d mid=%d unordered=%v\n", s.identifier, s.messageIdentifier, s.unordered)
168 }
169
170 return res.String()
171 }
172
173 type chunkIForwardTSNStream struct {
174 identifier uint16
175 unordered bool
176 messageIdentifier uint32
177 }
178
179 func (s *chunkIForwardTSNStream) length() int {
180 return iForwardTSNEntryLength
181 }
182
183 func (s *chunkIForwardTSNStream) unmarshal(raw []byte) error {
184 if len(raw) < iForwardTSNEntryLength {
185 return errIForwardTSNChunkTooShort
186 }
187 s.identifier = binary.BigEndian.Uint16(raw[0:])
188 flags := binary.BigEndian.Uint16(raw[2:])
189 s.unordered = flags&0x1 != 0
190 s.messageIdentifier = binary.BigEndian.Uint32(raw[4:])
191
192 return nil
193 }
194
195 func (s *chunkIForwardTSNStream) marshal() ([]byte, error) { // nolint:unparam
196 out := make([]byte, iForwardTSNEntryLength)
197
198 binary.BigEndian.PutUint16(out[0:], s.identifier)
199 if s.unordered {
200 binary.BigEndian.PutUint16(out[2:], 0x1)
201 } else {
202 binary.BigEndian.PutUint16(out[2:], 0x0)
203 }
204 binary.BigEndian.PutUint32(out[4:], s.messageIdentifier)
205
206 return out, nil
207 }