| 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 |
) |
| 10 |
|
| 11 |
// errorCauseHeader represents the shared header that is shared by all error causes. |
| 12 |
type errorCauseHeader struct { |
| 13 |
code errorCauseCode |
| 14 |
len uint16 |
| 15 |
raw []byte |
| 16 |
} |
| 17 |
|
| 18 |
const ( |
| 19 |
errorCauseHeaderLength = 4 |
| 20 |
) |
| 21 |
|
| 22 |
// ErrInvalidSCTPChunk is returned when an SCTP chunk is invalid. |
| 23 |
var ErrInvalidSCTPChunk = errors.New("invalid SCTP chunk") |
| 24 |
|
| 25 |
func (e *errorCauseHeader) marshal() ([]byte, error) { |
| 26 |
e.len = uint16(len(e.raw)) + uint16(errorCauseHeaderLength) //nolint:gosec // G115 |
| 27 |
raw := make([]byte, e.len) |
| 28 |
binary.BigEndian.PutUint16(raw[0:], uint16(e.code)) |
| 29 |
binary.BigEndian.PutUint16(raw[2:], e.len) |
| 30 |
copy(raw[errorCauseHeaderLength:], e.raw) |
| 31 |
|
| 32 |
return raw, nil |
| 33 |
} |
| 34 |
|
| 35 |
func (e *errorCauseHeader) unmarshal(raw []byte) error { |
| 36 |
e.code = errorCauseCode(binary.BigEndian.Uint16(raw[0:])) |
| 37 |
e.len = binary.BigEndian.Uint16(raw[2:]) |
| 38 |
if e.len < errorCauseHeaderLength || int(e.len) > len(raw) { |
| 39 |
return ErrInvalidSCTPChunk |
| 40 |
} |
| 41 |
valueLength := e.len - errorCauseHeaderLength |
| 42 |
e.raw = raw[errorCauseHeaderLength : errorCauseHeaderLength+valueLength] |
| 43 |
|
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func (e *errorCauseHeader) length() uint16 { |
| 48 |
return e.len |
| 49 |
} |
| 50 |
|
| 51 |
func (e *errorCauseHeader) errorCauseCode() errorCauseCode { |
| 52 |
return e.code |
| 53 |
} |
| 54 |
|
| 55 |
// String makes errorCauseHeader printable. |
| 56 |
func (e errorCauseHeader) String() string { |
| 57 |
return e.code.String() |
| 58 |
} |