| 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 |
"encoding/hex" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
type paramHeaderUnrecognizedAction byte |
| 14 |
|
| 15 |
type paramHeader struct { |
| 16 |
typ paramType |
| 17 |
unrecognizedAction paramHeaderUnrecognizedAction |
| 18 |
len int |
| 19 |
raw []byte |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
The Parameter Types are encoded such that the highest-order 2 bits specify |
| 24 |
the action that is taken if the processing endpoint does not recognize the |
| 25 |
Parameter Type. |
| 26 |
|
| 27 |
00 - Stop processing this parameter and do not process any further parameters within this chunk. |
| 28 |
|
| 29 |
01 - Stop processing this parameter, do not process any further parameters within this chunk, and |
| 30 |
report the unrecognized parameter, as described in Section 3.2.2. |
| 31 |
|
| 32 |
10 - Skip this parameter and continue processing. |
| 33 |
|
| 34 |
11 - Skip this parameter and continue processing, but report the unrecognized |
| 35 |
parameter, as described in Section 3.2.2. |
| 36 |
|
| 37 |
https://www.rfc-editor.org/rfc/rfc9260.html#section-3.2.1 |
| 38 |
*/ |
| 39 |
|
| 40 |
const ( |
| 41 |
paramHeaderUnrecognizedActionMask = 0b11000000 |
| 42 |
paramHeaderUnrecognizedActionStop paramHeaderUnrecognizedAction = 0b00000000 |
| 43 |
paramHeaderUnrecognizedActionStopAndReport paramHeaderUnrecognizedAction = 0b01000000 |
| 44 |
paramHeaderUnrecognizedActionSkip paramHeaderUnrecognizedAction = 0b10000000 |
| 45 |
paramHeaderUnrecognizedActionSkipAndReport paramHeaderUnrecognizedAction = 0b11000000 |
| 46 |
|
| 47 |
paramHeaderLength = 4 |
| 48 |
) |
| 49 |
|
| 50 |
// Parameter header parse errors. |
| 51 |
var ( |
| 52 |
ErrParamHeaderTooShort = errors.New("param header too short") |
| 53 |
ErrParamHeaderSelfReportedLengthShorter = errors.New("param self reported length is shorter than header length") |
| 54 |
ErrParamHeaderSelfReportedLengthLonger = errors.New("param self reported length is longer than header length") |
| 55 |
ErrParamHeaderParseFailed = errors.New("failed to parse param type") |
| 56 |
) |
| 57 |
|
| 58 |
func (p *paramHeader) marshal() ([]byte, error) { |
| 59 |
paramLengthPlusHeader := paramHeaderLength + len(p.raw) |
| 60 |
|
| 61 |
rawParam := make([]byte, paramLengthPlusHeader) |
| 62 |
binary.BigEndian.PutUint16(rawParam[0:], uint16(p.typ)) |
| 63 |
binary.BigEndian.PutUint16(rawParam[2:], uint16(paramLengthPlusHeader)) //nolint:gosec // G115 |
| 64 |
copy(rawParam[paramHeaderLength:], p.raw) |
| 65 |
|
| 66 |
return rawParam, nil |
| 67 |
} |
| 68 |
|
| 69 |
func (p *paramHeader) unmarshal(raw []byte) error { |
| 70 |
if len(raw) < paramHeaderLength { |
| 71 |
return ErrParamHeaderTooShort |
| 72 |
} |
| 73 |
|
| 74 |
paramLengthPlusHeader := binary.BigEndian.Uint16(raw[2:]) |
| 75 |
if int(paramLengthPlusHeader) < paramHeaderLength { |
| 76 |
return fmt.Errorf( |
| 77 |
"%w: param self reported length (%d) shorter than header length (%d)", |
| 78 |
ErrParamHeaderSelfReportedLengthShorter, int(paramLengthPlusHeader), paramHeaderLength, |
| 79 |
) |
| 80 |
} |
| 81 |
if len(raw) < int(paramLengthPlusHeader) { |
| 82 |
return fmt.Errorf( |
| 83 |
"%w: param length (%d) shorter than its self reported length (%d)", |
| 84 |
ErrParamHeaderSelfReportedLengthLonger, len(raw), int(paramLengthPlusHeader), |
| 85 |
) |
| 86 |
} |
| 87 |
|
| 88 |
typ, err := parseParamType(raw[0:]) |
| 89 |
if err != nil { |
| 90 |
return fmt.Errorf("%w: %v", ErrParamHeaderParseFailed, err) //nolint:errorlint |
| 91 |
} |
| 92 |
p.typ = typ |
| 93 |
p.unrecognizedAction = paramHeaderUnrecognizedAction(raw[0] & paramHeaderUnrecognizedActionMask) |
| 94 |
p.raw = raw[paramHeaderLength:paramLengthPlusHeader] |
| 95 |
p.len = int(paramLengthPlusHeader) |
| 96 |
|
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
func (p *paramHeader) length() int { |
| 101 |
return p.len |
| 102 |
} |
| 103 |
|
| 104 |
// String makes paramHeader printable. |
| 105 |
func (p paramHeader) String() string { |
| 106 |
return fmt.Sprintf("%s (%d): %s", p.typ, p.len, hex.Dump(p.raw)) |
| 107 |
} |