| 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 shall be used by the data sender to inform the data |
| 14 |
// receiver to adjust its cumulative received TSN point forward because |
| 15 |
// some missing TSNs are associated with data chunks that SHOULD NOT be |
| 16 |
// transmitted or retransmitted by the sender. |
| 17 |
// |
| 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 = 192 | Flags = 0x00 | Length = Variable | |
| 22 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 23 |
// | New Cumulative TSN | |
| 24 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 25 |
// | Stream-1 | Stream Sequence-1 | |
| 26 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 |
// \ / |
| 28 |
// / \ |
| 29 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 |
// | Stream-N | Stream Sequence-N | |
| 31 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 32 |
|
| 33 |
type chunkForwardTSN struct { |
| 34 |
chunkHeader |
| 35 |
|
| 36 |
// This indicates the new cumulative TSN to the data receiver. Upon |
| 37 |
// the reception of this value, the data receiver MUST consider |
| 38 |
// any missing TSNs earlier than or equal to this value as received, |
| 39 |
// and stop reporting them as gaps in any subsequent SACKs. |
| 40 |
newCumulativeTSN uint32 |
| 41 |
|
| 42 |
streams []chunkForwardTSNStream |
| 43 |
} |
| 44 |
|
| 45 |
const ( |
| 46 |
newCumulativeTSNLength = 4 |
| 47 |
forwardTSNStreamLength = 4 |
| 48 |
) |
| 49 |
|
| 50 |
// Forward TSN chunk errors. |
| 51 |
var ( |
| 52 |
ErrMarshalStreamFailed = errors.New("failed to marshal stream") |
| 53 |
ErrChunkTooShort = errors.New("chunk too short") |
| 54 |
) |
| 55 |
|
| 56 |
func (c *chunkForwardTSN) unmarshal(raw []byte) error { |
| 57 |
if err := c.chunkHeader.unmarshal(raw); err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
|
| 61 |
if len(c.raw) < newCumulativeTSNLength { |
| 62 |
return ErrChunkTooShort |
| 63 |
} |
| 64 |
|
| 65 |
c.newCumulativeTSN = binary.BigEndian.Uint32(c.raw[0:]) |
| 66 |
|
| 67 |
offset := newCumulativeTSNLength |
| 68 |
remaining := len(c.raw) - offset |
| 69 |
for remaining > 0 { |
| 70 |
s := chunkForwardTSNStream{} |
| 71 |
|
| 72 |
if err := s.unmarshal(c.raw[offset:]); err != nil { |
| 73 |
return fmt.Errorf("%w: %v", ErrMarshalStreamFailed, err) //nolint:errorlint |
| 74 |
} |
| 75 |
|
| 76 |
c.streams = append(c.streams, s) |
| 77 |
|
| 78 |
offset += s.length() |
| 79 |
remaining -= s.length() |
| 80 |
} |
| 81 |
|
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
func (c *chunkForwardTSN) marshal() ([]byte, error) { |
| 86 |
out := make([]byte, newCumulativeTSNLength) |
| 87 |
binary.BigEndian.PutUint32(out[0:], c.newCumulativeTSN) |
| 88 |
|
| 89 |
for _, s := range c.streams { |
| 90 |
b, err := s.marshal() |
| 91 |
if err != nil { |
| 92 |
return nil, fmt.Errorf("%w: %v", ErrMarshalStreamFailed, err) //nolint:errorlint |
| 93 |
} |
| 94 |
out = append(out, b...) //nolint:makezero // TODO: fix |
| 95 |
} |
| 96 |
|
| 97 |
c.typ = ctForwardTSN |
| 98 |
c.raw = out |
| 99 |
|
| 100 |
return c.chunkHeader.marshal() |
| 101 |
} |
| 102 |
|
| 103 |
func (c *chunkForwardTSN) check() (abort bool, err error) { |
| 104 |
return true, nil |
| 105 |
} |
| 106 |
|
| 107 |
// String makes chunkForwardTSN printable. |
| 108 |
func (c *chunkForwardTSN) String() string { |
| 109 |
var res strings.Builder |
| 110 |
fmt.Fprintf(&res, "New Cumulative TSN: %d\n", c.newCumulativeTSN) |
| 111 |
for _, s := range c.streams { |
| 112 |
fmt.Fprintf(&res, " - si=%d, ssn=%d\n", s.identifier, s.sequence) |
| 113 |
} |
| 114 |
|
| 115 |
return res.String() |
| 116 |
} |
| 117 |
|
| 118 |
type chunkForwardTSNStream struct { |
| 119 |
// This field holds a stream number that was skipped by this |
| 120 |
// FWD-TSN. |
| 121 |
identifier uint16 |
| 122 |
|
| 123 |
// This field holds the sequence number associated with the stream |
| 124 |
// that was skipped. The stream sequence field holds the largest |
| 125 |
// stream sequence number in this stream being skipped. The receiver |
| 126 |
// of the FWD-TSN's can use the Stream-N and Stream Sequence-N fields |
| 127 |
// to enable delivery of any stranded TSN's that remain on the stream |
| 128 |
// re-ordering queues. This field MUST NOT report TSN's corresponding |
| 129 |
// to DATA chunks that are marked as unordered. For ordered DATA |
| 130 |
// chunks this field MUST be filled in. |
| 131 |
sequence uint16 |
| 132 |
} |
| 133 |
|
| 134 |
func (s *chunkForwardTSNStream) length() int { |
| 135 |
return forwardTSNStreamLength |
| 136 |
} |
| 137 |
|
| 138 |
func (s *chunkForwardTSNStream) unmarshal(raw []byte) error { |
| 139 |
if len(raw) < forwardTSNStreamLength { |
| 140 |
return ErrChunkTooShort |
| 141 |
} |
| 142 |
s.identifier = binary.BigEndian.Uint16(raw[0:]) |
| 143 |
s.sequence = binary.BigEndian.Uint16(raw[2:]) |
| 144 |
|
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
func (s *chunkForwardTSNStream) marshal() ([]byte, error) { // nolint:unparam |
| 149 |
out := make([]byte, forwardTSNStreamLength) |
| 150 |
|
| 151 |
binary.BigEndian.PutUint16(out[0:], s.identifier) |
| 152 |
binary.BigEndian.PutUint16(out[2:], s.sequence) |
| 153 |
|
| 154 |
return out, nil |
| 155 |
} |